1

I downloaded ERA5 netcdf data covering the region [30°W-30°E; 0-20°N]. This data downloaded contain horizontal wind components (u, v) and specific humidity (q). I need to calculate horizontal advection of humidity through the use of Metpy function advection.

I get this error

/usr/local/lib/python3.8/dist-packages/metpy/xarray.py:1445: UserWarning: Vertical dimension number not found. Defaulting to (..., Z, Y, X) order.
  warnings.warn(
Traceback (most recent call last):
  File "temp_avect_calc.py", line 33, in <module>
    adv = mpcalc.advection(q, [u, v], (dx, dy))
  File "/usr/local/lib/python3.8/dist-packages/metpy/xarray.py", line 1470, in wrapper
    grid_deltas_from_dataarray(grid_prototype, kind='actual')
  File "/usr/local/lib/python3.8/dist-packages/metpy/xarray.py", line 1380, in grid_deltas_from_dataarray
    geod=f.metpy.pyproj_crs.get_geod()))
  File "/usr/local/lib/python3.8/dist-packages/metpy/xarray.py", line 253, in pyproj_crs
    return self.crs.to_pyproj()
  File "/usr/local/lib/python3.8/dist-packages/metpy/xarray.py", line 233, in crs
    raise AttributeError('crs attribute is not available.')
AttributeError: crs attribute is not available.

The code that i am using is shown below

import cartopy.crs as ccrs
import metpy.calc as mpcalc
from metpy.units import units
import xarray as xr

fname  =  "uwnd_vwnd_shum_750hPa_2019.nc"
ds = xr.open_dataset(fname)

lat = ds["latitude"][::-1]
lon = ds["longitude"]


u = ds["u"][:,  ::-1, :] 
v = ds["v"][:,  ::-1, :] 
q = ds["q"][:,  ::-1, :] 


dx, dy = mpcalc.lat_lon_grid_deltas(lon, lat)

adv = mpcalc.advection(q, [u, v], (dx, dy))
1
  • The data is lacking the crs attribute, as stated by the error. Please check if this is in the raw data. You can probably add it in if it is not, and the data is of a suitable grid Commented Jan 23, 2021 at 14:51

1 Answer 1

1

Based on your provided error, it looks like you're using MetPy 1.0. If this is the case, the function signature for metpy.calc.advection has been changed [link] and can also be simplified with xarray [example]. You can drop calculating the grid spacing, as this will be pulled in from your latitude and longitude coordinates. For this to work, you will have to explicitly have MetPy parse some information in your dataset to create crs information, in this case from your latitudes and longitudes. The example below should work with your data on MetPy 1.0.

ds = xr.open_dataset(fname).metpy.parse_cf()

adv = mpcalc.advection(ds["q"], ds["u"], ds["v"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.