コード例 #1
0
ファイル: xspectrum1d.py プロジェクト: Waelthus/linetools
    def write_to_fits(self, outfil, clobber=True, add_wave=False):

        """ Write to a FITS file
        Should generate a separate code to make a Binary FITS table format

        Parameters
        ----------
        outfil: String
          Name of the FITS file
        clobber: bool (True)
          Clobber existing file?
        add_wave: bool (False)
          Force writing of wavelength array
        """
        # TODO
        #  1. Add unit support for wavelength arrays

        from specutils.wcs.specwcs import Spectrum1DPolynomialWCS, Spectrum1DLookupWCS
        from specutils.io import write_fits as sui_wf

        prihdu = sui_wf._make_hdu(self.data)  # Not for binary table format
        prihdu.name = "FLUX"

        hdu = fits.HDUList([prihdu])

        # Type
        if type(self.wcs) is Spectrum1DPolynomialWCS:  # CRVAL1, etc. WCS
            # WCS
            wcs = self.wcs
            wcs.write_fits_header(prihdu.header)
            # Error array?
            if self.sig is not None:
                sighdu = fits.ImageHDU(self.sig)
                sighdu.name = "ERROR"
                hdu.append(sighdu)
                #
            if add_wave:
                wvhdu = fits.ImageHDU(self.dispersion.value)
                wvhdu.name = "WAVELENGTH"
                hdu.append(wvhdu)

        elif type(self.wcs) is Spectrum1DLookupWCS:
            # Wavelengths as an array (without units for now)
            # Add sig, wavelength to HDU
            if self.sig is not None:
                sighdu = fits.ImageHDU(self.sig)
                sighdu.name = "ERROR"
                hdu.append(sighdu)
            wvhdu = fits.ImageHDU(self.dispersion.value)
            wvhdu.name = "WAVELENGTH"
            hdu.append(wvhdu)
        else:
            raise ValueError("write_to_fits: Not ready for this type of spectrum wavelengths")

        if hasattr(self, "co") and self.co is not None:
            cohdu = fits.ImageHDU(self.co)
            cohdu.name = "CONTINUUM"
            hdu.append(cohdu)

        # Deal with header
        if hasattr(self, "head"):
            hdukeys = prihdu.header.keys()
            # Append ones to avoid
            hdukeys = hdukeys + ["BUNIT", "COMMENT", "", "NAXIS2", "HISTORY"]
            for key in self.head.keys():
                # Use new ones
                if key in hdukeys:
                    continue
                # Update unused ones
                try:
                    prihdu.header[key] = self.head[key]
                except ValueError:
                    raise ValueError("l.spectra.utils: Bad header key card")
            # History
            if "HISTORY" in self.head.keys():
                # Strip \n
                tmp = str(self.head["HISTORY"]).replace("\n", " ")
                try:
                    prihdu.header.add_history(str(tmp))
                except ValueError:
                    import pdb

                    pdb.set_trace()

        if self.meta is not None and len(self.meta) > 0:
            d = liu.jsonify_dict(self.meta)
            prihdu.header["METADATA"] = json.dumps(d)

        hdu.writeto(outfil, clobber=clobber)
        print("Wrote spectrum to {:s}".format(outfil))
コード例 #2
0
ファイル: xspectrum1d.py プロジェクト: MSeifert04/linetools
    def write_to_fits(self, outfil, clobber=True, add_wave=False):
        ''' Write to a FITS file.

        Note that this does not generate a binary FITS table format.

        Parameters
        ----------
        outfil : str
          Name of the FITS file
        clobber : bool (True)
          Clobber existing file?
        add_wave : bool (False)
          Force writing of wavelengths as array, instead of using FITS
          header keywords to specify a wcs.
        '''
        # TODO
        #  1. Add unit support for wavelength arrays

        from specutils.wcs.specwcs import Spectrum1DPolynomialWCS, Spectrum1DLookupWCS
        from specutils.io import write_fits as sui_wf
        prihdu = sui_wf._make_hdu(self.data)  # Not for binary table format
        prihdu.name = 'FLUX'

        hdu = fits.HDUList([prihdu])

        # Type
        if type(self.wcs) is Spectrum1DPolynomialWCS:  # CRVAL1, etc. WCS
            # WCS
            wcs = self.wcs
            wcs.write_fits_header(prihdu.header)
            # Error array?
            if self.sig is not None:
                sighdu = fits.ImageHDU(self.sig)
                sighdu.name = 'ERROR'
                hdu.append(sighdu)
                #
            if add_wave:
                wvhdu = fits.ImageHDU(self.wavelength.value)
                wvhdu.name = 'WAVELENGTH'
                hdu.append(wvhdu)

        elif type(self.wcs) is Spectrum1DLookupWCS:
            # Wavelengths as an array (without units for now)
            # Add sig, wavelength to HDU
            if self.sig is not None:
                sighdu = fits.ImageHDU(self.sig)
                sighdu.name = 'ERROR'
                hdu.append(sighdu)
            wvhdu = fits.ImageHDU(self.wavelength.value)
            wvhdu.name = 'WAVELENGTH'
            hdu.append(wvhdu)
        else:
            raise ValueError(
                'write_to_fits: Not ready for this for wavelength WCS')

        if hasattr(self, 'co') and self.co is not None:
            cohdu = fits.ImageHDU(self.co)
            cohdu.name = 'CONTINUUM'
            hdu.append(cohdu)

        # Deal with header
        if hasattr(self, 'head'):
            hdukeys = list(prihdu.header.keys())
            # Append ones to avoid
            hdukeys = hdukeys + ['BUNIT', 'COMMENT', '', 'NAXIS2', 'HISTORY']
            for key in self.head.keys():
                # Use new ones
                if key in hdukeys:
                    continue
                # Update unused ones
                try:
                    prihdu.header[key] = self.head[key]
                except ValueError:
                    raise ValueError('l.spectra.utils: Bad header key card')
            # History
            if 'HISTORY' in self.head.keys():
                # Strip \n
                tmp = str(self.head['HISTORY']).replace('\n', ' ')
                try:
                    prihdu.header.add_history(str(tmp))
                except ValueError:
                    import pdb
                    pdb.set_trace()

        if self.meta is not None and len(self.meta) > 0:
            d = liu.jsonify(self.meta)
            prihdu.header['METADATA'] = json.dumps(d)

        hdu.writeto(outfil, clobber=clobber)
        print('Wrote spectrum to {:s}'.format(outfil))
コード例 #3
0
ファイル: utils.py プロジェクト: mneeleman/xastropy
    def write_to_fits(self, outfil, clobber=True, add_wave=False):
        ''' Write to a FITS file
        Should generate a separate code to make a Binary FITS table format

        Parameters
        ----------
        outfil: String
          Name of the FITS file
        clobber: bool (True)
          Clobber existing file?
        add_wave: bool (False)
          Force writing of wavelength array
        '''
        # TODO
        #  1. Add unit support for wavelength arrays

        from specutils.io import write_fits as sui_wf
        prihdu = sui_wf._make_hdu(self.data)  # Not for binary table format
        prihdu.name = 'FLUX'
        multi = 0 #  Multi-extension?

        # Type
        if type(self.wcs) is Spectrum1DPolynomialWCS:  # CRVAL1, etc. WCS
            # WCS
            wcs = self.wcs
            wcs.write_fits_header(prihdu.header)
            # Error array?
            if self.sig is not None:
                sighdu = fits.ImageHDU(self.sig)
                sighdu.name='ERROR'
                # 
                if add_wave:
                    wvhdu = fits.ImageHDU(self.dispersion.value)
                    wvhdu.name = 'WAVELENGTH'
                    hdu = fits.HDUList([prihdu, sighdu, wvhdu])
                else:
                    hdu = fits.HDUList([prihdu, sighdu])
                multi=1
            else:
                hdu = prihdu

        elif type(self.wcs) is Spectrum1DLookupWCS: # Wavelengths as an array (without units for now)
            # Add sig, wavelength to HDU
            sighdu = fits.ImageHDU(self.sig)
            sighdu.name='ERROR'
            wvhdu = fits.ImageHDU(self.dispersion.value)
            wvhdu.name = 'WAVELENGTH'
            hdu = fits.HDUList([prihdu, sighdu, wvhdu])
            multi=1
        else:
            raise ValueError('write_to_fits: Not ready for this type of spectrum wavelengths')

        # Deal with header
        if hasattr(self,'head'):
            hdukeys = prihdu.header.keys()
            # Append ones to avoid
            hdukeys = hdukeys +ZZ ['BUNIT','COMMENT','', 'NAXIS2', 'HISTORY']
            for key in self.head.keys():
                # Use new ones
                if key in hdukeys:
                    continue
                # Update unused ones
                try:
                    prihdu.header[key] = self.head[key]
                except ValueError:
                    xdb.set_trace()
            # History
            if 'HISTORY' in self.head.keys():
                prihdu.header.add_history(str(self.head['HISTORY']))

        # Write
        hdu.writeto(outfil, clobber=clobber)
        print('Wrote spectrum to {:s}'.format(outfil))
コード例 #4
0
ファイル: utils.py プロジェクト: profxj/old_xastropy
    def write_to_fits(self, outfil, clobber=True, add_wave=False):
        ''' Write to a FITS file
        Should generate a separate code to make a Binary FITS table format

        Parameters
        ----------
        outfil: String
          Name of the FITS file
        clobber: bool (True)
          Clobber existing file?
        add_wave: bool (False)
          Force writing of wavelength array
        '''
        # TODO
        #  1. Add unit support for wavelength arrays

        from specutils.io import write_fits as sui_wf
        prihdu = sui_wf._make_hdu(self.data)  # Not for binary table format
        prihdu.name = 'FLUX'
        multi = 0  #  Multi-extension?

        # Type
        if type(self.wcs) is Spectrum1DPolynomialWCS:  # CRVAL1, etc. WCS
            # WCS
            wcs = self.wcs
            wcs.write_fits_header(prihdu.header)
            # Error array?
            if self.sig is not None:
                sighdu = fits.ImageHDU(self.sig)
                sighdu.name = 'ERROR'
                #
                if add_wave:
                    wvhdu = fits.ImageHDU(self.dispersion.value)
                    wvhdu.name = 'WAVELENGTH'
                    hdu = fits.HDUList([prihdu, sighdu, wvhdu])
                else:
                    hdu = fits.HDUList([prihdu, sighdu])
                multi = 1
            else:
                hdu = prihdu

        elif type(
                self.wcs
        ) is Spectrum1DLookupWCS:  # Wavelengths as an array (without units for now)
            # Add sig, wavelength to HDU
            sighdu = fits.ImageHDU(self.sig)
            sighdu.name = 'ERROR'
            wvhdu = fits.ImageHDU(self.dispersion.value)
            wvhdu.name = 'WAVELENGTH'
            hdu = fits.HDUList([prihdu, sighdu, wvhdu])
            multi = 1
        else:
            raise ValueError(
                'write_to_fits: Not ready for this type of spectrum wavelengths'
            )

        # Deal with header
        if hasattr(self, 'head'):
            hdukeys = prihdu.header.keys()
            # Append ones to avoid
            hdukeys = hdukeys + ZZ['BUNIT', 'COMMENT', '', 'NAXIS2', 'HISTORY']
            for key in self.head.keys():
                # Use new ones
                if key in hdukeys:
                    continue
                # Update unused ones
                try:
                    prihdu.header[key] = self.head[key]
                except ValueError:
                    xdb.set_trace()
            # History
            if 'HISTORY' in self.head.keys():
                prihdu.header.add_history(str(self.head['HISTORY']))

        # Write
        hdu.writeto(outfil, clobber=clobber)
        print('Wrote spectrum to {:s}'.format(outfil))