Ejemplo n.º 1
0
def read_ms(infile, verbosity=1):
    """ Convert MS to a HDF file
    :param infile:  Measurement Set path
    :return: HDU version of Measurement Set
    """
    pp = PrintLog(verbosity=verbosity)
    ms = pt.table(infile)

    # Create a HDU List for storing HDUs
    hdul = IdiHdulist(verbosity=verbosity)

    # Add each column to the main HDU
    hdu_main = table2hdu(ms, "MAIN", verbosity=verbosity, close_after=False)
    hdul["MAIN"] = hdu_main

    # Now look for other keyword tables
    for key, val in ms.getkeywords().items():
        pp.debug(val)
        if type(val) in (unicode, str):
            if val.startswith("Table: "):
                tblpath = val.strip().split("Table: ")[1]
                pp.h2("Opening %s" % key)
                t = pt.table(tblpath)
                t_hdu = table2hdu(t, key, verbosity=verbosity)
                hdul[key] = t_hdu
        else:
            hdul["MAIN"].header.vals[key] = val

    ms.close()
    return hdul
Ejemplo n.º 2
0
def table2hdu(table, hd, verbosity=1, close_after=True):
    """ Convert MS table to a Header-Data unit
    :param table: name of table (MS path)
    :param hdu: header data unit, either a string or a HDU
    :return:  HDU version of MS table
    """
    pp = PrintLog(verbosity=verbosity)

    if isinstance(hd, str):
        pp.h3("Creating %s HDU" % hd)
        hd = IdiTableHdu(name=hd)

    colnames = table.colnames()
    keywords = table.getkeywords()

    for colname in colnames:
        try:
            pp.debug("Reading col %s" % colname)
            hd.add_column(table.getcol(colname), name=colname)
        except RuntimeError:
            # This can be raised when no data is in the column
            pp.warn("Could not add %s" % colname)

    for key, val in keywords.items():
        hd.header.vals[key] = val

    if close_after:
        table.close()
    return hd
Ejemplo n.º 3
0
def read_ms(infile, verbosity=1):
    """ Convert MS to a HDF file
    :param infile:  Measurement Set path
    :return: HDU version of Measurement Set
    """
    pp = PrintLog(verbosity=verbosity)
    ms = pt.table(infile)

    # Create a HDU List for storing HDUs
    hdul = IdiHdulist(verbosity=verbosity)

    # Add each column to the main HDU
    hdu_main = table2hdu(ms, "MAIN", verbosity=verbosity, close_after=False)
    hdul["MAIN"] = hdu_main

    # Now look for other keyword tables
    for key, val in ms.getkeywords().items():
        pp.debug(val)
        if type(val) in (unicode, str):
            if val.startswith("Table: "):
                tblpath = val.strip().split("Table: ")[1]
                pp.h2("Opening %s" % key)
                t = pt.table(tblpath)
                t_hdu = table2hdu(t, key, verbosity=verbosity)
                hdul[key] = t_hdu
        else:
            hdul["MAIN"].header.vals[key] = val

    ms.close()
    return hdul
Ejemplo n.º 4
0
def table2hdu(table, hd, verbosity=1, close_after=True):
    """ Convert MS table to a Header-Data unit
    :param table: name of table (MS path)
    :param hdu: header data unit, either a string or a HDU
    :return:  HDU version of MS table
    """
    pp = PrintLog(verbosity=verbosity)

    if isinstance(hd, str):
        pp.h3("Creating %s HDU" % hd)
        hd = IdiTableHdu(name=hd)

    colnames = table.colnames()
    keywords = table.getkeywords()

    for colname in colnames:
        try:
            pp.debug("Reading col %s" % colname)
            hd.add_column(table.getcol(colname), name=colname)
        except RuntimeError:
            # This can be raised when no data is in the column
            pp.warn("Could not add %s" % colname)


    for key, val in keywords.items():
        hd.header.vals[key] = val

    if close_after:
        table.close()
    return hd
Ejemplo n.º 5
0
def create_dataset(hgroup, name, data, **kwargs):
    """ Create dataset from data, will attempt to compress

    :param hgroup: h5py group in which to add dataset
    :param name: name of dataset
    :param data: data to write
    """


    verbosity = 0
    if 'verbosity' in kwargs:
        verbosity = kwargs.pop('verbosity')

    pp = PrintLog(verbosity)

    np_types = [
            np.uint8,
            np.uint16,
            np.uint32,
            np.uint64,
            np.int8,
            np.int16,
            np.int32,
            np.int64,
            np.float16,
            np.float32,
            np.float64,
            np.complex64,
            np.complex128,
            np.void]

    np_types = set(np_types)

    #print name, str(data.dtype)
    #print data.dtype.type, data.dtype.type in np_types
    if data.dtype.type in np_types:
        pp.debug("Creating compressed %s" % name)
        dset = create_compressed(hgroup, name, data, **kwargs)
    else:
        try:
            pp.debug("Creating non-compressed %s" % name)
            dset = hgroup.create_dataset(name, data=data)
        except TypeError:
            #print name, data.dtype
            raise

    return dset
Ejemplo n.º 6
0
def create_dataset(hgroup, name, data, **kwargs):
    """ Create dataset from data, will attempt to compress

    :param hgroup: h5py group in which to add dataset
    :param name: name of dataset
    :param data: data to write
    """

    verbosity = 0
    if 'verbosity' in kwargs:
        verbosity = kwargs.pop('verbosity')

    pp = PrintLog(verbosity)

    np_types = [
        np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32,
        np.int64, np.float16, np.float32, np.float64, np.complex64,
        np.complex128, np.void
    ]

    np_types = set(np_types)

    #print name, str(data.dtype)
    #print data.dtype.type, data.dtype.type in np_types
    if data.dtype.type in np_types:
        pp.debug("Creating compressed %s" % name)
        dset = create_compressed(hgroup, name, data, **kwargs)
    else:
        try:
            pp.debug("Creating non-compressed %s" % name)
            dset = hgroup.create_dataset(name, data=data)
        except TypeError:
            #print name, data.dtype
            raise

    return dset