CEMS-Flood data comes primarily in GRIB2 format.
To read GRIB files we encourage using Python 'xarray' and 'cfgrib' packages.
This guideline provides instructions about how to install required libraries (assuming you are working on a Linux OS) and documents the datasets' specific configurations that must be set when reading GRIBs.
Table of Contents |
---|
Set up Python environment, tools and libraries installation
First of all install Conda, a Python packages and environments manager.
Then open a terminal and type:
Code Block | ||||
---|---|---|---|---|
| ||||
# create a local virtual environment, you can call it as you wish, here 'myenv' is used. conda create -n myenv python=3.8 # add repository channel conda config --add channels conda-forge # activate the local environment. conda activate myenv # install the required packages conda install -c conda-forge/label/main xarray cfgrib eccodes netcdf4 # make sure you have installed eccodes version >= 2.23.0 python -c "import eccodes; print(eccodes.__version__)" |
Provided that you have downloaded an EFAS or GloFAS GRIB file from CDS, start a python console (it is important that you have activated the local environment) and type:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
In [1]: import xarray as xr In [2]: ds = xr.open_dataset('download.grib',engine='cfgrib') In [3]: ds Out[4]: <xarray.Dataset> Dimensions: (latitude: 1500, longitude: 3600, step: 3, time: 3) Coordinates: number int64 ... * time (time) datetime64[ns] 2019-12-01 2019-12-02 2019-12-03 * step (step) timedelta64[ns] 1 days 2 days 3 days surface int64 ... * latitude (latitude) float64 89.95 89.85 89.75 ... -59.75 -59.85 -59.95 * longitude (longitude) float64 -179.9 -179.8 -179.8 ... 179.7 179.8 179.9 valid_time (time, step) datetime64[ns] ... Data variables: dis24 (time, step, latitude, longitude) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: 2021-02-11T11:00:21 GRIB to CDM+CF via cfgrib-0.... |
Dataset's specific cfgrib configurations
The different GRIB data structures of the EFAS and GloFAS datasets may require some additional configurations to be set in the backend_kwargs argument of the xarray.open_dataset function.
Read GRIB historical datasets:
CEMS-Floods offers two historical datasets: GloFAS and EFAS historical.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import xarray as xr # GloFAS historical ds = xr.open_dataset("glofas_historical_201901.grib",engine="cfgrib",backend_kwargs={'time_dims':['time']}) |
Read GRIB file that has multiple product types:
There are 5 datasets that can have more that one product type in a GRIB file, depending whether you decide to request more than one product type in a single request. These datasets and corresponding product types are:
- EFAS forecast: "control reforecast", "ensemble perturbed forecasts", "high resolution forecast"
- EFAS reforecast: "control reforecast", "ensemble perturbed reforecasts"
- GloFAS historical: "consolidated", "intermediate"
- GloFAS forecast: "control reforecast", "ensemble perturbed forecasts"
- GloFAS reforecast: "control reforecast", "ensemble perturbed reforecasts"
In order to read them you need to specify which product type you are reading using the backend_kwargs:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import xarray as xr # Reading the Control reforecast (cf) data glofas_cf = xr.open_dataset("Glofas_forecast.grib", engine='cfgrib', backend_kwargs={'filter_by_keys': {'dataType': 'cf'}, 'indexpath':''}) # Reading the Ensemble perturbed reforecasts (pf) data glofas_pf = xr.open_dataset("Glofas_forecast.grib ", engine='cfgrib', backend_kwargs={'filter_by_keys': {'dataType': 'pf'}, 'indexpath':''}) # Reading the Historical Consolidated (0001) data consololidated = xr.open_dataset('glofas.grib', engine='cfgrib', backend_kwargs={'read_keys': {'experimentVersionNumber':'0001'}}) # Reading the Historical Intermediate (0005) data intermediate = xr.open_dataset('glofas.grib', engine='cfgrib', backend_kwargs={'read_keys': {'experimentVersionNumber':'0005'}}) |