Index: branches/alma/external/atnf/PKSIO/PKSSDwriter.cc
===================================================================
--- branches/alma/external/atnf/PKSIO/PKSSDwriter.cc	(revision 1453)
+++ branches/alma/external/atnf/PKSIO/PKSSDwriter.cc	(revision 1757)
@@ -2,36 +2,47 @@
 //# PKSSDwriter.cc: Class to write Parkes multibeam data to an SDFITS file.
 //#---------------------------------------------------------------------------
-//# Copyright (C) 2000-2006
-//# Associated Universities, Inc. Washington DC, USA.
-//#
-//# This library is free software; you can redistribute it and/or modify it
-//# under the terms of the GNU Library General Public License as published by
-//# the Free Software Foundation; either version 2 of the License, or (at your
-//# option) any later version.
-//#
-//# This library is distributed in the hope that it will be useful, but
-//# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-//# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
-//# License for more details.
-//#
-//# You should have received a copy of the GNU Library General Public License
-//# along with this library; if not, write to the Free Software Foundation,
-//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
-//#
-//# Correspondence concerning AIPS++ should be addressed as follows:
-//#        Internet email: aips2-request@nrao.edu.
-//#        Postal address: AIPS++ Project Office
-//#                        National Radio Astronomy Observatory
-//#                        520 Edgemont Road
-//#                        Charlottesville, VA 22903-2475 USA
-//#
-//# $Id$
+//# livedata - processing pipeline for single-dish, multibeam spectral data.
+//# Copyright (C) 2000-2009, Australia Telescope National Facility, CSIRO
+//#
+//# This file is part of livedata.
+//#
+//# livedata is free software: you can redistribute it and/or modify it under
+//# the terms of the GNU General Public License as published by the Free
+//# Software Foundation, either version 3 of the License, or (at your option)
+//# any later version.
+//#
+//# livedata is distributed in the hope that it will be useful, but WITHOUT
+//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+//# more details.
+//#
+//# You should have received a copy of the GNU General Public License along
+//# with livedata.  If not, see <http://www.gnu.org/licenses/>.
+//#
+//# Correspondence concerning livedata may be directed to:
+//#        Internet email: mcalabre@atnf.csiro.au
+//#        Postal address: Dr. Mark Calabretta
+//#                        Australia Telescope National Facility, CSIRO
+//#                        PO Box 76
+//#                        Epping NSW 1710
+//#                        AUSTRALIA
+//#
+//# http://www.atnf.csiro.au/computing/software/livedata.html
+//# $Id: PKSSDwriter.cc,v 19.17 2009-09-29 07:33:38 cal103 Exp $
 //#---------------------------------------------------------------------------
 
-#include <atnf/PKSIO/PKSMBrecord.h>
+#include <atnf/PKSIO/MBrecord.h>
 #include <atnf/PKSIO/PKSSDwriter.h>
 
+#include <casa/Logging/LogIO.h>
+
+#include <casa/stdio.h>
 #include <casa/Quanta/MVTime.h>
 
+#include <string>
+#include <cstring>
+
+// Class name
+const string className = "PKSSDwriter" ;
 
 //--------------------------------------------------- PKSSDwriter::PKSSDwriter
@@ -63,4 +74,5 @@
         const Vector<Double> antPosition,
         const String obsMode,
+        const String bunit,
         const Float  equinox,
         const String dopplerFrame,
@@ -68,7 +80,9 @@
         const Vector<uInt> nPol,
         const Vector<Bool> haveXPol,
-        const Bool   haveBase,
-        const String fluxUnit)
-{
+        const Bool   haveBase)
+{
+  const string methodName = "create()" ;
+  LogIO os( LogOrigin( className, methodName, WHERE ) ) ;
+
   double antPos[3];
   antPos[0] = antPosition(0);
@@ -78,7 +92,5 @@
   cNIF = nChan.nelements();
   if (nPol.nelements() != cNIF || haveXPol.nelements() != cNIF) {
-    cerr << "PKSSDwriter::create: "
-         << "Inconsistent number of IFs for nChan, nPol, and/or haveXPol."
-         << endl;
+    os << LogIO::SEVERE << "Inconsistent number of IFs for nChan, nPol, and/or haveXPol." << LogIO::POST ;
     return 1;
   }
@@ -102,11 +114,12 @@
   Int status = cSDwriter.create((char *)sdName.chars(),
         (char *)observer.chars(), (char *)project.chars(),
-        (char *)antName.chars(), antPos, (char *)obsMode.chars(), equinox,
-        (char *)dopplerFrame.chars(), cNIF,
+        (char *)antName.chars(), antPos, (char *)obsMode.chars(),
+        (char *)bunit.chars(), equinox, (char *)dopplerFrame.chars(), cNIF,
         (int *)cNChan.getStorage(deleteIt),
         (int *)cNPol.getStorage(deleteIt),
         (int *)cHaveXPol.getStorage(deleteIt), (int)cHaveBase, 1);
+  //logMsg(cSDwriter.getMsg());
+  //cSDwriter.clearMsg();
   if (status) {
-    cSDwriter.reportError();
     cSDwriter.deleteFile();
     close();
@@ -121,106 +134,69 @@
 
 Int PKSSDwriter::write(
-        const Int             scanNo,
-        const Int             cycleNo,
-        const Double          mjd,
-        const Double          interval,
-        const String          fieldName,
-        const String          srcName,
-        const Vector<Double>  srcDir,
-        const Vector<Double>  srcPM,
-        const Double          srcVel,
-        const String          obsMode,
-        const Int             IFno,
-        const Double          refFreq,
-        const Double          bandwidth,
-        const Double          freqInc,
-        //const Double          restFreq,
-        const Vector<Double>  restFreq,
-        const Vector<Float>   tcal,
-        const String          tcalTime,
-        const Float           azimuth,
-        const Float           elevation,
-        const Float           parAngle,
-        const Float           focusAxi,
-        const Float           focusTan,
-        const Float           focusRot,
-        const Float           temperature,
-        const Float           pressure,
-        const Float           humidity,
-        const Float           windSpeed,
-        const Float           windAz,
-        const Int             refBeam,
-        const Int             beamNo,
-        const Vector<Double>  direction,
-        const Vector<Double>  scanRate,
-        const Vector<Float>   tsys,
-        const Vector<Float>   sigma,
-        const Vector<Float>   calFctr,
-        const Matrix<Float>   baseLin,
-        const Matrix<Float>   baseSub,
-        const Matrix<Float>   &spectra,
-        const Matrix<uChar>   &flagged,
-        const Complex         xCalFctr,
-        const Vector<Complex> &xPol)
-{
+        const PKSrecord &pksrec)
+{
+  const string methodName = "write()" ;
+  LogIO os( LogOrigin( className, methodName, WHERE ) ) ;
+
   // Do basic checks.
+  Int IFno = pksrec.IFno;
   uInt iIF = IFno - 1;
   if (IFno < 1 || Int(cNIF) < IFno) {
-    cerr << "PKSDwriter::write: "
-         << "Invalid IF number " << IFno
-         << " (maximum " << cNIF << ")." << endl;
+    os << LogIO::SEVERE 
+       << "Invalid IF number " << IFno
+       << " (maximum " << cNIF << ")." << LogIO::POST ;
     return 1;
   }
 
-  uInt nChan = spectra.nrow();
+  uInt nChan = pksrec.spectra.nrow();
   if (nChan != cNChan(iIF)) {
-    cerr << "PKSDwriter::write: "
-         << "Wrong number of channels for IF " << IFno << "," << endl
-         << "                   "
+    os << LogIO::SEVERE << "Wrong number of channels for IF " << IFno << "," << endl
          << "got " << nChan << " should be " << cNChan(iIF) << "." << endl;
+    os << LogIO::POST ;
     return 1;
   }
 
-  uInt nPol = spectra.ncolumn();
+  uInt nPol = pksrec.spectra.ncolumn();
   if (nPol != cNPol(iIF)) {
-    cerr << "PKSDwriter::write: "
-         << "Wrong number of polarizations for IF " << IFno << "," << endl
-         << "                   "
-         << "got " << nPol << " should be " << cNPol(iIF) << "." << endl;
+    os << LogIO::SEVERE << "Wrong number of polarizations for IF " << IFno << "," << endl
+       << "got " << nPol << " should be " << cNPol(iIF) << "." << endl;
+    os << LogIO::POST ;
     return 1;
   }
 
-  // Extract calendar information from mjd.
-  MVTime time(mjd);
+  // Extract calendar information frrom mjd.
+  MVTime time(pksrec.mjd);
   Int year  = time.year();
   Int month = time.month();
   Int day   = time.monthday();
 
-  // Transfer data to a single-IF PKSMBrecord.
-  PKSMBrecord mbrec(1);
+  // Transfer data to a single-IF MBrecord.
+  MBrecord mbrec(1);
 
   // Start with basic beam- and IF-independent bookkeeping information.
-  mbrec.scanNo  = scanNo;
-  mbrec.cycleNo = cycleNo;
+  mbrec.scanNo  = pksrec.scanNo;
+  mbrec.cycleNo = pksrec.cycleNo;
 
   sprintf(mbrec.datobs, "%4.4d-%2.2d-%2.2d", year, month, day);
-  mbrec.utc      = fmod(mjd, 1.0) * 86400.0;
-  mbrec.exposure = float(interval);
-
-  strncpy(mbrec.srcName, (char *)srcName.chars(), 17);
-  mbrec.srcRA    = srcDir(0);
-  mbrec.srcDec   = srcDir(1);
-
-  //mbrec.restFreq = restFreq;
-  mbrec.restFreq = restFreq(0);
-
-  strncpy(mbrec.obsType, (char *)obsMode.chars(), 16);
+  mbrec.utc      = fmod(pksrec.mjd, 1.0) * 86400.0;
+  mbrec.exposure = float(pksrec.interval);
+
+  strncpy(mbrec.srcName, (char *)pksrec.srcName.chars(), 17);
+  mbrec.srcRA    = pksrec.srcDir(0);
+  mbrec.srcDec   = pksrec.srcDir(1);
+  if (pksrec.restFreq.shape()==0) { 
+     mbrec.restFreq = 0;
+  }
+  else {
+     mbrec.restFreq = pksrec.restFreq(0);
+  }
+  strncpy(mbrec.obsType, (char *)pksrec.obsType.chars(), 16);
 
   // Now beam-dependent parameters.
-  mbrec.beamNo   = beamNo;
-  mbrec.ra       = direction(0);
-  mbrec.dec      = direction(1);
-  mbrec.raRate   = scanRate(0);
-  mbrec.decRate  = scanRate(1);
+  mbrec.beamNo   = pksrec.beamNo;
+  mbrec.ra       = pksrec.direction(0);
+  mbrec.dec      = pksrec.direction(1);
+  mbrec.raRate   = pksrec.scanRate(0);
+  mbrec.decRate  = pksrec.scanRate(1);
 
   // Now IF-dependent parameters.
@@ -231,19 +207,19 @@
 
   mbrec.fqRefPix[0] = (nChan/2) + 1;
-  mbrec.fqRefVal[0] = refFreq;
-  mbrec.fqDelt[0]   = freqInc;
+  mbrec.fqRefVal[0] = pksrec.refFreq;
+  mbrec.fqDelt[0]   = pksrec.freqInc;
 
   // Now the data itself.
-  for (uInt i = 0; i < tsys.nelements(); i++) {
-    mbrec.tsys[0][i] = tsys(i);
+  for (uInt i = 0; i < pksrec.tsys.nelements(); i++) {
+    mbrec.tsys[0][i] = pksrec.tsys(i);
   }
 
   for (uInt ipol = 0; ipol < nPol; ipol++) {
-    mbrec.calfctr[0][ipol] = calFctr(ipol);
+    mbrec.calfctr[0][ipol] = pksrec.calFctr(ipol);
   }
 
   if (cHaveXPol(iIF)) {
-    mbrec.xcalfctr[0][0] = xCalFctr.real();
-    mbrec.xcalfctr[0][1] = xCalFctr.imag();
+    mbrec.xcalfctr[0][0] = pksrec.xCalFctr.real();
+    mbrec.xcalfctr[0][1] = pksrec.xCalFctr.imag();
   } else {
     mbrec.xcalfctr[0][0] = 0.0f;
@@ -255,11 +231,11 @@
 
     for (uInt ipol = 0; ipol < nPol; ipol++) {
-      mbrec.baseLin[0][ipol][0] = baseLin(0,ipol);
-      mbrec.baseLin[0][ipol][1] = baseLin(1,ipol);
-
-      for (uInt j = 0; j < baseSub.nrow(); j++) {
-        mbrec.baseSub[0][ipol][j] = baseSub(j,ipol);
+      mbrec.baseLin[0][ipol][0] = pksrec.baseLin(0,ipol);
+      mbrec.baseLin[0][ipol][1] = pksrec.baseLin(1,ipol);
+
+      for (uInt j = 0; j < pksrec.baseSub.nrow(); j++) {
+        mbrec.baseSub[0][ipol][j] = pksrec.baseSub(j,ipol);
       }
-      for (uInt j = baseSub.nrow(); j < 9; j++) {
+      for (uInt j = pksrec.baseSub.nrow(); j < 24; j++) {
         mbrec.baseSub[0][ipol][j] = 0.0f;
       }
@@ -271,9 +247,9 @@
 
   Bool delSpectra = False;
-  const Float *specstor = spectra.getStorage(delSpectra);
+  const Float *specstor = pksrec.spectra.getStorage(delSpectra);
   mbrec.spectra[0] = (float *)specstor;
 
   Bool delFlagged = False;
-  const uChar *flagstor = flagged.getStorage(delFlagged);
+  const uChar *flagstor = pksrec.flagged.getStorage(delFlagged);
   mbrec.flagged[0] = (unsigned char *)flagstor;
 
@@ -281,5 +257,5 @@
   const Complex *xpolstor;
   if (cHaveXPol(iIF)) {
-    xpolstor = xPol.getStorage(delXPol);
+    xpolstor = pksrec.xPol.getStorage(delXPol);
   } else {
     xpolstor = 0;
@@ -289,34 +265,49 @@
   // Finish off with system calibration parameters.
   mbrec.extraSysCal = 1;
-  mbrec.refBeam     = refBeam;
-  for (uInt i = 0; i < tcal.nelements(); i++) {
-    mbrec.tcal[0][i] = tcal(i);
-  }
-  strncpy(mbrec.tcalTime, (char *)tcalTime.chars(), 16);
-  mbrec.azimuth   = azimuth;
-  mbrec.elevation = elevation;
-  mbrec.parAngle  = parAngle;
-  mbrec.focusAxi  = focusAxi;
-  mbrec.focusTan  = focusTan;
-  mbrec.focusRot  = focusRot;
-  mbrec.temp      = temperature;
-  mbrec.pressure  = pressure;
-  mbrec.humidity  = humidity;
-  mbrec.windSpeed = windSpeed;
-  mbrec.windAz    = windAz;
+  mbrec.refBeam     = pksrec.refBeam;
+  for (uInt i = 0; i < pksrec.tcal.nelements(); i++) {
+    mbrec.tcal[0][i] = pksrec.tcal(i);
+  }
+  strncpy(mbrec.tcalTime, (char *)pksrec.tcalTime.chars(), 16);
+  mbrec.azimuth   = pksrec.azimuth;
+  mbrec.elevation = pksrec.elevation;
+  mbrec.parAngle  = pksrec.parAngle;
+  mbrec.focusAxi  = pksrec.focusAxi;
+  mbrec.focusTan  = pksrec.focusTan;
+  mbrec.focusRot  = pksrec.focusRot;
+  mbrec.temp      = pksrec.temperature;
+  mbrec.pressure  = pksrec.pressure;
+  mbrec.humidity  = pksrec.humidity;
+  mbrec.windSpeed = pksrec.windSpeed;
+  mbrec.windAz    = pksrec.windAz;
 
   Int status = cSDwriter.write(mbrec);
+  //logMsg(cSDwriter.getMsg());
+  //cSDwriter.clearMsg();
   if (status) {
-    cSDwriter.reportError();
     status = 1;
   }
 
-  spectra.freeStorage(specstor, delSpectra);
-  flagged.freeStorage(flagstor, delFlagged);
-  xPol.freeStorage(xpolstor, delXPol);
+  pksrec.spectra.freeStorage(specstor, delSpectra);
+  pksrec.flagged.freeStorage(flagstor, delFlagged);
+  pksrec.xPol.freeStorage(xpolstor, delXPol);
 
   return status;
 }
 
+//------------------------------------------------------- PKSSDwriter::history
+
+// Write a history record.
+
+Int PKSSDwriter::history(const String text)
+{
+  return cSDwriter.history((char *)text.chars());
+}
+
+Int PKSSDwriter::history(const char *text)
+{
+  return cSDwriter.history((char *)text);
+}
+
 //--------------------------------------------------------- PKSSDwriter::close
 
@@ -326,3 +317,5 @@
 {
   cSDwriter.close();
-}
+  //logMsg(cSDwriter.getMsg());
+  //cSDwriter.clearMsg();
+}
