[1131] | 1 | """ |
---|
| 2 | A representation of a spectra line catalog. |
---|
| 3 | |
---|
| 4 | Author: Malte Marquarding |
---|
| 5 | |
---|
| 6 | """ |
---|
[1133] | 7 | __revision__ = "$Revision: 1156 $" |
---|
[1131] | 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): |
---|
[1154] | 26 | fpath = os.path.abspath(os.path.expandvars(os.path.expanduser(name))) |
---|
| 27 | lcbase.__init__(self, fpath) |
---|
[1131] | 28 | |
---|
| 29 | def summary(self): |
---|
| 30 | """ |
---|
| 31 | Print the contents of the table. |
---|
| 32 | """ |
---|
| 33 | try: |
---|
| 34 | from IPython.genutils import page as pager |
---|
| 35 | except ImportError: |
---|
| 36 | from pydoc import pager |
---|
[1150] | 37 | pager(lcbase.summary(self, -1)) |
---|
[1131] | 38 | |
---|
| 39 | def set_name(self, name, mode="pattern"): |
---|
| 40 | """ |
---|
| 41 | Set a name restriction on the table. This can be a standard unix-style |
---|
| 42 | pattern or a regular expression. |
---|
| 43 | Parameters: |
---|
| 44 | name: the name patterrn/regex |
---|
| 45 | mode: the matching mode, i.e. "pattern" (default) or "regex" |
---|
| 46 | """ |
---|
| 47 | validmodes = "pattern regex".split() |
---|
| 48 | if not mode.lower() in validmodes: |
---|
| 49 | return |
---|
| 50 | lcbase.set_name(self, name, mode) |
---|
| 51 | |
---|
| 52 | def set_frequency_limits(self, fmin=1.0, fmax=120.0, unit="GHz"): |
---|
| 53 | """ |
---|
| 54 | Set frequency limits on the table. |
---|
| 55 | Parameters: |
---|
| 56 | fmin: the lower bound |
---|
| 57 | fmax: the upper bound |
---|
| 58 | unit: the frequency unit (default "GHz") |
---|
| 59 | |
---|
| 60 | Note: |
---|
| 61 | The underlying table conatins frequency values in MHz |
---|
| 62 | """ |
---|
| 63 | base = { "GHz": 1000.0, "MHz": 1.0 } |
---|
| 64 | if not base.has_key(unit): |
---|
| 65 | raise ValueError("%s is not a valid unit." % unit) |
---|
| 66 | # the table conatins values in MHz |
---|
[1156] | 67 | lcbase.set_frequency_limits(self, fmin*base[unit], fmax*base[unit]) |
---|
[1131] | 68 | |
---|
| 69 | def set_strength_limits(self, smin, smax): |
---|
| 70 | """ |
---|
| 71 | Set line strength limits on the table (arbitrary units) |
---|
| 72 | Parameters: |
---|
| 73 | smin: the lower bound |
---|
| 74 | smax: the upper bound |
---|
| 75 | """ |
---|
| 76 | lcbase.set_strength_limits(self, smin, smax) |
---|
| 77 | |
---|
| 78 | def save(self, name, overwrite=False): |
---|
| 79 | """ |
---|
| 80 | Save the subset of the table to disk. This uses an internal data format |
---|
| 81 | and can be read in again. |
---|
| 82 | """ |
---|
[1154] | 83 | name = os.path.expanduser(os.path.expandvars(name)) |
---|
[1131] | 84 | if os.path.isfile(name) or os.path.isdir(name): |
---|
| 85 | if not overwrite: |
---|
| 86 | msg = "File %s exists." % name |
---|
| 87 | if rcParams['verbose']: |
---|
| 88 | print msg |
---|
| 89 | return |
---|
| 90 | else: |
---|
| 91 | raise IOError(msg) |
---|
| 92 | lcbase.save(self, name) |
---|
| 93 | |
---|
| 94 | def reset(self): |
---|
| 95 | """ |
---|
| 96 | Reset the table to its initial state, i.e. undo all calls to set_ |
---|
| 97 | """ |
---|
| 98 | lcbase.reset() |
---|
| 99 | |
---|
| 100 | def get_row(self, row=0): |
---|
| 101 | """ |
---|
| 102 | Get the values in a specified row of the table. |
---|
| 103 | Parameters: |
---|
| 104 | row: the row to retrieve |
---|
| 105 | """ |
---|
[1153] | 106 | freq = lcbase.get_frequency(self, row) |
---|
| 107 | name = lcbase.get_name(self, row) |
---|
[1156] | 108 | return { 'name':name, 'value': freq } |
---|
[1153] | 109 | |
---|
| 110 | def __len__(self): |
---|
| 111 | return self.nrow() |
---|
| 112 | |
---|
| 113 | def __getitem__(self, k): |
---|
| 114 | if k < 0: k = self.nrow()-k |
---|
| 115 | return self.get_row(k) |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|