Hi folks, I'm wondering if anyone has computed spherical harmonics in Python from model output. So far I have used NCL's shaeC. I see that there is the pyspharm package, and will probably try that if there isn't a GeoCAT function that mimics the old NCL shaeC.
@Alea Kootz is working on something related to this for the @geocat team after I had requested functionality to replicate NCL's gradsf function which I used to calculate lat/lon gradients on a fixed grid. Relevant Github issues are pasted below, and there may be something coming via geocat-comp:
https://github.com/NCAR/geocat-comp/issues/215
https://github.com/NCAR/geocat-comp/issues/147
@Falko Judt Yes there is a spherical harmonics decomposition and recomposition set of functions in geocat-comp. They were released last month. :)
Thanks @Alea Kootz and @Katie Dagon! I think this is exactly what I need.
Let me know if you have any questions.
Hi @Alea Kootz & @geocat ! So I tried to use geocat.comp.spherical.decomposition
in the same way I used sheaC
in NCL. But there are some issues.
In NCL, I provided the data on a fixed lat-lon grid (this is the only way the NCL function works), geocat.comp.spherical.decomposition
seems to be more general, and so one needs to create weights beforehand with geocat.comp.spherical.scale_voronoi
. This is where I'm stuck...
Here's what I did:
import xarray as xr
import numpy as np
ds = xr.open_dataset('/glade/campaign/mmm/wmr/fjudt/projects/dyamond_1/3.75km/latlon/latlon.nc')
lat = np.deg2rad(ds.latitude+90.) #---lat array needs to go from 0--pi, but in our lats go from pi/2 to -pi/2.
lon = np.deg2rad(ds.longitude)
data = ds.vorticity_200hPa
#---lat/lon need to be 2D, but in the fixed grid data file they're just 1D arrays. So make 2D arrays via broadcasting...
lats1, lats2 = xr.broadcast(lat,lon)
lons1, lons2 = xr.broadcast(lon,lat)
%%time
weights = gc.spherical.scale_voronoi(lons1, lats1)
---> get an error here (also it's slow)
%%time
gc.spherical.decomposition(data, weights, lons1, lats1)
Any idea why, and is there an example that tells us how to use geocat.comp.spherical.decomposition
if you're coming from NCL's shaeC
?
@Falko Judt Check what you are doing with the broadcast you should only need to call it once.
And I would suggest using numpy.meshgrid instead of xr.broadcast for your 2d lat/lon arrays.
see here to understand how that works from the spherical.py test setup.
Last updated: May 16 2025 at 17:14 UTC