import atlite

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

cutout = atlite.Cutout(path="western-europe-2011-01.nc",
                       module=["sarah", "era5"],
                       sarah_dir="/home/vres-climate/data/sarah_v2",
                       x=slice(-13.6913, 1.7712),
                       y=slice(49.9096, 60.8479),
                       time="2013-01",
                       chunks={'time': 100}
                       )

"""
Explanation: Creating a Cutout with the SARAH-2 dataset
This walkthrough describes the process of creating a cutout using the SARAH-2 dataset by EUMETSAT.
The SARAH-2 dataset contains extensive information on solar radiation variables, like surface incoming direct radiation (SID) or surface incoming shortwave radiation (SIS).
It serves as an addition to the ERA5 dataset and as such requires the cdsapi to be setup properly.

Recommendation
This is a reduced version for cutout creation. Creating cutouts with ERA-5 is simpler and explained in more details.
We therefore recommend you have a look at this example first.
Note:
For creating a cutout from this dataset, you need to download large files and your computers memory needs to be able to handle these as well.

Downloading the data set
To download the dataset, head to the EUMETSTATs website (the link points to the current 2.1 edition)
https://wui.cmsaf.eu/safira/action/viewDoiDetails?acronym=SARAH_V002_01 
On the bottom, select the products you want to include in the cutout, i.e. for us:
| variable | time span | time resolution | 
| --- | --- | --- |
| Surface incoming direct radiation (SID) | 2013 | Instantaneous |
| Surface incoming shortwave radiation (SIS) | 2013 | Instantaneous |

Add each product to your cart and register with the website.
Follow the instructions to activate your account, confirm your order and wait for the download to be ready.
You will be notified by email with the download instructions.
Download the ordered files of your order into a directory, e.g. sarah-2.
Extract the tar files (e.g. for linux systems tar -xvf * or with 7zip for windows) into the same folder

You are now ready to create cutouts using the SARAH-2 dataset.
Specifying the cutout
Import the package and set recommended logging settings:
End of explanation
"""


cutout.available_features.to_frame()

"""
Explanation: Let's see what the available features that is the available weather data variables are.
End of explanation
"""


cutout.prepare()

"""
Explanation: Preparing the Cutout
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 (especially memory for SARAH-2).
End of explanation
"""


cutout # basic information

cutout.data.attrs # cutout meta data

cutout.prepared_features # included weather variables

cutout.data # access to underlying xarray data

"""
Explanation: Querying the cutout gives us basic information on which data is contained and can already be used.
Inspecting the Cutout
End of explanation
"""


