3131# DEFINITIONS
3232# ======================================================================
3333
34- # Try to import pyshtools with proper error handling
35- try :
36- import pyshtools
37- PYSHTOOLS_AVAILABLE = True
38- except ImportError :
39- PYSHTOOLS_AVAILABLE = False
40- print (
41- f"{ Yellow } __________________\n "
42- f"Tidal decomposition relies on the pyshtools library, "
43- f"referenced at:\n \n "
44- f"Mark A. Wieczorek and Matthias Meschede (2018). "
45- f"SHTools - Tools for working with spherical harmonics,"
46- f"Geochemistry, Geophysics, Geosystems, 2574-2592, "
47- f"doi:10.1029/2018GC007529\n \n Please consult pyshtools "
48- f"documentation at:\n "
49- f" { Cyan } https://pypi.org/project/pyshtools\n "
50- f"{ Yellow } And installation instructions for CAP with pyshtools:\n "
51- f" { Cyan } https://amescap.readthedocs.io/en/latest/installation."
52- f"html#_spectral_analysis{ Yellow } \n "
53- f"__________________{ Nclr } \n \n "
54- )
34+ def import_pyshtools ():
35+ """
36+ Attempt to import pyshtools and set the global variable
37+ PYSHTOOLS_AVAILABLE accordingly.
38+ Check if pyshtools is available and return its availability status
39+
40+ :return: True if pyshtools is available, False otherwise
41+ :rtype: bool
42+ """
43+
44+ global PYSHTOOLS_AVAILABLE
45+ # Try to import pyshtools with proper error handling
46+ try :
47+ import pyshtools
48+ global pyshtools
49+ PYSHTOOLS_AVAILABLE = True
50+ except ImportError :
51+ PYSHTOOLS_AVAILABLE = False
52+ print (
53+ f"{ Yellow } __________________\n "
54+ f"Tidal decomposition relies on the pyshtools library, "
55+ f"referenced at:\n "
56+ f"{ Cyan } Mark A. Wieczorek and Matthias Meschede (2018). "
57+ f"SHTools - Tools for working with spherical harmonics, "
58+ f"Geochemistry, Geophysics, Geosystems, 2574-2592, "
59+ f"doi:10.1029/2018GC007529\n \n "
60+ f"Please consult pyshtools documentation at:\n "
61+ f"{ Cyan } https://pypi.org/project/pyshtools\n \n "
62+ f"{ Yellow } And installation instructions for CAP with pyshtools:\n "
63+ f"{ Cyan } https://amescap.readthedocs.io/en/latest/installation."
64+ f"html#_spectral_analysis{ Yellow } \n "
65+ f"__________________{ Nclr } \n \n "
66+ )
67+ return PYSHTOOLS_AVAILABLE
5568
5669
5770def diurn_extract (VAR , N , tod , lon ):
@@ -74,7 +87,8 @@ def diurn_extract(VAR, N, tod, lon):
7487 (e.g., size ``[Nh, time, lat, lon]``)
7588 :rtype: ND arrays
7689 """
77-
90+ import_pyshtools ()
91+
7892 dimsIN = VAR .shape
7993 nsteps = len (tod )
8094 period = 24
@@ -140,6 +154,7 @@ def diurn_extract(VAR, N, tod, lon):
140154 # Return the phase and amplitude
141155 return amp .reshape (dimsOUT ), phas .reshape (dimsOUT )
142156
157+
143158def reconstruct_diurn (amp , phas , tod , lon , sumList = []):
144159 """
145160 Reconstructs a field wave based on its diurnal harmonics
@@ -162,7 +177,8 @@ def reconstruct_diurn(amp, phas, tod, lon, sumList=[]):
162177 aggregated (i.e., size = ``[tod, time, lat, lon]``)
163178 :rtype: _type_
164179 """
165-
180+ import_pyshtools ()
181+
166182 dimsIN = amp .shape
167183 N = dimsIN [0 ]
168184 dimsSUM = np .append ([len (tod )], dimsIN [1 :])
@@ -207,6 +223,7 @@ def reconstruct_diurn(amp, phas, tod, lon, sumList=[]):
207223 # Return harmonics separately
208224 return varOUT
209225
226+
210227def extract_diurnal_harmonics (kmx , tmx , varIN , tod , lon ):
211228 """
212229 Obtain west and east propagating waves. This is a Python
@@ -344,6 +361,7 @@ def zeroPhi_filter(VAR, btype, low_highcut, fs, axis=0, order=4,
344361 .. note:: ``Wn=[low, high]`` are expressed as a function of the
345362 Nyquist frequency
346363 """
364+ import_pyshtools ()
347365
348366 # Create the filter
349367 low_highcut = np .array (low_highcut )
@@ -381,6 +399,7 @@ def zonal_decomposition(VAR):
381399 .. note:: Output size is (``[...,lat/2, lat/2]``) as lat is the
382400 smallest dimension. This matches the Nyquist frequency.
383401 """
402+ import_pyshtools ()
384403
385404 if not PYSHTOOLS_AVAILABLE :
386405 raise ImportError (
@@ -440,6 +459,7 @@ def zonal_construct(COEFFS_flat, VAR_shape, btype=None, low_highcut=None):
440459 print("(kmin,kmax) = ({kmin}, {kmax})
441460 -> dx min = {L_min} km, dx max = {L_max} km")
442461 """
462+ import_pyshtools ()
443463
444464 if not PYSHTOOLS_AVAILABLE :
445465 raise ImportError (
@@ -472,12 +492,3 @@ def zonal_construct(COEFFS_flat, VAR_shape, btype=None, low_highcut=None):
472492 return VAR .reshape (VAR_shape )
473493
474494
475- def init_shtools ():
476- """
477- Check if pyshtools is available and return its availability status
478-
479- :return: True if pyshtools is available, False otherwise
480- :rtype: bool
481- """
482-
483- return PYSHTOOLS_AVAILABLE
0 commit comments