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


local Name = "Ocean Temperature at Depth - Seasonal"
local Identifier = "noaa-sos-oceans-ocean_depths_temp"
local Description = [[Though satellites provide highly detailed analyses of the 
temperature of the ocean surface, they cannot measure temperatures below the first 1 mm 
of water. For that deeper understanding, NOAA relies on thousands of buoys, ships, 
undersea gliders and other devices to measure temperatures at depth. The measurements are 
consolidated every few years by the National Oceanographic Data Center into a product 
known as the World Ocean Atlas. The measurements shown here go as deep as 5,000 m — far 
deeper than many places in the ocean — which is why there is a lack of data (black areas) 
in some of the deeper imagery]]
local URL = "https://sos.noaa.gov/catalog/datasets/ocean-temperature-at-depth-seasonal/"


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

local imagesDestination = syncedDirectory .. "images"
local imagesByDepthDestination = syncedDirectory .. "bydepth"

local layer = {
  Identifier = Identifier,
  Name = Name,
  Enabled = asset.enabled,
  Type = "ImageSequenceTileLayer",
  FolderPath = imagesDestination,
  Description = Description
}

local layerByDepth = {
  Identifier = Identifier .. "-bydepth",
  Name = Name .. " (by Depth)",
  Enabled = asset.enabled,
  Type = "ImageSequenceTileLayer",
  FolderPath = imagesByDepthDestination,
  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 .. "4096.zip", imagesDestination, true)
  end

  if not openspace.directoryExists(imagesByDepthDestination) then
    openspace.printInfo("Extracting " .. Name)
    openspace.unzipFile(syncedDirectory .. "4096_by_depth.zip", imagesByDepthDestination, true)
  end

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

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

asset.export(layer)
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/"
}
