| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import argparse |
| 4 |
import juliandate as jd |
| 5 |
|
| 6 |
|
| 7 |
def convert(j, args): |
| 8 |
d = int(j + 0.5) if args.round else j |
| 9 |
c = jd.to_julian(d) if args.julian else jd.to_gregorian(d) |
| 10 |
|
| 11 |
if args.tabs: |
| 12 |
return "\t".join([str(x) for x in c]) |
| 13 |
|
| 14 |
return ", ".join([str(x) for x in c]) |
| 15 |
|
| 16 |
|
| 17 |
def main(): |
| 18 |
parser = argparse.ArgumentParser(description="Convert Julian Days to calendar days") |
| 19 |
parser.add_argument("jds", metavar='J', type=float, nargs='*', |
| 20 |
help="Julian days to convert") |
| 21 |
parser.add_argument("-f", "--file", type=argparse.FileType('r'), |
| 22 |
help="Read from file or stdin ('-')") |
| 23 |
parser.add_argument("-j", "--julian", action="store_true", |
| 24 |
help="Convert to Julian date instead of Gregorian") |
| 25 |
parser.add_argument("-r", "--round", action="store_true", |
| 26 |
help="Round to simple day") |
| 27 |
parser.add_argument("-t", "--tabs", action="store_true", |
| 28 |
help="Separate output with tabs instead of commas") |
| 29 |
|
| 30 |
args = parser.parse_args() |
| 31 |
|
| 32 |
if args.file is None: |
| 33 |
for j in args.jds: |
| 34 |
try: |
| 35 |
print(convert(j, args)) |
| 36 |
except ValueError as e_info: |
| 37 |
if (str(e_info).startswith("JDN")): |
| 38 |
print(f"Error: Cannot convert {j}, {e_info}") |
| 39 |
pass |
| 40 |
|
| 41 |
else: |
| 42 |
for j in args.file: |
| 43 |
try: |
| 44 |
print(convert(float(j), args)) |
| 45 |
except ValueError as e_info: |
| 46 |
if (str(e_info).startswith("JDN")): |
| 47 |
print(f"Error: Cannot convert {j}, {e_info}") |
| 48 |
pass |
| 49 |
|
| 50 |
|
| 51 |
if __name__ == "__main__": |
| 52 |
main() |