Index: /trunk/src/PSRFits.cc
===================================================================
--- /trunk/src/PSRFits.cc	(revision 2)
+++ /trunk/src/PSRFits.cc	(revision 3)
@@ -42,19 +42,66 @@
   int PSRFits::openFile(std::string name)
   {
-    int status = 0;
-    char *template_file = (char *)getPSRFITSdefn().c_str();
-    fits_create_template(&this->fptr, name.c_str(), template_file, &status);
+    /**
+     *  int PSRFits::openFile(string)
+     *   Opens the file named by the argument.
+     *   If the file exists, it is opened.
+     *   If not, it is created using the correct template file.
+     *    In the latter case, the fix function is called, and the file is
+     *    flagged as "new".
+     */
+    int status = 0, exists;
+    fits_file_exists(name.c_str(),&exists,&status);
+    if(exists<=0){
+      char *template_file = (char *)getPSRFITSdefn().c_str();
+      fits_create_template(&this->fptr, name.c_str(), template_file, &status);
+    }
+    else{
+      fits_open_file(&fptr,name.c_str(),READWRITE,&status);
+    }
     if(status) FITSError("PSRFits::openFile",status);
     else{
-      this->newFile = true;
+      if(exists<=0){
+	this->newFile = true;
+	this->fixPSRFITS();
+      }
       this->filestate = OPEN;
-      this->fixPSRFITS();
-    }
-    return status;
-  }
+    }
+    return status;
+  }
+ //--------------------------------------------------------------------------
+
+  void PSRFits::fixPSRFITS()
+  {
+    /**
+     *  void PSRFits::fixPSRFITS()
+     *   Corrects errors that exist in the PSRFITS definition:
+     *      - set the TFORM keyword for the data column in the SUBINT table
+     *      - fix the units for the RA & DEC columns
+     */
+    this->gotoSubint();
+    
+    int status=0;
+    char cval[9];
+    if(USE_X) strcpy(cval,"16X");
+    else      strcpy(cval,"2B");
+    fits_update_key(fptr, TSTRING, "TFORM19", cval, NULL, &status);
+    if(status) FITSError("PSRFits::fixPSRFITS", status);
+    strcpy(cval,"deg");
+    fits_update_key(fptr, TSTRING, "TUNIT6", cval, NULL, &status);
+    if(status) FITSError("PSRFits::fixPSRFITS", status);
+    fits_update_key(fptr, TSTRING, "TUNIT7", cval, NULL, &status);
+    if(status) FITSError("PSRFits::fixPSRFITS", status);
+
+    this->gotoPHU();
+  }
+
  //--------------------------------------------------------------------------
   
   int PSRFits::closeFile()
   {
+    /**
+     *  PSRFits::closeFile()
+     *   Closes the file if it is open. Adjusts the filestate variable as well.
+     */
     int status = 0;
     if(this->filestate == OPEN){
@@ -69,4 +116,8 @@
   int PSRFits::gotoPHU()
   {
+    /**
+     *  int PSRFits::gotoPHU()
+     *   Moves to the primary header unit
+     */
     int status = 0;
     fits_movabs_hdu(this->fptr, 1, NULL, &status);    
@@ -78,4 +129,8 @@
   int PSRFits::gotoSubint()
   {
+    /**
+     *  int PSRFits::gotoSubint()
+     *   Moves to the SUBINT binary table extension.
+     */
     int status = 0;
     fits_movnam_hdu(this->fptr, ANY_HDU, "subint", 0, &status);
@@ -85,4 +140,10 @@
  //--------------------------------------------------------------------------
   
+  /**
+   *  int PSRFits::updateKeyword(name, value)
+   *   Updates the keyword "name" in the current header unit, giving
+   *    it the value given by "value".
+   *   A templated function that should work for all cases.
+   */
   template <typename T> int PSRFits::updateKeyword(std::string name, T value)
   {
@@ -129,5 +190,10 @@
  //--------------------------------------------------------------------------
 
-  int PSRFits::getColnum(char *name){
+  int PSRFits::getColnum(char *name)
+  {
+    /**
+     *  int PSRFits::getColnum()
+     *    Returns the number of the column called "name".
+     */
     int status=0,colnum=0;
     fits_get_colnum(this->fptr, CASEINSEN, name, &colnum, &status);
@@ -136,5 +202,10 @@
   }
   
-  long PSRFits::getNumRows(){
+  long PSRFits::getNumRows()
+  {
+    /**
+     *  long PSRFits::getNumRows()
+     *    Returns the number of rows in the current binary table.
+     */
     int status=0;
     long nrows=0;
@@ -143,5 +214,13 @@
     return nrows;
   }
-  
+ //--------------------------------------------------------------------------
+  
+  /**
+   *  int PSRFits::writeColumnValue(name, value)
+   *   Writes a new value in the column "name" in the current binary table.
+   *   It adds a single value at the bottom of the table (ie. in the largest
+   *    numbered row).
+   *   A templated function that should work for all cases.
+   */
   template <class T> int PSRFits::writeColumnValue(char *name, T value)
   {
@@ -184,4 +263,9 @@
   int PSRFits::startNewRow()
   {
+    /**
+     *  int PSRFits::startNewRow()
+     *    Adds a new row to the bottom of the current binary table.
+     *    Also adds the row number as the ISUBINT column value.
+     */
     int nrows = this->getNumRows();
     int status = 0;
@@ -195,4 +279,10 @@
   void PSRFits::writePHU(DataBlock &block)
   {
+    /**
+     *  void PSRFits::writePHU(block)
+     *   Writes the header information contained in the DataBlock block
+     *    to the primary header unit.
+     *   At the end the current header unit will be the PHU.
+     */
     this->gotoPHU();
 
@@ -254,4 +344,16 @@
   void PSRFits::writeSubint(DataBlock &block)
   {
+    /**
+     *  void PSRFits::writeSubint(block)
+     *   Writes the header information and data contained in the DataBlock 
+     *    block to the SUBINT binary table extension.
+     *   This does the following:
+     *     - Update the table headers
+     *     - Start a new row.
+     *     - Update the single-valued columns for the current row.
+     *     - Call writeSupportArrays to update the array-valued columns.
+     *     - Call writeDataArray to write the data column.
+     *   At the end the current header unit will be the SUBINT extension.
+     */
     this->gotoSubint();
     int ct = 1;
@@ -298,4 +400,14 @@
   void PSRFits::writeSupportArrays(DataBlock &block)
   {
+    /**
+     *  void PSRFits::writeSupportArrays(block)
+     *   A function to write the multi-valued columns to the SUBINT table.
+     *   These columns are:
+     *      - DAT_FREQ: the frequencies of each channel
+     *      - DAT_WTS:  all set to 1. 
+     *      - DAT_OFFS: all set to 0.
+     *      - DAT_SCL:  all set to 1.
+     *   The current header at the end is still SUBINT.
+     */
     int numChannels = block.getKeyword<int>(NCHAN);
     float *dat_freq = new float[numChannels];
@@ -355,4 +467,11 @@
   void PSRFits::writeDataArray(DataBlock &block)
   {
+    /**
+     *  void PSRFits::writeDataArray(block)
+     *   A function to write the multi-dimensional data column to the SUBINT 
+     *    binary table.
+     *   
+     *   The current header at the end is still SUBINT.
+     */
     long nrows = this->getNumRows();
     int colnum = this->getColnum("DATA");
@@ -414,25 +533,4 @@
 
   }
- //--------------------------------------------------------------------------
-
-  void PSRFits::fixPSRFITS()
-  {
-    this->gotoSubint();
-    
-    int status=0;
-    char cval[9];
-    if(USE_X) strcpy(cval,"16X");
-    else      strcpy(cval,"2B");
-    fits_update_key(fptr, TSTRING, "TFORM19", cval, NULL, &status);
-    if(status) FITSError("PSRFits::fixPSRFITS", status);
-//     strcpy(cval,"deg");
-//     fits_update_key(fptr, TSTRING, "TUNIT6", cval, NULL, &status);
-//     if(status) FITSError("PSRFits::fixPSRFITS", status);
-//     fits_update_key(fptr, TSTRING, "TUNIT7", cval, NULL, &status);
-//     if(status) FITSError("PSRFits::fixPSRFITS", status);
-
-    this->gotoPHU();
-  }
-
 
 
