Commit eb9b3f229ebdd450eec790176fa9b16515e54cc3

Authored by Josh Cardenzana
1 parent 6642ae65

Add parameter and methods for accessing generated GFits object in ctskymap

This enables accessing the generated skymaps without calling 'save()' first
pyext/ctskymap.i
@@ -49,6 +49,7 @@ public: @@ -49,6 +49,7 @@ public:
49 void save(void); 49 void save(void);
50 void publish(const std::string& name = ""); 50 void publish(const std::string& name = "");
51 const GSkyMap& skymap(void) const; 51 const GSkyMap& skymap(void) const;
  52 + const GFits& fits(void) const;
52 void exclusion_map(const GSkyRegionMap& exclusion_map); 53 void exclusion_map(const GSkyRegionMap& exclusion_map);
53 GSkyRegionMap exclusion_map(void) const; 54 GSkyRegionMap exclusion_map(void) const;
54 }; 55 };
src/ctskymap/ctskymap.cpp
@@ -240,6 +240,9 @@ void ctskymap::run(void) @@ -240,6 +240,9 @@ void ctskymap::run(void)
240 publish(); 240 publish();
241 } 241 }
242 242
  243 + // Create the output fits object
  244 + construct_fits();
  245 +
243 // Return 246 // Return
244 return; 247 return;
245 } 248 }
@@ -262,51 +265,60 @@ void ctskymap::save(void) @@ -262,51 +265,60 @@ void ctskymap::save(void)
262 // Get sky map filename 265 // Get sky map filename
263 m_outmap = (*this)["outmap"].filename(); 266 m_outmap = (*this)["outmap"].filename();
264 267
265 - // Create empty FITS file  
266 - GFits fits; 268 + // Save FITS file to disk
  269 + m_fits.saveto(m_outmap, clobber());
267 270
268 - // Write sky map into FITS file  
269 - write_map(fits, m_skymap, "SKYMAP"); 271 + } // endif: filename and map were not empty
270 272
271 - // If background subtraction is requested then write the background map,  
272 - // the significance map, the counts map and the acceptance map to the  
273 - // FITS file  
274 - if (m_bkgsubtract != "NONE") { 273 + // Write into logger what has been done
  274 + std::string fname = (m_outmap.is_empty()) ? "NONE" : m_outmap.url();
  275 + if (m_skymap.is_empty()) {
  276 + fname.append(" (map is empty, no file created)");
  277 + }
  278 + log_value(NORMAL, "Sky map file", fname);
275 279
276 - // Write background map into FITS file  
277 - write_map(fits, m_bkgmap, "BACKGROUND"); 280 + // Return
  281 + return;
  282 +}
278 283
279 284
280 - // Write significance map into FITS file  
281 - write_map(fits, m_sigmap, "SIGNIFICANCE"); 285 +/***********************************************************************//**
  286 + * @brief Construct GFits object consisting of all maps
  287 + *
  288 + * Assembles all of the skymaps into a GFits object. This object is saved
  289 + * when ctskymap::save() is called. the object can also be accessed by
  290 + * calling ctskymap::fits().
  291 + ***************************************************************************/
  292 +void ctskymap::construct_fits(void)
  293 +{
  294 + // Write sky map into FITS object
  295 + write_map(m_fits, m_skymap, "SKYMAP");
282 296
283 - // Write counts map into FITS file  
284 - write_map(fits, m_counts, "COUNTS"); 297 + // If background subtraction is requested then write the background map,
  298 + // the significance map, the counts map and the acceptance map to the
  299 + // FITS file
  300 + if (m_bkgsubtract != "NONE") {
285 301
286 - // Write acceptance map into FITS file  
287 - write_map(fits, m_acceptance, "ACCEPTANCE"); 302 + // Write background map into FITS file
  303 + write_map(m_fits, m_bkgmap, "BACKGROUND");
288 304
289 - // If RING background subtraction is requested then write also  
290 - // the exclusion map to the FITS file  
291 - if (m_bkgsubtract == "RING") {  
292 - write_map(fits, m_exclmap, "EXCLUSION");  
293 - } 305 + // Write significance map into FITS file
  306 + write_map(m_fits, m_sigmap, "SIGNIFICANCE");
294 307
295 - } // endif: background subtraction was requested 308 + // Write counts map into FITS file
  309 + write_map(m_fits, m_counts, "COUNTS");
296 310
297 - // Save FITS file to disk  
298 - fits.saveto(m_outmap, clobber()); 311 + // Write acceptance map into FITS file
  312 + write_map(m_fits, m_acceptance, "ACCEPTANCE");
299 313
300 - } // endif: filename and map were not empty 314 + // If RING background subtraction is requested then write also
  315 + // the exclusion map to the FITS file
  316 + if (m_bkgsubtract == "RING") {
  317 + write_map(m_fits, m_exclmap, "EXCLUSION");
  318 + }
301 319
302 - // Write into logger what has been done  
303 - std::string fname = (m_outmap.is_empty()) ? "NONE" : m_outmap.url();  
304 - if (m_skymap.is_empty()) {  
305 - fname.append(" (map is empty, no file created)");  
306 - }  
307 - log_value(NORMAL, "Sky map file", fname); 320 + } // endif: background subtraction was requested
308 321
309 - // Return  
310 return; 322 return;
311 } 323 }
312 324
@@ -372,6 +384,7 @@ void ctskymap::init_members(void) @@ -372,6 +384,7 @@ void ctskymap::init_members(void)
372 m_counts.clear(); 384 m_counts.clear();
373 m_acceptance.clear(); 385 m_acceptance.clear();
374 m_exclmap.clear(); 386 m_exclmap.clear();
  387 + m_fits.clear();
375 388
376 // Initialise cache 389 // Initialise cache
377 m_solidangle.clear(); 390 m_solidangle.clear();
@@ -415,6 +428,7 @@ void ctskymap::copy_members(const ctskymap& app) @@ -415,6 +428,7 @@ void ctskymap::copy_members(const ctskymap& app)
415 m_counts = app.m_counts; 428 m_counts = app.m_counts;
416 m_acceptance = app.m_acceptance; 429 m_acceptance = app.m_acceptance;
417 m_exclmap = app.m_exclmap; 430 m_exclmap = app.m_exclmap;
  431 + m_fits = app.m_fits;
418 432
419 // Copy cache 433 // Copy cache
420 m_solidangle = app.m_solidangle; 434 m_solidangle = app.m_solidangle;
src/ctskymap/ctskymap.hpp
@@ -61,6 +61,7 @@ public: @@ -61,6 +61,7 @@ public:
61 void save(void); 61 void save(void);
62 void publish(const std::string& name = ""); 62 void publish(const std::string& name = "");
63 const GSkyMap& skymap(void) const; 63 const GSkyMap& skymap(void) const;
  64 + const GFits& fits(void) const;
64 void exclusion_map(const GSkyRegionMap& exclusion_map); 65 void exclusion_map(const GSkyRegionMap& exclusion_map);
65 GSkyRegionMap exclusion_map(void) const; 66 GSkyRegionMap exclusion_map(void) const;
66 67
@@ -87,6 +88,7 @@ protected: @@ -87,6 +88,7 @@ protected:
87 double& non, 88 double& non,
88 double& noff, 89 double& noff,
89 double& alpha); 90 double& alpha);
  91 + void construct_fits(void);
90 void ring_bounding_box(const int& ipixel, int& ix1, int& ix2, 92 void ring_bounding_box(const int& ipixel, int& ix1, int& ix2,
91 int& iy1, int& iy2) const; 93 int& iy1, int& iy2) const;
92 GSkyMap ring_convolve(const GSkyMap& map, const double& rmin, 94 GSkyMap ring_convolve(const GSkyMap& map, const double& rmin,
@@ -123,6 +125,7 @@ protected: @@ -123,6 +125,7 @@ protected:
123 GSkyMap m_counts; //!< Counts map 125 GSkyMap m_counts; //!< Counts map
124 GSkyMap m_acceptance; //!< Acceptance map 126 GSkyMap m_acceptance; //!< Acceptance map
125 GSkyMap m_exclmap; //!< Exclusion map for RING background 127 GSkyMap m_exclmap; //!< Exclusion map for RING background
  128 + GFits m_fits; //!< Output GFits object
126 129
127 // Caching variables to prevent multiple computations of the same thing 130 // Caching variables to prevent multiple computations of the same thing
128 std::vector<double> m_solidangle; //!< Cached pixel solid angles 131 std::vector<double> m_solidangle; //!< Cached pixel solid angles
@@ -145,6 +148,13 @@ const GSkyMap&amp; ctskymap::skymap(void) const @@ -145,6 +148,13 @@ const GSkyMap&amp; ctskymap::skymap(void) const
145 } 148 }
146 149
147 150
  151 +inline
  152 +const GFits& ctskymap::fits(void) const
  153 +{
  154 + return m_fits;
  155 +}
  156 +
  157 +
148 /***********************************************************************//** 158 /***********************************************************************//**
149 * @brief Set exclusion region map 159 * @brief Set exclusion region map
150 * 160 *
test/test_ctskymap.py
@@ -290,6 +290,11 @@ class Test(test): @@ -290,6 +290,11 @@ class Test(test):
290 self._check_result_file('ctskymap_ring_py1.fits[BACKGROUND]', 10, 10) 290 self._check_result_file('ctskymap_ring_py1.fits[BACKGROUND]', 10, 10)
291 self._check_result_file('ctskymap_ring_py1.fits[SIGNIFICANCE]', 10, 10) 291 self._check_result_file('ctskymap_ring_py1.fits[SIGNIFICANCE]', 10, 10)
292 292
  293 + # Check generated fits object
  294 + self._check_map(gammalib.GSkyMap(skymap.fits().image('SKYMAP')), nx=10, ny=10)
  295 + self._check_map(gammalib.GSkyMap(skymap.fits().image('BACKGROUND')), nx=10, ny=10)
  296 + self._check_map(gammalib.GSkyMap(skymap.fits().image('SIGNIFICANCE')), nx=10, ny=10)
  297 +
293 # Test iterative RING background subtraction 298 # Test iterative RING background subtraction
294 skymap = ctools.ctskymap() 299 skymap = ctools.ctskymap()
295 skymap['inobs'] = self._events 300 skymap['inobs'] = self._events