Skip to content

Commit

Permalink
MNT: Ensure float dtype conversion to avoid int32 overflow in met con…
Browse files Browse the repository at this point in the history
…version
  • Loading branch information
greglucas committed Jun 27, 2024
1 parent a1ff70a commit 9bb5208
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion imap_processing/cdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def met_to_j2000ns(
# float input and we want to keep ns precision in those floats
# NOTE: We need int64 here when running on 32bit systems as plain int will default
# to 32bit and overflow due to the nanosecond multiplication
time_array = (np.asarray(met) * 1e9).astype(np.int64)
time_array = (np.asarray(met, dtype=float) * 1e9).astype(np.int64)
# Calculate the time difference between our reference system and J2000
j2000_offset = (
(reference_epoch - J2000_EPOCH).astype("timedelta64[ns]").astype(np.int64)
Expand Down
3 changes: 3 additions & 0 deletions imap_processing/tests/cdf/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def test_met_to_j2000ns():
imap_epoch_offset = (IMAP_EPOCH - J2000_EPOCH).astype(np.int64)
assert met_to_j2000ns(0) == imap_epoch_offset
assert met_to_j2000ns(1) == imap_epoch_offset + 1e9
# Large input should work (avoid overflow with int32 SHCOARSE inputs)
assert met_to_j2000ns(np.int32(2**30)) == imap_epoch_offset + 2**30 * 1e9
assert met_to_j2000ns(0).dtype == np.int64
# Float input should work
assert met_to_j2000ns(0.0) == imap_epoch_offset
assert met_to_j2000ns(1.2) == imap_epoch_offset + 1.2e9
Expand Down

0 comments on commit 9bb5208

Please sign in to comment.