Python bindings for the CASACORE radio astronomy library providing comprehensive interfaces for table operations, image processing, coordinate systems, and astronomical measurements.
—
Complete interface to casacore's table system including table creation, data access, querying with TaQL (Table Query Language), and MeasurementSet operations. The table system provides SQL-like functionality for radio astronomy data with support for n-dimensional arrays in table cells.
from casacore.tables import table, taql, tablecommand
from casacore.tables import tablecolumn, tablerow, tableiter, tableindex
from casacore.tables import default_ms, default_ms_subtable
# Table utilities
from casacore.tables import tablefromascii
from casacore.tables import makescacoldesc, makearrcoldesc, makecoldesc, maketabdesc
# MeasurementSet utilities
from casacore.tables import (addImagingColumns, removeImagingColumns,
addDerivedMSCal, removeDerivedMSCal,
msconcat, msregularize,
required_ms_desc, complete_ms_desc)Open existing tables or create new ones with support for various access modes and table types including MeasurementSets.
def table(tablename, readonly=True, ack=True, **kwargs):
"""
Open or create a casacore table.
Parameters:
- tablename: str, path to table
- readonly: bool, open in read-only mode (default True)
- ack: bool, acknowledge table opening (default True)
- lockoptions: str, locking options ('auto', 'user', 'usernoread')
- endian: str, endianness ('big', 'little', 'local', 'aipsrc')
- memorytable: bool, create in-memory table
- concurrency: str, concurrency options
Returns:
table object
"""
def default_ms(name, tabdesc=None, dminfo=None):
"""
Create a default MeasurementSet structure.
Parameters:
- name: str, MeasurementSet name
- tabdesc: dict, optional table description overrides
- dminfo: dict, optional data manager info
Returns:
table object representing MeasurementSet
"""
def default_ms_subtable(subtable, name=None, tabdesc=None, dminfo=None):
"""
Create a MeasurementSet subtable.
Parameters:
- subtable: str, subtable type ('ANTENNA', 'SPECTRAL_WINDOW', etc.)
- name: str, optional path (defaults to subtable name)
- tabdesc: dict, optional table description overrides
- dminfo: dict, optional data manager info
Returns:
table object representing subtable
"""Execute SQL-like queries on tables with TaQL, supporting complex selection criteria, joins, and aggregations.
def taql(command, style='Python', tables=[], globals={}, locals={}):
"""
Execute a TaQL (Table Query Language) command.
Parameters:
- command: str, TaQL query string
- style: str, result style ('Python' or 'Glish')
- tables: list, additional tables to make available
- globals: dict, global variables for query
- locals: dict, local variables for query
Returns:
table object with query results
"""
# Alias for taql
tablecommand = taqlRetrieve and modify table data at cell, column, and row levels with support for slicing and partial reads.
class table:
def getcell(self, columnname, rownr):
"""
Get value from a single table cell.
Parameters:
- columnname: str, column name
- rownr: int, row number (0-based)
Returns:
Cell value (type depends on column)
"""
def putcell(self, columnname, rownr, value):
"""
Set value in a single table cell.
Parameters:
- columnname: str, column name
- rownr: int, row number (0-based)
- value: cell value to set
"""
def getcol(self, columnname, startrow=0, nrow=-1, rowincr=1):
"""
Get entire column or column slice.
Parameters:
- columnname: str, column name
- startrow: int, starting row (default 0)
- nrow: int, number of rows (-1 for all, default -1)
- rowincr: int, row increment (default 1)
Returns:
numpy array with column data
"""
def putcol(self, columnname, value, startrow=0, nrow=-1, rowincr=1):
"""
Set entire column or column slice.
Parameters:
- columnname: str, column name
- value: numpy array with values to set
- startrow: int, starting row (default 0)
- nrow: int, number of rows (-1 for all, default -1)
- rowincr: int, row increment (default 1)
"""
def getcolslice(self, columnname, blc, trc, inc=[], startrow=0, nrow=-1):
"""
Get slice of array column.
Parameters:
- columnname: str, array column name
- blc: list, bottom-left corner indices
- trc: list, top-right corner indices
- inc: list, increment per axis (default [])
- startrow: int, starting row (default 0)
- nrow: int, number of rows (-1 for all, default -1)
Returns:
numpy array with sliced data
"""Access table properties, column information, and metadata including table description and data manager information.
class table:
def nrows(self):
"""Get number of rows in table."""
def ncols(self):
"""Get number of columns in table."""
def colnames(self):
"""Get list of column names."""
def coldesc(self, columnname=""):
"""
Get column description(s).
Parameters:
- columnname: str, specific column (empty for all columns)
Returns:
dict with column description information
"""
def info(self):
"""Get table information and properties."""
def summary(self, header=True, columns=True, dataman=False, subtables=False):
"""
Get table summary.
Parameters:
- header: bool, include header info
- columns: bool, include column info
- dataman: bool, include data manager info
- subtables: bool, include subtable info
Returns:
dict with summary information
"""Perform table operations including copying, selection, sorting, and joining.
class table:
def copy(self, newtablename, deep=True, valuecopy=False, dminfo={}):
"""
Copy table to new location.
Parameters:
- newtablename: str, new table name
- deep: bool, deep copy (default True)
- valuecopy: bool, copy values not references (default False)
- dminfo: dict, data manager info for new table
Returns:
table object for new table
"""
def select(self, columnnames="", offset=0, limit=0):
"""
Select subset of columns and rows.
Parameters:
- columnnames: str or list, column names to select (empty for all)
- offset: int, starting row offset (default 0)
- limit: int, maximum rows to return (0 for all, default 0)
Returns:
table object with selection
"""
def sort(self, sortlist, unique=False):
"""
Sort table by specified columns.
Parameters:
- sortlist: str or list, column names for sorting
- unique: bool, remove duplicate rows (default False)
Returns:
table object with sorted data
"""
def query(self, query, sortlist="", columns="", offset=0, limit=0):
"""
Query table with selection criteria.
Parameters:
- query: str, selection expression
- sortlist: str, sort specification (default "")
- columns: str, columns to select (default "")
- offset: int, starting row offset (default 0)
- limit: int, maximum rows (default 0)
Returns:
table object with query results
"""Low-level table management including locking, flushing, and resource cleanup.
class table:
def lock(self, mode="write", nattempts=0):
"""
Lock table for exclusive access.
Parameters:
- mode: str, lock mode ('read' or 'write')
- nattempts: int, number of lock attempts (0 for infinite)
Returns:
bool, True if lock acquired
"""
def unlock(self):
"""Release table lock."""
def haslock(self, mode="write"):
"""
Check if table is locked.
Parameters:
- mode: str, lock mode to check
Returns:
bool, True if locked in specified mode
"""
def flush(self, recursive=True):
"""
Flush table changes to disk.
Parameters:
- recursive: bool, flush subtables too (default True)
"""
def close(self):
"""Close table and release resources."""
def done(self):
"""Close table (alias for close)."""Specialized utilities for working with MeasurementSets including concatenation, column management, and structure descriptions.
def msconcat(names, newname, concatTime=False):
"""
Concatenate multiple MeasurementSets into a single MeasurementSet.
Parameters:
- names: list of str, paths to MeasurementSets to concatenate
- newname: str, path for output concatenated MeasurementSet
- concatTime: bool, concatenate in time rather than frequency (default False)
Returns:
None (creates new MeasurementSet file)
"""
def msregularize(msname, newname):
"""
Regularize a MeasurementSet by removing duplicate rows and fixing inconsistencies.
Parameters:
- msname: str, input MeasurementSet path
- newname: str, output regularized MeasurementSet path
Returns:
None (creates new MeasurementSet file)
"""
def addImagingColumns(msname, ack=True):
"""
Add standard imaging columns to MeasurementSet.
Adds MODEL_DATA and CORRECTED_DATA columns needed for imaging and calibration.
Parameters:
- msname: str, MeasurementSet path
- ack: bool, acknowledge operation (default True)
Returns:
None (modifies MeasurementSet in place)
"""
def removeImagingColumns(msname):
"""
Remove imaging columns from MeasurementSet.
Removes MODEL_DATA and CORRECTED_DATA columns to save disk space.
Parameters:
- msname: str, MeasurementSet path
Returns:
None (modifies MeasurementSet in place)
"""
def addDerivedMSCal(msname):
"""
Add derived calibration columns to MeasurementSet.
Adds computed columns for calibration purposes like UVW coordinates.
Parameters:
- msname: str, MeasurementSet path
Returns:
None (modifies MeasurementSet in place)
"""
def removeDerivedMSCal(msname):
"""
Remove derived calibration columns from MeasurementSet.
Parameters:
- msname: str, MeasurementSet path
Returns:
None (modifies MeasurementSet in place)
"""
def required_ms_desc(table=None):
"""
Get required table description for MeasurementSet or subtable.
Parameters:
- table: str, table type (None/'MAIN' for main table, or subtable name)
Returns:
dict, table description with required columns only
"""
def complete_ms_desc(table=None):
"""
Get complete table description for MeasurementSet or subtable.
Parameters:
- table: str, table type (None/'MAIN' for main table, or subtable name)
Returns:
dict, complete table description with all standard columns
"""Functions for creating table and column descriptions programmatically for table creation.
def tablefromascii(tablename, asciifile, headerfile='', autoheader=False,
autoshape=[], columnnames=[], datatypes=[], sep=' ',
commentmarker='', firstline=1, lastline=-1, readonly=True,
lockoptions='default', ack=True):
"""
Create table from ASCII file with automatic or manual column type detection.
Parameters:
- tablename: str, output table path
- asciifile: str, input ASCII data file path
- headerfile: str, optional header file path (default '')
- autoheader: bool, automatically detect column types (default False)
- autoshape: list, automatic array shape detection (default [])
- columnnames: list, explicit column names (default [])
- datatypes: list, explicit data types (default [])
- sep: str, field separator (default ' ')
- commentmarker: str, comment line marker (default '')
- firstline: int, first line to read (default 1)
- lastline: int, last line to read (-1 for all, default -1)
- readonly: bool, open result table readonly (default True)
- lockoptions: str, locking options (default 'default')
- ack: bool, acknowledge operation (default True)
Returns:
table object for created table
"""
def makescacoldesc(columnname, value, option=0, comment=''):
"""
Create scalar column description.
Parameters:
- columnname: str, column name
- value: default value (determines data type)
- option: int, column options (default 0)
- comment: str, column comment (default '')
Returns:
dict, column description
"""
def makearrcoldesc(columnname, value, ndim=0, shape=[], option=0, comment=''):
"""
Create array column description.
Parameters:
- columnname: str, column name
- value: default value (determines data type)
- ndim: int, number of dimensions (0 for variable, default 0)
- shape: list, fixed shape specification (default [])
- option: int, column options (default 0)
- comment: str, column comment (default '')
Returns:
dict, column description
"""
def makecoldesc(columnname, desc, datamanager='', datamanagertype='',
datamanagergroup='', option=0, maxlen=0, comment='',
valuetype='', shape=[], ndim=-1):
"""
Create general column description with full control.
Parameters:
- columnname: str, column name
- desc: value type descriptor or default value
- datamanager: str, data manager name (default '')
- datamanagertype: str, data manager type (default '')
- datamanagergroup: str, data manager group (default '')
- option: int, column options (default 0)
- maxlen: int, maximum string length (default 0)
- comment: str, column comment (default '')
- valuetype: str, explicit value type (default '')
- shape: list, array shape specification (default [])
- ndim: int, number of dimensions (-1 for scalar, default -1)
Returns:
dict, column description
"""
def maketabdesc(descs, **kwargs):
"""
Create table description from column descriptions.
Parameters:
- descs: list of dict, column descriptions
- **kwargs: additional table description parameters
Returns:
dict, complete table description
"""Convenient column-oriented interface that eliminates need to repeatedly specify column names.
class tablecolumn:
def __init__(self, table, columnname):
"""
Create column interface.
Parameters:
- table: table object
- columnname: str, column name
"""
def getcell(self, rownr):
"""Get single cell value from this column."""
def putcell(self, rownr, value):
"""Set single cell value in this column."""
def getcol(self, startrow=0, nrow=-1, rowincr=1):
"""Get column data."""
def putcol(self, value, startrow=0, nrow=-1, rowincr=1):
"""Set column data."""
def name(self):
"""Get column name."""
def datatype(self):
"""Get column data type."""
def ndim(self, rownr=-1):
"""Get number of dimensions (for array columns)."""
def shape(self, rownr=-1):
"""Get shape (for array columns)."""Access table data in row-oriented fashion with support for row iteration and bulk row operations.
class tablerow:
def __init__(self, table, columnnames=[], exclude=False):
"""
Create row interface.
Parameters:
- table: table object
- columnnames: list, columns to include (empty for all)
- exclude: bool, exclude specified columns instead
"""
def get(self, rownr):
"""Get row data as dictionary."""
def put(self, rownr, value, matchingfields=True):
"""
Set row data from dictionary.
Parameters:
- rownr: int, row number
- value: dict, row data
- matchingfields: bool, only update matching fields
"""Iterate through table based on grouping criteria with automatic sorting and grouping by column values.
class tableiter:
def __init__(self, table, columnnames, order="", sort=True):
"""
Create table iterator.
Parameters:
- table: table object
- columnnames: list, columns to group by
- order: str, sort order specification
- sort: bool, sort before iterating (default True)
"""
def next(self):
"""Get next group as table object."""
def reset(self):
"""Reset iterator to beginning."""Create and use indexes on tables for fast lookups and range queries.
class tableindex:
def __init__(self, table, columnnames, sort=True):
"""
Create table index.
Parameters:
- table: table object
- columnnames: list, columns to index
- sort: bool, sort index (default True)
"""
def find(self, key):
"""
Find rows matching key.
Parameters:
- key: value or list of values to find
Returns:
list of row numbers
"""
def findrows(self, key):
"""Find rows matching key (alias for find)."""from casacore.tables import table, taql
# Open table and get basic info
t = table('observation.ms')
print(f"Rows: {t.nrows()}, Columns: {t.ncols()}")
print("Column names:", t.colnames())
# Access specific data
times = t.getcol('TIME')
first_data = t.getcell('DATA', 0)
# Query with TaQL
selected = taql("SELECT TIME, ANTENNA1, ANTENNA2 FROM observation.ms WHERE ANTENNA1 < 5")
print(f"Selected {selected.nrows()} rows")
# Clean up
selected.close()
t.close()from casacore.tables import default_ms, default_ms_subtable
# Create MeasurementSet with custom columns
desc = {
'DATA': {'comment': 'Visibility data', 'shape': [4, 64]},
'FLAG': {'comment': 'Flag data', 'shape': [4, 64]}
}
ms = default_ms('new_observation.ms', tabdesc=desc)
# Create subtables
ant_table = default_ms_subtable('ANTENNA', 'new_observation.ms/ANTENNA')
spw_table = default_ms_subtable('SPECTRAL_WINDOW', 'new_observation.ms/SPECTRAL_WINDOW')
ms.close()
ant_table.close()
spw_table.close()# Complex TaQL query with joins and aggregation
query = """
SELECT ANTENNA1, ANTENNA2, mean(amplitude(DATA)) as avg_amp
FROM observation.ms
WHERE TIME > mjd('2023-01-01') AND TIME < mjd('2023-01-02')
GROUP BY ANTENNA1, ANTENNA2
ORDER BY avg_amp DESC
"""
result = taql(query)
print("Average amplitudes by baseline:")
for i in range(result.nrows()):
ant1 = result.getcell('ANTENNA1', i)
ant2 = result.getcell('ANTENNA2', i)
avg_amp = result.getcell('avg_amp', i)
print(f" {ant1}-{ant2}: {avg_amp:.3f}")
result.close()Install with Tessl CLI
npx tessl i tessl/pypi-python-casacore