#!/usr/bin/env python

# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/iif/lbtoEquat2Horiz.py 379 2018-01-12 10:27:17Z depa $

# @file
# A python program that transforms Alt-Az (horizontal) to Ra-Dec (equatorial) and vice versa.
# It uses the LBTO frame and time at the time of the call.

# Usage:
#  ./LbtoEquat2Horiz.py raDegrees decDegrees
#  ./LbtoEquat2Horiz.py hh:mm:ss +-dd:mm:ss
#  python3.4 Equat2Horiz.py raDegrees decDegrees
#  ./LbtoEquat2Horiz.py -r altDegrees azDegrees
#  python3.4 Equat2Horiz.py -r altDegrees azDegrees

# Make sure that PYTHONPATH includes ${INSROOT}/lib/python/site-packages
#   and (if applicable) ${ICEDIR}/python to avoid errors with imports here...
import sys
import time
import datetime

# you need astropy >= 1.3 to avoid an error from importing astroplan...
# https://github.com/astropy/astropy
import astropy
import astropy.units as units
from astropy.time import Time
from astropy import coordinates
from astropy.coordinates import SkyCoord, EarthLocation, Angle, AltAz, ICRS, FK5, Longitude, Latitude

# https://github.com/astropy/astroplan
# needs to be part of PYTHONPATH
# import astroplan
# from astroplan import observer

# print current local alt and azimuth
# @param ra Right ascension in degrees
# @param dec declination in degrees
# @return True if the altitude is >10 deg above the horizon
# @since 2017-01-31
def getHoriz(lbtoLoc,raDeg,decDeg):
	nowT = Time.now()
	print(nowT)

	pointing = astropy.coordinates.SkyCoord(ra=raDeg*units.deg, dec=decDeg*units.deg, frame='icrs')
	print()
	print(pointing)
	# let astropy compute altitude and azimuth
	aafr = AltAz(obstime=nowT,location=lbtoLoc,pressure=68100*units.Pa,
		temperature=5*units.deg_C,relative_humidity=0.4)
	altaz = pointing.transform_to(aafr)
	# print('Alt',altaz.alt, 'Az',altaz.az)
	print()
	print(altaz)
	print('alt',altaz.alt.to_string(unit=units.deg,sep=':'),'az',
		altaz.az.to_string(unit=units.deg,sep=':'))
	if altaz.az/units.deg > 10.0:
		return True
	else:
		return False

# print current local ra and dec
# @param alt Star altitude above horizon in degrees
# @param az Star azimuth in degrees, North over East
# @since 2017-01-31
def getEquat(lbtoLoc,altDeg,azDeg):
	nowT = Time.now()
	print(nowT)

	# Horizontal frame at the observatory
	aafr = AltAz(obstime=nowT,location=lbtoLoc,pressure=68100*units.Pa,
		temperature=5*units.deg_C,relative_humidity=0.4)
	# direction of the pointing
	# Longitudes are E=positive. Plugging this in here gives the associated N=0, E=90
	# without redefining the sign or adding 180 degrees to the azimuth.
	lon = Longitude(azDeg*units.deg)
	lat = Latitude(altDeg*units.deg)
	rep = SkyCoord(lon,lat,frame=aafr)
	print()
	print(rep)

	# fk5 = FK5(equinox="J2000")
	# radDec = rep.transform_to(fk5)
	icrs = ICRS()
	radDec = rep.transform_to(icrs)
	print()
	print(radDec)
	print('ra',radDec.ra.to_string(unit=units.hour,sep=':',pad=True),'dec',
		radDec.dec.to_string(unit=units.deg,sep=':',pad=True,alwayssing=True))

jtzt=datetime.datetime.utcnow()
print(str(jtzt),'UTC')

# specify the MtGraham observatory by longitude, latitude and altitude above s.l.
lbtoLoc = EarthLocation(lat=32.701309*units.deg, lon=-109.889064*units.deg, height=3221*units.m)
# lbtobs = astroplan.Observer(location=lbtoLoc,name="LBT", timezone="MST")

# If the command line has two arguments, assume these are ra and dec
# If the command line has three arguments, assume the last two are elevation and azimuth
if len(sys.argv) >= 4:
	# altDeg = float(sys.argv[-2])
	altDeg = Angle(sys.argv[-2].lower(), unit=units.deg)
	# azDeg = float(sys.argv[-1])
	azDeg = Angle(sys.argv[-1].lower(), unit=units.deg)
	getEquat(lbtoLoc,altDeg/units.deg,azDeg/units.deg)
elif len(sys.argv) >= 3:
	if ':' in sys.argv[-2]:
		raDeg = Angle(sys.argv[-2].lower(), unit=units.hour)
	else:
		raDeg = Angle(sys.argv[-2].lower(),unit=units.deg)
	# decDeg = float(sys.argv[-1])
	decDeg = Angle(sys.argv[-1].lower(), unit=units.deg)
	getHoriz(lbtoLoc,raDeg/units.deg,decDeg/units.deg)
else:
	print("usage:",sys.argv[0],"raInDegrees decInDegrees")
	print("usage:",sys.argv[0],"-r altInDegrees azInDegrees")
