Ejemplo n.º 1
0
 def write(self, outfile, clobber=True):
     from galsim._pyfits import pyfits
     output = pyfits.HDUList()
     output.append(pyfits.PrimaryHDU())
     for i, screen in enumerate(self.screens):
         output.append(pyfits.ImageHDU(np.array(screen)))
         output[-1].name = "Layer %i" % i
     output.writeto(outfile, clobber=clobber)
Ejemplo n.º 2
0
def WriteMEDS(obj_list, file_name, clobber=True):
    """
    Writes a MEDS file from a list of MultiExposureObjects.

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

    from galsim._pyfits import pyfits

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

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

    # initialise the image vector index
    n_vec = 0
    psf_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 = np.ones(MAX_NCUTOUTS) * EMPTY_START_INDEX
        psf_start_rows = np.ones(MAX_NCUTOUTS) * EMPTY_START_INDEX
        dudrow = np.ones(MAX_NCUTOUTS) * EMPTY_JAC_diag
        dudcol = np.ones(MAX_NCUTOUTS) * EMPTY_JAC_offdiag
        dvdrow = np.ones(MAX_NCUTOUTS) * EMPTY_JAC_offdiag
        dvdcol = np.ones(MAX_NCUTOUTS) * EMPTY_JAC_diag
        row0 = np.ones(MAX_NCUTOUTS) * EMPTY_SHIFT
        col0 = np.ones(MAX_NCUTOUTS) * EMPTY_SHIFT

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

        # append the catalog for this object
        cat['id'].append(obj.id)
        cat['box_size'].append(obj.box_size)
        # TODO: If the config defines a world position, get the right ra, dec here.
        cat['ra'].append(0.)
        cat['dec'].append(0.)
        cat['ncutout'].append(n_cutout)
        cat['psf_box_size'].append(obj.psf_box_size)

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

            # assign the start row to the end of image vector
            start_rows[i] = n_vec
            psf_start_rows[i] = psf_n_vec
            # update n_vec to point to the end of image vector
            n_vec += len(obj.images[i].array.flatten())
            if obj.psf is not None:
                psf_n_vec += len(obj.psf[i].array.flatten())

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

            # append the Jacobian
            # col == x
            # row == y
            dudcol[i] = obj.wcs[i].dudx
            dudrow[i] = obj.wcs[i].dudy
            dvdcol[i] = obj.wcs[i].dvdx
            dvdrow[i] = obj.wcs[i].dvdy
            col0[i] = obj.wcs[i].origin.x
            row0[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)
        cat['psf_start_row'].append(psf_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'] = np.concatenate(vec['image'])
    vec['seg'] = np.concatenate(vec['seg'])
    vec['weight'] = np.concatenate(vec['weight'])
    vec['psf'] = np.concatenate(vec['psf'])

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

    # second hdu is the object_data
    # cf. https://github.com/esheldon/meds/wiki/MEDS-Format
    cols = []
    cols.append(pyfits.Column(name='id', format='K', array=cat['id']))
    cols.append(pyfits.Column(name='number', format='K', array=cat['id']))
    cols.append(pyfits.Column(name='ra', format='D', array=cat['ra']))
    cols.append(pyfits.Column(name='dec', format='D', array=cat['dec']))
    cols.append(
        pyfits.Column(name='box_size', format='K', array=cat['box_size']))
    cols.append(pyfits.Column(name='ncutout', format='K',
                              array=cat['ncutout']))
    cols.append(
        pyfits.Column(name='file_id',
                      format='%dK' % MAX_NCUTOUTS,
                      array=[1] * n_obj))
    cols.append(
        pyfits.Column(name='start_row',
                      format='%dK' % MAX_NCUTOUTS,
                      array=np.array(cat['start_row'])))
    cols.append(
        pyfits.Column(name='orig_row',
                      format='%dD' % MAX_NCUTOUTS,
                      array=[[0] * MAX_NCUTOUTS] * n_obj))
    cols.append(
        pyfits.Column(name='orig_col',
                      format='%dD' % MAX_NCUTOUTS,
                      array=[[0] * MAX_NCUTOUTS] * n_obj))
    cols.append(
        pyfits.Column(name='orig_start_row',
                      format='%dK' % MAX_NCUTOUTS,
                      array=[[0] * MAX_NCUTOUTS] * n_obj))
    cols.append(
        pyfits.Column(name='orig_start_col',
                      format='%dK' % MAX_NCUTOUTS,
                      array=[[0] * MAX_NCUTOUTS] * n_obj))
    cols.append(
        pyfits.Column(name='cutout_row',
                      format='%dD' % MAX_NCUTOUTS,
                      array=np.array(cat['row0'])))
    cols.append(
        pyfits.Column(name='cutout_col',
                      format='%dD' % MAX_NCUTOUTS,
                      array=np.array(cat['col0'])))
    cols.append(
        pyfits.Column(name='dudrow',
                      format='%dD' % MAX_NCUTOUTS,
                      array=np.array(cat['dudrow'])))
    cols.append(
        pyfits.Column(name='dudcol',
                      format='%dD' % MAX_NCUTOUTS,
                      array=np.array(cat['dudcol'])))
    cols.append(
        pyfits.Column(name='dvdrow',
                      format='%dD' % MAX_NCUTOUTS,
                      array=np.array(cat['dvdrow'])))
    cols.append(
        pyfits.Column(name='dvdcol',
                      format='%dD' % MAX_NCUTOUTS,
                      array=np.array(cat['dvdcol'])))
    cols.append(
        pyfits.Column(name='psf_box_size',
                      format='K',
                      array=cat['psf_box_size']))
    cols.append(
        pyfits.Column(name='psf_start_row',
                      format='%dK' % MAX_NCUTOUTS,
                      array=np.array(cat['psf_start_row'])))

    # Depending on the version of pyfits, one of these should work:
    try:
        object_data = pyfits.BinTableHDU.from_columns(cols)
        object_data.name = 'object_data'
    except:  # pragma: no cover
        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='image_ext', format='I', array=[0]))
    cols.append(
        pyfits.Column(name='weight_path',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='weight_ext', format='I', array=[0]))
    cols.append(
        pyfits.Column(name='seg_path',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='seg_ext', format='I', array=[0]))
    cols.append(
        pyfits.Column(name='bmask_path',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='bmask_ext', format='I', array=[0]))
    cols.append(
        pyfits.Column(name='bkg_path',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='bkg_ext', format='I', array=[0]))
    cols.append(pyfits.Column(name='image_id', format='K', array=[-1]))
    cols.append(pyfits.Column(name='image_flags', format='K', array=[-1]))
    cols.append(pyfits.Column(name='magzp', format='E', array=[30.]))
    cols.append(pyfits.Column(name='scale', format='E', array=[1.]))
    # TODO: Not sure if this is right!
    cols.append(pyfits.Column(name='position_offset', format='D', array=[0.]))
    try:
        image_info = pyfits.BinTableHDU.from_columns(cols)
        image_info.name = 'image_info'
    except:  # pragma: no cover
        image_info = pyfits.new_table(pyfits.ColDefs(cols))
        image_info.update_ext_name('image_info')

    # fourth hdu is metadata
    # default values?
    cols = []
    cols.append(pyfits.Column(name='magzp_ref', format='E', array=[30.]))
    cols.append(
        pyfits.Column(name='DESDATA',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='cat_file',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(
        pyfits.Column(name='coadd_image_id',
                      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='K', array=[9999]))
    cols.append(pyfits.Column(name='coadd_seg_hdu', format='K', array=[9999]))
    cols.append(
        pyfits.Column(name='coadd_srclist',
                      format='A256',
                      array=['generated_by_galsim']))
    cols.append(pyfits.Column(name='coadd_wt_hdu', format='K', array=[9999]))
    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=['-1']))
    cols.append(pyfits.Column(name='medsconf', format='A3', array=['x']))
    cols.append(pyfits.Column(name='min_boxsize', format='A2', array=['-1']))
    cols.append(pyfits.Column(name='se_badpix_hdu', format='K', array=[9999]))
    cols.append(pyfits.Column(name='se_hdu', format='K', array=[9999]))
    cols.append(pyfits.Column(name='se_wt_hdu', format='K', array=[9999]))
    cols.append(pyfits.Column(name='seg_hdu', format='K', array=[9999]))
    cols.append(pyfits.Column(name='psf_hdu', format='K', array=[9999]))
    cols.append(pyfits.Column(name='sky_hdu', format='K', array=[9999]))
    cols.append(pyfits.Column(name='fake_coadd_seg', format='K', array=[9999]))
    try:
        metadata = pyfits.BinTableHDU.from_columns(cols)
        metadata.name = 'metadata'
    except:  # pragma: no cover
        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')
    psf_cutouts = pyfits.ImageHDU(vec['psf'], name='psf')

    # write all
    hdu_list = pyfits.HDUList([
        primary, object_data, image_info, metadata, image_cutouts,
        weight_cutouts, seg_cutouts, psf_cutouts
    ])
    hdu_list.writeto(file_name, clobber=clobber)
Ejemplo n.º 3
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._pyfits 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)