Beispiel #1
0
    def toFits(self, fits):
        """Write to a FITS file

        Parameters
        ----------
        fits : `astropy.io.fits.HDUList`
            Opened FITS file.
        """
        from astropy.io.fits import BinTableHDU, Column
        maxLength = max(
            len(ff) for ff in self.fiberMags.keys()) if self.fiberMags else 1
        header = astropyHeaderFromDict(
            {attr.upper(): getattr(self, attr)
             for attr in self._attributes})
        header.update(TargetType.getFitsHeaders())
        hdu = BinTableHDU.from_columns([
            Column("filterName",
                   "%dA" % maxLength,
                   array=list(self.fiberMags.keys())),
            Column("fiberMag",
                   "E",
                   array=np.array(list(self.fiberMags.values()))),
        ],
                                       header=header,
                                       name="TARGET")
        fits.append(hdu)
Beispiel #2
0
def beams_to_bintable(beams):
    """
    Convert a list of beams to a CASA-style BinTableHDU
    """

    c1 = Column(name='BMAJ',
                format='1E',
                array=[bm.major.to(u.arcsec).value for bm in beams],
                unit=u.arcsec.to_string('FITS'))
    c2 = Column(name='BMIN',
                format='1E',
                array=[bm.minor.to(u.arcsec).value for bm in beams],
                unit=u.arcsec.to_string('FITS'))
    c3 = Column(name='BPA',
                format='1E',
                array=[bm.pa.to(u.deg).value for bm in beams],
                unit=u.deg.to_string('FITS'))
    c4 = Column(
        name='CHAN',
        format='1J',
        array=[bm.meta['CHAN'] if 'CHAN' in bm.meta else 0 for bm in beams])
    c5 = Column(
        name='POL',
        format='1J',
        array=[bm.meta['POL'] if 'POL' in bm.meta else 0 for bm in beams])

    bmhdu = BinTableHDU.from_columns([c1, c2, c3, c4, c5])
    bmhdu.header['EXTNAME'] = 'BEAMS'
    bmhdu.header['EXTVER'] = 1
    bmhdu.header['XTENSION'] = 'BINTABLE'
    bmhdu.header['NCHAN'] = len(beams)
    bmhdu.header['NPOL'] = len(set([bm.meta['POL'] for bm in beams]))
    return bmhdu
Beispiel #3
0
def append_fits_entry(base_rec, new_entry):
    '''
    Helper script to append an entry to a FITS_rec object.
    Based on astropy documentation here:
    https://docs.astropy.org/en/stable/io/fits/usage/table.html

    '''
    from astropy.io.fits import BinTableHDU
    
    old_rows = base_rec.shape[0]
    new_rows = old_rows + 1
    new_rec = hdu = BinTableHDU.from_columns(base_rec.columns, nrows=new_rows)
    new_rec.data[old_rows:] = new_entry
    return new_rec.data
Beispiel #4
0
    def _writeImpl(self, fits):
        """Implementation for writing to FITS file

        Parameters
        ----------
        fits : `astropy.io.fits.HDUList`
            List of FITS HDUs. This has a Primary HDU already, the header of
            which may be supplemented with additional keywords.
        """
        from astropy.io.fits import BinTableHDU, Column
        fits.append(BinTableHDU.from_columns([
            Column("wavelength", "D", array=self.wavelength),
            Column("flux", "D", array=self.flux),
            Column("mask", "K", array=self.mask),
        ], header=astropyHeaderFromDict(self.flags.toFitsHeader()), name="FLUXTBL"))
        self.target.toFits(fits)
Beispiel #5
0
    def _writeImpl(self, fits):
        """Implementation for writing to FITS file

        Parameters
        ----------
        fits : `astropy.io.fits.HDUList`
            List of FITS HDUs. This has a Primary HDU already, the header of
            which may be supplemented with additional keywords.
        """
        from astropy.io.fits import BinTableHDU, Column
        fits.append(BinTableHDU.from_columns([
            Column("wavelength", "D", array=self.wavelength),
            Column("flux", "D", array=self.flux),
            Column("mask", "K", array=self.mask),
        ], header=astropyHeaderFromDict(self.flags.toFitsHeader()), name="FLUXTBL"))
        self.target.toFits(fits)
Beispiel #6
0
def beams_to_bintable(beams):
    """
    Convert a list of beams to a CASA-style BinTableHDU
    """

    c1 = Column(name='BMAJ', format='1E', array=[bm.major.to(u.arcsec).value for bm in beams], unit=u.arcsec.to_string('FITS'))
    c2 = Column(name='BMIN', format='1E', array=[bm.minor.to(u.arcsec).value for bm in beams], unit=u.arcsec.to_string('FITS'))
    c3 = Column(name='BPA', format='1E', array=[bm.pa.to(u.deg).value for bm in beams], unit=u.deg.to_string('FITS'))
    c4 = Column(name='CHAN', format='1J', array=[bm.meta['CHAN'] if 'CHAN' in bm.meta else 0 for bm in beams])
    c5 = Column(name='POL', format='1J', array=[bm.meta['POL'] if 'POL' in bm.meta else 0 for bm in beams])

    bmhdu = BinTableHDU.from_columns([c1, c2, c3, c4, c5])
    bmhdu.header['EXTNAME'] = 'BEAMS'
    bmhdu.header['EXTVER'] = 1
    bmhdu.header['XTENSION'] = 'BINTABLE'
    bmhdu.header['NCHAN'] = len(beams)
    bmhdu.header['NPOL'] = len(set([bm.meta['POL'] for bm in beams]))
    return bmhdu
Beispiel #7
0
    def toFits(self, fits):
        """Write to a FITS file

        Parameters
        ----------
        fits : `astropy.io.fits.HDUList`
            Opened FITS file.
        """
        from astropy.io.fits import BinTableHDU, Column
        identityLength = max(len(str(ident)) for ident in self.identity)
        hdu = BinTableHDU.from_columns([
            Column("identity", "%dA" % identityLength, array=self.identity),
            Column("fiberId", "K", array=self.fiberId),
            Column("pfiNominal", "2D", array=self.pfiNominal),
            Column("pfiCenter", "2D", array=self.pfiCenter),
        ],
                                       name="OBSERVATIONS")
        fits.append(hdu)
Beispiel #8
0
    def toFits(self, fits):
        """Write to a FITS file

        Parameters
        ----------
        fits : `astropy.io.fits.HDUList`
            Opened FITS file.
        """
        from astropy.io.fits import BinTableHDU, Column
        header = self.flags.toFitsHeader()
        hdu = BinTableHDU.from_columns([
            Column("wavelength", "E", array=self.wavelength),
            Column("flux", "E", array=self.flux),
            Column("error", "E", array=self.error),
            Column("mask", "K", array=self.mask),
        ],
                                       header=astropyHeaderFromDict(header),
                                       name=self._hduName)
        fits.append(hdu)
Beispiel #9
0
    def toFits(self, fits):
        """Write to a FITS file

        Parameters
        ----------
        fits : `astropy.io.fits.HDUList`
            Opened FITS file.
        """
        armLength = max(len(arm) for arm in self.arm)
        columns = [
            Column("visit", "J", array=self.visit),
            Column("arm", f"{armLength}A", array=self.arm),
            Column("spectrograph", "J", array=self.spectrograph),
            Column("fiberId", "J", array=self.fiberId),
            Column("pfsDesignId", "K", array=self.pfsDesignId),
            Column("pfiNominal", "2E", array=self.pfiNominal),
            Column("pfiCenter", "2E", array=self.pfiCenter),
        ]
        hdu = BinTableHDU.from_columns(columns, name="OBSERVATIONS")
        fits.append(hdu)
Beispiel #10
0
def write_to_fits(output, chi2, sampler, nwalkers, thin, params, jfree,
                  metadata, meta_names, iternum, nwritten,
                  Nobsbins, array, BinTableHDU, Column, ctime, enumerate,
                  isfile, izip, transpose, xrange):
    nexclude = len(chi2)
    lnprior, lnPderived, chi2, lnlike = chi2
    if isfile(output):
        remove(output)
    chain = transpose(sampler.chain, axes=(2,1,0))
    columns = [Column(name=param, format='E', array=data[:iternum].flatten())
               for param, data in izip(params[jfree], chain)]
    columns.append(Column(name='lnprob', format='E',
                          array=sampler.lnprobability.T[:iternum].flatten()))
    if len(meta_names) > 0:
        # save only the last chunk (starting from t),
        # all others are already in metadata.
        # NOTE that this is only implemented for a model with the
        # same format as fiducial()
        for j, blob in izip(xrange(nwritten, iternum),
                            sampler.blobs[nwritten:]):
            data = [transpose([b[i] for b in blob])
                    for i in xrange(len(blob[0])-nexclude)]
            # re-arrange blobs
            if Nobsbins == 1:
                for i in xrange(len(data)):
                    if len(data[i].shape) == 2:
                        data[i] = array([b[i] for b in blob])
            else:
                for i in xrange(len(data)):
                    if len(data[i].shape) == 3:
                        data[i] = transpose([b[i] for b in blob],
                                            axes=(1,0,2))
            # store data
            for k in xrange(len(data)):
                for i in xrange(len(data[k])):
                    metadata[k][i][j*nwalkers:(j+1)*nwalkers] = data[k][i]
            lnPderived[j*nwalkers:(j+1)*nwalkers] = array([b[-4]
                                                           for b in blob])
            lnprior[j*nwalkers:(j+1)*nwalkers] = array([b[-3] for b in blob])
            chi2[j*nwalkers:(j+1)*nwalkers] = array([b[-2] for b in blob])
            lnlike[j*nwalkers:(j+1)*nwalkers] = array([b[-1] for b in blob])
        columns.append(Column(name='lnprior', format='E', array=lnprior))
        columns.append(Column(name='lnPderived', format='E',
                              array=lnPderived))
        columns.append(Column(name='chi2', format='E', array=chi2))
        columns.append(Column(name='lnlike', format='E', array=lnlike))
        # this handles exclude_bins properly
        for name, val in izip(meta_names, metadata):
            for name_i, val_i in izip(name, val):
                try:
                    fmt = '{0}E'.format(val_i.shape[1])
                except IndexError:
                    fmt = 'E'
                columns.append(Column(name=name_i, array=val_i, format=fmt))
        nwritten = iternum * nwalkers
    fitstbl = BinTableHDU.from_columns(columns)
    fitstbl.writeto(output)
    print 'Saved to {0} with {1} samples'.format(output, iternum*nwalkers),
    if thin > 1:
        print '(printing every {0}th sample)'.format(thin),
    print '- {0}'.format(ctime())
    return metadata, nwritten
Beispiel #11
0
 def time_from_columns_bytes(self):
     x = np.repeat(b'a', 2_000_000)
     array = np.array(x, dtype=[('col', 'S1')])
     BinTableHDU.from_columns(array)