Index: trunk/Poubelle/archTOI.old/archtoi.cc
===================================================================
--- trunk/Poubelle/archTOI.old/archtoi.cc	(revision 350)
+++ trunk/Poubelle/archTOI.old/archtoi.cc	(revision 352)
@@ -34,4 +34,6 @@
   undef  = "#";
   allBolos = false;
+  fptr = NULL;
+  ostr = NULL;
 }
 
@@ -54,5 +56,5 @@
   // find TOI kind, index and options
   TOIKind kind= (TOIKind)-1;
-  int index=0;
+  int index=-1;
   bool interp=false;
   bool repet =false;
@@ -108,5 +110,16 @@
   }
   headertoi.push_back(line);
+  string toiname = key.substr(1); 
+  if (index>=0) {
+    char idx[10]; 
+    sprintf(idx,"_%d",index);
+    toiname += idx;
+  }
+  if (flag) {
+    toinames.push_back("flg_"+toiname);
+  }
+  toinames.push_back(toiname);
   toiflags.push_back(flg((flag?hasflag:0)+((!repet&&!interp)?useNA:0)));
+  if (index<0) index=0;
   svr.AddInfo(kind, index, !notrig, interp);
 } 
@@ -158,44 +171,26 @@
 
 
-void ArchTOI::run(string const& filename)
-{
-  ofstream str(filename.c_str());
-  run(str);
-}
-
-void ArchTOI::run(ostream& str)
-{
-  // output header
-  for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++)
-    str << (*i) << '\n';
-  for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++)
-    str << (*i) << '\n';;
+void ArchTOI::run(string const& outfilename)
+{
+  if (format == ascii_fmt) {
+    openFile   = &openFile_A;
+    outHeader  = &outHeader_A;
+    outValue   = &outValue_A;
+    endLine    = &endLine_A;
+    closeFile  = &closeFile_A;
+  } else { // fits_fmt 
+    openFile   = &openFile_F;
+    outHeader  = &outHeader_F;
+    outValue   = &outValue_F;
+    endLine    = &endLine_F;
+    closeFile  = &closeFile_F;
+  }
   
   cout << "starting query" << endl;
   TOIIter iter = svr.DoQuery();
-  // Normalement, on a un bloc param...
-  block_type_param* blk = iter.lastParam();
-  if (blk) {
-    int nb = blk->param.nb_bolo;
-    for (int i=0; i<nb; i++) {
-#if version_num > 25
-      if (allBolos) {
-        str << "$BOLO " << i << " " 
-            << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << " " 
-            << blk->param.bolo[i].bolo_code_util << '\n';
-      } else if (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
-                 blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis) {
-        str << "$BOLO " << i << " " 
-            << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << "\n";
-      }
-#else
-      str << "$BOLO " << i << " " << 
-        blk->param.bolo[i].bolo_nom << '\n';
-#endif
-    }
-  }
-  str << "#END" << endl;
+  (this->*openFile)(outfilename);
+  (this->*outHeader)(iter);
+  
   cout << "processing" << endl;
-  string ofn;
   while (iter.Next()) {
     int nn = iter.getSampleIndex();
@@ -211,4 +206,5 @@
     }
     if (!hasValue) continue;
+    int icol=0;
     for (int i=0; i<toiflags.size(); i++) {
       double value = iter.getValue(i);
@@ -216,14 +212,167 @@
       bool   isnew = iter.newValue(i);
       flg    flag  = toiflags[i];
-      if (flag & hasflag) str << (ok && isnew ? 1 : 0) << " \t";
+      if (flag & hasflag) {
+        (this->*outValue)(icol,  (ok && isnew ? 1 : 0));
+        icol++;
+      }
       if (((flag & useNA)!=0 && !isnew) || !ok) 
-        str << undef << "\t";
+        (this->*outValue)(icol,  0, true);
       else
-        str << setprecision(11) << value << " \t";
-    }
-    str << '\n';
-  }
-  str.flush();
+        (this->*outValue)(icol, value);
+      icol++;
+    }
+    (this->*endLine)();
+  }
+  (this->*closeFile)();
   cout << "\nDone." << endl;
 }
 
+
+void ArchTOI::openFile_A(string const& filename) {
+  ostr = new ofstream(filename.c_str());
+}
+
+void ArchTOI::outHeader_A(TOIIter& iter) {
+  if (!ostr) return;
+  for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++)
+    *ostr << (*i) << '\n';
+  for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++)
+    *ostr << (*i) << '\n';;
+  block_type_param* blk = iter.lastParam();
+  if (blk) {
+    int nb = blk->param.nb_bolo;
+    for (int i=0; i<nb; i++) {
+#if version_num > 25
+      if (allBolos) {
+        *ostr << "$BOLO " << i << " " 
+            << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << " " 
+            << blk->param.bolo[i].bolo_code_util << '\n';
+      } else if (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
+                 blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis) {
+        *ostr << "$BOLO " << i << " " 
+            << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << "\n";
+      }
+#else
+      *ostr << "$BOLO " << i << " " << 
+        blk->param.bolo[i].bolo_nom << '\n';
+#endif
+    }
+  }
+  *ostr << "#END" << endl;
+}
+
+void ArchTOI::outValue_A(int icolumn, double value, bool notdef) {
+  if (!ostr) return;
+  if (icolumn > 0) *ostr << '\t';
+  if (notdef) {
+     *ostr << undef ;
+  } else {
+     *ostr << setprecision(11) << value ;
+  }
+}
+
+void ArchTOI::endLine_A() {
+  if (!ostr) return;
+  *ostr << '\n';
+}
+
+void ArchTOI::closeFile_A() {
+  delete ostr;
+  ostr = NULL;
+}
+
+
+void ArchTOI::openFile_F(string const& filename) {
+  fitsStatus=0;
+  remove(filename.c_str());
+  if (fits_create_file(&fptr, filename.c_str(), &fitsStatus)) {
+    fits_report_error(stderr, fitsStatus);
+    exit(-1);
+  }
+}
+
+void ArchTOI::outHeader_F(TOIIter& iter) {  
+  int ncols=toinames.size();
+  
+  char** colnames = new (char*[ncols]);
+  char** coltypes = new (char*[ncols]);
+  char** colunits = new (char*[ncols]);
+  int j=0;
+  for (list<string>::iterator i = toinames.begin(); i != toinames.end(); i++,j++) {
+    colnames[j] = const_cast<char*>((*i).c_str()); // should work for most STL implementations... Check...
+    coltypes[j] = "1D";
+    colunits[j] = " ";
+  }
+  
+  fits_create_tbl(fptr, BINARY_TBL, 0, ncols, colnames, coltypes, colunits, NULL, &fitsStatus);
+  fits_write_date(fptr, &fitsStatus);
+
+  delete[] colunits;
+  delete[] coltypes;
+  delete[] colnames;
+
+  j=1;
+  // Rappel dans le header des requetes...
+  int ntoireq = headertoi.size();
+  fits_write_key(fptr, TINT, "TOIREQ", &ntoireq, NULL, &fitsStatus);
+  for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++,j++) {
+    char line[80];
+    strcpy(line, (*i).c_str());
+    char* pline = line;
+    fits_write_keys_str(fptr, "TOIREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
+  }
+  j=1;
+  int noptreq = headeropt.size();
+  fits_write_key(fptr, TINT, "OPTREQ", &noptreq, NULL, &fitsStatus);
+  for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++,j++) {
+    char line[80];
+    strcpy(line, (*i).c_str());
+    char* pline = line;
+    fits_write_keys_str(fptr, "OPTREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
+  }
+
+  // Noms des bolos
+  block_type_param* blk = iter.lastParam();
+  if (blk) {
+    int nb = blk->param.nb_bolo;
+    j=0;
+    for (int i=0; i<nb; i++) {
+#if version_num > 25
+      if (allBolos || 
+           (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
+            blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis)) {
+        j++;
+        char line[80];
+        strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom);
+        char* pline = line;
+        fits_write_keys_str(fptr, "BOLO", j, 1, &pline, (char**) NULL, &fitsStatus);
+      }
+#else
+        j++;
+        char line[80];
+        strcpy(line, blk->param.bolo[i].bolo_nom);
+        char* pline = line;
+        fits_write_keys_str(fptr, "BOLO", j, 1, &pline, (char**) NULL, &fitsStatus);
+#endif
+    }
+    fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus);
+  }
+  fitsLine = 1;
+}
+
+void ArchTOI::outValue_F(int icolumn, double value, bool notdef) {
+  if (notdef) {
+    fits_write_col_null(fptr, icolumn+1, fitsLine, 1, 1, &fitsStatus); 
+  } else {
+    fits_write_col_dbl(fptr, icolumn+1, fitsLine, 1, 1, &value, &fitsStatus);
+  }
+}
+
+void ArchTOI::endLine_F() {
+  fitsLine++;
+}
+
+void ArchTOI::closeFile_F() {
+  fits_close_file(fptr, &fitsStatus);
+  fits_report_error(stderr, fitsStatus);  /* print out any error messages */
+}
