Cross Section Analysis Issue

124 views Asked by At

I'm trying to perform a Cross Section Analysis on a gridded data set following the example code from the Metpy website.

First of all, I'm able to read the original grib2 dataset with xarray and also after converting it to netCDF format.

I successfully imported all the required packages:

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import metpy.calc as mpcalc
from metpy.interpolate import cross_section

Then, I made sure I could read the file using the following command:

data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc', False)
print(data)
<xarray.Dataset>
Dimensions:            (alt: 52, time: 1, x: 451, y: 337)
Coordinates:
  * time               (time) datetime64[ns] 2020-06-18T22:15:00
  * x                  (x) float64 0.0 1.355e+04 ... 6.082e+06 6.095e+06
  * y                  (y) float64 0.0 1.355e+04 ... 4.538e+06 4.551e+06
  * alt                (alt) float64 0.0 30.0 304.0 ... 1.494e+04 1.524e+04
Data variables:
    Lambert_Conformal  int32 ...
    param30.19.0       (time, alt, y, x) float32 ...
Attributes:
    CDI:          Climate Data Interface version 1.9.8 (https://mpimet.mpg.de...
    Conventions:  CF-1.6
    history:      Wed Oct 07 17:19:41 2020: cdo -f nc copy gtgn.t2215z.edr.f0...
    institution:  National Center for Atmospheric Research
    CDO:          Climate Data Operators version 1.9.8 (https://mpimet.mpg.de...

Adding MetPy’s CF parsing command gives me the following ValueError:

data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc')
data = data.metpy.parse_cf().squeeze()
print(data)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fa63435fbf07> in <module>
      1 data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc')
----> 2 data = data.metpy.parse_cf().squeeze()
      3 print(data)

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/xarray.py in parse_cf(self, varname, coordinates)
    510         """
    511         from .cbook import iterable
--> 512         from .plots.mapping import CFProjection
    513 
    514         if varname is None:

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/plots/__init__.py in <module>
     25                 convert_gempak_color])  # pylint: disable=undefined-variable
     26 try:
---> 27     from .cartopy_utils import USCOUNTIES, USSTATES
     28     __all__.extend([USCOUNTIES, USSTATES])
     29 except ImportError:

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/plots/cartopy_utils.py in <module>
     41 
     42 
---> 43 USCOUNTIES = MetPyMapFeature('us_counties', '20m', facecolor='None', edgecolor='black')
     44 
     45 USSTATES = MetPyMapFeature('us_states', '20m', facecolor='None', edgecolor='black')

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/plots/cartopy_utils.py in __init__(self, name, scale, **kwargs)
     14     def __init__(self, name, scale, **kwargs):
     15         """Create USCountiesFeature instance."""
---> 16         super().__init__('', name, scale, **kwargs)
     17 
     18     def geometries(self):

~/miniconda3/envs/metpy/lib/python3.7/site-packages/cartopy/feature/__init__.py in __init__(self, category, name, scale, **kwargs)
    262         self.scaler = scale
    263         # Make sure this is a valid resolution
--> 264         self._validate_scale()
    265 
    266     @property

~/miniconda3/envs/metpy/lib/python3.7/site-packages/cartopy/feature/__init__.py in _validate_scale(self)
    272             raise ValueError(
    273                 '{} is not a valid Natural Earth scale. '.format(self.scale) +
--> 274                 'Valid scales are "110m", "50m", and "10m".'
    275             )
    276 

ValueError: 20m is not a valid Natural Earth scale. Valid scales are "110m", "50m", and "10m".

I'm using Metpy v0.12.1 and xarray v0.15.1

And, interestingly, when I re-run the same code I get another error message: NameError: name 'ctables' is not defined

Please advise what's going wrong when parsing the data.

1

There are 1 answers

2
DopplerShift On BEST ANSWER

get_test_data() is only intended for use when using one of MetPy's built-in testing datasets. When using your own data, you should omit calling this function, so your code using open_dataset should look like:

data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc')