#include "machdefs.h" #include #include #include #include #include #include #include #include "tmatrix.h" #include "tvector.h" #include "vector3d.h" #include "timing.h" #include "sambainit.h" #include "pexceptions.h" #include "datacards.h" #include "fitsioserver.h" #include "radspecvector.h" #include "blackbody.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); template void addComponent(SpectralResponse& sr, PixelMap& finalMap, PixelMap& mapToAdd, RadSpectra& rs, double K=1.); template void MeanSig(NDataBlock const & dbl, double& gmoy, double& gsig); float ComputeFrequency(DataCards &,string&); // ----------------------------------------------------------------- // ----- Global (static) variables ------------ static bool rdmap = false; // true -> Read map first static char mapPath[256]; // Path for input maps 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 < 4) || ((narg > 1) && (strcmp(arg[1], "-h") == 0) )) { cout << " Usage: extractRS parameterFile outputfitsnameformaps outputfitsnameforsource [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 << " !!!! extractRS/ Readcard - Catched exception - Msg= " << exc.Msg() << endl; return(90); } cout << " extractRS/Info : NComp = " << nskycomp << 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("extrRSdbg.ppf"); TMatrix outgs; bool okout = false; try { FitsIoServer fios; // Our FITS IO Server char * flnm, buff[90]; string key; string key2; double K = 1.; // Loop over sky frequencies components int maxOfSkyComp = nskycomp; int nbOfPix; int sk; Vector freq(maxOfSkyComp); TMatrix spectrum; TMatrix fdenu; //par freq et par pixel int nb_source; TMatrix rareSource; //par freq et par numero de source for(sk = 0; sk ings; // Opening the file containing the map if (printlev > 2) cout << " Processing map's frequency component No " << sk+1 << endl; sprintf(buff, "%d", sk+1); key = (string)"RADSPECMAP" + buff; flnm = BuildFITSFileName(dc.SParam(key,0)); if (printlev > 2) cout << " Reading Input FITS map " << (string)flnm << endl; fios.load(ings, flnm) ; if (printlev > 2) { MeanSig(ings.DataBlock(), moy, sig ); cout << " MeanSig for input map - Mean= " << moy << " Sigma= " << sig << endl; } // Computing the frequency according to the name of the file freq(sk) = ComputeFrequency(dc,key); if (printlev > 2) cout << "filling spectrum for freq = " << freq(sk) << endl; // Opening the file containing the point source sprintf(buff, "%d", sk+1); key2 = (string)"SOURCEPTMAP" + buff; flnm = BuildFITSFileName(dc.SParam(key2,0)); const char* nameOfFile = flnm ; if (printlev > 2) cout << " Reading Input FITS map " << nameOfFile << endl; ifstream from(nameOfFile); char ch; string dum; from >> nb_source >> dum; if(sk==0) rareSource.ReSize(maxOfSkyComp,nb_source); for (int ii=0; ii< nb_source; ii++) { double value = 0; from >> value; rareSource(sk,ii) = value*1.e-26; //for Jansky->W } from.close(); nbOfPix = ings.NRows()*ings.NCols(); spectrum.ReSize(ings.NRows(),ings.NCols()); if(sk==0) fdenu.ReSize(maxOfSkyComp,nbOfPix); int xy=0; for(int x=0;xW fdenu(sk,xy) = spectrum(x,y); xy += 1; } } if (printlev > 2) { MeanSig(spectrum.DataBlock(), moy, sig ); cout << " MeanSig for spectrum map - Mean= " << moy << " Sigma= " << sig << endl; } K = dc.DParam(key, 1, 1.); if (debuglev > 4) { // Writing the input map to the outppf FIO_TMatrix fiog; fiog.Write(*so, key); } if(printlev>2) { sprintf(buff, "End of Proc. Comp. %d ", sk+1); cout << " --------------------------------------- \n" << endl; PrtTim(buff); } } // End of sky component loop // --------------------------------------------------------------------- // Integration in the detector band of the maps // --------------------------------------------------------------------- SpectralResponse* sr = NULL; sr = getSpectralResponse(dc); if (printlev > 2) cout << "SpectralResponse!" << *sr << endl; TVector outCoeff(nbOfPix); for(int pix=1;pix 10) cout << "RadiationSpectra" << radSV << endl; TVector smallFDeNuT(maxOfSkyComp); for(sk = 0; sk) written to FITS file " << arg[2]<< endl; cout << endl; // --------------------------------------------------------------------- // Integration in the detector band of the sources // --------------------------------------------------------------------- TVector sourceCoeff(nb_source); for(int nsource=0;nsource) written to FITS file " << arg[3]<< endl; cout << endl; // --------------------------------------------------------------------- // Check of write/read on FITS file // --------------------------------------------------------------------- if (printlev > 2) { cout << " Check of write/read on FITS file for skymap!" << endl; TVector outout; FitsIoServer fiotest; fiotest.load(outout, arg[2]); cout << "Coeff(2)" << outout(2) << " ? " << outCoeff(2) << endl; cout << endl; } if (printlev > 2) { cout << " Check of write/read on FITS file for sources!" << endl; FitsIoServer fiotest; TVector outout2; fiotest.load(outout2, arg[3]); cout << "sourceCoeff(2)" << outout2(2) << " ? " << sourceCoeff(2) << endl; } } // End of try block catch (PException exc) { msg = exc.Msg(); cerr << " !!!! extractRS - Catched PException - Msg= " << exc.Msg() << endl; rc = 50; } catch (PThrowable exc) { msg = exc.Msg(); cerr << " !!!! extractRS - Catched PThrowable - Msg= " << exc.Msg() << endl; rc = 50; } catch (...) { cerr << " Une exception de type inconnue !!! " << endl; exit(99); } // Saving the output map in FITS format if (okout) { FitsIoServer fios; fios.save(outgs, arg[2]); cout << "Output Map (TMatrix) written to FITS file " << (string)(arg[2]) << endl; if(printlev>2) PrtTim("End of WriteFITS "); // Saving the output map in PPF format if (narg > 3) { POutPersist s(arg[3]); FIO_TMatrix fiog ; fiog.Write(s); cout << "Output Map (TMatrix) written to POutPersist file " << (string)(arg[3]) << endl; if(printlev>2) 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'; nskycomp = 0; debuglev = 0; printlev = 0; int rc = 0; string key, key2; // Checking datacards if (dc.NbParam("EXT_RS") < 1) { rc = 71; msg = "Invalid parameters - Check @EXT_RS card "; return(rc); } int kc; char buff[256]; bool pb = false; string key3; int ncomp = 0; ncomp = dc.IParam("EXT_RS", 0); for(kc=0; kc 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 */ 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 1) SpectralResponse2Nt(*filt, *so, ppfname); return(filt); } float ComputeFrequency(DataCards & dc,string& key) { ofstream filename("temp_filename"); filename << dc.SParam(key,0) << '\n'; filename.close(); ifstream fromtempfile("temp_filename"); char chtemp; string temp_str; int count = 0; do { fromtempfile.get(chtemp); if(count> 12 && count < 20) { temp_str+= chtemp; } count+=1; } while(chtemp!='\n'); fromtempfile.close(); ofstream filename2("temp_filename"); filename2 << temp_str << '\n'; filename2.close(); int frequency; ifstream fromtempfile2("temp_filename"); fromtempfile2 >> frequency; fromtempfile2.close(); return ((float)frequency/1000.); }