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


local Name = "Koppen-Geiger Climate Changes - 1901 - 2100"
local Identifier = "noaa-sos-land-koppen_climate-koppen_1901_2100"
local Description = [[Köppen climate classification is a widely used vegetation-based 
empirical climate classification system developed by German botanist-climatologist 
Wladimir Köppen. It's based on the idea that climate is best defined by native 
vegetation. The formulas used in the classification correspond to those of the vegetation 
zones (biomes) that were being mapped for the first time in the late 19th century. It was 
first published in 1884 and was revised until 1940 with collaboration by German 
climatologist Rudolf Geiger]]
local URL = "https://sos.noaa.gov/catalog/datasets/koppen-geiger-climate-changes-1901-2100/"


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 legend = {
  Identifier = Identifier,
  Name = Name,
  Enabled = asset.enabled,
  Type = "ScreenSpaceImageLocal",
  TexturePath = syncedDirectory .. "legend.jpg",
  FaceCamera = false,
  Scale = 0.375,
  CartesianPosition = { -0.75, -0.5, -2.0 }
}

local legend1 = {
  Identifier = Identifier .. "-1",
  Name = Name .. " (1)",
  Enabled = asset.enabled,
  Type = "ScreenSpaceImageLocal",
  TexturePath = syncedDirectory .. "legend2.png",
  FaceCamera = false,
  CartesianPosition = { 0.75, -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(legend);  
  openspace.addScreenSpaceRenderable(legend1);  
end)

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

asset.export(layer)
asset.export(legend)
asset.export(legend1)


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