[2079] | 1 | // Pour merger des fichiers FITS cmv 19/06/2002
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <unistd.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <math.h>
|
---|
| 7 | #include <string.h>
|
---|
| 8 | #include "toi.h"
|
---|
| 9 | #include "toimanager.h"
|
---|
| 10 | #include "cgt.h"
|
---|
| 11 | #include "fitstoirdr.h"
|
---|
| 12 | #include "fitstoiwtr.h"
|
---|
| 13 | #include "sophyainit.h"
|
---|
| 14 | #include "strutilxx.h"
|
---|
| 15 | #include "timing.h"
|
---|
| 16 | #include <stdexcept>
|
---|
| 17 |
|
---|
| 18 | struct In_TOI {
|
---|
| 19 | string ColNameIn;
|
---|
| 20 | string ColNameOut;
|
---|
| 21 | bool ColFlagOut;
|
---|
| 22 | vector<string> FileNameIn;
|
---|
| 23 | void Init(void) {
|
---|
| 24 | ColNameIn=""; ColNameOut=""; ColFlagOut=false;
|
---|
| 25 | FileNameIn.resize(0);
|
---|
| 26 | }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | int DecodeDatac(string datacname,vector< struct In_TOI > &TOI_Merge);
|
---|
| 30 | void PrintDatac(vector< struct In_TOI > &TOI_Merge);
|
---|
| 31 | void usage(void);
|
---|
| 32 |
|
---|
| 33 | void usage(void) {
|
---|
| 34 | cout
|
---|
| 35 | <<"toimerge [-h] [-I] [-p lp] [-s samplemin,samplemax] [-w data_window_size]"<<endl
|
---|
| 36 | <<" -o nameout.fits inout.datacard"<<endl
|
---|
| 37 | <<" -p lp : print level (def=0)"<<endl
|
---|
| 38 | <<" -s samplemin,samplemax : sample range to be treated (def=all)"<<endl
|
---|
| 39 | <<" -w data_window_size : window size for pipe (def=8192)"<<endl
|
---|
| 40 | <<" -o : nom du fichier FITS en sortie [toimerge.fits]"<<endl
|
---|
| 41 | <<" inout.datacard : nom datacard descriptif [toimerge.datac]"<<endl;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | ////////////////////////////////////////////////////////////////
|
---|
| 45 | int main(int narg, char** arg)
|
---|
| 46 | {
|
---|
| 47 | //-- Decodage arguments
|
---|
| 48 | int lp=0, width=8192;
|
---|
| 49 | long sdeb,sfin;
|
---|
| 50 | bool gsetstart=false;
|
---|
| 51 | string namefitsout="toimerge.fits";
|
---|
| 52 | string namedatacard="toimerge.datac";
|
---|
| 53 | bool fgsegmented=false;
|
---|
| 54 | int c;
|
---|
| 55 | while((c = getopt(narg,arg,"hp:s:w:o:")) != -1) {
|
---|
| 56 | switch (c) {
|
---|
| 57 | case 's' :
|
---|
| 58 | sscanf(optarg,"%ld,%ld",&sdeb,&sfin);
|
---|
| 59 | cout<<"Requested Samples from "<<sdeb<<" , "<<sfin<<endl;
|
---|
| 60 | gsetstart = true;
|
---|
| 61 | break;
|
---|
| 62 | case 'w' :
|
---|
| 63 | sscanf(optarg,"%d",&width);
|
---|
| 64 | if(width<=0) width=8192;
|
---|
| 65 | cout<<"Data window size "<<width<<endl;
|
---|
| 66 | break;
|
---|
| 67 | case 'p' :
|
---|
| 68 | sscanf(optarg,"%d",&lp);
|
---|
| 69 | if(lp<0) lp=0;
|
---|
| 70 | break;
|
---|
| 71 | case 'o' :
|
---|
| 72 | namefitsout=(string)optarg;
|
---|
| 73 | cout<<"Fits outfile name : "<<namefitsout<<endl;
|
---|
| 74 | break;
|
---|
| 75 | case 'h' :
|
---|
| 76 | default:
|
---|
| 77 | usage(); exit(1);
|
---|
| 78 | break;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | if(optind<narg) {
|
---|
| 82 | namedatacard = arg[optind];
|
---|
| 83 | cout<<"Input datacards : "<<namedatacard<<endl;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | SophyaInit();
|
---|
| 87 | InitTim();
|
---|
| 88 |
|
---|
| 89 | //--------------------------------------------------------------------
|
---|
| 90 | try {
|
---|
| 91 | //--------------------------------------------------------------------
|
---|
| 92 |
|
---|
| 93 | // Decode datacards
|
---|
| 94 | vector< struct In_TOI > TOI_Merge;
|
---|
| 95 | int ntoi = DecodeDatac(namedatacard,TOI_Merge);
|
---|
| 96 | if(lp) PrintDatac(TOI_Merge);
|
---|
| 97 | if(ntoi<=0) throw ParmError("Error: No Connected TOI");
|
---|
| 98 |
|
---|
| 99 | // Set Pipe
|
---|
| 100 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 101 | if(gsetstart) mgr->setRequestedSample(sdeb,sfin);
|
---|
| 102 |
|
---|
| 103 | // Create FITS writer
|
---|
| 104 | cout<<"> Creating FITSTOIWriter OutFitsName= "<<namefitsout<<endl;
|
---|
| 105 | string cmd = "rm -f "; cmd += namefitsout; system(cmd.c_str());
|
---|
| 106 | FITSTOIWriter wfits(namefitsout);
|
---|
| 107 |
|
---|
| 108 | // Create FITS reader table
|
---|
| 109 | cout<<"> Creating "<<ntoi<<" FITSTOIReader"<<endl;
|
---|
| 110 | FITSTOIReader **rfits = new FITSTOIReader*[ntoi];
|
---|
| 111 | for(int i=0;i<ntoi;i++) rfits[i]=NULL;
|
---|
| 112 |
|
---|
| 113 | // Create FITS reader and connect with CGT
|
---|
| 114 | cout<<"> Connecting Processors through plombier ... "<<endl;
|
---|
| 115 | CGT plombier(fgsegmented,width);
|
---|
| 116 | //plombier.SetDebugLevel(lp);
|
---|
| 117 | for(int i=0;i<ntoi;i++) {
|
---|
| 118 | int nread = TOI_Merge[i].FileNameIn.size();
|
---|
| 119 | if(lp>1) cout<<"---TOI "<<i<<", "<<nread<<" input files connected"<<endl;
|
---|
| 120 | string colin = TOI_Merge[i].ColNameIn;
|
---|
| 121 | string colout = TOI_Merge[i].ColNameOut;
|
---|
| 122 | bool withflag = TOI_Merge[i].ColFlagOut;
|
---|
| 123 | if(nread<=0) throw ParmError("Error: No input file for that TOI");
|
---|
| 124 | for(int j=0;j<nread;j++) {
|
---|
| 125 | string fname = TOI_Merge[i].FileNameIn[j];
|
---|
| 126 | if(j==0) rfits[i] = new FITSTOIReader(fname,width);
|
---|
| 127 | else rfits[i]->addFile(fname);
|
---|
| 128 | }
|
---|
| 129 | plombier.Connect(*(rfits[i]),colin,wfits,colout,"",width,withflag);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | // Start Pipe
|
---|
| 133 | cout<<"> Plombier status before start"<<endl;
|
---|
| 134 | cout<<plombier;
|
---|
| 135 | PrtTim("starting processors");
|
---|
| 136 | plombier.Start();
|
---|
| 137 |
|
---|
| 138 | // ------------------- Impression continu de stat ------------------------
|
---|
[2086] | 139 | if(lp>1) {
|
---|
| 140 | sleep(3); // Il faut attendre que le FitsWriter ait demarre
|
---|
| 141 | ProcSampleCounter<FITSTOIWriter> stats(wfits);
|
---|
| 142 | stats.InfoMessage() = "toimerge/Info";
|
---|
| 143 | stats.PrintStats();
|
---|
| 144 | }
|
---|
[2079] | 145 | // -----------------------------------------------------------------------
|
---|
| 146 |
|
---|
| 147 | // End Pipe
|
---|
| 148 | mgr->joinAll();
|
---|
| 149 | PrtTim("End threads");
|
---|
| 150 |
|
---|
| 151 | // Cleaning
|
---|
| 152 | for(int i=0;i<ntoi;i++) if(rfits[i]) delete rfits[i];
|
---|
| 153 | delete rfits;
|
---|
| 154 |
|
---|
| 155 | //--------------------------------------------------------------------
|
---|
| 156 | } catch (PThrowable & exc) {
|
---|
| 157 | cout<<"\ntoimerge: Catched Exception \n"<<(string)typeid(exc).name()
|
---|
| 158 | <<" - Msg= "<<exc.Msg()<<endl;
|
---|
| 159 | } catch (const std::exception & sex) {
|
---|
| 160 | cout<<"\ntoimerge: Catched std::exception \n"
|
---|
| 161 | <<(string)typeid(sex).name()<<endl;
|
---|
[2086] | 162 | } //catch (...) {
|
---|
| 163 | //cout<<"\ntoimerge: some other exception was caught ! "<<endl;
|
---|
| 164 | //}
|
---|
[2079] | 165 | //--------------------------------------------------------------------
|
---|
| 166 |
|
---|
| 167 | exit(0);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | //--------------------------------------------------------------------
|
---|
| 172 | //--------------------------------------------------------------------
|
---|
| 173 | int DecodeDatac(string datacname,vector< struct In_TOI > &TOI_Merge)
|
---|
| 174 | /*
|
---|
| 175 | Format du fichier de datacards:
|
---|
| 176 | @TOI nom_col_int nom_col_out flag
|
---|
| 177 | nom_col_int : nom de la colonne a lire dans le FITS d'entree
|
---|
| 178 | nom_col_out : nom de la colonne a ecrire dans le FITS de sortie
|
---|
| 179 | flag_out = n'importe quoi : ecriture d'une colonne de flag pour ce TOI
|
---|
| 180 | absent : pas de flag pour ce TOI (default)
|
---|
| 181 | @REP nom_rep
|
---|
| 182 | nom_rep : nom du repertoire courant pour lire les fichiers en entree.
|
---|
| 183 | Peut etre change en cours de route. Defaut=./
|
---|
| 184 | @FILE nom_fichier
|
---|
| 185 | nom_fichier : en entree (il est dans le dernier nom_rep appelle).
|
---|
| 186 | Return: le nombre de TOI en entree qui ont ete connectees
|
---|
| 187 |
|
---|
| 188 | --- Exemple:
|
---|
| 189 | #######################################################
|
---|
| 190 | >>> 1er TOI avec ecriture de flag
|
---|
| 191 | @TOI boloMuV_24 bolomuv FLAG
|
---|
| 192 | @REP $PLANCK/Kiruna_2002_02_07/TOI/Full_sampling/Bolos_optfilt
|
---|
| 193 | @FILE b143k03_0.fits
|
---|
| 194 | @FILE b143k03_1.fits
|
---|
| 195 | @FILE b143k03_2.fits
|
---|
| 196 | @REP $PLANCK/Kiruna_2002_02_07/TOI/Full_sampling/Bolos_optfilt_2
|
---|
| 197 | @FILE b143k03_a.fits
|
---|
| 198 | @FILE b143k03_b.fits
|
---|
| 199 | >>> 2sd TOI sans ecriture de flag
|
---|
| 200 | @REP $PLANCK/Kiruna_2002_02_07/TOI/Full_sampling/Pointing_LALv3
|
---|
| 201 | @TOI GLON_24 glon
|
---|
| 202 | @FILE bolo24_gal0.fits
|
---|
| 203 | @FILE bolo24_gal1.fits
|
---|
| 204 | @FILE bolo24_gal2.fits
|
---|
| 205 | @FILE bolo24_gal3.fits
|
---|
| 206 | @FILE bolo24_gal4.fits
|
---|
| 207 | @FILE bolo24_gal5.fits
|
---|
| 208 | @FILE bolo24_gal6.fits
|
---|
| 209 | @TOI GLAT_24 glat
|
---|
| 210 | @FILE bolo24_gal0.fits
|
---|
| 211 | @FILE bolo24_gal1.fits
|
---|
| 212 | @FILE bolo24_gal2.fits
|
---|
| 213 | @FILE bolo24_gal3.fits
|
---|
| 214 | @FILE bolo24_gal4.fits
|
---|
| 215 | @FILE bolo24_gal5.fits
|
---|
| 216 | @FILE bolo24_gal6.fits
|
---|
| 217 | #######################################################
|
---|
| 218 | ############### Fin du fichier datacard ###############
|
---|
| 219 | #######################################################
|
---|
| 220 | */
|
---|
| 221 | {
|
---|
| 222 | #define LENLINE 4096
|
---|
| 223 | char line[LENLINE];
|
---|
| 224 | FILE* FileDatacard = fopen(datacname.c_str(),"r");
|
---|
| 225 | //cout<<">>> DecodeDatac:"<<datacname<<endl;
|
---|
| 226 | if(FileDatacard==NULL) throw IOExc("DecodeDatac: Error opening datacards");
|
---|
| 227 |
|
---|
| 228 | string repcur = "./";
|
---|
| 229 | struct In_TOI intoi;
|
---|
| 230 | int ntoi=-1;
|
---|
| 231 |
|
---|
| 232 | while(fgets(line,LENLINE,FileDatacard) != NULL) {
|
---|
| 233 |
|
---|
| 234 | string sline = line;
|
---|
| 235 | // Attention, il y a un '\n' a la fin de ligne ?
|
---|
| 236 | size_t pos = sline.rfind('\n');
|
---|
| 237 | if(pos<sline.size()) sline.resize(pos);
|
---|
| 238 | //cout<<">>>>"<<sline<<endl;
|
---|
| 239 |
|
---|
| 240 | vector<string> vsline;
|
---|
| 241 | FillVStringFrString(sline,vsline,' ');
|
---|
| 242 | if(vsline.size()==0) continue;
|
---|
| 243 | //for(int i=0;i<vsline.size();i++) cout<<"vsline["<<i<<"]="<<vsline[i]<<endl;
|
---|
| 244 | if(vsline[0][0] != '@' ) continue;
|
---|
| 245 |
|
---|
| 246 | if(strstr(vsline[0].c_str(),"@TOI") == vsline[0].c_str()) {
|
---|
| 247 | if(vsline.size()<3)
|
---|
| 248 | throw ParmError("DecodeDatac: Give a col name for @TOI IN and OUT");
|
---|
| 249 | ntoi++;
|
---|
| 250 | intoi.Init();
|
---|
| 251 | TOI_Merge.push_back(intoi);
|
---|
| 252 | TOI_Merge[ntoi].ColNameIn = vsline[1];
|
---|
| 253 | TOI_Merge[ntoi].ColNameOut = vsline[2];
|
---|
| 254 | TOI_Merge[ntoi].ColFlagOut = false;
|
---|
| 255 | if(vsline.size()>3) TOI_Merge[ntoi].ColFlagOut = true;
|
---|
| 256 | //cout<<"New TOI("<<ntoi<<") ColNameIn="<<vsline[1]<<endl;
|
---|
| 257 | //cout<<" ColNameOut="<<vsline[2]<<" flag="<<TOI_Merge[ntoi].ColFlagOut<<endl;
|
---|
| 258 |
|
---|
| 259 | } else if(strstr(vsline[0].c_str(),"@REP") == vsline[0].c_str()) {
|
---|
| 260 | if(vsline.size()>1) repcur = vsline[1];
|
---|
| 261 | if(repcur.size()>0)
|
---|
| 262 | if(repcur[repcur.size()-1] != '/') repcur += '/';
|
---|
| 263 | //cout<<"repcur: "<<repcur<<endl;
|
---|
| 264 |
|
---|
| 265 | } else if(strstr(vsline[0].c_str(),"@FILE") == vsline[0].c_str()) {
|
---|
| 266 | if(ntoi<0) continue;
|
---|
| 267 | if(vsline.size()<2) throw ParmError("DecodeDatac: Give filename AND colname for @FILE");
|
---|
| 268 | string fname = repcur + vsline[1];
|
---|
| 269 | TOI_Merge[ntoi].FileNameIn.push_back(fname);
|
---|
| 270 | //cout<<"File: "<<fname<<endl;
|
---|
| 271 |
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | return ntoi+1;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | void PrintDatac(vector< struct In_TOI > &TOI_Merge)
|
---|
| 279 | {
|
---|
| 280 | int ntoi = TOI_Merge.size();
|
---|
| 281 | cout<<"PrintDatac: ntoi="<<ntoi<<endl;
|
---|
| 282 | for(int i=0;i<ntoi;i++) {
|
---|
| 283 | int n = TOI_Merge[i].FileNameIn.size();
|
---|
| 284 | cout<<"TOI("<<i<<") NameIn="<<TOI_Merge[i].ColNameIn
|
---|
| 285 | <<" NameOut="<<TOI_Merge[i].ColNameOut
|
---|
| 286 | <<" FlagOut="<<TOI_Merge[i].ColFlagOut
|
---|
| 287 | <<", "<<n<<" connected TOI"<<endl;
|
---|
| 288 | if(n>0) for(int j=0;j<n;j++)
|
---|
| 289 | cout<<" "<<j<<" File="<<TOI_Merge[i].FileNameIn[j]<<endl;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|