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