Skip to content

Commit 3f12a17

Browse files
committed
Moved CLI datetime methods to their own module.
1 parent 9c4136b commit 3f12a17

File tree

3 files changed

+143
-137
lines changed

3 files changed

+143
-137
lines changed

cli_lib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/*

cli_lib/pa_cli_datetime.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import json
2+
from dateutil.parser import parse
3+
import lib.pa_datetime as PA_DT
4+
5+
def doe_year(year):
6+
'''
7+
Calculate date of Easter for a given year.
8+
'''
9+
easter_month,easter_day,easter_year = PA_DT.get_date_of_easter(year)
10+
easter_dict = dict(easterMonth=easter_month, easterDay=easter_day, easterYear=easter_year)
11+
12+
print(json.dumps(easter_dict))
13+
14+
def cd_to_dn(date_string):
15+
'''
16+
Convert civil date to day number.
17+
'''
18+
dt = parse(date_string)
19+
day_number = PA_DT.civil_date_to_day_number(dt.month, dt.day, dt.year)
20+
day_num_dict = dict(dayNumber=day_number)
21+
22+
print(json.dumps(day_num_dict))
23+
24+
def gd_to_jd(greenwich_date):
25+
'''
26+
Convert Greenwich date to Julian date.
27+
'''
28+
dt_split = greenwich_date.split("/")
29+
30+
julian_date = PA_DT.greenwich_date_to_julian_date(float(dt_split[1]), int(dt_split[0]), int(dt_split[2]))
31+
julian_date_dict = dict(julianDate=julian_date)
32+
33+
print(json.dumps(julian_date_dict))
34+
35+
def jd_to_gd(julian_date):
36+
'''
37+
Convert Julian date to Greenwich date.
38+
'''
39+
day,month,year = PA_DT.julian_date_to_greenwich_date(julian_date)
40+
greenwich_date_dict = dict(greenwichDay=day, greenwichMonth=month, greenwichYear=year)
41+
42+
print(json.dumps(greenwich_date_dict))
43+
44+
def ct_to_dh(civil_time):
45+
'''
46+
Convert civil time to decimal hours.
47+
'''
48+
ct_split = civil_time.split(":")
49+
50+
decimal_hours = PA_DT.civil_time_to_decimal_hours(int(ct_split[0]), int(ct_split[1]), int(ct_split[2]))
51+
decimal_hours = round(decimal_hours,8)
52+
decimal_hours_dict = dict(decimalHours=decimal_hours)
53+
54+
print(json.dumps(decimal_hours_dict))
55+
56+
def dh_to_ct(decimal_hours):
57+
'''
58+
Convert decimal hours to civil time.
59+
'''
60+
hours,minutes,seconds = PA_DT.decimal_hours_to_civil_time(decimal_hours)
61+
civil_time_dict = dict(civilTimeHours=hours, civilTimeMinutes=minutes, civilTimeSeconds=seconds)
62+
63+
print(json.dumps(civil_time_dict))
64+
65+
def lct_to_ut(civil_date, civil_time, is_dst, zone_correction):
66+
'''
67+
Convert local civil time to universal time.
68+
'''
69+
cd_split = civil_date.split("/")
70+
ct_split = civil_time.split(":")
71+
ut_hours,ut_minutes,ut_seconds,gw_day,gw_month,gw_year = PA_DT.local_civil_time_to_universal_time(int(ct_split[0]), int(ct_split[1]), int(ct_split[2]), is_dst, zone_correction, int(cd_split[1]), int(cd_split[0]), int(cd_split[2]))
72+
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)
73+
74+
print(json.dumps(ut_dict))
75+
76+
def ut_to_lct(civil_date, universal_time, is_dst, zone_correction):
77+
'''
78+
Convert universal time to local civil time.
79+
'''
80+
cd_split = civil_date.split("/")
81+
ut_split = universal_time.split(":")
82+
lct_hours,lct_minutes,lct_seconds,gw_day,gw_month,gw_year = PA_DT.universal_time_to_local_civil_time(int(ut_split[0]), int(ut_split[1]), int(ut_split[2]), is_dst, zone_correction, int(cd_split[1]), int(cd_split[0]), int(cd_split[2]))
83+
lct_dict = dict(lctHours=lct_hours,lctMinutes=lct_minutes,lctSeconds=lct_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)
84+
85+
print(json.dumps(lct_dict))
86+
87+
def ut_to_gst(universal_time, greenwich_date):
88+
'''
89+
Convert Universal time to Greenwich sidereal time
90+
'''
91+
ut_split = universal_time.split(":")
92+
gd_split = greenwich_date.split("/")
93+
gst_hours,gst_minutes,gst_seconds = PA_DT.universal_time_to_greenwich_sidereal_time(int(ut_split[0]), int(ut_split[1]), float(ut_split[2]), int(gd_split[1]), int(gd_split[0]), int(gd_split[2]))
94+
gst_dict = dict(greenwichSiderealTimeHours=gst_hours, greenwichSiderealTimeMinutes=gst_minutes, greenwichSiderealTimeSeconds=gst_seconds)
95+
96+
print(json.dumps(gst_dict))
97+
98+
def gst_to_ut(greenwich_sidereal_time, greenwich_date):
99+
'''
100+
Convert Greenwich sidereal time to universal time
101+
'''
102+
gst_split = greenwich_sidereal_time.split(":")
103+
gd_split = greenwich_date.split("/")
104+
ut_hours,ut_minutes,ut_seconds,status_message = PA_DT.greenwich_sidereal_time_to_universal_time(int(gst_split[0]),int(gst_split[1]),float(gst_split[2]),int(gd_split[1]),int(gd_split[0]),int(gd_split[2]))
105+
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,statusMessage=status_message)
106+
107+
print(json.dumps(ut_dict))
108+
109+
def gst_to_lst(greenwich_sidereal_time, geographical_longitude):
110+
'''
111+
Convert Greenwich sidereal time to local sidereal time
112+
'''
113+
gst_split = greenwich_sidereal_time.split(":")
114+
lst_hours,lst_minutes,lst_seconds = PA_DT.greenwich_sidereal_time_to_local_sidereal_time(int(gst_split[0]), int(gst_split[1]), float(gst_split[2]), geographical_longitude)
115+
lst_dict = dict(lstHours=lst_hours,lstMinutes=lst_minutes,lstSeconds=lst_seconds)
116+
117+
print(json.dumps(lst_dict))
118+
119+
def lst_to_gst(local_sidereal_time, geographical_longitude):
120+
'''
121+
Convert local sidereal time to Greenwich sidereal time
122+
'''
123+
lst_split = local_sidereal_time.split(":")
124+
gst_hours,gst_minutes,gst_seconds = PA_DT.local_sidereal_time_to_greenwich_sidereal_time(int(lst_split[0]),int(lst_split[1]),float(lst_split[2]),geographical_longitude)
125+
gst_dict = dict(gstHours=gst_hours,gstMinutes=gst_minutes,gstSeconds=gst_seconds)
126+
127+
print(json.dumps(gst_dict))

pa-cli.py

Lines changed: 15 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import argparse
44
import json
5-
import lib.pa_datetime as PA_DT
65
from dateutil.parser import parse
6+
import lib.pa_datetime as PA_DT
7+
import cli_lib.pa_cli_datetime as PA_CLI_DT
78

89
def display_error(error_description):
910
'''
@@ -13,130 +14,6 @@ def display_error(error_description):
1314

1415
print(json.dumps(error_dict))
1516

16-
def doe_year(year):
17-
'''
18-
Calculate date of Easter for a given year.
19-
'''
20-
easter_month,easter_day,easter_year = PA_DT.get_date_of_easter(year)
21-
easter_dict = dict(easterMonth=easter_month, easterDay=easter_day, easterYear=easter_year)
22-
23-
print(json.dumps(easter_dict))
24-
25-
def cd_to_dn(date_string):
26-
'''
27-
Convert civil date to day number.
28-
'''
29-
dt = parse(date_string)
30-
day_number = PA_DT.civil_date_to_day_number(dt.month, dt.day, dt.year)
31-
day_num_dict = dict(dayNumber=day_number)
32-
33-
print(json.dumps(day_num_dict))
34-
35-
def gd_to_jd(greenwich_date):
36-
'''
37-
Convert Greenwich date to Julian date.
38-
'''
39-
dt_split = greenwich_date.split("/")
40-
41-
julian_date = PA_DT.greenwich_date_to_julian_date(float(dt_split[1]), int(dt_split[0]), int(dt_split[2]))
42-
julian_date_dict = dict(julianDate=julian_date)
43-
44-
print(json.dumps(julian_date_dict))
45-
46-
def jd_to_gd(julian_date):
47-
'''
48-
Convert Julian date to Greenwich date.
49-
'''
50-
day,month,year = PA_DT.julian_date_to_greenwich_date(julian_date)
51-
greenwich_date_dict = dict(greenwichDay=day, greenwichMonth=month, greenwichYear=year)
52-
53-
print(json.dumps(greenwich_date_dict))
54-
55-
def ct_to_dh(civil_time):
56-
'''
57-
Convert civil time to decimal hours.
58-
'''
59-
ct_split = civil_time.split(":")
60-
61-
decimal_hours = PA_DT.civil_time_to_decimal_hours(int(ct_split[0]), int(ct_split[1]), int(ct_split[2]))
62-
decimal_hours = round(decimal_hours,8)
63-
decimal_hours_dict = dict(decimalHours=decimal_hours)
64-
65-
print(json.dumps(decimal_hours_dict))
66-
67-
def dh_to_ct(decimal_hours):
68-
'''
69-
Convert decimal hours to civil time.
70-
'''
71-
hours,minutes,seconds = PA_DT.decimal_hours_to_civil_time(decimal_hours)
72-
civil_time_dict = dict(civilTimeHours=hours, civilTimeMinutes=minutes, civilTimeSeconds=seconds)
73-
74-
print(json.dumps(civil_time_dict))
75-
76-
def lct_to_ut(civil_date, civil_time, is_dst, zone_correction):
77-
'''
78-
Convert local civil time to universal time.
79-
'''
80-
cd_split = civil_date.split("/")
81-
ct_split = civil_time.split(":")
82-
ut_hours,ut_minutes,ut_seconds,gw_day,gw_month,gw_year = PA_DT.local_civil_time_to_universal_time(int(ct_split[0]), int(ct_split[1]), int(ct_split[2]), is_dst, zone_correction, int(cd_split[1]), int(cd_split[0]), int(cd_split[2]))
83-
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)
84-
85-
print(json.dumps(ut_dict))
86-
87-
def ut_to_lct(civil_date, universal_time, is_dst, zone_correction):
88-
'''
89-
Convert universal time to local civil time.
90-
'''
91-
cd_split = civil_date.split("/")
92-
ut_split = universal_time.split(":")
93-
lct_hours,lct_minutes,lct_seconds,gw_day,gw_month,gw_year = PA_DT.universal_time_to_local_civil_time(int(ut_split[0]), int(ut_split[1]), int(ut_split[2]), is_dst, zone_correction, int(cd_split[1]), int(cd_split[0]), int(cd_split[2]))
94-
lct_dict = dict(lctHours=lct_hours,lctMinutes=lct_minutes,lctSeconds=lct_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)
95-
96-
print(json.dumps(lct_dict))
97-
98-
def ut_to_gst(universal_time, greenwich_date):
99-
'''
100-
Convert Universal time to Greenwich sidereal time
101-
'''
102-
ut_split = universal_time.split(":")
103-
gd_split = greenwich_date.split("/")
104-
gst_hours,gst_minutes,gst_seconds = PA_DT.universal_time_to_greenwich_sidereal_time(int(ut_split[0]), int(ut_split[1]), float(ut_split[2]), int(gd_split[1]), int(gd_split[0]), int(gd_split[2]))
105-
gst_dict = dict(greenwichSiderealTimeHours=gst_hours, greenwichSiderealTimeMinutes=gst_minutes, greenwichSiderealTimeSeconds=gst_seconds)
106-
107-
print(json.dumps(gst_dict))
108-
109-
def gst_to_ut(greenwich_sidereal_time, greenwich_date):
110-
'''
111-
Convert Greenwich sidereal time to universal time
112-
'''
113-
gst_split = greenwich_sidereal_time.split(":")
114-
gd_split = greenwich_date.split("/")
115-
ut_hours,ut_minutes,ut_seconds,status_message = PA_DT.greenwich_sidereal_time_to_universal_time(int(gst_split[0]),int(gst_split[1]),float(gst_split[2]),int(gd_split[1]),int(gd_split[0]),int(gd_split[2]))
116-
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,statusMessage=status_message)
117-
118-
print(json.dumps(ut_dict))
119-
120-
def gst_to_lst(greenwich_sidereal_time, geographical_longitude):
121-
'''
122-
Convert Greenwich sidereal time to local sidereal time
123-
'''
124-
gst_split = greenwich_sidereal_time.split(":")
125-
lst_hours,lst_minutes,lst_seconds = PA_DT.greenwich_sidereal_time_to_local_sidereal_time(int(gst_split[0]), int(gst_split[1]), float(gst_split[2]), geographical_longitude)
126-
lst_dict = dict(lstHours=lst_hours,lstMinutes=lst_minutes,lstSeconds=lst_seconds)
127-
128-
print(json.dumps(lst_dict))
129-
130-
def lst_to_gst(local_sidereal_time, geographical_longitude):
131-
'''
132-
Convert local sidereal time to Greenwich sidereal time
133-
'''
134-
lst_split = local_sidereal_time.split(":")
135-
gst_hours,gst_minutes,gst_seconds = PA_DT.local_sidereal_time_to_greenwich_sidereal_time(int(lst_split[0]),int(lst_split[1]),float(lst_split[2]),geographical_longitude)
136-
gst_dict = dict(gstHours=gst_hours,gstMinutes=gst_minutes,gstSeconds=gst_seconds)
137-
138-
print(json.dumps(gst_dict))
139-
14017
def main():
14118
parser = argparse.ArgumentParser(description='Practical Astronomy CLI.')
14219

@@ -183,37 +60,38 @@ def main():
18360
if not args.year:
18461
display_error("'year' argument is required.")
18562
else:
186-
doe_year(args.year)
63+
PA_CLI_DT.doe_year(args.year)
64+
#doe_year(args.year)
18765

18866
if args.cd_to_dn:
18967
if not args.cd:
19068
display_error("'cd' argument is required.")
19169
else:
192-
cd_to_dn(args.cd)
70+
PA_CLI_DT.cd_to_dn(args.cd)
19371

19472
if args.gd_to_jd:
19573
if not args.gd:
19674
display_error("'gd' argument is required.")
19775
else:
198-
gd_to_jd(args.gd)
76+
PA_CLI_DT.gd_to_jd(args.gd)
19977

20078
if args.jd_to_gd:
20179
if not args.jd:
20280
display_error("'jd' argument is required.")
20381
else:
204-
jd_to_gd(args.jd)
82+
PA_CLI_DT.jd_to_gd(args.jd)
20583

20684
if args.ct_to_dh:
20785
if not args.ct:
20886
display_error("'ct' argument is required.")
20987
else:
210-
ct_to_dh(args.ct)
88+
PA_CLI_DT.ct_to_dh(args.ct)
21189

21290
if args.dh_to_ct:
21391
if not args.dh:
21492
display_error("'dh' argument is required.")
21593
else:
216-
dh_to_ct(args.dh)
94+
PA_CLI_DT.dh_to_ct(args.dh)
21795

21896
if args.lct_to_ut:
21997
if not args.cd:
@@ -223,7 +101,7 @@ def main():
223101
elif not args.zc:
224102
display_error("'zc', argument is required.")
225103
else:
226-
lct_to_ut(args.cd, args.ct, args.is_daylight_savings, args.zc)
104+
PA_CLI_DT.lct_to_ut(args.cd, args.ct, args.is_daylight_savings, args.zc)
227105

228106
if args.ut_to_lct:
229107
if not args.cd:
@@ -233,39 +111,39 @@ def main():
233111
elif not args.zc:
234112
display_error("'zc', argument is required.")
235113
else:
236-
ut_to_lct(args.cd, args.ut, args.is_daylight_savings, args.zc)
114+
PA_CLI_DT.ut_to_lct(args.cd, args.ut, args.is_daylight_savings, args.zc)
237115

238116
if args.ut_to_gst:
239117
if not args.ut:
240118
display_error("'ut' argument is required.")
241119
elif not args.gd:
242120
display_error("'gd' argument is required.")
243121
else:
244-
ut_to_gst(args.ut, args.gd)
122+
PA_CLI_DT.ut_to_gst(args.ut, args.gd)
245123

246124
if args.gst_to_ut:
247125
if not args.gst:
248126
display_error("'gst' argument is required.")
249127
elif not args.gd:
250128
display_error("'gd' argument is required.")
251129
else:
252-
gst_to_ut(args.gst, args.gd)
130+
PA_CLI_DT.gst_to_ut(args.gst, args.gd)
253131

254132
if args.gst_to_lst:
255133
if not args.gst:
256134
display_error("'gst' argument is required.")
257135
elif not args.gl:
258136
display_error("'gl' argument is required.")
259137
else:
260-
gst_to_lst(args.gst, args.gl)
138+
PA_CLI_DT.gst_to_lst(args.gst, args.gl)
261139

262140
if args.lst_to_gst:
263141
if not args.lst:
264142
display_error("'lst' argument is required.")
265143
elif not args.gl:
266144
display_error("'gl' argument is required.")
267145
else:
268-
lst_to_gst(args.lst, args.gl)
146+
PA_CLI_DT.lst_to_gst(args.lst, args.gl)
269147

270148
if __name__ == "__main__":
271149
main()

0 commit comments

Comments
 (0)