source: trunk/python/linecatalog.py @ 1534

Last change on this file since 1534 was 1534, checked in by Malte Marquarding, 15 years ago

Fix for Ticket #90; treating integer as double columns now

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