Beispiel #1
0
    def write_fits(self, filename, hdr=None, primhdr=None):
        tt = type(self)
        sky_type = '%s.%s' % (tt.__module__, tt.__name__)
        if hdr is None:
            import fitsio
            hdr = fitsio.FITSHDR()

        if primhdr is None:
            import fitsio
            primhdr = fitsio.FITSHDR()
        hdr.add_record(dict(name='SKY', value=sky_type, comment='Sky class'))

        primhdr.add_record(
            dict(name='SKY', value=sky_type, comment='Sky class'))
        #hdr.add_record(dict(name='SPL_ORD', value=self.order,
        #                    comment='Spline sky order'))
        # this writes all params as header cards
        #self.toFitsHeader(hdr, prefix='SKY_')

        #fits = fitsio.FITS(filename, 'rw')
        #fits.write(None, header=primhdr, clobber=True)
        #fits.write(self.c, header=hdr)
        #fits.close()
        from astrometry.util.fits import fits_table
        T = fits_table()
        T.xgrid = np.atleast_2d(self.xgrid).astype(np.int32)
        T.ygrid = np.atleast_2d(self.ygrid).astype(np.int32)
        T.x0 = np.atleast_1d(self.x0).astype(np.int32)
        T.y0 = np.atleast_1d(self.y0).astype(np.int32)
        gridvals = self.spl(self.xgrid, self.ygrid).T
        T.gridvals = np.array([gridvals]).astype(np.float32)
        T.order = np.atleast_1d(self.order).astype(np.uint8)
        assert (len(T) == 1)
        T.writeto(filename, header=hdr, primheader=primhdr)
Beispiel #2
0
    def writeImageFile(self,
                       im,
                       filepath,
                       visit,
                       addCards=None,
                       comment=None,
                       cmd=None):
        """ Actually write the FITS file. 

        Args
        ----
        im : `numpy.ndarray`
          The image.
        filepath : `str` or `pathlib.Path`
          The full pathname of the file to write.
        visit : `int`
          The PFS visit number
        addCards : sequence of fitsio card dicts
          FITS cards to add.
        comment : `str`
          A comment to put at the start of the headeer.
        cmd : `actorcore.Command`
          Where to dribble info

        Returns
        -------
        filepath : `str`
          the input filepath

        The file is saved with RICE compression.

        """
        self.logger.info('creating fits file: %s', filepath)
        cmd.debug('text="creating fits file %s' % (filepath))

        cards = []
        if comment is not None:
            cards.append(dict(name='comment', value=comment))

        if addCards is not None:
            cards.extend(addCards)
        cards.extend(self.headerCards)

        try:
            hdr = fitsio.FITSHDR(cards)
            fitsFile = fitsio.FITS(str(filepath), 'rw')
            fitsFile.write(None, header=hdr)
            fitsFile[-1].write_checksum()
            imHdr = fitsio.FITSHDR(self.getImageCards(cmd))
            fitsFile.write(im, extname="image", header=imHdr, compress='RICE')
            fitsFile[-1].write_checksum()
            fitsFile.close()
        except Exception as e:
            cmd.warn('text="failed to write fits file %s: %s"' % (filepath, e))
            self.logger.warn('failed to write fits file %s: %s', filepath, e)
            self.logger.warn('hdr : %s', hdr)

        return filepath
Beispiel #3
0
    def getFitsHeader(self, header=None, prefix=''):
        psf = self.getPsf()
        wcs = self.getWcs()
        sky = self.getSky()
        pcal = self.getPhotoCal()

        if header is None:
            import fitsio
            hdr = fitsio.FITSHDR()
        else:
            hdr = header
        tt = type(psf)
        psf_type = '%s.%s' % (tt.__module__, tt.__name__)
        tt = type(wcs)
        wcs_type = '%s.%s' % (tt.__module__, tt.__name__)
        tt = type(sky)
        sky_type = '%s.%s' % (tt.__module__, tt.__name__)
        tt = type(pcal)
        pcal_type = '%s.%s' % (tt.__module__, tt.__name__)
        hdr.add_record(
            dict(name=prefix + 'PSF', value=psf_type, comment='PSF class'))
        hdr.add_record(
            dict(name=prefix + 'WCS', value=wcs_type, comment='WCS class'))
        hdr.add_record(
            dict(name=prefix + 'SKY', value=sky_type, comment='Sky class'))
        hdr.add_record(
            dict(name=prefix + 'PHO',
                 value=pcal_type,
                 comment='PhotoCal class'))
        psf.toFitsHeader(hdr, prefix + 'PSF_')
        wcs.toFitsHeader(hdr, prefix + 'WCS_')
        sky.toFitsHeader(hdr, prefix + 'SKY_')
        pcal.toFitsHeader(hdr, prefix + 'PHO_')
        return hdr
Beispiel #4
0
 def __init__(self, d, detpos, header=None):
     """
     A sky pc is a 3d array with index 0 enumerating 2d principal components of sky.
     """
     self.d = np.array(d)
     self.detpos = detpos
     self.header = fitsio.FITSHDR(header)
Beispiel #5
0
    def _write_hpx_fits(pixlist):
        """from files that touch a pixel, write out objects in each pixel"""
        pixnum, files = pixlist
        # ADM only proceed if some files touch a pixel.
        if len(files) > 0:
            # ADM track if it's our first time through the files loop.
            first = True
            # ADM Read in files that touch a pixel.
            for file in files:
                filename = os.path.join(fitsdir, file)
                objs = fitsio.read(filename)
                # ADM only retain objects in the correct pixel.
                pix = radec2pix(nside, objs["RA"], objs["DEC"])
                if first:
                    done = objs[pix == pixnum]
                    first = False
                else:
                    done = np.hstack([done, objs[pix == pixnum]])
            # ADM construct the name of the output file.
            outfilename = 'healpix-{:05d}.fits'.format(pixnum)
            outfile = os.path.join(hpxdir, outfilename)
            # ADM write out the file.
            hdr = fitsio.FITSHDR()
            hdr['HPXNSIDE'] = nside
            hdr['HPXNEST'] = True
            fitsio.write(outfile, done, extname='URATHPX', header=hdr)

        return
Beispiel #6
0
    def save_pars(self, filename, clobber=False):
        """
        Save the parameters to a fits file.

        Parameters
        ----------
        filename: `str`
           Filename to save to.
        clobber: `bool`, optional
           Clobber any existing file?  Default is False.
        """

        hdr = fitsio.FITSHDR()
        hdr['NCOL'] = self.config.nmag - 1
        hdr['MSTARSUR'] = self.config.mstar_survey
        hdr['MSTARBAN'] = self.config.mstar_band
        hdr['LIMMAG'] = self.config.limmag_catalog
        # Saved with arbitrary cushion that seems to work well
        hdr['ZRANGE0'] = np.clip(self.config.zrange[0] - 0.07, 0.01, None)
        hdr['ZRANGE1'] = self.config.zrange[1] + 0.07
        hdr['ALPHA'] = self.config.calib_lumfunc_alpha
        hdr['ZBINFINE'] = self.config.zredc_binsize_fine
        hdr['ZBINCOAR'] = self.config.zredc_binsize_coarse
        hdr['LOWZMODE'] = 0
        hdr['REF_IND'] = self.config.ref_ind
        # Only save the b values if they're > 0 (that means we have
        # luptitudes).
        if self.config.b[0] > 0.0:
            for j, b in enumerate(self.config.b):
                hdr['BVALUE%d' % (j + 1)] = b

        self.pars.to_fits_file(filename, header=hdr, clobber=clobber)
Beispiel #7
0
    def saveAtm(self, filename, clobber=False):
        """
        Save the atmosphere to a fits file

        parameters
        ----------
        filename: string
            output filename
        clobber: bool, optional
            clobber existing output file.  Default is False
        """

        import fitsio

        hdr = fitsio.FITSHDR()
        hdr['PMB'] = self.pmb
        hdr['PWV'] = self.pwv
        hdr['O3'] = self.o3
        hdr['TAU'] = self.tau
        hdr['LAMNORM'] = self.lambdaNorm
        hdr['ALPHA'] = self.alpha
        hdr['ZENITH'] = self.zenith
        hdr['CO2MX'] = self.co2MX
        hdr['ELEV'] = self.elevation

        fitsio.write(filename, self.atm, header=hdr, clobber=clobber)
Beispiel #8
0
    def _consumeRead(self, path, cmd, header=None):
        #  /home/data/wincharis/H2RG-C17206-ASIC-104/UpTheRamp/20160712210126/H2RG_R01_M01_N01.fits
        dirName, fileName = os.path.split(path)
        cmd.diag('text="checking %s"' % (fileName))
        match = re.match('^H2RG_R0*(\d+)_M0*(\d+)_N0*(\d+)\.fits', fileName)
        if match is None:
            cmd.warn("failed to split up filename: %s" % (file))
            return
        rampN, groupN, readN = [int(m) for m in match.group(1, 2, 3)]
        cmd.diag('text="new read %d %d %d"' % (rampN, groupN, readN))
        if readN == 1:
            if header is not None:
                cmd.diag('text="getting header"')
                subaruHdr = header
            else:
                subaruHdr = pyfits.Header()
            cards = [dict(name='IDLPATH', value=dirName)]
            for c in subaruHdr.cards:
                cards.append(
                    dict(name=c.keyword, value=c.value, comment=c.comment))
            phdu = fitsio.FITSHDR(cards)
            fitsio.write(self.outfile, None, header=phdu, clobber=True)
            cmd.diag('text="new file %s"' % (self.outfile))

        inData, inHdr = fitsio.read(path, header=True)
        stackFile = fitsio.FITS(self.outfile, mode='rw')
        stackFile.write(inData, header=inHdr)
        stackFile[-1].write_checksum()
        stackFile.close()
        cmd.inform('readN=%d,%d,%d,%s' % (rampN, groupN, readN, self.outfile))
Beispiel #9
0
def write_targets(filename,
                  data,
                  indir=None,
                  qso_selection=None,
                  sandboxcuts=False,
                  nside=None):
    """Write a target catalogue.

    Parameters
    ----------
    filename : output target selection file
    data     : numpy structured array of targets to save
    nside: :class:`int`
        If passed, add a column to the targets array popluated
        with HEALPix pixels at resolution nside
    """
    # FIXME: assert data and tsbits schema

    #ADM set up the default logger
    from lvmutil.log import get_logger
    log = get_logger()

    #ADM use RELEASE to determine the release string for the input targets
    if len(data) == 0:
        #ADM if there are no targets, then we don't know the Data Release
        drstring = 'unknowndr'
    else:
        drint = np.max(data['RELEASE'] // 1000)
        drstring = 'dr' + str(drint)

    #- Create header to include versions, etc.
    hdr = fitsio.FITSHDR()
    depend.setdep(hdr, 'lvmtarget', lvmtarget_version)
    depend.setdep(hdr, 'lvmtarget-git', gitversion())
    depend.setdep(hdr, 'sandboxcuts', sandboxcuts)
    depend.setdep(hdr, 'photcat', drstring)

    if indir is not None:
        depend.setdep(hdr, 'tractor-files', indir)

    if qso_selection is None:
        log.warning('qso_selection method not specified for output file')
        depend.setdep(hdr, 'qso-selection', 'unknown')
    else:
        depend.setdep(hdr, 'qso-selection', qso_selection)

    #ADM add HEALPix column, if requested by input
    if nside is not None:
        theta, phi = np.radians(90 - data["DEC"]), np.radians(data["RA"])
        hppix = hp.ang2pix(nside, theta, phi, nest=True)
        data = rfn.append_fields(data, 'HPXPIXEL', hppix, usemask=False)
        hdr['HPXNSIDE'] = nside
        hdr['HPXNEST'] = True

    #ADM add PHOTSYS column, mapped from RELEASE
    photsys = release_to_photsys(data["RELEASE"])
    data = rfn.append_fields(data, 'PHOTSYS', photsys, usemask=False)

    fitsio.write(filename, data, extname='TARGETS', header=hdr, clobber=True)
Beispiel #10
0
 def load_templates(self):
     """ Load in all of the header templates """
     self.header = fitsio.FITSHDR()
     for hdrname in self.hdrlist:
         hdrfile = os.path.join(HEADERSERVICE_DIR,'etc','SciCamera',"%s.header" % hdrname)
         # We read and append at the same time, but we might want
         # to de-couple these two actions, if we want to keept the
         # sections separated so they can be streamed independently
         self.header = fitsio.read_scamp_head(hdrfile,header=self.header)
Beispiel #11
0
def sim_stuck_sky(dirnm, hw, tiles):
    from desiutil.brick import Bricks
    bricks = Bricks(bricksize=1)
    tab = bricks.to_table()
    skybricks_dir = os.path.join(dirnm, 'skybricks')
    os.makedirs(skybricks_dir)
    skyfn = os.path.join(skybricks_dir, 'skybricks-exist.fits')
    tab.write(skyfn)

    # create bricks touched by 'tiles'.
    rad = hw.focalplane_radius_deg

    def radec2xyz(r, d):
        r = np.deg2rad(r)
        d = np.deg2rad(d)
        x = np.cos(r) * np.cos(d)
        y = np.sin(r) * np.cos(d)
        z = np.sin(d)
        return np.vstack((x, y, z)).T

    skyxyz = radec2xyz(tab['RA'], tab['DEC'])
    ns = skyxyz.shape[0]
    tilexyz = radec2xyz(tiles.ra, tiles.dec)
    nt = tilexyz.shape[0]
    maxr2 = (np.deg2rad(rad + np.sqrt(2) / 2.))**2

    skybricks = set()
    for i in range(nt):
        r2 = np.sum((tilexyz[i, :] - skyxyz)**2, axis=1)
        J = np.flatnonzero(r2 < maxr2)
        skybricks.update(J)
    for i in skybricks:
        print('Writing fake SKYBRICK', tab['BRICKNAME'][i])
        # skybrick size in pixels
        sz = 3672
        # fraction of pixels to say are good sky
        goodsky_frac = 0.75
        randsky = (16 *
                   (np.random.uniform(size=(sz, sz)) > goodsky_frac)).astype(
                       np.uint8)
        # Add WCS header
        hdr = fitsio.FITSHDR()
        hdr['CTYPE1'] = 'RA---TAN'
        hdr['CTYPE2'] = 'DEC--TAN'
        hdr['CRVAL1'] = tab['RA'][i]
        hdr['CRVAL2'] = tab['DEC'][i]
        hdr['CRPIX1'] = (sz + 1) / 2.
        hdr['CRPIX2'] = (sz + 1) / 2.
        hdr['CD1_1'] = -1. / 3600.
        hdr['CD1_2'] = 0.
        hdr['CD2_1'] = 0.
        hdr['CD2_2'] = 1. / 3600.
        outfn = os.path.join(skybricks_dir,
                             'sky-%s.fits.gz' % tab['BRICKNAME'][i])
        fitsio.write(outfn, randsky, header=hdr)

    os.environ['SKYBRICKS_DIR'] = skybricks_dir
Beispiel #12
0
def write_gfas(filename, data, indir=None, nside=None, survey="?",
               gaiaepoch=None):
    """Write a catalogue of Guide/Focus/Alignment targets.

    Parameters
    ----------
    filename : :class:`str`
        Output file name.
    data  : :class:`~numpy.ndarray`
        Array of GFAs to write to file.
    indir : :class:`str`, optional, defaults to None.
        Name of input Legacy Survey Data Release directory, write to header
        of output file if passed (and if not None).
    nside: :class:`int`, defaults to None.
        If passed, add a column to the GFAs array popluated with HEALPixels
        at resolution `nside`.
    survey : :class:`str`, optional, defaults to "?"
        Written to output file header as the keyword `SURVEY`.
    gaiaepoch: :class:`float`, defaults to None
        Gaia proper motion reference epoch. If not None, write to header of
        output file. If None, default to an epoch of 2015.5.
    """
    # ADM rename 'TYPE' to 'MORPHTYPE'.
    data = rfn.rename_fields(data, {'TYPE': 'MORPHTYPE'})

    # ADM create header to include versions, etc.
    hdr = fitsio.FITSHDR()
    depend.setdep(hdr, 'desitarget', desitarget_version)
    depend.setdep(hdr, 'desitarget-git', gitversion())

    if indir is not None:
        depend.setdep(hdr, 'input-data-release', indir)
        # ADM note that if 'dr' is not in the indir DR
        # ADM directory structure, garbage will
        # ADM be rewritten gracefully in the header.
        drstring = 'dr'+indir.split('dr')[-1][0]
        depend.setdep(hdr, 'photcat', drstring)

    # ADM add HEALPix column, if requested by input.
    if nside is not None:
        theta, phi = np.radians(90-data["DEC"]), np.radians(data["RA"])
        hppix = hp.ang2pix(nside, theta, phi, nest=True)
        data = rfn.append_fields(data, 'HPXPIXEL', hppix, usemask=False)
        hdr['HPXNSIDE'] = nside
        hdr['HPXNEST'] = True

    # ADM add the type of survey (main, or commissioning "cmx") to the header.
    hdr["SURVEY"] = survey

    # ADM add the Gaia reference epoch, or pass 2015.5 if not included.
    hdr['REFEPOCH'] = {'name': 'REFEPOCH',
                       'value': 2015.5,
                       'comment': "Gaia Proper Motion Reference Epoch"}
    if gaiaepoch is not None:
        hdr['REFEPOCH'] = gaiaepoch

    fitsio.write(filename, data, extname='GFA_TARGETS', header=hdr, clobber=True)
Beispiel #13
0
def writeCatalog(outname, nrows, ncols, dtype='f8'):

    # Names of the columns and dtypes
    dtypes = [("VAR%s" % (k + 1), dtype) for k in range(ncols)]
    data = numpy.zeros(nrows, dtypes)

    for var, vartype in dtypes:
        data[var] = numpy.random.random((nrows))

    header = fitsio.FITSHDR()
    fitsio.write(outname, data, extname='OBJECTS', clobber=True, header=header)
Beispiel #14
0
    def write_fits(self, filename, hdr=None):
        import fitsio

        tt = type(self)
        sky_type = '%s.%s' % (tt.__module__, tt.__name__)
        if hdr is None:
            hdr = fitsio.FITSHDR()
        hdr.add_record(dict(name='SKY', value=sky_type, comment='Sky class'))
        self.toFitsHeader(hdr, prefix='SKY_')

        fitsio.write(filename, None, header=hdr, clobber=True)
Beispiel #15
0
def convert_depthfile_to_healsparse(depthfile,
                                    healsparsefile,
                                    nsideCoverage,
                                    clobber=False):
    """
    Convert an old depthfile to a new healsparsefile

    Parameters
    ----------
    depthfile: `str`
       Input depth file
    healsparsefile: `str`
       Output healsparse file
    nsideCoverage: `int`
       Nside for sparse coverage map
    clobber: `bool`, optional
       Clobber existing healsparse file?  Default is False.
    """

    old_depth, old_hdr = fitsio.read(depthfile, ext=1, header=True, lower=True)

    nside = old_hdr['nside']

    # Need to remove the HPIX from the dtype

    dtype_new = []
    names = []
    for d in old_depth.dtype.descr:
        if d[0] != 'hpix':
            dtype_new.append(d)
            names.append(d[0])

    sparseMap = healsparse.HealSparseMap.makeEmpty(nsideCoverage,
                                                   nside,
                                                   dtype_new,
                                                   primary='fracgood')

    old_depth_sub = np.zeros(old_depth.size, dtype=dtype_new)
    for name in names:
        old_depth_sub[name] = old_depth[name]

    sparseMap.updateValues(old_depth['hpix'],
                           old_depth_sub,
                           nest=old_hdr['nest'])

    hdr = fitsio.FITSHDR()
    hdr['NSIG'] = old_hdr['NSIG']
    hdr['ZP'] = old_hdr['ZP']
    hdr['NBAND'] = old_hdr['NBAND']
    hdr['W'] = old_hdr['W']
    hdr['EFF'] = old_hdr['EFF']

    sparseMap.write(healsparsefile, clobber=clobber, header=hdr)
Beispiel #16
0
def copy_header_with_wcs(source_header, wcs):
    import fitsio
    hdr = fitsio.FITSHDR()
    if source_header is not None:
        for r in source_header.records():
            hdr.add_record(r)
    # Plug the WCS header cards into these images
    wcs.add_to_header(hdr)
    hdr.add_record(dict(name='EQUINOX', value=2000., comment='WCS epoch'))
    hdr.delete('IMAGEW')
    hdr.delete('IMAGEH')
    return hdr
Beispiel #17
0
def get_version_header(program_name, decals_dir, git_version=None):
    '''
    Creates a fitsio header describing a DECaLS data product.
    '''
    import datetime

    if program_name is None:
        import sys
        program_name = sys.argv[0]

    if git_version is None:
        git_version = get_git_version()
        #print('Version:', git_version)

    hdr = fitsio.FITSHDR()

    for s in [
        'Data product of the DECam Legacy Survey (DECaLS)',
        'Full documentation at http://legacysurvey.org',
        ]:
        hdr.add_record(dict(name='COMMENT', value=s, comment=s))
    hdr.add_record(dict(name='LEGPIPEV', value=git_version,
                        comment='legacypipe git version'))
    hdr.add_record(dict(name='DECALSV', value=decals_dir,
                        comment='DECaLS version'))
    hdr.add_record(dict(name='DECALSDR', value='DR2',
                        comment='DECaLS release name'))
    decalsdir_ver = get_git_version(decals_dir)
    hdr.add_record(dict(name='DECALSDV', value=decalsdir_ver,
                        comment='legacypipe-dir git version'))
    hdr.add_record(dict(name='DECALSDT', value=datetime.datetime.now().isoformat(),
                        comment='%s run time' % program_name))
    hdr.add_record(dict(name='SURVEY', value='DECaLS',
                        comment='DECam Legacy Survey'))

    # Requested by NOAO
    hdr.add_record(dict(name='SURVEYID', value='DECam Legacy Survey (DECaLS)',
                        comment='Survey name'))
    hdr.add_record(dict(name='DRVERSIO', value='DR2',
                        comment='Survey data release number'))
    hdr.add_record(dict(name='OBSTYPE', value='object',
                        comment='Observation type'))
    hdr.add_record(dict(name='PROCTYPE', value='tile',
                        comment='Processing type'))
    
    import socket
    hdr.add_record(dict(name='HOSTNAME', value=socket.gethostname(),
                        comment='Machine where runbrick.py was run'))
    hdr.add_record(dict(name='HOSTFQDN', value=socket.getfqdn(),
                        comment='Machine where runbrick.py was run'))
    hdr.add_record(dict(name='NERSC', value=os.environ.get('NERSC_HOST', 'none'),
                        comment='NERSC machine where runbrick.py was run'))
    return hdr
Beispiel #18
0
def write_partial_map(filename,
                      data,
                      nside,
                      coord=None,
                      nest=False,
                      header=None,
                      dtype=None,
                      **kwargs):
    """
    Partial HEALPix maps are used to efficiently store maps of the sky by only
    writing out the pixels that contain data.

    Three-dimensional data can be saved by supplying a distance modulus array
    which is stored in a separate extension.

    Parameters:
    -----------
    filename : output file name
    data     : dictionary or recarray of data to write (must contain 'PIXEL')
    nside    : healpix nside of data
    coord    : 'G'alactic, 'C'elestial, 'E'cliptic
    ordering : 'RING' or 'NEST'
    kwargs   : Passed to fitsio.write

    Returns:
    --------
    None
    """
    import fitsio

    # ADW: Do we want to make everything uppercase?

    if isinstance(data, dict):
        names = list(data.keys())
    else:
        names = data.dtype.names

    if 'PIXEL' not in names:
        msg = "'PIXEL' column not found."
        raise ValueError(msg)

    hdr = header_odict(nside=nside, coord=coord, nest=nest)
    fitshdr = fitsio.FITSHDR(list(hdr.values()))
    if header is not None:
        for k, v in header.items():
            fitshdr.add_record({'name': k, 'value': v})
    # ADW: Should this be a debug?
    logger.info("Writing %s..." % filename)
    fitsio.write(filename,
                 data,
                 extname='PIX_DATA',
                 header=fitshdr,
                 clobber=True)
Beispiel #19
0
    def make_zred_table(self, indices_and_filenames):
        """
        Make a zred table from a list of indices and filenames

        Saves to self.config.zredfile.

        Parameters
        ----------
        indices_and_filenames: `list`
           List of (index, outfile) tuples describing zred files.
        """

        # figure out longest filename
        maxlen = 0
        for index, filename in indices_and_filenames:
            if len(os.path.basename(filename)) > maxlen:
                maxlen = len(os.path.basename(filename))

        gal_dtype = self.galtable.dtype

        zred_dtype = []
        for dt in gal_dtype.descr:
            if dt[0].lower() == 'filenames':
                dt = ('filenames', 'S%d' % (maxlen + 1), dt[2])
            zred_dtype.append(dt)

        zredtable = Entry(np.zeros(1, dtype=zred_dtype))
        for name in gal_dtype.names:
            zredtable._ndarray[name] = self.galtable._ndarray[name]

        zredtable.filenames = ''
        zredtable.ngals = 0

        for index, filename in indices_and_filenames:
            # Make sure file exists
            if not os.path.isfile(filename):
                raise ValueError("Could not find zredfile: %s" % (filename))
            # check size of file
            hdr = fitsio.read_header(filename, ext=1)
            if hdr['NAXIS2'] != self.galtable.ngals[index]:
                raise ValueError("Length mismatch for zredfile: %s" %
                                 (filename))

            zredtable.filenames[index] = os.path.basename(filename)
            zredtable.ngals[index] = self.galtable.ngals[index]

        hdr = fitsio.FITSHDR()
        hdr['PIXELS'] = 1

        zredtable.to_fits_file(self.config.zredfile, header=hdr, clobber=True)
Beispiel #20
0
 def getStandardFitsHeader(self, header=None):
     if header is None:
         import fitsio
         hdr = fitsio.FITSHDR()
     else:
         hdr = header
     psf = self.getPsf()
     wcs = self.getWcs()
     sky = self.getSky()
     pcal = self.getPhotoCal()
     psf.toStandardFitsHeader(hdr)
     wcs.toStandardFitsHeader(hdr)
     sky.toStandardFitsHeader(hdr)
     pcal.toStandardFitsHeader(hdr)
     return hdr
Beispiel #21
0
 def finish(self, survey, brickname, version_header):
     from legacypipe.survey import imsave_jpeg
     for band, co, n, com, coiv in zip([1, 2, 3, 4], self.unwise_co,
                                       self.unwise_con, self.unwise_com,
                                       self.unwise_coiv):
         hdr = fitsio.FITSHDR()
         for r in version_header.records():
             hdr.add_record(r)
         hdr.add_record(dict(name='TELESCOP', value='WISE'))
         hdr.add_record(
             dict(name='FILTER', value='W%i' % band, comment='WISE band'))
         self.unwise_wcs.add_to_header(hdr)
         hdr.delete('IMAGEW')
         hdr.delete('IMAGEH')
         hdr.add_record(dict(name='EQUINOX', value=2000.))
         hdr.add_record(
             dict(name='MAGZERO', value=22.5,
                  comment='Magnitude zeropoint'))
         hdr.add_record(
             dict(name='MAGSYS',
                  value='Vega',
                  comment='This WISE image is in Vega fluxes'))
         co /= np.maximum(n, 1)
         com /= np.maximum(n, 1)
         with survey.write_output('image',
                                  brick=brickname,
                                  band='W%i' % band,
                                  shape=co.shape) as out:
             out.fits.write(co, header=hdr)
         with survey.write_output('model',
                                  brick=brickname,
                                  band='W%i' % band,
                                  shape=com.shape) as out:
             out.fits.write(com, header=hdr)
         with survey.write_output('invvar',
                                  brick=brickname,
                                  band='W%i' % band,
                                  shape=co.shape) as out:
             out.fits.write(coiv, header=hdr)
     # W1/W2 color jpeg
     rgb = _unwise_to_rgb(self.unwise_co[:2])
     with survey.write_output('wise-jpeg', brick=brickname) as out:
         imsave_jpeg(out.fn, rgb, origin='lower')
         info('Wrote', out.fn)
     rgb = _unwise_to_rgb(self.unwise_com[:2])
     with survey.write_output('wisemodel-jpeg', brick=brickname) as out:
         imsave_jpeg(out.fn, rgb, origin='lower')
         info('Wrote', out.fn)
Beispiel #22
0
def read_head_template(fname, header=None):
    """
    Function to read in the templates used for the HeaderService.
    This function is based on fitsio.read_scamp_head() and has been
    modified to treat comments (when the KEYWORD field is blank) according
    to the definition of Pence et al. 2010 (section 4.4.2.4)

    read a template header file as a fits header FITSHDR object
    parameters
    ----------
    fname: string
        The path to the header file
    header: FITSHDR, optional
        Optionally combine the header with the input one. The input can
        be any object convertable to a FITSHDR object
    returns
    -------
    header: FITSHDR
        A fits header object of type FITSHDR
    """
    with open(fname) as fobj:
        lines = fobj.readlines()

    lines = [line.rstrip() for line in lines if line[0:3] != 'END']

    # if header is None an empty FITSHDR is created
    hdr = fitsio.FITSHDR(header)

    for line in lines:
        hdr.add_record(line)

    # Nedd to fix the keyword for HIERARCH, as fitsio removes the
    # HIERARCH string from the keyword. Here we put it back
    index_map = copy.deepcopy(hdr._index_map)
    for keyword in index_map:
        rec = get_record(hdr, keyword)
        try:
            is_hierarch = check_hierarch(rec)
        except Exception:
            is_hierarch = False
        if is_hierarch:
            name_old = rec['name']
            name_new = f"HIERARCH {rec['name']}"
            rec['name'] = name_new
            hdr._index_map[name_new] = hdr._index_map.pop(name_old)

    return hdr
Beispiel #23
0
def read_primary_header(fn):
    '''
    Reads the FITS primary header (HDU 0) from the given filename.
    This is just a faster version of fitsio.read_header(fn).
    '''
    import fitsio
    if fn.endswith('.gz'):
        return fitsio.read_header(fn)

    # Weirdly, this can be MUCH faster than letting fitsio do it...
    hdr = fitsio.FITSHDR()
    foundEnd = False
    ff = open(fn, 'rb')
    h = b''
    while True:
        hnew = ff.read(32768)
        if len(hnew) == 0:
            # EOF
            ff.close()
            raise RuntimeError(
                'Reached end-of-file in "%s" before finding end of FITS header.'
                % fn)
        h = h + hnew
        while True:
            line = h[:80]
            h = h[80:]
            #print('Header line "%s"' % line)
            # HACK -- fitsio apparently can't handle CONTINUE.
            # It also has issues with slightly malformed cards, like
            # KEYWORD  =      / no value
            if line[:8] != b'CONTINUE':
                try:
                    hdr.add_record(line.decode())
                except OSError as err:
                    print('Warning: failed to parse FITS header line: ' +
                          ('"%s"; error "%s"; skipped' %
                           (line.strip(), str(err))))

            if line == (b'END' + b' ' * 77):
                foundEnd = True
                break
            if len(h) < 80:
                break
        if foundEnd:
            break
    ff.close()
    return hdr
Beispiel #24
0
    def read_image_primary_header(self, **kwargs):
        '''
        Reads the FITS primary (HDU 0) header from self.imgfn.

        Returns
        -------
        primary_header : fitsio header
            The FITS header
        '''
        if self.imgfn.endswith('.gz'):
            return fitsio.read_header(self.imgfn)

        # Crazily, this can be MUCH faster than letting fitsio do it...
        hdr = fitsio.FITSHDR()
        foundEnd = False
        ff = open(self.imgfn, 'r')
        h = ''
        while True:
            h = h + ff.read(32768)
            while True:
                line = h[:80]
                h = h[80:]
                #print('Header line "%s"' % line)
                # HACK -- fitsio apparently can't handle CONTINUE.
                # It also has issues with slightly malformed cards, like
                # KEYWORD  =      / no value
                if line[:8] != 'CONTINUE':
                    try:
                        hdr.add_record(line)
                    except:
                        print('Warning: failed to parse FITS header line: ' +
                              ('"%s"; skipped' % line.strip()))

                if line == ('END' + ' ' * 77):
                    foundEnd = True
                    break
                if len(h) < 80:
                    break
            if foundEnd:
                break
        ff.close()
        return hdr
Beispiel #25
0
    def save_pars(self, filename, clobber=False):
        """
        """

        hdr = fitsio.FITSHDR()
        hdr['NCOL'] = self.config.nmag - 1
        hdr['MSTARSUR'] = self.config.mstar_survey
        hdr['MSTARBAN'] = self.config.mstar_band
        hdr['LIMMAG'] = self.config.limmag_catalog
        # Saved with arbitrary cushion that seems to work well
        hdr['ZRANGE0'] = np.clip(self.config.zrange[0] - 0.07, 0.01, None)
        hdr['ZRANGE1'] = self.config.zrange[1] + 0.07
        hdr['ALPHA'] = self.config.calib_lumfunc_alpha
        hdr['ZBINFINE'] = self.config.zredc_binsize_fine
        hdr['ZBINCOAR'] = self.config.zredc_binsize_coarse
        hdr['LOWZMODE'] = 0
        hdr['REF_IND'] = self.config.ref_ind
        for j, b in enumerate(self.config.b):
            hdr['BVALUE%d' % (j + 1)] = b

        self.pars.to_fits_file(filename, header=hdr, clobber=clobber)
Beispiel #26
0
def _get_wcs_header():
    cards = [
        "NAXIS   =                    2 / 2-dimensional binary table",
        "NAXIS1  =                 2048 / length of data axix 1",
        "NAXIS2  =                 4096 / length of data axix 2",
        "CRPIX1  =                6660. / Reference pixel on this axis",
        "CRPIX2  =               -4341. / Reference pixel on this axis",
        "CCDBIN1 =                    1 / Pixel binning, axis 1",
        "CCDBIN2 =                    1 / Pixel binning, axis 2",
        "CUNIT1  = 'deg     '           / Axis unit",
        "CD1_1   =   1.013141083499E-08 / Linear projection matrix",
        "CD1_2   =   7.285974030215E-05 / Linear projection matrix",
        "CUNIT2  = 'deg     '           / Axis unit",
        "CD2_1   =  -7.285598092499E-05 / Linear projection matrix",
        "CD2_2   =   1.572634539309E-08 / Linear projection matrix",
        "CTYPE2  = 'DEC--TPV'           / WCS projection type for this axis",
        "CTYPE1  = 'RA---TPV'           / WCS projection type for this axis",
        "CRVAL1  =       70.12751953531 / World coordinate on this axis",
        "CRVAL2  =      -54.55415863979 / World coordinate on this axis",
    ]
    return fitsio.FITSHDR(cards)
Beispiel #27
0
 def writeto(self, fn):
     from astrometry.util.fits import fits_table
     import fitsio
     T = fits_table()
     T.psf_mask = np.array([self.psfbases])
     hdr = fitsio.FITSHDR()
     hdr['POLNAME1'] = 'X_IMAGE'
     hdr['POLNAME2'] = 'Y_IMAGE'
     hdr['POLGRP1'] = 1
     hdr['POLGRP2'] = 1
     hdr['POLNGRP'] = 1
     # number of terms in polynomial
     ne = (self.degree + 1) * (self.degree + 2) / 2
     hdr['PSFAXIS3'] = ne
     hdr['POLZERO1'] = self.x0
     hdr['POLZERO2'] = self.y0
     hdr['POLSCAL1'] = self.xscale
     hdr['POLSCAL2'] = self.yscale
     hdr['POLDEG1'] = self.degree
     hdr['PSF_SAMP'] = self.sampling
     T.writeto(fn, header=hdr)
Beispiel #28
0
    def __init__(self,
                 U,
                 blocksize=DEFAULT_BLOCKSIZE,
                 mask_value=DEFAULT_MASK_VALUE,
                 invalid=DEFAULT_IGNORE.split(','),
                 header=None,
                 halfS7=True):
        """
        :Parameters:

        `U`: 2d numpy array with shape (number of sky superpixels : number of pcs)
        """
        self.U = np.array(U)
        self.blocksize = blocksize
        self.mask_value = mask_value
        self.invalid = set()
        if invalid is not None:
            for detpos in invalid:
                self.invalid.add(detpos.strip())
        self.header = fitsio.FITSHDR(header)
        self.halfS7 = halfS7
Beispiel #29
0
def parse_head_file(fn):
    import fitsio
    f = open(fn, 'r')
    hdrs = []
    #s = ''
    hdr = None
    for line in f.readlines():
        #print('Read %i chars' % len(line), ': ' + line[:25]+'...')
        line = line.strip()
        #print('Stripped to %i' % len(line))
        #line = line + ' ' * (80 - len(line))
        #assert(len(line) == 80)
        #s += line
        if line.startswith('END'):
            #print('Found END')
            hdr = None
            continue
        if hdr is None:
            hdr = fitsio.FITSHDR()
            hdrs.append(hdr)
            #print('Started header number', len(hdrs))
        hdr.add_record(line)
    return hdrs
Beispiel #30
0
def read_image_header(row, img_file):
    """Read some information from the image header and write into the df row.
    """
    hdu = 0

    # Note: The next line usually works, but fitsio doesn't support CONTINUE lines, which DES
    #       image headers sometimes include.
    #h = fitsio.read_header(img_file, hdu)
    # I don't  care about any of the lines the sometimes use CONITNUE (e.g. OBSERVER), so I
    # just remove them and make the header with the rest of the entries.
    f = fitsio.FITS(img_file)
    header_list = f[hdu].read_header_list()
    header_list = [ d for d in header_list if 'CONTINUE' not in d['name'] ]
    h = fitsio.FITSHDR(header_list)
    try:
        date = h['DATE-OBS']
        date, time = date.strip().split('T',1)

        filter = h['FILTER']
        filter = filter.split()[0]

        sat = h['SATURATE']
        fwhm = h['FWHM']

        ccdnum = int(h['CCDNUM'])
        detpos = h['DETPOS'].strip()

        telra = h['TELRA']
        teldec = h['TELDEC']
        telha = h['HA']
        if galsim.__version__ >= '1.5.1':
            telra = galsim.Angle.from_hms(telra) / galsim.degrees
            teldec = galsim.Angle.from_dms(teldec) / galsim.degrees
            telha = galsim.Angle.from_hms(telha) / galsim.degrees
        else:
            telra = galsim.HMS_Angle(telra) / galsim.degrees
            teldec = galsim.DMS_Angle(teldec) / galsim.degrees
            telha = galsim.HMS_Angle(telha) / galsim.degrees

        airmass = float(h.get('AIRMASS',-999))
        sky = float(h.get('SKYBRITE',-999))
        sigsky = float(h.get('SKYSIGMA',-999))

        tiling = int(h.get('TILING',0))
        hex = int(h.get('HEX',0))

    except Exception as e:
        logger.info("Caught %s",e)
        logger.info("Cannot read header information from %s", img_file)
        raise

    row['date'] = date
    row['time'] = time
    row['sat'] = sat
    row['fits_filter'] = filter
    row['fits_fwhm'] = fwhm
    row['fits_ccdnum'] = ccdnum
    row['telra'] = telra
    row['teldec'] = teldec
    row['telha'] = telha
    row['airmass'] = airmass
    row['sky'] = sky
    row['sigsky'] = sigsky
    row['tiling'] = tiling
    row['hex'] = hex