Created
October 12, 2023 23:46
-
-
Save robbibt/7bc249986115ac8c739b0ed751e88f19 to your computer and use it in GitHub Desktop.
OTPS tide modelling function for TPXO8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import otps | |
import pytz | |
def otps_tides(lats, lons, times, timezone=None): | |
""" | |
Model tide heights for one or more locations and times using the | |
OTPS TPXO8 tidal model. | |
Parameters: | |
----------- | |
lats, lons : numeric or list of numeric values | |
One or more latitudes and longitude coordinates used to define | |
the location at which to model tides. | |
times : datetime.datetime or list of datetime.datetimes | |
One or more `datatime.datetime` objects providing the times at | |
which to model tides. By default these are assumed to be in UTC | |
time; if this is not the case, use `timezone` below. | |
timezone : string, optional | |
If provided `datatime.datetime`s are not in UTC times, use this | |
parameter to declare a timezone. E.g. to model tides for times | |
expressed in local time at Darwin, Australia, provide | |
`timezone='Australia/Darwin'`. Defaults to `None`, which assumes | |
provided times are UTC. This is used to convert all times to UTC | |
using the `pytz` module. For a full list of timezones, run: | |
`import pytz; pytz.all_timezones`. | |
Returns: | |
-------- | |
tidepoints_df : pandas.DataFrame | |
An `pandas.DataFrame` with a "time" index, "lat" and "lon" | |
columns, and a "tide_m" column giving tide heights at each | |
point location. | |
""" | |
# Convert to list if provided as individual values | |
if not isinstance(lats, list): | |
lats = [lats] | |
if not isinstance(lons, list): | |
lons = [lons] | |
if not isinstance(times, list): | |
times = [times] | |
# If a timezone is provided, localise the input times then | |
# standardise to UTC times | |
if timezone: | |
times = [ | |
pytz.timezone(timezone).localize(time).astimezone(pytz.utc) | |
for time in times | |
] | |
# Create list of lat/lon/time scenarios to model tides | |
observed_timepoints = [ | |
otps.TimePoint(lon, lat, time) | |
for time in times | |
for lon, lat in zip(lons, lats) | |
] | |
# Model tides for each lat/lon/time | |
observed_predictedtides = otps.predict_tide(observed_timepoints) | |
# Output results into pandas.DataFrame | |
tidepoints_df = pd.DataFrame( | |
[(i.timepoint.timestamp, i.timepoint.lon, i.timepoint.lat, i.tide_m) | |
for i in observed_predictedtides], | |
columns=['time', 'lon', 'lat', 'tide_m']) | |
return tidepoints_df.set_index('time') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment