stud/misc/deg.py

27 lines
649 B
Python
Raw Normal View History

2019-11-21 11:16:24 +02:00
from collections import namedtuple
2019-11-21 11:13:42 +02:00
from decimal import Decimal as Dec
2019-11-21 11:16:24 +02:00
class Deg(namedtuple('Deg', ['deg', 'mm', 'ss'])):
def __str__(self):
return "%03d-%02d-%.1f" % (self.deg, self.mm, self.ss)
2019-11-21 11:13:42 +02:00
def guess(inp):
if isinstance(inp, Dec):
return inp
if '-' in instr:
deg, mm, ss = instr.split('-')
ddeg, dmm, dss = Dec(deg), Dec(mm), Dec(ss)
return deg + dmm/60 + dss/3600
else:
return Dec(instr)
def hms(deg):
assert isinstance(deg, Dec)
pdeg, pmm = divmod(deg, 1)
pmm = pmm * Dec(60)
pmm, pss = divmod(pmm, 1)
pss = pss * Dec(60)
return Deg(pdeg, pmm, pss)