Exemple #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')
Exemple #2
0
            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),
            pyfits.Column(name='mean_wcs_scale', format='D', array=wcs_scale),
            pyfits.Column(name='mean_wcs_theta', format='D', array=wcs_theta),
            ]
coldefs = pyfits.ColDefs(columns)
table = pyfits.new_table(coldefs)
table.writeto(out_truth_file, clobber=True)

# Also, it turns out to be useful to have the coadd catalog in this directory as well:
# Update: now we make our own, which means the truth catalog isn't correct anymore (need to 
# match up objects by ra,dec).
#shutil.copy2(coaddcat_file, out_dir)

# Do the final processing on each single epoch image and write them to disk.
for image_num in range(1,nimages):
    im = images[image_num]
    file = image_path[image_num]
    print 'Finalize ',file

    # Get the catalog name.
    cat_file = file.replace('.fits.fz','_cat.fits')
        pyfits.Column(name='GAL_HDU', format='J', array=range(0, n_gals)))
    columns.append(
        pyfits.Column(name='PSF_HDU', format='J', array=range(0, n_gals)))
    columns.append(
        pyfits.Column(name='PIXEL_SCALE',
                      format='D',
                      array=[pixel_scale] * n_gals))
    columns.append(
        pyfits.Column(name='NOISE_MEAN', format='D', array=[0] * n_gals))
    columns.append(
        pyfits.Column(name='NOISE_VARIANCE',
                      format='D',
                      array=[noise_var] * n_gals))

    # create table
    hdu_table = pyfits.new_table(columns)

    # save all catalogs
    hdu_table.writeto(filename_rgc, clobber=True)
    logger.info('saved real galaxy catalog %s' % filename_rgc)

    # if in debug mode, save some plots
    # if config['args'].debug : SavePreviewRGC(config,filename_rgc)


def SavePreviewRGC(config, filename_rgc, n_gals_preview=10):
    """
    Function for eyeballing the contents of the created mock RGC catalogs.
    Arguments
    ---------
    config              config dict 
    # 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)       ))    
    columns.append( pyfits.Column( name='PSF_HDU'       ,format='J'  ,array=range(0,n_gals)       ))    
    columns.append( pyfits.Column( name='PIXEL_SCALE'   ,format='D'  ,array=[pixel_scale] * n_gals))        
    columns.append( pyfits.Column( name='NOISE_MEAN'    ,format='D'  ,array=[0] * n_gals          ))
    columns.append( pyfits.Column( name='NOISE_VARIANCE',format='D'  ,array=[noise_var] * n_gals  ))        

    # create table
    hdu_table = pyfits.new_table(columns)
    
    # save all catalogs
    hdu_table.writeto(filename_rgc,clobber=True)
    logger.info('saved real galaxy catalog %s' % filename_rgc)

    # if in debug mode, save some plots
    # if config['args'].debug : SavePreviewRGC(config,filename_rgc)

def SavePreviewRGC(config,filename_rgc,n_gals_preview=10):
    """
    Function for eyeballing the contents of the created mock RGC catalogs.
    Arguments
    ---------
    config              config dict 
    filename_rgc        filename of the newly created real galaxy catalog fits 
Exemple #5
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='number', format='K', array=range(n_obj) ) )
    cols.append( pyfits.Column(name='ncutout', format='K', array=cat['ncutout'] ) )
    cols.append( pyfits.Column(name='id', format='K', array=cat['id'] ) )
    cols.append( pyfits.Column(name='box_size', format='K', array=cat['box_size'] ) )
    cols.append( pyfits.Column(name='file_id', format='%dK' % MAX_NCUTOUTS , array=numpy.array([[1]]*n_obj)) )
    cols.append( pyfits.Column(name='start_row', format='%dK' % MAX_NCUTOUTS,
                                                    array=numpy.array(cat['start_row'])) )
    cols.append( pyfits.Column(name='orig_row', format='%dD' % MAX_NCUTOUTS, array=numpy.array([[1]]*n_obj) ) )
    cols.append( pyfits.Column(name='orig_col', format='%dD' % MAX_NCUTOUTS, array=numpy.array([[1]]*n_obj) ) )
    cols.append( pyfits.Column(name='orig_start_row', format='%dK' % MAX_NCUTOUTS,
                                                    array=numpy.array([[1]]*n_obj) ) )
    cols.append( pyfits.Column(name='orig_start_col', format='%dK' % MAX_NCUTOUTS,
                                                    array=numpy.array([[1]]*n_obj) ) )
    cols.append( pyfits.Column(name='dudrow', format='%dD'% MAX_NCUTOUTS,
                               array=numpy.array(cat['dudrow']) ) )
    cols.append( pyfits.Column(name='dudcol', format='%dD'% MAX_NCUTOUTS,
                               array=numpy.array(cat['dudcol']) ) )
    cols.append( pyfits.Column(name='dvdrow', format='%dD'% MAX_NCUTOUTS,
                               array=numpy.array(cat['dvdrow']) ) )
    cols.append( pyfits.Column(name='dvdcol', format='%dD'% MAX_NCUTOUTS,
                               array=numpy.array(cat['dvdcol']) ) )
    cols.append( pyfits.Column(name='cutout_row', format='%dD'% MAX_NCUTOUTS,
                               array=numpy.array(cat['row0']) ) )
    cols.append( pyfits.Column(name='cutout_col', format='%dD'% 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)
Exemple #6
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)