-
Notifications
You must be signed in to change notification settings - Fork 1
/
gnsscal.py
272 lines (227 loc) · 7.76 KB
/
gnsscal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python
# coding=utf-8
# creater: Jiang Yingming
# date: 2017-05-17
# BSD 3-Clause License
# Copyright (c) 2017, Jon Jiang
# All rights reserved.
"""Switch Gregorian date to day of year, GNSS calendar (GPS & BDS)."""
from __future__ import print_function
from __future__ import division
import datetime
__all__ = [
'GPS_START_DATE', 'BDS_START_DATE',
'bdswd2date', 'bdswd2yrdoy',
'date2bdswd', 'date2doy', 'date2gpswd', 'date2yrdoy',
'gpswd2date', 'gpswd2yrdoy','gpsws2date',
'yrdoy2bdswd', 'yrdoy2date', 'yrdoy2gpswd'
]
GPS_START_DATE = datetime.date(1980, 1, 6)
BDS_START_DATE = datetime.date(2006, 1, 1)
def __date2weeksday(date, start):
"""Convert date to spent weeks and day of week from a start date."""
delta = date - start
if delta.days >= 0:
weeks = delta.days // 7
dayofweek = delta.days % 7
return weeks, dayofweek
else:
raise ValueError('Invalid date: %s, too early.' %date)
def __weeksday2date(weeks, dayofweek, start):
"""Convert spent weeks and day of week to date."""
if weeks < 0 or dayofweek < 0 or dayofweek > 6:
raise ValueError('Invalid week or day: %s, %s' %(weeks, dayofweek))
delta = datetime.timedelta(weeks=weeks, days=dayofweek)
return start + delta
def date2doy(date):
"""Convert date to day of year, return int doy.
Example:
>>> from datetime import date
>>> date2doy(date(2017, 5, 17))
137
"""
first_day = datetime.date(date.year, 1, 1)
delta = date - first_day
return delta.days + 1
def date2yrdoy(date):
"""Convert date to year and day of year, return int tuple (year, doy).
Example:
>>> from datetime import date
>>> date2yrdoy(date(2017, 5, 17))
(2017, 137)
"""
return date.year, date2doy(date)
def yrdoy2date(year, doy):
"""Convert year and day of year to date, return datetime.date.
Example:
>>> yrdoy2date(2017, 137)
datetime.date(2017, 5, 17)
"""
if year < 0 or doy < 1:
raise ValueError('Invalid year or day of year: %s, %s' %(year, doy))
if year < 100:
year = year + 2000 if year < 80 else year + 1900
first_day = datetime.date(year, 1, 1)
delta = datetime.timedelta(days=doy-1)
return first_day + delta
def date2gpswd(date):
"""Convert date to GPS week and day of week, return int tuple (week, day).
Example:
>>> from datetime import date
>>> date2gpswd(date(2017, 5, 17))
(1949, 3)
>>> date2gpswd(date(1917, 5, 17))
Traceback (most recent call last):
...
ValueError: Invalid date: 1917-05-17, too early.
"""
return __date2weeksday(date, GPS_START_DATE)
def date2bdswd(date):
"""Convert date to BDS week and day of week, return int tuple (week, day).
Example:
>>> from datetime import date
>>> date2bdswd(date(2017, 5, 17))
(593, 3)
>>> date2bdswd(date(2003, 1, 17))
Traceback (most recent call last):
...
ValueError: Invalid date: 2003-01-17, too early.
"""
return __date2weeksday(date, BDS_START_DATE)
def gpswd2date(gpsweek, dayofweek):
"""Convert GPS week and day of week to date, return datetime.date
Example:
>>> gpswd2date(1949, 3)
datetime.date(2017, 5, 17)
>>> gpswd2date(1949, 8)
Traceback (most recent call last):
...
ValueError: Invalid week or day: 1949, 8
"""
return __weeksday2date(gpsweek, dayofweek, GPS_START_DATE)
def bdswd2date(bdsweek, dayofweek):
"""Convert BDS week and day of week to date, return datetime.date
Example:
>>> bdswd2date(593, 3)
datetime.date(2017, 5, 17)
>>> bdswd2date(-1, 3)
Traceback (most recent call last):
...
ValueError: Invalid week or day: -1, 3
"""
return __weeksday2date(bdsweek, dayofweek, BDS_START_DATE)
def yrdoy2gpswd(year, doy):
"""Convert year and day of year to GPS week and day of week,
return int tuple (week, day).
Example:
>>> yrdoy2gpswd(2017, 137)
(1949, 3)
>>> yrdoy2gpswd(1937, 147)
Traceback (most recent call last):
...
ValueError: Invalid date: 1937-05-27, too early.
"""
date = yrdoy2date(year, doy)
return date2gpswd(date)
def yrdoy2bdswd(year, doy):
"""Convert year and day of year to BDS week and day of week,
return int tuple (week, day).
Example:
>>> yrdoy2bdswd(2017, 137)
(593, 3)
"""
date = yrdoy2date(year, doy)
return date2bdswd(date)
def gpswd2yrdoy(gpsweek, dayofweek):
"""Convert GPS week and day of week to year and day of year,
return int tuple (year, doy).
Example:
>>> gpswd2yrdoy(1949, 3)
(2017, 137)
"""
date = gpswd2date(gpsweek, dayofweek)
return date2yrdoy(date)
def bdswd2yrdoy(bdsweek, dayofweek):
"""Convert GPS week and day of week to year and day of year,
return int tuple (year, doy).
Example:
>>> bdswd2yrdoy(593, 3)
(2017, 137)
"""
date = bdswd2date(bdsweek, dayofweek)
return date2yrdoy(date)
def gpsw2bdsw(gpsweek):
"""Convert GPS week to BDS week, return int bdsweek.
Example:
>>> gpsw2bdsw(1949)
593
"""
date = gpswd2date(gpsweek, 0)
return date2bdswd(date)[0]
def bdsw2gpsw(bdsweek):
"""Convert BDS week to GPS week, return int gpsweek.
Example:
>>> bdsw2gpsw(593)
1949
"""
date = bdswd2date(bdsweek, 0)
return date2gpswd(date)[0]
def gpsws2date(week, seconds):
"""Convert GPS week and Seconds of GPS Week to date, return datetime.datetime.
Example:
>>> gpsws2date(2103, 385914)
datetime.datetime(2020, 4, 30, 11, 11, 54)
"""
start = datetime.datetime(year= 1980, month= 1, day =6)
totals = datetime.timedelta(seconds = week*7*24*3600 + seconds)
day = start + totals
return day
def __handle_cmd(args):
"""Handle command line arguments."""
# Calculate Gregorian date from arguments
if args.date:
date = datetime.date(*args.date)
elif args.ydoy:
date = yrdoy2date(*args.ydoy)
elif args.gpswd:
date = gpswd2date(*args.gpswd)
elif args.bdswd:
date = bdswd2date(*args.bdswd)
else:
date = datetime.date.today()
print('Gregorian date: %s' %date.strftime('%Y-%m-%d'))
# Convert date into doy, GPS week and BDS week
doy = date2doy(date)
print('year, doy: %d, %03d' %(date.year, doy))
if date >= GPS_START_DATE:
gpsw, dow = date2gpswd(date)
print('GPS week: %04d, %d' %(gpsw, dow))
else:
print('GPS week: date too early!')
if date >= BDS_START_DATE:
bdsw, _ = date2bdswd(date)
print('BDS week: %04d, %d' %(bdsw, dow))
else:
print('BDS week: date too early!')
return 0
def __init_args():
"""Parse user input from command line."""
import argparse
# initilize a argument parser
parser = argparse.ArgumentParser(
description='Convert Gregorian date to GNSS calendar, or vice versa.'
)
# add arguments
parser.add_argument('-v', '--version', action='version',
version='%(prog)s 1.1.0')
parser.add_argument('-date', metavar='<n>', nargs=3, type=int,
help='year, month, day')
parser.add_argument('-ydoy', metavar='<n>', nargs=2, type=int,
help='year, day of year')
parser.add_argument('-gpswd', metavar='<n>', nargs=2, type=int,
help='GPS week, day of week')
parser.add_argument('-bdswd', metavar='<n>', nargs=2, type=int,
help='BDS week, day of week')
return __handle_cmd(parser.parse_args())
if __name__ == '__main__':
__init_args()