Skip to content

Commit

Permalink
Moved CLI datetime methods to their own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcarr committed Sep 14, 2020
1 parent 9c4136b commit 3f12a17
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 137 deletions.
1 change: 1 addition & 0 deletions cli_lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/*
127 changes: 127 additions & 0 deletions cli_lib/pa_cli_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import json
from dateutil.parser import parse
import lib.pa_datetime as PA_DT

def doe_year(year):
'''
Calculate date of Easter for a given year.
'''
easter_month,easter_day,easter_year = PA_DT.get_date_of_easter(year)
easter_dict = dict(easterMonth=easter_month, easterDay=easter_day, easterYear=easter_year)

print(json.dumps(easter_dict))

def cd_to_dn(date_string):
'''
Convert civil date to day number.
'''
dt = parse(date_string)
day_number = PA_DT.civil_date_to_day_number(dt.month, dt.day, dt.year)
day_num_dict = dict(dayNumber=day_number)

print(json.dumps(day_num_dict))

def gd_to_jd(greenwich_date):
'''
Convert Greenwich date to Julian date.
'''
dt_split = greenwich_date.split("/")

julian_date = PA_DT.greenwich_date_to_julian_date(float(dt_split[1]), int(dt_split[0]), int(dt_split[2]))
julian_date_dict = dict(julianDate=julian_date)

print(json.dumps(julian_date_dict))

def jd_to_gd(julian_date):
'''
Convert Julian date to Greenwich date.
'''
day,month,year = PA_DT.julian_date_to_greenwich_date(julian_date)
greenwich_date_dict = dict(greenwichDay=day, greenwichMonth=month, greenwichYear=year)

print(json.dumps(greenwich_date_dict))

def ct_to_dh(civil_time):
'''
Convert civil time to decimal hours.
'''
ct_split = civil_time.split(":")

decimal_hours = PA_DT.civil_time_to_decimal_hours(int(ct_split[0]), int(ct_split[1]), int(ct_split[2]))
decimal_hours = round(decimal_hours,8)
decimal_hours_dict = dict(decimalHours=decimal_hours)

print(json.dumps(decimal_hours_dict))

def dh_to_ct(decimal_hours):
'''
Convert decimal hours to civil time.
'''
hours,minutes,seconds = PA_DT.decimal_hours_to_civil_time(decimal_hours)
civil_time_dict = dict(civilTimeHours=hours, civilTimeMinutes=minutes, civilTimeSeconds=seconds)

print(json.dumps(civil_time_dict))

def lct_to_ut(civil_date, civil_time, is_dst, zone_correction):
'''
Convert local civil time to universal time.
'''
cd_split = civil_date.split("/")
ct_split = civil_time.split(":")
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]))
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)

print(json.dumps(ut_dict))

def ut_to_lct(civil_date, universal_time, is_dst, zone_correction):
'''
Convert universal time to local civil time.
'''
cd_split = civil_date.split("/")
ut_split = universal_time.split(":")
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]))
lct_dict = dict(lctHours=lct_hours,lctMinutes=lct_minutes,lctSeconds=lct_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)

print(json.dumps(lct_dict))

def ut_to_gst(universal_time, greenwich_date):
'''
Convert Universal time to Greenwich sidereal time
'''
ut_split = universal_time.split(":")
gd_split = greenwich_date.split("/")
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]))
gst_dict = dict(greenwichSiderealTimeHours=gst_hours, greenwichSiderealTimeMinutes=gst_minutes, greenwichSiderealTimeSeconds=gst_seconds)

print(json.dumps(gst_dict))

def gst_to_ut(greenwich_sidereal_time, greenwich_date):
'''
Convert Greenwich sidereal time to universal time
'''
gst_split = greenwich_sidereal_time.split(":")
gd_split = greenwich_date.split("/")
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]))
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,statusMessage=status_message)

print(json.dumps(ut_dict))

def gst_to_lst(greenwich_sidereal_time, geographical_longitude):
'''
Convert Greenwich sidereal time to local sidereal time
'''
gst_split = greenwich_sidereal_time.split(":")
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)
lst_dict = dict(lstHours=lst_hours,lstMinutes=lst_minutes,lstSeconds=lst_seconds)

print(json.dumps(lst_dict))

def lst_to_gst(local_sidereal_time, geographical_longitude):
'''
Convert local sidereal time to Greenwich sidereal time
'''
lst_split = local_sidereal_time.split(":")
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)
gst_dict = dict(gstHours=gst_hours,gstMinutes=gst_minutes,gstSeconds=gst_seconds)

print(json.dumps(gst_dict))
152 changes: 15 additions & 137 deletions pa-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import argparse
import json
import lib.pa_datetime as PA_DT
from dateutil.parser import parse
import lib.pa_datetime as PA_DT
import cli_lib.pa_cli_datetime as PA_CLI_DT

def display_error(error_description):
'''
Expand All @@ -13,130 +14,6 @@ def display_error(error_description):

print(json.dumps(error_dict))

def doe_year(year):
'''
Calculate date of Easter for a given year.
'''
easter_month,easter_day,easter_year = PA_DT.get_date_of_easter(year)
easter_dict = dict(easterMonth=easter_month, easterDay=easter_day, easterYear=easter_year)

print(json.dumps(easter_dict))

def cd_to_dn(date_string):
'''
Convert civil date to day number.
'''
dt = parse(date_string)
day_number = PA_DT.civil_date_to_day_number(dt.month, dt.day, dt.year)
day_num_dict = dict(dayNumber=day_number)

print(json.dumps(day_num_dict))

def gd_to_jd(greenwich_date):
'''
Convert Greenwich date to Julian date.
'''
dt_split = greenwich_date.split("/")

julian_date = PA_DT.greenwich_date_to_julian_date(float(dt_split[1]), int(dt_split[0]), int(dt_split[2]))
julian_date_dict = dict(julianDate=julian_date)

print(json.dumps(julian_date_dict))

def jd_to_gd(julian_date):
'''
Convert Julian date to Greenwich date.
'''
day,month,year = PA_DT.julian_date_to_greenwich_date(julian_date)
greenwich_date_dict = dict(greenwichDay=day, greenwichMonth=month, greenwichYear=year)

print(json.dumps(greenwich_date_dict))

def ct_to_dh(civil_time):
'''
Convert civil time to decimal hours.
'''
ct_split = civil_time.split(":")

decimal_hours = PA_DT.civil_time_to_decimal_hours(int(ct_split[0]), int(ct_split[1]), int(ct_split[2]))
decimal_hours = round(decimal_hours,8)
decimal_hours_dict = dict(decimalHours=decimal_hours)

print(json.dumps(decimal_hours_dict))

def dh_to_ct(decimal_hours):
'''
Convert decimal hours to civil time.
'''
hours,minutes,seconds = PA_DT.decimal_hours_to_civil_time(decimal_hours)
civil_time_dict = dict(civilTimeHours=hours, civilTimeMinutes=minutes, civilTimeSeconds=seconds)

print(json.dumps(civil_time_dict))

def lct_to_ut(civil_date, civil_time, is_dst, zone_correction):
'''
Convert local civil time to universal time.
'''
cd_split = civil_date.split("/")
ct_split = civil_time.split(":")
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]))
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)

print(json.dumps(ut_dict))

def ut_to_lct(civil_date, universal_time, is_dst, zone_correction):
'''
Convert universal time to local civil time.
'''
cd_split = civil_date.split("/")
ut_split = universal_time.split(":")
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]))
lct_dict = dict(lctHours=lct_hours,lctMinutes=lct_minutes,lctSeconds=lct_seconds,greenwichDay=gw_day,greenwichMonth=gw_month,greenwichYear=gw_year)

print(json.dumps(lct_dict))

def ut_to_gst(universal_time, greenwich_date):
'''
Convert Universal time to Greenwich sidereal time
'''
ut_split = universal_time.split(":")
gd_split = greenwich_date.split("/")
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]))
gst_dict = dict(greenwichSiderealTimeHours=gst_hours, greenwichSiderealTimeMinutes=gst_minutes, greenwichSiderealTimeSeconds=gst_seconds)

print(json.dumps(gst_dict))

def gst_to_ut(greenwich_sidereal_time, greenwich_date):
'''
Convert Greenwich sidereal time to universal time
'''
gst_split = greenwich_sidereal_time.split(":")
gd_split = greenwich_date.split("/")
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]))
ut_dict = dict(utHours=ut_hours,utMinutes=ut_minutes,utSeconds=ut_seconds,statusMessage=status_message)

print(json.dumps(ut_dict))

def gst_to_lst(greenwich_sidereal_time, geographical_longitude):
'''
Convert Greenwich sidereal time to local sidereal time
'''
gst_split = greenwich_sidereal_time.split(":")
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)
lst_dict = dict(lstHours=lst_hours,lstMinutes=lst_minutes,lstSeconds=lst_seconds)

print(json.dumps(lst_dict))

def lst_to_gst(local_sidereal_time, geographical_longitude):
'''
Convert local sidereal time to Greenwich sidereal time
'''
lst_split = local_sidereal_time.split(":")
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)
gst_dict = dict(gstHours=gst_hours,gstMinutes=gst_minutes,gstSeconds=gst_seconds)

print(json.dumps(gst_dict))

def main():
parser = argparse.ArgumentParser(description='Practical Astronomy CLI.')

Expand Down Expand Up @@ -183,37 +60,38 @@ def main():
if not args.year:
display_error("'year' argument is required.")
else:
doe_year(args.year)
PA_CLI_DT.doe_year(args.year)
#doe_year(args.year)

if args.cd_to_dn:
if not args.cd:
display_error("'cd' argument is required.")
else:
cd_to_dn(args.cd)
PA_CLI_DT.cd_to_dn(args.cd)

if args.gd_to_jd:
if not args.gd:
display_error("'gd' argument is required.")
else:
gd_to_jd(args.gd)
PA_CLI_DT.gd_to_jd(args.gd)

if args.jd_to_gd:
if not args.jd:
display_error("'jd' argument is required.")
else:
jd_to_gd(args.jd)
PA_CLI_DT.jd_to_gd(args.jd)

if args.ct_to_dh:
if not args.ct:
display_error("'ct' argument is required.")
else:
ct_to_dh(args.ct)
PA_CLI_DT.ct_to_dh(args.ct)

if args.dh_to_ct:
if not args.dh:
display_error("'dh' argument is required.")
else:
dh_to_ct(args.dh)
PA_CLI_DT.dh_to_ct(args.dh)

if args.lct_to_ut:
if not args.cd:
Expand All @@ -223,7 +101,7 @@ def main():
elif not args.zc:
display_error("'zc', argument is required.")
else:
lct_to_ut(args.cd, args.ct, args.is_daylight_savings, args.zc)
PA_CLI_DT.lct_to_ut(args.cd, args.ct, args.is_daylight_savings, args.zc)

if args.ut_to_lct:
if not args.cd:
Expand All @@ -233,39 +111,39 @@ def main():
elif not args.zc:
display_error("'zc', argument is required.")
else:
ut_to_lct(args.cd, args.ut, args.is_daylight_savings, args.zc)
PA_CLI_DT.ut_to_lct(args.cd, args.ut, args.is_daylight_savings, args.zc)

if args.ut_to_gst:
if not args.ut:
display_error("'ut' argument is required.")
elif not args.gd:
display_error("'gd' argument is required.")
else:
ut_to_gst(args.ut, args.gd)
PA_CLI_DT.ut_to_gst(args.ut, args.gd)

if args.gst_to_ut:
if not args.gst:
display_error("'gst' argument is required.")
elif not args.gd:
display_error("'gd' argument is required.")
else:
gst_to_ut(args.gst, args.gd)
PA_CLI_DT.gst_to_ut(args.gst, args.gd)

if args.gst_to_lst:
if not args.gst:
display_error("'gst' argument is required.")
elif not args.gl:
display_error("'gl' argument is required.")
else:
gst_to_lst(args.gst, args.gl)
PA_CLI_DT.gst_to_lst(args.gst, args.gl)

if args.lst_to_gst:
if not args.lst:
display_error("'lst' argument is required.")
elif not args.gl:
display_error("'gl' argument is required.")
else:
lst_to_gst(args.lst, args.gl)
PA_CLI_DT.lst_to_gst(args.lst, args.gl)

if __name__ == "__main__":
main()

0 comments on commit 3f12a17

Please sign in to comment.