Index: /trunk/Makefile
===================================================================
--- /trunk/Makefile	(revision 10)
+++ /trunk/Makefile	(revision 11)
@@ -47,4 +47,7 @@
 	$(TEST)/testDataAccess.o\
 	$(TEST)/testFitsCreation.o
+
+pvoconvert : $(BASE)/mainConvert.o $(HEADER) $(BLOCK) $(PSRFITS) $(UTILS)
+	$(CXX) -o pvoconvert.x $(BASE)/mainConvert.o $(HEADER) $(BLOCK) $(PSRFITS) $(UTILS) $(LIBS)
 
 testhead : $(TEST)/headertest.o $(HEADER)
Index: /trunk/docs/conversionCodeDetails.tex
===================================================================
--- /trunk/docs/conversionCodeDetails.tex	(revision 10)
+++ /trunk/docs/conversionCodeDetails.tex	(revision 11)
@@ -256,8 +256,13 @@
   may be better to use the SDFITS standard of \texttt{BMAJ, BMIN \&
   BPA}. 
-\item \texttt{OBSFREQ} (or similar) -- the central frequency of
-  the observation. This is stored in some of the binary tables
-  (\textsc{history, subint, ...}, but it would be useful to have this
-  upfront as well.
+\item \texttt{OBSFREQ} (or similar) -- the central frequency of the
+  observation. This is stored in some of the binary tables
+  (\textsc{history, subint, ...}), but it would be useful to have this
+  upfront as well so the user doesn't have to drill down into the
+  tables. 
+
+  I guess it is not in the PSRFITS definition as the observing
+  frequency could conceivably change (and be recorded in the \subint\
+  table), but for \emph{this} case it might be useful to include. 
 \item \texttt{DATE-OBS} -- the standard FITS way of presenting the
   date of the observation.
@@ -269,5 +274,6 @@
   format, not just \texttt{STT\_CRD1} etc. This is to make it more
   accessible for users (\ie more obvious what the position is, and
-  potentially recognisable for other software).
+  potentially recognisable for other software). (See the note about
+  the \texttt{OBSFREQ} keyword.
 \item \texttt{UT} -- a new field for the UT might be needed in the
   \subint\ binary table (there isn't one at the moment). The UT
@@ -275,9 +281,11 @@
   data. It is calculatable from the LST and the telescope location,
   but having it present in the file simplifies the searching
-  possibilities. 
+  possibilities. That said, the \texttt{offs\_sub} field does provide
+  it (via an offset from the \textsc{ut} keyword in the PHU to the
+  \emph{centre} of the current subint).
 \end{itemize}
 
 Additionally, here is a list of problems with the PSRFITS definition
-that might need changed at the same time:
+that might need to be changed at the same time:
 \begin{itemize}
 \item The \texttt{TTFORMn} keyword for the \texttt{DATA} entry in the
@@ -289,4 +297,7 @@
   ``turns'' -- not sure why. This should be changed to ``deg''.
 \end{itemize}
+
+\newpage
+\section{Questions that need to be resolved.}
 
 Questions that need to be asked about the equivalence between keywords
@@ -310,9 +321,29 @@
 \item What do we put for \textsc{projid} for each survey? (\ie how do
   we want the survey names recorded?)
-\item A check needed on the definition of \textsc{off\_sub} : I
+\item Do we use ``TRACK'' or ``SCANGC''/''SCANLAT'' for the value of
+  the PHU keyword \textsc{trk\_mode}?
+\item The \subint\ column \textsc{posang} -- is this defined anywhere?
+  Should this be the \texttt{feed\_angle} parameter in the existing
+  headers? 
+\item A check needed on the definition of \textsc{offs\_sub} : I
   understand it to be the time from the start (as given by the PHU
-  keyword \textsc{ut\_strt}) of the centre of the current
+  keyword \textsc{ut}) of the centre of the current
   subintegration (\ie the centre of the block stored in the data entry
   of the current row of the \subint\ table). Is this correct?
+\item There might be a problem with the time information, at least in
+  the S70 data -- there are successive blocks with \emph{the same} LST
+  and UT time. It seems that the time information is only sampled
+  every second, so with 0.46~sec long subintegrations, you get about 2
+  per second.
+\item Do we need to add information into any other extensions? The
+  only possibility could be the \textsc{history} table, but this seems
+  to be the \emph{processing} history, so may not be applicable.
+\item How do we get the position of a given beam in the multibeam
+  case? Presumably the position stored in the header will be the
+  central position, so we need to convert using the beam number and
+  the position/feed angle.
+\item Is there a difference between the PM survey data and the PH
+  survey data? Can we tell the difference by looking at the first few
+  bytes (as we do for S70 data files)?
 \end{itemize}
 
Index: /trunk/src/Header.cc
===================================================================
--- /trunk/src/Header.cc	(revision 10)
+++ /trunk/src/Header.cc	(revision 11)
@@ -15,11 +15,11 @@
     this->ant_y    = 0.;
     this->ant_z    = 0.;
-    this->frontend = " ";
-    this->fd_poln  = " ";
+    this->frontend = "<TBD>";
+    this->fd_poln  = "<TBD>";
     this->fd_hand  = 1;
     this->fd_sang  = 0.;
     this->fd_xyph  = 0.;
-    this->backend  = " ";
-    this->beconfig = " ";
+    this->backend  = "<TBD>";
+    this->beconfig = "<TBD>";
     this->be_phase = 0;  // 0=unknown
     this->be_dcc   = 0;
Index: /trunk/src/Header.hh
===================================================================
--- /trunk/src/Header.hh	(revision 10)
+++ /trunk/src/Header.hh	(revision 11)
@@ -6,5 +6,9 @@
 namespace Header
 {
-  enum SURVEY { S70, PM, PH };
+  enum SURVEY { S70=0, PM, PH, DUD };
+  const std::string surveyNames[DUD+1] = {"70-cm survey", 
+					  "Parkes Multibeam survey",
+					  "Parkes High-latitude survey",
+					  "Not a valid survey"};
   inline SURVEY idSurvey(std::string file)
   {
@@ -19,5 +23,6 @@
     compstring_pm[3] = '\0';
     if(strcmp((const char*)compstring_s70,hdr)==0) return S70;
-    if(strncmp((const char*)compstring_pm,hdr,3)==0) return PM;
+    else if(strncmp((const char*)compstring_pm,hdr,3)==0) return PM;
+    else return DUD;
   }
 
Index: /trunk/src/PSRFits.cc
===================================================================
--- /trunk/src/PSRFits.cc	(revision 10)
+++ /trunk/src/PSRFits.cc	(revision 11)
@@ -50,5 +50,5 @@
 		 dmsToDec(block.getKeyword<std::string>(DEC)));
     name += ".fits";
-    std::cerr << "Opening file " << name;
+    std::cerr << "Opening file " << name << "\n";
     return this->openFile(name);
   }
@@ -68,5 +68,4 @@
     int status = 0, exists;
     fits_file_exists(name.c_str(),&exists,&status);
-    std::cerr << " Exists = " << exists << "\n";
     if(exists<=0){
       char *template_file = (char *)getPSRFITSdefn().c_str();
@@ -592,4 +591,8 @@
     if(this->newFile){
       status=0;
+      // delete the existing "TDIM19 = (*,*,*)"
+      fits_delete_key(this->fptr, "TDIM19", &status);
+      if(status) FITSError("PSRFits::writeDataArray", status);
+      status=0;
       fits_write_tdim(this->fptr, colnum, naxis, naxes, &status);
       if(status) FITSError("PSRFits::writeDataArray", status);
@@ -598,5 +601,5 @@
     status=0;
     fits_write_col_byt(this->fptr, colnum, nrows, 1, numBytes, data, &status);
-//     fits_write_col(this->fptr, TBYTE, colnum, nrows, 1, numBytes, data, &status);
+//fits_write_col(this->fptr, TBYTE, colnum, nrows, 1, numBytes, data, &status);
     if(status) FITSError("PSRFits::writeDataArray1", status);
 
Index: /trunk/src/mainConvert.cc
===================================================================
--- /trunk/src/mainConvert.cc	(revision 11)
+++ /trunk/src/mainConvert.cc	(revision 11)
@@ -0,0 +1,135 @@
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <iomanip>
+#include <string>
+#include "Block.hh"
+#include "Header.hh"
+#include "PSRFits.hh"
+
+using namespace Header;
+using namespace Block;
+using namespace FITS;
+using std::string;
+
+ISMOVING moveStatus(std::string file)
+{
+  std::ifstream fin;
+  fin.open(file.c_str(), std::ios::binary);
+  DataBlock block(idSurvey(file));
+  block.readBlock(fin);
+  fin.close();
+  return ISMOVING(block.getKeyword<int>(MOVE));
+}
+
+int main(int argc, char * argv[])
+{
+  std::string fname;
+  if(argc==2) fname = argv[1];
+  else{
+    std::cerr << "Usage: \"pvoconvert.x filename\"...\n";
+    return 1;
+  }
+
+  SURVEY currentSurvey = idSurvey(fname);
+  std::cerr << "File " << fname <<" : " << surveyNames[currentSurvey]<<"\n";
+  if(currentSurvey == DUD){
+    std::cerr << "Exiting...\n";
+    return 1;
+  }
+
+
+  ISMOVING status = moveStatus(fname);
+  if(status == NOTGRID){
+    std::cerr 
+      << "\aOops! This is not a gridded observation -- and so not a survey\n"
+      << "Exiting...";
+    return 1;
+  }
+  else if(status == SCANNING){
+    std::cerr 
+      << "\aOops! This is a scanning observation -- how did that happen?\n"
+      << "Exiting...";
+    return 1;
+  }
+  else if((status != MOVING)&&(status != POINT)){
+    std::cerr <<"\a\a\aOops! status is an unknown value (" 
+	      << status << ")\nExiting...\n";
+    return 1;
+  }
+  
+
+  std::ifstream fin;
+  fin.open(fname.c_str(), std::ios::binary);
+
+  bool eof;
+  DataBlock block(currentSurvey);
+   block.readBlock(fin);
+  
+
+  int filecount = 0;
+  PSRFits fitsfile;
+  if(fitsfile.openFile(block)) {
+    std::cerr << "\aCannot open file.\nExiting...\n";
+    return 1;
+  }
+  fitsfile.writePHU(block);
+  
+  int blockcount = 0;
+  std::string newRA  = block.getKeyword<std::string>(RA);
+  std::string newDec = block.getKeyword<std::string>(DEC);
+  int newBeam = block.getKeyword<int>(IBEAM);
+  int move=block.getKeyword<int>(MOVE);
+  int oldBeam;
+  std::string oldRA,oldDec;
+  do{
+
+    if(move != MOVING) {
+      // if we aren't moving, write the subint data.
+      if(fitsfile.writeSubint(block)){
+	std::cerr << "Error. Exiting...\n";
+	return 1;
+      }
+    }
+    
+    oldRA   = newRA;
+    oldDec  = newDec;
+    oldBeam = newBeam;
+
+    // We've done all we can for this block. Read a new one.
+    eof = !block.readBlock(fin);
+
+    if(!eof){ // if the file is still open
+      
+      // get info on the new block
+      newBeam = block.getKeyword<int>(IBEAM);
+      newRA   = block.getKeyword<std::string>(RA);
+      newDec  = block.getKeyword<std::string>(DEC);
+      move    = block.getKeyword<int>(MOVE);
+
+      if((newRA   != oldRA)   || (newDec != oldDec) || 
+	 (newBeam != oldBeam) || (move   == MOVING)   ){
+	//if we've moved, or it's a new beam, close the file.
+	fitsfile.closeFile();
+      }
+
+      if((fitsfile.getFilestate()==CLOSED) && (move == POINT)){
+	// if there is no file open, but we are not moving, 
+	//  open a new file
+	fitsfile.openFile(block);
+	fitsfile.writePHU(block);
+	newRA   = block.getKeyword<std::string>(RA);
+	newDec  = block.getKeyword<std::string>(DEC);
+	newBeam = block.getKeyword<int>(IBEAM);
+	move    = block.getKeyword<int>(MOVE);
+      }
+
+    }
+	
+	
+  }while(!eof);
+  
+  fitsfile.closeFile();
+  fin.close();
+
+}
