Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

python - Issue with the computation of advection in Metpy [Solved]

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))
question from:https://stackoverflow.com/questions/65857648/issue-with-the-computation-of-advection-in-metpy-solved

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...