stud/misc/deg.py

28 lines
624 B
Python
Raw Normal View History

2019-11-21 11:16:24 +02:00
from collections import namedtuple
2020-03-30 16:42:53 +03:00
2019-11-21 11:13:42 +02:00
from decimal import Decimal as Dec
2020-03-30 16:42:53 +03:00
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
2020-03-30 16:42:53 +03:00
2019-11-21 11:13:42 +02:00
def guess(inp):
2019-11-24 18:10:17 +02:00
if isinstance(inp, str) and '-' in inp:
deg, mm, ss = inp.split('-')
2019-11-21 11:13:42 +02:00
ddeg, dmm, dss = Dec(deg), Dec(mm), Dec(ss)
2019-11-24 22:34:43 +02:00
return ddeg + dmm/60 + dss/3600
2019-11-21 11:13:42 +02:00
else:
2020-03-30 16:42:53 +03:00
return Dec(inp)
2019-11-21 11:13:42 +02:00
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)