#include "machdefs.h" #include #include #include #include #include #include #include "timing.h" #include "sambainit.h" #include "pexceptions.h" #include "datacards.h" #include "fitsioserver.h" #include "spherehealpix.h" #include "fiospherehealpix.h" #include "radspecvector.h" #include "blackbody.h" #include "derivblackbody.h" #include "nupower.h" #include "squarefilt.h" #include "trianglefilt.h" #include "specrespvector.h" #include "gaussfilt.h" // ----------------------------------------------------------------- // ------------- Function declaration ------------------------------ int CheckCards(DataCards & dc, string & msg); char * BuildFITSFileName(string const & fname); SpectralResponse * getSpectralResponse(DataCards & dc); RadSpectra * getEmissionSpectra(DataCards & dc, int nc); void RadSpec2Nt(RadSpectra & rs, POutPersist & so, string name); void SpectralResponse2Nt(SpectralResponse& sr, POutPersist & so, string name); // // treating maps //--------------------------------------------------------- template void MeanSig(NDataBlock const & dbl, double& gmoy, double& gsig); template void Sph2Sph(PixelMap& in, PixelMap& out); // // to add different sky components and corresponding tools //---------------------------------------------------------- template void addComponent(SpectralResponse& sr, PixelMap& finalMap, PixelMap& mapToAdd, RadSpectra& rs, double K=1.); // template void addComponentBeta(SphereHEALPix& finalMap, SphereHEALPix& mapToAdd,SpectralResponse& sr, SphereHEALPix& betaMap, double normFreq, double K); // template void integratedMap(SpectralResponse& sr, SphereHEALPix& betaMap, double normFreq, SphereHEALPix& intBetaMap); //template //void integratedMap(SpectralResponse& sr, // PixelMap& betaMap, // double normFreq, // PixelMap& outMap); template void addComponentBeta(SphereHEALPix& finalMap, SphereHEALPix& mapToAdd, SphereHEALPix& intBetaMap, double K); // // ----------------------------------------------------------------- // ----- Global (static) variables ------------ static bool rdmap = false; // true -> Read map first static char mapPath[256]; // Path for input maps static int hp_nside = 32; // HealPix NSide static int nskycomp = 0; // Number of sky components static int debuglev = 0; // Debug Level static int printlev = 0; // Print Level static POutPersist * so = NULL; // Debug PPFOut file // ------------------------------------------------------------------------- // main program // ------------------------------------------------------------------------- int main(int narg, char * arg[]) { if ((narg < 3) || ((narg > 1) && (strcmp(arg[1], "-h") == 0) )) { cout << " Usage: skymixer parameterFile outputfitsname [outppfname]" << endl; exit(0); } InitTim(); string msg; int rc = 0; RadSpectra * es = NULL; SpectralResponse * sr = NULL; double moy, sig; DataCards dc; so = NULL; try { string dcard = arg[1]; cout << " Decoding parameters from file " << dcard << endl; dc.ReadFile(dcard); rc = CheckCards(dc, msg); if (rc) { cerr << " Error condition -> Rc= " << rc << endl; cerr << " Msg= " << msg << endl; return(rc); } } catch (PException exc) { msg = exc.Msg(); cerr << " !!!! skymixer/ Readcard - Catched exception - Msg= " << exc.Msg() << endl; return(90); } cout << " skymix/Info : NComp = " << nskycomp << " SphereHEALPix_NSide= " << hp_nside << endl; cout << " ... MapPath = " << (string)mapPath << " DebugLev= " << debuglev << " PrintLev= " << printlev << endl; // We create an output persist file for writing debug objects if (debuglev > 0) so = new POutPersist("skymixdbg.ppf"); SphereHEALPix outgs(hp_nside); bool okout = false; try { if (rdmap) { // Reading map from FITS file FitsIoServer fios; char ifnm[256]; strncpy(ifnm, dc.SParam("READMAP", 0).c_str(), 255); ifnm[255] = '\0'; cout << " Reading output HealPix map from FITS file " << (string)ifnm << endl; fios.load(outgs, ifnm, 2); cout << " Output HealPIx Map read - NbPixels= " << outgs.NbPixels() << endl; if (printlev > 0) { MeanSig(outgs.DataBlock(), moy, sig ); cout << " MeanSig for outpout map - Mean= " << moy << " Sigma= " << sig << endl; } } else { cout << " Output HealPix Map created - NbPixels= " << outgs.NbPixels() << endl; outgs.SetPixels(0.); } // Decoding detection pass-band filter sr = getSpectralResponse(dc); PrtTim(" After FilterCreation "); FitsIoServer fios; // Our FITS IO Server char * flnm, buff[90]; string key; SphereHEALPix ings(hp_nside); // The input map double K = 1.; double freqOfMap = 1.; // Loop over sky component int sk; for(sk = 0; sk 4) { // Writing the input map to the outppf FIO_SphereHEALPix fiog(ings); fiog.Write(*so, key); } if (printlev > 2) { MeanSig(ings.DataBlock(), moy, sig ); cout << " MeanSig for input map - Mean= " << moy << " Sigma= " << sig << endl; } // getting Emission spectra if(!mapDependentOfFreq) { if (es) { delete es; es = NULL; } es = getEmissionSpectra(dc, sk); addComponent(*sr, outgs, ings, *es, K); } else { key = (string)"BETAFITSFILE"+ buff; SphereHEALPix betaMap; flnm = BuildFITSFileName(dc.SParam(key, 0)); double normFreq = dc.DParam(key, 1, 1.); if (printlev > 4) cout << "....BetaFits... normalization Freq = " << normFreq << endl; int nSideForInt = dc.DParam(key, 2, 1.); if (printlev > 4) cout << "....BetaFits... NSide for Integration map = " << nSideForInt << endl; cout << "....BetaFits... Reading Beta FITS map " << (string)flnm << endl; fios.load(betaMap, flnm, 2); if(nSideForInt<0) nSideForInt = sqrt(betaMap.NbPixels()/12); bool bydefault = true; if(!bydefault) addComponentBeta(outgs,ings,*sr,betaMap,normFreq, K); else { // integrate the betamap over the SpectralResponse SphereHEALPix intBetaMap(nSideForInt); integratedMap(*sr, betaMap, normFreq, intBetaMap); betaMap.Resize(8); MeanSig(intBetaMap.DataBlock(), moy, sig ); if (printlev > 4) cout << "....BetaFits... MeanSig for intBetaMap - Mean= " << moy << " Sigma= " << sig << endl; // add the integrated beta map addComponentBeta(outgs,ings,intBetaMap, K); } MeanSig(outgs.DataBlock(), moy, sig ); cout << " MeanSig for Sum map - Mean= " << moy << " Sigma= " << sig << endl; } okout = true; if (printlev > 1) { MeanSig(outgs.DataBlock(), moy, sig ); cout << " MeanSig for Sum map - Mean= " << moy << " Sigma= " << sig << endl; } sprintf(buff, "End of Proc. Comp. %d ", sk+1); cout << " --------------------------------------- \n" << endl; PrtTim(buff); } // End of sky component loop } // End of try block catch (PException exc) { msg = exc.Msg(); cerr << " !!!! skymixer - Catched exception - Msg= " << exc.Msg() << endl; rc = 50; } // Saving the output map in FITS format if (okout) { FitsIoServer fios; fios.save(outgs, arg[2]); cout << "Output Map (SphereHEALPix) written to FITS file " << (string)(arg[2]) << endl; PrtTim("End of WriteFITS "); // Saving the output map in PPF format if (narg > 3) { POutPersist s(arg[3]); FIO_SphereHEALPix fiog(&outgs) ; fiog.Write(s); cout << "Output Map (SphereHEALPix) written to POutPersist file " << (string)(arg[3]) << endl; PrtTim("End of WritePPF "); } } if (so) delete so; // Closing the debug ppf file return(rc); } /* Nouvelle-Fonction */ int CheckCards(DataCards & dc, string & msg) // Function to check datacards { rdmap = false; mapPath[0] = '\0'; hp_nside = 32; nskycomp = 0; debuglev = 0; printlev = 0; int rc = 0; string key, key2; // Cheking datacards if (dc.NbParam("SKYMIX") < 2) { rc = 71; msg = "Invalid parameters - Check @SKYMIX card "; return(rc); } key = "READMAP"; if (dc.HasKey(key)) { if (dc.NbParam(key) < 1) { rc = 72; msg = "Invalid parameters - Check @READMAP card "; return(rc); } else rdmap = true; } // Checking detection filter specification key = "GAUSSFILTER"; key2 = "FILTERFITSFILE"; if ( (dc.NbParam(key) < 5) && (dc.NbParam(key2) < 3)) { msg = "Missing card or parameters : Check @GAUSSFILTER or @FILTERFITSFILE"; rc = 73; return(rc); } // Decoding number of component and pixelisation parameter int mg = 32; int ncomp = 0; ncomp = dc.IParam("SKYMIX", 0, 0); mg = dc.IParam("SKYMIX", 1, 32); if (ncomp < 1) { msg = "Invalid parameters - Check datacards @SKYMIX "; rc = 74; return(rc); } // Checking detection filter specification // Checking input FITS file specifications int kc; char buff[256]; bool pb = false; string key3; string key4; string key5; for(kc=0; kc 1) SpectralResponse2Nt(*filt, *so, ppfname); return(filt); } /* Nouvelle-Fonction */ RadSpectra * getEmissionSpectra(DataCards & dc, int nc) { char numb[16]; sprintf(numb, "%d", nc+1); string key = (string)"SPECTRAFITSFILE" + numb; string key2 = (string)"BLACKBODY" + numb; string key5 = (string)"DIPOLE" + numb; string key3 = (string)"POWERLAWSPECTRA" + numb; string ppfname = "espectra"; RadSpectra * rs = NULL; if (dc.HasKey(key) ) { // Reading emission spectra from file char * ifnm = BuildFITSFileName(dc.SParam(key, 0)); cout << " Reading Input FITS spectra file " << (string)ifnm << endl; FitsIoServer fios; Matrix mtx(2,10); fios.load(mtx, ifnm); double numin = dc.DParam(key, 2, 1.); double numax = dc.DParam(key, 3, 9999.); Vector nu(mtx.NCols()); Vector tnu(mtx.NCols()); for(int k=0; k 2) RadSpec2Nt(*rs, *so, ppfname); return(rs); } /* Nouvelle-Fonction */ template void addComponent(SpectralResponse& sr, PixelMap& finalMap, PixelMap& mapToAdd, RadSpectra& rs, double K) { // finalMap = finalMap + coeff* mapToAdd // coeff = convolution of sr and rs // compute the coefficient corresponding to mapToAdd if (finalMap.NbPixels() != mapToAdd.NbPixels()) throw SzMismatchError("addComponent()/Error: Unequal number of Input/Output map pixels"); double coeff = rs.filteredIntegratedFlux(sr) * K; if (printlev > 1) cout << " addComponent - Coeff= " << coeff << " (K= " << K << ")" << endl; for(int i=0; i void addComponentBeta(SphereHEALPix& finalMap, SphereHEALPix& mapToAdd,SpectralResponse& sr, SphereHEALPix& betaMap, double normFreq, double K) { // finalMap = finalMap + coeff* mapToAdd // coeff = convolution of sr and rs // compute the coefficient corresponding to mapToAdd // betaMap is the map of (beta(theta,phi)) int nbpix = finalMap.NbPixels(); if (nbpix != mapToAdd.NbPixels()) throw SzMismatchError("addComponentBeta()/Error: Unequal number of Input/Output map pixels"); if (printlev > 1) { cout << "addComponentBeta - Coeff= " << K << endl; cout << "nb pixels: " << finalMap.NbPixels() << endl; } SphereHEALPix bigBetaMap(sqrt(nbpix/12)); if(nbpix != betaMap.NbPixels()) { Sph2Sph(betaMap,bigBetaMap); } for(int i=0; ifilteredIntegratedFlux(sr); finalMap(i) += coeff*K*mapToAdd(i); } } template void integratedMap(SpectralResponse& sr, SphereHEALPix& betaMap, double normFreq, SphereHEALPix& intBetaMap) { // int nbpix = intBetaMap.NbPixels(); // SphereHEALPix newMap(sqrt(nbpix/12)); Sph2Sph(betaMap,intBetaMap); for(int i=0; ifilteredIntegratedFlux(sr); intBetaMap(i) = coeff; } } template void addComponentBeta(SphereHEALPix& finalMap, SphereHEALPix& mapToAdd,SphereHEALPix& intBetaMap, double K) { // finalMap = finalMap + coeff* mapToAdd // coeff = convolution of sr and rs // compute the coefficient corresponding to mapToAdd // integBetaMap is the map of the integration (nu/normFreq)^(-beta(theta,phi)) over // the spectralResponse // different from addComponentBeta(PixelMap& finalMap, // PixelMap& mapToAdd,SpectralResponse& sr, PixelMap& betaMap, double normFreq, double K) // since it permits to use a intBetaMap with a different number of pixels than // the other maps int nbpix = finalMap.NbPixels(); if (nbpix != mapToAdd.NbPixels()) throw SzMismatchError("addComponentBeta(PixelMap&,PixelMap&,PixelMap&,double)/Error: Unequal number of Input/Output map pixels"); double coeff = K; if(nbpix != intBetaMap.NbPixels()) { cout << "nbpix != intBetaMap.NbPixels()" << endl; SphereHEALPix bigBetaMap(sqrt(nbpix/12)); cout << "new map with size corresponding to mapToAdd" << sqrt(nbpix/12) << endl; Sph2Sph(intBetaMap,bigBetaMap); for(int i=0; i 1) { cout << "addComponentBeta(SG,SG,SG,double) - Coeff= " << K << endl; } } /* Nouvelle-Fonction */ template void Sph2Sph(PixelMap& in, PixelMap& out) // Cette fonction remplit la sphere out a partir de la sphere in // Les spheres peuvent etre de type et de pixelisations differentes { int k,kin,kout; double teta,phi; int* cnt = new int[out.NbPixels()+1]; for(kout=0; kout 0) out(kout) /= dcn; else { out.PixThetaPhi(kout, teta, phi); int pixel = in.PixIndexSph(teta,phi); out(kout) = in.PixVal(pixel); } } moy /= out.NbPixels(); sig = sig/out.NbPixels() - moy*moy; if (sig >= 0.) sig = sqrt(sig); delete[] cnt; printf("Sph2Sph_Info NbPix In= %d Out= %d CntMoy,Sig= %g %g\n", in.NbPixels(), out.NbPixels(), moy, sig); PrtTim("End of Sph2Sph() "); } /* Nouvelle-Fonction */ template void MeanSig(NDataBlock const & dbl, double& gmoy, double& gsig) { gmoy=0.; gsig = 0.; double valok; for(int k=0; k<(int)dbl.Size(); k++) { valok = dbl(k); gmoy += valok; gsig += valok*valok; } gmoy /= (double)dbl.Size(); gsig = gsig/(double)dbl.Size() - gmoy*gmoy; if (gsig >= 0.) gsig = sqrt(gsig); } /* Nouvelle-Fonction */ void RadSpec2Nt(RadSpectra & rs, POutPersist & so, string name) { char *ntn[2] = {"nu","fnu"}; NTuple nt(2,ntn); // Creation NTuple (AVEC new ) float xnt[2]; double nu; double numin = rs.minFreq(); double numax = rs.maxFreq(); int nmax = 500; double dnu = (numax-numin)/nmax; for(int k=0; k oiont(nt); oiont.Write(so, name); return; } /* Nouvelle-Fonction */ void SpectralResponse2Nt(SpectralResponse& sr, POutPersist & so, string name) { char *ntn[2] = {"nu","tnu"}; NTuple nt(2,ntn); // Creation NTuple (AVEC new ) float xnt[2]; double nu; double numin = sr.minFreq(); double numax = sr.maxFreq(); int nmax = 500; double dnu = (numax-numin)/nmax; for(int k=0; k oiont(nt); oiont.Write(so, name); return; }