4949UPDATE HISTORY:
5050 Updated 07/2026: use np.einsum for spherical harmonic summations
5151 use np.radians to convert from degrees to radians
52+ added custom weighting function for gridded data
5253 Updated 03/2023: improve typing for variables in docstrings
5354 Updated 01/2023: refactored associated legendre polynomials
5455 Updated 04/2022: updated docstrings to numpy documentation format
@@ -129,7 +130,16 @@ def gen_harmonics(data, lon, lat, **kwargs):
129130 return Ylms
130131
131132
132- def integration (data , lon , lat , LMAX = 60 , MMAX = None , PLM = 0 , ** kwargs ):
133+ def integration (
134+ data ,
135+ lon ,
136+ lat ,
137+ LMAX = 60 ,
138+ MMAX = None ,
139+ WEIGHT = None ,
140+ PLM = 0 ,
141+ ** kwargs ,
142+ ):
133143 """
134144 Converts data from the spatial domain to spherical harmonic coefficients
135145
@@ -145,6 +155,8 @@ def integration(data, lon, lat, LMAX=60, MMAX=None, PLM=0, **kwargs):
145155 Upper bound of Spherical Harmonic Degrees
146156 MMAX: int or NoneType, default None
147157 Upper bound of Spherical Harmonic Orders
158+ WEIGHT: np.ndarray or NoneType, default None
159+ Custom latitudinal weighting function for gridded data
148160 PLM: float, default 0
149161 input Legendre polynomials
150162
@@ -164,19 +176,28 @@ def integration(data, lon, lat, LMAX=60, MMAX=None, PLM=0, **kwargs):
164176 th = np .radians (90.0 - np .squeeze (lat ))
165177 # reformatting longitudes to range 0:360 (if previously -180:180)
166178 phi = np .where (phi < 0 , phi + 2.0 * np .pi , phi )
167- # grid step in radians
168- dphi = np .abs (phi [1 ] - phi [0 ])
169- dth = np .abs (th [1 ] - th [0 ])
179+ # grid dimensions
180+ nlat = np .int64 (len (th ))
170181
171182 # LMAX+1 as there are LMAX+1 elements between 0 and LMAX
172183 ll = np .arange (LMAX + 1 )
173184 mm = np .arange (MMAX + 1 )
174185 # Calculating cos/sin of phi arrays (output [m,phi])
175186 m_phi = np .exp (1j * np .einsum ('m...,p...->mp...' , mm , phi ))
176187
177- # Multiplying sin(th) with differentials of theta and phi
178- # to calculate the integration factor at each latitude
179- int_fact = np .sin (th ) * dphi * dth
188+ # use an integration factor for gridded data or
189+ # calculate from sin(theta)*dtheta*dphi
190+ int_fact = np .zeros ((nlat ))
191+ if WEIGHT is not None :
192+ # Weighting function for integrating gridded data
193+ int_fact [:] = np .broadcast_to (np .atleast_1d (WEIGHT ), nlat )
194+ else :
195+ # Multiplying sin(th) with differentials of theta and phi
196+ # to calculate the integration factor at each latitude
197+ dphi = np .abs (phi [1 ] - phi [0 ])
198+ dth = np .abs (th [1 ] - th [0 ])
199+ int_fact [:] = np .sin (th ) * dphi * dth
200+ # normalizing coefficients
180201 coeff = 1.0 / (4.0 * np .pi )
181202
182203 # Calculate polynomials using Holmes and Featherstone (2002) relation
@@ -204,7 +225,15 @@ def integration(data, lon, lat, LMAX=60, MMAX=None, PLM=0, **kwargs):
204225 return Ylms
205226
206227
207- def fourier (data , lon , lat , LMAX = 60 , MMAX = None , PLM = 0 , ** kwargs ):
228+ def fourier (
229+ data ,
230+ lon ,
231+ lat ,
232+ LMAX = 60 ,
233+ MMAX = None ,
234+ PLM = 0 ,
235+ ** kwargs ,
236+ ):
208237 """
209238 Computes the spherical harmonic coefficients of a spatial field
210239
0 commit comments