local globeIdentifier = asset.require("../../earth").Earth.Identifier


local Name = "Ocean Acidification: Saturation State"
local Identifier = "noaa-sos-oceans-ocean_acid-saturation"
local Description = [[Ocean acidification is an often overlooked consequence of 
humankind's release of carbon dioxide emissions into the atmosphere from fossil fuel 
burning. Excess carbon dioxide enters the ocean and reacts with water to form carbonic 
acid, which decreases ocean pH (i.e., makes seawater less basic), and lowers carbonate 
ion concentrations. Organisms such as corals, clams, oysters, and some plankton use 
carbonate ions to create their shells and skeletons. Decreases in carbonate ion 
concentration will make it difficult for these creatures to form hard structures, 
particularly for juveniles. Ocean acidification may cause some organisms to die, 
reproduce less successfully, or leave an area. Other organisms such as seagrass and some 
plankton species may do better in oceans affected by ocean acidification because they use 
carbon dioxide to photosynthesize, but do not require carbonate ions to survive. Ocean 
ecosystem diversity and ecosystem services may therefore change dramatically from ocean 
acidification]]
local URL = "https://sos.noaa.gov/catalog/datasets/ocean-acidification-saturation-state/"


local syncedDirectory = asset.syncedResource({
  Name = Name,
  Type = "HttpSynchronization",
  Identifier = Identifier,
  Version = 1
})

local imagesDestination = syncedDirectory .. "images"

local layer = {
  Identifier = Identifier,
  Name = Name,
  Enabled = asset.enabled,
  Type = "TemporalTileLayer",
  Mode = "Folder",
  Folder = {
    Folder = imagesDestination,
    -- See https://en.cppreference.com/w/cpp/io/manip/get_time for an explanation of the
    -- time formatting string
    Format = "ARAG.yearly.%Y.color.png"
  },
  Description = Description
}

local reefs = {
  Identifier = Identifier .. "-reefs",
  Name = Name .. " (Reefs)",
  Enabled = asset.enabled,
  FilePath = syncedDirectory .. "reefs.png",
  Description = Description,
}

local colorbar = {
  Identifier = Identifier,
  Name = Name,
  Enabled = asset.enabled,
  Type = "ScreenSpaceImageLocal",
  TexturePath = syncedDirectory .. "colorbar.png",
  FaceCamera = false,
  CartesianPosition = { 0.0, -0.5, -2.0 }
}

asset.onInitialize(function()
  if not openspace.directoryExists(imagesDestination) then
    openspace.printInfo("Extracting " .. Name)
    openspace.unzipFile(syncedDirectory .. "images.zip", imagesDestination, true)
  end

  openspace.globebrowsing.addLayer(globeIdentifier, "ColorLayers", layer)
  openspace.globebrowsing.addLayer(globeIdentifier, "ColorLayers", reefs)
  openspace.addScreenSpaceRenderable(colorbar);  
end)

asset.onDeinitialize(function()
  openspace.globebrowsing.deleteLayer(globeIdentifier, "ColorLayers", layer)
  openspace.globebrowsing.deleteLayer(globeIdentifier, "ColorLayers", reefs)
  openspace.removeScreenSpaceRenderable(colorbar)
end)

asset.export(layer)
asset.export(reefs)
asset.export(colorbar)


asset.meta = {
  Name = Name,
  Version = "1.0",
  Description = Description .. "Data provided by the National Oceanic and Atmospheric Administration",
  Author = "National Oceanic and Atmospheric Administration",
  URL = URL,
  License = "https://sos.noaa.gov/copyright/"
}
