# -*- coding: utf-8 -*-
"""
Created on Thu May  5 16:07:19 2016

@author:
Maximilian N. Guenther
Battcock Centre for Experimental Astrophysics,
Cavendish Laboratory,
JJ Thomson Avenue
Cambridge CB3 0HE
Email: mg719@cam.ac.uk
"""

import numpy as np
import ngtsio_get
from astropy import units as u
from astropy.coordinates import SkyCoord



def find(RA, DEC, unit='hmsdms', frame='icrs', ngts_version='all', 
         give_obj_id=True, search_radius=0.0014, field_radius=2.):
    
    '''
    input:
    RA:           in h m s as space seperated string
    DEC:          in d m s as space seperated string
    unit:         'hmsdms', 'deg', 'rad'
    frame:        'icrs' or others that astropy accepts
    ngts_version: 'all' or e.g. 'CYCLE1706'
    give_obj_id:   also retrieve the obj_id
    search_radius: 0.0014 degree = 4.97 arcsec = 1 NGTS pixel
    field_radius:  1.92 degree, from center to the corner for square 7.4 sq deg FoV
    ''' 
    
    
    #convert RA and DEC from string into skycoords
    RA_input  = str(RA)
    DEC_input = str(DEC)
    if unit=='hmsdms':
        c   = SkyCoord(RA+' '+DEC, frame=frame, unit=(u.hourangle, u.deg))
        RA  = c.ra.deg
        DEC = c.dec.deg
    elif unit=='deg':
        pass
    elif unit=='rad':
        RA  = RA*180./np.pi
        DEC = DEC*180./np.pi
    
    
    
    #read list of observed NGTS fields from opis (needs to be manually updated)
    d = np.genfromtxt('List_of_observed_NGTS_fields.txt', usecols=[0,3], dtype=None)
    fieldnames    = d[:,0]
    ngts_versions = d[:,1]
    
    
    
    #fc = field_center
    RA_DEC_fc = [ x[2:4]+' '+x[4:6]+' 00' + ' ' + x[6:9]+' '+x[9:11]+' 00' for x in fieldnames ] 
    c_fc      = [ SkyCoord(x, frame='icrs', unit=(u.hourangle, u.deg)) for x in RA_DEC_fc ] 
    RA_fc     = np.array([ x.ra.deg for x in c_fc ])
    DEC_fc    = np.array([ x.dec.deg for x in c_fc ])
    
    
    
    #indices where the searched RA / DEC may be covered in an NGTS field (upper limit)
    if ngts_version == 'all':
        ind_field = np.where( (np.abs(RA_fc - RA) < field_radius) 
                      & (np.abs(DEC_fc - DEC) < field_radius) )[0]
    else:
        ind_field = np.where( (np.abs(RA_fc - RA) < field_radius) 
                      & (np.abs(DEC_fc - DEC) < field_radius) 
                      & (ngts_versions == ngts_version) )[0]
    
    

    #retrieve object ids via ngtsio_get   
    if (give_obj_id is True):
        obj_id = ['None']*len(fieldnames)
        for i in ind_field:
            
            dic = ngtsio_get.get(fieldnames[i][0:11], ['RA','DEC'], ngts_version=ngts_versions[i], silent=True) 
            RA_objs = dic['RA']*180./np.pi
            DEC_objs = dic['DEC']*180./np.pi
            
            ind_obj = np.where( (np.abs(RA_objs - RA) < search_radius) 
                              & (np.abs(DEC_objs - DEC) < search_radius) )[0]
                             
            if len(ind_obj) > 0:
                obj_id[i] = dic['OBJ_ID'][ind_obj]
    else:
        obj_id = ['']*len(fieldnames)
        
        
        
    #output
    if len(ind_field) == 0:
        print RA_input +'\t'+ DEC_input +'\t'+ 'no match'
    else:
#        print RA, DEC, 'in fields:'
        for i in ind_field:
#            print list(obj_id[i])
            if obj_id[i] is not 'None':
                obj_id_str = '\t'.join( list(obj_id[i]) )
            else:
                obj_id_str = 'None'
            print RA_input +'\t'+ DEC_input +'\t'+ fieldnames[i][0:11] +'\t'+ ngts_versions[i] +'\t'+ obj_id_str
        
    
    
if __name__ == '__main__':
    pass