00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <iostream>
00014 #include <string.h>
00015 #include <stdio.h>
00016 extern "C"
00017 {
00018 #include <chealpix.h>
00019 };
00020 #include "healpixmap.h"
00021 #include "controldialog.h"
00022 #include "str_funcs.h"
00023
00024 using namespace std;
00025
00026
00027
00028 static double pi = 3.141592653589793;
00029 static double deg2rad = pi / 360.0;
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 void HealpixMap::readFITSPrimaryHeader (fitsfile* fptr)
00047 {
00048 char tmp[24], comm[80];
00049 int status = 0;
00050 Skymap::readFITSPrimaryHeader(fptr);
00051 if (fits_read_keyword(fptr, "ORDERING", tmp, comm, &status) != 0)
00052 return;
00053 fits_str_cull(tmp);
00054 if (strncmp(tmp, "RING", 4) == 0) ordering_ = Ring;
00055 else ordering_ = Nested;
00056 return;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 void HealpixMap::writeFITSPrimaryHeader (fitsfile* fptr)
00073 {
00074 char stmp[80], comm[80];
00075 unsigned int itmp;
00076 int status = 0;
00077
00078 if (ordering_ == Undefined) throw MapException(MapException::Undefined);
00079
00080 Skymap::writeFITSPrimaryHeader(fptr);
00081
00082 strcpy(comm, "Pixel algorithm");
00083 strcpy(stmp, "HEALPIX");
00084 fits_write_key(fptr, TSTRING, "PIXTYPE", stmp, comm, &status);
00085
00086 strcpy(comm, "Ordering scheme");
00087 strcpy(stmp, ordering());
00088 fits_write_key(fptr, TSTRING, "ORDERING", stmp, comm, &status);
00089
00090 strcpy(comm, "Resolution index");
00091 itmp = NSide2Res(nside());
00092 fits_write_key(fptr, TUINT, "RESOLUTN", &itmp, comm, &status);
00093
00094 strcpy(comm, "Resolution parameter");
00095 itmp = nside();
00096 fits_write_key(fptr, TUINT, "NSIDE", &itmp, comm, &status);
00097
00098 strcpy(comm, "First pixel index (0 based)");
00099 itmp = 0;
00100 fits_write_key(fptr, TUINT, "FIRSTPIX", &itmp, comm, &status);
00101
00102 strcpy(comm, "Last pixel index (0 based)");
00103 itmp = size() - 1;
00104 fits_write_key(fptr, TUINT, "LASTPIX", &itmp, comm, &status);
00105
00106 return;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 void HealpixMap::readFITSExtensionHeader (fitsfile* fptr)
00122 {
00123 char tmp[24], comm[80];
00124 int status = 0;
00125 Skymap::readFITSExtensionHeader(fptr);
00126 if (fits_read_keyword(fptr, "ORDERING", tmp, comm, &status) != 0)
00127 return;
00128 fits_str_cull(tmp);
00129 if (strncmp(tmp, "RING", 4) == 0) ordering_ = Ring;
00130 else ordering_ = Nested;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 void HealpixMap::writeFITSExtensionHeader (fitsfile* fptr)
00146 {
00147 char stmp[80], comm[80];
00148 unsigned int itmp;
00149 int status = 0;
00150
00151 Skymap::writeFITSExtensionHeader(fptr);
00152
00153 strcpy(comm, "Pixel algorithm");
00154 strcpy(stmp, "HEALPIX");
00155 fits_write_key(fptr, TSTRING, "PIXTYPE", stmp, comm, &status);
00156
00157 strcpy(comm, "Ordering scheme");
00158 strcpy(stmp, ordering());
00159 fits_write_key(fptr, TSTRING, "ORDERING", stmp, comm, &status);
00160
00161 strcpy(comm, "Resolution index");
00162 itmp = NSide2Res(nside());
00163 fits_write_key(fptr, TUINT, "RESOLUTN", &itmp, comm, &status);
00164
00165 strcpy(comm, "Resolution parameter");
00166 itmp = nside();
00167 fits_write_key(fptr, TUINT, "NSIDE", &itmp, comm, &status);
00168
00169 strcpy(comm, "First pixel index (0 based)");
00170 itmp = 0;
00171 fits_write_key(fptr, TUINT, "FIRSTPIX", &itmp, comm, &status);
00172
00173 strcpy(comm, "Last pixel index (0 based)");
00174 itmp = size() - 1;
00175 fits_write_key(fptr, TUINT, "LASTPIX", &itmp, comm, &status);
00176
00177 return;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 unsigned int HealpixMap::degrade_pixindex (unsigned int i, unsigned int nsi,
00196 unsigned int nso)
00197 {
00198 long ti, tj;
00199 double scl;
00200 if (ordering_ == Undefined) throw MapException(MapException::Undefined);
00201 if (ordering_ == Ring) ::ring2nest(long(nsi), long(i), &ti);
00202 else ti = long(i);
00203 scl = double(nso) / double(nsi);
00204 tj = long(double(ti) * scl * scl);
00205 if (ordering_ == Ring) ::nest2ring(long(nso), tj, &tj);
00206 return ((unsigned int) tj);
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 void HealpixMap::degrade_map (unsigned int ns)
00220 {
00221 BasePixel *arr;
00222 unsigned int i, j;
00223 unsigned int nnew = NSide2NPix(ns);
00224 unsigned int *cnt;
00225
00226
00227
00228 switch (type())
00229 {
00230 case TPix:
00231 arr = new TPixel[nnew];
00232 if (arr == NULL) throw MapException(MapException::Memory);
00233 break;
00234 case PPix:
00235 arr = new TPPixel[nnew];
00236 if (arr == NULL) throw MapException(MapException::Memory);
00237 break;
00238 case TnobsPix:
00239 arr = new TnobsPixel[nnew];
00240 if (arr == NULL) throw MapException(MapException::Memory);
00241 break;
00242 case TPnobsPix:
00243 arr = new TPnobsPixel[nnew];
00244 if (arr == NULL) throw MapException(MapException::Memory);
00245 break;
00246 default:
00247 throw MapException(MapException::InvalidType);
00248 break;
00249 }
00250 if ((cnt = new unsigned int[nnew]) == NULL)
00251 throw MapException(MapException::InvalidType);
00252 for (i = 0; i < nnew; i++) cnt[i] = 0;
00253
00254
00255
00256 for (j = 0; j < size(); j++)
00257 {
00258 i = degrade_pixindex(j, nside_, ns);
00259 if (i >= nnew) break;
00260 cnt[i] += 1;
00261 try { arr[i].T() += (*this)[j].T(); } catch (MapException &exc) { ; }
00262 try { arr[i].Nobs() += (*this)[j].Nobs(); } catch (MapException &exc) { ; }
00263 try { arr[i].Q() += (*this)[j].Q(); } catch (MapException &exc) { ; }
00264 try { arr[i].U() += (*this)[j].U(); } catch (MapException &exc) { ; }
00265 }
00266
00267
00268
00269 for (i = 0; i < nnew; i++)
00270 {
00271 try { arr[i].T() /= double(cnt[i]); } catch (MapException &exc) { ; }
00272 try { arr[i].Q() /= double(cnt[i]); } catch (MapException &exc) { ; }
00273 try { arr[i].U() /= double(cnt[i]); } catch (MapException &exc) { ; }
00274 }
00275
00276
00277
00278
00279 delete [] cnt;
00280 switch (type())
00281 {
00282 case TPix:
00283 if (tpix != 0) delete [] tpix;
00284 tpix = (TPixel*) arr;
00285 break;
00286 case PPix:
00287 if (tppix != 0) delete [] tppix;
00288 tppix = (TPPixel*) arr;
00289 break;
00290 case TnobsPix:
00291 if (tnobspix != 0) delete [] tnobspix;
00292 tnobspix = (TnobsPixel*) arr;
00293 break;
00294 case TPnobsPix:
00295 if (tpnobspix != 0) delete [] tpnobspix;
00296 tpnobspix = (TPnobsPixel*) arr;
00297 break;
00298 default:
00299 throw MapException(MapException::InvalidType);
00300 break;
00301 }
00302 nside_ = ns;
00303 n_ = nnew;
00304 return;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 void HealpixMap::upgrade_map (unsigned int ns)
00318 {
00319 BasePixel *arr;
00320 unsigned int i, j;
00321 unsigned int nnew = NSide2NPix(ns);
00322 double scl;
00323
00324
00325
00326 switch (type())
00327 {
00328 case TPix:
00329 arr = new TPixel[nnew];
00330 if (arr == NULL) throw MapException(MapException::Memory);
00331 break;
00332 case PPix:
00333 arr = new TPPixel[nnew];
00334 if (arr == NULL) throw MapException(MapException::Memory);
00335 break;
00336 case TnobsPix:
00337 arr = new TnobsPixel[nnew];
00338 if (arr == NULL) throw MapException(MapException::Memory);
00339 break;
00340 case TPnobsPix:
00341 arr = new TPnobsPixel[nnew];
00342 if (arr == NULL) throw MapException(MapException::Memory);
00343 break;
00344 default:
00345 throw MapException(MapException::InvalidType);
00346 break;
00347 }
00348
00349
00350
00351 scl = double(nnew) / double(size());
00352 for (j = 0; j < nnew; j++)
00353 {
00354 i = degrade_pixindex(j, ns, nside_);
00355 i = (unsigned int)(double(j) / scl);
00356 if (i >= size()) break;
00357 try { arr[j].T() = (*this)[i].T(); } catch (MapException &exc) { ; }
00358 try { arr[j].Nobs() = (*this)[i].Nobs(); } catch (MapException &exc) { ; }
00359 try { arr[j].Q() = (*this)[i].Q(); } catch (MapException &exc) { ; }
00360 try { arr[j].U() = (*this)[i].U(); } catch (MapException &exc) { ; }
00361 }
00362
00363
00364
00365
00366 switch (type())
00367 {
00368 case TPix:
00369 if (tpix != 0) delete [] tpix;
00370 tpix = (TPixel*) arr;
00371 break;
00372 case PPix:
00373 if (tppix != 0) delete [] tppix;
00374 tppix = (TPPixel*) arr;
00375 break;
00376 case TnobsPix:
00377 if (tnobspix != 0) delete [] tnobspix;
00378 tnobspix = (TnobsPixel*) arr;
00379 break;
00380 case TPnobsPix:
00381 if (tpnobspix != 0) delete [] tpnobspix;
00382 tpnobspix = (TPnobsPixel*) arr;
00383 break;
00384 default:
00385 throw MapException(MapException::InvalidType);
00386 break;
00387 }
00388 nside_ = ns;
00389 n_ = nnew;
00390 return;
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 HealpixMap::HealpixMap () : Skymap()
00402 {
00403 nside_ = 0;
00404 ordering_ = Undefined;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 HealpixMap::HealpixMap (unsigned int n_in, Type type_in, PixOrder ord) :
00418 Skymap(n_in, type_in)
00419 {
00420 nside_ = NPix2NSide(n_in);
00421 ordering_ = ord;
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 HealpixMap::~HealpixMap ()
00433 {
00434 ;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 void HealpixMap::copy(HealpixMap &imap)
00446 {
00447 Skymap::copy(imap);
00448 nside_ = imap.nside_;
00449 ordering_ = imap.ordering_;
00450
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 const char* HealpixMap::ordering () const
00464 {
00465 static char str[12];
00466 if (ordering_ == Ring ) strcpy(str, "RING");
00467 if (ordering_ == Nested) strcpy(str, "NESTED");
00468 else strcpy(str, "UNDEFINED");
00469 return str;
00470 }
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484 void HealpixMap::pixel2vector (long pix, double *vector)
00485 {
00486 switch (ordering_)
00487 {
00488 case Nested:
00489 ::pix2vec_nest(long(nside_), pix, vector);
00490 break;
00491 case Ring:
00492 ::pix2vec_ring(long(nside_), pix, vector);
00493 break;
00494 default:
00495 throw MapException(MapException::Undefined);
00496 break;
00497 }
00498 return;
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 void HealpixMap::vector2pixel (double *vector, long &pix)
00514 {
00515 pix = 0;
00516 switch (ordering_)
00517 {
00518 case Nested:
00519 ::vec2pix_nest(long(nside_), vector, &pix);
00520 break;
00521 case Ring:
00522 ::vec2pix_ring(long(nside_), vector, &pix);
00523 break;
00524 default:
00525 throw MapException(MapException::Undefined);
00526 break;
00527 }
00528 return;
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 void HealpixMap::pixel2angles (long pix, double &theta, double &phi, int deg)
00549 {
00550 theta = phi = 0.0;
00551 switch (ordering_)
00552 {
00553 case Nested:
00554 ::pix2ang_nest(long(nside_), pix, &theta, &phi);
00555 break;
00556 case Ring:
00557 ::pix2ang_ring(long(nside_), pix, &theta, &phi);
00558 break;
00559 default:
00560 throw MapException(MapException::Undefined);
00561 break;
00562 }
00563 if (deg != 0)
00564 {
00565 theta /= deg2rad;
00566 phi /= deg2rad;
00567 }
00568 return;
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 void HealpixMap::angles2pixel (double theta, double phi, long &pix, int deg)
00589 {
00590 pix = 0;
00591 if (deg != 0)
00592 {
00593 theta *= deg2rad;
00594 phi *= deg2rad;
00595 }
00596 switch (ordering_)
00597 {
00598 case Nested:
00599 ::ang2pix_nest(long(nside_), theta, phi, &pix);
00600 break;
00601 case Ring:
00602 ::ang2pix_ring(long(nside_), theta, phi, &pix);
00603 break;
00604 default:
00605 throw MapException(MapException::Undefined);
00606 break;
00607 }
00608 return;
00609 }
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 long HealpixMap::pix2ordering (long ipix, PixOrder dord)
00625 {
00626 long opix = 0;
00627 if ((dord == Undefined) || (ordering_ == Undefined))
00628 throw MapException(MapException::Undefined);
00629 if (dord == ordering_) return ipix;
00630 switch (ordering_)
00631 {
00632 case Nested:
00633 ::nest2ring(long(nside_), ipix, &opix);
00634 break;
00635 case Ring:
00636 ::ring2nest(long(nside_), ipix, &opix);
00637 break;
00638 default:
00639 throw MapException(MapException::Undefined);
00640 break;
00641 }
00642 return opix;
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 void HealpixMap::resize (unsigned int ns)
00656 {
00657 if (ordering_ == Undefined) throw MapException(MapException::Undefined);
00658 if (ns == nside_) return;
00659 if (ns > nside_) upgrade_map(ns);
00660 else degrade_map(ns);
00661 return;
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 BasePixel& HealpixMap::getPixel (double theta, double phi, int deg)
00695 {
00696 long pix;
00697 angles2pixel(theta, phi, pix, deg);
00698 return (*this)[pix];
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712 BasePixel& HealpixMap::getPixel (double *vector)
00713 {
00714 long pix;
00715 vector2pixel(vector, pix);
00716 return (*this)[pix];
00717 }
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760 void HealpixMap::readFITS(const char* filename, ControlDialog *progwin)
00761 {
00762 Skymap::readFITS(filename, progwin);
00763 nside_ = NPix2NSide(size());
00764 if (progwin != NULL) progwin->loadNSide(nside_, ordering_);
00765 }
00766 void HealpixMap::readFITS(string filename, ControlDialog *progwin)
00767 {
00768 readFITS(filename.c_str(), progwin);
00769 if (progwin != NULL) progwin->loadNSide(nside_, ordering_);
00770 }
00771 void HealpixMap::readFITS(QString filename, ControlDialog *progwin)
00772 {
00773 readFITS(filename.toStdString(), progwin);
00774 if (progwin != NULL) progwin->loadNSide(nside_, ordering_);
00775 }