# 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/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


import os
import time
import glob
import shutil
import numpy as np
import numpy.lib.recfunctions as recfuncs
import fitsio
import ugali.analysis.loglike
import ugali.simulation.simulator
import ugali.utils.skymap
        import ugali.utils.plotting
        import pylab as plt
import pylab as plt
from os.path import join, splitext, exists
from ugali.analysis.pipeline import Pipeline
from ugali.utils.shell import mkdir
from ugali.utils.logger import logger
from ugali.utils.healpix import pix2ang
        if self.opts.num is None: self.opts.num = self.config['simulator']['njobs']
        for i in range(self.opts.num):
            outfile=join(outdir,self.config['output']['simfile']%i)
            base = splitext(os.path.basename(outfile))[0]
            logfile=join(logdir,base+'.log')
            jobname=base
            script = self.config['simulator']['script']
            cmd='%s %s %s --seed %i'%(script,configfile,outfile,i)
            #cmd='%s %s %s'%(script,self.opts.config,outfile)
            self.batch.submit(cmd,jobname,logfile)
            time.sleep(0.1)

    if 'analyze' in self.opts.run:
        logger.info("Running 'analyze'...")
        dirname = self.config['simulate']['dirname']
        catfiles = sorted(glob.glob(join(dirname,self.config['simulate']['catfile'])))
        popfile = join(dirname,self.config['simulate']['popfile'])
        batch = self.config['simulate']['batch']

        for i,catfile in enumerate(catfiles):
            basename = os.path.basename(catfile)
            outfile = join(outdir,basename)
            base = splitext(os.path.basename(outfile))[0]
            logfile=join(logdir,base+'.log')
            jobname=base

            if exists(outfile) and not self.opts.force:
                msg = "Found %s;"%outfile
                if exists(logfile) and len(self.batch.bfail(logfile)):
                    msg += " failed."

# 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/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/pipeline/run_06.0_simulate.py
import os
import time
import glob
import shutil
import numpy as np
import numpy.lib.recfunctions as recfuncs
import fitsio
import ugali.analysis.loglike
import ugali.simulation.simulator
import ugali.utils.skymap
        import ugali.utils.plotting
        import pylab as plt
import pylab as plt
from os.path import join, splitext, exists
from ugali.analysis.pipeline import Pipeline
from ugali.utils.shell import mkdir
from ugali.utils.logger import logger
from ugali.utils.healpix import pix2ang

        if self.opts.num is None: self.opts.num = self.config['simulator']['njobs']
        for i in range(self.opts.num):
            outfile=join(outdir,self.config['output']['simfile']%i)
            base = splitext(os.path.basename(outfile))[0]
            logfile=join(logdir,base+'.log')
            jobname=base
            script = self.config['simulator']['script']
            cmd='%s %s %s --seed %i'%(script,configfile,outfile,i)
            #cmd='%s %s %s'%(script,self.opts.config,outfile)
            self.batch.submit(cmd,jobname,logfile)
            time.sleep(0.1)

    if 'analyze' in self.opts.run:
        logger.info("Running 'analyze'...")
        dirname = self.config['simulate']['dirname']
        catfiles = sorted(glob.glob(join(dirname,self.config['simulate']['catfile'])))
        popfile = join(dirname,self.config['simulate']['popfile'])
        batch = self.config['simulate']['batch']

        for i,catfile in enumerate(catfiles):
            basename = os.path.basename(catfile)
            outfile = join(outdir,basename)
            base = splitext(os.path.basename(outfile))[0]
            logfile=join(logdir,base+'.log')
            jobname=base

            if exists(outfile) and not self.opts.force:
                msg = "Found %s;"%outfile
                if exists(logfile) and len(self.batch.bfail(logfile)):
                    msg += " failed."

                    logger.info(msg)