Skip to content

Commit b8f78c5

Browse files
committed
Add_time_utils
1 parent d3d5297 commit b8f78c5

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

kessler/cdm.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import datetime
1515
import copy
1616
import pandas as pd
17-
17+
from . import time_utils
1818
from . import util
1919

2020

@@ -191,11 +191,18 @@ def __eq__(self, other):
191191
if isinstance(other, ConjunctionDataMessage):
192192
return hash(self) == hash(other)
193193
return False
194+
195+
194196

195197
def set_header(self, key, value):
196198
if key in self._keys_header:
197199
if key in self._keys_with_dates:
198200
# We have a field with a date string as the value. Check if the string is in the format needed by the CCSDS 508.0-B-1 standard
201+
timeFormat = time_utils.getCcsdsTimeFormat(value)
202+
idx = timeFormat.find('DDD')
203+
if idx!=-1:
204+
[DateNum, DateVec] = time_utils.DOY2Date(value[idx:idx+3], value[0:4])
205+
value = str(DateVec[0]) +'-' + str(DateVec[1]) + '-' + str(DateVec[2]) + 'T' + value[idx+4:-1] #figure this out!!!
199206
try:
200207
_ = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
201208
except Exception as e:

kessler/time_utils.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import datetime
2+
def getCcsdsTimeFormat(timeString):
3+
4+
'''
5+
Adapted by Andrew Ng, 18/3/2022.
6+
Original MATLAB source code found at: https://github.com/nasa/CARA_Analysis_Tools/blob/master/two-dimension_Pc/Main/TransformationCode/TimeTransformations/getCcsdsTimeFormat.m
7+
The CCSDS time format is required to be of the general form
8+
9+
yyyy-[mm-dd|ddd]THH:MM:SS[.F*][Z]
10+
(1) The date and time fields are separated by a "T".
11+
(2) The date field has a four digit year followed by either a two digit
12+
month and two digit day, or a three digit day-of-year.
13+
(3) The year, month, day, and day-of-year fields are separated by a dash.
14+
(4) The hours, minutes and seconds fields are each two digits separated
15+
by colons.
16+
(5) The fraction of seconds is optional and can have any number of
17+
digits.
18+
(6) If a fraction of seconds is provided, it is separated from the two
19+
digit seconds by a period.
20+
(7) The time string can end with an optional "Z" time zone indicator
21+
'''
22+
timeFormat = []
23+
numT = timeString.count('T')
24+
if numT == -1:
25+
# Case when this is 'T' does not exist in timeString
26+
print(f"*** Error -- Invalid CCSDS time string: {timeString}")
27+
print(f" No 'T' separator found between date and time portions of the string\n")
28+
return
29+
elif numT > 1:
30+
print(f"*** Error -- Invalid CCSDS time string: {timeString} \n")
31+
print(f" More than one 'T' separator found between date and time portions of the string\n")
32+
return
33+
idxT = timeString.find('T')
34+
if idxT ==10:
35+
timeFormat = "yyyy-mm-ddTHH:MM:SS"
36+
elif idxT ==8:
37+
timeFormat = "yyyy-DDDTHH:MM:SS"
38+
else:
39+
print(f"*** Error -- Invalid CCSDS time string: {timeString} \n", timeString)
40+
print(f" Date format not one of yyyy-mm-dd or yyyy-DDD\n")
41+
return
42+
# % Check if 'Z' time zone indicator appended to the string
43+
if timeString[-1]=='Z':
44+
zOpt = True
45+
else:
46+
zOpt = False
47+
# % Find location of the fraction of seconds decimal separator
48+
numDecimal = timeString.count('.')
49+
if numDecimal > 1:
50+
print(f"*** Error -- Invalid CCSDS time string: {timeString} \n")
51+
print(f" More than one fraction of seconds decimal separator ('.') found.\n")
52+
timeFormat = []
53+
return
54+
idxDecimal = timeString.find('.')
55+
nfrac = 0
56+
if numDecimal != 0:
57+
if zOpt:
58+
nfrac = len(timeString) - 1 - idxDecimal -1
59+
else:
60+
nfrac = len(timeString) - 1 - idxDecimal
61+
if nfrac > 0:
62+
fracStr = '.' + ('F'*nfrac)
63+
else:
64+
fracStr = ""
65+
if zOpt:
66+
fracStr = fracStr+'Z'
67+
timeFormat = timeFormat+fracStr
68+
return timeFormat
69+
70+
def DOY2Date(DOY, YEAR):
71+
'''
72+
Written by Andrew Ng, 18/03/2022,
73+
Based on source code @ https://github.com/nasa/CARA_Analysis_Tools/blob/master/two-dimension_Pc/Main/TransformationCode/TimeTransformations/DOY2Date.m
74+
Use the datetime python package.
75+
DOY2DATE - Converts Day of Year (DOY) to date number and full
76+
calendar date.
77+
78+
'''
79+
# Calculate datetime format
80+
DateNum = datetime.datetime(int(YEAR), 1, 1) + datetime.timedelta(int(DOY) - 1)
81+
# Split datetime object into a date list
82+
DateVec = [DateNum.year, DateNum.month, DateNum.day, DateNum.hour, DateNum.minute]
83+
return DateNum, DateVec

0 commit comments

Comments
 (0)