# Path: sedfitter/fit_info.py
# class FitInfoFile(object):
# 
#     def __init__(self, fits, mode=None):
# 
#         if isinstance(fits, six.string_types):
# 
#             if mode not in 'wr':
#                 raise ValueError('mode should be r or w')
# 
#             self._handle = open(fits, mode + 'b')
#             self._mode = mode
# 
#             if mode == 'r':
#                 self._first_meta = FitInfoMeta()
#                 self._first_meta.model_dir = pickle.load(self._handle)
#                 self._first_meta.filters = pickle.load(self._handle)
#                 self._first_meta.extinction_law = pickle.load(self._handle)
#             else:
#                 self._first_meta = None
# 
#             self._fits = None
# 
#         elif isinstance(fits, FitInfo):
# 
#             self._fits = [fits]
# 
#         elif isinstance(fits, (list, tuple)):
# 
#             for info in self._fits[1:]:
#                 if info.meta != self._fits[0].meta:
#                     raise ValueError("The meta property of all FitInfo instances should match")
# 
#             self._fits = fits
# 
#         else:
# 
#             raise TypeError('fits should be a string, FitInfo instance, or iterable of FitInfo instances')
# 
#     @property
#     def meta(self):
#         if self._fits is None:
#             if self._mode != 'r':
#                 raise ValueError("meta property is only available in read mode")
#             return self._first_meta
#         else:
#             return self._fits[0].meta
# 
#     def write(self, info):
# 
#         if self._mode != 'w':
#             raise ValueError("File not open for writing")
# 
#         # We only write the metadata for the first source, and we then check
#         # the metadata of other sources against the first one to make sure it
#         # matches.
# 
#         if self._first_meta is None:
#             pickle.dump(info.meta.model_dir, self._handle, 2)
#             pickle.dump(info.meta.filters, self._handle, 2)
#             pickle.dump(info.meta.extinction_law, self._handle, 2)
#             self._first_meta = info.meta
#         else:
#             if not info.meta == self._first_meta:
#                 raise ValueError("meta does not match previously written value")
# 
#         pickle.dump(info, self._handle, 2)
# 
#     def close(self):
#         if self._fits is None:
#             self._handle.close()
# 
#     def __iter__(self):
#         if self._fits is None:
#             if self._mode != 'r':
#                 raise ValueError("File not open for reading")
#             while True:
#                 try:
#                     info = pickle.load(self._handle)
#                 except EOFError:
#                     return
#                 else:
#                     info.meta = self._first_meta
#                     yield info
#         else:
#             for info in self._fits:
#                 yield info
# 
# Path: sedfitter/models.py
# def load_parameter_table(model_dir):
# 
#     if os.path.exists(model_dir + '/parameters.fits'):
#         t = read_table(model_dir + '/parameters.fits')
#     elif os.path.exists(model_dir + '/parameters.fits.gz'):
#         t = read_table(model_dir + '/parameters.fits.gz')
#     else:
#         raise Exception("Parameter file not found in %s" % model_dir)
# 
#     return t


import numpy as np
from .fit_info import FitInfoFile
from .models import load_parameter_table

NODATA = '-'.center(10)


def write_parameter_ranges(input_fits, output_file, select_format=("N", 1), additional={}):
    """
    Write out an ASCII file with ranges of paramters for each source.

    Parameters
    ----------
    input_fits : str or :class:`sedfitter.fit_info.FitInfo` or iterable
        This should be either a file containing the fit information, a
        :class:`sedfitter.fit_info.FitInfo` instance, or an iterable containing
        :class:`sedfitter.fit_info.FitInfo` instances.
    output_file : str, optional
        The output ASCII file containing the parameter ranges
    select_format : tuple, optional
        Tuple specifying which fits should be output. See the documentation
        for a description of the tuple syntax.
    additional : dict, optional
        A dictionary giving additional parameters for each model. This should
        be a dictionary where each key is a parameter, and each value is a
        dictionary mapping the model names to the parameter values.
    """

    # Open input and output file
    fin = FitInfoFile(input_fits, 'r')
    fout = open(output_file, 'w')

    # Read in table of parameters for model grid

# Path: sedfitter/fit_info.py
# class FitInfoFile(object):
# 
#     def __init__(self, fits, mode=None):
# 
#         if isinstance(fits, six.string_types):
# 
#             if mode not in 'wr':
#                 raise ValueError('mode should be r or w')
# 
#             self._handle = open(fits, mode + 'b')
#             self._mode = mode
# 
#             if mode == 'r':
#                 self._first_meta = FitInfoMeta()
#                 self._first_meta.model_dir = pickle.load(self._handle)
#                 self._first_meta.filters = pickle.load(self._handle)
#                 self._first_meta.extinction_law = pickle.load(self._handle)
#             else:
#                 self._first_meta = None
# 
#             self._fits = None
# 
#         elif isinstance(fits, FitInfo):
# 
#             self._fits = [fits]
# 
#         elif isinstance(fits, (list, tuple)):
# 
#             for info in self._fits[1:]:
#                 if info.meta != self._fits[0].meta:
#                     raise ValueError("The meta property of all FitInfo instances should match")
# 
#             self._fits = fits
# 
#         else:
# 
#             raise TypeError('fits should be a string, FitInfo instance, or iterable of FitInfo instances')
# 
#     @property
#     def meta(self):
#         if self._fits is None:
#             if self._mode != 'r':
#                 raise ValueError("meta property is only available in read mode")
#             return self._first_meta
#         else:
#             return self._fits[0].meta
# 
#     def write(self, info):
# 
#         if self._mode != 'w':
#             raise ValueError("File not open for writing")
# 
#         # We only write the metadata for the first source, and we then check
#         # the metadata of other sources against the first one to make sure it
#         # matches.
# 
#         if self._first_meta is None:
#             pickle.dump(info.meta.model_dir, self._handle, 2)
#             pickle.dump(info.meta.filters, self._handle, 2)
#             pickle.dump(info.meta.extinction_law, self._handle, 2)
#             self._first_meta = info.meta
#         else:
#             if not info.meta == self._first_meta:
#                 raise ValueError("meta does not match previously written value")
# 
#         pickle.dump(info, self._handle, 2)
# 
#     def close(self):
#         if self._fits is None:
#             self._handle.close()
# 
#     def __iter__(self):
#         if self._fits is None:
#             if self._mode != 'r':
#                 raise ValueError("File not open for reading")
#             while True:
#                 try:
#                     info = pickle.load(self._handle)
#                 except EOFError:
#                     return
#                 else:
#                     info.meta = self._first_meta
#                     yield info
#         else:
#             for info in self._fits:
#                 yield info
# 
# Path: sedfitter/models.py
# def load_parameter_table(model_dir):
# 
#     if os.path.exists(model_dir + '/parameters.fits'):
#         t = read_table(model_dir + '/parameters.fits')
#     elif os.path.exists(model_dir + '/parameters.fits.gz'):
#         t = read_table(model_dir + '/parameters.fits.gz')
#     else:
#         raise Exception("Parameter file not found in %s" % model_dir)
# 
#     return t


# Path: sedfitter/write_parameter_ranges.py
import numpy as np
from .fit_info import FitInfoFile
from .models import load_parameter_table


NODATA = '-'.center(10)


def write_parameter_ranges(input_fits, output_file, select_format=("N", 1), additional={}):
    """
    Write out an ASCII file with ranges of paramters for each source.

    Parameters
    ----------
    input_fits : str or :class:`sedfitter.fit_info.FitInfo` or iterable
        This should be either a file containing the fit information, a
        :class:`sedfitter.fit_info.FitInfo` instance, or an iterable containing
        :class:`sedfitter.fit_info.FitInfo` instances.
    output_file : str, optional
        The output ASCII file containing the parameter ranges
    select_format : tuple, optional
        Tuple specifying which fits should be output. See the documentation
        for a description of the tuple syntax.
    additional : dict, optional
        A dictionary giving additional parameters for each model. This should
        be a dictionary where each key is a parameter, and each value is a
        dictionary mapping the model names to the parameter values.
    """

    # Open input and output file
    fin = FitInfoFile(input_fits, 'r')
    fout = open(output_file, 'w')

    # Read in table of parameters for model grid

    t = load_parameter_table(fin.meta.model_dir)