Exemplo n.º 1
0
def set_arrays(filename, args, fields=None, ascii=True, clobber=False):

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

    if not numpy.iterable(args) or len(args) == 0:
        raise IOErr('noarrayswrite')

    if not numpy.iterable(args[0]):
        raise IOErr('noarrayswrite')

    size = len(args[0])
    for arg in args:
        if not numpy.iterable(arg):
            raise IOErr('noarrayswrite')
        elif len(arg) != size:
            raise IOErr('arraysnoteq')

    if ascii and '[' not in filename and ']' not in filename:
        filename += "[opt kernel=text/simple]"

    tbl = pycrates.TABLECrate()

    if fields is None:
        fields = ['col%i' % (ii+1) for ii in range(len(args))]

    if len(args) != len(fields):
        raise IOErr('toomanycols', str(len(fields)), str(len(args)))

    for val, name in izip(args, fields):
        _set_column(tbl, name, val)

    pycrates.write_file(tbl, filename, clobber=True)
    close_crate_dataset(tbl.get_dataset())
Exemplo n.º 2
0
def set_arrays(filename, args, fields=None, ascii=True, clobber=False):

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

    if not numpy.iterable(args) or len(args) == 0:
        raise IOErr('noarrayswrite')

    if not numpy.iterable(args[0]):
        raise IOErr('noarrayswrite')

    size = len(args[0])
    for arg in args:
        if not numpy.iterable(arg):
            raise IOErr('noarrayswrite')
        elif len(arg) != size:
            raise IOErr('arraysnoteq')

    if ascii and '[' not in filename and ']' not in filename:
        filename += "[opt kernel=text/simple]"

    tbl = pycrates.TABLECrate()

    if fields is None:
        fields = ['col%i' % (ii + 1) for ii in range(len(args))]

    if len(args) != len(fields):
        raise IOErr('toomanycols', str(len(fields)), str(len(args)))

    for val, name in izip(args, fields):
        _set_column(tbl, name, val)

    pycrates.write_file(tbl, filename, clobber=True)
    close_crate_dataset(tbl.get_dataset())
Exemplo n.º 3
0
def set_table_data(filename, data, col_names, hdr=None, hdrnames=None,
                   ascii=False, clobber=False, packup=False):

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

    tbl = pycrates.TABLECrate()

    col_names = [name for name in col_names if data[name] != None]
    col_names.remove('name')    
    try:
        cols = [pycrates.CrateData() for i in range(len(col_names))]
        for name, col in izip(col_names, cols):
            if data[name] is None:
                continue
            _set_column( tbl, col, name, data[name] )

    finally:
        if packup:
            return tbl

        if ascii and '[' not in filename and ']' not in filename:
            filename += "[opt kernel=text/simple]"

        pycrates.write_file(tbl, filename)
Exemplo n.º 4
0
def noise(infits,outimage,mask=None):
	""" Module to add poisson noise to the image data

	Input: Fits image
	Output: Fits image - An image with possion randon noise

	"""
	ctimg = pycrates.read_file(infits)
	img = ctimg.get_image()
	pix = img.values
	noiseimg = poisson(pix)

	if mask != None:
		bla = pycrates.read_file(mask)
		msk_values = bla.get_image().values
		msk = msk_values == 0
		noiseimg[msk] = msk_values[msk]
	
	img.values = noiseimg
	pycrates.write_file(ctimg,outimage,clobber=True)
Exemplo n.º 5
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, pycrates.CrateKey(), 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, pycrates.CrateKey(), 'MTYPE1', 'sky     ')
        _set_key(img, pycrates.CrateKey(), 'MFORM1', 'x,y     ')
        _set_key(img, pycrates.CrateKey(), 'CTYPE1P','x       ')
        _set_key(img, pycrates.CrateKey(), 'CTYPE2P','y       ')
        _set_key(img, pycrates.CrateKey(), 'WCSNAMEP','PHYSICAL')
        _set_key(img, pycrates.CrateKey(), 'CDELT1P', cdeltp[0])
        _set_key(img, pycrates.CrateKey(), 'CDELT2P', cdeltp[1])
        _set_key(img, pycrates.CrateKey(), 'CRPIX1P', crpixp[0])
        _set_key(img, pycrates.CrateKey(), 'CRPIX2P', crpixp[1])
        _set_key(img, pycrates.CrateKey(), 'CRVAL1P', crvalp[0])
        _set_key(img, pycrates.CrateKey(), '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, pycrates.CrateKey(), 'MTYPE2', 'EQPOS   ')
        _set_key(img, pycrates.CrateKey(), 'MFORM2', 'RA,DEC  ')
        _set_key(img, pycrates.CrateKey(), 'CTYPE1', 'RA---TAN')
        _set_key(img, pycrates.CrateKey(), 'CTYPE2', 'DEC--TAN')
        _set_key(img, pycrates.CrateKey(), 'CDELT1', cdeltw[0])
        _set_key(img, pycrates.CrateKey(), 'CDELT2', cdeltw[1])
        _set_key(img, pycrates.CrateKey(), 'CRPIX1', crpixw[0])
        _set_key(img, pycrates.CrateKey(), 'CRPIX2', crpixw[1])
        _set_key(img, pycrates.CrateKey(), 'CRVAL1', crvalw[0])
        _set_key(img, pycrates.CrateKey(), 'CRVAL2', crvalw[1])
        _set_key(img, pycrates.CrateKey(), 'EQUINOX', equin)

    # Write Image pixel values
    shape = data['pixels'].shape
    pix_col = pycrates.CrateData()
    pix_col.set_dimarr( shape, len(shape) )
    pix_col.set_nsets(1)
    pix_col.set_values( data['pixels'].ravel() )

    pycrates.add_piximg(img, 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)
Exemplo n.º 6
0
def write_draws(draws, outfile, params=None, format="ascii"):
    """Writes the draws (except for iteration number 0) to the file outfile.

    Over-writes the file if it exists. If params is given then it
    should be the output of get_parameter_info and is used to
    add extra information to the output file (well, it will do,
    for now it does not).

    The output format is determined by the format parameter; it
    can be one of
      "fits"  - FITS binary table
      "ascii" - simple ASCII table (two comment lines then data)
      "dtf"   - ASCII file in CIAO TEXT/DTF format (CIAO tools can
                treat it similarly to a FITS table)
      None    - let Crates determine, probably because you have
                specified an option within the outfile name
                (specialised use only)
    """

    formats = {"ascii": "text/simple", "dtf": "text/dtf", "fits": "fits"}
    if format != None and not formats.has_key(format.lower()):
        raise RuntimeError, "Unknown output format '%s'" % format

    # Use crates to create the output file.
    #
    cr = crates.TABLECrate()

    # We need to store the crate data objects so that they are not
    # garbage collected until after the write_file() call.
    #
    store = {}

    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

    def create_key(kname, kval, desc=None):
        store[kname] = crates.CrateKey()
        store[kname].name = kname
        if desc != None:
            store[kname].desc = desc
        if 1 != store[kname].load(kval):
            raise RuntimeError, "Unable to set key %s to %g" % (name, kval)
        if 1 != crates.add_key(cr, store[kname]):
            raise RuntimeError, "Unable to add key %s to Crates." % kname

    # Column data, and then keywords.
    # Note that we do not write out any iteration numbered 0 as this is
    # assumed to be the "best-fit" position.
    #
    idx = draws["iteration"] > 0
    for cname in ["iteration", "accept", "statistic"]:
        create_col(cname, draws[cname][idx])
    for cname in draws["parnames"]:
        create_col(cname, draws[cname][idx])

    if params == None:
        create_key("statmin", draws["statistic"][0], "statistic at best-fit location")
        for k in draws["parnames"]:
            create_key("P_%s" % k, draws[k][0], "best-fit value")
    else:
        create_key("statmin", params["statval"], "statistic at best-fit location")
        create_key("sigma", params["sigma"], "sigma value for lower/upper limits")
        for (k, v, l, h) in zip(params["parnames"], params["parvals"], params["parmins"], params["parmaxes"]):
            create_key("p_%s" % k, v, "best-fit value")
            create_key("dl_%s" % k, l, "best-fit lower limit")
            create_key("dh_%s" % k, h, "best-fit upper limit")

    # Write out the file
    #
    if format == None:
        outname = outfile
    else:
        outname = outfile + "[opt kernel=%s]" % formats[format.lower()]
    if 1 != crates.write_file(cr, outname):
        raise IOError, "Unable to write data out to '%s'" % outname
    print "Created: %s" % outfile
Exemplo n.º 7
0
def write_draws(draws, outfile, params=None, format="ascii"):
    """Writes the draws (except for iteration number 0) to the file outfile.

    Over-writes the file if it exists. If params is given then it
    should be the output of get_parameter_info and is used to
    add extra information to the output file (well, it will do,
    for now it does not).

    The output format is determined by the format parameter; it
    can be one of
      "fits"  - FITS binary table
      "ascii" - simple ASCII table (two comment lines then data)
      "dtf"   - ASCII file in CIAO TEXT/DTF format (CIAO tools can
                treat it similarly to a FITS table)
      None    - let Crates determine, probably because you have
                specified an option within the outfile name
                (specialised use only)
    """

    formats = {"ascii": "text/simple", "dtf": "text/dtf", "fits": "fits"}
    if format != None and not formats.has_key(format.lower()):
        raise RuntimeError, "Unknown output format '%s'" % format

    # Use crates to create the output file.
    #
    cr = crates.TABLECrate()

    # We need to store the crate data objects so that they are not
    # garbage collected until after the write_file() call.
    #
    store = {}

    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

    def create_key(kname, kval, desc=None):
        store[kname] = crates.CrateKey()
        store[kname].name = kname
        if desc != None:
            store[kname].desc = desc
        if 1 != store[kname].load(kval):
            raise RuntimeError, "Unable to set key %s to %g" % (name, kval)
        if 1 != crates.add_key(cr, store[kname]):
            raise RuntimeError, "Unable to add key %s to Crates." % kname

    # Column data, and then keywords.
    # Note that we do not write out any iteration numbered 0 as this is
    # assumed to be the "best-fit" position.
    #
    idx = draws["iteration"] > 0
    for cname in ["iteration", "accept", "statistic", "alphas"]:
        create_col(cname, draws[cname][idx])
    for cname in draws["parnames"]:
        create_col(cname, draws[cname][idx])

    if params == None:
        create_key("statmin", draws["statistic"][0],
                   "statistic at best-fit location")
        for k in draws["parnames"]:
            create_key("P_%s" % k, draws[k][0], "best-fit value")
    else:
        create_key("statmin", params["statval"],
                   "statistic at best-fit location")
        create_key("sigma", params["sigma"],
                   "sigma value for lower/upper limits")
        for (k, v, l, h) in zip(params["parnames"], params["parvals"],
                                params["parmins"], params["parmaxes"]):
            create_key("p_%s" % k, v, "best-fit value")
            create_key("dl_%s" % k, l, "best-fit lower limit")
            create_key("dh_%s" % k, h, "best-fit upper limit")

    # Write out the file
    #
    if format == None:
        outname = outfile
    else:
        outname = outfile + "[opt kernel=%s]" % formats[format.lower()]
    if 1 != crates.write_file(cr, outname):
        raise IOError, "Unable to write data out to '%s'" % outname
    print "Created: %s" % outfile