Skip to content

Commit

Permalink
refactor: use model class attributes for file format and corrections
Browse files Browse the repository at this point in the history
feat: add command line option to select nodal corrections type
  • Loading branch information
tsutterley committed Sep 10, 2024
1 parent 3f3c1b1 commit d1186eb
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 50 deletions.
24 changes: 18 additions & 6 deletions tides/compute_tides_ICESat2_ATL03.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
UPDATE HISTORY:
Updated 09/2024: use JSON database for known model parameters
drop support for the ascii definition file format
use model class attributes for file format and corrections
add command line option to select nodal corrections type
Updated 08/2024: project bounds for cropping non-geographic OTIS models
added option to allow inferring only specific minor constituents
added option to try automatic detection of definition file format
Expand Down Expand Up @@ -151,6 +153,7 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
METHOD='spline',
EXTRAPOLATE=False,
CUTOFF=None,
CORRECTIONS=None,
INFER_MINOR=False,
MINOR_CONSTITUENTS=None,
APPLY_FLEXURE=False,
Expand Down Expand Up @@ -229,11 +232,10 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
BOUNDS[3] = np.maximum(BOUNDS[3], np.max(lat))

# read tidal constants
corrections, _, grid = model.format.partition('-')
if model.format in ('OTIS','ATLAS-compact','TMD3'):
constituents = pyTMD.io.OTIS.read_constants(model.grid_file,
model.model_file, model.projection, type=model.type,
grid=corrections, crop=CROP, bounds=BOUNDS,
grid=model.file_format, crop=CROP, bounds=BOUNDS,
apply_flexure=APPLY_FLEXURE)
# available model constituents
c = constituents.fields
Expand All @@ -245,7 +247,8 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
c = constituents.fields
elif model.format in ('GOT-ascii','GOT-netcdf'):
constituents = pyTMD.io.GOT.read_constants(model.model_file,
compressed=model.compressed, grid=grid, crop=CROP, bounds=BOUNDS)
compressed=model.compressed, grid=model.file_format,
crop=CROP, bounds=BOUNDS)
# available model constituents
c = constituents.fields
elif model.format in ('FES-ascii','FES-netcdf'):
Expand Down Expand Up @@ -330,16 +333,19 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
# calculate constituent oscillation
hc = amp*np.exp(cph)

# nodal corrections to apply
nodal_corrections = CORRECTIONS or model.corrections
# minor constituents to infer
minor_constituents = MINOR_CONSTITUENTS or model.minor
# predict tidal elevations at time
tide = np.ma.empty((n_seg),fill_value=fv)
tide.mask = np.any(hc.mask,axis=1)
tide.data[:] = pyTMD.predict.drift(ts.tide, hc, c,
deltat=deltat, corrections=corrections)
deltat=deltat, corrections=nodal_corrections)
# calculate values for minor constituents by inferrence
minor_constituents = model.minor or MINOR_CONSTITUENTS
if INFER_MINOR:
minor = pyTMD.predict.infer_minor(ts.tide, hc, c,
deltat=deltat, corrections=corrections,
deltat=deltat, corrections=nodal_corrections,
minor=minor_constituents)
tide.data[:] += minor.data[:]
# replace masked and nan values with fill value
Expand Down Expand Up @@ -674,6 +680,11 @@ def arguments():
parser.add_argument('--cutoff','-c',
type=np.float64, default=10.0,
help='Extrapolation cutoff in kilometers')
# specify nodal corrections type
nodal_choices = ('OTIS', 'FES', 'GOT', 'perth3')
parser.add_argument('--nodal-corrections',
metavar='CORRECTIONS', type=str, choices=nodal_choices,
help='Nodal corrections to apply')
# infer minor constituents from major
parser.add_argument('--infer-minor',
default=False, action='store_true',
Expand Down Expand Up @@ -715,6 +726,7 @@ def main():
METHOD=args.interpolate,
EXTRAPOLATE=args.extrapolate,
CUTOFF=args.cutoff,
CORRECTIONS=args.nodal_corrections,
INFER_MINOR=args.infer_minor,
MINOR_CONSTITUENTS=args.minor_constituents,
APPLY_FLEXURE=args.apply_flexure,
Expand Down
24 changes: 18 additions & 6 deletions tides/compute_tides_ICESat2_ATL06.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
UPDATE HISTORY:
Updated 09/2024: use JSON database for known model parameters
drop support for the ascii definition file format
use model class attributes for file format and corrections
add command line option to select nodal corrections type
Updated 08/2024: project bounds for cropping non-geographic OTIS models
added option to allow inferring only specific minor constituents
added option to try automatic detection of definition file format
Expand Down Expand Up @@ -150,6 +152,7 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
METHOD='spline',
EXTRAPOLATE=False,
CUTOFF=None,
CORRECTIONS=None,
INFER_MINOR=False,
MINOR_CONSTITUENTS=None,
APPLY_FLEXURE=False,
Expand Down Expand Up @@ -228,11 +231,10 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
BOUNDS[3] = np.maximum(BOUNDS[3], np.max(lat))

# read tidal constants
corrections, _, grid = model.format.partition('-')
if model.format in ('OTIS','ATLAS-compact','TMD3'):
constituents = pyTMD.io.OTIS.read_constants(model.grid_file,
model.model_file, model.projection, type=model.type,
grid=corrections, crop=CROP, bounds=BOUNDS,
grid=model.file_format, crop=CROP, bounds=BOUNDS,
apply_flexure=APPLY_FLEXURE)
# available model constituents
c = constituents.fields
Expand All @@ -244,7 +246,8 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
c = constituents.fields
elif model.format in ('GOT-ascii','GOT-netcdf'):
constituents = pyTMD.io.GOT.read_constants(model.model_file,
compressed=model.compressed, grid=grid, crop=CROP, bounds=BOUNDS)
compressed=model.compressed, grid=model.file_format,
crop=CROP, bounds=BOUNDS)
# available model constituents
c = constituents.fields
elif model.format in ('FES-ascii','FES-netcdf'):
Expand Down Expand Up @@ -322,16 +325,19 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
# calculate constituent oscillation
hc = amp*np.exp(cph)

# nodal corrections to apply
nodal_corrections = CORRECTIONS or model.corrections
# minor constituents to infer
minor_constituents = MINOR_CONSTITUENTS or model.minor
# predict tidal elevations at time
tide = np.ma.empty((n_seg),fill_value=fv)
tide.mask = np.any(hc.mask,axis=1)
tide.data[:] = pyTMD.predict.drift(ts.tide, hc, c,
deltat=deltat, corrections=corrections)
deltat=deltat, corrections=nodal_corrections)
# calculate values for minor constituents by inferrence
minor_constituents = model.minor or MINOR_CONSTITUENTS
if INFER_MINOR:
minor = pyTMD.predict.infer_minor(ts.tide, hc, c,
deltat=deltat, corrections=corrections,
deltat=deltat, corrections=nodal_corrections,
minor=minor_constituents)
tide.data[:] += minor.data[:]
# replace masked and nan values with fill value
Expand Down Expand Up @@ -677,6 +683,11 @@ def arguments():
parser.add_argument('--cutoff','-c',
type=np.float64, default=10.0,
help='Extrapolation cutoff in kilometers')
# specify nodal corrections type
nodal_choices = ('OTIS', 'FES', 'GOT', 'perth3')
parser.add_argument('--nodal-corrections',
metavar='CORRECTIONS', type=str, choices=nodal_choices,
help='Nodal corrections to apply')
# infer minor constituents from major
parser.add_argument('--infer-minor',
default=False, action='store_true',
Expand Down Expand Up @@ -718,6 +729,7 @@ def main():
METHOD=args.interpolate,
EXTRAPOLATE=args.extrapolate,
CUTOFF=args.cutoff,
CORRECTIONS=args.nodal_corrections,
INFER_MINOR=args.infer_minor,
MINOR_CONSTITUENTS=args.minor_constituents,
APPLY_FLEXURE=args.apply_flexure,
Expand Down
24 changes: 18 additions & 6 deletions tides/compute_tides_ICESat2_ATL07.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
UPDATE HISTORY:
Updated 09/2024: use JSON database for known model parameters
drop support for the ascii definition file format
use model class attributes for file format and corrections
add command line option to select nodal corrections type
Updated 08/2024: project bounds for cropping non-geographic OTIS models
added option to allow inferring only specific minor constituents
added option to try automatic detection of definition file format
Expand Down Expand Up @@ -146,6 +148,7 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
METHOD='spline',
EXTRAPOLATE=False,
CUTOFF=None,
CORRECTIONS=None,
INFER_MINOR=False,
MINOR_CONSTITUENTS=None,
VERBOSE=False,
Expand Down Expand Up @@ -221,11 +224,10 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
BOUNDS[3] = np.maximum(BOUNDS[3], np.max(lat))

# read tidal constants
corrections, _, grid = model.format.partition('-')
if model.format in ('OTIS','ATLAS-compact','TMD3'):
constituents = pyTMD.io.OTIS.read_constants(model.grid_file,
model.model_file, model.projection, type=model.type,
grid=corrections, crop=CROP, bounds=BOUNDS)
grid=model.file_format, crop=CROP, bounds=BOUNDS)
# available model constituents
c = constituents.fields
elif model.format in ('ATLAS-netcdf',):
Expand All @@ -236,7 +238,8 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
c = constituents.fields
elif model.format in ('GOT-ascii','GOT-netcdf'):
constituents = pyTMD.io.GOT.read_constants(model.model_file,
compressed=model.compressed, grid=grid, crop=CROP, bounds=BOUNDS)
compressed=model.compressed, grid=model.file_format,
crop=CROP, bounds=BOUNDS)
# available model constituents
c = constituents.fields
elif model.format in ('FES-ascii','FES-netcdf'):
Expand Down Expand Up @@ -312,16 +315,19 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
# calculate constituent oscillation
hc = amp*np.exp(cph)

# nodal corrections to apply
nodal_corrections = CORRECTIONS or model.corrections
# minor constituents to infer
minor_constituents = MINOR_CONSTITUENTS or model.minor
# predict tidal elevations at time
tide = np.ma.empty((n_seg))
tide.mask = np.any(hc.mask,axis=1)
tide.data[:] = pyTMD.predict.drift(ts.tide, hc, c,
deltat=deltat, corrections=corrections)
deltat=deltat, corrections=nodal_corrections)
# calculate values for minor constituents by inferrence
minor_constituents = model.minor or MINOR_CONSTITUENTS
if INFER_MINOR:
minor = pyTMD.predict.infer_minor(ts.tide, hc, c,
deltat=deltat, corrections=corrections,
deltat=deltat, corrections=nodal_corrections,
minor=minor_constituents)
tide.data[:] += minor.data[:]
# replace masked and nan values with fill value
Expand Down Expand Up @@ -702,6 +708,11 @@ def arguments():
parser.add_argument('--cutoff','-c',
type=np.float64, default=10.0,
help='Extrapolation cutoff in kilometers')
# specify nodal corrections type
nodal_choices = ('OTIS', 'FES', 'GOT', 'perth3')
parser.add_argument('--nodal-corrections',
metavar='CORRECTIONS', type=str, choices=nodal_choices,
help='Nodal corrections to apply')
# infer minor constituents from major
parser.add_argument('--infer-minor',
default=False, action='store_true',
Expand Down Expand Up @@ -739,6 +750,7 @@ def main():
METHOD=args.interpolate,
EXTRAPOLATE=args.extrapolate,
CUTOFF=args.cutoff,
CORRECTIONS=args.nodal_corrections,
INFER_MINOR=args.infer_minor,
MINOR_CONSTITUENTS=args.minor_constituents,
VERBOSE=args.verbose,
Expand Down
24 changes: 18 additions & 6 deletions tides/compute_tides_ICESat2_ATL10.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
UPDATE HISTORY:
Updated 09/2024: use JSON database for known model parameters
drop support for the ascii definition file format
use model class attributes for file format and corrections
add command line option to select nodal corrections type
Updated 08/2024: project bounds for cropping non-geographic OTIS models
added option to allow inferring only specific minor constituents
added option to try automatic detection of definition file format
Expand Down Expand Up @@ -147,6 +149,7 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
METHOD='spline',
EXTRAPOLATE=False,
CUTOFF=None,
CORRECTIONS=None,
INFER_MINOR=False,
MINOR_CONSTITUENTS=None,
VERBOSE=False,
Expand Down Expand Up @@ -224,11 +227,10 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
BOUNDS[3] = np.maximum(BOUNDS[3], np.max(lat))

# read tidal constants
corrections, _, grid = model.format.partition('-')
if model.format in ('OTIS','ATLAS-compact','TMD3'):
constituents = pyTMD.io.OTIS.read_constants(model.grid_file,
model.model_file, model.projection, type=model.type,
grid=corrections, crop=CROP, bounds=BOUNDS)
grid=model.file_format, crop=CROP, bounds=BOUNDS)
# available model constituents
c = constituents.fields
elif model.format in ('ATLAS-netcdf',):
Expand All @@ -239,7 +241,8 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
c = constituents.fields
elif model.format in ('GOT-ascii','GOT-netcdf'):
constituents = pyTMD.io.GOT.read_constants(model.model_file,
compressed=model.compressed, grid=grid, crop=CROP, bounds=BOUNDS)
compressed=model.compressed, grid=model.file_format,
crop=CROP, bounds=BOUNDS)
# available model constituents
c = constituents.fields
elif model.format in ('FES-ascii','FES-netcdf'):
Expand Down Expand Up @@ -337,16 +340,19 @@ def compute_tides_ICESat2(tide_dir, INPUT_FILE,
# calculate constituent oscillation
hc = amp*np.exp(cph)

# nodal corrections to apply
nodal_corrections = CORRECTIONS or model.corrections
# minor constituents to infer
minor_constituents = MINOR_CONSTITUENTS or model.minor
# predict tidal elevations at time
tide = np.ma.empty((n_seg))
tide.mask = np.any(hc.mask,axis=1)
tide.data[:] = pyTMD.predict.drift(ts.tide, hc, c,
deltat=deltat, corrections=corrections)
deltat=deltat, corrections=nodal_corrections)
# calculate values for minor constituents by inferrence
minor_constituents = model.minor or MINOR_CONSTITUENTS
if INFER_MINOR:
minor = pyTMD.predict.infer_minor(ts.tide, hc, c,
deltat=deltat, corrections=corrections,
deltat=deltat, corrections=nodal_corrections,
minor=minor_constituents)
tide.data[:] += minor.data[:]
# replace masked and nan values with fill value
Expand Down Expand Up @@ -667,6 +673,11 @@ def arguments():
parser.add_argument('--cutoff','-c',
type=np.float64, default=10.0,
help='Extrapolation cutoff in kilometers')
# specify nodal corrections type
nodal_choices = ('OTIS', 'FES', 'GOT', 'perth3')
parser.add_argument('--nodal-corrections',
metavar='CORRECTIONS', type=str, choices=nodal_choices,
help='Nodal corrections to apply')
# infer minor constituents from major
parser.add_argument('--infer-minor',
default=False, action='store_true',
Expand Down Expand Up @@ -704,6 +715,7 @@ def main():
METHOD=args.interpolate,
EXTRAPOLATE=args.extrapolate,
CUTOFF=args.cutoff,
CORRECTIONS=args.nodal_corrections,
INFER_MINOR=args.infer_minor,
MINOR_CONSTITUENTS=args.minor_constituents,
VERBOSE=args.verbose,
Expand Down
Loading

0 comments on commit d1186eb

Please sign in to comment.