Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert TLE times (decimal days) to seconds after epoch

Tags:

python

time

The standard two line element (TLE) format contains times as 2-digit year plus decimal days, so 16012.375 would be January 12, 2016 at 09:00. Using python's time or datatime modules, how can I convert this to seconds after epoch? I think I should use structured time but I am not sure how. seconds_of is a fictitious function - need to replace with something real.

EDIT: It will be most helpful if the answer is long (verbose) - like one step per line or so, so I can understand what is happening.

EDIT 2: After seeing the comments from @J.F.Sebastian I looked at the link for TLE and found it nowhere states "UTC". So I should point out the initial information and final information are UTC. There is no reference to local time, time zone, or system time.

e.g.

tim = "16012.375"

year = 2000 + int(tim[0:2])
decimal_days = float(tim[2:])

print year, decimal_days

2016, 12.375

# seconds_of is a fictitious function - need to replace with something real
seconds_after_epoch = seconds_of(2016,1,1) + (3600. * 24.) * decimal_days
like image 479
uhoh Avatar asked Oct 25 '25 08:10

uhoh


2 Answers

You could try something like this [EDIT according to the comments].

import datetime
import time
# get year 2 digit and floating seconds days 
y_d, nbs = "16012.375".split('.') 

# parse to datetime (since midnight and add the seconds) %j Day of the year as a zero-padded decimal number.
d = datetime.datetime.strptime(y_d, "%y%j") + datetime.timedelta(seconds=float("." + nbs) * 24 * 60 * 60)
# 1.0 => 1 day
# from time tuple get epoch time. 
time.mktime(d.timetuple())

#1481896800.0
like image 131
Ali SAID OMAR Avatar answered Oct 27 '25 00:10

Ali SAID OMAR


It is easy to get datetime object given year and decimal_days:

>>> from datetime import datetime, timedelta
>>> year = 2016
>>> decimal_days = 12.375
>>> datetime(year, 1, 1) + timedelta(decimal_days - 1)
datetime.datetime(2016, 1, 12, 9, 0)

How to convert the datetime object into "seconds since epoch" depends on the timezone (local, utc, etc). See Converting datetime.date to UTC timestamp in Python e.g., if your input is in UTC then it is simple to get "seconds since the Epoch":

>>> utc_time = datetime(2016, 1, 12, 9, 0)
>>> (utc_time - datetime(1970, 1, 1)).total_seconds()
1452589200.0
like image 38
jfs Avatar answered Oct 26 '25 23:10

jfs