Index: /trunk/src/Block.cc
===================================================================
--- /trunk/src/Block.cc	(revision 24)
+++ /trunk/src/Block.cc	(revision 25)
@@ -58,9 +58,9 @@
      * \return False if the file is at eof, else read header and return true.
      */
-    char *hdr = new char[headerLength];
+    char *hdr = new char[Block::headerLength];
     file.read( hdr, headerLength*sizeof(char) );
     if(file.eof()) return false;
     for(int c=0;c<headerLength;c++) this->headerString[c] = hdr[c];
-    delete hdr;
+    delete [] hdr;
     return true;
   }
Index: /trunk/src/Header.cc
===================================================================
--- /trunk/src/Header.cc	(revision 24)
+++ /trunk/src/Header.cc	(revision 25)
@@ -85,5 +85,4 @@
     this->ant_y    = 2816759.1;
     this->ant_z    = -3454036.3;
-    this->fd_poln  = "LIN";
     this->backend  = "FB_1BIT";
     this->beconfig = "";
@@ -97,6 +96,4 @@
     this->cal_dcyc = 0.;
     this->cal_phs  = 0.;
-    // Set the correct units for the DAT_FREQ entry in the subint table.
-    //     this->freq_units = "MHz";
     
     if(surveyType == S70){
@@ -107,4 +104,8 @@
       this->frontend = "70cm_Cavity_Disk";
 //       this->fd_mode  = "FIXED";
+      this->fd_poln  = "LIN";
+      this->fd_hand  = 1;
+      this->fd_sang  = 0.;
+      this->fd_xyph  = 0.;
 
       this->bmaj = 0.75;
Index: /trunk/src/Header.hh
===================================================================
--- /trunk/src/Header.hh	(revision 24)
+++ /trunk/src/Header.hh	(revision 25)
@@ -164,7 +164,4 @@
     float       bpa;
 
-    // This is the units for DAT_FREQ in the subint table
-//     std::string freq_units;
-    
 
   };
Index: /trunk/src/PSRFits.cc
===================================================================
--- /trunk/src/PSRFits.cc	(revision 24)
+++ /trunk/src/PSRFits.cc	(revision 25)
@@ -19,4 +19,5 @@
     this->newFile = true;
     this->filestate = CLOSED;
+    this->UT_initial = decToDMS(0.);
   }
  //--------------------------------------------------------------------------
@@ -101,15 +102,10 @@
       fits_create_template(&this->fptr, name.c_str(), template_file, &status);
       std::cerr << "Creating new file " << name << "\n";
+      this->newFile = true;
     }
-    else{
-      this->newFile = false;
-      fits_open_file(&this->fptr, name.c_str(), READWRITE, &status);
-    }
+    else fits_open_file(&this->fptr, name.c_str(), READWRITE, &status);
     if(status) FITSError("PSRFits::openFile 2",status);
     else{
-      if(exists<=0){
-	this->newFile = true;
-	this->fixPSRFITS();
-      }
+      if(exists<=0) this->fixPSRFITS();
       this->gotoPHU();
       this->filestate = OPEN;
Index: /trunk/src/PSRFits.hh
===================================================================
--- /trunk/src/PSRFits.hh	(revision 24)
+++ /trunk/src/PSRFits.hh	(revision 25)
@@ -139,4 +139,6 @@
     /** Is the file newly created? */
     bool        isNewFile(){return newFile;};
+    void        setNewFile(){newFile = true;};
+    void        setNewFile(bool f){newFile = f;};
 
     /** Is the file open or closed? */
@@ -178,14 +180,19 @@
 
     // Functions found in PSRFits.cc (in the same order...)
+    /** Generate the name of the fits file. */
     void setFITSname(Header::SURVEY surveyType); 
-         ///< Generate the name of the fits file.
+
+    /** Does the file exist on disk already? */
     bool fileExists();                           
-         ///< Does the file exist on disk already?
+       
+    /** Opens the file named by the argument. */
     int  openFile(std::string name);              
-         ///< Opens the file named by the argument.
-    void fixPSRFITS();                           
-         ///< Makes corrections to PSRFITS definition.
-    void addNewKeywords(DataBlock &block);       
-         ///< Writes new keywords to the primary fits header.
+       
+    /** Makes corrections to PSRFITS definition. */
+    void fixPSRFITS();  
+       
+    /** Writes new keywords to the primary fits header. */
+    void addNewKeywords(DataBlock &block); 
+         
     int  closeFile();   ///< Closes the file.
     int  gotoPHU();     ///< Moves to the primary header unit
@@ -193,32 +200,37 @@
     int  gotoSubint();  ///< Moves to the SUBINT binary table extension.
 
+    /** Update the value of a keyword. */
     template <class T> int updateKeyword(std::string name, T value); 
-         ///< Update the value of a keyword.
-
-    int  getColnum(char *name); 
-         ///< Returns the number of the named column.
+
+    /** Returns the number of the named column. */
+    int  getColnum(char *name);
+       
+    /** Returns the number of rows in the current binary table. */
     long getNumRows();          
-         ///< Returns the number of rows in the current binary table.
+
+    /** Write a value to a named column. */
     template <class T> int writeColumnValue(char *name, T value);
-         ///< Write a value to a named column.
-
+
+    /** Add new row to current table. */
     int  startNewRow();         
-         ///< Add new row to current table.
+
+    /** Add new row to SUBINT table. */
     void startNewSubintRow();   
-         ///< Add new row to SUBINT table.
-
-    void writePHU(DataBlock &block);             
-         ///< Write Primary Header Unit data.
+
+    /** Write Primary Header Unit data. */
+    void writePHU(DataBlock &block);      
+
+    /** Set up a FITS file */       
     int  initialise(DataBlock &block);
+
+    /** Define and write the header keywords for the SUBINT table. */
     void setupSubint(DataBlock &block);
-    int  writeSubint(DataBlock &block);          
+
+    /** Write a row of SUBINT table data. */
     void writeSubintRow(DataBlock &block);          
-         ///< Write SUBINT table data.
+
+    /** Write information to HISTORY table. */
     void writeHistory(int argc, char * argv[]); 
-         ///< Write information to HISTORY table.
-    void writeSupportArrays(DataBlock &block); 
-         ///< Write multi-valued columns in SUBINT table.
-    void writeDataArray(DataBlock &block); 
-         ///< Write data to SUBINT table.
+
 
   private:
Index: /trunk/src/PSRFits_fileIO.cc
===================================================================
--- /trunk/src/PSRFits_fileIO.cc	(revision 24)
+++ /trunk/src/PSRFits_fileIO.cc	(revision 25)
@@ -54,4 +54,332 @@
   }
 
+ //--------------------------------------------------------------------------
+
+  int PSRFits::initialise(DataBlock &block)
+  {
+    /**
+     * Initialises a fits file for use in the conversion program. The
+     * order of business is as follows: 
+     *
+     * <ul>
+     *   <li> When new:
+     *   <ul><li> Set the initial date & time (UT) for the file. 
+     *       <li> If a new file is beginning, and the time has changed
+     *       and is smaller than the existing time, we need to
+     *       increment the date. This may be the case if we are
+     *       working through a large tape file and the blocks go past
+     *       midnight. 
+     *   </ul>
+     *   <li> Set the beam used in the file.
+     *   <li> Define the FITS file name.
+     *   <li> Set the starting block value.
+     *   <li> If new, test whether file exists.
+     *   <li> Open the fits file.
+     *   <li> If new:
+     *   <ul><li> Write the PHU headers using writePHU();
+     *       <li> Setup and write the SUBINT header with setupSubint(). 
+     *   </ul>
+     * </ul>
+     * \param block The block used to get the header information.
+     */
+
+    std::string newUT,date;
+
+
+    if(this->newFile){
+      newUT = block.getKeyword<std::string>(UT);
+//     newUT = block.getKeyword<std::string>(UT_STRT);
+      date   = block.getKeyword<std::string>(DATE);
+
+      // if we have gone over midnight.
+      if(dmsToDec(this->UT_initial) > dmsToDec(newUT) ) date = addDay(date);
+
+      this->setUT(newUT);
+      this->setDate(date);
+    }
+
+    this->setBeam(block.getKeyword<int>(IBEAM));
+
+    this->setFITSname(block.getSurveyType());
+
+    this->setInitialBlock(block.getKeyword<int>(BLK_CNTR));
+
+    if(this->newFile && this->fileExists()){
+      std::cerr << "\aThe file "<< this->getFITSname() << " already exists.\n"
+		<< "Don't want to overwrite, so exiting...\n";
+      return FAILURE;
+    }
+
+    if(this->openFile()) {
+      std::cerr << "\aCannot open file "<< this->getFITSname()
+		<< ".\nExiting...\n";
+      return FAILURE;
+    }
+
+    if(this->newFile){
+      
+      this->writePHU(block);
+
+      this->setupSubint(block);
+    
+    }
+
+    this->newFile = false;
+
+    return SUCCESS;
+
+  }
+ //--------------------------------------------------------------------------
+
+  void PSRFits::setupSubint(DataBlock &block)
+  {
+    /**
+     *  A function to set up the subintegration binary table
+     *  extension.  This defines the necessary keywords, as well as
+     *  the DAT_... arrays to go in the columns (as these will be the
+     *  same for all rows of the subintegration table -- at least as
+     *  far as the conversion program goes.)
+     *
+     *  It then calls the Subint::writeKeywords() function to write
+     *  the header keywords to the table.
+     *
+     * \param block The block containing the header information.
+     */
+    
+    this->subintTable.nbin = 1; //CHECK --> needs to be made more general!
+    this->subintTable.nch_file = block.getKeyword<int>(NCHAN);
+    this->subintTable.npol = 1;
+    this->subintTable.nsblk = block.getKeyword<int>(SAMP_GRP,0) * 
+      block.getKeyword<int>(GRP_BLK);
+    this->subintTable.tsamp = block.getKeyword<float>(SAMP_INT,0)/1000.;
+
+    // define the DAT_FREQ array;
+    float deltaFreq = block.getKeyword<float>(CHANBW) / 
+      float(this->subintTable.nch_file);
+    this->subintTable.dat_freq = new float[this->subintTable.nch_file];
+    this->subintTable.dat_freq[0] = block.getKeyword<float>(FREQ_CHAN1);
+    for(int i=1;i<this->subintTable.nch_file;i++) 
+      this->subintTable.dat_freq[i] = this->subintTable.dat_freq[i-1] + deltaFreq;
+
+    // define the DAT_WTS array;
+    this->subintTable.dat_wts = new float[this->subintTable.nch_file];
+    for(int i=0;i<this->subintTable.nch_file;i++) this->subintTable.dat_wts[i] = 1.;
+
+    // define the DAT_OFFS array;
+    this->subintTable.dat_offs = new float[this->subintTable.nch_file];
+    for(int i=0;i<this->subintTable.nch_file;i++) this->subintTable.dat_offs[i] = 0.;
+
+    // define the DAT_SCL array;
+    this->subintTable.dat_scl = new float[this->subintTable.nch_file];
+    for(int i=0;i<this->subintTable.nch_file;i++) this->subintTable.dat_scl[i] = 1.;
+    
+    
+    this->subintTable.setAllocatedFlag(true);
+
+    // now write the keywords to the subint table
+    if(this->gotoSubint()){
+      std::cerr << 
+	"Error <PSRFits::initialise> : Could not go to the SUBINT table.\n";
+    }
+    else this->subintTable.writeKeywords(this->fptr);
+    
+  }
+
+ //--------------------------------------------------------------------------
+
+  void Subint::writeKeywords(fitsfile *fptr)
+  {
+    /**
+     *  A special function in the Subint class to write all the header
+     *  keywords necessary for the subintegration binary table
+     *  extension. This should only need to be done once per file.
+     *
+     *  The keywords are stored as private members of Subint, and need
+     *  to be defined beforehand.
+     *
+     *  \param fptr The fitsfile pointer corresponding to the file to
+     *  be written to.
+     */
+
+    int status=0;
+    fits_update_key(fptr,TINT,"NCH_FILE",&this->nch_file,NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 1",status);
+    
+    status=0;
+    fits_update_key(fptr,TSTRING,"INT_TYPE", (char *)this->int_type.c_str(),NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 2",status);
+    
+    status=0;
+    fits_update_key(fptr,TSTRING,"INT_UNIT",(char *)this->int_unit.c_str(),NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 3",status);
+    
+    status=0;
+    fits_update_key(fptr,TINT,"NCH_STRT",&this->nch_strt,NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 4",status);
+    
+    status=0;
+    fits_update_key(fptr,TINT,"NPOL",&this->npol,NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 5",status);
+    
+    status=0;
+    fits_update_key(fptr,TINT,"NBIN",&this->nbin,NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 6",status);
+    
+    status=0;
+    fits_update_key(fptr,TINT,"NSBLK",&this->nsblk,NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 7",status);
+    
+    status=0;
+    fits_update_key(fptr,TFLOAT,"TSAMP",&this->tsamp,NULL,&status);
+    if(status) FITSError("Subint::writeKeywords 8",status);
+    
+    status=0;
+    int colnum=0;
+    fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status);
+    if(status) FITSError("Subint::writeKeywords 10", status);
+
+    int numData = this->nbin * this->nch_file * this->npol * this->nsblk;
+    //    int dataPerByte = 8 / block.getKeyword<int>(NBITS,0);
+    //    int numBytes = numData / dataPerByte;   
+    int numBytes = numData / 8;
+    status=0;
+    if(USE_X) fits_modify_vector_len (fptr, colnum, numData, &status);
+    else      fits_modify_vector_len (fptr, colnum, numBytes, &status);
+    if(status) FITSError("Subint::writeKeywords 12", status);
+
+    // delete the existing "TDIM19 = (*,*,*,*)"
+    status=0;
+    fits_delete_key(fptr, "TDIM19", &status);
+    if(status) FITSError("Subint::writeKeywords 9", status);
+    int naxis=4;
+    long naxes[4];
+    naxes[0] = this->nbin;
+    naxes[1] = this->nch_file;
+    naxes[2] = this->npol;
+    naxes[3] = this->nsblk;
+    status=0;
+    fits_write_tdim(fptr, colnum, naxis, naxes, &status);
+    if(status) FITSError("Subint::writeKeywords 11", status);
+
+  }
+ //--------------------------------------------------------------------------
+
+  void PSRFits::writeSubintRow(DataBlock &block)
+  {
+    /**
+     * This function writes an entire new row in the SUBINT table extension.
+     * The procedure is as follows:
+     * <ul> <li> Move to the subint table.
+     *      <li> Start a new row with startNewSubintRow()
+     *      <li> Write the single-valued columns with writeColumnValue()
+     *      <li> Write the muli-valued DAT_... columns
+     *      <li> Write the data.
+     * </ul>
+     * \param block The block containing the header info & the data.
+     */
+
+    this->gotoSubint();
+    this->startNewSubintRow(); 
+    // CHECK -- need some check of blk_cntr to make sure the number is 
+    // CHECK -- incrementing correctly?
+    float tsubint = block.getKeyword<float>(BLK_SEC);
+    double off_sub;
+    
+    off_sub = 0.5*(2.*(block.getKeyword<int>(BLK_CNTR)-this->blk_initial)+1)
+      * block.getKeyword<double>(BLK_SEC);
+
+    double lst_sub  = dmsToDec(block.getKeyword<std::string>(LST));
+    double ra_sub   = dmsToDec(block.getKeyword<std::string>(RA))*15.;
+    double dec_sub  = dmsToDec(block.getKeyword<std::string>(DEC));
+    double glon_sub = block.getKeyword<double>(GL);
+    double glat_sub = block.getKeyword<double>(GB);
+    float  fd_ang   = block.getKeyword<float>(FEED_ANGLE);
+    float  par_ang  = block.getKeyword<float>(PANGLE);
+    float  pos_ang  = remainderf(par_ang + fd_ang, 360.);
+    float  tel_az   = block.getKeyword<float>(TEL_AZ);
+    float  tel_zen  = block.getKeyword<float>(TEL_ZEN);
+    
+    this->writeColumnValue("TSUBINT",  tsubint);
+    this->writeColumnValue("OFFS_SUB", off_sub);
+    this->writeColumnValue("LST_SUB",  lst_sub);
+    this->writeColumnValue("RA_SUB",   ra_sub);
+    this->writeColumnValue("DEC_SUB",  dec_sub);
+    this->writeColumnValue("GLON_SUB", glon_sub);
+    this->writeColumnValue("GLAT_SUB", glat_sub);
+    this->writeColumnValue("FD_ANG",   fd_ang);
+    this->writeColumnValue("POS_ANG",  pos_ang);
+    this->writeColumnValue("PAR_ANG",  par_ang);
+    this->writeColumnValue("TEL_AZ",   tel_az);
+    this->writeColumnValue("TEL_ZEN",  tel_zen);
+
+    /////////////////////////////////////
+
+    long nrows = this->getNumRows();
+    int colnum = this->getColnum("DAT_FREQ");
+    int status=0;
+    fits_modify_vector_len (this->fptr, colnum, 
+			    this->subintTable.nch_file, &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+    status=0;
+    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
+		   this->subintTable.nch_file, this->subintTable.dat_freq, 
+		   &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+
+    colnum = this->getColnum("DAT_WTS");
+    status=0;
+    fits_modify_vector_len (this->fptr, colnum, 
+			    this->subintTable.nch_file, &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+    status=0;
+    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
+		   this->subintTable.nch_file, this->subintTable.dat_wts, 
+		   &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+
+    colnum = this->getColnum("DAT_OFFS");
+    status=0;
+    fits_modify_vector_len (this->fptr, colnum, 
+			    this->subintTable.nch_file, &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+    status=0;
+    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
+		   this->subintTable.nch_file, this->subintTable.dat_offs, 
+		   &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+
+    colnum = this->getColnum("DAT_SCL");
+    status=0;
+    fits_modify_vector_len (this->fptr, colnum, 
+			    this->subintTable.nch_file, &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+    status=0;
+    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
+		   this->subintTable.nch_file, this->subintTable.dat_scl, 
+		   &status);
+    if(status) FITSError("PSRFits::writeSubintRow", status);
+
+    //////////////////////////////////////////
+
+    unsigned char *data = (unsigned char *)block.getData();
+
+    if(this->FITSname == "PM_061222_150342_01.fits" && nrows==1){
+      std::cout << charToBinary(data[0]);
+      std::cout << charToBinary(data[1]);
+      std::cout << charToBinary(data[2]);
+      std::cout << "\n";
+    }
+
+    int numData = this->subintTable.nbin * this->subintTable.nch_file * 
+      this->subintTable.npol * this->subintTable.nsblk;
+    int dataPerByte = 8 / block.getKeyword<int>(NBITS,0);
+    int numBytes = numData / dataPerByte;   
+    colnum = this->getColnum("DATA");
+    status=0;
+    fits_write_col_byt(this->fptr, colnum, nrows, 1, numBytes, data, &status);
+    if(status) FITSError("PSRFits::writeDataArray 4", status);
+    
+
+  }
  //--------------------------------------------------------------------------
 
@@ -189,480 +517,3 @@
  //--------------------------------------------------------------------------
 
-  int PSRFits::writeSubint(DataBlock &block)
-  {
-    /**
-     *   Writes the header information and data contained in the DataBlock 
-     *    block to the SUBINT binary table extension.
-     *   This does the following:
-     *   <ul><li> Update the table headers
-     *       <li> Start a new row.
-     *       <li> Update the single-valued columns for the current row.
-     *       <li> Call writeSupportArrays to update the array-valued columns.
-     *       <li> Call writeDataArray to write the data column.
-     *   </ul>
-     *   At the end the current header unit will be the SUBINT extension.
-     */
-    if(this->gotoSubint()){
-      std::cerr << 
-	"Error <PSRFits::writeSubint> : Could not go to the SUBINT table.\n";
-      return 1;
-    }
-    
-    this->updateKeyword("NCH_FILE", block.getKeyword<int>(NCHAN));
-    this->updateKeyword("INT_TYPE", SUBINT_kw_int_type);
-    this->updateKeyword("INT_UNIT", SUBINT_kw_int_unit);
-    this->updateKeyword("NCH_STRT", SUBINT_kw_nch_strt);
-
-    this->startNewSubintRow(); 
-    // CHECK -- need some check of blk_cntr to make sure the number is 
-    // CHECK -- incrementing correctly?
-    float tsubint = block.getKeyword<float>(BLK_SEC);
-    double off_sub;
-    // CHECK -- OFF_SUB is the offset, from the start (taken to mean the 
-    // CHECK -- starting time listed in the PHU), of the centre of the 
-    // CHECK -- current subintegration.
-
-//     off_sub = dmsToDec(block.getKeyword<std::string>(UT)) - 
-//       dmsToDec(this->UT_initial);
-//     if(off_sub<0.) off_sub += 24.;
-//     off_sub = off_sub*3600. + block.getKeyword<float>(BLK_SEC)/2.;
-    
-    off_sub = 0.5*(2.*(block.getKeyword<int>(BLK_CNTR)-this->blk_initial)+1)
-      * block.getKeyword<double>(BLK_SEC);
-
-    double lst_sub  = dmsToDec(block.getKeyword<std::string>(LST));
-    double ra_sub   = dmsToDec(block.getKeyword<std::string>(RA))*15.;
-    double dec_sub  = dmsToDec(block.getKeyword<std::string>(DEC));
-    double glon_sub = block.getKeyword<double>(GL);
-    double glat_sub = block.getKeyword<double>(GB);
-    float  fd_ang   = block.getKeyword<float>(FEED_ANGLE);
-    float  par_ang  = block.getKeyword<float>(PANGLE);
-    float  pos_ang  = remainderf(par_ang + fd_ang, 360.);
-    float  tel_az   = block.getKeyword<float>(TEL_AZ);
-    float  tel_zen  = block.getKeyword<float>(TEL_ZEN);
-    
-    this->writeColumnValue("TSUBINT",  tsubint);
-    this->writeColumnValue("OFFS_SUB", off_sub);
-    this->writeColumnValue("LST_SUB",  lst_sub);
-    this->writeColumnValue("RA_SUB",   ra_sub);
-    this->writeColumnValue("DEC_SUB",  dec_sub);
-    this->writeColumnValue("GLON_SUB", glon_sub);
-    this->writeColumnValue("GLAT_SUB", glat_sub);
-    this->writeColumnValue("FD_ANG",   fd_ang);
-    this->writeColumnValue("POS_ANG",  pos_ang);
-    this->writeColumnValue("PAR_ANG",  par_ang);
-    this->writeColumnValue("TEL_AZ",   tel_az);
-    this->writeColumnValue("TEL_ZEN",  tel_zen);
-    
-    this->writeSupportArrays(block); //DAT_FREQ,DAT_WTS,DAT_OFFS,DAT_SCL
-
-    this->writeDataArray(block);
-   
-    this->gotoPHU();
-
-    return 0;
-
-  }
- //--------------------------------------------------------------------------
-
-  void PSRFits::writeSupportArrays(DataBlock &block)
-  {
-    /**
-     *   A function to write the multi-valued columns to the SUBINT table.
-     *   These columns are:
-     *   <ul><li> DAT_FREQ: the frequencies of each channel
-     *       <li> DAT_WTS:  all set to 1. 
-     *       <li> DAT_OFFS: all set to 0.
-     *       <li> DAT_SCL:  all set to 1.
-     *   </ul>
-     *   The current header at the end is still SUBINT.
-     */
-    int numChannels = block.getKeyword<int>(NCHAN);
-    float *dat_freq = new float[numChannels];
-    dat_freq[0] = block.getKeyword<float>(FREQ_CHAN1);
-    float deltaFreq = block.getKeyword<float>(CHANBW) / float(numChannels);
-    for(int i=1;i<numChannels;i++) dat_freq[i] = dat_freq[i-1] + deltaFreq;
-
-    float *dat_wts = new float[numChannels];
-    for(int i=0;i<numChannels;i++) dat_wts[i] = 1.;
-    float *dat_offs = new float[numChannels];
-    for(int i=0;i<numChannels;i++) dat_offs[i] = 0.;
-    float *dat_scl = new float[numChannels];
-    for(int i=0;i<numChannels;i++) dat_scl[i] = 1.;
-
-    int status = 0;
-    long nrows = this->getNumRows();
-
-    int colnum = this->getColnum("DAT_FREQ");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, numChannels, &status);
-    if(status) FITSError("PSRFits::writeSupportArray", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   numChannels, dat_freq, &status);
-    if(status) FITSError("PSRFits::writeSupportArrays", status);
-
-    colnum = this->getColnum("DAT_WTS");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, numChannels, &status);
-    if(status) FITSError("PSRFits::writeSupportArray", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   numChannels, dat_wts, &status);
-    if(status) FITSError("PSRFits::writeSupportArrays", status);
-
-    colnum = this->getColnum("DAT_OFFS");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, numChannels, &status);
-    if(status) FITSError("PSRFits::writeSupportArray", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   numChannels, dat_offs, &status);
-    if(status) FITSError("PSRFits::writeSupportArrays", status);
-
-    colnum = this->getColnum("DAT_SCL");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, numChannels, &status);
-    if(status) FITSError("PSRFits::writeSupportArray", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   numChannels, dat_scl, &status);
-    if(status) FITSError("PSRFits::writeSupportArrays", status);
-
-  }
- //--------------------------------------------------------------------------
-
-  void PSRFits::writeDataArray(DataBlock &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");
-    int status = 0;
-
-    int numBin = 1;
-
-    int numChan = block.getKeyword<int>(NCHAN,0);
-
-    int numPol;
-    if(block.getKeyword<int>(NBITS,1)>0) numPol=2;
-    else numPol = 1;
-
-    int numSampBlock  = block.getKeyword<int>(SAMP_GRP,0) * 
-      block.getKeyword<int>(GRP_BLK);
-
-    int dataPerByte = 8 / block.getKeyword<int>(NBITS,0);
-
-
-    int numData = numChan * numPol * numSampBlock;
-    //CHECK -- IS THIS CONVERSION TO BYTES CORRECT?
-    int numBytes = numData / dataPerByte;   
-
-    unsigned char *data = (unsigned char *)block.getData();
-
-    //Fixing size of vector
-    if(this->newFile){
-      status=0;
-      if(USE_X) fits_modify_vector_len (this->fptr, colnum, numData, &status);
-      else      fits_modify_vector_len (this->fptr, colnum, numBytes, &status);
-      if(status) FITSError("PSRFits::writeDataArray 1", status);
-    }
- 
-    long naxis=4,naxes[4];
-    // CHECK -- ORDER OF AXES: first axis is quickest-varying one, so this is 
-    // CHECK --  the channel axis. Next quickest is the pol axis (equivalent 
-    // CHECK --  to the filter? CHECK THIS). Slowest varying is the sample  
-    // CHECK --  axis (which has length numBin).
-    // CHECK -- IS THE CONVERSION TO BYTES CORRECT? We are dividing the channel
-    // CHECK --  axis, as this should be divisible by 8.
-    naxes[0] = numBin;
-
-    if(USE_X) naxes[1] = numChan; 
-    else      naxes[1] = numChan / dataPerByte; 
-
-    naxes[2] = numPol;
-
-    naxes[3] = numSampBlock;
-
-    if(this->newFile){
-      status=0;
-      // delete the existing "TDIM19 = (*,*,*,*)"
-      fits_delete_key(this->fptr, "TDIM19", &status);
-      if(status) FITSError("PSRFits::writeDataArray 2", status);
-      status=0;
-      fits_write_tdim(this->fptr, colnum, naxis, naxes, &status);
-      if(status) FITSError("PSRFits::writeDataArray 3", status);
-    }
-
-    status=0;
-    fits_write_col_byt(this->fptr, colnum, nrows, 1, numBytes, data, &status);
-    if(status) FITSError("PSRFits::writeDataArray 4", status);
-
-//     if(USE_X && this->newFile) {
-    if(this->newFile && this->getNumRows()>1) {
-      this->newFile = false;
-      status=0;
-      fits_delete_rows(this->fptr, nrows+1, 1, &status);
-      if(status) FITSError("PSRFits::writeDataArray 5", status);
-    }
-
-  }
- //--------------------------------------------------------------------------
-
-  int PSRFits::initialise(DataBlock &block)
-  {
-    /**
-     * Initialises a fits file by setting the start variables,
-     *  calling the PHU setup, and setting the header keywords for
-     *  the subint table.
-     */
-
-    this->setUT(block.getKeyword<std::string>(UT_STRT));
-
-    this->setDate(block.getKeyword<std::string>(DATE));
-
-    this->setBeam(block.getKeyword<int>(IBEAM));
-
-    this->setFITSname(block.getSurveyType());
-
-    this->setInitialBlock(block.getKeyword<int>(BLK_CNTR));
-
-    if(this->fileExists()){
-      std::cerr << "\aThe file "<< this->getFITSname() 
-		<< " already exists.\nDon't want to overwrite, so exiting...\n";
-      return FAILURE;
-    }
-    if(this->openFile()) {
-      std::cerr << "\aCannot open file "<< this->getFITSname()
-		<< ".\nExiting...\n";
-      return FAILURE;
-    }
-
-    this->writePHU(block);
-
-    this->setupSubint(block);
-    
-
-    return SUCCESS;
-
-  }
- //--------------------------------------------------------------------------
-
-  void PSRFits::setupSubint(DataBlock &block)
-  {
-    
-    this->subintTable.nbin = 1; //CHECK --> needs to be made more general!
-    this->subintTable.nch_file = block.getKeyword<int>(NCHAN);
-    this->subintTable.npol = 1;
-    this->subintTable.nsblk = block.getKeyword<int>(SAMP_GRP,0) * 
-      block.getKeyword<int>(GRP_BLK);
-    this->subintTable.tsamp = block.getKeyword<float>(SAMP_INT,0)/1000.;
-
-    // define the DAT_FREQ array;
-    float deltaFreq = block.getKeyword<float>(CHANBW) / 
-      float(this->subintTable.nch_file);
-    this->subintTable.dat_freq = new float[this->subintTable.nch_file];
-    this->subintTable.dat_freq[0] = block.getKeyword<float>(FREQ_CHAN1);
-    for(int i=1;i<this->subintTable.nch_file;i++) 
-      this->subintTable.dat_freq[i] = this->subintTable.dat_freq[i-1] + deltaFreq;
-
-    // define the DAT_WTS array;
-    this->subintTable.dat_wts = new float[this->subintTable.nch_file];
-    for(int i=0;i<this->subintTable.nch_file;i++) this->subintTable.dat_wts[i] = 1.;
-
-    // define the DAT_OFFS array;
-    this->subintTable.dat_offs = new float[this->subintTable.nch_file];
-    for(int i=0;i<this->subintTable.nch_file;i++) this->subintTable.dat_offs[i] = 0.;
-
-    // define the DAT_SCL array;
-    this->subintTable.dat_scl = new float[this->subintTable.nch_file];
-    for(int i=0;i<this->subintTable.nch_file;i++) this->subintTable.dat_scl[i] = 1.;
-    
-    
-    this->subintTable.setAllocatedFlag(true);
-
-    // now write the keywords to the subint table
-    if(this->gotoSubint()){
-      std::cerr << 
-	"Error <PSRFits::initialise> : Could not go to the SUBINT table.\n";
-    }
-    else this->subintTable.writeKeywords(this->fptr);
-    
-  }
-
- //--------------------------------------------------------------------------
- //--------------------------------------------------------------------------
-
-  void Subint::writeKeywords(fitsfile *fptr)
-  {
-    int status=0;
-    fits_update_key(fptr,TINT,"NCH_FILE",&this->nch_file,NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 1",status);
-    
-    status=0;
-    fits_update_key(fptr,TSTRING,"INT_TYPE", (char *)this->int_type.c_str(),NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 2",status);
-    
-    status=0;
-    fits_update_key(fptr,TSTRING,"INT_UNIT",(char *)this->int_unit.c_str(),NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 3",status);
-    
-    status=0;
-    fits_update_key(fptr,TINT,"NCH_STRT",&this->nch_strt,NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 4",status);
-    
-    status=0;
-    fits_update_key(fptr,TINT,"NPOL",&this->npol,NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 5",status);
-    
-    status=0;
-    fits_update_key(fptr,TINT,"NBIN",&this->nbin,NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 6",status);
-    
-    status=0;
-    fits_update_key(fptr,TINT,"NSBLK",&this->nsblk,NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 7",status);
-    
-    status=0;
-    fits_update_key(fptr,TFLOAT,"TSAMP",&this->tsamp,NULL,&status);
-    if(status) FITSError("Subint::writeKeywords 8",status);
-    
-    status=0;
-    int colnum=0;
-    fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status);
-    if(status) FITSError("Subint::writeKeywords 10", status);
-
-    int numData = this->nbin * this->nch_file * this->npol * this->nsblk;
-    //    int dataPerByte = 8 / block.getKeyword<int>(NBITS,0);
-    //    int numBytes = numData / dataPerByte;   
-    int numBytes = numData / 8;
-    status=0;
-    if(USE_X) fits_modify_vector_len (fptr, colnum, numData, &status);
-    else      fits_modify_vector_len (fptr, colnum, numBytes, &status);
-    if(status) FITSError("Subint::writeKeywords 12", status);
-
-    // delete the existing "TDIM19 = (*,*,*,*)"
-    status=0;
-    fits_delete_key(fptr, "TDIM19", &status);
-    if(status) FITSError("Subint::writeKeywords 9", status);
-    int naxis=4;
-    long naxes[4];
-    naxes[0] = this->nbin;
-    naxes[1] = this->nch_file;
-    naxes[2] = this->npol;
-    naxes[3] = this->nsblk;
-    std::cerr << naxes[0] << " "
-	      << naxes[1] << " "
-	      << naxes[2] << " "
-	      << naxes[3] << " "
-	      << naxes[0]*naxes[1]*naxes[2]*naxes[3] << "\n";
-      
-    status=0;
-    fits_write_tdim(fptr, colnum, naxis, naxes, &status);
-    if(status) FITSError("Subint::writeKeywords 11", status);
-
-  }
- //--------------------------------------------------------------------------
-
-  void PSRFits::writeSubintRow(DataBlock &block)
-  {
-    this->startNewSubintRow(); 
-    // CHECK -- need some check of blk_cntr to make sure the number is 
-    // CHECK -- incrementing correctly?
-    float tsubint = block.getKeyword<float>(BLK_SEC);
-    double off_sub;
-    
-    off_sub = 0.5*(2.*(block.getKeyword<int>(BLK_CNTR)-this->blk_initial)+1)
-      * block.getKeyword<double>(BLK_SEC);
-
-    double lst_sub  = dmsToDec(block.getKeyword<std::string>(LST));
-    double ra_sub   = dmsToDec(block.getKeyword<std::string>(RA))*15.;
-    double dec_sub  = dmsToDec(block.getKeyword<std::string>(DEC));
-    double glon_sub = block.getKeyword<double>(GL);
-    double glat_sub = block.getKeyword<double>(GB);
-    float  fd_ang   = block.getKeyword<float>(FEED_ANGLE);
-    float  par_ang  = block.getKeyword<float>(PANGLE);
-    float  pos_ang  = remainderf(par_ang + fd_ang, 360.);
-    float  tel_az   = block.getKeyword<float>(TEL_AZ);
-    float  tel_zen  = block.getKeyword<float>(TEL_ZEN);
-    
-    this->writeColumnValue("TSUBINT",  tsubint);
-    this->writeColumnValue("OFFS_SUB", off_sub);
-    this->writeColumnValue("LST_SUB",  lst_sub);
-    this->writeColumnValue("RA_SUB",   ra_sub);
-    this->writeColumnValue("DEC_SUB",  dec_sub);
-    this->writeColumnValue("GLON_SUB", glon_sub);
-    this->writeColumnValue("GLAT_SUB", glat_sub);
-    this->writeColumnValue("FD_ANG",   fd_ang);
-    this->writeColumnValue("POS_ANG",  pos_ang);
-    this->writeColumnValue("PAR_ANG",  par_ang);
-    this->writeColumnValue("TEL_AZ",   tel_az);
-    this->writeColumnValue("TEL_ZEN",  tel_zen);
-
-    /////////////////////////////////////
-
-    long nrows = this->getNumRows();
-    int colnum = this->getColnum("DAT_FREQ");
-    int status=0;
-    fits_modify_vector_len (this->fptr, colnum, 
-			    this->subintTable.nch_file, &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   this->subintTable.nch_file, this->subintTable.dat_freq, 
-		   &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-
-    colnum = this->getColnum("DAT_WTS");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, 
-			    this->subintTable.nch_file, &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   this->subintTable.nch_file, this->subintTable.dat_wts, 
-		   &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-
-    colnum = this->getColnum("DAT_OFFS");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, 
-			    this->subintTable.nch_file, &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   this->subintTable.nch_file, this->subintTable.dat_offs, 
-		   &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-
-    colnum = this->getColnum("DAT_SCL");
-    status=0;
-    fits_modify_vector_len (this->fptr, colnum, 
-			    this->subintTable.nch_file, &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-    status=0;
-    fits_write_col(this->fptr, TFLOAT, colnum, nrows, 1, 
-		   this->subintTable.nch_file, this->subintTable.dat_scl, 
-		   &status);
-    if(status) FITSError("PSRFits::writeSubintRow", status);
-
-    //////////////////////////////////////////
-
-    unsigned char *data = (unsigned char *)block.getData();
-    int numData = this->subintTable.nbin * this->subintTable.nch_file * 
-      this->subintTable.npol * this->subintTable.nsblk;
-    int dataPerByte = 8 / block.getKeyword<int>(NBITS,0);
-    int numBytes = numData / dataPerByte;   
-    colnum = this->getColnum("DATA");
-    status=0;
-    fits_write_col_byt(this->fptr, colnum, nrows, 1, numBytes, data, &status);
-    if(status) FITSError("PSRFits::writeDataArray 4", status);
-    
-
-  }
-
 }
Index: /trunk/src/mainConvert.cc
===================================================================
--- /trunk/src/mainConvert.cc	(revision 24)
+++ /trunk/src/mainConvert.cc	(revision 25)
@@ -38,45 +38,21 @@
   block.readBlock(fin);
   
-  int block_num      = block.getKeyword<int>(BLK_CNTR);
-  int newBeam        = block.getKeyword<int>(IBEAM);
-  std::string newUT  = block.getKeyword<std::string>(UT_STRT);
-  std::string date   = block.getKeyword<std::string>(DATE);
+  int newBeam = block.getKeyword<int>(IBEAM);
+  int oldBeam;
   std::string newmove= block.getKeyword<std::string>(MOVE);
-
-  int oldBeam;
   std::string oldmove;
 
   PSRFits fitsfile;
-  fitsfile.setUT(newUT);
-  fitsfile.setDate(date);
-  fitsfile.setBeam(newBeam);
-  fitsfile.setFITSname(currentSurvey);
-  fitsfile.setInitialBlock(block_num);
-  if(fitsfile.fileExists()){
-    std::cerr << "\aThe file "<< fitsfile.getFITSname() 
-	      << " already exists.\nDon't want to overwrite, so exiting...\n";
-    return 1;
-  }
-  if(fitsfile.openFile()) {
-    std::cerr << "\aCannot open file "<< fitsfile.getFITSname()
-	      << ".\nExiting...\n";
-    return 1;
-  }
-  fitsfile.writePHU(block);
+
+  if(fitsfile.initialise(block) == FAILURE) return 1;
   fitsfile.writeHistory(argc,argv);
   
   do{
 
-    if(block.telescopeOK()) {
-      // if we aren't moving, write the subint data.
-      if(fitsfile.writeSubint(block)){
-	std::cerr << "Error. Exiting...\n";
-	return 1;
-      }
-    }
+    if(block.telescopeOK())  fitsfile.writeSubintRow(block);
     
     oldBeam = newBeam;
     oldmove = newmove;
-
+ 	
     // We've done all we can for this block. Read a new one.
     eof = !block.readBlock(fin);
@@ -87,9 +63,14 @@
       newBeam   = block.getKeyword<int>(IBEAM);
       newmove   = block.getKeyword<std::string>(MOVE);
-      newUT     = block.getKeyword<std::string>(UT);
-      block_num = block.getKeyword<int>(BLK_CNTR);
+      
       if((newBeam != oldBeam) || !block.telescopeOK()  ){
 	//if we've moved, or it's a new beam, close the file.
 	fitsfile.closeFile();
+      }
+
+      if(newmove!=oldmove){
+	// if this is true, then we were moving but now are not.
+	// this means it is a new file.
+	fitsfile.setNewFile();
       }
 
@@ -98,18 +79,7 @@
 	//  open a new file
 
-	if(newmove!=oldmove){ 
-	  // if this is true, then we were moving but now are not.
-	  // we therefore need to set the new date and time.
-	  if(dmsToDec(fitsfile.getUT()) > dmsToDec(newUT)) 
-	    date = addDay(date);
-	  fitsfile.setUT(newUT);
-	  fitsfile.setDate(date);
-	}
-	fitsfile.setBeam(newBeam);  // always set the new beam number
-	fitsfile.setInitialBlock(block_num);
-	fitsfile.setFITSname(currentSurvey);
-	fitsfile.openFile();
-	fitsfile.writePHU(block);
-	fitsfile.writeHistory(argc,argv);
+	fitsfile.initialise(block);
+
+	if(fitsfile.isNewFile()) fitsfile.writeHistory(argc,argv);
       }
 
