-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmjd2local.py
executable file
·68 lines (52 loc) · 2.06 KB
/
mjd2local.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
#!/usr/bin/env python3
"""
For a given MJD value or list of MJD values, return the range of local times
associated with that MJD.
"""
import sys
import pytz
import argparse
from datetime import datetime
from lsl.common.mcs import mjdmpm_to_datetime
from lsl.misc import parser as aph
MST = pytz.timezone('US/Mountain')
UTC = pytz.utc
def main(args):
otz = MST
if args.utc:
otz = UTC
if not args.pairs:
for arg in args.mjd:
mjd1 = int(arg)
mjd2 = float(mjd1) + 0.99999
d1 = mjdmpm_to_datetime(mjd1, 0)
d1 = UTC.localize(d1)
d1 = d1.astimezone(otz)
d2 = mjdmpm_to_datetime(mjd2, 0)
d2 = UTC.localize(d2)
d2 = d2.astimezone(otz)
tzname = d1.strftime('%Z')
print("MJD: %i" % mjd1)
print("%s: %s to %s" % (tzname, d1.strftime("%B %d, %Y at %H:%M:%S %Z"), d2.strftime("%B %d, %Y at %H:%M:%S %Z")))
else:
for arg in zip(args.mjd[0::2], args.mjd[1::2]):
mjd, mpm = [int(i) for i in arg]
d = mjdmpm_to_datetime(mjd, mpm)
d = UTC.localize(d)
d = d.astimezone(otz)
tzname = d.strftime('%Z')
print("MJD: %i, MPM: %i" % (mjd, mpm))
print("%s: %s" % (tzname, d.strftime("%B %d, %Y at %H:%M:%S %Z")))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='for a given MJD value or list of MJD values, return the range of local times associated with that MJD',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('mjd', type=aph.positive_int, nargs='+',
help='local date in YYYY/MM/DD')
parser.add_argument('-u', '--utc', action='store_true',
help='report times in UTC rather than local')
parser.add_argument('-p', '--pairs', action='store_true',
help='interpret the input as MJD, MPM pairs')
args = parser.parse_args()
main(args)