Index: trunk/AddOn/TAcq/mcrd.cc
===================================================================
--- trunk/AddOn/TAcq/mcrd.cc	(revision 3640)
+++ trunk/AddOn/TAcq/mcrd.cc	(revision 3645)
@@ -39,4 +39,5 @@
 #include "brfitsrd.h"
 #include "brproc.h"
+#include "racqurw.h"
 
 
@@ -53,19 +54,26 @@
 }
 
+//--  Parametres globaux
+static int NZones=4;
+static int NPaqinZone=128;
+static int NMean=1024;
+static int NGenZ=100;
+static int GPaqSz=16424;
+static double LossRate=0.1;
+// ----
+int Usage(bool fgshort=true);
+// Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
+int Proc1FRawA(string& outname, string& inpath, int jf1, int jf2, bool fgnotrl=false, 
+               bool fgrdfile=true);
+// Pour traitement (calcul FFT et visibilites (ProcA,ProcB) 2 fibre, 4 voies RAW)
+int Proc2FRawAB(string& outname, string& path1, string& path2, int jf1, int jf2, 
+                bool fgnotrl=false, bool fgrdfile=true);
+
 //----------------------------------------------------
 //----------------------------------------------------
 int main(int narg, char* arg[])
 {
-  if (narg < 4) {
-    cout << " --- Reading/Processing BAORadio FITS files" << endl;
-    cout << " Usage:  mcrd ACT OutPPF -infits DirName Imin Imax [NZones,NbPaqinZone] " << endl;
-    cout << " ACT= notrl FITS files without frame trailer \n" 
-         << " OutPPF : Output PPF file name (without .ppf)" << endl;
-    cout << " -infiles DirName Imin Imax : Input fits directory and sequence numbers \n" 
-         << "    FileNames=DirName/signalII.fits Imin<=II<=Imax \n"
-         << " - NZones,NbPaqinZone : Number of Zones and number of paquets in one zone\n" 
-         << "     of the RAcqMemZoneMgr memory manager (default = 4,128) " << endl;
-    return 1;
-  }
+  if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage(false);
+  if (narg<5) return Usage(true);
 
   TArrayInitiator  _inia;
@@ -74,50 +82,32 @@
   try {
     string act = arg[1];
-    string outname = arg[2];
-    vector<string> infiles;
-    if ((strcmp(arg[3],"-infits")!=0) || (narg<7)) {
-        cout << " mcrd/Error arguments - type mcrd for help" << endl;
-        return 2;
-    }
-    char nbuff[1024];
-    char* dirname = arg[4]; 
-    int imin = atoi(arg[5]);
-    int imax = atoi(arg[6]);
-    for(int ii=imin; ii<=imax; ii++) {
-      sprintf(nbuff,"%s/signal%d.fits",dirname,ii);
-      infiles.push_back(nbuff);
-    }
-    int nzones = 4;
-    int npaqz = 128;
-    if (narg>7) sscanf(arg[7],"%d,%d",&nzones,&npaqz);	
+    bool fg4c=false;  // true -> 4 channels (2 fibers)
+    bool fgrdfits=false;  // false -> Don't read fits files, generate paquets
+    bool fgnotrl=false;   // true -> fichier fits SANS Trailer de frame (< mai 2009)
+    if (act.substr(0,2)=="-4") fg4c=true;
+    if (act.substr(2)=="g")  fgrdfits=false;
+    else if (act.substr(2)=="nt")  fgnotrl=true;
+    if (fg4c && (narg<6)) return Usage(true);
+
+    string outname = arg[2];    
+    string inpath2;
+    string inpath = arg[3]; 
+    int offa=4;
+    if(fg4c) { 
+      inpath2=arg[4];    offa=5; 
+    } 
+    int imin=0;
+    int imax=0;
+    sscanf(arg[offa],"%d,%d",&imin,&imax);
+    if (narg>offa+1) sscanf(arg[offa+1],"%d,%d",&NZones,&NPaqinZone);	
+    if (narg>offa+2) NMean=atoi(arg[offa+2]);
+    if (narg>offa+3) sscanf(arg[offa+3],"%d,%d,%lg",&GPaqSz,&NGenZ,&LossRate);	
 
     cout << " ---------- mcrd.cc Start - ACT= " << act << " ------------- " << endl;
     ResourceUsage resu;
-    bool fgnotrl=false;   // fichier fits avec Trailer de frame (> mai 2009)
-    if (act=="notrl") fgnotrl=true;  // fichier fits SANS trailer 
-
-    uint_4 paqsz, npaqf;
-    DecodeMiniFitsHeader(infiles[0],paqsz, npaqf, fgnotrl); 
-
-    cout << " mcrd: NZones=" << nzones << " NbPaq=" << npaqz << "(in file=" << npaqf
-         << " ) PaqSz=" << paqsz << endl;
-         
-    RAcqMemZoneMgr mmgr(nzones, npaqz, paqsz, sizeof(complex<r_4>)*paqsz);
-    mmgr.SetFinalizedMask((uint_4)MemZS_ProcA);
-
-    BRFitsReader reader(mmgr, infiles, fgnotrl);
-    BRProcARaw2C proc(mmgr, outname, npaqf, 1, npaqf*infiles.size()/npaqz);
-
-    cout << " mcrd.cc : Starting threads (reader, proc) ... " << endl;
-    reader.start();
-    proc.start();
-    sleep(1);
-    cout << " mcrd.cc : Waiting for reader thread to finish ... " << endl;
-    reader.join();
-    cout << " mcrd.cc : Reader finished, waiting for process thread to finish ... " << endl;
-    sleep(2);
-    mmgr.Stop();
-    proc.join();
-    mmgr.Print(cout);
+    if (fg4c) 
+      rc =  Proc2FRawAB(outname, inpath, inpath2, imin, imax, fgnotrl, fgrdfits); 
+    else 
+      rc =  Proc1FRawA(outname, inpath, imin, imax, fgnotrl, fgrdfits); 
     cout << resu ; 
   }
@@ -137,6 +127,178 @@
   } 
 
-  cout << ">>>> mcrd.cc ------- FIN ----------- RC=" << rc << endl;
+  cout << ">>>> mcrd.cc ------- END ----------- RC=" << rc << endl;
   return rc;
 
 }
+
+
+
+// Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
+int Proc1FRawA(string& outname, string& inpath, int imin, int imax, 
+               bool fgnotrl, bool fgrdfile)
+{
+  vector<string> infiles;
+  char nbuff[1024];
+  for(int ii=imin; ii<=imax; ii++) {
+    sprintf(nbuff,"%s/signal%d.fits",inpath.c_str(),ii);
+    infiles.push_back(nbuff);
+  }
+  uint_4 nmaxz;
+  uint_4 paqsz, npaqf;
+  if (fgrdfile) {
+    DecodeMiniFitsHeader(infiles[0],paqsz, npaqf, fgnotrl); 
+    nmaxz = infiles.size()*npaqf/NPaqinZone;
+    cout << " mcrd/Proc1FRawA/ReadFits: NZones=" << NZones << " NbPaq=" << NPaqinZone 
+         << " NPaq in file=" << npaqf
+         << " PaqSz=" << paqsz << " NMaxZ=" << nmaxz << " NMean=" << NMean << endl;
+  }
+  else {
+    paqsz = GPaqSz;
+    nmaxz = NGenZ;
+    cout << " mcrd/Proc1FRawA/GeneratePaquets: NZones=" << NZones << " NbPaq=" << NPaqinZone 
+         << "  PaqSz=" << paqsz << " NMean=" << NMean << endl;
+  }
+  RAcqMemZoneMgr mmgr(NZones, NPaqinZone, paqsz, sizeof(complex<r_4>)*paqsz);
+  mmgr.SetFinalizedMask((uint_4)MemZS_ProcA);
+
+  BRFitsReader reader(mmgr, infiles, fgnotrl);
+  TestPCIWrapperNODMA pciw(paqsz,LossRate);
+  PCIEReader pcird(pciw, paqsz, paqsz, mmgr, nmaxz, BR_Copy);
+
+  outname += "/Ch12";
+  BRProcARaw2C proc(mmgr, outname, NMean, nmaxz, fgnotrl);
+
+  cout << " mcrd/Proc1FRawA: Starting threads (reader, proc) ... " << endl;
+
+  if (fgrdfile) reader.start();
+  else pcird.start();
+
+  proc.start();
+  sleep(1);
+  cout << " mcrd/Proc1FRawA: Waiting for reader thread to finish ... " << endl;
+  if (fgrdfile) reader.join();
+  else pcird.join();
+  cout << " mcrd/Proc1FRawA: Reader finished, waiting for process thread to finish ... " << endl;
+  sleep(2);
+  mmgr.Stop();
+  proc.join();
+  mmgr.Print(cout);
+  return 0;
+}
+
+
+// Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
+int Proc2FRawAB(string& outname, string& path1, string& path2, int imin, int imax, 
+               bool fgnotrl, bool fgrdfile)
+{
+  vector<string> infiles1;
+  vector<string> infiles2;
+  char nbuff[1024];
+  for(int ii=imin; ii<=imax; ii++) {
+    sprintf(nbuff,"%s/signal%d.fits",path1.c_str(),ii);
+    infiles1.push_back(nbuff);
+    sprintf(nbuff,"%s/signal%d.fits",path2.c_str(),ii);
+    infiles2.push_back(nbuff);
+  }
+  uint_4 nmaxz;
+  uint_4 paqsz, npaqf;
+  if (fgrdfile) {
+    DecodeMiniFitsHeader(infiles1[0],paqsz, npaqf, fgnotrl); 
+    nmaxz = infiles1.size()*npaqf/NPaqinZone;
+    cout << " mcrd/Proc2FRawAB/ReadFits: NZones=" << NZones << " NbPaq=" << NPaqinZone 
+         << " NPaq in file=" << npaqf
+         << " PaqSz=" << paqsz << " NMaxZ=" << nmaxz << " NMean=" << NMean << endl;
+  }
+  else {
+    paqsz = GPaqSz;
+    nmaxz = NGenZ;
+    cout << " mcrd/Proc2FRawAB/GeneratePaquets: NZones=" << NZones << " NbPaq=" << NPaqinZone 
+         << "  PaqSz=" << paqsz << " NMean=" << NMean << endl;
+  }
+  RAcqMemZoneMgr mmgr1(NZones, NPaqinZone, paqsz, sizeof(complex<r_4>)*paqsz);
+  mmgr1.SetFinalizedMask((uint_4)(MemZS_ProcA)|(uint_4)(MemZS_ProcB));
+//  mmgr1.SetFinalizedMask((uint_4)(MemZS_ProcA));
+  RAcqMemZoneMgr mmgr2(NZones, NPaqinZone, paqsz, sizeof(complex<r_4>)*paqsz);
+  mmgr2.SetFinalizedMask((uint_4)(MemZS_ProcA)|(uint_4)(MemZS_ProcB));
+//  mmgr2.SetFinalizedMask((uint_4)(MemZS_ProcA));
+
+  BRFitsReader reader1(mmgr1, infiles1, fgnotrl);
+  BRFitsReader reader2(mmgr1, infiles2, fgnotrl);
+
+  TestPCIWrapperNODMA pciw1(paqsz,LossRate);
+  PCIEReader pcird1(pciw1, paqsz, paqsz, mmgr1, nmaxz, BR_Copy);
+  TestPCIWrapperNODMA pciw2(paqsz,LossRate);
+  PCIEReader pcird2(pciw2, paqsz, paqsz, mmgr2, nmaxz, BR_Copy);
+
+  string outname1 = outname;
+  outname1 += "/Ch12";
+  BRProcARaw2C proc1(mmgr1, outname1, NMean, nmaxz, fgnotrl);
+  string outname2 = outname;
+  outname2 += "/Ch34";
+  BRProcARaw2C proc2(mmgr2, outname2, NMean, nmaxz, fgnotrl,2);
+  string outname12 = outname;
+  outname12 += "/Ch1234";
+  BRProcBRaw4C proc12(mmgr1, mmgr2, outname12, NMean, nmaxz, fgnotrl);
+
+  cout << " mcrd/Proc2FRawAB: Starting threads (reader1,2, procA1,2, procAB) ... " << endl;
+
+  if (fgrdfile) { 
+    reader1.start();
+    reader2.start();
+  }
+  else { 
+    pcird1.start();
+    pcird2.start();
+  }
+  proc1.start();
+  proc2.start();
+  proc12.start();
+
+  sleep(1);
+  cout << " mcrd/Proc2FRawAB: Waiting for reader threads to finish ... " << endl;
+  if (fgrdfile) { 
+    reader1.join();
+    reader2.join();
+  }
+  else { 
+    pcird1.join();
+    pcird2.join();
+  }
+  cout << " mcrd/Proc2FRawAB: Readers finished, waiting for process thread to finish ... " << endl;
+  sleep(2);
+  mmgr1.Stop();
+  mmgr2.Stop();
+  proc1.join();
+  proc2.join();
+  proc12.join();
+  mmgr1.Print(cout);
+  mmgr2.Print(cout);
+  return 0;
+}
+
+
+/* --Fonction-- */
+int Usage(bool fgshort)
+{
+  cout << " --- mcrd.cc : Reading/Processing BAORadio FITS files" << endl;
+  cout << " Usage:  mcrd ACT OutPath InPath [InPath2] Imin,Imax\n" 
+       << "              [NZones,NPaqinZone] [NMean] [GPaqSz,NGenZones,LossRate]" << endl;
+  if (fgshort) {
+    cout << " mcrd -h for detailed instructions" << endl;
+    return 1;
+  }
+  cout << " ACT= -2 , -2g , -2nt -> 1 fiber, 2 raw channel processing (ProcA)\n"
+       << " ACT= -4 , -4g , -4g , -4nt -> 2 fibers, 4 raw channels (ProcA, ProcB)\n"
+       << "   nt (notrl) -> FITS files without frame trailer \n" 
+       << "   g (generate paquets) -> generate paquets instead of reading fits files" <<endl; 
+  cout << " OutPath : Output directory name " << endl;
+  cout << " InPath [InPath2] Imin,Imax: Input fits files directory name(s)\n"
+       << "    (Inpath=Fiber1, InPath2=Fiber2) and sequence numbers \n" 
+       << "    FileNames=InPath/signalII.fits Imin<=II<=Imax \n"
+       << " NZones,NbPaqinZone : Number of Zones and number of paquets in one zone\n"
+       << "     of the RAcqMemZoneMgr memory manager (default = 4,128)\n"
+       << " NMean: Number of packet used for spectra/visibility computation (def=1024)\n"
+       << " GPaqSz,NGenZones,LossRate: Paquet Size, Number of memory zones filled and\n"
+       << "      loss rate (-2g,-4g) , default=16424,100,0.1" << endl;
+  return 1;
+}
