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


local Name = "Day/Night Terminator (daily)"
local Identifier = "noaa-sos-land-day_night-06z_only"
local Description = [[The line that separates day and night is called the terminator. It 
is also referred to as the "grey line" and the "twilight zone." It is a fuzzy line due to 
our atmosphere bending sunlight. In fact, the atmosphere bends sunlight by half a degree, 
which is about 37 miles (60 km). It is commonly thought that while half of the Earth is 
covered in darkness, the other half is covered in sunlight. This is actually not true 
because of the bending of the sunlight results in the land covered by sunlight having 
greater area than the land covered by darkness]]
local URL = "https://sos.noaa.gov/catalog/datasets/daynight-terminator-daily/"


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 = "TemporalTileLayer",
  Mode = "Folder",
  Folder = {
    Folder = imagesDestination,
    -- See https://en.cppreference.com/w/cpp/io/manip/get_time for an explanation of the
    -- time formatting string
    Format = "daynite_%Y%j%H%M.jpg"
  },
  Description = Description
}

asset.onInitialize(function()
  if not openspace.directoryExists(imagesDestination) then
    openspace.printInfo("Extracting " .. Name)
    openspace.unzipFile(syncedDirectory .. "2048.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/"
}
