Index: trunk/SophyaExt/FitsIOServer/fabtwriter.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fabtwriter.cc	(revision 2174)
+++ trunk/SophyaExt/FitsIOServer/fabtwriter.cc	(revision 2450)
@@ -12,23 +12,24 @@
   \verbatim
   //-----------------------------------------------------------
+  OPENING A NEW FILE AND WRITING INTO
   -- Exemple 1: Writing element by element
   FitsABTWriter fbtw(fitswrit,BINARY_TBL);
   fbtw.SetExtName("MY_OWN_EXTENSION");
-  fbtw.AddCol("vars",NULL,"km",TSHORT);       // col=0
-  fbtw.AddCol("vars2",NULL,"km",TSHORT);      // col=1
-  fbtw.AddCol("varl",NULL,"Degre",TLONG);     // col=2
-  fbtw.AddCol("varf",NULL,"",TFLOAT);         // col=3
-  fbtw.AddCol("vard","","arcmin",TDOUBLE);    // col=4
+  int c1 = fbtw.AddCol("vars",NULL,"km",TSHORT);       // col=0
+  int c2 = fbtw.AddCol("vars2",NULL,"km",TSHORT);      // col=1
+  int c3 = fbtw.AddCol("varl",NULL,"Degre",TLONG);     // col=2
+  int c4 = fbtw.AddCol("varf",NULL,"",TFLOAT);         // col=3
+  int c5 = fbtw.AddCol("vard","","arcmin",TDOUBLE);    // col=4
   fbtw.SetDebug(3);
   for(long i=0;i<1000;i++) {
     double x = i;
-    fbtw.Write(0,i,1000.*x); // if overflow, managed by cfitsio
+    fbtw.Write(c1,i,1000.*x); // if overflow, managed by cfitsio
                              // Write(int,long,double) is called
-    fbtw.Write(1,i,(short)(1000.*x));
+    fbtw.Write(c2,i,(short)(1000.*x));
                              // if overflow, managed by language
                              // Write(int,long,short) is called
-    fbtw.Write(2,i,x);
-    fbtw.Write(3,i,x);
-    fbtw.Write(4,i,x);
+    fbtw.Write(c3,i,x);
+    fbtw.Write(c4,i,x);
+    fbtw.Write(c5,i,x);
   }
   cout<<"Number of Overflows when writing: "
@@ -51,4 +52,59 @@
   }
   \endverbatim
+  \verbatim
+  //-----------------------------------------------------------
+  //-----------------------------------------------------------
+  //-----------------------------------------------------------
+  OPENING A NEW FILE AND WRITING 2 TABLE EXTENSIONS SIMULTANEOUSLY INTO
+ try {
+
+   cout<<">>>>> Creating a new fits file with FitsABTWriter"<<endl;
+   FitsABTWriter fbtw("!cmvtstfits3.fits",BINARY_TBL,3);
+   //FitsABTWriter fbtw("!cmvtstfits3.fits",false,BINARY_TBL,3);
+   fbtw.SetExtName("Test fits table 1");
+   cout<<"Writing Keys"<<endl;
+   fbtw.WriteKey("MYKEYL",(long)123456789,"my LONG key");
+   fbtw.WriteKey("MYKEYD",1.9999999,"my DOUBLE key");
+   fbtw.WriteKey("MYKEYC","how are you ?","my CHAR* key");
+   dum="do you feel better ?";
+   fbtw.WriteKey("MYKEYS",dum,"my STRING key");
+   i1 = fbtw.AddCol("x1",NULL,"unit1",TDOUBLE);
+   i2 = fbtw.AddCol("x2",NULL,"unit2",TDOUBLE);
+   i3 = fbtw.AddCol("x3",NULL,"unit3",TDOUBLE);
+   fbtw.Print();
+
+   cout<<">>>>> Another extension fits table with FitsABTWriter"<<endl;
+   FitsABTWriter fbtw2("cmvtstfits3.fits",true,BINARY_TBL,3);
+   fbtw2.SetExtName("Test fits table 2");
+   cout<<"Writing Keys"<<endl;
+   fbtw2.WriteKey("MYKEYL",(long)-123456789,"my new LONG key");
+   fbtw2.WriteKey("MYKEYD",-1.9999999,"my new clef DOUBLE key");
+   fbtw2.WriteKey("MYKEYC","how are you NOW ?","my new CHAR* key");
+   dum="do you feel better NOW ?";
+   fbtw2.WriteKey("MYKEYS",dum,"my new STRING key");
+   i11 = fbtw2.AddCol("x11",NULL,"unit11",TDOUBLE);
+   i12 = fbtw2.AddCol("x12",NULL,"unit12",TDOUBLE);
+   i13 = fbtw2.AddCol("x13",NULL,"unit13",TDOUBLE);
+   fbtw2.Print();
+
+   cout<<">>>>> Write into the 2 tables simultaneously"<<endl;
+   for(int i=0;i<NNN;i++) {
+     fbtw.Write(i1,i,i+1.);
+     fbtw.Write(i2,i,i+11.);
+     fbtw.Write(i3,i,i+101.);
+     fbtw2.Write(i11,i,-(i+1.));
+     fbtw2.Write(i12,i,-(i+11.));
+     fbtw2.Write(i13,i,-(i+101.));
+   }
+
+ } catch (PThrowable & exc) {
+   cout<<"Exception : "<<(string)typeid(exc).name()
+       <<" - Msg= "<<exc.Msg()<<endl;
+   return -2;
+ } catch (...) {
+   cout<<" some other exception was caught !"<<endl;
+   return -2;
+ }
+  \endverbatim
 */
 
@@ -56,5 +112,5 @@
 /*!
   Constructor.
-  \param fname : FITS file name to be written
+  \param fname : FITS file name to be written (a new fits file is created)
   \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
   \param lp : debug level
@@ -62,15 +118,44 @@
 FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
 {
-  createfits(fname.c_str(),hdutype,lp);
+  cr_or_upd_fits(fname.c_str(),false,hdutype,lp);
+}
+
+/*!
+  Constructor.
+  \param cfname : FITS file name to be written (a new fits file is created)
+  \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
+  \param lp : debug level
+*/
+FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
+{
+  cr_or_upd_fits(cfname,false,hdutype,lp);
+}
+
+/*!
+  Constructor.
+  \param fname : FITS file name to be written (created or updated)
+  \param update : file is created if FALSE, open for update if TRUE
+  \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
+  \param lp : debug level
+*/
+FitsABTWriter::FitsABTWriter(string fname,bool update,int hdutype,int lp)
+{
+  cr_or_upd_fits(fname.c_str(),update,hdutype,lp);
+}
+
+/*!
+  Constructor.
+  \param cfname : FITS file name to be written (created or updated)
+  \param update : file is created if FALSE, open for update if TRUE
+  \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
+  \param lp : debug level
+*/
+FitsABTWriter::FitsABTWriter(const char* cfname,bool update,int hdutype,int lp)
+{
+  cr_or_upd_fits(cfname,update,hdutype,lp);
 }
 
 /*! See FitsABTWriter() */
-FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
-{
-  createfits(cfname,hdutype,lp);
-}
-
-/*! See FitsABTWriter() */
-void FitsABTWriter::createfits(const char *cfname,int hdutype,int lp)
+void FitsABTWriter::cr_or_upd_fits(const char *cfname,bool update,int hdutype,int lp)
 {
  FirstTime = true;
@@ -80,20 +165,30 @@
  FitsFN = cfname;
  NOverFlow = 0;
+ Update = update;
 
  if(DbgLevel)
-   cout<<"FitsABTWriter::createfits FitsFN="<<FitsFN
+   cout<<"FitsABTWriter::cr_or_upd_fits FitsFN="<<FitsFN
        <<" HduType="<<HduType<<endl;
 
  if(FitsFN.size() <= 0 )
-   throw ParmError("FitsABTWriter::createfits: Fits file name error\n");
+   throw ParmError("FitsABTWriter::cr_or_upd_fits: Fits file name error\n");
 
  if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
-   throw TypeMismatchExc("FitsABTWriter::createfits: Only BINARY or ASCII table permitted\n");
-
- // create new FITS file
+   throw TypeMismatchExc("FitsABTWriter::cr_or_upd_fits: Only BINARY or ASCII table permitted\n");
+
+ // create or update FITS file
  int sta=0;
- if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) {
-   printerror(sta);
-   throw NullPtrError("FitsABTWriter::createfits: Error creating Fits file\n");
+ if(Update) {
+   if(fits_open_file(&FitsPtr,FitsFN.c_str(),READWRITE,&sta)) {
+     printerror(sta);
+     throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error opening Fits file for update\n");
+   }
+   if(DbgLevel) cout<<"FitsABTWriter::cr_or_upd_fits: fits file has been opened for update"<<endl;
+ } else {
+   if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) {
+     printerror(sta);
+     throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error creating new Fits file\n");
+   }
+   if(DbgLevel) cout<<"FitsABTWriter::cr_or_upd_fits: a new fits file has been created"<<endl;
  }
 
@@ -102,5 +197,5 @@
  //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
  //  printerror(sta);
- //  throw NullPtrError("FitsABTWriter::createfits: Error creating Primary extension\n");
+ //  throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error creating Primary extension\n");
  //}
 
@@ -144,4 +239,15 @@
  if(comment) k.comment=comment; else k.comment="";
  LongKey.push_back(k);
+}
+
+/*! Write a string value into Fits Header */
+void FitsABTWriter::WriteKey(const char *keyname,string val,char* comment)
+{
+ if(keyname==NULL || strlen(keyname)<=0) return;
+ KeyString k;
+ k.keyname=keyname;
+ k.val=val;
+ if(comment) k.comment=comment; else k.comment="";
+ StringKey.push_back(k);
 }
 
@@ -167,6 +273,15 @@
        printerror(sta);
    }
+ if(StringKey.size()>0)
+   for(unsigned int i=0;i<StringKey.size();i++) {
+     char* key = const_cast<char*>(StringKey[i].keyname.c_str());
+     char* com = const_cast<char*>(StringKey[i].comment.c_str());
+     char* val = const_cast<char*>(StringKey[i].val.c_str());
+     if(fits_update_key(FitsPtr,TSTRING,key,val,com,&sta))
+       printerror(sta);
+   }
  DoubleKey.resize(0);
  LongKey.resize(0);
+ StringKey.resize(0);
 }
 
