Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Those with root access, or that have already configured a working python environment can go tho the next session.

To fully cover the topic of installing virtual environments, users can look at this tutorial Using virtual Python environment.

...


Or in case of root install sudo pip install cdsapi


Installing the API using conda


Code Block
/<install_path>/conda/anaconda2/bin/conda config --add channels conda-forge
/<install_path>/conda/anaconda2/bin/conda install cdsapi


For non linux/mac users

The point is to download and install your favorite python version then install the command pip, following common instructions, set up the command line environment to point to python and pip and then run pip install cdsapi.

...

The change involves only the code line c = cdsapi.Client()

It must may be changed in:

Background Color
color#ddeedd
c = cdsapi.Client(api_key=[your key as specified in the api-how-to], end_pointurl=''https://cds.climate.copernicus.eu/api/v2")

This method for providing the credentials may be considered bad practice, because a user will put in plane text his credentials.

A more safe way usable also in all platforms is to set up 2 environmental variables with the required informations:

Code Block
languagebash
CDSAPI_URL
CDSAPI_KEY




More hands-on example:

Code Block
languagepy
#!/usr/bin/env python
import cdsapi
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

p_levels = [str(z)  for z in ([1] + list(range(50, 1050, 50)))]
c = cdsapi.Client()


def days_of_month(y, m):
    d0 = datetime(y, m, 1)
    d1 = datetime(y, m + 1, d0 + relativedelta(months=1)
    out = list()
    while d0 < d1:
        out.append(d0.strftime('%Y-%m-%d'))
        d0 += timedelta(days=1)
    return out

for y in range(2008, 2018):
    for m in range(1,13):
        for d in days_of_month(y, m):
            c.retrieve("reanalysis-era5-pressure-levels",
                       {
                           "variable": "temperature",
                           "pressure_level": p_levels,
                           "product_type": "reanalysis",
                           "date": d,
                           "time":[
                               '00:00','01:00','02:00',
                               '03:00','04:00','05:00',
                               '06:00','07:00','08:00',
                               '09:00','10:00','11:00',
                               '12:00','13:00','14:00',
                               '15:00','16:00','17:00',
                               '18:00','19:00','20:00',
                               '21:00','22:00','23:00'
                           ],
                           "format": "netcdf"
                       },
                       "ea_t_{day}.nc".format(day=d)
                       )

...