#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# System modules
import telnetlib

# Third-party modules
import astropy.units as u
import cabinet
from astropy.coordinates import EarthLocation, SkyCoord
from astropy.time import Time


def diff_altaz():

    cab = cabinet.ask()

    gps = Time(cab["utc"], format="unix")

    location = EarthLocation(lat=44.5912,
                             lon=9.2034,
                             height=1469.0)

    altaz = SkyCoord(alt=cab["alt"]*u.deg,
                     az=cab["az"]*u.deg,
                     frame="altaz",
                     location=location,
                     obstime=gps)

    radec = SkyCoord(ra=cab["ra"]*u.hourangle,
                     dec=cab["dec"]*u.deg,
                     frame="icrs",
                     location=location,
                     obstime=gps)

    diffalt = altaz.alt - radec.transform_to("altaz").alt
    diffaz = altaz.az - radec.transform_to("altaz").az

    print(
        f"Time:  {gps.unix} alt: {cab['alt']} az: {cab['az']} ra: {cab['ra']} dec: {cab['dec']} diff: {diffalt.to(u.arcsec) : .3f} {diffaz.to(u.arcsec) : .3f}")


if __name__ == "__main__":

    diff_altaz()
