Index: trunk/AddOn/TAcq/brproc.cc
===================================================================
--- trunk/AddOn/TAcq/brproc.cc	(revision 3881)
+++ trunk/AddOn/TAcq/brproc.cc	(revision 3886)
@@ -20,5 +20,5 @@
 #include "ctimer.h"
 #include "fftpserver.h"
-#include "fftwserver.h"
+#include "fitsarrhand.h"
 
 #include "FFTW/fftw3.h"
@@ -38,5 +38,7 @@
 					   bool fgdatafft, bool fgsinglechan)
   : BRBaseProcessor(memgr), outpath_(outpath), nmean_(nmean),
-    fgdatafft_(fgdatafft), fgsinglechannel_(fgsinglechan)
+    fgdatafft_(fgdatafft), fgsinglechannel_(fgsinglechan), 
+    clnflg_(fgsinglechan?memgr_.NbFibres():2*memgr_.NbFibres()),
+    nbadpaq_(fgsinglechan?memgr_.NbFibres():2*memgr_.NbFibres()) 
 {
   setNameId("meanSpecCalc",1);
@@ -55,4 +57,19 @@
   nbpaq4mean_=0;
   totnbpaq_=0;
+
+  SetVarianceLimits();
+
+  ofsdtp_=NULL;
+  dtp_=NULL;
+  string dtfile="!"+outpath_+"/dtspec.fits";
+  ofsdtp_ = new FitsInOutFile(dtfile,FitsInOutFile::Fits_Create);
+  dtp_ = new SwFitsDataTable(*ofsdtp_,1024,true);
+  int nchan=(fgsinglechannel_?memgr_.NbFibres():2*memgr_.NbFibres());
+  char cnom[32];
+  for(int i=0; i<nchan; i++) {
+    sprintf(cnom,"var%d",i);
+    dtp_->AddFloatColumn(cnom);    
+  }
+  xnt_=new double[nchan];
 }
 
@@ -61,4 +78,15 @@
 {
   if (nbpaq4mean_>1)  SaveSpectra();
+  cout << " ---------------- BRMeanSpecCalculator()_Finalizing -------------------- " << endl;
+  for(size_t i=0; i<nbadpaq_.size(); i++) {
+    cout << " Channel " << i << "  NBadPaq=" << nbadpaq_[i] << " / TotNbPaq=" << totnbpaq_ << endl;
+  }
+  if (dtp_) {
+    cout << *dtp_; 
+    delete dtp_;
+    delete ofsdtp_;
+    delete xnt_;
+  }
+  cout << " ------------------------------------------------------------------------ " << endl;
 }
 
@@ -70,9 +98,10 @@
 int BRMeanSpecCalculator::Process()
 {
-      
   if (nbpaq4mean_==nmean_)  SaveSpectra();
+  FlagBadPackets();
     
   if (fgdatafft_) {  // Donnees firmware FFT 
     for(sa_size_t i=0; i<(size_t)mspecmtx_.NRows(); i++) {
+      if (clnflg_[i])  { nbadpaq_[i]++;  continue; }   // si le paquet a ete flagge mauvais ( clnflg_[i] <> 0 ) 
       TwoByteComplex* zp=NULL;
       if (fgsinglechannel_) {
@@ -94,4 +123,5 @@
   else {  // Donnees RAW qui ont du etre processe par BRFFTCalculator
     for(sa_size_t i=0; i<(size_t)mspecmtx_.NRows(); i++) {
+      if (clnflg_[i])  { nbadpaq_[i]++;  continue; }   // si le paquet a ete flagge mauvais ( clnflg_[i] <> 0 ) 
       complex<ODT>* zp=NULL;
       if (fgsinglechannel_) {
@@ -116,4 +146,47 @@
 
 /* --Methode-- */
+void BRMeanSpecCalculator::FlagBadPackets()
+{
+  if (fgdatafft_) {  // Donnees firmware FFT 
+    for(sa_size_t i=0; i<(size_t)mspecmtx_.NRows(); i++) {
+      TwoByteComplex* zp=NULL;
+      if (fgsinglechannel_) {
+	zp=vpaq_[i].Data1C();
+      }
+      else {
+	zp=vpaq_[i/2].Data1C();
+	if (i%2==1)  zp=vpaq_[i/2].Data2C();
+      }
+      double variance=0.;
+      for(sa_size_t f=1; f<mspecmtx_.NCols(); f++)   variance+=(double)zp[f].module2F();
+      xnt_[i]=variance;
+      clnflg_[i]=0;
+      if (variance<varmin_) clnflg_[i]=1;
+      else if (variance>varmax_) clnflg_[i]=2;
+    }
+  }
+  else {  // Donnees RAW qui ont du etre processe par BRFFTCalculator
+    for(sa_size_t i=0; i<(size_t)mspecmtx_.NRows(); i++) {
+      complex<ODT>* zp=NULL;
+      if (fgsinglechannel_) {
+	zp=reinterpret_cast< complex<ODT>* > (vprocpaq_[i]);
+      }
+      else {
+	zp=reinterpret_cast< complex<ODT>* > (vprocpaq_[i/2]);
+	if (i%2==1)  zp= reinterpret_cast< complex<ODT>* >(vprocpaq_[i/2]+memgr_.ProcPaqSize()/2) ;
+      }
+      double variance=0.;
+      for(sa_size_t f=1; f<mspecmtx_.NCols(); f++)   variance+=(double)Zmod2(zp[f]);
+      xnt_[i]=variance;
+      clnflg_[i]=0;
+      if (variance<varmin_) clnflg_[i]=1;
+      else if (variance>varmax_) clnflg_[i]=2;     
+    }
+  }
+  dtp_->AddRow(xnt_);
+  return;
+}
+
+/* --Methode-- */
 void BRMeanSpecCalculator::SaveSpectra()
 {
@@ -125,4 +198,5 @@
   char nfile[64];
   string flnm;
+  /*
   {
   sprintf(nfile,"mspecmtx%d.ppf",numfile_);
@@ -137,4 +211,18 @@
   po << sigspecmtx_;
   }
+  */
+  {
+  sprintf(nfile,"mspecmtx%d.fits",numfile_);
+  flnm="!"+outpath_+nfile;
+  FitsInOutFile fos(flnm,FitsInOutFile::Fits_Create);
+  fos << mspecmtx_;
+  }
+  {
+  sprintf(nfile,"sigspecmtx%d.fits",numfile_);
+  flnm="!"+outpath_+nfile;
+  FitsInOutFile fos(flnm,FitsInOutFile::Fits_Create);
+  fos << sigspecmtx_;
+  }
+
   cout << numfile_ << "-BRMeanSpecCalculator::SaveSpectra() NPaqProc="
        << totnbpaq_ << "  -> Mean/Sig spectra Matrix in " << flnm << " /sigspec...ppf" << endl;
