Unidata / MetPy

MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data.

Home Page:https://unidata.github.io/MetPy/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mpcalc.isentropic_interpolation_as_dataset "vertical attribute is not available."

wenqi18 opened this issue · comments

I get an error "vertical attribute is not available." when interpolating bitwise vortices to isentropic surfaces using the mpcalc.isentropic_interpolation_as_dataset function.Here's the code and the specifics of the error reported:

f=xr.open_dataset(r'H:\data\dust\daily_atmosphere_data\daily_mean_era5_pv0.5_198604.nc')
f_1=xr.open_dataset(r'H:\data\dust\daily_atmosphere_data\daily_mean_era5_t0.5_198604.nc')
pv=f.pv.loc['1986-04-01',100000:20000,20:60,30:150] # (level,lat,lon)
t=f_1.t.loc['1986-04-01',100000:20000,20:60,30:150]# (level,lat,lon)
print(t,pv)
isentlevs = [345.] * units.kelvin
print(isen_lev)
isent_data = mpcalc.isentropic_interpolation_as_dataset(isentlevs, t, pv)
C:\Users\Administrator\AppData\Local\Temp\ipykernel_8224\906077059.py:12: UserWarning: More than one vertical coordinate present for variable  "t".
  isent_data = mpcalc.isentropic_interpolation_as_dataset(isentlevs, t, pv)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [20], in <cell line: 12>()
     10 print(isen_lev)
     11 # 调用 isentropic_interpolation_as_dataset 函数
---> 12 isent_data = mpcalc.isentropic_interpolation_as_dataset(isentlevs, t, pv)

File G:\Anaconda\lib\site-packages\metpy\calc\thermo.py:2761, in isentropic_interpolation_as_dataset(levels, temperature, max_iters, eps, bottom_up_search, *args)
   2756 all_args = xr.broadcast(temperature, *args)
   2758 # Obtain result as list of Quantities
   2759 ret = isentropic_interpolation(
   2760     levels,
-> 2761     all_args[0].metpy.vertical,
   2762     all_args[0].metpy.unit_array,
   2763     *(arg.metpy.unit_array for arg in all_args[1:]),
   2764     vertical_dim=all_args[0].metpy.find_axis_number('vertical'),
   2765     temperature_out=True,
   2766     max_iters=max_iters,
   2767     eps=eps,
   2768     bottom_up_search=bottom_up_search
   2769 )
   2771 # Reconstruct coordinates and dims (add isentropic levels, remove isobaric levels)
   2772 vertical_dim = all_args[0].metpy.find_axis_name('vertical')

File G:\Anaconda\lib\site-packages\metpy\xarray.py:486, in MetPyDataArrayAccessor.vertical(self)
    483 @property
    484 def vertical(self):
    485     """Return the vertical coordinate."""
--> 486     return self._axis('vertical')

File G:\Anaconda\lib\site-packages\metpy\xarray.py:418, in MetPyDataArrayAccessor._axis(self, axis)
    416 coord_var = self._metpy_axis_search(axis)
    417 if coord_var is None:
--> 418     raise AttributeError(axis + ' attribute is not available.')
    419 else:
    420     return coord_var

AttributeError: vertical attribute is not available.

If you look at the warning, it's saying that the t variable has more tan one coordinate detected as a potentially vertical coordinate; therefore when trying to use metpy's automatic determination of the vertical coordinate, the ambiguity causes it to fail.

Can you either point to/share the data, or at least share the output of print(t)?

<xarray.DataArray 't' (plev: 23, lat: 81, lon: 241)> Size: 2MB
[448983 values with dtype=float32]
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
  * lat          (lat) float64 648B 20.0 20.5 21.0 21.5 ... 58.5 59.0 59.5 60.0
  * lon          (lon) float64 2kB 30.0 30.5 31.0 31.5 ... 149.0 149.5 150.0
Attributes:
    long_name:      Temperature
    units:          K
    standard_name:  air_temperature
    comment:        Air temperature
    type:           real

<xarray.DataArray 'plev' (plev: 23)> Size: 184B
array([100000.,  97500.,  95000.,  92500.,  90000.,  87500.,  85000.,  82500.,
        80000.,  77500.,  75000.,  70000.,  65000.,  60000.,  55000.,  50000.,
        45000.,  40000.,  35000.,  30000.,  25000.,  22500.,  20000.])
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
Attributes:
    long_name:         pressure
    units:             Pa
    positive:          down
    stored_direction:  decreasing
    standard_name:     air_pressure
    axis:              Z
    type:              double

Can you share the output of:

for c in t.coords.values():
    print(t)

Thank you for your kind and patient reply, here are the output of my run of for c in t.coords.values(): print(t)

<xarray.DataArray 't' (plev: 23, lat: 81, lon: 241)> Size: 2MB
array([[[296.74127, 296.83005, ..., 297.4964 , 297.4444 ],
        [296.36545, 296.44113, ..., 297.32965, 297.30157],
        ...,
        [278.36902, 278.97278, ..., 263.3966 , 263.1721 ],
        [277.52542, 278.12903, ..., 263.13138, 262.63293]],

       [[294.85953, 295.24667, ..., 295.36108, 295.29068],
        [294.16635, 294.7395 , ..., 295.28003, 295.1769 ],
        ...,
        [276.9598 , 277.63232, ..., 261.61816, 261.77792],
        [276.30508, 276.79745, ..., 261.86417, 260.95508]],

       ...,

       [[225.48643, 225.51598, ..., 228.07014, 228.03014],
        [225.46547, 225.49323, ..., 228.0713 , 228.02582],
        ...,
        [224.0172 , 223.65071, ..., 215.03047, 214.88417],
        [223.92244, 223.58626, ..., 215.68819, 215.57509]],

       [[220.00923, 219.97023, ..., 221.78143, 221.78139],
        [220.1403 , 220.13548, ..., 221.72295, 221.72882],
        ...,
        [225.68274, 225.49408, ..., 216.64989, 216.5566 ],
        [225.9568 , 225.77208, ..., 217.19447, 217.11772]]], dtype=float32)
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
  * lat          (lat) float64 648B 20.0 20.5 21.0 21.5 ... 58.5 59.0 59.5 60.0
  * lon          (lon) float64 2kB 30.0 30.5 31.0 31.5 ... 149.0 149.5 150.0
Attributes:
    long_name:      Temperature
    units:          K
    standard_name:  air_temperature
    comment:        Air temperature
    type:           real
<xarray.DataArray 't' (plev: 23, lat: 81, lon: 241)> Size: 2MB
array([[[296.74127, 296.83005, ..., 297.4964 , 297.4444 ],
        [296.36545, 296.44113, ..., 297.32965, 297.30157],
        ...,
        [278.36902, 278.97278, ..., 263.3966 , 263.1721 ],
        [277.52542, 278.12903, ..., 263.13138, 262.63293]],

       [[294.85953, 295.24667, ..., 295.36108, 295.29068],
        [294.16635, 294.7395 , ..., 295.28003, 295.1769 ],
        ...,
        [276.9598 , 277.63232, ..., 261.61816, 261.77792],
        [276.30508, 276.79745, ..., 261.86417, 260.95508]],

       ...,

       [[225.48643, 225.51598, ..., 228.07014, 228.03014],
        [225.46547, 225.49323, ..., 228.0713 , 228.02582],
        ...,
        [224.0172 , 223.65071, ..., 215.03047, 214.88417],
        [223.92244, 223.58626, ..., 215.68819, 215.57509]],

       [[220.00923, 219.97023, ..., 221.78143, 221.78139],
        [220.1403 , 220.13548, ..., 221.72295, 221.72882],
        ...,
        [225.68274, 225.49408, ..., 216.64989, 216.5566 ],
        [225.9568 , 225.77208, ..., 217.19447, 217.11772]]], dtype=float32)
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
  * lat          (lat) float64 648B 20.0 20.5 21.0 21.5 ... 58.5 59.0 59.5 60.0
  * lon          (lon) float64 2kB 30.0 30.5 31.0 31.5 ... 149.0 149.5 150.0
Attributes:
    long_name:      Temperature
    units:          K
    standard_name:  air_temperature
    comment:        Air temperature
    type:           real
<xarray.DataArray 't' (plev: 23, lat: 81, lon: 241)> Size: 2MB
array([[[296.74127, 296.83005, ..., 297.4964 , 297.4444 ],
        [296.36545, 296.44113, ..., 297.32965, 297.30157],
        ...,
        [278.36902, 278.97278, ..., 263.3966 , 263.1721 ],
        [277.52542, 278.12903, ..., 263.13138, 262.63293]],

       [[294.85953, 295.24667, ..., 295.36108, 295.29068],
        [294.16635, 294.7395 , ..., 295.28003, 295.1769 ],
        ...,
        [276.9598 , 277.63232, ..., 261.61816, 261.77792],
        [276.30508, 276.79745, ..., 261.86417, 260.95508]],

       ...,

       [[225.48643, 225.51598, ..., 228.07014, 228.03014],
        [225.46547, 225.49323, ..., 228.0713 , 228.02582],
        ...,
        [224.0172 , 223.65071, ..., 215.03047, 214.88417],
        [223.92244, 223.58626, ..., 215.68819, 215.57509]],

       [[220.00923, 219.97023, ..., 221.78143, 221.78139],
        [220.1403 , 220.13548, ..., 221.72295, 221.72882],
        ...,
        [225.68274, 225.49408, ..., 216.64989, 216.5566 ],
        [225.9568 , 225.77208, ..., 217.19447, 217.11772]]], dtype=float32)
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
  * lat          (lat) float64 648B 20.0 20.5 21.0 21.5 ... 58.5 59.0 59.5 60.0
  * lon          (lon) float64 2kB 30.0 30.5 31.0 31.5 ... 149.0 149.5 150.0
Attributes:
    long_name:      Temperature
    units:          K
    standard_name:  air_temperature
    comment:        Air temperature
    type:           real
<xarray.DataArray 't' (plev: 23, lat: 81, lon: 241)> Size: 2MB
array([[[296.74127, 296.83005, ..., 297.4964 , 297.4444 ],
        [296.36545, 296.44113, ..., 297.32965, 297.30157],
        ...,
        [278.36902, 278.97278, ..., 263.3966 , 263.1721 ],
        [277.52542, 278.12903, ..., 263.13138, 262.63293]],

       [[294.85953, 295.24667, ..., 295.36108, 295.29068],
        [294.16635, 294.7395 , ..., 295.28003, 295.1769 ],
        ...,
        [276.9598 , 277.63232, ..., 261.61816, 261.77792],
        [276.30508, 276.79745, ..., 261.86417, 260.95508]],

       ...,

       [[225.48643, 225.51598, ..., 228.07014, 228.03014],
        [225.46547, 225.49323, ..., 228.0713 , 228.02582],
        ...,
        [224.0172 , 223.65071, ..., 215.03047, 214.88417],
        [223.92244, 223.58626, ..., 215.68819, 215.57509]],

       [[220.00923, 219.97023, ..., 221.78143, 221.78139],
        [220.1403 , 220.13548, ..., 221.72295, 221.72882],
        ...,
        [225.68274, 225.49408, ..., 216.64989, 216.5566 ],
        [225.9568 , 225.77208, ..., 217.19447, 217.11772]]], dtype=float32)
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
  * lat          (lat) float64 648B 20.0 20.5 21.0 21.5 ... 58.5 59.0 59.5 60.0
  * lon          (lon) float64 2kB 30.0 30.5 31.0 31.5 ... 149.0 149.5 150.0
Attributes:
    long_name:      Temperature
    units:          K
    standard_name:  air_temperature
    comment:        Air temperature
    type:           real
<xarray.DataArray 't' (plev: 23, lat: 81, lon: 241)> Size: 2MB
array([[[296.74127, 296.83005, ..., 297.4964 , 297.4444 ],
        [296.36545, 296.44113, ..., 297.32965, 297.30157],
        ...,
        [278.36902, 278.97278, ..., 263.3966 , 263.1721 ],
        [277.52542, 278.12903, ..., 263.13138, 262.63293]],

       [[294.85953, 295.24667, ..., 295.36108, 295.29068],
        [294.16635, 294.7395 , ..., 295.28003, 295.1769 ],
        ...,
        [276.9598 , 277.63232, ..., 261.61816, 261.77792],
        [276.30508, 276.79745, ..., 261.86417, 260.95508]],

       ...,

       [[225.48643, 225.51598, ..., 228.07014, 228.03014],
        [225.46547, 225.49323, ..., 228.0713 , 228.02582],
        ...,
        [224.0172 , 223.65071, ..., 215.03047, 214.88417],
        [223.92244, 223.58626, ..., 215.68819, 215.57509]],

       [[220.00923, 219.97023, ..., 221.78143, 221.78139],
        [220.1403 , 220.13548, ..., 221.72295, 221.72882],
        ...,
        [225.68274, 225.49408, ..., 216.64989, 216.5566 ],
        [225.9568 , 225.77208, ..., 217.19447, 217.11772]]], dtype=float32)
Coordinates:
    time         datetime64[ns] 8B 1986-04-01
    realization  int64 8B ...
  * plev         (plev) float64 184B 1e+05 9.75e+04 9.5e+04 ... 2.25e+04 2e+04
  * lat          (lat) float64 648B 20.0 20.5 21.0 21.5 ... 58.5 59.0 59.5 60.0
  * lon          (lon) float64 2kB 30.0 30.5 31.0 31.5 ... 149.0 149.5 150.0
Attributes:
    long_name:      Temperature
    units:          K
    standard_name:  air_temperature
    comment:        Air temperature
    type:           real

1

My apologies, I had a typo in my line above. What I need is the output of:

for c in t.coords.values():
    print(c)

Note the c rather than t in the last line.

Here is the output of print(c), in addition, I found that I implemented isentropic surface interpolation using mpcalc.isentropic_interpolation instead of mpcalc.isentropic_interpolation_as_dataset.

<xarray.DataArray 'time' ()> Size: 8B
array('1995-05-17T00:00:00.000000000', dtype='datetime64[ns]')
Coordinates:
    time         datetime64[ns] 8B 1995-05-17
    realization  int64 8B ...
Attributes:
    long_name:         time
    standard_name:     time
    axis:              T
    stored_direction:  increasing
    type:              double
<xarray.DataArray 'realization' ()> Size: 8B
[1 values with dtype=int64]
Coordinates:
    time         datetime64[ns] 8B 1995-05-17
    realization  int64 8B ...
Attributes:
    long_name:         realization
    units:             1
    standard_name:     realization
    stored_direction:  increasing
    type:              integer
<xarray.DataArray 'plev' (plev: 27)> Size: 216B
array([100000.,  97500.,  95000.,  92500.,  90000.,  87500.,  85000.,  82500.,
        80000.,  77500.,  75000.,  70000.,  65000.,  60000.,  55000.,  50000.,
        45000.,  40000.,  35000.,  30000.,  25000.,  22500.,  20000.,  17500.,
        15000.,  12500.,  10000.])
Coordinates:
    time         datetime64[ns] 8B 1995-05-17
    realization  int64 8B ...
  * plev         (plev) float64 216B 1e+05 9.75e+04 9.5e+04 ... 1.25e+04 1e+04
Attributes:
    long_name:         pressure
    units:             Pa
    positive:          down
    stored_direction:  decreasing
    standard_name:     air_pressure
    axis:              Z
    type:              double
<xarray.DataArray 'lat' (lat: 76)> Size: 608B
array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23.,
       24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37.,
       38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51.,
       52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65.,
       66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
       80., 81., 82., 83., 84., 85.])
Coordinates:
    time         datetime64[ns] 8B 1995-05-17
    realization  int64 8B ...
  * lat          (lat) float64 608B 10.0 11.0 12.0 13.0 ... 82.0 83.0 84.0 85.0
Attributes:
    units:             degrees_north
    standard_name:     latitude
    long_name:         latitude
    stored_direction:  decreasing
    axis:              Y
    positive:          up
    type:              double
    valid_max:         90.0
    valid_min:         -90.0
<xarray.DataArray 'lon' (lon: 91)> Size: 728B
array([ 30.,  31.,  32.,  33.,  34.,  35.,  36.,  37.,  38.,  39.,  40.,  41.,
        42.,  43.,  44.,  45.,  46.,  47.,  48.,  49.,  50.,  51.,  52.,  53.,
        54.,  55.,  56.,  57.,  58.,  59.,  60.,  61.,  62.,  63.,  64.,  65.,
        66.,  67.,  68.,  69.,  70.,  71.,  72.,  73.,  74.,  75.,  76.,  77.,
        78.,  79.,  80.,  81.,  82.,  83.,  84.,  85.,  86.,  87.,  88.,  89.,
        90.,  91.,  92.,  93.,  94.,  95.,  96.,  97.,  98.,  99., 100., 101.,
       102., 103., 104., 105., 106., 107., 108., 109., 110., 111., 112., 113.,
       114., 115., 116., 117., 118., 119., 120.])
Coordinates:
    time         datetime64[ns] 8B 1995-05-17
    realization  int64 8B ...
  * lon          (lon) float64 728B 30.0 31.0 32.0 33.0 ... 118.0 119.0 120.0
Attributes:
    units:          degrees_east
    standard_name:  longitude
    long_name:      longitude
    axis:           X
    positive:       up
    type:           double
    valid_max:      360.0
    valid_min:      -180.0

I've been failing to reproduce this with manually generated data following the metadata above, but was able to download some ERA5 data myself and reproduce. I'm digging in now to see what's going on.

Ok, so the problem is that the lat and lon coordinates have a 'positive' attribute, which in compliance with the CF metadata specification, causes them to be identified as vertical coordinates. I'm going to address this in MetPy to avoid this, as well as try to report this to ECMWF so that this potentially can be corrected.

As a workaround, can you try to add these lines to your script:

t.lon.attrs.pop('positive')
t.lat.attrs.pop('positive')
pv.lon.attrs.pop('positive')
pv.lat.attrs.pop('positive')