Index: /BAORadio/AmasNancay/v2/analyse.cc
===================================================================
--- /BAORadio/AmasNancay/v2/analyse.cc	(revision 538)
+++ /BAORadio/AmasNancay/v2/analyse.cc	(revision 538)
@@ -0,0 +1,1722 @@
+//
+// Analyse of Amas@Nancay runs by J.E Campagne (LAL)
+// Version 0: 1/6/2011
+//-----------------------------
+
+// Utilisation de SOPHYA pour faciliter les tests ...
+#include "sopnamsp.h"
+#include "machdefs.h"
+
+#include <stdlib.h>
+#include <dirent.h>
+#include <matharr.h>
+
+// include standard c/c++
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+#include <map>
+#include <functional>
+#include <algorithm>
+#include <numeric>
+#include <list>
+#include <exception>
+
+// include sophya mesure ressource CPU/memoire ...
+#include "resusage.h"
+#include "ctimer.h"
+#include "timing.h"
+#include "timestamp.h"
+#include "strutilxx.h"
+#include "ntuple.h"
+#include "fioarr.h"
+#include "tarrinit.h"
+#include "histinit.h"
+#include "fitsioserver.h"
+#include "fiosinit.h"
+#include "ppersist.h"
+
+
+const sa_size_t NUMBER_OF_CHANNELS = 2;
+const sa_size_t  NUMBER_OF_FREQ = 8192;
+const r_4    LOWER_FREQUENCY = 1250.0; //MHz
+const r_4    TOTAL_BANDWIDTH = 250.0; //MHz
+
+//decalration of non class members functions
+extern "C" {
+  int Usage(bool);
+}
+
+
+//----------------------------------------------------------
+//Utility fonctions
+// Function for deleting pointers in map.
+template<class A, class B>
+struct DeleteMapFntor
+{
+    // Overloaded () operator.
+    // This will be called by for_each() function.
+    bool operator()(pair<A,B> x) const
+    {
+        // Assuming the second item of map is to be
+        // deleted. Change as you wish.
+        delete x.second;
+        return true;
+    }
+};
+//-----
+bool compare(const pair<string,r_4>& i, const pair<string,r_4>& j) {
+  return i.second < j.second;
+}
+//-----
+sa_size_t round_sa(r_4 r) {
+  return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5));
+}
+//-----
+string StringToLower(string strToConvert){
+  //change each element of the string to lower case
+  for(unsigned int i=0;i<strToConvert.length();i++) {
+    strToConvert[i] = tolower(strToConvert[i]);
+  }
+  return strToConvert;//return the converted string
+}
+//-----
+//JEC 22/9/11 comparison, not case sensitive to sort File liste START
+bool stringCompare( const string &left, const string &right ){
+   if( left.size() < right.size() )
+      return true;
+   return false;
+   for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
+      if( tolower( *lit ) < tolower( *rit ) )
+         return true;
+      else if( tolower( *lit ) > tolower( *rit ) )
+         return false;
+}//JEC 22/9/11 comparison, not case sensitive to sort File liste END
+//-----
+list<string> ListOfFileInDir(string dir, string filePettern) throw(string) {
+  list<string> theList;
+
+
+  DIR *dip;
+  struct dirent *dit;
+  string msg;  string fileName;
+  string fullFileName;
+  size_t found;
+
+  if ((dip=opendir(dir.c_str())) == NULL ) {
+    msg = "opendir failed on directory "+dir;
+    throw msg;
+  }
+  while ( (dit = readdir(dip)) != NULL ) {
+    fileName = dit->d_name;
+    found=fileName.find(filePettern);
+    if (found != string::npos) {
+      fullFileName = dir + "/";
+      fullFileName += fileName;
+      theList.push_back(fullFileName);
+    }
+  }//eo while
+  if (closedir(dip) == -1) {
+    msg = "closedir failed on directory "+dir;
+    throw msg;
+  }
+  
+  //JEC 22/9/11 START
+  theList.sort(stringCompare);
+  //JEC 22/9/11 END
+
+  return theList;
+}
+
+
+//Declaration of local classes
+//----------------------------------------------
+//Process Interface
+class IProcess {
+public:
+  IProcess() {}
+  virtual ~IProcess() {}
+  virtual int processCmd() throw(string) =0;
+};
+//------------
+//Common Process
+class ProcessBase : public IProcess {
+public:
+  ProcessBase();
+  virtual ~ProcessBase();
+  void SetInputPath(const string& inputPath) {inputPath_ = inputPath;}
+  void SetOutputPath(const string& outputPath) {outputPath_ = outputPath;}
+  void SetSourceName(const string& sourceName) {sourceName_ = sourceName;}
+  void SetDateOfRun(const string& dateOfRun) {dateOfRun_ = dateOfRun;}
+  void SetSpectraDirectory(const string& spectraDirectory) {spectraDirectory_ = spectraDirectory;}
+  void SetTypeOfFile(const string& typeOfFile) { typeOfFile_ = typeOfFile; }
+  void SetNumCycle(const string& numcycle) {numcycle_ = numcycle; }
+  void SetScaFileName(const string& scaFileName) { scaFileName_ =scaFileName; }
+
+  void SetDebugLevel(const string& debuglev) {
+    debuglev_ = atoi(debuglev.c_str());
+  }
+
+  virtual int processCmd() throw(string);
+  
+protected:
+  string inputPath_;
+  string outputPath_;
+  string sourceName_;
+  string dateOfRun_;
+  string spectraDirectory_;
+  string typeOfFile_;
+
+  string numcycle_; //cycle numbers format="first,last"
+  sa_size_t ifirstCycle_;
+  sa_size_t ilastCycle_;
+
+
+  uint_4 debuglev_; 
+  string scaFileName_;
+  NTuple* scaTuple_;
+  map<sa_size_t,sa_size_t> idCycleInTuple_;
+};
+ProcessBase::ProcessBase() {
+  scaTuple_ = 0;
+}
+ProcessBase::~ProcessBase() {
+  if (scaTuple_) delete scaTuple_;
+  scaTuple_ = 0;
+}
+//------------
+//Process ON/OFF data
+//------------
+class ProcessONOFFData : public ProcessBase {
+protected:
+  string  freqBAOCalibration_;//string MHz
+public:
+  ProcessONOFFData(){}
+  virtual ~ProcessONOFFData(){}
+
+  void SetFreqBAOCalibration(const string& freqBAOCalibration) { 
+    freqBAOCalibration_ = freqBAOCalibration; 
+  }
+  
+  virtual int processCmd() throw(string);
+};
+
+//JEC 22/9/11 Make ON-OFF analysis WO any calibration START
+//------------
+//Process ON/OFF Raw data
+//------------
+class ProcessONOFFRawData : public ProcessBase {
+
+public:
+  ProcessONOFFRawData(){}
+  virtual ~ProcessONOFFRawData(){}
+  
+  virtual int processCmd() throw(string);
+};
+//JEC 22/9/11 Make ON-OFF analysis WO any calibration END
+
+//------------
+//Process Gain
+//------------
+class ProcessGain : public ProcessBase {
+protected:
+  string mode_; //mode of data taken for gain computation On || Off
+public:
+  ProcessGain(){}
+  virtual ~ProcessGain(){}
+
+  void SetMode(const string& mode) {mode_ = mode;}
+  
+  virtual int processCmd() throw(string);
+};
+//------------
+//Process Calibration
+//------------
+class ProcessCalibration : public ProcessBase {
+protected:
+  string option_; //option of calibration procedure
+  string  freqBAOCalibration_;//string MHz
+  r_4 valfreqBAOCalibration_; //value MHz
+  string bandWidthBAOCalibration_;//string MHz
+  r_4 valbandWidthBAOCalibration_;//value MHz
+  
+  sa_size_t lowerFreqBin_;
+  sa_size_t upperFreqBin_;
+
+public:
+  ProcessCalibration() {}
+  virtual ~ProcessCalibration(){}
+
+  void SetOption(const string& option) {option_ = option;}
+  void SetFreqBAOCalibration(const string& freqBAOCalibration) { 
+    freqBAOCalibration_ = freqBAOCalibration; 
+    valfreqBAOCalibration_ = atof(freqBAOCalibration_.c_str());
+  }
+  void SetBandWidthBAOCalibration(const string& bandWidthBAOCalibration) { 
+    bandWidthBAOCalibration_ = bandWidthBAOCalibration; 
+    valbandWidthBAOCalibration_ = atof(bandWidthBAOCalibration_.c_str());
+  }
+
+  void ComputeLowerUpperFreqBin();
+      
+  virtual int processCmd() throw(string);
+};
+void ProcessCalibration::ComputeLowerUpperFreqBin() {
+  sa_size_t c0 = round_sa(NUMBER_OF_FREQ*(valfreqBAOCalibration_-LOWER_FREQUENCY)/TOTAL_BANDWIDTH);
+  sa_size_t dc = round_sa(NUMBER_OF_FREQ*valbandWidthBAOCalibration_/TOTAL_BANDWIDTH);
+  lowerFreqBin_ = c0-dc/2;
+  upperFreqBin_ = c0+dc/2;
+}
+
+
+//----------------------------------------------------
+//----------------------------------------------------
+int main(int narg, char* arg[])
+{
+
+  //Init process types
+  map<string,IProcess*> process;
+  //JEC 22/9/11 Make ON-OFF analysis WO any calibration START
+  process["rawOnOff"] = new ProcessONOFFRawData();
+  //JEC 22/9/11 Make ON-OFF analysis WO any calibration END
+  process["dataOnOff"] = new ProcessONOFFData();
+  process["gain"]      = new ProcessGain();
+  process["calib"]     = new ProcessCalibration();
+
+  //Init Sophya related modules
+  //  SophyaInit();
+  TArrayInitiator _inia; //nneded for TArray persistancy
+  FitsIOServerInit(); //needed for input file
+
+  //message used in Exceptions
+  string msg;
+
+  //Return code
+  int rc = 0;
+
+  //Arguments managements
+  if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage(false);
+  if (narg<11) return Usage(true);
+
+  string action;
+  string inputPath = "."; 
+  string outputPath = "."; 
+  string sourceName;
+  string scaFile;
+  string dateOfRun;
+  string spectraDirectory;
+  string freqBAOCalib = "";
+  string bandWidthBAOCalib = "";
+  string debuglev = "0";
+  string mode = "";
+  string numcycle;
+  string calibrationOpt = "";  
+
+  string typeOfFile="medfiltmtx";
+  
+
+  //  bool okarg=false;
+  int ka=1;
+  while (ka<(narg-1)) {
+    if (strcmp(arg[ka],"-debug")==0) {
+      debuglev=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-act")==0) {
+      action=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-inPath")==0) {
+      inputPath=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-outPath")==0) {
+      outputPath=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-source")==0) {
+      sourceName=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-sca")==0) {
+      scaFile=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-date")==0) {
+      dateOfRun=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-specdir")==0) {
+      spectraDirectory=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-specname")==0) {
+      typeOfFile=arg[ka+1];
+      ka+=2;
+    }    
+    else if (strcmp(arg[ka],"-freqBAOCalib")==0) {
+      freqBAOCalib = arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-bwBAOCalib")==0) {
+      bandWidthBAOCalib = arg[ka+1];
+      ka+=2;
+    } 
+    else if (strcmp(arg[ka],"-mode")==0) {
+      mode =arg[ka+1];
+      ka+=2; 
+    }
+    else if (strcmp(arg[ka],"-numcycle")==0) {
+      numcycle =arg[ka+1];
+      ka+=2; 
+    }
+    else if (strcmp(arg[ka],"-calibopt")==0) {
+      calibrationOpt =arg[ka+1];
+      ka+=2; 
+    }
+    else ka++;
+  }//eo while
+
+
+  //JEC 21/9/11 Give the input parameters START
+  cout << "Dump Iiitial parameters ............" << endl;
+  cout << " action = " << action << "\n"
+       << " inputPath = " << inputPath << "\n" 
+       << " outputPath = " << outputPath << "\n"
+       << " sourceName = " << sourceName << "\n"
+       << " scaFile = " << scaFile << "\n"
+       << " dateOfRun = " << dateOfRun << "\n"
+       << " spectraDirectory = " << spectraDirectory << "\n"
+       << " freqBAOCalib = " << freqBAOCalib  << "\n"
+       << " bandWidthBAOCalib = " << bandWidthBAOCalib << "\n"
+       << " debuglev = "  << debuglev  << "\n"
+       << " mode = " << mode << "\n"
+       << " numcycle = " << numcycle << "\n"
+       << " calibrationOpt = " << calibrationOpt << endl;
+  cout << "...................................." << endl;
+  //JEC 21/9/11 Give the input parameters END
+
+
+  try {
+    //verification of action
+    if(process.find(action) == process.end()) {
+      msg = "action ";
+      msg += action + " not valid... FATAL";
+      rc = 999;
+      throw msg;
+    }
+    
+
+    //
+    //Process initialisation...
+    //
+    try {
+      ProcessBase* procbase = dynamic_cast<ProcessBase*>(process[action]);
+      if (procbase == 0) {
+	msg= "action ";
+	msg += action + "Not a <process base> type...FATAL";
+	rc = 999;
+	throw msg;
+      }
+      procbase->SetInputPath(inputPath);
+      procbase->SetOutputPath(outputPath);
+
+      if ("" == sourceName) {
+	msg = "(FATAL) missingsourceName  for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetSourceName(sourceName);
+
+      if ("" == dateOfRun) {
+	msg = "(FATAL) missing dateOfRun for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetDateOfRun(dateOfRun);
+
+      
+      if ("" == spectraDirectory) {
+	msg = "(FATAL) missing spectraDirectory for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetSpectraDirectory(spectraDirectory);
+
+      if ("" == scaFile) {
+	msg = "(FATAL) missing scaFile for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetScaFileName(scaFile);
+
+      if ("" == numcycle) {
+	msg = "(FATAL) missing cycle number for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetNumCycle(numcycle);
+
+
+      procbase->SetTypeOfFile(typeOfFile);
+
+      procbase->SetDebugLevel(debuglev);
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+
+    //JEC 22/9/11 Make ON-OFF analysis WO any calibration START
+//     try {
+//       ProcessONOFFRawData* procRawdata = dynamic_cast<ProcessONOFFRawData*>(process[action]);
+//     }
+//     catch(exception& e){
+//       throw e.what();
+//     }
+    //JEC 22/9/11 Make ON-OFF analysis WO any calibration END
+
+
+    try {
+      ProcessONOFFData* procdata = dynamic_cast<ProcessONOFFData*>(process[action]);
+      if (procdata) {
+	if (freqBAOCalib == "") {
+	  msg = "(FATAL) missing calibration BAO frequency for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	procdata->SetFreqBAOCalibration(freqBAOCalib);
+      }
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+    
+
+    try {
+      ProcessGain* procgain = dynamic_cast<ProcessGain*>(process[action]);
+      if(procgain) {
+	if (mode == "") {
+	  msg = "(FATAL) missing mode-type for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	procgain->SetMode(mode);
+      }
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+
+    try {
+      ProcessCalibration* proccalib = dynamic_cast<ProcessCalibration*>(process[action]);
+      if(proccalib) {
+	if (calibrationOpt == "") {
+	  msg = "(FATAL) missing calibration option";
+	  Usage(true);
+	  throw msg;
+	}
+	if (freqBAOCalib == "") {
+	  msg = "(FATAL) missing calibration BAO frequency for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	if (bandWidthBAOCalib == "") {
+	  msg = "(FATAL) missing calibration BAO frequency band width for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	proccalib->SetOption(calibrationOpt);
+	proccalib->SetFreqBAOCalibration(freqBAOCalib);
+	proccalib->SetBandWidthBAOCalibration(bandWidthBAOCalib);
+	proccalib->ComputeLowerUpperFreqBin();
+      }
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+
+    //
+    //execute command
+    //
+    rc = process[action]->processCmd();
+
+  }
+  catch (std::exception& sex) {
+    cerr << "\n analyse.cc std::exception :"  << (string)typeid(sex).name() 
+	 << "\n msg= " << sex.what() << endl;
+    rc = 78;
+  }
+  catch ( string str ) {
+    cerr << "analyse.cc Exception raised: " << str << endl;
+  }
+  catch (...) {
+    cerr << " analyse.cc catched unknown (...) exception  " << endl; 
+    rc = 79; 
+  } 
+
+  
+
+
+  cout << ">>>> analyse.cc ------- END ----------- RC=" << rc << endl;
+  
+  //Delete processes
+  for_each(process.begin(),process.end(), DeleteMapFntor<string,IProcess*>());
+
+  return rc;
+}
+
+//---------------------------------------------------
+int Usage(bool flag) {
+  cout << "Analyse.cc usage...." << endl;
+  cout << "analyse  -act <action_type>: dataOnOff, rawOnOff, gain, calib\n"
+       << "         -inPath <path for input files: default='.'>\n"
+       << "         -outPath <path for output files: default='.'>\n"
+       << "         -source <source name> \n" 
+       << "         -date <YYYYMMDD>\n"
+       << "         -sca <file name scaXYZ.sum.trans>\n"
+       << "         -specdir <generic directory name of spectra fits files>\n"
+       << "         -specname <generic name of spectra fits files>\n"
+       << "         -freqBAOCalib <freq in MHZ> freq. of calibration BAO\n"
+       << "            valid for act=dataOnOff\n"
+       << "         -bwBAOCalib <band width MHz> band width arround central freq. for calibration BAO\n"
+       << "            valid for act=calib\n"
+       << "         -mode <mode_type>:\n"
+       << "            valid for act=gain, mode_type: On, Off\n"
+       << "         -numcycle <number>,<number>:\n"
+       << "            valid for all actions"
+       << "         -calibopt <option>:\n"
+       << "            valid for act=calib: indiv OR mean (NOT USED)"
+       << "         -debuglev <number> [0=default]\n"
+       << "           1: normal print\n"
+       << "           2: save intermediate spectra\n"
+       << endl;
+  if (flag) {
+    cout << "use <path>/analyse -h for detailed instructions" << endl;
+    return 5;
+  }
+  return 1;
+}
+
+int ProcessBase::processCmd() throw(string) {
+  int rc =0;
+  string msg;
+  if(debuglev_>0)cout << "Process Base" << endl;
+  //------------------------
+  //Use the sca file informations
+  //------------------------
+  //  string scaFullPathName = "./";
+  //TOBE FIXED  scaFullPathName += sourceName_+"/"+dateOfRun_ + StringToLower(sourceName_)+"/";
+  string scaFullPathName = inputPath_ + "/" 
+    + sourceName_+ "/" +dateOfRun_ + StringToLower(sourceName_)+ "/" + scaFileName_;
+  char* scaTupleColumnName[9] = {"cycle","stcalOn","spcalOn","stOn","spOn","stcalOff","spcalOff","stOff","spOff"};
+  scaTuple_ = new NTuple(9,scaTupleColumnName);
+  int n = scaTuple_->FillFromASCIIFile(scaFullPathName);
+  if(n<0){ //Error
+    msg = "(FATAL) NTuple error loading "+ scaFullPathName;
+    rc = 999;
+    throw msg;
+  }
+  
+  if(debuglev_>1){
+    cout << "ProcessBase::processCmd: dump tuple in " << scaFullPathName << endl;
+    scaTuple_->Show(cout);
+  }
+  
+  
+  //Get the cycles (here consider consecutive cycles)    
+  //The SCA file cannot be used as the DAQ can miss some cycles...
+  //     r_8 firstCycle, lastCycle;
+  //     scaTuple_->GetMinMax("cycle",firstCycle,lastCycle);
+  //     ifirstCycle_ = (sa_size_t)firstCycle;
+  //     ilastCycle_  = (sa_size_t)lastCycle;
+  //Analyse the string given by -numcycle command line
+  int ai1=0,ai2=0;
+  sscanf(numcycle_.c_str(),"%d,%d",&ai1,&ai2);
+  ifirstCycle_ = (sa_size_t)ai1;
+  ilastCycle_  = (sa_size_t)ai2;
+  
+
+  //associate cycle number to index line in tuple
+  sa_size_t nLines = scaTuple_->NbLines();
+  for(sa_size_t iL=0; iL<nLines; ++iL){
+    idCycleInTuple_[(sa_size_t)scaTuple_->GetCell(iL,"cycle")]=iL;
+  }
+
+
+  return rc;
+}
+//JEC 22/9/11 Make ON-OFF analysis WO any calibration START
+//----------------------------------------------
+int ProcessONOFFRawData::processCmd() throw(string) {
+  int rc = 0;
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+  if(debuglev_>0)cout << "Process Raw Data ON OFF" << endl;
+  vector<string> modeList;
+  modeList.push_back("On");
+  modeList.push_back("Off");
+  vector<string>::const_iterator iMode;
+  
+  uint_4 id; 
+  string tag;
+
+  //
+  //Process to get sucessively
+  //Raw Spectra, 
+  //The pocesses are separated to allow intermediate save of results
+
+  map< pair<string, sa_size_t>, TMatrix<r_4> > spectreCollect;
+  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iSpectre, iSpectreEnd;
+  
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process RAW Mode " << mode << endl;
+
+    //------------------------------------------
+    //Produce Raw spectra per cycle
+    //------------------------------------------
+
+    string directoryName;
+    list<string> listOfSpecFiles;
+    list<string>::const_iterator iFile, iFileEnd;
+    
+        
+    //
+    //loop on cycles
+    //
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      directoryName = "./" + mode + "/";
+      stringstream sicycle;
+      sicycle << icycle;
+      directoryName += spectraDirectory_ + sicycle.str() + "/";
+
+      //read directory
+      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+      
+
+      //compute mean of spectra created in a cycle
+      if(debuglev_>0)cout << "compute mean for cycle " << icycle << endl;
+      TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+      iFileEnd = listOfSpecFiles.end();
+      r_4 nSpectres  = 0;
+      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+	FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+	TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+	aSpectrum >> spectre;
+	spectreMean += spectre;
+	nSpectres++;
+      }// end of for files 
+      if(nSpectres>0) spectreMean /= nSpectres;
+      
+      //save mean spectrum
+      spectreCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreMean,false) ));
+    }//end of for cycles
+  }//end loop on mode for raw preocess
+
+  //JEC 23/9/11 DO IT 
+  //  if(debuglev_>1) {//save mean spectra on file 
+  cout << "Save mean raw spectra" << endl;
+  string fileName;
+  fileName = "./dataRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+
+  POutPersist fos(fileName);
+  id=0;
+  iSpectreEnd = spectreCollect.end();
+  for (iSpectre = spectreCollect.begin();
+       iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+    tag = "specRaw";
+    tag += (iSpectre->first).first;
+    stringstream sid;
+    sid << (iSpectre->first).second;
+    tag += sid.str();
+    if(debuglev_>9) {
+      cout << "save tag<" << tag << ">" << endl;
+    }
+    fos << PPFNameTag(tag) << iSpectre->second;
+  }
+    //  }//end of save fits
+  
+
+  //------------------------------------------
+  // Perform ON-OFF
+  //------------------------------------------
+  
+  map< sa_size_t, TMatrix<r_4> > diffCollect;
+  map< sa_size_t, TMatrix<r_4> >::iterator iDiff, iDiffEnd;
+
+  TMatrix<r_4> diffMeanOnOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //init zero
+  r_4 nCycles = 0;
+  for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+    nCycles++;
+    TMatrix<r_4> specmtxOn(spectreCollect[make_pair(modeList[0],icycle)],false); //clone the memory 
+    TMatrix<r_4> specmtxOff(spectreCollect[make_pair(modeList[1],icycle)],false); //clone the memory 
+    TMatrix<r_4> diffOnOff = specmtxOn - specmtxOff;
+    diffCollect.insert(pair< sa_size_t,TMatrix<r_4> >(icycle,TMatrix<r_4>(diffOnOff,false) ));
+    diffMeanOnOff += diffOnOff;
+  }//end loop on cycle
+  if(nCycles>0) diffMeanOnOff/=nCycles;
+
+  //exctract channels and do the mean
+  TVector<r_4> meanOfChan(NUMBER_OF_FREQ); //implicitly init to 0
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
+    meanOfChan += diffMeanOnOff.Row(iCh).Transpose();
+  }
+  meanOfChan /= (r_4)NUMBER_OF_CHANNELS;
+  
+
+
+  {//save diff ON-OFF on Raw data
+    if(debuglev_>0)cout << "save ON-OFF RAW spectra" << endl;
+    string fileName;
+    fileName = "./diffOnOffRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist fos(fileName);
+    iDiffEnd = diffCollect.end();
+    id = 0;
+
+    //JEC 22/9/11 Mean & Sigma in 32-bins size START
+    sa_size_t nSliceFreq = 32; //TODO: put as an input parameter option ?
+    sa_size_t deltaFreq = NUMBER_OF_FREQ/nSliceFreq;
+    //JEC 22/9/11 Mean & Sigma in 32-bins size END
+
+    for (iDiff = diffCollect.begin();iDiff != iDiffEnd ; ++iDiff, id++) {
+      tag = "specONOFFRaw";
+      stringstream sid;
+      sid << iDiff->first;
+      tag += sid.str();
+      fos << PPFNameTag(tag) << iDiff->second;
+
+      //JEC 22/9/11 Mean & Sigma in 32-bins size START
+      if (debuglev_>9) {
+ 	cout << "Debug slicing: slice/delta " << nSliceFreq << " " << deltaFreq << endl;
+      }
+      TMatrix<r_4> reducedMeanDiffOnOff(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
+      TMatrix<r_4> reducedSigmaDiffOnOff(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
+      for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
+	sa_size_t freqLow= iSlice*deltaFreq;
+	sa_size_t freqHigh= freqLow + deltaFreq -1;
+	if (debuglev_>9) {
+	  cout << "Debug .......... slicing ["<< iSlice << "]: low/high freq" << freqLow << "/" << freqHigh << endl;
+	}
+	for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
+	  if (debuglev_>9) {
+	    cout << "Debug .......... Channel " << iCh;
+	  }
+	  TVector<r_4> reducedRow;
+	  reducedRow = (iDiff->second).SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
+	  double mean; 
+	  double sigma;
+	  MeanSigma(reducedRow,mean,sigma);
+	  if (debuglev_>9) {
+	    cout << "mean/signa " << mean << "/" << sigma << endl;
+	  }
+	  reducedMeanDiffOnOff(iCh,iSlice) = mean;
+	  reducedSigmaDiffOnOff(iCh,iSlice) = sigma;
+	}//loop on Channel
+      }//loop on Freq. slice 
+      tag = "redMeanONOFFRaw";
+      tag += sid.str();
+      fos << PPFNameTag(tag) << reducedMeanDiffOnOff;
+      tag = "redSigmaONOFFRaw";
+      tag += sid.str();
+      fos << PPFNameTag(tag) << reducedSigmaDiffOnOff;
+      //JEC 22/9/11 END
+
+    }//loop on ON-OFF spectre
+    //save the mean also
+    fos << PPFNameTag("specONOFFRawMean") << diffMeanOnOff;
+
+    //JEC 22/9/11 START
+    TMatrix<r_4> reducedMeanDiffOnOffAll(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
+    TMatrix<r_4> reducedSigmaDiffOnOffAll(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
+    for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
+      sa_size_t freqLow= iSlice*deltaFreq;
+      sa_size_t freqHigh= freqLow + deltaFreq -1;
+      if (debuglev_>9) {
+	cout << "Debug .......... slicing ["<< iSlice << "]: low/high freq" << freqLow << "/" << freqHigh << endl;
+      }
+      for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
+	if (debuglev_>9) {
+	  cout << "Debug .......... Channel " << iCh;
+	}
+	TVector<r_4> reducedRow;
+	reducedRow = diffMeanOnOff.SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
+	double mean; 
+	double sigma;
+	MeanSigma(reducedRow,mean,sigma);
+	if (debuglev_>9) {
+	  cout << "mean/signa " << mean << "/" << sigma << endl;
+	}
+	reducedMeanDiffOnOffAll(iCh,iSlice) = mean;
+	reducedSigmaDiffOnOffAll(iCh,iSlice) = sigma;
+      }//loop on Channel
+    }//loop on Freq. slice 
+    tag = "redMeanONOFFRawAll";
+    fos << PPFNameTag(tag) << reducedMeanDiffOnOffAll;
+    tag = "redSigmaONOFFRawAll";
+    fos << PPFNameTag(tag) << reducedSigmaDiffOnOffAll;
+    //JEC 22/9/11 END
+
+
+
+    fos << PPFNameTag("specONOFFRaw2ChanMean") << meanOfChan;
+  }//end of save fits
+
+  
+  cout << "OK rawOnOff finished" <<endl;
+  return rc;
+} //ProcessONOFFRawData::processCmd
+
+//JEC 22/9/11 Make ON-OFF analysis WO any calibration END
+//----------------------------------------------
+int ProcessONOFFData::processCmd() throw(string) {
+  int rc = 0;
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+   if(debuglev_>0)cout << "Process Data" << endl;
+  vector<string> modeList;
+  modeList.push_back("On");
+  modeList.push_back("Off");
+  vector<string>::const_iterator iMode;
+  
+  uint_4 id; 
+  string tag;
+
+  //
+  //Process to get sucessively
+  //Raw Spectra, 
+  //BAO Calibrated Spectra 
+  //and RT Calibrated Spectra
+  //The pocesses are separated to allow intermediate save of results
+
+  map< pair<string, sa_size_t>, TMatrix<r_4> > spectreCollect;
+  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iSpectre, iSpectreEnd;
+  
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process RAW Mode " << mode << endl;
+
+    //------------------------------------------
+    //Produce Raw spectra per cycle
+    //------------------------------------------
+
+    string directoryName;
+    list<string> listOfSpecFiles;
+    list<string>::const_iterator iFile, iFileEnd;
+    
+        
+    //
+    //loop on cycles
+    //
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      //TOBE FIXED      directoryName = "./" + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode + "/";
+      directoryName = "./" + mode + "/";
+      stringstream sicycle;
+      sicycle << icycle;
+      directoryName += spectraDirectory_ + sicycle.str() + "/";
+
+      //read directory
+      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+      
+
+      //compute mean of spectra created in a cycle
+      if(debuglev_>0)cout << "compute mean for cycle " << icycle << endl;
+      TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+      iFileEnd = listOfSpecFiles.end();
+      r_4 nSpectres  = 0;
+      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+	FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+	TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+	aSpectrum >> spectre;
+	spectreMean += spectre;
+	nSpectres++;
+      }// end of for files 
+      if(nSpectres>0) spectreMean /= nSpectres;
+      
+      //save mean spectrum
+      spectreCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreMean,false) ));
+    }//end of for cycles
+  }//end loop on mode for raw preocess
+
+  if(debuglev_>1) {//save mean spectra on file 
+    cout << "Save mean raw spectra" << endl;
+    string fileName;
+    //TOBE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataRaw" + ".ppf";
+    fileName = "./dataRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+
+    POutPersist fos(fileName);
+    id=0;
+    iSpectreEnd = spectreCollect.end();
+    for (iSpectre = spectreCollect.begin();
+	 iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+      tag = "specRaw";
+
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
+//       stringstream sid;
+//       sid << id;
+//       tag += sid.str();
+      tag += (iSpectre->first).first;
+      stringstream sid;
+      sid << (iSpectre->first).second;
+      tag += sid.str();
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
+
+      fos << PPFNameTag(tag) << iSpectre->second;
+    }
+  }//end of save fits
+  
+
+
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process CALIB BAO Mode " << mode << endl;
+    //------------------------------------------
+    // Correct Raw spectra for BAO calibration
+    //------------------------------------------
+    //Read BAO calibration files
+    sa_size_t nr,nc; //values read 
+    
+    string calibFileName = inputPath_+ "/" 
+      + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) 
+      + "/calib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+      + mode + "_" + freqBAOCalibration_ + "MHz-All.txt";
+
+    if(debuglev_>0) cout << "Read file " << calibFileName << endl;
+    ifstream ifs(calibFileName.c_str());
+    if ( ! ifs.is_open() ) {
+      rc = 999;
+      throw calibFileName + " cannot be opened...";
+    }	
+    TVector<r_4> calibBAOfactors;
+    if(debuglev_>9) cout << "Debug 1" << endl;
+    calibBAOfactors.ReadASCII(ifs,nr,nc);
+    if(debuglev_>9){
+      cout << "Debug 2: (nr,nc): "<< nr << "," << nc << endl;
+      calibBAOfactors.Print(cout);
+    }
+
+//JEC 20/9/11 use mean calibration coeff upon all cycles END
+
+    //
+    //spectra corrected by BAO calibration factor 
+    //-----make it different on Channels and Cycles (1/06/2011) OBSOLETE
+    //use mean calibration coeff upon all cycles (20/6/11)
+    //warning cycles are numbered from 1,...,N
+    //
+    if(debuglev_>0)cout << "do calibration..." << endl;
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      TMatrix<r_4> specmtx(spectreCollect[make_pair(mode,icycle)],true); //share the memory 
+      
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
+	//JEC 20/9/11 use mean calibration coeff upon all cycles START
+
+	//	specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh,icycle-1);
+	specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh);
+	//JEC 20/9/11 use mean calibration coeff upon all cycles END
+      }
+    }
+  } //end loop mode for BAO calib
+
+  if(debuglev_>1){ //save mean spectra BAO calibrated on file
+    cout << "save calibrated BAO spectra" << endl;
+    string fileName;
+    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataBAOCalib" + ".ppf";
+    fileName = "./dataBAOCalib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+
+    POutPersist fos(fileName);
+    iSpectreEnd = spectreCollect.end();
+    id=0;
+    for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+
+      tag = "specBAOCalib";
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
+//       stringstream sid;
+//       sid << id;
+//       tag += sid.str();
+      tag += (iSpectre->first).first;
+      stringstream sid;
+      sid << (iSpectre->first).second;
+      tag += sid.str();
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
+
+      fos << PPFNameTag(tag) << iSpectre->second;
+    }
+  }//end of save fits
+
+  
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process CALIB RT Mode " << mode << endl;
+    //------------------------------------------
+    // Correct BAO calib spectra for RT calibration
+    //------------------------------------------
+    //Very Preliminary May-June 11
+    //coef RT @ 1346MHz Ouest - Est associees a Ch 0 et 1
+    r_4 calibRT[NUMBER_OF_CHANNELS] = {27.724, 22.543};
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      TMatrix<r_4> specmtx(spectreCollect[make_pair(mode,icycle)],true); //share the memory    
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
+	specmtx( Range(iCh), Range::all() ) *= calibRT[iCh];
+      }
+    }
+  }//end loop on mode RT calib
+
+  {//save mean spectra BAO & RT calibrated on file
+    if(debuglev_>0)cout << "save calibrated BAO & RT spectra" << endl;
+    string fileName;
+    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataBAORTCalib" + ".ppf";
+     fileName = "./dataBAORTCalib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";   
+    POutPersist fos(fileName);
+    iSpectreEnd = spectreCollect.end();
+    id = 0;
+    for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+      tag = "specBAORTCalib";
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
+//       stringstream sid;
+//       sid << id;
+//       tag += sid.str();
+      tag += (iSpectre->first).first;
+      stringstream sid;
+      sid << (iSpectre->first).second;
+      tag += sid.str();
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
+      fos << PPFNameTag(tag) << iSpectre->second;
+    }
+  }//end of save fits
+
+  //------------------------------------------
+  // Perform ON-OFF
+  //------------------------------------------
+  
+  map< sa_size_t, TMatrix<r_4> > diffCollect;
+  map< sa_size_t, TMatrix<r_4> >::iterator iDiff, iDiffEnd;
+
+  TMatrix<r_4> diffMeanOnOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //init zero
+  r_4 nCycles = 0;
+  for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+    nCycles++;
+    TMatrix<r_4> specmtxOn(spectreCollect[make_pair(modeList[0],icycle)],false); //clone the memory 
+    TMatrix<r_4> specmtxOff(spectreCollect[make_pair(modeList[1],icycle)],false); //clone the memory 
+    TMatrix<r_4> diffOnOff = specmtxOn - specmtxOff;
+    diffCollect.insert(pair< sa_size_t,TMatrix<r_4> >(icycle,TMatrix<r_4>(diffOnOff,false) ));
+    diffMeanOnOff += diffOnOff;
+  }//end loop on cycle
+  if(nCycles>0) diffMeanOnOff/=nCycles;
+
+  //exctract channels and do the mean
+  TVector<r_4> meanOfChan(NUMBER_OF_FREQ); //implicitly init to 0
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
+    meanOfChan += diffMeanOnOff.Row(iCh).Transpose();
+  }
+  meanOfChan /= (r_4)NUMBER_OF_CHANNELS;
+  
+
+
+  {//save diff ON-OFF BAO & RT calibrated
+    if(debuglev_>0)cout << "save ON-OFF spectra" << endl;
+    string fileName;
+    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "diffOnOff" + ".ppf";
+    fileName = "./diffOnOff_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist fos(fileName);
+    iDiffEnd = diffCollect.end();
+    id = 0;
+    for (iDiff = diffCollect.begin();iDiff != iDiffEnd ; ++iDiff, id++) {
+      tag = "specONOFF";
+      stringstream sid;
+      //JEC 20/9/11      sid << id;
+      sid << iDiff->first;
+      tag += sid.str();
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      fos << PPFNameTag(tag) << iDiff->second;
+    }
+    //save the mean also
+    fos << PPFNameTag("specONOFFMean") << diffMeanOnOff;
+    fos << PPFNameTag("specONOFF2ChanMean") << meanOfChan;
+  }//end of save fits
+
+  cout << "OK dataOnOff finished";
+
+  return rc;
+} //ProcessONOFFData::processCmd
+//
+//----------------------------------------------
+//
+int ProcessGain::processCmd() throw(string) {
+  int rc = 0;
+  string msg = "";
+
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+  if(debuglev_>0)cout << "Process Gain" << endl;
+  
+  string directoryName;
+  //TOBE FIXED  directoryName = "./" + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode_ + "/";
+  //JEC 6/09/2011 numcycle_ repalced by ifirstCycle_ according to definition of numcycle_ and the fact that for GAIN 1 cycle is involved
+  stringstream thegaincycle;
+  thegaincycle << ifirstCycle_;
+  directoryName = inputPath_ + "/" 
+    + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/" 
+    + mode_ + "/" + spectraDirectory_ + thegaincycle.str()  + "/";
+  
+  list<string> listOfSpecFiles;
+  list<string>::const_iterator iFile, iFileEnd;
+  //read directory
+
+  listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+  
+  //Mean power computed over the N channels to select the spectra for gain computation 
+  //The threshold is computed "on-line" as  1%  of the difference (max power -min power) over the
+  // the min power.
+  //A possible alternative is to set an absolute value...
+  if(debuglev_>0)cout << "compute mean poser spectra for files in " << directoryName << endl;
+  iFileEnd = listOfSpecFiles.end();
+  map<string, r_4> meanpowerCollect;
+  //JEC 21/9/11 add meanpower for each Channels START
+  map<string, r_4> meanPowerPerChanCollect;
+  //JEC 21/9/11 add meanpower for each Channels END
+
+  map<string, r_4>::const_iterator iMeanPow, iMeanPowEnd;
+
+  for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+    FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+    TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+    aSpectrum >> spectre;
+    meanpowerCollect[*iFile] = spectre.Sum()/spectre.Size();
+    //JEC 21/9/11 add meanpower for each Channels START
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
+      TVector<r_4> specChan(NUMBER_OF_FREQ);
+      specChan = spectre.Row(iCh).Transpose();
+      stringstream tmp;
+      tmp << iCh;
+      string tag = *iFile + "_" + tmp.str();
+      meanPowerPerChanCollect[tag] = specChan.Sum()/specChan.Size();
+    }
+    //JEC 21/9/11 add meanpower for each Channels END
+  }//end of for files
+  pair<string, r_4> minelement = *min_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
+  pair<string, r_4> maxelement = *max_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
+  if(debuglev_>1){
+    cout << "meanpowerCollect has " << meanpowerCollect.size() << " spectra registered" << endl;
+    cout << "find min mean power "<<minelement.second << " for " << minelement.first << endl;
+    cout << "find max mean power "<<maxelement.second << " for " << maxelement.first << endl;
+  }
+  r_4 threshold = minelement.second + 0.01*(maxelement.second-minelement.second);
+  if(debuglev_>1){
+    cout << "threshold found at " << threshold <<endl;
+  }  
+
+  TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+  r_4 nSpectres  = 0;
+  iMeanPowEnd = meanpowerCollect.end();
+  for (iMeanPow = meanpowerCollect.begin(); iMeanPow != iMeanPowEnd; ++iMeanPow) {
+    if ( iMeanPow->second <= threshold ) {
+      //TODO avoid the reloading of the file may speed up
+      FitsInOutFile aSpectrum(iMeanPow->first,FitsInOutFile::Fits_RO);
+      TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+      aSpectrum >> spectre;
+      spectreMean += spectre;
+      nSpectres++;
+    }
+  }
+  if(debuglev_>1){
+    cout << "Number of files selected for gain " << nSpectres <<endl;
+  }    
+  if(nSpectres>0) {
+    spectreMean /= nSpectres;
+  } else {
+    stringstream tmp;
+    tmp << threshold;
+    msg = "Gain: cannot find a spectra above threshold value =" + tmp.str() + " ... FATAL";
+    throw msg;
+  }
+
+  //Save gain spectra
+  {
+    //use ! to override the file: special features of cfitsio library
+    string fileName;
+    //TOBE FIXED   fileName = "!./" + sourceName_ + "/gain_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".fits";
+    fileName = "!"+ outputPath_ + "/gain_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".fits";
+    if(debuglev_>1){
+      cout << "save gains in " << fileName << endl;
+    }
+    FitsInOutFile fos(fileName, FitsInOutFile::Fits_Create);
+    fos << spectreMean;
+  }
+  //save mean power values
+  {
+    vector<r_4> tmp;
+    //JEC 21/9/11 add meanpower for each Channels START
+    vector<r_4> tmpCh0; //for Chan 0
+    vector<r_4> tmpCh1; //for Chan 1
+    //JEC 21/9/11 add meanpower for each Channels END
+    for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+      if (debuglev_>9) {
+	cout << "Gain: save mean power of file: " << *iFile << endl;
+      }
+      tmp.push_back(meanpowerCollect[*iFile]);
+      //JEC 21/9/11 add meanpower for each Channels START  
+      stringstream tmp0;
+      tmp0 << (sa_size_t)0;
+      string tag0 = *iFile + "_" + tmp0.str();
+      tmpCh0.push_back(meanPowerPerChanCollect[tag0]);
+      if (NUMBER_OF_CHANNELS>1){
+	stringstream tmp1;
+	tmp1 << (sa_size_t)1;
+	string tag1 = *iFile + "_" + tmp1.str();
+	tmpCh1.push_back(meanPowerPerChanCollect[tag1]);
+      }
+      //JEC 21/9/11 add meanpower for each Channels END
+    }
+    string fileName;
+    //TOBE FIXED    fileName = "./" + sourceName_ + "/gain_monitor_" + dateOfRun_ 
+    fileName = outputPath_ + "/gain_monitor_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist fos(fileName); 
+    TVector<r_4> tvtmp(tmp);
+    fos.PutObject(tvtmp,"gainmoni"); //Attention initialement le tag etait "monitor"...
+    //JEC 21/9/11 add meanpower for each Channels START  
+    TVector<r_4> tvtmp0(tmpCh0);
+    fos.PutObject(tvtmp0,"gainmoni0");
+    if (NUMBER_OF_CHANNELS>1){
+      TVector<r_4> tvtmp1(tmpCh1);
+      fos.PutObject(tvtmp1,"gainmoni1");
+    }
+    //JEC 21/9/11 add meanpower for each Channels END
+  }
+  
+  cout << "OK gain finished" <<endl;
+   return rc;
+}//ProcessGain::processCmd
+//
+//----------------------------------------------
+//
+int ProcessCalibration::processCmd() throw(string) {
+  int rc = 0;
+  string msg = "";
+
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+  if(debuglev_>0)cout << "Process Calibration with option "<< option_ << endl;
+  
+  vector<string> modeList;
+  modeList.push_back("On");
+  modeList.push_back("Off");
+
+  r_8 t0absolute = -1;  //TIMEWIN of the first spectra used
+  r_8 timeTag0   = -1;   //MEANTT, mean TIME TAG of the first paquet window  
+  bool first = true;     // for initialisation
+  
+  // Power Tuple
+  // mode, cycle, time, {Ch0, ... ,ChN} mean poser in the range [f0-Bw/2, f0+Bw/2] 
+  vector<string> varPowerTupleName; //ntuple variable naming
+  varPowerTupleName.push_back("mode");
+  varPowerTupleName.push_back("cycle");  
+  varPowerTupleName.push_back("time");
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
+    stringstream tmp;
+    tmp << iCh;
+    varPowerTupleName.push_back("Ch"+tmp.str());
+  }
+  r_4 valPowerTuple[varPowerTupleName.size()];
+  NTuple powerEvolution(varPowerTupleName); 
+  
+  
+  //-----------------
+  //Start real process
+  //------------------
+  for (size_t iMode = 0; iMode < modeList.size(); ++iMode) {
+    string mode = modeList[iMode];
+    if(debuglev_>0)cout << "Process Calibration for Mode " << mode << endl;
+
+    //initialize the start of each calibration procedure given by the RT SCA file
+    //see ProcessBase::processCmd for the structure of the sca file
+    string scaStartCalibName = "stcal"+mode; 
+    sa_size_t idStartCalib   = scaTuple_->ColumnIndex(scaStartCalibName);
+
+    string directoryName;
+    list<string> listOfSpecFiles;
+    list<string>::const_iterator iFile, iFileEnd;            
+    //
+    //loop on cycles
+    //
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+
+      directoryName = inputPath_ + "/" 
+	+ sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode + "/";
+      stringstream sicycle;
+      sicycle << icycle;
+      directoryName += spectraDirectory_ + sicycle.str() + "/";
+      
+      //read directory
+      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+
+      iFileEnd = listOfSpecFiles.end();
+      DVList header;
+      TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+
+      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+	FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+	aSpectrum.GetHeaderRecords(header);
+	aSpectrum >> spectre;
+
+	if(first){//initialise the timer
+	  first = false;
+	  t0absolute = header.GetD("TIMEWIN")/1000.;
+	  timeTag0 = header.GetD("MEANTT");
+	  if (debuglev_>1){
+	    cout << "debug Header of " << *iFile << endl;
+	    cout << "TIMEWIN = " << setprecision(12) << t0absolute << " "
+		 << "MEANTT = " << setprecision(12) << timeTag0 << endl;
+	  }
+	}//end init timer
+	
+	//Set time of current file
+	//Add to the non-precise absolute origin an precise increment
+	r_4 timeTag = t0absolute + (header.GetD("MEANTT") - timeTag0);
+	int index=0;
+	valPowerTuple[index++] = iMode;
+	valPowerTuple[index++] = icycle;
+	valPowerTuple[index++] = timeTag-scaTuple_->GetCell(idCycleInTuple_[icycle],idStartCalib); //take the RT time start as refernce 
+
+	//Compute the mean power of the two channel (separatly) in the range [f-bw/2, f+bw/2]
+	for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
+	  TMatrix<r_4> tmp(spectre(Range(iCh),Range(lowerFreqBin_,upperFreqBin_)),false);
+	  r_4 mean = tmp.Sum()/(r_4)tmp.Size();
+	  valPowerTuple[index++] = mean;
+	}//end of channel loop
+       
+	//Fill Tuple
+	powerEvolution.Fill(valPowerTuple);
+      }//end of files loop
+    }//end of cycles loop
+  }//end of mode loop
+
+  //store power evolution Tuple
+  if(debuglev_>0){
+    cout << "ProcessCalibration::processCmd: dump powerEvolution tuple" << endl;
+    powerEvolution.Show(cout);
+  }
+  //
+  //Compute Calibration Coefficients as function of mode/cycle/channels
+  //
+
+  //Tsys,Incremant Intensity... Tuple
+  //mode mode cycle [(tsys0,dint0),...,(tsysN,dintN)]
+  vector<string> varCalibTupleName;
+  varCalibTupleName.push_back("mode");
+  varCalibTupleName.push_back("cycle");
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
+    stringstream tmp;
+    tmp << iCh;
+    varCalibTupleName.push_back("tsys"+tmp.str());
+    varCalibTupleName.push_back("dint"+tmp.str());
+  }
+  r_4 valCalibTuple[varCalibTupleName.size()];
+  NTuple calibEvolution(varCalibTupleName);
+
+  // select time E [twin0,twin1] U [twin2,twin3] for Tsys
+  // time unit = second 
+  const r_4 twin0 = -3.;
+  const r_4 twin1 = -1.;
+  const r_4 twin2 =  6.;
+  const r_4 twin3 =  8;
+  const r_4 thresholdFactor = 0.80; //80% of the diff. Max-Min intensity
+
+  sa_size_t inModeIdx = powerEvolution.ColumnIndex("mode");
+  sa_size_t inCycleIdx= powerEvolution.ColumnIndex("cycle");
+  sa_size_t inTimeIdx = powerEvolution.ColumnIndex("time");
+  sa_size_t inOffsetCh0 = powerEvolution.ColumnIndex("Ch0"); //nb Ch0 position in the powerEvolution tuple  
+  if(debuglev_>1) cout << "DEBUG: in Idx: (" 
+		       << inModeIdx << ", "
+		       << inCycleIdx << ", "
+		       << inTimeIdx << ", "
+		       << inOffsetCh0 << ")"
+		       << endl;
+
+ 
+  size_t outModeIdx = calibEvolution.ColumnIndex("mode");
+  size_t outCycleIdx= calibEvolution.ColumnIndex("cycle");
+  size_t outOffsetCh0 = calibEvolution.ColumnIndex("tsys0"); //nb Ch0 position in the monitor tuple  
+  size_t outNDataPerCh= 2;
+  if(debuglev_>1)  cout << "DEBUG: out Idx: (" 
+			<< outModeIdx << ", "
+			<< outCycleIdx << ", "
+			<< outOffsetCh0 << ")"
+			<< endl;
+
+  sa_size_t nPowerEvolEntry = powerEvolution.NEntry();
+  double minMode;
+  double maxMode;
+  powerEvolution.GetMinMax("mode",minMode,maxMode);
+  double minCycleNum;
+  double maxCycleNum;
+  powerEvolution.GetMinMax("cycle",minCycleNum,maxCycleNum);
+  if(debuglev_>1)  cout << "DEBUG mode ("<<minMode<<","<<maxMode<<")\n"
+			<< "cycle ("<<minCycleNum<<","<<maxCycleNum<<")"
+			<< endl;
+
+  r_4* aPowerEvolEntry; // a ligne of the powerEvolution tuple
+//   r_4* aPowerEvolEntry = new r_4[powerEvolution.NbColumns()]; // a ligne of the powerEvolution tuple
+
+  r_4 minMean;
+  r_4 maxMean;
+
+  for (size_t iMode = (size_t)minMode; iMode <= (size_t)maxMode; iMode++){
+    for (size_t iCycle = (size_t)minCycleNum; iCycle <= (size_t)maxCycleNum; iCycle++ ){
+      if(debuglev_>1) cout<<"DEBUG >>>>>>> mode/cycle: " << iMode << "/" << iCycle << endl;
+ 
+      valCalibTuple[outModeIdx]=iMode;
+      valCalibTuple[outCycleIdx]=iCycle;
+      //
+      //Compute the Min && Max value of each Ch<i> data
+      if(debuglev_>1) cout<<"DEBUG compute Min and Max for each channels" << endl;
+      //
+      TVector<r_4> chMean[NUMBER_OF_CHANNELS];
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
+	chMean[iCh].SetSize(nPowerEvolEntry);
+      }
+      for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
+	aPowerEvolEntry = powerEvolution.GetVec(ie);
+	if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
+	  for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
+	    chMean[iCh](ie) = aPowerEvolEntry[iCh+inOffsetCh0];
+	  }//end cut
+	}
+      }//eo loop on tuple entries
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
+	//
+	//select values to compute background Tsys
+	if(debuglev_>1) {
+	  cout<< "DEBUG >>>> Ch[" << iCh << "]" << endl;
+	  cout<< "DEBUG select values to compute background Tsys " << endl;
+	}
+	//
+       
+	std::vector<r_4> lowerInt;
+	for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
+	  aPowerEvolEntry = powerEvolution.GetVec(ie);
+	  if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
+	    r_4 time = aPowerEvolEntry[inTimeIdx];
+	    if ( (time >= twin0 && time <= twin1) ||
+		 (time >= twin2 && time <= twin3)
+		 ) lowerInt.push_back(aPowerEvolEntry[iCh+inOffsetCh0]);
+	  }//end cut
+	} //end loop entries
+	//
+	//compute the Tsys
+	if(debuglev_>1) cout <<"DEBUG compute Tsys" << endl;
+	//
+	std::nth_element(lowerInt.begin(),
+			 lowerInt.begin()+lowerInt.size()/2,
+			 lowerInt.end());
+	r_4 tsys = *(lowerInt.begin()+lowerInt.size()/2);
+	if(debuglev_>1) cout << "DEBUG tsys["<<iCh<<"] : " << tsys <<endl;
+	//
+	//set the threshold for DAB detection
+	//
+	chMean[iCh].MinMax(minMean,maxMean);
+	minMean = (tsys > minMean) ? tsys : minMean; //pb of empty vector
+	if(debuglev_>1) cout << "DEBUG min["<<iCh<<"] : " << minMean
+			     << " max["<<iCh<<"] : " << maxMean
+			     <<endl;
+	r_4 deltathres = thresholdFactor * (maxMean-minMean);
+	r_4 thres =  tsys + deltathres;
+	if(debuglev_>1) cout << "DEBUG thres["<<iCh<<"] : " << thres <<endl;
+	//
+	//collect upper part of the DAB
+	if(debuglev_>1) cout <<"DEBUG collect upper part of the DAB" << endl;
+	//
+	std::vector<r_4> upperInt;
+	for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
+	  aPowerEvolEntry = powerEvolution.GetVec(ie);
+	  if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
+	    r_4 mean = aPowerEvolEntry[iCh+inOffsetCh0];
+	    if (mean >= thres) upperInt.push_back(mean);
+	  }//end cut
+	}//eo loop on channels
+	//
+	//compute adjacent differences to detect the 2 DAB levels
+	//
+	if(debuglev_>1) cout << "(DEBUG )size upper [" << iCh << "]: " << upperInt.size() <<endl;
+	std::vector<r_4> upperIntAdjDiff(upperInt.size());
+	std::adjacent_difference(upperInt.begin(),
+				 upperInt.end(),
+				 upperIntAdjDiff.begin());
+	//
+	//Search the 2 DAB states
+	if(debuglev_>1) cout<<"DEBUG Search the 2 DAB states" << endl;
+	//
+	std::vector<r_4> upIntState[2];
+	int state=-1;
+	for (size_t i=1;i<upperInt.size();i++) {//skip first value
+	  if (fabs(upperIntAdjDiff[i])<upperInt[i]*0.010) {
+	    if(state == -1) state=0;
+	    if(state == -2) state=1;
+	    upIntState[state].push_back(upperInt[i]);
+	  } else {
+	    if (state == 0) state=-2;
+	  }
+	}
+	//
+	//Take the mean of the median values of each levels
+	if(debuglev_>1)cout << "DEBUG mean of the median values of each levels" << endl;       
+	//
+	r_4 meanUpper = 0;
+	r_4 nval = 0;
+	for (int i=0;i<2;i++) {
+	  if (!upIntState[i].empty()) {
+	    std::nth_element(upIntState[i].begin(),
+			     upIntState[i].begin()+upIntState[i].size()/2,
+			     upIntState[i].end());
+	    meanUpper += *(upIntState[i].begin()+upIntState[i].size()/2);
+	    nval++;
+	  } 
+	}
+	meanUpper /= nval;
+	//
+	//Finaly the increase of power due to the DAB is:
+	//
+	r_4 deltaInt = meanUpper - tsys;
+	if(debuglev_>1) cout << "DEBUG deltaInt["<<iCh<<"] : " << deltaInt <<endl;
+	//
+	//Save for monitoring and final calibration operations
+	//
+	valCalibTuple[outOffsetCh0+outNDataPerCh*iCh]   = tsys;
+	valCalibTuple[outOffsetCh0+outNDataPerCh*iCh+1] = deltaInt;
+      }//end loop on channels
+      if(debuglev_>1) cout<<"DEBUG Fill the calibEvolution tuple" << endl;
+      calibEvolution.Fill(valCalibTuple);
+    }//eo loop on Cycles
+  }//eo loop on Modes
+  //
+  //store calibration evolution Tuple
+  //
+  if(debuglev_>0){
+    cout << "ProcessCalibration::processCmd: dump calibEvolution tuple" << endl;
+    calibEvolution.Show(cout);
+  }
+  //
+  //Compute the means per channel of the calibration coefficients (deltaInt)
+  //and save cycle based Calibration Ctes in file named as 
+  //    <source>-<date>-<mode>-<fcalib>MHz-Ch<channel>Cycles.txt
+  //   format <cycle> <coef> 
+  //the means are stored in files     
+  //    <source>-<date>-<mode>-<fcalib>MHz-All.txt
+  //   format <channel> <coef>
+  //
+  sa_size_t nModes = (sa_size_t)(maxMode - minMode) + 1;
+  string calibCoefFileName;
+  ofstream  calibMeanCoefFile[nModes]; //Mean over cycles
+  ofstream  calibCoefFile[nModes][NUMBER_OF_CHANNELS]; //cycle per cycle
+  for (sa_size_t iMode=0; iMode<nModes; iMode++){
+    //The file for the Means Coeff.
+    calibCoefFileName = outputPath_ + "/calib_" 
+      + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+      + modeList[iMode] + "_"
+      + freqBAOCalibration_ + "MHz-All.txt";
+    calibMeanCoefFile[iMode].open(calibCoefFileName.c_str());
+    //The file for the cycle per cycle Coeff.
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      stringstream chString;
+      chString << iCh;
+      calibCoefFileName = outputPath_ + "/calib_" 
+	+ dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+	+ modeList[iMode] + "_"
+	+ freqBAOCalibration_ + "MHz-Ch" + chString.str() + "Cycles.txt";
+      calibCoefFile[iMode][iCh].open(calibCoefFileName.c_str());
+    }
+  }
+
+  r_4* aCalibEvolEntry;
+  sa_size_t nCalibEvolEntry = calibEvolution.NEntry();
+
+  TMatrix<r_4> meanCalibCoef(nModes,NUMBER_OF_CHANNELS); //by default init to 0
+  TMatrix<r_4> nData4Mean(nModes,NUMBER_OF_CHANNELS);
+
+  //READ the calibration tuple, fill the array for mean and write to ascii file
+  for (sa_size_t ie=0; ie<nCalibEvolEntry; ie++){
+    aCalibEvolEntry = calibEvolution.GetVec(ie);
+    if(debuglev_>1){
+      cout << "DEBUG mode/cycle/Coef " 
+	   << aCalibEvolEntry[outModeIdx] << " "
+	   << aCalibEvolEntry[outCycleIdx] << " ";
+    }
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      if(debuglev_>1) cout << aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1] << " "; // for debug
+      sa_size_t currentMode = (sa_size_t)(aCalibEvolEntry[outModeIdx] - minMode); //ok even if minMode <> 0
+      nData4Mean(currentMode,iCh)++;
+      meanCalibCoef(currentMode,iCh) += aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1];
+
+      calibCoefFile[currentMode][iCh] << aCalibEvolEntry[outCycleIdx] << " "
+				      << setprecision(12)
+				      << aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1]
+				      << endl;
+    }
+    if(debuglev_>1) cout << endl; //for debug
+  }
+  
+  //Perform means: true means that div per 0 is treated (slower but safer) 
+  meanCalibCoef.Div(nData4Mean,true);
+  
+  if(debuglev_>0){
+    cout << "DEBUG nData4Mean" << endl;
+    nData4Mean.Print(cout);
+    cout << "DEBUG meanCalibCoef (averaged)" << endl;
+    meanCalibCoef.Print(cout);
+  }
+
+  //Save means Coef
+  for (sa_size_t iMode=0; iMode<nModes; iMode++){
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      calibMeanCoefFile[iMode] << setprecision(12)
+			       << meanCalibCoef(iMode,iCh)
+			       << endl;
+    }
+  }
+
+  //Save Monitor File
+  {
+    string fileName = outputPath_ + "/calib_monitor_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist calibMonitorFile(fileName);
+    calibMonitorFile << PPFNameTag("powermoni") <<  powerEvolution;
+    calibMonitorFile << PPFNameTag("calibmoni") <<  calibEvolution;
+  }
+
+  //Clean & Return
+  for (sa_size_t iMode=0; iMode<nModes; iMode++){
+    calibMeanCoefFile[iMode].close();
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      calibCoefFile[iMode][iCh].close();
+    }
+  }
+
+  cout << "Ok calibration finished" << endl;  
+  return rc;
+}//ProcessCalibration::processCmd
+
Index: /BAORadio/AmasNancay/v2/analysis-scriptsAndprg-description.txt
===================================================================
--- /BAORadio/AmasNancay/v2/analysis-scriptsAndprg-description.txt	(revision 538)
+++ /BAORadio/AmasNancay/v2/analysis-scriptsAndprg-description.txt	(revision 538)
@@ -0,0 +1,193 @@
+Title: Status of the programs and scripts used for Amas@Nancay analysis at CCIN2P3
+Revision: 14/9/11
+Author: J.E Campagne 22/7/11
+----------------------------
+
+Intro:
+All these tools are located for the while in J.E Campagne private directory, so they are not
+yet available to public usage and this note is essentially for information and software tracking.
+
+-----------------------------------------------------------
+Analysis program
+------------------------------------------------------------
+o analysis.cc
+	- perfom
+		. gain spectra determination + monitoring
+		. calibration coefficient determination + monitoring
+		. ON-OFF spectra determination (no RT calibration included)
+	- Todo
+		. include RT calibration
+	- comments
+		. ON-OFF part only validate for a single Abell85 run and not in batch mode (preliminary stage)
+		. GAIN part should be revalidated according to inPath argument
+		
+-----------------------------------------------------------
+Collection of  Bourne SHELL script for analysis
+------------------------------------------------------------
+
+A) Utilitary scripts to follow the DAQ & Analysis progress
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+o statusdaq.sh
+--------------
+	- perform
+		. scan all the /baoradio/data/AmamNancay to look at the directories and and make
+			a summary of the Data Acquisition
+
+o statusheader.sh
+-----------------
+	- perform
+		. scan Irods directories to look at header fits files extraction progress 
+		
+o statusgain.sh
+---------------
+	- perform
+		. scan Irods directories to look at FINAL gain_* files production progress 
+
+o statuscalib.sh
+----------------
+	- perform
+		. scan Irods directories to look at INTERMEDIATE calibcycle files production progress
+	- Todo
+		. scan also for FINAL calib_* files progress
+
+B) Utilitary scripts to send to BQS system high level task
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++		
+o submit2bqs-hdrfitsextractor.sh
+--------------------------------
+        - SHOULD BE DONE BEFORE gain/calib/analysis
+	- send the hdrfitsextractor.sh script to BQS system	
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+o submit2bqs-scaextractor.sh
+--------------------------------
+        - SHOULD BE DONE BEFORE gain/calib/analysis
+	- send statusdaq if necessary
+	- send the scaextractor.sh script to BQS system
+		although this script is called by getscafiles lower level script
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+o submit2qs-gainmaker.sh
+-------------------------
+	- send the gainmaker.sh script to BQS system 
+	- Todo list
+		- TO MIGRATE TO NEW BATCH
+	
+o submit2qs-calibmaker.sh
+--------------------------
+	- send the calibmaker.sh script to BQS system
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+o submit2qs-onoffmaker.sh
+--------------------------
+	- send the onoffmaker.sh script to BQS system
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+		
+C) High level scripts 
+++++++++++++++++++++++		
+o hdrfitsextractor.sh
+---------------------
+	- SHOULD BE DONE BEFORE ANYTHING
+	- perform
+		. extraction of the header part of the signalXYZ.fits files and
+		. put the results into Irods	
+	- use 
+		. statusdaq.sh	
+
+o gainmaker.sh
+---------------
+	- perform 
+		1) the extraction of signalXYZ.fits files 
+			concerned by the OFF-calibration time window
+			of the cycle located at the middle of the run
+		2) . the analysis of the above fits files to determine the 
+			gains of each channel: produce gain_* files (monitoring + gain files)
+			. put the results into Irods
+	- use 
+		- statusdaq.sh, proc_script.sh, getscafiles.sh
+		- analysis.cc	
+	
+o calibmaker.sh
+---------------
+	- perfom
+		1) several x-checks to see at which stage of the analysis process we are
+		2) trig the proc_script.sh script if necessary to create the spectra fits files via specmfib.cc prg 
+		3) . prepare the analysis of the above fits files
+		   . put calib_* files (monitoring +  calibration files) into Irods
+	- use 
+		- statusdaq.sh, proc_script.sh, getscafiles.sh
+		- analysis.cc	
+
+o onoffmaker.sh (preliminary)
+----------------------------
+	- perform
+		1) several x-checks to see at which stage of the analysis process we are
+		2) trig the proc_script.sh script if necessary to create the spectra fits files via specmfib.cc prg 
+		3) prepare the analysis of the above fits files
+		4) put dataRaw_*        (raw spectra), 
+		       dataBAOCalib_*   (after BAO calibration), 
+		       dataBAORTCalib_* (after BAO & RT calibration), 
+		       diffOnOff_*      (ON-OFF spectra) 
+
+D) Lower level scripts
+++++++++++++++++++++++
+		
+o proc_script.sh
+-----------------
+	- VERY BUSY script
+	- perform 
+		1) all the preparation work to send in batch the "specmfib" program 
+			which perform the FFT and median/mean spectra for GAIN, CALIBRATION, ON-OFF task
+		2) the script that is sent in batch put the results into Irods	
+	- use
+		- getscafiles.sh, select.sh, getsignalfiles.sh
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+		
+
+o scaextractor.sh
+-----------------
+	- perform
+		1) extraction of the scaXYZ file
+		2) if the scaXYZ.sum and the scaXYZ.sum.trans files do not exist
+			. create and put them into Irods
+			. scaXYZ.sum = summary of the SCA file
+			. scaXYZ.sum.trans = transformation of the date format of the scaXYZ.sum file	
+	- use
+		- sca.sh, transdate.sh
+		
+o sca.sh
+--------
+	- perform
+		. produce the scaXYZ.sum (= summary of the SCA file) from a scaXYZ file
+		
+o transdate.sh
+--------------
+	- perform
+		. produce the scaXYZ.sum.trans (= transformation of the date format) from the scaXYZ.sum file	
+
+o select.sh
+-----------
+	-perform 
+		. the selection of the signalXYZ.fits files according to the type of task requested
+			as ON, OFF, CALIBON, CALIBOFF, GAIN
+		
+o getscafiles.sh
+----------------
+	- perform
+		. download from Irods the scaXYZ, scaXYZ.sum and scaXYZ.sum.trans files	
+	- use
+		scaextractor.sh
+		
+o getsignals.sh
+---------------
+	- perform
+		. download from Irods the signalXYZ.fits files
+		
+		
+		
+
Index: /BAORadio/AmasNancay/v2/anarawonoffmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/anarawonoffmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/anarawonoffmaker.sh	(revision 538)
@@ -0,0 +1,349 @@
+#!/bin/sh -xvf
+#Perform the ON-OFF analysis
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+#which source to analyse
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+  do
+  case "$1" in
+      -src)  sourceRadio=$2;    shift;;
+-date) dateSelected=$2;     shift;;
+-sim)  simulationMode=On;;
+-h)
+echo >&2 \
+    "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
+exit 1;;
+*)  break;;	# terminate while loop
+esac
+shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+#wakeup the NFS disk
+$LS -l ${tmppublicpath} > /dev/null
+#clean previous spurious files
+$RM -f ${tmppublicpath}/*.finished
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+
+
+$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
+tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+IFS='
+' 
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+#
+# start a "fresh" session  
+  cd ${topDir}
+  $LS | $XARGS -i $RM -rf {}
+  
+  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+
+  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
+  $LS -lrt
+
+
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the gain & calibration files exist
+#
+  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
+  if [ ! ${gainDone} -gt 0 ]; then
+      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
+      continue
+  fi
+
+######
+  $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
+#go to the .../source/date-sourcelowercase directory
+#download Off/datacycle<> and On/datacycle<> directories
+  $MKDIR -p ${topDir}/${dateDAQ}${srclower}
+  cd ${topDir}/${dateDAQ}${srclower}
+  aboveOnOffDir=`pwd`
+  $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
+#
+  mode="Off"
+  $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
+  inFileDirectory="./${mode}"
+  $MKDIR -p ${inFileDirectory}
+  listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
+  IFS='
+'
+  listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+  nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
+
+  if [ ${nCyclesOff} -eq -1 ]; then
+      continue
+  fi
+
+  lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+  listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
+
+  
+  for j in ${listOfDataCycle[@]}
+    do
+    irodsDownDir="${j}"
+    OUT=./getInFits.$$
+    iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+    igetStatus=`$GREP "^ERROR" ${OUT}`
+    if [ "<$igetStatus>" != "<>" ]; then
+	$ECHO "FATAL (${scriptName}): error while iget fits files:"
+	$ECHO $igetStatus
+	$ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	continue
+    fi
+    $RM -f ${OUT}
+  done
+  $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
+#
+  mode="On"
+  $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
+  inFileDirectory="./${mode}"
+  $MKDIR -p ${inFileDirectory}
+
+  listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
+  IFS='
+'
+  listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+  nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
+
+  if [ ${nCyclesOn} -eq -1 ]; then
+      continue
+  fi
+
+  lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+  listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
+
+#
+#Check if there is the same number of cycles in Off and On mode
+#      
+#first cycle = max(firstCycleOn, firstCycleOff)
+#last  cycle = min(lastCycleOn,lastCycleOff)
+
+  if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
+      firstCycle=${firstCycleOff}
+      lastCycle=${lastCycleOff}
+  else
+      $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
+      $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
+      firstCycle=${firstCycleOff}
+      if [ ${firstCycleOn} -gt ${firstCycle} ]; then
+	  firstCycle=${firstCycleOn}
+      fi
+      lastCycle=${lastCycleOff}
+      if [ ${lastCycleOn} -lt ${lastCycle} ]; then
+	  lastCycle=${lastCycleOn}
+      fi
+  fi
+  $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
+  
+
+  for j in ${listOfDataCycle[@]}
+    do
+    irodsDownDir="${j}"
+    OUT=./getInFits.$$
+    iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+    igetStatus=`$GREP "^ERROR" ${OUT}`
+    if [ "<$igetStatus>" != "<>" ]; then
+	$ECHO "FATAL (${scriptName}): error while iget fits files:"
+	$ECHO $igetStatus
+	$ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	continue
+    fi
+    $RM -f ${OUT}
+  done
+  $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
+#Get sca file
+  $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+  OUT1=./getScaStatus.$$
+  $RM -f ${tmppublicpath}/getscafiles.finished
+  ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+  while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+      date +%T
+      sleep 30
+  done
+  $RM ${tmppublicpath}/getscafiles.finished
+  $ECHO "DEBUG (${scriptName}): END"
+#
+  getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+#	
+  if [ "<${getScaStatus}>" != "<>" ]; then
+      $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+      $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+      $CAT ${OUT1}
+      $ECHO "DEBUG (${scriptName}): END"
+      continue
+  fi
+  $RM -f ${OUT1}
+
+  scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
+
+  
+#prepare job submission
+  cd ${aboveOnOffDir}
+#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
+
+  logFile="analyse_RAWDATA_${sourceRadio}_${dateDAQ}.log"
+  $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
+  if [ ${simulationMode} = "On" ]; then
+      $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act rawOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debug 1 >& ${logFile}"
+      cat > ${logFile} <<EOF
+      Ok rawOnOff finished
+EOF
+  else
+      ${scriptpath}/Objs/analyse -act rawOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debug 1 >& ${logFile}
+  fi
+  $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
+  rcstatus=`$GREP -i "Ok rawOnOff finished" ${logFile} | $WC -l`
+  if [ ${rcstatus} -eq 0 ]; then
+      $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
+      $ECHO "                      START logfile"
+      $CAT ${logFile}
+      $ECHO "                      END   logFile"
+#clean and leave
+      cd ${aboveOnOffDir}
+      $LS | $XARGS -i $RM -rf {}
+      continue
+  fi
+#Save into Irods
+  $ECHO "DEBUG (${scriptName}): START save output files"
+  $LS ${aboveOnOffDir}
+#use -f option for iput ONLY to force override
+  if [ ${simulationMode} = "On" ]; then
+      $ECHO "SIMUL: here we look for output files and put them in Irods"
+  else
+      $LS -lrth
+      $FIND . -name "dataRaw_*"      -print | $XARGS -i  iput -f -v -K {} ${i}
+      $FIND . -name "diffOnOffRaw_*" -print | $XARGS -i  iput -f -v -K {} ${i}
+  fi
+#save analysis logfile
+  $LS -l ${tmppublicpath} > /dev/null
+  $CP ${logFile} ${tmppublicpath}
+  $ECHO "DEBUG (${scriptName}): END save output files"
+#clean up to avoid scratch SIZE EXCEED LIMIT
+  cd ${aboveOnOffDir}
+  $LS | $XARGS -i $RM -rf {}
+
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/calibmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/calibmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/calibmaker.sh	(revision 538)
@@ -0,0 +1,422 @@
+#!/bin/sh -xvf
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+#which source to analyse
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+freqBAOCalib=1346
+bwBAOCalib=6.25
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+      -freq) freqBAOCalib=$2; shift;;
+      -bwd)  bwBAOCalib=$2; shift;;
+      -sim) simulationMode=On;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce [-freq freq in MHz (default ${freqBAOCalib})] [-bwd bandwidth in MHz (default ${bwBAOCalib})]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+#wakeup the NFS disk
+$LS -l ${tmppublicpath} > /dev/null
+#clean previous spurious files
+$RM -f ${tmppublicpath}/*.finished
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+
+#Nb: if we want to implement a loop on sources we have to take care of srclower...
+
+$ECHO "You have selected sourceRadio = $sourceRadio"
+tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+IFS='
+' 
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+#
+# start a "fresh" session  
+  cd ${topDir}
+  $LS | $XARGS -i $RM -rf {}
+  
+  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+
+  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
+  $LS -lrt
+
+
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the calibration files already exist
+#
+  alreadyDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
+  if [ ${alreadyDone} -gt 0 ]; then
+      $ECHO "INFO (${scriptName}): calibration coef files already exist"
+      continue
+  fi
+#
+  somethingTodo=1
+  input4calibOff=`ils ${i}/Off 2>&1 | $GREP -i "calibcycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n`  
+  input4calibOn=`ils ${i}/On 2>&1 | $GREP -i "calibcycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n`  
+#
+  if [ "<${input4calibOff}>" != "<>" -a "<${input4calibOn}>" != "<>"  ]; then
+#look at the content of the calibcycle directory as may be the previous batch job has crashed
+      tableauOff=( `$ECHO ${input4calibOff}` )
+      IFS='
+'
+      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOff=0
+      for j in ${tableauOff[@]}
+	do
+	nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
+      done
+#
+      tableauOn=( `$ECHO ${input4calibOn}` )
+      IFS='
+      '
+      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOn=0
+      for j in ${tableauOn[@]}
+	do
+	nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
+      done
+      diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
+      if [ ${diffOnOff} -lt 0 ]; then
+	  zero=0
+	  diffOnOff=`expr ${zero} - ${diffOnOff}` 
+      fi
+#allow up to 5 files differences
+      if [  ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0  ]; then
+	  somethingTodo=0
+	  $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
+      else
+	  $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
+      fi
+# 
+  fi 
+  if [ ${somethingTodo} -eq 1 ]; then
+#
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ CALIBOFF"
+      $RM -f ${tmppublicpath}/proc_script.finished
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBOFF"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBOFF
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ CALIBOFF"
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ CALIBON"
+      $RM ${tmppublicpath}/proc_script.finished
+      
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBON"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBON
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $RM ${tmppublicpath}/proc_script.finished
+#Todo see if there is errors in the log file before copy
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ CALIBON"
+######
+  else
+######
+      $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
+      #go to the .../source/date-sourcelowercase directory
+#download Off/calibcycle<> and On/calibcycle<> directories
+      $MKDIR -p ${topDir}/${dateDAQ}${srclower}
+      cd ${topDir}/${dateDAQ}${srclower}
+      aboveOnOffDir=`pwd`
+      $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
+#
+      mode="Off"
+      $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
+      inFileDirectory="./${mode}"
+      $MKDIR -p ${inFileDirectory}
+      listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+      IFS='
+'
+      listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+
+      firstCycleOff=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+      nCyclesOff=`expr ${#listOfCalibCycle[*]} - 1`
+
+      lastCycleOff=`$ECHO ${listOfCalibCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+      listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
+
+      
+      for j in ${listOfCalibCycle[@]}
+	do
+	irodsDownDir="${j}"
+	OUT=./getInFits.$$
+	iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget fits files:"
+	    $ECHO $igetStatus
+	    $ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	    continue
+	fi
+	$RM -f ${OUT}
+      done
+      $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
+#
+      mode="On"
+      $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
+      inFileDirectory="./${mode}"
+      $MKDIR -p ${inFileDirectory}
+
+      listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+      IFS='
+'
+      listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+
+      firstCycleOn=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+      nCyclesOn=`expr ${#listOfCalibCycle[*]} - 1`
+
+      lastCycleOn=`$ECHO ${listOfCalibCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+      listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
+
+
+#
+#Check if there is the same number of cycles in Off and On mode
+#      
+#first cycle = max(firstCycleOn, firstCycleOff)
+#last  cycle = min(lastCycleOn,lastCycleOff)
+
+      if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
+	  firstCycle=${firstCycleOff}
+	  lastCycle=${lastCycleOff}
+      else
+	  $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
+	  $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
+	  firstCycle=${firstCycleOff}
+	  if [ ${firstCycleOn} -gt ${firstCycle} ]; then
+	      firstCycle=${firstCycleOn}
+	  fi
+	  lastCycle=${lastCycleOff}
+	  if [ ${lastCycleOn} -lt ${lastCycle} ]; then
+	      lastCycle=${lastCycleOn}
+	  fi
+      fi
+      $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
+      
+
+      for j in ${listOfCalibCycle[@]}
+	do
+	irodsDownDir="${j}"
+	OUT=./getInFits.$$
+	iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget fits files:"
+	    $ECHO $igetStatus
+	    $ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	    continue
+	fi
+	$RM -f ${OUT}
+      done
+      $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
+#Get sca file
+      $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+      OUT1=./getScaStatus.$$
+      $RM -f ${tmppublicpath}/getscafiles.finished
+      ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+      while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+	  $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+	  date +%T
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/getscafiles.finished
+      $ECHO "DEBUG (${scriptName}): END"
+#
+      getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+#	
+      if [ "<${getScaStatus}>" != "<>" ]; then
+	  $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+	  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+	  $CAT ${OUT1}
+	  $ECHO "DEBUG (${scriptName}): END"
+	  continue
+      fi
+      $RM -f ${OUT1}
+
+      scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
+      
+#prepare job submission
+      cd ${aboveOnOffDir}
+#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
+
+      logFile="analyse_CALIB_${sourceRadio}_${dateDAQ}.log"
+      $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle -numcycle ${firstCycle},${lastCycle} -calibopt mean  -debuglev 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}"
+	  cat > ${logFile} <<EOF
+	  Ok calibration finished
+EOF
+      else
+	  ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle  -numcycle ${firstCycle},${lastCycle} -calibopt mean  -debuglev 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}
+      fi
+      $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
+      rcstatus=`$GREP -i "Ok calibration finished" ${logFile} | $WC -l`
+      if [ ${rcstatus} -eq 0 ]; then
+	  $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
+	  $ECHO "                      START logfile"
+	  $CAT ${logFile}
+	  $ECHO "                      END   logFile"
+#clean and leave
+	  cd ${aboveOnOffDir}
+	  $LS | $XARGS -i $RM -rf {}
+	  continue
+      fi
+#Save into Irods
+      $ECHO "DEBUG (${scriptName}): START save output files"
+      $LS ${aboveOnOffDir}
+#use -f option for iput ONLY to force override
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: here we look for calib file and put them in Irods"
+      else
+	  $FIND . -name "calib_*" -print | $XARGS -i  iput  -v -K {} ${i}
+      fi
+#save analysis logfile
+      $LS -l ${tmppublicpath} > /dev/null
+      $CP ${logFile} ${tmppublicpath}
+      $ECHO "DEBUG (${scriptName}): END save output files"
+#clean up to avoid scratch SIZE EXCEED LIMIT
+      cd ${aboveOnOffDir}
+      $LS | $XARGS -i $RM -rf {}
+  fi
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/etude_data.pic
===================================================================
--- /BAORadio/AmasNancay/v2/etude_data.pic	(revision 538)
+++ /BAORadio/AmasNancay/v2/etude_data.pic	(revision 538)
@@ -0,0 +1,227 @@
+echo ".... execute ON"
+exec analdata 20110416abell85 151300 On datacycle 1
+echo ".... execute OFF"
+exec analdata 20110416abell85 151300 Off datacycle 1
+
+echo ".... execute Merging"
+
+c++compile offset
+
+
+#
+#follow cycle to cycle variation of calibration coeff
+#
+vecfrascii calibOnCh0   20110416abell85-On-1346MHz-Ch0Cycles.txt
+vecfrascii calibOnCh1   20110416abell85-On-1346MHz-Ch1Cycles.txt
+vecfrascii calibOffCh0  20110416abell85-Off-1346MHz-Ch0Cycles.txt
+vecfrascii calibOffCh1  20110416abell85-Off-1346MHz-Ch1Cycles.txt
+
+del diff0
+del diff1
+
+set ncount 0
+set first 1
+
+for ic ${firstcycle}:${ncycles}
+  echo "...... cycle $ic"
+  ncount = $ncount + 1
+  k = ${ic} -1
+  del diffcycle${ic}v0 diffcycle${ic}v1
+  if ( $first == 1 ) then
+     set first 0
+     c++exec \
+        TVector<r_4> sigcalOnv0(sumc${ic}mOnv0,false); sigcalOnv0/=calibOnCh0[$k]; \
+        TVector<r_4> sigcalOnv1(sumc${ic}mOnv1,false); sigcalOnv1/=calibOnCh1[$k]; \
+        TVector<r_4> sigcalOffv0(sumc${ic}mOffv0,false); sigcalOffv0/=calibOffCh0[$k]; \
+        TVector<r_4> sigcalOffv1(sumc${ic}mOffv1,false); sigcalOffv1/=calibOffCh1[$k]; \
+        TVector<r_4> diffcycle${ic}v0 = sigcalOnv0-sigcalOffv0; \
+        TVector<r_4> diffcycle${ic}v1 = sigcalOnv1-sigcalOffv1; \
+	TVector<r_4> diff0(diffcycle${ic}v0,false); \
+	TVector<r_4> diff1(diffcycle${ic}v1,false); \
+        KeepObj(diff0);KeepObj(diff1); \
+	KeepObj(diffcycle${ic}v0); KeepObj(diffcycle${ic}v1);
+  else
+     c++exec \
+        TVector<r_4> sigcalOnv0(sumc${ic}mOnv0,false); sigcalOnv0/=calibOnCh0[$k]; \
+        TVector<r_4> sigcalOnv1(sumc${ic}mOnv1,false); sigcalOnv1/=calibOnCh1[$k]; \
+        TVector<r_4> sigcalOffv0(sumc${ic}mOffv0,false); sigcalOffv0/=calibOffCh0[$k]; \
+        TVector<r_4> sigcalOffv1(sumc${ic}mOffv1,false); sigcalOffv1/=calibOffCh1[$k]; \
+        TVector<r_4> diffcycle${ic}v0 = sigcalOnv0-sigcalOffv0; \
+        TVector<r_4> diffcycle${ic}v1 = sigcalOnv1-sigcalOffv1; \
+	diff0+=diffcycle${ic}v0; \
+	diff1+=diffcycle${ic}v1; \
+	KeepObj(diffcycle${ic}v0); KeepObj(diffcycle${ic}v1);
+  endif
+end
+
+
+newvec nmean 1 $ncount nodisp
+#coef RT @ 1346MHz Ouest - Est associees a Ch 0 et 1
+set xcal ( 27.724 22.543 )
+line2vec calib $xcal
+del difftot
+c++exec diff0/=nmean[0]; diff1/=nmean[0]; \
+        diff0*=calib[0];diff1*=calib[1]; \
+        TVector<r_4> difftot(diff0,false);difftot+=diff1;difftot/=2.; \
+        KeepObj(difftot);
+
+
+#
+#Calibrate and Offset Correction
+#
+for ic ${firstcycle}:${ncycles}
+  c++exec diffcycle${ic}v0*=calib[0];diffcycle${ic}v1*=calib[1]; 
+end
+
+del modiff0 moddiff1
+set first 1
+for ic ${firstcycle}:${ncycles}
+  echo "modif ...... cycle $ic"
+  
+  del inarr outarr  modiffc${ic}v0
+  cp  diffcycle${ic}v0 inarr
+  c++link offset.so meanoffset
+  call meanoffset 1285 1335 1355 1450
+  cp outarr modiffc${ic}v0
+
+  del inarr outarr modiffc${ic}v1
+  cp  diffcycle${ic}v1 inarr
+  c++link offset.so meanoffset
+  call meanoffset 1285 1335 1355 1450
+  cp outarr modiffc${ic}v1
+
+  if ( $first == 1 ) then
+     set first 0
+     c++exec \ 
+       TVector<r_4> modiff0(modiffc${ic}v0,false); \ 
+       TVector<r_4> modiff1(modiffc${ic}v1,false); \ 
+       KeepObj(modiff0);KeepObj(modiff1);
+  else
+     c++exec modiff0+=modiffc${ic}v0;  modiff1+=modiffc${ic}v1;
+  endif
+end  
+
+
+newwin 1 1
+setaxesatt $defatt
+graphicatt "$defatt xylimits=1250,1500,-1.,1."
+set first 1
+for ic ${firstcycle}:${ncycles}
+  echo "...... cycle $ic"
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}
+  if ( $first == 1 ) then
+     set first 0
+     plot2d  diffcycle${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+  else
+     plot2d  diffcycle${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+  endif
+end
+settitle "Diff. On-Off No Corr. $source Ch 0 cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+
+newwin 1 1
+setaxesatt $defatt
+graphicatt "$defatt xylimits=1250,1500,-1.,1."
+set first 1
+for ic ${firstcycle}:${ncycles}
+  echo "...... cycle $ic"
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}
+  if ( $first == 1 ) then
+     set first 0
+     plot2d  diffcycle${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+  else
+     plot2d  diffcycle${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+  endif
+end
+settitle "Diff. On-Off No Corr. $source Ch 1 cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+
+newwin 1 1
+setaxesatt $defatt
+graphicatt "$defatt xylimits=1250,1500,-50.,50."
+plot2d diff0  (n/8192)*250+1250 val*1000 n>0 "blue cpts nsta notit"
+plot2d diff1  (n/8192)*250+1250 val*1000 n>0 "red same cpts nsta notit"
+plot2d difftot  (n/8192)*250+1250 val*1000 n>0 "black same cpts nsta notit"
+settitle "Mean diff calib No Corr. per cycle ON-OFF Int.  $source cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "Int. (mJy)"
+
+newwin 1 1
+graphicatt "$defatt"
+n/plot difftot.val*1000 n>1090&&n<4000 ! ! "black  notit"
+n/plot diff0.val*1000 n>1090&&n<4000 ! ! "blue same notit"
+n/plot diff1.val*1000 n>1090&&n<4000 ! ! "red same notit"
+settitle "Diff ON-OFF No Corr. $source [$firstcycle,$lastcycle] 1280MHz - 1370MHz"
+setaxelabels "mJy" " "
+
+
+
+newwin 1 1
+setaxesatt $defatt
+graphicatt "$defatt xylimits=1250,1500,-1.,1."
+set first 1
+for ic ${firstcycle}:${ncycles}
+  echo "...... cycle $ic"
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}
+  if ( $first == 1 ) then
+     set first 0
+     plot2d  modiffc${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+  else
+     plot2d  modiffc${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+  endif
+end
+settitle "Diff. On-Off With Corr. $source Ch 0 cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+newwin 1 1
+setaxesatt $defatt
+graphicatt "$defatt xylimits=1250,1500,-1.,1."
+set first 1
+for ic ${firstcycle}:${ncycles}
+  echo "...... cycle $ic"
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}
+  if ( $first == 1 ) then
+     set first 0
+     plot2d  modiffc${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+  else
+     plot2d  modiffc${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+  endif
+end
+settitle "Diff. On-Off With Corr. $source Ch 1 cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+
+
+
+  
+del modifftot
+c++exec modiff0/=nmean[0]; modiff1/=nmean[0]; \
+        TVector<r_4> modifftot(modiff0,false);modifftot+=modiff1;modifftot/=2.; \
+        KeepObj(modifftot);
+
+newwin 1 1
+setaxesatt $defatt"
+graphicatt "$defatt xylimits=1250,1500,-50.,50."
+plot2d modiff0  (n/8192)*250+1250 val*1000 n>0 "blue cpts nsta notit"
+plot2d modiff1  (n/8192)*250+1250 val*1000 n>0 "red same cpts nsta notit"
+plot2d modifftot  (n/8192)*250+1250 val*1000 n>0 "black same cpts nsta notit"
+settitle "Mean diff ON-OFF With Corr.  $source cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "Int. (mJy)"
+
+newwin 1 1
+graphicatt "$defatt"
+n/plot modifftot.val*1000 n>1090&&n<4000 ! ! "black  notit"
+n/plot modiff0.val*1000 n>1090&&n<4000 ! ! "blue same notit"
+n/plot modiff1.val*1000 n>1090&&n<4000 ! ! "red same notit"
+settitle "Diff ON-OFF With Corr. $source [$firstcycle,$lastcycle] 1280MHz - 1370MHz"
+setaxelabels "mJy" " "
+
+
+newwin 1 1
+setaxesatt $defatt"
+graphicatt "$defatt xylimits=1250,1500,-50.,50."
+plot2d difftot  (n/8192)*250+1250 val*1000 n>0 "blue  cpts nsta notit"
+plot2d modifftot  (n/8192)*250+1250 val*1000 n>0 "red same  cpts nsta notit"
+settitle "Mean diff ON-OFF NO Corr (blue) With Corr (red)  $source cycles [$firstcycle,$lastcycle]"
+setaxelabels "Freq. (MHz)" "Int. (mJy)"
Index: /BAORadio/AmasNancay/v2/etude_gain.pic
===================================================================
--- /BAORadio/AmasNancay/v2/etude_gain.pic	(revision 538)
+++ /BAORadio/AmasNancay/v2/etude_gain.pic	(revision 538)
@@ -0,0 +1,30 @@
+set toppath "/sps/baoradio/AmasNancay/JEC"
+set source "Abell85"
+set srclower "abell85"
+set date   "20110428"
+
+####################################################
+# Gains
+####################################################
+newwin 1 1
+graphicatt "xylimits=1250,1500,0,0.008"
+
+openfits ${toppath}/${source}/${date}${srclower}/gain_${date}_${srclower}.fits
+del g0 g1
+objaoper gain_20110428_abell85 row 0 g0
+objaoper gain_20110428_abell85 row 1 g1
+plot2d g1 (n/8192)*250+1250 val n>0 "red cpts nsta notit"
+plot2d g0 (n/8192)*250+1250 val n>0 "same blue cpts nsta notit"
+settitle "Gain $source ${date} Ch 0 (blue) Ch 1 (red)"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+
+####################################################
+# Control of the gains
+####################################################
+newwin 1 1
+graphicatt "xylimits=0,25,0,0.006"
+
+openppf ${toppath}/${source}/${date}${srclower}/gain_monitor_${date}_${srclower}.ppf
+plot2d gainmoni n val n>0 "black cpts nsta notit"
+plot2d gainmoni0 n val n>0 "same blue cpts nsta notit"
+plot2d gainmoni1 n val n>0 "same red cpts nsta notit"
Index: /BAORadio/AmasNancay/v2/etude_onoff.pic
===================================================================
--- /BAORadio/AmasNancay/v2/etude_onoff.pic	(revision 538)
+++ /BAORadio/AmasNancay/v2/etude_onoff.pic	(revision 538)
@@ -0,0 +1,285 @@
+
+set cols ( blue red black green purple orange cyan violet yellow )
+
+set toppath "/sps/baoradio/AmasNancay/JEC"
+set source "Abell${1}"
+set srclower "abell${1}"
+set date   "${2}"
+
+
+#####################################
+#Plots ON-OFF with all calibrations 
+#####################################
+openppf ${toppath}/${source}/${date}${srclower}/diffOnOff_${date}_${srclower}.ppf
+
+
+#specONOFF<i>       Matrix 2 x 8192 : i=0...
+#specONOFFMean      Matrix 2 x 8192
+#specONOFF2ChanMean Vector 8192 x 1
+
+
+#####################################
+#Plots with different Means: per channel and all 
+#####################################
+del sallcyclev0
+objaoper specONOFFMean row 0 sallcyclev0
+del sallcyclev1
+objaoper specONOFFMean row 1 sallcyclev1
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.5,0.5"
+plot2d specONOFF2ChanMean (n/8192)*250+1250 val n>0 "black cpts nsta notit"
+plot2d sallcyclev0        (n/8192)*250+1250 val n>0 "same blue cpts nsta notit"
+plot2d sallcyclev1        (n/8192)*250+1250 val n>0 "same red cpts nsta notit"
+settitle "ON-OFF  ${source}/${date} Ch 0 (blue) Ch 1 (red) Mean (black)  All cycles"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+#####################################
+#Plots ON-OFF with all calibrations: chan 0 & 1 separaed and cycle per cycle 
+#####################################
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.5,0.5"
+del sc1v0
+objaoper specONOFF1 row 0 sc1v0
+plot2d sc1v0 (n/8192)*250+1250 val n>0 "$cols[0] cpts nsta notit"
+for ic 2:13
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v0
+    objaoper specONOFF${ic} row 0 sc${ic}v0	
+    plot2d sc${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+end
+settitle "ON-OFF  ${source}/${date} Ch 0 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.5,0.5"
+del sc1v1
+objaoper specONOFF1 row 1 sc1v1
+plot2d sc1v1 (n/8192)*250+1250 val n>0 "$cols[0] cpts nsta notit"
+for ic 2:13
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v1
+    objaoper specONOFF${ic} row 1 sc${ic}v1	
+    plot2d sc${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+end
+settitle "ON-OFF  ${source}/${date} Ch 1 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (Jy)"
+
+
+#####################################
+# Plots RAW no calibration at all
+#####################################
+openppf ${toppath}/${source}/${date}${srclower}/dataRaw_${date}_${srclower}.ppf
+newwin 2 2
+graphicatt "xylimits=1250,1500,0.95,1.05"
+
+###         #
+### On Ch 0 # On Ch 1 
+######################
+### Off Ch 0# Off Ch 1
+###         #
+
+set mode On	
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specRaw${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: On Ch 0"
+
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specRaw${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: On Ch 1"
+
+set mode Off
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specRaw${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: Off Ch 0"
+
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specRaw${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: Off Ch 1"
+
+#####################################
+# Plots BAO calibration included
+#####################################
+openppf ${toppath}/${source}/${date}${srclower}/dataBAOCalib_${date}_${srclower}.ppf
+newwin 2 2
+#graphicatt "xylimits=1250,1500,0.95,1.05"
+graphicatt ""
+
+
+###         #
+### On Ch 0 # On Ch 1 
+######################
+### Off Ch 0# Off Ch 1
+###         #
+
+set mode On	
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specBAOCalib${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAOCalib: On Ch 0"
+
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specBAOCalib${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAOCalib: On Ch 1"
+
+set mode Off
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specBAOCalib${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAOCalib: Off Ch 0"
+
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specBAOCalib${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAOCalib: Off Ch 1"
+
+#####################################
+# Plots BAO+RT calibration included
+#####################################
+openppf ${toppath}/${source}/${date}${srclower}/dataBAORTCalib_${date}_${srclower}.ppf
+newwin 2 2
+#graphicatt "xylimits=1250,1500,0.95,1.05"
+graphicatt ""
+
+
+###         #
+### On Ch 0 # On Ch 1 
+######################
+### Off Ch 0# Off Ch 1
+###         #
+
+set mode On	
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specBAORTCalib${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAORTCalib: On Ch 0"
+
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specBAORTCalib${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAORTCalib: On Ch 1"
+
+set mode Off
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specBAORTCalib${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAORTCalib: Off Ch 0"
+
+set first 1
+for ic 1:13
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specBAORTCalib${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} BAORTCalib: Off Ch 1"
+
Index: /BAORadio/AmasNancay/v2/etude_rawonoff.pic
===================================================================
--- /BAORadio/AmasNancay/v2/etude_rawonoff.pic	(revision 538)
+++ /BAORadio/AmasNancay/v2/etude_rawonoff.pic	(revision 538)
@@ -0,0 +1,307 @@
+
+set cols ( blue red black green purple orange cyan violet yellow )
+
+set toppath "/sps/baoradio/AmasNancay/JEC"
+set source "Abell${1}"
+set srclower "abell${1}"
+set date   "${2}"
+# implicitement firstcycle =1
+set lastcycle $3
+lastP1cycle = ${lastcycle}+1
+
+echo Analyse ${source}/${data} from cycles 1-${lastcycle} (${lastP1cycle})
+
+pssetfilename rawonoff-${source}-${date}.ps
+
+
+
+
+#set showFigs ( 0 0 0 0 0 0 0 )
+
+
+
+#####################################
+#Plots RAW ON-OFF (RAW = WO calibrations) 
+#####################################
+openppf ${toppath}/${source}/${date}${srclower}/diffOnOffRaw_${date}_${srclower}.ppf
+
+
+#specONOFFRaw<i>       Matrix 2 x 8192 : i=0...
+#specONOFFRawMean      Matrix 2 x 8192
+#specONOFFRaw2ChanMean Vector 8192 x 1
+
+
+#####################################
+#Plots RAW ON-OFF with mean computed among the cycles AND(or NOT) the Channels
+#####################################
+
+#if ( $showFigs[0] == 1 ) then
+echo "Figure 1.........."
+
+del sallcyclev0
+objaoper specONOFFRawMean row 0 sallcyclev0
+del sallcyclev1
+objaoper specONOFFRawMean row 1 sallcyclev1
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+plot2d specONOFFRaw2ChanMean (n/8192)*250+1250 val n>0 "black cpts nsta notit"
+plot2d sallcyclev0        (n/8192)*250+1250 val n>0 "same blue cpts nsta notit"
+plot2d sallcyclev1        (n/8192)*250+1250 val n>0 "same red cpts nsta notit"
+settitle "ON-OFF  ${source}/${date} Ch 0 (blue) Ch 1 (red) Mean (black)  All cycles"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+
+w2ps
+
+#endif
+
+#####################################
+#Plots RAW ON-OFF  chan 0 & 1 separaed and cycle per cycle 
+#####################################
+
+
+#if ( $showFigs[1] == 1 ) then
+echo "Figure 2.........."
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+del sc1v0
+objaoper specONOFFRaw1 row 0 sc1v0
+plot2d sc1v0 (n/8192)*250+1250 val n>0 "$cols[0] cpts nsta notit"
+for ic 2:${lastP1cycle}
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v0
+    objaoper specONOFFRaw${ic} row 0 sc${ic}v0	
+    plot2d sc${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+end
+settitle "ON-OFF  ${source}/${date} Ch 0 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+del sc1v1
+objaoper specONOFFRaw1 row 1 sc1v1
+plot2d sc1v1 (n/8192)*250+1250 val n>0 "$cols[0] cpts nsta notit"
+for ic 2:${lastP1cycle}
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v1
+    objaoper specONOFFRaw${ic} row 1 sc${ic}v1	
+    plot2d sc${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+end
+settitle "ON-OFF  ${source}/${date} Ch 1 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+
+
+#endif
+
+#####################################
+# Plots RAW no calibration at all and no Diff ON-OFF
+#####################################
+openppf ${toppath}/${source}/${date}${srclower}/dataRaw_${date}_${srclower}.ppf
+
+#if ( $showFigs[2] == 1 ) then
+echo "Figure 3........."
+
+newwin 2 2
+graphicatt "xylimits=1250,1500,0.95,1.05"
+
+###         #
+### On Ch 0 # On Ch 1 
+######################
+### Off Ch 0# Off Ch 1
+###         #
+
+set mode On	
+set first 1
+for ic 1:${lastP1cycle}
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specRaw${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: On Ch 0"
+
+set first 1
+for ic 1:${lastP1cycle}
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specRaw${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: On Ch 1"
+
+set mode Off
+set first 1
+for ic 1:${lastP1cycle}
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v0
+    objaoper specRaw${mode}${ic} row 0 sm${mode}c${ic}v0
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v0 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: Off Ch 0"
+
+set first 1
+for ic 1:${lastP1cycle}
+  k = ${ic}-floor(${ic}/${#cols})*${#cols}	
+    del sm${mode}c${ic}v1
+    objaoper specRaw${mode}${ic} row 1 sm${mode}c${ic}v1
+    if ( ${first} == 1 ) then
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "$cols[k] cpts nsta notit"
+    else
+      plot2d sm${mode}c${ic}v1 (n/8192)*250+1250 val n>0 "same $cols[k] cpts nsta notit"
+    endif
+  set first 0
+end
+settitle "${source}/${date} Raw: Off Ch 1"
+
+w2ps
+
+#endif
+
+#####################################
+#Plots RAW ON-OFF  MEAN REDUCED  chan 0 & 1 separaed and cycle per cycle 
+#####################################
+
+#if ( $showFigs[3] == 1 ) then
+echo "Figure 4.........."
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+del sc1v0
+objaoper redMeanONOFFRaw1 row 0 sc1v0
+plot2d sc1v0 (n/32)*250+1250 val n>0 "$cols[0] marker=fcircle,7  nsta notit"
+for ic 2:${lastP1cycle}
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v0
+    objaoper redMeanONOFFRaw${ic} row 0 sc${ic}v0	
+    plot2d sc${ic}v0 (n/32)*250+1250 val n>0 "same $cols[k] marker=fcircle,7 nsta notit"
+end
+settitle "ON-OFF Mean 32bins ${source}/${date} Ch 0 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+del sc1v1
+objaoper redMeanONOFFRaw1 row 1 sc1v1
+plot2d sc1v1 (n/32)*250+1250 val n>0 "$cols[0] marker=fcircle,7 nsta notit"
+for ic 2:${lastP1cycle}
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v1
+    objaoper redMeanONOFFRaw${ic} row 1 sc${ic}v1	
+    plot2d sc${ic}v1 (n/32)*250+1250 val n>0 "same $cols[k] marker=fcircle,7 nsta notit"
+end
+settitle "ON-OFF Mean 32bins ${source}/${date} Ch 1 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+
+#endif
+
+#####################################
+#Plots RAW ON-OFF  SIGMA REDUCED  chan 0 & 1 separaed and cycle per cycle 
+#####################################
+
+
+#if ( $showFigs[4] == 1 ) then
+echo "Figure 5.........."
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,0,0.01"
+del sc1v0
+objaoper redSigmaONOFFRaw1 row 0 sc1v0
+plot2d sc1v0 (n/32)*250+1250 val n>0 "$cols[0] marker=fcircle,7  nsta notit"
+for ic 2:${lastP1cycle}
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v0
+    objaoper redSigmaONOFFRaw${ic} row 0 sc${ic}v0	
+    plot2d sc${ic}v0 (n/32)*250+1250 val n>0 "same $cols[k] marker=fcircle,7 nsta notit"
+end
+settitle "ON-OFF Sigma over 32bins ${source}/${date} Ch 0 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,0,0.01"
+del sc1v1
+objaoper redSigmaONOFFRaw1 row 1 sc1v1
+plot2d sc1v1 (n/32)*250+1250 val n>0 "$cols[0] marker=fcircle,7 nsta notit"
+for ic 2:${lastP1cycle}
+    k = ${ic}-floor(${ic}/${#cols})*${#cols}
+    del sc${ic}v1
+    objaoper redSigmaONOFFRaw${ic} row 1 sc${ic}v1	
+    plot2d sc${ic}v1 (n/32)*250+1250 val n>0 "same $cols[k] marker=fcircle,7 nsta notit"
+end
+settitle "ON-OFF Sigma over 32bins ${source}/${date} Ch 1 cycle per cycle"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+#endif
+
+#####################################
+#Plots RAW ON-OFF  MEAN REDUCED  chan 0 & 1 separaed ALL cycles 
+#####################################
+
+#if ( $showFigs[5] == 1 ) then
+echo "Figure 6.........."
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+del scallv0
+del scallv1
+
+objaoper redMeanONOFFRawAll row 0 scallv0
+objaoper redMeanONOFFRawAll row 1 scallv1
+plot2d scallv0 (n/32)*250+1250 val n>0 "blue marker=fcircle,7  nsta notit"
+plot2d scallv1 (n/32)*250+1250 val n>0 "red same marker=fcircle,7  nsta notit"
+settitle "ON-OFF Mean 32bins ${source}/${date} Ch 0 (blue) Ch 1 (red) All cycles"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+#endif
+
+#####################################
+#Plots RAW ON-OFF  SIGMA REDUCED  chan 0 & 1 separaed and cycle per cycle 
+#####################################
+
+
+#if ( $showFigs[5] == 1 ) then
+echo "Figure 6.........."
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,0,0.001"
+del scallv0
+del scallv1
+objaoper redSigmaONOFFRawAll row 0 scallv0
+objaoper redSigmaONOFFRawAll row 1 scallv1
+plot2d scallv0 (n/32)*250+1250 val n>0 "blue marker=fcircle,7  nsta notit"
+plot2d scallv1 (n/32)*250+1250 val n>0 "red same marker=fcircle,7  nsta notit"
+settitle "ON-OFF Sigma over 32bins ${source}/${date} Ch 0 (blue) Ch 1 (red) All cycles"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+w2ps
+
+#endif
+
+######################
+psclosefile
Index: /BAORadio/AmasNancay/v2/gainmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/gainmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/gainmaker.sh	(revision 538)
@@ -0,0 +1,291 @@
+#!/bin/sh -xvf
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+#$ECHO "Processing script ${scriptName} at `date`"
+
+#$ECHO "SHOULD BE RETUNED FOR THE inPath cmd line of analyse.cc (Sorry...): 21/7/11 by JEC"
+#exit 1
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+      -sim) simulationMode=On;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce [-sim]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+#
+#We forbib to perform the gain for all the sources in one shot!
+#
+#if [ ! "<${sourceRadio}>" = "All" ]; then
+$ECHO "You have selected sourceRadio = $sourceRadio"
+tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+#else
+#    tableau=( `$CAT ${OUT1} | $AWK '( NF==4 ) { print $2 }'`  )
+#fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+  $ECHO "DEBUG (${scriptName}): irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the fits file of the gains already exists
+  alreadyDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
+  if [ ${alreadyDone} -gt 0 ]; then
+      $ECHO "INFO (${scriptName}): Fits file for gains already exists"
+      continue
+  fi
+# Something should be done...
+#look if the files for the input to gain computations have been done
+  somethingTodo=1
+  input4gainOk1=`ils ${i}/Off 2>&1 | $GREP -i "^ERROR" | $WC -l` 
+  input4gainOk2=`ils ${i}/Off 2>&1 | $GREP -i "gaincycle"` 
+  if [ $input4gainOk1 -eq 0 -a "<${input4gainOk2}>" != "<>"  ]; then
+      gaincycle=`$ECHO ${input4gainOk2} | $AWK '($1 == "C-"){print $2}'| $XARGS -i  basename {}`
+      input4gainOk3=`ils ${i}/Off/${gaincycle} 2>&1 | $GREP -i "medfiltmtx" | $WC -l`
+      if [ $input4gainOk3 -gt 0 ]; then
+	  somethingTodo=0
+      fi
+  fi
+  if [ $somethingTodo -eq 1 ]; then
+#
+      $ECHO ">>>>>>>>>>>>>>>> Do proc_script $sourceRadio $dateDAQ GAIN"
+      $RM -f ${tmppublicpath}/proc_script.finished
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ GAIN"
+      else      
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ GAIN
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $RM ${tmppublicpath}/proc_script.finished
+#Todo see if there is errors in the log file before copy
+      $ECHO ">>>>>>>>>>>>>>>> Done proc_script $sourceRadio $dateDAQ GAIN"
+######
+  fi
+######
+  $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
+#go to the .../source/date-sourcelowercase directory
+  $MKDIR -p ${topDir}/${dateDAQ}${srclower}
+  cd ${topDir}/${dateDAQ}${srclower}
+  aboveOnOffDir=`pwd`
+  $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
+# So the input files are there, so the fits file can be determined
+  mode="Off"
+  spectraGenDirName=`$ECHO ${gaincycle} | $AWK '{match($0,"[a-zA-Z]+",arr); print arr[0]}'`
+  cycleNumber=`$ECHO ${gaincycle} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+#care: the gaincycle variable contains the cycle number
+  $ECHO "DEBUG (${scriptName}): START download the fits files"
+  inFileDirectory="./${mode}"
+  $MKDIR -p ${inFileDirectory}
+  irodsDownDir="${i}/${mode}/${gaincycle}"
+  OUT=./getInFits.$$
+  iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+  $LS ${inFileDirectory}/${gaincycle}
+  igetStatus=`$GREP "^ERROR" ${OUT}`
+  if [ "<$igetStatus>" != "<>" ]; then
+      $ECHO "FATAL (${scriptName}): error while iget fits files:"
+      $ECHO $igetStatus
+      continue
+  fi
+  $RM -f ${OUT}
+  $ECHO "DEBUG (${scriptName}): END download the fits files"
+#Get sca files
+  $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+  OUT1=./getScaStatus.$$
+  $RM -f ${tmppublicpath}/getscafiles.finished
+  ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+  while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+      date +%T
+      sleep 30
+  done
+  $RM ${tmppublicpath}/getscafiles.finished
+  $ECHO "DEBUG (${scriptName}): END"
+#
+  getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+#	
+  if [ "<${getScaStatus}>" != "<>" ]; then
+      $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+      $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+      $CAT ${OUT1}
+      $ECHO "DEBUG (${scriptName}): END"
+      continue
+  fi
+  $RM -f ${OUT1}
+  $ECHO "DEBUG (${scriptName}): where am i `pwd`"
+  $LS -lrt
+  
+  scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
+#	
+#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
+  logFile="analyse_GAIN_${sourceRadio}_${dateDAQ}.log"
+  $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
+  if [ ${simulationMode} = "On" ]; then
+      $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act gain  -inPath ${iojobpath} -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir ${spectraGenDirName} -mode ${mode} -numcycle ${cycleNumber},${cycleNumber} -debug 1 >& ${logFile}"
+      cat > ${logFile} <<EOF
+      Ok gain finished
+EOF
+
+  else
+      ${scriptpath}/Objs/analyse -act gain  -inPath ${iojobpath} -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir ${spectraGenDirName} -mode ${mode} -numcycle ${cycleNumber},${cycleNumber} -debuglev 1 >& ${logFile}
+  fi
+#
+  $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
+  rcstatus=`$GREP -i "OK gain finished" ${logFile} | $WC -l`
+  if [ ${rcstatus} -eq 0 ]; then
+      $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
+      $ECHO "                      START logfile"
+      $CAT ${logFile}
+      $ECHO "                      END   logFile"
+#clean and leave
+      cd ${aboveOnOffDir}
+      $RM -rf ./*
+      continue
+  fi
+#Save into Irods
+  $ECHO "DEBUG (${scriptName}): START save output files"
+  $LS ${aboveOnOffDir}
+#use -f option of iput ONLY to force override
+  if [ ${simulationMode} = "On" ]; then
+      $ECHO "SIMUL: here we look for gain file and put them in Irods"
+  else
+      $FIND . -name "gain_*" -print | $XARGS -i  iput  -v -K {} ${i}
+  fi
+#save analysis logfile
+  $LS -l ${tmppublicpath} > /dev/null
+  $CP ${logFile} ${tmppublicpath}
+  $ECHO "DEBUG (${scriptName}): END save output files"
+#clean
+  cd ${aboveOnOffDir}
+  $RM -rf ./*
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/getscafiles.sh
===================================================================
--- /BAORadio/AmasNancay/v2/getscafiles.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/getscafiles.sh	(revision 538)
@@ -0,0 +1,153 @@
+#!/bin/sh
+#Get SCA file and summary + transformed date file
+#
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+TR=/usr/bin/tr
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+
+if [ ! $# = 2 ]; then
+  $ECHO "usage: ${script
+Name} <source:ex. Abell85> <date:YYYYMMDD>"
+  touch ${tmppublicpath}/getscafiles.finished
+  exit 0
+fi
+
+# Irods environment settings
+#export PATH=$PATH:/home/bao/irods2.5_LinuxSL5
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+
+sourceRadio=$1
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+dateDAQ=$2
+
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+ iojobpath=${TMPBATCH}
+else
+   $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+   touch ${tmppublicpath}/getscafiles.finished
+   exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateDAQ}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#look if the run exists
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+#    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+#    $RM -f ${tmppublicpath}/statusdaq.finished
+#    ${scriptpath}/statusdaq.sh > ${OUT1}
+#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+# 	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/statusdaq.finished"
+# 	date +%T
+# 	sleep 30
+#     done
+#     $RM ${tmppublicpath}/statusdaq.finished
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/getscafiles.finished
+    exit 1
+fi
+
+nfiles=`${GREP} "${sourceRadio}.*${dateDAQ}" ${OUT1} | ${AWK} '(NF==4){print $4}'` 
+
+if [ "<${nfiles}>" = "<>" ]; then
+    $ECHO "FATAL (${scriptName}): No run found for <${sourceRadio}> and date <${dateDAQ}>"
+    touch ${tmppublicpath}/getscafiles.finished
+    exit 1
+fi
+
+
+#Look if the run should/can be prepared wrt SCA file transformation 
+OUT2=./scaStatus.$$
+$RM -f ${tmppublicpath}/scaextractor.finished
+${scriptpath}/scaextractor.sh $sourceRadio > ${OUT2} 2>&1
+while [ ! -f "${tmppublicpath}/scaextractor.finished" ]; do
+    $ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/scaextractor.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/scaextractor.finished
+
+
+status=`$GREP "^FATAL.*${sourceRadio}.*${dateDAQ}" ${OUT2}`
+
+if [ "<$status>" != "<>" ]; then
+    $ECHO "FATAL (${scriptName}): connot proceed further with $sourceRadio $dateDAQ"
+    touch ${tmppublicpath}/getscafiles.finished
+    exit 1
+fi
+$RM $OUT2
+
+#dowload sca files...
+dirIrod="/baoradio/data/AmasNancay/${sourceRadio}/${dateDAQ}${srclower}"
+dirLocal="."
+
+if [ ! -e $dirLocal ]; then
+ $MKDIR -p $dirLocal
+fi
+
+cd $dirLocal
+
+OUT3=./scaFiles.$$
+ils $dirIrod | $GREP -i "sca" > ${OUT3}
+tableau=( `$CAT ${OUT3}`  )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+$RM $OUT3
+
+OUT4=./igetStatus.$$
+for i in ${tableau[@]}
+do
+  if [ ! -e $i ]; then
+    iget -r -f -v -K ${dirIrod}/$i ${dirLocal} > ${OUT4} 2>&1
+  fi
+done
+
+igetStatus=`$GREP "^ERROR" ${OUT4}`
+if [ "<$igetStatus>" != "<>" ]; then
+  $ECHO "FATAL (${scriptName}): error while iget:"
+  $ECHO $igetStatus
+  touch ${tmppublicpath}/getscafiles.finished
+  exit 1
+fi
+
+$RM -f $OUT4
+
+touch ${tmppublicpath}/getscafiles.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/getsignalfiles.sh
===================================================================
--- /BAORadio/AmasNancay/v2/getsignalfiles.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/getsignalfiles.sh	(revision 538)
@@ -0,0 +1,151 @@
+#!/bin/sh
+#
+#Script sh d'extraction de fichiers d'irod 
+# Auteur: JE Campagne (LAL Orsay)
+# Creation: 2 mai 2011
+# adaptation pour le CC 17 juin 2011
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+TR=/usr/bin/tr
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+
+
+if [ ! $# = 4 ]; then
+  $ECHO "usage: ${scriptName} <source:ex. Abell85> <date:YYYYMMDD> first last"
+  touch ${tmppublicpath}/getsignalfiles.finished
+  exit 0
+fi
+
+# Irods environment settings
+#export PATH=$PATH:/home/bao/irods2.5_LinuxSL5
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+sourceRadio=$1
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+dateDAQ=$2
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+   $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+   touch ${tmppublicpath}/getsignalfiles.finished
+   exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateDAQ}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#determine the sequence of signal files to download
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+#     $ECHO "We should bring the DAQ status more up to date, this take few 
+# sec..."
+#     $RM -f ${tmppublicpath}/statusdaq.finished
+#     ${scriptpath}/statusdaq.sh > ${OUT1}
+#     while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+# 	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/statusdaq.finished"
+# 	date +%T
+# 	sleep 30
+#     done
+#     $RM ${tmppublicpath}/statusdaq.finished
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+nfiles=`${GREP} "${sourceRadio}.*${dateDAQ}" ${OUT1} | ${AWK} '(NF==4){print $4}'` 
+if [ "<${nfiles}>" == "<>" ]; then
+    $ECHO "FATAL (${scriptName}): No run found for <${sourceRadio}> and date <${dateDAQ}>"
+    $CAT $OUT1
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+
+firstFile=$3
+if [ ${firstFile} -gt  ${nfiles} ]; then
+    $ECHO "FATAL (${scriptName}): ${firstFile} >  ${nfiles}"
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+
+lastFile=$4
+if [ ${lastFile} -gt  ${nfiles} ]; then
+    $ECHO "FATAL (${scriptName}): ${lastFile} >  ${nfiles}"
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+if [ ${firstFile} -gt  ${lastFile} ]; then
+    $ECHO "FATAL (${scriptName}): ${firstFile} >  ${lastFile}"
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+
+dirIrod="/baoradio/data/AmasNancay/${sourceRadio}/${dateDAQ}${srclower}"
+dirLocal="."
+
+##$ECHO "Ready to download [${firstFile},${lastFile}] in $dirLocal from $dirIrod"
+#Nb we are in the localpath
+$MKDIR -p Fiber1
+$MKDIR -p Fiber2
+
+OUT2=./igetStatus.$$
+
+n=$firstFile
+while [[ $n -le $lastFile ]]; do
+   iget -r -f -v -K ${dirIrod}/Fiber1/signal$n.fits ${dirLocal}/Fiber1 >${OUT2} 2>&1
+   igetStatus=`$GREP "^ERROR" ${OUT2}`
+   if [ "<$igetStatus>" != "<>" ]; then
+       $ECHO "FATAL (${scriptName}): error while iget:"
+       $ECHO $igetStatus
+       touch ${tmppublicpath}/getsignalfiles.finished
+       exit 1
+   fi
+   iget -r -f -v -K ${dirIrod}/Fiber2/signal$n.fits ${dirLocal}/Fiber2 >${OUT2} 2>&1
+   igetStatus=`$GREP "^ERROR" ${OUT2}`
+   if [ "<$igetStatus>" != "<>" ]; then
+       $ECHO "FATAL (${scriptName}): error while iget:"
+       $ECHO $igetStatus
+       touch ${tmppublicpath}/getsignalfiles.finished
+       exit 1
+   fi
+   n=$(($n+1))
+done
+
+touch ${tmppublicpath}/getsignalfiles.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/hdrfitsextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/hdrfitsextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/hdrfitsextractor.sh	(revision 538)
@@ -0,0 +1,152 @@
+#!/bin/sh
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+defaultIFS=$IFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#which source to analyse
+sourceRadio=$1
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+fi
+
+if [ ! "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "You have selected sourceRadio = $sourceRadio"
+    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+else
+    tableau=( `$CAT ${OUT1} | $AWK '( NF==4 ) { print $2 }'`  )
+fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+
+
+workArea="headers"
+$MKDIR -p ./${workArea}
+cd ./${workArea}
+
+
+nloop4dbg=0
+
+
+for i in ${tableau[@]}
+do
+#look if the headers have been already extracted
+  $ECHO "DEBUG (${scriptName}): irods root dir $i"
+  for fibdir in Fiber1 Fiber2; do
+      $ECHO "DEBUG (${scriptName}): Fiber $fibdir"
+#NB: care should be taken to the fact that ILS returns all the instance of the file (HPSS, disk...)
+#    so one should use a trick to count unique instance
+      listOfsig=( `ils ${i}/${fibdir} | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+      IFS='
+'
+      listOfsig=( $( $PRINTF "%s\n" "${listOfsig[@]}" | $AWK 'x[$0]++ == 0' ) )
+      nblinesSIG=${#listOfsig[@]}
+      listOfhdr=( `ils ${i}/${fibdir} | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+      IFS='
+'
+      listOfhdr=( $( $PRINTF "%s\n" "${listOfhdr[@]}" | $AWK 'x[$0]++ == 0' ) )
+      nblinesHDR=${#listOfhdr[@]}
+      $ECHO "(DEBUG) (${scriptName}:  test if $nblinesHDR != $nblinesSIG "
+      if [ $nblinesHDR != $nblinesSIG ]; then
+#do the header extraction
+#build signal files list 
+#loop on signal files
+	  $ECHO "(DEBUG) (${scriptName}):  ${listOfsig[@]}"
+
+	  for sigfname in ${listOfsig[@]}
+	    do
+	    filename=$(basename $sigfname)
+	    extension=${filename##*.}
+	    filename=${filename%.*}
+            number=`$ECHO $filename | $AWK '{len=length("signal");len=len+1;a=substr($1,len);print a}'`
+	    hdrname="header${number}.txt"
+#look if header file already exists: if so skip it
+	    hdrIsthere=`ils ${i}/${fibdir}/${hdrname} 2>&1 | $GREP -i "^ERROR" | $WC -l`
+	    $ECHO "(DEBUG) (${scriptName}): look if ${hdrname}  exist (${hdrIsthere})"
+	    if [ $hdrIsthere -eq 0 ]; then 
+		continue
+	    fi
+#get the signal file
+	    $ECHO "DEBUG (${scriptName}): iget $sigfname (dgg = ${nloop4dbg})"
+            OUT3=./igetlog${nloop4dbg}.$$
+	    iget -f -v -K ${i}/${fibdir}/${sigfname} . >${OUT3} 2>&1
+	    igetStatus=`$GREP "^ERROR" ${OUT3}`
+	    if [ "<$igetStatus>" != "<>" ]; then
+		$ECHO "INFO (${scriptName}): error while iget:"
+		$ECHO $igetStatus
+		continue
+	    fi
+            $RM $OUT3
+#extract the header part of the  signal file
+	    $ECHO "DEBUG (${scriptName}): get header from ${sigfname} and put it in ${hdrname}"
+	    IFS=$defaultIFS
+	    while read -n80 L
+	      do
+	      [ "${L::3}" = "END" ] && break
+	      $ECHO $L
+	    done < ${sigfname} > ${hdrname}
+	    $RM ${sigfname}
+	    nhdrlines=`$WC -l ${hdrname} | $AWK '{print $1}' `
+	    if [  "${nhdrlines}" = "0"  ]; then
+		$ECHO "WARNING (${scriptName}): ${sigfname} has no valid header"
+	    else
+#transfert to irod the header
+		$ECHO "INFO (${scriptName}): transfert ${hdrname} to ${i}/${fibdir}"
+#use -f option ONLY to force override
+		iput  -v -K ${hdrname} ${i}/${fibdir}
+	    fi
+            $RM ${hdrname}
+	    nloop4dbg=$(expr $nloop4dbg + 1)
+	  done
+      else
+	  $ECHO "INFO (${scriptName}): nothing to do for ${i}/${fibdir}"
+      fi
+  done
+done
Index: /BAORadio/AmasNancay/v2/makefile
===================================================================
--- /BAORadio/AmasNancay/v2/makefile	(revision 538)
+++ /BAORadio/AmasNancay/v2/makefile	(revision 538)
@@ -0,0 +1,30 @@
+include $(SOPHYABASE)/include/sophyamake.inc
+
+OBJ = ./Objs/
+EXE = ./Objs/
+
+
+PKGOLIST =
+
+EXELIST = $(EXE)analyse
+
+EXEOLIST = $(EXE)analyse.o
+
+
+all : analyse
+
+clean :
+	rm -f $(PKGOLIST) $(EXELIST) $(EXEOLIST)
+
+########################################################
+
+#......
+analyse : $(EXE)analyse
+	echo '---analyse made'
+
+$(EXE)analyse : $(OBJ)analyse.o $(PKGOLIST)
+	$(CXXLINK) -o $(EXE)analyse $(OBJ)analyse.o $(PKGOLIST) $(SOPHYAALLSLBLIST)
+
+$(OBJ)analyse.o : analyse.cc $(PKGHLIST)
+	$(CXXCOMPILE) -c  -o $(OBJ)analyse.o analyse.cc 
+
Index: /BAORadio/AmasNancay/v2/mergeRawOnOff.pic
===================================================================
--- /BAORadio/AmasNancay/v2/mergeRawOnOff.pic	(revision 538)
+++ /BAORadio/AmasNancay/v2/mergeRawOnOff.pic	(revision 538)
@@ -0,0 +1,92 @@
+defscript sumSpec
+
+set source "Abell${1}"
+set srclower "abell${1}"
+set date $2
+# implicitement firstcycle =1
+set lastcycle $3
+lastP1cycle = ${lastcycle}+1
+
+openppf ${toppath}/${source}/${date}${srclower}/diffOnOffRaw_${date}_${srclower}.ppf
+
+#specONOFFRaw<i>       Matrix 2 x 8192 : i=0...
+
+for ic 1:${lastP1cycle}
+    del sOnOffv0
+    del sOnOffv1
+    objaoper specONOFFRaw${ic} row 0 sOnOffv0	
+    objaoper specONOFFRaw${ic} row 1 sOnOffv1	
+    c++exec specONOFFv0+=sOnOffv0; specONOFFv1+=sOnOffv1;
+end
+
+
+endscript
+
+
+########################################################################
+#MAIN
+########################################################################
+set cols ( blue red black green purple orange cyan violet yellow )
+
+set toppath "/sps/baoradio/AmasNancay/JEC"
+set source "Abell${1}"
+set srclower "abell${1}"
+
+#The output spectra 
+#newvec specONOFFv0 8192
+#newvec specONOFFv1 8192
+
+c++exec TVector<r_4> specONOFFv0(8192,2); TVector<r_4> specONOFFv1(8192,2); \
+	KeepObj(specONOFFv0);KeepObj(specONOFFv1);
+
+#set the runs to merge
+
+set dateTag ( 20110507 20110602 20110729 20110806 )
+set nCycles ( 24 14 27 26 )
+line2vec tmpvec $nCycles
+
+#do It
+for i 0:${tmpvec.size}
+  sumSpec 85 $dateTag[i] $nCycles[i]
+end
+c++exec specONOFFv0/=tmpvec.Sum(); specONOFFv1/=tmpvec.Sum();
+
+
+#########
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+plot2d   specONOFFv0    (n/8192)*250+1250 val n>0 "same blue cpts nsta notit"
+plot2d   specONOFFv1    (n/8192)*250+1250 val n>0 "same red cpts nsta notit"
+settitle "ON-OFF  ${source} Ch 0 (blue) Ch 1 (red) ${tmpvec.sum} cycles"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+
+#########
+#Perform the frequancy rebinning to compute MEAN & SIGMA
+#########
+c++compile reducing        
+c++link reducing.so doreduc
+
+
+call doreduc
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+ del redMeanv0                                                                      
+ del redMeanv1                                                                      
+ objaoper reducedMeanDiffOnOff row 0 redMeanv0                                      
+ objaoper reducedMeanDiffOnOff row 1 redMeanv1                                      
+ plot2d redMeanv0 (n/32)*250+1250 val n>0 "blue marker=fcircle,7 nsta notit"        
+ plot2d redMeanv1 (n/32)*250+1250 val n>0 "same red marker=fcircle,7 nsta notit"
+settitle "ON-OFF  ${source} Ch 0 (blue) Ch 1 (red) ${tmpvec.sum} cycles"
+setaxelabels "Freq. (MHz)" "I (a.u)"
+
+newwin 1 1
+graphicatt "xylimits=1250,1500,0,0.001"
+ del redSigmav0                                                                      
+ del redSigmav1                                                                      
+ objaoper reducedSigmaDiffOnOff row 0 redSigmav0                                      
+ objaoper reducedSigmaDiffOnOff row 1 redSigmav1                                      
+ plot2d redSigmav0 (n/32)*250+1250 val n>0 "blue marker=fcircle,7 nsta notit"        
+ plot2d redSigmav1 (n/32)*250+1250 val n>0 "same red marker=fcircle,7 nsta notit"
+settitle "ON-OFF  ${source} Ch 0 (blue) Ch 1 (red) ${tmpvec.sum} cycles"
+setaxelabels "Freq. (MHz)" "I (a.u)"
Index: /BAORadio/AmasNancay/v2/onoffmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/onoffmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/onoffmaker.sh	(revision 538)
@@ -0,0 +1,455 @@
+#!/bin/sh -xvf
+#Perform the ON-OFF analysis
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+#which source to analyse
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+freqBAOCalib=1346
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2;    shift;;
+      -date) dateSelected=$2;     shift;;
+      -freq) freqBAOCalib=$2;   shift;;
+      -sim)  simulationMode=On;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+#wakeup the NFS disk
+$LS -l ${tmppublicpath} > /dev/null
+#clean previous spurious files
+$RM -f ${tmppublicpath}/*.finished
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+
+#Nb: if we want to implement a loop on sources we have to take care of srclower...
+
+$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
+tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+IFS='
+' 
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+#
+# start a "fresh" session  
+  cd ${topDir}
+  $LS | $XARGS -i $RM -rf {}
+  
+  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+
+  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
+  $LS -lrt
+
+
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the gain & calibration files exist
+#
+  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
+  if [ ! ${gainDone} -gt 0 ]; then
+      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
+      continue
+  fi
+
+  calibDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
+  if [ ! ${calibDone} -gt 0 ]; then
+      $ECHO "FATAL (${scriptName}): calibration files DO NOT exist skip `basename $i` "
+      continue
+  fi
+#
+  somethingTodo=1
+  input4dataOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n`  
+  input4dataOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n`  
+#
+  if [ "<${input4dataOff}>" != "<>" -a "<${input4dataOn}>" != "<>"  ]; then
+#look at the content of the datacycle directory as may be the previous batch job has crashed
+      tableauOff=( `$ECHO ${input4dataOff}` )
+      IFS='
+'
+      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOff=0
+      for j in ${tableauOff[@]}
+	do
+	nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
+      done
+#
+      tableauOn=( `$ECHO ${input4dataOn}` )
+      IFS='
+      '
+      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOn=0
+      for j in ${tableauOn[@]}
+	do
+	nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
+      done
+      diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
+      if [ ${diffOnOff} -lt 0 ]; then
+	  zero=0
+	  diffOnOff=`expr ${zero} - ${diffOnOff}` 
+      fi
+#allow files differences
+      if [  ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0  ]; then
+	  somethingTodo=0
+	  $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
+      else
+	  $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
+      fi
+# 
+  fi 
+  if [ ${somethingTodo} -eq 1 ]; then
+#
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ANALOFF"
+      $RM -f ${tmppublicpath}/proc_script.finished
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ OFF"
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ON"
+      $RM ${tmppublicpath}/proc_script.finished
+      
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $RM ${tmppublicpath}/proc_script.finished
+#Todo see if there is errors in the log file before copy
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ ON"
+######
+  else
+######
+      $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
+      #go to the .../source/date-sourcelowercase directory
+#download Off/datacycle<> and On/datacycle<> directories
+      $MKDIR -p ${topDir}/${dateDAQ}${srclower}
+      cd ${topDir}/${dateDAQ}${srclower}
+      aboveOnOffDir=`pwd`
+      $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
+#
+      mode="Off"
+      $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
+      inFileDirectory="./${mode}"
+      $MKDIR -p ${inFileDirectory}
+      listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
+      IFS='
+'
+      listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+
+      firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+      nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
+
+      lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+      listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
+
+      
+      for j in ${listOfDataCycle[@]}
+	do
+	irodsDownDir="${j}"
+	OUT=./getInFits.$$
+	iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget fits files:"
+	    $ECHO $igetStatus
+	    $ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	    continue
+	fi
+	$RM -f ${OUT}
+      done
+      $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
+#
+      mode="On"
+      $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
+      inFileDirectory="./${mode}"
+      $MKDIR -p ${inFileDirectory}
+
+      listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
+      IFS='
+'
+      listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+
+      firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+      nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
+
+      lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+      listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
+
+
+#
+#Check if there is the same number of cycles in Off and On mode
+#      
+#first cycle = max(firstCycleOn, firstCycleOff)
+#last  cycle = min(lastCycleOn,lastCycleOff)
+
+      if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
+	  firstCycle=${firstCycleOff}
+	  lastCycle=${lastCycleOff}
+      else
+	  $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
+	  $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
+	  firstCycle=${firstCycleOff}
+	  if [ ${firstCycleOn} -gt ${firstCycle} ]; then
+	      firstCycle=${firstCycleOn}
+	  fi
+	  lastCycle=${lastCycleOff}
+	  if [ ${lastCycleOn} -lt ${lastCycle} ]; then
+	      lastCycle=${lastCycleOn}
+	  fi
+      fi
+      $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
+      
+
+      for j in ${listOfDataCycle[@]}
+	do
+	irodsDownDir="${j}"
+	OUT=./getInFits.$$
+	iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget fits files:"
+	    $ECHO $igetStatus
+	    $ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	    continue
+	fi
+	$RM -f ${OUT}
+      done
+      $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
+#Get sca file
+      $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+      OUT1=./getScaStatus.$$
+      $RM -f ${tmppublicpath}/getscafiles.finished
+      ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+      while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+	  $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+	  date +%T
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/getscafiles.finished
+      $ECHO "DEBUG (${scriptName}): END"
+#
+      getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+#	
+      if [ "<${getScaStatus}>" != "<>" ]; then
+	  $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+	  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+	  $CAT ${OUT1}
+	  $ECHO "DEBUG (${scriptName}): END"
+	  continue
+      fi
+      $RM -f ${OUT1}
+
+      scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
+
+
+#Get the calibration files
+      OUT2=./calibFiles.$$
+      ils ${i} | ${GREP} -i "calib.*\.txt" > ${OUT2}
+      listeOfCalib=( `$CAT ${OUT2}` )
+      IFS='
+'
+      listeOfCalib=( $( $PRINTF "%s\n" "${listeOfCalib[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+      $RM $OUT2
+      for fcal in ${listeOfCalib[@]}
+      do
+	OUT3=./getCalib.$$
+	iget -f -K -v ${i}/${fcal} ${aboveOnOffDir}> ${OUT3} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT3}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget calib files:"
+	    $ECHO $igetStatus
+	    exit 1
+	fi
+	$RM -f ${OUT3}
+      done
+      
+#prepare job submission
+      cd ${aboveOnOffDir}
+#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
+
+      logFile="analyse_DATA_${sourceRadio}_${dateDAQ}.log"
+      $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act dataOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debuglev 1 -freqBAOCalib ${freqBAOCalib} >& ${logFile}"
+	  cat > ${logFile} <<EOF
+	  Ok calibration finished
+EOF
+      else
+	  ${scriptpath}/Objs/analyse -act dataOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debuglev 1 -freqBAOCalib ${freqBAOCalib} >& ${logFile}
+      fi
+      $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
+      rcstatus=`$GREP -i "Ok dataOnOff finished" ${logFile} | $WC -l`
+      if [ ${rcstatus} -eq 0 ]; then
+	  $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
+	  $ECHO "                      START logfile"
+	  $CAT ${logFile}
+	  $ECHO "                      END   logFile"
+#clean and leave
+	  cd ${aboveOnOffDir}
+	  $LS | $XARGS -i $RM -rf {}
+	  continue
+      fi
+#Save into Irods
+      $ECHO "DEBUG (${scriptName}): START save output files"
+      $LS ${aboveOnOffDir}
+#use -f option for iput ONLY to force override
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: here we look for calib file and put them in Irods"
+      else
+	  $LS -lrth
+	  $FIND . -name "dataRaw_*" -print | $XARGS -i  iput  -v -K {} ${i}
+	  $FIND . -name "dataBAOCalib_*" -print | $XARGS -i  iput  -v -K {} ${i}
+	  $FIND . -name "dataBAORTCalib_*" -print | $XARGS -i  iput  -v -K {} ${i}
+	  $FIND . -name "diffOnOff_*" -print | $XARGS -i  iput  -v -K {} ${i}
+      fi
+#save analysis logfile
+      $LS -l ${tmppublicpath} > /dev/null
+      $CP ${logFile} ${tmppublicpath}
+      $ECHO "DEBUG (${scriptName}): END save output files"
+#clean up to avoid scratch SIZE EXCEED LIMIT
+      cd ${aboveOnOffDir}
+      $LS | $XARGS -i $RM -rf {}
+  fi
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/proc_rawonoff.sh
===================================================================
--- /BAORadio/AmasNancay/v2/proc_rawonoff.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/proc_rawonoff.sh	(revision 538)
@@ -0,0 +1,514 @@
+#!/bin/sh -xvf
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+TR=/usr/bin/tr
+FIND=/usr/bin/find
+PRINTF=/usr/bin/printf
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+QSUB=qsub
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+######
+# Local functions
+######
+usage() {
+  echo "usage: $scriptName <source> <YYYYMMDD-directory> <type of process>"
+  echo "      -<source> as Abell85"
+  echo "      -<YYYYMMDD-directory> as 20110428"
+  echo "      -<type of process> as GAIN|CALIBON|CALIBOFF|ON|OFF"
+}
+######
+# argument test
+######
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+typeofproc=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+#
+#first and last cycle to process
+#
+firstCycle=
+lastCycle=
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2;    shift;;
+      -date) dateSelected=$2;     shift;;
+      -type) typeofproc=$2; shift;;
+      -fcycle) firstCycle=$2; shift;;
+      -lcycle) lastCycle=$2; shift;;
+      -sim)  simulationMode=On;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce -date YYYYMMDD -type type [-fcycle firstCycle] [-lcycle lastCycle] [-sim to trig simulation mode]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+#Check input
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+if [ "<${dateSelected}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the date option (-date)"
+    exit 1
+fi
+
+if [ "<${typeofproc}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the type option (-type)"
+    exit 1
+fi
+
+
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateSelected}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+mainDirectory=`pwd`
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#select file per cycle
+#$ECHO "Select files process..."
+#keep only lines with structure: cycle_number first,last,step
+#Get sca files
+$ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateSelected}"
+OUT1=./getScaStatus.$$
+$RM -f ${tmppublicpath}/getscafiles.finished
+${scriptpath}/getscafiles.sh ${sourceRadio} ${dateSelected} > ${OUT1} 2>&1
+while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+    $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+    date +%T
+    sleep 30
+done
+$RM -f ${tmppublicpath}/getscafiles.finished
+$ECHO "DEBUG (${scriptName}): END"
+
+
+getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+
+if [ "<${getScaStatus}>" != "<>" ]; then
+  $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateSelected"
+  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+  $CAT ${OUT1}
+  $ECHO "DEBUG (${scriptName}): END"
+  touch ${tmppublicpath}/proc_script.finished
+  exit 1
+fi
+
+
+scaFileSummarized=`$FIND . -name "sca*.sum" -print`
+if [ "<${scaFileSummarized}>" = "<>" ]; then
+  $ECHO "FATAL (${scriptName}): strange sca file summarized not available: $sourceRadio $dateSelected"
+  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+  $CAT ${OUT1}
+  $ECHO "DEBUG (${scriptName}): END"
+  touch ${tmppublicpath}/proc_script.finished
+  exit 1
+fi
+$RM ${OUT1}
+
+scaFileSummarized=`basename $scaFileSummarized`
+
+#set Irods environment
+. /usr/local/shared/bin/irods_env.sh -noverbose
+#download header files to be used for signal files selction
+$MKDIR -p ./Fiber1
+dirFiberfile=`pwd`/Fiber1
+
+dirIrods="/baoradio/data/AmasNancay/${sourceRadio}/${dateSelected}${srclower}/Fiber1"
+
+listOfFiles=( `ils ${dirIrods} | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+IFS='
+'
+listOfFiles=( $( $PRINTF "%s\n" "${listOfFiles[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+#
+$ECHO "DEBUG (${scriptName}): Download if necessary the headers"
+#
+for ifile in ${listOfFiles[@]}
+do
+#test if the headers have not yet been downloaded
+  if [ ! -e ${dirFiberfile}/${ifile} ]; then
+      OUT2=./getheaders.$$
+      iget -f -K ${dirIrods}/${ifile} ${dirFiberfile} >${OUT2} 2>&1
+      igetStatus=`$GREP "^ERROR" ${OUT2}`
+      if [ "<$igetStatus>" != "<>" ]; then
+	  $ECHO "FATAL (${scriptName}): error while iget:"
+	  $ECHO $igetStatus
+	  touch ${tmppublicpath}/proc_script.finished
+	  exit 1
+      fi
+      $RM -f $OUT2
+  fi
+done
+#
+$ECHO "DEBUG (${scriptName}): START call select.sh ${scaFileSummarized} ${dirFiberfile} ${typeofproc}"
+#
+#Get list of file to process
+headerFileSelected=./selectSignal.$$
+$RM -f ${tmppublicpath}/select.finished
+${scriptpath}/select.sh ${scaFileSummarized} ${dirFiberfile} ${typeofproc} | $GREP "^#[0-9]" | $SED "s/^#//" > $headerFileSelected
+while [ ! -f "${tmppublicpath}/select.finished" ]; do
+    $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/select.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/select.finished
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+$ECHO "DEBUG (${scriptName}): START filling cycleArray, filesArray"
+#
+declare -a cycleArray
+declare -a filesArray
+$ECHO "DEBUG (${scriptName}): IFS value between<>: <$IFS>"
+IFS=$DefaultIFS
+$ECHO "DEBUG (${scriptName}): START cat $headerFileSelected"
+$CAT $headerFileSelected
+$ECHO "DEBUG (${scriptName}): END cat $headerFileSelected"
+
+while read v1 v2
+do
+ cycleArray[${#cycleArray[*]}]=$v1
+ filesArray[${#filesArray[*]}]=$v2
+done < $headerFileSelected
+
+if [ ${#cycleArray[*]} -eq 0 -o ${#filesArray[*]} -eq 0 ]; then
+   $ECHO "FATAL (${scriptName}): no header file selected (${#cycleArray[*]}, ${#filesArray[*]})"
+   touch ${tmppublicpath}/proc_script.finished
+   exit 1
+else
+   $ECHO "DEBUG (${scriptName}): non zero elements found for cycleArray and filesArray" 
+fi
+
+#
+$ECHO "DEBUG (${scriptName}): number of elements <${#cycleArray[*]}> <${#filesArray[*]}>"
+$ECHO "DEBUG (${scriptName}): END"
+#
+# (timeShift)
+#Add 3sec to start ON / OFF
+#Substract 4sec to start DAB ON / OFF
+#
+#Take care: Cycle numbering starts at 1 in SCAFiles while Arrays index start at 0
+#
+#for GAIN the reference cycle takes the middle of the run
+#
+#nwinmean donne le nombre de fenetre tspwin utilise pour faire calculer la moyenne des medianes de chaque fenetre
+#This is the nimber of paquets of the time window which change according to processing type
+$ECHO "DEBUG (${scriptName}): START switch on $typeofproc"
+#
+case "$typeofproc" in
+    GAIN)
+      $ECHO "process signal files for GAIN phase"
+      ingain=""
+      freqfilter="-freqfilter -"
+      nwinmean="1"
+      tspwin="5120"
+      dirName="Off"
+      cyclebasename="gaincycle"
+      indexStartHour="6"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId=`expr  ${#cycleArray[*]} / 2`
+      firstCycleId=`expr ${firstCycleId} - 1`
+      lastp1CycleId=`expr ${firstCycleId} + 1`
+      ;;
+    ON)
+      $ECHO "process signal files for ON phases"
+      ingain="-gain gain_${dateSelected}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="5"
+      tspwin="5120"
+      dirName="On"
+      cyclebasename="datacycle"
+      indexStartHour="4"
+      timeShift="3"
+      timeDuration="30"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    OFF)
+      $ECHO "process signal files for OFF phases"
+      ingain="-gain gain_${dateSelected}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="5"
+      tspwin="5120"
+      dirName="Off"
+      cyclebasename="datacycle"
+      indexStartHour="8"
+      timeShift="3"
+      timeDuration="30"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    CALIBON)
+      $ECHO "process signal files for CALIBRATION ON phases"
+      ingain="-gain gain_${dateSelected}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="1"
+      tspwin="1024"
+      dirName="On"
+      cyclebasename="calibcycle"
+      indexStartHour="2"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    CALIBOFF)
+      $ECHO "process signal files for CALIBRATION OFF phases"
+      ingain="-gain gain_${dateSelected}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="1"
+      tspwin="1024"
+      dirName="Off"
+      cyclebasename="calibcycle"
+      indexStartHour="6"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    *)
+    $ECHO "FATAL (${scriptName}): type of processing"
+    usage
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+    ;;
+esac
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+#
+#Compute time windows (Warning usage of GMT date)
+#
+$ECHO "DEBUG (${scriptName}): START compute time windows"
+OUT=./baotiming.$$
+${AWK} -v datesys=${DATE} -v type=${indexStartHour} -v shift=${timeShift} '
+BEGIN {FS=","}
+{
+  phaseStart=$type
+  cmd=sprintf("%s +%%s -d  \"%s\" ",datesys,phaseStart);
+  (cmd | getline phstart);
+  close(cmd);
+  newstart = phstart+shift;
+  cmd=sprintf("%s +%%T -d \"1970-01-01 %s sec GMT\"",datesys,newstart);
+  (cmd | getline newStart);
+  close(cmd);
+  print $1,newStart,phaseStart;
+}
+'  ${scaFileSummarized} > ${OUT}
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+$ECHO "DEBUG (${scriptName}): START cat ${OUT}"
+$CAT ${OUT}
+$ECHO "DEBUG (${scriptName}): END cat ${OUT}"
+#
+$ECHO "DEBUG (${scriptName}): START filling cylceArrayBis timeArray"
+#
+declare -a cycleArrayBis
+declare -a timeArray
+$ECHO "DEBUG (${scriptName}): IFS value between<>: <$IFS>"
+IFS=$DefaultIFS
+#select the selected cycles
+idtofind="0"
+while read v1 v2 v3
+do
+ if [ ${idtofind} -lt ${#cycleArray[*]} ]; then
+    if [ ${v1} = ${cycleArray[${idtofind}]} ]; then
+	cycleArrayBis[${#cycleArrayBis[*]}]=$v1
+	timeArray[${#timeArray[*]}]=$v2
+	idtofind=`expr ${idtofind} + 1`
+    fi
+ fi
+done < ${OUT}
+$ECHO "DEBUG (${scriptName}): number of elements ${#cycleArrayBis[*]} ${#timeArray[*]}"
+$ECHO "DEBUG (${scriptName}): END"
+
+
+#########################
+# specmfib exe
+#########################
+prg="/afs/in2p3.fr/throng/baoradio/Library/Progs/TAcq/Objs/specmfib"
+if [ ! -e $prg ]; then
+    $ECHO "FATAL (${scriptName}): $prg not found"
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+fi
+
+
+dirFiberfile=${iojobpath}/${localpath}
+
+curDir=`pwd`
+#
+$ECHO "DEBUG (${scriptName}): in principle $dirFiberfile == $curDir "
+#
+#force single channel
+forceSingle="-singlechan"
+#debug
+prtlevel="1"
+
+
+#Loop on cycles
+##
+$ECHO "DEBUG (${scriptName}): START loop on cycles Ids ${firstCycleId} to ${lastp1CycleId}"
+
+#write the script to be send to Batch
+#get gain file if necessary
+if [ ${typeofproc} != "GAIN" ]; then
+    OUT2=./igetStatus.$$
+    iget /baoradio/data/AmasNancay/${localpath}/gain_${dateSelected}_${srclower}.fits >${OUT2} 2>&1
+    igetStatus=`$GREP "^ERROR" ${OUT2}`
+    if [ "<${igetStatus}>" != "<>" ]; then
+	$ECHO "FATAL (${scriptName}): error while iget:"
+	$ECHO $igetStatus
+	touch ${tmppublicpath}/${jobBatchName}.finished
+	exit 1
+    fi
+    $RM ${OUT2}
+fi    
+
+
+for ((i=${firstCycleId};i<${lastp1CycleId};i++)); do
+#
+#time window selection
+    tmproc="-tmproc ${timeArray[${i}]},${timeDuration}"
+#cycle <number>
+    cycle="${cycleArray[${i}]}"
+    if [ "<${firstCycle}>" != "<>" -a "<${lastCycle}>" != "<>" ]; then
+	if [ ${cycle} -lt ${firstCycle} -o ${cycle} -gt ${lastCycle} ]; then
+	    continue
+	fi
+    fi
+    $ECHO "DEBUG (${scriptName}): process cycle $cycle"
+
+#cycle <name><number>
+    cycle="${cyclebasename}${cycleArray[${i}]}"
+#signal files selection
+    infiles="${filesArray[${i}]}"
+#
+#Prepare BQS parameters: MEMORY, CPU...#
+    infilesArray=( `$ECHO $infiles | $AWK 'BEGIN {FS=","} {for(i=1;i<NF;i++)print $i;}'` )
+    IFS='
+    '
+    infilesArray=( $( printf "%s\n" "${infilesArray[@]}" | awk 'x[$0]++ == 0' ) )
+    IFS=$DefaultIFS
+    firstFile=${infilesArray[0]}
+    lastFile=${infilesArray[1]}
+#
+#
+#
+#get signals files
+    OUT3=./getsignals.$$
+    $RM -f ${tmppublicpath}/getsignalfiles.finished
+    ${scriptpath}/getsignalfiles.sh ${sourceRadio} ${dateSelected} ${firstFile}  ${lastFile} > ${OUT3} 2>&1
+    while [ ! -f "${tmppublicpath}/getsignalfiles.finished" ]; do
+	$ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getsignalfiles.finished"
+	date +%T
+	sleep 30
+    done
+    $RM ${tmppublicpath}/getsignalfiles.finished
+
+    getSignals=`$GREP "^FATAL" ${OUT3}`
+    if [ "<$getSignals>" != "<>" ]; then
+	$ECHO "FATAL (${scriptName}): error while get signals"
+	$ECHO $getSignals
+	touch ${tmppublicpath}/${jobBatchName}.finished
+	exit 1
+    fi
+    $RM ${OUT3}
+
+#location of the signal file
+    fibfile="${curDir} 1,2"
+#
+    outDir=${curDir}/${dirName}/${cycle}
+    $MKDIR -p ${outDir}
+
+#
+# where ami...
+#
+    $ECHO "We are here `pwd`"
+    $LS -lrth
+    $FIND . -name "igetStatus.*" | $XARGS -i  $CAT {}
+    $LS ./Fiber1 | $WC -l
+    $LS ./Fiber2 | $WC -l
+    $LS -lR ${outDir}
+
+#Note: here act=gain means only that specmfib will use median-like algorithms (not only for gains...)
+    $prg -act gain $forceSingle $freqfilter  -prt $prtlevel -out ${outDir} -nmean $nwinmean $ingain $tmproc -tspwin ${tspwin},0,0 -in $infiles ${fibfile}
+
+    $ECHO    ${outDir}
+    $LS -lrt ${outDir}
+
+#save results to Irods (use option -f ONLY to force override)
+    iput -v -K -r ${curDir}/${dirName} /baoradio/data/AmasNancay/${localpath}
+#
+#clean the signal files which can at the end of the day fill completly the scratch directory of the batch work/AmasNancayer
+    $RM -rf ${iojobpath}/${localpath}/Fiber1
+    $RM -rf ${iojobpath}/${localpath}/Fiber2
+    $RM -rf ${iojobpath}/${localpath}/${typeofproc}
+
+#
+#
+
+#end loop on cycles
+done
+#
+$ECHO "DEBUG (${scriptName}): END loop on cycles"
+#
+touch ${tmppublicpath}/proc_script.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/proc_script.sh
===================================================================
--- /BAORadio/AmasNancay/v2/proc_script.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/proc_script.sh	(revision 538)
@@ -0,0 +1,517 @@
+#!/bin/sh -xvf
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+TR=/usr/bin/tr
+FIND=/usr/bin/find
+PRINTF=/usr/bin/printf
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+QSUB=qsub
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+######
+# Local functions
+######
+usage() {
+  echo "usage: $scriptName <source> <YYYYMMDD-directory> <type of process>"
+  echo "      -<source> as Abell85"
+  echo "      -<YYYYMMDD-directory> as 20110428"
+  echo "      -<type of process> as GAIN|CALIBON|CALIBOFF|ON|OFF"
+}
+######
+# argument test
+######
+if [ ! $# = 3 ]; then
+  usage
+  touch proc_script.finished
+  exit 1
+fi
+sourceRadio=$1
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+dateDAQ=$2
+typeofproc=$3
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateDAQ}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+mainDirectory=`pwd`
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#select file per cycle
+#$ECHO "Select files process..."
+#keep only lines with structure: cycle_number first,last,step
+#Get sca files
+$ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+OUT1=./getScaStatus.$$
+$RM -f ${tmppublicpath}/getscafiles.finished
+${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+    $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/getscafiles.finished
+$ECHO "DEBUG (${scriptName}): END"
+
+
+getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+
+if [ "<${getScaStatus}>" != "<>" ]; then
+  $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+  $CAT ${OUT1}
+  $ECHO "DEBUG (${scriptName}): END"
+  touch ${tmppublicpath}/proc_script.finished
+  exit 1
+fi
+
+
+scaFileSummarized=`$FIND . -name "sca*.sum" -print`
+if [ "<${scaFileSummarized}>" = "<>" ]; then
+  $ECHO "FATAL (${scriptName}): strange sca file summarized not available: $sourceRadio $dateDAQ"
+  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+  $CAT ${OUT1}
+  $ECHO "DEBUG (${scriptName}): END"
+  touch ${tmppublicpath}/proc_script.finished
+  exit 1
+fi
+$RM ${OUT1}
+
+scaFileSummarized=`basename $scaFileSummarized`
+
+#set Irods environment
+. /usr/local/shared/bin/irods_env.sh -noverbose
+#download header files to be used for signal files selction
+$MKDIR -p ./Fiber1
+dirFiberfile=`pwd`/Fiber1
+
+dirIrods="/baoradio/data/AmasNancay/${sourceRadio}/${dateDAQ}${srclower}/Fiber1"
+
+listOfFiles=( `ils ${dirIrods} | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+IFS='
+'
+listOfFiles=( $( $PRINTF "%s\n" "${listOfFiles[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+#
+$ECHO "DEBUG (${scriptName}): Download if necessary the headers"
+#
+for ifile in ${listOfFiles[@]}
+do
+#test if the headers have not yet been downloaded
+  if [ ! -e ${dirFiberfile}/${ifile} ]; then
+      OUT2=./getheaders.$$
+      iget -f -K ${dirIrods}/${ifile} ${dirFiberfile} >${OUT2} 2>&1
+      igetStatus=`$GREP "^ERROR" ${OUT2}`
+      if [ "<$igetStatus>" != "<>" ]; then
+	  $ECHO "FATAL (${scriptName}): error while iget:"
+	  $ECHO $igetStatus
+	  touch ${tmppublicpath}/proc_script.finished
+	  exit 1
+      fi
+      $RM -f $OUT2
+  fi
+done
+#
+$ECHO "DEBUG (${scriptName}): START call select.sh ${scaFileSummarized} ${dirFiberfile} ${typeofproc}"
+#
+#Get list of file to process
+headerFileSelected=./selectSignal.$$
+$RM -f ${tmppublicpath}/select.finished
+${scriptpath}/select.sh ${scaFileSummarized} ${dirFiberfile} ${typeofproc} | $GREP "^#[0-9]" | $SED "s/^#//" > $headerFileSelected
+while [ ! -f "${tmppublicpath}/select.finished" ]; do
+    $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/select.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/select.finished
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+$ECHO "DEBUG (${scriptName}): START filling cycleArray, filesArray"
+#
+declare -a cycleArray
+declare -a filesArray
+$ECHO "DEBUG (${scriptName}): IFS value between<>: <$IFS>"
+IFS=$DefaultIFS
+$ECHO "DEBUG (${scriptName}): START cat $headerFileSelected"
+$CAT $headerFileSelected
+$ECHO "DEBUG (${scriptName}): END cat $headerFileSelected"
+
+while read v1 v2
+do
+ cycleArray[${#cycleArray[*]}]=$v1
+ filesArray[${#filesArray[*]}]=$v2
+done < $headerFileSelected
+
+if [ ${#cycleArray[*]} -eq 0 -o ${#filesArray[*]} -eq 0 ]; then
+   $ECHO "FATAL (${scriptName}): no header file selected (${#cycleArray[*]}, ${#filesArray[*]})"
+   touch ${tmppublicpath}/proc_script.finished
+   exit 1
+else
+   $ECHO "DEBUG (${scriptName}): non zero elements found for cycleArray and filesArray" 
+fi
+
+#
+$ECHO "DEBUG (${scriptName}): number of elements <${#cycleArray[*]}> <${#filesArray[*]}>"
+$ECHO "DEBUG (${scriptName}): END"
+#
+# (timeShift)
+#Add 3sec to start ON / OFF
+#Substract 4sec to start DAB ON / OFF
+#
+#Take care: Cycle numbering starts at 1 in SCAFiles while Arrays index start at 0
+#
+#for GAIN the reference cycle takes the middle of the run
+#
+#nwinmean donne le nombre de fenetre tspwin utilise pour faire calculer la moyenne des medianes de chaque fenetre
+#This is the nimber of paquets of the time window which change according to processing type
+$ECHO "DEBUG (${scriptName}): START switch on $typeofproc"
+#
+case "$typeofproc" in
+    GAIN)
+      $ECHO "process signal files for GAIN phase"
+      ingain=""
+      freqfilter="-freqfilter -"
+      nwinmean="1"
+      tspwin="5120"
+      dirName="Off"
+      cyclebasename="gaincycle"
+      indexStartHour="6"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId=`expr  ${#cycleArray[*]} / 2`
+      firstCycleId=`expr ${firstCycleId} - 1`
+      lastp1CycleId=`expr ${firstCycleId} + 1`
+      ;;
+    ON)
+      $ECHO "process signal files for ON phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="5"
+      tspwin="5120"
+      dirName="On"
+      cyclebasename="datacycle"
+      indexStartHour="4"
+      timeShift="3"
+      timeDuration="30"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    OFF)
+      $ECHO "process signal files for OFF phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="5"
+      tspwin="5120"
+      dirName="Off"
+      cyclebasename="datacycle"
+      indexStartHour="8"
+      timeShift="3"
+      timeDuration="30"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    CALIBON)
+      $ECHO "process signal files for CALIBRATION ON phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="1"
+      tspwin="1024"
+      dirName="On"
+      cyclebasename="calibcycle"
+      indexStartHour="2"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    CALIBOFF)
+      $ECHO "process signal files for CALIBRATION OFF phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      nwinmean="1"
+      tspwin="1024"
+      dirName="Off"
+      cyclebasename="calibcycle"
+      indexStartHour="6"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    *)
+    $ECHO "FATAL (${scriptName}): type of processing"
+    usage
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+    ;;
+esac
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+#
+#Compute time windows (Warning usage of GMT date)
+#
+$ECHO "DEBUG (${scriptName}): START compute time windows"
+OUT=./baotiming.$$
+${AWK} -v datesys=${DATE} -v type=${indexStartHour} -v shift=${timeShift} '
+BEGIN {FS=","}
+{
+  phaseStart=$type
+  cmd=sprintf("%s +%%s -d  \"%s\" ",datesys,phaseStart);
+  (cmd | getline phstart);
+  close(cmd);
+  newstart = phstart+shift;
+  cmd=sprintf("%s +%%T -d \"1970-01-01 %s sec GMT\"",datesys,newstart);
+  (cmd | getline newStart);
+  close(cmd);
+  print $1,newStart,phaseStart;
+}
+'  ${scaFileSummarized} > ${OUT}
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+$ECHO "DEBUG (${scriptName}): START cat ${OUT}"
+$CAT ${OUT}
+$ECHO "DEBUG (${scriptName}): END cat ${OUT}"
+#
+$ECHO "DEBUG (${scriptName}): START filling cylceArrayBis timeArray"
+#
+declare -a cycleArrayBis
+declare -a timeArray
+$ECHO "DEBUG (${scriptName}): IFS value between<>: <$IFS>"
+IFS=$DefaultIFS
+#select the selected cycles
+idtofind="0"
+while read v1 v2 v3
+do
+ if [ ${idtofind} -lt ${#cycleArray[*]} ]; then
+    if [ ${v1} = ${cycleArray[${idtofind}]} ]; then
+	cycleArrayBis[${#cycleArrayBis[*]}]=$v1
+	timeArray[${#timeArray[*]}]=$v2
+	idtofind=`expr ${idtofind} + 1`
+    fi
+ fi
+done < ${OUT}
+$ECHO "DEBUG (${scriptName}): number of elements ${#cycleArrayBis[*]} ${#timeArray[*]}"
+$ECHO "DEBUG (${scriptName}): END"
+
+
+#########################
+# specmfib exe
+#########################
+prg="/afs/in2p3.fr/throng/baoradio/Library/Progs/TAcq/Objs/specmfib"
+if [ ! -e $prg ]; then
+    $ECHO "FATAL (${scriptName}): $prg not found"
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+fi
+
+
+dirFiberfile=${iojobpath}/${localpath}
+
+curDir=`pwd`
+#
+$ECHO "DEBUG (${scriptName}): in principle $dirFiberfile == $curDir "
+#
+#force single channel
+forceSingle="-singlechan"
+#debug
+prtlevel="1"
+
+
+#Loop on cycles to write batch script and send to batch queue
+##
+$ECHO "DEBUG (${scriptName}): START loop on cycles ${firstCycleId} to ${lastp1CycleId}"
+
+for ((i=${firstCycleId};i<${lastp1CycleId};i++)); do
+#
+$ECHO "DEBUG (${scriptName}): process cycle $i"
+#time window selection
+    tmproc="-tmproc ${timeArray[${i}]},${timeDuration}"
+#cycle <name><number>
+    cycle="${cyclebasename}${cycleArray[${i}]}"
+#signal files selection
+    infiles="${filesArray[${i}]}"
+#
+#Prepare BQS parameters: MEMORY, CPU...#
+    infilesArray=( `$ECHO $infiles | $AWK 'BEGIN {FS=","} {for(i=1;i<NF;i++)print $i;}'` )
+    IFS='
+    '
+    infilesArray=( $( printf "%s\n" "${infilesArray[@]}" | awk 'x[$0]++ == 0' ) )
+    IFS=$DefaultIFS
+    firstFile=${infilesArray[0]}
+    lastFile=${infilesArray[1]}
+#compute Scratch Size MB (400MB per signal files), CPU Time
+    nFiles=`expr ${lastFile} - ${firstFile} + 1`
+# Fiber 1 & Fiber 2 => double the number of files
+    nFiles=`expr ${nFiles} \* 2`
+#add number of spectra generated (cf. (Nbrepaquets_per_files * nFiles /tspwin) * 100KB
+    nSpectra=`expr ${nFiles} \* 25600 /  ${tspwin}`
+#Guess formula to reserved the amount of space needed in scratch
+    scratchSize=`expr ${nFiles} \* 500 + ${nSpectra} \* 200 / 1000 + 5`
+# 60sec/signal_file * empirical_conversion_factor (=50)
+    cpu=`expr ${nFiles} \* 60 \* 50`
+#Virtual Memory (MB) Nb: at CCIN2P3 the reasonable max is 1GB
+    virtualmem=1000
+#Job and batch names
+    jobBatchName="${sourceRadio}-${dateDAQ}-${typeofproc}-cycle${cycleArray[${i}]}"
+    jobScriptName="${jobBatchName}.sh"
+    jobLogName="${jobBatchName}.log"
+#
+$ECHO "DEBUG (${scriptName}): start writing ${jobScriptName}"
+#
+#
+#write the script to be send to Batch
+#
+    $CAT > ${jobScriptName} <<EOF
+#!/bin/sh -xvf
+#    jobBatchName=$jobBatchName
+#    jobScriptName=$jobScriptName
+#    jobLogName=$jobLogName
+    scriptName=$jobScriptName
+
+    . /afs/in2p3.fr/home/throng/baoradio/setup.sh
+    . /usr/local/shared/bin/irods_env.sh -noverbose
+
+    iojobpath=\${TMPBATCH}
+    cd \${iojobpath}
+    localpath=${sourceRadio}/${dateDAQ}${srclower}
+    $MKDIR -p ./\${localpath}
+    cd ./\${localpath}
+
+    curDir=\`pwd\`
+#get gain file if necessary
+    if [ ${typeofproc} != "GAIN" ]; then
+	OUT2=./igetStatus.$$
+	iget /baoradio/data/AmasNancay/${localpath}/gain_${dateDAQ}_${srclower}.fits >\${OUT2} 2>&1
+	igetStatus=\`$GREP "^ERROR" \${OUT2}\`
+	if [ "<\${igetStatus}>" != "<>" ]; then
+	    $ECHO "FATAL (\${scriptName}): error while iget:"
+	    $ECHO \$igetStatus
+	    touch ${tmppublicpath}/${jobBatchName}.finished
+	    exit 1
+	fi
+        $RM \${OUT2}
+    fi    
+#get signals files
+    OUT3=./getsignals.$$
+    $RM -f ${tmppublicpath}/getsignalfiles.finished
+    ${scriptpath}/getsignalfiles.sh ${sourceRadio} ${dateDAQ} ${firstFile}  ${lastFile} > \${OUT3} 2>&1
+    while [ ! -f "${tmppublicpath}/getsignalfiles.finished" ]; do
+	$ECHO "INFO (\${scriptName}): waiting for ${tmppublicpath}/getsignalfiles.finished"
+	date +%T
+	sleep 30
+    done
+    $RM ${tmppublicpath}/getsignalfiles.finished
+
+    getSignals=\`$GREP "^FATAL" \${OUT3}\`
+    if [ "<\$getSignals>" != "<>" ]; then
+	$ECHO "FATAL (\${scriptName}): error while get signals"
+	$ECHO \$getSignals
+	touch ${tmppublicpath}/${jobBatchName}.finished
+	exit 1
+    fi
+    $RM \${OUT3}
+#location of the signal file
+    fibfile="\${curDir} 1,2"
+#
+    outDir=\${curDir}/${dirName}/${cycle}
+    $MKDIR -p \${outDir}
+
+#
+# where ami...
+#
+    $ECHO "We are here \`pwd\` to send the batch"
+    $LS -lrth
+    $FIND . -name "igetStatus.*" | $XARGS -i  $CAT {}
+    $LS ./Fiber1 | $WC -l
+    $LS ./Fiber2 | $WC -l
+    $LS -lR \${outDir}
+
+#Note: here act=gain means only that specmfib will use median-like algorithms (not only for gains...)
+    $prg -act gain $forceSingle $freqfilter  -prt $prtlevel -out \${outDir} -nmean $nwinmean $ingain $tmproc -tspwin ${tspwin},0,0 -in $infiles \${fibfile}
+
+    $ECHO    \${outDir}
+    $LS -lrt \${outDir}
+
+#save results to Irods (use option -f ONLY to force override)
+    iput -v -K -r \${curDir}/${dirName} /baoradio/data/AmasNancay/\${localpath}
+#
+#clean the signal files which can at the end of the day fill completly the scratch directory of the batch work/AmasNancayer
+    $RM -f \${iojobpath}/\${localpath}/Fiber1/signal*.fits
+    $RM -f \${iojobpath}/\${localpath}/Fiber2/signal*.fits
+    touch ${tmppublicpath}/${jobBatchName}.finished
+    exit 0
+EOF
+#
+#The EOF tag MUST BEGIN the line, this is the end of the script
+#
+    $ECHO "DEBUG (${scriptName}); START listing of script to be send to batch queue"
+    $CAT -v ${jobScriptName}
+    $ECHO "DEBUG (${scriptName}): END listing"
+#
+#send to BATCH
+#
+    $ECHO "DEBUG (${scriptName}): START send to batch listing"
+#
+    $RM -f ${tmppublicpath}/${jobBatchName}.finished
+
+##26/9/11 JEC use option -h to trig manualy the job on the queue...
+    qsub -h -l u_sps_baoradio,u_irods,T=${cpu},M=${virtualmem}MB,scratch=${scratchSize}MB,platform=LINUX -eo -o ${tmppublicpath}/${jobLogName} -N ${jobBatchName}  ${jobScriptName}
+
+#    while [ ! -f "${tmppublicpath}/${jobBatchName}.finished" ]; do
+#	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/${jobBatchName}.finished"
+#	date +%T
+#	sleep 600
+#    done
+#    $RM ${tmppublicpath}/${jobBatchName}.finished
+    $ECHO "DEBUG (${scriptName}): end send to batch listing"
+#end loop on cycles
+done
+#
+$ECHO "DEBUG (${scriptName}): END loop on cycles"
+#
+touch ${tmppublicpath}/proc_script.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/rawonoffmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/rawonoffmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/rawonoffmaker.sh	(revision 538)
@@ -0,0 +1,323 @@
+#!/bin/sh
+#Perform the ON-OFF analysis
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+#which source to analyse
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2;    shift;;
+      -date) dateSelected=$2;     shift;;
+      -sim)  simulationMode=On;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+#wakeup the NFS disk
+$LS -l ${tmppublicpath} > /dev/null
+#clean previous spurious files
+$RM -f ${tmppublicpath}/*.finished
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+
+#Nb: if we want to implement a loop on sources we have to take care of srclower...
+
+$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
+tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+IFS='
+' 
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+#
+# start a "fresh" session  
+  cd ${topDir}
+  $LS | $XARGS -i $RM -rf {}
+  
+  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+
+  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
+  $LS -lrt
+
+
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the gain & calibration files exist
+#
+  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
+  if [ ! ${gainDone} -gt 0 ]; then
+      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
+      continue
+  fi
+
+#
+  somethingTodo=1
+  input4dataOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n`  
+  input4dataOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n`  
+#
+  if [ "<${input4dataOff}>" != "<>" -a "<${input4dataOn}>" != "<>"  ]; then
+#look at the content of the datacycle directory as may be the previous batch job has crashed
+      tableauOff=( `$ECHO ${input4dataOff}` )
+      IFS='
+'
+      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOff=0
+      for j in ${tableauOff[@]}
+	do
+	nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
+      done
+#
+      tableauOn=( `$ECHO ${input4dataOn}` )
+      IFS='
+      '
+      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOn=0
+      for j in ${tableauOn[@]}
+	do
+	nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
+      done
+      diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
+      if [ ${diffOnOff} -lt 0 ]; then
+	  zero=0
+	  diffOnOff=`expr ${zero} - ${diffOnOff}` 
+      fi
+#allow files differences but non zero
+      if [  ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0  ]; then
+	  somethingTodo=0
+	  $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
+      else
+	  $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
+      fi
+#
+#look at consecutive cycles
+
+      rmDirectory=0
+
+      cycleIdOff=`$ECHO $input4dataOff | sed 's/datacycle//g'`
+      tableauOff=( `$ECHO ${cycleIdOff}` )
+      IFS='
+'
+      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+      
+      lastIdOff=`expr ${#tableauOff[*]} - 1`
+      ncycleOff=`expr ${tableauOff[${lastIdOff}]} - ${tableauOff[0]} + 1`
+      ncycleOffVerif=${#tableauOff[*]}
+      if [ "<${ncycleOff}>" != "<${ncycleOffVerif}>"  ]; then
+	  $ECHO "$sourceRadio $dateDAQ: There is a lack of OFF cycles in the sequence: ${tableauOff[@]}"
+	  rmDirectory=1
+	  somethingTodo=1
+      else
+	  $ECHO "$sourceRadio $dateDAQ: Ok at least consecutive OFF cycles: ${tableauOff[@]}"
+      fi
+
+      cycleIdOn=`$ECHO $input4dataOn | sed 's/datacycle//g'`
+      tableauOn=( `$ECHO ${cycleIdOn}` )
+      IFS='
+'
+      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+      
+      lastIdOn=`expr ${#tableauOn[*]} - 1`
+      ncycleOn=`expr ${tableauOn[${lastIdOn}]} - ${tableauOn[0]} + 1`
+      ncycleOnVerif=${#tableauOn[*]}
+      if [ "<${ncycleOn}>" != "<${ncycleOnVerif}>"  ]; then
+	  $ECHO "$sourceRadio $dateDAQ: There is a lack of ON cycles in the sequence: ${tableauOn[@]}"
+	  rmDirectory=1
+	  somethingTodo=1
+      else
+	  $ECHO "$sourceRadio $dateDAQ: Ok at least consecutive ON cycles:  ${tableauOn[@]}"
+      fi
+
+      if [ "<${cycleIdOff}>" != "<${cycleIdOn}>" ]; then
+	  $ECHO "$sourceRadio $dateDAQ: cycles seq. missmatch ON and OFF"
+	  rmDirectory=1
+	  somethingTodo=1	  
+      fi
+# 
+  fi 
+########
+  if [ ${somethingTodo} -eq 1 ]; then
+#
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ANALOFF"
+
+      if [ ${rmDirectory} -eq 1 ]; then
+#delete datacycle Off
+	  dirToRmOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}'`  
+	  tableauOff=( `$ECHO ${dirToRmOff}` )
+	  IFS='
+'
+	  tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+	  IFS=$DefaultIFS
+	  for j in ${tableauOff[@]}
+	    do
+	    irm -f -r -v ${j}
+	  done
+#delete datacycle On
+	  dirToRmOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}'`  
+	  tableauOn=( `$ECHO ${dirToRmOn}` )
+	  IFS='
+'
+	  tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+	  IFS=$DefaultIFS
+	  for j in ${tableauOn[@]}
+	    do
+	    irm -f -r -v ${j}
+	  done
+      fi
+	  
+      $RM -f ${tmppublicpath}/proc_script.finished
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ OFF"
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ON"
+      $RM ${tmppublicpath}/proc_script.finished
+      
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $RM ${tmppublicpath}/proc_script.finished
+#Todo see if there is errors in the log file before copy
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ ON"
+  fi
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/reducing.cc
===================================================================
--- /BAORadio/AmasNancay/v2/reducing.cc	(revision 538)
+++ /BAORadio/AmasNancay/v2/reducing.cc	(revision 538)
@@ -0,0 +1,148 @@
+#include "machdefs.h"
+
+//---- System et stdc++ include files
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <ctype.h>
+#include <string.h>
+#include <iostream>
+#include <fstream>
+#include <complex>
+
+#include <typeinfo>
+#include <string>
+#include <vector>
+#include <map>
+#include <functional>
+#include <list>
+
+//---- Sophya include files
+#include "sopnamsp.h"
+#include "basetools.h"
+#include "systools.h"
+#include "sutils.h"
+#include "ntools.h"
+#include "array.h"
+#include "histats.h"
+
+//---- Spiapp include files
+#include "nobjmgr.h"
+#include "servnobjm.h"
+
+//---- Include files from additionnal modules
+
+//---- function to compare bits on double 
+int_8 BitCmp64(double v,int_8 flg) 
+{return ((int_8)((v<0.) ? v-0.1 : v+0.1))&flg;} 
+
+//---- function for Adding and displaying Objects  
+void Keep_Object(AnyDataObj & obj, string const & nom) 
+{ 
+  string name = nom; 
+  NamedObjMgr om; 
+  if (om.GetObj(name)) 
+    cerr << "KeepObj()/Warning Already kept object " << endl; 
+  else om.AddObj(obj, name); 
+} 
+
+void Display_Object(AnyDataObj & obj, string const & opt, string const & nom) 
+ { 
+  string name = nom; 
+  NamedObjMgr om; 
+  if (!om.GetObj(name)) 
+    om.AddObj(obj, name); 
+  om.DisplayObj(name, opt); 
+} 
+
+//---- function for getting and setting ObjectManager variables  
+void Set_ObjMgrVar(MuTyV v, string const & nom) 
+{ 
+  NamedObjMgr om; 
+  om.SetVar(nom, (string)v); 
+} 
+
+MuTyV Get_ObjMgrVar(const char * nom) 
+{ 
+  string name = nom; NamedObjMgr om; 
+  MuTyV v = om.GetVar(name); 
+  return v; 
+} 
+
+//---- Macro for Objects and variables saving
+#define KeepObj(obj) Keep_Object(obj, #obj)
+#define GetOMVar(var) Get_ObjMgrVar( #var )
+#define SetOMVar(var) Set_ObjMgrVar(var, #var )
+
+//---- Macro Displaying objects and command execution
+#define DispObj(obj, att) Display_Object(obj, att, #obj); 
+
+#define ExecCmd(cmd) srvo.ExecuteCommand(cmd); 
+
+
+
+//-------------------------------------------------//
+//----------------- User Functions ----------------//
+//-------------------------------------------------//
+
+extern "C" {
+  int doreduc( vector<string>& args );
+}
+
+int doreduc( vector<string>& args )
+{
+// Some definitions to help using spiapp;
+NamedObjMgr omg;
+Services2NObjMgr& srvo = *omg.GetServiceObj();
+
+//-------------- Object List --------------
+//Number of objects = 2
+string ___nomobj;
+
+___nomobj = "specONOFFv0";
+TVector< r_4 > * ___specONOFFv0 = dynamic_cast< TVector< r_4 >  * >(omg.GetObj(___nomobj));
+if(___specONOFFv0==NULL) throw NullPtrError("CxxExecutor::PutObject: Non existing object specONOFFv0... please update file");
+TVector< r_4 > & specONOFFv0 = (*___specONOFFv0);
+
+___nomobj = "specONOFFv1";
+TVector< r_4 > * ___specONOFFv1 = dynamic_cast< TVector< r_4 >  * >(omg.GetObj(___nomobj));
+if(___specONOFFv1==NULL) throw NullPtrError("CxxExecutor::PutObject: Non existing object specONOFFv1... please update file");
+TVector< r_4 > & specONOFFv1 = (*___specONOFFv1);
+
+
+
+//--------------------------------------------//
+//----------------- User Code ----------------//
+//--------------------------------------------//
+
+ sa_size_t NUMBER_OF_CHANNELS = 2;
+ sa_size_t NUMBER_OF_FREQ = 8192;
+ TMatrix<r_4> specMtxInPut(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+ specMtxInPut.Row(0)=specONOFFv0;
+ specMtxInPut.Row(1)=specONOFFv1;
+
+ //JEC 22/9/11 Mean & Sigma in 32-bins size START
+ sa_size_t nSliceFreq = 32; //TODO: put as an input parameter option ?
+ sa_size_t deltaFreq =  NUMBER_OF_FREQ/nSliceFreq;
+ //JEC 22/9/11 Mean & Sigma in 32-bins size END
+ 
+ TMatrix<r_4> reducedMeanDiffOnOff(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
+ TMatrix<r_4> reducedSigmaDiffOnOff(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
+ for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
+   sa_size_t freqLow= iSlice*deltaFreq;
+   sa_size_t freqHigh= freqLow + deltaFreq -1;
+   for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
+     TVector<r_4> reducedRow;
+     reducedRow = specMtxInPut.SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
+     double mean; 
+     double sigma;
+     MeanSigma(reducedRow,mean,sigma);
+     reducedMeanDiffOnOff(iCh,iSlice) = mean;
+     reducedSigmaDiffOnOff(iCh,iSlice) = sigma;
+   }
+ } 
+ KeepObj(reducedMeanDiffOnOff);
+ KeepObj(reducedSigmaDiffOnOff);
+
+return 0;
+}
Index: /BAORadio/AmasNancay/v2/sca.sh
===================================================================
--- /BAORadio/AmasNancay/v2/sca.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/sca.sh	(revision 538)
@@ -0,0 +1,177 @@
+#!/bin/sh
+#
+#Script sh d'extraction de fichier SCA des sequances Diodes a Bruit (DAB) 
+#et Data des cycles ON/OFF
+#Bash script that extract from SCA files the DAB/DATA phases in ON/OFF cycles
+#It is supposed that the sequence is: DAB1 DATA1 DAB2 DATA2
+#and that the DAB1 and DAB2 have equal number of subsequences
+# Auteur: JE Campagne (LAL Orsay)
+# Creation: 11 Avril 2011
+# Revision: 12 Avril 2011
+#  o 12/04/11: mv date format to %Y-%m-%d %T for further treatment by 'date' 
+#              system function 
+
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc 
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/sca.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+if [ ! $# = 1 ]
+    then
+    $ECHO "FATAL  (${scriptName}): usage: ./sca.sh <file>"
+    touch ${tmppublicpath}/sca.finished
+    exit 1
+fi
+
+
+fullfile=$1
+if [ ! -e $fullfile ] 
+    then
+    $ECHO "FATAL (${scriptName}): $fullfile not found: FATAL"
+    touch ${tmppublicpath}/sca.finished
+    exit 1
+fi
+
+#The output file would be in "fullprocfile"
+filename=$(basename $fullfile)
+#extension=${filename##*.}
+filename=${filename%.*}
+extension=".sum"
+fullprocfile=${filename}${extension}
+#create empty output file
+$RM -f $fullprocfile
+$TOUCH $fullprocfile
+
+
+#extract the PERI lines valid for DAB and DATA
+OUT1=./tmp1.$$
+$AWK '
+$1=="PERI" {
+   print "CYCLE",$6,$2,$3,$7,$8
+  }
+' $fullfile > $OUT1
+
+
+#Sequence of the cycle numbers (perform a unique filtering)
+tableau=( `cat $OUT1 | $AWK '{print $2}'` )
+IFS='
+'
+tableau=( $( printf "%s\n" "${tableau[@]}" | awk 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+#scan each cycle
+OUT2=./tmp2.$$
+for i in ${tableau[@]}
+do
+#extract the DAB calibration phases. Hypothesis: 2 identical sequences 
+  CALIB=./tmp_cycle_${i}_calib.$$
+  $AWK -v cycle=$i '$2==cycle && $5=="-1" {print $0} ' $OUT1 > $CALIB
+  nlines=`$WC $CALIB | $AWK '{print $1}'`
+  start1=1
+  let "end1=nlines/2"
+  let "start2=end1+1"
+  end2=$nlines
+
+#date Start/End of DAB1 
+  $SED -e "${start1}!d" $CALIB | $AWK -v cycle=$i 'BEGIN {ORS=","} {
+    split($4,b,".");   
+    print cycle "," $3,b[1]}' >> ${OUT2}
+  $SED -e "${end1}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
+    split($4,b,".");   
+    print $3,b[1]}' >> ${OUT2}
+
+
+#date Start/End of DATA1: use date system function to add the duration to
+#compute the End,cf. End = Start + duration 
+  $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="1" ) {
+    split($3,a,"/"); 
+    split($4,b,".");   
+    dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
+    duree=$6 " sec";
+    cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
+   (cmd | getline dateEnd);
+    close(cmd);  
+    ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
+    ORS=","; print dateEnd;
+  } ' $OUT1 >> ${OUT2}
+ 
+#same as DAB1 for DAB2
+  $SED -e "${start2}!d" $CALIB | $AWK 'BEGIN {ORS=","} {
+    split($4,b,".");   
+    print $3,b[1]}' >> ${OUT2}
+  $SED -e "${end2}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
+     split($4,b,".");   
+     print $3,b[1]}' >> ${OUT2}
+#same as DATA1 for DATA2
+  $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="2" ) {
+    split($3,a,"/"); 
+    split($4,b,".")   
+    dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
+    duree=$6 " sec";
+    cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
+   (cmd | getline dateEnd);
+    close(cmd);  
+    ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
+    ORS="\n"; print  dateEnd;
+  } ' $OUT1 >> ${OUT2}
+#clean
+  $RM $CALIB
+done
+
+#transform all the date/time into %Y-%m-%d %T
+$AWK -v datesys=$DATE ' 
+BEGIN { FS=","; OFS="," }
+{
+  for (i=2;i<=NF;i++) {
+    split($i,a,"/");
+    newdate=a[2]"/"a[1]"/"a[3];
+    cmd=sprintf("%s --date \"%s\" \"+%%Y-%%m-%%d %%T\" ",datesys,newdate);
+    (cmd | getline newdate);
+    close(cmd);
+    $i=newdate;
+  } 
+  print $0;
+}' ${OUT2} > ${fullprocfile}
+
+
+#clean
+$RM $OUT1
+$RM $OUT2
+
+touch ${tmppublicpath}/sca.finished
+exit 0
+
+
+
Index: /BAORadio/AmasNancay/v2/scaIsOk.sh
===================================================================
--- /BAORadio/AmasNancay/v2/scaIsOk.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/scaIsOk.sh	(revision 538)
@@ -0,0 +1,87 @@
+#!/bin/sh
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+MKDIR=/bin/mkdir
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#which source to analyse
+sourceRadio=$1
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/scaIsOk.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+OUT1=${publicpath}/baodaqstatus-2011-09-11.txt
+
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/scaIsOk.finished
+    exit 1
+fi
+
+if [ ! "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "You have selected sourceRadio = $sourceRadio"
+    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+else
+    tableau=( `$CAT ${OUT1} | $GREP -i "baoradio" | $AWK '( NF==4 ) { print $2 }'`  )
+fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+for i in ${tableau[@]}
+do
+#scrutinize irods directory
+#Nb: we do not care here if ILS returns non unique instance of a file
+  scafile=`ils -l $i 2>&1 | $GREP -i "sca[0-9][0-9]*\.[0-9][0-9]*" | $AWK '{print $NF}'`
+  if [ "<${scafile}>" = "<>" ]; then
+      $ECHO "INFO (${scriptName}): Cannot download a SCA file for `basename $i`"
+  fi
+done
Index: /BAORadio/AmasNancay/v2/scaextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/scaextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/scaextractor.sh	(revision 538)
@@ -0,0 +1,145 @@
+#!/bin/sh -xvf
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+MKDIR=/bin/mkdir
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#which source to analyse
+sourceRadio=$1
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/scaextractor.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+#    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+#    $RM -f ${tmppublicpath}/statusdaq.finished
+#    ${scriptpath}/statusdaq.sh > ${OUT1}
+#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+#	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/statusdaq.finished"
+#	date +%T
+#	sleep 30 
+#    done
+#    $RM ${tmppublicpath}/statusdaq.finished
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/scaextractor.finished
+    exit 1
+fi
+
+if [ ! "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "You have selected sourceRadio = $sourceRadio"
+    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+else
+    tableau=( `$CAT ${OUT1} | $AWK'( NF==4 ) { print $2 }'`  )
+fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+for i in ${tableau[@]}
+do
+OUT2=./baodir.$$
+#scrutinize irods directory
+#Nb: we do not care here if ILS returns non unique instance of a file
+  ils -l $i 2>&1 | $GREP -i "sca" > ${OUT2}
+  $CAT ${OUT2}
+#test if the SCA file has been already processed
+  scatrans=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $NF}'`
+#test if the file is empty due to previous processing dysfunction
+  emptyfile=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $4}'`
+#  echo "scrutinize directory <$i>: [$nblines] [$scatrans]" 
+  if [ "<${scatrans}>" = "<>" -o "<${emptyfile}>" = "<0>" ]; then
+      $ECHO "$i may be non prepared for analysis..."
+      scafile=`$GREP -i "sca[0-9][0-9]*\.[0-9][0-9]*" ${OUT2} | $AWK '{print $NF}'`
+      if [ "<${scafile}>" = "<>" ]; then
+	  $ECHO "WARNING (${scriptName}): Cannot download a SCA file for $i"
+      else
+#get sca file
+        iget -r -f -v -K ${i}/${scafile} ${iojobpath}
+#extract summary (sum extension)
+	$RM -f ${tmppublicpath}/sca.finished
+	${scriptpath}/sca.sh ${scafile}
+	while [ ! -e "${tmppublicpath}/sca.finished" ]; do
+	    $ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/sca.finished"
+	    date +%T
+	    sleep 30
+	done
+	$ECHO "INFO (${scriptName}): sca.sh has finished"
+	$RM ${tmppublicpath}/sca.finished
+#TODO see if there is errors...
+        scaFileName=$(basename $scafile)
+	scaFileName=${scaFileName%.*}
+#transform the date (sum.trans extension)
+	$RM -f ${tmppublicpath}/transdate.finished
+        ${scriptpath}/transdate.sh ${scaFileName}.sum
+	while [ ! -e "${tmppublicpath}/transdate.finished" ]; do
+	    $ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/transdate.finished"
+	    date +%T
+	    sleep 30
+	done
+	$ECHO "INFO (${scriptName}): transdate.sh  has finished"
+	$RM ${tmppublicpath}/transdate.finished
+#TODO see if there is errors...	
+#put back into Irod
+#	$ECHO "INFO (${scriptName}): where am i: `pwd`"
+#	$LS -lrt
+#	$ECHO "INFO (${scriptName}): see up directory"
+#	$LS -lrt ..
+	iput -f -v -K ${iojobpath}/${scaFileName}.sum $i
+	iput -f -v -K ${iojobpath}/${scaFileName}.sum.trans $i
+      fi
+  fi
+$RM ${OUT2}
+done
+
+
+touch ${tmppublicpath}/scaextractor.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/select.sh
===================================================================
--- /BAORadio/AmasNancay/v2/select.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/select.sh	(revision 538)
@@ -0,0 +1,227 @@
+#!/bin/sh
+
+#hypothesis: ON = data1 phase, OFF data2 phase
+
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+FIND=/usr/bin/find
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+MKDIR=/bin/mkdir
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+if [ ! $# = 3 ]; then
+  $ECHO "usage: ${scriptName} <scaXYZ.sum> <signalXYZDirectory> <type of phase:ON|OFF|CALIBON|CALIBOFF>"
+  touch ${tmppublicpath}/select.finished
+  exit 0
+fi
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+OUT1=./selectFits.$$
+
+#inputs x-chexk sca file
+inScaFile=$1
+if [ ! -e $inScaFile ]; then
+  $ECHO "FATAL (${scriptName}): $inScaFile not found"
+  touch ${tmppublicpath}/select.finished
+  exit 1
+fi
+
+#Where to look at
+inFiberDir=$2
+
+inTypeOfPhase=$3
+case "$inTypeOfPhase" in
+    ON) 
+    $ECHO "INFO (${scriptName}): filter signal files for ON phases";;
+    OFF) 
+    $ECHO "INFO (${scriptName}): filter signal files for OFF phases";;
+    CALIBON) 
+    $ECHO "INFO (${scriptName}): filter signal files for CALIBRATION ON phases";;
+    CALIBOFF) 
+    $ECHO "INFO (${scriptName}): filter signal files for CALIBRATION OFF phases";;
+    GAIN) 
+    $ECHO "INFO (${scriptName}): filter signal files for GAIN = CALIB OFF phases";;
+    *) 
+    $ECHO "FATAL (${scriptName}): use ON|OFF|CALIBON|CALIBOFF phase tags."
+    touch ${tmppublicpath}/select.finished
+    exit 1
+    ;;
+esac
+
+
+#################
+#fill the arrays association header file with start/end date&time 
+# arrays
+declare -a sigStart
+declare -a sigEnd
+declare -a sigFile
+
+for fullfile in $( $FIND $inFiberDir -name "header*" | $XARGS -i basename {} | $SORT -k1.7n)
+  do
+  filename=$(basename $fullfile)
+  extension=${filename##*.}
+  filename=${filename%.*}
+  ffn=${inFiberDir}/${filename}.${extension}
+#  echo "process $ffn"
+  while read -n80 L
+    do
+    [ "${L::3}" = "END" ] && break
+    echo $L
+  done < $ffn > $OUT1
+  
+  strStart=`$GREP TMSTART $OUT1 | $AWK '{split($0,a,"/"); split(a[1],b,"="); print b[2];}' | $SED 's/^[ \t]*//;s/[ \t]*$//'  | $SED "s/^\([\"']\)\(.*\)\1\$/\2/g" | $AWK '{split($0,a,"T"); print a[1]" "a[2];}' `
+  
+  strEnd=`$GREP TMEND $OUT1 | $AWK '{split($0,a,"/"); split(a[1],b,"="); print b[2];}' |  $SED 's/^[ \t]*//;s/[ \t]*$//'  | $SED "s/^\([\"']\)\(.*\)\1\$/\2/g" | $AWK '{split($0,a,"T"); print a[1]" "a[2];}' `
+  
+  if [[ -n "$strStart"  &&  -n "$strEnd" ]]; then
+      timeStart=`${DATE} +%s -d "${strStart}"`
+      timeEnd=`${DATE} +%s -d "${strEnd}"`
+#      echo "$fullfile start $strStart   <$timeStart>, end  $strEnd <$timeEnd>"
+      sigStart[${#sigStart[*]}]=$timeStart
+      sigEnd[${#sigEnd[*]}]=$timeEnd
+      sigFile[${#sigFile[*]}]=$filename
+  else
+      $ECHO "WARNING (${scriptName}): $ffn PATHOLOGIQUE"
+  fi
+done
+
+############
+#scan all the cycles 
+#  calib1start=$2; calib1end=$3; 
+#  data1start=$4; data1end=$5;
+#  calib2start=$6; calib2end=$7; 
+#  data2start=$8; data2end=$9;
+#to pass to awk: use a "," as field separator
+awk_sigStart=$( printf "%s," "${sigStart[@]}" )
+awk_sigEnd=$( printf "%s," "${sigEnd[@]}" )
+awk_sigFile=$( printf "%s," "${sigFile[@]}" )
+
+#attention a la derniere "," pour le calcul de nfile
+OUT1=./tmpselect1.$$
+#selection d'une seul cycle pour debug 
+$AWK -v datesys=$DATE -v type=$inTypeOfPhase -v str_sigStart="$awk_sigStart"  -v str_sigEnd="$awk_sigEnd"  -v str_sigFile="$awk_sigFile" '
+BEGIN { 
+  FS=",";
+  split(str_sigStart,a_sigStart,",");
+  split(str_sigEnd,a_sigEnd,",");
+  nfile=split(str_sigFile,a_sigFile,",");
+  delete a_sigFile[nfile];
+  delete a_sigStart[nfile];
+  delete a_sigEnd[nfile];
+  nfile--;  
+
+}
+
+ {
+  cycle=$1; 
+
+  if ( type == "ON" ) {
+    phaseStart=$4; phaseEnd=$5;
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+    # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=4;
+    deltaEndSafety=-4;
+  }
+  else if ( type == "OFF" ) {
+    phaseStart=$8; phaseEnd=$9;
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+    # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=4;
+    deltaEndSafety=-4;
+  } 
+  else if ( type == "CALIBON" ) {
+    phaseStart=$2; phaseEnd=$3;
+    print "Phase: ",type," cycle: ",cycle,": <",phaseStart,">:<",phaseEnd,">";
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+#    print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=-6;
+    deltaEndSafety=6;
+  } 
+  else if ( (type == "CALIBOFF") || (type == "GAIN") ) {
+    phaseStart=$6; phaseEnd=$7;
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+    # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=-6;
+    deltaEndSafety=6;
+  } 
+  else { 
+    print "FATAL: ",type," NOT YET SUPPORTED"; exit 1;
+  }
+
+  for (i=1; i<=nfile; i++) {
+    diffstart = a_sigStart[i] - phstart;
+    diffend   = a_sigEnd[i] - phend;
+#     print "file ",i," start ",a_sigStart[i],": diff <" diffstart ">:<" diffend ">";
+    if ( diffstart >= deltaStartSafety && diffend <= deltaEndSafety )
+       print "cycle ",cycle," file ",i-1," accepted ",a_sigFile[i];
+  }  
+     
+
+}' $inScaFile > ${OUT1}
+
+OUT2=./tmpselect2.$$
+$AWK '
+BEGIN { max=0; min=999999; }; 
+
+($1=="cycle") { table[$2] = table[$2]","$4; if($2>max){max=$2}; if($2<min){min=$2} }; 
+
+END { for (j=min;j<=max;j++) {print j,table[j];} }
+' ${OUT1} > ${OUT2}
+
+$AWK '
+BEGIN { FS=","}
+{
+ print "#"$1,$2","$NF",1";
+}
+' ${OUT2}
+
+$RM $OUT1
+$RM $OUT2
+
+touch ${tmppublicpath}/select.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/statusCycles.sh
===================================================================
--- /BAORadio/AmasNancay/v2/statusCycles.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/statusCycles.sh	(revision 538)
@@ -0,0 +1,107 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+TR=/usr/bin/tr
+FIND=/usr/bin/find
+PRINTF=/usr/bin/printf
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+QSUB=qsub
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+trim() { echo $1; }
+
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/statusdaq.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  OUT2=./baodir.$$
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  for j in ${listedate[@]}
+  do
+    ils -l $j 2>&1 | $GREP -i "sca" > ${OUT2}
+#test if the SCA file has been already processed
+    scatrans=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $NF}'`
+#test if the file is empty due to previous processing dysfunction
+    emptyfile=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $4}'`
+#  echo "scrutinize directory <$i>: [$nblines] [$scatrans]" 
+    if [ "<${scatrans}>" != "<>" ]; then
+	scafile=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $NF}'`
+	if [ "<${scafile}>" = "<>" ]; then
+	    $ECHO "WARNING (${scriptName}): Cannot download a SCA file for $j"
+	else
+#get sca file
+	    iget -r -f -K ${j}/${scafile}
+	    ncycle=`$CAT ${scafile} | $WC -l`
+	    $RM ${scafile}
+	    $ECHO ">>> $j ---> ${ncycle} cycles"
+	fi
+    else
+	$ECHO ">>> $j ---> NO SCA summary"
+    fi
+  done 
+  $RM ${OUT2}
+done
+
+
+
Index: /BAORadio/AmasNancay/v2/statuscalib.sh
===================================================================
--- /BAORadio/AmasNancay/v2/statuscalib.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/statuscalib.sh	(revision 538)
@@ -0,0 +1,138 @@
+#!/bin/sh
+#Status de l'etat de la production des fichiers INPUT de calibrations
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+listeSources=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+listeSources=( $( $PRINTF "%s\n" "${listeSources[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+$RM $OUT2
+
+for i in ${listeSources[@]}
+do
+#nb: $i contains the full path
+  $ECHO "source ${i}"
+  sourceName=`basename ${i}`
+  listeDates=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listeDates=( $( $PRINTF "%s\n" "${listeDates[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  for j in ${listeDates[@]}
+  do
+#nb: $j contains the full path
+#  $ECHO "run ${j}"
+#
+#Off directory
+#
+    dateField=`basename ${j}`
+    sourceDate="${sourceName}-${dateField}"
+#look if final calibration files exist
+    calibOk=`ils ${j} | $GREP -i "calib_" | $WC -l`
+
+    offDirExist=`ils ${j} | $GREP -i "Off" | $WC -l`
+    if [ ${offDirExist} != 0 ]; then
+	listeCyclesOff=( `ils ${j}/Off | $GREP -i "calibcycle" | $AWK '($1=="C-"){print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+	IFS='
+  '
+	listeCyclesOff=( $( $PRINTF "%s\n" "${listeCyclesOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+	IFS=$DefaultIFS
+	
+	if [ ${#listeCyclesOff[*]} != 0 ]; then    
+           for c in ${listeCyclesOff[@]}
+	   do
+#nb: $c has the format of calibcycle<number>
+	     nFiles=`ils ${j}/Off/${c} | $GREP -i "medfiltmtx" | $WC -l`
+	     if [ ${nFiles} != 0 ]; then
+		 $ECHO "${sourceDate}/Off/${c}: ${nFiles} spectra (${calibOk} calib)"
+	     else
+		 $ECHO "${sourceDate}/Off/${c}: Calibration failed"
+	     fi
+#end of loop on cycles 
+	   done
+	else
+	    $ECHO "${sourceDate} : No Calibration done for Off Diectory"
+	fi
+#end of Off exist
+    else
+	$ECHO "${sourceDate} : No Off Diectory"
+    fi
+#
+#On directory
+#
+    dateField=`basename ${j}`
+    sourceDate="${sourceName}-${dateField}"
+#look if final calibration files exist
+    calibOk=`ils ${j} | $GREP -i "calib_" | $WC -l`
+    onDirExist=`ils ${j} | $GREP -i "On" | $WC -l`
+    if [ ${onDirExist} != 0 ]; then
+	listeCyclesOn=( `ils ${j}/On | $GREP -i "calibcycle" | $AWK '($1=="C-"){print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+	IFS='
+  '
+	listeCyclesOn=( $( $PRINTF "%s\n" "${listeCyclesOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+	IFS=$DefaultIFS
+	
+	if [ ${#listeCyclesOn[*]} != 0 ]; then    
+           for c in ${listeCyclesOn[@]}
+	   do
+#nb: $c has the format of calibcycle<number>
+	     nFiles=`ils ${j}/On/${c} | $GREP -i "medfiltmtx" | $WC -l`
+	     if [ ${nFiles} != 0 ]; then
+		 $ECHO "${sourceDate}/On/${c}: ${nFiles} spectra (${calibOk} calib)"
+	     else
+		 $ECHO "${sourceDate}/On/${c}: Calibration failed"
+	     fi
+#end of loop on cycles 
+	   done
+	else
+	    $ECHO "${sourceDate} : No Calibration done for On Diectory"
+	fi
+#end of On exist
+    else
+	$ECHO "${sourceDate} : No On Diectory"
+    fi
+#end of loop on dates
+  done 
+#end of loop on sources
+done
Index: /BAORadio/AmasNancay/v2/statusdaq.sh
===================================================================
--- /BAORadio/AmasNancay/v2/statusdaq.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/statusdaq.sh	(revision 538)
@@ -0,0 +1,88 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+LS=/bin/ls
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/statusdaq.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  ntotfiles=0
+  for j in ${listedate[@]}
+  do
+#Nb: care should be taken to the possible instances of a file returned by ILS
+    listOfsig=( `ils ${j}/Fiber1 | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+  '
+    listOfsig=( $( $PRINTF "%s\n" "${listOfsig[@]}" | $AWK 'x[$0]++ == 0' ) )
+    IFS=$DefaultIFS
+    nfiles=${#listOfsig[@]}
+    $ECHO ">>> $j ---> $nfiles"
+    let "ntotfiles=ntotfiles+nfiles"
+  done 
+  $ECHO "total files = $ntotfiles"
+
+done
+
+touch ${tmppublicpath}/statusdaq.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/statusgain.sh
===================================================================
--- /BAORadio/AmasNancay/v2/statusgain.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/statusgain.sh	(revision 538)
@@ -0,0 +1,70 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  for j in ${listedate[@]}
+  do
+    gainOk=`ils ${j} | $GREP -i "gain_" | $WC -l`
+    if [ ${gainOk} -gt 0 ]; then
+	$ECHO ">>> $j ---> GAIN Done (${gainOk})"
+    else
+	$ECHO ">>> $j ---> GAIN To be computed"
+    fi
+  done 
+done
Index: /BAORadio/AmasNancay/v2/statusheader.sh
===================================================================
--- /BAORadio/AmasNancay/v2/statusheader.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/statusheader.sh	(revision 538)
@@ -0,0 +1,94 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+
+  ntotfiles=0
+  for j in ${listedate[@]}
+  do
+#Fiber 1
+    listOfsig1=( `ils ${j}/Fiber1 | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+  '
+    listOfsig1=( $( $PRINTF "%s\n" "${listOfsig1[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesSIG1=${#listOfsig1[@]}
+    listOfhdr1=( `ils ${j}/Fiber1 | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+    '
+    listOfhdr1=( $( $PRINTF "%s\n" "${listOfhdr1[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesHDR1=${#listOfhdr1[@]}
+
+#Fiber 2
+    listOfsig2=( `ils ${j}/Fiber2 | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+  '
+    listOfsig2=( $( $PRINTF "%s\n" "${listOfsig2[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesSIG2=${#listOfsig2[@]}
+    listOfhdr2=( `ils ${j}/Fiber2 | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+    '
+    listOfhdr2=( $( $PRINTF "%s\n" "${listOfhdr2[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesHDR2=${#listOfhdr2[@]}
+
+
+
+    $ECHO ">>> $j --->  Fib1: $nblinesHDR1 / $nblinesSIG1 Fib2: $nblinesHDR2 / $nblinesSIG2"
+    let "ntotfiles=ntotfiles+$nblinesSIG1"
+  done 
+  $ECHO "total signal Fiber1 files = $ntotfiles"
+
+done
Index: /BAORadio/AmasNancay/v2/statusonoff.sh
===================================================================
--- /BAORadio/AmasNancay/v2/statusonoff.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/statusonoff.sh	(revision 538)
@@ -0,0 +1,137 @@
+#!/bin/sh
+#Status de l'etat de la production des fichiers INPUT de calibrations
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+listeSources=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+listeSources=( $( $PRINTF "%s\n" "${listeSources[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+$RM $OUT2
+
+for i in ${listeSources[@]}
+do
+#nb: $i contains the full path
+  $ECHO "source ${i}"
+  sourceName=`basename ${i}`
+  listeDates=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listeDates=( $( $PRINTF "%s\n" "${listeDates[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  for j in ${listeDates[@]}
+  do
+#nb: $j contains the full path
+#  $ECHO "run ${j}"
+#
+#Off directory
+#
+    dateField=`basename ${j}`
+    sourceDate="${sourceName}-${dateField}"
+#look if final data processing files exist
+    onOffOk=`ils ${j} | $GREP -i "data.*\.ppf\|diff.*\.ppf" | $WC -l`
+    offDirExist=`ils ${j} | $GREP -i "Off" | $WC -l`
+    if [ ${offDirExist} != 0 ]; then
+	listeCyclesOff=( `ils ${j}/Off | $GREP -i "datacycle" | $AWK '($1=="C-"){print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+	IFS='
+  '
+	listeCyclesOff=( $( $PRINTF "%s\n" "${listeCyclesOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+	IFS=$DefaultIFS
+	
+	if [ ${#listeCyclesOff[*]} != 0 ]; then    
+           for c in ${listeCyclesOff[@]}
+	   do
+#nb: $c has the format of datacycle<number>
+	     nFiles=`ils ${j}/Off/${c} | $GREP -i "medfiltmtx" | $WC -l`
+	     if [ ${nFiles} != 0 ]; then
+		 $ECHO "${sourceDate}/Off/${c}: ${nFiles} spectra (${onOffOk} ana)"
+	     else
+		 $ECHO "${sourceDate}/Off/${c}: ON-OFF failed"
+	     fi
+#end of loop on cycles 
+	   done
+	else
+	    $ECHO "${sourceDate} : No ON-OFF done for Off Diectory"
+	fi
+#end of Off exist
+    else
+	$ECHO "${sourceDate} : No Off Diectory"
+    fi
+#
+#On directory
+#
+    dateField=`basename ${j}`
+    sourceDate="${sourceName}-${dateField}"
+#look if final calibration files exist
+    onOnOk=`ils ${j} | $GREP -i "data.*\.ppf\|diff.*\.ppf" | $WC -l`
+    onDirExist=`ils ${j} | $GREP -i "On" | $WC -l`
+    if [ ${onDirExist} != 0 ]; then
+	listeCyclesOn=( `ils ${j}/On | $GREP -i "datacycle" | $AWK '($1=="C-"){print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+	IFS='
+  '
+	listeCyclesOn=( $( $PRINTF "%s\n" "${listeCyclesOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+	IFS=$DefaultIFS
+	
+	if [ ${#listeCyclesOn[*]} != 0 ]; then    
+           for c in ${listeCyclesOn[@]}
+	   do
+#nb: $c has the format of datacycle<number>
+	     nFiles=`ils ${j}/On/${c} | $GREP -i "medfiltmtx" | $WC -l`
+	     if [ ${nFiles} != 0 ]; then
+		 $ECHO "${sourceDate}/On/${c}: ${nFiles} spectra (${onOnOk} ana)"
+	     else
+		 $ECHO "${sourceDate}/On/${c}: ON-OFF failed"
+	     fi
+#end of loop on cycles 
+	   done
+	else
+	    $ECHO "${sourceDate} : No ON-OFF done for On Diectory"
+	fi
+#end of On exist
+    else
+	$ECHO "${sourceDate} : No On Diectory"
+    fi
+#end of loop on dates
+  done 
+#end of loop on sources
+done
Index: /BAORadio/AmasNancay/v2/submit2bqs-anarawonoffmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-anarawonoffmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-anarawonoffmaker.sh	(revision 538)
@@ -0,0 +1,42 @@
+#!/bin/sh -xv
+
+scriptName="`basename $0`"
+echo "Processing script ${scriptName} at `date`"
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+dateJob=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio="$2"; shift;;
+      -date) dateSelected="-date $2"; dateJob="$2";     shift;;
+      -sim) 
+      simulationMode="-sim On"
+      $ECHO "INFO ${scriptName} running in SIMUL mode";;
+	-h)
+	echo >&2 \
+	    "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
+	exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+
+jobBatchName="rawonoff-${sourceRadio}${dateJob}"
+
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=120000,M=1000MB,scratch=1000MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/anarawonoffmaker.sh  -src ${sourceRadio} ${dateSelected} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/submit2bqs-calibmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-calibmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-calibmaker.sh	(revision 538)
@@ -0,0 +1,43 @@
+#!/bin/sh -xv
+
+scriptName="`basename $0`"
+echo "Processing script ${scriptName} at `date`"
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio
+freqBAOCalib=
+bwBAOCalib=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio="$2"; shift;;
+      -freq) freqBAOCalib="-freq $2"; shift;;
+      -bwd)  bwBAOCalib="-bwd $2"; shift;;
+      -sim) 
+      simulationMode="-sim On"
+      $ECHO "INFO ${scriptName} running in SIMUL mode";;
+	-h)
+	echo >&2 \
+	    "usage: $0 -src souce [-freq freq in MHz (default ${freqBAOCalib})] [-bwd bandwidth in MHz (default ${bwBAOCalib})]"
+	exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+
+jobBatchName="calibs-${sourceRadio}"
+
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=120000,M=1000MB,scratch=1000MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/calibmaker.sh  -src ${sourceRadio} ${freqBAOCalib} ${bwBAOCalib} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/submit2bqs-gainmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-gainmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-gainmaker.sh	(revision 538)
@@ -0,0 +1,26 @@
+#!/bin/sh -xv
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+      -sim) simulationMode="-sim";;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce [-sim]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+jobBatchName="gains-$sourceRadio"
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=120000,M=1000MB,scratch=500MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/gainmaker.sh  -src ${sourceRadio} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/submit2bqs-hdrfitsextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-hdrfitsextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-hdrfitsextractor.sh	(revision 538)
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+jobBatchName="headerMaker-$sourceRadio"
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=100000,M=256MB,scratch=700MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/hdrfitsextractor.sh  $sourceRadio
+EOF
Index: /BAORadio/AmasNancay/v2/submit2bqs-onoffmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-onoffmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-onoffmaker.sh	(revision 538)
@@ -0,0 +1,44 @@
+#!/bin/sh -xv
+
+scriptName="`basename $0`"
+echo "Processing script ${scriptName} at `date`"
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+dateJob=
+freqBAOCalib=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio="$2"; shift;;
+      -date) dateSelected="-date $2"; dateJob="$2";     shift;;
+      -freq) freqBAOCalib="-freq $2"; shift;;
+      -sim) 
+      simulationMode="-sim On"
+      $ECHO "INFO ${scriptName} running in SIMUL mode";;
+	-h)
+	echo >&2 \
+	    "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
+	exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+
+jobBatchName="onoff-${sourceRadio}${dateJob}"
+
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=120000,M=1000MB,scratch=1000MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/onoffmaker.sh  -src ${sourceRadio} ${dateSelected} ${freqBAOCalib} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/submit2bqs-rawonoffmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-rawonoffmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-rawonoffmaker.sh	(revision 538)
@@ -0,0 +1,119 @@
+#!/bin/sh -xv
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+TR=/usr/bin/tr
+FIND=/usr/bin/find
+PRINTF=/usr/bin/printf
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+QSUB=qsub
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+echo "Processing script ${scriptName} at `date`"
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+dateSelected=
+typeofproc=
+#first and last cycle to process
+#
+firstCycle=
+lastCycle=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio="$2"; shift;;
+      -date) dateSelected="-date $2"; dateJob="$2";     shift;;
+      -type) typeofproc="-type $2"; type=$2; shift;;
+      -fcycle) firstCycle="-fcycle $2"; fcycle="$2"; shift;;
+      -lcycle) lastCycle="-lcycle $2"; lcycle="$2"; shift;;
+       -sim) 
+      simulationMode="-sim On"
+      $ECHO "INFO ${scriptName} running in SIMUL mode";;
+	-h)
+	echo >&2 \
+	    "usage: $0 -src souce -date YYYYMMDD -type type [-fcycle firstCycle] [-lcycle lastCycle] [-sim to trig simulation mode]"
+	exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+if [ "<${dateSelected}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the date option (-date)"
+    exit 1
+fi
+
+if [ "<${typeofproc}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the type option (-type)"
+    exit 1
+fi
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+#assume 20 files/cycles cf. 30sec/cycle et 3sec par signal et fact 2 margin 
+if [ "<${firstCycle}>" != "<>" -a "<${lastCycle}>" != "<>" ]; then
+    jobBatchName="rawonoff-${sourceRadio}${dateJob}${type}-${fcycle}-${lcycle}"
+    nFiles=`expr ${lcycle} - ${fcycle} + 1`
+    nFiles=`expr ${nFiles} \* 20`
+else
+    jobBatchName="rawonoff-${sourceRadio}${dateJob}${type}-All"
+    #assume 30 cycles maxi!
+    nFiles="20 \* 30"
+
+fi
+
+jobLogName="${jobBatchName}.log.$$"
+
+#Nbre de paquets utilises pour 1 spectre
+tspwin="5120"
+#calcul du scratch sachant que l'on nettoie apres chaque cycle
+nSpectra=`expr 20 \* 25600 /  ${tspwin}`
+scratchSize=`expr  20 \* 500 + ${nSpectra} \* 200 / 1000 + 5`
+
+#le cpu total tient compte de l'emsemble des ficheirs a taiter
+cpu=`expr ${nFiles} \* 60 \* 50`
+
+qsub -l u_sps_baoradio,u_irods,T=${cpu},M=1000MB,scratch=${scratchSize}MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/proc_rawonoff.sh  -src ${sourceRadio} ${dateSelected} ${typeofproc} ${firstCycle} ${lastCycle} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/submit2bqs-scaextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/submit2bqs-scaextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/submit2bqs-scaextractor.sh	(revision 538)
@@ -0,0 +1,66 @@
+#!/bin/sh -xvf
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+jobBatchName="scaMaker-$sourceRadio"
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=100000,M=256MB,scratch=700MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/scaextractor.sh  $sourceRadio
+EOF
Index: /BAORadio/AmasNancay/v2/transdate.sh
===================================================================
--- /BAORadio/AmasNancay/v2/transdate.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/transdate.sh	(revision 538)
@@ -0,0 +1,79 @@
+#!/bin/sh
+# transforme les dates en nombre de seconde depuis minuit
+# pour reecrire les start/stop des differentes phases du fichier sca
+
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+     iojobpath=${TMPBATCH}
+else
+     $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/transdate.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+if [ ! $# = 1 ]; then
+  $ECHO "FATAL (${scriptName}) usage: ./transdate.sh <scaXYZ.sum>"
+  touch ${tmppublicpath}/transdate.finished
+  exit 0
+fi
+
+#inputs x-chexk sca file
+inScaFile=$1
+if [ ! -e $inScaFile ]; then
+  $ECHO "FATAL (${scriptName}): $inScaFile not found"
+  touch ${tmppublicpath}/transdate.finished
+  exit 1
+fi
+
+$AWK -v datesys=$DATE '
+BEGIN {
+  FS=",";
+}
+
+{
+  cycle=$1;
+  for (i=2; i<=NF; i++) {
+    cmd=sprintf("%s +%%F -d \"%s\" ",datesys,$i);
+    (cmd | getline day);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,day);
+    (cmd | getline s0);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,$i);
+    (cmd | getline s1);
+    close(cmd);
+    $i = s1 - s0;    
+  }
+  print $0;
+}
+
+' $inScaFile >> $inScaFile.trans
+
+touch ${tmppublicpath}/transdate.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/analyse.cc
===================================================================
--- /BAORadio/AmasNancay/v2/v1/analyse.cc	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/analyse.cc	(revision 538)
@@ -0,0 +1,1356 @@
+//
+// Analyse of Amas@Nancay runs by J.E Campagne (LAL)
+// Version 0: 1/6/2011
+//-----------------------------
+
+// Utilisation de SOPHYA pour faciliter les tests ...
+#include "sopnamsp.h"
+#include "machdefs.h"
+
+#include <stdlib.h>
+#include <dirent.h>
+
+
+// include standard c/c++
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+#include <map>
+#include <functional>
+#include <algorithm>
+#include <numeric>
+#include <list>
+#include <exception>
+
+// include sophya mesure ressource CPU/memoire ...
+#include "resusage.h"
+#include "ctimer.h"
+#include "timing.h"
+#include "timestamp.h"
+#include "strutilxx.h"
+#include "ntuple.h"
+#include "fioarr.h"
+#include "tarrinit.h"
+#include "histinit.h"
+#include "fitsioserver.h"
+#include "fiosinit.h"
+#include "ppersist.h"
+
+
+const sa_size_t NUMBER_OF_CHANNELS = 2;
+const sa_size_t  NUMBER_OF_FREQ = 8192;
+const r_4    LOWER_FREQUENCY = 1250.0; //MHz
+const r_4    TOTAL_BANDWIDTH = 250.0; //MHz
+
+//decalration of non class members functions
+extern "C" {
+  int Usage(bool);
+}
+
+
+//----------------------------------------------------------
+//Utility fonctions
+// Function for deleting pointers in map.
+template<class A, class B>
+struct DeleteMapFntor
+{
+    // Overloaded () operator.
+    // This will be called by for_each() function.
+    bool operator()(pair<A,B> x) const
+    {
+        // Assuming the second item of map is to be
+        // deleted. Change as you wish.
+        delete x.second;
+        return true;
+    }
+};
+//-----
+bool compare(const pair<string,r_4>& i, const pair<string,r_4>& j) {
+  return i.second < j.second;
+}
+//-----
+sa_size_t round_sa(r_4 r) {
+  return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5));
+}
+//-----
+string StringToLower(string strToConvert){
+  //change each element of the string to lower case
+  for(unsigned int i=0;i<strToConvert.length();i++) {
+    strToConvert[i] = tolower(strToConvert[i]);
+  }
+  return strToConvert;//return the converted string
+}
+//-----
+list<string> ListOfFileInDir(string dir, string filePettern) throw(string) {
+  list<string> theList;
+
+
+  DIR *dip;
+  struct dirent *dit;
+  string msg;
+  string fileName;
+  string fullFileName;
+  size_t found;
+
+  if ((dip=opendir(dir.c_str())) == NULL ) {
+    msg = "opendir failed on directory "+dir;
+    throw msg;
+  }
+  while ( (dit = readdir(dip)) != NULL ) {
+    fileName = dit->d_name;
+    found=fileName.find(filePettern);
+    if (found != string::npos) {
+      fullFileName = dir + "/";
+      fullFileName += fileName;
+      theList.push_back(fullFileName);
+    }
+  }//eo while
+  if (closedir(dip) == -1) {
+    msg = "closedir failed on directory "+dir;
+    throw msg;
+  }
+  return theList;
+}
+
+
+//Declaration of local classes
+//----------------------------------------------
+//Process Interface
+class IProcess {
+public:
+  IProcess() {}
+  virtual ~IProcess() {}
+  virtual int processCmd() throw(string) =0;
+};
+//------------
+//Common Process
+class ProcessBase : public IProcess {
+public:
+  ProcessBase();
+  virtual ~ProcessBase();
+  void SetInputPath(const string& inputPath) {inputPath_ = inputPath;}
+  void SetOutputPath(const string& outputPath) {outputPath_ = outputPath;}
+  void SetSourceName(const string& sourceName) {sourceName_ = sourceName;}
+  void SetDateOfRun(const string& dateOfRun) {dateOfRun_ = dateOfRun;}
+  void SetSpectraDirectory(const string& spectraDirectory) {spectraDirectory_ = spectraDirectory;}
+  void SetTypeOfFile(const string& typeOfFile) { typeOfFile_ = typeOfFile; }
+  void SetNumCycle(const string& numcycle) {numcycle_ = numcycle; }
+  void SetScaFileName(const string& scaFileName) { scaFileName_ =scaFileName; }
+
+  void SetDebugLevel(const string& debuglev) {
+    debuglev_ = atoi(debuglev.c_str());
+  }
+
+  virtual int processCmd() throw(string);
+  
+protected:
+  string inputPath_;
+  string outputPath_;
+  string sourceName_;
+  string dateOfRun_;
+  string spectraDirectory_;
+  string typeOfFile_;
+
+  string numcycle_; //cycle numbers format="first,last"
+  sa_size_t ifirstCycle_;
+  sa_size_t ilastCycle_;
+
+
+  uint_4 debuglev_; 
+  string scaFileName_;
+  NTuple* scaTuple_;
+  map<sa_size_t,sa_size_t> idCycleInTuple_;
+};
+ProcessBase::ProcessBase() {
+  scaTuple_ = 0;
+}
+ProcessBase::~ProcessBase() {
+  if (scaTuple_) delete scaTuple_;
+  scaTuple_ = 0;
+}
+//------------
+//Process ON/OFF data
+//------------
+class ProcessONOFFData : public ProcessBase {
+protected:
+  string  freqBAOCalibration_;//string MHz
+public:
+  ProcessONOFFData(){}
+  virtual ~ProcessONOFFData(){}
+
+  void SetFreqBAOCalibration(const string& freqBAOCalibration) { 
+    freqBAOCalibration_ = freqBAOCalibration; 
+  }
+  
+  virtual int processCmd() throw(string);
+};
+//------------
+//Process Gain
+//------------
+class ProcessGain : public ProcessBase {
+protected:
+  string mode_; //mode of data taken for gain computation On || Off
+public:
+  ProcessGain(){}
+  virtual ~ProcessGain(){}
+
+  void SetMode(const string& mode) {mode_ = mode;}
+  
+  virtual int processCmd() throw(string);
+};
+//------------
+//Process Calibration
+//------------
+class ProcessCalibration : public ProcessBase {
+protected:
+  string option_; //option of calibration procedure
+  string  freqBAOCalibration_;//string MHz
+  r_4 valfreqBAOCalibration_; //value MHz
+  string bandWidthBAOCalibration_;//string MHz
+  r_4 valbandWidthBAOCalibration_;//value MHz
+  
+  sa_size_t lowerFreqBin_;
+  sa_size_t upperFreqBin_;
+
+public:
+  ProcessCalibration() {}
+  virtual ~ProcessCalibration(){}
+
+  void SetOption(const string& option) {option_ = option;}
+  void SetFreqBAOCalibration(const string& freqBAOCalibration) { 
+    freqBAOCalibration_ = freqBAOCalibration; 
+    valfreqBAOCalibration_ = atof(freqBAOCalibration_.c_str());
+  }
+  void SetBandWidthBAOCalibration(const string& bandWidthBAOCalibration) { 
+    bandWidthBAOCalibration_ = bandWidthBAOCalibration; 
+    valbandWidthBAOCalibration_ = atof(bandWidthBAOCalibration_.c_str());
+  }
+
+  void ComputeLowerUpperFreqBin();
+      
+  virtual int processCmd() throw(string);
+};
+void ProcessCalibration::ComputeLowerUpperFreqBin() {
+  sa_size_t c0 = round_sa(NUMBER_OF_FREQ*(valfreqBAOCalibration_-LOWER_FREQUENCY)/TOTAL_BANDWIDTH);
+  sa_size_t dc = round_sa(NUMBER_OF_FREQ*valbandWidthBAOCalibration_/TOTAL_BANDWIDTH);
+  lowerFreqBin_ = c0-dc/2;
+  upperFreqBin_ = c0+dc/2;
+}
+
+
+//----------------------------------------------------
+//----------------------------------------------------
+int main(int narg, char* arg[])
+{
+
+  //Init process types
+  map<string,IProcess*> process;
+  process["dataOnOff"] = new ProcessONOFFData();
+  process["gain"]      = new ProcessGain();
+  process["calib"]     = new ProcessCalibration();
+
+  //Init Sophya related modules
+  //  SophyaInit();
+  TArrayInitiator _inia; //nneded for TArray persistancy
+  FitsIOServerInit(); //needed for input file
+
+  //message used in Exceptions
+  string msg;
+
+  //Return code
+  int rc = 0;
+
+  //Arguments managements
+  if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage(false);
+  if (narg<11) return Usage(true);
+
+  string action;
+  string inputPath = "."; 
+  string outputPath = "."; 
+  string sourceName;
+  string scaFile;
+  string dateOfRun;
+  string spectraDirectory;
+  string freqBAOCalib = "";
+  string bandWidthBAOCalib = "";
+  string debuglev = "0";
+  string mode = "";
+  string numcycle;
+  string calibrationOpt = "";  
+
+  string typeOfFile="medfiltmtx";
+  
+
+  //  bool okarg=false;
+  int ka=1;
+  while (ka<(narg-1)) {
+    if (strcmp(arg[ka],"-debug")==0) {
+      debuglev=arg[ka+1];
+      ka+=2;
+    }
+    if (strcmp(arg[ka],"-act")==0) {
+      action=arg[ka+1];
+      ka+=2;
+    }
+    if (strcmp(arg[ka],"-inPath")==0) {
+      inputPath=arg[ka+1];
+      ka+=2;
+    }
+    if (strcmp(arg[ka],"-outPath")==0) {
+      outputPath=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-source")==0) {
+      sourceName=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-sca")==0) {
+      scaFile=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-date")==0) {
+      dateOfRun=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-specdir")==0) {
+      spectraDirectory=arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-specname")==0) {
+      typeOfFile=arg[ka+1];
+      ka+=2;
+    }    
+    else if (strcmp(arg[ka],"-freqBAOCalib")==0) {
+      freqBAOCalib = arg[ka+1];
+      ka+=2;
+    }
+    else if (strcmp(arg[ka],"-bwBAOCalib")==0) {
+      bandWidthBAOCalib = arg[ka+1];
+      ka+=2;
+    } 
+    else if (strcmp(arg[ka],"-mode")==0) {
+      mode =arg[ka+1];
+      ka+=2; 
+    }
+    else if (strcmp(arg[ka],"-numcycle")==0) {
+      numcycle =arg[ka+1];
+      ka+=2; 
+    }
+    else if (strcmp(arg[ka],"-calibopt")==0) {
+      calibrationOpt =arg[ka+1];
+      ka+=2; 
+    }
+    else ka++;
+  }//eo while
+
+
+
+  try {
+    //verification of action
+    if(process.find(action) == process.end()) {
+      msg = "action ";
+      msg += action + " not valid... FATAL";
+      rc = 999;
+      throw msg;
+    }
+    
+
+    //
+    //Process initialisation...
+    //
+    try {
+      ProcessBase* procbase = dynamic_cast<ProcessBase*>(process[action]);
+      if (procbase == 0) {
+	msg= "action ";
+	msg += action + "Not a <process base> type...FATAL";
+	rc = 999;
+	throw msg;
+      }
+      procbase->SetInputPath(inputPath);
+      procbase->SetOutputPath(outputPath);
+
+      if ("" == sourceName) {
+	msg = "(FATAL) missingsourceName  for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetSourceName(sourceName);
+
+      if ("" == dateOfRun) {
+	msg = "(FATAL) missing dateOfRun for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetDateOfRun(dateOfRun);
+
+      
+      if ("" == spectraDirectory) {
+	msg = "(FATAL) missing spectraDirectory for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetSpectraDirectory(spectraDirectory);
+
+      if ("" == scaFile) {
+	msg = "(FATAL) missing scaFile for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetScaFileName(scaFile);
+
+      if ("" == numcycle) {
+	msg = "(FATAL) missing cycle number for action " + action;
+	Usage(true);
+	throw msg;
+      }
+      procbase->SetNumCycle(numcycle);
+
+
+      procbase->SetTypeOfFile(typeOfFile);
+
+      procbase->SetDebugLevel(debuglev);
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+
+    try {
+      ProcessONOFFData* procdata = dynamic_cast<ProcessONOFFData*>(process[action]);
+      if (procdata) {
+	if (freqBAOCalib == "") {
+	  msg = "(FATAL) missing calibration BAO frequency for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	procdata->SetFreqBAOCalibration(freqBAOCalib);
+      }
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+    
+
+    try {
+      ProcessGain* procgain = dynamic_cast<ProcessGain*>(process[action]);
+      if(procgain) {
+	if (mode == "") {
+	  msg = "(FATAL) missing mode-type for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	procgain->SetMode(mode);
+      }
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+
+    try {
+      ProcessCalibration* proccalib = dynamic_cast<ProcessCalibration*>(process[action]);
+      if(proccalib) {
+	if (calibrationOpt == "") {
+	  msg = "(FATAL) missing calibration option";
+	  Usage(true);
+	  throw msg;
+	}
+	if (freqBAOCalib == "") {
+	  msg = "(FATAL) missing calibration BAO frequency for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	if (bandWidthBAOCalib == "") {
+	  msg = "(FATAL) missing calibration BAO frequency band width for action " + action;
+	  Usage(true);
+	  throw msg;
+	}
+	proccalib->SetOption(calibrationOpt);
+	proccalib->SetFreqBAOCalibration(freqBAOCalib);
+	proccalib->SetBandWidthBAOCalibration(bandWidthBAOCalib);
+	proccalib->ComputeLowerUpperFreqBin();
+      }
+    }
+    catch(exception& e){
+      throw e.what();
+    }
+
+    //
+    //execute command
+    //
+    rc = process[action]->processCmd();
+
+  }
+  catch (std::exception& sex) {
+    cerr << "\n analyse.cc std::exception :"  << (string)typeid(sex).name() 
+	 << "\n msg= " << sex.what() << endl;
+    rc = 78;
+  }
+  catch ( string str ) {
+    cerr << "analyse.cc Exception raised: " << str << endl;
+  }
+  catch (...) {
+    cerr << " analyse.cc catched unknown (...) exception  " << endl; 
+    rc = 79; 
+  } 
+
+  
+
+
+  cout << ">>>> analyse.cc ------- END ----------- RC=" << rc << endl;
+  
+  //Delete processes
+  for_each(process.begin(),process.end(), DeleteMapFntor<string,IProcess*>());
+
+  return rc;
+}
+
+//---------------------------------------------------
+int Usage(bool flag) {
+  cout << "Analyse.cc usage...." << endl;
+  cout << "analyse  -act <action_type>: dataOnOff, gain, calib\n"
+       << "         -inPath <path for input files: default='.'>\n"
+       << "         -outPath <path for output files: default='.'>\n"
+       << "         -source <source name> \n" 
+       << "         -date <YYYYMMDD>\n"
+       << "         -sca <file name scaXYZ.sum.trans>\n"
+       << "         -specdir <generic directory name of spectra fits files>\n"
+       << "         -specname <generic name of spectra fits files>\n"
+       << "         -freqBAOCalib <freq in MHZ> freq. of calibration BAO\n"
+       << "            valid for act=dataOnOff\n"
+       << "         -bwBAOCalib <band width MHz> band width arround central freq. for calibration BAO\n"
+       << "            valid for act=calib\n"
+       << "         -mode <mode_type>:\n"
+       << "            valid for act=gain, mode_type: On, Off\n"
+       << "         -numcycle <number>,<number>:\n"
+       << "            valid for all actions"
+       << "         -calibopt <option>:\n"
+       << "            valid for act=calib: indiv OR mean (NOT USED)"
+       << "         -debuglev <number> [0=default]\n"
+       << "           1: normal print\n"
+       << "           2: save intermediate spectra\n"
+       << endl;
+  if (flag) {
+    cout << "use <path>/analyse -h for detailed instructions" << endl;
+    return 5;
+  }
+  return 1;
+}
+
+int ProcessBase::processCmd() throw(string) {
+  int rc =0;
+  string msg;
+  if(debuglev_>0)cout << "Process Base" << endl;
+  //------------------------
+  //Use the sca file informations
+  //------------------------
+  //  string scaFullPathName = "./";
+  //TOBE FIXED  scaFullPathName += sourceName_+"/"+dateOfRun_ + StringToLower(sourceName_)+"/";
+  string scaFullPathName = inputPath_ + "/" 
+    + sourceName_+ "/" +dateOfRun_ + StringToLower(sourceName_)+ "/" + scaFileName_;
+  char* scaTupleColumnName[9] = {"cycle","stcalOn","spcalOn","stOn","spOn","stcalOff","spcalOff","stOff","spOff"};
+  scaTuple_ = new NTuple(9,scaTupleColumnName);
+  int n = scaTuple_->FillFromASCIIFile(scaFullPathName);
+  if(n<0){ //Error
+    msg = "(FATAL) NTuple error loading "+ scaFullPathName;
+    rc = 999;
+    throw msg;
+  }
+  
+  if(debuglev_>1){
+    cout << "ProcessBase::processCmd: dump tuple in " << scaFullPathName << endl;
+    scaTuple_->Show(cout);
+  }
+  
+  
+  //Get the cycles (here consider consecutive cycles)    
+  //The SCA file cannot be used as the DAQ can miss some cycles...
+  //     r_8 firstCycle, lastCycle;
+  //     scaTuple_->GetMinMax("cycle",firstCycle,lastCycle);
+  //     ifirstCycle_ = (sa_size_t)firstCycle;
+  //     ilastCycle_  = (sa_size_t)lastCycle;
+  //Analyse the string given by -numcycle command line
+  int ai1=0,ai2=0;
+  sscanf(numcycle_.c_str(),"%d,%d",&ai1,&ai2);
+  ifirstCycle_ = (sa_size_t)ai1;
+  ilastCycle_  = (sa_size_t)ai2;
+  
+
+  //associate cycle number to index line in tuple
+  sa_size_t nLines = scaTuple_->NbLines();
+  for(sa_size_t iL=0; iL<nLines; ++iL){
+    idCycleInTuple_[(sa_size_t)scaTuple_->GetCell(iL,"cycle")]=iL;
+  }
+
+
+  return rc;
+}
+//----------------------------------------------
+int ProcessONOFFData::processCmd() throw(string) {
+  int rc = 0;
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+   if(debuglev_>0)cout << "Process Data" << endl;
+  vector<string> modeList;
+  modeList.push_back("On");
+  modeList.push_back("Off");
+  vector<string>::const_iterator iMode;
+  
+  uint_4 id; 
+  string tag;
+
+  //
+  //Process to get sucessively
+  //Raw Spectra, 
+  //BAO Calibrated Spectra 
+  //and RT Calibrated Spectra
+  //The pocesses are separated to allow intermediate save of results
+
+  map< pair<string, sa_size_t>, TMatrix<r_4> > spectreCollect;
+  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iSpectre, iSpectreEnd;
+  
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process RAW Mode " << mode << endl;
+
+    //------------------------------------------
+    //Produce Raw spectra per cycle
+    //------------------------------------------
+
+    string directoryName;
+    list<string> listOfSpecFiles;
+    list<string>::const_iterator iFile, iFileEnd;
+    
+        
+    //
+    //loop on cycles
+    //
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      //TOBE FIXED      directoryName = "./" + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode + "/";
+      directoryName = "./" + mode + "/";
+      stringstream sicycle;
+      sicycle << icycle;
+      directoryName += spectraDirectory_ + sicycle.str() + "/";
+
+      //read directory
+      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+      
+
+      //compute mean of spectra created in a cycle
+      if(debuglev_>0)cout << "compute mean for cycle " << icycle << endl;
+      TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+      iFileEnd = listOfSpecFiles.end();
+      r_4 nSpectres  = 0;
+      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+	FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+	TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+	aSpectrum >> spectre;
+	spectreMean += spectre;
+	nSpectres++;
+      }// end of for files 
+      if(nSpectres>0) spectreMean /= nSpectres;
+      
+      //save mean spectrum
+      spectreCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreMean,false) ));
+    }//end of for cycles
+  }//end loop on mode for raw preocess
+
+  if(debuglev_>1) {//save mean spectra on file 
+    cout << "Save mean raw spectra" << endl;
+    string fileName;
+    //TOBE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataRaw" + ".ppf";
+    fileName = "./dataRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+
+    POutPersist fos(fileName);
+    id=0;
+    iSpectreEnd = spectreCollect.end();
+    for (iSpectre = spectreCollect.begin();
+	 iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+      tag = "specRaw";
+      stringstream sid;
+      sid << id;
+      tag += sid.str();
+      fos << PPFNameTag(tag) << iSpectre->second;
+    }
+  }//end of save fits
+  
+
+
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process CALIB BAO Mode " << mode << endl;
+    //------------------------------------------
+    // Correct Raw spectra for BAO calibration
+    //------------------------------------------
+    //Read BAO calibration files
+    TMatrix<r_4> calibBAOfactors;
+    sa_size_t nr,nc; //values read 
+    bool first = true;
+
+    for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
+      string calibFileName = "./" + sourceName_ + "/" 
+	+ dateOfRun_ + StringToLower(sourceName_) + "-" + mode + "-" + freqBAOCalibration_ + "MHz";
+      stringstream channel;
+      channel << iCh;
+      calibFileName += "-Ch" + channel.str() + "Cycles.txt";
+      if(debuglev_>0) cout << "Read file " << calibFileName << endl;
+      ifstream ifs(calibFileName.c_str());
+      if ( ! ifs.is_open() ) {
+	rc = 999;
+	throw calibFileName + " cannot be opened...";
+      }	
+      TVector<r_4> aCalib;
+      aCalib.ReadASCII(ifs,nr,nc);
+      if(first){
+	first = false;
+	calibBAOfactors.SetSize(NUMBER_OF_CHANNELS,nr);
+      }
+      calibBAOfactors( Range(iCh), Range::all() ) = aCalib.Transpose();
+      ifs.close();
+    }//end of loop on channels
+
+
+    //
+    //spectra corrected by BAO calibration factor 
+    //make it different on Channels and Cycles (1/06/2011)
+    //warning cycles are numbered from 1,...,N
+    //
+    if(debuglev_>0)cout << "do calibration..." << endl;
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      TMatrix<r_4> specmtx(spectreCollect[make_pair(mode,icycle)],true); //share the memory 
+      
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
+	specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh,icycle-1);
+      }
+    }
+  } //end loop mode for BAO calib
+
+  if(debuglev_>1){ //save mean spectra BAO calibrated on file
+    cout << "save calibrated BAO spectra" << endl;
+    string fileName;
+    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataBAOCalib" + ".ppf";
+    fileName = "./dataBAOCalib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+
+    POutPersist fos(fileName);
+    iSpectreEnd = spectreCollect.end();
+    id=0;
+    for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+      tag = "specBAOCalib";
+      stringstream sid;
+      sid << id;
+      tag += sid.str();
+      fos << PPFNameTag(tag) << (*iSpectre).second;
+    }
+  }//end of save fits
+
+  
+  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
+    string mode = *iMode;
+    if(debuglev_>0)cout << "Process CALIB RT Mode " << mode << endl;
+    //------------------------------------------
+    // Correct BAO calib spectra for RT calibration
+    //------------------------------------------
+    //Very Preliminary May-June 11
+    //coef RT @ 1346MHz Ouest - Est associees a Ch 0 et 1
+    r_4 calibRT[NUMBER_OF_CHANNELS] = {27.724, 22.543};
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+      TMatrix<r_4> specmtx(spectreCollect[make_pair(mode,icycle)],true); //share the memory    
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
+	specmtx( Range(iCh), Range::all() ) *= calibRT[iCh];
+      }
+    }
+  }//end loop on mode RT calib
+
+  {//save mean spectra BAO & RT calibrated on file
+    if(debuglev_>0)cout << "save calibrated BAO & RT spectra" << endl;
+    string fileName;
+    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataBAORTCalib" + ".ppf";
+     fileName = "./dataBAORTCalib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";   
+    POutPersist fos(fileName);
+    iSpectreEnd = spectreCollect.end();
+    id = 0;
+    for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+      tag = "specBAORTCalib";
+      stringstream sid;
+      sid << id;
+      tag += sid.str();
+      fos << PPFNameTag(tag) << (*iSpectre).second;
+    }
+  }//end of save fits
+
+  //------------------------------------------
+  // Perform ON-OFF
+  //------------------------------------------
+  
+  map< sa_size_t, TMatrix<r_4> > diffCollect;
+  map< sa_size_t, TMatrix<r_4> >::iterator iDiff, iDiffEnd;
+
+  TMatrix<r_4> diffMeanOnOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //init zero
+  r_4 nCycles = 0;
+  for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+    nCycles++;
+    TMatrix<r_4> specmtxOn(spectreCollect[make_pair(modeList[0],icycle)],false); //clone the memory 
+    TMatrix<r_4> specmtxOff(spectreCollect[make_pair(modeList[1],icycle)],false); //clone the memory 
+    TMatrix<r_4> diffOnOff = specmtxOn - specmtxOff;
+    diffCollect.insert(pair< sa_size_t,TMatrix<r_4> >(icycle,TMatrix<r_4>(diffOnOff,false) ));
+    diffMeanOnOff += diffOnOff;
+  }//end loop on cycle
+  if(nCycles>0) diffMeanOnOff/=nCycles;
+
+  //exctract channels and do the mean
+  TVector<r_4> meanOfChan(NUMBER_OF_FREQ); //implicitly init to 0
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
+    meanOfChan += diffMeanOnOff.Row(iCh).Transpose();
+  }
+  meanOfChan /= (r_4)NUMBER_OF_CHANNELS;
+  
+
+
+  {//save diff ON-OFF BAO & RT calibrated
+    if(debuglev_>0)cout << "save ON-OFF spectra" << endl;
+    string fileName;
+    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "diffOnOff" + ".ppf";
+    fileName = "./diffOnOff_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist fos(fileName);
+    iDiffEnd = diffCollect.end();
+    id = 0;
+    for (iDiff = diffCollect.begin();iDiff != iDiffEnd ; ++iDiff, id++) {
+      tag = "specONOFF";
+      stringstream sid;
+      sid << id;
+      tag += sid.str();
+      fos << PPFNameTag(tag) << (*iDiff).second;
+    }
+    //save the mean also
+    fos << PPFNameTag("specONOFFMean") << diffMeanOnOff;
+    fos << PPFNameTag("specONOFF2ChanMean") << meanOfChan;
+  }//end of save fits
+
+  return rc;
+} //ProcessONOFFData::processCmd
+//
+//----------------------------------------------
+//
+int ProcessGain::processCmd() throw(string) {
+  int rc = 0;
+  string msg = "";
+
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+  if(debuglev_>0)cout << "Process Gain" << endl;
+  
+  string directoryName;
+  //TOBE FIXED  directoryName = "./" + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode_ + "/";
+  //JEC 6/09/2011 numcycle_ repalced by ifirstCycle_ according to definition of numcycle_ and the fact that for GAIN 1 cycle is involved
+  stringstream thegaincycle;
+  thegaincycle << ifirstCycle_;
+  directoryName = inputPath_ + "/" 
+    + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/" 
+    + mode_ + "/" + spectraDirectory_ + thegaincycle.str()  + "/";
+  
+  list<string> listOfSpecFiles;
+  list<string>::const_iterator iFile, iFileEnd;
+  //read directory
+
+  listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+  
+  //Mean power computed over the N channels to select the spectra for gain computation 
+  //The threshold is computed "on-line" as  1%  of the difference (max power -min power) over the
+  // the min power.
+  //A possible alternative is to set an absolute value...
+  if(debuglev_>0)cout << "compute mean poser spectra for files in " << directoryName << endl;
+  iFileEnd = listOfSpecFiles.end();
+  map<string, r_4> meanpowerCollect;
+  map<string, r_4>::const_iterator iMeanPow, iMeanPowEnd;
+
+  for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+    FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+    TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+    aSpectrum >> spectre;
+    meanpowerCollect[*iFile] = spectre.Sum()/spectre.Size();
+  }//end of for files
+  pair<string, r_4> minelement = *min_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
+  pair<string, r_4> maxelement = *max_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
+  if(debuglev_>1){
+    cout << "meanpowerCollect has " << meanpowerCollect.size() << " spectra registered" << endl;
+    cout << "find min mean power "<<minelement.second << " for " << minelement.first << endl;
+    cout << "find max mean power "<<maxelement.second << " for " << maxelement.first << endl;
+  }
+  r_4 threshold = minelement.second + 0.01*(maxelement.second-minelement.second);
+  if(debuglev_>1){
+    cout << "threshold found at " << threshold <<endl;
+  }  
+
+  TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+  r_4 nSpectres  = 0;
+  iMeanPowEnd = meanpowerCollect.end();
+  for (iMeanPow = meanpowerCollect.begin(); iMeanPow != iMeanPowEnd; ++iMeanPow) {
+    if ( iMeanPow->second <= threshold ) {
+      //TODO avoid the reloading of the file may speed up
+      FitsInOutFile aSpectrum(iMeanPow->first,FitsInOutFile::Fits_RO);
+      TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
+      aSpectrum >> spectre;
+      spectreMean += spectre;
+      nSpectres++;
+    }
+  }
+  if(debuglev_>1){
+    cout << "Number of files selected for gain " << nSpectres <<endl;
+  }    
+  if(nSpectres>0) {
+    spectreMean /= nSpectres;
+  } else {
+    stringstream tmp;
+    tmp << threshold;
+    msg = "Gain: cannot find a spectra above threshold value =" + tmp.str() + " ... FATAL";
+    throw msg;
+  }
+
+  //Save gain spectra
+  {
+    //use ! to override the file: special features of cfitsio library
+    string fileName;
+    //TOBE FIXED   fileName = "!./" + sourceName_ + "/gain_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".fits";
+    fileName = "!"+ outputPath_ + "/gain_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".fits";
+    if(debuglev_>1){
+      cout << "save gains in " << fileName << endl;
+    }
+    FitsInOutFile fos(fileName, FitsInOutFile::Fits_Create);
+    fos << spectreMean;
+  }
+  //save mean power values
+  {
+    vector<r_4> tmp;
+    for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+      tmp.push_back(meanpowerCollect[*iFile]);
+    }
+    string fileName;
+    //TOBE FIXED    fileName = "./" + sourceName_ + "/gain_monitor_" + dateOfRun_ 
+    fileName = outputPath_ + "/gain_monitor_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist fos(fileName); 
+    TVector<r_4> tvtmp(tmp);
+    fos.PutObject(tvtmp,"gainmoni");
+  }
+  
+
+  cout << "OK gain finished" <<endl;
+   return rc;
+}//ProcessGain::processCmd
+//
+//----------------------------------------------
+//
+int ProcessCalibration::processCmd() throw(string) {
+  int rc = 0;
+  string msg = "";
+
+  try {
+    rc = ProcessBase::processCmd();
+  } 
+  catch (string s) {
+    throw s;
+  }
+  if(debuglev_>0)cout << "Process Calibration with option "<< option_ << endl;
+  
+  vector<string> modeList;
+  modeList.push_back("On");
+  modeList.push_back("Off");
+
+  r_8 t0absolute = -1;  //TIMEWIN of the first spectra used
+  r_8 timeTag0   = -1;   //MEANTT, mean TIME TAG of the first paquet window  
+  bool first = true;     // for initialisation
+  
+  // Power Tuple
+  // mode, cycle, time, {Ch0, ... ,ChN} mean poser in the range [f0-Bw/2, f0+Bw/2] 
+  vector<string> varPowerTupleName; //ntuple variable naming
+  varPowerTupleName.push_back("mode");
+  varPowerTupleName.push_back("cycle");  
+  varPowerTupleName.push_back("time");
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
+    stringstream tmp;
+    tmp << iCh;
+    varPowerTupleName.push_back("Ch"+tmp.str());
+  }
+  r_4 valPowerTuple[varPowerTupleName.size()];
+  NTuple powerEvolution(varPowerTupleName); 
+  
+  
+  //-----------------
+  //Start real process
+  //------------------
+  for (size_t iMode = 0; iMode < modeList.size(); ++iMode) {
+    string mode = modeList[iMode];
+    if(debuglev_>0)cout << "Process Calibration for Mode " << mode << endl;
+
+    //initialize the start of each calibration procedure given by the RT SCA file
+    //see ProcessBase::processCmd for the structure of the sca file
+    string scaStartCalibName = "stcal"+mode; 
+    sa_size_t idStartCalib   = scaTuple_->ColumnIndex(scaStartCalibName);
+
+    string directoryName;
+    list<string> listOfSpecFiles;
+    list<string>::const_iterator iFile, iFileEnd;            
+    //
+    //loop on cycles
+    //
+    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
+
+      directoryName = inputPath_ + "/" 
+	+ sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode + "/";
+      stringstream sicycle;
+      sicycle << icycle;
+      directoryName += spectraDirectory_ + sicycle.str() + "/";
+      
+      //read directory
+      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
+
+      iFileEnd = listOfSpecFiles.end();
+      DVList header;
+      TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
+
+      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+	FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
+	aSpectrum.GetHeaderRecords(header);
+	aSpectrum >> spectre;
+
+	if(first){//initialise the timer
+	  first = false;
+	  t0absolute = header.GetD("TIMEWIN")/1000.;
+	  timeTag0 = header.GetD("MEANTT");
+	  if (debuglev_>1){
+	    cout << "debug Header of " << *iFile << endl;
+	    cout << "TIMEWIN = " << setprecision(12) << t0absolute << " "
+		 << "MEANTT = " << setprecision(12) << timeTag0 << endl;
+	  }
+	}//end init timer
+	
+	//Set time of current file
+	//Add to the non-precise absolute origin an precise increment
+	r_4 timeTag = t0absolute + (header.GetD("MEANTT") - timeTag0);
+	int index=0;
+	valPowerTuple[index++] = iMode;
+	valPowerTuple[index++] = icycle;
+	valPowerTuple[index++] = timeTag-scaTuple_->GetCell(idCycleInTuple_[icycle],idStartCalib); //take the RT time start as refernce 
+
+	//Compute the mean power of the two channel (separatly) in the range [f-bw/2, f+bw/2]
+	for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
+	  TMatrix<r_4> tmp(spectre(Range(iCh),Range(lowerFreqBin_,upperFreqBin_)),false);
+	  r_4 mean = tmp.Sum()/(r_4)tmp.Size();
+	  valPowerTuple[index++] = mean;
+	}//end of channel loop
+       
+	//Fill Tuple
+	powerEvolution.Fill(valPowerTuple);
+      }//end of files loop
+    }//end of cycles loop
+  }//end of mode loop
+
+  //store power evolution Tuple
+  if(debuglev_>0){
+    cout << "ProcessCalibration::processCmd: dump powerEvolution tuple" << endl;
+    powerEvolution.Show(cout);
+  }
+  //
+  //Compute Calibration Coefficients as function of mode/cycle/channels
+  //
+
+  //Tsys,Incremant Intensity... Tuple
+  //mode mode cycle [(tsys0,dint0),...,(tsysN,dintN)]
+  vector<string> varCalibTupleName;
+  varCalibTupleName.push_back("mode");
+  varCalibTupleName.push_back("cycle");
+  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
+    stringstream tmp;
+    tmp << iCh;
+    varCalibTupleName.push_back("tsys"+tmp.str());
+    varCalibTupleName.push_back("dint"+tmp.str());
+  }
+  r_4 valCalibTuple[varCalibTupleName.size()];
+  NTuple calibEvolution(varCalibTupleName);
+
+  // select time E [twin0,twin1] U [twin2,twin3] for Tsys
+  // time unit = second 
+  const r_4 twin0 = -3.;
+  const r_4 twin1 = -1.;
+  const r_4 twin2 =  6.;
+  const r_4 twin3 =  8;
+  const r_4 thresholdFactor = 0.80; //80% of the diff. Max-Min intensity
+
+  sa_size_t inModeIdx = powerEvolution.ColumnIndex("mode");
+  sa_size_t inCycleIdx= powerEvolution.ColumnIndex("cycle");
+  sa_size_t inTimeIdx = powerEvolution.ColumnIndex("time");
+  sa_size_t inOffsetCh0 = powerEvolution.ColumnIndex("Ch0"); //nb Ch0 position in the powerEvolution tuple  
+  if(debuglev_>1) cout << "DEBUG: in Idx: (" 
+		       << inModeIdx << ", "
+		       << inCycleIdx << ", "
+		       << inTimeIdx << ", "
+		       << inOffsetCh0 << ")"
+		       << endl;
+
+ 
+  size_t outModeIdx = calibEvolution.ColumnIndex("mode");
+  size_t outCycleIdx= calibEvolution.ColumnIndex("cycle");
+  size_t outOffsetCh0 = calibEvolution.ColumnIndex("tsys0"); //nb Ch0 position in the monitor tuple  
+  size_t outNDataPerCh= 2;
+  if(debuglev_>1)  cout << "DEBUG: out Idx: (" 
+			<< outModeIdx << ", "
+			<< outCycleIdx << ", "
+			<< outOffsetCh0 << ")"
+			<< endl;
+
+  sa_size_t nPowerEvolEntry = powerEvolution.NEntry();
+  double minMode;
+  double maxMode;
+  powerEvolution.GetMinMax("mode",minMode,maxMode);
+  double minCycleNum;
+  double maxCycleNum;
+  powerEvolution.GetMinMax("cycle",minCycleNum,maxCycleNum);
+  if(debuglev_>1)  cout << "DEBUG mode ("<<minMode<<","<<maxMode<<")\n"
+			<< "cycle ("<<minCycleNum<<","<<maxCycleNum<<")"
+			<< endl;
+
+  r_4* aPowerEvolEntry; // a ligne of the powerEvolution tuple
+//   r_4* aPowerEvolEntry = new r_4[powerEvolution.NbColumns()]; // a ligne of the powerEvolution tuple
+
+  r_4 minMean;
+  r_4 maxMean;
+
+  for (size_t iMode = (size_t)minMode; iMode <= (size_t)maxMode; iMode++){
+    for (size_t iCycle = (size_t)minCycleNum; iCycle <= (size_t)maxCycleNum; iCycle++ ){
+      if(debuglev_>1) cout<<"DEBUG >>>>>>> mode/cycle: " << iMode << "/" << iCycle << endl;
+ 
+      valCalibTuple[outModeIdx]=iMode;
+      valCalibTuple[outCycleIdx]=iCycle;
+      //
+      //Compute the Min && Max value of each Ch<i> data
+      if(debuglev_>1) cout<<"DEBUG compute Min and Max for each channels" << endl;
+      //
+      TVector<r_4> chMean[NUMBER_OF_CHANNELS];
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
+	chMean[iCh].SetSize(nPowerEvolEntry);
+      }
+      for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
+	aPowerEvolEntry = powerEvolution.GetVec(ie);
+	if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
+	  for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
+	    chMean[iCh](ie) = aPowerEvolEntry[iCh+inOffsetCh0];
+	  }//end cut
+	}
+      }//eo loop on tuple entries
+      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
+	//
+	//select values to compute background Tsys
+	if(debuglev_>1) {
+	  cout<< "DEBUG >>>> Ch[" << iCh << "]" << endl;
+	  cout<< "DEBUG select values to compute background Tsys " << endl;
+	}
+	//
+       
+	std::vector<r_4> lowerInt;
+	for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
+	  aPowerEvolEntry = powerEvolution.GetVec(ie);
+	  if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
+	    r_4 time = aPowerEvolEntry[inTimeIdx];
+	    if ( (time >= twin0 && time <= twin1) ||
+		 (time >= twin2 && time <= twin3)
+		 ) lowerInt.push_back(aPowerEvolEntry[iCh+inOffsetCh0]);
+	  }//end cut
+	} //end loop entries
+	//
+	//compute the Tsys
+	if(debuglev_>1) cout <<"DEBUG compute Tsys" << endl;
+	//
+	std::nth_element(lowerInt.begin(),
+			 lowerInt.begin()+lowerInt.size()/2,
+			 lowerInt.end());
+	r_4 tsys = *(lowerInt.begin()+lowerInt.size()/2);
+	if(debuglev_>1) cout << "DEBUG tsys["<<iCh<<"] : " << tsys <<endl;
+	//
+	//set the threshold for DAB detection
+	//
+	chMean[iCh].MinMax(minMean,maxMean);
+	minMean = (tsys > minMean) ? tsys : minMean; //pb of empty vector
+	if(debuglev_>1) cout << "DEBUG min["<<iCh<<"] : " << minMean
+			     << " max["<<iCh<<"] : " << maxMean
+			     <<endl;
+	r_4 deltathres = thresholdFactor * (maxMean-minMean);
+	r_4 thres =  tsys + deltathres;
+	if(debuglev_>1) cout << "DEBUG thres["<<iCh<<"] : " << thres <<endl;
+	//
+	//collect upper part of the DAB
+	if(debuglev_>1) cout <<"DEBUG collect upper part of the DAB" << endl;
+	//
+	std::vector<r_4> upperInt;
+	for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
+	  aPowerEvolEntry = powerEvolution.GetVec(ie);
+	  if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
+	    r_4 mean = aPowerEvolEntry[iCh+inOffsetCh0];
+	    if (mean >= thres) upperInt.push_back(mean);
+	  }//end cut
+	}//eo loop on channels
+	//
+	//compute adjacent differences to detect the 2 DAB levels
+	//
+	if(debuglev_>1) cout << "(DEBUG )size upper [" << iCh << "]: " << upperInt.size() <<endl;
+	std::vector<r_4> upperIntAdjDiff(upperInt.size());
+	std::adjacent_difference(upperInt.begin(),
+				 upperInt.end(),
+				 upperIntAdjDiff.begin());
+	//
+	//Search the 2 DAB states
+	if(debuglev_>1) cout<<"DEBUG Search the 2 DAB states" << endl;
+	//
+	std::vector<r_4> upIntState[2];
+	int state=-1;
+	for (size_t i=1;i<upperInt.size();i++) {//skip first value
+	  if (fabs(upperIntAdjDiff[i])<upperInt[i]*0.010) {
+	    if(state == -1) state=0;
+	    if(state == -2) state=1;
+	    upIntState[state].push_back(upperInt[i]);
+	  } else {
+	    if (state == 0) state=-2;
+	  }
+	}
+	//
+	//Take the mean of the median values of each levels
+	if(debuglev_>1)cout << "DEBUG mean of the median values of each levels" << endl;       
+	//
+	r_4 meanUpper = 0;
+	r_4 nval = 0;
+	for (int i=0;i<2;i++) {
+	  if (!upIntState[i].empty()) {
+	    std::nth_element(upIntState[i].begin(),
+			     upIntState[i].begin()+upIntState[i].size()/2,
+			     upIntState[i].end());
+	    meanUpper += *(upIntState[i].begin()+upIntState[i].size()/2);
+	    nval++;
+	  } 
+	}
+	meanUpper /= nval;
+	//
+	//Finaly the increase of power due to the DAB is:
+	//
+	r_4 deltaInt = meanUpper - tsys;
+	if(debuglev_>1) cout << "DEBUG deltaInt["<<iCh<<"] : " << deltaInt <<endl;
+	//
+	//Save for monitoring and final calibration operations
+	//
+	valCalibTuple[outOffsetCh0+outNDataPerCh*iCh]   = tsys;
+	valCalibTuple[outOffsetCh0+outNDataPerCh*iCh+1] = deltaInt;
+      }//end loop on channels
+      if(debuglev_>1) cout<<"DEBUG Fill the calibEvolution tuple" << endl;
+      calibEvolution.Fill(valCalibTuple);
+    }//eo loop on Cycles
+  }//eo loop on Modes
+  //
+  //store calibration evolution Tuple
+  //
+  if(debuglev_>0){
+    cout << "ProcessCalibration::processCmd: dump calibEvolution tuple" << endl;
+    calibEvolution.Show(cout);
+  }
+  //
+  //Compute the means per channel of the calibration coefficients (deltaInt)
+  //and save cycle based Calibration Ctes in file named as 
+  //    <source>-<date>-<mode>-<fcalib>MHz-Ch<channel>Cycles.txt
+  //   format <cycle> <coef> 
+  //the means are stored in files     
+  //    <source>-<date>-<mode>-<fcalib>MHz-All.txt
+  //   format <channel> <coef>
+  //
+  sa_size_t nModes = (sa_size_t)(maxMode - minMode) + 1;
+  string calibCoefFileName;
+  ofstream  calibMeanCoefFile[nModes]; //Mean over cycles
+  ofstream  calibCoefFile[nModes][NUMBER_OF_CHANNELS]; //cycle per cycle
+  for (sa_size_t iMode=0; iMode<nModes; iMode++){
+    //The file for the Means Coeff.
+    calibCoefFileName = outputPath_ + "/calib_" 
+      + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+      + modeList[iMode] + "_"
+      + freqBAOCalibration_ + "MHz-All.txt";
+    calibMeanCoefFile[iMode].open(calibCoefFileName.c_str());
+    //The file for the cycle per cycle Coeff.
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      stringstream chString;
+      chString << iCh;
+      calibCoefFileName = outputPath_ + "/calib_" 
+	+ dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+	+ modeList[iMode] + "_"
+	+ freqBAOCalibration_ + "MHz-Ch" + chString.str() + "Cycles.txt";
+      calibCoefFile[iMode][iCh].open(calibCoefFileName.c_str());
+    }
+  }
+
+  r_4* aCalibEvolEntry;
+  sa_size_t nCalibEvolEntry = calibEvolution.NEntry();
+
+  TMatrix<r_4> meanCalibCoef(nModes,NUMBER_OF_CHANNELS); //by default init to 0
+  TMatrix<r_4> nData4Mean(nModes,NUMBER_OF_CHANNELS);
+
+  //READ the calibration tuple, fill the array for mean and write to ascii file
+  for (sa_size_t ie=0; ie<nCalibEvolEntry; ie++){
+    aCalibEvolEntry = calibEvolution.GetVec(ie);
+    if(debuglev_>1){
+      cout << "DEBUG mode/cycle/Coef " 
+	   << aCalibEvolEntry[outModeIdx] << " "
+	   << aCalibEvolEntry[outCycleIdx] << " ";
+    }
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      if(debuglev_>1) cout << aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1] << " "; // for debug
+      sa_size_t currentMode = (sa_size_t)(aCalibEvolEntry[outModeIdx] - minMode); //ok even if minMode <> 0
+      nData4Mean(currentMode,iCh)++;
+      meanCalibCoef(currentMode,iCh) += aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1];
+
+      calibCoefFile[currentMode][iCh] << aCalibEvolEntry[outCycleIdx] << " "
+				      << setprecision(12)
+				      << aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1]
+				      << endl;
+    }
+    if(debuglev_>1) cout << endl; //for debug
+  }
+  
+  //Perform means: true means that div per 0 is treated (slower but safer) 
+  meanCalibCoef.Div(nData4Mean,true);
+  
+  if(debuglev_>0){
+    cout << "DEBUG nData4Mean" << endl;
+    nData4Mean.Print(cout);
+    cout << "DEBUG meanCalibCoef (averaged)" << endl;
+    meanCalibCoef.Print(cout);
+  }
+
+  //Save means Coef
+  for (sa_size_t iMode=0; iMode<nModes; iMode++){
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      calibMeanCoefFile[iMode] << setprecision(12)
+			       << meanCalibCoef(iMode,iCh)
+			       << endl;
+    }
+  }
+
+  //Save Monitor File
+  {
+    string fileName = outputPath_ + "/calib_monitor_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
+    POutPersist calibMonitorFile(fileName);
+    calibMonitorFile << PPFNameTag("powermoni") <<  powerEvolution;
+    calibMonitorFile << PPFNameTag("calibmoni") <<  calibEvolution;
+  }
+
+  //Clean & Return
+  for (sa_size_t iMode=0; iMode<nModes; iMode++){
+    calibMeanCoefFile[iMode].close();
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
+      calibCoefFile[iMode][iCh].close();
+    }
+  }
+
+  cout << "Ok calibration finished" << endl;  
+  return rc;
+}//ProcessCalibration::processCmd
+
Index: /BAORadio/AmasNancay/v2/v1/analysis-scriptsAndprg-description.txt
===================================================================
--- /BAORadio/AmasNancay/v2/v1/analysis-scriptsAndprg-description.txt	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/analysis-scriptsAndprg-description.txt	(revision 538)
@@ -0,0 +1,177 @@
+Title: Status of the programs and scripts used for Amas@Nancay analysis at CCIN2P3
+Revision: 7/9/11
+Author: J.E Campagne 22/7/11
+----------------------------
+
+Intro:
+All these tools are located for the while in J.E Campagne private directory, so they are not
+yet available to public usage and this note is essentially for information and software tracking.
+
+-----------------------------------------------------------
+Analysis program
+------------------------------------------------------------
+o analysis.cc
+	- perfom
+		. gain spectra determination + monitoring
+		. calibration coefficient determination + monitoring
+		. ON-OFF spectra determination (no RT calibration included)
+	- Todo
+		. include RT calibration
+	- comments
+		. ON-OFF part only validate for a single Abell85 run and not in batch mode
+		. GAIN part should be revalidated according to inPath argument
+		
+-----------------------------------------------------------
+Collection of  Bourne SHELL script for analysis
+------------------------------------------------------------
+
+A) Utilitary scripts to follow the DAQ & Analysis progress
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+o statusdaq.sh
+--------------
+	- perform
+		. scan all the /baoradio/data/AmamNancay to look at the directories and and make
+			a summary of the Data Acquisition
+
+o statusheader.sh
+-----------------
+	- perform
+		. scan Irods directories to look at header fits files extraction progress 
+		
+o statusgain.sh
+---------------
+	- perform
+		. scan Irods directories to look at FINAL gain_* files production progress 
+
+o statuscalib.sh
+----------------
+	- perform
+		. scan Irods directories to look at INTERMEDIATE calibcycle files production progress
+	- Todo
+		. scan also for FINAL calib_* files progress
+
+B) Utilitary scripts to send to BQS system high level task
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++		
+o submit2bqs-hdrfitsextractor.sh
+--------------------------------
+        - SHOULD BE DONE BEFORE gain/calib/analysis
+	- send the hdrfitsextractor.sh script to BQS system	
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+o submit2bqs-scaextractor.sh
+--------------------------------
+        - SHOULD BE DONE BEFORE gain/calib/analysis
+	- send statusdaq if necessary
+	- send the scaextractor.sh script to BQS system
+		although this script is called by getscafiles lower level script
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+o submit2qs-gainmaker.sh
+-------------------------
+	- send the gainmaker.sh script to BQS system 
+	- Todo list
+		- TO MIGRATE TO NEW BATCH
+	
+o submit2qs-calibmaker.sh
+--------------------------
+	- send the calibmaker.sh script to BQS system
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+
+		
+C) High level scripts 
+++++++++++++++++++++++		
+o hdrfitsextractor.sh
+---------------------
+	- SHOULD BE DONE BEFORE ANYTHING
+	- perform
+		. extraction of the header part of the signalXYZ.fits files and
+		. put the results into Irods	
+	- use 
+		. statusdaq.sh	
+
+o gainmaker.sh
+---------------
+	- perform 
+		1) the extraction of signalXYZ.fits files 
+			concerned by the OFF-calibration time window
+			of the cycle located at the middle of the run
+		2) . the analysis of the above fits files to determine the 
+			gains of each channel: produce gain_* files (monitoring + gain files)
+			. put the results into Irods
+	- use 
+		- statusdaq.sh, proc_script.sh, getscafiles.sh
+		- analysis.cc	
+	
+o calibmaker.sh
+---------------
+	- perfom
+		1) the extraction of signalXYZ.fits files 
+			concerned by the OFF & ON calibration time window of all cycles concerned
+		2) . the analysis of the above fits files to determine the 
+			calibration constantes of each channel: produce calib_* files (monitoring +  calibration files)
+			. put the results into Irods
+	- use 
+		- statusdaq.sh, proc_script.sh, getscafiles.sh
+		- analysis.cc	
+
+D) Lower level scripts
+++++++++++++++++++++++
+		
+o proc_script.sh
+-----------------
+	- VERY BUSY script
+	- perform 
+		1) all the preparation work to send in batch the "specmfib" program 
+			which perform the FFT and median/mean spectra for GAIN, CALIBRATION, ON-OFF task
+		2) the script that is sent in batch put the results into Irods	
+	- use
+		- getscafiles.sh, select.sh, getsignalfiles.sh
+	- Todo list
+		- TO MIGRATE TO NEW BATCH 
+		
+
+o scaextractor.sh
+-----------------
+	- perform
+		1) extraction of the scaXYZ file
+		2) if the scaXYZ.sum and the scaXYZ.sum.trans files do not exist
+			. create and put them into Irods
+			. scaXYZ.sum = summary of the SCA file
+			. scaXYZ.sum.trans = transformation of the date format of the scaXYZ.sum file	
+	- use
+		- sca.sh, transdate.sh
+		
+o sca.sh
+--------
+	- perform
+		. produce the scaXYZ.sum (= summary of the SCA file) from a scaXYZ file
+		
+o transdate.sh
+--------------
+	- perform
+		. produce the scaXYZ.sum.trans (= transformation of the date format) from the scaXYZ.sum file	
+
+o select.sh
+-----------
+	-perform 
+		. the selection of the signalXYZ.fits files according to the type of task requested
+			as ON, OFF, CALIBON, CALIBOFF, GAIN
+		
+o getscafiles.sh
+----------------
+	- perform
+		. download from Irods the scaXYZ, scaXYZ.sum and scaXYZ.sum.trans files	
+	- use
+		scaextractor.sh
+		
+o getsignals.sh
+---------------
+	- perform
+		. download from Irods the signalXYZ.fits files
+		
+		
+		
+
Index: /BAORadio/AmasNancay/v2/v1/calibmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/calibmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/calibmaker.sh	(revision 538)
@@ -0,0 +1,422 @@
+#!/bin/sh -xvf
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+#which source to analyse
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+freqBAOCalib=1346
+bwBAOCalib=6.25
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+      -freq) freqBAOCalib=$2; shift;;
+      -bwd)  bwBAOCalib=$2; shift;;
+      -sim) simulationMode=On; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce [-freq freq in MHz (default ${freqBAOCalib})] [-bwd bandwidth in MHz (default ${bwBAOCalib})]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+#wakeup the NFS disk
+$LS -l ${tmppublicpath} > /dev/null
+#clean previous spurious files
+$RM -f ${tmppublicpath}/*.finished
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+
+#Nb: if we want to implement a loop on sources we have to take care of srclower...
+
+$ECHO "You have selected sourceRadio = $sourceRadio"
+tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+IFS='
+' 
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+#
+# start a "fresh" session  
+  cd ${topDir}
+  $LS | $XARGS -i $RM -rf {}
+  
+  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+
+  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
+  $LS -lrt
+
+
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the calibration files already exist
+#
+  alreadyDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
+  if [ ${alreadyDone} -gt 0 ]; then
+      $ECHO "INFO (${scriptName}): calibration coef files already exist"
+      continue
+  fi
+#
+  somethingTodo=1
+  input4calibOff=`ils ${i}/Off 2>&1 | $GREP -i "calibcycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n`  
+  input4calibOn=`ils ${i}/On 2>&1 | $GREP -i "calibcycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n`  
+#
+  if [ "<${input4calibOff}>" != "<>" -a "<${input4calibOn}>" != "<>"  ]; then
+#look at the content of the calibcycle directory as may be the previous batch job has crashed
+      tableauOff=( `$ECHO ${input4calibOff}` )
+      IFS='
+'
+      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOff=0
+      for j in ${tableauOff[@]}
+	do
+	nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
+      done
+#
+      tableauOn=( `$ECHO ${input4calibOn}` )
+      IFS='
+      '
+      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+#
+      nTotFilesOn=0
+      for j in ${tableauOn[@]}
+	do
+	nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
+	nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
+      done
+      diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
+      if [ ${diffOnOff} -lt 0 ]; then
+	  zero=0
+	  diffOnOff=`expr ${zero} - ${diffOnOff}` 
+      fi
+#allow up to 5 files differences
+      if [  ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0  ]; then
+	  somethingTodo=0
+	  $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
+      else
+	  $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
+      fi
+# 
+  fi 
+  if [ ${somethingTodo} -eq 1 ]; then
+#
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ CALIBOFF"
+      $RM -f ${tmppublicpath}/proc_script.finished
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBOFF"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBOFF
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ CALIBOFF"
+      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ CALIBON"
+      $RM ${tmppublicpath}/proc_script.finished
+      
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBON"
+      else
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBON
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $RM ${tmppublicpath}/proc_script.finished
+#Todo see if there is errors in the log file before copy
+      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ CALIBON"
+######
+  else
+######
+      $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
+      #go to the .../source/date-sourcelowercase directory
+#download Off/calibcycle<> and On/calibcycle<> directories
+      $MKDIR -p ${topDir}/${dateDAQ}${srclower}
+      cd ${topDir}/${dateDAQ}${srclower}
+      aboveOnOffDir=`pwd`
+      $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
+#
+      mode="Off"
+      $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
+      inFileDirectory="./${mode}"
+      $MKDIR -p ${inFileDirectory}
+      listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+      IFS='
+'
+      listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+
+      firstCycleOff=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+      nCyclesOff=`expr ${#listOfCalibCycle[*]} - 1`
+
+      lastCycleOff=`$ECHO ${listOfCalibCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+      listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
+
+      
+      for j in ${listOfCalibCycle[@]}
+	do
+	irodsDownDir="${j}"
+	OUT=./getInFits.$$
+	iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget fits files:"
+	    $ECHO $igetStatus
+	    $ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	    continue
+	fi
+	$RM -f ${OUT}
+      done
+      $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
+#
+      mode="On"
+      $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
+      inFileDirectory="./${mode}"
+      $MKDIR -p ${inFileDirectory}
+
+      listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+      IFS='
+'
+      listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
+      IFS=$DefaultIFS
+
+      firstCycleOn=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+      nCyclesOn=`expr ${#listOfCalibCycle[*]} - 1`
+
+      lastCycleOn=`$ECHO ${listOfCalibCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+
+      listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
+
+
+#
+#Check if there is the same number of cycles in Off and On mode
+#      
+#first cycle = max(firstCycleOn, firstCycleOff)
+#last  cycle = min(lastCycleOn,lastCycleOff)
+
+      if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
+	  firstCycle=${firstCycleOff}
+	  lastCycle=${lastCycleOff}
+      else
+	  $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
+	  $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
+	  firstCycle=${firstCycleOff}
+	  if [ ${firstCycleOn} -gt ${firstCycle} ]; then
+	      firstCycle=${firstCycleOn}
+	  fi
+	  lastCycle=${lastCycleOff}
+	  if [ ${lastCycleOn} -lt ${lastCycle} ]; then
+	      lastCycle=${lastCycleOn}
+	  fi
+      fi
+      $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
+      
+
+      for j in ${listOfCalibCycle[@]}
+	do
+	irodsDownDir="${j}"
+	OUT=./getInFits.$$
+	iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+	igetStatus=`$GREP "^ERROR" ${OUT}`
+	if [ "<$igetStatus>" != "<>" ]; then
+	    $ECHO "FATAL (${scriptName}): error while iget fits files:"
+	    $ECHO $igetStatus
+	    $ECHO " ==> skip this run $sourceRadio $dateDAQ"
+	    continue
+	fi
+	$RM -f ${OUT}
+      done
+      $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
+#Get sca file
+      $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+      OUT1=./getScaStatus.$$
+      $RM -f ${tmppublicpath}/getscafiles.finished
+      ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+      while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+	  $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+	  date +%T
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/getscafiles.finished
+      $ECHO "DEBUG (${scriptName}): END"
+#
+      getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+#	
+      if [ "<${getScaStatus}>" != "<>" ]; then
+	  $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+	  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+	  $CAT ${OUT1}
+	  $ECHO "DEBUG (${scriptName}): END"
+	  continue
+      fi
+      $RM -f ${OUT1}
+
+      scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
+      
+#prepare job submission
+      cd ${aboveOnOffDir}
+#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
+
+      logFile="analyse_CALIB_${sourceRadio}_${dateDAQ}.log"
+      $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle -numcycle ${firstCycle},${lastCycle} -calibopt mean  -debuglev 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}"
+	  cat > ${logFile} <<EOF
+	  Ok calibration finished
+EOF
+      else
+	  ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle  -numcycle ${firstCycle},${lastCycle} -calibopt mean  -debuglev 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}
+      fi
+      $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
+      rcstatus=`$GREP -i "Ok calibration finished" ${logFile} | $WC -l`
+      if [ ${rcstatus} -eq 0 ]; then
+	  $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
+	  $ECHO "                      START logfile"
+	  $CAT ${logFile}
+	  $ECHO "                      END   logFile"
+#clean and leave
+	  cd ${aboveOnOffDir}
+	  $LS | $XARGS -i $RM -rf {}
+	  continue
+      fi
+#Save into Irods
+      $ECHO "DEBUG (${scriptName}): START save output files"
+      $LS ${aboveOnOffDir}
+#use -f option for iput ONLY to force override
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: here we look for calib file and put them in Irods"
+      else
+	  $FIND . -name "calib_*" -print | $XARGS -i  iput  -v -K {} ${i}
+      fi
+#save analysis logfile
+      $LS -l ${tmppublicpath} > /dev/null
+      $CP ${logFile} ${tmppublicpath}
+      $ECHO "DEBUG (${scriptName}): END save output files"
+#clean up to avoid scratch SIZE EXCEED LIMIT
+      cd ${aboveOnOffDir}
+      $LS | $XARGS -i $RM -rf {}
+  fi
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/gainmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/gainmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/gainmaker.sh	(revision 538)
@@ -0,0 +1,291 @@
+#!/bin/sh -xvf
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+#$ECHO "Processing script ${scriptName} at `date`"
+
+#$ECHO "SHOULD BE RETUNED FOR THE inPath cmd line of analyse.cc (Sorry...): 21/7/11 by JEC"
+#exit 1
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=Off
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+      -sim) simulationMode=On; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce [-sim]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+if [ ${simulationMode} = "On" ]; then
+    $ECHO "INFO ${scriptName} running in SIMUL mode"
+fi
+
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+case ${sourceRadio} in
+    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
+    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
+    exit 1;;
+esac
+
+
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#save the top directory
+topDir=`pwd`
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+#
+#We forbib to perform the gain for all the sources in one shot!
+#
+#if [ ! "<${sourceRadio}>" = "All" ]; then
+$ECHO "You have selected sourceRadio = $sourceRadio"
+tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+#else
+#    tableau=( `$CAT ${OUT1} | $AWK '( NF==4 ) { print $2 }'`  )
+#fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+
+for i in ${tableau[@]}
+  do
+  $ECHO "DEBUG (${scriptName}): irods root dir $i"
+  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
+#Look if the date has changed since beginning of the job!
+  curtag=`${DATE} +%F`
+  if [ "<${curtag}>" != "<${tag}>" ]; then
+      tag=${curtag}
+#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
+      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
+      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+      $RM -f ${tmppublicpath}/statusdaq.finished
+      ${scriptpath}/statusdaq.sh > ${OUT1}
+      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	  sleep 30
+      done
+      $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+      $CHMOD -v 444 ${OUT1} 
+  fi
+#
+# look if the fits file of the gains already exists
+  alreadyDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
+  if [ ${alreadyDone} -gt 0 ]; then
+      $ECHO "INFO (${scriptName}): Fits file for gains already exists"
+      continue
+  fi
+# Something should be done...
+#look if the files for the input to gain computations have been done
+  somethingTodo=1
+  input4gainOk1=`ils ${i}/Off 2>&1 | $GREP -i "^ERROR" | $WC -l` 
+  input4gainOk2=`ils ${i}/Off 2>&1 | $GREP -i "gaincycle"` 
+  if [ $input4gainOk1 -eq 0 -a "<${input4gainOk2}>" != "<>"  ]; then
+      gaincycle=`$ECHO ${input4gainOk2} | $AWK '($1 == "C-"){print $2}'| $XARGS -i  basename {}`
+      input4gainOk3=`ils ${i}/Off/${gaincycle} 2>&1 | $GREP -i "medfiltmtx" | $WC -l`
+      if [ $input4gainOk3 -gt 0 ]; then
+	  somethingTodo=0
+      fi
+  fi
+  if [ $somethingTodo -eq 1 ]; then
+#
+      $ECHO ">>>>>>>>>>>>>>>> Do proc_script $sourceRadio $dateDAQ GAIN"
+      $RM -f ${tmppublicpath}/proc_script.finished
+      if [ ${simulationMode} = "On" ]; then
+	  $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ GAIN"
+      else      
+	  ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ GAIN
+	  while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
+	      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
+	      date +%T
+	      sleep 60
+	  done
+      fi
+      $ECHO "INFO (${scriptName}): proc_script has finished"
+      $RM ${tmppublicpath}/proc_script.finished
+#Todo see if there is errors in the log file before copy
+      $ECHO ">>>>>>>>>>>>>>>> Done proc_script $sourceRadio $dateDAQ GAIN"
+######
+  fi
+######
+  $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
+#go to the .../source/date-sourcelowercase directory
+  $MKDIR -p ${topDir}/${dateDAQ}${srclower}
+  cd ${topDir}/${dateDAQ}${srclower}
+  aboveOnOffDir=`pwd`
+  $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
+# So the input files are there, so the fits file can be determined
+  mode="Off"
+  spectraGenDirName=`$ECHO ${gaincycle} | $AWK '{match($0,"[a-zA-Z]+",arr); print arr[0]}'`
+  cycleNumber=`$ECHO ${gaincycle} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
+#care: the gaincycle variable contains the cycle number
+  $ECHO "DEBUG (${scriptName}): START download the fits files"
+  inFileDirectory="./${mode}"
+  $MKDIR -p ${inFileDirectory}
+  irodsDownDir="${i}/${mode}/${gaincycle}"
+  OUT=./getInFits.$$
+  iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
+  $LS ${inFileDirectory}/${gaincycle}
+  igetStatus=`$GREP "^ERROR" ${OUT}`
+  if [ "<$igetStatus>" != "<>" ]; then
+      $ECHO "FATAL (${scriptName}): error while iget fits files:"
+      $ECHO $igetStatus
+      continue
+  fi
+  $RM -f ${OUT}
+  $ECHO "DEBUG (${scriptName}): END download the fits files"
+#Get sca files
+  $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+  OUT1=./getScaStatus.$$
+  $RM -f ${tmppublicpath}/getscafiles.finished
+  ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+  while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+      date +%T
+      sleep 30
+  done
+  $RM ${tmppublicpath}/getscafiles.finished
+  $ECHO "DEBUG (${scriptName}): END"
+#
+  getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+#	
+  if [ "<${getScaStatus}>" != "<>" ]; then
+      $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+      $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+      $CAT ${OUT1}
+      $ECHO "DEBUG (${scriptName}): END"
+      continue
+  fi
+  $RM -f ${OUT1}
+  $ECHO "DEBUG (${scriptName}): where am i `pwd`"
+  $LS -lrt
+  
+  scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
+#	
+#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
+  logFile="analyse_GAIN_${sourceRadio}_${dateDAQ}.log"
+  $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
+  if [ ${simulationMode} = "On" ]; then
+      $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act gain  -inPath ${iojobpath} -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir ${spectraGenDirName} -mode ${mode} -numcycle ${cycleNumber},${cycleNumber} -debuglev 1 >& ${logFile}"
+      cat > ${logFile} <<EOF
+      Ok gain finished
+EOF
+
+  else
+      ${scriptpath}/Objs/analyse -act gain  -inPath ${iojobpath} -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir ${spectraGenDirName} -mode ${mode} -numcycle ${cycleNumber},${cycleNumber} -debuglev 1 >& ${logFile}
+  fi
+#
+  $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
+  rcstatus=`$GREP -i "OK gain finished" ${logFile} | $WC -l`
+  if [ ${rcstatus} -eq 0 ]; then
+      $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
+      $ECHO "                      START logfile"
+      $CAT ${logFile}
+      $ECHO "                      END   logFile"
+#clean and leave
+      cd ${aboveOnOffDir}
+      $RM -rf ./*
+      continue
+  fi
+#Save into Irods
+  $ECHO "DEBUG (${scriptName}): START save output files"
+  $LS ${aboveOnOffDir}
+#use -f option of iput ONLY to force override
+  if [ ${simulationMode} = "On" ]; then
+      $ECHO "SIMUL: here we look for gain file and put them in Irods"
+  else
+      $FIND . -name "gain_*" -print | $XARGS -i  iput  -v -K {} ${i}
+  fi
+#save analysis logfile
+  $LS -l ${tmppublicpath} > /dev/null
+  $CP ${logFile} ${tmppublicpath}
+  $ECHO "DEBUG (${scriptName}): END save output files"
+#clean
+  cd ${aboveOnOffDir}
+  $RM -rf ./*
+done
+
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/getscafiles.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/getscafiles.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/getscafiles.sh	(revision 538)
@@ -0,0 +1,153 @@
+#!/bin/sh
+#Get SCA file and summary + transformed date file
+#
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+TR=/usr/bin/tr
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+
+if [ ! $# = 2 ]; then
+  $ECHO "usage: ${script
+Name} <source:ex. Abell85> <date:YYYYMMDD>"
+  touch ${tmppublicpath}/getscafiles.finished
+  exit 0
+fi
+
+# Irods environment settings
+#export PATH=$PATH:/home/bao/irods2.5_LinuxSL5
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+
+sourceRadio=$1
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+dateDAQ=$2
+
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+ iojobpath=${TMPBATCH}
+else
+   $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+   touch ${tmppublicpath}/getscafiles.finished
+   exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateDAQ}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#look if the run exists
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+#    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+#    $RM -f ${tmppublicpath}/statusdaq.finished
+#    ${scriptpath}/statusdaq.sh > ${OUT1}
+#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+# 	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/statusdaq.finished"
+# 	date +%T
+# 	sleep 30
+#     done
+#     $RM ${tmppublicpath}/statusdaq.finished
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/getscafiles.finished
+    exit 1
+fi
+
+nfiles=`${GREP} "${sourceRadio}.*${dateDAQ}" ${OUT1} | ${AWK} '(NF==4){print $4}'` 
+
+if [ "<${nfiles}>" = "<>" ]; then
+    $ECHO "FATAL (${scriptName}): No run found for <${sourceRadio}> and date <${dateDAQ}>"
+    touch ${tmppublicpath}/getscafiles.finished
+    exit 1
+fi
+
+
+#Look if the run should/can be prepared wrt SCA file transformation 
+OUT2=./scaStatus.$$
+$RM -f ${tmppublicpath}/scaextractor.finished
+${scriptpath}/scaextractor.sh $sourceRadio > ${OUT2} 2>&1
+while [ ! -f "${tmppublicpath}/scaextractor.finished" ]; do
+    $ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/scaextractor.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/scaextractor.finished
+
+
+status=`$GREP "^FATAL.*${sourceRadio}.*${dateDAQ}" ${OUT2}`
+
+if [ "<$status>" != "<>" ]; then
+    $ECHO "FATAL (${scriptName}): connot proceed further with $sourceRadio $dateDAQ"
+    touch ${tmppublicpath}/getscafiles.finished
+    exit 1
+fi
+$RM $OUT2
+
+#dowload sca files...
+dirIrod="/baoradio/data/AmasNancay/${sourceRadio}/${dateDAQ}${srclower}"
+dirLocal="."
+
+if [ ! -e $dirLocal ]; then
+ $MKDIR -p $dirLocal
+fi
+
+cd $dirLocal
+
+OUT3=./scaFiles.$$
+ils $dirIrod | $GREP -i "sca" > ${OUT3}
+tableau=( `$CAT ${OUT3}`  )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+$RM $OUT3
+
+OUT4=./igetStatus.$$
+for i in ${tableau[@]}
+do
+  if [ ! -e $i ]; then
+    iget -r -f -v -K ${dirIrod}/$i ${dirLocal} > ${OUT4} 2>&1
+  fi
+done
+
+igetStatus=`$GREP "^ERROR" ${OUT4}`
+if [ "<$igetStatus>" != "<>" ]; then
+  $ECHO "FATAL (${scriptName}): error while iget:"
+  $ECHO $igetStatus
+  touch ${tmppublicpath}/getscafiles.finished
+  exit 1
+fi
+
+$RM -f $OUT4
+
+touch ${tmppublicpath}/getscafiles.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/getsignalfiles.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/getsignalfiles.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/getsignalfiles.sh	(revision 538)
@@ -0,0 +1,151 @@
+#!/bin/sh
+#
+#Script sh d'extraction de fichiers d'irod 
+# Auteur: JE Campagne (LAL Orsay)
+# Creation: 2 mai 2011
+# adaptation pour le CC 17 juin 2011
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+TR=/usr/bin/tr
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+
+
+if [ ! $# = 4 ]; then
+  $ECHO "usage: ${scriptName} <source:ex. Abell85> <date:YYYYMMDD> first last"
+  touch ${tmppublicpath}/getsignalfiles.finished
+  exit 0
+fi
+
+# Irods environment settings
+#export PATH=$PATH:/home/bao/irods2.5_LinuxSL5
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+sourceRadio=$1
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+dateDAQ=$2
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+   $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+   touch ${tmppublicpath}/getsignalfiles.finished
+   exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateDAQ}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#determine the sequence of signal files to download
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+#     $ECHO "We should bring the DAQ status more up to date, this take few 
+# sec..."
+#     $RM -f ${tmppublicpath}/statusdaq.finished
+#     ${scriptpath}/statusdaq.sh > ${OUT1}
+#     while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+# 	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/statusdaq.finished"
+# 	date +%T
+# 	sleep 30
+#     done
+#     $RM ${tmppublicpath}/statusdaq.finished
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+nfiles=`${GREP} "${sourceRadio}.*${dateDAQ}" ${OUT1} | ${AWK} '(NF==4){print $4}'` 
+if [ "<${nfiles}>" == "<>" ]; then
+    $ECHO "FATAL (${scriptName}): No run found for <${sourceRadio}> and date <${dateDAQ}>"
+    $CAT $OUT1
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+
+firstFile=$3
+if [ ${firstFile} -gt  ${nfiles} ]; then
+    $ECHO "FATAL (${scriptName}): ${firstFile} >  ${nfiles}"
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+
+lastFile=$4
+if [ ${lastFile} -gt  ${nfiles} ]; then
+    $ECHO "FATAL (${scriptName}): ${lastFile} >  ${nfiles}"
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+if [ ${firstFile} -gt  ${lastFile} ]; then
+    $ECHO "FATAL (${scriptName}): ${firstFile} >  ${lastFile}"
+    touch ${tmppublicpath}/getsignalfiles.finished
+    exit 1
+fi
+
+
+dirIrod="/baoradio/data/AmasNancay/${sourceRadio}/${dateDAQ}${srclower}"
+dirLocal="."
+
+##$ECHO "Ready to download [${firstFile},${lastFile}] in $dirLocal from $dirIrod"
+#Nb we are in the localpath
+$MKDIR -p Fiber1
+$MKDIR -p Fiber2
+
+OUT2=./igetStatus.$$
+
+n=$firstFile
+while [[ $n -le $lastFile ]]; do
+   iget -r -f -v -K ${dirIrod}/Fiber1/signal$n.fits ${dirLocal}/Fiber1 >${OUT2} 2>&1
+   igetStatus=`$GREP "^ERROR" ${OUT2}`
+   if [ "<$igetStatus>" != "<>" ]; then
+       $ECHO "FATAL (${scriptName}): error while iget:"
+       $ECHO $igetStatus
+       touch ${tmppublicpath}/getsignalfiles.finished
+       exit 1
+   fi
+   iget -r -f -v -K ${dirIrod}/Fiber2/signal$n.fits ${dirLocal}/Fiber2 >${OUT2} 2>&1
+   igetStatus=`$GREP "^ERROR" ${OUT2}`
+   if [ "<$igetStatus>" != "<>" ]; then
+       $ECHO "FATAL (${scriptName}): error while iget:"
+       $ECHO $igetStatus
+       touch ${tmppublicpath}/getsignalfiles.finished
+       exit 1
+   fi
+   n=$(($n+1))
+done
+
+touch ${tmppublicpath}/getsignalfiles.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/hdrfitsextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/hdrfitsextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/hdrfitsextractor.sh	(revision 538)
@@ -0,0 +1,152 @@
+#!/bin/sh
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+defaultIFS=$IFS
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#which source to analyse
+sourceRadio=$1
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+fi
+
+if [ ! "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "You have selected sourceRadio = $sourceRadio"
+    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+else
+    tableau=( `$CAT ${OUT1} | $AWK '( NF==4 ) { print $2 }'`  )
+fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+
+
+workArea="headers"
+$MKDIR -p ./${workArea}
+cd ./${workArea}
+
+
+nloop4dbg=0
+
+
+for i in ${tableau[@]}
+do
+#look if the headers have been already extracted
+  $ECHO "DEBUG (${scriptName}): irods root dir $i"
+  for fibdir in Fiber1 Fiber2; do
+      $ECHO "DEBUG (${scriptName}): Fiber $fibdir"
+#NB: care should be taken to the fact that ILS returns all the instance of the file (HPSS, disk...)
+#    so one should use a trick to count unique instance
+      listOfsig=( `ils ${i}/${fibdir} | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+      IFS='
+'
+      listOfsig=( $( $PRINTF "%s\n" "${listOfsig[@]}" | $AWK 'x[$0]++ == 0' ) )
+      nblinesSIG=${#listOfsig[@]}
+      listOfhdr=( `ils ${i}/${fibdir} | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+      IFS='
+'
+      listOfhdr=( $( $PRINTF "%s\n" "${listOfhdr[@]}" | $AWK 'x[$0]++ == 0' ) )
+      nblinesHDR=${#listOfhdr[@]}
+      $ECHO "(DEBUG) (${scriptName}:  test if $nblinesHDR != $nblinesSIG "
+      if [ $nblinesHDR != $nblinesSIG ]; then
+#do the header extraction
+#build signal files list 
+#loop on signal files
+	  $ECHO "(DEBUG) (${scriptName}):  ${listOfsig[@]}"
+
+	  for sigfname in ${listOfsig[@]}
+	    do
+	    filename=$(basename $sigfname)
+	    extension=${filename##*.}
+	    filename=${filename%.*}
+            number=`$ECHO $filename | $AWK '{len=length("signal");len=len+1;a=substr($1,len);print a}'`
+	    hdrname="header${number}.txt"
+#look if header file already exists: if so skip it
+	    hdrIsthere=`ils ${i}/${fibdir}/${hdrname} 2>&1 | $GREP -i "^ERROR" | $WC -l`
+	    $ECHO "(DEBUG) (${scriptName}): look if ${hdrname}  exist (${hdrIsthere})"
+	    if [ $hdrIsthere -eq 0 ]; then 
+		continue
+	    fi
+#get the signal file
+	    $ECHO "DEBUG (${scriptName}): iget $sigfname (dgg = ${nloop4dbg})"
+            OUT3=./igetlog${nloop4dbg}.$$
+	    iget -f -v -K ${i}/${fibdir}/${sigfname} . >${OUT3} 2>&1
+	    igetStatus=`$GREP "^ERROR" ${OUT3}`
+	    if [ "<$igetStatus>" != "<>" ]; then
+		$ECHO "INFO (${scriptName}): error while iget:"
+		$ECHO $igetStatus
+		continue
+	    fi
+            $RM $OUT3
+#extract the header part of the  signal file
+	    $ECHO "DEBUG (${scriptName}): get header from ${sigfname} and put it in ${hdrname}"
+	    IFS=$defaultIFS
+	    while read -n80 L
+	      do
+	      [ "${L::3}" = "END" ] && break
+	      $ECHO $L
+	    done < ${sigfname} > ${hdrname}
+	    $RM ${sigfname}
+	    nhdrlines=`$WC -l ${hdrname} | $AWK '{print $1}' `
+	    if [  "${nhdrlines}" = "0"  ]; then
+		$ECHO "WARNING (${scriptName}): ${sigfname} has no valid header"
+	    else
+#transfert to irod the header
+		$ECHO "INFO (${scriptName}): transfert ${hdrname} to ${i}/${fibdir}"
+#use -f option ONLY to force override
+		iput  -v -K ${hdrname} ${i}/${fibdir}
+	    fi
+            $RM ${hdrname}
+	    nloop4dbg=$(expr $nloop4dbg + 1)
+	  done
+      else
+	  $ECHO "INFO (${scriptName}): nothing to do for ${i}/${fibdir}"
+      fi
+  done
+done
Index: /BAORadio/AmasNancay/v2/v1/makefile
===================================================================
--- /BAORadio/AmasNancay/v2/v1/makefile	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/makefile	(revision 538)
@@ -0,0 +1,30 @@
+include $(SOPHYABASE)/include/sophyamake.inc
+
+OBJ = ./Objs/
+EXE = ./Objs/
+
+
+PKGOLIST =
+
+EXELIST = $(EXE)analyse
+
+EXEOLIST = $(EXE)analyse.o
+
+
+all : analyse
+
+clean :
+	rm -f $(PKGOLIST) $(EXELIST) $(EXEOLIST)
+
+########################################################
+
+#......
+analyse : $(EXE)analyse
+	echo '---analyse made'
+
+$(EXE)analyse : $(OBJ)analyse.o $(PKGOLIST)
+	$(CXXLINK) -o $(EXE)analyse $(OBJ)analyse.o $(PKGOLIST) $(SOPHYAALLSLBLIST)
+
+$(OBJ)analyse.o : analyse.cc $(PKGHLIST)
+	$(CXXCOMPILE) -c  -o $(OBJ)analyse.o analyse.cc 
+
Index: /BAORadio/AmasNancay/v2/v1/proc_script.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/proc_script.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/proc_script.sh	(revision 538)
@@ -0,0 +1,501 @@
+#!/bin/sh -xv
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+TR=/usr/bin/tr
+FIND=/usr/bin/find
+PRINTF=/usr/bin/printf
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+QSUB=qsub
+
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+######
+# Local functions
+######
+usage() {
+  echo "usage: $scriptName <source> <YYYYMMDD-directory> <type of process>"
+  echo "      -<source> as Abell85"
+  echo "      -<YYYYMMDD-directory> as 20110428"
+  echo "      -<type of process> as GAIN|CALIBON|CALIBOFF|ON|OFF"
+}
+######
+# argument test
+######
+if [ ! $# = 3 ]; then
+  usage
+  touch proc_script.finished
+  exit 1
+fi
+sourceRadio=$1
+srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
+dateDAQ=$2
+typeofproc=$3
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+localpath="${sourceRadio}/${dateDAQ}${srclower}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+mainDirectory=`pwd`
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#select file per cycle
+#$ECHO "Select files process..."
+#keep only lines with structure: cycle_number first,last,step
+#Get sca files
+$ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
+OUT1=./getScaStatus.$$
+$RM -f ${tmppublicpath}/getscafiles.finished
+${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
+while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
+    $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/getscafiles.finished
+$ECHO "DEBUG (${scriptName}): END"
+
+
+getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
+
+if [ "<${getScaStatus}>" != "<>" ]; then
+  $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
+  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+  $CAT ${OUT1}
+  $ECHO "DEBUG (${scriptName}): END"
+  touch ${tmppublicpath}/proc_script.finished
+  exit 1
+fi
+
+
+scaFileSummarized=`$FIND . -name "sca*.sum" -print`
+if [ "<${scaFileSummarized}>" = "<>" ]; then
+  $ECHO "FATAL (${scriptName}): strange sca file summarized not available: $sourceRadio $dateDAQ"
+  $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
+  $CAT ${OUT1}
+  $ECHO "DEBUG (${scriptName}): END"
+  touch ${tmppublicpath}/proc_script.finished
+  exit 1
+fi
+$RM ${OUT1}
+
+scaFileSummarized=`basename $scaFileSummarized`
+
+#set Irods environment
+. /usr/local/shared/bin/irods_env.sh -noverbose
+#download header files to be used for signal files selction
+$MKDIR -p ./Fiber1
+dirFiberfile=`pwd`/Fiber1
+
+dirIrods="/baoradio/data/AmasNancay/${sourceRadio}/${dateDAQ}${srclower}/Fiber1"
+
+listOfFiles=( `ils ${dirIrods} | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+IFS='
+'
+listOfFiles=( $( $PRINTF "%s\n" "${listOfFiles[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+#
+$ECHO "DEBUG (${scriptName}): Download if necessary the headers"
+#
+for ifile in ${listOfFiles[@]}
+do
+#test if the headers have not yet been downloaded
+  if [ ! -e ${dirFiberfile}/${ifile} ]; then
+      OUT2=./getheaders.$$
+      iget -f -K ${dirIrods}/${ifile} ${dirFiberfile} >${OUT2} 2>&1
+      igetStatus=`$GREP "^ERROR" ${OUT2}`
+      if [ "<$igetStatus>" != "<>" ]; then
+	  $ECHO "FATAL (${scriptName}): error while iget:"
+	  $ECHO $igetStatus
+	  touch ${tmppublicpath}/proc_script.finished
+	  exit 1
+      fi
+      $RM -f $OUT2
+  fi
+done
+#
+$ECHO "DEBUG (${scriptName}): START call select.sh ${scaFileSummarized} ${dirFiberfile} ${typeofproc}"
+#
+#Get list of file to process
+headerFileSelected=./selectSignal.$$
+$RM -f ${tmppublicpath}/select.finished
+${scriptpath}/select.sh ${scaFileSummarized} ${dirFiberfile} ${typeofproc} | $GREP "^#[0-9]" | $SED "s/^#//" > $headerFileSelected
+while [ ! -f "${tmppublicpath}/select.finished" ]; do
+    $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/select.finished"
+    date +%T
+    sleep 30
+done
+$RM ${tmppublicpath}/select.finished
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+$ECHO "DEBUG (${scriptName}): START filling cycleArray, filesArray"
+#
+declare -a cycleArray
+declare -a filesArray
+$ECHO "DEBUG (${scriptName}): IFS value between<>: <$IFS>"
+IFS=$DefaultIFS
+$ECHO "DEBUG (${scriptName}): START cat $headerFileSelected"
+$CAT $headerFileSelected
+$ECHO "DEBUG (${scriptName}): END cat $headerFileSelected"
+
+while read v1 v2
+do
+ cycleArray[${#cycleArray[*]}]=$v1
+ filesArray[${#filesArray[*]}]=$v2
+done < $headerFileSelected
+
+if [ ${#cycleArray[*]} -eq 0 -o ${#filesArray[*]} -eq 0 ]; then
+   $ECHO "FATAL (${scriptName}): no header file selected (${#cycleArray[*]}, ${#filesArray[*]})"
+   touch ${tmppublicpath}/proc_script.finished
+   exit 1
+else
+   $ECHO "DEBUG (${scriptName}): non zero elements found for cycleArray and filesArray" 
+fi
+
+#
+$ECHO "DEBUG (${scriptName}): number of elements <${#cycleArray[*]}> <${#filesArray[*]}>"
+$ECHO "DEBUG (${scriptName}): END"
+#
+# (timeShift)
+#Add 3sec to start ON / OFF
+#Substract 4sec to start DAB ON / OFF
+#
+#Take care: Cycle numbering starts at 1 in SCAFiles while Arrays index start at 0
+#
+#for GAIN the reference cycle takes the middle of the run
+#
+$ECHO "DEBUG (${scriptName}): START switch on $typeofproc"
+#
+case "$typeofproc" in
+    GAIN)
+      $ECHO "process signal files for GAIN phase"
+      ingain=""
+      freqfilter="-freqfilter -"
+      tspwin="5120"
+      dirName="Off"
+      cyclebasename="gaincycle"
+      indexStartHour="6"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId=`expr  ${#cycleArray[*]} / 2`
+      firstCycleId=`expr ${firstCycleId} - 1`
+      lastp1CycleId=`expr ${firstCycleId} + 1`
+      ;;
+    ON)
+      $ECHO "process signal files for ON phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      tspwin="25000"
+      dirName="On"
+      cyclebasename="datacycle"
+      indexStartHour="4"
+      timeShift="3"
+      timeDuration="30"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    OFF)
+      $ECHO "process signal files for OFF phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      tspwin="25000"
+      dirName="Off"
+      cyclebasename="datacycle"
+      indexStartHour="8"
+      timeShift="3"
+      timeDuration="30"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    CALIBON)
+      $ECHO "process signal files for CALIBRATION ON phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      tspwin="1024"
+      dirName="On"
+      cyclebasename="calibcycle"
+      indexStartHour="2"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    CALIBOFF)
+      $ECHO "process signal files for CALIBRATION OFF phases"
+      ingain="-gain gain_${dateDAQ}_${srclower}.fits"
+      freqfilter="-freqfilter 16"
+      tspwin="1024"
+      dirName="Off"
+      cyclebasename="calibcycle"
+      indexStartHour="6"
+      timeShift="-4"
+      timeDuration="14"
+      firstCycleId="0"
+      lastp1CycleId="${#cycleArray[@]}"
+      ;;
+    *)
+    $ECHO "FATAL (${scriptName}): type of processing"
+    usage
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+    ;;
+esac
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+#
+#Compute time windows (Warning usage of GMT date)
+#
+$ECHO "DEBUG (${scriptName}): START compute time windows"
+OUT=./baotiming.$$
+${AWK} -v datesys=${DATE} -v type=${indexStartHour} -v shift=${timeShift} '
+BEGIN {FS=","}
+{
+  phaseStart=$type
+  cmd=sprintf("%s +%%s -d  \"%s\" ",datesys,phaseStart);
+  (cmd | getline phstart);
+  close(cmd);
+  newstart = phstart+shift;
+  cmd=sprintf("%s +%%T -d \"1970-01-01 %s sec GMT\"",datesys,newstart);
+  (cmd | getline newStart);
+  close(cmd);
+  print $1,newStart,phaseStart;
+}
+'  ${scaFileSummarized} > ${OUT}
+#
+$ECHO "DEBUG (${scriptName}): END"
+#
+$ECHO "DEBUG (${scriptName}): START cat ${OUT}"
+$CAT ${OUT}
+$ECHO "DEBUG (${scriptName}): END cat ${OUT}"
+#
+$ECHO "DEBUG (${scriptName}): START filling cylceArrayBis timeArray"
+#
+declare -a cycleArrayBis
+declare -a timeArray
+$ECHO "DEBUG (${scriptName}): IFS value between<>: <$IFS>"
+IFS=$DefaultIFS
+#select the selected cycles
+idtofind="0"
+while read v1 v2 v3
+do
+ if [ ${idtofind} -lt ${#cycleArray[*]} ]; then
+    if [ ${v1} = ${cycleArray[${idtofind}]} ]; then
+	cycleArrayBis[${#cycleArrayBis[*]}]=$v1
+	timeArray[${#timeArray[*]}]=$v2
+	idtofind=`expr ${idtofind} + 1`
+    fi
+ fi
+done < ${OUT}
+$ECHO "DEBUG (${scriptName}): number of elements ${#cycleArrayBis[*]} ${#timeArray[*]}"
+$ECHO "DEBUG (${scriptName}): END"
+
+
+#########################
+# specmfib exe
+#########################
+prg="/afs/in2p3.fr/throng/baoradio/Library/Progs/TAcq/Objs/specmfib"
+if [ ! -e $prg ]; then
+    $ECHO "FATAL (${scriptName}): $prg not found"
+    touch ${tmppublicpath}/proc_script.finished
+    exit 1
+fi
+
+
+dirFiberfile=${iojobpath}/${localpath}
+
+curDir=`pwd`
+#
+$ECHO "DEBUG (${scriptName}): in principle $dirFiberfile == $curDir "
+#
+#force single channel
+forceSingle="-singlechan"
+#nwinmean donne le nombre de fenetre tspwin utilise pour faire calculer la moyenne des medianes de chaque fenetre
+#This is the nimber of paquets of the time window which change according to processing type
+nwinmean="1"
+#debug
+prtlevel="1"
+
+
+#Loop on cycles to write batch script and send to batch queue
+##
+$ECHO "DEBUG (${scriptName}): START loop on cycles ${firstCycleId} to ${lastp1CycleId}"
+
+for ((i=${firstCycleId};i<${lastp1CycleId};i++)); do
+#
+$ECHO "DEBUG (${scriptName}): process cycle $i"
+#time window selection
+    tmproc="-tmproc ${timeArray[${i}]},${timeDuration}"
+#cycle <name><number>
+    cycle="${cyclebasename}${cycleArray[${i}]}"
+#signal files selection
+    infiles="${filesArray[${i}]}"
+#
+#Prepare BQS parameters: MEMORY, CPU...#
+    infilesArray=( `$ECHO $infiles | $AWK 'BEGIN {FS=","} {for(i=1;i<NF;i++)print $i;}'` )
+    IFS='
+    '
+    infilesArray=( $( printf "%s\n" "${infilesArray[@]}" | awk 'x[$0]++ == 0' ) )
+    IFS=$DefaultIFS
+    firstFile=${infilesArray[0]}
+    lastFile=${infilesArray[1]}
+#compute Scratch Size MB (400MB per signal files), CPU Time
+    nFiles=`expr ${lastFile} - ${firstFile} + 1`
+# Fiber 1 & Fiber 2 => double the number of files
+    nFiles=`expr ${nFiles} \* 2`
+#add number of spectra generated (cf. (Nbrepaquets_per_files * nFiles /tspwin) * 100KB
+    nSpectra=`expr ${nFiles} \* 25600 /  ${tspwin}`
+#Guess formula to reserved the amount of space needed in scratch
+    scratchSize=`expr ${nFiles} \* 500 + ${nSpectra} \* 200 / 1000 + 5`
+# 25sec/signal_file * empirical_conversion_factor (=50)
+    cpu=`expr ${nFiles} \* 25 \* 50`
+#Virtual Memory (MB)
+    virtualmem=1000
+#Job and batch names
+    jobBatchName="${sourceRadio}-${dateDAQ}-${typeofproc}-cycle${cycleArray[${i}]}"
+    jobScriptName="${jobBatchName}.sh"
+    jobLogName="${jobBatchName}.log"
+#
+$ECHO "DEBUG (${scriptName}): start writing ${jobScriptName}"
+#
+#
+#write the script to be send to Batch
+#
+    $CAT > ${jobScriptName} <<EOF
+#!/bin/sh
+#    jobBatchName=$jobBatchName
+#    jobScriptName=$jobScriptName
+#    jobLogName=$jobLogName
+    scriptName=$jobScriptName
+
+    . /afs/in2p3.fr/home/throng/baoradio/setup.sh
+    . /usr/local/shared/bin/irods_env.sh -noverbose
+
+    iojobpath=\${TMPBATCH}
+    cd \${iojobpath}
+    localpath=${sourceRadio}/${dateDAQ}${srclower}
+    $MKDIR -p ./\${localpath}
+    cd ./\${localpath}
+
+    curDir=\`pwd\`
+#get gain file if necessary
+    if [ ${typeofproc} != "GAIN" ]; then
+	OUT2=./igetStatus.$$
+	iget /baoradio/data/AmasNancay/${localpath}/gain_${dateDAQ}_${srclower}.fits >\${OUT2} 2>&1
+	igetStatus=\`$GREP "^ERROR" \${OUT2}\`
+	if [ "<\${igetStatus}>" != "<>" ]; then
+	    $ECHO "FATAL (\${scriptName}): error while iget:"
+	    $ECHO \$igetStatus
+	    touch ${tmppublicpath}/${jobBatchName}.finished
+	    exit 1
+	fi
+        $RM \${OUT2}
+    fi    
+#get signals files
+    OUT3=./getsignals.$$
+    $RM -f ${tmppublicpath}/getsignalfiles.finished
+    ${scriptpath}/getsignalfiles.sh ${sourceRadio} ${dateDAQ} ${firstFile}  ${lastFile} > \${OUT3} 2>&1
+    while [ ! -f "${tmppublicpath}/getsignalfiles.finished" ]; do
+	$ECHO "INFO (\${scriptName}): waiting for ${tmppublicpath}/getsignalfiles.finished"
+	date +%T
+	sleep 30
+    done
+    $RM ${tmppublicpath}/getsignalfiles.finished
+
+    getSignals=\`$GREP "^FATAL" \${OUT3}\`
+    if [ "<\$getSignals>" != "<>" ]; then
+	$ECHO "FATAL (\${scriptName}): error while get signals"
+	$ECHO \$getSignals
+	touch ${tmppublicpath}/${jobBatchName}.finished
+	exit 1
+    fi
+    $RM \${OUT3}
+#location of the signal file
+    fibfile="\${curDir} 1,2"
+#
+    outDir=\${curDir}/${dirName}/${cycle}
+    $MKDIR -p \${outDir}
+#Note: here act=gain means only that specmfib will use median-like algorithms (not only for gains...)
+    $prg -act gain $forceSingle $freqfilter  -prt $prtlevel -out \${outDir} -nmean $nwinmean $ingain $tmproc -tspwin ${tspwin},0,0 -in $infiles \${fibfile}
+
+    $ECHO    \${outDir}
+    $LS -lrt \${outDir}/*
+
+#save results to Irods (use option -f ONLY to force override)
+    iput -v -K -r \${curDir}/${dirName} /baoradio/data/AmasNancay/\${localpath}
+#
+#clean the signal files which can at the end of the day fill completly the scratch directory of the batch work/AmasNancayer
+    $RM -f \${iojobpath}/\${localpath}/Fiber1/signal*.fits
+    $RM -f \${iojobpath}/\${localpath}/Fiber2/signal*.fits
+    touch ${tmppublicpath}/${jobBatchName}.finished
+    exit 0
+EOF
+#
+#The EOF tag MUST BEGIN the line, this is the end of the script
+#
+    $ECHO "DEBUG (${scriptName}); START listing of script to be send to batch queue"
+    $CAT -v ${jobScriptName}
+    $ECHO "DEBUG (${scriptName}): END listing"
+#
+#send to BATCH
+#
+    $ECHO "DEBUG (${scriptName}): START send to batch listing"
+#
+    $RM -f ${tmppublicpath}/${jobBatchName}.finished
+
+    qsub -l u_sps_baoradio,u_irods,T=${cpu},M=${virtualmem}MB,scratch=${scratchSize}MB,platform=LINUX -eo -o ${tmppublicpath}/${jobLogName} -N ${jobBatchName}  ${jobScriptName}
+
+#    while [ ! -f "${tmppublicpath}/${jobBatchName}.finished" ]; do
+#	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/${jobBatchName}.finished"
+#	date +%T
+#	sleep 600
+#    done
+#    $RM ${tmppublicpath}/${jobBatchName}.finished
+    $ECHO "DEBUG (${scriptName}): end send to batch listing"
+#end loop on cycles
+done
+#
+$ECHO "DEBUG (${scriptName}): END loop on cycles"
+#
+touch ${tmppublicpath}/proc_script.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/sca.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/sca.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/sca.sh	(revision 538)
@@ -0,0 +1,177 @@
+#!/bin/sh
+#
+#Script sh d'extraction de fichier SCA des sequances Diodes a Bruit (DAB) 
+#et Data des cycles ON/OFF
+#Bash script that extract from SCA files the DAB/DATA phases in ON/OFF cycles
+#It is supposed that the sequence is: DAB1 DATA1 DAB2 DATA2
+#and that the DAB1 and DAB2 have equal number of subsequences
+# Auteur: JE Campagne (LAL Orsay)
+# Creation: 11 Avril 2011
+# Revision: 12 Avril 2011
+#  o 12/04/11: mv date format to %Y-%m-%d %T for further treatment by 'date' 
+#              system function 
+
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc 
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/sca.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+if [ ! $# = 1 ]
+    then
+    $ECHO "FATAL  (${scriptName}): usage: ./sca.sh <file>"
+    touch ${tmppublicpath}/sca.finished
+    exit 1
+fi
+
+
+fullfile=$1
+if [ ! -e $fullfile ] 
+    then
+    $ECHO "FATAL (${scriptName}): $fullfile not found: FATAL"
+    touch ${tmppublicpath}/sca.finished
+    exit 1
+fi
+
+#The output file would be in "fullprocfile"
+filename=$(basename $fullfile)
+#extension=${filename##*.}
+filename=${filename%.*}
+extension=".sum"
+fullprocfile=${filename}${extension}
+#create empty output file
+$RM -f $fullprocfile
+$TOUCH $fullprocfile
+
+
+#extract the PERI lines valid for DAB and DATA
+OUT1=./tmp1.$$
+$AWK '
+$1=="PERI" {
+   print "CYCLE",$6,$2,$3,$7,$8
+  }
+' $fullfile > $OUT1
+
+
+#Sequence of the cycle numbers (perform a unique filtering)
+tableau=( `cat $OUT1 | $AWK '{print $2}'` )
+IFS='
+'
+tableau=( $( printf "%s\n" "${tableau[@]}" | awk 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+#scan each cycle
+OUT2=./tmp2.$$
+for i in ${tableau[@]}
+do
+#extract the DAB calibration phases. Hypothesis: 2 identical sequences 
+  CALIB=./tmp_cycle_${i}_calib.$$
+  $AWK -v cycle=$i '$2==cycle && $5=="-1" {print $0} ' $OUT1 > $CALIB
+  nlines=`$WC $CALIB | $AWK '{print $1}'`
+  start1=1
+  let "end1=nlines/2"
+  let "start2=end1+1"
+  end2=$nlines
+
+#date Start/End of DAB1 
+  $SED -e "${start1}!d" $CALIB | $AWK -v cycle=$i 'BEGIN {ORS=","} {
+    split($4,b,".");   
+    print cycle "," $3,b[1]}' >> ${OUT2}
+  $SED -e "${end1}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
+    split($4,b,".");   
+    print $3,b[1]}' >> ${OUT2}
+
+
+#date Start/End of DATA1: use date system function to add the duration to
+#compute the End,cf. End = Start + duration 
+  $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="1" ) {
+    split($3,a,"/"); 
+    split($4,b,".");   
+    dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
+    duree=$6 " sec";
+    cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
+   (cmd | getline dateEnd);
+    close(cmd);  
+    ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
+    ORS=","; print dateEnd;
+  } ' $OUT1 >> ${OUT2}
+ 
+#same as DAB1 for DAB2
+  $SED -e "${start2}!d" $CALIB | $AWK 'BEGIN {ORS=","} {
+    split($4,b,".");   
+    print $3,b[1]}' >> ${OUT2}
+  $SED -e "${end2}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
+     split($4,b,".");   
+     print $3,b[1]}' >> ${OUT2}
+#same as DATA1 for DATA2
+  $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="2" ) {
+    split($3,a,"/"); 
+    split($4,b,".")   
+    dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
+    duree=$6 " sec";
+    cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
+   (cmd | getline dateEnd);
+    close(cmd);  
+    ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
+    ORS="\n"; print  dateEnd;
+  } ' $OUT1 >> ${OUT2}
+#clean
+  $RM $CALIB
+done
+
+#transform all the date/time into %Y-%m-%d %T
+$AWK -v datesys=$DATE ' 
+BEGIN { FS=","; OFS="," }
+{
+  for (i=2;i<=NF;i++) {
+    split($i,a,"/");
+    newdate=a[2]"/"a[1]"/"a[3];
+    cmd=sprintf("%s --date \"%s\" \"+%%Y-%%m-%%d %%T\" ",datesys,newdate);
+    (cmd | getline newdate);
+    close(cmd);
+    $i=newdate;
+  } 
+  print $0;
+}' ${OUT2} > ${fullprocfile}
+
+
+#clean
+$RM $OUT1
+$RM $OUT2
+
+touch ${tmppublicpath}/sca.finished
+exit 0
+
+
+
Index: /BAORadio/AmasNancay/v2/v1/scaIsOk.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/scaIsOk.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/scaIsOk.sh	(revision 538)
@@ -0,0 +1,87 @@
+#!/bin/sh
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+MKDIR=/bin/mkdir
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#which source to analyse
+sourceRadio=$1
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath=${TMPPUBLICPATH}
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/scaIsOk.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+OUT1=${publicpath}/baodaqstatus-2011-09-11.txt
+
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/scaIsOk.finished
+    exit 1
+fi
+
+if [ ! "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "You have selected sourceRadio = $sourceRadio"
+    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+else
+    tableau=( `$CAT ${OUT1} | $GREP -i "baoradio" | $AWK '( NF==4 ) { print $2 }'`  )
+fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+for i in ${tableau[@]}
+do
+#scrutinize irods directory
+#Nb: we do not care here if ILS returns non unique instance of a file
+  scafile=`ils -l $i 2>&1 | $GREP -i "sca[0-9][0-9]*\.[0-9][0-9]*" | $AWK '{print $NF}'`
+  if [ "<${scafile}>" = "<>" ]; then
+      $ECHO "INFO (${scriptName}): Cannot download a SCA file for `basename $i`"
+  fi
+done
Index: /BAORadio/AmasNancay/v2/v1/scaextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/scaextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/scaextractor.sh	(revision 538)
@@ -0,0 +1,145 @@
+#!/bin/sh -xvf
+#download SCA file, proceed to introspection, and upload results
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+MKDIR=/bin/mkdir
+RM=/bin/rm
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+#set Irods environment    
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#which source to analyse
+sourceRadio=$1
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/scaextractor.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+localpath="${sourceRadio}"
+$MKDIR -p ./${localpath}
+cd ./${localpath}
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+#get the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
+#    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+#    $RM -f ${tmppublicpath}/statusdaq.finished
+#    ${scriptpath}/statusdaq.sh > ${OUT1}
+#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+#	$ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/statusdaq.finished"
+#	date +%T
+#	sleep 30 
+#    done
+#    $RM ${tmppublicpath}/statusdaq.finished
+    $ECHO "FATAL (${scriptName}): ${OUT1} is missing or not readable..."
+    touch ${tmppublicpath}/scaextractor.finished
+    exit 1
+fi
+
+if [ ! "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "You have selected sourceRadio = $sourceRadio"
+    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
+else
+    tableau=( `$CAT ${OUT1} | $AWK'( NF==4 ) { print $2 }'`  )
+fi
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+for i in ${tableau[@]}
+do
+OUT2=./baodir.$$
+#scrutinize irods directory
+#Nb: we do not care here if ILS returns non unique instance of a file
+  ils -l $i 2>&1 | $GREP -i "sca" > ${OUT2}
+  $CAT ${OUT2}
+#test if the SCA file has been already processed
+  scatrans=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $NF}'`
+#test if the file is empty due to previous processing dysfunction
+  emptyfile=`$GREP -i "sca.*\.sum\.trans" ${OUT2} | $AWK '{print $4}'`
+#  echo "scrutinize directory <$i>: [$nblines] [$scatrans]" 
+  if [ "<${scatrans}>" = "<>" -o "<${emptyfile}>" = "<0>" ]; then
+      $ECHO "$i may be non prepared for analysis..."
+      scafile=`$GREP -i "sca[0-9][0-9]*\.[0-9][0-9]*" ${OUT2} | $AWK '{print $NF}'`
+      if [ "<${scafile}>" = "<>" ]; then
+	  $ECHO "WARNING (${scriptName}): Cannot download a SCA file for $i"
+      else
+#get sca file
+        iget -r -f -v -K ${i}/${scafile} ${iojobpath}
+#extract summary (sum extension)
+	$RM -f ${tmppublicpath}/sca.finished
+	${scriptpath}/sca.sh ${scafile}
+	while [ ! -e "${tmppublicpath}/sca.finished" ]; do
+	    $ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/sca.finished"
+	    date +%T
+	    sleep 30
+	done
+	$ECHO "INFO (${scriptName}): sca.sh has finished"
+	$RM ${tmppublicpath}/sca.finished
+#TODO see if there is errors...
+        scaFileName=$(basename $scafile)
+	scaFileName=${scaFileName%.*}
+#transform the date (sum.trans extension)
+	$RM -f ${tmppublicpath}/transdate.finished
+        ${scriptpath}/transdate.sh ${scaFileName}.sum
+	while [ ! -e "${tmppublicpath}/transdate.finished" ]; do
+	    $ECHO "INFO (${scriptName}); waiting for ${tmppublicpath}/transdate.finished"
+	    date +%T
+	    sleep 30
+	done
+	$ECHO "INFO (${scriptName}): transdate.sh  has finished"
+	$RM ${tmppublicpath}/transdate.finished
+#TODO see if there is errors...	
+#put back into Irod
+#	$ECHO "INFO (${scriptName}): where am i: `pwd`"
+#	$LS -lrt
+#	$ECHO "INFO (${scriptName}): see up directory"
+#	$LS -lrt ..
+	iput -f -v -K ${iojobpath}/${scaFileName}.sum $i
+	iput -f -v -K ${iojobpath}/${scaFileName}.sum.trans $i
+      fi
+  fi
+$RM ${OUT2}
+done
+
+
+touch ${tmppublicpath}/scaextractor.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/select.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/select.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/select.sh	(revision 538)
@@ -0,0 +1,227 @@
+#!/bin/sh
+
+#hypothesis: ON = data1 phase, OFF data2 phase
+
+#Basic tuning of system fuction used.
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+FIND=/usr/bin/find
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+MKDIR=/bin/mkdir
+LS=/bin/ls
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+if [ ! $# = 3 ]; then
+  $ECHO "usage: ${scriptName} <scaXYZ.sum> <signalXYZDirectory> <type of phase:ON|OFF|CALIBON|CALIBOFF>"
+  touch ${tmppublicpath}/select.finished
+  exit 0
+fi
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+
+OUT1=./selectFits.$$
+
+#inputs x-chexk sca file
+inScaFile=$1
+if [ ! -e $inScaFile ]; then
+  $ECHO "FATAL (${scriptName}): $inScaFile not found"
+  touch ${tmppublicpath}/select.finished
+  exit 1
+fi
+
+#Where to look at
+inFiberDir=$2
+
+inTypeOfPhase=$3
+case "$inTypeOfPhase" in
+    ON) 
+    $ECHO "INFO (${scriptName}): filter signal files for ON phases";;
+    OFF) 
+    $ECHO "INFO (${scriptName}): filter signal files for OFF phases";;
+    CALIBON) 
+    $ECHO "INFO (${scriptName}): filter signal files for CALIBRATION ON phases";;
+    CALIBOFF) 
+    $ECHO "INFO (${scriptName}): filter signal files for CALIBRATION OFF phases";;
+    GAIN) 
+    $ECHO "INFO (${scriptName}): filter signal files for GAIN = CALIB OFF phases";;
+    *) 
+    $ECHO "FATAL (${scriptName}): use ON|OFF|CALIBON|CALIBOFF phase tags."
+    touch ${tmppublicpath}/select.finished
+    exit 1
+    ;;
+esac
+
+
+#################
+#fill the arrays association header file with start/end date&time 
+# arrays
+declare -a sigStart
+declare -a sigEnd
+declare -a sigFile
+
+for fullfile in $( $FIND $inFiberDir -name "header*" | $XARGS -i basename {} | $SORT -k1.7n)
+  do
+  filename=$(basename $fullfile)
+  extension=${filename##*.}
+  filename=${filename%.*}
+  ffn=${inFiberDir}/${filename}.${extension}
+#  echo "process $ffn"
+  while read -n80 L
+    do
+    [ "${L::3}" = "END" ] && break
+    echo $L
+  done < $ffn > $OUT1
+  
+  strStart=`$GREP TMSTART $OUT1 | $AWK '{split($0,a,"/"); split(a[1],b,"="); print b[2];}' | $SED 's/^[ \t]*//;s/[ \t]*$//'  | $SED "s/^\([\"']\)\(.*\)\1\$/\2/g" | $AWK '{split($0,a,"T"); print a[1]" "a[2];}' `
+  
+  strEnd=`$GREP TMEND $OUT1 | $AWK '{split($0,a,"/"); split(a[1],b,"="); print b[2];}' |  $SED 's/^[ \t]*//;s/[ \t]*$//'  | $SED "s/^\([\"']\)\(.*\)\1\$/\2/g" | $AWK '{split($0,a,"T"); print a[1]" "a[2];}' `
+  
+  if [[ -n "$strStart"  &&  -n "$strEnd" ]]; then
+      timeStart=`${DATE} +%s -d "${strStart}"`
+      timeEnd=`${DATE} +%s -d "${strEnd}"`
+#      echo "$fullfile start $strStart   <$timeStart>, end  $strEnd <$timeEnd>"
+      sigStart[${#sigStart[*]}]=$timeStart
+      sigEnd[${#sigEnd[*]}]=$timeEnd
+      sigFile[${#sigFile[*]}]=$filename
+  else
+      $ECHO "WARNING (${scriptName}): $ffn PATHOLOGIQUE"
+  fi
+done
+
+############
+#scan all the cycles 
+#  calib1start=$2; calib1end=$3; 
+#  data1start=$4; data1end=$5;
+#  calib2start=$6; calib2end=$7; 
+#  data2start=$8; data2end=$9;
+#to pass to awk: use a "," as field separator
+awk_sigStart=$( printf "%s," "${sigStart[@]}" )
+awk_sigEnd=$( printf "%s," "${sigEnd[@]}" )
+awk_sigFile=$( printf "%s," "${sigFile[@]}" )
+
+#attention a la derniere "," pour le calcul de nfile
+OUT1=./tmpselect1.$$
+#selection d'une seul cycle pour debug 
+$AWK -v datesys=$DATE -v type=$inTypeOfPhase -v str_sigStart="$awk_sigStart"  -v str_sigEnd="$awk_sigEnd"  -v str_sigFile="$awk_sigFile" '
+BEGIN { 
+  FS=",";
+  split(str_sigStart,a_sigStart,",");
+  split(str_sigEnd,a_sigEnd,",");
+  nfile=split(str_sigFile,a_sigFile,",");
+  delete a_sigFile[nfile];
+  delete a_sigStart[nfile];
+  delete a_sigEnd[nfile];
+  nfile--;  
+
+}
+
+ {
+  cycle=$1; 
+
+  if ( type == "ON" ) {
+    phaseStart=$4; phaseEnd=$5;
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+    # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=4;
+    deltaEndSafety=-4;
+  }
+  else if ( type == "OFF" ) {
+    phaseStart=$8; phaseEnd=$9;
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+    # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=4;
+    deltaEndSafety=-4;
+  } 
+  else if ( type == "CALIBON" ) {
+    phaseStart=$2; phaseEnd=$3;
+    print "Phase: ",type," cycle: ",cycle,": <",phaseStart,">:<",phaseEnd,">";
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+#    print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=-6;
+    deltaEndSafety=6;
+  } 
+  else if ( (type == "CALIBOFF") || (type == "GAIN") ) {
+    phaseStart=$6; phaseEnd=$7;
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
+    (cmd | getline phstart);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
+    (cmd | getline phend);
+    close(cmd);
+    # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
+    deltaStartSafety=-6;
+    deltaEndSafety=6;
+  } 
+  else { 
+    print "FATAL: ",type," NOT YET SUPPORTED"; exit 1;
+  }
+
+  for (i=1; i<=nfile; i++) {
+    diffstart = a_sigStart[i] - phstart;
+    diffend   = a_sigEnd[i] - phend;
+#     print "file ",i," start ",a_sigStart[i],": diff <" diffstart ">:<" diffend ">";
+    if ( diffstart >= deltaStartSafety && diffend <= deltaEndSafety )
+       print "cycle ",cycle," file ",i-1," accepted ",a_sigFile[i];
+  }  
+     
+
+}' $inScaFile > ${OUT1}
+
+OUT2=./tmpselect2.$$
+$AWK '
+BEGIN { max=0; min=999999; }; 
+
+($1=="cycle") { table[$2] = table[$2]","$4; if($2>max){max=$2}; if($2<min){min=$2} }; 
+
+END { for (j=min;j<=max;j++) {print j,table[j];} }
+' ${OUT1} > ${OUT2}
+
+$AWK '
+BEGIN { FS=","}
+{
+ print "#"$1,$2","$NF",1";
+}
+' ${OUT2}
+
+$RM $OUT1
+$RM $OUT2
+
+touch ${tmppublicpath}/select.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/statuscalib.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/statuscalib.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/statuscalib.sh	(revision 538)
@@ -0,0 +1,138 @@
+#!/bin/sh
+#Status de l'etat de la production des fichiers INPUT de calibrations
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+listeSources=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+listeSources=( $( $PRINTF "%s\n" "${listeSources[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+$RM $OUT2
+
+for i in ${listeSources[@]}
+do
+#nb: $i contains the full path
+  $ECHO "source ${i}"
+  sourceName=`basename ${i}`
+  listeDates=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listeDates=( $( $PRINTF "%s\n" "${listeDates[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  for j in ${listeDates[@]}
+  do
+#nb: $j contains the full path
+#  $ECHO "run ${j}"
+#
+#Off directory
+#
+    dateField=`basename ${j}`
+    sourceDate="${sourceName}-${dateField}"
+#look if final calibration files exist
+    calibOk=`ils ${j} | $GREP -i "calib_" | $WC -l`
+
+    offDirExist=`ils ${j} | $GREP -i "Off" | $WC -l`
+    if [ ${offDirExist} != 0 ]; then
+	listeCyclesOff=( `ils ${j}/Off | $GREP -i "calibcycle" | $AWK '($1=="C-"){print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+	IFS='
+  '
+	listeCyclesOff=( $( $PRINTF "%s\n" "${listeCyclesOff[@]}" | $AWK 'x[$0]++ == 0' ) )
+	IFS=$DefaultIFS
+	
+	if [ ${#listeCyclesOff[*]} != 0 ]; then    
+           for c in ${listeCyclesOff[@]}
+	   do
+#nb: $c has the format of calibcycle<number>
+	     nFiles=`ils ${j}/Off/${c} | $GREP -i "medfiltmtx" | $WC -l`
+	     if [ ${nFiles} != 0 ]; then
+		 $ECHO "${sourceDate}/Off/${c}: ${nFiles} spectra (${calibOk} calib)"
+	     else
+		 $ECHO "${sourceDate}/Off/${c}: Calibration failed"
+	     fi
+#end of loop on cycles 
+	   done
+	else
+	    $ECHO "${sourceDate} : No Calibration done for Off Diectory"
+	fi
+#end of Off exist
+    else
+	$ECHO "${sourceDate} : No Off Diectory"
+    fi
+#
+#On directory
+#
+    dateField=`basename ${j}`
+    sourceDate="${sourceName}-${dateField}"
+#look if final calibration files exist
+    calibOk=`ils ${j} | $GREP -i "calib_" | $WC -l`
+    onDirExist=`ils ${j} | $GREP -i "On" | $WC -l`
+    if [ ${onDirExist} != 0 ]; then
+	listeCyclesOn=( `ils ${j}/On | $GREP -i "calibcycle" | $AWK '($1=="C-"){print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
+	IFS='
+  '
+	listeCyclesOn=( $( $PRINTF "%s\n" "${listeCyclesOn[@]}" | $AWK 'x[$0]++ == 0' ) )
+	IFS=$DefaultIFS
+	
+	if [ ${#listeCyclesOn[*]} != 0 ]; then    
+           for c in ${listeCyclesOn[@]}
+	   do
+#nb: $c has the format of calibcycle<number>
+	     nFiles=`ils ${j}/On/${c} | $GREP -i "medfiltmtx" | $WC -l`
+	     if [ ${nFiles} != 0 ]; then
+		 $ECHO "${sourceDate}/On/${c}: ${nFiles} spectra (${calibOk} calib)"
+	     else
+		 $ECHO "${sourceDate}/On/${c}: Calibration failed"
+	     fi
+#end of loop on cycles 
+	   done
+	else
+	    $ECHO "${sourceDate} : No Calibration done for On Diectory"
+	fi
+#end of On exist
+    else
+	$ECHO "${sourceDate} : No On Diectory"
+    fi
+#end of loop on dates
+  done 
+#end of loop on sources
+done
Index: /BAORadio/AmasNancay/v2/v1/statusdaq.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/statusdaq.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/statusdaq.sh	(revision 538)
@@ -0,0 +1,88 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+LS=/bin/ls
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/statusdaq.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  ntotfiles=0
+  for j in ${listedate[@]}
+  do
+#Nb: care should be taken to the possible instances of a file returned by ILS
+    listOfsig=( `ils ${j}/Fiber1 | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+  '
+    listOfsig=( $( $PRINTF "%s\n" "${listOfsig[@]}" | $AWK 'x[$0]++ == 0' ) )
+    IFS=$DefaultIFS
+    nfiles=${#listOfsig[@]}
+    $ECHO ">>> $j ---> $nfiles"
+    let "ntotfiles=ntotfiles+nfiles"
+  done 
+  $ECHO "total files = $ntotfiles"
+
+done
+
+touch ${tmppublicpath}/statusdaq.finished
+exit 0
Index: /BAORadio/AmasNancay/v2/v1/statusgain.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/statusgain.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/statusgain.sh	(revision 538)
@@ -0,0 +1,70 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+  IFS=$DefaultIFS
+
+  for j in ${listedate[@]}
+  do
+    gainOk=`ils ${j} | $GREP -i "gain_" | $WC -l`
+    if [ ${gainOk} -gt 0 ]; then
+	$ECHO ">>> $j ---> GAIN Done (${gainOk})"
+    else
+	$ECHO ">>> $j ---> GAIN To be computed"
+    fi
+  done 
+done
Index: /BAORadio/AmasNancay/v2/v1/statusheader.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/statusheader.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/statusheader.sh	(revision 538)
@@ -0,0 +1,94 @@
+#!/bin/sh
+#liste des "sources" enregistrees sous Amas
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+MKDIR=/bin/mkdir
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+RM=/bin/rm
+SORT=/bin/sort
+XARGS=/usr/bin/xargs
+WC=/usr/bin/wc
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+# Irods environment settings
+. /usr/local/shared/bin/irods_env.sh -noverbose
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} == "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} == "BATCH" ] ; then
+    iojobpath=${TMPBATCH}
+else
+    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    exit 1
+fi
+cd ${iojobpath}
+
+
+OUT1=./baoils.$$
+ils /baoradio/data/AmasNancay/ > ${OUT1}
+OUT2=./baoawk1.$$
+$AWK '($1=="C-") {print $2}' ${OUT1} > ${OUT2}
+$RM $OUT1
+#Sequence 
+tableau=( `$CAT $OUT2 | $AWK '{print $1}'` )
+IFS='
+'
+tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
+IFS=$DefaultIFS
+
+$RM $OUT2
+
+for i in ${tableau[@]}
+do
+  $ECHO "source $i"
+  listedate=( `ils $i | $AWK '($1=="C-"){print $2}'`)
+  IFS='
+'
+  listedate=( $( $PRINTF "%s\n" "${listedate[@]}" | $AWK 'x[$0]++ == 0' ) )
+
+  ntotfiles=0
+  for j in ${listedate[@]}
+  do
+#Fiber 1
+    listOfsig1=( `ils ${j}/Fiber1 | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+  '
+    listOfsig1=( $( $PRINTF "%s\n" "${listOfsig1[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesSIG1=${#listOfsig1[@]}
+    listOfhdr1=( `ils ${j}/Fiber1 | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+    '
+    listOfhdr1=( $( $PRINTF "%s\n" "${listOfhdr1[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesHDR1=${#listOfhdr1[@]}
+
+#Fiber 2
+    listOfsig2=( `ils ${j}/Fiber2 | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+  '
+    listOfsig2=( $( $PRINTF "%s\n" "${listOfsig2[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesSIG2=${#listOfsig2[@]}
+    listOfhdr2=( `ils ${j}/Fiber2 | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
+    IFS='
+    '
+    listOfhdr2=( $( $PRINTF "%s\n" "${listOfhdr2[@]}" | $AWK 'x[$0]++ == 0' ) )
+    nblinesHDR2=${#listOfhdr2[@]}
+
+
+
+    $ECHO ">>> $j --->  Fib1: $nblinesHDR1 / $nblinesSIG1 Fib2: $nblinesHDR2 / $nblinesSIG2"
+    let "ntotfiles=ntotfiles+$nblinesSIG1"
+  done 
+  $ECHO "total signal Fiber1 files = $ntotfiles"
+
+done
Index: /BAORadio/AmasNancay/v2/v1/submit2bqs-calibmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/submit2bqs-calibmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/submit2bqs-calibmaker.sh	(revision 538)
@@ -0,0 +1,44 @@
+#!/bin/sh -xv
+
+scriptName="`basename $0`"
+echo "Processing script ${scriptName} at `date`"
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio
+freqBAOCalib=
+bwBAOCalib=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio="$2"; shift;;
+      -freq) freqBAOCalib="-freq $2"; shift;;
+      -bwd)  bwBAOCalib="-bwd $2"; shift;;
+      -sim) 
+      simulationMode="-sim On"
+      $ECHO "INFO ${scriptName} running in SIMUL mode"
+      shift;;
+	-h)
+	echo >&2 \
+	    "usage: $0 -src souce [-freq freq in MHz (default ${freqBAOCalib})] [-bwd bandwidth in MHz (default ${bwBAOCalib})]"
+	exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+
+if [ "<${sourceRadio}>" = "<>" ]; then
+    $ECHO "FATAL: You have forgotten to select the source option (-src)"
+    exit 1
+fi
+
+
+jobBatchName="calibs-${sourceRadio}"
+
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=120000,M=1000MB,scratch=1000MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/calibmaker.sh  -src ${sourceRadio} ${freqBAOCalib} ${bwBAOCalib} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/v1/submit2bqs-gainmaker.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/submit2bqs-gainmaker.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/submit2bqs-gainmaker.sh	(revision 538)
@@ -0,0 +1,26 @@
+#!/bin/sh -xv
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+#use -sim option to simulate processing (debug the script as if...)
+simulationMode=""
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+      -sim) simulationMode="-sim"; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce [-sim]"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+jobBatchName="gains-$sourceRadio"
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=120000,M=1000MB,scratch=500MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/gainmaker.sh  -src ${sourceRadio} ${simulationMode}
+EOF
Index: /BAORadio/AmasNancay/v2/v1/submit2bqs-hdrfitsextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/submit2bqs-hdrfitsextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/submit2bqs-hdrfitsextractor.sh	(revision 538)
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+jobBatchName="headerMaker-$sourceRadio"
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=100000,M=256MB,scratch=700MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/hdrfitsextractor.sh  $sourceRadio
+EOF
Index: /BAORadio/AmasNancay/v2/v1/submit2bqs-scaextractor.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/submit2bqs-scaextractor.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/submit2bqs-scaextractor.sh	(revision 538)
@@ -0,0 +1,66 @@
+#!/bin/sh -xvf
+DATE=/bin/date
+GREP=/bin/grep
+AWK=/bin/awk
+ECHO=/bin/echo
+WC=/usr/bin/wc
+CAT=/bin/cat
+PRINTF=/usr/bin/printf
+FIND=/usr/bin/find
+MKDIR=/bin/mkdir
+XARGS=/usr/bin/xargs
+SORT=/bin/sort
+RM=/bin/rm
+TR=/usr/bin/tr
+CP=/bin/cp
+LS=/bin/ls
+CHMOD=/bin/chmod
+
+#Process cmd line args: the -src option is mandatory (source name as Abell85)
+sourceRadio=
+while [ $# -gt 0 ]
+do
+  case "$1" in
+      -src)  sourceRadio=$2; shift;;
+	-h)
+	    echo >&2 \
+	    "usage: $0 -src souce"
+	    exit 1;;
+	*)  break;;	# terminate while loop
+    esac
+    shift
+done
+
+#Path to public backupable path
+publicpath="/afs/in2p3.fr/home/c/campagne/public"
+
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+#Path of the utility scripts
+scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
+
+#the daq current irod status
+tag=`${DATE} +%F`
+OUT1=${publicpath}/baodaqstatus-${tag}.txt
+
+if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
+    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
+    $RM -f ${tmppublicpath}/statusdaq.finished
+    ${scriptpath}/statusdaq.sh > ${OUT1}
+    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
+	sleep 30
+    done
+    $RM ${tmppublicpath}/statusdaq.finished
+#protect against remove/rewriting    
+    $CHMOD -v 444 ${OUT1} 
+fi
+
+jobBatchName="scaMaker-$sourceRadio"
+jobLogName="${jobBatchName}.log.$$"
+
+qsub -l u_sps_baoradio,u_irods,T=100000,M=256MB,scratch=700MB,platform=LINUX -eo -o $jobLogName -N $jobBatchName -mb -me -mu campagne@lal.in2p3.fr <<EOF
+/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay/scaextractor.sh  $sourceRadio
+EOF
Index: /BAORadio/AmasNancay/v2/v1/transdate.sh
===================================================================
--- /BAORadio/AmasNancay/v2/v1/transdate.sh	(revision 538)
+++ /BAORadio/AmasNancay/v2/v1/transdate.sh	(revision 538)
@@ -0,0 +1,79 @@
+#!/bin/sh
+# transforme les dates en nombre de seconde depuis minuit
+# pour reecrire les start/stop des differentes phases du fichier sca
+
+AWK=/bin/awk
+SED=/bin/sed
+GREP=/bin/grep
+WC=/usr/bin/wc
+RM=/bin/rm
+CAT=/bin/cat
+TOUCH=/bin/touch
+DATE=/bin/date
+ECHO=/bin/echo
+LS=/bin/ls
+MKDIR=/bin/mkdir
+DefaultIFS=$' \t\n'
+IFS=$DefaultIFS
+
+
+scriptName="`basename $0`"
+$ECHO "Processing script ${scriptName} at `date`"
+
+#temporary files to synchronize scripts
+tmppublicpath="/sps/baoradio/AmasNancay"
+$LS -l ${tmppublicpath} > /dev/null
+
+#Path where the job will do temporary IO
+if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
+    iojobpath="/sps/baoradio/AmasNancay/JEC"
+    $MKDIR -p $iojobpath
+elif [ ${ENVIRONMENT} = "BATCH" ] ; then
+     iojobpath=${TMPBATCH}
+else
+     $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
+    touch ${tmppublicpath}/transdate.finished
+    exit 1
+fi
+cd ${iojobpath}
+
+if [ ! $# = 1 ]; then
+  $ECHO "FATAL (${scriptName}) usage: ./transdate.sh <scaXYZ.sum>"
+  touch ${tmppublicpath}/transdate.finished
+  exit 0
+fi
+
+#inputs x-chexk sca file
+inScaFile=$1
+if [ ! -e $inScaFile ]; then
+  $ECHO "FATAL (${scriptName}): $inScaFile not found"
+  touch ${tmppublicpath}/transdate.finished
+  exit 1
+fi
+
+$AWK -v datesys=$DATE '
+BEGIN {
+  FS=",";
+}
+
+{
+  cycle=$1;
+  for (i=2; i<=NF; i++) {
+    cmd=sprintf("%s +%%F -d \"%s\" ",datesys,$i);
+    (cmd | getline day);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,day);
+    (cmd | getline s0);
+    close(cmd);
+    cmd=sprintf("%s +%%s -d \"%s\" ",datesys,$i);
+    (cmd | getline s1);
+    close(cmd);
+    $i = s1 - s0;    
+  }
+  print $0;
+}
+
+' $inScaFile >> $inScaFile.trans
+
+touch ${tmppublicpath}/transdate.finished
+exit 0
