source: trunk/python/linecatalog.py@ 1198

Last change on this file since 1198 was 1156, checked in by mar637, 18 years ago

changed name set_freq_limits to set_frequency_limits

  • Property svn:keywords set to Date Revision
File size: 3.7 KB
Line 
1"""
2A representation of a spectra line catalog.
3
4Author: Malte Marquarding
5
6"""
7__revision__ = "$Revision: 1156 $"
8from asap._asap import linecatalog as lcbase
9from asap import rcParams
10import os
11
12class 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 lcbase.__init__(self, fpath)
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
37 pager(lcbase.summary(self, -1))
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
67 lcbase.set_frequency_limits(self, fmin*base[unit], fmax*base[unit])
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 """
83 name = os.path.expanduser(os.path.expandvars(name))
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 """
106 freq = lcbase.get_frequency(self, row)
107 name = lcbase.get_name(self, row)
108 return { 'name':name, 'value': freq }
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
Note: See TracBrowser for help on using the repository browser.