Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support arbitrary epoch for time inputs
Prior to this, time was assumed to be in the form of Matlab datenums--but this is not what any typical Python code would work with. Now the default is matplotlib date numbers, and any epoch can be specified.
- Loading branch information
Showing
11 changed files
with
136 additions
and
59 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
from __future__ import (absolute_import, division, print_function) | |||
|
|||
import datetime | |||
|
|||
import numpy as np | |||
from utide._time_conversion import _normalize_time | |||
|
|||
|
|||
def test_formats(): | |||
forms = [(np.array([693595.1]), 'python'), | |||
(np.array([693961.1]), 'matlab'), | |||
(np.array([2.1]), datetime.date(1899, 12, 29)), | |||
(np.array([3.1]), datetime.datetime(1899, 12, 28)), | |||
(np.array([2.6]), datetime.datetime(1899, 12, 28, 12)), | |||
(np.array([2.1]), '1899-12-29')] | |||
|
|||
expected = _normalize_time(*forms[0]) | |||
|
|||
for form in forms[1:]: | |||
np.testing.assert_almost_equal(_normalize_time(*form), expected) |
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
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
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
""" | |||
Utility for allowing flexible time input. | |||
""" | |||
|
|||
from __future__ import (absolute_import, division, print_function) | |||
|
|||
import warnings | |||
from datetime import date, datetime | |||
|
|||
try: | |||
from datetime import timezone | |||
have_tz = True | |||
except ImportError: | |||
have_tz = False | |||
|
|||
|
|||
def _normalize_time(t, epoch): | |||
if epoch == 'python': | |||
return t | |||
if epoch == 'matlab': | |||
return t - 366 | |||
try: | |||
epoch = datetime.strptime(epoch, "%Y-%m-%d") | |||
except (TypeError, ValueError): | |||
pass | |||
if isinstance(epoch, date): | |||
if not hasattr(epoch, 'time'): | |||
return t + epoch.toordinal() | |||
# It must be a datetime, which is also an instance of date. | |||
if epoch.tzinfo is not None: | |||
if have_tz: | |||
epoch = epoch.astimezone(timezone.utc) | |||
else: | |||
warnings.warn("Timezone info in epoch is being ignored;" | |||
" UTC is assumed.") | |||
ofs = (epoch.toordinal() + epoch.hour / 24 + | |||
epoch.minute / 1440 + epoch.second / 86400) | |||
return t + ofs | |||
raise ValueError("Cannot parse epoch as string or date or datetime") |
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
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
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