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