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


local Name = "Sea Surface Height Anomaly"
local Identifier = "noaa-sos-oceans-sea_surface_height_anomaly"
local Description = [[To gather long-term information about the world's oceans and 
currents, satellite instruments make extremely precise measurements of the height of the 
ocean surface above the center of the Earth, a measurement commonly called sea level. 
These data are combined to reveal the ocean surface topography (not to be confused with 
bathymetry, which is the relief on the bottom of the ocean). Sea surface height is of 
interest to scientists because it reveals information about how much heat is stored in 
the ocean. Warm water is less dense than cold water, so higher areas tend to be warmer 
than lower areas. Radar altimeter instruments onboard spacecraft have been continuously 
collecting sea surface height data over the global ocean for almost two decades]]
local URL = "https://sos.noaa.gov/catalog/datasets/sea-surface-height-anomaly/"


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 = "ImageSequenceTileLayer",
  FolderPath = imagesDestination,
  Description = Description
}

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

asset.onInitialize(function()
  if not openspace.directoryExists(imagesDestination) then
    openspace.printInfo("Extracting " .. Name)
    openspace.unzipFile(syncedDirectory .. "8192-1.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "8192-2.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "8192-3.zip", imagesDestination, 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/"
}
