Commit 030d68d6 authored by Yannick Roehlly's avatar Yannick Roehlly

Create an utility function to read table files

parent 387c9f25
......@@ -30,6 +30,7 @@ from copy import deepcopy
from scipy import stats
from progressbar import ProgressBar
from matplotlib import pyplot as plt
from ..utils import read_table
from . import AnalysisModule
from ..creation_modules import get_module
from ..warehouse import SedWarehouse
......@@ -183,10 +184,7 @@ class Psum(AnalysisModule):
# Read the observation table and complete it by adding error where
# none is provided and by adding the systematic deviation.
if data_file.endswith(('fits', 'vot', 'xml')):
obs_table = Table.read(data_file)
else:
obs_table = Table.read(data_file, format='ascii')
obs_table = read_table(data_file)
for name in filter_list:
name_err = name + '_err'
if name_err not in column_list:
......
......@@ -13,6 +13,7 @@ This module reads a SED spectrum from a file.
from astropy.table import Table
from collections import OrderedDict
from ..utils import read_table
from . import CreationModule
......@@ -49,7 +50,7 @@ class LoadSpecFile(CreationModule):
"""
filename = self.parameters['filename']
table = Table(filename, verbose=False)
table = read_table(filename)
sed.add_module(self.name, self.parameters)
......
......@@ -14,6 +14,7 @@ This module reads the star formation history in a file.
from astropy.table import Table
import numpy as np
from collections import OrderedDict
from ..utils import read_table
from . import CreationModule
......@@ -64,10 +65,7 @@ class SfhFromFile(CreationModule):
"""
filename = self.parameters['filename']
if filename.endswith(('fits', 'vot', 'xml')):
table = Table.read(filename)
else:
table = Table.read(filename, format='ascii')
table = read_table(filename)
time_column_name = table.columns.keys[0]
time_grid = table[time_column_name]
......
......@@ -14,6 +14,7 @@ from glob import glob # To allow the use of glob() in "eval..."
from textwrap import wrap
from .tools import param_dict_combine
from ..data import Database
from ..utils import read_table
from .. import creation_modules
from .. import analysis_modules
......@@ -149,10 +150,7 @@ class Configuration(object):
filter_list = base.get_filter_list()[0]
# Finding the known filters in the data table
if self.config['data_file'].endswith(('fits', 'vot', 'xml')):
obs_table = Table.read(self.config['data_file'])
else:
obs_table = Table.read(self.config['data_file'], format='ascii')
obs_table = read_table(self.config['data_file'])
column_list = []
for column in obs_table.columns:
filter_name = column[:-4] if column.endswith('_err') else column
......
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Yannick Roehlly
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly
"""
Various utility function for pcigale
"""
from astropy.table import Table
from astropy.io.ascii.core import InconsistentTableError
def read_table(file_):
"""Read a data table using astropy
This function first tries to automatically read the table with astropy,
if that fails, it tries with the ascii format.
Parameters
----------
file_ : string
Name of the file to read.
Return
------
an astropy.table.Table object
Raise
-----
An error is raised when the table can not be parsed.
"""
try:
table = Table.read(file_)
except Exception: # astropy should raise a specific exception
try:
table = Table.read(file_, format="ascii")
except InconsistentTableError:
raise StandardError("The file <{}> can not be parsed as a data "
"table.".format(file_))
return table
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment