def make_tev_catalog_data(): click.secho('Making TeV catalog data (from gamma-cat)...', fg='green') out_dir = DATA_DIR / 'cat/tev' out_dir.mkdir(parents=True, exist_ok=True) cat = SourceCatalogGammaCat(filename='input_data/gammacat.fits.gz') cols = ['source_id', 'common_name', 'ra', 'dec', 'glon', 'glat', 'other_names', 'classes'] cat.table = cat.table[cols] data = cat._data_python_list filename = out_dir / 'cat.json' click.secho('Writing tev {}'.format(filename), fg='green') dump_to_json(data, filename)
def __init__(self, row_idxs=None): folder = os.environ['GAMMA_CAT'] filename = os.path.join(folder, 'output/gammacat.fits.gz') log.info('Reading {}'.format(filename)) self.cat = SourceCatalogGammaCat(filename) all_row_idxs = list(range(len(self.cat.table))) if row_idxs: row_idxs = sorted(set(row_idxs) & set(all_row_idxs)) else: row_idxs = all_row_idxs self.row_idxs = row_idxs
def prepare_the_pwn_table(): gammacat = SourceCatalogGammaCat() cat_table = gammacat.table mask_pwn = cat_table['classes'] == 'pwn' cat_pwn = cat_table[mask_pwn] cat_pwn.pprint() mask_nan = np.isfinite(cat_pwn['spec_flux_above_1TeV']) cat_pwn = cat_pwn[mask_nan] hgpscat = SourceCatalogHGPS(os.environ['HGPS']) hgpscat_table = hgpscat.table mask_pwn_hgps = hgpscat_table['Source_Class'] == 'PWN' hgpscat_pwn = hgpscat_table[mask_pwn_hgps] # hgpscat_pwn.pprint() print('----------------- PWN -----------------------') print(hgpscat_pwn['Source_Name', 'Flux_Spec_Int_1TeV']) num_pwn = len(hgpscat_pwn) print('num_pwn = ', num_pwn) mask_composite_hgps = hgpscat_table['Source_Class'] == 'Composite' hgpscat_composite = hgpscat_table[mask_composite_hgps] # print(hgpscat_composite['Source_Name','Flux_Spec_Int_1TeV']) idxx1 = np.where(hgpscat_composite['Source_Name'] == 'HESS J1714-385')[0] # print(len(idxx1), idxx1[0]) hgpscat_composite.remove_row(int(idxx1[0])) print('----------------- composite -----------------------') print(hgpscat_composite['Source_Name', 'Flux_Spec_Int_1TeV']) num_composite = len(hgpscat_composite) print('num composite = ', num_composite) mask_unid_hgps = hgpscat_table['Source_Class'] == 'Unid' hgpscat_unid = hgpscat_table[mask_unid_hgps] # hgpscat_unid.pprint() # print(hgpscat_unid['Source_Name','Flux_Spec_Int_1TeV']) idx1 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1852-000')[0] hgpscat_unid.remove_row(int(idx1[0])) idx2 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1457-593')[0] hgpscat_unid.remove_row(int(idx2[0])) idx3 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1503-582')[0] hgpscat_unid.remove_row(int(idx3[0])) idx4 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1646-458')[0] hgpscat_unid.remove_row(int(idx4[0])) idx5 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1641-463')[0] hgpscat_unid.remove_row(int(idx5[0])) idx6 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1729-345')[0] hgpscat_unid.remove_row(int(idx6[0])) idx7 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1747-248')[0] hgpscat_unid.remove_row(int(idx7[0])) idx8 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1800-240')[0] hgpscat_unid.remove_row(int(idx8[0])) idx9 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1808-204')[0] hgpscat_unid.remove_row(int(idx9[0])) idx10 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1832-085')[0] hgpscat_unid.remove_row(int(idx10[0])) idx11 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1832-093')[0] hgpscat_unid.remove_row(int(idx11[0])) idx12 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1848-018')[0] hgpscat_unid.remove_row(int(idx12[0])) idx13 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1923+141')[0] hgpscat_unid.remove_row(int(idx13[0])) idx14 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1943+213')[0] hgpscat_unid.remove_row(int(idx14[0])) idx15 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1702-420')[0] hgpscat_unid.remove_row(int(idx15[0])) idx16 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1844-030')[0] hgpscat_unid.remove_row(int(idx16[0])) idx17 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1746-285')[0] hgpscat_unid.remove_row(int(idx17[0])) idx18 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1745-303')[0] hgpscat_unid.remove_row(int(idx18[0])) idx19 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1741-302')[0] hgpscat_unid.remove_row(int(idx19[0])) idx20 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1745-290')[0] hgpscat_unid.remove_row(int(idx20[0])) idx21 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1626-490')[0] hgpscat_unid.remove_row(int(idx21[0])) # idx2 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1708-410')[0] # hgpscat_unid.remove_row(int(idx2[0])) # idx1 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1018-589B')[0] # hgpscat_unid.remove_row(int(idx1[0])) # idx15 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1843-033')[0] # hgpscat_unid.remove_row(int(idx15[0])) # idx5 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1614-518')[0] # hgpscat_unid.remove_row(int(idx5[0])) # idx21 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1507-622')[0] # hgpscat_unid.remove_row(int(idx21[0])) # idx27 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1841-055')[0] # hgpscat_unid.remove_row(int(idx27[0])) # idx20 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1713-381')[0] # hgpscat_unid.remove_row(int(idx20[0])) # idx6 = np.where(hgpscat_unid['Source_Name'] == 'HESS J1634-472')[0] # hgpscat_unid.remove_row(int(idx6[0])) # print(hgpscat_unid['Source_Name','Flux_Spec_Int_1TeV']) num_unid = len(hgpscat_unid) print('num_unid = ', num_unid) hgpscat_pwn_extended = vstack( [hgpscat_pwn, hgpscat_unid, hgpscat_composite]) print('----------------- extended -----------------------') print(hgpscat_pwn_extended['Source_Name', 'Flux_Spec_Int_1TeV']) num_pwn_extended = len(hgpscat_pwn_extended) print('num_tot = ', num_pwn_extended) crab_flux_above_1TeV = define_flux_crab_above_energy() print('crab_flux_above_1TeV ', crab_flux_above_1TeV) flux_above_1TeV_cu = [] sigma = [] for id in range(len(hgpscat_pwn_extended)): # print((hgpscat_pwn_extended[id]['Source_Name']),(hgpscat_pwn_extended[id]['Flux_Spec_Int_1TeV'])) ff = hgpscat_pwn_extended[id]['Flux_Spec_Int_1TeV'] * u.Unit( 'cm-2 s-1') flux_cu = (ff / crab_flux_above_1TeV).to('%') flux_above_1TeV_cu.append(flux_cu) #print(hgpscat_pwn_extended[id]['Flux_Spec_Int_1TeV'], flux_cu) sigma.append(hgpscat_pwn_extended[id]['Size'] / 2.0) flux_above_1TeV_cuu = u.Quantity(flux_above_1TeV_cu) sigma_d = u.Quantity(sigma) hgpscat_pwn_extended['flux_above_1TeV_cu'] = Column( flux_above_1TeV_cuu, description='Integral flux above 1 TeV in crab units') hgpscat_pwn_extended['sigma'] = Column( sigma_d, description='radius of angular extension') # print(hgpscat_pwn_extended['flux_crab']) how_many_above_10, how_many_8to10, how_many_6to8, how_many_4to6, how_many_2to4, how_many_1to2, how_many_below1 = 0, 0, 0, 0, 0, 0, 0 for row in range(len(hgpscat_pwn_extended)): if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] > 10): #print('CC crab>10: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_above_10 += 1 if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] > 8 and hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] < 10): #print('CC 8-10: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_8to10 += 1 if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] > 6 and hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] < 8): #print('CC 6-8: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_6to8 += 1 if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] > 4 and hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] < 6): #print('CC 4-6: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_6to8 += 1 if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] > 2 and hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] < 4): #print('CC 2-4: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_2to4 += 1 if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] < 2 and hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] > 1): #print('CC 1-2: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_1to2 += 1 if (hgpscat_pwn_extended[row]['flux_above_1TeV_cu'] < 1): #print('CC below1: ', row, hgpscat_pwn_extended[row]['flux_above_1TeV_cu'], hgpscat_pwn_extended[row]['Source_Name']) how_many_below1 += 1 print('how_many_above_10: ', how_many_above_10) print('how_many_8to10: ', how_many_8to10) print('how_many_6to8: ', how_many_6to8) print('how_many_4to6: ', how_many_4to6) print('how_many_2to4: ', how_many_2to4) print('how_many_1to2: ', how_many_1to2) print('how_many_below1: ', how_many_below1) return hgpscat_pwn_extended
from gammapy.catalog import ( SourceCatalog3FGL, SourceCatalogGammaCat, SourceCatalog3FHL, ) from gammapy.utils.fitting import Fit # ## Load spectral points # # For this analysis we choose to work with the source 'HESS J1507-622' and the associated Fermi-LAT sources '3FGL J1506.6-6219' and '3FHL J1507.9-6228e'. We load the source catalogs, and then access source of interest by name: # In[ ]: fermi_3fgl = SourceCatalog3FGL() fermi_3fhl = SourceCatalog3FHL() gammacat = SourceCatalogGammaCat("$GAMMAPY_DATA/gamma-cat/gammacat.fits.gz") # In[ ]: source_gammacat = gammacat["HESS J1507-622"] source_fermi_3fgl = fermi_3fgl["3FGL J1506.6-6219"] source_fermi_3fhl = fermi_3fhl["3FHL J1507.9-6228e"] # The corresponding flux points data can be accessed with `.flux_points` attribute: # In[ ]: flux_points_gammacat = source_gammacat.flux_points flux_points_gammacat.table # In the Fermi-LAT catalogs, integral flux points are given. Currently the flux point fitter only works with differential flux points, so we apply the conversion here.
def gammacat(): filename = "$GAMMAPY_DATA/catalogs/gammacat/gammacat.fits.gz" return SourceCatalogGammaCat(filename=filename)
on_region = CircleSkyRegion(crab_pos, 0.15 * u.deg) model = LogParabola( alpha=2.3, beta=0.01, amplitude=1e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV, ) flux_point_binning = EnergyBounds.equal_log_spacing(0.7, 30, 5, u.TeV) # In[ ]: exclusion_mask = Map.create(skydir=crab_pos, width=(10, 10), binsz=0.02) gammacat = SourceCatalogGammaCat() regions = [] for source in gammacat: if not exclusion_mask.geom.contains(source.position): continue region = CircleSkyRegion(source.position, 0.15 * u.deg) regions.append(region) exclusion_mask.data = exclusion_mask.geom.region_mask(regions, inside=False) exclusion_mask.plot() # In[ ]: config = dict( outdir=".",
crab_01 = crab.integral(emin01, emax).value crab_1 = crab.integral(emin1, emax).value flux_int_cu = (ph_fl_01 / crab_01) flux_int_cu01 = (ph_fl_1 / crab_1) final['cr_fl_1'] = flux_int_cu final['cr_fl_01'] = flux_int_cu01 final2 = final[(final.GLAT <= 2.) & (final.GLAT >= -2.) & (final.GLON <= 130.) & (final.GLON >= -70)] final3 = final[(final.GLAT <= 2.) & (final.GLAT >= -2.)] gammacat_file = '../../known-sources/external-input/gammacat.fits.gz' gammacat = SourceCatalogGammaCat(gammacat_file) gammacat_pwn_glon, gammacat_pwn_glat = [], [] for source in gammacat: if source.data.where.startswith('gal'): if 'pwn' or 'unid' in source.data.classes: try: gammacat_pwn_glon.append(source.spatial_model().lon_0.value) gammacat_pwn_glat.append(source.spatial_model().lat_0.value) except: None gammacat_pwn_glat = np.array(gammacat_pwn_glat) gammacat_pwn_glon = np.array(gammacat_pwn_glon) gammacat_pwn_glon = np.concatenate([ gammacat_pwn_glon[gammacat_pwn_glon > 180] - 360,
#zenith_bins=[min(zen_ang), 10.0, 20.0, 30.0, 45.0, 60.0, max(zen_ang)] zenith_bins=zenith_bins*u.deg axes = [ObservationGroupAxis('ZEN_PNT', zenith_bins, fmt='edges')] # Create the ObservationGroups object obs_groups = ObservationGroups(axes) # write it to file filename = str(outdir + "/group-def.fits") obs_groups.obs_groups_table.write(filename, overwrite=True) obs_table_with_group_id = obs_groups.apply(datastore.obs_table.select_obs_id(agnid)) #gammacat exclusion mask fil_gammacat="/Users/asinha/Gammapy-dev/gammapy-extra/datasets/catalogs/gammacat/gammacat.fits.gz" cat = SourceCatalogGammaCat(filename=fil_gammacat) exclusion_table = cat.table.copy() exclusion_table.rename_column('ra', 'RA') exclusion_table.rename_column('dec', 'DEC') radius = exclusion_table['morph_sigma'].data radius[np.isnan(radius)] = 0.3 exclusion_table['Radius'] = radius * u.deg exclusion_table = Table(exclusion_table) #now run the bgmaker bgmaker = OffDataBackgroundMaker( data_store=datastore, outdir=outdir, run_list=None, obs_table=obs_table_with_group_id, ntot_group=obs_groups.n_groups,
def catalog(self): log.info('Reading catalog ...') filename = CollectionConfig( in_path=self.config.in_path, out_path=self.config.out_path).gammacat_fits return SourceCatalogGammaCat(filename=filename)
def make_cubes(ereco, etrue, use_etrue, center): tmpdir = os.path.expandvars('$GAMMAPY_EXTRA') + "/test_datasets/cube/data" outdir = tmpdir outdir2 = os.path.expandvars( '$GAMMAPY_EXTRA') + '/test_datasets/cube/background' if os.path.isdir("data"): shutil.rmtree("data") if os.path.isdir("background"): shutil.rmtree("background") Path(outdir2).mkdir() ds = DataStore.from_dir("$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2") ds.copy_obs(ds.obs_table, tmpdir) data_store = DataStore.from_dir(tmpdir) # Create a background model from the 4 crab run for the counts ouside the exclusion region. it's just for test, normaly you take 8000 thousands AGN runs to build this kind of model axes = [ObservationGroupAxis('ZEN_PNT', [0, 49, 90], fmt='edges')] obs_groups = ObservationGroups(axes) obs_table_with_group_id = obs_groups.apply(data_store.obs_table) obs_groups.obs_groups_table.write(outdir2 + "/group-def.fits", overwrite=True) # Exclusion sources table cat = SourceCatalogGammaCat() exclusion_table = cat.table exclusion_table.rename_column('ra', 'RA') exclusion_table.rename_column('dec', 'DEC') radius = exclusion_table['morph_sigma'] radius.value[np.isnan(radius)] = 0.3 exclusion_table['Radius'] = radius exclusion_table = Table(exclusion_table) bgmaker = OffDataBackgroundMaker(data_store, outdir2, run_list=None, obs_table=obs_table_with_group_id, ntot_group=obs_groups.n_groups, excluded_sources=exclusion_table) bgmaker.make_model("2D") bgmaker.smooth_models("2D") bgmaker.save_models("2D") bgmaker.save_models(modeltype="2D", smooth=True) shutil.move(str(outdir2), str(outdir)) fn = outdir + '/background/group-def.fits' hdu_index_table = bgmaker.make_total_index_table( data_store=data_store, modeltype='2D', out_dir_background_model="background", filename_obs_group_table=fn, smooth=True) fn = outdir + '/hdu-index.fits.gz' hdu_index_table.write(fn, overwrite=True) offset_band = Angle([0, 2.49], 'deg') ref_cube_images = make_empty_cube(image_size=50, energy=ereco, center=center) ref_cube_exposure = make_empty_cube(image_size=50, energy=etrue, center=center, data_unit="m2 s") data_store = DataStore.from_dir(tmpdir) refheader = ref_cube_images.sky_image_ref.to_image_hdu().header exclusion_mask = SkyMask.read( '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits') exclusion_mask = exclusion_mask.reproject(reference=refheader) # Pb with the load psftable for one of the run that is not implemented yet... data_store.hdu_table.remove_row(14) cube_maker = StackedObsCubeMaker(empty_cube_images=ref_cube_images, empty_exposure_cube=ref_cube_exposure, offset_band=offset_band, data_store=data_store, obs_table=data_store.obs_table, exclusion_mask=exclusion_mask, save_bkg_scale=True) cube_maker.make_cubes(make_background_image=True, radius=10.) obslist = [data_store.obs(id) for id in data_store.obs_table["OBS_ID"]] ObsList = ObservationList(obslist) mean_psf_cube = make_mean_psf_cube(image_size=50, energy_cube=etrue, center_maps=center, center=center, ObsList=ObsList, spectral_index=2.3) if use_etrue: mean_rmf = make_mean_rmf(energy_true=etrue, energy_reco=ereco, center=center, ObsList=ObsList) filename_mask = 'exclusion_mask.fits' filename_counts = 'counts_cube.fits' filename_bkg = 'bkg_cube.fits' filename_significance = 'significance_cube.fits' filename_excess = 'excess_cube.fits' if use_etrue: filename_exposure = 'exposure_cube_etrue.fits' filename_psf = 'psf_cube_etrue.fits' filename_rmf = 'rmf.fits' mean_rmf.write(filename_rmf, clobber=True) else: filename_exposure = 'exposure_cube.fits' filename_psf = 'psf_cube.fits' exclusion_mask.write(filename_mask, clobber=True) cube_maker.counts_cube.write(filename_counts, format="fermi-counts", clobber=True) cube_maker.bkg_cube.write(filename_bkg, format="fermi-counts", clobber=True) cube_maker.significance_cube.write(filename_significance, format="fermi-counts", clobber=True) cube_maker.excess_cube.write(filename_excess, format="fermi-counts", clobber=True) cube_maker.exposure_cube.write(filename_exposure, format="fermi-counts", clobber=True) mean_psf_cube.write(filename_psf, format="fermi-counts", clobber=True)