Skip to content

Commit

Permalink
feat: expose buffer distance to crop tide model data for #367
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutterley committed Nov 26, 2024
1 parent 9478ecb commit a254db0
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 22 deletions.
13 changes: 10 additions & 3 deletions pyTMD/compute.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
compute.py
Written by Tyler Sutterley (10/2024)
Written by Tyler Sutterley (11/2024)
Calculates tidal elevations for correcting elevation or imagery data
Calculates tidal currents at locations and times
Expand Down Expand Up @@ -60,6 +60,7 @@
interpolate.py: interpolation routines for spatial data
UPDATE HISTORY:
Updated 11/2024: expose buffer distance for cropping tide model data
Updated 10/2024: compute delta times based on corrections type
simplify by using wrapper functions to read and interpolate constants
added option to append equilibrium amplitudes for node tides
Expand Down Expand Up @@ -216,6 +217,7 @@ def tide_elevations(
DEFINITION_FILE: str | pathlib.Path | IOBase | None = None,
CROP: bool = False,
BOUNDS: list | np.ndarray | None = None,
BUFFER: int | float | None = None,
EPSG: str | int = 3031,
EPOCH: list | tuple = (2000, 1, 1, 0, 0, 0),
TYPE: str | None = 'drift',
Expand Down Expand Up @@ -255,6 +257,8 @@ def tide_elevations(
Crop tide model data to (buffered) bounds
BOUNDS: list, np.ndarray or NoneType, default None
Boundaries for cropping tide model data
BUFFER: int, float or NoneType, default None
Buffer distance for cropping tide model data
EPSG: int, default: 3031 (Polar Stereographic South, WGS84)
Input coordinate system
EPOCH: tuple, default (2000,1,1,0,0,0)
Expand Down Expand Up @@ -359,7 +363,7 @@ def tide_elevations(

# read tidal constants and interpolate to grid points
amp, ph, c = model.extract_constants(lon, lat, type=model.type,
crop=CROP, bounds=BOUNDS, method=METHOD,
crop=CROP, bounds=BOUNDS, buffer=BUFFER, method=METHOD,
extrapolate=EXTRAPOLATE, cutoff=CUTOFF,
append_node=APPEND_NODE, apply_flexure=APPLY_FLEXURE)
# calculate complex phase in radians for Euler's
Expand Down Expand Up @@ -441,6 +445,7 @@ def tide_currents(
DEFINITION_FILE: str | pathlib.Path | IOBase | None = None,
CROP: bool = False,
BOUNDS: list | np.ndarray | None = None,
BUFFER: int | float | None = None,
EPSG: str | int = 3031,
EPOCH: list | tuple = (2000, 1, 1, 0, 0, 0),
TYPE: str | None = 'drift',
Expand Down Expand Up @@ -478,6 +483,8 @@ def tide_currents(
Crop tide model data to (buffered) bounds
BOUNDS: list, np.ndarray or NoneType, default None
Boundaries for cropping tide model data
BUFFER: int, float or NoneType, default None
Buffer distance for cropping tide model data
EPSG: int, default: 3031 (Polar Stereographic South, WGS84)
Input coordinate system
EPOCH: tuple, default (2000,1,1,0,0,0)
Expand Down Expand Up @@ -580,7 +587,7 @@ def tide_currents(
for t in model.type:
# read tidal constants and interpolate to grid points
amp, ph, c = model.extract_constants(lon, lat, type=t,
crop=CROP, bounds=BOUNDS, method=METHOD,
crop=CROP, bounds=BOUNDS, buffer=BUFFER, method=METHOD,
extrapolate=EXTRAPOLATE, cutoff=CUTOFF)
# calculate complex phase in radians for Euler's
cph = -1j*ph*np.pi/180.0
Expand Down
17 changes: 14 additions & 3 deletions pyTMD/io/ATLAS.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
ATLAS.py
Written by Tyler Sutterley (10/2024)
Written by Tyler Sutterley (11/2024)
Reads files for a tidal model and makes initial calculations to run tide program
Includes functions to extract tidal harmonic constants from OTIS tide models for
Expand Down Expand Up @@ -55,6 +55,7 @@
interpolate.py: interpolation routines for spatial data
UPDATE HISTORY:
Updated 11/2024: expose buffer distance for cropping tide model data
Updated 10/2024: fix error when using default bounds in extract_constants
Updated 07/2024: added crop and bounds keywords for trimming model data
Updated 02/2024: changed variable for setting global grid flag to is_global
Expand Down Expand Up @@ -164,6 +165,8 @@ def extract_constants(
Crop tide model data to (buffered) bounds
bounds: list or NoneType, default None
Boundaries for cropping tide model data
buffer: int, float or NoneType, default None
Buffer angle for cropping tide model data
method: str, default 'spline'
Interpolation method
Expand Down Expand Up @@ -196,6 +199,7 @@ def extract_constants(
kwargs.setdefault('type', 'z')
kwargs.setdefault('crop', False)
kwargs.setdefault('bounds', None)
kwargs.setdefault('buffer', None)
kwargs.setdefault('method', 'spline')
kwargs.setdefault('extrapolate', False)
kwargs.setdefault('cutoff', 10.0)
Expand Down Expand Up @@ -235,6 +239,8 @@ def extract_constants(
bounds = kwargs['bounds'] or [xmin, xmax, ymin, ymax]
# grid step size of tide model
dlon = lon[1] - lon[0]
# default buffer if cropping data
buffer = kwargs['buffer'] or 4*dlon
# if global: extend limits
is_global = False

Expand All @@ -244,7 +250,7 @@ def extract_constants(
mlon, mlat = np.copy(lon), np.copy(lat)
bathymetry, lon, lat = _crop(bathymetry, mlon, mlat,
bounds=bounds,
buffer=4*dlon
buffer=buffer
)
elif (np.min(ilon) < 0.0) & (np.max(lon) > 180.0):
# input points convention (-180:180)
Expand Down Expand Up @@ -320,7 +326,7 @@ def extract_constants(
if kwargs['crop'] and np.any(bounds):
hc, _, _ = _crop(hc, mlon, mlat,
bounds=bounds,
buffer=4*dlon
buffer=buffer
)
# replace original values with extend matrices
if is_global:
Expand Down Expand Up @@ -412,6 +418,8 @@ def read_constants(
Crop tide model data to (buffered) bounds
bounds: list or NoneType, default None
Boundaries for cropping tide model data
buffer: int or float, default 0
Buffer angle for cropping tide model data
Returns
-------
Expand All @@ -423,6 +431,7 @@ def read_constants(
kwargs.setdefault('compressed', True)
kwargs.setdefault('crop', False)
kwargs.setdefault('bounds', None)
kwargs.setdefault('buffer', 0)

# raise warning if model files are entered as a string or path
if isinstance(model_files, (str, pathlib.Path)):
Expand All @@ -444,6 +453,7 @@ def read_constants(
mlon, mlat = np.copy(lon), np.copy(lat)
bathymetry, lon, lat = _crop(bathymetry, mlon, mlat,
bounds=kwargs['bounds'],
buffer=kwargs['buffer'],
)
# grid step size of tide model
dlon = lon[1] - lon[0]
Expand Down Expand Up @@ -473,6 +483,7 @@ def read_constants(
if kwargs['crop'] and np.any(kwargs['bounds']):
hc, lon, lat = _crop(hc, mlon, mlat,
bounds=kwargs['bounds'],
buffer=kwargs['buffer'],
)
# replace original values with extend matrices
if is_global:
Expand Down
14 changes: 12 additions & 2 deletions pyTMD/io/FES.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
FES.py
Written by Tyler Sutterley (10/2024)
Written by Tyler Sutterley (11/2024)
Reads files for a tidal model and makes initial calculations to run tide program
Includes functions to extract tidal harmonic constants from the
Expand Down Expand Up @@ -57,6 +57,7 @@
interpolate.py: interpolation routines for spatial data
UPDATE HISTORY:
Updated 11/2024: expose buffer distance for cropping tide model data
Updated 10/2024: fix error when using default bounds in extract_constants
Updated 07/2024: added new FES2022 to available known model versions
FES2022 have masked longitudes, only extract longitude data
Expand Down Expand Up @@ -175,6 +176,8 @@ def extract_constants(
Crop tide model data to (buffered) bounds
bounds: list or NoneType, default None
Boundaries for cropping tide model data
buffer: float or NoneType, default None
Buffer angle for cropping tide model data
method: str, default 'spline'
Interpolation method
Expand Down Expand Up @@ -203,6 +206,7 @@ def extract_constants(
kwargs.setdefault('compressed', False)
kwargs.setdefault('crop', False)
kwargs.setdefault('bounds', None)
kwargs.setdefault('buffer', None)
kwargs.setdefault('method', 'spline')
kwargs.setdefault('extrapolate', False)
kwargs.setdefault('cutoff', 10.0)
Expand Down Expand Up @@ -255,12 +259,14 @@ def extract_constants(
hc, lon, lat = read_netcdf_file(model_file, **kwargs)
# grid step size of tide model
dlon = lon[1] - lon[0]
# default buffer if cropping data
buffer = kwargs['buffer'] or 4*dlon
# crop tide model data to (buffered) bounds
# or adjust longitudinal convention to fit tide model
if kwargs['crop'] and np.any(bounds):
hc, lon, lat = _crop(hc, lon, lat,
bounds=bounds,
buffer=4*dlon
buffer=buffer,
)
elif (np.min(ilon) < 0.0) & (np.max(lon) > 180.0):
# input points convention (-180:180)
Expand Down Expand Up @@ -372,6 +378,8 @@ def read_constants(
Crop tide model data to (buffered) bounds
bounds: list or NoneType, default None
Boundaries for cropping tide model data
buffer: int or float, default 0
Buffer angle for cropping tide model data
Returns
-------
Expand All @@ -384,6 +392,7 @@ def read_constants(
kwargs.setdefault('compressed', False)
kwargs.setdefault('crop', False)
kwargs.setdefault('bounds', None)
kwargs.setdefault('buffer', 0)

# raise warning if model files are entered as a string or path
if isinstance(model_files, (str, pathlib.Path)):
Expand Down Expand Up @@ -414,6 +423,7 @@ def read_constants(
if kwargs['crop'] and np.any(kwargs['bounds']):
hc, lon, lat = _crop(hc, lon, lat,
bounds=kwargs['bounds'],
buffer=kwargs['buffer'],
)
# grid step size of tide model
dlon = lon[1] - lon[0]
Expand Down
14 changes: 12 additions & 2 deletions pyTMD/io/GOT.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
u"""
GOT.py
Written by Tyler Sutterley (10/2024)
Written by Tyler Sutterley (11/2024)
Reads files for Richard Ray's Global Ocean Tide (GOT) models and makes initial
calculations to run the tide program
Expand Down Expand Up @@ -42,6 +42,7 @@
interpolate.py: interpolation routines for spatial data
UPDATE HISTORY:
Updated 11/2024: expose buffer distance for cropping tide model data
Updated 10/2024: fix error when using default bounds in extract_constants
Updated 07/2024: added crop and bounds keywords for trimming model data
use parse function from constituents class to extract names
Expand Down Expand Up @@ -147,6 +148,8 @@ def extract_constants(
Crop tide model data to (buffered) bounds
bounds: list or NoneType, default None
Boundaries for cropping tide model data
buffer: int or float, default None
Buffer angle for cropping tide model data
method: str, default 'spline'
Interpolation method
Expand Down Expand Up @@ -176,6 +179,7 @@ def extract_constants(
kwargs.setdefault('compressed', False)
kwargs.setdefault('crop', False)
kwargs.setdefault('bounds', None)
kwargs.setdefault('buffer', None)
kwargs.setdefault('method', 'spline')
kwargs.setdefault('extrapolate', False)
kwargs.setdefault('cutoff', 10.0)
Expand Down Expand Up @@ -232,12 +236,14 @@ def extract_constants(
constituents.append(cons)
# grid step size of tide model
dlon = np.abs(lon[1] - lon[0])
# default buffer if cropping data
buffer = kwargs['buffer'] or 4*dlon
# crop tide model data to (buffered) bounds
# or adjust longitudinal convention to fit tide model
if kwargs['crop'] and np.any(bounds):
hc, lon, lat = _crop(hc, lon, lat,
bounds=bounds,
buffer=4*dlon
buffer=buffer,
)
elif (np.min(ilon) < 0.0) & (np.max(lon) > 180.0):
# input points convention (-180:180)
Expand Down Expand Up @@ -326,6 +332,8 @@ def read_constants(
Crop tide model data to (buffered) bounds
bounds: list or NoneType, default None
Boundaries for cropping tide model data
buffer: int or float, default 0
Buffer angle for cropping tide model data
Returns
-------
Expand All @@ -337,6 +345,7 @@ def read_constants(
kwargs.setdefault('compressed', False)
kwargs.setdefault('crop', False)
kwargs.setdefault('bounds', None)
kwargs.setdefault('buffer', 0)

# raise warning if model files are entered as a string
if isinstance(model_files, (str, pathlib.Path)):
Expand All @@ -362,6 +371,7 @@ def read_constants(
if kwargs['crop'] and np.any(kwargs['bounds']):
hc, lon, lat = _crop(hc, lon, lat,
bounds=kwargs['bounds'],
buffer=kwargs['buffer'],
)
# grid step size of tide model
dlon = np.abs(lon[1] - lon[0])
Expand Down
Loading

0 comments on commit a254db0

Please sign in to comment.