| 1 | #include <iostream>
|
|---|
| 2 | #include <math.h>
|
|---|
| 3 | #include "sopnamsp.h"
|
|---|
| 4 | #include "sambainit.h"
|
|---|
| 5 | #include "skymap.h"
|
|---|
| 6 |
|
|---|
| 7 | #include "tod.h"
|
|---|
| 8 | #include "fitstarray.h"
|
|---|
| 9 | #include "fitsspherehealpix.h"
|
|---|
| 10 | #include "nbrandom.h"
|
|---|
| 11 |
|
|---|
| 12 | #include "timing.h"
|
|---|
| 13 |
|
|---|
| 14 | template <class T>
|
|---|
| 15 | void MeanSig(PixelMap<T> const & map, double& gmoy, double& gsig)
|
|---|
| 16 | {
|
|---|
| 17 | gmoy=0.;
|
|---|
| 18 | gsig = 0.;
|
|---|
| 19 | double valok;
|
|---|
| 20 | for(int k=0; k<map.NbPixels(); k++) {
|
|---|
| 21 | valok = map(k);
|
|---|
| 22 | gmoy += valok; gsig += valok*valok;
|
|---|
| 23 | }
|
|---|
| 24 | gmoy /= (double)map.NbPixels();
|
|---|
| 25 | gsig = gsig/(double)map.NbPixels() - gmoy*gmoy;
|
|---|
| 26 | if (gsig >= 0.) gsig = sqrt(gsig);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | template <class T>
|
|---|
| 30 | void Project_Mol(PixelMap<T> const & map, TMatrix<T> & mtx, T defval=-999.)
|
|---|
| 31 | {
|
|---|
| 32 | r_8 xa, yd, teta,phi, facteur;
|
|---|
| 33 | int_4 l,c,k;
|
|---|
| 34 | int_4 nl = mtx.NRows();
|
|---|
| 35 | int_4 nc = mtx.NCols();
|
|---|
| 36 | mtx = defval; // On met tout a defval
|
|---|
| 37 | cout << " NRows= " << nl << " NCols= " << nc << endl;
|
|---|
| 38 | for(l=0; l<nl; l++) {
|
|---|
| 39 | yd = (r_8)(l+0.5)/(r_8)nl-0.5;
|
|---|
| 40 | facteur=2.*M_PI/sin(acos((double)yd*2));
|
|---|
| 41 | teta = (yd+0.5)*Pi;
|
|---|
| 42 | // teta = (0.5-yd)*M_PI;
|
|---|
| 43 | for(c=0; c<nc; c++) {
|
|---|
| 44 | xa = (r_8)(c+0.5)/(r_8)nc-0.5;
|
|---|
| 45 | phi = xa*facteur+M_PI;
|
|---|
| 46 | if ( (phi <= 2*M_PI) && (phi >= 0.) ) {
|
|---|
| 47 | k = map.PixIndexSph(teta, phi);
|
|---|
| 48 | mtx(l,c) = map(k);
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | int main(int narg, char* arg[])
|
|---|
| 56 | {
|
|---|
| 57 | double teta,phi;
|
|---|
| 58 | double gmoy=0., gsig=0.;
|
|---|
| 59 |
|
|---|
| 60 | SophyaInit();
|
|---|
| 61 | InitTim(); // Initializing the CPU timer
|
|---|
| 62 | if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) {
|
|---|
| 63 | cout << " tspm [HEALPix_M=32] : HEALPix Spherical Map Test " << endl;
|
|---|
| 64 | exit(0);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | int m=32;
|
|---|
| 68 | if (narg >1) m = atoi(arg[1]);
|
|---|
| 69 | cout << " ===== HEALPix Spherical Map Test M= " << m << endl;
|
|---|
| 70 |
|
|---|
| 71 | try {
|
|---|
| 72 |
|
|---|
| 73 | POutPersist s("spheres.ppf");
|
|---|
| 74 | string nomobj;
|
|---|
| 75 |
|
|---|
| 76 | {
|
|---|
| 77 | SphereHEALPix<double> sph(m);
|
|---|
| 78 |
|
|---|
| 79 | cout << "Filling spherical map NPixels= " << sph.NbPixels() << endl;
|
|---|
| 80 | for (int j=0;j<sph.NbPixels();j++)
|
|---|
| 81 | {
|
|---|
| 82 | sph.PixThetaPhi(j,teta,phi);
|
|---|
| 83 | sph(j)= 0.2* cos(3.*teta)*sin(8*phi);
|
|---|
| 84 | }
|
|---|
| 85 | PrtTim("End of Fill ");
|
|---|
| 86 |
|
|---|
| 87 | {
|
|---|
| 88 | FIO_SphereHEALPix<double> fiog(&sph) ;
|
|---|
| 89 | nomobj = "sphg1";
|
|---|
| 90 | fiog.Write(s, nomobj);
|
|---|
| 91 | cout << "SphMap SphereHEALPix<double> written to POutPersist with name " << nomobj << endl;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | TMatrix<double> mtx(3*m, 6*m);
|
|---|
| 96 | cout << " Project_Mol(sph, mtx) " << endl;
|
|---|
| 97 | Project_Mol(sph, mtx);
|
|---|
| 98 | {
|
|---|
| 99 | cout << " Writing to FITS prjmol1.fits " << endl;
|
|---|
| 100 | FITS_TArray<double> fios(mtx);
|
|---|
| 101 | fios.Write("prjmol1.fits");
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | // Computing mean and sigma on the sphere
|
|---|
| 105 | MeanSig(sph, gmoy, gsig);
|
|---|
| 106 | cout << "SphMap Mean= " << gmoy << " Sigma = " << gsig << endl;
|
|---|
| 107 | PrtTim("End of Mean-Sig ");
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | {
|
|---|
| 111 | SphereHEALPix<float> sph(m);
|
|---|
| 112 |
|
|---|
| 113 | cout << "Filling spherical map2 NPixels= " << sph.NbPixels() << endl;
|
|---|
| 114 | for (int j=0;j<sph.NbPixels();j++)
|
|---|
| 115 | {
|
|---|
| 116 | sph.PixThetaPhi(j,teta,phi);
|
|---|
| 117 | if (teta < 0.3) sph(j) = 30.;
|
|---|
| 118 | else if ((teta>1.4) && (teta<1.6) ) sph(j) = 20.;
|
|---|
| 119 | else {
|
|---|
| 120 | if (phi < 2.) sph(j) = 2.;
|
|---|
| 121 | else if (phi > 3.) sph(j) = 5.;
|
|---|
| 122 | else sph(j) = 0.;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | PrtTim("End of Fill2 ");
|
|---|
| 126 |
|
|---|
| 127 | {
|
|---|
| 128 | FIO_SphereHEALPix<float> fiog(&sph) ;
|
|---|
| 129 | nomobj = "sphg2";
|
|---|
| 130 | fiog.Write(s, nomobj);
|
|---|
| 131 | cout << "SphMap SphereHEALPix<float> written to POutPersist with name " << nomobj << endl;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | // On projete dans un fichier FITS
|
|---|
| 136 | {
|
|---|
| 137 | cout << "Test of Write/Read SphereHEALPix<float> to FITS (sphg_r4.fits) " << endl;
|
|---|
| 138 | FITS_SphereHEALPix<r_4> fiosw(sph);
|
|---|
| 139 | fiosw.Write("sphg_r4.fits");
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | SphereHEALPix<float> sphr;
|
|---|
| 144 | {
|
|---|
| 145 | FITS_SphereHEALPix<r_4> fiosr("sphg_r4.fits");
|
|---|
| 146 | sphr = fiosr;
|
|---|
| 147 | cout << " Read from file - SphereHEALPix<float> NPixels= " << sphr.NbPixels() << endl;
|
|---|
| 148 | int ndiff = 0;
|
|---|
| 149 | for(int k=0; k<sphr.NbPixels(); k++) {
|
|---|
| 150 | if ( sphr(k) != sph(k) ) {
|
|---|
| 151 | ndiff++;
|
|---|
| 152 | if (ndiff < 20) cout << "!!!Diff: K= " << k << " SPHR= " << sphr(k) << " SPH= " << sph(k) << endl;
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | cout << " ReadFrom FITS NDiff = " << ndiff << " (should be zero = 0) " << endl;
|
|---|
| 156 |
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | TMatrix<float> mtx(3*m, 6*m);
|
|---|
| 161 | Project_Mol(sph, mtx);
|
|---|
| 162 | {
|
|---|
| 163 | cout << " Writing to FITS prjmol2.fits " << endl;
|
|---|
| 164 | FITS_TArray<double> fios(mtx);
|
|---|
| 165 | fios.Write("prjmol2.fits");
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | // Computing mean and sigma on the sphere
|
|---|
| 169 | MeanSig(sph, gmoy, gsig);
|
|---|
| 170 | cout << "SphMap2 Mean= " << gmoy << " Sigma = " << gsig << endl;
|
|---|
| 171 | PrtTim("End of Mean-Sig2 ");
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | {
|
|---|
| 175 | SphereThetaPhi< complex<float> > sphc(m*10);
|
|---|
| 176 |
|
|---|
| 177 | cout << "Filling spherical map3 SphereThetaPhi(complex) NPixels= " << sphc.NbPixels() << endl;
|
|---|
| 178 | for (int j=0;j<sphc.NbPixels();j++)
|
|---|
| 179 | {
|
|---|
| 180 | sphc.PixThetaPhi(j,teta,phi);
|
|---|
| 181 | if (teta < 0.3) sphc(j) = (30., drandpm1()*3.);
|
|---|
| 182 | else if ((teta>1.4) && (teta<1.6) ) sphc(j) = (20., NorRand());
|
|---|
| 183 | else {
|
|---|
| 184 | if (phi < 2.) sphc(j) = 2.;
|
|---|
| 185 | else if (phi > 3.) sphc(j) = 5.;
|
|---|
| 186 | else sphc(j) = 0.;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | PrtTim("End of Fill2 ");
|
|---|
| 190 |
|
|---|
| 191 | {
|
|---|
| 192 | FIO_SphereThetaPhi< complex<float> > fio(&sphc) ;
|
|---|
| 193 | nomobj = "sphtp";
|
|---|
| 194 | fio.Write(s, nomobj);
|
|---|
| 195 | cout << "SphMap written to POutPersist with name " << nomobj << endl;
|
|---|
| 196 | }
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | catch (PThrowable & exc) {
|
|---|
| 200 | cerr << "tspm2: Catched Exception " << (string)typeid(exc).name()
|
|---|
| 201 | << " - Msg= " << exc.Msg() << endl;
|
|---|
| 202 | }
|
|---|
| 203 | catch (...) {
|
|---|
| 204 | cerr << "tspm2: some other exception was caught ! " << endl;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | cout << " ===== Fin de TSPM2_Test ======== " << endl;
|
|---|
| 208 | return 0;
|
|---|
| 209 | }
|
|---|