Go to the documentation of this file.00001 """\
00002 A representation of a spectral line catalog.
00003 """
00004 __revision__ = "$Revision: 1865 $"
00005 from asap._asap import linecatalog as lcbase
00006 from asap.logging import asaplog
00007 import os
00008
00009 class linecatalog(lcbase):
00010 """\
00011 This class is a warpper for line catalogs. These can be either ASCII tables
00012 or the tables saved from this class.
00013
00014 ASCII tables have the following restrictions:
00015
00016 * Comments can be present through lines starting with '#'.
00017
00018 * The first column contains the name of the Molecule. This can't contain
00019 spaces, if it does it has to be wrapped in double-quotes.
00020
00021 * The second column contains the frequency of the transition.
00022
00023 * The third column contains the error in frequency.
00024
00025 * The fourth column contains a value describing the intensity.
00026
00027 """
00028
00029 def __init__(self, name):
00030 fpath = os.path.abspath(os.path.expandvars(os.path.expanduser(name)))
00031 if os.path.exists(fpath):
00032 lcbase.__init__(self, fpath)
00033 else:
00034 msg = "File '%s' not found" % fpath
00035 raise IOError(msg)
00036
00037 def __repr__(self):
00038 return lcbase.summary(self, -1)
00039
00040 def summary(self):
00041 """
00042 Print the contents of the table.
00043 """
00044 try:
00045 from IPython.genutils import page as pager
00046 except ImportError:
00047 from pydoc import pager
00048 pager(lcbase.summary(self, -1))
00049
00050 def set_name(self, name, mode="pattern"):
00051 """\
00052 Set a name restriction on the table. This can be a standard unix-style
00053 pattern or a regular expression.
00054
00055 Parameters:
00056
00057 name: the name patterrn/regex
00058
00059 mode: the matching mode, i.e. "pattern" (default) or "regex"
00060
00061 """
00062 validmodes = "pattern regex".split()
00063 if not mode.lower() in validmodes:
00064 return
00065 lcbase.set_name(self, name, mode)
00066
00067 def set_frequency_limits(self, fmin=1.0, fmax=120.0, unit="GHz"):
00068 """\
00069 Set frequency limits on the table.
00070
00071 Parameters:
00072
00073 fmin: the lower bound
00074
00075 fmax: the upper bound
00076
00077 unit: the frequency unit (default "GHz")
00078
00079 .. note:: The underlying table contains frequency values in MHz
00080 """
00081 base = { "GHz": 1000.0, "MHz": 1.0 }
00082 if not base.has_key(unit):
00083 raise ValueError("%s is not a valid unit." % unit)
00084
00085 lcbase.set_frequency_limits(self, fmin*base[unit], fmax*base[unit])
00086
00087 def set_strength_limits(self, smin, smax):
00088 """\
00089 Set line strength limits on the table (arbitrary units)
00090
00091 Parameters:
00092
00093 smin: the lower bound
00094
00095 smax: the upper bound
00096
00097 """
00098 lcbase.set_strength_limits(self, smin, smax)
00099
00100 def save(self, name, overwrite=False):
00101 """\
00102 Save the subset of the table to disk. This uses an internal data format
00103 and can be read in again.
00104 """
00105 name = os.path.expanduser(os.path.expandvars(name))
00106 if os.path.isfile(name) or os.path.isdir(name):
00107 if not overwrite:
00108 msg = "File %s exists." % name
00109 raise IOError(msg)
00110 lcbase.save(self, name)
00111
00112 def reset(self):
00113 """\
00114 Reset the table to its initial state, i.e. undo all calls to ``set_``.
00115 """
00116 lcbase.reset(self)
00117
00118 def get_row(self, row=0):
00119 """\
00120 Get the values in a specified row of the table.
00121
00122 Parameters:
00123
00124 row: the row to retrieve
00125
00126 """
00127 if row < 0 or row > len(self)-1:
00128 raise IndexError("Row index out of bounds.")
00129 freq = lcbase.get_frequency(self, row)
00130 name = lcbase.get_name(self, row)
00131 return { 'name':name, 'value': freq }
00132
00133 def __len__(self):
00134 return self.nrow()
00135
00136 def __getitem__(self, k):
00137 if k < 0:
00138 k = self.nrow()-k
00139 return self.get_row(k)