Exemplo n.º 1
0
def tablestructure(tablename, dataman=True, column=True, subtable=False,
                   sort=False):
    """Print the structure of a table.

    It is the same as :func:`table.showstructure`, but without the need to open
    the table first.

    """
    t = table(tablename, ack=False)
    six.print_(t.showstructure(dataman, column, subtable, sort))
Exemplo n.º 2
0
def tablestructure(tablename,
                   dataman=True,
                   column=True,
                   subtable=False,
                   sort=False):
    """Print the structure of a table.

    It is the same as :func:`table.showstructure`, but without the need to open
    the table first.

    """
    t = table(tablename, ack=False)
    six.print_(t.showstructure(dataman, column, subtable, sort))
Exemplo n.º 3
0
def tabledelete (tablename, checksubtables=False, ack=True):
    """Delete a table on disk.

    It is the same as :func:`table.delete`, but without the need to open
    the table first.

    """
    tabname = _remove_prefix(tablename)
    t = table(tabname, ack=False)
    if t.ismultiused(checksubtables):
        six.print_('Table', tabname, 'cannot be deleted; it is still in use')
    else:
        t = 0
        table(tabname, readonly=False, _delete=True, ack=False)
        if ack:
            six.print_('Table', tabname, 'has been deleted')
Exemplo n.º 4
0
def tabledelete(tablename, checksubtables=False, ack=True):
    """Delete a table on disk.

    It is the same as :func:`table.delete`, but without the need to open
    the table first.

    """
    tabname = _remove_prefix(tablename)
    t = table(tabname, ack=False)
    if t.ismultiused(checksubtables):
        six.print_('Table', tabname, 'cannot be deleted; it is still in use')
    else:
        t = 0
        table(tabname, readonly=False, _delete=True, ack=False)
        if ack:
            six.print_('Table', tabname, 'has been deleted')
Exemplo n.º 5
0
 def addconstraint(self, x, y=0, fnct=None, fid=0):
     self._checkid(fid)
     i = 0
     if "constraint" in self._fitids[fid].has_key:
         i = len(self._fitids[fid]["constraint"])
     else:
         self._fitids[fid]["constraint"] = {}
     # dict key needs to be string
     i = str(i)
     self._fitids[fid]["constraint"][i] = {}
     if isinstance(fnct, functional):
         self._fitids[fid]["constraint"][i]["fnct"] = fnct.todict()
     else:
         self._fitids[fid]["constraint"][i]["fnct"] = \
             functional("hyper", len(x)).todict()
     self._fitids[fid]["constraint"][i]["x"] = [float(v) for v in x]
     self._fitids[fid]["constraint"][i]["y"] = float(y)
     six.print_(self._fitids[fid]["constraint"])
Exemplo n.º 6
0
 def addconstraint(self, x, y=0, fnct=None, fid=0):
     self._checkid(fid)
     i = 0
     if self._fitids[fid].has_key("constraint"):
         i = len(self._fitids[fid]["constraint"])
     else:
         self._fitids[fid]["constraint"] = {}
     # dict key needs to be string
     i = str(i)
     self._fitids[fid]["constraint"][i] = {}
     if isinstance(fnct, functional):
         self._fitids[fid]["constraint"][i]["fnct"] = fnct.todict()
     else:
         self._fitids[fid]["constraint"][i]["fnct"] = \
             functional("hyper", len(x)).todict()
     self._fitids[fid]["constraint"][i]["x"] = [float(v) for v in x]
     self._fitids[fid]["constraint"][i]["y"] = float(y)
     six.print_(self._fitids[fid]["constraint"])
Exemplo n.º 7
0
def tablefromascii (tablename, asciifile,
                    headerfile='',
                    autoheader=False, autoshape=[],
                    columnnames=[], datatypes=[],
                    sep=' ',
                    commentmarker='',
                    firstline=1, lastline=-1,
                    readonly=True,
                    lockoptions='default', ack=True):
    """Create a table from an ASCII file.

    Create a table from a file in ASCII format. Columnar data as well as
    table and column keywords may be specified.
    Once the table is created from the ASCII data, it is opened in the
    specified mode and a table object is returned.

    The table columns are filled from a file containing the data values
    separated by a separator (one line per table row). The default
    separator is a blank. Blanks before and after the separator are ignored.
    If a non-blank separator is used, values can be empty. Such values
    default to 0, empty string, or F depending on the data type. E.g.
    1,,2, has 4 values of which the 2nd and 4th are empty and default to 0.
    Similarly if fewer values are given than needed, the missing values
    get the default value.

    Either the data format can be explicitly specified or it can be found
    automatically. The former gives more control in ambiguous situations.
    Both scalar and array columns can be generated from the ASCII input.
    The format string determines the type and optional shape.

    It is possible to give the column names and their data types in
    various ways:

    - Using 2 header lines (as described below) as the first two lines
      in the data file or in a separate header file. This is the default way.
    - Derive them automatically from the data (`autoheader=True`).
    - Using the arguments `columnnames` and
      `datatypes` (as non-empty vectors of strings).
      It implies (`autoheader=False`). The data types should be
      given in the same way as done in headers.

    In automatic mode (`autoheader=True`) the first line
    of the ASCII data is analyzed
    to deduce the data types. Only the types I, D, and A can be
    recognized. A number without decimal point or exponent is I (integer),
    otherwise it is D (double). Any other string is A (string).
    Note that a number may contain a leading sign (+ or -).
    The `autoshape` argument can be used to specify if the input
    should be stored as multiple scalars (the default) or as a single
    array. In the latter case one axis in the shape can be defined as
    variable length by giving it the value 0. It means that the actual
    array shape in a row is determined by the number of values in the
    corresponding input line.
    Columns get the names `Column1`, `Column2`, etc..
    For example:

    1. `autoshape=[]` (which is the default) means that all values
       are to be stored as scalar columns.
    2. `autoshape=0` means that all values in a row are to be stored as
       a variable length vector.
    3. `autoshape=10` defines a fixed length vector. If an input
       line contains less than 10 values, the vector is filled with default
       values. If more than 10 values, the latter values are ignored.
    4. `autoshape=[5,0]` defines a 2-dim array of which the 2nd axis is
       variable. Note that if an input line does not contain a multiple of 5
       values, the array is filled with default values.

    If the format of the table is explicitly specified, it has to be done
    either in the first two lines of the data file (named by the
    argument filename), or in a separate header file (named by the
    argument headerfile). In both forms, table keywords may also be
    specified before the column definitions.
    The column names and types can be described by two lines:

    1. The first line contains the names of the columns.
       These names may be enclosed in quotes (either single or double). 
    2. The second line contains the data type and optionally the shape
       of each column. Valid types are: 

       - S for Short data 
       - I for Integer data 
       - R for Real data 
       - D for Double Precision data 
       - X for Complex data (Real followed by Imaginary) 
       - Z for Complex data (Amplitude then Phase) 
       - DX for Double Precision Complex data (Real followed by Imaginary) 
       - DZ for Double Precision Complex data (Amplitude then Phase) 
       - A for ASCII data (a value must be enclosed in single or double quotes
         if it contains whitespace) 
       - B for Boolean data (False are empty string, 0, or any string
         starting with F, f, N, or n).

    If a column is an array, the shape has to be given after the data type
    without any whitespace. E.g. `I10` defines an integer vector
    of length 10. `A2,5` defines a 2-dim string array with shape
    [2,5]. Note that `I` is not the same as `I1` as the
    first one defines a scalar and the other one a vector with length 1.
    The last column can have one variable length axis denoted by the value 0.
    It "consumes" the remainder of the input line.

    If the argument headerfile is set then the header information is
    read from that file instead of the first lines of the data file.

    To give a simple example of the form where the header information
    is located at the top of the data file::

      COLI   COLF   COLD       COLX        COLZ       COLS
        I      R      D          X           Z          A
        1      1.1    1.11       1.12 1.13   1.14 1.15  Str1
        10     11     12         13   14     15   16    ""

    Note that a complex number consists of 2 numbers.
    Also note that an empty string can be given.

    Let us now give an example of a separate header file that one might use to
    get interferometer data into casacore::

      U     V      W         TIME        ANT1       ANT2      DATA
      R     R      R          D           I          I        X1,0

    The data file would then look like::

      124.011 54560.0  3477.1  43456789.0990    1      2        4.327 -0.1132
      34561.0 45629.3  3900.5  43456789.0990    1      3        5.398 0.4521

    Note that the DATA column is defined as a 2-dim array of 1
    correlation and a variable number of channels, so the actual number of
    channels is determined by the input. In this example both rows will
    have 1 channel (note that a complex value contains 2 values).

    Tables may have keywords in addition to the columns. The keywords
    are useful for holding information that is global to the entire
    table (such as author, revision, history, etc.).
    The keywords in the header definitions must preceed the column descriptions.
    They must be enclosed between a line that starts with ".key..." and
    a line that starts with ".endkey..." (where ... can be anything).
    A table keywordset and column keywordsets can be specified.
    The latter can be specified by specifying the column name after
    the .keywords string.
    Between these two lines each line should contain the following:

    - The keyword name, e.g., ANYKEY 
    - The datatype and optional  shape of the keyword
      (cf. list of valid types above) 
    - The value or values for the keyword (the keyword may contain 
      a scalar or an array of values). e.g., 3.14159 21.78945 

    Thus to continue the example above, one might wish to add keywords
    as follows::

      .keywords
      DATE        A  "97/1/16"
      REVISION    D 2.01
      AUTHOR      A "Tim Cornwell"
      INSTRUMENT  A "VLA"
      .endkeywords
      .keywords TIME
      UNIT A "s"
      .endkeywords
      U     V      W         TIME        ANT1       ANT2      DATA
      R     R      R          D           I          I        X1,0

    Similarly to the column format string, the keyword formats can also
    contain shape information. The only difference is that if no shape is
    given, a keyword can have multiple values (making it a vector).

    It is possible to ignore comment lines in the header and data file
    by giving the `commentmarker`. It indicates that lines
    starting with the given marker are ignored. Note that the marker can
    be a regular expression (e.g. `' *//'` tells that lines starting
    with // and optionally preceeded by blanks have to be ignored).

    With the arguments `firstline` and `lastline` one can
    specify which lines have to be taken from the input file. A negative value
    means 1 for `firstline` or end-of-file for `lastline`.
    Note that if the headers and data are combined in one file,
    these line arguments apply to the whole file. If headers and data are in
    separate files, these line arguments apply to the data file only.

    Also note that ignored comment lines are counted, thus are used to
    determine which lines are in the line range.

    The number of rows is determined by the number of lines read from the data
    file.

    """
    import os.path
    filename = os.path.expandvars(asciifile)
    filename = os.path.expanduser(filename)
    if not os.path.exists(filename):
        s = "File '%s' not found" % (filename)
        raise IOError(s)
    if headerfile != '':
        filename = os.path.expandvars(headerfile)
        filename = os.path.expanduser(filename)
        if not os.path.exists(filename):
            s = "File '%s' not found" % (filename)
            raise IOError(s)
    tab = table(asciifile, headerfile, tablename, autoheader, autoshape,
                sep, commentmarker, firstline, lastline,
                _columnnames=columnnames, _datatypes=datatypes, _oper=1)
    six.print_('Input format: [' + tab._getasciiformat() +']')
    # Close table and reopen it in correct way.
    tab = 0
    return table(tablename, readonly=readonly, lockoptions=lockoptions,
                 ack=ack)
Exemplo n.º 8
0
def addImagingColumns(msname, ack=True):
    """ Add the columns to an MS needed for the casa imager.

    It adds the columns MODEL_DATA, CORRECTED_DATA, and IMAGING_WEIGHT.
    It also sets the CHANNEL_SELECTION keyword needed for the older casa
    imagers.

    A column is not added if already existing.
    """
    # numpy is needed
    import numpy as np
    # Open the MS
    t = table(msname, readonly=False, ack=False)
    cnames = t.colnames()
    # Get the description of the DATA column.
    try:
        cdesc = t.getcoldesc('DATA')
    except:
        raise ValueError('Column DATA does not exist')
    # Determine if the DATA storage specification is tiled.
    hasTiled = False
    try:
        dminfo = t.getdminfo("DATA")
        if dminfo['TYPE'][:5] == 'Tiled':
            hasTiled = True
    except:
        hasTiled = False
    # Use TiledShapeStMan if needed.
    if not hasTiled:
        dminfo = {'TYPE': 'TiledShapeStMan',
                  'SPEC': {'DEFAULTTILESHAPE': [4, 32, 128]}}
    # Add the columns(if not existing). Use the description of the DATA column.
    if 'MODEL_DATA' in cnames:
        six.print_("Column MODEL_DATA not added; it already exists")
    else:
        dminfo['NAME'] = 'modeldata'
        cdesc['comment'] = 'The model data column'
        t.addcols(maketabdesc(makecoldesc('MODEL_DATA', cdesc)), dminfo)
        if ack:
            six.print_("added column MODEL_DATA")
    if 'CORRECTED_DATA' in cnames:
        six.print_("Column CORRECTED_DATA not added; it already exists")
    else:
        dminfo['NAME'] = 'correcteddata'
        cdesc['comment'] = 'The corrected data column'
        t.addcols(maketabdesc(makecoldesc('CORRECTED_DATA', cdesc)), dminfo)
        if ack:
            six.print_("'added column CORRECTED_DATA")
    if 'IMAGING_WEIGHT' in cnames:
        six.print_("Column IMAGING_WEIGHT not added; it already exists")
    else:
        # Add IMAGING_WEIGHT which is 1-dim and has type float.
        # It needs a shape, otherwise the CASA imager complains.
        shp = []
        if 'shape' in cdesc:
            shp = cdesc['shape']
        if len(shp) > 0:
            shp = [shp[0]]  # use nchan from shape
        else:
            shp = [t.getcell('DATA', 0).shape[0]]  # use nchan from actual data
        cd = makearrcoldesc('IMAGING_WEIGHT', 0, ndim=1, shape=shp,
                            valuetype='float')
        dminfo = {'TYPE': 'TiledShapeStMan',
                  'SPEC': {'DEFAULTTILESHAPE': [32, 128]}}
        dminfo['NAME'] = 'imagingweight'
        t.addcols(maketabdesc(cd), dminfo)
        if ack:
            six.print_("added column IMAGING_WEIGHT")
    # Add or overwrite keyword CHANNEL_SELECTION.
    if 'CHANNEL_SELECTION' in t.colkeywordnames('MODEL_DATA'):
        t.removecolkeyword('MODEL_DATA', 'CHANNEL_SELECTION')
    # Define the CHANNEL_SELECTION keyword containing the channels of
    # all spectral windows.
    tspw = table(t.getkeyword('SPECTRAL_WINDOW'), ack=False)
    nchans = tspw.getcol('NUM_CHAN')
    chans = [[0, nch] for nch in nchans]
    t.putcolkeyword('MODEL_DATA', 'CHANNEL_SELECTION', np.int32(chans))
    if ack:
        six.print_("defined keyword CHANNEL_SELECTION in column MODEL_DATA")
    # Flush the table to make sure it is written.
    t.flush()
Exemplo n.º 9
0
def msregularize(msname, newname):
    """ Regularize an MS

    The output MS will be such that it has the same number of baselines
    for each time stamp. Where needed fully flagged rows are added.

    Possibly missing rows are written into a separate MS <newname>-add.
    It is concatenated with the original MS and sorted in order of TIME,
    DATADESC_ID, ANTENNA1,ANTENNA2 to form a new regular MS. Note that
    the new MS references the input MS (it does not copy the data).
    It means that changes made in the new MS are also made in the input MS.

    If no rows were missing, the new MS is still created referencing the
    input MS.
    """

    # Find out all baselines.
    t = table(msname)
    t1 = t.sort('unique ANTENNA1,ANTENNA2')
    nadded = 0
    # Now iterate in time,band over the MS.
    for tsub in t.iter(['TIME', 'DATA_DESC_ID']):
        nmissing = t1.nrows() - tsub.nrows()
        if nmissing < 0:
            raise ValueError("A time/band chunk has too many rows")
        if nmissing > 0:
            # Rows needs to be added for the missing baselines.
            ant1 = str(t1.getcol('ANTENNA1')).replace(' ', ',')
            ant2 = str(t1.getcol('ANTENNA2')).replace(' ', ',')
            ant1 = tsub.getcol('ANTENNA1')
            ant2 = tsub.getcol('ANTENNA2')
            t2 = taql('select from $t1 where !any(ANTENNA1 == $ant1 &&' +
                      ' ANTENNA2 == $ant2)')
            six.print_(nmissing, t1.nrows(), tsub.nrows(), t2.nrows())
            if t2.nrows() != nmissing:
                raise ValueError("A time/band chunk behaves strangely")
            # If nothing added yet, create a new table.
            # (which has to be reopened for read/write).
            # Otherwise append to that new table.
            if nadded == 0:
                tnew = t2.copy(newname + "_add", deep=True)
                tnew = table(newname + "_add", readonly=False)
            else:
                t2.copyrows(tnew)
            # Set the correct time and band in the new rows.
            tnew.putcell('TIME',
                         range(nadded, nadded + nmissing),
                         tsub.getcell('TIME', 0))
            tnew.putcell('DATA_DESC_ID',
                         range(nadded, nadded + nmissing),
                         tsub.getcell('DATA_DESC_ID', 0))
            nadded += nmissing
    # Combine the existing table and new table.
    if nadded > 0:
        # First initialize data and flags in the added rows.
        taql('update $tnew set DATA=0+0i')
        taql('update $tnew set FLAG=True')
        tcomb = table([t, tnew])
        tcomb.rename(newname + '_adds')
        tcombs = tcomb.sort('TIME,DATA_DESC_ID,ANTENNA1,ANTENNA2')
    else:
        tcombs = t.query(offset=0)
    tcombs.rename(newname)
    six.print_(newname, 'has been created; it references the original MS')
    if nadded > 0:
        six.print_(' and', newname + '_adds', 'containing', nadded, 'new rows')
    else:
        six.print_(' no rows needed to be added')
Exemplo n.º 10
0
 def summary(self):
     six.print_(str(self))
Exemplo n.º 11
0
 def summary(self):
     six.print_(str(self))
 def OnRightDown(self, event):                          #added
     six.print_(self.GetSelectedRows())                     #added
Exemplo n.º 13
0
def addImagingColumns(msname, ack=True):
    """ Add the columns to an MS needed for the casa imager.

    It adds the columns MODEL_DATA, CORRECTED_DATA, and IMAGING_WEIGHT.
    It also sets the CHANNEL_SELECTION keyword needed for the older casa
    imagers.

    A column is not added if already existing.
    """
    # numpy is needed
    import numpy as np
    # Open the MS
    t = table(msname, readonly=False, ack=False)
    cnames = t.colnames()
    # Get the description of the DATA column.
    try:
        cdesc = t.getcoldesc('DATA')
    except:
        raise ValueError('Column DATA does not exist')
    # Determine if the DATA storage specification is tiled.
    hasTiled = False
    try:
        dminfo = t.getdminfo("DATA")
        if dminfo['TYPE'][:5] == 'Tiled':
            hasTiled = True
    except:
        hasTiled = False
    # Use TiledShapeStMan if needed.
    if not hasTiled:
        dminfo = {
            'TYPE': 'TiledShapeStMan',
            'SPEC': {
                'DEFAULTTILESHAPE': [4, 32, 128]
            }
        }
    # Add the columns(if not existing). Use the description of the DATA column.
    if 'MODEL_DATA' in cnames:
        six.print_("Column MODEL_DATA not added; it already exists")
    else:
        dminfo['NAME'] = 'modeldata'
        cdesc['comment'] = 'The model data column'
        t.addcols(maketabdesc(makecoldesc('MODEL_DATA', cdesc)), dminfo)
        if ack:
            six.print_("added column MODEL_DATA")
    if 'CORRECTED_DATA' in cnames:
        six.print_("Column CORRECTED_DATA not added; it already exists")
    else:
        dminfo['NAME'] = 'correcteddata'
        cdesc['comment'] = 'The corrected data column'
        t.addcols(maketabdesc(makecoldesc('CORRECTED_DATA', cdesc)), dminfo)
        if ack:
            six.print_("'added column CORRECTED_DATA")
    if 'IMAGING_WEIGHT' in cnames:
        six.print_("Column IMAGING_WEIGHT not added; it already exists")
    else:
        # Add IMAGING_WEIGHT which is 1-dim and has type float.
        # It needs a shape, otherwise the CASA imager complains.
        shp = []
        if 'shape' in cdesc:
            shp = cdesc['shape']
        if len(shp) > 0:
            shp = [shp[0]]  # use nchan from shape
        else:
            shp = [t.getcell('DATA', 0).shape[0]]  # use nchan from actual data
        cd = makearrcoldesc('IMAGING_WEIGHT',
                            0,
                            ndim=1,
                            shape=shp,
                            valuetype='float')
        dminfo = {
            'TYPE': 'TiledShapeStMan',
            'SPEC': {
                'DEFAULTTILESHAPE': [32, 128]
            }
        }
        dminfo['NAME'] = 'imagingweight'
        t.addcols(maketabdesc(cd), dminfo)
        if ack:
            six.print_("added column IMAGING_WEIGHT")
    # Add or overwrite keyword CHANNEL_SELECTION.
    if 'CHANNEL_SELECTION' in t.colkeywordnames('MODEL_DATA'):
        t.removecolkeyword('MODEL_DATA', 'CHANNEL_SELECTION')
    # Define the CHANNEL_SELECTION keyword containing the channels of
    # all spectral windows.
    tspw = table(t.getkeyword('SPECTRAL_WINDOW'), ack=False)
    nchans = tspw.getcol('NUM_CHAN')
    chans = [[0, nch] for nch in nchans]
    t.putcolkeyword('MODEL_DATA', 'CHANNEL_SELECTION', np.int32(chans))
    if ack:
        six.print_("defined keyword CHANNEL_SELECTION in column MODEL_DATA")
    # Flush the table to make sure it is written.
    t.flush()
Exemplo n.º 14
0
 def __init__(self, imagename, axis=0, maskname="", images=(), values=None,
              coordsys=None, overwrite=True, ashdf5=False, mask=(),
              shape=None, tileshape=()):
     coord = {}
     if not coordsys is None:
         coord = coordsys.dict()
     if isinstance(imagename, Image):
         # Create from the value returned by subimage, etc.
         Image.__init__ (self, imagename)
     else:
         opened = False
         if isinstance(imagename, tuple)  or  isinstance(imagename, list):
             if len(imagename) == 0:
                 raise ValueError('No images given in list or tuple');
             if isinstance(imagename[0], str):
                 # Concatenate from image names
                 Image.__init__ (self, imagename, axis)
                 opened = True
             elif isinstance(imagename[0], image):
                 # Concatenate from image objects
                 Image.__init__ (self, imagename, axis, 0, 0)
                 opened = True
         if not opened:
             if not isinstance(imagename, str):
                 raise ValueError("first argument must be name or sequence of images or names")
             if shape is None:
                 if values is None:
                     # Open an image from name or expression
                     # Copy the tables argument and make sure it is a list
                     imgs = []
                     for img in images:
                         imgs += [img]
                     try:
                         # Substitute possible $ arguments
                         import casacore.util
                         imagename = casacore.util.substitute(imagename, [(image, '', imgs)], locals=casacore.util.getlocals(3))
                     except:
                         six.print_("Probably could not import casacore.util")
                         pass
                     Image.__init__ (self, imagename, maskname, imgs)
                 else:
                     # Create an image from an array
                     # The values can be a masked array;
                     #  use the mask if no explicit mask is given
                     if isinstance(values, nma.MaskedArray):
                         if len(mask) == 0:
                             mask = nma.getmaskarray(values)
                         values = values.data
                     if len(mask) > 0:
                         mask = -mask;  # casa and numpy have opposite flags
                     Image.__init__ (self, values, mask, coord,
                                     imagename, overwrite, ashdf5,
                                     maskname, tileshape)
             else:
                 # Create an image from a shape (values gives the data type)
                 # default type is float.
                 if values is None:
                     values = numpy.array([0],dtype='float32')[0]
                 Image.__init__ (self, shape, values, coord,
                                 imagename, overwrite, ashdf5,
                                 maskname, tileshape, 0)
Exemplo n.º 15
0
    def view(self, tempname='/tmp/tempimage'):
        """Display the image using casaviewer.

        If the image is not persistent, a copy will be made that the user
        has to delete once viewing has finished. The name of the copy can be
        given in argument `tempname`. Default is '/tmp/tempimage'.

        """
        import os
        # Test if casaviewer can be found.
        # On OS-X 'which' always returns 0, so use test on top of it.
        if os.system('test -x `which casaviewer` > /dev/null 2>&1') == 0:
            six.print_("Starting casaviewer in the background ...")
            self.unlock()
            if self.ispersistent():
                os.system('casaviewer ' + self.name() + ' &')
            elif len(tempname) > 0:
                six.print_("  making a persistent copy in " + tempname)
                six.print_(
                    "  which should be deleted after the viewer has ended")
                self.saveas(tempname)
                os.system('casaviewer ' + tempname + ' &')
            else:
                six.print_("Cannot view because the image is in memory only.")
                six.print_(
                    "You can browse a persistent copy of the image like:")
                six.print_("   t.view('/tmp/tempimage')")
        else:
            six.print_("casaviewer cannot be found")
Exemplo n.º 16
0
 def __init__(self,
              imagename,
              axis=0,
              maskname="",
              images=(),
              values=None,
              coordsys=None,
              overwrite=True,
              ashdf5=False,
              mask=(),
              shape=None,
              tileshape=()):
     coord = {}
     if coordsys is not None:
         coord = coordsys.dict()
     if isinstance(imagename, Image):
         # Create from the value returned by subimage, etc.
         Image.__init__(self, imagename)
     else:
         opened = False
         if isinstance(imagename, tuple) or isinstance(imagename, list):
             if len(imagename) == 0:
                 raise ValueError('No images given in list or tuple')
             if isinstance(imagename[0], str):
                 # Concatenate from image names
                 Image.__init__(self, imagename, axis)
                 opened = True
             elif isinstance(imagename[0], image):
                 # Concatenate from image objects
                 Image.__init__(self, imagename, axis, 0, 0)
                 opened = True
         if not opened:
             if not isinstance(imagename, str):
                 raise ValueError("first argument must be name or" +
                                  " sequence of images or names")
             if shape is None:
                 if values is None:
                     # Open an image from name or expression
                     # Copy the tables argument and make sure it is a list
                     imgs = []
                     for img in images:
                         imgs += [img]
                     try:
                         # Substitute possible $ arguments
                         import casacore.util as cu
                         imagename = cu.substitute(imagename,
                                                   [(image, '', imgs)],
                                                   locals=cu.getlocals(3))
                     except:
                         six.print_(
                             "Probably could not import casacore.util")
                         pass
                     Image.__init__(self, imagename, maskname, imgs)
                 else:
                     # Create an image from an array
                     # The values can be a masked array
                     #  use the mask if no explicit mask is given
                     if isinstance(values, nma.MaskedArray):
                         if len(mask) == 0:
                             mask = nma.getmaskarray(values)
                         values = values.data
                     if len(mask) > 0:
                         mask = -mask  # casa and numpy have opposite flags
                     Image.__init__(self, values, mask, coord, imagename,
                                    overwrite, ashdf5, maskname, tileshape)
             else:
                 # Create an image from a shape (values gives the data type)
                 # default type is float.
                 if values is None:
                     values = numpy.array([0], dtype='float32')[0]
                 Image.__init__(self, shape, values, coord, imagename,
                                overwrite, ashdf5, maskname, tileshape, 0)
Exemplo n.º 17
0
    def view (self, tempname='/tmp/tempimage'):
        """Display the image using casaviewer.

        If the image is not persistent, a copy will be made that the user
        has to delete once viewing has finished. The name of the copy can be
        given in argument `tempname`. Default is '/tmp/tempimage'.

        """
        import os
        # Test if casaviewer can be found.
        # On OS-X 'which' always returns 0, so use test on top of it.
        if os.system('test -x `which casaviewer` > /dev/null 2>&1') == 0:
            six.print_("Starting casaviewer in the background ...")
            self.unlock()
            if self.ispersistent():
                os.system ('casaviewer ' + self.name() + ' &')
            elif len(tempname) > 0:
                six.print_("  making a persistent copy in " + tempname)
                six.print_("  which should be deleted after the viewer has ended")
                self.saveas (tempname);
                os.system ('casaviewer ' + tempname + ' &')
            else:
                six.print_("Cannot view because the image is in memory only.")
                six.print_("You can browse a persistent copy of the image like:")
                six.print_("   t.view('/tmp/tempimage')")
        else:
            six.print_("casaviewer cannot be found")
Exemplo n.º 18
0
def tablefromascii(tablename,
                   asciifile,
                   headerfile='',
                   autoheader=False,
                   autoshape=[],
                   columnnames=[],
                   datatypes=[],
                   sep=' ',
                   commentmarker='',
                   firstline=1,
                   lastline=-1,
                   readonly=True,
                   lockoptions='default',
                   ack=True):
    """Create a table from an ASCII file.

    Create a table from a file in ASCII format. Columnar data as well as
    table and column keywords may be specified.
    Once the table is created from the ASCII data, it is opened in the
    specified mode and a table object is returned.

    The table columns are filled from a file containing the data values
    separated by a separator (one line per table row). The default
    separator is a blank. Blanks before and after the separator are ignored.
    If a non-blank separator is used, values can be empty. Such values
    default to 0, empty string, or F depending on the data type. E.g.
    1,,2, has 4 values of which the 2nd and 4th are empty and default to 0.
    Similarly if fewer values are given than needed, the missing values
    get the default value.

    Either the data format can be explicitly specified or it can be found
    automatically. The former gives more control in ambiguous situations.
    Both scalar and array columns can be generated from the ASCII input.
    The format string determines the type and optional shape.

    It is possible to give the column names and their data types in
    various ways:

    - Using 2 header lines (as described below) as the first two lines
      in the data file or in a separate header file. This is the default way.
    - Derive them automatically from the data (`autoheader=True`).
    - Using the arguments `columnnames` and
      `datatypes` (as non-empty vectors of strings).
      It implies (`autoheader=False`). The data types should be
      given in the same way as done in headers.

    In automatic mode (`autoheader=True`) the first line
    of the ASCII data is analyzed
    to deduce the data types. Only the types I, D, and A can be
    recognized. A number without decimal point or exponent is I (integer),
    otherwise it is D (double). Any other string is A (string).
    Note that a number may contain a leading sign (+ or -).
    The `autoshape` argument can be used to specify if the input
    should be stored as multiple scalars (the default) or as a single
    array. In the latter case one axis in the shape can be defined as
    variable length by giving it the value 0. It means that the actual
    array shape in a row is determined by the number of values in the
    corresponding input line.
    Columns get the names `Column1`, `Column2`, etc..
    For example:

    1. `autoshape=[]` (which is the default) means that all values
       are to be stored as scalar columns.
    2. `autoshape=0` means that all values in a row are to be stored as
       a variable length vector.
    3. `autoshape=10` defines a fixed length vector. If an input
       line contains less than 10 values, the vector is filled with default
       values. If more than 10 values, the latter values are ignored.
    4. `autoshape=[5,0]` defines a 2-dim array of which the 2nd axis is
       variable. Note that if an input line does not contain a multiple of 5
       values, the array is filled with default values.

    If the format of the table is explicitly specified, it has to be done
    either in the first two lines of the data file (named by the
    argument filename), or in a separate header file (named by the
    argument headerfile). In both forms, table keywords may also be
    specified before the column definitions.
    The column names and types can be described by two lines:

    1. The first line contains the names of the columns.
       These names may be enclosed in quotes (either single or double).
    2. The second line contains the data type and optionally the shape
       of each column. Valid types are:

       - S for Short data
       - I for Integer data
       - R for Real data
       - D for Double Precision data
       - X for Complex data (Real followed by Imaginary)
       - Z for Complex data (Amplitude then Phase)
       - DX for Double Precision Complex data (Real followed by Imaginary)
       - DZ for Double Precision Complex data (Amplitude then Phase)
       - A for ASCII data (a value must be enclosed in single or double quotes
         if it contains whitespace)
       - B for Boolean data (False are empty string, 0, or any string
         starting with F, f, N, or n).

    If a column is an array, the shape has to be given after the data type
    without any whitespace. E.g. `I10` defines an integer vector
    of length 10. `A2,5` defines a 2-dim string array with shape
    [2,5]. Note that `I` is not the same as `I1` as the
    first one defines a scalar and the other one a vector with length 1.
    The last column can have one variable length axis denoted by the value 0.
    It "consumes" the remainder of the input line.

    If the argument headerfile is set then the header information is
    read from that file instead of the first lines of the data file.

    To give a simple example of the form where the header information
    is located at the top of the data file::

      COLI   COLF   COLD       COLX        COLZ       COLS
        I      R      D          X           Z          A
        1      1.1    1.11       1.12 1.13   1.14 1.15  Str1
        10     11     12         13   14     15   16    ""

    Note that a complex number consists of 2 numbers.
    Also note that an empty string can be given.

    Let us now give an example of a separate header file that one might use to
    get interferometer data into casacore::

      U     V      W         TIME        ANT1       ANT2      DATA
      R     R      R          D           I          I        X1,0

    The data file would then look like::

      124.011 54560.0  3477.1  43456789.0990    1      2        4.327 -0.1132
      34561.0 45629.3  3900.5  43456789.0990    1      3        5.398 0.4521

    Note that the DATA column is defined as a 2-dim array of 1
    correlation and a variable number of channels, so the actual number of
    channels is determined by the input. In this example both rows will
    have 1 channel (note that a complex value contains 2 values).

    Tables may have keywords in addition to the columns. The keywords
    are useful for holding information that is global to the entire
    table (such as author, revision, history, etc.).
    The keywords in the header definitions must preceed the column descriptions.
    They must be enclosed between a line that starts with ".key..." and
    a line that starts with ".endkey..." (where ... can be anything).
    A table keywordset and column keywordsets can be specified.
    The latter can be specified by specifying the column name after
    the .keywords string.
    Between these two lines each line should contain the following:

    - The keyword name, e.g., ANYKEY
    - The datatype and optional  shape of the keyword
      (cf. list of valid types above)
    - The value or values for the keyword (the keyword may contain
      a scalar or an array of values). e.g., 3.14159 21.78945

    Thus to continue the example above, one might wish to add keywords
    as follows::

      .keywords
      DATE        A  "97/1/16"
      REVISION    D 2.01
      AUTHOR      A "Tim Cornwell"
      INSTRUMENT  A "VLA"
      .endkeywords
      .keywords TIME
      UNIT A "s"
      .endkeywords
      U     V      W         TIME        ANT1       ANT2      DATA
      R     R      R          D           I          I        X1,0

    Similarly to the column format string, the keyword formats can also
    contain shape information. The only difference is that if no shape is
    given, a keyword can have multiple values (making it a vector).

    It is possible to ignore comment lines in the header and data file
    by giving the `commentmarker`. It indicates that lines
    starting with the given marker are ignored. Note that the marker can
    be a regular expression (e.g. `' *//'` tells that lines starting
    with // and optionally preceeded by blanks have to be ignored).

    With the arguments `firstline` and `lastline` one can
    specify which lines have to be taken from the input file. A negative value
    means 1 for `firstline` or end-of-file for `lastline`.
    Note that if the headers and data are combined in one file,
    these line arguments apply to the whole file. If headers and data are in
    separate files, these line arguments apply to the data file only.

    Also note that ignored comment lines are counted, thus are used to
    determine which lines are in the line range.

    The number of rows is determined by the number of lines read from the data
    file.

    """
    import os.path
    filename = os.path.expandvars(asciifile)
    filename = os.path.expanduser(filename)
    if not os.path.exists(filename):
        s = "File '%s' not found" % (filename)
        raise IOError(s)
    if headerfile != '':
        filename = os.path.expandvars(headerfile)
        filename = os.path.expanduser(filename)
        if not os.path.exists(filename):
            s = "File '%s' not found" % (filename)
            raise IOError(s)
    tab = table(asciifile,
                headerfile,
                tablename,
                autoheader,
                autoshape,
                sep,
                commentmarker,
                firstline,
                lastline,
                _columnnames=columnnames,
                _datatypes=datatypes,
                _oper=1)
    six.print_('Input format: [' + tab._getasciiformat() + ']')
    # Close table and reopen it in correct way.
    tab = 0
    return table(tablename,
                 readonly=readonly,
                 lockoptions=lockoptions,
                 ack=ack)
Exemplo n.º 19
0
def msregularize(msname, newname):
    """ Regularize an MS

    The output MS will be such that it has the same number of baselines
    for each time stamp. Where needed fully flagged rows are added.

    Possibly missing rows are written into a separate MS <newname>-add.
    It is concatenated with the original MS and sorted in order of TIME,
    DATADESC_ID, ANTENNA1,ANTENNA2 to form a new regular MS. Note that
    the new MS references the input MS (it does not copy the data).
    It means that changes made in the new MS are also made in the input MS.

    If no rows were missing, the new MS is still created referencing the
    input MS.
    """

    # Find out all baselines.
    t = table(msname)
    t1 = t.sort('unique ANTENNA1,ANTENNA2')
    nadded = 0
    # Now iterate in time,band over the MS.
    for tsub in t.iter(['TIME', 'DATA_DESC_ID']):
        nmissing = t1.nrows() - tsub.nrows()
        if nmissing < 0:
            raise ValueError("A time/band chunk has too many rows")
        if nmissing > 0:
            # Rows needs to be added for the missing baselines.
            ant1 = str(t1.getcol('ANTENNA1')).replace(' ', ',')
            ant2 = str(t1.getcol('ANTENNA2')).replace(' ', ',')
            ant1 = tsub.getcol('ANTENNA1')
            ant2 = tsub.getcol('ANTENNA2')
            t2 = taql('select from $t1 where !any(ANTENNA1 == $ant1 &&' +
                      ' ANTENNA2 == $ant2)')
            six.print_(nmissing, t1.nrows(), tsub.nrows(), t2.nrows())
            if t2.nrows() != nmissing:
                raise ValueError("A time/band chunk behaves strangely")
            # If nothing added yet, create a new table.
            # (which has to be reopened for read/write).
            # Otherwise append to that new table.
            if nadded == 0:
                tnew = t2.copy(newname + "_add", deep=True)
                tnew = table(newname + "_add", readonly=False)
            else:
                t2.copyrows(tnew)
            # Set the correct time and band in the new rows.
            tnew.putcell('TIME', range(nadded, nadded + nmissing),
                         tsub.getcell('TIME', 0))
            tnew.putcell('DATA_DESC_ID', range(nadded, nadded + nmissing),
                         tsub.getcell('DATA_DESC_ID', 0))
            nadded += nmissing
    # Combine the existing table and new table.
    if nadded > 0:
        # First initialize data and flags in the added rows.
        taql('update $tnew set DATA=0+0i')
        taql('update $tnew set FLAG=True')
        tcomb = table([t, tnew])
        tcomb.rename(newname + '_adds')
        tcombs = tcomb.sort('TIME,DATA_DESC_ID,ANTENNA1,ANTENNA2')
    else:
        tcombs = t.query(offset=0)
    tcombs.rename(newname)
    six.print_(newname, 'has been created; it references the original MS')
    if nadded > 0:
        six.print_(' and', newname + '_adds', 'containing', nadded, 'new rows')
    else:
        six.print_(' no rows needed to be added')
Exemplo n.º 20
0
 def OnRightDown(self, event):  # added
     six.print_(self.GetSelectedRows())  # added