diff --git a/almanac.py b/almanac.py index 910da6d..3df4de3 100755 --- a/almanac.py +++ b/almanac.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import atexit import pickle from collections import defaultdict @@ -19,9 +19,17 @@ from corrections import * from formatting import * -from plots import * -__version__ = "0.2" +from datetime import datetime, timedelta +from os.path import isfile + +import seaborn as sns +from matplotlib import use +from matplotlib.pyplot import figure, plot, title, xlabel, ylabel, gca, xlim, ylim, savefig, tight_layout, legend +from numpy import array, nan +from progress.bar import Bar + +__version__ = "0.2.1" planets = "Venus", "Mars", "Jupiter", "Saturn" @@ -93,7 +101,7 @@ def iers_info(kind, mode): ARIES = "Aries" - +ts=None def init(iers_time=True, polar_motion=True, ephemeris="de440s", cache=None): "initialize SkyField and Almanac global state" @@ -482,6 +490,75 @@ def render(template, variables={}, progress=None): return template.generate() +def eqot_img(year, filename=None, size=(16, 9)): + sns.set_theme(context="notebook", style="whitegrid") + if filename: + if isfile(filename): return filename + use("Agg") + eqot = [] + xticks = [] + xlabels = [] + for d in Bar(filename or "Equation of Time Graph", max=365, suffix="%(percent)d%% %(eta_td)s").iter(range(365)): + t = datetime(year, 1, 1) + timedelta(days=d) + if t.day in [1, 10, 20]: + xticks.append(d) + xlabels.append(f"{t:%b}" if t.day == 1 else t.day) + eqot.append(equation_of_time(t) * 60) + + figure(figsize=size) + plot(eqot) + title(f"Equation of Time {year}") + xlabel("month of year") + ylabel("solar time - mean time (UT1) [minutes]") + xlim(0, 364) + gca().set_xticks(xticks, xlabels) + tight_layout() + if filename: + savefig(filename, pad_inches=0) + return filename + + +def mp_img(year, filename=None, size=(16, 9)): + sns.set_theme(context="notebook", style="whitegrid") + if filename: + if isfile(filename): return filename + use("Agg") + dashes = {"Sun": [], + "Jupiter": [6, 2], + "Saturn": [1, 2], + "Venus": [1, 2, 1, 2, 6, 2], + "Mars": [1, 2, 6, 2, 6, 2], + "Mercury": [1, 2, 6, 2], } + mp = {p: [] for p in dashes.keys()} + xticks = [] + xlabels = [] + for d in Bar(filename or "Meridian Passages Graph", max=365, suffix="%(percent)d%% %(eta_td)s").iter(range(365)): + t = datetime(year, 1, 1) + timedelta(days=d) + if t.day in [1, 10, 20]: + xticks.append(d) + xlabels.append(f"{t:%b}" if t.day == 1 else t.day) + for p in mp.keys(): + mp[p].append(meridian_passage(t, p)) + + figure(figsize=size) + for p, d in mp.items(): + d = array(d) + d[d > 23.8] = nan + plot(d, label=p, dashes=dashes[p]) + title(f"Meridian Passages {year}") + xlabel("month of year") + ylabel("MP [hours]") + xlim(0, 364) + ylim(0, 24) + legend() + gca().set_xticks(xticks, xlabels) + yticks = 0, 3, 6, 9, 12, 15, 18, 21 + gca().set_yticks(yticks, yticks) + tight_layout() + if filename: + savefig(filename, pad_inches=0) + return filename + def calculate(): import pyinputplus as pyip @@ -537,6 +614,7 @@ def process(template, out, variables, progress=None): # progress(1) # return init(variables["iers_time"], variables["polar_motion"], variables["ephemeris"], variables["cache"]) + if out == "-": for l in render(template, variables, progress): print(l, end="") @@ -614,13 +692,15 @@ def main(): parser.add_argument("-e", "--ephemeris", metavar="file", default="de440s", help="ephemeris file to use") parser.add_argument("-C", "--calculate", action="store_true", help="interactive sight reduction calculation") parser.add_argument("-p", "--parallel", type=int, default=1, help="number of parallel processes to use") - parser.add_argument("-m", "--multiple", type=int, default=3, help="number of parallel processes to use") + parser.add_argument("-m", "--multiple", type=int, default=3, + help="number of days to process together, smallest chunk size when splitting for parallel processing") parser.add_argument("-V", "--version", action="version", version=__version__) args = parser.parse_args() iers_time = not args.no_finals polar_motion = iers_time and not args.no_polar + if args.calculate: init(iers_time, polar_motion, args.ephemeris, args.cache) calculate() diff --git a/makefile b/makefile index fdbed73..3c32c31 100644 --- a/makefile +++ b/makefile @@ -4,15 +4,19 @@ pdf: Nautical-Almanac-$(Y).pdf txt: daily-pages-$(Y).txt Nautical-Almanac-$(Y).pdf: nautical-almanac.tex.j2 almanac.py - ./almanac.py $< -o $(@:.pdf=.tex) -f -c -y$(Y) + ./almanac.py $< -o $(@:.pdf=.tex) -f -c -y$(Y) $(O) latexmk -pdf $(@:.pdf=.tex) -interaction=batchmode daily-pages-$(Y).txt: daily-pages.txt.j2 almanac.py - ./almanac.py $< -o $@ -f -y$(Y) + ./almanac.py $< -o $@ -f -c -y$(Y) $(O) loop: while true; do inotifywait -q -e close_write *.j2 *.py makefile; make; done +test: + ./test.py + echo PASSED + clean: latexmk -C rm -f *.tex diff --git a/plots.py b/plots.py deleted file mode 100644 index 5f31759..0000000 --- a/plots.py +++ /dev/null @@ -1,80 +0,0 @@ -from datetime import datetime, timedelta -from os.path import isfile - -import seaborn as sns -from matplotlib import use -from matplotlib.pyplot import figure, plot, title, xlabel, ylabel, gca, xlim, ylim, savefig, tight_layout, legend -from numpy import array, nan -from progress.bar import Bar - -from almanac import equation_of_time, meridian_passage - - -def eqot_img(year, filename=None, size=(16, 9)): - sns.set_theme(context="notebook", style="whitegrid") - if filename: - if isfile(filename): return filename - use("Agg") - eqot = [] - xticks = [] - xlabels = [] - for d in Bar(filename or "Equation of Time Graph", max=365, suffix="%(percent)d%% %(eta_td)s").iter(range(365)): - t = datetime(year, 1, 1) + timedelta(days=d) - if t.day in [1, 10, 20]: - xticks.append(d) - xlabels.append(f"{t:%b}" if t.day == 1 else t.day) - eqot.append(equation_of_time(t) * 60) - - figure(figsize=size) - plot(eqot) - title(f"Equation of Time {year}") - xlabel("month of year") - ylabel("solar time - mean time (UT1) [minutes]") - xlim(0, 364) - gca().set_xticks(xticks, xlabels) - tight_layout() - if filename: - savefig(filename, pad_inches=0) - return filename - - -def mp_img(year, filename=None, size=(16, 9)): - sns.set_theme(context="notebook", style="whitegrid") - if filename: - if isfile(filename): return filename - use("Agg") - dashes = {"Sun": [], - "Jupiter": [6, 2], - "Saturn": [1, 2], - "Venus": [1, 2, 1, 2, 6, 2], - "Mars": [1, 2, 6, 2, 6, 2], - "Mercury": [1, 2, 6, 2], } - mp = {p: [] for p in dashes.keys()} - xticks = [] - xlabels = [] - for d in Bar(filename or "Meridian Passages Graph", max=365, suffix="%(percent)d%% %(eta_td)s").iter(range(365)): - t = datetime(year, 1, 1) + timedelta(days=d) - if t.day in [1, 10, 20]: - xticks.append(d) - xlabels.append(f"{t:%b}" if t.day == 1 else t.day) - for p in mp.keys(): - mp[p].append(meridian_passage(t, p)) - - figure(figsize=size) - for p, d in mp.items(): - d = array(d) - d[d > 23.8] = nan - plot(d, label=p, dashes=dashes[p]) - title(f"Meridian Passages {year}") - xlabel("month of year") - ylabel("MP [hours]") - xlim(0, 364) - ylim(0, 24) - legend() - gca().set_xticks(xticks, xlabels) - yticks = 0, 3, 6, 9, 12, 15, 18, 21 - gca().set_yticks(yticks, yticks) - tight_layout() - if filename: - savefig(filename, pad_inches=0) - return filename diff --git a/test.py b/test.py old mode 100644 new mode 100755 index 55aa57d..a65471c --- a/test.py +++ b/test.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import re from datetime import datetime, timedelta from os import listdir