Commit b4290847657d788b070728f708efb9f76be6ca99

Authored by Jürgen Knödlseder
1 parent e8293d76

Implement filling of map cubes

src/ctmapcube/ctmapcube.cpp
@@ -33,7 +33,6 @@ @@ -33,7 +33,6 @@
33 #include "GTools.hpp" 33 #include "GTools.hpp"
34 34
35 /* __ Method name definitions ____________________________________________ */ 35 /* __ Method name definitions ____________________________________________ */
36 -#define G_GET_PARAMETERS "ctmapcube::get_parameters()"  
37 36
38 /* __ Debug definitions __________________________________________________ */ 37 /* __ Debug definitions __________________________________________________ */
39 38
@@ -194,20 +193,8 @@ void ctmapcube::run(void) @@ -194,20 +193,8 @@ void ctmapcube::run(void)
194 log << m_models << std::endl; 193 log << m_models << std::endl;
195 } 194 }
196 195
197 - // Write header  
198 - if (logTerse()) {  
199 - log << std::endl;  
200 - log.header1("Generate map cube");  
201 - }  
202 -  
203 - //TODO  
204 -  
205 - // Log cube  
206 - if (logTerse()) {  
207 - log << std::endl;  
208 - log.header1("Map cube");  
209 - log << m_cube << std::endl;  
210 - } 196 + // Generate map cube
  197 + create_cube();
211 198
212 // Optionally publish map cube 199 // Optionally publish map cube
213 if (m_publish) { 200 if (m_publish) {
@@ -240,8 +227,8 @@ void ctmapcube::save(void) @@ -240,8 +227,8 @@ void ctmapcube::save(void)
240 227
241 // Log filename 228 // Log filename
242 if (logTerse()) { 229 if (logTerse()) {
243 - log << "Save map cube into file \""+m_outcube+"\".";  
244 - log << std::endl; 230 + log << gammalib::parformat("Map cube file");
  231 + log << m_outcube.url() << std::endl;
245 } 232 }
246 233
247 // Save map cube into FITS file 234 // Save map cube into FITS file
@@ -275,7 +262,8 @@ void ctmapcube::publish(const std::string&amp; name) @@ -275,7 +262,8 @@ void ctmapcube::publish(const std::string&amp; name)
275 262
276 // Log filename 263 // Log filename
277 if (logTerse()) { 264 if (logTerse()) {
278 - log << "Publish \""+user_name+"\" map cube." << std::endl; 265 + log << gammalib::parformat("Map cube");
  266 + log << user_name << std::endl;
279 } 267 }
280 268
281 // Publish map cube 269 // Publish map cube
@@ -360,13 +348,6 @@ void ctmapcube::get_parameters(void) @@ -360,13 +348,6 @@ void ctmapcube::get_parameters(void)
360 int nxpix = (*this)["nxpix"].integer(); 348 int nxpix = (*this)["nxpix"].integer();
361 int nypix = (*this)["nypix"].integer(); 349 int nypix = (*this)["nypix"].integer();
362 350
363 - // Allocate map cube  
364 - GSkyMap cube(proj, coordsys, xref, yref, -binsz, binsz, nxpix, nypix,  
365 - energies.size());  
366 -  
367 - // Allocate map cube  
368 - m_cube = GModelSpatialDiffuseCube(cube, energies);  
369 -  
370 // Read model definition file if required 351 // Read model definition file if required
371 if (m_models.size() == 0) { 352 if (m_models.size() == 0) {
372 353
@@ -386,6 +367,13 @@ void ctmapcube::get_parameters(void) @@ -386,6 +367,13 @@ void ctmapcube::get_parameters(void)
386 m_outcube = (*this)["outcube"].filename(); 367 m_outcube = (*this)["outcube"].filename();
387 } 368 }
388 369
  370 + // Allocate map cube
  371 + GSkyMap cube(proj, coordsys, xref, yref, -binsz, binsz, nxpix, nypix,
  372 + energies.size());
  373 +
  374 + // Allocate map cube
  375 + m_cube = GModelSpatialDiffuseCube(cube, energies);
  376 +
389 // Write parameters into logger 377 // Write parameters into logger
390 if (logTerse()) { 378 if (logTerse()) {
391 log_parameters(); 379 log_parameters();
@@ -400,9 +388,6 @@ void ctmapcube::get_parameters(void) @@ -400,9 +388,6 @@ void ctmapcube::get_parameters(void)
400 /***********************************************************************//** 388 /***********************************************************************//**
401 * @brief Create energy vector from user parameters 389 * @brief Create energy vector from user parameters
402 * 390 *
403 - * @exception GException::invalid_value  
404 - * No valid energy boundary extension found.  
405 - *  
406 * Get the energy vector according to the user parameters. The method 391 * Get the energy vector according to the user parameters. The method
407 * supports loading of energy information from the ENERGIES extension (or 392 * supports loading of energy information from the ENERGIES extension (or
408 * any other extension specified by the "ebinfile" parameters), or setting 393 * any other extension specified by the "ebinfile" parameters), or setting
@@ -459,3 +444,152 @@ GEnergies ctmapcube::create_energies(void) @@ -459,3 +444,152 @@ GEnergies ctmapcube::create_energies(void)
459 // Return energies 444 // Return energies
460 return energies; 445 return energies;
461 } 446 }
  447 +
  448 +
  449 +/***********************************************************************//**
  450 + * @brief Generate map cube
  451 + *
  452 + * Generate map cube by looping over all sky models in the container.
  453 + ***************************************************************************/
  454 +void ctmapcube::create_cube(void)
  455 +{
  456 + // Write header
  457 + if (logTerse()) {
  458 + log << std::endl;
  459 + log.header1("Generate map cube");
  460 + }
  461 +
  462 + // Loop over all models
  463 + for (int i = 0; i < m_models.size(); ++i) {
  464 +
  465 + // Get pointer to sky model
  466 + GModelSky* model = dynamic_cast<GModelSky*>(m_models[i]);
  467 +
  468 + // Fall through if model is not a sky model
  469 + if (model == NULL) {
  470 +
  471 + // Signal that model is skipped
  472 + if (logNormal()) {
  473 + log << gammalib::parformat("Skip model");
  474 + log << m_models[i]->name() << std::endl;
  475 + }
  476 +
  477 + // Continue
  478 + continue;
  479 +
  480 + }
  481 +
  482 + // Signal that model is used
  483 + if (logNormal()) {
  484 + log << gammalib::parformat("Use model");
  485 + log << m_models[i]->name() << std::endl;
  486 + }
  487 +
  488 + // Add sky model
  489 + if (dynamic_cast<GModelSpatialPointSource*>(model->spatial()) != NULL) {
  490 + add_ptsrc_model(model);
  491 + }
  492 + else {
  493 + add_model(model);
  494 + }
  495 +
  496 + } // endfor: looped over energies
  497 +
  498 + // Log cube
  499 + if (logTerse()) {
  500 + log << std::endl;
  501 + log.header1("Map cube");
  502 + log << m_cube << std::endl;
  503 + }
  504 +
  505 + // Return
  506 + return;
  507 +}
  508 +
  509 +
  510 +/***********************************************************************//**
  511 + * @brief Add one model to map cube
  512 + *
  513 + * Adds one model to map cube.
  514 + ***************************************************************************/
  515 +void ctmapcube::add_model(GModelSky* model)
  516 +{
  517 + // Get cube energies
  518 + GEnergies energies = m_cube.energies();
  519 +
  520 + // Get pointer to sky map
  521 + GSkyMap* map = const_cast<GSkyMap*>(&m_cube.cube());
  522 +
  523 + // Loop over all energy bins
  524 + for (int iebin = 0; iebin < energies.size(); ++iebin) {
  525 +
  526 + // Loop over all spatial pixels
  527 + for (int ipixel = 0; ipixel < map->npix(); ++ipixel) {
  528 +
  529 + // Set photon
  530 + GPhoton photon(map->inx2dir(ipixel), energies[iebin], GTime());
  531 +
  532 + // Add model value
  533 + map->operator()(ipixel, iebin) += model->value(photon);
  534 +
  535 + } // endfor: looped over all pixels
  536 +
  537 + } // endfor: looped over all energies
  538 +
  539 + // Return
  540 + return;
  541 +}
  542 +
  543 +
  544 +/***********************************************************************//**
  545 + * @brief Add one point source model to map cube
  546 + *
  547 + * Adds one point source model to map cube.
  548 + ***************************************************************************/
  549 +void ctmapcube::add_ptsrc_model(GModelSky* model)
  550 +{
  551 + // Get point source spatial component
  552 + GModelSpatialPointSource* ptsrc = static_cast<GModelSpatialPointSource*>(model->spatial());
  553 +
  554 + // Get point source direction
  555 + GSkyDir dir = ptsrc->dir();
  556 +
  557 + // Get cube energies
  558 + GEnergies energies = m_cube.energies();
  559 +
  560 + // Get pointer to sky map
  561 + GSkyMap* map = const_cast<GSkyMap*>(&m_cube.cube());
  562 +
  563 + // Get test sky map for flux computation
  564 + GSkyMap test_map(*map);
  565 +
  566 + // Continue only if map contains point source sky direction
  567 + if (map->contains(dir)) {
  568 +
  569 + // Get map pixel
  570 + int ipixel = map->dir2inx(dir);
  571 +
  572 + // Loop over all energy bins
  573 + for (int iebin = 0; iebin < energies.size(); ++iebin) {
  574 +
  575 + // Set photon using the point source sky direction
  576 + GPhoton photon(dir, energies[iebin], GTime());
  577 +
  578 + // Compute model value
  579 + double value = model->value(photon);
  580 +
  581 + // Compute flux normalization
  582 + test_map(ipixel, iebin) = value;
  583 + double flux = test_map.flux(ipixel, iebin);
  584 + double norm = (flux > 0.0) ? value / flux : 0.0;
  585 +
  586 + // Add model to sky map
  587 + map->operator()(ipixel, iebin) += norm * value;
  588 +
  589 + } // endfor: looped over all energies
  590 +
  591 + } // endif: map contained sky direction
  592 +
  593 + // Return
  594 + return;
  595 +}
src/ctmapcube/ctmapcube.hpp
@@ -68,6 +68,9 @@ protected: @@ -68,6 +68,9 @@ protected:
68 void free_members(void); 68 void free_members(void);
69 void get_parameters(void); 69 void get_parameters(void);
70 GEnergies create_energies(void); 70 GEnergies create_energies(void);
  71 + void create_cube(void);
  72 + void add_model(GModelSky* model);
  73 + void add_ptsrc_model(GModelSky* model);
71 74
72 // User parameters 75 // User parameters
73 GFilename m_outcube; //!< Output map cube filename 76 GFilename m_outcube; //!< Output map cube filename