1 | """\ |
---|
2 | A representation of a spectral line catalog. |
---|
3 | """ |
---|
4 | __revision__ = "$Revision: 1865 $" |
---|
5 | from asap._asap import linecatalog as lcbase |
---|
6 | from asap.logging import asaplog |
---|
7 | import os |
---|
8 | |
---|
9 | class linecatalog(lcbase): |
---|
10 | """\ |
---|
11 | This class is a warpper for line catalogs. These can be either ASCII tables |
---|
12 | or the tables saved from this class. |
---|
13 | |
---|
14 | ASCII tables have the following restrictions: |
---|
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 | |
---|
27 | """ |
---|
28 | |
---|
29 | def __init__(self, name): |
---|
30 | fpath = os.path.abspath(os.path.expandvars(os.path.expanduser(name))) |
---|
31 | if os.path.exists(fpath): |
---|
32 | lcbase.__init__(self, fpath) |
---|
33 | else: |
---|
34 | msg = "File '%s' not found" % fpath |
---|
35 | raise IOError(msg) |
---|
36 | |
---|
37 | def __repr__(self): |
---|
38 | return lcbase.summary(self, -1) |
---|
39 | |
---|
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 |
---|
48 | pager(lcbase.summary(self, -1)) |
---|
49 | |
---|
50 | def set_name(self, name, mode="pattern"): |
---|
51 | """\ |
---|
52 | Set a name restriction on the table. This can be a standard unix-style |
---|
53 | pattern or a regular expression. |
---|
54 | |
---|
55 | Parameters: |
---|
56 | |
---|
57 | name: the name patterrn/regex |
---|
58 | |
---|
59 | mode: the matching mode, i.e. "pattern" (default) or "regex" |
---|
60 | |
---|
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"): |
---|
68 | """\ |
---|
69 | Set frequency limits on the table. |
---|
70 | |
---|
71 | Parameters: |
---|
72 | |
---|
73 | fmin: the lower bound |
---|
74 | |
---|
75 | fmax: the upper bound |
---|
76 | |
---|
77 | unit: the frequency unit (default "GHz") |
---|
78 | |
---|
79 | .. note:: The underlying table contains frequency values in MHz |
---|
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 |
---|
85 | lcbase.set_frequency_limits(self, fmin*base[unit], fmax*base[unit]) |
---|
86 | |
---|
87 | def set_strength_limits(self, smin, smax): |
---|
88 | """\ |
---|
89 | Set line strength limits on the table (arbitrary units) |
---|
90 | |
---|
91 | Parameters: |
---|
92 | |
---|
93 | smin: the lower bound |
---|
94 | |
---|
95 | smax: the upper bound |
---|
96 | |
---|
97 | """ |
---|
98 | lcbase.set_strength_limits(self, smin, smax) |
---|
99 | |
---|
100 | def save(self, name, overwrite=False): |
---|
101 | """\ |
---|
102 | Save the subset of the table to disk. This uses an internal data format |
---|
103 | and can be read in again. |
---|
104 | """ |
---|
105 | name = os.path.expanduser(os.path.expandvars(name)) |
---|
106 | if os.path.isfile(name) or os.path.isdir(name): |
---|
107 | if not overwrite: |
---|
108 | msg = "File %s exists." % name |
---|
109 | raise IOError(msg) |
---|
110 | lcbase.save(self, name) |
---|
111 | |
---|
112 | def reset(self): |
---|
113 | """\ |
---|
114 | Reset the table to its initial state, i.e. undo all calls to ``set_``. |
---|
115 | """ |
---|
116 | lcbase.reset(self) |
---|
117 | |
---|
118 | def get_row(self, row=0): |
---|
119 | """\ |
---|
120 | Get the values in a specified row of the table. |
---|
121 | |
---|
122 | Parameters: |
---|
123 | |
---|
124 | row: the row to retrieve |
---|
125 | |
---|
126 | """ |
---|
127 | if row < 0 or row > len(self)-1: |
---|
128 | raise IndexError("Row index out of bounds.") |
---|
129 | freq = lcbase.get_frequency(self, row) |
---|
130 | name = lcbase.get_name(self, row) |
---|
131 | return { 'name':name, 'value': freq } |
---|
132 | |
---|
133 | def __len__(self): |
---|
134 | return self.nrow() |
---|
135 | |
---|
136 | def __getitem__(self, k): |
---|
137 | if k < 0: |
---|
138 | k = self.nrow()-k |
---|
139 | return self.get_row(k) |
---|