# Path: ugali/utils/projector.py
# class SphericalRotator:
# class Projector:
#     def __init__(self, lon_ref, lat_ref, zenithal=False):
#     def setReference(self, lon_ref, lat_ref, zenithal=False):
#     def cartesian(self,lon,lat):
#     def rotate(self, lon, lat, invert=False):
#     def __init__(self, lon_ref, lat_ref, proj_type = 'ait'):
#             def rotate(lon,lat,invert=False):
#     def sphereToImage(self, lon, lat):
#     def imageToSphere(self, x, y):
# def sphere2image(lon_ref,lat_ref,lon,lat):
# def image2sphere(lon_ref,lat_ref,x,y):
# def cartesianSphereToImage(lon, lat):
# def cartesianImageToSphere(x,y):
# def aitoffSphereToImage(lon, lat):
# def aitoffImageToSphere(x, y):
# def gnomonicSphereToImage(lon, lat):
# def gnomonicImageToSphere(x, y):
# def angsep2(lon_1, lat_1, lon_2, lat_2):
# def angsep(lon1,lat1,lon2,lat2):
# def galToCel(ll, bb):
# def celToGal(ra, dec):
# def estimate_angle(angle, origin, new_frame, offset=1e-7):
# def gal2cel_angle(glon,glat,angle,offset=1e-7):
# def cel2gal_angle(ra,dec,angle,offset=1e-7):
# def dec2hms(dec):
# def dec2dms(dec):
# def hms2dec(hms):
# def dms2dec(dms):
# def sr2deg(solid_angle):
# def deg2sr(solid_angle):
# def distanceToDistanceModulus(distance):
# def distanceModulusToDistance(distance_modulus):
# def ang2const(lon,lat,coord='gal'):
# def ang2iau(lon,lat,coord='gal'):
# def match(lon1, lat1, lon2, lat2, tol=None, nnearest=1):
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
# 
# Path: ugali/utils/healpix.py
# def ang2pix(nside, lon, lat, nest=False):
#     """
#     Input (lon, lat) in degrees instead of (theta, phi) in radians
#     """
#     theta = np.radians(90. - lat)
#     phi = np.radians(lon)
#     return hp.ang2pix(nside, theta, phi, nest=nest)
# 
# Path: ugali/utils/logger.py
# class SpecialFormatter(logging.Formatter):
#     FORMATS = {'DEFAULT'       : "%(message)s",
#                logging.WARNING : "WARNING: %(message)s",
#                logging.ERROR   : "ERROR: %(message)s"}
#     def format(self, record):
# def file_found(filename,force):


import os,sys
import inspect
import numpy as np
import fitsio
import ugali.utils.projector
import ugali.utils.idl
    import argparse
from os.path import join,abspath,split
from collections import OrderedDict as odict
from numpy.lib.recfunctions import stack_arrays
from ugali.utils.projector import gal2cel, cel2gal
from ugali.utils.healpix import ang2pix
from ugali.utils.shell import get_ugali_dir, get_cat_dir
from ugali.utils.logger import logger

class Corwen04(SourceCatalog):
    """
    Modern compilation of the New General Catalogue and IC
    """
    def _load(self,filename):
        kwargs = dict(delimiter=[1,1,4,15,3,3,8,3,3,7],usecols=[1,2]+list(range(4,10)),dtype=['S1']+[int]+6*[float])
        if filename is None: 
            raw = []
            for basename in ['VII_239A/ngcpos.dat','VII_239A/icpos.dat']:
                filename = os.path.join(self.DATADIR,basename)
                raw.append(np.genfromtxt(filename,**kwargs))
            raw = np.concatenate(raw)
        else:
            raw = np.genfromtxt(filename,**kwargs)
        self.filename = filename

        # Some entries are missing...
        raw['f4'] = np.where(np.isnan(raw['f4']),0,raw['f4'])
        raw['f7'] = np.where(np.isnan(raw['f7']),0,raw['f7'])

        self.data.resize(len(raw))
        names = np.where(raw['f0'] == 'N', 'NGC %04i', 'IC %04i')
        self.data['name'] = np.char.mod(names,raw['f1'])

        ra = raw[['f2','f3','f4']].view(float).reshape(len(raw),-1)
        dec = raw[['f5','f6','f7']].view(float).reshape(len(raw),-1)
        self.data['ra'] = ugali.utils.projector.hms2dec(ra)
        self.data['dec'] = ugali.utils.projector.dms2dec(dec)


# Path: ugali/utils/projector.py
# class SphericalRotator:
# class Projector:
#     def __init__(self, lon_ref, lat_ref, zenithal=False):
#     def setReference(self, lon_ref, lat_ref, zenithal=False):
#     def cartesian(self,lon,lat):
#     def rotate(self, lon, lat, invert=False):
#     def __init__(self, lon_ref, lat_ref, proj_type = 'ait'):
#             def rotate(lon,lat,invert=False):
#     def sphereToImage(self, lon, lat):
#     def imageToSphere(self, x, y):
# def sphere2image(lon_ref,lat_ref,lon,lat):
# def image2sphere(lon_ref,lat_ref,x,y):
# def cartesianSphereToImage(lon, lat):
# def cartesianImageToSphere(x,y):
# def aitoffSphereToImage(lon, lat):
# def aitoffImageToSphere(x, y):
# def gnomonicSphereToImage(lon, lat):
# def gnomonicImageToSphere(x, y):
# def angsep2(lon_1, lat_1, lon_2, lat_2):
# def angsep(lon1,lat1,lon2,lat2):
# def galToCel(ll, bb):
# def celToGal(ra, dec):
# def estimate_angle(angle, origin, new_frame, offset=1e-7):
# def gal2cel_angle(glon,glat,angle,offset=1e-7):
# def cel2gal_angle(ra,dec,angle,offset=1e-7):
# def dec2hms(dec):
# def dec2dms(dec):
# def hms2dec(hms):
# def dms2dec(dms):
# def sr2deg(solid_angle):
# def deg2sr(solid_angle):
# def distanceToDistanceModulus(distance):
# def distanceModulusToDistance(distance_modulus):
# def ang2const(lon,lat,coord='gal'):
# def ang2iau(lon,lat,coord='gal'):
# def match(lon1, lat1, lon2, lat2, tol=None, nnearest=1):
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
#     DEGREE = 360.
#     HOUR = 24.
#     MINUTE = 60.
#     SECOND = 3600.
# 
# Path: ugali/utils/healpix.py
# def ang2pix(nside, lon, lat, nest=False):
#     """
#     Input (lon, lat) in degrees instead of (theta, phi) in radians
#     """
#     theta = np.radians(90. - lat)
#     phi = np.radians(lon)
#     return hp.ang2pix(nside, theta, phi, nest=nest)
# 
# Path: ugali/utils/logger.py
# class SpecialFormatter(logging.Formatter):
#     FORMATS = {'DEFAULT'       : "%(message)s",
#                logging.WARNING : "WARNING: %(message)s",
#                logging.ERROR   : "ERROR: %(message)s"}
#     def format(self, record):
# def file_found(filename,force):


# Path: ugali/candidate/associate.py
import os,sys
import inspect
import numpy as np
import fitsio
import ugali.utils.projector
import ugali.utils.idl
    import argparse
from os.path import join,abspath,split
from collections import OrderedDict as odict
from numpy.lib.recfunctions import stack_arrays
from ugali.utils.projector import gal2cel, cel2gal
from ugali.utils.healpix import ang2pix
from ugali.utils.shell import get_ugali_dir, get_cat_dir
from ugali.utils.logger import logger


class Corwen04(SourceCatalog):
    """
    Modern compilation of the New General Catalogue and IC
    """
    def _load(self,filename):
        kwargs = dict(delimiter=[1,1,4,15,3,3,8,3,3,7],usecols=[1,2]+list(range(4,10)),dtype=['S1']+[int]+6*[float])
        if filename is None: 
            raw = []
            for basename in ['VII_239A/ngcpos.dat','VII_239A/icpos.dat']:
                filename = os.path.join(self.DATADIR,basename)
                raw.append(np.genfromtxt(filename,**kwargs))
            raw = np.concatenate(raw)
        else:
            raw = np.genfromtxt(filename,**kwargs)
        self.filename = filename

        # Some entries are missing...
        raw['f4'] = np.where(np.isnan(raw['f4']),0,raw['f4'])
        raw['f7'] = np.where(np.isnan(raw['f7']),0,raw['f7'])

        self.data.resize(len(raw))
        names = np.where(raw['f0'] == 'N', 'NGC %04i', 'IC %04i')
        self.data['name'] = np.char.mod(names,raw['f1'])

        ra = raw[['f2','f3','f4']].view(float).reshape(len(raw),-1)
        dec = raw[['f5','f6','f7']].view(float).reshape(len(raw),-1)
        self.data['ra'] = ugali.utils.projector.hms2dec(ra)
        self.data['dec'] = ugali.utils.projector.dms2dec(dec)


        glon,glat = cel2gal(self.data['ra'],self.data['dec'])