Skip to content

Commit

Permalink
Merge pull request #98 from jamal919/master
Browse files Browse the repository at this point in the history
:enh: Updates time input handling
  • Loading branch information
efiring committed Apr 22, 2022
2 parents 46a80e2 + 354f0e5 commit 6afd379
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 199 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ __pycache__/
# C extensions
*.so

# Jupyter
.ipynb_checkpoints

# Distribution / packaging
.Python
env/
Expand Down
136 changes: 106 additions & 30 deletions notebooks/utide_real_data_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:48:45.015791Z",
"start_time": "2021-04-07T18:48:44.379460Z"
}
},
"outputs": [],
"source": [
"%matplotlib notebook\n",
Expand All @@ -12,7 +17,6 @@
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.dates as mdates\n",
"\n",
"import pandas as pd\n",
"\n",
Expand All @@ -31,7 +35,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:48:51.738343Z",
"start_time": "2021-04-07T18:48:51.721594Z"
}
},
"outputs": [],
"source": [
"with open('can1998.dtf') as f:\n",
Expand All @@ -49,7 +58,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:48:59.807296Z",
"start_time": "2021-04-07T18:48:59.691677Z"
}
},
"outputs": [],
"source": [
"\n",
Expand Down Expand Up @@ -88,7 +102,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:49:10.424925Z",
"start_time": "2021-04-07T18:49:10.394298Z"
}
},
"outputs": [],
"source": [
"bad = obs['flag'] == 2\n",
Expand All @@ -105,18 +124,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The utide package works with ordinary numpy arrays, not with Pandas Series or Dataframes, so we need to make a `time` variable in floating point days since a given epoch, and use the `values` attribute of the elevation anomaly (a Pandas Series) to extract the underlying numpy ndarray."
"From **utide v2.7** pandas `datetime` index can be passed directly as the time variable. For elevation or current, utide only works with ordinary numpy arrays, so we use the `to_numpy()` method of the elevation anomaly (a Pandas Series) to extract the underlying numpy ndarray.\n",
"\n",
"Note: The utide package before 2.7 worked only with a `time` array in floating point days since `0000-12-31`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:50:30.514193Z",
"start_time": "2021-04-07T18:50:29.975228Z"
}
},
"outputs": [],
"source": [
"time = mdates.date2num(obs.index.to_pydatetime())\n",
"\n",
"coef = utide.solve(time, obs['anomaly'].values,\n",
"coef = utide.solve(obs.index, obs['anomaly'].to_numpy(),\n",
" lat=-25,\n",
" method='ols',\n",
" conf_int='MC')"
Expand All @@ -132,7 +156,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:50:36.744260Z",
"start_time": "2021-04-07T18:50:36.734396Z"
}
},
"outputs": [],
"source": [
"print(coef.keys())"
Expand All @@ -141,10 +170,15 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:50:56.895599Z",
"start_time": "2021-04-07T18:50:56.504970Z"
}
},
"outputs": [],
"source": [
"tide = utide.reconstruct(time, coef)"
"tide = utide.reconstruct(obs.index, coef)"
]
},
{
Expand All @@ -157,7 +191,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:50:59.473572Z",
"start_time": "2021-04-07T18:50:59.461815Z"
}
},
"outputs": [],
"source": [
"print(tide.keys())"
Expand All @@ -166,13 +205,16 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2021-04-07T18:51:02.163453Z",
"start_time": "2021-04-07T18:51:02.035621Z"
}
},
"outputs": [],
"source": [
"#t = obs.index.values # dtype is '<M8[ns]' (numpy datetime64)\n",
"# It is more efficient to supply the time directly as matplotlib\n",
"# datenum floats:\n",
"t = tide.t_mpl\n",
"# t = obs.index.values # dtype is '<M8[ns]' (numpy datetime64)\n",
"t = tide.t_in\n",
"\n",
"fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, sharey=True, sharex=True)\n",
"\n",
Expand All @@ -183,21 +225,13 @@
"fig.legend(ncol=3, loc='upper center')\n",
"fig.autofmt_xdate()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"hide_input": false,
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -211,7 +245,49 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.7.10"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 6afd379

Please sign in to comment.