Index: /trunk/ArchTOIPipe/ProcWSophya/Makefile.in
===================================================================
--- /trunk/ArchTOIPipe/ProcWSophya/Makefile.in	(revision 1732)
+++ /trunk/ArchTOIPipe/ProcWSophya/Makefile.in	(revision 1733)
@@ -42,5 +42,5 @@
 
 
-SRCFILES=map2toi.cc toi2map.cc rztoi.cc simtoipr.cc
+SRCFILES=map2toi.cc toi2map.cc rztoi.cc simtoipr.cc ktoibad.cc
 
 FILES=$(patsubst %.c,%.o,$(SRCFILES:.cc=.o))
Index: /trunk/ArchTOIPipe/ProcWSophya/ktoibad.cc
===================================================================
--- /trunk/ArchTOIPipe/ProcWSophya/ktoibad.cc	(revision 1733)
+++ /trunk/ArchTOIPipe/ProcWSophya/ktoibad.cc	(revision 1733)
@@ -0,0 +1,219 @@
+#include "machdefs.h"
+#include "toimanager.h"
+#include "pexceptions.h"
+#include "ntuple.h"
+#include "ktoibad.h"
+
+////////////////////////////////////////////////////////////////////////
+// Recherche des glitches :
+// INPUT:
+// lg : longeur maxi d'un glitch (impair, si pair lg++)
+//      doit etre suffisamment long pour que les glitches soient <lg
+//      suffisamment court pour ne pas tuer la galaxie
+// lm : longueur de la fenetre glissante (de chaque cote)
+//      <---lm---><--lg--><---lm--->
+//      doit etre suffisamment long pour avoir une bonne stabilite
+//      des calculs (a0,a1) et suffisamment court pour ne pas
+//      tuer la galaxie.
+// nsg : nombre de sigmas pour valider un glitch
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+FlagGlitch::FlagGlitch(uint_4 lg,uint_4 lm,r_8 nsg)
+  : mLP(0), mLPsnb(-1), mLPsne(-1)
+{
+ // Mise en forme des arguments
+ if(nsg<=0) nsg = 3.;
+ if(lg<=0) lg=3; if(lg%2==0) lg++;
+ if(lm<3) lm = 5*lg;
+
+ LGlitch = lg;
+ LSide =  lm;
+ NSCut = nsg;
+ Slpb[0] = Slpb[1] = NULL;
+ DoNotLookAt();
+ SetFlag();
+ SetBuffUpd();
+}
+
+FlagGlitch::~FlagGlitch()
+{
+ for(int i=0;i<2;i++) {if(Slpb[i]) delete Slpb[i]; Slpb[i]=NULL;}
+}
+
+////////////////////////////////////////////////////////////////////////
+void FlagGlitch::Print(ostream & os)
+{
+ os<<"FlagGlitch::Print"
+   <<" LGlitch="<<LGlitch<<" LSide="<<LSide<<" NSCut="<<NSCut
+   <<" flgNotLookAt="<<flgNotLookAt<<" flgGlitch="<<flgGlitch
+   <<" BuffUpdate="<<BuffUpdate<<endl;
+}
+
+////////////////////////////////////////////////////////////////////////
+void FlagGlitch::init() {
+  if(mLP) cout << "FlagGlitch::init" << endl;
+  declareInput("BoloIn");       // input index 0
+  declareOutput("BoloOut");     // output index 0
+
+  for(int i=0;i<2;i++)
+    Slpb[i] = new SLinParBuff(LSide,BuffUpdate,0.,0.,true);
+
+  int_4 lbuff = 2*LSide + LGlitch +10;
+  if(neededHistory<lbuff) setNeededHistory(lbuff);
+}
+
+////////////////////////////////////////////////////////////////////////
+void FlagGlitch::run()
+{
+long snb = getMinIn();
+long sne = getMaxIn();
+
+if(snb>sne) {
+  cout<<"FlagGlitch::run() - Bad sample interval"<<snb<<" , "<<sne<<endl;
+  throw ParmError("FlagGlitch::run() - Bad sample interval");
+}
+if(!checkInputTOIIndex(0)) {
+  cout<<"FlagGlitch::run() - Input TOI (BoloIn) not connected! "<<endl;
+  throw ParmError("FlagGlitch::run() Input TOI (BoloIn) not connected!");
+}
+if(!checkOutputTOIIndex(0)) {
+  cout<<"FlagGlitch::run() - Output TOI (BoloOut) not connected! "<<endl;
+  throw ParmError("FlagGlitch::run() Output TOI (BoloOut) not connected!");
+}
+
+//---------------------------------------------------------
+try {
+
+char* nament_[8] = {"s","bol","sg","sd","slg","sld","ypg","ypd"};
+r_4 xnt_[8];
+NTuple nt_(8,nament_);
+int intuple = mLP/10;
+
+if(mLP) cout<<"FlagGlitch::run(): SampleNum de "<<snb<<" a "<<sne<<endl;
+uint_4 nflag=0;
+r_8 nsg = NSCut;
+int_4 lg = LGlitch;
+int_4 lgs2 = (lg-1)/2;
+int_4 lm = LSide;
+int_4 lmmin = lm/2; if(lmmin<2) lmmin=2;
+
+for(int_4 sn=snb;sn<=sne;sn++) {
+  int_4 lp = (mLPsnb<=sn && sn<=mLPsne) ? mLP%10: 0;
+  r_8 bolog; uint_8 fgbolog=0;
+
+  getData(0,sn,bolog,fgbolog);
+  if(fgbolog && flgNotLookAt) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+
+  //                 i
+  //    <---lm---><--lg--><---lm--->
+  //    |   c=0  |        |   c=1  |
+  // ideb        |        |        ifin
+  //             ifin     ideb
+  r_8 bolo; uint_8 fgbolo=0;
+  int_4 ifill[2] = {sn-lgs2-1,sn+lgs2+lm};
+  if(lp>1) printf(">>>> sn=%d v=(%f,%ld)  ifill=[%d,%d]\n"
+                 ,sn,bolog,fgbolog,ifill[0],ifill[1]);
+
+  if(ifill[0]<snb) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+  getData(0,ifill[0],bolo,fgbolo);
+  if(fgbolo&flgNotLookAt) Slpb[0]->Pop();
+    else Slpb[0]->Push((r_8)ifill[0],bolo);
+  if((int_4)Slpb[0]->NPoints()<lmmin) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+
+  if(ifill[1]<=sne) {
+    getData(0,ifill[1],bolo,fgbolo);
+    if(fgbolo&flgNotLookAt) Slpb[1]->Pop();
+      else Slpb[1]->Push((r_8)ifill[1],bolo);
+  } else Slpb[1]->Pop();
+  if((int_4)Slpb[1]->NPoints()<lmmin) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+
+  // Calcul des valeurs pour decider
+  double s[2],slin[2],ypred[2];
+  for(int c=0;c<2;c++) {
+    r_8 mean,a0,a1;
+    s[c] = Slpb[c]->Compute(mean);
+    slin[c] = Slpb[c]->Compute(a0,a1);
+    ypred[c] = a0+a1*sn;
+    if(lp>1) printf("... c=%d m=%f  s=%f   ypred=%f slin=%f (%d)\n"
+                   ,c,mean,s[c],ypred[c],slin[c],Slpb[c]->NPoints());
+    if(slin[c]<0. || s[c]<0.) {
+      putData(0,sn,bolog,fgbolog);
+      continue;
+    }
+  }
+
+  if(intuple>0) {
+    xnt_[0] = sn;  xnt_[1] = bolog;
+    xnt_[2] = s[0];     xnt_[3] = s[1];
+    xnt_[4] = slin[0];  xnt_[5] = slin[1];
+    xnt_[6] = ypred[0]; xnt_[7] = ypred[1];
+    nt_.Fill(xnt_);
+  }
+
+  // Si les predictions ne sont pas compatibles (sig), pas de deglitch
+  r_8 yy = ypred[1]-ypred[0];
+  r_8 sy = nsg*sqrt(s[0]*s[0]+s[1]*s[1]);
+  if(lp>1) printf("... yy=|%f| > sy=%f ???\n",yy,sy);
+  if(fabs(yy) > sy) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+
+  // Si la valeur est entre les 2 predictions, pas de deglitch
+  r_8 dp0 = ypred[0];
+  r_8 dp1 = ypred[1];
+  if(ypred[1]<ypred[0]) {dp0 = ypred[1]; dp1 = ypred[0];}
+  if(lp>1) printf("... %f dans [%f,%f] ???\n",bolog,dp0,dp1);
+  if(dp0<bolog && bolog<dp1) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+
+  // A + de N sigmas (lin) des 2 predictions
+  dp0 = fabs(bolog-ypred[0]); slin[0] *= nsg;
+  dp1 = fabs(bolog-ypred[1]); slin[1] *= nsg;
+  if(lp>1) printf("... dp0=%f > %f  et   dp1=%f > %f ???\n"
+                 ,dp0,slin[0],dp1,slin[1]);
+  if(dp0 < slin[0] || dp1 < slin[1]) {
+    putData(0,sn,bolog,fgbolog);
+    continue;
+  }
+
+  // C'est un glitch
+  nflag++;
+  fgbolog |= flgGlitch;
+  if(lp>1)
+    printf("......... Glitch en sn=%d ...  %d glitches found\n",sn,nflag);
+  putData(0,sn,bolog,fgbolog);
+
+}
+if(mLP) cout<<"FlagGlitch::run(): Fin de boucle: nflag="<<nflag
+            <<" / "<<sne-snb+1<<endl;
+
+if(intuple>0)  {
+ char *fileout_ = "flagglitch.ppf";
+ string tag_ = "nfg";
+ POutPersist pos_(fileout_); pos_.PutObject(nt_,tag_);
+}
+
+//---------------------------------------------------------
+} catch (PException & exc) {
+  cout<<"FlagGlitch: Catched Exception "<<(string)typeid(exc).name()
+      <<"\n .... Msg= "<<exc.Msg()<<endl;
+}
+
+return;                                                                            
+}
Index: /trunk/ArchTOIPipe/ProcWSophya/ktoibad.h
===================================================================
--- /trunk/ArchTOIPipe/ProcWSophya/ktoibad.h	(revision 1733)
+++ /trunk/ArchTOIPipe/ProcWSophya/ktoibad.h	(revision 1733)
@@ -0,0 +1,45 @@
+// This may look like C code, but it is really -*- C++ -*-
+#ifndef TOI2MAP_H
+#define TOI2MAP_H
+
+#include "toiprocessor.h"
+#include "flagtoidef.h"
+#include "slinparbuff.h"
+
+//-- Pour rechercher les glitches
+//   toi BoloIn ---> |FlagGlitch | ---> toi BoloOut
+
+class FlagGlitch : public TOIProcessor {
+public:
+  FlagGlitch(uint_4 lg,uint_4 lm,r_8 nsg);
+  virtual ~FlagGlitch();
+
+  virtual void	init(void);
+  virtual void	run(void);
+
+  //! Ne pas traiter les sample deja flaggues par la valeur flag
+  inline void DoNotLookAt(uint_8 flag=0) {flgNotLookAt = flag;}
+  //! Flagguer les glitches avec la valeur flag
+  inline void SetFlag(uint_8 flag=FlgToiSpike) {flgGlitch = flag;}
+  //! Frequency to update SLinParBuff buffer
+  void SetBuffUpd(uint_4 bupd=1000) {BuffUpdate = bupd;}
+  //! Print
+  void Print(ostream & os);
+  inline void Print(void) {Print(cout);}
+  //! Debugging print lp=ab
+  //    a>0 ntuple entre snb,sne
+  //    b>0 print general
+  //     >1 print debug entre snb,sne
+  inline void SetDebug(int_4 lp,int_4 snb,int_4 sne)
+              {mLP=(lp>=0)?lp:0; mLPsnb=snb; mLPsne=sne;}
+
+protected:
+  int_4 mLP,mLPsnb,mLPsne;
+  uint_8 flgNotLookAt,flgGlitch;
+  r_8 NSCut;
+  uint_4 BuffUpdate;
+  uint_4 LGlitch,LSide;
+  SLinParBuff* Slpb[2];
+};
+
+#endif
Index: /trunk/ArchTOIPipe/TestPipes/tstktoibad.cc
===================================================================
--- /trunk/ArchTOIPipe/TestPipes/tstktoibad.cc	(revision 1733)
+++ /trunk/ArchTOIPipe/TestPipes/tstktoibad.cc	(revision 1733)
@@ -0,0 +1,123 @@
+#include <stdexcept>
+#include <unistd.h>
+#include "toi.h"
+#include "toiprocessor.h"
+#include "fitstoirdr.h"
+#include "fitstoiwtr.h"
+#include "toimanager.h"
+#include "toisegment.h"
+#include "sophyainit.h"
+#include "ktoibad.h"
+
+void usage(void);
+void usage(void) {
+ cout<<"tstktoibad [-h] [options] fits_in fits_out"
+ <<" -s sdeb,sfin"
+ <<" -b label_bolomuv"
+ <<" -g lg,lm,nsg,bupd"
+ <<" -p lp,sn1,sn2"
+ <<endl;
+}
+
+int main(int narg, char** arg)
+{
+ int_4 sdeb=0,sfin=-1;
+ int_4 lp=1,sn1=-1,sn2=-1;
+ uint_4 lg=3,lm=15,bupd=1000;
+ r_8 nsg=5.;
+ char *label_bolomuv = "boloMuV";
+  int c;
+ while((c = getopt(narg,arg,"hs:g:b:p:")) != -1) {
+  switch (c) {
+  case 's' :
+   sscanf(optarg,"%d,%d",&sdeb,&sfin);
+   break;
+  case 'g' :
+   sscanf(optarg,"%d,%d,%lf,%d",&lg,&lm,&nsg,&bupd);
+   break;
+  case 'b' :
+    label_bolomuv = optarg;
+    break;
+  case 'p' :
+   sscanf(optarg,"%d,%d,%d",&lp,&sn1,&sn2);
+   break;
+  case 'h' :
+  default:
+   usage(); exit(1);
+  }
+ }
+ if(optind+1>=narg) {usage(); exit(2);}
+ char * fitsin  = arg[optind];
+ char * fitsout = arg[optind+1];
+
+ SophyaInit();
+ TOIManager* mgr = TOIManager::getManager();
+ if(sfin>=sdeb) mgr->setRequestedSample(sdeb,sfin);
+
+ //--------------------------------------------------------------------
+ try {
+ //--------------------------------------------------------------------
+
+ // FITS reader
+ FITSTOIReader rfits(fitsin);
+ cout<<"Lecteur: created"<<endl;
+
+ // FITS writter
+ FITSTOIWriter wfits(fitsout);
+ cout<<"Ecriveur: created"<<endl;
+
+ // TOI processeurs
+ FlagGlitch deglitch(lg,lm,nsg);
+ deglitch.SetBuffUpd(bupd);
+ deglitch.SetDebug(lp,sn1,sn2);
+ deglitch.Print();
+
+ // Creation des tuyaux et des connections associees
+ int taille = 8192;
+ // tuyau bolo pour entree processeur
+ TOISegmented * boloin = new TOISegmented("tuyau_bolo_in",taille);
+ rfits.addOutput(label_bolomuv,boloin);
+ deglitch.addInput("BoloIn",boloin);
+
+ // tuyau bolo pour sortie processeur
+ bool writeflag=true;
+ TOISegmented * boloout = new TOISegmented("tuyau_bolo_out",taille);
+ deglitch.addOutput("BoloOut",boloout);
+ wfits.addInput(label_bolomuv,boloout,writeflag);
+
+ // Print de status avant lancement des taches:
+ cout<<"----- FITSReaderTOI::PrintStatus() 1 : -----"<<endl;
+ rfits.PrintStatus(cout);
+ cout<<"----- FITSWriterTOI::PrintStatus() : -----"<<endl;
+ wfits.PrintStatus(cout);  
+ cout<<"----- TOISegmented::PrintStatus() : -----"<<endl;
+ boloin->PrintStatus(cout);  
+ boloout->PrintStatus(cout);  
+ cout<<"-----FlagGlitch ::PrintStatus() : -----"<<endl;
+ deglitch.PrintStatus(cout);  
+
+ // Lancement des taches
+
+ rfits.start();
+ wfits.start();
+ deglitch.start();
+
+ // Gestion de la re-connection des threads
+ mgr->joinAll();
+ cout<<"End threads"<<endl;
+
+ //--------------------------------------------------------------------
+ // Que fait on avec les exceptions qui ont ete lancees ?
+ } catch (PThrowable & exc) {    // Sophya exceptions
+  cout<<"\ntstktoibad: Catched Exception \n"<<(string)typeid(exc).name() 
+      <<" - Msg= "<<exc.Msg()<<endl;
+ } catch (const std::exception & sex) {    // Standard exceptions
+  cout<<"\ntstktoibad: Catched std::exception \n" 
+      <<(string)typeid(sex).name()<<endl; 
+ } catch (...) {    // Other exceptions
+  cout<<"\ntstktoibad: some other exception was caught ! "<<endl;
+ }
+ //--------------------------------------------------------------------
+
+ return(0);
+}
Index: /trunk/ArchTOIPipe/files_pipes
===================================================================
--- /trunk/ArchTOIPipe/files_pipes	(revision 1732)
+++ /trunk/ArchTOIPipe/files_pipes	(revision 1733)
@@ -16,2 +16,3 @@
 tsttoi2.cc
 tsttoi2map.cc
+tstktoibad.cc
Index: /trunk/ArchTOIPipe/files_sophya
===================================================================
--- /trunk/ArchTOIPipe/files_sophya	(revision 1732)
+++ /trunk/ArchTOIPipe/files_sophya	(revision 1733)
@@ -8,2 +8,4 @@
 toi2map.h
 flagtoidef.h
+ktoibad.h
+ktoibad.cc
