pywgrib2_xr.make_template

pywgrib2_xr.make_template(files, *predicates, vertlevels=None, reftime=None, save=False, invdir=None)[source]

Creates template from GRIB2 files.

Parameters
  • files (str of iterable of str.) – List of GRIB files containing messages with unique reftime. For example, files for all or a subset of forecast times.

  • predicates (callable) – Zero or more boolean functions to select desired variables. A variable is selected if one of predicates returns True. The default is None, means matches everything.

  • vertlevels (str or list of str, optional.) – One of {‘isobaric’, ‘height_asl’, ‘height_agl’, ‘sigma’, ‘hybrid’}. Specifies vertical coordinates. If None (default), all data variables will be 2-D in space.

  • reftime (str or datetime, optional) – Reference time. Default is None. A string must be in the ISO format: YYYY-mm-ddTHH:MM:SS. This argument must be specified for files with multiple reference times.

  • save (bool, optional.) – If True, inventory will be saved to a file. File name and location depends on ‘invdir’. If ‘invdir’ is given, the inventory file will be a hashed path of the GRIB file written to ‘invdir’. Otherwise file name will be that of the GRIB file, with appended extension pyinv. The intention is to allow for temporary inventory when GRIB files are on a read-only medium. Default is False.

  • invdir (str, optional) – Location of inventory files.

Returns

Instantiated class defining dataset structure. None is returned if no messages match the selection criteria.

Return type

Template

Examples

The two equivalent functions select temperature at pressure level:

>>> lambda x: x.varname == 'TMP' and x.bot_level_code == 100 and x.top_level_code = 255
>>> lambda x: x.varname == 'TMP' and re.match(r'\d+ mb', x.level_str)

To select accumulated 3 hour precipitation, define function match_pcp3:

>>> def match_pcp3(x):
>>>     return x.varname == 'APCP' and x.end_ft - x.start_ft == timedelta(hours=3)