Skip to content

Commit

Permalink
refactor: comment format to try to match flake8 styling for #90 (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutterley authored Nov 29, 2022
1 parent 74c0bbd commit d508d93
Show file tree
Hide file tree
Showing 95 changed files with 6,762 additions and 6,762 deletions.
66 changes: 33 additions & 33 deletions gravity_toolkit/clenshaw_summation.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,91 +132,91 @@ def clenshaw_summation(clm, slm, lon, lat, RAD=0, UNITS=0, LMAX=0, LOVE=None,
`doi: 10.1029/2000JB900113 <https://doi.org/10.1029/2000JB900113>`_
"""

#-- check if lat and lon are the same size
# check if lat and lon are the same size
if (len(lat) != len(lon)):
raise ValueError('Incompatable vector dimensions (lon, lat)')

#-- calculate colatitude and longitude in radians
# calculate colatitude and longitude in radians
th = (90.0 - lat)*np.pi/180.0
phi = np.squeeze(lon*np.pi/180.0)
#-- calculate cos and sin of colatitudes
# calculate cos and sin of colatitudes
t = np.cos(th)
u = np.sin(th)

#-- dimensions of theta and phi
# dimensions of theta and phi
npts = len(th)

#-- Gaussian Smoothing
# Gaussian Smoothing
if (RAD != 0):
wl = 2.0*np.pi*gauss_weights(RAD,LMAX)
else:
#-- else = 1
# else = 1
wl = np.ones((LMAX+1))

#-- Setting units factor for output
#-- extract arrays of kl, hl, and ll Love Numbers
# Setting units factor for output
# extract arrays of kl, hl, and ll Love Numbers
factors = units(lmax=LMAX).harmonic(*LOVE)
#-- dfactor computes the degree dependent coefficients
# dfactor computes the degree dependent coefficients
if (UNITS == 0):
#-- 0: keep original scale
# 0: keep original scale
dfactor = factors.norm
elif (UNITS == 1):
#-- 1: cmH2O, centimeters water equivalent
# 1: cmH2O, centimeters water equivalent
dfactor = factors.cmwe
elif (UNITS == 2):
#-- 2: mmGH, mm geoid height
# 2: mmGH, mm geoid height
dfactor = factors.mmGH
elif (UNITS == 3):
#-- 3: mmCU, mm elastic crustal deformation
# 3: mmCU, mm elastic crustal deformation
dfactor = factors.mmCU
elif (UNITS == 4):
#-- 4: micGal, microGal gravity perturbations
# 4: micGal, microGal gravity perturbations
dfactor = factors.microGal
elif (UNITS == 5):
#-- 5: mbar, equivalent surface pressure
# 5: mbar, equivalent surface pressure
dfactor = factors.mbar
elif (UNITS == 6):
#-- 6: cmVCU, cm viscoelastic crustal uplift (GIA ONLY)
# 6: cmVCU, cm viscoelastic crustal uplift (GIA ONLY)
dfactor = factors.cmVCU
elif isinstance(UNITS,(list,np.ndarray)):
#-- custom units
# custom units
dfactor = np.copy(UNITS)
else:
raise ValueError(f'Unknown units {UNITS}')

#-- calculate arrays for clenshaw summations over colatitudes
# calculate arrays for clenshaw summations over colatitudes
s_m_c = np.zeros((npts,LMAX*2+2))
for m in range(LMAX, -1, -1):
#-- convolve harmonics with unit factors and smoothing
# convolve harmonics with unit factors and smoothing
s_m_c[:,2*m:2*m+2] = clenshaw_s_m(t, dfactor*wl, m, clm, slm,
LMAX, SCALE=SCALE)

#-- calculate cos(phi)
# calculate cos(phi)
cos_phi_2 = 2.0*np.cos(phi)
#-- matrix of cos/sin m*phi summation
# matrix of cos/sin m*phi summation
cos_m_phi = np.zeros((npts,LMAX+2),dtype=ASTYPE)
sin_m_phi = np.zeros((npts,LMAX+2),dtype=ASTYPE)
#-- initialize matrix with values at lmax+1 and lmax
# initialize matrix with values at lmax+1 and lmax
cos_m_phi[:,LMAX+1] = np.cos(ASTYPE(LMAX + 1)*phi)
sin_m_phi[:,LMAX+1] = np.sin(ASTYPE(LMAX + 1)*phi)
cos_m_phi[:,LMAX] = np.cos(ASTYPE(LMAX)*phi)
sin_m_phi[:,LMAX] = np.sin(ASTYPE(LMAX)*phi)
#-- calculate summation for order LMAX
# calculate summation for order LMAX
s_m = s_m_c[:,2*LMAX]*cos_m_phi[:,LMAX] + s_m_c[:,2*LMAX+1]*sin_m_phi[:,LMAX]
#-- iterate to calculate complete summation
# iterate to calculate complete summation
for m in range(LMAX-1, 0, -1):
cos_m_phi[:,m] = cos_phi_2*cos_m_phi[:,m+1] - cos_m_phi[:,m+2]
sin_m_phi[:,m] = cos_phi_2*sin_m_phi[:,m+1] - sin_m_phi[:,m+2]
#-- calculate summation for order m
# calculate summation for order m
a_m = np.sqrt((2.0*m+3.0)/(2.0*m+2.0))
s_m = a_m*u*s_m + s_m_c[:,2*m]*cos_m_phi[:,m] + s_m_c[:,2*m+1]*sin_m_phi[:,m]
#-- calculate spatial field
# calculate spatial field
spatial = np.sqrt(3.0)*u*s_m + s_m_c[:,0]
#-- return the calculated spatial field
# return the calculated spatial field
return spatial

#-- PURPOSE: compute conditioned arrays for Clenshaw summation from the
#-- fully-normalized associated Legendre's function for an order m
# PURPOSE: compute conditioned arrays for Clenshaw summation from the
# fully-normalized associated Legendre's function for an order m
def clenshaw_s_m(t, f, m, clm1, slm1, lmax, ASTYPE=np.longdouble, SCALE=1e-280):
"""
Compute conditioned arrays for Clenshaw summation from the fully-normalized
Expand All @@ -235,13 +235,13 @@ def clenshaw_s_m(t, f, m, clm1, slm1, lmax, ASTYPE=np.longdouble, SCALE=1e-280):
-------
s_m_c: conditioned array for clenshaw summation
"""
#-- allocate for output matrix
# allocate for output matrix
N = len(t)
s_m = np.zeros((N,2),dtype=ASTYPE)
#-- scaling to prevent overflow
# scaling to prevent overflow
clm = SCALE*clm1.astype(ASTYPE)
slm = SCALE*slm1.astype(ASTYPE)
#-- convert lmax and m to float
# convert lmax and m to float
lm = ASTYPE(lmax)
mm = ASTYPE(m)
if (m == lmax):
Expand Down Expand Up @@ -281,5 +281,5 @@ def clenshaw_s_m(t, f, m, clm1, slm1, lmax, ASTYPE=np.longdouble, SCALE=1e-280):
s_mm_c_pre_2 = np.copy(s_mm_c_pre_1)
s_mm_c_pre_1 = np.copy(s_mm_c)
s_m[:,0] = np.copy(s_mm_c)
#-- return s_m rescaled with scalef
# return s_m rescaled with scalef
return s_m/SCALE
14 changes: 7 additions & 7 deletions gravity_toolkit/degree_amplitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ def degree_amplitude(clm, slm, LMAX=None, MMAX=None):
amp: float
degree amplitude
"""
#-- add a singleton dimension to input harmonics
# add a singleton dimension to input harmonics
clm = np.atleast_3d(clm)
slm = np.atleast_3d(slm)
#-- check shape
# check shape
LMp1,MMp1,nt = np.shape(clm)

#-- upper bound of spherical harmonic degrees
# upper bound of spherical harmonic degrees
if LMAX is None:
LMAX = LMp1 - 1
#-- upper bound of spherical harmonic orders
# upper bound of spherical harmonic orders
if MMAX is None:
MMAX = MMp1 - 1

#-- allocating for output array
# allocating for output array
amp = np.zeros((LMAX+1,nt))
for l in range(LMAX+1):
m = np.arange(0,MMAX+1)
#-- degree amplitude of spherical harmonic degree
# degree amplitude of spherical harmonic degree
amp[l,:] = np.sqrt(np.sum(clm[l,m,:]**2 + slm[l,m,:]**2,axis=0))

#-- return the degree amplitude with singleton dimensions removed
# return the degree amplitude with singleton dimensions removed
return np.squeeze(amp)
72 changes: 36 additions & 36 deletions gravity_toolkit/destripe_harmonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,42 @@ def destripe_harmonics(clm1, slm1, LMIN=2, LMAX=60, MMAX=None,
`doi: 10.1029/2005GL025285 <https://doi.org/10.1029/2005GL025285>`_
"""

#-- tests if spherical harmonics have been imported
# tests if spherical harmonics have been imported
if (clm1.shape[0] == 1) or (slm1.shape[0] == 1):
raise ValueError('Input harmonics need to be matrices')

#-- upper bound of spherical harmonic orders (default = LMAX)
# upper bound of spherical harmonic orders (default = LMAX)
if MMAX is None:
MMAX = np.copy(LMAX)

#-- output filtered coefficients (copy to not modify input)
# output filtered coefficients (copy to not modify input)
Wclm = clm1.copy()
Wslm = slm1.copy()
#-- matrix size declarations
# matrix size declarations
clmeven = np.zeros((LMAX), dtype=np.float64)
slmeven = np.zeros((LMAX), dtype=np.float64)
clmodd = np.zeros((LMAX+1), dtype=np.float64)
slmodd = np.zeros((LMAX+1), dtype=np.float64)
clmsm = np.zeros((LMAX+1,MMAX+1), dtype=np.float64)
slmsm = np.zeros((LMAX+1,MMAX+1), dtype=np.float64)

#-- start of the smoothing over orders (m)
# start of the smoothing over orders (m)
for m in range(int(MMAX+1)):
smooth = np.exp(-np.float64(m)/10.0)*15.0
if ROUND:
#-- round(smooth) to nearest even instead of int(smooth)
# round(smooth) to nearest even instead of int(smooth)
nsmooth = np.around(smooth)
else:
#-- Sean's method for finding nsmooth (use floor of smooth)
# Sean's method for finding nsmooth (use floor of smooth)
nsmooth = np.int64(smooth)

if (nsmooth < 2):
#-- Isabella's method of picking nsmooth sets minimum to 2
# Isabella's method of picking nsmooth sets minimum to 2
nsmooth = np.int64(2)

rmat = np.zeros((3,3), dtype=np.float64)
lll = np.arange(np.float64(nsmooth)*2.+1.)-np.float64(nsmooth)
#-- create design matrix to have the following form:
# create design matrix to have the following form:
# [ 1 ll ll^2 ]
# [ ll ll^2 ll^3 ]
# [ ll^2 ll^3 ll^4 ]
Expand All @@ -147,14 +147,14 @@ def destripe_harmonics(clm1, slm1, LMIN=2, LMAX=60, MMAX=None,
rmat[2,1] += ill**3
rmat[2,2] += ill**4

#-- put the even and odd l's into their own arrays
# put the even and odd l's into their own arrays
ieven = -1
iodd = -1
leven = np.zeros((LMAX), dtype=np.int64)
lodd = np.zeros((LMAX), dtype=np.int64)

for l in range(int(m),int(LMAX+1)):
#-- check if degree is odd or even
# check if degree is odd or even
if np.remainder(l,2).astype(bool):
iodd += 1
lodd[iodd] = l
Expand All @@ -166,21 +166,21 @@ def destripe_harmonics(clm1, slm1, LMIN=2, LMAX=60, MMAX=None,
clmeven[ieven] = clm1[l,m].copy()
slmeven[ieven] = slm1[l,m].copy()

#-- smooth, by fitting a quadratic polynomial to 7 points at a time
#-- deal with even stokes coefficients
# smooth, by fitting a quadratic polynomial to 7 points at a time
# deal with even stokes coefficients
l1 = 0
l2 = ieven

if (l1 > (l2-2*nsmooth)):
for l in range(l1,l2+1):
if NARROW:
#-- Sean's method
#-- Clm=Slm=0 if number of points is less than window size
# Sean's method
# Clm=Slm=0 if number of points is less than window size
clmsm[leven[l],m] = 0.0
slmsm[leven[l],m] = 0.0
else:
#-- Isabella's method
#-- Clm and Slm passed through unaltered
# Isabella's method
# Clm and Slm passed through unaltered
clmsm[leven[l],m] = clm1[leven[l],m].copy()
slmsm[leven[l],m] = slm1[leven[l],m].copy()
else:
Expand All @@ -195,46 +195,46 @@ def destripe_harmonics(clm1, slm1, LMIN=2, LMAX=60, MMAX=None,
rhss[1] += slmeven[l+ll]*np.float64(ll)
rhss[2] += slmeven[l+ll]*np.float64(ll**2)

#-- fit design matrix to coefficients
#-- to get beta parameters
# fit design matrix to coefficients
# to get beta parameters
bhsc = np.linalg.lstsq(rmat,rhsc.T,rcond=-1)[0]
bhss = np.linalg.lstsq(rmat,rhss.T,rcond=-1)[0]

#-- all other l is assigned as bhsc
# all other l is assigned as bhsc
clmsm[leven[l],m] = bhsc[0].copy()
#-- all other l is assigned as bhss
# all other l is assigned as bhss
slmsm[leven[l],m] = bhss[0].copy()

if (l == (l1+nsmooth)):
#-- deal with l=l1+nsmooth
# deal with l=l1+nsmooth
for ll in range(int(-nsmooth),0):
clmsm[leven[l+ll],m] = bhsc[0]+bhsc[1]*np.float64(ll) + \
bhsc[2]*np.float64(ll**2)
slmsm[leven[l+ll],m] = bhss[0]+bhss[1]*np.float64(ll) + \
bhss[2]*np.float64(ll**2)

if (l == (l2-nsmooth)):
#-- deal with l=l2-nsmnooth
# deal with l=l2-nsmnooth
for ll in range(1,int(nsmooth+1)):
clmsm[leven[l+ll],m] = bhsc[0]+bhsc[1]*np.float64(ll) + \
bhsc[2]*np.float64(ll**2)
slmsm[leven[l+ll],m] = bhss[0]+bhss[1]*np.float64(ll) + \
bhss[2]*np.float64(ll**2)

#-- deal with odd stokes coefficients
# deal with odd stokes coefficients
l1 = 0
l2 = iodd

if (l1 > (l2-2*nsmooth)):
for l in range(l1,l2+1):
if NARROW:
#-- Sean's method
#-- Clm=Slm=0 if number of points is less than window size
# Sean's method
# Clm=Slm=0 if number of points is less than window size
clmsm[lodd[l],m] = 0.0
slmsm[lodd[l],m] = 0.0
else:
#-- Isabella's method
#-- Clm and Slm passed through unaltered
# Isabella's method
# Clm and Slm passed through unaltered
clmsm[lodd[l],m] = clm1[lodd[l],m].copy()
slmsm[lodd[l],m] = slm1[lodd[l],m].copy()
else:
Expand All @@ -249,36 +249,36 @@ def destripe_harmonics(clm1, slm1, LMIN=2, LMAX=60, MMAX=None,
rhss[1] += slmodd[l+ll]*np.float64(ll)
rhss[2] += slmodd[l+ll]*np.float64(ll**2)

#-- fit design matrix to coefficients
#-- to get beta parameters
# fit design matrix to coefficients
# to get beta parameters
bhsc = np.linalg.lstsq(rmat,rhsc.T,rcond=-1)[0]
bhss = np.linalg.lstsq(rmat,rhss.T,rcond=-1)[0]

#-- all other l is assigned as bhsc
# all other l is assigned as bhsc
clmsm[lodd[l],m] = bhsc[0].copy()
#-- all other l is assigned as bhss
# all other l is assigned as bhss
slmsm[lodd[l],m] = bhss[0].copy()

if (l == (l1+nsmooth)):
#-- deal with l=l1+nsmooth
# deal with l=l1+nsmooth
for ll in range(int(-nsmooth),0):
clmsm[lodd[l+ll],m] = bhsc[0]+bhsc[1]*np.float64(ll) + \
bhsc[2]*np.float64(ll**2)
slmsm[lodd[l+ll],m] = bhss[0]+bhss[1]*np.float64(ll) + \
bhss[2]*np.float64(ll**2)

if (l == (l2-nsmooth)):
#-- deal with l=l2-nsmnooth
# deal with l=l2-nsmnooth
for ll in range(1,int(nsmooth+1)):
clmsm[lodd[l+ll],m] = bhsc[0]+bhsc[1]*np.float64(ll) + \
bhsc[2]*np.float64(ll**2)
slmsm[lodd[l+ll],m] = bhss[0]+bhss[1]*np.float64(ll) + \
bhss[2]*np.float64(ll**2)

#-- deal with m greater than or equal to 5
# deal with m greater than or equal to 5
for l in range(int(m),int(LMAX+1)):
if (m >= 5):
#-- remove smoothed clm/slm from original spherical harmonics
# remove smoothed clm/slm from original spherical harmonics
Wclm[l,m] -= clmsm[l,m]
Wslm[l,m] -= slmsm[l,m]

Expand Down
Loading

0 comments on commit d508d93

Please sign in to comment.