Python astropy.io.fits.getheader() Examples

The following are 30 code examples of astropy.io.fits.getheader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module astropy.io.fits , or try the search function .
Example #1
Source File: read_spec.py    From serval with MIT License 6 votes vote down vote up
def write_res(filename, datas, extnames, header='', hdrref=None, clobber=False):
   if not header and hdrref: header = pyfits.getheader(hdrref)
   hdu = pyfits.PrimaryHDU(header=header)
   warnings.resetwarnings() # supress nasty overwrite warning http://pythonhosted.org/pyfits/users_guide/users_misc.html
   warnings.filterwarnings('ignore', category=UserWarning, append=True)
   hdu.writeto(filename, clobber=clobber, output_verify='fix')
   warnings.resetwarnings()
   warnings.filterwarnings('always', category=UserWarning, append=True)

   for i,extname in enumerate(extnames):
     data = datas[extname]
     if isinstance(data, np.ndarray):
        pyfits.append(filename, data)
     else:
        1/0

     pyfits.setval(filename, 'EXTNAME', value=extname, ext=i+1)
   #fitsio.write(filename, flux) 
Example #2
Source File: test_convenience.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_image_extension_update_header(self):
        """
        Test that _makehdu correctly includes the header. For example in the
        fits.update convenience function.
        """
        filename = self.temp('twoextension.fits')

        hdus = [fits.PrimaryHDU(np.zeros((10, 10))),
                fits.ImageHDU(np.zeros((10, 10)))]

        fits.HDUList(hdus).writeto(filename)

        fits.update(filename,
                    np.zeros((10, 10)),
                    header=fits.Header([('WHAT', 100)]),
                    ext=1)
        h_out = fits.getheader(filename, ext=1)
        assert h_out['WHAT'] == 100 
Example #3
Source File: test_convenience.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_fileobj_not_closed(self):
        """
        Tests that file-like objects are not closed after being passed
        to convenience functions.

        Regression test for https://github.com/astropy/astropy/issues/5063
        """

        f = open(self.data('test0.fits'), 'rb')
        _ = fits.getdata(f)
        assert not f.closed

        f.seek(0)
        _ = fits.getheader(f)
        assert not f.closed

        f.close()  # Close it now 
Example #4
Source File: wcs_helpers.py    From Aegean with Academic Free License v3.0 6 votes vote down vote up
def from_file(cls, filename, beam=None, psf_file=None):
        """
        Create a new WCSHelper class from a given fits file.

        Parameters
        ----------
        filename : string
            The file to be read

        beam : :class:`AegeanTools.wcs_helpers.Beam` or None
            The synthesized beam. If the supplied beam is None then one is constructed form the header.

        psf_file : str
            Filename for a psf map

        Returns
        -------
        obj : :class:`AegeanTools.wcs_helpers.WCSHelper`
            A helper object
        """
        header = fits.getheader(filename)
        return cls.from_header(header, beam, psf_file=psf_file) 
Example #5
Source File: test_ticket146.py    From pysynphot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write(self, tmpdir, fname, precision, ans):
        f = str(tmpdir.join(fname))
        self.sp.writefits(f, precision=precision)
        hdr = fits.getheader(f, ext=1)
        assert hdr['tform2'].lower() == ans 
Example #6
Source File: psf_selection.py    From mirage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _load_itm_library(library_file):
    """Load ITM FITS file

    Parameters
    ----------
    library_path : str
        Path pointing to the location of the PSF library

    Returns
    -------
    library : photutils.griddedPSFModel
        Object containing PSF library
    """
    data = fits.getdata(library_file)
    hdr = fits.getheader(library_file)
    if data.shape == (2048, 2048):
        # Normalize the data
        data /= np.sum(data)

        # Add PSF location and oversampling keywords
        hdr['DET_YX0'] = ('(1023, 1023)', "The #0 PSF's (y,x) detector pixel position")
        hdr['OVERSAMP'] = (1, 'Oversampling factor for FFTs in computation')

        # Convert to HDUList and create library
        phdu = fits.PrimaryHDU(data, hdr)
        hdulist = fits.HDUList(phdu)
        library = to_griddedpsfmodel(hdulist)

        return library
    else:
        raise ValueError('Expecting ITM data of size (2048, 2048), not {}'.format(data.shape)) 
Example #7
Source File: test_general.py    From marvin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def wcs(galaxy):
    return WCS(fits.getheader(galaxy.cubepath, 1)) 
Example #8
Source File: plate.py    From marvin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _getPlateFromFile(self):
        ''' Initialize a Plate from a Cube/RSS File'''

        # Load file
        try:
            self._hdr = fits.getheader(self.filename, 1)
            self.plateid = int(self._hdr['PLATEID'])
        except Exception as e:
            raise MarvinError('Could not initialize via filename: {0}'
                              .format(e))
        else:
            self.data_origin = 'file'
            self._makePdict() 
Example #9
Source File: mosaic.py    From k2mosaic with MIT License 5 votes vote down vote up
def add_tpf(self, tpf_filename):
        #print("Adding {}".format(tpf_filename))
        if tpf_filename.startswith("http"):
            tpf_filename = astropy.utils.data.download_file(tpf_filename, cache=True)

        self.template_tpf_header0 = getheader(tpf_filename, 0)
        self.template_tpf_header1 = getheader(tpf_filename, 1)

        tpf = fitsio.FITS(tpf_filename)
        self.add_pixels(tpf)
        tpf.close() 
Example #10
Source File: reconstruction.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def load_interaction_matrix(self):
        """
        Loads the interaction matrix from file

        AO interaction matrices can get very big, so its useful to be able to load it frmo file
        rather than make it new everytime. It is assumed that the iMat is saved as "FITS" in the
        simulation saved directory, with other accompanying FITS files that contain the indices
        of actuators which are "valid". Some DMs also use pre made influence functions, which are
        also loaded here.
        """

        filename = self.sim_config.simName+"/iMat.fits"

        imat_header = fits.getheader(filename)
        imat_data = fits.getdata(filename)

        # Check that imat shape is copatibile with curretn sim
        if imat_data.shape != (self.sim_config.totalActs, self.sim_config.totalWfsData):
            logger.warning(
                "interaction matrix does not match required required size."
            )
            raise IOError("interaction matrix does not match required required size.")

        self.interaction_matrix[:] = imat_data

        # Load valid actuators
        for i in range(self.n_dms):
            valid_acts_filename =  self.sim_config.simName+"/active_acts_dm{}.fits".format(i)
            valid_acts = fits.getdata(valid_acts_filename)
            self.dms[i].valid_actuators = valid_acts

            # DM may also have preloaded influence functions
            try:
                dm_shapes_filename = self.sim_config.simName + "/dmShapes_dm{}.fits".format(i)
                dm_shapes = fits.getdata(dm_shapes_filename)
                self.dms[i].iMatShapes = dm_shapes

            except IOError:
                # Found no DM influence funcs
                logger.info("DM Influence functions not found. If the DM doesn't use them, this is ok. If not, set 'forceNew=True' when making IMat") 
Example #11
Source File: test_header.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_header_extend_exact(self):
        """
        Test that extending an empty header with the contents of an existing
        header can exactly duplicate that header, given strip=False and
        end=True.
        """

        header = fits.getheader(self.data('test0.fits'))
        header2 = fits.Header()
        header2.extend(header, strip=False, end=True)
        assert header == header2 
Example #12
Source File: test_header.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_invalid_characters(self):
        """
        Test header with invalid characters
        """

        # Generate invalid file with non-ASCII character
        h = fits.Header()
        h['FOO'] = 'BAR'
        h['COMMENT'] = 'hello'
        hdul = fits.PrimaryHDU(header=h, data=np.arange(5))
        hdul.writeto(self.temp('test.fits'))

        with open(self.temp('test.fits'), 'rb') as f:
            out = f.read()
        out = out.replace(b'hello', 'héllo'.encode('latin1'))
        out = out.replace(b'BAR', 'BÀR'.encode('latin1'))
        with open(self.temp('test2.fits'), 'wb') as f2:
            f2.write(out)

        with catch_warnings() as w:
            h = fits.getheader(self.temp('test2.fits'))
            assert h['FOO'] == 'B?R'
            assert h['COMMENT'] == 'h?llo'
            assert len(w) == 1
            assert str(w[0].message).startswith(
                "non-ASCII characters are present in the FITS file") 
Example #13
Source File: test_header.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_header_fromstring_bytes(self):
        """
        Test reading a Header from a `bytes` string.

        See https://github.com/astropy/astropy/issues/8706
        """

        with open(self.data('test0.fits'), 'rb') as fobj:
            pri_hdr_from_bytes = fits.Header.fromstring(fobj.read())

        pri_hdr = fits.getheader(self.data('test0.fits'))
        assert pri_hdr['NAXIS'] == pri_hdr_from_bytes['NAXIS']
        assert pri_hdr == pri_hdr_from_bytes
        assert pri_hdr.tostring() == pri_hdr_from_bytes.tostring() 
Example #14
Source File: test_convenience.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_resource_warning(self):
        warnings.simplefilter('always', ResourceWarning)
        with catch_warnings() as w:
            _ = fits.getdata(self.data('test0.fits'))
        assert len(w) == 0

        with catch_warnings() as w:
            _ = fits.getheader(self.data('test0.fits'))
        assert len(w) == 0 
Example #15
Source File: util.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def verifyUpdatewcs(fname):
    """
    Verify the existence of WCSNAME in the file.  If it is not present,
    report this to the user and raise an exception.  Returns True if WCSNAME
    was found in all SCI extensions.
    """
    updated = True
    numsci,extname = count_sci_extensions(fname)
    for n in range(1,numsci+1):
        hdr = fits.getheader(fname, extname=extname, extver=n, memmap=False)
        if 'wcsname' not in hdr:
            updated = False
            break
    return updated 
Example #16
Source File: poller_utils.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_primary_fits_header_as_attr(hap_obj, log_level=logutil.logging.NOTSET):
    """create object attribute containing image primary header info

    Parameters
    ----------
    hap_obj : drizzlepac.haputils.Product.TotalProduct, drizzlepac.haputils.Product.FilterProduct, or
    drizzlepac.haputils.Product.ExposureProduct, depending on input
        object to update

    log_level : int, optional.
        The desired level of verboseness in the log statements displayed on the screen and written to the .log file.

    Returns
    -------
    hap_obj : drizzlepac.haputils.Product.TotalProduct, drizzlepac.haputils.Product.FilterProduct, or
    drizzlepac.haputils.Product.ExposureProduct, depending on input
        updated version of input object
    """
    if os.path.exists(hap_obj.drizzle_filename):
        file_name = hap_obj.drizzle_filename
    else:
        file_name = hap_obj.full_filename

    hap_obj.primary_header = fits.getheader(file_name)
    log.info("added primary header info from file {} to {}".format(file_name,hap_obj))

    return hap_obj 
Example #17
Source File: astrometric_utils.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def find_gsc_offset(image, input_catalog='GSC1', output_catalog='GAIA'):
    """Find the GSC to GAIA offset based on guide star coordinates

    Parameters
    ----------
    image : str
        Filename of image to be processed.

    Returns
    -------
    delta_ra, delta_dec : tuple of floats
        Offset in decimal degrees of image based on correction to guide star
        coordinates relative to GAIA.
    """
    serviceType = "GSCConvert/GSCconvert.aspx"
    spec_str = "TRANSFORM={}-{}&IPPPSSOOT={}"

    if 'rootname' in fits.getheader(image):
        ippssoot = fits.getval(image, 'rootname').upper()
    else:
        ippssoot = fu.buildNewRootname(image).upper()

    spec = spec_str.format(input_catalog, output_catalog, ippssoot)
    serviceUrl = "{}/{}?{}".format(SERVICELOCATION, serviceType, spec)
    rawcat = requests.get(serviceUrl)
    if not rawcat.ok:
        log.info("Problem accessing service with:\n{{}".format(serviceUrl))
        raise ValueError

    delta_ra = delta_dec = None
    tree = BytesIO(rawcat.content)
    for _, element in etree.iterparse(tree):
        if element.tag == 'deltaRA':
            delta_ra = float(element.text)
        elif element.tag == 'deltaDEC':
            delta_dec = float(element.text)

    return delta_ra, delta_dec 
Example #18
Source File: fits_utils.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def get_primary_header(filename) -> Optional[fits.Header]:
    try:
        header = fits.getheader(filename, ext=0)
        for keyword in header:
            if keyword not in FITS_MANDATORY_KEYWORDS:
                return header
        return fits.getheader(filename, ext=1)

    except Exception:
        logger.error("Unable to open fits file: {}".format(logs.format_exception()), extra_tags={'filename': filename})
        return None


# Stop after 4 attempts, and back off exponentially with a minimum wait time of 4 seconds, and a maximum of 10.
# If it fails after 4 attempts, "reraise" the original exception back up to the caller. 
Example #19
Source File: test_wcs_helpers.py    From Aegean with Academic Free License v3.0 5 votes vote down vote up
def test_from_header():
    """Test that we can make a beam from a fitsheader"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    header = fits.getheader(fname)
    helper = WCSHelper.from_header(header)
    if helper.beam is None: raise AssertionError()
    del header['BMAJ'], header['BMIN'], header['BPA']
    # Raise an error when the beam information can't be determined
    try:
        _ = WCSHelper.from_header(header)
    except AssertionError as e:
        pass
    else:
        raise AssertionError("Header with no beam information should thrown an exception.")
    return 
Example #20
Source File: test_fits_image.py    From Aegean with Academic Free License v3.0 5 votes vote down vote up
def test_fix_aips_header():
    """TEst that we can fix an aips generated fits header"""
    header = fits.getheader('tests/test_files/1904-66_SIN.fits')
    # test when this function is not needed
    _ = AegeanTools.wcs_helpers.fix_aips_header(header)

    # test when beam params are not present, but there is no aips history
    del header['BMAJ'], header['BMIN'], header['BPA']
    _ = AegeanTools.wcs_helpers.fix_aips_header(header)

    # test with some aips history
    header['HISTORY'] = 'AIPS   CLEAN BMAJ=  1.2500E-02 BMIN=  1.2500E-02 BPA=   0.00'
    _ = AegeanTools.wcs_helpers.fix_aips_header(header) 
Example #21
Source File: test_fits_image.py    From Aegean with Academic Free License v3.0 5 votes vote down vote up
def test_get_beam():
    """Test that we can recover the beam from the fits header"""
    header = fits.getheader('tests/test_files/1904-66_SIN.fits')
    beam = AegeanTools.wcs_helpers.get_beam(header)
    print(beam)
    if beam is None : raise AssertionError()
    if beam.pa != header['BPA']: raise AssertionError()

    del header['BMAJ'], header['BMIN'], header['BPA']
    beam = AegeanTools.wcs_helpers.get_beam(header)
    if beam is not None : raise AssertionError() 
Example #22
Source File: test_fits_image.py    From Aegean with Academic Free License v3.0 5 votes vote down vote up
def test_get_pixinfo():
    """Test that we can get info from various header styles"""
    header = fits.getheader('tests/test_files/1904-66_SIN.fits')

    area, scale = AegeanTools.wcs_helpers.get_pixinfo(header)
    if not area > 0: raise AssertionError()
    if not len(scale) == 2: raise AssertionError()

    header['CD1_1'] = header['CDELT1']
    del header['CDELT1']
    header['CD2_2'] = header['CDELT2']
    del header['CDELT2']
    area, scale = AegeanTools.wcs_helpers.get_pixinfo(header)
    if not area > 0: raise AssertionError()
    if not len(scale) == 2: raise AssertionError()

    header['CD1_2'] = 0
    header['CD2_1'] = 0
    area, scale = AegeanTools.wcs_helpers.get_pixinfo(header)
    if not area > 0: raise AssertionError()
    if not len(scale) == 2: raise AssertionError()

    header['CD1_2'] = header['CD1_1']
    header['CD2_1'] = header['CD2_2']
    area, scale = AegeanTools.wcs_helpers.get_pixinfo(header)
    if not area == 0: raise AssertionError()
    if not len(scale) == 2: raise AssertionError()

    for f in ['CD1_1', 'CD1_2', 'CD2_2', 'CD2_1']:
        del header[f]
    area, scale = AegeanTools.wcs_helpers.get_pixinfo(header)
    if not area == 0: raise AssertionError()
    if not scale == (0, 0): raise AssertionError() 
Example #23
Source File: wcs_helpers.py    From Aegean with Academic Free License v3.0 5 votes vote down vote up
def psf_wcs(self):
        if self._psf_wcs is None:
            header = fits.getheader(self.psf_file)
            try:
                wcs = WCS(header, naxis=2)
            except:
                wcs = WCS(str(header), naxis=2)
            self._psf_wcs = wcs
        return self._psf_wcs 
Example #24
Source File: bary.py    From serval with MIT License 5 votes vote down vote up
def bary_harps(file, ra=None, dec=None, epoch=2000, pma=0.0, pmd=0.0):
   ''' compute barycentric correction for HARPS spctrum using coordinates from
    input or fits header
    ra - rammss e.g. -142942.94 (for 14:29:42.94)
    dec - demmss e.g. -624046.16 (-62:40:46.16)
    pma - [mas/yr] proper motion in alpha (-3775.75)
    pmd - [mas/yr] proper motion in delta (765.54)
    epoch - epoch of coordinates
   '''
   head = pyfits.getheader(file)
   if ra is None:
      ra = head['HIERARCH ESO TEL TARG ALPHA']
      dec = head['HIERARCH ESO TEL TARG DELTA']
      epoch = head['HIERARCH ESO TEL TARG EPOCH']
      pma = head['HIERARCH ESO TEL TARG PMA']*1000
      pmd = head['HIERARCH ESO TEL TARG PMD']*1000

   exptime = head['EXPTIME'] * 2*head['HIERARCH ESO INS DET1 TMMEAN']
   dateobs = head['DATE-OBS']
   #bjd = head['HIERARCH ESO DRS BERV']
   #berv = head['HIERARCH ESO DRS BJD']

   x = str(ra).split('.')
   rammss = float(x[0][:-4]), float(x[0][-4:-2]), float(x[0][-2:]+'.'+x[1])
   # rammss = divmod(ra,10000); rammss = (rammss[0],) + divmod(rammss[1],100)
   x = str(dec).split('.') 
   demmss = float(x[0][:-4]), float(x[0][-4:-2]), float(x[0][-2:]+'.'+x[1])
   #print ra, dec, pma, pmd
   return bary(dateobs, rammss, demmss, 14, epoch, exptime, pma, pmd) #, bjd, berv 
Example #25
Source File: read_spec.py    From serval with MIT License 5 votes vote down vote up
def write_fits(filename, data, header='', hdrref=None, clobber=True):
   if not header and hdrref: header = pyfits.getheader(hdrref)
   warnings.resetwarnings() # supress nasty overwrite warning http://pythonhosted.org/pyfits/users_guide/users_misc.html
   warnings.filterwarnings('ignore', category=UserWarning, append=True)
   pyfits.writeto(filename, data, header, clobber=clobber, output_verify='fix')
   warnings.resetwarnings()
   warnings.filterwarnings('always', category=UserWarning, append=True) 
Example #26
Source File: read_spec.py    From serval with MIT License 5 votes vote down vote up
def write_template(filename, flux, wave, header=None, hdrref=None, clobber=False):
   if not header and hdrref: header = pyfits.getheader(hdrref)
   hdu = pyfits.PrimaryHDU(header=header)
   warnings.resetwarnings() # supress nasty overwrite warning http://pythonhosted.org/pyfits/users_guide/users_misc.html
   warnings.filterwarnings('ignore', category=UserWarning, append=True)
   hdu.writeto(filename, clobber=clobber, output_verify='fix')
   warnings.resetwarnings()
   warnings.filterwarnings('always', category=UserWarning, append=True)

   if isinstance(flux, np.ndarray):
      pyfits.append(filename, flux)
      pyfits.append(filename, wave)
   else:
      # pad arrays with zero to common size
      maxpix = max(arr.size for arr in flux if isinstance(arr, np.ndarray))
      flux_new = np.zeros((len(flux), maxpix))
      wave_new = np.zeros((len(flux), maxpix))
      for o,arr in enumerate(flux):
          if isinstance(arr, np.ndarray): flux_new[o,:len(arr)] = arr
      for o,arr in enumerate(wave):
          if isinstance(arr, np.ndarray): wave_new[o,:len(arr)] = arr
      pyfits.append(filename, flux_new)
      pyfits.append(filename, wave_new)

   pyfits.setval(filename, 'EXTNAME', value='SPEC', ext=1)
   pyfits.setval(filename, 'EXTNAME', value='WAVE', ext=2)
   #fitsio.write(filename, flux) 
Example #27
Source File: psf.py    From grizli with MIT License 5 votes vote down vote up
def __init__(self, flt_files=DEMO_LIST, info=None, driz_image=DEMO_IMAGE, driz_hdu=None, beams=None):
        """
        Object for making drizzled PSFs

        Parameters
        ----------
        flt_files : list
            List of FLT files that were used to create the drizzled image.

        driz_image : str
            Filename of the drizzled image.

        """
        if info is None:
            if beams is not None:
                info = self._get_wcs_from_beams(beams)
            else:
                if flt_files is None:
                    info = self._get_wcs_from_hdrtab(driz_image)
                else:
                    info = self._get_flt_wcs(flt_files)

        self.flt_keys, self.wcs, self.footprint = info
        self.flt_files = list(np.unique([key[0] for key in self.flt_keys]))

        self.ePSF = utils.EffectivePSF()

        if driz_hdu is None:
            self.driz_image = driz_image
            self.driz_header = pyfits.getheader(driz_image)
            self.driz_wcs = pywcs.WCS(self.driz_header)
            self.driz_pscale = utils.get_wcs_pscale(self.driz_wcs)
        else:
            self.driz_image = driz_image
            self.driz_header = driz_hdu.header
            self.driz_wcs = pywcs.WCS(self.driz_header)
            self.driz_pscale = utils.get_wcs_pscale(self.driz_wcs)

        self.driz_wcs.pscale = self.driz_pscale 
Example #28
Source File: combine.py    From grizli with MIT License 4 votes vote down vote up
def get_shifts(files, ref_pixel=[507, 507]):
    """Compute relative pixel shifts based on header WCS

    Parameters
    ----------
    files : list of exposure filenames

    ref_pixel : [int, int] or [float, float]
        Reference pixel for the computed shifts

    Returns
    -------
    h : `~astropy.io.fits.Header`
        Header of the first exposure modified with the total exposure time
        and filenames of the input files in the combination.

    xshift, yshift : array-like
        Computed pixel shifts

    """
    f0 = pyfits.open(files[0])
    h0 = f0[0].header.copy()
    h0['EXPTIME'] = 0.

    out_wcs = pywcs.WCS(f0[1].header, relax=True)
    out_wcs.pscale = utils.get_wcs_pscale(out_wcs)

    # Offsets
    ra0, de0 = out_wcs.all_pix2world([ref_pixel[0]], [ref_pixel[1]], 0)

    x0 = np.zeros(len(files))
    y0 = np.zeros(len(files))

    for i, file in enumerate(files):
        hx = pyfits.getheader(file, 0)
        h0['EXPTIME'] += hx['EXPTIME']
        h0['FILE{0:04d}'.format(i)] = file, 'Included file #{0:d}'.format(i)

        h = pyfits.getheader(file, 1)
        flt_wcs = pywcs.WCS(h, relax=True)
        x0[i], y0[i] = flt_wcs.all_world2pix(ra0, de0, 0)

    return h0, x0-ref_pixel[0], y0-ref_pixel[1] 
Example #29
Source File: standalone.py    From everest with MIT License 4 votes vote down vote up
def DetrendFITS(fitsfile, raw=False, season=None, clobber=False, **kwargs):
    """
    De-trend a K2 FITS file using :py:class:`everest.detrender.rPLD`.

    :param str fitsfile: The full path to the FITS file
    :param ndarray aperture: A 2D integer array corresponding to the \
           desired photometric aperture (1 = in aperture, 0 = outside \
           aperture). Default is to interactively select an aperture.
    :param kwargs: Any kwargs accepted by :py:class:`everest.detrender.rPLD`.

    :returns: An :py:class:`everest.Everest` instance.

    """
    # Get info
    EPIC = pyfits.getheader(fitsfile, 0)['KEPLERID']
    if season is None:
        season = pyfits.getheader(fitsfile, 0)['CAMPAIGN']
        if season is None or season == "":
            season = 0
    everestfile = os.path.join(
        everest.missions.k2.TargetDirectory(EPIC, season),
        everest.missions.k2.FITSFile(EPIC, season))

    # De-trend?
    if clobber or not os.path.exists(everestfile):

        # Get raw data
        data = GetData(fitsfile, EPIC, season, clobber=clobber, **kwargs)

        # De-trend
        model = everest.rPLD(EPIC,
                             data=data,
                             season=season, debug=True,
                             clobber=clobber, **kwargs)

        # Publish it
        everest.fits.MakeFITS(model)
        shutil.copyfile(os.path.join(model.dir, model.name + '.pdf'),
                        os.path.join(model.dir,
                                     model._mission.DVSFile(model.ID,
                                                            model.season,
                                                            model.cadence)))

    # Return an Everest instance
    return everest.Everest(EPIC, season=season) 
Example #30
Source File: user.py    From everest with MIT License 4 votes vote down vote up
def __init__(self, ID, season=None, mission='k2', quiet=False,
                 clobber=False, cadence='lc', **kwargs):
        '''

        '''

        # Read kwargs
        self.ID = ID
        self.mission = mission
        self.clobber = clobber
        if season is not None:
            self._season = season

        # Initialize preliminary logging
        if not quiet:
            screen_level = logging.DEBUG
        else:
            screen_level = logging.CRITICAL
        InitLog(None, logging.DEBUG, screen_level, False)

        # Check the cadence
        if cadence not in ['lc', 'sc']:
            raise ValueError("Invalid cadence selected.")
        self.cadence = cadence

        # Download the FITS file if necessary
        self.fitsfile = DownloadFile(
            ID, season=season, mission=mission, clobber=clobber,
            cadence=cadence)
        self.model_name = pyfits.getheader(self.fitsfile, 1)['MODEL']
        self._weights = None

        # Check the pipeline version. Do we need to upgrade?
        subversion = pyfits.getheader(self.fitsfile, 1).get('SUBVER', None)
        if subversion is not None:
            if LooseVersion(subversion) > LooseVersion(EVEREST_VERSION):
                raise Exception("Desired light curve was generated with " +
                                "EVEREST version %s, but current version " +
                                "is %s.\n" % (subversion, EVEREST_VERSION) +
                                "Please upgrade EVEREST by running " +
                                "`pip install everest-pipeline --upgrade`.")

        # Load the FITS file
        self.load_fits()