NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

[python-users] metpy.parse_cf doesn't set metpy_crs w/ nomads grib2 hrrr ?

People :

I'm trying to plot HRRR data from a nomads grib2 file, but
metpy.parse_cf doesn't set metpy_crs .  There seems to be enough
attributes (eg : GRIB_gridType, etc).  Attached is some python code
that fetches a grib2 file from nomads (and writes it into the local
directory) , then reads (w/ xarray w/ cfgrib) then metpy.parse_cf,
then prints metpy_crs.  It's works OK w/ GFS ( Projection:
latitude_longitude ), but the HRRR files gives an error : KeyError:
'metpy_crs'

Do I have to call assign_crs / assign_latitude_longitude or should
parse_cf do this ?

Thanks,
Ken
nomadsmodels = {
    'gfs_1p00' : {
        'dir' : 'gfs.{t0:%Y%m%d/%H}/atmos',
        'file' : 'gfs.t{t0:%H}z.pgrb2.1p00.f{ff1:03d}',
        'var0' : 'var_PRMSL=on',
        'var1' : 'prmsl',
    },
    'hrrr_2d' : { # conus wrfsfc
        'dir' : 'hrrr.{t0:%Y%m%d}/conus',
        'file' : 'hrrr.t{t0:%H}z.wrfsfcf{ff1:02d}.grib2',
        'var0' : 'var_MSLMA=on',
        'var1' : 'mslma',
    },
    'hrrrak_2d' : { # alaska wrfsfc
        'dir' : 'hrrr.{t0:%Y%m%d}/alaska',
        'file' : 'hrrr.t{t0:%H}z.wrfsfcf{ff1:02d}.ak.grib2',
        'var0' : 'var_MSLMA=on',
        'var1' : 'mslma',
    },
}

import datetime
import os
import xarray
import metpy

def nomads(filt0):
    model0 = nomadsmodels[filt0]

    u0 = f'https://nomads.ncep.noaa.gov/cgi-bin/filter_{filt0}.pl'
    u0 += '?dir=/' + model0['dir']
    u0 += '&file=' + model0['file']
    u0 += '&' + model0['var0']

    t1 = datetime.datetime.utcnow()
    t1 = t1.replace(hour = 0) # hack to make GFS work
    t1 -= datetime.timedelta(days = 1)

    u1 = u0.format(t0 = t1, ff1 = 0)

    fn0 = filt0 + '-parse_cf.grib2'

    c0 = f"wget -O {fn0} '{u1}'" # get grib file and write to fn0
    print(f'======= c0 : {c0}')

    os.system(c0)

    xds0 = xarray.open_dataset(fn0 , engine='cfgrib')

    #print(xds0)

    xda0 = xds0.metpy.parse_cf(model0['var1'])

    print(' # GRIB_gridType : ', xda0.GRIB_gridType)
    print(' # metpy_crs : ', xda0['metpy_crs'].item()) # HRRR : KeyError: 
'metpy_crs'

nomads('gfs_1p00')
nomads('hrrr_2d')
nomads('hrrrak_2d')

  • 2022 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the python-users archives: