cfgrib

How do I extract information by pressure level?


I am a complete novice with cfgrib, so please excuse what is probably a straight forward question..

I have a heterogeneous grib file that represents weather information at points on a grid and at atmospheric pressure levels.

For the life of me I cannot work out how to extract the information I need in a usable way.

I want to get, for example, the temperature information (Data variable=t), for each point in the grid at each of the available pressure levels. Or, if that isnt possible get the temperatures for each lat,lon for a specific pressure level.

My grib file looks like this:

Dimensions:        (isobaricInhPa: 17, latitude: 145, longitude: 288)
Coordinates:
    time           datetime64[ns] 2020-10-16T12:00:00
    step           timedelta64[ns] 06:00:00
  * isobaricInhPa  (isobaricInhPa) int64 850 750 700 600 500 ... 175 150 125 100
  * latitude       (latitude) float64 -90.0 -88.75 -87.5 ... 87.5 88.75 90.0
  * longitude      (longitude) float64 0.0 1.25 2.5 3.75 ... 356.2 357.5 358.8
    valid_time     datetime64[ns] 2020-10-16T18:00:00
Data variables:
    t              (isobaricInhPa, latitude, longitude) float32 ...
    u              (isobaricInhPa, latitude, longitude) float32 ...
    v              (isobaricInhPa, latitude, longitude) float32 ...
    gh             (isobaricInhPa, latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             egrr
    GRIB_centreDescription:  U.K. Met Office - Exeter
    GRIB_subCentre:          5
    Conventions:             CF-1.7
    institution:             U.K. Met Office - Exeter, <xarray.Dataset>
Dimensions:        (isobaricInhPa: 5, latitude: 145, longitude: 288)
Coordinates:
    time           datetime64[ns] 2020-10-16T12:00:00
    step           timedelta64[ns] 06:00:00
  * isobaricInhPa  (isobaricInhPa) int64 850 750 700 600 500
  * latitude       (latitude) float64 -90.0 -88.75 -87.5 ... 87.5 88.75 90.0
  * longitude      (longitude) float64 0.0 1.25 2.5 3.75 ... 356.2 357.5 358.8
    valid_time     datetime64[ns] ...
Data variables:
    r              (isobaricInhPa, latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             egrr
    GRIB_centreDescription:  U.K. Met Office - Exeter
    GRIB_subCentre:          5
    Conventions:             CF-1.7
    institution:             U.K. Met Office - Exeter, <xarray.Dataset>
Dimensions:     (latitude: 145, longitude: 288)
Coordinates:
    time        datetime64[ns] 2020-10-16T12:00:00
    step        timedelta64[ns] 06:00:00
    maxWind     int64 0
  * latitude    (latitude) float64 -90.0 -88.75 -87.5 -86.25 ... 87.5 88.75 90.0
  * longitude   (longitude) float64 0.0 1.25 2.5 3.75 ... 356.2 357.5 358.8
    valid_time  datetime64[ns] 2020-10-16T18:00:00
Data variables:
    u           (latitude, longitude) float32 ...
    v           (latitude, longitude) float32 ...
    icaht       (latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             egrr
    GRIB_centreDescription:  U.K. Met Office - Exeter
    GRIB_subCentre:          5
    Conventions:             CF-1.7
    institution:             U.K. Met Office - Exeter, <xarray.Dataset>
Dimensions:     (latitude: 145, longitude: 288)
Coordinates:
    time        datetime64[ns] 2020-10-16T12:00:00
    step        timedelta64[ns] 06:00:00
    tropopause  int64 0
  * latitude    (latitude) float64 -90.0 -88.75 -87.5 -86.25 ... 87.5 88.75 90.0
  * longitude   (longitude) float64 0.0 1.25 2.5 3.75 ... 356.2 357.5 358.8
    valid_time  datetime64[ns] 2020-10-16T18:00:00
Data variables:
    t           (latitude, longitude) float32 ...
    icaht       (latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             egrr
    GRIB_centreDescription:  U.K. Met Office - Exeter
    GRIB_subCentre:          5
    Conventions:             CF-1.7
    institution:             U.K. Met Office - Exeter]

Any help greatly appreciated!


Solution

  • This works for me using NWS NCEP grib2 files and should work with U.K. Met Office grib files. It also only grabs the 't' at a specific pressure level.

    Use cfgrib.open_datasets() to open the grib file. (I'm using 'data.grib' as an example file name here and using your output of the grib file to get variables):

    import cfgrib
    ds = cfgrib.open_datasets('data.grib')
    

    Since you want the temperature variable 't' which is located in the 0th index of the grib output, use:

    tmp = ds[0].t
    

    However, you still need to specify a pressure level. In order to know what index to use, look at the 'isobaricInhPa' values found in the grib output:

    * isobaricInhPa (isobaricInhPa) int64 850 750 700 600 500 ... 175 150 125 100
    

    Think of the values as a list:

    [850 750 700 600 500 ... 175 150 125 100]
    

    So if you wanted 't' at 700mb, you would use t[2] because 700 is located at the 2nd index:

    tmp700 = ds[0].t[2]
    

    (If you need 'isobaricInhPa' values that aren't shown in the grib file output, use:)

    ds[0].isobaricInhPa
    

    And then for the latitude and longitude, use:

    lat = ds[0].latitude
    lon = ds[0].longitude