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


local Name = "Surface Temperature"
local Identifier = "noaa-sos-land-surface_temperature"
local Description = [[These maps combine high-resolution daytime surface temperatures 
derived from satellite observations for both land and sea and display them with a common 
color scale. Displaying these high-resolution data with a common color scale makes 
evident the large difference between surface heating on land versus the sea. It also 
shows the relatively small seasonal variation in sea surface temperature over the globe 
relative to land temperatures. The larger seasonal variation in average temperature of 
the northern hemisphere relative to the southern hemisphere, a result of the larger land 
area in the northern hemisphere, is also evident]]
local URL = "https://sos.noaa.gov/catalog/datasets/surface-temperature/"


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",
  Enabled = false,
  FaceCamera = false,
  Scale = 0.07,
  CartesianPosition = { 1.0, 0.0, -2.0 }
}

local colorbar2 = {
  Identifier = Identifier .. "-1",
  Name = Name .. " (1)",
  Enabled = asset.enabled,
  Type = "ScreenSpaceImageLocal",
  TexturePath = syncedDirectory .. "colorbar2.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

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

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

asset.export(layer)
asset.export(colorbar)
asset.export(colorbar2)


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/"
}
