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