# 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/preprocess/padova.py
# class Download(object):
# 
#     isochrone = None
#     
#     def __init__(self,survey='des',**kwargs):
#         self.survey=survey.lower()
# 
#     def create_grid(self,abins,zbins):
#         arange = np.linspace(abins[0],abins[1],abins[2]+1)
#         zrange = np.logspace(np.log10(zbins[0]),np.log10(zbins[1]),zbins[2]+1)
#         aa,zz = np.meshgrid(arange,zrange)
#         return aa.flatten(),zz.flatten()
# 
#     def print_info(self,age,metallicity):
#         params = dict(age=age,z=metallicity)
#         params['name'] = self.__class__.__name__
#         params['survey'] = self.survey
#         params['feh'] = self.isochrone.z2feh(metallicity)
#         msg = 'Downloading: %(name)s (survey=%(survey)s, age=%(age).1fGyr, Z=%(z).5f, Fe/H=%(feh).3f)'%params
#         logger.info(msg)
#         return msg
# 
#     def query_server(self,outfile,age,metallicity):
#         msg = "'query_server' not implemented by base class."
#         logger.error(msg)
#         raise RuntimeError(msg)
# 
#     @classmethod
#     def verify(cls,filename,survey,age,metallicity):
#         msg = "'verify' not implemented by base class."
#         logger.error(msg)
#         raise RuntimeError(msg)
# 
#     def download(self,age,metallicity,outdir=None,force=False):
#         """
#         Check valid parameter range and download isochrones from:
#         http://stev.oapd.inaf.it/cgi-bin/cmd
#         """
#         if outdir is None: outdir = './'
#         basename = self.isochrone.params2filename(age,metallicity)
#         outfile = os.path.join(outdir,basename)
#             
#         if os.path.exists(outfile) and not force:
#             try:
#                 self.verify(outfile,self.survey,age,metallicity)
#                 logger.info("Found %s; skipping..."%(outfile))
#                 return
#             except Exception as e:
#                 msg = "Overwriting corrupted %s..."%(outfile)
#                 logger.warn(msg)
#                 #os.remove(outfile)
#                 
#         mkdir(outdir)
# 
#         self.print_info(age,metallicity)
# 
#         try:
#             self.query_server(outfile,age,metallicity)
#         except Exception as e:
#             logger.debug(str(e))
#             raise RuntimeError('Bad server response')
# 
#         if not os.path.exists(outfile):
#             raise RuntimeError('Download failed')
# 
#         try:
#             self.verify(outfile,self.survey,age,metallicity)
#         except Exception as e:
#             msg = "Output file is corrupted."
#             logger.error(msg)
#             #os.remove(outfile)
#             raise(e)
# 
#         return outfile


import os
import re
import requests
import sys
import copy
import tempfile
import subprocess
import shutil
import numpy
import numpy as np
import ugali.utils.logger
import ugali.utils.shell
import ugali.analysis.isochrone as iso
    import ugali.utils.parser
    from urllib.parse import urlencode
    from urllib.request import urlopen
    from urllib import urlencode
    from urllib2 import urlopen
from multiprocessing import Pool
from collections import OrderedDict as odict
from ugali.utils.logger import logger
from ugali.utils.shell import mkdir
from ugali.preprocess.padova import Download
    from ugali.utils.factory import factory
            from http.client import HTTPConnection
            from httplib import HTTPConnection
    def verify(cls, filename, survey, age, metallicity):
        nlines=8
        with open(filename,'r') as f:
            lines = [f.readline() for i in range(nlines)]

            if len(lines) < nlines:
                msg = "Incorrect file size"
                raise Exception(msg)

            try:
                z = lines[3].split()[4]
                assert np.allclose(metallicity,float(z),atol=1e-3)
            except:
                msg = "Metallicity does not match:\n"+lines[3]
                raise Exception(msg)

            try:
                s = lines[5].split()[2]
                assert dict_output[survey][:4] in s
            except:
                msg = "Incorrect survey:\n"+lines[5]
                raise Exception(msg)

            try:
                a = lines[7].split('=')[1].strip().split()[0]
                assert np.allclose(age,float(a),atol=1e-5)
            except:
                msg = "Age does not match:\n"+lines[7]
                raise Exception(msg)


# 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/preprocess/padova.py
# class Download(object):
# 
#     isochrone = None
#     
#     def __init__(self,survey='des',**kwargs):
#         self.survey=survey.lower()
# 
#     def create_grid(self,abins,zbins):
#         arange = np.linspace(abins[0],abins[1],abins[2]+1)
#         zrange = np.logspace(np.log10(zbins[0]),np.log10(zbins[1]),zbins[2]+1)
#         aa,zz = np.meshgrid(arange,zrange)
#         return aa.flatten(),zz.flatten()
# 
#     def print_info(self,age,metallicity):
#         params = dict(age=age,z=metallicity)
#         params['name'] = self.__class__.__name__
#         params['survey'] = self.survey
#         params['feh'] = self.isochrone.z2feh(metallicity)
#         msg = 'Downloading: %(name)s (survey=%(survey)s, age=%(age).1fGyr, Z=%(z).5f, Fe/H=%(feh).3f)'%params
#         logger.info(msg)
#         return msg
# 
#     def query_server(self,outfile,age,metallicity):
#         msg = "'query_server' not implemented by base class."
#         logger.error(msg)
#         raise RuntimeError(msg)
# 
#     @classmethod
#     def verify(cls,filename,survey,age,metallicity):
#         msg = "'verify' not implemented by base class."
#         logger.error(msg)
#         raise RuntimeError(msg)
# 
#     def download(self,age,metallicity,outdir=None,force=False):
#         """
#         Check valid parameter range and download isochrones from:
#         http://stev.oapd.inaf.it/cgi-bin/cmd
#         """
#         if outdir is None: outdir = './'
#         basename = self.isochrone.params2filename(age,metallicity)
#         outfile = os.path.join(outdir,basename)
#             
#         if os.path.exists(outfile) and not force:
#             try:
#                 self.verify(outfile,self.survey,age,metallicity)
#                 logger.info("Found %s; skipping..."%(outfile))
#                 return
#             except Exception as e:
#                 msg = "Overwriting corrupted %s..."%(outfile)
#                 logger.warn(msg)
#                 #os.remove(outfile)
#                 
#         mkdir(outdir)
# 
#         self.print_info(age,metallicity)
# 
#         try:
#             self.query_server(outfile,age,metallicity)
#         except Exception as e:
#             logger.debug(str(e))
#             raise RuntimeError('Bad server response')
# 
#         if not os.path.exists(outfile):
#             raise RuntimeError('Download failed')
# 
#         try:
#             self.verify(outfile,self.survey,age,metallicity)
#         except Exception as e:
#             msg = "Output file is corrupted."
#             logger.error(msg)
#             #os.remove(outfile)
#             raise(e)
# 
#         return outfile


# Path: ugali/preprocess/dotter.py
import os
import re
import requests
import sys
import copy
import tempfile
import subprocess
import shutil
import numpy
import numpy as np
import ugali.utils.logger
import ugali.utils.shell
import ugali.analysis.isochrone as iso
    import ugali.utils.parser
    from urllib.parse import urlencode
    from urllib.request import urlopen
    from urllib import urlencode
    from urllib2 import urlopen
from multiprocessing import Pool
from collections import OrderedDict as odict
from ugali.utils.logger import logger
from ugali.utils.shell import mkdir
from ugali.preprocess.padova import Download
    from ugali.utils.factory import factory
            from http.client import HTTPConnection
            from httplib import HTTPConnection

    def verify(cls, filename, survey, age, metallicity):
        nlines=8
        with open(filename,'r') as f:
            lines = [f.readline() for i in range(nlines)]

            if len(lines) < nlines:
                msg = "Incorrect file size"
                raise Exception(msg)

            try:
                z = lines[3].split()[4]
                assert np.allclose(metallicity,float(z),atol=1e-3)
            except:
                msg = "Metallicity does not match:\n"+lines[3]
                raise Exception(msg)

            try:
                s = lines[5].split()[2]
                assert dict_output[survey][:4] in s
            except:
                msg = "Incorrect survey:\n"+lines[5]
                raise Exception(msg)

            try:
                a = lines[7].split('=')[1].strip().split()[0]
                assert np.allclose(age,float(a),atol=1e-5)
            except:
                msg = "Age does not match:\n"+lines[7]
                raise Exception(msg)


class Dotter2016(Download):