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

Add functions to convert from met to ET and UTC #1210

Conversation

subagonsouth
Copy link
Contributor

Change Summary

Overview

Adds functions for converting from Mission Elapsed Time (MET) to ET and UTC

Updated Files

  • imap_processing/cdf/utils.py
  • imap_processing/spice/time.py
    • Definition of J2000_EPOCH moved into the spice.time.py module
    • Extracted met_to_sclkticks function for reuse
    • Added met_to_utc function
    • Added met_to_datetime64 function with SPICE backed implementation.
  • imap_processing/tests/spice/test_time.py
    • Add test coverage for 3 new spice/time.py functions.
    • Don't import spiceypy as spice because it is confusing.

Closes: #1183

@subagonsouth subagonsouth added the SPICE Related to SPICE label Dec 6, 2024
@subagonsouth subagonsouth self-assigned this Dec 6, 2024
Copy link
Contributor

@nkerman nkerman left a comment

Choose a reason for hiding this comment

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

LGTM! Nice, thorough tests.

Will the fix to the 5 second just be replacing this constant J2000_EPOCH with a spice call?

@@ -13,6 +13,30 @@

TICK_DURATION = 2e-5 # 20 microseconds as defined in imap_sclk_0000.tsc

# Hard code the J2000 epoch. This allows for CDF epoch to be converted without
# use of SPICE though it should be noted that this results in a 5-second error
# due to the occurrence of 5 leap-seconds since the J2000 epoch.
Copy link
Contributor

Choose a reason for hiding this comment

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

Arguably a TODO should go here, since this is where the epoch is defined, and where the 5 leapsecond fix will go, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

True Nat. I meant to ask Tim if there is a ticket to revisit for this?

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 will add the TODO. Yes, there is a ticket for this. There are two ways to fix this each with a pro and con. See ticket: #1208

To clarify, this J2000EPOCH value is correct. The bug is in the code in the cdf/utils.py here:

  dt64 = J2000_EPOCH + dataset["epoch"].values[0].astype("timedelta64[ns]")
  start_time = np.datetime_as_string(dt64, unit="D").replace("-", "")

Copy link
Contributor

@tech3371 tech3371 left a comment

Choose a reason for hiding this comment

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

LGTM! I just had one minor doc string requests.

# use of SPICE though it should be noted that this results in a 5-second error
# due to the occurrence of 5 leap-seconds since the J2000 epoch.
# The UTC string was generated by:
# >>> spiceypy.et2utc(0, "ISOC", 9)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for these doc strings. Helps someone like me.

Returns
-------
numpy.ndarray[str]
The mission elapsed time converted to UTC string.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add an example about the format it returns in? If I understand correctly, ISOC format is something like YYYY-MM-DDTHH:MM:SSZ. Is that right? or is it like this 1987-04-12T16:31:12.814?

"""
if isinstance(met, typing.Iterable):
return np.asarray([np.datetime64(utc) for utc in met_to_utc(met)])
return np.datetime64(met_to_utc(met))
Copy link
Contributor

Choose a reason for hiding this comment

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

These two functions are gonna be so helpful! Thank you for adding those

dt64 = met_to_datetime64(met)
np.testing.assert_array_equal(
dt64.astype("datetime64[us]"), expected_dt64.astype("datetime64[us]")
)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

@subagonsouth
Copy link
Contributor Author

Will the fix to the 5 second just be replacing this constant J2000_EPOCH with a spice call?

No, actually, it is a bit more subtle. That epoch definition is correct in that it is the number of nanoseconds elapsed since the Unix epoch (00:00:00 UTC on 1 January 1970). The problem is that np.datetime64 does not take leap-seconds into account so when an offset is added to this epoch and then converted to a string, if the offset passed any leap-seconds, the string will be incorrect by the number of leap-seconds passed by adding the offset.

@subagonsouth subagonsouth merged commit aa2ecc4 into IMAP-Science-Operations-Center:dev Dec 9, 2024
17 checks passed
@subagonsouth subagonsouth deleted the 1183-spice-met-to-utc branch December 9, 2024 17:04
nkerman pushed a commit to nkerman/imap_processing that referenced this pull request Dec 18, 2024
…ons-Center#1210)

* Add functions to convert from met to ET and UTC

* Switch MAG and GLOWS imports to use spice.time definition of J2000_EPOCH

* PR feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SPICE Related to SPICE
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

SPICE - met to utc
3 participants