Commit 918adf59f394ab6b33e0bbc75044775de59fa91a

Authored by Jürgen Knödlseder
2 parents 2a8f01d9 7dffa5fb

Merge branch 'integration' into devel

include/GSkyMap.hpp
1 1 /***************************************************************************
2 2 * GSkyMap.hpp - Sky map class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2016 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2017 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -85,6 +85,10 @@ class GVector;
85 85 class GSkyMap : public GBase {
86 86  
87 87 friend GSkyMap sqrt(const GSkyMap& map);
  88 + friend GSkyMap log(const GSkyMap& map);
  89 + friend GSkyMap log10(const GSkyMap& map);
  90 + friend GSkyMap abs(const GSkyMap& map);
  91 + friend GSkyMap sign(const GSkyMap& map);
88 92  
89 93 public:
90 94 // Constructors and destructors
... ...
pyext/GSkyMap.i
... ... @@ -245,6 +245,18 @@ public:
245 245 GSkyMap sqrt() {
246 246 return sqrt(*self);
247 247 }
  248 + GSkyMap log() {
  249 + return log(*self);
  250 + }
  251 + GSkyMap log10() {
  252 + return log10(*self);
  253 + }
  254 + GSkyMap abs() {
  255 + return abs(*self);
  256 + }
  257 + GSkyMap sign() {
  258 + return sign(*self);
  259 + }
248 260 // Python 2.x operator/=
249 261 GSkyMap __idiv__(const GSkyMap& map) {
250 262 self->operator/=(map);
... ...
src/sky/GSkyMap.cpp
... ... @@ -76,6 +76,9 @@
76 76 #define G_READ_HEALPIX "GSkyMap::read_healpix(GFitsTable*)"
77 77 #define G_READ_WCS "GSkyMap::read_wcs(GFitsImage*)"
78 78 #define G_ALLOC_WCS "GSkyMap::alloc_wcs(GFitsImage*)"
  79 +#define G_SQRT "GSkyMap sqrt(GSkyMap&)"
  80 +#define G_LOG "GSkyMap log(GSkyMap&)"
  81 +#define G_LOG10 "GSkyMap log10(GSkyMap&)"
79 82  
80 83 /* __ Macros _____________________________________________________________ */
81 84  
... ... @@ -3019,10 +3022,10 @@ bool GSkyMap::is_wcs(const GFitsHDU& hdu) const
3019 3022 ==========================================================================*/
3020 3023  
3021 3024 /***********************************************************************//**
3022   - * @brief Computes square root of skymap elements
  3025 + * @brief Computes square root of sky map elements
3023 3026 *
3024   - * @param[in] map Skymap.
3025   - * @return Skymap containing the square root of every element.
  3027 + * @param[in] map Sky map.
  3028 + * @return Sky map containing the square root of every element.
3026 3029 ***************************************************************************/
3027 3030 GSkyMap sqrt(const GSkyMap& map)
3028 3031 {
... ... @@ -3038,20 +3041,177 @@ GSkyMap sqrt(const GSkyMap& map)
3038 3041 // Get the content from the bin
3039 3042 double content = map(j,i);
3040 3043  
3041   - // Check if content is not negative
  3044 + // Throw an exception if content is negative
3042 3045 if (content < 0.0) {
3043   - std::string msg = "Negative value envountered."
3044   - " Cannot take the sqrt from a negative value";
3045   - throw GException::invalid_value(G_OP_UNARY_DIV, msg);
  3046 + std::string msg = "Negative value encountered. "
  3047 + "Cannot take the sqrt from a negative "
  3048 + "value";
  3049 + throw GException::invalid_value(G_SQRT, msg);
3046 3050 }
3047 3051  
3048 3052 // Set content of the result map
3049 3053 result(j,i) = std::sqrt(content);
3050 3054  
3051   - } // endfor: Loop over all bins
  3055 + } // endfor: looped over all bins
  3056 +
  3057 + } // endfor: looped over all maps
  3058 +
  3059 + // Return sky map
  3060 + return result;
  3061 +}
  3062 +
  3063 +
  3064 +/***********************************************************************//**
  3065 + * @brief Computes the natural logarithm of sky map elements
  3066 + *
  3067 + * @param[in] map Sky map.
  3068 + * @return Sky map containing the natural logarithm of every element.
  3069 + ***************************************************************************/
  3070 +GSkyMap log(const GSkyMap& map)
  3071 +{
  3072 + // Initialise result vector
  3073 + GSkyMap result(map);
  3074 +
  3075 + // Loop over all maps
  3076 + for (int i = 0; i < map.nmaps(); ++i) {
  3077 +
  3078 + // Loop over all bins
  3079 + for (int j = 0; j < map.npix(); ++j) {
  3080 +
  3081 + // Get the content from the bin
  3082 + double content = map(j,i);
  3083 +
  3084 + // Throw an exception if content is negative
  3085 + if (content <= 0.0) {
  3086 + std::string msg = "Negative or zero value encountered. "
  3087 + "Cannot calculate the logarithm of a "
  3088 + "negative or zero value";
  3089 + throw GException::invalid_value(G_LOG, msg);
  3090 + }
  3091 +
  3092 + // Set content of the result map
  3093 + result(j,i) = std::log(content);
  3094 +
  3095 + } // endfor: looped over all bins
  3096 +
  3097 + } // endfor: looped over all maps
  3098 +
  3099 + // Return sky map
  3100 + return result;
  3101 +}
  3102 +
  3103 +
  3104 +/***********************************************************************//**
  3105 + * @brief Computes the base 10 logarithm of sky map elements
  3106 + *
  3107 + * @param[in] map Sky map.
  3108 + * @return Sky map containing the base 10 logarithm of every element.
  3109 + ***************************************************************************/
  3110 +GSkyMap log10(const GSkyMap& map)
  3111 +{
  3112 + // Initialise result vector
  3113 + GSkyMap result(map);
  3114 +
  3115 + // Loop over all maps
  3116 + for (int i = 0; i < map.nmaps(); ++i) {
  3117 +
  3118 + // Loop over all bins
  3119 + for (int j = 0; j < map.npix(); ++j) {
  3120 +
  3121 + // Get the content from the bin
  3122 + double content = map(j,i);
  3123 +
  3124 + // Throw an exception if content is negative
  3125 + if (content <= 0.0) {
  3126 + std::string msg = "Negative or zero value encountered. "
  3127 + "Cannot calculate the logarithm of a "
  3128 + "negative or zero value";
  3129 + throw GException::invalid_value(G_LOG10, msg);
  3130 + }
  3131 +
  3132 + // Set content of the result map
  3133 + result(j,i) = std::log10(content);
  3134 +
  3135 + } // endfor: looped over all bins
  3136 +
  3137 + } // endfor: looped over all maps
  3138 +
  3139 + // Return sky map
  3140 + return result;
  3141 +}
  3142 +
  3143 +
  3144 +/***********************************************************************//**
  3145 + * @brief Computes the absolute value of sky map elements
  3146 + *
  3147 + * @param[in] map Sky map.
  3148 + * @return Sky map containing the absolute value of every element.
  3149 + ***************************************************************************/
  3150 +GSkyMap abs(const GSkyMap& map)
  3151 +{
  3152 + // Initialise result vector
  3153 + GSkyMap result(map);
  3154 +
  3155 + // Loop over all maps
  3156 + for (int i = 0; i < map.nmaps(); ++i) {
  3157 +
  3158 + // Loop over all bins
  3159 + for (int j = 0; j < map.npix(); ++j) {
  3160 +
  3161 + // Get the content from the bin
  3162 + double content = map(j,i);
  3163 +
  3164 + // Set content of the result map
  3165 + result(j,i) = std::abs(content);
  3166 +
  3167 + } // endfor: looped over all bins
  3168 +
  3169 + } // endfor: looped over all maps
  3170 +
  3171 + // Return sky map
  3172 + return result;
  3173 +}
  3174 +
  3175 +
  3176 +/***********************************************************************//**
  3177 + * @brief Computes the sign value of sky map elements
  3178 + *
  3179 + * @param[in] map Sky map.
  3180 + * @return Sky map containing the sign value of every pixel.
  3181 + *
  3182 + * This method returns a sky map filled with a value of 1 if the pixel
  3183 + * is positive, a value of -1 if the pixel is negative or a value of 0
  3184 + * if the pixel is 0.
  3185 + ***************************************************************************/
  3186 +GSkyMap sign(const GSkyMap& map)
  3187 +{
  3188 + // Initialise result vector
  3189 + GSkyMap result(map);
  3190 +
  3191 + // Loop over all maps
  3192 + for (int i = 0; i < map.nmaps(); ++i) {
  3193 +
  3194 + // Loop over all bins
  3195 + for (int j = 0; j < map.npix(); ++j) {
  3196 +
  3197 + // Get the content from the bin
  3198 + double content = map(j,i);
  3199 +
  3200 + // Handle the 3 cases
  3201 + if (content < 0.0) {
  3202 + result(j,i) = -1.0;
  3203 + }
  3204 + else if (content > 0.0) {
  3205 + result(j,i) = +1.0;
  3206 + }
  3207 + else {
  3208 + result(j,i) = 0.0;
  3209 + }
  3210 +
  3211 + } // endfor: looped over all bins
3052 3212  
3053   - } // endfor: Loop over all maps
  3213 + } // endfor: looed over all maps
3054 3214  
3055   - // Return vector
  3215 + // Return sky map
3056 3216 return result;
3057 3217 }
... ...
test/test_GSky.py
... ... @@ -72,6 +72,7 @@ class Test(gammalib.GPythonTestSuite):
72 72  
73 73 # Append tests
74 74 self.append(self.test_methods, "Test sky map methods")
  75 + self.append(self.test_friend_methods, "Test sky map friend methods")
75 76 self.append(self.test_operators, "Test sky map operators")
76 77 self.append(self.test_skymap_healpix, "Test HEALPix map")
77 78 self.append(self.test_skymap_ait, "Test AIT projection map")
... ... @@ -123,7 +124,35 @@ class Test(gammalib.GPythonTestSuite):
123 124  
124 125 # Return
125 126 return
126   -
  127 +
  128 + # Test sky map friend methods
  129 + def test_friend_methods(self):
  130 + """
  131 + Test the sky map friend methods
  132 + """
  133 + # Setup sky maps
  134 + m = gammalib.GSkyMap('CAR', 'CEL', 83.63, 22.01, -3.7, 2.6, 10, 8, 12)
  135 +
  136 + # Fill all values of skymaps with numbers > 0
  137 + values = range(1,m.npix()+1)
  138 + for j in range(m.nmaps()):
  139 + for i in range(m.npix()):
  140 + m[i,j] = values[i]
  141 +
  142 + # Test map values after calling friend methods on one pixel
  143 + ipix = 18
  144 + self.test_value(m.sqrt()[ipix,0], values[ipix]**0.5, 'Check sqrt method')
  145 + self.test_value(m.log()[ipix,0], math.log(values[ipix]), 'Check log method')
  146 + self.test_value(m.log10()[ipix,0], math.log10(values[ipix]), 'Check log10 method')
  147 +
  148 + # Set the interesting value to a negative value
  149 + m[ipix,0] = -values[ipix]
  150 + self.test_value(m.abs()[ipix,0], values[ipix], 'Check abs method')
  151 + self.test_value(m.sign()[ipix,0], -1, 'Check sign method')
  152 +
  153 + # Return
  154 + return
  155 +
127 156 # Test sky map operators
128 157 def test_operators(self):
129 158 """
... ...