# Path: ugali/utils/projector.py
# def celToGal(ra, dec):
#     """
#     Converts Celestial J2000 (deg) to Calactic (deg) coordinates
#     """
#     dec = np.radians(dec)
#     sin_dec = np.sin(dec)
#     cos_dec = np.cos(dec)
# 
#     ra = np.radians(ra)    
#     ra_gp = np.radians(192.85948)
#     de_gp = np.radians(27.12825)
# 
#     sin_ra_gp = np.sin(ra - ra_gp)
#     cos_ra_gp = np.cos(ra - ra_gp)
# 
#     lcp = np.radians(122.932)    
#     sin_b = (np.sin(de_gp) * sin_dec) \
#             + (np.cos(de_gp) * cos_dec * cos_ra_gp)
#     lcpml = np.arctan2(cos_dec * sin_ra_gp,
#                           (np.cos(de_gp) * sin_dec) \
#                           - (np.sin(de_gp) * cos_dec * cos_ra_gp))
#     bb = np.arcsin(sin_b)
#     ll = (lcp - lcpml + (2. * np.pi)) % (2. * np.pi)
#     return np.degrees(ll), np.degrees(bb)
# 
# 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 healpy
import pylab as plt
import numpy
import ugali.utils.skymap
from ugali.utils.projector import celToGal
from ugali.utils.logger import logger
    from optparse import OptionParser
    if opts.verbose: logger.setLevel(logger.DEBUG)

    map = ugali.utils.skymap.readSparseHealpixMaps(args,opts.field)
    if opts.coord.upper() == "GAL":
        coord = 'G'
    elif opts.coord.upper() == "CEL":
        coord = 'GC'
    if opts.proj.upper() == "MOL":
        #map = numpy.where( map < 20, healpy.UNSEEN, map)
        healpy.mollview(map,coord=coord,xsize=1000,min=20)
    elif opts.proj.upper() == "CAR":
        healpy.cartview(map,coord=coord,xsize=1000)
    else:
        raise Exception("...")
    healpy.graticule()

    if opts.targets:
        targets = numpy.genfromtxt(opts.targets,unpack=True,dtype=None)
        if not targets.shape: targets = targets.reshape(-1)
        coord = 'CG' # For RA/DEC input
        healpy.projscatter(targets['f1'],targets['f2'],
                           lonlat=True,coord=coord,marker='o',c='w')
        fig = plt.gcf()
        # This is pretty hackish (but is how healpy does it...)
        for ax in fig.get_axes():
            if isinstance(ax,healpy.projaxes.SphericalProjAxes): break

        for target in targets:
            text = TARGETS[target[0]][0]
            kwargs = TARGETS[target[0]][1]

# Path: ugali/utils/projector.py
# def celToGal(ra, dec):
#     """
#     Converts Celestial J2000 (deg) to Calactic (deg) coordinates
#     """
#     dec = np.radians(dec)
#     sin_dec = np.sin(dec)
#     cos_dec = np.cos(dec)
# 
#     ra = np.radians(ra)    
#     ra_gp = np.radians(192.85948)
#     de_gp = np.radians(27.12825)
# 
#     sin_ra_gp = np.sin(ra - ra_gp)
#     cos_ra_gp = np.cos(ra - ra_gp)
# 
#     lcp = np.radians(122.932)    
#     sin_b = (np.sin(de_gp) * sin_dec) \
#             + (np.cos(de_gp) * cos_dec * cos_ra_gp)
#     lcpml = np.arctan2(cos_dec * sin_ra_gp,
#                           (np.cos(de_gp) * sin_dec) \
#                           - (np.sin(de_gp) * cos_dec * cos_ra_gp))
#     bb = np.arcsin(sin_b)
#     ll = (lcp - lcpml + (2. * np.pi)) % (2. * np.pi)
#     return np.degrees(ll), np.degrees(bb)
# 
# 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/scratch/PlotAllSkyHealpix.py
import healpy
import pylab as plt
import numpy
import ugali.utils.skymap
from ugali.utils.projector import celToGal
from ugali.utils.logger import logger
    from optparse import OptionParser

    if opts.verbose: logger.setLevel(logger.DEBUG)

    map = ugali.utils.skymap.readSparseHealpixMaps(args,opts.field)
    if opts.coord.upper() == "GAL":
        coord = 'G'
    elif opts.coord.upper() == "CEL":
        coord = 'GC'
    if opts.proj.upper() == "MOL":
        #map = numpy.where( map < 20, healpy.UNSEEN, map)
        healpy.mollview(map,coord=coord,xsize=1000,min=20)
    elif opts.proj.upper() == "CAR":
        healpy.cartview(map,coord=coord,xsize=1000)
    else:
        raise Exception("...")
    healpy.graticule()

    if opts.targets:
        targets = numpy.genfromtxt(opts.targets,unpack=True,dtype=None)
        if not targets.shape: targets = targets.reshape(-1)
        coord = 'CG' # For RA/DEC input
        healpy.projscatter(targets['f1'],targets['f2'],
                           lonlat=True,coord=coord,marker='o',c='w')
        fig = plt.gcf()
        # This is pretty hackish (but is how healpy does it...)
        for ax in fig.get_axes():
            if isinstance(ax,healpy.projaxes.SphericalProjAxes): break

        for target in targets:
            text = TARGETS[target[0]][0]
            kwargs = TARGETS[target[0]][1]

            glon,glat = celToGal(target[1],target[2])