Skip to content

Commit

Permalink
feat: include crop in notebook demos
Browse files Browse the repository at this point in the history
refactor: use `timescale` in notebook demos for temporal operations
  • Loading branch information
tsutterley committed Jul 22, 2024
1 parent f5004ca commit 06bd6b3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 95 deletions.
5 changes: 0 additions & 5 deletions notebooks/Plot ATLAS Compact.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
"\n",
"#### Program Dependencies\n",
"\n",
"- `crs.py`: Coordinate Reference System (CRS) routines \n",
"- `io.model.py`: retrieves tide model parameters for named tide models\n",
"- `io.OTIS.py`: extract tidal harmonic constants from OTIS tide models \n",
"- `predict.py`: predict tidal values using harmonic constants \n",
"- `time.py`: utilities for calculating time operations\n",
"\n",
"This notebook uses Jupyter widgets to set parameters for calculating the tidal maps."
]
Expand All @@ -49,8 +46,6 @@
"import os\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import pyTMD.time\n",
"import pyTMD.tools\n",
"import pyTMD.io\n",
"\n",
Expand Down
32 changes: 15 additions & 17 deletions notebooks/Plot Antarctic Tidal Currents.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
"\n",
"# import tide programs\n",
"import pyTMD.io\n",
"import pyTMD.time\n",
"import pyTMD.predict\n",
"import pyTMD.tools\n",
"import pyTMD.utilities\n",
"import timescale.time\n",
"\n",
"# autoreload\n",
"%load_ext autoreload\n",
Expand Down Expand Up @@ -177,10 +177,8 @@
"source": [
"# convert from calendar date to days relative to Jan 1, 1992 (48622 MJD)\n",
"YMD = TMDwidgets.datepick.value\n",
"tide_time = pyTMD.time.convert_calendar_dates(YMD.year, YMD.month,\n",
"ts = timescale.time.Timescale().from_calendar(YMD.year, YMD.month,\n",
" YMD.day, hour=np.arange(24))\n",
"# delta time (TT - UT1) file\n",
"delta_file = pyTMD.utilities.get_data_path(['data','merged_deltat.data'])\n",
"\n",
"# save tide currents\n",
"tide = {}\n",
Expand All @@ -189,28 +187,28 @@
" # read tidal constants and interpolate to grid points\n",
" if model.format in ('OTIS','ATLAS','TMD3'):\n",
" amp,ph,D,c = pyTMD.io.OTIS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file['u'], model.projection, type=TYPE,\n",
" model.model_file['u'], model.projection, type=TYPE, crop=True,\n",
" method='spline', grid=model.format)\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
" elif (model.format == 'netcdf'):\n",
" amp,ph,D,c = pyTMD.io.ATLAS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file[TYPE], type=TYPE, method='spline',\n",
" model.model_file[TYPE], type=TYPE, crop=True, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
" elif (model.format == 'GOT'):\n",
" amp,ph,c = pyTMD.io.GOT.extract_constants(lon, lat, model.model_file[TYPE],\n",
" method='spline', scale=model.scale,\n",
" crop=True, method='spline', scale=model.scale,\n",
" compressed=model.compressed)\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
" elif (model.format == 'FES'):\n",
" amp,ph = pyTMD.io.FES.extract_constants(lon, lat, model.model_file[TYPE],\n",
" type=TYPE, version=model.version, method='spline',\n",
" type=TYPE, version=model.version, crop=True, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" c = model.constituents\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" \n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
"\n",
" # calculate complex phase in radians for Euler's\n",
" cph = -1j*ph*np.pi/180.0\n",
" # calculate constituent oscillation\n",
Expand All @@ -220,9 +218,9 @@
" tide[TYPE] = np.ma.zeros((ny,nx,24))\n",
" for hour in range(24):\n",
" # predict tidal elevations at time and infer minor corrections\n",
" TIDE = pyTMD.predict.map(tide_time[hour], hc, c, deltat=DELTAT[hour],\n",
" TIDE = pyTMD.predict.map(ts.tide[hour], hc, c, deltat=DELTAT[hour],\n",
" corrections=model.format)\n",
" MINOR = pyTMD.predict.infer_minor(tide_time[hour], hc, c,\n",
" MINOR = pyTMD.predict.infer_minor(ts.tide[hour], hc, c,\n",
" deltat=DELTAT[hour], corrections=model.format)\n",
" # add major and minor components and reform grid\n",
" tide[TYPE][:,:,hour] = np.reshape((TIDE+MINOR),(ny,nx))"
Expand Down
10 changes: 5 additions & 5 deletions notebooks/Plot Antarctic Tide Range.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,20 @@
"# read tidal constants and interpolate to grid points\n",
"if model.format in ('OTIS','ATLAS','TMD3'):\n",
" amp,ph,D,c = pyTMD.io.OTIS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file, model.projection, type=model.type,\n",
" model.model_file, model.projection, type=model.type, crop=True,\n",
" method='spline', grid=model.format)\n",
"elif (model.format == 'netcdf'):\n",
" amp,ph,D,c = pyTMD.io.ATLAS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file, type=model.type, method='spline',\n",
" model.model_file, type=model.type, crop=True, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
"elif (model.format == 'GOT'):\n",
" amp,ph,c = pyTMD.io.GOT.extract_constants(lon, lat, model.model_file,\n",
" method='spline', scale=model.scale,\n",
" crop=True, method='spline', scale=model.scale,\n",
" compressed=model.compressed)\n",
"elif (model.format == 'FES'):\n",
" amp,ph = pyTMD.io.FES.extract_constants(lon, lat, model.model_file,\n",
" type=model.type, version=model.version, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" type=model.type, version=model.version, crop=True,\n",
" method='spline', scale=model.scale, compressed=model.compressed)\n",
" c = model.constituents\n",
"\n",
"# calculate minor constituent amplitudes\n",
Expand Down
32 changes: 15 additions & 17 deletions notebooks/Plot Arctic Ocean Map.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
"\n",
"# import tide programs\n",
"import pyTMD.io\n",
"import pyTMD.time\n",
"import pyTMD.predict\n",
"import pyTMD.tools\n",
"import pyTMD.utilities\n",
"import timescale.time\n",
"\n",
"# autoreload\n",
"%load_ext autoreload\n",
Expand Down Expand Up @@ -179,35 +179,33 @@
"source": [
"# convert from calendar date to days relative to Jan 1, 1992 (48622 MJD)\n",
"YMD = TMDwidgets.datepick.value\n",
"tide_time = pyTMD.time.convert_calendar_dates(YMD.year, YMD.month,\n",
"ts = timescale.time.Timescale().from_calendar(YMD.year, YMD.month,\n",
" YMD.day, hour=np.arange(24))\n",
"# delta time (TT - UT1) file\n",
"delta_file = pyTMD.utilities.get_data_path(['data','merged_deltat.data'])\n",
"\n",
"# read tidal constants and interpolate to grid points\n",
"if model.format in ('OTIS','ATLAS','TMD3'):\n",
" amp,ph,D,c = pyTMD.io.OTIS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file, model.projection, type=model.type,\n",
" model.model_file, model.projection, type=model.type, crop=True,\n",
" method='spline', grid=model.format)\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
"elif (model.format == 'netcdf'):\n",
" amp,ph,D,c = pyTMD.io.ATLAS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file, type=model.type, method='spline',\n",
" model.model_file, type=model.type, crop=True, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
"elif (model.format == 'GOT'):\n",
" amp,ph,c = pyTMD.io.GOT.extract_constants(lon, lat, model.model_file,\n",
" method='spline', scale=model.scale,\n",
" crop=True, method='spline', scale=model.scale,\n",
" compressed=model.compressed)\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
"elif (model.format == 'FES'):\n",
" amp,ph = pyTMD.io.FES.extract_constants(lon, lat, model.model_file,\n",
" type=model.type, version=model.version, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" type=model.type, version=model.version, crop=True,\n",
" method='spline', scale=model.scale, compressed=model.compressed)\n",
" c = model.constituents\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
" \n",
"# calculate complex phase in radians for Euler's\n",
"cph = -1j*ph*np.pi/180.0\n",
Expand All @@ -218,9 +216,9 @@
"tide_cm = np.ma.zeros((ny,nx,24))\n",
"for hour in range(24):\n",
" # predict tidal elevations at time and infer minor corrections\n",
" TIDE = pyTMD.predict.map(tide_time[hour], hc, c, deltat=DELTAT[hour],\n",
" TIDE = pyTMD.predict.map(ts.tide[hour], hc, c, deltat=DELTAT[hour],\n",
" corrections=model.format)\n",
" MINOR = pyTMD.predict.infer_minor(tide_time[hour], hc, c,\n",
" MINOR = pyTMD.predict.infer_minor(ts.tide[hour], hc, c,\n",
" deltat=DELTAT[hour], corrections=model.format)\n",
" # add major and minor components and reform grid\n",
" # convert from meters to centimeters\n",
Expand Down
41 changes: 16 additions & 25 deletions notebooks/Plot Ross Ice Shelf Map.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
"\n",
"# import tide programs\n",
"import pyTMD.io\n",
"import pyTMD.time\n",
"import pyTMD.predict\n",
"import pyTMD.tools\n",
"import pyTMD.utilities\n",
"import timescale.time\n",
"\n",
"# autoreload\n",
"%load_ext autoreload\n",
Expand Down Expand Up @@ -179,35 +179,33 @@
"source": [
"# convert from calendar date to days relative to Jan 1, 1992 (48622 MJD)\n",
"YMD = TMDwidgets.datepick.value\n",
"tide_time = pyTMD.time.convert_calendar_dates(YMD.year, YMD.month,\n",
"ts = timescale.time.Timescale().from_calendar(YMD.year, YMD.month,\n",
" YMD.day, hour=np.arange(24))\n",
"# delta time (TT - UT1) file\n",
"delta_file = pyTMD.utilities.get_data_path(['data','merged_deltat.data'])\n",
"\n",
"# read tidal constants and interpolate to grid points\n",
"if model.format in ('OTIS','ATLAS','TMD3'):\n",
" amp,ph,D,c = pyTMD.io.OTIS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file, model.projection, type=model.type,\n",
" model.model_file, model.projection, type=model.type, crop=True,\n",
" method='spline', grid=model.format)\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
"elif (model.format == 'netcdf'):\n",
" amp,ph,D,c = pyTMD.io.ATLAS.extract_constants(lon, lat, model.grid_file,\n",
" model.model_file, type=model.type, method='spline',\n",
" model.model_file, type=model.type, crop=True, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
"elif (model.format == 'GOT'):\n",
" amp,ph,c = pyTMD.io.GOT.extract_constants(lon, lat, model.model_file,\n",
" method='spline', scale=model.scale,\n",
" crop=True, method='spline', scale=model.scale,\n",
" compressed=model.compressed)\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
"elif (model.format == 'FES'):\n",
" amp,ph = pyTMD.io.FES.extract_constants(lon, lat, model.model_file,\n",
" type=model.type, version=model.version, method='spline',\n",
" scale=model.scale, compressed=model.compressed)\n",
" type=model.type, version=model.version, crop=True,\n",
" method='spline', scale=model.scale, compressed=model.compressed)\n",
" c = model.constituents\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
" \n",
"# calculate complex phase in radians for Euler's\n",
"cph = -1j*ph*np.pi/180.0\n",
Expand All @@ -218,9 +216,9 @@
"tide_cm = np.ma.zeros((ny,nx,24))\n",
"for hour in range(24):\n",
" # predict tidal elevations at time and infer minor corrections\n",
" TIDE = pyTMD.predict.map(tide_time[hour], hc, c, deltat=DELTAT[hour],\n",
" TIDE = pyTMD.predict.map(ts.tide[hour], hc, c, deltat=DELTAT[hour],\n",
" corrections=model.format)\n",
" MINOR = pyTMD.predict.infer_minor(tide_time[hour], hc, c,\n",
" MINOR = pyTMD.predict.infer_minor(ts.tide[hour], hc, c,\n",
" deltat=DELTAT[hour], corrections=model.format)\n",
" # add major and minor components and reform grid\n",
" # convert from meters to centimeters\n",
Expand Down Expand Up @@ -295,13 +293,6 @@
"%matplotlib inline\n",
"HTML(anim.to_jshtml())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -323,7 +314,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
24 changes: 12 additions & 12 deletions notebooks/Plot Tide Forecasts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
"\n",
"# import tide programs\n",
"import pyTMD.io\n",
"import pyTMD.time\n",
"import pyTMD.predict\n",
"import pyTMD.tools\n",
"import pyTMD.utilities\n",
"import timescale.time\n",
"\n",
"# autoreload\n",
"%load_ext autoreload\n",
Expand Down Expand Up @@ -133,7 +133,7 @@
"# calculate a weeks forecast every minute\n",
"minutes = np.arange(7*1440)\n",
"# convert time from MJD to days relative to Jan 1, 1992 (48622 MJD)\n",
"tide_time = pyTMD.time.convert_calendar_dates(YMD.year, YMD.month,\n",
"ts = timescale.time.Timescale().from_calendar(YMD.year, YMD.month,\n",
" YMD.day, minute=minutes)\n",
"hours = minutes/60.0\n",
"\n",
Expand Down Expand Up @@ -162,26 +162,26 @@
" model.projection, type=model.type,\n",
" grid=model.format)\n",
" c = constituents.fields\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
"elif (model.format == 'netcdf'):\n",
" constituents = pyTMD.io.ATLAS.read_constants(\n",
" model.grid_file, model.model_file,\n",
" type=model.type, compressed=model.compressed)\n",
" c = constituents.fields\n",
" DELTAT = np.zeros_like(tide_time)\n",
" DELTAT = np.zeros_like(ts.tide)\n",
"elif (model.format == 'GOT'):\n",
" constituents = pyTMD.io.GOT.read_constants(\n",
" model.model_file, compressed=model.compressed)\n",
" c = constituents.fields\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
"elif (model.format == 'FES'):\n",
" constituents = pyTMD.io.FES.read_constants(model.model_file,\n",
" type=model.type, version=model.version,\n",
" compressed=model.compressed)\n",
" c = model.constituents\n",
" # interpolate delta times from calendar dates to tide time\n",
" DELTAT = pyTMD.time.interpolate_delta_time(delta_file, tide_time)\n",
" # delta time (TT - UT1)\n",
" DELTAT = ts.tt_ut1\n",
"\n",
"# update the tide prediction and plot\n",
"def update_tide_prediction(*args):\n",
Expand Down Expand Up @@ -213,17 +213,17 @@
" cph = -1j*ph*np.pi/180.0\n",
" # calculate constituent oscillation\n",
" hc = amp*np.exp(cph)\n",
" # predict tidal elevations at time 1 and infer minor corrections\n",
" TIDE = pyTMD.predict.time_series(tide_time, hc, c,\n",
" # predict tidal elevations at time and infer minor corrections\n",
" TIDE = pyTMD.predict.time_series(ts.tide, hc, c,\n",
" deltat=DELTAT, corrections=model.format)\n",
" MINOR = pyTMD.predict.infer_minor(tide_time, hc, c,\n",
" MINOR = pyTMD.predict.infer_minor(ts.tide, hc, c,\n",
" deltat=DELTAT, corrections=model.format)\n",
" TIDE.data[:] += MINOR.data[:]\n",
" # convert to centimeters\n",
" TIDE.data[:] *= 100.0\n",
"\n",
" # differentiate to calculate high and low tides\n",
" diff = np.zeros_like(tide_time, dtype=np.float64)\n",
" diff = np.zeros_like(ts.tide, dtype=np.float64)\n",
" # forward differentiation for starting point\n",
" diff[0] = TIDE.data[1] - TIDE.data[0]\n",
" # backward differentiation for end point\n",
Expand Down
Loading

0 comments on commit 06bd6b3

Please sign in to comment.