Index: trunk/AddOn/TAcq/brviscalc.cc
===================================================================
--- trunk/AddOn/TAcq/brviscalc.cc	(revision 3923)
+++ trunk/AddOn/TAcq/brviscalc.cc	(revision 3956)
@@ -12,5 +12,7 @@
 #include "pexceptions.h"
 #include "fioarr.h"
+#include "fitsarrhand.h"
 #include "matharr.h"
+#include "arrctcast.h"
 #include "timestamp.h"
 #include "ctimer.h"
@@ -26,8 +28,11 @@
 BRVisibilityCalculator::BRVisibilityCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, size_t nthr)
   : BRBaseProcessor(memgr), paralex_(*this, nthr), nparthr_(nthr), 
-    outpath_(outpath), nmean_(nmean), nbcalc_(1), calcid_(0), vpdata_(2*memgr.NbFibres())
+    outpath_(outpath), nmean_(nmean), nbcalc_(1), calcid_(0), 
+    vpdata_(2*memgr.NbFibres()), vpdatar_(2*memgr.NbFibres())
     // , dtfos_(outpath+"visdt.fits", Fits_Create), visdt_(dtfos_, 1024, true);
 {  
+  SetFFTData();
   DefineRank(1,0);
+  SetPPFOutput();
 
   uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
@@ -35,5 +40,7 @@
   chanpairnumall_.SetSize(maxnpairs);
   chanpairsall_.SetSize(maxnpairs,2);
-  for(size_t i=0; i<2*memgr_.NbFibres(); i++)   vpdata_[i]=NULL;
+  for(size_t i=0; i<2*memgr_.NbFibres(); i++)   { 
+    vpdata_[i]=NULL;   vpdatar_[i]=NULL;  
+  }
   SelectPairs();
 
@@ -113,5 +120,6 @@
     else 
       SelectPairs(calcid_*npairspth+pair1, npairspth, fgpimp);
-    MemZaction mmzas[6]={MemZA_ProcA,MemZA_ProcB,MemZA_ProcC,MemZA_ProcD,MemZA_ProcE,MemZA_ProcF};
+    MemZaction mmzas[10]={MemZA_ProcA,MemZA_ProcB,MemZA_ProcC,MemZA_ProcD,MemZA_ProcE,MemZA_ProcF,
+                          MemZA_ProcG,MemZA_ProcH,MemZA_ProcI,MemZA_ProcJ};
     SetMemZAction(mmzas[calcid_]);
     setNameId("viscalc_grp", calcid_); 
@@ -197,17 +205,32 @@
 
   string filename;
-  filename = outpath_+"chanum.ppf";
+  filename = (outpath_+"chanum.")+OutFileExtension();
   if (nbcalc_>1) {
     char sbuff[32];
-    sprintf(sbuff,"chanum_%d.ppf",(int)calcid_);
+    sprintf(sbuff,"chanum_%d.%s",(int)calcid_,OutFileExtension());
     filename = outpath_+sbuff;
   }
-  POutPersist poc(filename);
-  poc << PPFNameTag("chanids") << chanids_; 
-  poc << PPFNameTag("chanpairs") << chanpairs_; 
-  poc << PPFNameTag("chanpairnum") << chanpairnum_; 
-  poc << PPFNameTag("chanpairsall") << chanpairsall_; 
-  poc << PPFNameTag("chanpairnumall") << chanpairnumall_; 
-  cout << "BRVisibilityCalculator[" << calcid_ << "]::UpdateChanIds() Channel Ids/Pairs saved to PPF file " 
+  if (fgfitsout_) {  // Ecriture au format FITS
+    FitsInOutFile foc(filename, FitsInOutFile::Fits_Create);
+    foc.SetNextExtensionName("chanids"); 
+    foc << chanids_; 
+    foc.SetNextExtensionName("chanpairs");
+    foc << chanpairs_; 
+    foc.SetNextExtensionName("chanpairnum");
+    foc << chanpairnum_; 
+    foc.SetNextExtensionName("chanpairsall");
+    foc << chanpairsall_; 
+    foc.SetNextExtensionName("chanpairnumall");
+    foc << chanpairnumall_; 
+  }
+  else {  // Format PPF 
+    POutPersist poc(filename);
+    poc << PPFNameTag("chanids") << chanids_; 
+    poc << PPFNameTag("chanpairs") << chanpairs_; 
+    poc << PPFNameTag("chanpairnum") << chanpairnum_; 
+    poc << PPFNameTag("chanpairsall") << chanpairsall_; 
+    poc << PPFNameTag("chanpairnumall") << chanpairnumall_; 
+  }
+  cout << "BRVisibilityCalculator[" << calcid_ << "]::UpdateChanIds() Channel Ids/Pairs saved to file " 
        << filename << endl; 
   cout << " ... NbVisib=NbChanPairs=" << chanpairs_.NRows() << " ChannelPairs= " ;
@@ -266,4 +289,5 @@
 
     size_t paqsz=memgr_.PaqSize();
+    size_t procpaqsz=memgr_.ProcPaqSize();
     bool fgrun=true;
     while (fgrun) {
@@ -289,4 +313,10 @@
 	  break;
 	}
+	if ((procpaqsz>0)&&((fprocbuff_[fib]=memgr_.GetProcMemZone(mid,fib))==NULL)) {   // cela ne devrait pas arriver non plus
+	  cout << "BRVisibilityCalculator[" << calcid_ << "]::run()/ERROR memgr.GetProcMemZone(" 
+	       << mid << "," << fib << ") -> NULL" << endl;
+	  setRC(9);	  fgrun=false;		
+	  break;
+	}
       }
 
@@ -299,10 +329,18 @@
 	  char nfile[48];
 	  if (nbcalc_==1) 
-	    sprintf(nfile,"vismtx%d.ppf",numfile_);
+	    sprintf(nfile,"vismtx%d.%s",numfile_,OutFileExtension());
 	  else 
-	    sprintf(nfile,"vismtx_%d_%d.ppf",(int)calcid_,numfile_);
+	    sprintf(nfile,"vismtx_%d_%d.%s",(int)calcid_,numfile_,OutFileExtension());
 	  string flnm=outpath_+nfile;
-	  POutPersist po(flnm);
-	  po << vismtx_;
+	  if (fgfitsout_) {  // Ecriture au format FITS
+	    FitsInOutFile fo(flnm, FitsInOutFile::Fits_Create);
+	    TArray<r_4> arvismtx = ArrCastC2R(vismtx_);
+	    arvismtx.Info()=vismtx_.Info();
+	    fo << arvismtx;
+	  }
+	  else {  // Format PPF 
+	    POutPersist po(flnm);
+	    po << vismtx_;
+	  }
 	  if ((prtlev_>0)&&(numfile_%prtmodulo_==0)) {
 	    cout << numfile_ << "-BRVisCalc[" << calcid_ << "/" << nbcalc_ << "]::run() NPaqProc="
@@ -324,4 +362,5 @@
 	  vfgok_[fib] = vpchk_[fib].Check(vpaq_[fib],curfc_[fib]);
 	  if (!vfgok_[fib])  fgallfibok[jp]=fgokallfibers_=false;
+	  if (procpaqsz>0)    vprocpaq_[fib] = fprocbuff_[fib]+jp*procpaqsz;
 	}
 	if (fgokallfibers_)  {
@@ -375,7 +414,15 @@
 {
   if (totnbpaq_==0) UpdateChanIds();  // Appele ici pour etre sur que le thread de remplissage a mis l'info a jour.
-  for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
-    vpdata_[2*fib] = vpaq_[fib].Data1C();
-    vpdata_[2*fib+1] = vpaq_[fib].Data2C();
+  if (fgdataraw_) {  // Donnees firmware RAW apres TF soft 
+    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+      vpdatar_[2*fib] = reinterpret_cast< complex<r_4>* > (vprocpaq_[fib]);
+      vpdatar_[2*fib+1] = reinterpret_cast< complex<r_4>* >(vprocpaq_[fib]+memgr_.ProcPaqSize()/2) ;
+    }
+  } 
+  else {   // donnees firmware FFT 
+    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+      vpdata_[2*fib] = vpaq_[fib].Data1C();
+      vpdata_[2*fib+1] = vpaq_[fib].Data2C();
+    }
   }
   
@@ -388,10 +435,18 @@
       char nfile[48];
       if (nbcalc_==1) 
-	sprintf(nfile,"vismtx%d.ppf",numfile_);
+	sprintf(nfile,"vismtx%d.%s",numfile_,OutFileExtension());
       else 
-	sprintf(nfile,"vismtx_%d_%d.ppf",(int)calcid_,numfile_);
+	sprintf(nfile,"vismtx_%d_%d.%s",(int)calcid_,numfile_,OutFileExtension());
       string flnm=outpath_+nfile;
-      POutPersist po(flnm);
-      po << vismtx_;
+      if (fgfitsout_) {  // Ecriture au format FITS
+	FitsInOutFile fo(flnm, FitsInOutFile::Fits_Create);
+	TArray<r_4> arvismtx = ArrCastC2R(vismtx_);
+	arvismtx.Info()=vismtx_.Info();
+	fo << arvismtx;
+      }
+      else {  // Format PPF 
+	POutPersist po(flnm);
+	po << vismtx_;
+      }
       if ((prtlev_>0)&&(numfile_%prtmodulo_==0)) {
 	cout << numfile_ << "-BRVisCalc[" << calcid_ << "/" << nbcalc_ << "]::Process() NPaqProc="
@@ -419,7 +474,15 @@
       if (fgpimp_&&(i!=j)&&((i+j)%2==0))  continue;  // calcul des visib avec numero pair-impair + autocorrel
       TVector< complex<r_4> > vis = vismtx_.Row(k);  k++;   
-      for(sa_size_t f=1; f<vis.Size(); f++) {
-	vis(f) += complex<r_4>((r_4)vpdata_[i][f].realB(), (r_4)vpdata_[i][f].imagB()) * 
-	  complex<r_4>((r_4)vpdata_[j][f].realB(), -(r_4)vpdata_[j][f].imagB());
+
+      if (fgdataraw_) {  // Donnees firmware RAW apres TF soft 
+	for(sa_size_t f=1; f<vis.Size(); f++) {
+	  vis(f) += vpdatar_[i][f] * vpdatar_[j][f];
+	}
+      }
+      else {   // donnees firmware FFT 
+	for(sa_size_t f=1; f<vis.Size(); f++) {
+	  vis(f) += complex<r_4>((r_4)vpdata_[i][f].realB(), (r_4)vpdata_[i][f].imagB()) * 
+	    complex<r_4>((r_4)vpdata_[j][f].realB(), -(r_4)vpdata_[j][f].imagB());
+	}
       }
       nb_flop_ += (8.*(r_8)(vis.Size()-1));
@@ -468,4 +531,5 @@
 {
   vector<TwoByteComplex*>  pvpdata(2*memgr_.NbFibres());
+  vector< complex<r_4>* >  pvpdatar(2*memgr_.NbFibres());
   size_t paqsz=memgr_.PaqSize();
   BRPaquet ppaq(paqsz);
@@ -481,8 +545,17 @@
   for(size_t jp=0; jp<memgr_.NbPaquets(); jp++) {   // boucle sur les paquets d'une zone  
     if (!fgallfibok[jp])  continue;
-    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
-      ppaq.Set(fbuff_[fib]+jp*paqsz);
-      pvpdata[2*fib] = ppaq.Data1C();
-      pvpdata[2*fib+1] = ppaq.Data2C();
+    if (fgdataraw_) {  // Donnees firmware RAW apres TF soft 
+      size_t procpaqsz=memgr_.ProcPaqSize();
+      for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) { 
+	pvpdatar[2*fib] = reinterpret_cast< complex<r_4>* > (fprocbuff_[fib]+jp*procpaqsz); 
+	pvpdatar[2*fib+1] = reinterpret_cast< complex<r_4>* >(vprocpaq_[fib]+jp*procpaqsz+procpaqsz/2) ;
+      }
+    } 
+    else {  // donnees firmware FFT 
+      for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+	ppaq.Set(fbuff_[fib]+jp*paqsz);
+	pvpdata[2*fib] = ppaq.Data1C();
+	pvpdata[2*fib+1] = ppaq.Data2C();
+      }
     }
 
@@ -498,7 +571,14 @@
 	if (fgpimp_&&(i!=j)&&((i+j)%2==0))  continue;  // calcul des visib avec numero pair-impair + autocorrel
 	TVector< complex<r_4> > vis = vismtx_.Row(k);  k++;  
-	for(sa_size_t f=fdeb; f<ffin; f++) {
-	  vis(f) += complex<r_4>((r_4)pvpdata[i][f].realB(), (r_4)pvpdata[i][f].imagB()) * 
-	    complex<r_4>((r_4)pvpdata[j][f].realB(), -(r_4)pvpdata[j][f].imagB());
+	if (fgdataraw_) {  // Donnees firmware RAW apres TF soft 
+	  for(sa_size_t f=fdeb; f<ffin; f++) {
+	    vis(f) += pvpdatar[i][f] * pvpdatar[j][f];
+	  }
+	}
+	else {  // donnees firmware FFT 
+	  for(sa_size_t f=fdeb; f<ffin; f++) {
+	    vis(f) += complex<r_4>((r_4)pvpdata[i][f].realB(), (r_4)pvpdata[i][f].imagB()) * 
+	      complex<r_4>((r_4)pvpdata[j][f].realB(), -(r_4)pvpdata[j][f].imagB());
+	  }
 	}
 	nb_flop_ += (8.*(r_8)(ffin-fdeb));
@@ -609,4 +689,24 @@
 }
 /* --Methode-- */
+MemZStatus BRVisCalcGroup::SetMemZAction(MemZaction mmact)
+{
+  MemZaction mmzas[10]={MemZA_ProcA,MemZA_ProcB,MemZA_ProcC,MemZA_ProcD,MemZA_ProcE,MemZA_ProcF,
+			MemZA_ProcG,MemZA_ProcH,MemZA_ProcI,MemZA_ProcJ};
+  MemZStatus mmzss[10]={MemZS_ProcA,MemZS_ProcB,MemZS_ProcC,MemZS_ProcD,MemZS_ProcE,MemZS_ProcF,
+			MemZS_ProcG,MemZS_ProcH,MemZS_ProcI,MemZS_ProcJ};
+  size_t ia=9999;
+  for(int i=0; i<10; i++) {
+    if (mmact==mmzas[i]) { ia=i; break; }
+  }
+  if (ia>=10)  
+    throw ParmError("BRVisCalcGroup::SetMemZAction() Bad MemZaction mmact !");
+  if ((ia+viscalcp_.size())>10)  
+    throw ParmError("BRVisCalcGroup::SetMemZAction() MemZaction mmact too high !");
+  for(size_t i=0; i<viscalcp_.size(); i++) {
+    viscalcp_[i]->SetMemZAction(mmzas[ia]);   ia++;
+  }
+  return mmzss[ia-1];
+}
+/* --Methode-- */
 int BRVisCalcGroup::SelectFreqBinning(uint_4 freq1, uint_4 freq2, uint_4 nbfreq)
 {
@@ -622,4 +722,16 @@
     viscalcp_[i]->ActivateVisDTable(fgfdt);
 }
+/* --Methode-- */
+void BRVisCalcGroup::SetFitsOutput()
+{
+  for(size_t i=0; i<viscalcp_.size(); i++)
+    viscalcp_[i]->SetFitsOutput();
+} 
+/* --Methode-- */
+void BRVisCalcGroup::SetPPFOutput()
+{
+  for(size_t i=0; i<viscalcp_.size(); i++)
+    viscalcp_[i]->SetPPFOutput();
+} 
 /* --Methode-- */
 void BRVisCalcGroup::SetPrintLevel(int lev, uint_8 prtmodulo)
