def src_detect(inskymap, outmodel, outds9file, threshold=5.0, maxsrcs=20, avgrad=1.0, corr_rad=0.2, exclrad=0.2, logfile=None, silent=False): """ Detect sources from a map See http://cta.irap.omp.eu/ctools/users/reference_manual/cssrcdetect.html Parameters ---------- - inskymap (str): input skymap file - outmodel (str): output xml model filename - outds9file (str): output ds9 region filename - threshold (float): treshold (sigma gaussian) for detection - maxsrcs (int): maximum number of sources - avgrad (float): averaging radius for significance computation (degrees) - corr_rad (float): correlation kernel (deg) - exclrad (float): Radius around a detected source that is excluded from further source detection (degrees). - silent (bool): print information or not Outputs -------- - Write xml model file - Write a DS9 region file """ srcdet = cscripts.cssrcdetect() srcdet['inmap'] = inskymap srcdet['outmodel'] = outmodel srcdet['outds9file'] = outds9file srcdet[ 'srcmodel'] = 'POINT' # For the moment, only point source + power law srcdet['bkgmodel'] = 'NONE' # NONE|IRF|AEFF|CUBE|RACC srcdet['threshold'] = threshold srcdet['maxsrcs'] = maxsrcs srcdet['avgrad'] = avgrad srcdet['corr_rad'] = corr_rad srcdet['corr_kern'] = 'GAUSSIAN' #<NONE|DISK|GAUSSIAN> srcdet['exclrad'] = exclrad srcdet['fit_pos'] = True srcdet['fit_shape'] = True if logfile is not None: srcdet['logfile'] = logfile if logfile is not None: srcdet.logFileOpen() srcdet.execute() if logfile is not None: srcdet.logFileClose() if not silent: print(srcdet) print('') return srcdet
def _test_python(self): """ Test cssrcdetect from Python """ # Allocate empty cssrcdetect script srcdetect = cscripts.cssrcdetect() # Check that saving does not nothing srcdetect['outmodel'] = 'cssrcdetect_py0.xml' srcdetect['outds9file'] = 'cssrcdetect_py0.reg' srcdetect['logfile'] = 'cssrcdetect_py0.log' srcdetect.logFileOpen() srcdetect.save() # Check than an empty model definition file has been created self._check_model_file('cssrcdetect_py0.xml', 0) # Check that clearing does not lead to an exception or segfault srcdetect.clear() # Now set cssrcdetect parameters srcdetect['inmap'] = self._map srcdetect['outmodel'] = 'cssrcdetect_py1.xml' srcdetect['outds9file'] = 'cssrcdetect_py1.reg' srcdetect['srcmodel'] = 'POINT' srcdetect['bkgmodel'] = 'NONE' srcdetect['threshold'] = 8.0 srcdetect['corr_rad'] = 0.1 srcdetect['logfile'] = 'cssrcdetect_py1.log' srcdetect['chatter'] = 4 # Run cssrcdetect script and save models srcdetect.logFileOpen() # Make sure we get a log file srcdetect.run() srcdetect.save() # Check model file self._check_model_file('cssrcdetect_py1.xml', 2) # Execute cssrcdetect script again, now with a higher chatter level # than before srcdetect['outmodel'] = 'cssrcdetect_py2.xml' srcdetect['outds9file'] = 'cssrcdetect_py2.reg' srcdetect['bkgmodel'] = 'IRF' srcdetect['threshold'] = 8.0 srcdetect['logfile'] = 'cssrcdetect_py2.log' srcdetect['chatter'] = 3 srcdetect.logFileOpen() # Needed to get a new log file srcdetect.execute() # Check model file self._check_model_file('cssrcdetect_py2.xml', 3) # Return return
def _test_python(self): """ Test cssrcdetect from Python """ # Allocate empty cssrcdetect script srcdetect = cscripts.cssrcdetect() # Check that saving does not nothing srcdetect['outmodel'] = 'cssrcdetect_py0.xml' srcdetect['outds9file'] = 'cssrcdetect_py0.reg' srcdetect['logfile'] = 'cssrcdetect_py0.log' srcdetect.logFileOpen() srcdetect.save() # Check than an empty model definition file has been created self._check_model_file('cssrcdetect_py0.xml', 0) # Check that clearing does not lead to an exception or segfault srcdetect.clear() # Now set cssrcdetect parameters srcdetect['inmap'] = self._map srcdetect['outmodel'] = 'cssrcdetect_py1.xml' srcdetect['outds9file'] = 'cssrcdetect_py1.reg' srcdetect['srcmodel'] = 'POINT' srcdetect['bkgmodel'] = 'NONE' srcdetect['threshold'] = 10.0 srcdetect['logfile'] = 'cssrcdetect_py1.log' srcdetect['chatter'] = 2 # Run cssrcdetect script and save models srcdetect.logFileOpen() # Make sure we get a log file srcdetect.run() srcdetect.save() # Check model file self._check_model_file('cssrcdetect_py1.xml', 2) # Execute cssrcdetect script again, now with a higher chatter level # than before srcdetect['outmodel'] = 'cssrcdetect_py2.xml' srcdetect['outds9file'] = 'cssrcdetect_py2.reg' srcdetect['bkgmodel'] = 'IRF' srcdetect['logfile'] = 'cssrcdetect_py2.log' srcdetect['chatter'] = 3 srcdetect.logFileOpen() # Needed to get a new log file srcdetect.execute() # Check model file self._check_model_file('cssrcdetect_py2.xml', 3) # Return return
def gw_simulation(sim_in, config_in, model_xml, fits_model, counter): """ :param sim_in: :param config_in: :param model_xml: :param fits_model: :param counter: :return: """ src_name = fits_model.split("/")[-1][:-5] run_id, merger_id = src_name.split('_') fits_header_0 = fits.open(fits_model)[0].header ra_src = fits_header_0['RA'] dec_src = fits_header_0['DEC'] coordinate_source = SkyCoord(ra=ra_src * u.deg, dec=dec_src * u.deg, frame="icrs") src_yaml = sim_in['source'] point_path = create_path(src_yaml['pointings_path']) opt_point_path = f"{point_path}/optimized_pointings" ctools_pipe_path = create_path(config_in['exe']['software_path']) ctobss_params = sim_in['ctobssim'] seed = int(counter)*10 # # PARAMETERS FROM THE CTOBSSIM sim_e_min = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV) sim_e_max = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV) sim_rad = ctobss_params['radius'] output_path = create_path(sim_in['output']['path'] + f"/{src_name}/seed-{seed:03}") irf_dict = sim_in['IRF'] site = irf_dict['site'] detection = sim_in['detection'] significance_map = detection['skymap_significance'] srcdetect_ctlike = detection['srcdetect_likelihood'] save_simulation = ctobss_params['save_simulation'] try: mergers_data = pd.read_csv( f"{point_path}/BNS-GW-Time_onAxis5deg.txt", sep=" ") except FileNotFoundError: print("merger data not present. check that the text file with the correct pointings is in the 'pointings' folder!") sys.exit() filter_mask = (mergers_data["run"] == run_id) & (mergers_data["MergerID"] == f"Merger{merger_id}") merger_onset_data = mergers_data[filter_mask] time_onset_merger = merger_onset_data['Time'].values[0] with open(f"{output_path}/GW-{src_name}_seed-{seed:03}_site-{site}.txt", "w") as f: f.write(f"GW_name\tRA_src\tDEC_src\tseed\tpointing_id\tsrc_to_point\tsrc_in_point\tra_point\tdec_point\tradius\ttime_start\ttime_end\tsignificanceskymap\tsigmasrcdetectctlike\n") try: file_name = f"{opt_point_path}/{run_id}_Merger{merger_id}_GWOptimisation_v3.txt" pointing_data = pd.read_csv( file_name, header=0, sep=",") except FileNotFoundError: print("File not found\n") sys.exit() RA_data = pointing_data['RA(deg)'] DEC_data = pointing_data['DEC(deg)'] times = pointing_data['Observation Time UTC'] durations = pointing_data['Duration'] # LOOP OVER POINTINGS for index in range(0, len(pointing_data)): RA_point = RA_data[index] DEC_point = DEC_data[index] coordinate_pointing = SkyCoord( ra=RA_point * u.degree, dec=DEC_point * u.degree, frame="icrs" ) src_from_pointing = coordinate_pointing.separation(coordinate_source) t_in_point = Time(times[index]) obs_condition = Observability(site=site) obs_condition.set_irf(irf_dict) obs_condition.Proposal_obTime = 10 obs_condition.TimeOffset = 0 obs_condition.Steps_observability = 10 condition_check = obs_condition.check(RA=RA_point, DEC=DEC_point, t_start=t_in_point) # once the IRF has been chosen, the times are shifted # this is a quick and dirty solution to handle the times in ctools...not elegant for sure t_in_point = (Time(times[index]) - Time(time_onset_merger)).to(u.s) t_end_point = t_in_point + durations[index] * u.s if len(condition_check) == 0: print(f"Source Not Visible in pointing {index}") f.write( f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point}\t{DEC_point}\t{sim_rad}\t{t_in_point.value:.2f}\t{t_end_point.value:.2f}\t -1 \t -1\n") continue name_irf = condition_check['IRF_name'][0] irf = condition_check['IRF'][0] # model loading if irf.prod_number == "3b" and irf.prod_version == 0: caldb = "prod3b" else: caldb = f'prod{irf.prod_number}-v{irf.prod_version}' # simulation sim = ctools.ctobssim() sim['inmodel'] = model_xml sim['caldb'] = caldb sim['irf'] = name_irf sim['ra'] = RA_point sim['dec'] = DEC_point sim['rad'] = sim_rad sim['tmin'] = t_in_point.value sim['tmax'] = t_end_point.value sim['emin'] = sim_e_min sim['emax'] = sim_e_max sim['seed'] = seed if save_simulation: event_list_path = create_path(f"{ctobss_params['output_path']}/{src_name}/seed-{seed:03}/") sim['outevents'] = f"{event_list_path}/event_list_source-{src_name}_seed-{seed:03}_pointingID-{index}.fits" sim.execute() f.write( f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point}\t{DEC_point}\t{sim_rad}\t{t_in_point.value:.2f}\t{t_end_point.value:.2f}\t -1 \t -1\n" ) continue else: sim.run() obs = sim.obs() obs.models(gammalib.GModels()) # ctskymap sigma_onoff = -1 sqrt_ts_like = -1 if significance_map: pars_skymap = detection['parameters_skymap'] scale = float(pars_skymap['scale']) npix = 2 * int(sim_rad / scale) fits_temp_title = f"{output_path}/GW-skymap_point-{index}_{seed}.fits" skymap = ctools.ctskymap(obs.copy()) skymap['proj'] = 'CAR' skymap['coordsys'] = 'CEL' skymap['xref'] = RA_point skymap['yref'] = DEC_point skymap['binsz'] = scale skymap['nxpix'] = npix skymap['nypix'] = npix skymap['emin'] = sim_e_min skymap['emax'] = sim_e_max skymap['bkgsubtract'] = 'RING' skymap['roiradius'] = pars_skymap['roiradius'] skymap['inradius'] = pars_skymap['inradius'] skymap['outradius'] = pars_skymap['outradius'] skymap['iterations'] = pars_skymap['iterations'] skymap['threshold'] = pars_skymap['threshold'] skymap['outmap'] = fits_temp_title skymap.execute() input_fits = fits.open(fits_temp_title) datain = input_fits['SIGNIFICANCE'].data datain[np.isnan(datain)] = 0.0 datain[np.isinf(datain)] = 0.0 sigma_onoff = np.max(datain) if pars_skymap['remove_fits']: os.remove(fits_temp_title) if srcdetect_ctlike: pars_detect = detection['parameters_detect'] scale = float(pars_detect['scale']) npix = 2 * int(sim_rad / scale) skymap = ctools.ctskymap(obs.copy()) skymap['proj'] = 'TAN' skymap['coordsys'] = 'CEL' skymap['xref'] = RA_point skymap['yref'] = DEC_point skymap['binsz'] = scale skymap['nxpix'] = npix skymap['nypix'] = npix skymap['emin'] = sim_e_min skymap['emax'] = sim_e_max skymap['bkgsubtract'] = 'NONE' skymap.run() # cssrcdetect srcdetect = cscripts.cssrcdetect(skymap.skymap().copy()) srcdetect['srcmodel'] = 'POINT' srcdetect['bkgmodel'] = 'NONE' srcdetect['corr_kern'] = 'GAUSSIAN' srcdetect['threshold'] = pars_detect['threshold'] srcdetect['corr_rad'] = pars_detect['correlation'] srcdetect.run() models = srcdetect.models() # if there's some detection we can do the likelihood. # Spectral model is a PL and the spatial model is the one from cssrcdetect if len(models) > 0: hotspot = models['Src001'] ra_hotspot = hotspot['RA'].value() dec_hotspot = hotspot['DEC'].value() models_ctlike = gammalib.GModels() src_dir = gammalib.GSkyDir() src_dir.radec_deg(ra_hotspot, dec_hotspot) spatial = gammalib.GModelSpatialPointSource(src_dir) spectral = gammalib.GModelSpectralPlaw() spectral['Prefactor'].value(5.5e-16) spectral['Prefactor'].scale(1e-16) spectral['Index'].value(-2.6) spectral['Index'].scale(-1.0) spectral['PivotEnergy'].value(50000) spectral['PivotEnergy'].scale(1e3) model_src = gammalib.GModelSky(spatial, spectral) model_src.name('PL_fit_GW') model_src.tscalc(True) models_ctlike.append(model_src) spectral_back = gammalib.GModelSpectralPlaw() spectral_back['Prefactor'].value(1.0) spectral_back['Prefactor'].scale(1.0) spectral_back['Index'].value(0) spectral_back['PivotEnergy'].value(300000) spectral_back['PivotEnergy'].scale(1e6) back_model = gammalib.GCTAModelIrfBackground() back_model.instruments('CTA') back_model.name('Background') back_model.spectral(spectral_back.copy()) models_ctlike.append(back_model) xmlmodel_PL_ctlike_std = f"{output_path}/model_PL_ctlike_std_seed-{seed}_pointing-{index}.xml" models_ctlike.save(xmlmodel_PL_ctlike_std) like_pl = ctools.ctlike(obs.copy()) like_pl['inmodel'] = xmlmodel_PL_ctlike_std like_pl['caldb'] = caldb like_pl['irf'] = name_irf like_pl.run() ts = -like_pl.obs().models()[0].ts() if ts > 0: sqrt_ts_like = np.sqrt(ts) else: sqrt_ts_like = 0 if pars_detect['remove_xml']: os.remove(xmlmodel_PL_ctlike_std) f.write( f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point:.2f}\t{DEC_point:.2f}\t{sim_rad}\t{t_in_point:.2f}\t{t_end_point:.2f}\t{sigma_onoff:.2f}\t{sqrt_ts_like}\n")
import ctools import cscripts debug = True skymap = ctools.ctskymap() skymap['inobs'] = 'obs_selected.xml' skymap['outmap'] = 'skymap_bkgsubtract.fits' skymap['emin'] = 0.1 skymap['emax'] = 100.0 skymap['nxpix'] = 600 skymap['nypix'] = 600 skymap['binsz'] = 0.02 skymap['coordsys'] = 'GAL' skymap['proj'] = 'CAR' skymap['xref'] = 0.0 skymap['yref'] = 0.0 skymap['bkgsubtract'] = 'IRF' skymap["debug"] = debug skymap.execute() detect = cscripts.cssrcdetect() detect["inmap"] = 'skymap_bkgsubtract.fits' detect["outmodel"] = 'stacked_models.xml' detect["outds9file"] = 'ds9_1.reg' detect["srcmodel"] = 'POINT' detect["bkgmodel"] = 'CUBE' detect["threshold"] = 15.0 detect["debug"] = debug detect.execute()