Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use wrapper to importlib for optional dependencies #146

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions .github/workflows/auto-update-files.yml

This file was deleted.

9 changes: 0 additions & 9 deletions doc/source/api_reference/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,3 @@ General Methods
.. autofunction:: gravity_toolkit.time.convert_calendar_decimal

.. autofunction:: gravity_toolkit.time.convert_julian

.. autoclass:: gravity_toolkit.time.timescale
:members:

.. autofunction:: gravity_toolkit.time.count_leap_seconds

.. autofunction:: gravity_toolkit.time.get_leap_seconds

.. autofunction:: gravity_toolkit.time.update_leap_seconds
11 changes: 11 additions & 0 deletions doc/source/api_reference/time_series/lomb_scargle.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
========================
time_series.lomb_scargle
========================

- Wrapper function for computing Lomb-Scargle periodograms using ``scipy.signal.lombscargle``

`Source code`__

.. __: https://github.com/tsutterley/gravity-toolkit/blob/main/gravity_toolkit/time_series/lomb_scargle.py

.. autofunction:: gravity_toolkit.time_series.lomb_scargle
2 changes: 1 addition & 1 deletion doc/source/api_reference/time_series/regress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Calling Sequence
.. code-block:: python
import gravity_toolkit.time_series
tsbeta = gravity_toolkit.time_series.regress(t_in, d_in, ORDER=1, CYCLES=[0.5,1.0], CONF=0.95)
tsbeta = gravity_toolkit.time_series.regress(t_in, d_in, ORDER=1, CYCLES=[0.5,1.0], CONF=0.95)
`Source code`__

Expand Down
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ missions
api_reference/time.rst
api_reference/time_series/amplitude.rst
api_reference/time_series/fit.rst
api_reference/time_series/lomb_scargle.rst
api_reference/time_series/piecewise.rst
api_reference/time_series/regress.rst
api_reference/time_series/savitzky_golay.rst
Expand Down
18 changes: 11 additions & 7 deletions gravity_toolkit/geocenter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
geocenter.py
Written by Tyler Sutterley (09/2023)
Written by Tyler Sutterley (06/2024)
Data class for reading and processing geocenter data
PYTHON DEPENDENCIES:
Expand All @@ -15,6 +15,8 @@
https://github.com/yaml/pyyaml
UPDATE HISTORY:
Updated 06/2024: use wrapper to importlib for optional dependencies
Updated 05/2024: make subscriptable and allow item assignment
Updated 09/2023: add group option to netCDF read function
add functions to return variables or class attributes
prevent double printing of filenames when using debug
Expand Down Expand Up @@ -47,17 +49,13 @@
import time
import uuid
import yaml
import logging
import pathlib
import warnings
import numpy as np
import gravity_toolkit.time
from gravity_toolkit.utilities import import_dependency

# attempt imports
try:
import netCDF4
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("netCDF4 not available", ImportWarning)
netCDF4 = import_dependency('netCDF4')

class geocenter(object):
"""
Expand Down Expand Up @@ -1300,3 +1298,9 @@ def __next__(self):
# add to index
self.__index__ += 1
return temp

def __getitem__(self, key):
return getattr(self, key)

def __setitem__(self, key, value):
setattr(self, key, value)
43 changes: 27 additions & 16 deletions gravity_toolkit/harmonics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
harmonics.py
Written by Tyler Sutterley (10/2023)
Written by Tyler Sutterley (06/2024)
Contributions by Hugo Lecomte
Spherical harmonic data class for processing GRACE/GRACE-FO Level-2 data
Expand All @@ -25,6 +25,8 @@
destripe_harmonics.py: filters spherical harmonics for correlated errors
UPDATE HISTORY:
Updated 06/2024: use wrapper to importlib for optional dependencies
Updated 05/2024: make subscriptable and allow item assignment
Updated 10/2023: place time and month variables in try/except block
Updated 09/2023: prevent double printing of filenames when using debug
Updated 08/2023: add string representation of the harmonics object
Expand Down Expand Up @@ -100,28 +102,19 @@
import logging
import pathlib
import zipfile
import warnings
import numpy as np
import gravity_toolkit.version
from gravity_toolkit.time import adjust_months,calendar_to_grace
from gravity_toolkit.destripe_harmonics import destripe_harmonics
from gravity_toolkit.read_gfc_harmonics import read_gfc_harmonics
from gravity_toolkit.read_GRACE_harmonics import read_GRACE_harmonics
from gravity_toolkit.utilities import reify
from gravity_toolkit.utilities import import_dependency, reify

# attempt imports
try:
from geoid_toolkit.read_ICGEM_harmonics import read_ICGEM_harmonics
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("geoid_toolkit not available", ImportWarning)
try:
import h5py
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("h5py not available", ImportWarning)
try:
import netCDF4
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("netCDF4 not available", ImportWarning)
geoidtk = import_dependency('geoid_toolkit')
h5py = import_dependency('h5py')
netCDF4 = import_dependency('netCDF4')
sparse = import_dependency('sparse')

class harmonics(object):
"""
Expand Down Expand Up @@ -520,7 +513,7 @@ def from_gfc(self, filename, **kwargs):
Ylms = read_gfc_harmonics(self.filename,
TIDE=kwargs['tide'])
else:
Ylms = read_ICGEM_harmonics(self.filename,
Ylms = geoidtk.read_ICGEM_harmonics(self.filename,
TIDE=kwargs['tide'])
# Output file information
logging.info(self.filename)
Expand Down Expand Up @@ -1188,6 +1181,18 @@ def to_masked_array(self):
self.squeeze()
# return the triangular matrix
return Ylms

def to_coo_array(self):
"""
Convert data arrays to a COO sparse matrices
"""
# assign degree and order fields
self.update_dimensions()
# create COO sparse matrices
self.clm = sparse.COO(self.clm)
self.slm = sparse.COO(self.slm)
# return the harmonics object
return self

def update_dimensions(self):
"""
Expand Down Expand Up @@ -1930,3 +1935,9 @@ def __next__(self):
# add to index
self.__index__ += 1
return temp

def __getitem__(self, key):
return getattr(self, key)

def __setitem__(self, key, value):
setattr(self, key, value)
12 changes: 5 additions & 7 deletions gravity_toolkit/read_gfc_harmonics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
read_gfc_harmonics.py
Written by Tyler Sutterley (05/2023)
Written by Tyler Sutterley (06/2023)
Contributions by Hugo Lecomte
Reads gfc files and extracts spherical harmonics for Swarm and
Expand Down Expand Up @@ -55,6 +55,7 @@
calculate_tidal_offset.py: calculates the C20 offset for a tidal system
UPDATE HISTORY:
Updated 06/2024: use wrapper to importlib for optional dependencies
Updated 05/2023: use pathlib to define and operate on paths
Updated 03/2023: improve typing for variables in docstrings
Updated 04/2022: updated docstrings to numpy documentation format
Expand All @@ -69,15 +70,12 @@
"""
import re
import pathlib
import warnings
import numpy as np
import gravity_toolkit.time
from gravity_toolkit.utilities import import_dependency

# attempt imports
try:
from geoid_toolkit.read_ICGEM_harmonics import read_ICGEM_harmonics
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("geoid_toolkit not available", ImportWarning)
geoidtk = import_dependency('geoid_toolkit')

# PURPOSE: read spherical harmonic coefficients of a gravity model
def read_gfc_harmonics(input_file, TIDE=None, FLAG='gfc'):
Expand Down Expand Up @@ -199,7 +197,7 @@ def read_gfc_harmonics(input_file, TIDE=None, FLAG='gfc'):

# python dictionary with model input and headers
ZIP = bool(re.search('ZIP', SFX, re.IGNORECASE))
model_input = read_ICGEM_harmonics(input_file, TIDE=TIDE,
model_input = geoidtk.read_ICGEM_harmonics(input_file, TIDE=TIDE,
FLAG=FLAG, ZIP=ZIP)

# start and end day of the year
Expand Down
9 changes: 8 additions & 1 deletion gravity_toolkit/read_love_numbers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
read_love_numbers.py
Written by Tyler Sutterley (08/2023)
Written by Tyler Sutterley (05/2024)
Reads sets of load Love numbers from PREM and applies isomorphic parameters
Linearly interpolates load Love/Shida numbers for missing degrees
Expand Down Expand Up @@ -56,6 +56,7 @@
103(B12), 30205-30229, (1998)
UPDATE HISTORY:
Updated 05/2024: make subscriptable and allow item assignment
Updated 08/2023: add string representation of the love_numbers class
Updated 05/2023: use pathlib to define and operate on paths
Updated 03/2023: improve typing for variables in docstrings
Expand Down Expand Up @@ -621,3 +622,9 @@ def __iter__(self):
yield self.hl
yield self.kl
yield self.ll

def __getitem__(self, key):
return getattr(self, key)

def __setitem__(self, key, value):
setattr(self, key, value)
22 changes: 12 additions & 10 deletions gravity_toolkit/spatial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
spatial.py
Written by Tyler Sutterley (10/2023)
Written by Tyler Sutterley (06/2024)
Data class for reading, writing and processing spatial data
Expand All @@ -20,6 +20,8 @@
time.py: utilities for calculating time operations
UPDATE HISTORY:
Updated 06/2024: use wrapper to importlib for optional dependencies
Updated 05/2024: make subscriptable and allow item assignment
Updated 10/2023: place time and month variables in try/except block
Updated 09/2023: prevent double printing of filenames when using debug
Updated 08/2023: add string representation of the spatial object
Expand Down Expand Up @@ -80,20 +82,14 @@
import logging
import pathlib
import zipfile
import warnings
import numpy as np
import gravity_toolkit.version
from gravity_toolkit.time import adjust_months, calendar_to_grace
from gravity_toolkit.utilities import import_dependency

# attempt imports
try:
import h5py
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("h5py not available", ImportWarning)
try:
import netCDF4
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.warn("netCDF4 not available", ImportWarning)
h5py = import_dependency('h5py')
netCDF4 = import_dependency('netCDF4')

class spatial(object):
"""
Expand Down Expand Up @@ -2019,3 +2015,9 @@ def update_mask(self):
if getattr(self, 'magnitude') is not None:
self.magnitude[self.mask] = self.fill_value
return self

def __getitem__(self, key):
return getattr(self, key)

def __setitem__(self, key, value):
setattr(self, key, value)
Loading
Loading