def test_detrend_IO(): #the mock_flc needs ra, dec, mission, and channel only for k2sc detrending! test_detrend(save_k2sc=True, folder='{}/tests/testfiles/'.format(PACKAGEDIR)) flc = fitsopen('{}/tests/testfiles/pony_k2sc_k2_llc_800000000-c05_kepler_v2_lc.fits'.format(PACKAGEDIR)) flc = flc[1].data mockflc = mock_flc() print (flc['TRTIME']-mockflc.flux) assert (flc['TIME'] == mockflc.time).all() assert (flc['CADENCE'] == mockflc.cadenceno).all()
def get_hdulist(fits_reference): """ Open an HDU list from the fits reference. Note that the reference may already be an HDUList """ # If the reference is a string, presume its a file path result = fits_reference if isinstance(fits_reference, str): try: result = fitsopen(fits_reference) except Exception as exc: raise IOError("error opening file (%s): %s: %s" % (fits_reference, exc.__class__.__name__, exc.args[0])) from exc return result
def test_path(self, capsys): os.mkdir(self.temp('sub/')) tmp_b = self.temp('sub/ascii.fits') tmp_g = self.temp('sub/group.fits') tmp_h = self.data('group.fits') with hdulist.fitsopen(tmp_h) as hdu_b: hdu_b.writeto(tmp_g) writeto(tmp_b, np.arange(100).reshape(10, 10)) # one modified file and a directory assert fitsdiff.main(["-q", self.data_dir, tmp_b]) == 1 assert fitsdiff.main(["-q", tmp_b, self.data_dir]) == 1 # two directories tmp_d = self.temp('sub/') assert fitsdiff.main(["-q", self.data_dir, tmp_d]) == 1 assert fitsdiff.main(["-q", tmp_d, self.data_dir]) == 1 with pytest.warns(UserWarning, match=r"Field 'ORBPARM' has a repeat " "count of 0 in its format code"): assert fitsdiff.main(["-q", self.data_dir, self.data_dir]) == 0 # no match tmp_c = self.data('arange.fits') fitsdiff.main([tmp_c, tmp_d]) out, err = capsys.readouterr() assert "'arange.fits' has no match in" in err # globbing with pytest.warns(UserWarning, match=r"Field 'ORBPARM' has a repeat " "count of 0 in its format code"): assert fitsdiff.main( ["-q", self.data_dir + '/*.fits', self.data_dir]) == 0 assert fitsdiff.main(["-q", self.data_dir + '/g*.fits', tmp_d]) == 0 # one file and a directory tmp_f = self.data('tb.fits') assert fitsdiff.main(["-q", tmp_f, self.data_dir]) == 0 assert fitsdiff.main(["-q", self.data_dir, tmp_f]) == 0
def test_path(self, capsys): os.mkdir(self.temp('sub/')) tmp_b = self.temp('sub/ascii.fits') tmp_g = self.temp('sub/group.fits') tmp_h = self.data('group.fits') with hdulist.fitsopen(tmp_h) as hdu_b: hdu_b.writeto(tmp_g) writeto(tmp_b, np.arange(100).reshape(10, 10)) # one modified file and a directory assert fitsdiff.main(["-q", self.data_dir, tmp_b]) == 1 assert fitsdiff.main(["-q", tmp_b, self.data_dir]) == 1 # two directories tmp_d = self.temp('sub/') assert fitsdiff.main(["-q", self.data_dir, tmp_d]) == 1 assert fitsdiff.main(["-q", tmp_d, self.data_dir]) == 1 with pytest.warns(UserWarning, match="Field 'ORBPARM' has a repeat " "count of 0 in its format code"): assert fitsdiff.main(["-q", self.data_dir, self.data_dir]) == 0 # no match tmp_c = self.data('arange.fits') fitsdiff.main([tmp_c, tmp_d]) out, err = capsys.readouterr() assert "'arange.fits' has no match in" in err # globbing with pytest.warns(UserWarning, match="Field 'ORBPARM' has a repeat " "count of 0 in its format code"): assert fitsdiff.main(["-q", self.data_dir+'/*.fits', self.data_dir]) == 0 assert fitsdiff.main(["-q", self.data_dir+'/g*.fits', tmp_d]) == 0 # one file and a directory tmp_f = self.data('tb.fits') assert fitsdiff.main(["-q", tmp_f, self.data_dir]) == 0 assert fitsdiff.main(["-q", self.data_dir, tmp_f]) == 0
def from_fits_file(path): ''' Loads a FlareLightCurve from some fits file. If targetid is not given in the header, function takes the last 9 digits before the first "-" in the filename. In case of a K2 light curve file this would be the EPIC ID. Parameters: ----------- path : str path to light curve fits file Return: ----------- FlareLightCurve ''' hdu = fitsopen(path) dr = hdu[1].data if 'TARGETID' in hdu[1].header: targetid = hdu[1].header['TARGETID'] else: targetid = int(path.split('-')[0][-9:]) keys = { 'time': 'TIME', 'detrended_flux': 'FLUX', 'detrended_flux_err': 'ERROR', 'cadenceno': 'CADENCE', 'flux_trends': 'TRTIME', 'campaign': 'CAMPAIGN', 'centroid_col': 'CENTROID_COL', 'centroid_row': 'CENTROID_ROW', 'time_format': 'TIME_FORMAT', 'time_scale': 'TIME_SCALE', 'ra': 'RA', 'dec': 'DEC', 'channel': 'CHANNEL', 'time_unit': 'TIME_UNIT', 'flux_unit': 'FLUX_UNIT', 'origin': 'ORIGIN', 'pos_corr1': 'X', 'pos_corr2': 'Y', 'quality': 'QUALITY', 'pixel_flux': 'PIXEL_FLUX', 'pixel_flux_err': 'PIXEL_FLUX_ERR', 'quality_bitmask': 'QUALITY_BITMASK', 'pipeline_mask': 'PIPELINE_MASK' } for k, v in keys.items(): if v in hdu[1].header: keys[k] = hdu[1].header[v] elif ((v.lower() in list(dr.names)) | (v in list(dr.names))): keys[k] = dr[v] else: keys[k] = None flc = FlareLightCurve(targetid=targetid, **keys) hdu.close() del dr flc = flc[(np.isfinite(flc.detrended_flux)) & (np.isfinite(flc.detrended_flux_err))] return flc
def from_K2SC_file(path, add_TPF=True, **kwargs): """ Read in a K2SC de-trended light curve and convert it to a ``FlareLightCurve``. Parameters ------------ path : str path to light curve add_TPF : True or bool Default fetches TPF for additional info. Required for K2SC de-trending. kwargs : dict Keyword arguments to pass to `KeplerLightCurveFile.from_archive <https://lightkurve.keplerscience.org/api/lightkurve.lightcurvefile.KeplerLightCurveFile.html#lightkurve.lightcurvefile.KeplerLightCurveFile.from_archive>`_ Returns -------- FlareLightCurve """ if add_TPF == True: hdu = fitsopen(path) dr = hdu[1].data targetid = int(path.split('-')[0][-9:]) tpf_list = search_targetpixelfile(targetid, **kwargs) #raw flux,campaign if len(tpf_list) > 1: LOG.error( 'Target data identifier must be unique. Provide campaign or cadence.' ) return else: ktpf = tpf_list.download() klc = ktpf.to_lightcurve() #Only use those cadences that are present in all TPF, KLC, and K2SC LC: values, counts = np.unique(np.concatenate( (klc.cadenceno, dr['CADENCE'], ktpf.cadenceno)), return_counts=True) cadences = values[np.where( counts == 3 )] #you could check if counts can be 4 or more and throw an exception in that case #note that order of cadences is irrelevant for the following to be right dr = dr[np.isin(dr['CADENCE '], cadences)] klc = klc[np.isin(klc.cadenceno, cadences)] ktpf = ktpf[np.isin(ktpf.cadenceno, cadences)] flc = FlareLightCurve(time=dr['TIME'], flux=klc.flux, detrended_flux=dr['FLUX'], detrended_flux_err=dr['ERROR'], cadenceno=dr['CADENCE'], flux_trends=dr['TRTIME'], targetid=targetid, campaign=klc.campaign, centroid_col=klc.centroid_col, centroid_row=klc.centroid_row, time_format=klc.time_format, time_scale=klc.time_scale, ra=klc.ra, dec=klc.dec, channel=klc.channel, time_unit=u.day, flux_unit=u.electron / u.s, origin='K2SC', pos_corr1=dr['X'], pos_corr2=dr['Y'], quality=klc.quality, pixel_flux=ktpf.flux, pixel_flux_err=ktpf.flux_err, quality_bitmask=ktpf.quality_bitmask, pipeline_mask=ktpf.pipeline_mask) hdu.close() del dr flc = flc[(np.isfinite(flc.detrended_flux)) & (np.isfinite(flc.detrended_flux_err))] return flc elif add_TPF == False: return from_fits_file(path)