'''hatpilc.py - Waqas Bhatti (wbhatti@astro.princeton.edu) - May 2017
License: MIT. See LICENSE for full text.

This is mostly for internal use. Contains functions to read text light curves
produced by the HATPI prototype system.

'''

####################
## SYSTEM IMPORTS ##
####################

import logging
from datetime import datetime
from traceback import format_exc
import os.path
import gzip
import re

import numpy as np


#############
## LOGGING ##
#############

# setup a logger
LOGGER = None

def set_logger_parent(parent_name):
    globals()['LOGGER'] = logging.getLogger('%s.hatpilc' % parent_name)

def LOGDEBUG(message):
    if LOGGER:
        LOGGER.debug(message)
    elif DEBUG:
        print('%sZ [DBUG]: %s' % (datetime.utcnow().isoformat(), message))

def LOGINFO(message):
    if LOGGER:
        LOGGER.info(message)
    else:
        print('%sZ [INFO]: %s' % (datetime.utcnow().isoformat(), message))

def LOGERROR(message):
    if LOGGER:
        LOGGER.error(message)
    else:
        print('%sZ [ERR!]: %s' % (datetime.utcnow().isoformat(), message))

def LOGWARNING(message):
    if LOGGER:
        LOGGER.warning(message)
    else:
        print('%sZ [WRN!]: %s' % (datetime.utcnow().isoformat(), message))

def LOGEXCEPTION(message):
    if LOGGER:
        LOGGER.exception(message)
    else:
        print(
            '%sZ [EXC!]: %s\nexception was: %s' % (
                datetime.utcnow().isoformat(),
                message, format_exc()
                )
            )


###################
## USEFUL CONFIG ##
###################

HATIDREGEX = re.compile(r'HAT-\d{3}-\d{7}')

COLDEFS = [('rjd',float),
           ('stf',str),
           ('hat',str),
           ('xcc',float),
           ('ycc',float),
           ('xic',float),
           ('yic',float),
           ('fsv',float),
           ('fdv',float),
           ('fkv',float),
           ('bgv',float),
           ('bge',float),
           ('ifl1',float),
           ('ife1',float),
           ('irm1',float),
           ('ire1',float),
           ('irq1',str),
           ('ifl2',float),
           ('ife2',float),
           ('irm2',float),
           ('ire2',float),
           ('irq2',str),
           ('ifl3',float),
           ('ife3',float),
           ('irm3',float),
           ('ire3',float),
           ('irq3',str),
           ('iep1',float),
           ('iep2',float),
           ('iep3',float)]


######################
## READING TEXT LCS ##
######################

def read_hatpi_txtlc(lcfile):
    '''
    This reads in a textlc that is complete up to the TFA stage.

    '''

    if 'TF1' in lcfile:
        thiscoldefs = COLDEFS + [('itf1',float)]
    elif 'TF2' in lcfile:
        thiscoldefs = COLDEFS + [('itf2',float)]
    elif 'TF3' in lcfile:
        thiscoldefs = COLDEFS + [('itf3',float)]

    LOGINFO('reading %s' % lcfile)

    with gzip.open(lcfile,'r') as infd:

        lclines = infd.read().decode().split('\n')
        lclines = [x.split() for x in lclines if ('#' not in x and len(x) > 0)]
        ndet = len(lclines)

        if ndet > 0:

            lccols = list(zip(*lclines))
            lcdict = {x[0]:y for (x,y) in zip(thiscoldefs, lccols)}

            # convert to ndarray
            for col in thiscoldefs:
                lcdict[col[0]] = np.array([col[1](x) for x in lcdict[col[0]]])

        else:

            lcdict = {}
            LOGWARNING('no detections in %s' % lcfile)

        # add the object's name to the lcdict
        hatid = HATIDREGEX.findall(lcfile)
        lcdict['objectid'] = hatid[0] if hatid else 'unknown object'

        # add the columns to the lcdict
        lcdict['columns'] = [x[0] for x in thiscoldefs]

        # add some basic info
        lcdict['objectinfo'] = {'ndet':ndet,
                                'hatid':hatid}

    return lcdict

@@ -3748,66 +3748,102 @@
  = %7B
-'ndet':ndet,%0A                                'hatid':hatid
+%0A            'ndet':ndet,%0A            'hatid':hatid%5B0%5D if hatid else 'unknown object'%0A        
 %7D%0A%0A 

make a bit more organized