Commit e35c6b256848c995fff3e1260bc133c565b47cf0

Authored by Jürgen Knödlseder
1 parent 14a46107

Add ctool::create_energies() method

pyext/ctool.i
@@ -100,6 +100,7 @@ public: @@ -100,6 +100,7 @@ public:
100 %rename(_setup_observations) setup_observations; 100 %rename(_setup_observations) setup_observations;
101 %rename(_setup_models) setup_models; 101 %rename(_setup_models) setup_models;
102 %rename(_create_ebounds) create_ebounds; 102 %rename(_create_ebounds) create_ebounds;
  103 + %rename(_create_energies) create_energies;
103 %rename(_create_map) create_map; 104 %rename(_create_map) create_map;
104 %rename(_create_cube) create_cube; 105 %rename(_create_cube) create_cube;
105 %rename(_create_cta_obs) create_cta_obs; 106 %rename(_create_cta_obs) create_cta_obs;
src/support/ctool.cpp
@@ -54,6 +54,7 @@ @@ -54,6 +54,7 @@
54 #define G_SETUP_MODELS "ctool::setup_models(GObservations&, std::string&)" 54 #define G_SETUP_MODELS "ctool::setup_models(GObservations&, std::string&)"
55 #define G_GET_MEAN_POINTING "ctool::get_mean_pointing(GObservations&)" 55 #define G_GET_MEAN_POINTING "ctool::get_mean_pointing(GObservations&)"
56 #define G_CREATE_EBOUNDS "ctool::create_ebounds()" 56 #define G_CREATE_EBOUNDS "ctool::create_ebounds()"
  57 +#define G_CREATE_ENERGIES "ctool::create_energies()"
57 #define G_RESTORE_EDISP "ctool::restore_edisp(GObservations&, "\ 58 #define G_RESTORE_EDISP "ctool::restore_edisp(GObservations&, "\
58 "std::vector<bool>&)" 59 "std::vector<bool>&)"
59 #define G_SET_OBS_RESPONSE "ctool::set_obs_response(GCTAObservation*)" 60 #define G_SET_OBS_RESPONSE "ctool::set_obs_response(GCTAObservation*)"
@@ -680,6 +681,136 @@ GEbounds ctool::create_ebounds(void) @@ -680,6 +681,136 @@ GEbounds ctool::create_ebounds(void)
680 681
681 682
682 /***********************************************************************//** 683 /***********************************************************************//**
  684 + * @brief Create energies from user parameters
  685 + *
  686 + * @exception GException::invalid_value
  687 + * No valid energies extension found or invalid extension
  688 + * name.
  689 + *
  690 + * Get the energy boundaries according to the user parameters. The method
  691 + * supports loading of energy node information from the `ENERGIES`
  692 + * extension, loading energy nodes from energy the boundary extensions
  693 + * `EBOUNDS` or `ENERGYBINS` extension, or setting energy nodes using a
  694 + * linear or logarithmical spacing.
  695 + *
  696 + * The following parameters are read:
  697 + *
  698 + * ebinalg - Energy binning algorithm
  699 + * ebinfile - Name of file with energy binning
  700 + * emin - Minimum energy (if ebinalg != FILE)
  701 + * emax - Maximum energy (if ebinalg != FILE)
  702 + * enumbins - Number of energy bins (if ebinalg != FILE)
  703 + ***************************************************************************/
  704 +GEnergies ctool::create_energies(void)
  705 +{
  706 + // Allocate energies
  707 + GEnergies energies;
  708 +
  709 + // Get energy binning algorithm
  710 + std::string ebinalg = (*this)["ebinalg"].string();
  711 +
  712 + // If energy binning algorithm is of type "FILE" (case sensitive), then
  713 + // read energy boundaries from FITS file ...
  714 + if (ebinalg == "FILE") {
  715 +
  716 + // Get filename
  717 + GFilename ebinfile = (*this)["ebinfile"].filename();
  718 +
  719 + // If no extension name was provided then use default extension names
  720 + if (!ebinfile.has_extname()) {
  721 +
  722 + // Open energy boundary file using the EBOUNDS or ENERGYBINS
  723 + // extension or energy values using the ENERGIES extension.
  724 + // Throw an exception if opening fails.
  725 + GFits file(ebinfile.url());
  726 + if (file.contains(gammalib::extname_energies)) {
  727 + file.close();
  728 + energies.load(ebinfile);
  729 + }
  730 + else if (file.contains(gammalib::extname_ebounds)) {
  731 + file.close();
  732 + energies.set(GEbounds(ebinfile));
  733 + }
  734 + else if (file.contains("ENERGYBINS")) {
  735 + file.close();
  736 + ebinfile = ebinfile.url() + "[ENERGYBINS]";
  737 + energies.set(GEbounds(ebinfile));
  738 + }
  739 + else {
  740 + file.close();
  741 + std::string msg = "No extension with name \""+
  742 + gammalib::extname_energies+"\", "
  743 + "\"ENERGYBINS\" or \""+
  744 + gammalib::extname_ebounds+"\" found in FITS "
  745 + "file \""+ebinfile+"\". Please specify a "
  746 + "valid energy binning file.";
  747 + throw GException::invalid_value(G_CREATE_ENERGIES, msg);
  748 + }
  749 + }
  750 +
  751 + // ... otherwise load energy boundaries from filename including
  752 + // extension
  753 + else {
  754 +
  755 + // Open energy boundary file
  756 + GFits file(ebinfile.url());
  757 +
  758 + // If FITS file does not contain requested extension then throw
  759 + // an exception
  760 + if (!file.contains(ebinfile.extname())) {
  761 + std::string msg = "No extension \""+ebinfile.extname()+"\" "
  762 + "found in energy node file \""+
  763 + ebinfile.url()+"\". Please provide a valid "
  764 + "extension name.";
  765 + throw GException::invalid_value(G_CREATE_ENERGIES, msg);
  766 + }
  767 +
  768 + // Get table extension
  769 + GFitsTable& table = *file.table(ebinfile.extname());
  770 +
  771 + // If table contains one column then load energies, otherwise
  772 + // load energy boundaries.
  773 + if (table.ncols() == 1) {
  774 + file.close();
  775 + energies.load(ebinfile);
  776 + }
  777 + else {
  778 + file.close();
  779 + energies.set(GEbounds(ebinfile));
  780 + }
  781 +
  782 + } // endelse: loaded energy boundaries from table extension
  783 +
  784 + } // endif: ebinalg was "FILE"
  785 +
  786 + // ... otherwise use a linear or a logarithmically-spaced energy binning
  787 + else {
  788 +
  789 + // Get task parameters
  790 + double emin = (*this)["emin"].real();
  791 + double emax = (*this)["emax"].real();
  792 + int enumbins = (*this)["enumbins"].integer();
  793 +
  794 + // Initialise log mode for ebinning
  795 + bool log = true;
  796 +
  797 + // Check if algorithm is linear
  798 + if (ebinalg == "LIN") {
  799 + log = false;
  800 + }
  801 +
  802 + // Setup energy nodes
  803 + energies = GEnergies(enumbins, GEnergy(emin, "TeV"),
  804 + GEnergy(emax, "TeV"), log);
  805 +
  806 + } // endelse: ebinalg was not "FILE"
  807 +
  808 + // Return energy nodes
  809 + return energies;
  810 +}
  811 +
  812 +
  813 +/***********************************************************************//**
683 * @brief Create a skymap from user parameters 814 * @brief Create a skymap from user parameters
684 * 815 *
685 * @param[in] obs Observation container. 816 * @param[in] obs Observation container.
src/support/ctool.hpp
@@ -93,6 +93,7 @@ protected: @@ -93,6 +93,7 @@ protected:
93 93
94 // Protected methods that create objects from user parameters 94 // Protected methods that create objects from user parameters
95 GEbounds create_ebounds(void); 95 GEbounds create_ebounds(void);
  96 + GEnergies create_energies(void);
96 GSkyMap create_map(const GObservations& obs); 97 GSkyMap create_map(const GObservations& obs);
97 GCTAEventCube create_cube(const GObservations& obs); 98 GCTAEventCube create_cube(const GObservations& obs);
98 GCTAObservation create_cta_obs(void); 99 GCTAObservation create_cta_obs(void);