| 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL | 
|---|
| 2 | //                               Eric Aubourg | 
|---|
| 3 | //                               Christophe Magneville | 
|---|
| 4 | //                               Reza Ansari | 
|---|
| 5 | // $Id: toi2map.cc,v 1.34 2003-06-05 02:18:06 aubourg Exp $ | 
|---|
| 6 |  | 
|---|
| 7 | #include "machdefs.h" | 
|---|
| 8 | #include "toimanager.h" | 
|---|
| 9 | #include "pexceptions.h" | 
|---|
| 10 | #include "ctimer.h" | 
|---|
| 11 | #include "toi2map.h" | 
|---|
| 12 | // La valeur "Pi" doit etre celle de smathconst.h a cause du test sur theta | 
|---|
| 13 | #include "smathconst.h" | 
|---|
| 14 |  | 
|---|
| 15 | #include "fitsspherehealpix.h" | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 19 | TOI2Map::TOI2Map(string mapFName, string wmapFName, int nside) | 
|---|
| 20 | : mMap(new SphereHEALPix<r_8>(nside)), mWMap(new SphereHEALPix<r_8>(nside)), | 
|---|
| 21 | mMapFName(mapFName), mWMapFName(wmapFName), | 
|---|
| 22 | mWMapInternal(false), totnscount(0), usednscount(0) | 
|---|
| 23 | { | 
|---|
| 24 | commonConst(); | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 28 | TOI2Map::TOI2Map(PixelMap<r_8>* map,PixelMap<r_8>* wmap) | 
|---|
| 29 | : mMap(map), mWMap(wmap), mWMapInternal(false), | 
|---|
| 30 | mMapFName(""), mWMapFName(""), | 
|---|
| 31 | totnscount(0), usednscount(0) | 
|---|
| 32 | { | 
|---|
| 33 | commonConst(); | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | void TOI2Map::commonConst() { | 
|---|
| 37 | SetEquinox(); | 
|---|
| 38 | SetCoorIn(); | 
|---|
| 39 | SetCoorMap(); | 
|---|
| 40 | SetCalibrationFactor(); | 
|---|
| 41 | SetTestFlag(); | 
|---|
| 42 | SetTestMin(); | 
|---|
| 43 | SetTestMax(); | 
|---|
| 44 |  | 
|---|
| 45 | if(mMap->NbPixels()<1) { | 
|---|
| 46 | cout<<"TOI2Map::TOI2Map() Bad number of pixels in sphere mMap " | 
|---|
| 47 | <<mMap->NbPixels()<<endl; | 
|---|
| 48 | throw ParmError("TOI2Map::TOI2Map() - Bad number of pixels in sphere"); | 
|---|
| 49 | } | 
|---|
| 50 | mMap->SetPixels(0.); | 
|---|
| 51 | int nlat = mMap->SizeIndex(); | 
|---|
| 52 | string typmap = mMap->TypeOfMap(); | 
|---|
| 53 | cout << typmap << endl; | 
|---|
| 54 |  | 
|---|
| 55 | if(mWMap==NULL) { | 
|---|
| 56 | // We would need a cloning function in maps. $$TBD$$ $CHECK$ | 
|---|
| 57 | if (typmap == "LOCAL") { | 
|---|
| 58 | mWMap = new LocalMap<r_8>(*(LocalMap<r_8>*)mMap,false); | 
|---|
| 59 | } else if (typmap == "RING") { | 
|---|
| 60 | mWMap = new SphereHEALPix<r_8>(nlat); | 
|---|
| 61 | } else { | 
|---|
| 62 | cout << "TOI2Map::TOI2Map() cannot handle map of type " << typmap << endl; | 
|---|
| 63 | throw ParmError("TOI2Map::TOI2Map() - bad type of map"); | 
|---|
| 64 | } | 
|---|
| 65 | mWMapInternal = true; | 
|---|
| 66 | } else { | 
|---|
| 67 | mWMapInternal = false; | 
|---|
| 68 | if(nlat != mWMap->SizeIndex()) { | 
|---|
| 69 | cout<<"TOI2Map::TOI2Map() Bad size for sphere mWMap, does not " | 
|---|
| 70 | <<"correspond to mMap : "<<mMap->SizeIndex()<<", "<<mWMap->SizeIndex()<<endl; | 
|---|
| 71 | if (typmap == "LOCAL") { | 
|---|
| 72 | throw ParmError("TOI2Map::TOI2Map() - Different sizes for map and wmap"); | 
|---|
| 73 | } else if(typmap == "RING") { | 
|---|
| 74 | ((SphereHEALPix<r_8> *)mWMap)->Resize(nlat); | 
|---|
| 75 | cout<<"Resize have been done..."<<endl; | 
|---|
| 76 | } else { | 
|---|
| 77 | cout << "TOI2Map::TOI2Map() cannot handle map of type " << typmap << endl; | 
|---|
| 78 | throw ParmError("TOI2Map::TOI2Map() - bad type of map"); | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | if(mWMap->NbPixels()<1) { | 
|---|
| 83 | cout<<"TOI2Map::TOI2Map() Bad number of pixels in sphere mWMap " | 
|---|
| 84 | <<mWMap->NbPixels()<<endl; | 
|---|
| 85 | throw ParmError("TOI2Map::TOI2Map() - Bad number of pixels in sphere"); | 
|---|
| 86 | } | 
|---|
| 87 | mWMap->SetPixels(0); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | TOI2Map::~TOI2Map() | 
|---|
| 91 | { | 
|---|
| 92 | if(mWMap && mWMapInternal) delete mWMap; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 96 | void TOI2Map::Print(::ostream & os) | 
|---|
| 97 | { | 
|---|
| 98 | os<<"TOI2Map::Print -- Map type " << mMap->TypeOfMap() << " SizeIndex = "<<mMap->SizeIndex()<<endl | 
|---|
| 99 | <<"   - Equinoxe="<<mActualYear<<endl | 
|---|
| 100 | <<"   - TypCoorIn:  "<<mTypCoorIn<<" = "<<DecodeTypAstro(mTypCoorIn)<<endl | 
|---|
| 101 | <<"   - TypCoorMap: "<<mTypCoorMap<<" = "<<DecodeTypAstro(mTypCoorMap)<<endl | 
|---|
| 102 | <<"  - Tests: Flag("<<mTFlag<<") bad="<<mBadFlag | 
|---|
| 103 | <<" / Value Min("<<mTMin<<")="<<mValMin | 
|---|
| 104 | <<" , Max("<<mTMax<<")="<<mValMax<<endl; | 
|---|
| 105 | os<<" TotalProcessedSamples="<<ProcessedSampleCount() | 
|---|
| 106 | <<" UsedSampleCount="<<UsedSampleCount()<<endl; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 110 | void TOI2Map::init() { | 
|---|
| 111 | cout << "TOI2Map::init" << endl; | 
|---|
| 112 | declareInput("Coord1In");     // input index 0 | 
|---|
| 113 | declareInput("Coord2In");     // input index 1 | 
|---|
| 114 | declareInput("BoloIn");       // input index 2 | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 118 | // define SANS_BUFFER | 
|---|
| 119 | void TOI2Map::run() | 
|---|
| 120 | { | 
|---|
| 121 | long snb = getMinIn(); | 
|---|
| 122 | long sne = getMaxIn(); | 
|---|
| 123 |  | 
|---|
| 124 | if(snb>sne) { | 
|---|
| 125 | cout<<"TOI2Map::run() - Bad sample interval"<<snb<<" , "<<sne<<endl; | 
|---|
| 126 | throw ParmError("TOI2Map::run() - Bad sample interval"); | 
|---|
| 127 | } | 
|---|
| 128 | if(!checkInputTOIIndex(0) || !checkInputTOIIndex(1) || !checkInputTOIIndex(2)) { | 
|---|
| 129 | cout<<"TOI2Map::run() - Input TOI (Coord1In or Coord2In or BoloIn) not connected! "<<endl; | 
|---|
| 130 | throw ParmError("TOI2Map::run() Output TOI (Coord1In or Coord2In or BoloIn) not connected!"); | 
|---|
| 131 | } | 
|---|
| 132 | if( !(mTypCoorIn&TypCoordEq || mTypCoorIn&TypCoordGal) ) { | 
|---|
| 133 | cout<<"TOI2Map::run() - Input Coordinates not Eq or Gal! "<<endl; | 
|---|
| 134 | throw ParmError("TOI2Map::run() - Input Coordinates not Eq or Gal!"); | 
|---|
| 135 | } | 
|---|
| 136 | if( !(mTypCoorMap&TypCoordEq || mTypCoorMap&TypCoordGal) ) { | 
|---|
| 137 | cout<<"TOI2Map::run() - Output Coordinates not Eq or Gal! "<<endl; | 
|---|
| 138 | throw ParmError("TOI2Map::run() - Output Coordinates not Eq or Gal!"); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | //--------------------------------------------------------- | 
|---|
| 142 | #define NFILL 25 | 
|---|
| 143 | try { | 
|---|
| 144 |  | 
|---|
| 145 | int ii; | 
|---|
| 146 | uint_4 mNSnFill=0, mNpixFill=0, NFill[NFILL], BadCoorRange=0, nbadsn=0; | 
|---|
| 147 | for(ii=0;ii<NFILL;ii++) NFill[ii]=0; | 
|---|
| 148 | double mjd = MJDfrYear(mActualYear); | 
|---|
| 149 |  | 
|---|
| 150 | cout<<"TOI2Map::run() from "<<snb<<" to "<<sne; | 
|---|
| 151 | #ifndef SANS_BUFFER | 
|---|
| 152 | int bufsz = 100; | 
|---|
| 153 | uint_8* bfgbolo = new uint_8[bufsz]; | 
|---|
| 154 | double* bbolo   = new double[bufsz]; | 
|---|
| 155 | double* bc1     = new double[bufsz]; | 
|---|
| 156 | double* bc2     = new double[bufsz]; | 
|---|
| 157 | int i0 = -1; | 
|---|
| 158 | cout<<" (getData() bufferized)"<<endl; | 
|---|
| 159 | #else | 
|---|
| 160 | cout<<" (getData() not bufferized)"<<endl; | 
|---|
| 161 | #endif | 
|---|
| 162 | cout << "calib = " << mCalibFactor << endl; | 
|---|
| 163 | // Remplissage des spheres | 
|---|
| 164 | for(int s=snb;s<=sne;s++) { | 
|---|
| 165 | uint_8 fgbolo = 0; | 
|---|
| 166 | double bolo,coord1,coord2; | 
|---|
| 167 | //              Equatoriales   /   Galactiques | 
|---|
| 168 | // coord1,2 =   alpha,delta    /   gLon,gLat | 
|---|
| 169 |  | 
|---|
| 170 | #ifndef SANS_BUFFER | 
|---|
| 171 | if(i0<0 || s<i0 || s>=i0+bufsz) { | 
|---|
| 172 | i0 = s; | 
|---|
| 173 | int nget = (sne-s+1<bufsz)? nget=sne-s+1: bufsz; | 
|---|
| 174 | getData(0,i0,nget,bc1); | 
|---|
| 175 | getData(1,i0,nget,bc2); | 
|---|
| 176 | getData(2,i0,nget,bbolo,bfgbolo); | 
|---|
| 177 | totnscount += nget; | 
|---|
| 178 | } | 
|---|
| 179 | bolo = bbolo[s-i0]; fgbolo = bfgbolo[s-i0]; | 
|---|
| 180 | coord1 = bc1[s-i0]; coord2 = bc2[s-i0]; | 
|---|
| 181 | #else | 
|---|
| 182 | getData(2,s,bolo,fgbolo); | 
|---|
| 183 | coord1 = getData(0,s); | 
|---|
| 184 | coord2 = getData(1,s); | 
|---|
| 185 | totnscount++; | 
|---|
| 186 | #endif | 
|---|
| 187 |  | 
|---|
| 188 | // Comme il n'y a pas de toi en sortie, il faut dire | 
|---|
| 189 | // aux processeur/toi que l'on a plus besoin des donnees. | 
|---|
| 190 | if (s%100 == 0) wontNeedBefore(s-1); | 
|---|
| 191 | //if(fgbolo>0) cout<<fgbolo<<" , "<<(fgbolo&mBadFlag)<<endl; | 
|---|
| 192 | if(mTFlag && (fgbolo&mBadFlag)) {nbadsn++; continue;} | 
|---|
| 193 | if(mTMin && (bolo<mValMin))     {nbadsn++; continue;} | 
|---|
| 194 | if(mTMax && (bolo>mValMax))     {nbadsn++; continue;} | 
|---|
| 195 | usednscount++; | 
|---|
| 196 |  | 
|---|
| 197 | // sphere phi   entre [0,2*Pi] en radians | 
|---|
| 198 | // sphere theta entre [0,Pi]   en radians | 
|---|
| 199 | double phi=-1.; | 
|---|
| 200 | CoordConvertToStd(mTypCoorIn,&coord1,&coord2); | 
|---|
| 201 |  | 
|---|
| 202 | if(mTypCoorIn&TypCoordEq && mTypCoorMap&TypCoordGal) { // Eq -> Gal | 
|---|
| 203 | EqtoGal(mjd,coord1,coord2,&coord1,&coord2); | 
|---|
| 204 | phi   = coord1 * Pi/180.; | 
|---|
| 205 | } else if(mTypCoorIn&TypCoordGal && mTypCoorMap&TypCoordEq) { // Gal -> Eq | 
|---|
| 206 | GaltoEq(mjd,coord1,coord2,&coord1,&coord2); | 
|---|
| 207 | phi   = coord1 * Pi/12.; | 
|---|
| 208 | } else if(mTypCoorMap&TypCoordGal) { // Gal -> Gal | 
|---|
| 209 | phi   = coord1 * Pi/180.; | 
|---|
| 210 | } else if(mTypCoorMap&TypCoordEq) { // Eq -> Eq | 
|---|
| 211 | phi   = coord1 * Pi/12.; | 
|---|
| 212 | } | 
|---|
| 213 | ToCoLat(&coord2,TypUniteD); | 
|---|
| 214 | double theta = coord2 * Pi/180.; | 
|---|
| 215 |  | 
|---|
| 216 | if(phi<0. | phi>=2*Pi || theta<0. || theta>Pi) | 
|---|
| 217 | {BadCoorRange++; continue;} | 
|---|
| 218 |  | 
|---|
| 219 | int_4 ipix = mMap->PixIndexSph(theta,phi); | 
|---|
| 220 | if ((ipix < 0) || (ipix >= mMap->NbPixels()) ) continue; | 
|---|
| 221 | /* | 
|---|
| 222 | if (mNSnFill % 1000 == 0) { | 
|---|
| 223 | cout << ipix << " " << (*mMap)(ipix) << " + " << bolo*mCalibFactor << | 
|---|
| 224 | " " << ((*mWMap)(ipix)) << endl; | 
|---|
| 225 | } | 
|---|
| 226 | */ | 
|---|
| 227 | (*mMap)(ipix) += bolo*mCalibFactor; | 
|---|
| 228 | ((*mWMap)(ipix)) += 1; | 
|---|
| 229 | mNSnFill++; | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | cout<<"TOI2Map::run(): End of SN loop - TotalProcessedSamples=" | 
|---|
| 233 | <<ProcessedSampleCount()<<" UsedSampleCount="<<UsedSampleCount()<<endl; | 
|---|
| 234 |  | 
|---|
| 235 | // Remplissage des spheres | 
|---|
| 236 | for(int_4 i=0;i<mMap->NbPixels();i++) { | 
|---|
| 237 | r_8 wf = (*mWMap)(i); | 
|---|
| 238 | /* | 
|---|
| 239 | if (i%100 == 0) { | 
|---|
| 240 | cout << "pix " << i << " w = " << wf << " map = " << | 
|---|
| 241 | (*mMap)(i); | 
|---|
| 242 | } | 
|---|
| 243 | */ | 
|---|
| 244 | if(wf>0.) {mNpixFill++; (*mMap)(i) /= wf;} | 
|---|
| 245 | /* | 
|---|
| 246 | if (i%100 == 0) { | 
|---|
| 247 | cout << " -> " << (*mMap)(i); | 
|---|
| 248 | } | 
|---|
| 249 | */ | 
|---|
| 250 | int_4 nf = int_4(wf); | 
|---|
| 251 | if(nf>=NFILL) nf=NFILL-1; NFill[nf]++; | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | cout<<"TOI2Map::run(): mNpixTot="<<mMap->NbPixels() | 
|---|
| 255 | <<"  mNpixFill="<<mNpixFill | 
|---|
| 256 | <<"  --> FracSky="<<mNpixFill*100./(double)mMap->NbPixels()<<"%" | 
|---|
| 257 | <<"  NFill["<<NFILL<<"] ="<<endl; | 
|---|
| 258 | for(ii=0;ii<NFILL;ii++) {cout<<NFill[ii]<<" "; if(ii%10==9) cout<<endl;} | 
|---|
| 259 | cout<<endl; | 
|---|
| 260 | cout<<"  mNSnFill="<<mNSnFill<<" NBadSn="<<nbadsn | 
|---|
| 261 | <<"  BadCoorRange="<<BadCoorRange<<endl; | 
|---|
| 262 |  | 
|---|
| 263 | #ifndef SANS_BUFFER | 
|---|
| 264 | delete [] bbolo; delete [] bfgbolo; | 
|---|
| 265 | delete [] bc1;   delete [] bc2; | 
|---|
| 266 | #endif | 
|---|
| 267 |  | 
|---|
| 268 | /* if (mMapFName != "") { | 
|---|
| 269 | cout << "saving maps" << endl; | 
|---|
| 270 | { | 
|---|
| 271 | FitsOutFile sfits(mMapFName,FitsFile::clear); | 
|---|
| 272 | sfits << * (SphereHEALPix<r_8>*)mMap; | 
|---|
| 273 | } | 
|---|
| 274 | { | 
|---|
| 275 | FitsOutFile sfits(mWMapFName,FitsFile::clear); | 
|---|
| 276 | sfits << * (SphereHEALPix<r_8>*)mWMap; | 
|---|
| 277 | } | 
|---|
| 278 | } | 
|---|
| 279 | */ | 
|---|
| 280 | //--------------------------------------------------------- | 
|---|
| 281 | } catch (PException & exc) { | 
|---|
| 282 | cout<<"TOI2Map: Catched Exception "<<(string)typeid(exc).name() | 
|---|
| 283 | <<"\n .... Msg= "<<exc.Msg()<<endl; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | return; | 
|---|
| 287 | } | 
|---|