Index: /trunk/AddOn/TAcq/chanum_1210.cc
===================================================================
--- /trunk/AddOn/TAcq/chanum_1210.cc	(revision 3925)
+++ /trunk/AddOn/TAcq/chanum_1210.cc	(revision 3925)
@@ -0,0 +1,135 @@
+// Lecture des fichiers chanum_?.ppf et recherche des visibilites ecrites dans les matrices d'acq
+// > make OBJ=$CMVPROG/obj/ EXE=$CMVPROG/exe/ chanum_1210
+#include "sopnamsp.h"
+#include "machdefs.h"
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <algorithm>
+
+#include "pexceptions.h"
+#include "tvector.h"
+#include "nbtrixx.h"
+
+// > chanum_1210 -f 1375.0610351 -o CasA02Dec.txt crt*/CasA02Dec/chanum_*.ppf
+// > chanum_1210 -f 1375.0610351 -o Crab03Dec.txt crt*/Crab03Dec/chanum_*.ppf
+// > chanum_1210 -f 1382.5378418 -o Sun03Dec.txt  crt*/Sun03Dec/chanum_*.ppf
+// > chanum_1210 -f 1390.0146484 -o CasA03Dec.txt crt*/CasA03Dec/chanum_*.ppf
+
+int decode_numthread(string filein,string& dir);
+
+//--------------------------- Fonctions de ce fichier   ------------------- 
+void usage(void);
+void usage(void)
+{
+cout<<"Usage: chanum_1210 [options] chanum_?.ppf ..."<<endl
+    <<" -f F0MHz : 1ere frequence pour cette acquisition"<<endl
+    <<" -o fichier.txt : nom du fichier de sortie pour les commandes a lancer"<<endl;
+}
+
+//----------------------------------------------------
+int main(int narg, char* arg[])
+{
+  string outname = "chanum_1210.txt";
+  double freq0 = 0.;
+
+  char c;
+  while((c = getopt(narg,arg,"hO:o:f:")) != -1) {
+    switch (c) {
+    case 'f' :
+      freq0 = atof(optarg);
+      break;
+    case 'o' :
+      outname = optarg;
+      break;
+    case 'h' :
+    default :
+      usage(); return -1;
+    }
+  }
+  if(optind>=narg) {usage(); return -2;}
+
+  vector<uint_4> vcode, vnth, vrow;
+  vector<string> vdir;
+  int nvisi = 0;
+  for(int ifile=optind;ifile<narg;ifile++) {
+    // --- lecture file
+    string filein = arg[ifile];
+    cout<<"..file: "<<filein<<endl;
+    struct stat buffer;
+    int status = stat(filein.c_str(),&buffer);
+    if(status) {cout<<"!!!UNKNOWN FILE"<<endl; continue;}
+    PInPersist pin(filein);
+    TVector<uint_4> chanpairnum;
+    if(pin.GotoNameTag("chanpairnum")) pin.GetObject(chanpairnum,"chanpairnum");
+    else {cout<<"!!!UNKNOWN OBJECTchanpairnum "<<endl; continue;}
+    // --- decodage repertoire et num thread
+    string dir;
+    int nth = decode_numthread(filein,dir);
+    if(nth<0) {cout<<"!!!DECODING PB for thread number"<<endl; continue;}
+    // --- lecture des paires et des infos
+    for(int row=0;row<chanpairnum.Size();row++) {
+      vcode.push_back(chanpairnum(row));
+      vnth.push_back(nth);
+      vrow.push_back(row);
+      vdir.push_back(dir);
+      nvisi++;
+    }
+  }
+  cout<<"nread="<<nvisi<<endl;
+
+  // sort
+  TVector<uint_4> Vcode(vcode), IVcode(vcode.size());
+  TabSortInd(Vcode.Size(),Vcode.Data(),IVcode.Data());
+
+  // ecriture
+  ofstream ftxt(outname.c_str(), ofstream::out);
+  cout<<"writing in "<<outname<<" (is_open="<<ftxt.is_open()<<")"<<endl;
+  if(!ftxt) {cout<<"!!!!OPENING failed "<<outname<<endl; return -3;}
+  int dupli = 0, ndupli = 0;
+  nvisi = 0;
+  for(uint_4 i=0;i<vcode.size();i++) {
+    char str[128];
+    sprintf(str,"svv2mtx2_1210 -T 0,999999 -F 0,9999,1 -f %.7f",freq0);
+    string argu = str;
+    uint_4 ip = IVcode(i);
+    uint_4 vcode_prec = (i>0) ? vcode[IVcode(i-1)]: 99999999;
+    uint_4 vcode_suiv = (i<vcode.size()-1) ? vcode[IVcode(i+1)]: 99999999;
+    uint_4 v1 = vcode[ip]/1000, v2 = vcode[ip]%100;
+    if(v1>v2) {swap(v1,v2); argu += " -c";}  // faut-il conjuger ?
+    sprintf(str," -t %d",vnth[ip]); argu += str;  // numero de threads
+    sprintf(str," -r %d",vrow[ip]); argu += str;  // ligne de la matrice acq
+    //  nom du fichier de sortie avec gestion des duplications
+    if(vcode_suiv==vcode[ip] || vcode_prec!=vcode[ip]) argu += " -D";
+    if(vcode_prec==vcode[ip]) { dupli++; argu += " -D"; }
+        else { ndupli+=dupli; dupli=0; }
+    sprintf(str," -o visi%d_%02d_%02d.ppf",dupli,v1,v2); argu += str;
+    argu += " "; argu += vdir[ip];  // repertoire des fichiers acq
+    ftxt << argu <<endl;
+    nvisi++;
+    cout<<i<<" "<<ip<<" "<<vcode[ip]<<" "<<v1<<","<<v2<<" th="<<vnth[ip]<<" ro="<<vrow[ip]<<" du="<<dupli<<" "<<vdir[ip]<<endl;
+  }
+  cout<<"nvisi tot="<<nvisi<<", number of duplicate="<<ndupli<<", nvisi="<<nvisi-ndupli<<" / 528"<<endl;
+
+ return 0;
+}
+
+//-------------------------------------------------
+int decode_numthread(string filein,string& dir)
+{
+  unsigned int p = filein.rfind("chanum_");
+  if(p>=filein.size()) return -1;
+  if(p<=1) dir = ""; else dir = filein.substr(0,p-1);
+  const char * c = (filein.c_str() + p);
+  int nth = -2;
+  sscanf(c,"chanum_%d.ppf",&nth);
+  //cout<<"\""<<filein<<"\" "<<p<<" \""<<c<<"\" "<<nth<<" \""<<dir<<"\""<<endl;
+  return nth;
+}
+
Index: /trunk/AddOn/TAcq/makefile
===================================================================
--- /trunk/AddOn/TAcq/makefile	(revision 3924)
+++ /trunk/AddOn/TAcq/makefile	(revision 3925)
@@ -16,14 +16,16 @@
 
 EXELIST = $(EXE)mfacq $(EXE)vismfib $(EXE)specmfib $(EXE)treadmfib $(EXE)tmtacq \
-          $(EXE)mfits2spec $(EXE)mcrd $(EXE)svv2mtx $(EXE)svv2mtx2 $(EXE)tsok \
-          $(EXE)traidio $(EXE)tstminifits $(EXE)tbrpaq
+          $(EXE)mfits2spec $(EXE)mcrd $(EXE)svv2mtx $(EXE)svv2mtx2 \
+          $(EXE)chanum_1210 $(EXE)svv2mtx2_1210 \
+          $(EXE)tsok $(EXE)traidio $(EXE)tstminifits $(EXE)tbrpaq
 
 EXEOLIST = $(EXE)mfacq.o $(EXE)vismfib.o $(EXE)specmfib.o $(EXE)treadmfib.o $(EXE)tmtacq.o \
-           $(EXE)mfits2spec.o $(EXE)mcrd.o $(EXE)svv2mtx.o $(EXE)svv2mtx2.o $(EXE)tsok.o \
-           $(EXE)traidio.o $(EXE)tstminifits.o $(EXE)tbrpaq.o
+           $(EXE)mfits2spec.o $(EXE)mcrd.o $(EXE)svv2mtx.o $(EXE)svv2mtx2.o \
+           $(EXE)chanum_1210.o $(EXE)svv2mtx2_1210.o \
+           $(EXE)tsok.o $(EXE)traidio.o $(EXE)tstminifits.o $(EXE)tbrpaq.o
 
 # all : traidio  tmtfft tstminifits tbrpaq tpciew tmtacq tstrdfits mfits2spec tsok 
 # all : traidio  tmtfft tstminifits tmtacq tstrdfits mfits2spec tsok mcrd
-all :  mfacq vismfib specmfib treadmfib tmtacq mfits2spec mcrd svv2mtx svv2mtx2 tsok traidio tstminifits tbrpaq
+all :  mfacq vismfib specmfib treadmfib tmtacq mfits2spec mcrd svv2mtx svv2mtx2 chanum_1210 svv2mtx2_1210 tsok traidio tstminifits tbrpaq
 
 clean :
@@ -31,5 +33,5 @@
 
 cleanold :
-	rm -f *.o traidio tmtfft tstminifits tpciew tbrpaq tmtacq tstrdfits mfits2spec mcrd svv2mtx svv2mtx2
+	rm -f *.o traidio tmtfft tstminifits tpciew tbrpaq tmtacq tstrdfits mfits2spec mcrd svv2mtx svv2mtx2 chanum_1210 svv2mtx2_1210
 	rm -f Objs/* *.ppf *.fits 
 ########################################################
@@ -147,4 +149,22 @@
 $(OBJ)svv2mtx2.o : svv2mtx2.cc  $(PKGHLIST)
 	$(CXXCOMPILE) -o $(OBJ)svv2mtx2.o svv2mtx2.cc
+
+chanum_1210 : $(EXE)chanum_1210
+	echo '---chanum_1210 made'
+
+$(EXE)chanum_1210 : $(OBJ)chanum_1210.o   $(PKGOLIST)
+	$(CXXLINK) -o $(EXE)chanum_1210 $(OBJ)chanum_1210.o  $(SOPHYAEXTSLBLIST)
+
+$(OBJ)chanum_1210.o : chanum_1210.cc  $(PKGHLIST)
+	$(CXXCOMPILE) -o $(OBJ)chanum_1210.o chanum_1210.cc
+
+svv2mtx2_1210 : $(EXE)svv2mtx2_1210
+	echo '---svv2mtx2_1210 made'
+
+$(EXE)svv2mtx2_1210 : $(OBJ)svv2mtx2_1210.o   $(PKGOLIST)
+	$(CXXLINK) -o $(EXE)svv2mtx2_1210 $(OBJ)svv2mtx2_1210.o  $(SOPHYAEXTSLBLIST)
+
+$(OBJ)svv2mtx2_1210.o : svv2mtx2_1210.cc  $(PKGHLIST)
+	$(CXXCOMPILE) -o $(OBJ)svv2mtx2_1210.o svv2mtx2_1210.cc
 
 ##   --------------  Lecture minifits et calcul de spectre 
Index: /trunk/AddOn/TAcq/svv2mtx2_1210.cc
===================================================================
--- /trunk/AddOn/TAcq/svv2mtx2_1210.cc	(revision 3925)
+++ /trunk/AddOn/TAcq/svv2mtx2_1210.cc	(revision 3925)
@@ -0,0 +1,277 @@
+// lecture des fichiers acq de Pittsburgh Dec 2010
+// et ecriture des matrices de visi temps-frequence
+// > make OBJ=$CMVPROG/obj/ EXE=$CMVPROG/exe/ chanum_1210 svv2mtx2_1210
+
+#include "sopnamsp.h"
+#include "machdefs.h"
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <iostream>
+#include <string>
+
+#include "pexceptions.h"
+#include "tvector.h"
+#include "fioarr.h"
+
+//--------------------------- Fonctions de ce fichier   ------------------- 
+void usage(void);
+void usage(void)
+{
+cout<<"svv2mtx2_1012 [options] dir : lecture des fichiers acq de Pittsburgh Dec 2010"<<endl
+    <<" dir : repertoire ou se trouvent les fichiers d'acq"<<endl
+    <<" -D : visi is a duplicated one"<<endl
+    <<" -o visi.ppf : nom du ficher ppf pour ecrire la visi temps-frequence"<<endl
+    <<" -t thr : numero de la thread ayant traite cette visi [0-N]"<<endl
+    <<" -r row : numero de la ligne de la matrice acq pour cette visi [0-Nrow["<<endl
+    <<" -f freq0 : 1ere frequence en MHz"<<endl
+    <<" -T it1,it2 : numero (temps) des fichiers a traiter [it1,it2]"<<endl
+    <<" -F if1,if2,ngrpfreq : numero des frequences [if1,if2] de [0,N[ a traiter et regroupement par ngrpfreq"<<endl;
+}
+
+//----------------------------------------------------
+int main(int narg, char* arg[])
+{
+  // --- Decodage des arguments et traitement 
+  string outname = "";
+  int numthread = -1, numrow = -1;
+  bool dupli = false;
+  double freq0 = 0.;
+  int ifilmin=0, ifilmax=999999;
+  int jfr1=0, jfr2=-1, ngrpfreq=1;
+  char str[2048];
+
+  char c;
+  while((c = getopt(narg,arg,"hDo:t:r:f:T:F:")) != -1) {
+    switch (c) {
+    case 'D' :
+      dupli = true;
+      break;
+    case 'o' :
+      outname = optarg;
+      break;
+    case 't' :
+      numthread = atoi(optarg);
+      break;
+    case 'r' :
+      numrow = atoi(optarg);
+      break;
+    case 'f' :
+      freq0 = atof(optarg);
+      break;
+    case 'T' :
+      sscanf(optarg,"%d,%d",&ifilmin,&ifilmax);
+      if(ifilmin<0) ifilmin=0;
+      break;
+    case 'F' :
+      sscanf(optarg,"%d,%d,%d",&jfr1,&jfr2,&ngrpfreq);
+      if(ngrpfreq<=0) ngrpfreq=1;
+      break;
+    case 'h' :
+    default :
+      usage(); return -1;
+    }
+  }
+  if(optind>=narg) {usage(); return -1;}
+  string indir = arg[optind];
+
+  cout<<"thread="<<numthread<<endl; if(numthread<0) return -2;
+  cout<<"numrow="<<numrow<<endl;  if(numrow<0) return -2;
+  cout<<"dupli="<<(int)dupli<<endl;
+  cout<<"outname="<<outname<<endl; if(outname.size()<=0) return -2;
+  cout<<"indir="<<indir<<endl;
+  cout<<"freq0="<<freq0<<" MHz"<<endl;
+  cout<<"request: file "<<ifilmin<<" to "<<ifilmax<<endl; if(ifilmax<ifilmin) return -3;
+  cout<<"request: freq "<<jfr1<<" to "<<jfr2<<" grouped by "<<ngrpfreq<<endl;
+
+  // --- recherche et comptage des fichiers de visibilites
+  int nfile = 0;
+  {
+  struct stat buffer;
+  int i2 = ifilmax; ifilmax = 0;
+  bool foundfirst = false;
+  for(int ifile=0; ifile<=i2; ifile++) {
+    sprintf(str, "%s/vismtx_%d_%d.ppf",indir.c_str(),numthread,ifile);
+    int status = stat(str,&buffer);
+    if(!foundfirst && status==0) {
+      cout<<"first file found: "<<str<<endl;
+      foundfirst = true;
+      if(ifile>ifilmin) ifilmin = ifile;
+    }
+    if(!foundfirst) continue;
+    if(ifile<ifilmin) continue;
+    if(foundfirst && status) break;
+    nfile++;
+    ifilmax = ifile;
+  }
+  cout<<"Found "<<nfile<<" files from "<<ifilmin<<" to "<<ifilmax<<endl;
+  if(nfile==0 || ifilmax<ifilmin) return -4;
+  }
+
+
+  //--------------------------------------------------------------------
+  int rc = 0;
+  try {
+
+  // --- read visibility files
+  TMatrix< complex<r_4> > MVisi;
+  TVector<r_8> MeanTT(nfile);
+  TVector<r_4> Freq;
+  TVector<int_4> Npaqsum(nfile);
+  string tudeb, tufin;
+  double tudeb_day, tufin_day;
+  int nfreq = 0;
+  int lpmod = nfile/10; if(lpmod<=0) lpmod=1;
+
+  int ntimefill = 0;
+  for(int ifile=ifilmin; ifile<=ifilmax; ifile++) {
+
+    // --- Lecture d'une visi elementaire (fichier acq)
+    sprintf(str, "%s/vismtx_%d_%d.ppf",indir.c_str(),numthread,ifile);
+    if(ifile==ifilmin || ifile==ifilmax || (ifile-ifilmin)%lpmod==0)
+      cout<<ifile<<" opening: "<<str<<endl;
+    PInPersist pin(str);
+    TMatrix< complex<r_4> > vismtx;
+    pin >> vismtx;
+    tufin = (string)vismtx.Info()["DATEOBS"];
+
+    // --- Time keeping and number of summed elementary visibilities
+    MeanTT(ifile-ifilmin) = (double)vismtx.Info()["MeanTT"]/125.e6;
+    uint_4 npaqsum = vismtx.Info()["NPAQSUM"];
+
+    // --- Initialisation purposes for the first read file
+    if (ifile==ifilmin) {
+
+      tudeb = tufin;
+      printf("Reference Time: tt = %.5f sec, TU = %s\n",MeanTT(0),tudeb.c_str());
+      if(numrow>=vismtx.NRows()) {
+        cout<<"ERROR: requested numrow="<<numrow<<" => "<<vismtx.NRows()<<endl;
+        return -5;
+      }
+
+      // frequency grouping
+      int nfreq0 = vismtx.NCols();
+      cout<<"vismtx: number of frequencies = "<<nfreq0<<endl;
+      if(jfr1<=0) jfr1=0; if(jfr1>=nfreq0) jfr1=nfreq0-1;
+      if(jfr2<jfr1 || jfr2>=nfreq0) jfr2=nfreq0-1;
+      cout<<"frequency from jfr1="<<jfr1<<" to jfr2="<<jfr2<<" grouped by "<<ngrpfreq<<endl;
+      nfreq = 0; for(int i=jfr1;i<=jfr2;i+=ngrpfreq) nfreq++;
+      cout<<"frequency from [jfr1="<<jfr1<<" , jfr2="<<jfr2<<"] grouped by "<<ngrpfreq
+          <<": total of "<<nfreq<<" freq"<<endl;
+      Freq.ReSize(nfreq); Freq = 0.;
+      for(int i=0;i<nfreq;i++) {
+        int nf=0;
+        for(int j=0; j<ngrpfreq; j++) {
+          int f = jfr1 + i*ngrpfreq + j;
+          if(f>jfr2) break;
+          Freq(i) += f;
+	  nf++;
+	}
+        if(nf>0) Freq(i) /= (double)nf;
+	else {cout<<"ERROR: last freq bin with 0 freq in it"<<endl; return -6;}
+        if(i<4 || i>nfreq-4) cout<<"  F("<<i<<") = "<<Freq(i)<<" nf="<<nf<<endl;
+      }
+
+      // allocate visib matrice <f> vs t
+      cout<<"allocating visibility matrice ("<<nfile<<","<<nfreq<<") "
+          <<nfile*nfreq*sizeof(complex<r_4>)/1.e6<<" Mo"<<endl;
+      MVisi.ReSize(nfile,nfreq);
+      MVisi = complex<r_4>(0.,0.);
+    }
+
+    // Fill time-freq visibility matrix
+    for(int i=0;i<nfreq;i++) {
+      int nf=0;
+      for(int j=0; j<ngrpfreq; j++) {
+        int f = jfr1 + i*ngrpfreq + j;
+        if(f>jfr2) break;
+        MVisi(ifile-ifilmin,i) += vismtx(numrow,f);
+        nf++;
+      }
+      Npaqsum(ifile-ifilmin) = nf * npaqsum;
+      MVisi(ifile-ifilmin,i) /= double(Npaqsum(ifile-ifilmin));
+    }
+    ntimefill++;
+
+  }  // fin boucle sur le fichiers d'acq
+  cout<<"ntimefill = "<<ntimefill<<" / "<<nfile<<endl;
+
+  // --- keeping info with visi
+  {
+  // bricolo en unite de jours depuis le 0/12/2010 a 0h
+  int y,m,d,h,mn; double s;
+  sscanf(tudeb.c_str(),"%d-%d-%dT%d:%d:%lf",&y,&m,&d,&h,&mn,&s);
+  tudeb_day = (double)d + ((double)h + mn/60. + s/3600.)/24.;
+  sscanf(tufin.c_str(),"%d-%d-%dT%d:%d:%lf",&y,&m,&d,&h,&mn,&s);
+  tufin_day = (double)d + ((double)h + mn/60. + s/3600.)/24.;
+  }
+  MVisi.Info()["nth"] = numthread;
+  MVisi.Info()["row"] = numrow;
+  MVisi.Info()["dir"] = indir;
+  MVisi.Info()["dupli"] = (dupli) ? 1: 0;
+  MVisi.Info()["TUobs_0"] = tudeb;
+  MVisi.Info()["TUobs_N"] = tufin;
+  MVisi.Info()["TUday_0"] = tudeb_day;
+  MVisi.Info()["TUday_N"] = tufin_day;
+  MVisi.Info()["TTag_0"] = MeanTT(0);
+  MVisi.Info()["TTag_N"] = MeanTT(MeanTT.Size()-1);
+  MVisi.Info()["freq0"] = freq0;
+  MVisi.Info()["dfreq0"] = 500./8192.;
+  MVisi.Info()["jfr1"] = jfr1;
+  MVisi.Info()["jfr2"] = jfr2;
+  MVisi.Info()["ngrpfreq"] = ngrpfreq;
+  MVisi.Info()["ifilmin"] = ifilmin;
+  MVisi.Info()["ifilmax"] = ifilmax;
+
+  // --- writing visibility matrix to file
+  sprintf(str,"%s",outname.c_str());
+  cout<<"writing visibility matrix to file "<<str<<endl;
+  POutPersist pos(str);
+  pos.PutObject(MeanTT,"meantt");
+  pos.PutObject(Npaqsum,"npaqsum");
+  pos.PutObject(Freq,"ifreq");
+  pos.PutObject(MVisi,"visi");
+  }
+
+  //--------------------------------------------------------------------
+  catch(PException& exc) {
+    cerr<<" svv2mtx2_1210.cc catched PException "<<exc.Msg()<<endl;
+    rc = 77;
+  }
+  catch (std::exception& sex) {
+    cerr<<"\n svv2mtx2_1210.cc std::exception :" 
+        <<(string)typeid(sex).name() << "\n msg= "<<sex.what()<<endl;
+    rc = 78;
+  }
+  catch(...) {
+    cerr<<" svv2mtx2_1210.cc catched unknown (...) exception  "<<endl; 
+    rc = 79; 
+  }
+  cout<<">>>> svv2mtx2_1210.cc ------- END ----------- RC="<<rc<<endl;
+  return rc;
+}
+
+
+/*
+openppf visi1_01_05.ppf
+
+print visi 5
+
+n/plot ifreq.val%n ! ! "cpts"
+
+n/plot npaqsum.val%n ! ! "cpts"
+
+n/plot meantt.val%n ! ! "cpts"
+cp meantt dmeantt
+c++exec dmeantt=0.; for(int i=1;i<dmeantt.Size();i++) dmeantt(i)=meantt(i)-meantt(i-1);
+n/plot dmeantt.val%n n>0 ! "cpts nsta"
+
+imag visi "cdmod"
+imag visi "cdreal"
+imag visi "cdimag"
+imag visi "cdphas"
+
+ */
