[1865] | 1 | """\ |
---|
[1534] | 2 | A representation of a spectral line catalog. |
---|
[1131] | 3 | """ |
---|
[1133] | 4 | __revision__ = "$Revision: 1865 $" |
---|
[1131] | 5 | from asap._asap import linecatalog as lcbase |
---|
[1826] | 6 | from asap.logging import asaplog |
---|
[1131] | 7 | import os |
---|
| 8 | |
---|
| 9 | class linecatalog(lcbase): |
---|
[1865] | 10 | """\ |
---|
[1131] | 11 | This class is a warpper for line catalogs. These can be either ASCII tables |
---|
| 12 | or the tables saved from this class. |
---|
[1865] | 13 | |
---|
[1131] | 14 | ASCII tables have the following restrictions: |
---|
[1865] | 15 | |
---|
| 16 | * Comments can be present through lines starting with '#'. |
---|
| 17 | |
---|
| 18 | * The first column contains the name of the Molecule. This can't contain |
---|
| 19 | spaces, if it does it has to be wrapped in double-quotes. |
---|
| 20 | |
---|
| 21 | * The second column contains the frequency of the transition. |
---|
| 22 | |
---|
| 23 | * The third column contains the error in frequency. |
---|
| 24 | |
---|
| 25 | * The fourth column contains a value describing the intensity. |
---|
| 26 | |
---|
[1131] | 27 | """ |
---|
| 28 | |
---|
| 29 | def __init__(self, name): |
---|
[1154] | 30 | fpath = os.path.abspath(os.path.expandvars(os.path.expanduser(name))) |
---|
[1259] | 31 | if os.path.exists(fpath): |
---|
| 32 | lcbase.__init__(self, fpath) |
---|
| 33 | else: |
---|
| 34 | msg = "File '%s' not found" % fpath |
---|
[1859] | 35 | raise IOError(msg) |
---|
[1131] | 36 | |
---|
[1534] | 37 | def __repr__(self): |
---|
| 38 | return lcbase.summary(self, -1) |
---|
| 39 | |
---|
[1131] | 40 | def summary(self): |
---|
| 41 | """ |
---|
| 42 | Print the contents of the table. |
---|
| 43 | """ |
---|
| 44 | try: |
---|
| 45 | from IPython.genutils import page as pager |
---|
| 46 | except ImportError: |
---|
| 47 | from pydoc import pager |
---|
[1150] | 48 | pager(lcbase.summary(self, -1)) |
---|
[1131] | 49 | |
---|
| 50 | def set_name(self, name, mode="pattern"): |
---|
[1865] | 51 | """\ |
---|
[1131] | 52 | Set a name restriction on the table. This can be a standard unix-style |
---|
| 53 | pattern or a regular expression. |
---|
[1865] | 54 | |
---|
[1131] | 55 | Parameters: |
---|
[1865] | 56 | |
---|
[1131] | 57 | name: the name patterrn/regex |
---|
[1865] | 58 | |
---|
[1131] | 59 | mode: the matching mode, i.e. "pattern" (default) or "regex" |
---|
[1865] | 60 | |
---|
[1131] | 61 | """ |
---|
| 62 | validmodes = "pattern regex".split() |
---|
| 63 | if not mode.lower() in validmodes: |
---|
| 64 | return |
---|
| 65 | lcbase.set_name(self, name, mode) |
---|
| 66 | |
---|
| 67 | def set_frequency_limits(self, fmin=1.0, fmax=120.0, unit="GHz"): |
---|
[1865] | 68 | """\ |
---|
[1131] | 69 | Set frequency limits on the table. |
---|
[1865] | 70 | |
---|
[1131] | 71 | Parameters: |
---|
[1865] | 72 | |
---|
[1131] | 73 | fmin: the lower bound |
---|
[1865] | 74 | |
---|
[1131] | 75 | fmax: the upper bound |
---|
[1865] | 76 | |
---|
[1131] | 77 | unit: the frequency unit (default "GHz") |
---|
| 78 | |
---|
[1865] | 79 | .. note:: The underlying table contains frequency values in MHz |
---|
[1131] | 80 | """ |
---|
| 81 | base = { "GHz": 1000.0, "MHz": 1.0 } |
---|
| 82 | if not base.has_key(unit): |
---|
| 83 | raise ValueError("%s is not a valid unit." % unit) |
---|
| 84 | # the table conatins values in MHz |
---|
[1156] | 85 | lcbase.set_frequency_limits(self, fmin*base[unit], fmax*base[unit]) |
---|
[1131] | 86 | |
---|
| 87 | def set_strength_limits(self, smin, smax): |
---|
[1865] | 88 | """\ |
---|
[1131] | 89 | Set line strength limits on the table (arbitrary units) |
---|
[1865] | 90 | |
---|
[1131] | 91 | Parameters: |
---|
[1865] | 92 | |
---|
[1131] | 93 | smin: the lower bound |
---|
[1865] | 94 | |
---|
[1131] | 95 | smax: the upper bound |
---|
[1865] | 96 | |
---|
[1131] | 97 | """ |
---|
| 98 | lcbase.set_strength_limits(self, smin, smax) |
---|
| 99 | |
---|
| 100 | def save(self, name, overwrite=False): |
---|
[1865] | 101 | """\ |
---|
[1131] | 102 | Save the subset of the table to disk. This uses an internal data format |
---|
| 103 | and can be read in again. |
---|
| 104 | """ |
---|
[1154] | 105 | name = os.path.expanduser(os.path.expandvars(name)) |
---|
[1131] | 106 | if os.path.isfile(name) or os.path.isdir(name): |
---|
| 107 | if not overwrite: |
---|
| 108 | msg = "File %s exists." % name |
---|
[1859] | 109 | raise IOError(msg) |
---|
[1131] | 110 | lcbase.save(self, name) |
---|
| 111 | |
---|
| 112 | def reset(self): |
---|
[1865] | 113 | """\ |
---|
| 114 | Reset the table to its initial state, i.e. undo all calls to ``set_``. |
---|
[1131] | 115 | """ |
---|
[1259] | 116 | lcbase.reset(self) |
---|
[1131] | 117 | |
---|
| 118 | def get_row(self, row=0): |
---|
[1865] | 119 | """\ |
---|
[1131] | 120 | Get the values in a specified row of the table. |
---|
[1865] | 121 | |
---|
[1131] | 122 | Parameters: |
---|
[1865] | 123 | |
---|
[1131] | 124 | row: the row to retrieve |
---|
[1865] | 125 | |
---|
[1131] | 126 | """ |
---|
[1865] | 127 | if row < 0 or row > len(self)-1: |
---|
| 128 | raise IndexError("Row index out of bounds.") |
---|
[1153] | 129 | freq = lcbase.get_frequency(self, row) |
---|
| 130 | name = lcbase.get_name(self, row) |
---|
[1156] | 131 | return { 'name':name, 'value': freq } |
---|
[1153] | 132 | |
---|
| 133 | def __len__(self): |
---|
| 134 | return self.nrow() |
---|
| 135 | |
---|
| 136 | def __getitem__(self, k): |
---|
[1865] | 137 | if k < 0: |
---|
| 138 | k = self.nrow()-k |
---|
[1153] | 139 | return self.get_row(k) |
---|