ecmwf / cfgrib

A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes
Apache License 2.0
400 stars 77 forks source link

Overwriting Variables? #253

Closed jqs-noaa closed 3 years ago

jqs-noaa commented 3 years ago

I'm seeing an problem where I have two variables defined using custom definitions for the eccodes library. When reading in this file with cfgrib, I only get a dataset with one variable instead of 2. For some reason, cfgrib is only returning the first variable it sees. What am I missing?

Example project: test.zip

First I set my ECCODE_DEFINITION_PATH export ECCODES_DEFINITION_PATH=custom_definitions:/usr/share/eccodes/definitions

grib_ls see's both variables:

grib_ls test.grib2
test.grib2
edition      centre       date         dataType     gridType     stepRange    typeOfLevel  level        shortName    packingType  
2            kwbc         20210922     fc           lambert      6            hybrid       16           cimixr       grid_complex_spatial_differencing 
2            kwbc         20210922     fc           lambert      6            hybrid       16           massden      grid_complex_spatial_differencing 
2 of 2 messages in test.grib2

2 of 2 total messages in 1 files

Example Code:

import cfgrib

datasets = cfgrib.open_datasets("test.grib2")

for d in datasets:
    print (d)

Results:

<xarray.Dataset>
Dimensions:     (x: 1799, y: 1059)
Coordinates:
    time        datetime64[ns] 2021-09-22
    step        timedelta64[ns] 06:00:00
    hybrid      float64 16.0
    latitude    (y, x) float64 ...
    longitude   (y, x) float64 ...
    valid_time  datetime64[ns] ...
Dimensions without coordinates: x, y
Data variables:
    cimixr      (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP 
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP 

I expected to see a dataset with both 'cimixr' and 'massden' variables

cfgrib version:

pip list | grep cfgrib
cfgrib            0.9.9.0

Other Details: OS: PopOS 20.04 Env: Conda 4.10.3

iainrussell commented 3 years ago

Hi @jqs-noaa , this is happening because cfgrib uses the paramId key to distinguish between parameters, and this is not set in your custom tables and is therefore zero for both fields. You can check out the ecCodes FAQ for how to add a paramId to your data: https://confluence.ecmwf.int/display/UDOC/Creating+your+own+local+definitions+-+ecCodes+GRIB+FAQ I could get your fields into a single dataset by creating a paramId.def file with these contents (I chose random paramId numbers for testing):

# cimixr
'239' = {
         discipline = 0 ;
         parameterCategory = 1 ;
         parameterNumber = 82 ;
}
# massden
'240' = {
         discipline = 0 ;
         parameterCategory = 20 ;
         parameterNumber = 0 ;
}

It's actually the first time I've personally created one of these files, so I hope I did it right, but it works for me nonetheless :)

I hope this helps!

Best regards, Iain

jqs-noaa commented 3 years ago

That works! Thank you.

iainrussell commented 3 years ago

Excellent - thanks for letting me know!