| 1 | """
 | 
|---|
| 2 | A representation of a spectra line catalog.
 | 
|---|
| 3 | 
 | 
|---|
| 4 | Author: Malte Marquarding
 | 
|---|
| 5 | 
 | 
|---|
| 6 | """
 | 
|---|
| 7 | __revision__ = "$Revision: 1133 $"
 | 
|---|
| 8 | from asap._asap import linecatalog as lcbase
 | 
|---|
| 9 | from asap import rcParams
 | 
|---|
| 10 | import os
 | 
|---|
| 11 | 
 | 
|---|
| 12 | class linecatalog(lcbase):
 | 
|---|
| 13 |     """
 | 
|---|
| 14 |     This class is a warpper for line catalogs. These can be either ASCII tables
 | 
|---|
| 15 |     or the tables saved from this class.
 | 
|---|
| 16 |     ASCII tables have the following restrictions:
 | 
|---|
| 17 |     Comments can be present through lines starting with '#'.
 | 
|---|
| 18 |     The first column contains the name of the Molecule. This can't contain spaces,
 | 
|---|
| 19 |     if it does it has to be wrapped in ""
 | 
|---|
| 20 |     The second column contains the frequency of the transition.
 | 
|---|
| 21 |     The third column contains the error in frequency.
 | 
|---|
| 22 |     The fourth column contains a value describing the intensity
 | 
|---|
| 23 |     """
 | 
|---|
| 24 | 
 | 
|---|
| 25 |     def __init__(self, name):
 | 
|---|
| 26 |         lcbase.__init__(self, name)
 | 
|---|
| 27 | 
 | 
|---|
| 28 |     def summary(self):
 | 
|---|
| 29 |         """
 | 
|---|
| 30 |         Print the contents of the table.
 | 
|---|
| 31 |         """
 | 
|---|
| 32 |         try:
 | 
|---|
| 33 |             from IPython.genutils import page as pager
 | 
|---|
| 34 |         except ImportError:
 | 
|---|
| 35 |             from pydoc import pager
 | 
|---|
| 36 |         pager(lcbase.summary(-1))
 | 
|---|
| 37 | 
 | 
|---|
| 38 |     def set_name(self, name, mode="pattern"):
 | 
|---|
| 39 |         """
 | 
|---|
| 40 |         Set a name restriction on the table. This can be a standard unix-style
 | 
|---|
| 41 |         pattern or a regular expression.
 | 
|---|
| 42 |         Parameters:
 | 
|---|
| 43 |             name:       the name patterrn/regex
 | 
|---|
| 44 |             mode:       the matching mode, i.e. "pattern" (default) or "regex"
 | 
|---|
| 45 |         """
 | 
|---|
| 46 |         validmodes = "pattern regex".split()
 | 
|---|
| 47 |         if not mode.lower() in validmodes:
 | 
|---|
| 48 |             return
 | 
|---|
| 49 |         lcbase.set_name(self, name, mode)
 | 
|---|
| 50 | 
 | 
|---|
| 51 |     def set_frequency_limits(self, fmin=1.0, fmax=120.0, unit="GHz"):
 | 
|---|
| 52 |         """
 | 
|---|
| 53 |         Set frequency limits on the table.
 | 
|---|
| 54 |         Parameters:
 | 
|---|
| 55 |             fmin:       the lower bound
 | 
|---|
| 56 |             fmax:       the upper bound
 | 
|---|
| 57 |             unit:       the frequency unit (default "GHz")
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         Note:
 | 
|---|
| 60 |             The underlying table conatins frequency values in MHz
 | 
|---|
| 61 |         """
 | 
|---|
| 62 |         base = { "GHz": 1000.0, "MHz": 1.0 }
 | 
|---|
| 63 |         if not base.has_key(unit):
 | 
|---|
| 64 |             raise ValueError("%s is not a valid unit." % unit)
 | 
|---|
| 65 |         # the table conatins values in MHz
 | 
|---|
| 66 |         lcbase.set_freq_limits(self, fmin/base[unit], fmax/base[unit])
 | 
|---|
| 67 | 
 | 
|---|
| 68 |     def set_strength_limits(self, smin, smax):
 | 
|---|
| 69 |         """
 | 
|---|
| 70 |         Set line strength limits on the table (arbitrary units)
 | 
|---|
| 71 |         Parameters:
 | 
|---|
| 72 |             smin:       the lower bound
 | 
|---|
| 73 |             smax:       the upper bound
 | 
|---|
| 74 |         """
 | 
|---|
| 75 |         lcbase.set_strength_limits(self, smin, smax)
 | 
|---|
| 76 | 
 | 
|---|
| 77 |     def save(self, name, overwrite=False):
 | 
|---|
| 78 |         """
 | 
|---|
| 79 |         Save the subset of the table to disk. This uses an internal data format
 | 
|---|
| 80 |         and can be read in again.
 | 
|---|
| 81 |         """
 | 
|---|
| 82 |         name = os.path.expandvars(name)
 | 
|---|
| 83 |         if os.path.isfile(name) or os.path.isdir(name):
 | 
|---|
| 84 |             if not overwrite:
 | 
|---|
| 85 |                 msg = "File %s exists." % name
 | 
|---|
| 86 |                 if rcParams['verbose']:
 | 
|---|
| 87 |                     print msg
 | 
|---|
| 88 |                     return
 | 
|---|
| 89 |                 else:
 | 
|---|
| 90 |                     raise IOError(msg)
 | 
|---|
| 91 |         lcbase.save(self, name)
 | 
|---|
| 92 | 
 | 
|---|
| 93 |     def reset(self):
 | 
|---|
| 94 |         """
 | 
|---|
| 95 |         Reset the table to its initial state, i.e. undo all calls to set_
 | 
|---|
| 96 |         """
 | 
|---|
| 97 |         lcbase.reset()
 | 
|---|
| 98 | 
 | 
|---|
| 99 |     def get_row(self, row=0):
 | 
|---|
| 100 |         """
 | 
|---|
| 101 |         Get the values in a specified row of the table.
 | 
|---|
| 102 |         Parameters:
 | 
|---|
| 103 |               row:        the row to retrieve
 | 
|---|
| 104 |         """
 | 
|---|
| 105 |         freq = lcbase.get_frequency(row)
 | 
|---|
| 106 |         name = lcbase.get_name(row)
 | 
|---|
| 107 |         return (freq, name)
 | 
|---|