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