Skip to content

Commit

Permalink
feat: output netCDF4 files for all geocenter runs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutterley committed Sep 18, 2023
1 parent a9f666d commit 58b5702
Show file tree
Hide file tree
Showing 34 changed files with 296 additions and 245 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/auto-update-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
shell: bash -l {0}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up mamba ${{ matrix.python-version }}
uses: mamba-org/setup-micromamba@v1
with:
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
fi
- name: Create pull request
if: steps.changes.outputs.DETECTED == 'true'
uses: peter-evans/create-pull-request@v3
uses: peter-evans/create-pull-request@v5
with:
assignees: ${{ github.actor }}
title: "data: automatic time updates"
4 changes: 2 additions & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
shell: bash -l {0}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up mamba ${{ matrix.python-version }}
uses: mamba-org/setup-micromamba@v1
with:
Expand Down
47 changes: 40 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 (05/2023)
Written by Tyler Sutterley (09/2023)
Data class for reading and processing geocenter data
PYTHON DEPENDENCIES:
Expand All @@ -15,6 +15,8 @@
https://github.com/yaml/pyyaml
UPDATE HISTORY:
Updated 09/2023: add group option to netCDF read function
add functions to return variables or class attributes
Updated 05/2023: use pathlib to define and operate on paths
Updated 03/2023: convert shape and ndim to harmonic class properties
improve typing for variables in docstrings
Expand Down Expand Up @@ -54,10 +56,7 @@
try:
import netCDF4
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("netCDF4 not available", ImportWarning)
# ignore warnings
warnings.filterwarnings("ignore")

class geocenter(object):
"""
Expand Down Expand Up @@ -788,7 +787,7 @@ def from_tellus(self, geocenter_file, **kwargs):
# return the geocenter harmonics
return self

def from_netCDF4(self, geocenter_file, **kwargs):
def from_netCDF4(self, geocenter_file, group=None, **kwargs):
"""
Reads geocenter file and extracts dates and spherical harmonic data
from a netCDF4 file
Expand All @@ -797,6 +796,8 @@ def from_netCDF4(self, geocenter_file, **kwargs):
----------
geocenter_file: str
degree 1 netCDF4 file
group: str or NoneType, default None
netCDF4 group name
compression: str or NoneType, default None
file compression type
Expand All @@ -807,7 +808,7 @@ def from_netCDF4(self, geocenter_file, **kwargs):
and ocean model outputs", *Remote Sensing*, 11(18), 2108, (2019).
`doi: 10.3390/rs11182108 <https://doi.org/10.3390/rs11182108>`_
"""
kwargs.setdefault('compression',None)
kwargs.setdefault('compression', None)
# set filename
self.case_insensitive_filename(geocenter_file)
# Open the netCDF4 file for reading
Expand All @@ -822,10 +823,12 @@ def from_netCDF4(self, geocenter_file, **kwargs):
memory=self.filename.read())
else:
fileID = netCDF4.Dataset(self.filename, mode='r')
# check if reading from root group or sub-group
ncf = fileID.groups[group] if group else fileID
# Getting the data from each netCDF4 variable
DEG1 = {}
# converting netCDF4 objects into numpy arrays
for key,val in fileID.variables.items():
for key, val in ncf.variables.items():
DEG1[key] = val[:].copy()
# close the netCDF4 file
fileID.close()
Expand Down Expand Up @@ -1241,6 +1244,36 @@ def power(self, power):
temp.S11 = np.power(self.S11,power)
return temp

def get(self, field: str):
"""
Get a field from the ``geocenter`` object
Parameters
----------
field: str
field name
Returns
-------
var: np.ndarray
variable for field
"""
var = getattr(self, field)
return copy.copy(var)

@property
def fields(self):
return ['C10', 'C11', 'S11']

def __str__(self):
"""String representation of the ``geocenter`` object
"""
properties = ['gravity_toolkit.geocenter']
if self.month:
properties.append(f" start_month: {min(self.month)}")
properties.append(f" end_month: {max(self.month)}")
return '\n'.join(properties)

def __len__(self):
"""Number of months
"""
Expand Down
5 changes: 0 additions & 5 deletions gravity_toolkit/harmonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,15 @@
try:
from geoid_toolkit.read_ICGEM_harmonics import read_ICGEM_harmonics
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("geoid_toolkit not available", ImportWarning)
try:
import h5py
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("h5py not available", ImportWarning)
try:
import netCDF4
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("netCDF4 not available", ImportWarning)
# ignore warnings
warnings.filterwarnings("ignore")

class harmonics(object):
"""
Expand Down
3 changes: 0 additions & 3 deletions gravity_toolkit/read_gfc_harmonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@
try:
from geoid_toolkit.read_ICGEM_harmonics import read_ICGEM_harmonics
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("geoid_toolkit not available", ImportWarning)
# ignore warnings
warnings.filterwarnings("ignore")

# PURPOSE: read spherical harmonic coefficients of a gravity model
def read_gfc_harmonics(input_file, TIDE=None, FLAG='gfc'):
Expand Down
4 changes: 0 additions & 4 deletions gravity_toolkit/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,11 @@
try:
import h5py
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("h5py not available", ImportWarning)
try:
import netCDF4
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("netCDF4 not available", ImportWarning)
# ignore warnings
warnings.filterwarnings("ignore")

class spatial(object):
"""
Expand Down
4 changes: 0 additions & 4 deletions gravity_toolkit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,20 @@
try:
import ipywidgets
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("ipywidgets not available", ImportWarning)
try:
import matplotlib.cm as cm
import matplotlib.colors as colors
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("matplotlib not available", ImportWarning)
try:
from tkinter import Tk, filedialog
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("tkinter not available", ImportWarning)
filedialog = None
try:
import IPython.display
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("IPython.display not available", ImportWarning)
# ignore warnings
warnings.filterwarnings('ignore')
Expand Down
3 changes: 0 additions & 3 deletions gravity_toolkit/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@
try:
import boto3
except (AttributeError, ImportError, ModuleNotFoundError) as exc:
warnings.filterwarnings("module")
warnings.warn("boto3 not available", ImportWarning)
# ignore warnings
warnings.filterwarnings("ignore")

# PURPOSE: get absolute path within a package from a relative path
def get_data_path(relpath: list | str | pathlib.Path):
Expand Down
Loading

0 comments on commit 58b5702

Please sign in to comment.