Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes for ITRF transformation code #10

Merged
merged 5 commits into from
Aug 16, 2024
Merged

Conversation

trey-stafford
Copy link
Contributor

No description provided.

"""

def sinceEpoch(date):
# returns seconds since epoch
return time.mktime(date.timetuple())
return calendar.timegm(date.timetuple())
Copy link
Contributor Author

@trey-stafford trey-stafford Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a subtle bug (thanks stackoverflow!). time.mktime assumes the local timezone, not UTC. Since all of our timestamps are UTC, calendar.timegm should be used instead.

Prior to this cahnge, a the UTC timestamp "1993-07-02 12:00:00" was being converted into decimal year 1993.4998858447489 instead of 1993.5.

You can actually replicate the correct behavior without changing the code by setting the TZ envvar to UTC:

$ python -c "from iceflow.itrf.converter import _datetime_to_decimal_year; import pandas as pd; print(_datetime_to_decimal_year(pd.to_datetime('1993-07-02 12:00:00')))"
1993.4998858447489
$ TZ=UTC  python -c "from iceflow.itrf.converter import _datetime_to_decimal_year; import pandas as pd; print(_datetime_to_decimal_year(pd.to_datetime('1993-07-02 12:00:00')))"
1993.5


# Before and after values were extracted from the synthetic dataset in the
# ITRF corrections notebook put together by Kevin. See:
# https://github.com/nsidc/NSIDC-Data-Tutorials/blob/a35203f18841456d258fac3a11dc04f44f839d9d/notebooks/iceflow/corrections.ipynb
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the datetime conversion bug because the notebook hard-codes the decimal year as 1993.5 - it doesn't calculate it from a datetime like we normally do for real data.

@trey-stafford trey-stafford marked this pull request as ready for review August 16, 2024 00:44
@trey-stafford trey-stafford changed the title Bugfix transform Bugfixes for ITRF transformation code Aug 16, 2024
@@ -82,7 +84,7 @@ def transform_itrf(
# TODO: Should we create a new decimalyears when doing an epoch
# propagation since PROJ doesn't do this?

lats, lons, elevs, _ = transformer.transform(
lons, lats, elevs, _ = transformer.transform(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was my mistake when re-factoring the code I borrowed from valkyrie.

Copy link
Contributor

@mfisher87 mfisher87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

tests/test_itrf_converter.py Outdated Show resolved Hide resolved
The code as currently written should be invariant with respect to the set
timezone. Prior to the bugfix that led to this test, the result would be wrong
when the local TZ was "America/Denver".
ensures that the env is cleaned up after testrun
@trey-stafford trey-stafford merged commit 48123cd into main Aug 16, 2024
1 check passed
@trey-stafford trey-stafford deleted the bugfix-transform branch August 16, 2024 15:46

@pytest.mark.parametrize("timezone", ["America/Denver", "UTC"])
def test__datetime_to_decimal_year(timezone, monkeypatch):
monkeypatch.setenv("TZ", timezone)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩 Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants