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


local Name = "Blue Marble and Nighttime Lights"
local Identifier = "noaa-sos-land-bluemarble_nightlights"
local Description = [[This dataset shows the Earth with daytime and nighttime views of 
the Earth, to demonstrate how only half of the Earth is illumintated at one time. The 
Blue Marble is an incredibly detailed, true-color depiction of the Earth, and is shown on 
the daylight side of the Earth in this dataset. NASA is responsible for The Blue Marble, 
made from a compilation of satellite images throughout 2001. Most of the information came 
from NASA's MODIS, the Moderate Resolution Imaging Spectroradiometer, which is attached 
to the Terra satellite 435 miles above Earth. The background image of the land and oceans 
was created using data from June through September of 2001. This could not be done in a 
single day or even a week because on any given day clouds are blocking a significant 
portion of the surface. The cloud image is a composite of three days worth of data. The 
first two days of data were collected in the visible wavelength and the third day was 
needed to get a view of the clouds over the poles using thermal infrared imagery]]
local URL = "https://sos.noaa.gov/catalog/datasets/blue-marble-and-nighttime-lights/"


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
}

asset.onInitialize(function()
  if not openspace.directoryExists(imagesDestination) then
    openspace.printInfo("Extracting " .. Name)
    openspace.unzipFile(syncedDirectory .. "4096-1.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "4096-2.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "4096-3.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "4096-4.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "4096-5.zip", imagesDestination, true)
    openspace.unzipFile(syncedDirectory .. "4096-6.zip", imagesDestination, true)
  end

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

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

asset.export(layer)


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