Exemplo n.º 1
0
 def create_col(cname, cval):
     store[cname] = crates.CrateData()
     store[cname].name = cname
     if 1 != store[cname].load(cval, 1):
         raise RuntimeError, "Unable to add column %s to the Crate Data object." % cname
     if 1 != crates.add_col(cr, store[cname]):
         raise RuntimeError, "Unable to add column %s to the Crates." % cname
Exemplo n.º 2
0
def _set_column(crate, name, val):
    """
    checked for new crates
    """
    col = pycrates.CrateData()
    col.name = name.upper()
    col.values = numpy.array(val)
    pycrates.add_col(crate, col)
Exemplo n.º 3
0
def make_image_crate(pixvals):
    """Create an image Crate from the given array.

    Parameters
    ----------
    pixvals
        The pixel values.

    Returns
    -------
    icrate
        An image crate which contains the pixel values and has
        some metadata items attached to it (CREATOR and DATE
        keywords).

    See Also
    --------
    make_table_crate

    Notes
    -----
    The semantics of whether the input array is copied or
    just referenced in the crate are currently not specified.
    Changing the original array after the crate has been created
    *may* change the value in the Crate, but this behavior should
    not be taken advantage of.

    Examples
    --------

    >>> a = np.arange(400 * 500).reshape(400, 500)
    >>> cr = make_image_crate(a)

    """

    cd = pycrates.CrateData()
    cd.values = pixvals

    cr = pycrates.IMAGECrate()
    cr.name = "IMAGE"
    cr.add_image(cd)

    _add_basic_metadata(cr, 'make_image_crate')
    return cr
Exemplo n.º 4
0
def set_image_data(filename,
                   data,
                   header,
                   ascii=False,
                   clobber=False,
                   packup=False):

    if not packup and os.path.isfile(filename) and not clobber:
        raise IOErr('filefound', filename)

    img = pycrates.IMAGECrate()

    # Write Image Header Keys
    for key in header.keys():
        if header[key] is None:
            continue
        _set_key(img, key, header[key])

    # Write Image WCS Header Keys
    if data['eqpos'] is not None:
        cdeltw = data['eqpos'].cdelt
        crvalw = data['eqpos'].crval
        crpixw = data['eqpos'].crpix
        equin = data['eqpos'].equinox

    if data['sky'] is not None:
        cdeltp = data['sky'].cdelt
        crvalp = data['sky'].crval
        crpixp = data['sky'].crpix

        _set_key(img, 'MTYPE1', 'sky     ')
        _set_key(img, 'MFORM1', 'x,y     ')
        _set_key(img, 'CTYPE1P', 'x       ')
        _set_key(img, 'CTYPE2P', 'y       ')
        _set_key(img, 'WCSNAMEP', 'PHYSICAL')
        _set_key(img, 'CDELT1P', cdeltp[0])
        _set_key(img, 'CDELT2P', cdeltp[1])
        _set_key(img, 'CRPIX1P', crpixp[0])
        _set_key(img, 'CRPIX2P', crpixp[1])
        _set_key(img, 'CRVAL1P', crvalp[0])
        _set_key(img, 'CRVAL2P', crvalp[1])

        if data['eqpos'] is not None:
            # Simply the inverse of read transformations in get_image_data
            cdeltw = cdeltw * cdeltp
            crpixw = ((crpixw - crvalp) / cdeltp + crpixp)

    if data['eqpos'] is not None:
        _set_key(img, 'MTYPE2', 'EQPOS   ')
        _set_key(img, 'MFORM2', 'RA,DEC  ')
        _set_key(img, 'CTYPE1', 'RA---TAN')
        _set_key(img, 'CTYPE2', 'DEC--TAN')
        _set_key(img, 'CDELT1', cdeltw[0])
        _set_key(img, 'CDELT2', cdeltw[1])
        _set_key(img, 'CRPIX1', crpixw[0])
        _set_key(img, 'CRPIX2', crpixw[1])
        _set_key(img, 'CRVAL1', crvalw[0])
        _set_key(img, 'CRVAL2', crvalw[1])
        _set_key(img, 'EQUINOX', equin)

    # Write Image pixel values
    pix_col = pycrates.CrateData()
    pix_col.values = data['pixels']
    # pycrates.add_piximg(img, pix_col)
    img.add_image(pix_col)

    if packup:
        return img

    if ascii and '[' not in filename and ']' not in filename:
        # filename += "[opt kernel=text/simple]"
        raise IOErr('writenoimg')

    # pycrates.write_file(img, filename)
    img.write(filename, clobber=True)
    close_crate_dataset(img.get_dataset())
Exemplo n.º 5
0
def add_colvals(cr, colname, colvals, unit=None, desc=None):
    """Add a new column to the crate.

    Create a new column in the crate with the given name and data
    values. The unit and desc fields are used to set the column unit
    and description if given.

    Parameters
    ----------
    cr
        A TABLECrate.
    colname : str
        The name of the column to add. If it already exists in the
        crate then a ValueError is raised, and the check is
        case insensitive.
    colvals
        The column data to add. It can be two dimensional, in which
        case each row of the table contains an array. It is expected
        that this is a NumPy array, but any iterable can be used.
        Note that unsupported data types are only recognized when the
        file is written out, not when the data is added to the crate.
    unit : None or str, optional
        The units value for the column. There are approved formats
        for FITS files but no check is made here.  This may be
        truncated, or not written out, depending on the file format
        used.
    desc : None or str, optional
        The description for the column. This may be truncated, or
        not written out, depending on the file format used.

    See Also
    --------
    make_table_crate

    Notes
    -----
    Validation of the column values - such as supported data types
    and whether the number of rows matches the other columns - is
    done when the crate is written out.

    Example
    -------

    >>> cr = pycrates.TABLECrate()
    >>> add_colvals(cr, 'index', [1, 2, 4], desc='Index')
    >>> add_colvals(cr, 'age', x, unit='year')
    >>> add_colvals(cr, 'blen', y, unit='cm', desc='Beard Length')

    """

    # In CIAO 4.4 there is some confusion within Crates if you
    # over-write a column, so force users to be explicit here.
    # In more recent versions of CIAO the add_column call will fail
    # if the column already exists, but leave this check in.
    #
    if cr.column_exists(colname):
        raise ValueError("Column {} already exists in the table crate.".format(colname))

    cd = pycrates.CrateData()
    cd.name = colname

    cd.values = colvals
    if unit is not None:
        cd.unit = unit
    if desc is not None:
        cd.desc = desc

    cr.add_column(cd)