Exemplo n.º 1
0
def run_corr2(x, y, g1, g2, k):
    from galsim import pyfits
    import os
    # Use fits binary table for faster I/O. (Converting to/from strings is slow.)
    assert x.shape == y.shape
    assert x.shape == g1.shape
    assert x.shape == g2.shape
    assert x.shape == k.shape
    x_col = pyfits.Column(name='x', format='1D', array=x.flatten() )
    y_col = pyfits.Column(name='y', format='1D', array=y.flatten() )
    g1_col = pyfits.Column(name='g1', format='1D', array=g1.flatten() )
    g2_col = pyfits.Column(name='g2', format='1D', array=g2.flatten() )
    k_col = pyfits.Column(name='k', format='1D', array=k.flatten() )
    cols = pyfits.ColDefs([x_col, y_col, g1_col, g2_col, k_col])
    table = pyfits.new_table(cols)
    phdu = pyfits.PrimaryHDU()
    hdus = pyfits.HDUList([phdu,table])
    hdus.writeto('temp.fits',clobber=True)
    subprocess.Popen(['corr2','corr2.params',
                      'e2_file_name=temp.e2', 'k2_file_name=temp.k2',
                      'min_sep=%f'%min_sep,'max_sep=%f'%max_sep,'nbins=%f'%nbins]).wait()
    subprocess.Popen(['corr2','corr2.params',
                      'file_name2=temp.fits', 'ke_file_name=temp.ke',
                      'min_sep=%f'%min_sep,'max_sep=%f'%max_sep,'nbins=%f'%nbins]).wait()
    os.remove('temp.fits')
Exemplo n.º 2
0
a fits file) into images that can be used for the SBProfile tests.
The Maple program is saved in the same directory as moffat_pixel.mw.
And the output that it produces is saved as moffat_pixel.dat.
This program converts that into a fits file called moffat_pixel.fits.
"""

import numpy
from galsim import pyfits
import os

for input_file in ["moffat_pixel.dat", "moffat_pixel_distorted.dat"]:

    output_file = input_file.split('.')[0] + '.fits'
    print input_file, output_file

    nx = 61
    ny = 61

    fin = open(input_file, 'r')
    vals = map(float, fin.readlines())

    array = numpy.array(vals).reshape(nx, ny).transpose()

    hdus = pyfits.HDUList()
    hdu = pyfits.PrimaryHDU(array)
    hdus.append(hdu)

    if os.path.isfile(output_file):
        os.remove(output_file)
    hdus.writeto(output_file)
Exemplo n.º 3
0
noise_sigma = min_flux/1000.
print 'add noise with sigma = ',noise_sigma
noise = galsim.GaussianNoise(ud, sigma = noise_sigma)

# For the coadd image, we just need to add noise and write the file to disk.
coadd_im = images[0]
coadd_im.addNoise(noise)
print 'Added noise to coadd image'
coadd_file = image_path[0]
print 'Original coadd file = ',coadd_file

# We will build a new hdulist for the new file and copy what we need from the old one.
# Also, we write this in uncompressed form and then fpack it to make sure that the 
# final result is funpack-able.
hdu_list = pyfits.open(coadd_file)
new_hdu_list = pyfits.HDUList()
# Copy the primary hdu
#new_hdu_list.append(hdu_list[0])

assert coadd_hdu == 1
coadd_im.write(hdu_list=new_hdu_list)
# copy over the header item SEXMGZPT
new_hdu_list[0].header['SEXMGZPT'] = hdu_list[coadd_hdu].header['SEXMGZPT']

# Next is the weight image
assert coadd_wt_hdu == 2
coadd_wt_im = galsim.fits.read(hdu_list=hdu_list[coadd_wt_hdu], compression='rice')
coadd_wt_im *= (1./noise_sigma**2) / coadd_wt_im.array.mean()
print 'coadd_wt_im.mean = ',coadd_wt_im.array.mean(),' should = ',1./noise_sigma**2
coadd_wt_im.write(hdu_list=new_hdu_list)
Exemplo n.º 4
0
def write_meds(file_name, obj_list, clobber=True):
    """
    @brief Writes the galaxy, weights, segmaps images to a MEDS file.

    Arguments:
    ----------
    @param file_name:    Name of meds file to be written
    @param obj_list:     List of MultiExposureObjects
    @param clobber       Setting `clobber=True` when `file_name` is given will silently overwrite 
                         existing files. (Default `clobber = True`.)
    """

    import numpy
    import sys
    from galsim import pyfits

    # initialise the catalog
    cat = {}
    cat['ncutout'] = []
    cat['box_size'] = []
    cat['start_row'] = []
    cat['id'] = []
    cat['dudrow'] = []
    cat['dudcol'] = []
    cat['dvdrow'] = []
    cat['dvdcol'] = []
    cat['row0'] = []
    cat['col0'] = []

    # initialise the image vectors
    vec = {}
    vec['image'] = []
    vec['seg'] = []
    vec['weight'] = []

    # initialise the image vector index
    n_vec = 0

    # get number of objects
    n_obj = len(obj_list)

    # loop over objects
    for obj in obj_list:

        # initialise the start indices for each image
        start_rows = numpy.ones(MAX_NCUTOUTS) * EMPTY_START_INDEX
        dudrow = numpy.ones(MAX_NCUTOUTS) * EMPTY_JAC_diag
        dudcol = numpy.ones(MAX_NCUTOUTS) * EMPTY_JAC_offdiag
        dvdrow = numpy.ones(MAX_NCUTOUTS) * EMPTY_JAC_offdiag
        dvdcol = numpy.ones(MAX_NCUTOUTS) * EMPTY_JAC_diag
        row0 = numpy.ones(MAX_NCUTOUTS) * EMPTY_SHIFT
        col0 = numpy.ones(MAX_NCUTOUTS) * EMPTY_SHIFT

        # get the number of cutouts (exposures)
        n_cutout = obj.n_cutouts

        # append the catalog for this object
        cat['ncutout'].append(n_cutout)
        cat['box_size'].append(obj.box_size)
        cat['id'].append(obj.id)

        # loop over cutouts
        for i in range(n_cutout):

            # assign the start row to the end of image vector
            start_rows[i] = n_vec
            # update n_vec to point to the end of image vector
            n_vec += len(obj.images[i].array.flatten())

            # append the image vectors
            vec['image'].append(obj.images[i].array.flatten())
            vec['seg'].append(obj.segs[i].array.flatten())
            vec['weight'].append(obj.weights[i].array.flatten())

            # append the Jacobian
            dudrow[i] = obj.wcs[i].dudx
            dudcol[i] = obj.wcs[i].dudy
            dvdrow[i] = obj.wcs[i].dvdx
            dvdcol[i] = obj.wcs[i].dvdy
            row0[i] = obj.wcs[i].origin.x
            col0[i] = obj.wcs[i].origin.y

            # check if we are running out of memory
            if sys.getsizeof(vec) > MAX_MEMORY:
                raise MemoryError(
                    'Running out of memory > %1.0fGB ' % MAX_MEMORY / 1.e9 +
                    '- you can increase the limit by changing MAX_MEMORY')

        # update the start rows fields in the catalog
        cat['start_row'].append(start_rows)

        # add lists of Jacobians
        cat['dudrow'].append(dudrow)
        cat['dudcol'].append(dudcol)
        cat['dvdrow'].append(dvdrow)
        cat['dvdcol'].append(dvdcol)
        cat['row0'].append(row0)
        cat['col0'].append(col0)

    # concatenate list to one big vector
    vec['image'] = numpy.concatenate(vec['image'])
    vec['seg'] = numpy.concatenate(vec['seg'])
    vec['weight'] = numpy.concatenate(vec['weight'])

    # get the primary HDU
    primary = pyfits.PrimaryHDU()

    # second hdu is the object_data
    cols = []
    cols.append(
        pyfits.Column(name='ncutout', format='i4', array=cat['ncutout']))
    cols.append(pyfits.Column(name='id', format='i4', array=cat['id']))
    cols.append(
        pyfits.Column(name='box_size', format='i4', array=cat['box_size']))
    cols.append(pyfits.Column(name='file_id', format='i4', array=[1] * n_obj))
    cols.append(
        pyfits.Column(name='start_row',
                      format='%di4' % MAX_NCUTOUTS,
                      array=numpy.array(cat['start_row'])))
    cols.append(pyfits.Column(name='orig_row', format='f8', array=[1] * n_obj))
    cols.append(pyfits.Column(name='orig_col', format='f8', array=[1] * n_obj))
    cols.append(
        pyfits.Column(name='orig_start_row', format='i4', array=[1] * n_obj))
    cols.append(
        pyfits.Column(name='orig_start_col', format='i4', array=[1] * n_obj))
    cols.append(
        pyfits.Column(name='dudrow',
                      format='%df8' % MAX_NCUTOUTS,
                      array=numpy.array(cat['dudrow'])))
    cols.append(
        pyfits.Column(name='dudcol',
                      format='%df8' % MAX_NCUTOUTS,
                      array=numpy.array(cat['dudcol'])))
    cols.append(
        pyfits.Column(name='dvdrow',
                      format='%df8' % MAX_NCUTOUTS,
                      array=numpy.array(cat['dvdrow'])))
    cols.append(
        pyfits.Column(name='dvdcol',
                      format='%df8' % MAX_NCUTOUTS,
                      array=numpy.array(cat['dvdcol'])))
    cols.append(
        pyfits.Column(name='cutout_row',
                      format='%df8' % MAX_NCUTOUTS,
                      array=numpy.array(cat['row0'])))
    cols.append(
        pyfits.Column(name='cutout_col',
                      format='%df8' % MAX_NCUTOUTS,
                      array=numpy.array(cat['col0'])))

    object_data = pyfits.new_table(pyfits.ColDefs(cols))
    object_data.update_ext_name('object_data')

    # third hdu is image_info
    cols = []
    cols.append(
        pyfits.Column(name='image_path',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='sky_path',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='seg_path',
                      format='A256',
                      array=['generated_by_galsim']))
    image_info = pyfits.new_table(pyfits.ColDefs(cols))
    image_info.update_ext_name('image_info')

    # fourth hdu is metadata
    cols = []
    cols.append(
        pyfits.Column(name='cat_file',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='coadd_file',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='coadd_hdu', format='A1', array=['x']))
    cols.append(pyfits.Column(name='coadd_seg_hdu', format='A1', array=['x']))
    cols.append(
        pyfits.Column(name='coadd_srclist',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='coadd_wt_hdu', format='A1', array=['x']))
    cols.append(
        pyfits.Column(name='coaddcat_file',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='coaddseg_file',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='cutout_file',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='max_boxsize', format='A3', array=['x']))
    cols.append(pyfits.Column(name='medsconf', format='A3', array=['x']))
    cols.append(pyfits.Column(name='min_boxsize', format='A2', array=['x']))
    cols.append(pyfits.Column(name='se_badpix_hdu', format='A1', array=['x']))
    cols.append(pyfits.Column(name='se_hdu', format='A1', array=['x']))
    cols.append(pyfits.Column(name='se_wt_hdu', format='A1', array=['x']))
    cols.append(pyfits.Column(name='seg_hdu', format='A1', array=['x']))
    cols.append(pyfits.Column(name='sky_hdu', format='A1', array=['x']))
    metadata = pyfits.new_table(pyfits.ColDefs(cols))
    metadata.update_ext_name('metadata')

    # rest of HDUs are image vectors
    image_cutouts = pyfits.ImageHDU(vec['image'], name='image_cutouts')
    weight_cutouts = pyfits.ImageHDU(vec['weight'], name='weight_cutouts')
    seg_cutouts = pyfits.ImageHDU(vec['seg'], name='seg_cutouts')

    # write all
    hdu_list = pyfits.HDUList([
        primary, object_data, image_info, metadata, image_cutouts,
        weight_cutouts, seg_cutouts
    ])
    hdu_list.writeto(file_name, clobber=clobber)