Index: branches/alma/external/atnf/PKSIO/PKSFITSreader.cc
===================================================================
--- branches/alma/external/atnf/PKSIO/PKSFITSreader.cc	(revision 1453)
+++ branches/alma/external/atnf/PKSIO/PKSFITSreader.cc	(revision 1757)
@@ -2,29 +2,32 @@
 //# PKSFITSreader.cc: Class to read Parkes multibeam data from a FITS file.
 //#---------------------------------------------------------------------------
-//# Copyright (C) 2000-2006
-//# Associated Universities, Inc. Washington DC, USA.
+//# livedata - processing pipeline for single-dish, multibeam spectral data.
+//# Copyright (C) 2000-2009, Australia Telescope National Facility, CSIRO
 //#
-//# 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 file is part of livedata.
 //#
-//# 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.
+//# 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.
 //#
-//# 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.
+//# 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.
 //#
-//# 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
+//# You should have received a copy of the GNU General Public License along
+//# with livedata.  If not, see <http://www.gnu.org/licenses/>.
 //#
-//# $Id$
+//# 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: PKSFITSreader.cc,v 19.23 2009-09-29 07:33:38 cal103 Exp $
 //#---------------------------------------------------------------------------
 //# Original: 2000/08/02, Mark Calabretta, ATNF
@@ -34,11 +37,11 @@
 #include <atnf/PKSIO/SDFITSreader.h>
 #include <atnf/PKSIO/PKSFITSreader.h>
-#include <atnf/PKSIO/PKSMBrecord.h>
-
+#include <atnf/PKSIO/PKSrecord.h>
+
+#include <casa/stdio.h>
 #include <casa/Arrays/Array.h>
 #include <casa/BasicMath/Math.h>
 #include <casa/Quanta/MVTime.h>
-
-#include <casa/stdio.h>
+#include <casa/Logging/LogIO.h>
 
 //----------------------------------------------- PKSFITSreader::PKSFITSreader
@@ -76,4 +79,5 @@
 Int PKSFITSreader::open(
         const String fitsName,
+        const String antenna,
         Vector<Bool> &beams,
         Vector<Bool> &IFs,
@@ -86,7 +90,10 @@
   int    extraSysCal, haveBase_, *haveXPol_, haveSpectra_, nBeam, *nChan_,
          nIF, *nPol_, status;
-  if ((status = cReader->open((char *)fitsName.chars(), nBeam, cBeams, nIF,
-                              cIFs, nChan_, nPol_, haveXPol_, haveBase_,
-                              haveSpectra_, extraSysCal))) {
+  status = cReader->open((char *)fitsName.chars(), nBeam, cBeams, nIF, cIFs,
+                         nChan_, nPol_, haveXPol_, haveBase_, haveSpectra_,
+                         extraSysCal);
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
+  if (status) {
     return status;
   }
@@ -138,23 +145,26 @@
         Vector<Double> &antPosition,
         String &obsType,
+        String &bunit,
         Float  &equinox,
         String &dopplerFrame,
         Double &mjd,
         Double &refFreq,
-        Double &bandwidth,
-        String &fluxunit)
-{
-  char   datobs[32], dopplerFrame_[32], observer_[32], obsType_[32],
-         project_[32], radecsys[32], telescope[32];
+        Double &bandwidth)
+{
+  char   bunit_[32], datobs[32], dopplerFrame_[32], observer_[32],
+         obsType_[32], project_[32], radecsys[32], telescope[32];
+  int    status;
   float  equinox_;
   double antPos[3], utc;
 
-  if (cReader->getHeader(observer_, project_, telescope, antPos, obsType_,
-                         equinox_, radecsys, dopplerFrame_, datobs, utc,
-                         refFreq, bandwidth)) {
+  status = cReader->getHeader(observer_, project_, telescope, antPos,
+                              obsType_, bunit_, equinox_, radecsys,
+                              dopplerFrame_, datobs, utc, refFreq, bandwidth);
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
+  if (status) {
     return 1;
   }
 
-  fluxunit = "";
   observer = trim(observer_);
   project  = trim(project_);
@@ -165,4 +175,5 @@
   antPosition(2) = antPos[2];
   obsType = trim(obsType_);
+  bunit   = trim(bunit_);
   equinox = equinox_;
   dopplerFrame = trim(dopplerFrame_);
@@ -188,6 +199,9 @@
   double *startfreq, *endfreq;
 
-  Int status;
-  if (!(status = cReader->getFreqInfo(nIF, startfreq, endfreq))) {
+  Int status = cReader->getFreqInfo(nIF, startfreq, endfreq);
+
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
+  if (!status) {
     startFreq.takeStorage(IPosition(1,nIF), startfreq, TAKE_OVER);
     endFreq.takeStorage(IPosition(1,nIF), endfreq, TAKE_OVER);
@@ -209,5 +223,7 @@
         const Bool getSpectra,
         const Bool getXPol,
-        const Bool getFeedPos)
+        const Bool getFeedPos,
+        const Bool getPointing,
+        const Int  coordSys)
 {
   // Apply beam selection.
@@ -277,7 +293,11 @@
   cGetXPol    = getXPol;
   cGetFeedPos = getFeedPos;
+  cGetPointing = getPointing;
+  cCoordSys   = coordSys;
 
   uInt maxNChan = cReader->select(start, end, ref, cGetSpectra, cGetXPol,
-                                  cGetFeedPos);
+                                  cGetFeedPos, cGetPointing, cCoordSys);
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
 
   delete [] end;
@@ -302,6 +322,9 @@
   double* posns;
 
-  Int status;
-  if (!(status = cReader->findRange(nRow, nSel, dateSpan, utcSpan, posns))) {
+  Int status = cReader->findRange(nRow, nSel, dateSpan, utcSpan, posns);
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
+
+  if (!status) {
     timeSpan.resize(2);
 
@@ -322,50 +345,11 @@
 // Read the next data record.
 
-Int PKSFITSreader::read(
-        Int             &scanNo,
-        Int             &cycleNo,
-        Double          &mjd,
-        Double          &interval,
-        String          &fieldName,
-        String          &srcName,
-        Vector<Double>  &srcDir,
-        Vector<Double>  &srcPM,
-        Double          &srcVel,
-        String          &obsType,
-        Int             &IFno,
-        Double          &refFreq,
-        Double          &bandwidth,
-        Double          &freqInc,
-        Vector<Double>  &restFreq,
-        Vector<Float>   &tcal,
-        String          &tcalTime,
-        Float           &azimuth,
-        Float           &elevation,
-        Float           &parAngle,
-        Float           &focusAxi,
-        Float           &focusTan,
-        Float           &focusRot,
-        Float           &temperature,
-        Float           &pressure,
-        Float           &humidity,
-        Float           &windSpeed,
-        Float           &windAz,
-        Int             &refBeam,
-        Int             &beamNo,
-        Vector<Double>  &direction,
-        Vector<Double>  &scanRate,
-        Vector<Float>   &tsys,
-        Vector<Float>   &sigma,
-        Vector<Float>   &calFctr,
-        Matrix<Float>   &baseLin,
-        Matrix<Float>   &baseSub,
-        Matrix<Float>   &spectra,
-        Matrix<uChar>   &flagged,
-        Complex         &xCalFctr,
-        Vector<Complex> &xPol)
-{
-  Int status;
-
-  if ((status = cReader->read(cMBrec))) {
+Int PKSFITSreader::read(PKSrecord &pksrec)
+{
+  Int status = cReader->read(cMBrec);
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
+
+  if (status) {
     if (status != -1) {
       status = 1;
@@ -379,101 +363,169 @@
   uInt nPol  = cMBrec.nPol[0];
 
-  scanNo  = cMBrec.scanNo;
-  cycleNo = cMBrec.cycleNo;
+  pksrec.scanNo  = cMBrec.scanNo;
+  pksrec.cycleNo = cMBrec.cycleNo;
+  pksrec.polNo = cMBrec.polNo ;
 
   // Extract MJD.
   Int day, month, year;
-  sscanf(cMBrec.datobs, "%4d-%2d-%2d", &year, &month, &day);
-  mjd = MVTime(year, month, Double(day)).day() + cMBrec.utc/86400.0;
-
-  interval  = cMBrec.exposure;
-
-  fieldName = trim(cMBrec.srcName);
-  srcName   = fieldName;
-  srcDir(0) = cMBrec.srcRA;
-  srcDir(1) = cMBrec.srcDec;
-  srcPM(0)  = 0.0;
-  srcPM(1)  = 0.0;
-  srcVel    = 0.0;
-  obsType   = trim(cMBrec.obsType);
-
-  IFno = cMBrec.IFno[0];
+  if ( strstr( cMBrec.datobs, "T" ) == NULL ) {
+    sscanf(cMBrec.datobs, "%4d-%2d-%2d", &year, &month, &day);
+    pksrec.mjd = MVTime(year, month, Double(day)).day() + cMBrec.utc/86400.0;
+  }
+  else {
+    Double dd, hour, min, sec ;
+    sscanf( cMBrec.datobs, "%4d-%2d-%2lfT%lf:%lf:%lf", &year, &month, &dd, &hour, &min, &sec ) ;
+    dd = dd + ( hour * 3600.0 + min * 60.0 + sec ) / 86400.0 ;
+    pksrec.mjd = MVTime(year, month, dd).day() ;
+  }
+
+  pksrec.interval  = cMBrec.exposure;
+
+  pksrec.fieldName = trim(cMBrec.srcName);
+  pksrec.srcName   = pksrec.fieldName;
+
+  int namelen = pksrec.srcName.length() ;
+  if ( namelen > 4 ) {
+    String srcsub = pksrec.srcName.substr( namelen-4, 4 ) ;
+    if ( srcsub.find( "_psc" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_ps_calon" ;
+    }
+    else if ( srcsub.find( "_pso" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_ps" ;
+    }
+    else if ( srcsub.find( "_prc" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_psr_calon" ;
+    }
+    else if ( srcsub.find( "_pro" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_psr" ;
+    }
+    else if ( srcsub.find( "_fsc" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_fs_calon" ;
+    }
+    else if ( srcsub.find( "_fso" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_fs" ;
+    }
+    else if ( srcsub.find( "_frc" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_fsr_calon" ;
+    }
+    else if ( srcsub.find( "_fro" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_fsr" ;
+    }
+    else if ( srcsub.find( "_nsc" ) != string::npos || srcsub.find( "_nrc" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_nod_calon" ;
+    }
+    else if ( srcsub.find( "_nso" ) != string::npos || srcsub.find( "_nro" ) != string::npos ) {
+      pksrec.fieldName = pksrec.srcName.substr( 0, namelen-4 ) ;
+      pksrec.srcName = pksrec.fieldName + "_nod" ;
+    }
+  }
+
+  pksrec.srcDir.resize(2);
+  pksrec.srcDir(0) = cMBrec.srcRA;
+  pksrec.srcDir(1) = cMBrec.srcDec;
+
+  pksrec.srcPM.resize(2);
+  pksrec.srcPM(0)  = 0.0;
+  pksrec.srcPM(1)  = 0.0;
+  pksrec.srcVel    = cMBrec.srcVelocity;
+  pksrec.obsType   = trim(cMBrec.obsType);
+
+  pksrec.IFno = cMBrec.IFno[0];
   Double chanWidth = fabs(cMBrec.fqDelt[0]);
-  refFreq   = cMBrec.fqRefVal[0];
-  bandwidth = chanWidth * nChan;
-  freqInc   = cMBrec.fqDelt[0];
-  //restFreq  = cMBrec.restFreq;
-  restFreq.resize(1);
-  restFreq(0)  = cMBrec.restFreq;
-
-  tcal.resize(nPol);
+  pksrec.refFreq   = cMBrec.fqRefVal[0];
+  pksrec.bandwidth = chanWidth * nChan;
+  pksrec.freqInc   = cMBrec.fqDelt[0];
+  pksrec.restFreq.resize(1) ;
+  pksrec.restFreq(0)  = cMBrec.restFreq;
+
+  pksrec.tcal.resize(nPol);
   for (uInt ipol = 0; ipol < nPol; ipol++) {
-    tcal(ipol) = cMBrec.tcal[0][ipol];
-  }
-  tcalTime  = trim(cMBrec.tcalTime);
-  azimuth   = cMBrec.azimuth;
-  elevation = cMBrec.elevation;
-  parAngle  = cMBrec.parAngle;
-  focusAxi  = cMBrec.focusAxi;
-  focusTan  = cMBrec.focusTan;
-  focusRot  = cMBrec.focusRot;
-
-  temperature = cMBrec.temp;
-  pressure    = cMBrec.pressure;
-  humidity    = cMBrec.humidity;
-  windSpeed   = cMBrec.windSpeed;
-  windAz      = cMBrec.windAz;
-
-  refBeam = cMBrec.refBeam;
-  beamNo  = cMBrec.beamNo;
-
-  direction(0) = cMBrec.ra;
-  direction(1) = cMBrec.dec;
-  scanRate(0)  = cMBrec.raRate;
-  scanRate(1)  = cMBrec.decRate;
-
-  tsys.resize(nPol);
-  sigma.resize(nPol);
-  calFctr.resize(nPol);
+    pksrec.tcal(ipol) = cMBrec.tcal[0][ipol];
+  }
+  pksrec.tcalTime  = trim(cMBrec.tcalTime);
+  pksrec.azimuth   = cMBrec.azimuth;
+  pksrec.elevation = cMBrec.elevation;
+  pksrec.parAngle  = cMBrec.parAngle;
+
+  pksrec.focusAxi  = cMBrec.focusAxi;
+  pksrec.focusTan  = cMBrec.focusTan;
+  pksrec.focusRot  = cMBrec.focusRot;
+
+  pksrec.temperature = cMBrec.temp;
+  pksrec.pressure    = cMBrec.pressure;
+  pksrec.humidity    = cMBrec.humidity;
+  pksrec.windSpeed   = cMBrec.windSpeed;
+  pksrec.windAz      = cMBrec.windAz;
+
+  pksrec.refBeam = cMBrec.refBeam;
+  pksrec.beamNo  = cMBrec.beamNo;
+
+  pksrec.direction.resize(2);
+  pksrec.direction(0) = cMBrec.ra;
+  pksrec.direction(1) = cMBrec.dec;
+  pksrec.pCode        = cMBrec.pCode;
+  pksrec.rateAge      = cMBrec.rateAge;
+  pksrec.scanRate.resize(2);
+  pksrec.scanRate(0)  = cMBrec.raRate;
+  pksrec.scanRate(1)  = cMBrec.decRate;
+  pksrec.paRate       = cMBrec.paRate;
+
+  pksrec.tsys.resize(nPol);
+  pksrec.sigma.resize(nPol);
+  pksrec.calFctr.resize(nPol);
   for (uInt ipol = 0; ipol < nPol; ipol++) {
-    tsys(ipol)  = cMBrec.tsys[0][ipol];
-    sigma(ipol) = tsys(ipol) / 0.81 / sqrt(interval * chanWidth);
-    calFctr(ipol) = cMBrec.calfctr[0][ipol];
+    pksrec.tsys(ipol)  = cMBrec.tsys[0][ipol];
+    pksrec.sigma(ipol) = (pksrec.tsys(ipol) / 0.81) /
+                            sqrt(pksrec.interval * chanWidth);
+    pksrec.calFctr(ipol) = cMBrec.calfctr[0][ipol];
   }
 
   if (cMBrec.haveBase) {
-    baseLin.resize(2,nPol);
-    baseSub.resize(9,nPol);
+    pksrec.baseLin.resize(2,nPol);
+    pksrec.baseSub.resize(24,nPol);
 
     for (uInt ipol = 0; ipol < nPol; ipol++) {
-      baseLin(0,ipol) = cMBrec.baseLin[0][ipol][0];
-      baseLin(1,ipol) = cMBrec.baseLin[0][ipol][1];
-
-      for (uInt j = 0; j < 9; j++) {
-        baseSub(j,ipol) = cMBrec.baseSub[0][ipol][j];
+      pksrec.baseLin(0,ipol) = cMBrec.baseLin[0][ipol][0];
+      pksrec.baseLin(1,ipol) = cMBrec.baseLin[0][ipol][1];
+
+      for (uInt j = 0; j < 24; j++) {
+        pksrec.baseSub(j,ipol) = cMBrec.baseSub[0][ipol][j];
       }
     }
 
   } else {
-    baseLin.resize(0,0);
-    baseSub.resize(0,0);
+    pksrec.baseLin.resize(0,0);
+    pksrec.baseSub.resize(0,0);
   }
 
   if (cGetSpectra && cMBrec.haveSpectra) {
-    spectra.resize(nChan,nPol);
-    spectra.takeStorage(IPosition(2,nChan,nPol), cMBrec.spectra[0], SHARE);
-
-    flagged.resize(nChan,nPol);
-    flagged.takeStorage(IPosition(2,nChan,nPol), cMBrec.flagged[0], SHARE);
+    pksrec.spectra.resize(nChan,nPol);
+    pksrec.spectra.takeStorage(IPosition(2,nChan,nPol), cMBrec.spectra[0],
+      SHARE);
+
+    pksrec.flagged.resize(nChan,nPol);
+    pksrec.flagged.takeStorage(IPosition(2,nChan,nPol), cMBrec.flagged[0],
+      SHARE);
 
   } else {
-    spectra.resize(0,0);
-    flagged.resize(0,0);
+    pksrec.spectra.resize(0,0);
+    pksrec.flagged.resize(0,0);
   }
 
   if (cGetXPol) {
-    xCalFctr = Complex(cMBrec.xcalfctr[0][0], cMBrec.xcalfctr[0][1]);
-    xPol.resize(nChan);
-    xPol.takeStorage(IPosition(1,nChan), (Complex *)cMBrec.xpol[0], SHARE);
+    pksrec.xCalFctr = Complex(cMBrec.xcalfctr[0][0],
+                             cMBrec.xcalfctr[0][1]);
+    pksrec.xPol.resize(nChan);
+    pksrec.xPol.takeStorage(IPosition(1,nChan), (Complex *)cMBrec.xpol[0],
+      SHARE);
   }
 
@@ -481,72 +533,4 @@
 }
 
-//-------------------------------------------------------- PKSFITSreader::read
-
-// Read the next data record, just the basics.
-
-Int PKSFITSreader::read(
-        Int           &IFno,
-        Vector<Float> &tsys,
-        Vector<Float> &calFctr,
-        Matrix<Float> &baseLin,
-        Matrix<Float> &baseSub,
-        Matrix<Float> &spectra,
-        Matrix<uChar> &flagged)
-{
-  Int status;
-
-  if ((status = cReader->read(cMBrec))) {
-    if (status != -1) {
-      status = 1;
-    }
-
-    return status;
-  }
-
-  IFno = cMBrec.IFno[0];
-
-  uInt nChan = cMBrec.nChan[0];
-  uInt nPol  = cMBrec.nPol[0];
-
-  tsys.resize(nPol);
-  calFctr.resize(nPol);
-  for (uInt ipol = 0; ipol < nPol; ipol++) {
-    tsys(ipol) = cMBrec.tsys[0][ipol];
-    calFctr(ipol) = cMBrec.calfctr[0][ipol];
-  }
-
-  if (cMBrec.haveBase) {
-    baseLin.resize(2,nPol);
-    baseSub.resize(9,nPol);
-
-    for (uInt ipol = 0; ipol < nPol; ipol++) {
-      baseLin(0,ipol) = cMBrec.baseLin[0][ipol][0];
-      baseLin(1,ipol) = cMBrec.baseLin[0][ipol][1];
-
-      for (uInt j = 0; j < 9; j++) {
-        baseSub(j,ipol) = cMBrec.baseSub[0][ipol][j];
-      }
-    }
-
-  } else {
-    baseLin.resize(0,0);
-    baseSub.resize(0,0);
-  }
-
-  if (cGetSpectra && cMBrec.haveSpectra) {
-    spectra.resize(nChan,nPol);
-    spectra.takeStorage(IPosition(2,nChan,nPol), cMBrec.spectra[0], SHARE);
-
-    flagged.resize(nChan,nPol);
-    flagged.takeStorage(IPosition(2,nChan,nPol), cMBrec.flagged[0], SHARE);
-
-  } else {
-    spectra.resize(0,0);
-    flagged.resize(0,0);
-  }
-
-  return 0;
-}
-
 //------------------------------------------------------- PKSFITSreader::close
 
@@ -556,4 +540,6 @@
 {
   cReader->close();
+  //logMsg(cReader->getMsg());
+  //cReader->clearMsg();
 }
 
