[540] | 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 | //-----------------------------------------------
|
---|
[542] | 40 | const sa_size_t NUMBER_OF_CHANNELS = 2;
|
---|
| 41 | const sa_size_t NUMBER_OF_FREQ = 8192;
|
---|
| 42 | const r_4 LOWER_FREQUENCY = 1250.0; //MHz
|
---|
| 43 | const r_4 TOTAL_BANDWIDTH = 250.0; //MHz
|
---|
| 44 | //-----------------------------------------------
|
---|
[540] | 45 | char *regexp (const char *string, const char *patrn, int *begin, int *end) {
|
---|
| 46 | int i, w=0, len;
|
---|
| 47 | char *word = NULL;
|
---|
| 48 | regex_t rgT;
|
---|
| 49 | regmatch_t match;
|
---|
| 50 | regcomp(&rgT,patrn,REG_EXTENDED);
|
---|
| 51 | if ((regexec(&rgT,string,1,&match,0)) == 0) {
|
---|
| 52 | *begin = (int)match.rm_so;
|
---|
| 53 | *end = (int)match.rm_eo;
|
---|
| 54 | len = *end-*begin;
|
---|
| 55 | word=(char*)malloc(len+1);
|
---|
| 56 | for (i=*begin; i<*end; i++) {
|
---|
| 57 | word[w] = string[i];
|
---|
| 58 | w++; }
|
---|
| 59 | word[w]=0;
|
---|
| 60 | }
|
---|
| 61 | regfree(&rgT);
|
---|
| 62 | return word;
|
---|
| 63 | }
|
---|
| 64 | //------------------------------------------------
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | struct Param {
|
---|
[542] | 68 | int debuglev_;
|
---|
[540] | 69 | string inPath_;
|
---|
| 70 | string outPath_;
|
---|
| 71 | string sourceName_;
|
---|
[541] | 72 | string ppfFile_;
|
---|
[540] | 73 | } para;
|
---|
| 74 | //-----
|
---|
| 75 | sa_size_t round_sa(r_4 r) {
|
---|
| 76 | return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5));
|
---|
| 77 | }
|
---|
| 78 | //-----
|
---|
| 79 | string StringToLower(string strToConvert){
|
---|
| 80 | //change each element of the string to lower case
|
---|
| 81 | for(unsigned int i=0;i<strToConvert.length();i++) {
|
---|
| 82 | strToConvert[i] = tolower(strToConvert[i]);
|
---|
| 83 | }
|
---|
| 84 | return strToConvert;//return the converted string
|
---|
| 85 | }
|
---|
| 86 | //-----
|
---|
| 87 | bool stringCompare( const string &left, const string &right ){
|
---|
| 88 | if( left.size() < right.size() )
|
---|
| 89 | return true;
|
---|
| 90 | for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
|
---|
| 91 | if( tolower( *lit ) < tolower( *rit ) )
|
---|
| 92 | return true;
|
---|
| 93 | else if( tolower( *lit ) > tolower( *rit ) )
|
---|
| 94 | return false;
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 | //-----
|
---|
| 98 | list<string> ListOfFileInDir(string dir, string filePettern) throw(string) {
|
---|
| 99 | list<string> theList;
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | DIR *dip;
|
---|
| 103 | struct dirent *dit;
|
---|
| 104 | string msg; string fileName;
|
---|
| 105 | string fullFileName;
|
---|
| 106 | size_t found;
|
---|
| 107 |
|
---|
| 108 | if ((dip=opendir(dir.c_str())) == NULL ) {
|
---|
| 109 | msg = "opendir failed on directory "+dir;
|
---|
| 110 | throw msg;
|
---|
| 111 | }
|
---|
| 112 | while ( (dit = readdir(dip)) != NULL ) {
|
---|
| 113 | fileName = dit->d_name;
|
---|
| 114 | found=fileName.find(filePettern);
|
---|
| 115 | if (found != string::npos) {
|
---|
| 116 | fullFileName = dir + "/";
|
---|
| 117 | fullFileName += fileName;
|
---|
| 118 | theList.push_back(fullFileName);
|
---|
| 119 | }
|
---|
| 120 | }//eo while
|
---|
| 121 | if (closedir(dip) == -1) {
|
---|
| 122 | msg = "closedir failed on directory "+dir;
|
---|
| 123 | throw msg;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | theList.sort(stringCompare);
|
---|
| 127 |
|
---|
| 128 | return theList;
|
---|
| 129 |
|
---|
| 130 | }
|
---|
| 131 | //
|
---|
| 132 | class StringMatch : public unary_function<string,bool> {
|
---|
| 133 | public:
|
---|
| 134 | StringMatch(const string& pattern): pattern_(pattern){}
|
---|
| 135 | bool operator()(const string& aStr) const {
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | int b,e;
|
---|
| 139 | regexp(aStr.c_str(),pattern_.c_str(),&b,&e);
|
---|
| 140 |
|
---|
[541] | 141 | // cout << "investigate " << aStr << " to find " << pattern_
|
---|
| 142 | // << "[" <<b<<","<<e<<"]"
|
---|
| 143 | // << endl;
|
---|
[540] | 144 |
|
---|
| 145 |
|
---|
| 146 | if (b != 0) return false;
|
---|
| 147 | if (e != aStr.size()) return false;
|
---|
| 148 | return true;
|
---|
| 149 |
|
---|
| 150 | }
|
---|
| 151 | private:
|
---|
| 152 | string pattern_;
|
---|
| 153 | };
|
---|
| 154 | //-------------------------------------------------------
|
---|
[541] | 155 | //-------------------------------------------------------
|
---|
[540] | 156 | void meanOnCycles() throw(string) {
|
---|
| 157 | list<string> listOfFiles;
|
---|
| 158 | string directoryName;
|
---|
| 159 | directoryName = para.inPath_ + "/" + para.sourceName_;
|
---|
| 160 |
|
---|
[542] | 161 | //Make the listing of the directory
|
---|
[541] | 162 | listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
|
---|
[540] | 163 |
|
---|
[541] | 164 | list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd;
|
---|
[540] | 165 | iFileEnd = listOfFiles.end();
|
---|
| 166 |
|
---|
[542] | 167 | StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects
|
---|
| 168 | TMatrix<r_4> sumOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
|
---|
| 169 | uint_4 nSpectra=0;
|
---|
| 170 | //Loop on files
|
---|
[540] | 171 | for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
|
---|
[542] | 172 | if (para.debuglev_>90){
|
---|
| 173 | cout << "load file <" << *iFile << ">" << endl;
|
---|
| 174 | }
|
---|
[540] | 175 | PInPersist fin(*iFile);
|
---|
| 176 | vector<string> vec = fin.GetNameTags();
|
---|
[541] | 177 | list<string> listOfSpectra;
|
---|
[542] | 178 | //Keep only required PPF objects
|
---|
[540] | 179 | std::remove_copy_if(
|
---|
| 180 | vec.begin(), vec.end(), back_inserter(listOfSpectra),
|
---|
| 181 | not1(match)
|
---|
| 182 | );
|
---|
| 183 |
|
---|
[541] | 184 | iSpecEnd = listOfSpectra.end();
|
---|
| 185 | listOfSpectra.sort(stringCompare);
|
---|
[542] | 186 | //Loop of spectra matrix
|
---|
[541] | 187 | for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd; ++iSpec){
|
---|
[542] | 188 | if (para.debuglev_>90){
|
---|
| 189 | cout << " spactra <" << *iSpec << ">" << endl;
|
---|
| 190 | }
|
---|
| 191 | TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
|
---|
| 192 | fin.GetObject(aSpec,*iSpec);
|
---|
| 193 | //How to see if the GetObject is ok?? Ask Reza
|
---|
| 194 | nSpectra++;
|
---|
| 195 | sumOfSpectra+=aSpec;
|
---|
| 196 | }//eo loop on spectra in a file
|
---|
| 197 | }//eo loop on files
|
---|
| 198 |
|
---|
| 199 | //Normalisation
|
---|
| 200 | if(nSpectra>0)sumOfSpectra/=(r_4)(nSpectra);
|
---|
[541] | 201 |
|
---|
[542] | 202 | {//Save the result
|
---|
| 203 | stringstream tmp;
|
---|
| 204 | tmp << nSpectra;
|
---|
| 205 | string fileName = para.outPath_+"/meanDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
|
---|
| 206 | cout << "Save mean based on " << nSpectra << " cycles " << endl;
|
---|
| 207 | string tag = "mean";
|
---|
| 208 | POutPersist fos(fileName);
|
---|
| 209 | fos << PPFNameTag(tag) << sumOfSpectra;
|
---|
[540] | 210 | }
|
---|
| 211 | }
|
---|
| 212 | //-------------------------------------------------------
|
---|
| 213 | int main(int narg, char* arg[])
|
---|
| 214 | {
|
---|
| 215 |
|
---|
| 216 | int rc = 0; //return code
|
---|
| 217 | string msg; //message used in Exceptions
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | string debuglev = "0";
|
---|
| 221 | string action = "default";
|
---|
| 222 | string inputPath = ".";
|
---|
| 223 | string outputPath = ".";
|
---|
| 224 | string sourceName = "Abell85";
|
---|
[541] | 225 | string ppfFile;
|
---|
[540] | 226 |
|
---|
| 227 |
|
---|
| 228 | // bool okarg=false;
|
---|
| 229 | int ka=1;
|
---|
| 230 | while (ka<(narg-1)) {
|
---|
| 231 | if (strcmp(arg[ka],"-debug")==0) {
|
---|
| 232 | debuglev=arg[ka+1];
|
---|
| 233 | ka+=2;
|
---|
| 234 | }
|
---|
| 235 | else if (strcmp(arg[ka],"-act")==0) {
|
---|
| 236 | action=arg[ka+1];
|
---|
| 237 | ka+=2;
|
---|
| 238 | }
|
---|
| 239 | else if (strcmp(arg[ka],"-inPath")==0) {
|
---|
| 240 | inputPath=arg[ka+1];
|
---|
| 241 | ka+=2;
|
---|
| 242 | }
|
---|
| 243 | else if (strcmp(arg[ka],"-outPath")==0) {
|
---|
| 244 | outputPath=arg[ka+1];
|
---|
| 245 | ka+=2;
|
---|
| 246 | }
|
---|
[541] | 247 | else if (strcmp(arg[ka],"-src")==0) {
|
---|
[540] | 248 | sourceName=arg[ka+1];
|
---|
| 249 | ka+=2;
|
---|
| 250 | }
|
---|
[541] | 251 | else if (strcmp(arg[ka],"-ppfFile")==0) {
|
---|
| 252 | ppfFile=arg[ka+1];
|
---|
[540] | 253 | ka+=2;
|
---|
| 254 | }
|
---|
| 255 | else ka++;
|
---|
| 256 | }//eo while
|
---|
| 257 |
|
---|
[542] | 258 | para.debuglev_ = atoi(debuglev.c_str());
|
---|
[540] | 259 | para.inPath_ = inputPath;
|
---|
| 260 | para.outPath_ = outputPath;
|
---|
| 261 | para.sourceName_ = sourceName;
|
---|
[541] | 262 | para.ppfFile_ = ppfFile;
|
---|
[540] | 263 |
|
---|
| 264 | cout << "Dump Initial parameters ............" << endl;
|
---|
| 265 | cout << " action = " << action << "\n"
|
---|
| 266 | << " inputPath = " << inputPath << "\n"
|
---|
| 267 | << " outputPath = " << outputPath << "\n"
|
---|
| 268 | << " sourceName = " << sourceName << "\n"
|
---|
[541] | 269 | << " ppfFile = " << ppfFile << "\n"
|
---|
[540] | 270 | << " debuglev = " << debuglev << "\n";
|
---|
| 271 | cout << "...................................." << endl;
|
---|
| 272 |
|
---|
[541] | 273 | if ( "" == ppfFile ) {
|
---|
| 274 | cerr << "mergeRawOnOff.cc: you have forgotten ppfFile option"
|
---|
| 275 | << endl;
|
---|
| 276 | return 999;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
[540] | 280 | try {
|
---|
| 281 |
|
---|
[542] | 282 | // int b,e;
|
---|
| 283 | // char *match=regexp("truc0machin","[a-z]+[0-9]*",&b,&e);
|
---|
| 284 | // printf("->%s<-\n(b=%d e=%d)\n",match,b,e);
|
---|
[540] | 285 |
|
---|
| 286 | if ( action == "default" ) {
|
---|
| 287 | meanOnCycles();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | } catch (std::exception& sex) {
|
---|
| 291 | cerr << "mergeRawOnOff.cc std::exception :" << (string)typeid(sex).name()
|
---|
| 292 | << "\n msg= " << sex.what() << endl;
|
---|
| 293 | rc = 78;
|
---|
| 294 | }
|
---|
| 295 | catch ( string str ) {
|
---|
| 296 | cerr << "mergeRawOnOff.cc Exception raised: " << str << endl;
|
---|
| 297 | }
|
---|
| 298 | catch (...) {
|
---|
| 299 | cerr << "mergeRawOnOff.cc catched unknown (...) exception " << endl;
|
---|
| 300 | rc = 79;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | return 0;
|
---|
| 304 |
|
---|
| 305 | }
|
---|