"
253 | for colname in colnames:
254 | out += ""
255 | out += _format_cell(row[colname], tab.getcolkeywords(colname))
256 | out += " | \n"
257 | out += "
\n"
258 | return out
259 |
--------------------------------------------------------------------------------
/casacore/tables/tableindex.py:
--------------------------------------------------------------------------------
1 | # tableindex.py: Python tableindex functions
2 | # Copyright (C) 2006
3 | # Associated Universities, Inc. Washington DC, USA.
4 | #
5 | # This library is free software; you can redistribute it and/or modify it
6 | # under the terms of the GNU Lesser General Public License as published by
7 | # the Free Software Foundation; either version 3 of the License, or (at your
8 | # option) any later version.
9 | #
10 | # This library is distributed in the hope that it will be useful, but WITHOUT
11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13 | # License for more details.
14 | #
15 | # You should have received a copy of the GNU Lesser General Public License
16 | # along with this library; if not, write to the Free Software Foundation,
17 | # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 | #
19 | # Correspondence concerning AIPS++ should be addressed as follows:
20 | # Internet email: aips2-request@nrao.edu.
21 | # Postal address: AIPS++ Project Office
22 | # National Radio Astronomy Observatory
23 | # 520 Edgemont Road
24 | # Charlottesville, VA 22903-2475 USA
25 | #
26 | # $Id: tableindex.py,v 1.6 2006/11/08 00:12:55 gvandiep Exp $
27 |
28 | # Make interface to class TableIndexProxy available.
29 | from ._tables import TableIndex
30 |
31 |
32 | class tableindex(TableIndex):
33 | """The Python interface to Casacore table index.
34 |
35 | A tableindex makes it possible to find rows in a :class:`table` based on
36 | the contents of one or more columns. When constructing the `tableindex` it
37 | has to be specified for which column or columns an index has to be built.
38 | Those columns will be loaded in memory and thereafter row numbers can be
39 | found in a fast way using a binary search.
40 |
41 | Using a table index is only useful if many searches will be done in the
42 | table. For a single or few searches it is better to query the table using
43 | method :func:`table.query`.
44 |
45 | Normally an index will be build on one or more scalar columns (e.g.
46 | on ANTENNA1 and ANTENNA2 in a measurementset table). However, it is also
47 | possible to buo.d an index for a column containing arrays (e.g. for a
48 | column where each cell can contain multiple names. In that case only a
49 | single column can be indexed.
50 |
51 | The index can be unique, but does not need to be.
52 | A unique index can be asked for the row number containing a given key.
53 | A non-unique index can only be asked for the row numbers containing a key.
54 | The returned sequence can thereafter be used in :func:`table.selectrows`
55 | to form that subset of the table.
56 |
57 | `tableindex` supports Python's index operator [] as explained in the
58 | methods :func:`rownr` and :func:`rownrs`.
59 |
60 | """
61 |
62 | def __init__(self, table, columnnames, sort=True):
63 | TableIndex.__init__(self, table, columnnames, not sort)
64 |
65 | """Create the index on one or more columns.
66 |
67 | By default the columns get sorted when forming in the index. By giving
68 | `sort=False` this can be omitted in case the table is already in the
69 | correct order.
70 |
71 | Method :func:`table.index` is a somewhat easier way to create a
72 | `tableindex` object.
73 |
74 | """
75 |
76 | # Turn a key into a dict if needed.
77 | def _makekey(self, key):
78 | d = key
79 | if not isinstance(d, dict):
80 | cols = self.colnames()
81 | if len(cols) != 1:
82 | raise RuntimeError("key has to be given as a dict for a multi-column index")
83 | d = {cols[0]: key}
84 | return d
85 |
86 | def rownr(self, key):
87 | """Get the unique row number containing the key.
88 |
89 | If the index is made from a single column, the keycan be given as a
90 | single value.
91 | Otherwise the key has to be given as a dict where the name of each
92 | field in the dict should correspond with the column name in the index.
93 |
94 | For example::
95 |
96 | t = table('3c343.MS/ANTENNA')
97 | tinx = t.index ('NAME') # build index for antenna name
98 | rownr = tinx.rownr('RTE') # find an antenna by name
99 | rownr = tinx['RTE'] # same as above
100 | t.getcell ('POSITION', rownr) # get position of that antenna
101 |
102 | As shown in the example above the python index operator can also
103 | be used to find a row number if the index if made of a single column.
104 |
105 | An exception will be raised if the index is not unique. In that case
106 | method :func:`rownrs` should be used instead.
107 |
108 | """
109 | return self._rownr(self._makekey(key))
110 |
111 | def rownrs(self, key, upperkey={}, lowerincl=True, upperincl=True):
112 | """Get a sequence of row numbers containing the key(s).
113 |
114 | A single key can be given, but by giving argument `upperkey` as well
115 | a key range can be given (where upper key must be > lower).
116 | One can specify if the lower and upper key should be part of the range
117 | (`incl=True`) or not. By default both keys are part of the range.
118 |
119 | The key and optional upper key have to be given in the same way as
120 | for method :func:`rownr`.
121 |
122 | Similar to method :func:`rownr`. python's index operator [] can be used
123 | if the index consists of a single column. However, in this case only
124 | key ranges can be used (because the index operator with a single key
125 | returns a single row number, thus can only be used for unique indices).
126 | The lower key is inclusive, but the upper key is exclusive conform
127 | the standard python index semantics.
128 |
129 | For example::
130 |
131 | t = table('3c343.MS')
132 | tinx = t.index ('ANTENNA1') # build index for antenna name
133 | rownr = tinx.rownr(0) # find antenna1 = 0
134 | rownr = tinx[0:1] # same as above
135 |
136 | """
137 | lkey = self._makekey(key)
138 | ukey = self._makekey(upperkey)
139 | if len(ukey) == 0:
140 | return self._rownrs(lkey)
141 | return self._rownrsrange(lkey, ukey, lowerincl, upperincl)
142 |
143 | def isunique(self):
144 | """Tell if all keys in the index are unique."""
145 | return self._isunique()
146 |
147 | def colnames(self):
148 | """Return the column names the index is made of."""
149 | return self._colnames()
150 |
151 | def setchanged(self, columnnames=[]):
152 | """Tell the index that data has changed.
153 |
154 | The index is smart enough to detect that the number of rows in the
155 | indexed table has changed. However, it cannot detect if a value in
156 | a column contained in this inex has changed. So it has to be told
157 | explicitly.
158 |
159 | `columnnames`
160 | The names of the columns in which data have changed.
161 | Giving no names means that all columns in the index have changed.
162 | """
163 | return self._setchanged(columnnames)
164 |
165 | def __getitem__(self, key):
166 | if not isinstance(key, slice):
167 | rnr = self.rownr(key)
168 | if rnr < 0:
169 | raise KeyError("key not found in tableindex")
170 | return rnr
171 | if key.step is not None:
172 | raise RuntimeError("tableindex slicing cannot have a step")
173 | lowerkey = 0
174 | if key.start is not None:
175 | lowerkey = key.start
176 | upperkey = 2147483647; # highest int
177 | if key.stop is not None:
178 | upperkey = key.stop
179 | if (lowerkey >= upperkey):
180 | raise RuntimeError("tableindex slice stop must be > start")
181 | rnrs = self.rownrs(lowerkey, upperkey, True, False)
182 | if len(rnrs) == 0:
183 | raise KeyError("keys not found in tableindex")
184 | return rnrs
185 |
--------------------------------------------------------------------------------
/casacore/tables/tableiter.py:
--------------------------------------------------------------------------------
1 | # tableiter.py: Python tableiter functions
2 | # Copyright (C) 2006
3 | # Associated Universities, Inc. Washington DC, USA.
4 | #
5 | # This library is free software; you can redistribute it and/or modify it
6 | # under the terms of the GNU Lesser General Public License as published by
7 | # the Free Software Foundation; either version 3 of the License, or (at your
8 | # option) any later version.
9 | #
10 | # This library is distributed in the hope that it will be useful, but WITHOUT
11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13 | # License for more details.
14 | #
15 | # You should have received a copy of the GNU Lesser General Public License
16 | # along with this library; if not, write to the Free Software Foundation,
17 | # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 | #
19 | # Correspondence concerning AIPS++ should be addressed as follows:
20 | # Internet email: aips2-request@nrao.edu.
21 | # Postal address: AIPS++ Project Office
22 | # National Radio Astronomy Observatory
23 | # 520 Edgemont Road
24 | # Charlottesville, VA 22903-2475 USA
25 | #
26 | # $Id: tableiter.py,v 1.6 2006/12/11 02:46:08 gvandiep Exp $
27 |
28 | # Make interface to class TableIterProxy available.
29 | from ._tables import TableIter
30 |
31 | from .table import table
32 |
33 |
34 | class tableiter(TableIter):
35 | """The Python interface to Casacore table iterators
36 |
37 | A `tableiter` allows iteration through a table based on the contents
38 | of one or more columns. Each step in the iteration process forms
39 | a subset of the table for which the specified columns have the same value.
40 |
41 | It can easily be constructed using the :func:`table.iter` method as done
42 | in the example below::
43 |
44 | t = table('3c343.MS')
45 | for ts in t.iter('ANTENNA1'):
46 | print ts.nrows()
47 |
48 | In this example `ts` will be a so-called reference table which can be
49 | operated on like any other table object.
50 |
51 | Multiple column names should be given in a sequence (tuple or list).
52 |
53 | """
54 |
55 | def __init__(self, table, columnnames, order='', sort=True):
56 | st = sort
57 | if isinstance(sort, bool):
58 | st = 'heapsort'
59 | if not sort:
60 | st = 'nosort'
61 | TableIter.__init__(self, table, columnnames, order, st)
62 |
63 | def __iter__(self):
64 | # __iter__ is needed
65 | return self
66 |
67 | def next(self):
68 | # next returns a Table object, so turn that into table.
69 | return table(self._next(), _oper=3)
70 |
71 | def reset(self):
72 | """Reset the iterator to the beginning."""
73 | self._reset()
74 |
75 | __next__ = next
76 |
--------------------------------------------------------------------------------
/casacore/tables/tablerow.py:
--------------------------------------------------------------------------------
1 | # tablerow.py: Python tablerow functions
2 | # Copyright (C) 2006
3 | # Associated Universities, Inc. Washington DC, USA.
4 | #
5 | # This library is free software; you can redistribute it and/or modify it
6 | # under the terms of the GNU Lesser General Public License as published by
7 | # the Free Software Foundation; either version 3 of the License, or (at your
8 | # option) any later version.
9 | #
10 | # This library is distributed in the hope that it will be useful, but WITHOUT
11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13 | # License for more details.
14 | #
15 | # You should have received a copy of the GNU Lesser General Public License
16 | # along with this library; if not, write to the Free Software Foundation,
17 | # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 | #
19 | # Correspondence concerning AIPS++ should be addressed as follows:
20 | # Internet email: aips2-request@nrao.edu.
21 | # Postal address: AIPS++ Project Office
22 | # National Radio Astronomy Observatory
23 | # 520 Edgemont Road
24 | # Charlottesville, VA 22903-2475 USA
25 | #
26 | # $Id: tablerow.py,v 1.6 2007/08/28 07:22:18 gvandiep Exp $
27 |
28 | # Make interface to class TableRowProxy available.
29 | from ._tables import TableRow
30 |
31 | from .tablehelper import _check_key_slice
32 |
33 |
34 | # A normal tablerow object keeps a reference to a table object to be able
35 | # to know the actual number of rows.
36 | # However, a mutual dependency is created when doing that for the tablerow
37 | # object inside the table object.
38 | # Therefore an intermediate _tablerow exists to be used in class table.
39 |
40 | class _tablerow(TableRow):
41 | def __init__(self, table, columnnames, exclude=False):
42 | TableRow.__init__(self, table, columnnames, exclude)
43 |
44 | def iswritable(self):
45 | """Tell if all columns in the row object are writable."""
46 | return self._iswritable()
47 |
48 | def get(self, rownr):
49 | """Get the contents of the given row."""
50 | return self._get(rownr)
51 |
52 | def put(self, rownr, value, matchingfields=True):
53 | """Put the values into the given row.
54 |
55 | The value should be a dict (as returned by method :func:`get`.
56 | The names of the fields in the dict should match the names of the
57 | columns used in the `tablerow` object.
58 |
59 | `matchingfields=True` means that the value may contain more fields
60 | and only fields matching a column name will be used.
61 |
62 | """
63 | self._put(rownr, value, matchingfields)
64 |
65 | def _getitem(self, key, nrows):
66 | sei = _check_key_slice(key, nrows, 'tablerow')
67 | rownr = sei[0]
68 | if len(sei) == 1:
69 | return self.get(rownr)
70 | result = []
71 | inx = 0
72 | while inx < sei[1]:
73 | result.append(self.get(rownr))
74 | rownr += sei[2]
75 | inx += 1
76 | return result
77 |
78 | def _setitem(self, key, value, nrows):
79 | sei = _check_key_slice(key, nrows, 'tablerow')
80 | rownr = sei[0]
81 | if len(sei) == 1:
82 | return self.put(rownr, value)
83 | if isinstance(value, dict):
84 | # The same value is put in all rows.
85 | inx = 0
86 | while inx < sei[1]:
87 | self.put(rownr, value, True)
88 | rownr += sei[2]
89 | inx += 1
90 | else:
91 | # Each row has its own value.
92 | if len(value) != sei[1]:
93 | raise RuntimeError("tablerow slice length differs from value length")
94 | for val in value:
95 | self.put(rownr, val, True)
96 | rownr += sei[2]
97 |
98 |
99 | class tablerow(_tablerow):
100 | """The Python interface to Casacore table rows.
101 |
102 | A table row is a record (dict) containing the values of a single row for
103 | one or more columns in a table. In constructing the `tablerow` object, one
104 | can specify which columns are to be included or excluded.
105 | By default all columns will be used, but if the table is writable,
106 | only writable columns will be used.
107 |
108 | A `tablerow` object can easily be constructed using :func:`table.row`.
109 |
110 | One or more rows can be read or written using the standard python indexing
111 | syntax where (negative) strides are possible.
112 | For example:
113 |
114 | t = table ('3c343.MS')
115 | tr = t.row (['ANTENNA1', 'ANTENNA2', 'ARRAY_ID'])
116 | tr[0] # get row 0
117 | tr[:5] # get row 0,1,2,3,4
118 | tr[-5,-1,] # get last 4 rows
119 | tr[-1,-5,-1] # get last 4 rows in reversed order
120 | tr[1] = tr[0] # put values of row 0 into row 1
121 |
122 | Note that the last line will fail because the table is opened readonly.
123 | The argument `readonly=False` is needed in the table constructor to make
124 | it work.
125 |
126 | The `tablerow` class supports the context manager idiom (__enter__ and __exit__).
127 | When used in a `with` statement, the table changes will be flushed
128 | automatically, which is handy when writing to table rows.
129 | For example::
130 |
131 | with t.row() as tr:
132 | tr.put (1, tr.get(0)) # copy row 0 to row 1
133 |
134 | """
135 |
136 | def __init__(self, table, columnnames=[], exclude=False):
137 | _tablerow.__init__(self, table, columnnames, exclude)
138 | self._table = table
139 |
140 | def __enter__(self):
141 | """Function to enter a with block."""
142 | return self
143 |
144 | def __exit__(self, type, value, traceback):
145 | """Function to exit a with block which flushes the table object."""
146 | self._table.flush()
147 |
148 | def __len__(self):
149 | return self._table.nrows()
150 |
151 | def __getitem__(self, key):
152 | return self._getitem(key, self._table.nrows())
153 |
154 | def __setitem__(self, key, value):
155 | return self._setitem(key, value, self._table.nrows())
156 |
--------------------------------------------------------------------------------
/casacore/tables/wxtablebrowser.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | from wxPython.grid import *
4 | from wxPython.wx import *
5 |
6 |
7 | class wxCasaTable(wxPyGridTableBase):
8 | """
9 | This is all it takes to make a custom data table to plug into a
10 | wxGrid. There are many more methods that can be overridden, but
11 | the ones shown below are the required ones. This table simply
12 | provides strings containing the row and column values.
13 | """
14 |
15 | def __init__(self, log, ctable):
16 | wxPyGridTableBase.__init__(self)
17 | self.log = log
18 | self.casatab = ctable
19 | self.odd = wxGridCellAttr()
20 | self.odd.SetBackgroundColour("gray90")
21 | self.even = wxGridCellAttr()
22 | self.even.SetBackgroundColour("white")
23 |
24 | def GetAttr(self, row, col, kind):
25 | attr = [self.even, self.odd][row % 2]
26 | attr.IncRef()
27 | return attr
28 |
29 | def GetColLabelValue(self, col):
30 | colnames = self.casatab.colnames()
31 | return colnames[col]
32 |
33 | def GetNumberRows(self):
34 | return self.casatab.nrows()
35 |
36 | def GetNumberCols(self):
37 | return self.casatab.ncols()
38 |
39 | def IsEmptyCell(self, row, col):
40 | return False
41 |
42 | def GetValue(self, row, col):
43 | coln = self.casatab.colnames()
44 | cell = 'array'
45 | ## if self.casatab.isscalarcol(coln[col]):
46 | ## cellval = self.casatab.getcell(coln[col],row)
47 | ## if isinstance(cellval,float):
48 | ## if coln[col] == "TIME":
49 | ## cell = str(cellval)
50 | ## else:
51 | ## cell = "%3.5f" % cellval
52 | ## else:
53 | ## cell = str(cellval)
54 | ## else:
55 | ## cell += self.casatab.getcolshapestring(coln[col],row,1)[0]
56 | ## return cell
57 | return str(self.casatab.getcell(coln[col], row))
58 |
59 | def SetValue(self, row, col, value):
60 | self.log.write('SetValue(%d, %d, "%s") ignored.\n' % (row, col, value))
61 |
62 |
63 | # ---------------------------------------------------------------------------
64 |
65 | class wxCasaTableGrid(wxGrid):
66 | def __init__(self, parent, log, ctable):
67 | wxGrid.__init__(self, parent, -1)
68 | table = wxCasaTable(log, ctable)
69 | # The second parameter means that the grid is to take ownership of the
70 | # table and will destroy it when done. Otherwise you would need to keep
71 | # a reference to it and call it's Destroy method later.
72 | self.SetTable(table, True)
73 | EVT_GRID_CELL_RIGHT_CLICK(self, self.OnRightDown) # added
74 |
75 | def OnRightDown(self, event): # added
76 | print(self.GetSelectedRows()) # added
77 |
78 |
79 | # ---------------------------------------------------------------------------
80 |
81 | class CasaTestFrame(wxFrame):
82 | def __init__(self, parent, log, ctable):
83 | wxFrame.__init__(self, parent, -1, "Casa Table Browser",
84 | size=(640, 480))
85 | grid = wxCasaTableGrid(self, log, ctable)
86 | grid.EnableEditing(False)
87 | # grid.AutoSizeColumns()
88 |
89 |
90 | # ---------------------------------------------------------------------------
91 |
92 | if __name__ == '__main__':
93 | import sys
94 |
95 | app = wxPySimpleApp()
96 | from casacore.tables import table as casatable
97 |
98 | casatab = casatable('/nfs/aips++/data/atnf/scripts/C972.ms')
99 | frame = CasaTestFrame(None, sys.stdout, casatab)
100 | frame.Show(True)
101 | app.MainLoop()
102 |
103 |
104 | # ---------------------------------------------------------------------------
105 |
--------------------------------------------------------------------------------
/casacore/util/__init__.py:
--------------------------------------------------------------------------------
1 | """
2 | Utilities for casacore modules.
3 | """
4 | from .substitute import substitute, getlocals, getvariable
5 |
--------------------------------------------------------------------------------
/cmake/FindCFITSIO.cmake:
--------------------------------------------------------------------------------
1 | # - Try to find CFITSIO.
2 | # Variables used by this module:
3 | # CFITSIO_ROOT_DIR - CFITSIO root directory
4 | # Variables defined by this module:
5 | # CFITSIO_FOUND - system has CFITSIO
6 | # CFITSIO_INCLUDE_DIR - the CFITSIO include directory (cached)
7 | # CFITSIO_INCLUDE_DIRS - the CFITSIO include directories
8 | # (identical to CFITSIO_INCLUDE_DIR)
9 | # CFITSIO_LIBRARY - the CFITSIO library (cached)
10 | # CFITSIO_LIBRARIES - the CFITSIO libraries
11 | # (identical to CFITSIO_LIBRARY)
12 | # CFITSIO_VERSION_STRING the found version of CFITSIO, padded to 3 digits
13 |
14 | # Copyright (C) 2009
15 | # ASTRON (Netherlands Institute for Radio Astronomy)
16 | # P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
17 | #
18 | # This file is part of the LOFAR software suite.
19 | # The LOFAR software suite is free software: you can redistribute it and/or
20 | # modify it under the terms of the GNU General Public License as published
21 | # by the Free Software Foundation, either version 3 of the License, or
22 | # (at your option) any later version.
23 | #
24 | # The LOFAR software suite is distributed in the hope that it will be useful,
25 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 | # GNU General Public License for more details.
28 | #
29 | # You should have received a copy of the GNU General Public License along
30 | # with the LOFAR software suite. If not, see