def run_analysis(self, argv): """Run this analysis""" args = self._parser.parse_args(argv) exttype = splitext(args.infile)[-1] if exttype in ['.fits', '.npy']: castro_data = CastroData.create_from_sedfile(args.infile) elif exttype in ['.yaml']: castro_data = CastroData.create_from_yamlfile(args.infile) else: raise ValueError("Can not read file type %s for SED" % extype) ylims = [1e-8, 1e-5] if args.summaryfile is not None: bands = fits.open(args.summaryfile) fig, ax = plotBands(castro_data, ylims, bands[1], band_type=args.band_type) plotSED_OnAxis(ax, castro_data) else: fig, ax = plotSED_OnAxis(ax, ylims) if args.outfile: fig.savefig(args.outfile)
def run_analysis(self, argv): """Run this analysis""" args = self._parser.parse_args(argv) channels = [ 'ee', 'mumu', 'tautau', 'bb', 'tt', 'gg', 'ww', 'zz', 'cc', 'uu', 'dd', 'ss' ] norm_type = 'eflux' spec_table = DMSpecTable.create_from_fits(args.spec) profile = load_yaml(args.profile_yaml) j_value = profile.get('j_integ') j_sigma = profile.get('j_sigma', None) if args.jprior is None or args.jprior == 'None' or j_sigma is None or j_sigma == 0.0: j_factor = j_value j_prior_key = 'none' else: j_factor = dict(functype=args.jprior, j_value=j_value, mu=j_value, sigma=j_sigma) j_prior_key = args.jprior sed = CastroData.create_from_sedfile(args.sed_file, norm_type) c_list, t_list, n_list = DMCastroConvertor.convert_sed_to_dm( spec_table, sed, channels, norm_type, j_factor) fits_utils.write_tables_to_fits(args.outfile, t_list, clobber=args.clobber, namelist=n_list)
def run_analysis(self, argv): """Run this analysis""" args = self._parser.parse_args(argv) exttype = splitext(args.infile)[-1] if exttype in ['.fits', '.npy']: castro_data = CastroData.create_from_sedfile(args.infile) elif exttype in ['.yaml']: castro_data = CastroData.create_from_yamlfile(args.infile) else: raise ValueError("Can not read file type %s for SED" % extype) ylims = [1e-8, 1e-5] plot = plotCastro(castro_data, ylims) if args.outfile: plot[0].savefig(args.outfile)
def run_analysis(self, argv): """Run this analysis""" args = self._parser.parse_args(argv) castro_data = CastroData.create_from_sedfile(args.input) ylims = [1e-8, 1e-5] plot = plotCastro(castro_data, ylims) if args.output: plot[0].savefig(args.output) return None return plot
def load_sed_from_txt(sedfile): """Load an SED from a txt file in the format used for online materials associated with dsph papers..""" sed = np.loadtxt(sedfile, unpack=True) emin = np.unique(sed[0]) emax = np.unique(sed[1]) nbin = len(emin) sed = sed.reshape((4, nbin, -1)) ref_spec = ReferenceSpec(emin, emax, np.ones(nbin), np.ones(nbin), np.ones(nbin), np.ones(nbin)) cd = CastroData(sed[2], -1.0 * sed[3], ref_spec, 'eflux') return cd
def convert_sed(spec_table, channels, sed_file, outfile, limitfile, **kwargs): """Convert a single SED to DM space. Parameters ---------- spec_table : `DMSpecTable` Object with all the DM spectra channels : list List of the channels to convert sed_file : str Path to the SED file outfile : str Path to write the output `DMCastroData` object to limitfile : str Path to write the output limits to. Keyword arguments ----------------- norm_type : str Normalization type to use j_factor : dict Dictionary with information about the J-factor d_factor : dict Dictionary with information about the J-factor clobber : bool Flag to overwrite existing files. """ norm_type = kwargs.get('norm_type', 'eflux') j_factor = kwargs.get('j_factor', None) d_factor = kwargs.get('d_factor', None) clobber = kwargs.get('clobber', False) use_chans = ConvertCastro.select_channels(channels, sed_file) exttype = os.path.splitext(sed_file)[-1] if exttype in ['.fits', '.npy']: sed = CastroData.create_from_sedfile(sed_file, norm_type) elif exttype in ['.yaml']: sed = CastroData.create_from_yamlfile(sed_file) else: raise ValueError("Can not read file type %s for SED" % exttype) c_list, t_list, n_list = ConvertCastro.convert_sed_to_dm( spec_table, sed, use_chans, norm_type=norm_type, j_val=j_factor, d_val=d_factor) if is_not_null(outfile): fits_utils.write_tables_to_fits(outfile, t_list, clobber=clobber, namelist=n_list) if is_not_null(limitfile): mass_table = t_list[-1] _, t_list_lim, n_list_lim = ConvertCastro.extract_dm_limits( c_list, use_chans, [0.68, 0.95], mass_table) fits_utils.write_tables_to_fits(limitfile, t_list_lim, clobber=clobber, namelist=n_list_lim)
def compute_limits(sedfile, roster, chan, masses, alpha=0.05, apply_prior=False, outprefix='lims2'): """Generate DM limits from an SED file. Parameters ---------- sedfile : str Path to SED file. """ dm_tab = None names = [] cd_stack = [] lims_out = [] ts_out = [] # Loop over targets in roster for name, target in list(roster.items()): names += [name] if len(roster) > 1: cd = CastroData.create_from_sedfile(sedfile, target=name) else: cd = CastroData.create_from_sedfile(sedfile) jfactor = target.params['j_integ'].value jsigma = target.params['j_sigma'].value if dm_tab is None: dm_tab = DMSpecTable.create(cd.refSpec.emin, cd.refSpec.emax, [], masses) chan_code = DMFitFunction.channel_rev_map[chan] if apply_prior: jfactor = { 'j_value': jfactor, 'functype': 'lgauss_like', 'mu': 1.0, 'sigma': jsigma } cd_dm = dm_tab.convert_castro_data(cd, chan_code, 'eflux', jfactor=jfactor) print(name, jfactor) lims_out += [cd_dm.getLimits(alpha)] ts_out += [np.clip(cd_dm.ts_vals(), 0, None)] cd_stack += [cd_dm] cd_dm_comb = DMCastroData.create_from_stack(cd_stack) lims_out += [cd_dm_comb.getLimits(alpha)] ts_out += [cd_dm_comb.ts_vals()] names += ['combined'] masses = np.vstack([masses for i in range(len(names))]) lims_out = np.vstack(lims_out) ts_out = np.vstack(ts_out) header = 'Channel: %s\n' % chan header += 'CL: %.3f\n' % (1.0 - alpha) header += 'Column 00: Mass (GeV)\n' for i, name in enumerate(names): header += 'Column %02i: %s sigma-v UL (cm^3 s^-1)\n' % (i + 1, name) header += 'Column %02i: combined sigma-v UL (cm^3 s^-1)\n' % (len(names) + 1) cols = [ Column(name='name', data=names), Column(name='mass', data=masses, unit='GeV'), Column(name='sigmav_ul', data=lims_out, unit='cm^3 / s'), Column(name='ts', data=ts_out), ] text_out = np.vstack([masses[0]] + [lims_out]) tab = Table(cols) tab.write('%s_%s.fits' % (outprefix, chan), overwrite=True) np.savetxt('%s_%s.txt' % (outprefix, chan), text_out.T, fmt='%12.5g', header=header)
def convert_sed(spec_table, specs, sed_file, outfile, limitfile, **kwargs): """Convert a single SED to stacking space. Parameters ---------- spec_table : `StackSpecTable` Object with all the stacking spectra specs : list List of the specs to convert sed_file : str Path to the SED file outfile : str Path to write the output `StackCastroData` object to limitfile : str Path to write the output limits to. Keyword arguments ----------------- norm_type : str Normalization type to use astro_val : dict Dictionary with information about the astrophysical norm clobber : bool Flag to overwrite existing files. """ norm_type = kwargs.get('norm_type', 'eflux') astro_val = kwargs.get('astro_val', None) clobber = kwargs.get('clobber', False) use_specs = specs exttype = os.path.splitext(sed_file)[-1] if exttype in ['.fits', '.npy']: sed = CastroData.create_from_sedfile(sed_file, norm_type) elif exttype in ['.yaml']: sed = CastroData.create_from_yamlfile(sed_file) else: raise ValueError("Can not read file type %s for SED" % exttype) c_list, t_list, n_list = ConvertCastro.convert_sed_to_stack_var( spec_table, sed, norm_type=norm_type, astro_val=astro_val) if is_not_null(outfile): fits_utils.write_tables_to_fits(outfile, t_list, clobber=clobber, namelist=n_list) if is_not_null(limitfile): stacked_lists = ConvertCastro.extract_stack_limits( c_list, use_specs, [0.68, 0.95]) fits_utils.write_tables_to_fits(limitfile, stacked_lists[1], clobber=clobber, namelist=stacked_lists[2])