Compare commits

..

4 Commits

Author SHA1 Message Date
Dessa Simpson 809e9ed87c allow adjusting moon size 2024-07-14 16:31:20 -07:00
Dessa Simpson 7e36e07996 make elevation float not int 2024-07-14 14:28:12 -07:00
Dessa Simpson 10693ff1f4 initial pretty (at one size lol) 2024-07-14 14:02:00 -07:00
Dessa Simpson bc5f5932f1 rewrite with skyfield 2024-07-14 01:31:24 -07:00
2 changed files with 95 additions and 135 deletions

View File

@ -34,22 +34,26 @@ from moons import backgrounds
# If you change the aspect ratio, the canned backgrounds won't work. # If you change the aspect ratio, the canned backgrounds won't work.
ASPECTRATIO = 0.5 ASPECTRATIO = 0.5
def putmoon(pctphase, lines, atfiller, hemisphere): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,too-many-arguments def putmoon(fracphase, lines, hemisphere, atfiller='@'): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,too-many-arguments
"""Print the moon""" """Print the moon
Arguments:
fracphase: A float 0 <= n < 1 representing the current point in the cycle
lines: An integer representing the number of lines in the output
hemisphere: A string 'north' or 'south' representing the observer's hemisphere
atfiller: What character to use in place of '@'
"""
output = "" output = ""
def putchar(char):
nonlocal output
output += char
# Find the length of the atfiller string # Find the length of the atfiller string
atflrlen = len(atfiller) atflrlen = len(atfiller)
# Fix waxes and wanes direction for south hemisphere # Fix waxes and wanes direction for south hemisphere
if hemisphere == 'south': if hemisphere == 'south':
pctphase = 1 - pctphase fracphase = 1 - pctphase
angphase = pctphase * 2.0 * math.pi angphase = fracphase * 2.0 * math.pi
mcap = -math.cos(angphase) mcap = -math.cos(angphase)
# Figure out how big the moon is # Figure out how big the moon is
@ -75,7 +79,7 @@ def putmoon(pctphase, lines, atfiller, hemisphere): # pylint: disable=too-many-
# Now output the slice # Now output the slice
col = 0 col = 0
while col < colleft: while col < colleft:
putchar(' ') output += ' '
col += 1 col += 1
while col <= colright: while col <= colright:
if hemisphere == 'north': if hemisphere == 'north':
@ -94,16 +98,16 @@ def putmoon(pctphase, lines, atfiller, hemisphere): # pylint: disable=too-many-
# rotate char upside-down if needed # rotate char upside-down if needed
char = char.translate(str.maketrans("().`_'", char = char.translate(str.maketrans("().`_'",
")(`.^,")) "!!!!!!"))
if char != '@': if char != '@':
putchar(char) output += char
else: else:
putchar(atfiller[atflridx]) output += atfiller[atflridx]
atflridx = (atflridx + 1) % atflrlen atflridx = (atflridx + 1) % atflrlen
col += 1 col += 1
putchar('\n') output += '\n'
lin += 1 lin += 1
return output return output

View File

@ -1,105 +1,70 @@
#!/usr/bin/env python #!/usr/bin/env python
from datetime import datetime, timezone """Xaphoon - Displays the phase of the moon as well as other related information."""
import math
import time import time
from argparse import ArgumentParser from argparse import ArgumentParser
import ephem from datetime import datetime, timezone
from skyfield import almanac
from skyfield_data import get_skyfield_data_path
import skyfield.api
from pyphoon import putmoon from pyphoon import putmoon
# Second resolution for culmination/illumination calculations # Initialize certain skyfield parameters globally
DAY_INCREMENT=1/86400 sf_load = skyfield.api.Loader(get_skyfield_data_path(), expire=False) # loader
ts = sf_load.timescale(builtin=False) # timescale
eph = sf_load('de421.bsp') # ephemerides
earth, sun, moon = eph['Earth'], eph['Sun'], eph['Moon'] # moooon
def to_deg(rad): def to_timestr(t, date=False, local=True):
"""Convert radians to a displayable integer number of degrees.""" """Convert a skyfield time to a time string, optionally in the local time zone."""
return round(math.degrees(rad)) t = t.utc_datetime()
def to_timestr(date, local=True):
"""Convert a pyephem date to a time string in the local time zone."""
if local: if local:
date = ephem.localtime(date) t = t.astimezone()
else: if date:
date = date.datetime() return t.strftime('%Y-%m-%d %H:%M:%S')
return date.strftime("%H:%M:%S") return t.strftime('%H:%M:%S')
def find_target_rising(moon, me): def fmt(cols, t, az, el, phase, illum, moonrise, transit, moonset, moonlines, hemi):
"""Return the relevant moonrise to base display and calculations off of.""" """Formats data into string to print"""
if moon.alt == 0: # i would love a better way to do this _date = t.utc_datetime().astimezone().strftime('%Y-%m-%d %H:%M:%S') # 18 chars
me = me.copy() _azel = f"Az:{az.degrees:.0f}° El:{el.degrees:.0f}°".ljust(16) # 16 chars
me.date = me.previous_rising(moon) _phil = f"Ph: {phase.degrees:.0f}° Ill:{illum*100:.0f}%".rjust(16) # 16 chars
return me.next_rising(moon) _r = f"R:{to_timestr(moonrise)}" # 10 chars
if moon.alt > 0: _t = f"T:{to_timestr(transit)}" # 10 chars
return me.previous_rising(moon) _s = f"S:{to_timestr(moonset)}" # 10 chars
# moon.alt < 0 _moon = putmoon(phase.degrees/360, moonlines, hemi) # ! scalable width
return me.next_rising(moon)
def cmp_culmination(moon, me, t): # 2 groups of spacing, filling cols minus total RTS width
"""Determine whether the culmination is before, after, or at t. _rts_spacing = ' '*int((cols-30)/2)
Returns 0 if t is the culmination, -1 if t if culmination is before t, or 1 ret = f"{_date.center(cols)}\n"
if culmination is after t. Assumes there is exactly one peak elevation, ret += f"{_azel}{' '*(cols-32)}{_phil}\n"
which seems to cause error of up to about 7 seconds due to float precision. # split moon on newlines, right-pad to center moon in original width, center to center
""" # in new width, then rejoin with newlines and tack an extra newline on the end
me.date = t - DAY_INCREMENT ret += '\n'.join([line.ljust(44).center(cols) for line in _moon.split('\n')]) + '\n'
moon.compute(me) ret += f"{_r}{_rts_spacing}{_t}{_rts_spacing}{_s}"
e1 = moon.alt return ret
me.date = t
moon.compute(me)
e2 = moon.alt
me.date = t + DAY_INCREMENT
moon.compute(me)
e3 = moon.alt
if e1 > e2:
return -1
if e3 > e2:
return 1
return 0
def find_culmination(moon, me, rising, setting):
"""Finds culmination via binary search.
Assumes rising and setting are from same pass.
"""
moon = moon.copy()
me = me.copy()
t1 = rising
t3 = setting
while True:
t2 = (t1 + t3) / 2
match cmp_culmination(moon,me,t2):
case 0: return ephem.date(t2)
case -1: t3 = t2
case 1: t1 = t2
def cmp_illumination(moon, me, t):
"""Determine whether the moon is waxing, waning, or either full or new.
Returns 0 if the moon is either full or new, -1 if moon is waning, or 1
if moon is waxing.
"""
moon = moon.copy()
me = me.copy()
me.date = t - DAY_INCREMENT
moon.compute(me)
i1 = moon.moon_phase
me.date = t + DAY_INCREMENT
moon.compute(me)
i2 = moon.moon_phase
if i1 > i2:
return -1
if i1 < i2:
return 1
return 0
def main(): def main():
"""Main function
Parses arguments, calculates values, and displays them.
"""
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument("lat", parser.add_argument("lat",
help="Observer latitude") help="Observer latitude",
parser.add_argument("long", type=float)
help="Observer longitude") parser.add_argument("lon",
help="Observer longitude",
type=float)
parser.add_argument("elevation", parser.add_argument("elevation",
help="Observer elevation in meters", help="Observer elevation in meters",
type=float)
parser.add_argument("-l", "--lines",
help="Number of lines for the output to use (default 25)",
default=25,
type=int) type=int)
parser.add_argument("-c", "--columns", parser.add_argument("-c", "--columns",
help="Number of columns for the output to use (default 70)", help="Number of columns for the output to use (default 70)",
@ -111,47 +76,38 @@ def main():
type=int) type=int)
args = parser.parse_args() args = parser.parse_args()
now = ephem.date(datetime.fromtimestamp(args.time, timezone.utc)) t = ts.from_datetime(datetime.fromtimestamp(args.time, timezone.utc)) # current time
print(f"Current time: {to_timestr(now)}")
me = ephem.Observer() obs_geo = skyfield.api.wgs84.latlon(args.lat, args.lon,
me.date = now elevation_m=args.elevation) # geographic position vector
me.lat = args.lat obs = earth + obs_geo # barycentric position vector
me.lon = args.long
me.elevation = args.elevation
moon = ephem.Moon(me) moon_apparent = obs.at(t).observe(moon).apparent()
el, az, _ = moon_apparent.altaz('standard')
az = to_deg(moon.az) # Find relevant moonrise. el is based on apparent location, so accounts
el = to_deg(moon.alt) # for atmospheric refraction. y shouldn't be needed unless user is near
print(f"Az: {az}° El: {el}°") # one of the poles, so ignored for now. First [0] discards y (second
# element of tuple); second [] selects from array of moonrises/moonsets
rising = find_target_rising(moon, me) if el.degrees > 0:
setting = me.next_setting(moon) # Moon is up. Find last moonrise in the past 24 hours.
moonrise = almanac.find_risings(obs, moon, t-1, t)[0][-1]
print (f"Rise: {to_timestr(rising)} Set: {to_timestr(setting)}")
culm = find_culmination(moon, me, rising, setting)
print(f"Culmination: {to_timestr(culm)}")
direction = cmp_illumination(moon, me, now)
match direction:
case -1:
direction_indicator = '-'
case 0:
direction_indicator = ''
case 1:
direction_indicator = '+'
print(f"Phase: {moon.moon_phase:.0%}{direction_indicator}")
# Convert illumination percentage and waxing/waning status to percent through full cycle
if direction < 0: # waning
full_cycle_phase = 1 - (moon.moon_phase / 2)
else: else:
full_cycle_phase = moon.moon_phase / 2 # Moon is not up. Find first moonrise in the next 24 hours.
moonrise = almanac.find_risings(obs, moon, t, t+1)[0][0]
print(putmoon(full_cycle_phase, 20, '@', 'northern' if me.lat > 0 else 'southern')) # Find first moonset in the next 24 hours after moonrise.
moonset = almanac.find_settings(obs, moon, moonrise, moonrise+1)[0][0]
transit = almanac.find_transits(obs, moon, moonrise, moonrise+1)[0]
phase = almanac.moon_phase(eph, t)
illum = moon_apparent.fraction_illuminated(sun)
hemi = 'north' if args.lat > 0 else 'south'
#print(phase.degrees/360*100)
#print(putmoon(phase.degrees/360, 21, 'north' if args.lat > 0 else 'south'))
print(fmt(args.columns, t, az, el, phase, illum, moonrise, transit, moonset, args.lines - 4,hemi))
#print(putmoon(phase.degrees/360, 21, '@', hemi))
main() main()