99from  astropy .time  import  Time 
1010import  astropy .units  as  u 
1111from  astropy .visualization  import  quantity_support 
12+ from  gammapy .irf  import  load_irf_dict_from_file 
13+ 
14+ from  gammapy .datasets  import  SpectrumDataset 
15+ from  gammapy .makers  import  (
16+     ReflectedRegionsBackgroundMaker ,
17+     SpectrumDatasetMaker ,
18+     WobbleRegionsFinder ,
19+ )
20+ from  gammapy .modeling .models  import  Models 
1221import  matplotlib .pyplot  as  plt 
1322
1423from  .. import  __version__ 
2130    source_detection ,
2231    calculate ,
2332)
24- from  ..plots  import  plot_spectrum , plot_sed , plot_transit , plot_altitude_airmass 
33+ from  ..gammapy_interface  import  (
34+     load_real_observations ,
35+     create_simulated_observation ,
36+     has_radmax_2d ,
37+     define_on_region_geometry ,
38+     estimate_sed ,
39+     fake_onoff_from_fake_observation ,
40+     fake_onoff_from_real_observations ,
41+     load_energy_axis_from_config ,
42+     get_wstat_table ,
43+ )
44+ from  ..plots  import  (
45+     plot_spectrum ,
46+     plot_sed ,
47+     plot_transit ,
48+     plot_altitude_airmass ,
49+     plot_gammapy_sed ,
50+ )
2551from  .. import  RESOURCES_PATH 
2652
2753parser  =  argparse .ArgumentParser ()
3258
3359parser_config  =  subparsers .add_parser (
3460    "config" ,
35-     help = "Copy the default  configuration file  to your new project directory." ,
61+     help = "Copy the configuration files  to your new project directory." ,
3662)
3763
3864parser_config .add_argument (
4773    help = "Launch the estimation of observability and physical properties of the target source." ,
4874)
4975
76+ parser_run .add_argument (
77+     "--use-gammapy" , action = "store_true" , help = "Use also the new gammapy interface." 
78+ )
79+ 
5080parser_run .add_argument (
5181    "--config" , required = True , type = str , help = "Path to configuration file." 
5282)
83+ parser_run .add_argument (
84+     "--models" ,
85+     required = False ,
86+     type = str ,
87+     help = "Path to gammapy model file (models.yml)." ,
88+ )
5389parser_run .add_argument (
5490    "--source-name" ,
5591    default = "test_source" ,
@@ -88,6 +124,7 @@ def main():
88124            Path .cwd () if  args_config .to  is  None  else  Path (args_config .to )
89125        )
90126        shutil .copy (RESOURCES_PATH  /  "config.yml" , config_output_path )
127+         shutil .copy (RESOURCES_PATH  /  "models.yml" , config_output_path )
91128
92129        parser .exit (0 , message = "You're halfway there! Have fun!\n " )
93130
@@ -154,9 +191,9 @@ def main():
154191            energy_bins , gamma_rate , background_rate , config , assumed_spectrum 
155192        )
156193
157-         combined_significance  =  source_detection ( 
158-              sigmas ,  u . Quantity ( config [ "observation_time" ]) 
159-         )
194+         simulated_observation_livetime  =  u . Quantity ( config [ "observation_time" ]) 
195+ 
196+         combined_significance   =   source_detection ( sigmas ,  simulated_observation_livetime )
160197
161198        with  quantity_support ():
162199            plot_sed (
@@ -181,6 +218,143 @@ def main():
181218
182219        crab  =  FixedTarget .from_name ("Crab" )
183220
221+         target_source_coordinates  =  target_source .coord 
222+ 
223+         if  args .use_gammapy :
224+             gammapy_config  =  config ["gammapy_interface" ]
225+ 
226+             gammapy_analysis_type  =  gammapy_config ["datasets" ]["type" ]
227+ 
228+             if  gammapy_analysis_type  ==  "1d" :
229+                 required_irfs  =  "point-like" 
230+             else :
231+                 raise  NotImplementedError (
232+                     "Only 1D analyses from point-like simulations are available at the moment with the gammapy interface." 
233+                 )
234+ 
235+             gammapy_input  =  Path (gammapy_config ["input" ])
236+ 
237+             sky_models  =  Models .from_dict (read_yaml (args .models ))
238+ 
239+             if  gammapy_input .is_dir ():
240+                 logger .info (
241+                     "Creating data store from real observations at %s" , gammapy_input 
242+                 )
243+                 observations  =  load_real_observations (gammapy_input , required_irfs )
244+             else :
245+                 logger .info (
246+                     "Loading instrument response functions from %s" , gammapy_input 
247+                 )
248+                 irfs  =  load_irf_dict_from_file (gammapy_input )
249+ 
250+                 observations  =  create_simulated_observation (
251+                     target_source_coordinates ,
252+                     observer ,
253+                     simulated_observation_livetime ,
254+                     irfs ,
255+                 )
256+ 
257+             reco_energy_axis  =  load_energy_axis_from_config (gammapy_config , "energy" )
258+             true_energy_axis  =  load_energy_axis_from_config (
259+                 gammapy_config , "energy_true" 
260+             )
261+ 
262+             on_region_config  =  gammapy_config ["datasets" ]["on_region" ]
263+             offset  =  on_region_config ["offset" ]
264+             on_region_radius  =  on_region_config ["radius" ] or  None 
265+             on_region_geometry  =  define_on_region_geometry (
266+                 target_source_coordinates ,
267+                 observations [0 ],
268+                 offset ,
269+                 reco_energy_axis ,
270+                 on_region_radius = on_region_radius ,
271+             )
272+ 
273+             empty_spectrum_dataset  =  SpectrumDataset .create (
274+                 geom = on_region_geometry ,
275+                 energy_axis_true = true_energy_axis ,
276+             )
277+ 
278+             logger .info ("Angular cut depends on reconstructed energy." )
279+ 
280+             is_point_like  =  has_radmax_2d (observations [0 ])
281+ 
282+             containment_correction  =  gammapy_config ["datasets" ][
283+                 "containment_correction" 
284+             ]
285+             if  is_point_like :
286+                 logger .warning (
287+                     "Input observation is point-like, so containment correction will be disabled." 
288+                 )
289+                 containment_correction  =  False 
290+ 
291+             n_off_regions  =  config ["n_off_regions" ]
292+ 
293+             if  len (observations ) >=  1  and  has_radmax_2d (observations [0 ]):
294+                 region_finder  =  WobbleRegionsFinder (n_off_regions = n_off_regions )
295+                 background_maker  =  ReflectedRegionsBackgroundMaker (
296+                     region_finder = region_finder 
297+                 )
298+ 
299+                 spectrum_dataset_maker  =  SpectrumDatasetMaker (
300+                     containment_correction = containment_correction ,
301+                     selection = ["counts" , "exposure" , "edisp" ],
302+                 )
303+ 
304+                 empty_spectrum_dataset  =  SpectrumDataset .create (
305+                     geom = on_region_geometry ,
306+                     energy_axis_true = true_energy_axis ,
307+                 )
308+ 
309+                 spectrum_datasets_on_off  =  fake_onoff_from_real_observations (
310+                     observations ,
311+                     empty_spectrum_dataset ,
312+                     simulated_observation_livetime ,
313+                     spectrum_dataset_maker ,
314+                     background_maker ,
315+                     sky_models ,
316+                 )
317+ 
318+             else :
319+                 spectrum_dataset_maker  =  SpectrumDatasetMaker (
320+                     containment_correction = containment_correction ,
321+                     selection = ["exposure" , "edisp" , "background" ],
322+                 )
323+ 
324+                 spectrum_datasets_on_off  =  fake_onoff_from_fake_observation (
325+                     observations [0 ],
326+                     empty_spectrum_dataset ,
327+                     spectrum_dataset_maker ,
328+                     sky_models ,
329+                     n_off_regions ,
330+                     target_source ,
331+                 )
332+ 
333+             spectrum_datasets_on_off_stacked  =  spectrum_datasets_on_off .stack_reduce ()
334+             spectrum_datasets_on_off_stacked .models  =  sky_models 
335+             wstat_table  =  get_wstat_table (spectrum_datasets_on_off_stacked )
336+             logging .info ("Computing WSTAT on stacked spectrum ON/OFF dataset" )
337+             print (wstat_table )
338+ 
339+             flux_points  =  estimate_sed (
340+                 target_source ,
341+                 reco_energy_axis ,
342+                 spectrum_datasets_on_off ,
343+                 ** gammapy_config ["estimators" ]["flux_points" ] or  {},
344+             )
345+ 
346+             plot_gammapy_sed (
347+                 simulated_observation_livetime ,
348+                 flux_points ,
349+                 assumed_spectrum ,
350+                 plot_ts_profiles = True ,
351+                 energy_flux_unit = "TeV cm-2 s-1" ,
352+                 source_name = target_source .name ,
353+                 grid = {"which" : "both" , "axis" : "both" , "alpha" : 0.5 , "ls" : "dashed" },
354+                 savefig = {"format" : "pdf" },
355+                 seaborn = {"context" : config ["seaborn_options" ]["context" ]},
356+             )
357+ 
184358        with  quantity_support ():
185359            plot_transit (
186360                config ,
0 commit comments