Template ======== The template defines dataset structure. It holds parameters common to all messages within the dataset: data origin (from Section 1), geolocation (Section 3), variable names, optional forecast times and vertical dimensions. To make a template, use function :py:func:`~pywgrib2_xr.make_template`. The first argument is a GRIB2 file or a list of files sharing common **reference time**. Other arguments are optional, if not specified, all messages present in GRIB2 files will be processed. :py:func:`~pywgrib2_xr.make_template` uses inventory files to get the required metadata. If those files do not exist, inventory is created by a call to :py:func:`~pywgrib2_xr.make_inventory`. To select subset of variables, specify one or more boolean functions (predicates) accepting inventory item as the single input argument. A message is selected if any of the predicates return True. For example, to select temperature and relative humidity at 2 m, one could write: .. code-block:: python def tmp_2m(item): return item.varname == 'TMP' and item.level_str == '2 m above ground' def rh_2m(item): return item.varname == 'RH' and item.level_str == '2 m above ground' and call the above as: .. code-block:: python tmpl = pywgrib2.make_template(gribfiles, tmp_2m, rh_2m) A shorter version: .. code-block:: python tmpl = pywgrib2.make_template(gribfiles, lambda x: x.varname in ('TMP', 'RH') and x.level_code==103 and x.level_value==2) The list of all available attributes is :ref:`here `. When a data file contains messages for multiple model outputs, :py:func:`~pywgrib2_xr.make_template` requires argument ``reftime``. The value does not matter, as long as it is listed in wgrib2 inventory output. To group several levels into 3-dimensional spacial array, pass: ``vertlevels=levels`` where ``levels`` is a string or a list of strings specifying vertical dimension(s) to be combined, for example: ``vertlevels='isobaric'`` combines pressure levels. Available levels are ``isobaric``, ``height_asl``, ``height_asl``, ``sigma``, ``hybrid``. There are more arguments to :py:func:`~pywgrib2_xr.make_template`, refer to the doc string for explanation. To allow variable with the same names in a GRIB2 file, but describing different physical quantities to coexist within the same dataset, variable name is made by concatenating ``varname``, ``level_str`` and ``time_str``, sparated by a dot. Blanks are replaced by an underscore. ``varname`` is what **wgrib2** terms `extended name`, which can be obtained by passing the option ``-ext_name`` (or ``-pyinv``). Consider: .. code-block:: console wgrib2 albers.grb .. parsed-literal:: 1:0:d=2009060500:HGT:200 mb:330 hour fcst:ens std dev .. code-block:: console wgrib2 albers.grb -ext_name .. parsed-literal:: 1:0:HGT.ens_std_dev The `level` part is taken verbatim from ``level_str``. Without argument ``vertlevels='isobaric'``, temperature at 500 hPa is coded as ``TMP.500_mb``. When ``vertlevels`` is specified, variable name is ``TMP.isobaric``. Compare: .. code-block:: python files = sorted(glob.glob('nam.t00z.afwahi??.tm00.grib2')) tmpl = pywgrib2.make_template(files, lambda x: x.varname=='TMP' and x.level_code==100 and 90000`__), and, if present, calculates period between ``start_ft`` and ``end_ft``. When the period is 0, the time part is null, otherwise is formed from the processing type and the period. For example, the string `12-15 hour acc fcst` translates to `3_hour_acc`, while `12 hour fcst` is ignored. The time coordinate value is always that of ``end_ft``.