Esempio 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')
Esempio n. 2
0
# Run fpack on the file
subprocess.Popen(['fpack','-D','-Y',out_coadd_file], close_fds=True).communicate()

# We will use the bounds of the coadd image below...
# GalSim doesn't currently convert easily between BoundsI and BoundsD...
coadd_bounds = galsim.BoundsD(coadd_im.bounds.xmin, coadd_im.bounds.xmax,
                              coadd_im.bounds.ymin, coadd_im.bounds.ymax)
# I add a slight border to make sure we don't draw things near the border twice. It seems 
# like that would be more problematic than missing some objects near the border.
coadd_bounds = coadd_bounds.addBorder(20.)
print 'using coadd_bounds = ',coadd_bounds

# Write the truth file
out_truth_file = os.path.join(out_dir,'end2end-truth.fits')
columns = [ pyfits.Column(name='id', format='J', array=coadd_cat['NUMBER']),
            pyfits.Column(name='flags', format='J', array=flags),
            pyfits.Column(name='is_star', format='I', array=is_star),
            pyfits.Column(name='true_g1', format='D', array=true_g1),
            pyfits.Column(name='true_g2', format='D', array=true_g2),
            pyfits.Column(name='true_hlr', format='D', array=true_hlr),
            pyfits.Column(name='ra', format='D', array=coadd_cat['ALPHAWIN_J2000']),
            pyfits.Column(name='dec', format='D', array=coadd_cat['DELTAWIN_J2000']),
            pyfits.Column(name='mag', format='D', array=coadd_cat['MAG_AUTO']),
            pyfits.Column(name='flux', format='D', array=coadd_cat['FLUX_AUTO']),
            pyfits.Column(name='nexp', format='I', array=nexp),
            pyfits.Column(name='mean_psf_g1', format='D', array=psf_g1),
            pyfits.Column(name='mean_psf_g2', format='D', array=psf_g2),
            pyfits.Column(name='mean_psf_fwhm', format='D', array=psf_fwhm),
            pyfits.Column(name='mean_wcs_g1', format='D', array=wcs_g1),
            pyfits.Column(name='mean_wcs_g2', format='D', array=wcs_g2),
        config['cosmos_images']['output']['psf']['file_name'])
    filename_rgc = os.path.join(
        config['reconvolved_images']['input']['real_catalog']['dir'],
        config['reconvolved_images']['input']['real_catalog']['file_name'])

    # get some additional parameters to put in the catalog
    pixel_scale = config['cosmos_images']['image']['pixel_scale']
    noise_var = config['cosmos_images']['image']['noise']['variance']
    BAND = 'F814W'  # copied from example RGC
    MAG = 20  # no idea if this is right
    WEIGHT = 10  # ditto

    # get the columns of the catalog
    columns = []
    columns.append(
        pyfits.Column(name='IDENT', format='J', array=range(0, n_gals)))
    columns.append(pyfits.Column(name='MAG', format='D', array=[MAG] * n_gals))
    columns.append(
        pyfits.Column(name='BAND', format='5A', array=[BAND] * n_gals))
    columns.append(
        pyfits.Column(name='WEIGHT', format='D', array=[WEIGHT] * n_gals))
    columns.append(
        pyfits.Column(name='GAL_FILENAME',
                      format='23A',
                      array=[filename_gals] * n_gals))
    columns.append(
        pyfits.Column(name='PSF_FILENAME',
                      format='27A',
                      array=[filename_psfs] * n_gals))
    columns.append(
        pyfits.Column(name='GAL_HDU', format='J', array=range(0, n_gals)))
Esempio 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)