Can metpy.parse_cf() be used on WRF files?

Viewed 246

I'm trying to follow the MetPy cross-section plotting example (https://unidata.github.io/MetPy/latest/examples/cross_section.html), but applying it to WRF output data. I read in the WRF file as an xarray dataset, and then apply metpy.parse_cf().squeeze() to it, as in the example. However, I get this set of errors:

Traceback (most recent call last):
  File "plot_wrf_cross_overlays.py", line 252, in <module>
    main(init_dt_first, init_dt_last, init_stride_h, plot_beg_lead_time, plot_end_lead_time, plot_stride, domain)
  File "plot_wrf_cross_overlays.py", line 236, in main
    DS_wrf_xr = DS_wrf_xr.metpy.parse_cf().squeeze()
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/metpy/xarray.py", line 521, in parse_cf
    for single_varname in varname])
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/metpy/xarray.py", line 521, in <listcomp>
    for single_varname in varname])
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/metpy/xarray.py", line 537, in parse_cf
    var = self._fixup_coords(var)
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/metpy/xarray.py", line 566, in _fixup_coords
    var = var.metpy.convert_coordinate_units(coord_name, 'meter')
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/metpy/xarray.py", line 159, in convert_coordinate_units
    data=self._data_array[coord].metpy.unit_array.m_as(units)
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/metpy/xarray.py", line 135, in unit_array
    return self._data_array.values * self.units
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/pint/quantity.py", line 1540, in __array_ufunc__
    return numpy_wrap("ufunc", ufunc, inputs, kwargs, types)
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/pint/numpy_func.py", line 894, in numpy_wrap
    return handled[name](*args, **kwargs)
  File "/glade/work/jaredlee/python/my_npl_clone_20200417_cheyenne_3.7.5/lib/python3.7/site-packages/pint/numpy_func.py", line 289, in implementation
    result_magnitude = func(*stripped_args, **stripped_kwargs)
numpy.core._exceptions.UFuncTypeError: ufunc 'multiply' cannot use operands with types dtype('<M8[ns]') and dtype('int64')

I get the same thing even when I drop the squeeze() function call at the end. Any ideas what's causing this, or how to fix this? This is a standard wrfout file from WRF v4.2. I know WRF unfortunately doesn't follow CF-compliant conventions, and that's probably what's causing this problem, but surely this is a problem that others have had, so I'm hoping someone can help point me in the right direction here so that I could still hopefully use MetPy for WRF cross-section plots.

1 Answers

In direct answer to your question, no, .metpy.parse_cf() cannot be used with WRF files, as they are not CF-compliant with regards to grid mapping metadata. However, for datasets without CF-compliant grid mappings, MetPy provides the .metpy.assign_crs() method to use instead to register the needed projection information for calculations like cross_section to work. For example, for a WRF dataset on a Lambert Conformal Conic projection, you can use the following as a replacement for parse_cf():

DS_wrf_xr = DS_wrf_xr.metpy.assign_crs({
    'grid_mapping_name': 'lambert_conformal_conic',
    'earth_radius': 6370000,
    'standard_parallel': [DS_wrf_xr.TRUELAT1, DS_wrf_xr.TRUELAT2],
    'longitude_of_central_meridian': DS_wrf_xr.STAND_LON,
    'latitude_of_projection_origin': DS_wrf_xr.MOAD_CEN_LAT
})

This being said, taking a look at your problem as a whole, the nature of the error you encountered in attempting to use .metpy.parse_cf() is an unexpected bug, and so I would recommend raising an issue on MetPy's issue tracker. Additionally, documentation (or helper routines) for using MetPy with WRF's ...peculiar... metadata is admittedly still lacking (see https://github.com/Unidata/MetPy/issues/1004 and https://github.com/Unidata/MetPy/issues/1089 for relevant discussion). For example, an issue you are likely to encounter with WRF cross-sections is that WRF datasets lack dimension coordinates, which will have to be derived using .assign_y_x or custom routines.

Related