# Path: ugali/utils/healpix.py
# def pix2ang(nside, pix, nest=False):
#     """
#     Return (lon, lat) in degrees instead of (theta, phi) in radians
#     """
#     theta, phi =  hp.pix2ang(nside, pix, nest=nest)
#     lon = phi2lon(phi)
#     lat = theta2lat(theta)
#     return lon, lat
# 
# 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/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
import argparse
import numpy as np
import numpy.lib.recfunctions as recfuncs
            import fitsio
            import yaml
from ugali.utils.healpix import pix2ang
from ugali.utils.projector import cel2gal
from ugali.utils.logger import logger
        from ugali import __version__
            from numpy.lib import NumpyVersion
        base,ext = os.path.splitext(filename)
        if (ext=='.fits'):
            data = fitsio.read(filename)
        elif (ext=='.txt'):
            if NumpyVersion(np.__version__) < '1.14.0':
                data = np.genfromtxt(filename,names=True,dtype=None)
            else:
                data = np.genfromtxt(filename,names=True,dtype=None,encoding=None)
            #data = np.genfromtxt(filename,unpack=True,usecols=list(range(5)),dtype=object,names=True)
        elif (ext=='.yaml'):
            data = [(k,v['kernel']['lon']['value'],v['kernel']['lat']['value'],0.5,'CEL') for k,v in yaml.load(open(filename)).items()]
            data = np.rec.fromrecords(data,names=['name','lon','lat','radius','coord'])
        else:
            msg = "Unrecognized file type: %s"%filename
            raise IOError(msg)

        data = np.atleast_1d(data)
        data.dtype.names = list(map(str.lower,data.dtype.names))

        # Deal with one-line input files
        #if data.ndim == 1: data = np.array([data]).T
        names = data['name']
        out   = data[['lon','lat','radius']].copy()
         
        coord = np.char.lower(data['coord'])
        gal = (coord=='gal')
        cel = (coord=='cel')
        hpx = (coord=='hpx')
         
        if cel.any():

# Path: ugali/utils/healpix.py
# def pix2ang(nside, pix, nest=False):
#     """
#     Return (lon, lat) in degrees instead of (theta, phi) in radians
#     """
#     theta, phi =  hp.pix2ang(nside, pix, nest=nest)
#     lon = phi2lon(phi)
#     lat = theta2lat(theta)
#     return lon, lat
# 
# 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/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/utils/parser.py
import os
import argparse
import numpy as np
import numpy.lib.recfunctions as recfuncs
            import fitsio
            import yaml
from ugali.utils.healpix import pix2ang
from ugali.utils.projector import cel2gal
from ugali.utils.logger import logger
        from ugali import __version__
            from numpy.lib import NumpyVersion

        base,ext = os.path.splitext(filename)
        if (ext=='.fits'):
            data = fitsio.read(filename)
        elif (ext=='.txt'):
            if NumpyVersion(np.__version__) < '1.14.0':
                data = np.genfromtxt(filename,names=True,dtype=None)
            else:
                data = np.genfromtxt(filename,names=True,dtype=None,encoding=None)
            #data = np.genfromtxt(filename,unpack=True,usecols=list(range(5)),dtype=object,names=True)
        elif (ext=='.yaml'):
            data = [(k,v['kernel']['lon']['value'],v['kernel']['lat']['value'],0.5,'CEL') for k,v in yaml.load(open(filename)).items()]
            data = np.rec.fromrecords(data,names=['name','lon','lat','radius','coord'])
        else:
            msg = "Unrecognized file type: %s"%filename
            raise IOError(msg)

        data = np.atleast_1d(data)
        data.dtype.names = list(map(str.lower,data.dtype.names))

        # Deal with one-line input files
        #if data.ndim == 1: data = np.array([data]).T
        names = data['name']
        out   = data[['lon','lat','radius']].copy()
         
        coord = np.char.lower(data['coord'])
        gal = (coord=='gal')
        cel = (coord=='cel')
        hpx = (coord=='hpx')
         
        if cel.any():

            glon,glat = cel2gal(data['lon'][cel],data['lat'][cel])