File: jd.py

package info (click to toggle)
python-juliandate 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: python: 168; makefile: 13
file content (52 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download
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
#!/usr/bin/env python3

import argparse
import juliandate as jd


def convert(j, args):
    d = int(j + 0.5) if args.round else j
    c = jd.to_julian(d) if args.julian else jd.to_gregorian(d)

    if args.tabs:
        return "\t".join([str(x) for x in c])

    return ", ".join([str(x) for x in c])
    

def main():
    parser = argparse.ArgumentParser(description="Convert Julian Days to calendar days")
    parser.add_argument("jds", metavar='J', type=float, nargs='*',
                        help="Julian days to convert")
    parser.add_argument("-f", "--file", type=argparse.FileType('r'),
                        help="Read from file or stdin ('-')")
    parser.add_argument("-j", "--julian", action="store_true",
                        help="Convert to Julian date instead of Gregorian")
    parser.add_argument("-r", "--round", action="store_true",
                        help="Round to simple day")
    parser.add_argument("-t", "--tabs", action="store_true",
                        help="Separate output with tabs instead of commas")

    args = parser.parse_args()

    if args.file is None:
        for j in args.jds:
            try:
                print(convert(j, args))
            except ValueError as e_info:
                if (str(e_info).startswith("JDN")):
                    print(f"Error: Cannot convert {j}, {e_info}")
                pass

    else:
        for j in args.file:
            try:
                print(convert(float(j), args))
            except ValueError as e_info:
                if (str(e_info).startswith("JDN")):
                    print(f"Error: Cannot convert {j}, {e_info}")
                pass


if __name__ == "__main__":
    main()