# coding: utf-8

""" Convenience functions for observational data """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os, sys

# Third-party
import numpy as np
import astropy.coordinates as coord
import astropy.units as u

__all__ = ["apparent_magnitude", "absolute_magnitude", "distance_modulus", \
           "distance"]

def apparent_magnitude(M, d):
    """ Compute the apparent magnitude of a source given an absolute magnitude
        and a distance.
        
        Parameters
        ----------
        M : numeric or iterable
            Absolute magnitude of a source.
        d : astropy.units.Quantity
            The distance to the source as a Quantity object.
            
    """
    
    if not isinstance(d, u.Quantity):
        raise TypeError("Distance must be an Astropy Quantity object!")
    
    # Compute the apparent magnitude -- ignores extinction
    return M + distance_modulus(d)

def absolute_magnitude(m, d):
    """ Compute the absolute magnitude of a source given an apparent magnitude
        and a distance.
        
        Parameters
        ----------
        m : numeric or iterable
            Apparent magnitude of a source.
        d : astropy.units.Quantity
            The distance to the source as a Quantity object.
    """
    
    if not isinstance(d, u.Quantity):
        raise TypeError("Distance must be an Astropy Quantity object!")
    
    # Compute the apparent magnitude -- ignores extinction
    return m - distance_modulus(d)

def distance_modulus(d):
    """ Compute the distance modulus given a distance. 
        
        Parameters
        ----------
        d : astropy.units.Quantity
            The distance as a Quantity object.
    """
    if not isinstance(d, u.Quantity):
        raise TypeError("Distance must be an Astropy Quantity object!")
        
    return 5.*(np.log10(d.to(u.pc).value) - 1.)

def distance(distance_modulus):
    """ Compute the distance from the distance modulus. 
    
        Parameters:
        dm : numeric or iterable
            The distance modulus
    """
    return 10**(distance_modulus/5. + 1) * u.pc
# coding: utf-8

""" Convenience functions for observational data """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os, sys
from datetime import datetime, timedelta

# Third-party
import numpy as np
import astropy.coordinates as coord
import astropy.units as u
from astropy.time import Time

__all__ = ["apparent_magnitude", "absolute_magnitude", "distance_modulus", \
           "distance", "gmst_time_to_utc"]

def apparent_magnitude(M, d):
    """ Compute the apparent magnitude of a source given an absolute magnitude
        and a distance.
        
        Parameters
        ----------
        M : numeric or iterable
            Absolute magnitude of a source.
        d : astropy.units.Quantity
            The distance to the source as a Quantity object.
            
    """
    
    if not isinstance(d, u.Quantity):
        raise TypeError("Distance must be an Astropy Quantity object!")
    
    # Compute the apparent magnitude -- ignores extinction
    return M + distance_modulus(d)

def absolute_magnitude(m, d):
    """ Compute the absolute magnitude of a source given an apparent magnitude
        and a distance.
        
        Parameters
        ----------
        m : numeric or iterable
            Apparent magnitude of a source.
        d : astropy.units.Quantity
            The distance to the source as a Quantity object.
    """
    
    if not isinstance(d, u.Quantity):
        raise TypeError("Distance must be an Astropy Quantity object!")
    
    # Compute the apparent magnitude -- ignores extinction
    return m - distance_modulus(d)

def distance_modulus(d):
    """ Compute the distance modulus given a distance. 
        
        Parameters
        ----------
        d : astropy.units.Quantity
            The distance as a Quantity object.
    """
    if not isinstance(d, u.Quantity):
        raise TypeError("Distance must be an Astropy Quantity object!")
        
    return 5.*(np.log10(d.to(u.pc).value) - 1.)

def distance(distance_modulus):
    """ Compute the distance from the distance modulus. 
    
        Parameters:
        dm : numeric or iterable
            The distance modulus
    """
    return 10**(distance_modulus/5. + 1) * u.pc

def gmst_time_to_utc(t):
    jd = int(t.jd) + 0.5
    
    S = jd - 2451545.0
    T = S / 36525.0
    T0 = 6.697374558 + (2400.051336*T) + (0.000025862*T**2)
    T0 = T0 % 24
    
    h = (t.jd - jd)*24.
    GST = (h - T0) % 24
    UT = GST * 0.9972695663
    
    tt = Time(jd, format='jd', scale='utc')
    dt = tt.datetime + timedelta(hours=UT)
    
    return Time(dt, scale='utc')
add gmst utc thing