source: Sophya/trunk/ArchTOIPipe/TestPipes/toimerge.cc@ 2079

Last change on this file since 2079 was 2079, checked in by cmv, 23 years ago

merge de TOI (essai ca plante!) cmv 19/06/2002

File size: 8.9 KB
Line 
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
18struct 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
29int DecodeDatac(string datacname,vector< struct In_TOI > &TOI_Merge);
30void PrintDatac(vector< struct In_TOI > &TOI_Merge);
31void usage(void);
32
33void 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////////////////////////////////////////////////////////////////
45int main(int narg, char** arg)
46{
47//-- Decodage arguments
48int lp=0, width=8192;
49long sdeb,sfin;
50bool gsetstart=false;
51string namefitsout="toimerge.fits";
52string namedatacard="toimerge.datac";
53bool fgsegmented=false;
54int c;
55while((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}
81if(optind<narg) {
82 namedatacard = arg[optind];
83 cout<<"Input datacards : "<<namedatacard<<endl;
84}
85
86SophyaInit();
87InitTim();
88
89//--------------------------------------------------------------------
90try {
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 ------------------------
139 //if(lp>1) {
140 // ProcSampleCounter<FITSTOIWriter> stats(wfits);
141 // stats.InfoMessage() = "toimerge/Info";
142 // stats.PrintStats();
143 //}
144 // -----------------------------------------------------------------------
145
146 // End Pipe
147 mgr->joinAll();
148 PrtTim("End threads");
149
150 // Cleaning
151 for(int i=0;i<ntoi;i++) if(rfits[i]) delete rfits[i];
152 delete rfits;
153
154//--------------------------------------------------------------------
155} catch (PThrowable & exc) {
156 cout<<"\ntoimerge: Catched Exception \n"<<(string)typeid(exc).name()
157 <<" - Msg= "<<exc.Msg()<<endl;
158} catch (const std::exception & sex) {
159 cout<<"\ntoimerge: Catched std::exception \n"
160 <<(string)typeid(sex).name()<<endl;
161} catch (...) {
162 cout<<"\ntoimerge: some other exception was caught ! "<<endl;
163}
164//--------------------------------------------------------------------
165
166exit(0);
167}
168
169
170//--------------------------------------------------------------------
171//--------------------------------------------------------------------
172int DecodeDatac(string datacname,vector< struct In_TOI > &TOI_Merge)
173/*
174Format du fichier de datacards:
175@TOI nom_col_int nom_col_out flag
176 nom_col_int : nom de la colonne a lire dans le FITS d'entree
177 nom_col_out : nom de la colonne a ecrire dans le FITS de sortie
178 flag_out = n'importe quoi : ecriture d'une colonne de flag pour ce TOI
179 absent : pas de flag pour ce TOI (default)
180@REP nom_rep
181 nom_rep : nom du repertoire courant pour lire les fichiers en entree.
182 Peut etre change en cours de route. Defaut=./
183@FILE nom_fichier
184 nom_fichier : en entree (il est dans le dernier nom_rep appelle).
185Return: le nombre de TOI en entree qui ont ete connectees
186
187--- Exemple:
188#######################################################
189>>> 1er TOI avec ecriture de flag
190@TOI boloMuV_24 bolomuv FLAG
191@REP $PLANCK/Kiruna_2002_02_07/TOI/Full_sampling/Bolos_optfilt
192 @FILE b143k03_0.fits
193 @FILE b143k03_1.fits
194 @FILE b143k03_2.fits
195@REP $PLANCK/Kiruna_2002_02_07/TOI/Full_sampling/Bolos_optfilt_2
196 @FILE b143k03_a.fits
197 @FILE b143k03_b.fits
198>>> 2sd TOI sans ecriture de flag
199@REP $PLANCK/Kiruna_2002_02_07/TOI/Full_sampling/Pointing_LALv3
200@TOI GLON_24 glon
201 @FILE bolo24_gal0.fits
202 @FILE bolo24_gal1.fits
203 @FILE bolo24_gal2.fits
204 @FILE bolo24_gal3.fits
205 @FILE bolo24_gal4.fits
206 @FILE bolo24_gal5.fits
207 @FILE bolo24_gal6.fits
208@TOI GLAT_24 glat
209 @FILE bolo24_gal0.fits
210 @FILE bolo24_gal1.fits
211 @FILE bolo24_gal2.fits
212 @FILE bolo24_gal3.fits
213 @FILE bolo24_gal4.fits
214 @FILE bolo24_gal5.fits
215 @FILE bolo24_gal6.fits
216#######################################################
217############### Fin du fichier datacard ###############
218#######################################################
219*/
220{
221#define LENLINE 4096
222 char line[LENLINE];
223 FILE* FileDatacard = fopen(datacname.c_str(),"r");
224 //cout<<">>> DecodeDatac:"<<datacname<<endl;
225 if(FileDatacard==NULL) throw IOExc("DecodeDatac: Error opening datacards");
226
227 string repcur = "./";
228 struct In_TOI intoi;
229 int ntoi=-1;
230
231 while(fgets(line,LENLINE,FileDatacard) != NULL) {
232
233 string sline = line;
234 // Attention, il y a un '\n' a la fin de ligne ?
235 size_t pos = sline.rfind('\n');
236 if(pos<sline.size()) sline.resize(pos);
237 //cout<<">>>>"<<sline<<endl;
238
239 vector<string> vsline;
240 FillVStringFrString(sline,vsline,' ');
241 if(vsline.size()==0) continue;
242 //for(int i=0;i<vsline.size();i++) cout<<"vsline["<<i<<"]="<<vsline[i]<<endl;
243 if(vsline[0][0] != '@' ) continue;
244
245 if(strstr(vsline[0].c_str(),"@TOI") == vsline[0].c_str()) {
246 if(vsline.size()<3)
247 throw ParmError("DecodeDatac: Give a col name for @TOI IN and OUT");
248 ntoi++;
249 intoi.Init();
250 TOI_Merge.push_back(intoi);
251 TOI_Merge[ntoi].ColNameIn = vsline[1];
252 TOI_Merge[ntoi].ColNameOut = vsline[2];
253 TOI_Merge[ntoi].ColFlagOut = false;
254 if(vsline.size()>3) TOI_Merge[ntoi].ColFlagOut = true;
255 //cout<<"New TOI("<<ntoi<<") ColNameIn="<<vsline[1]<<endl;
256 //cout<<" ColNameOut="<<vsline[2]<<" flag="<<TOI_Merge[ntoi].ColFlagOut<<endl;
257
258 } else if(strstr(vsline[0].c_str(),"@REP") == vsline[0].c_str()) {
259 if(vsline.size()>1) repcur = vsline[1];
260 if(repcur.size()>0)
261 if(repcur[repcur.size()-1] != '/') repcur += '/';
262 //cout<<"repcur: "<<repcur<<endl;
263
264 } else if(strstr(vsline[0].c_str(),"@FILE") == vsline[0].c_str()) {
265 if(ntoi<0) continue;
266 if(vsline.size()<2) throw ParmError("DecodeDatac: Give filename AND colname for @FILE");
267 string fname = repcur + vsline[1];
268 TOI_Merge[ntoi].FileNameIn.push_back(fname);
269 //cout<<"File: "<<fname<<endl;
270
271 }
272 }
273
274 return ntoi+1;
275}
276
277void PrintDatac(vector< struct In_TOI > &TOI_Merge)
278{
279 int ntoi = TOI_Merge.size();
280 cout<<"PrintDatac: ntoi="<<ntoi<<endl;
281 for(int i=0;i<ntoi;i++) {
282 int n = TOI_Merge[i].FileNameIn.size();
283 cout<<"TOI("<<i<<") NameIn="<<TOI_Merge[i].ColNameIn
284 <<" NameOut="<<TOI_Merge[i].ColNameOut
285 <<" FlagOut="<<TOI_Merge[i].ColFlagOut
286 <<", "<<n<<" connected TOI"<<endl;
287 if(n>0) for(int j=0;j<n;j++)
288 cout<<" "<<j<<" File="<<TOI_Merge[i].FileNameIn[j]<<endl;
289 }
290}
Note: See TracBrowser for help on using the repository browser.