| [591] | 1 | #include <iostream.h> | 
|---|
|  | 2 | #include <math.h> | 
|---|
| [857] | 3 | #include "sambainit.h" | 
|---|
|  | 4 | #include "skymap.h" | 
|---|
| [591] | 5 |  | 
|---|
|  | 6 | #include "tod.h" | 
|---|
|  | 7 | #include "fitsioserver.h" | 
|---|
|  | 8 | #include "nbrandom.h" | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include "timing.h" | 
|---|
|  | 11 |  | 
|---|
|  | 12 | template <class T> | 
|---|
|  | 13 | void  MeanSig(PixelMap<T> const & map, double& gmoy, double& gsig) | 
|---|
|  | 14 | { | 
|---|
|  | 15 | gmoy=0.; | 
|---|
|  | 16 | gsig = 0.; | 
|---|
|  | 17 | double valok; | 
|---|
|  | 18 | for(int k=0; k<map.NbPixels(); k++) { | 
|---|
|  | 19 | valok = map(k); | 
|---|
|  | 20 | gmoy += valok;  gsig += valok*valok; | 
|---|
|  | 21 | } | 
|---|
|  | 22 | gmoy /= (double)map.NbPixels(); | 
|---|
|  | 23 | gsig = gsig/(double)map.NbPixels() - gmoy*gmoy; | 
|---|
|  | 24 | if (gsig >= 0.) gsig = sqrt(gsig); | 
|---|
|  | 25 | } | 
|---|
|  | 26 |  | 
|---|
|  | 27 | template <class T> | 
|---|
|  | 28 | void  Project_Mol(PixelMap<T> const & map, TMatrix<T> & mtx, T defval=-999.) | 
|---|
|  | 29 | { | 
|---|
|  | 30 | r_8 xa, yd, teta,phi, facteur; | 
|---|
|  | 31 | int_4 l,c,k; | 
|---|
|  | 32 | int_4 nl = mtx.NRows(); | 
|---|
|  | 33 | int_4 nc = mtx.NCols(); | 
|---|
| [819] | 34 | mtx = defval;   // On met tout a defval | 
|---|
| [591] | 35 | cout << " NRows= " << nl << "  NCols= " << nc  << endl; | 
|---|
|  | 36 | for(l=0; l<nl; l++) { | 
|---|
|  | 37 | yd = (r_8)(l+0.5)/(r_8)nl-0.5; | 
|---|
|  | 38 | facteur=2.*M_PI/sin(acos((double)yd*2)); | 
|---|
|  | 39 | teta = (yd+0.5)*Pi; | 
|---|
|  | 40 | //    teta = (0.5-yd)*M_PI; | 
|---|
|  | 41 | for(c=0; c<nc; c++)  { | 
|---|
|  | 42 | xa = (r_8)(c+0.5)/(r_8)nc-0.5; | 
|---|
|  | 43 | phi = xa*facteur+M_PI; | 
|---|
|  | 44 | if ( (phi <= 2*M_PI) && (phi >= 0.) ) { | 
|---|
|  | 45 | k = map.PixIndexSph(teta, phi); | 
|---|
|  | 46 | mtx(l,c) = map(k); | 
|---|
|  | 47 | } | 
|---|
|  | 48 | } | 
|---|
|  | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | } | 
|---|
|  | 52 | template <class T> | 
|---|
|  | 53 | void  Project_Mol_Old(PixelMap<T> const & map, ImageR4 & img, float defval=-999.) | 
|---|
|  | 54 | { | 
|---|
|  | 55 | r_8 xa, yd, teta,phi, facteur; | 
|---|
|  | 56 | int_4 i,j,k,n; | 
|---|
|  | 57 | printf("Xsize= %d   Ysize= %d  NPix= %d\n",img.XSize(),img.YSize(),img.XSize()*img.YSize() ); | 
|---|
|  | 58 | n = 0; | 
|---|
| [606] | 59 | img.Zero(); | 
|---|
| [591] | 60 | for(j=0; j<img.YSize(); j++) { | 
|---|
|  | 61 | yd = (r_4)(j+0.5)/(r_4)img.YSize()-0.5; | 
|---|
|  | 62 | facteur=2.*M_PI/sin(acos((double)yd*2)); | 
|---|
|  | 63 | teta = (yd+0.5)*M_PI; | 
|---|
|  | 64 | //    teta = (0.5-yd)*M_PI; | 
|---|
|  | 65 | for(i=0; i<img.XSize(); i++)  { | 
|---|
|  | 66 | xa = (r_4)(i+0.5)/(r_4)img.XSize()-0.5; | 
|---|
|  | 67 | phi = xa*facteur+M_PI; | 
|---|
|  | 68 | if ( (phi <= 2*M_PI) && (phi >= 0.) ) { | 
|---|
|  | 69 | k = map.PixIndexSph(teta, phi); | 
|---|
|  | 70 | img(i,j) = map(k); | 
|---|
|  | 71 | //        if (n<20) { | 
|---|
|  | 72 | //          printf("i,j= %d %d -> %g %g -> k=%d -> val= %g \n", | 
|---|
|  | 73 | //               i,j,teta,phi,k,map(k)); n++; } | 
|---|
|  | 74 | } | 
|---|
|  | 75 | } | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 | int main(int narg, char* arg[]) | 
|---|
|  | 81 | { | 
|---|
|  | 82 | double teta,phi; | 
|---|
|  | 83 | double gmoy=0., gsig=0.; | 
|---|
|  | 84 |  | 
|---|
| [768] | 85 | SophyaInit(); | 
|---|
| [591] | 86 | InitTim();   // Initializing the CPU timer | 
|---|
|  | 87 | if ((narg > 1) && (strcmp(arg[1],"-h") == 0) )  { | 
|---|
| [857] | 88 | cout << " tspm [HEALPix_M=32] : HEALPix Spherical Map Test " << endl; | 
|---|
| [591] | 89 | exit(0); | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
|  | 92 | int  m=32; | 
|---|
|  | 93 | if (narg >1)  m = atoi(arg[1]); | 
|---|
| [857] | 94 | cout <<  " ===== HEALPix Spherical Map Test M= " << m << endl; | 
|---|
| [591] | 95 |  | 
|---|
|  | 96 | POutPersist s("spheres.ppf"); | 
|---|
|  | 97 | string nomobj; | 
|---|
|  | 98 |  | 
|---|
|  | 99 | { | 
|---|
| [857] | 100 | SphereHEALPix<double> sph(m); | 
|---|
| [591] | 101 |  | 
|---|
|  | 102 | cout << "Filling spherical map NPixels= " << sph.NbPixels() << endl; | 
|---|
|  | 103 | for (int j=0;j<sph.NbPixels();j++) | 
|---|
|  | 104 | { | 
|---|
|  | 105 | sph.PixThetaPhi(j,teta,phi); | 
|---|
|  | 106 | sph(j)= 0.2* cos(3.*teta)*sin(8*phi); | 
|---|
|  | 107 | } | 
|---|
|  | 108 | PrtTim("End of Fill "); | 
|---|
|  | 109 |  | 
|---|
|  | 110 | { | 
|---|
| [857] | 111 | FIO_SphereHEALPix<double> fiog(&sph) ; | 
|---|
| [591] | 112 | nomobj = "sphg1"; | 
|---|
|  | 113 | fiog.Write(s, nomobj); | 
|---|
| [857] | 114 | cout << "SphMap SphereHEALPix<double> written to POutPersist with name "  << nomobj << endl; | 
|---|
| [591] | 115 | } | 
|---|
|  | 116 |  | 
|---|
| [606] | 117 | FitsIoServer fios; | 
|---|
| [591] | 118 | // On projete dans un fichier FITS | 
|---|
|  | 119 | fios.sinus_picture_projection(sph, "gsin1.fits"); | 
|---|
|  | 120 | fios.save(sph, "sph1.fits"); | 
|---|
|  | 121 |  | 
|---|
|  | 122 | TMatrix<double> mtx(3*m, 6*m); | 
|---|
|  | 123 | Project_Mol(sph, mtx); | 
|---|
|  | 124 | fios.save(mtx, "mtx1.fits"); | 
|---|
|  | 125 |  | 
|---|
|  | 126 | cout << " Project_Mol_Old(sph, img) " << endl; | 
|---|
|  | 127 | ImageR4 img(6*m, 3*m); | 
|---|
|  | 128 | Project_Mol_Old(sph, img); | 
|---|
|  | 129 | img.CheckDyn(); | 
|---|
|  | 130 | cout << img; | 
|---|
|  | 131 | fios.save(img, "img1.fits"); | 
|---|
|  | 132 | cout << " Apres writeFits " << endl; | 
|---|
|  | 133 | img.CheckDyn(); | 
|---|
|  | 134 | cout << img; | 
|---|
|  | 135 | /* | 
|---|
|  | 136 | ImageR4 imgr; | 
|---|
|  | 137 | fios.load(imgr, "img1.fits"); | 
|---|
|  | 138 | cout << " Apres readFits " << endl; | 
|---|
|  | 139 | imgr.CheckDyn(); | 
|---|
|  | 140 | cout << imgr; | 
|---|
|  | 141 | */ | 
|---|
|  | 142 |  | 
|---|
|  | 143 | // Computing mean and sigma on the sphere | 
|---|
|  | 144 | MeanSig(sph, gmoy, gsig); | 
|---|
|  | 145 | cout << "SphMap Mean= " << gmoy << "  Sigma = " << gsig << endl; | 
|---|
|  | 146 | PrtTim("End of Mean-Sig "); | 
|---|
|  | 147 | } | 
|---|
| [606] | 148 |  | 
|---|
| [591] | 149 | { | 
|---|
| [857] | 150 | SphereHEALPix<float> sph(m); | 
|---|
| [591] | 151 |  | 
|---|
|  | 152 | cout << "Filling spherical map2 NPixels= " << sph.NbPixels() << endl; | 
|---|
|  | 153 | for (int j=0;j<sph.NbPixels();j++) | 
|---|
|  | 154 | { | 
|---|
|  | 155 | sph.PixThetaPhi(j,teta,phi); | 
|---|
|  | 156 | if (teta < 0.3)  sph(j) = 30.; | 
|---|
|  | 157 | else if ((teta>1.4) && (teta<1.6) ) sph(j) = 20.; | 
|---|
|  | 158 | else { | 
|---|
|  | 159 | if (phi < 2.)    sph(j) = 2.; | 
|---|
|  | 160 | else if (phi > 3.) sph(j) = 5.; | 
|---|
|  | 161 | else sph(j) = 0.; | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } | 
|---|
|  | 164 | PrtTim("End of Fill2 "); | 
|---|
|  | 165 |  | 
|---|
|  | 166 | { | 
|---|
| [857] | 167 | FIO_SphereHEALPix<float> fiog(&sph) ; | 
|---|
| [591] | 168 | nomobj = "sphg2"; | 
|---|
|  | 169 | fiog.Write(s, nomobj); | 
|---|
| [857] | 170 | cout << "SphMap SphereHEALPix<float> written to POutPersist with name "  << nomobj << endl; | 
|---|
| [591] | 171 | } | 
|---|
|  | 172 |  | 
|---|
|  | 173 |  | 
|---|
|  | 174 | // On projete dans un fichier FITS | 
|---|
|  | 175 | FitsIoServer fios; | 
|---|
| [606] | 176 | { | 
|---|
| [857] | 177 | cout << "Test of Write/Read SphereHEALPix<float> to FITS (sphg_r4.fits) " << endl; | 
|---|
| [606] | 178 | fios.save(sph, "sphg_r4.fits"); | 
|---|
|  | 179 |  | 
|---|
| [857] | 180 | SphereHEALPix<float> sphr(4); | 
|---|
| [606] | 181 | fios.load(sphr, "sphg_r4.fits", 2); | 
|---|
| [857] | 182 | cout << " Read from file - SphereHEALPix<float> NPixels= " << sphr.NbPixels() << endl; | 
|---|
| [606] | 183 | int ndiff = 0; | 
|---|
|  | 184 | for(int k=0; k<sphr.NbPixels(); k++) { | 
|---|
|  | 185 | if ( sphr(k) != sph(k) )   { | 
|---|
|  | 186 | ndiff++; | 
|---|
|  | 187 | if (ndiff < 20)  cout << "!!!Diff: K= " << k << " SPHR= " << sphr(k) << " SPH= " << sph(k) << endl; | 
|---|
|  | 188 | } | 
|---|
|  | 189 | } | 
|---|
|  | 190 | cout << " ReadFrom FITS NDiff = " << ndiff << " (should be zero = 0) " << endl; | 
|---|
|  | 191 |  | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 |  | 
|---|
| [591] | 195 | fios.sinus_picture_projection(sph, "gsin2.fits"); | 
|---|
|  | 196 | fios.save(sph, "sph2.fits"); | 
|---|
|  | 197 |  | 
|---|
| [606] | 198 | TMatrix<float> mtx(3*m, 6*m); | 
|---|
| [591] | 199 | Project_Mol(sph, mtx); | 
|---|
|  | 200 | fios.save(mtx, "mtx2.fits"); | 
|---|
|  | 201 |  | 
|---|
|  | 202 |  | 
|---|
|  | 203 | cout << " Project_Mol_Old(sph, img) " << endl; | 
|---|
|  | 204 | ImageR4 img(6*m, 3*m); | 
|---|
|  | 205 | Project_Mol_Old(sph, img); | 
|---|
|  | 206 | img.CheckDyn(); | 
|---|
|  | 207 | cout << img; | 
|---|
|  | 208 | fios.save(img, "img2.fits"); | 
|---|
|  | 209 | cout << " Apres writeFits " << endl; | 
|---|
|  | 210 | img.CheckDyn(); | 
|---|
|  | 211 | cout << img; | 
|---|
|  | 212 |  | 
|---|
|  | 213 | // Computing mean and sigma on the sphere | 
|---|
|  | 214 | MeanSig(sph, gmoy, gsig); | 
|---|
|  | 215 | cout << "SphMap2 Mean= " << gmoy << "  Sigma = " << gsig << endl; | 
|---|
|  | 216 | PrtTim("End of Mean-Sig2 "); | 
|---|
|  | 217 | } | 
|---|
|  | 218 |  | 
|---|
|  | 219 | { | 
|---|
|  | 220 | SphereThetaPhi< complex<float> > sphc(m*10); | 
|---|
|  | 221 |  | 
|---|
|  | 222 | cout << "Filling spherical map3 SphereThetaPhi(complex) NPixels= " << sphc.NbPixels() << endl; | 
|---|
|  | 223 | for (int j=0;j<sphc.NbPixels();j++) | 
|---|
|  | 224 | { | 
|---|
|  | 225 | sphc.PixThetaPhi(j,teta,phi); | 
|---|
|  | 226 | if (teta < 0.3)   sphc(j) = (30., drandpm1()*3.); | 
|---|
|  | 227 | else if ((teta>1.4) && (teta<1.6) ) sphc(j) = (20., NorRand()); | 
|---|
|  | 228 | else { | 
|---|
|  | 229 | if (phi < 2.)     sphc(j) = 2.; | 
|---|
|  | 230 | else if (phi > 3.) sphc(j) = 5.; | 
|---|
|  | 231 | else sphc(j) = 0.; | 
|---|
|  | 232 | } | 
|---|
|  | 233 | } | 
|---|
|  | 234 | PrtTim("End of Fill2 "); | 
|---|
|  | 235 |  | 
|---|
|  | 236 | { | 
|---|
|  | 237 | FIO_SphereThetaPhi< complex<float> > fio(&sphc) ; | 
|---|
|  | 238 | nomobj = "sphtp"; | 
|---|
|  | 239 | fio.Write(s, nomobj); | 
|---|
|  | 240 | cout << "SphMap written to POutPersist with name "  << nomobj << endl; | 
|---|
|  | 241 | } | 
|---|
|  | 242 | } | 
|---|
|  | 243 | cout <<  " ===== Fin de TSPM2_Test ======== " << endl; | 
|---|
|  | 244 | return 0; | 
|---|
|  | 245 | } | 
|---|