def run(self, method='sherpa', outdir=None): """Run all steps Parameters ---------- method : str {sherpa} Fit method to use outdir : Path, str directory to write results files to """ cwd = Path.cwd() outdir = cwd if outdir is None else make_path(outdir) outdir.mkdir(exist_ok=True) os.chdir(str(outdir)) self.set_default_thresholds() if method == 'hspec': self._run_hspec_fit() elif method == 'sherpa': self._run_sherpa_fit() else: raise ValueError('Undefined fitting method') modelname = self.result.spectral_model self.result.to_yaml('fit_result_{}.yaml'.format(modelname)) self.write_npred() os.chdir(str(cwd))
def read(cls, phafile, rmffile=None): """Read PHA fits file The energy binning is not contained in the PHA standard. Therefore is is inferred from the corresponding RMF EBOUNDS extension. Parameters ---------- phafile : str PHA file with ``SPECTRUM`` extension rmffile : str RMF file with ``EBOUNDS`` extennsion, optional """ phafile = make_path(phafile) spectrum = fits.open(str(phafile))['SPECTRUM'] counts = [val[1] for val in spectrum.data] if rmffile is None: val = spectrum.header['RESPFILE'] if val == '': raise ValueError('RMF file not set in PHA header. ' 'Please provide RMF file for energy binning') parts = phafile.parts[:-1] rmffile = Path.cwd() for part in parts: rmffile = rmffile.joinpath(part) rmffile = rmffile.joinpath(val) rmffile = make_path(rmffile) ebounds = fits.open(str(rmffile))['EBOUNDS'] bins = EnergyBounds.from_ebounds(ebounds) livetime = Quantity(0, 's') return cls(counts, bins, livetime=livetime)
def run(self, method='sherpa', outdir=None): """Run all steps Parameters ---------- method : str {sherpa} Fit method to use outdir : Path, str directory to write results files to """ cwd = Path.cwd() outdir = cwd if outdir is None else make_path(outdir) outdir.mkdir(exist_ok=True) os.chdir(str(outdir)) self.set_default_thresholds() if method == 'hspec': self._run_hspec_fit() elif method == 'sherpa': self._run_sherpa_fit() else: raise ValueError('Undefined fitting method') modelname = self.result.spectral_model self.result.to_yaml('fit_result_{}.yaml'.format(modelname)) self.write_npred() os.chdir(str(cwd))
def stack_groups(self): """Stack observations in each group """ stacked_obs = list() sorted_table = self.obs_table.group_by('GROUP_ID') for group in sorted_table.groups: group_id = group['GROUP_ID'][0] log.info('Stacking observations in group {}'.format(group_id)) log.info('{}'.format([group['OBS_ID']])) temp = SpectrumObservationList.from_observation_table(group) stacked = SpectrumObservation.stack_observation_list(temp) stacked.meta.phafile = 'pha_group{}.fits'.format(group_id) stacked.meta.ogip_dir = Path.cwd() / 'ogip_data_stacked' stacked_obs.append(stacked) self.stacked_observations = SpectrumObservationList(stacked_obs)
def stack_groups(self): """Stack observations in each group """ stacked_obs = list() sorted_table = self.obs_table.group_by('GROUP_ID') for group in sorted_table.groups: group_id = group['GROUP_ID'][0] log.info('Stacking observations in group {}'.format(group_id)) log.info('{}'.format([group['OBS_ID']])) temp = SpectrumObservationList.from_observation_table(group) stacked = SpectrumObservation.stack_observation_list(temp) stacked.meta.phafile = 'pha_group{}.fits'.format(group_id) stacked.meta.ogip_dir = Path.cwd() / 'ogip_data_stacked' stacked_obs.append(stacked) self.stacked_observations = SpectrumObservationList(stacked_obs)