import atlite

"""
Explanation: Creating a Cutout with ERA5
In this example we download ERA5 data on-demand for a cutout we want to create.
(Atlite does also work with other datasources, but ERA5 is the easiest one to get started.)
This only works if you have in before

Installed the Copernicus Climate Data Store cdsapi package
Registered and setup your CDS API key as described on their website here

Import the package first:
End of explanation
"""


import logging
logging.basicConfig(level=logging.INFO)

"""
Explanation: We implement notifications in atlite using loggers from the logging library.
We recommend you always launch a logger to get information on what is going on.
For debugging, you can use the more verbose level=logging.DEBUG:
End of explanation
"""


cutout = atlite.Cutout(path="western-europe-2011-01.nc",
                       module="era5",
                       x=slice(-13.6913, 1.7712),
                       y=slice(49.9096, 60.8479),
                       time="2011-01"
                       )

"""
Explanation: Defining the Cutout extent

This will not yet trigger any major operations.

A cutout is the basis for any of your work and calculations.
The cutout is created in the directory and file specified by the relative path
If a cutout at the given location already exists, then this command will simply load the cutout again.
If the cutout does not yet exist, it will specify the new to-be-created cutout.
End of explanation
"""


cutout.prepare()

"""
Explanation: For creating the cutout, you need to specify

The dataset to create the cutout with (era5)
The time period it covers
The longitude x and latitude y it stretches

Here we went with the ERA5 dataset from ECMWF
module="era5"
Here we decided to provide the time period of the cutout as a string, because it is only a month.
You could have also specify it as a time range
slice("2011-01","2011-01")
The regional bounds (space the cutout stretches) where specified by the
x=slice(-13.6913, 1.7712) # Longitude
y=slice(49.9096, 60.8479) # Latitude
and describe a rectangle's edges.
In this case we drew a rectangle containing some parts of the atlantic ocean,
the Republic of Ireland and the UK.
Preparing the Cutout
If the cutout does not yet exist or has some features which are not yet included, we have to tell atlite to go ahead and do so.
No matter which dataset you use, this is where all the work actually happens.
This can be fast or take some or a lot of time and resources, among others depending on
your computer ressources and (for downloading e.g. ERA5 data) your internet connection.
End of explanation
"""


cutout.data

"""
Explanation: The cutout.prepare() function takes a list of features which should be prepared. When this is not specified, all available features are build. 
After, the execution all downloaded data is stored at cutout.path. Per default it is not loaded into memory, but into dask arrays. This keeps the memory consumption extremely low.  
The data is accessible in cutout.data which is an xarray.Dataset. 
Querying the cutout gives us some basic information on which data is contained
in it.
End of explanation
"""


cutout.prepared_features

"""
Explanation: We can again breakdown which data array belongs to which feature.
End of explanation
"""


