ajdawson / windspharm

A Python library for spherical harmonic computations on vector winds.
http://ajdawson.github.io/windspharm
MIT License
82 stars 36 forks source link

Calculating shear and stretch deform using windspharm #110

Open winash12 opened 5 years ago

winash12 commented 5 years ago

NCL has this functionality - https://www.ncl.ucar.edu/Document/Functions/Contributed/shear_stretch_deform.shtml. Is it possible to calculate shear and stretch deform using windspharm ?

All we need is the vector gradient of the wind and then we can add and subtract the components to get the respective quantities. Is this doable ?

ajdawson commented 5 years ago

Why not use the gradient method to compute this, this is pretty much exactly what the NCL code does?

vw = VectorWind(u, v, ...)

# standard interface:
dudx, dudy = vw.gradient(u)
dvdx, dvdy = vw.gradient(v)

# metadata interfaces
dudx, dudy = vw.gradient(vw.u())
dvdx, dvdy = vw.gradient(vw.v())

# Compute stretch and shear
shear   = dvdx + dudy
stretch = dudx - dvdy
winash12 commented 5 years ago

@ajdawson Looks great. Very easy and very clear.

What is the difference between the first set of gradients and the metadata ones ?

ajdawson commented 5 years ago

Nothing except their input arguments. I just realised there was a typo too, I have corrected it.

winash12 commented 3 years ago

Sorry to reopen this nearly 18 months later and I actually never tried this till now and I get this exception -

`def main():

file1 = 'uwnd_200_2020_3_10_00Z.nc'
file2 = 'vwnd_200_2020_3_10_00Z.nc'

nc_uwndFile = Dataset(file1,'r')
nc_vwndFile = Dataset(file2,'r')
lats = nc_uwndFile.variables['lat'][:]  # extract/copy the data
lons = nc_uwndFile.variables['lon'][:]

u = nc_uwndFile.variables['uwnd'][:]
v = nc_vwndFile.variables['vwnd'][:]
u = u[0,0,:,:]
v = v[0,0,:,:]
print(u.shape,v.shape)
vw = VectorWind(u,v)
dudx,dudy = vw.gradient(u)
dvdx,dvdy = vw.gradient(v)
sys.exit()`

and I am getting this exception

`raceback (most recent call last): File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 58, in _wrapfunc return bound(*args, **kwds) TypeError: 'float' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "testspharm.py", line 40, in main() File "testspharm.py", line 23, in main dudx,dudy = vw.gradient(u) File "/usr/local/lib/python3.8/dist-packages/windspharm-1.7.0-py3.8.egg/windspharm/standard.py", line 607, in gradient uchi, vchi = self.s.getgrad(chispec) File "/usr/local/lib/python3.8/dist-packages/spharm/spharm.py", line 878, in getgrad chispec = numpy.reshape(chispec, ((ntrunc+1)(ntrunc+2)/2,1)) File "<__array_function__ internals>", line 5, in reshape File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 299, in reshape return _wrapfunc(a, 'reshape', newshape, order=order) File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 67, in _wrapfunc return _wrapit(obj, method, args, *kwds) File "/usr/local/lib/python3.8/dist-packages/numpy-1.19.1-py3.8-linux-x86_64.egg/numpy/core/fromnumeric.py", line 44, in _wrapit result = getattr(asarray(obj), method)(args, **kwds) TypeError: 'float' object cannot be interpreted as an integer`