Index: /trunk/AddOn/TAcq/brfitsrd.cc
===================================================================
--- /trunk/AddOn/TAcq/brfitsrd.cc	(revision 3645)
+++ /trunk/AddOn/TAcq/brfitsrd.cc	(revision 3646)
@@ -42,5 +42,5 @@
     TimeStamp ts; 
     Timer tm("BRFitsReader", false);
-    BRPaqChecker pcheck(~fgnotrl_);  // Verification/comptage des paquets 
+    BRPaqChecker pcheck(!fgnotrl_);  // Verification/comptage des paquets 
     
     size_t totnbytesrd = 0;
@@ -72,5 +72,5 @@
       if (fgnotrl_) {
         incpaqsz=16;
-        cout << " Warning : FITS files without frame trailers ..." << endl;
+        if (ifile==0) cout << " Warning : FITS files without frame trailers ..." << endl;
       }
       if (paqsz == 0)  {  // premier passage, on fixe la taille de paquet et on verifie compatibilite avec memgr
Index: /trunk/AddOn/TAcq/brfitsrd.h
===================================================================
--- /trunk/AddOn/TAcq/brfitsrd.h	(revision 3645)
+++ /trunk/AddOn/TAcq/brfitsrd.h	(revision 3646)
Index: /trunk/AddOn/TAcq/brproc.cc
===================================================================
--- /trunk/AddOn/TAcq/brproc.cc	(revision 3645)
+++ /trunk/AddOn/TAcq/brproc.cc	(revision 3646)
@@ -9,4 +9,5 @@
 #include "pexceptions.h"
 #include "tvector.h"
+#include "ntuple.h"
 #include "fioarr.h"
 #include "timestamp.h"
@@ -63,5 +64,5 @@
     Timer tm("BRProcARaw2C", false);
     TimeStamp ts; 
-    BRPaqChecker pcheck(~fgnotrl_);  // Verification/comptage des paquets 
+    BRPaqChecker pcheck(!fgnotrl_);  // Verification/comptage des paquets 
     
     size_t totnbytesout = 0;
@@ -77,8 +78,16 @@
 //    filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;	
 
+// NTuple 
+    const char* nnames[8] = {"fcs","tts","s1","s2","s12","s12re","s12im","s12phi"};
+    NTuple nt(8, nnames);
+    double xnt[10];
+    uint_4 nmnt = 0;
+    double ms1,ms2,ms12,ms12re,ms12im,ms12phi;
 // Initialisation pour calcul FFT 
     TVector< complex<r_4> > cfour1;  // composant TF
     uint_4 paqsz = memgr.PaqSize();
     uint_4 procpaqsz = memgr.ProcPaqSize();
+    
+   
     BRPaquet pq(NULL, NULL, paqsz); 
     TVector<r_4> vx(pq.DataSize()/2);
@@ -86,4 +95,5 @@
     FFTPackServer ffts;
     ffts.FFTForward(vx, cfour1);
+//    cfour1.SetSize((paqsz-40)/2+1);
     TVector< complex<r_4> > cfour2(cfour1.Size());
     
@@ -92,4 +102,7 @@
     TVector< complex<r_4> > visiV12( cfour1.Size() );
 
+    cout << " *DBG*BRProcARaw2C PaqSz=" << paqsz << " ProcPaqSize=" << procpaqsz 
+         << " procpaqsz/2=" << procpaqsz/2 << " cfour1.Size()=" << cfour1.Size()
+         << " *8="  << cfour1.Size()*8 << endl;
 
     fftwf_plan plan1 = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(), 
@@ -99,5 +112,5 @@
 
     uint_4 ifile = 0;  			        
-    uint_4 nzm = 0;
+    uint_4 nzm = 0;  
     for (uint_4 kmz=0; kmz<nmax_; kmz++) {
       if (stop_) break;
@@ -113,15 +126,15 @@
         break;	
       }
-
+      nmnt=0;  ms1=ms2=ms12=ms12re=ms12im=ms12phi=0.;
       for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
   	    BRPaquet paq(NULL, buff+i*paqsz, paqsz); 
         if (!pcheck.Check(paq)) continue;   // on ne traite que les paquets OK
-
+        
 // Traitement voie 1  	    
-  	  for(sa_size_t j=0; j<vx.Size(); j++) 
-  	    vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
+  	    for(sa_size_t j=0; j<vx.Size(); j++) 
+  	      vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
 //        fftwf_complex* coeff1 = (fftwf_complex*)(procbuff+i*procpaqsz);
-  	  fftwf_execute(plan1); 
-//        complex<r_4>* zp1 = (complex<r_4>*)(coeff1);
+  	    fftwf_execute(plan1); 
+//  	  complex<r_4>* zp1 = (complex<r_4>*)(vx.Data());
 //        ffts.FFTForward(vx, cfour1);
         for(sa_size_t j=0; j<spectreV1.Size(); j++) 
@@ -129,9 +142,8 @@
         memcpy(procbuff+i*procpaqsz, cfour1.Data(), sizeof(complex<r_4>)*cfour1.Size());
 // Traitement voie 2  	    
-   	for(sa_size_t j=0; j<vx.Size(); j++) 
- 	  vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
-
-  	fftwf_execute(plan2); 
-
+   	    for(sa_size_t j=0; j<vx.Size(); j++) 
+ 	      vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
+
+        fftwf_execute(plan2); 
         for(sa_size_t j=0; j<spectreV2.Size(); j++) 
           spectreV2(j) += Zmod2(cfour2(j));  // Zmod2(zp2[j]); 
@@ -143,8 +155,26 @@
 //        for(sa_size_t j=0; j<visiV12.Size(); j++) visiV12(j)+=zp1[j]*zp2[j];
         nzm++;   
+        if (nmnt==0)  { xnt[0]=paq.FrameCounter();  xnt[1]=paq.TimeTag(); }
+        for(sa_size_t j=2700; j<2800; j++) { 
+          ms1 += Zmod2(cfour1(j)); ms2 += Zmod2(cfour2(j)); 
+          complex<r_4> zvis =  cfour1(j)*conj(cfour2(j));
+          ms12 += Zmod2(zvis);   ms12re += zvis.real();  ms12im += zvis.imag();
+          ms12phi+= atan2(zvis.imag(),zvis.real());
+        } 
+        nmnt++;    
         totnbytesproc += paq.DataSize();
         totnbytesout += (2*sizeof(complex<r_4>)*cfour1.Size());
 
       } // Fin de boucle sur les paquets d'une zone
+      if (nmnt>0)  {
+      	double fnorm = (double)nmnt*(2800-2700); 
+      	xnt[2] = ms1 /= fnorm;
+      	xnt[3] = ms2 /= fnorm;
+      	xnt[4] = ms12 /= fnorm;
+      	xnt[5] = ms12re /= fnorm;
+      	xnt[6] = ms12im /= fnorm;
+      	xnt[7] = ms12phi /= fnorm;
+        nt.Fill(xnt);
+      }
       if ((nzm >= nmean_) || ((kmz==(nmax_-1))&&(nzm>1))) {
         spectreV1 /= (r_4)(nzm);
@@ -184,4 +214,11 @@
   cout << " ------------  BRProcARaw2C::run() END " << card2name_(card_) 
        << " ------------ " << endl;
+  {     
+  cout << nt;
+  sprintf(fname,"%s_nt.ppf",path_.c_str());
+  POutPersist po(fname);
+  po << PPFNameTag("ntv12") << nt; 
+  cout << " BRProcARaw2C::run() created NTuple file " << fname << card2name_(card_) << endl;
+  }     
   ts.SetNow();
   tm.SplitQ();
@@ -240,6 +277,6 @@
     Timer tm("BRProcBRaw4C", false);
     TimeStamp ts; 
-    BRPaqChecker pcheck1(~fgnotrl_);  // Verification/comptage des paquets 
-    BRPaqChecker pcheck2(~fgnotrl_);  // Verification/comptage des paquets 
+    BRPaqChecker pcheck1(!fgnotrl_);  // Verification/comptage des paquets 
+    BRPaqChecker pcheck2(!fgnotrl_);  // Verification/comptage des paquets 
 
     size_t totnbytesout = 0;
@@ -261,5 +298,6 @@
 
     TVector< complex<r_4> > cfour;  // composant TF
-    BRPaquet pq(NULL, NULL, paqsz); 
+/*   
+    BRPaquet pq(NULL, NULL, paqsz);     
     TVector<r_4> vx(pq.DataSize()/2);
     vx = (r_4)(0.);
@@ -271,4 +309,14 @@
     TVector< complex<r_4> > visiV23( cfour.Size() );
     TVector< complex<r_4> > visiV24( cfour.Size() );
+*/
+    int szfour = (paqsz-40)/2+1;
+    TVector< complex<r_4> > visiV13( szfour  );
+    TVector< complex<r_4> > visiV14( szfour );
+    TVector< complex<r_4> > visiV23( szfour );
+    TVector< complex<r_4> > visiV24( szfour );
+    // cout << " *DBG*AAAAA ---- Vectors OK" << endl; 
+    cout << " *DBG*BRProcBRaw4C PaqSz=" << paqsz << " ProcPaqSize=" << procpaqsz 
+         << " procpaqsz/2=" << procpaqsz/2 << " cfour.Size()=" << szfour
+         << " *8="  << szfour*8 << endl;
 
     uint_4 nzm = 0;
@@ -281,4 +329,6 @@
       uint_4 nokpaq = 0;
       if (stop_) break;
+       // cout << " *DBG*BBBBB" << kmz << endl; 
+
       int mid1 = memgr1.FindMemZoneId(MemZA_ProcB);
       Byte* buff1 = memgr1.GetMemZone(mid1);
@@ -305,4 +355,5 @@
       uint_4 i1,i2;
       i1=i2=0;
+//      cout << " *DBG*CCCCCC " << kmz << " memgr1.NbPaquets() =" << memgr1.NbPaquets() << endl;
       while((i1<memgr1.NbPaquets())&&(i2<memgr2.NbPaquets())) {
 	BRPaquet paq1(NULL, buff1+i1*paqsz, paqsz); 
@@ -320,4 +371,8 @@
 // <<","<<paq2.FrameCounter()<<endl;
 
+        if ((i1>=memgr1.NbPaquets())||(i2>=memgr1.NbPaquets())) {
+          cout << " *BUG*BUG i1=" << i1 << " i2=" << i2 << endl;
+          break;
+        }
         // Les deux framecounters sont identiques ...
         noksfc++;
@@ -335,4 +390,7 @@
         totnbytesproc += 2*paq1.DataSize();
       } // Fin de boucle sur les paquets d'une zone
+      memgr1.FreeMemZone(mid1, MemZS_ProcB);
+      memgr2.FreeMemZone(mid2, MemZS_ProcB);
+
       if ((nzm >= nmean_) || ((kmz==(nmax_-1))&&(nzm>1))) {
         visiV13 /= complex<r_4>((r_4)nzm, 0.);
@@ -357,13 +415,11 @@
         visiV23 = complex<r_4>(0., 0.);
         visiV24 = complex<r_4>(0., 0.);
-        nzm = 0;  
+        nzm = 0;  ifile++; 
 //        ts.SetNow();
 //        filog << ts << " :  proc file  " << fname << endl;		       
         cout << " BRProcBRaw4C::run() created file  " << fname << endl;
       }   
-      memgr1.FreeMemZone(mid1, MemZS_ProcB);
-      memgr2.FreeMemZone(mid2, MemZS_ProcB);
       double okfrac = (nokpaq>1)?((double)noksfc/(double)nokpaq*100.):0.;
-      cout << "BRProcBRaw2C ["<<kmz<<"] NOKPaq=" << nokpaq << " NSameFC=" << noksfc 
+      cout << "BRProcBRaw4C ["<<kmz<<"] NOKPaq=" << nokpaq << " NSameFC=" << noksfc 
            << " (" << okfrac << " %)" << endl;
       totnokpaq += nokpaq;
Index: /trunk/AddOn/TAcq/makefile
===================================================================
--- /trunk/AddOn/TAcq/makefile	(revision 3645)
+++ /trunk/AddOn/TAcq/makefile	(revision 3646)
@@ -2,8 +2,9 @@
 
 # all : traidio  tmtfft tstminifits tbrpaq tpciew tmtacq tstrdfits mfits2spec tsok 
-all : traidio  tmtfft tstminifits tmtacq tstrdfits mfits2spec tsok mcrd
+# all : traidio  tmtfft tstminifits tmtacq tstrdfits mfits2spec tsok mcrd
+all : traidio tstminifits tmtacq mfits2spec mcrd svv2mtx 
 
 clean :
-	rm -f *.o traidio tmtfft tstminifits tpciew tbrpaq tmtacq tstrdfits mfits2spec mcrd
+	rm -f *.o traidio tmtfft tstminifits tpciew tbrpaq tmtacq tstrdfits mfits2spec mcrd svv2mtx
 	rm -f mfits2spec tsok *.ppf *.fits 
 
@@ -40,4 +41,10 @@
 	$(CXXCOMPILE) -c  -o brproc.o brproc.cc 
 
+##   --------------
+svv2mtx : svv2mtx.o 
+	$(CXXLINK) -o svv2mtx svv2mtx.o  $(SOPHYAEXTSLBLIST)
+
+svv2mtx.o : svv2mtx.cc 
+	$(CXXCOMPILE) -o svv2mtx.o svv2mtx.cc
 
 ##   --------------
Index: /trunk/AddOn/TAcq/mcrd.cc
===================================================================
--- /trunk/AddOn/TAcq/mcrd.cc	(revision 3645)
+++ /trunk/AddOn/TAcq/mcrd.cc	(revision 3646)
@@ -83,5 +83,5 @@
     string act = arg[1];
     bool fg4c=false;  // true -> 4 channels (2 fibers)
-    bool fgrdfits=false;  // false -> Don't read fits files, generate paquets
+    bool fgrdfits=true;  // 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;
@@ -242,5 +242,5 @@
 
   cout << " mcrd/Proc2FRawAB: Starting threads (reader1,2, procA1,2, procAB) ... " << endl;
-
+//  cout << "[1]--- CR to continue ..." << endl;   char ans[32]; gets(ans); 
   if (fgrdfile) { 
     reader1.start();
@@ -251,4 +251,5 @@
     pcird2.start();
   }
+//  sleep(1);  mmgr1.Print(cout);  mmgr2.Print(cout);
   proc1.start();
   proc2.start();
Index: /trunk/AddOn/TAcq/svv2mtx.cc
===================================================================
--- /trunk/AddOn/TAcq/svv2mtx.cc	(revision 3646)
+++ /trunk/AddOn/TAcq/svv2mtx.cc	(revision 3646)
@@ -0,0 +1,225 @@
+// Utilisation de SOPHYA pour faciliter les tests ...
+#include "sopnamsp.h"
+#include "machdefs.h"
+
+/* ---------------------------------------------------------- 
+   Programme de lecture multi canaux pour BAORadio  
+   R. Ansari, C. Magneville
+   V : Mai 2009
+   ---------------------------------------------------------- */
+
+// include standard c/c++
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <iostream>
+#include <string>
+
+#include "pexceptions.h"
+#include "tvector.h"
+#include "fioarr.h"
+// #include "tarrinit.h"
+#include "ntuple.h" 
+#include "histinit.h" 
+#include "matharr.h" 
+#include "timestamp.h"
+/*
+#include "fftpserver.h"
+#include "fftwserver.h"
+
+#include "FFTW/fftw3.h"
+*/
+// include sophya mesure ressource CPU/memoire ...
+#include "resusage.h"
+#include "ctimer.h"
+#include "timing.h"
+
+
+int Usage(bool fgshort=true);
+// Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
+int ProcSVFiles(string& inoutpath, int imin, int imax, int istep, int jf1, int jf2, int card=1);
+
+//----------------------------------------------------
+//----------------------------------------------------
+int main(int narg, char* arg[])
+{
+  if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage(false);
+  if (narg<4) return Usage(true);
+
+  HiStatsInitiator _inia;
+  //   TArrayInitiator  _inia;
+
+  int rc = 0;
+  try {
+    string inoutpath = arg[1];    
+    int imin=0;
+    int imax=0;
+    int istep=1;
+    sscanf(arg[2],"%d,%d,%d",&imin,&imax,&istep);
+    int jf1=0;
+    int jf2=0;
+    sscanf(arg[3],"%d,%d",&jf1,&jf2);
+    cout << " ----- svv2mtx.cc Start - InOutPath= " << inoutpath << " IMin,Max,Step=" 
+         << imin << "," << imax << "," << istep << " JF=" << jf1 << "," << jf2 << "  ------- " << endl;
+    int card=1;
+    if (narg>4) card=atoi(arg[3]); 
+    ResourceUsage resu;
+    rc=ProcSVFiles(inoutpath, imin, imax, istep, jf1, jf2, card);
+    resu.Update();
+    cout << resu;
+  }
+  catch (PException& exc) {
+    cerr << " svv2mtx.cc catched MiniFITSException " << exc.Msg() << endl;
+    rc = 77;
+  }  
+  catch (std::exception& sex) {
+    cerr << "\n svv2mtx.cc std::exception :" 
+         << (string)typeid(sex).name() << "\n msg= " 
+         << sex.what() << endl;
+    rc = 78;
+  }
+  catch (...) {
+    cerr << " svv2mtx.cc catched unknown (...) exception  " << endl; 
+    rc = 79; 
+  } 
+
+  cout << ">>>> svv2mtx.cc ------- END ----------- RC=" << rc << endl;
+  return rc;
+
+}
+
+
+
+// Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
+int ProcSVFiles(string& inoutpath, int imin, int imax, int istep, int jf1, int jf2, int card)
+{
+  Timer tm("ProcSVFiles");
+  char fname[512];
+// NTuple 
+  const char* nnames[8] = {"fcs","tts","s1","s2","s12","s12re","s12im","s12phi"};
+  NTuple nt(8, nnames);
+  double xnt[10];
+  uint_4 nmnt = 0;
+  double ms1,ms2,ms12,ms12re,ms12im,ms12phi;
+
+  TMatrix<r_4> s1, s2;
+  TMatrix<r_4> v12re, v12im, v12phi;
+  sa_size_t ncols = (imax-imin+1)/istep;
+  sa_size_t nrows = 10;
+  sa_size_t kc=0;
+  for(int ifile=imin; ifile<=imax; ifile+=istep) {
+    if (card==2) 
+      sprintf(fname, "%s/Ch34_%d.ppf",inoutpath.c_str(),ifile);
+    else 
+      sprintf(fname, "%s/Ch12_%d.ppf",inoutpath.c_str(),ifile);
+    cout << " ProcSVFiles[" << ifile << "] opening file " << fname << endl;
+    PInPersist pin(fname);
+    string tag1="specV1";
+    string tag2="specV2";
+    string tag12="visiV12";
+    if (card==2) {
+      tag1 = "specV3";
+      tag2 = "specV4";
+      tag12="visiV34";
+    }
+    TVector<r_4> sv1;
+    TVector<r_4> sv2; 
+    TVector< complex<r_4> > vv12;
+    pin >> PPFNameTag(tag1) >> sv1; 
+    pin >> PPFNameTag(tag2) >> sv2; 
+    pin >> PPFNameTag(tag12) >> vv12; 
+    if (ifile==imin) {
+      nrows = sv1.Size();
+      cout << " ProcSVFiles/Info: Output s1,s2 matrix size NRows=NFreq=" 
+           << nrows << " NCols=NFiles=" << ncols << endl;
+      s1.SetSize(nrows, ncols);
+      s2.SetSize(nrows, ncols);
+      nrows = vv12.Size();
+      cout << " ProcSVFiles/Info: Output v12 matrix size NRows=NFreq=" 
+           << nrows << " NCols=NFiles=" << ncols << endl;
+      v12re.SetSize(nrows, ncols);
+      v12im.SetSize(nrows, ncols);
+      v12phi.SetSize(nrows, ncols);
+    }
+    s1.Column(kc) = sv1;
+    s2.Column(kc) = sv2;
+    v12re.Column(kc) = real(vv12);
+    v12im.Column(kc) = imag(vv12);
+    v12phi.Column(kc) = phase(vv12);
+    nmnt=0;  ms1=ms2=ms12=ms12re=ms12im=ms12phi=0.;
+    for(sa_size_t jf=jf1; jf<=jf2; jf++) { 
+      ms1 += s1(jf,kc);
+      ms2 += s2(jf,kc);
+      ms12re += v12re(jf,kc);
+      ms12im += v12im(jf,kc);
+      ms12phi += v12phi(jf,kc);
+    }
+    nmnt = (jf2-jf1+1);
+    if (nmnt>0)  {
+      double fnorm = (double)nmnt; 
+      xnt[0] = kc;
+      xnt[1] = 0;
+      xnt[2] = ms1 /= fnorm;
+      xnt[3] = ms2 /= fnorm;
+      xnt[4] = ms12 /= fnorm;
+      xnt[5] = ms12re /= fnorm;
+      xnt[6] = ms12im /= fnorm;
+      xnt[7] = ms12phi /= fnorm;
+      nt.Fill(xnt);
+    }
+
+    kc++;
+
+  }
+  if (card==2) 
+    sprintf(fname, "%s/Ch34mtx.ppf",inoutpath.c_str());
+  else 
+    sprintf(fname, "%s/Ch12mtx.ppf",inoutpath.c_str());
+
+  cout << nt;
+  cout << "ProcSVFiles: Opening file " << fname << " for writing" << endl;  
+  POutPersist po(fname);
+  string tag1="s1";
+  string tag2="s2";
+  string tag12r="v12re";
+  string tag12i="v12im";
+  string tag12p="v12phi";
+  string tagnt="nt12";
+  if (card==2) {
+    tag1="s3";
+    tag2="s4";
+    tag12r="v34re";
+    tag12i="v34im";
+    tag12p="v34phi";
+    tagnt="nt34";
+    }
+  po <<  PPFNameTag(tag1) << s1;
+  po <<  PPFNameTag(tag2) << s2;
+  po <<  PPFNameTag(tag12r) << v12re;
+  po <<  PPFNameTag(tag12i) << v12im;
+  po <<  PPFNameTag(tag12p) << v12phi;
+  po <<  PPFNameTag(tagnt) << nt;
+  cout << "ProcSVFiles: Matrices s1, s2, v12re, v12im, v12phi, NTuple nt written to file " << fname << endl;  
+  return 0;
+}
+
+
+
+
+/* --Fonction-- */
+int Usage(bool fgshort)
+{
+  cout << " --- svv2mtx.cc : Read PPF files produced by mcrd to make matrices BAORadio" << endl;
+  cout << " Usage:  mcrd InOutPath Imin,Imax,step NumFreq1,NumFreq2 [card=1]" << endl; 
+  if (fgshort) {
+    cout << " mcrd -h for detailed instructions" << endl;
+    return 1;
+  }
+  cout << " InOutPath : Input/Output directory name " << endl;
+  cout << " Imin,Imax,IStep: Input PPF files sequence number \n"
+       << "    FileNames=InOutPath/Ch12_II.fits Imin<=II<=Imax II+=IStep \n" 
+       << " card=1 Ch12 , card=2 Ch34 " << endl;
+  return 1;
+}
