1 | // Utilisation de SOPHYA pour faciliter les tests ... |
---|
2 | #include <regex.h> |
---|
3 | //#include <regexp.h> |
---|
4 | #include <stdio.h> |
---|
5 | |
---|
6 | #include "sopnamsp.h" |
---|
7 | #include "machdefs.h" |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <dirent.h> |
---|
11 | #include <matharr.h> |
---|
12 | |
---|
13 | // include standard c/c++ |
---|
14 | #include <iostream> |
---|
15 | #include <fstream> |
---|
16 | #include <string> |
---|
17 | #include <vector> |
---|
18 | #include <map> |
---|
19 | #include <functional> |
---|
20 | #include <algorithm> |
---|
21 | #include <numeric> |
---|
22 | #include <list> |
---|
23 | #include <exception> |
---|
24 | |
---|
25 | // include sophya mesure ressource CPU/memoire ... |
---|
26 | #include "resusage.h" |
---|
27 | #include "ctimer.h" |
---|
28 | #include "timing.h" |
---|
29 | #include "timestamp.h" |
---|
30 | #include "strutilxx.h" |
---|
31 | #include "ntuple.h" |
---|
32 | #include "fioarr.h" |
---|
33 | #include "tarrinit.h" |
---|
34 | #include "histinit.h" |
---|
35 | #include "fitsioserver.h" |
---|
36 | #include "fiosinit.h" |
---|
37 | #include "ppersist.h" |
---|
38 | |
---|
39 | //----------------------------------------------- |
---|
40 | char *regexp (const char *string, const char *patrn, int *begin, int *end) { |
---|
41 | int i, w=0, len; |
---|
42 | char *word = NULL; |
---|
43 | regex_t rgT; |
---|
44 | regmatch_t match; |
---|
45 | regcomp(&rgT,patrn,REG_EXTENDED); |
---|
46 | if ((regexec(&rgT,string,1,&match,0)) == 0) { |
---|
47 | *begin = (int)match.rm_so; |
---|
48 | *end = (int)match.rm_eo; |
---|
49 | len = *end-*begin; |
---|
50 | word=(char*)malloc(len+1); |
---|
51 | for (i=*begin; i<*end; i++) { |
---|
52 | word[w] = string[i]; |
---|
53 | w++; } |
---|
54 | word[w]=0; |
---|
55 | } |
---|
56 | regfree(&rgT); |
---|
57 | return word; |
---|
58 | } |
---|
59 | //------------------------------------------------ |
---|
60 | |
---|
61 | |
---|
62 | struct Param { |
---|
63 | string debuglev_; |
---|
64 | string inPath_; |
---|
65 | string outPath_; |
---|
66 | string sourceName_; |
---|
67 | string ppfFile_; |
---|
68 | } para; |
---|
69 | //----- |
---|
70 | sa_size_t round_sa(r_4 r) { |
---|
71 | return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5)); |
---|
72 | } |
---|
73 | //----- |
---|
74 | string StringToLower(string strToConvert){ |
---|
75 | //change each element of the string to lower case |
---|
76 | for(unsigned int i=0;i<strToConvert.length();i++) { |
---|
77 | strToConvert[i] = tolower(strToConvert[i]); |
---|
78 | } |
---|
79 | return strToConvert;//return the converted string |
---|
80 | } |
---|
81 | //----- |
---|
82 | bool stringCompare( const string &left, const string &right ){ |
---|
83 | if( left.size() < right.size() ) |
---|
84 | return true; |
---|
85 | for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit ) |
---|
86 | if( tolower( *lit ) < tolower( *rit ) ) |
---|
87 | return true; |
---|
88 | else if( tolower( *lit ) > tolower( *rit ) ) |
---|
89 | return false; |
---|
90 | return false; |
---|
91 | } |
---|
92 | //----- |
---|
93 | list<string> ListOfFileInDir(string dir, string filePettern) throw(string) { |
---|
94 | list<string> theList; |
---|
95 | |
---|
96 | |
---|
97 | DIR *dip; |
---|
98 | struct dirent *dit; |
---|
99 | string msg; string fileName; |
---|
100 | string fullFileName; |
---|
101 | size_t found; |
---|
102 | |
---|
103 | if ((dip=opendir(dir.c_str())) == NULL ) { |
---|
104 | msg = "opendir failed on directory "+dir; |
---|
105 | throw msg; |
---|
106 | } |
---|
107 | while ( (dit = readdir(dip)) != NULL ) { |
---|
108 | fileName = dit->d_name; |
---|
109 | found=fileName.find(filePettern); |
---|
110 | if (found != string::npos) { |
---|
111 | fullFileName = dir + "/"; |
---|
112 | fullFileName += fileName; |
---|
113 | theList.push_back(fullFileName); |
---|
114 | } |
---|
115 | }//eo while |
---|
116 | if (closedir(dip) == -1) { |
---|
117 | msg = "closedir failed on directory "+dir; |
---|
118 | throw msg; |
---|
119 | } |
---|
120 | |
---|
121 | theList.sort(stringCompare); |
---|
122 | |
---|
123 | return theList; |
---|
124 | |
---|
125 | } |
---|
126 | // |
---|
127 | class StringMatch : public unary_function<string,bool> { |
---|
128 | public: |
---|
129 | StringMatch(const string& pattern): pattern_(pattern){} |
---|
130 | bool operator()(const string& aStr) const { |
---|
131 | |
---|
132 | |
---|
133 | int b,e; |
---|
134 | regexp(aStr.c_str(),pattern_.c_str(),&b,&e); |
---|
135 | |
---|
136 | // cout << "investigate " << aStr << " to find " << pattern_ |
---|
137 | // << "[" <<b<<","<<e<<"]" |
---|
138 | // << endl; |
---|
139 | |
---|
140 | |
---|
141 | if (b != 0) return false; |
---|
142 | if (e != aStr.size()) return false; |
---|
143 | return true; |
---|
144 | |
---|
145 | } |
---|
146 | private: |
---|
147 | string pattern_; |
---|
148 | }; |
---|
149 | //------------------------------------------------------- |
---|
150 | //------------------------------------------------------- |
---|
151 | void meanOnCycles() throw(string) { |
---|
152 | list<string> listOfFiles; |
---|
153 | string directoryName; |
---|
154 | directoryName = para.inPath_ + "/" + para.sourceName_; |
---|
155 | |
---|
156 | listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_); |
---|
157 | |
---|
158 | list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd; |
---|
159 | iFileEnd = listOfFiles.end(); |
---|
160 | |
---|
161 | StringMatch match("specONOFFRaw[0-9]+"); |
---|
162 | for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) { |
---|
163 | cout << "load file <" << *iFile << ">" << endl; |
---|
164 | PInPersist fin(*iFile); |
---|
165 | vector<string> vec = fin.GetNameTags(); |
---|
166 | list<string> listOfSpectra; |
---|
167 | std::remove_copy_if( |
---|
168 | vec.begin(), vec.end(), back_inserter(listOfSpectra), |
---|
169 | not1(match) |
---|
170 | ); |
---|
171 | |
---|
172 | iSpecEnd = listOfSpectra.end(); |
---|
173 | listOfSpectra.sort(stringCompare); |
---|
174 | |
---|
175 | for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd; ++iSpec){ |
---|
176 | cout << " spactra <" << *iSpec << ">" << endl; |
---|
177 | } |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | } |
---|
182 | //------------------------------------------------------- |
---|
183 | int main(int narg, char* arg[]) |
---|
184 | { |
---|
185 | |
---|
186 | int rc = 0; //return code |
---|
187 | string msg; //message used in Exceptions |
---|
188 | |
---|
189 | |
---|
190 | string debuglev = "0"; |
---|
191 | string action = "default"; |
---|
192 | string inputPath = "."; |
---|
193 | string outputPath = "."; |
---|
194 | string sourceName = "Abell85"; |
---|
195 | string ppfFile; |
---|
196 | |
---|
197 | |
---|
198 | // bool okarg=false; |
---|
199 | int ka=1; |
---|
200 | while (ka<(narg-1)) { |
---|
201 | if (strcmp(arg[ka],"-debug")==0) { |
---|
202 | debuglev=arg[ka+1]; |
---|
203 | ka+=2; |
---|
204 | } |
---|
205 | else if (strcmp(arg[ka],"-act")==0) { |
---|
206 | action=arg[ka+1]; |
---|
207 | ka+=2; |
---|
208 | } |
---|
209 | else if (strcmp(arg[ka],"-inPath")==0) { |
---|
210 | inputPath=arg[ka+1]; |
---|
211 | ka+=2; |
---|
212 | } |
---|
213 | else if (strcmp(arg[ka],"-outPath")==0) { |
---|
214 | outputPath=arg[ka+1]; |
---|
215 | ka+=2; |
---|
216 | } |
---|
217 | else if (strcmp(arg[ka],"-src")==0) { |
---|
218 | sourceName=arg[ka+1]; |
---|
219 | ka+=2; |
---|
220 | } |
---|
221 | else if (strcmp(arg[ka],"-ppfFile")==0) { |
---|
222 | ppfFile=arg[ka+1]; |
---|
223 | ka+=2; |
---|
224 | } |
---|
225 | else ka++; |
---|
226 | }//eo while |
---|
227 | |
---|
228 | para.debuglev_ = debuglev; |
---|
229 | para.inPath_ = inputPath; |
---|
230 | para.outPath_ = outputPath; |
---|
231 | para.sourceName_ = sourceName; |
---|
232 | para.ppfFile_ = ppfFile; |
---|
233 | |
---|
234 | cout << "Dump Initial parameters ............" << endl; |
---|
235 | cout << " action = " << action << "\n" |
---|
236 | << " inputPath = " << inputPath << "\n" |
---|
237 | << " outputPath = " << outputPath << "\n" |
---|
238 | << " sourceName = " << sourceName << "\n" |
---|
239 | << " ppfFile = " << ppfFile << "\n" |
---|
240 | << " debuglev = " << debuglev << "\n"; |
---|
241 | cout << "...................................." << endl; |
---|
242 | |
---|
243 | if ( "" == ppfFile ) { |
---|
244 | cerr << "mergeRawOnOff.cc: you have forgotten ppfFile option" |
---|
245 | << endl; |
---|
246 | return 999; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | try { |
---|
251 | |
---|
252 | int b,e; |
---|
253 | char *match=regexp("truc0machin","[a-z]+[0-9]*",&b,&e); |
---|
254 | printf("->%s<-\n(b=%d e=%d)\n",match,b,e); |
---|
255 | |
---|
256 | if ( action == "default" ) { |
---|
257 | meanOnCycles(); |
---|
258 | } |
---|
259 | |
---|
260 | } catch (std::exception& sex) { |
---|
261 | cerr << "mergeRawOnOff.cc std::exception :" << (string)typeid(sex).name() |
---|
262 | << "\n msg= " << sex.what() << endl; |
---|
263 | rc = 78; |
---|
264 | } |
---|
265 | catch ( string str ) { |
---|
266 | cerr << "mergeRawOnOff.cc Exception raised: " << str << endl; |
---|
267 | } |
---|
268 | catch (...) { |
---|
269 | cerr << "mergeRawOnOff.cc catched unknown (...) exception " << endl; |
---|
270 | rc = 79; |
---|
271 | } |
---|
272 | |
---|
273 | return 0; |
---|
274 | |
---|
275 | } |
---|