Commit 1eb6a5c9a65c0d140ebd5529ce5c8feac3ec840e
1 parent
98b5d10c
start load method implementation
Showing
2 changed files
with
171 additions
and
4 deletions
include/GHealpix.hpp
| @@ -20,6 +20,7 @@ | @@ -20,6 +20,7 @@ | ||
| 20 | #define GHEALPIX_HPP | 20 | #define GHEALPIX_HPP |
| 21 | 21 | ||
| 22 | /* __ Includes ___________________________________________________________ */ | 22 | /* __ Includes ___________________________________________________________ */ |
| 23 | +#include "GFitsHDU.hpp" | ||
| 23 | 24 | ||
| 24 | /* __ Namespaces _________________________________________________________ */ | 25 | /* __ Namespaces _________________________________________________________ */ |
| 25 | 26 | ||
| @@ -41,16 +42,27 @@ public: | @@ -41,16 +42,27 @@ public: | ||
| 41 | GHealpix& operator= (const GHealpix& pixels); | 42 | GHealpix& operator= (const GHealpix& pixels); |
| 42 | 43 | ||
| 43 | // Methods | 44 | // Methods |
| 45 | + void load(const GFitsHDU* hdu); | ||
| 46 | + int nside(void) const; | ||
| 47 | + int num_pixels(void) const; | ||
| 48 | + double omega(void) const; | ||
| 44 | 49 | ||
| 45 | -protected: | ||
| 46 | - // Protected methods | 50 | +private: |
| 51 | + // Private methods | ||
| 47 | void init_members(void); | 52 | void init_members(void); |
| 48 | void copy_members(const GHealpix& pixels); | 53 | void copy_members(const GHealpix& pixels); |
| 49 | void free_members(void); | 54 | void free_members(void); |
| 50 | GHealpix* clone(void) const; | 55 | GHealpix* clone(void) const; |
| 51 | 56 | ||
| 52 | - // Protected data area | ||
| 53 | -private: | 57 | + // Private data area |
| 58 | + int m_nside; //!< Number of divisions of each base pixel (1,2,..) | ||
| 59 | + int m_order; //!< Ordering (0=ring, 1=nested) | ||
| 60 | + int m_coordsys; //!< Coordinate system (0=equatorial, 1=galactic) | ||
| 61 | + int m_num_pixels; //!< Number of pixels | ||
| 62 | + int m_size_pixels; //!< Vector size of each pixel | ||
| 63 | + double* m_pixels; //!< Pixel array | ||
| 64 | + double* m_lon; //!< RA or GLON values for each pixel | ||
| 65 | + double* m_lat; //!< DEC or GLAT values for each pixel | ||
| 54 | }; | 66 | }; |
| 55 | 67 | ||
| 56 | #endif /* GHEALPIX_HPP */ | 68 | #endif /* GHEALPIX_HPP */ |
src/GHealpix.cpp
| @@ -19,6 +19,7 @@ | @@ -19,6 +19,7 @@ | ||
| 19 | 19 | ||
| 20 | /* __ Includes ___________________________________________________________ */ | 20 | /* __ Includes ___________________________________________________________ */ |
| 21 | #include "GException.hpp" | 21 | #include "GException.hpp" |
| 22 | +#include "GTools.hpp" | ||
| 22 | #include "GHealpix.hpp" | 23 | #include "GHealpix.hpp" |
| 23 | 24 | ||
| 24 | /* __ Namespaces _________________________________________________________ */ | 25 | /* __ Namespaces _________________________________________________________ */ |
| @@ -121,6 +122,124 @@ GHealpix& GHealpix::operator= (const GHealpix& pixels) | @@ -121,6 +122,124 @@ GHealpix& GHealpix::operator= (const GHealpix& pixels) | ||
| 121 | = = | 122 | = = |
| 122 | ==========================================================================*/ | 123 | ==========================================================================*/ |
| 123 | 124 | ||
| 125 | +/***********************************************************************//** | ||
| 126 | + * @brief Load Healpix data from table. | ||
| 127 | + * | ||
| 128 | + * @param hdu FITS HDU containing the Healpix data | ||
| 129 | + ***************************************************************************/ | ||
| 130 | +void GHealpix::load(const GFitsHDU* hdu) | ||
| 131 | +{ | ||
| 132 | + // Free memory and initialise members | ||
| 133 | + free_members(); | ||
| 134 | + init_members(); | ||
| 135 | + | ||
| 136 | + // Get HDU header | ||
| 137 | + GFitsHeader* hdr = hdu->header(); | ||
| 138 | + if (hdr == NULL) | ||
| 139 | + throw; | ||
| 140 | + | ||
| 141 | + // Check if we have a healpix representation | ||
| 142 | + if (hdr->card("PIXTYPE")->string() != "HEALPIX") | ||
| 143 | + throw; | ||
| 144 | + | ||
| 145 | + // Get Healpix resolution and determine number of pixels | ||
| 146 | + m_nside = hdr->card("NSIDE")->integer(); | ||
| 147 | + m_num_pixels = 12*m_nside*m_nside; | ||
| 148 | + | ||
| 149 | + // Get ordering | ||
| 150 | + try { | ||
| 151 | + std::string ordering = hdr->card("ORDERING")->string(); | ||
| 152 | + if (ordering == "RING") | ||
| 153 | + m_order = 0; | ||
| 154 | + else if (ordering == "NESTED") | ||
| 155 | + m_order = 1; | ||
| 156 | + else | ||
| 157 | + throw; | ||
| 158 | + } | ||
| 159 | + catch (exception &e) { | ||
| 160 | + m_order = 1; // Default is NESTED | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + // Get coordinate system | ||
| 164 | + try { | ||
| 165 | + std::string coordsys = hdr->card("COORDSYS")->string(); | ||
| 166 | + if (coordsys == "EQUATORIAL") | ||
| 167 | + m_coordsys = 0; | ||
| 168 | + else if (coordsys == "GALACTIC") | ||
| 169 | + m_coordsys = 1; | ||
| 170 | + else | ||
| 171 | + throw; | ||
| 172 | + } | ||
| 173 | + catch (exception &e) { | ||
| 174 | + m_coordsys = 0; // Default is EQUATORIAL | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + // Continue only of we have pixels | ||
| 178 | + if (m_num_pixels > 0) { | ||
| 179 | + | ||
| 180 | + // Get first column | ||
| 181 | + GFitsTableCol* col = hdu->column(1); | ||
| 182 | + | ||
| 183 | + // Check column consistency | ||
| 184 | + if (col->length() != m_num_pixels) | ||
| 185 | + throw; | ||
| 186 | + | ||
| 187 | + // Extract vector size of each pixel | ||
| 188 | + m_size_pixels = col->number(); | ||
| 189 | + | ||
| 190 | + // If there are pixels then load them | ||
| 191 | + int size = m_num_pixels * m_size_pixels; | ||
| 192 | + if (size > 0) { | ||
| 193 | + m_pixels = new double[size]; | ||
| 194 | + double* ptr = m_pixels; | ||
| 195 | + for (int row = 0; row < m_num_pixels; ++row) { | ||
| 196 | + for (int inx = 0; inx < m_size_pixels; ++inx, ++ptr) | ||
| 197 | + *ptr = col->real(row,inx); | ||
| 198 | + } | ||
| 199 | + } // endif: there were pixels to load | ||
| 200 | + | ||
| 201 | + // Calculate longitude and latitude for each pixel | ||
| 202 | + | ||
| 203 | + } // endif: we had pixels | ||
| 204 | + | ||
| 205 | + // Return | ||
| 206 | + return; | ||
| 207 | +} | ||
| 208 | + | ||
| 209 | + | ||
| 210 | + | ||
| 211 | +/***********************************************************************//** | ||
| 212 | + * @brief Returns number of divisions of the side of each base pixel. | ||
| 213 | + ***************************************************************************/ | ||
| 214 | +int GHealpix::nside(void) const | ||
| 215 | +{ | ||
| 216 | + // Return nside | ||
| 217 | + return m_nside; | ||
| 218 | +} | ||
| 219 | + | ||
| 220 | + | ||
| 221 | +/***********************************************************************//** | ||
| 222 | + * @brief Returns number of pixels. | ||
| 223 | + ***************************************************************************/ | ||
| 224 | +int GHealpix::num_pixels(void) const | ||
| 225 | +{ | ||
| 226 | + // Return nside | ||
| 227 | + return m_num_pixels; | ||
| 228 | +} | ||
| 229 | + | ||
| 230 | + | ||
| 231 | +/***********************************************************************//** | ||
| 232 | + * @brief Returns solid angle of pixel | ||
| 233 | + ***************************************************************************/ | ||
| 234 | +double GHealpix::omega(void) const | ||
| 235 | +{ | ||
| 236 | + // Set solid angle | ||
| 237 | + double omega = fourpi / m_num_pixels; | ||
| 238 | + | ||
| 239 | + // Return solid angle | ||
| 240 | + return omega; | ||
| 241 | +} | ||
| 242 | + | ||
| 124 | 243 | ||
| 125 | /*========================================================================== | 244 | /*========================================================================== |
| 126 | = = | 245 | = = |
| @@ -134,6 +253,14 @@ GHealpix& GHealpix::operator= (const GHealpix& pixels) | @@ -134,6 +253,14 @@ GHealpix& GHealpix::operator= (const GHealpix& pixels) | ||
| 134 | void GHealpix::init_members(void) | 253 | void GHealpix::init_members(void) |
| 135 | { | 254 | { |
| 136 | // Initialise members | 255 | // Initialise members |
| 256 | + m_nside = 0; | ||
| 257 | + m_order = 0; | ||
| 258 | + m_coordsys = 0; | ||
| 259 | + m_num_pixels = 0; | ||
| 260 | + m_size_pixels = 0; | ||
| 261 | + m_pixels = NULL; | ||
| 262 | + m_lon = NULL; | ||
| 263 | + m_lat = NULL; | ||
| 137 | 264 | ||
| 138 | // Return | 265 | // Return |
| 139 | return; | 266 | return; |
| @@ -148,6 +275,28 @@ void GHealpix::init_members(void) | @@ -148,6 +275,28 @@ void GHealpix::init_members(void) | ||
| 148 | void GHealpix::copy_members(const GHealpix& pixels) | 275 | void GHealpix::copy_members(const GHealpix& pixels) |
| 149 | { | 276 | { |
| 150 | // Copy attributes | 277 | // Copy attributes |
| 278 | + m_nside = pixels.m_nside; | ||
| 279 | + m_order = pixels.m_order; | ||
| 280 | + m_coordsys = pixels.m_coordsys; | ||
| 281 | + m_num_pixels = pixels.m_num_pixels; | ||
| 282 | + m_size_pixels = pixels.m_size_pixels; | ||
| 283 | + | ||
| 284 | + // Copy arrays | ||
| 285 | + if (m_num_pixels > 0) { | ||
| 286 | + if (m_size_pixels > 0 && pixels.m_pixels != NULL) { | ||
| 287 | + int size = m_num_pixels*m_size_pixels; | ||
| 288 | + m_pixels = new double[size]; | ||
| 289 | + memcpy(m_pixels, pixels.m_pixels, size*sizeof(double)); | ||
| 290 | + } | ||
| 291 | + if (pixels.m_lon != NULL) { | ||
| 292 | + m_lon = new double[m_num_pixels]; | ||
| 293 | + memcpy(m_lon, pixels.m_lon, m_num_pixels*sizeof(double)); | ||
| 294 | + } | ||
| 295 | + if (pixels.m_lat != NULL) { | ||
| 296 | + m_lat = new double[m_num_pixels]; | ||
| 297 | + memcpy(m_lat, pixels.m_lat, m_num_pixels*sizeof(double)); | ||
| 298 | + } | ||
| 299 | + } | ||
| 151 | 300 | ||
| 152 | // Return | 301 | // Return |
| 153 | return; | 302 | return; |
| @@ -160,8 +309,14 @@ void GHealpix::copy_members(const GHealpix& pixels) | @@ -160,8 +309,14 @@ void GHealpix::copy_members(const GHealpix& pixels) | ||
| 160 | void GHealpix::free_members(void) | 309 | void GHealpix::free_members(void) |
| 161 | { | 310 | { |
| 162 | // Free memory | 311 | // Free memory |
| 312 | + if (m_pixels != NULL) delete [] m_pixels; | ||
| 313 | + if (m_lon != NULL) delete [] m_lon; | ||
| 314 | + if (m_lat != NULL) delete [] m_lat; | ||
| 163 | 315 | ||
| 164 | // Mark memory as free | 316 | // Mark memory as free |
| 317 | + m_pixels = NULL; | ||
| 318 | + m_lon = NULL; | ||
| 319 | + m_lat = NULL; | ||
| 165 | 320 | ||
| 166 | // Return | 321 | // Return |
| 167 | return; | 322 | return; |