Hello,
I am using Orekit Python Wrapper for Orekit version v12.1.2 and I have noticed an unexpected behaviour when using the orekit.pyhelpers methods to map Python datetime objects to Orekit AbsoluteDate and then back to Python datetime objects. Below you can find an example and its output.
import datetime as dt
from orekit import pyhelpers
epoch = dt.datetime(2020, 1, 1, 12, 0, 59, 605000)
epoch_orekit = pyhelpers.datetime_to_absolutedate(epoch)
epoch_mapped_back = pyhelpers.absolutedate_to_datetime(epoch_orekit)
print(f"epoch: {epoch}")
print(f"epoch_orekit: {epoch_orekit}")
print(f"epoch_mapped_back: {epoch_mapped_back}")
print(f"Evaluation 1: {epoch == epoch_mapped_back}")
print(f"Evaluation 2: {epoch == epoch_mapped_back + dt.timedelta(microseconds=1)}")
As you can see from the output, I would have expected Evaluation 1 to evaluate to True and Evaluation 2 to evaluate to False. However, it seems that for some cases the mapping between Python datetime objects and Orekit AbsoluteDate is inaccurate by 1 microsecond. I did some investigation (I have also looked into this thread Datetime and Absolutedate conversions in python wrapper) and I think the cause of this behaviour is a casting performed in the orekit.pyhelpers.absolutedate_to_datetime() method.
Specifically, at line 142, rather than casting to the int type as
microseconds = int(1000000.0 * (seconds - math.floor(seconds)))
I would suggest rounding (or doing a similar operation) as
microseconds = round(1000000.0 * (seconds - math.floor(seconds)))
My questions are:
- Do you think this could improve the conversion accuracy in specific circumstances for version
12.1.2(in v13, thepyhelpershave a different implementation when mappingAbsoluteDateobjects todatetimeobjects, according to the discussion in Datetime and Absolutedate conversions in python wrapper)? - Is there a specific reason for your choice of using the casting to
inttype rather than the rounding? I am asking this because it could be that what I am suggesting may improve the accuracy in some scenarios and worsen it in others that you may have already considered.
Thanks in advance for your support!
Best,
Gianmario

