source: branches/Release2.1/python/linecatalog.py@ 2939

Last change on this file since 2939 was 1239, checked in by mar637, 18 years ago

Ticket #72 - fixed reset. Ticket #70 - added file not found error to contructor

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