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

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

intermediaire cmv echanch EA 13/9/02

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