Index: trunk/external-alma/asdm2ASAP/ASDMReader.cc
===================================================================
--- trunk/external-alma/asdm2ASAP/ASDMReader.cc	(revision 2252)
+++ trunk/external-alma/asdm2ASAP/ASDMReader.cc	(revision 2254)
@@ -1441,4 +1441,8 @@
   unsigned int index = dataIdList_[idx] ;
   vector<PointingRow *> *prows = asdm_->getPointing().getByContext( atag ) ;
+
+  if ( prows == 0 )
+    return ;
+
   PointingRow *prow ;
   PointingRow *qrow ;
@@ -1459,7 +1463,4 @@
 //   }
   
-  if ( nrow == 0 )
-    return ;
-
   srate.resize(2) ;
   srate[0] = 0.0 ;
@@ -1467,5 +1468,6 @@
   az = 0.0 ;
   el = 0.0 ;
-  double tcen = 0.0 ;
+  //double tcen = 0.0 ;
+  double tcen = getMidTime( tint ).getMJD() ;
 
   // 
@@ -1483,50 +1485,38 @@
   ArrayTimeInterval pTime0 = (*prows)[0]->getTimeInterval() ;
   ArrayTimeInterval pTime1 = (*prows)[nrow-1]->getTimeInterval() ;
-  if ( tint.getStartInMJD()+tint.getDurationInDays() < pTime0.getStartInMJD() ) {
+  //if ( tint.getStartInMJD()+tint.getDurationInDays() < pTime0.getStartInMJD() ) {
+  if ( getEndTime( tint ) < getStartTime( pTime0 ) ) {
     logsink_->postLocally( LogMessage( "ArrayTimeInterval out of bounds: no data for given position (tint < ptime)", LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
     prow = (*prows)[0] ;
-    //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
-    vector< vector<Angle> > dirA = prow->getTarget() ;
-    vector< vector<Angle> > offA = prow->getOffset() ;
-    //az = dirA[0][0].get() ;
-    //el = dirA[0][1].get() ;
-    az = dirA[0][0].get() + offA[0][0].get() ;
-    el = dirA[0][1].get() + offA[0][1].get() ;
+    vector< vector<double> > dirA = pointingDir( prow ) ;
+    az = dirA[0][0] ;
+    el = dirA[0][1] ;
     if ( prow->getUsePolynomials() && prow->getNumTerm() > 1 ) {
-      //srate[0] = dirA[1][0].get() ;
-      //srate[1] = dirA[1][1].get() ;
-      srate[0] = dirA[1][0].get() + offA[1][0].get() ;
-      srate[1] = dirA[1][1].get() + offA[1][1].get() ;
+      srate[0] = dirA[1][0] ;
+      srate[1] = dirA[1][1] ;
     }      
   }
-  else if ( tint.getStartInMJD() > pTime1.getStartInMJD()+pTime1.getDurationInDays() ) {
+  //else if ( tint.getStartInMJD() > pTime1.getStartInMJD()+pTime1.getDurationInDays() ) {
+  else if ( getStartTime( tint ) > getEndTime( pTime1 ) ) {
     logsink_->postLocally( LogMessage( "ArrayTimeInterval out of bounds: no data for given position (tint > ptime)", LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
     prow = (*prows)[nrow-1] ;
     int numSample = prow->getNumSample() ;
-    //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
-    vector< vector<Angle> > dirA = prow->getTarget() ;
-    vector< vector<Angle> > offA = prow->getOffset() ;
+    vector< vector<double> > dirA = pointingDir( prow ) ;
     if ( prow->getUsePolynomials() ) {
-      //az = dirA[0][0].get() ;
-      //el = dirA[0][1].get() ; 
-      az = dirA[0][0].get() + offA[0][0].get() ;
-      el = dirA[0][1].get() + offA[0][1].get() ; 
+      az = dirA[0][0] ;
+      el = dirA[0][1] ; 
       if ( prow->getNumTerm() > 1 ) {
-        //srate[0] = dirA[1][0].get() ;
-        //srate[1] = dirA[1][1].get() ;
-        srate[0] = dirA[1][0].get() + offA[1][0].get() ;
-        srate[1] = dirA[1][1].get() + offA[1][0].get() ;
-      }
-    }
-    else {
-      //az = dirA[numSample-1][0].get() ;
-      //el = dirA[numSample-1][1].get() ;
-      az = dirA[numSample-1][0].get() + offA[numSample-1][0].get() ;
-      el = dirA[numSample-1][1].get() + offA[numSample-1][1].get() ;
+        srate[0] = dirA[1][0] ;
+        srate[1] = dirA[1][1] ;
+      }
+    }
+    else {
+      az = dirA[numSample-1][0] ;
+      el = dirA[numSample-1][1] ;
     }
   }
   else {
     ArrayTime startTime = tint.getStart() ;
-    ArrayTime endTime( tint.getStartInMJD()+tint.getDurationInDays() ) ;
+    ArrayTime endTime = getEndTime( tint ) ;
     int row0 = -1 ;
     int row1 = -1 ;
@@ -1553,32 +1543,19 @@
       double t0 = getEndTime( prow->getTimeInterval() ).getMJD() ;
       double t1 = qrow->getTimeInterval().getStartInMJD() ;
-      double t = startTime.getMJD() ;
-      //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
-      //vector< vector<Angle> > dirB = qrow->getPointingDirection() ;
-      vector< vector<Angle> > dirA = prow->getTarget() ;
-      vector< vector<Angle> > offA = prow->getOffset() ;
-      vector< vector<Angle> > dirB = qrow->getTarget() ;
-      vector< vector<Angle> > offB = qrow->getOffset() ;
-      //double da0 = dirA[0][0].get() ;
-      //double db0 = dirB[0][0].get() ;
-      //double da1 = dirA[0][1].get() ;
-      //double db1 = dirB[0][1].get() ;
-      double da0 = dirA[0][0].get() + offA[0][0].get() ;
-      double db0 = dirB[0][0].get() + offB[0][0].get() ;
-      double da1 = dirA[0][1].get() + offA[0][1].get() ;
-      double db1 = dirB[0][1].get() + offB[0][1].get() ;
+      vector< vector<double> > dirA = pointingDir( prow ) ;
+      vector< vector<double> > dirB = pointingDir( qrow ) ;
+      double da0 = dirA[0][0] ;
+      double db0 = dirB[0][0] ;
+      double da1 = dirA[0][1] ;
+      double db1 = dirB[0][1] ;
       if ( prow->getUsePolynomials() && qrow->getUsePolynomials() ) {
-        double dt = ( t - t0 ) / ( t1 - t0 ) ;
+        double dt = ( tcen - t0 ) / ( t1 - t0 ) ;
         az = da0 + ( db0 - da0 ) * dt ;
         el = da1 + ( db1 - da1 ) * dt ;
         if ( prow->getNumTerm() > 0 && qrow->getNumTerm() > 1 ) {
-          //double ra0 = dirA[1][0].get() ;
-          //double rb0 = dirB[1][0].get() ;
-          //double ra1 = dirA[1][1].get() ;
-          //double rb1 = dirB[1][1].get() ;
-          double ra0 = dirA[1][0].get() + offA[1][0].get() ;
-          double rb0 = dirB[1][0].get() + offB[1][0].get() ;
-          double ra1 = dirA[1][1].get() + offA[1][1].get() ;
-          double rb1 = dirB[1][1].get() + offB[1][1].get() ;          
+          double ra0 = dirA[1][0] ;
+          double rb0 = dirB[1][0] ;
+          double ra1 = dirA[1][1] ;
+          double rb1 = dirB[1][1] ;
           srate[0] = ra0 + ( rb0 - ra0 ) * dt ;
           srate[1] = ra1 + ( rb1 - ra1 ) * dt ;
@@ -1586,5 +1563,5 @@
       }
       else if ( !(qrow->getUsePolynomials()) ) {
-        double dt = ( t - t0 ) / ( t1 - t0 ) ;
+        double dt = ( tcen - t0 ) / ( t1 - t0 ) ;
         az = da0 + ( db0 - da0 ) * dt ;
         el = da1 + ( db1 - da1 ) * dt ;
@@ -1608,16 +1585,10 @@
         if ( row0 == row1 ) {
           prow = (*prows)[row0] ;
-          //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
-          vector< vector<Angle> > dirA = prow->getTarget() ;
-          vector< vector<Angle> > offA = prow->getOffset() ;
-          //az = dirA[0][0].get() ;
-          //el = dirA[0][1].get() ; 
-          az = dirA[0][0].get() + offA[0][0].get() ;
-          el = dirA[0][1].get() + offA[0][1].get() ; 
+          vector< vector<double> > dirA = pointingDir( prow ) ;
+          az = dirA[0][0] ;
+          el = dirA[0][1] ; 
           if ( prow->getNumTerm() > 1 ) {
-            //srate[0] = dirA[1][0].get() ;
-            //srate[1] = dirA[1][1].get() ;
-            srate[0] = dirA[1][0].get() + offA[1][0].get() ;
-            srate[1] = dirA[1][1].get() + offA[1][0].get() ;
+            srate[0] = dirA[1][0] ;
+            srate[1] = dirA[1][1] ;
           }
         }
@@ -1625,33 +1596,20 @@
           prow = (*prows)[row0] ;
           qrow = (*prows)[row1] ;
-          //vector< vector<Angle> > dirA = qrow->getPointingDirection() ;
-          //vector< vector<Angle> > dirB = qrow->getPointingDirection() ;
-          vector< vector<Angle> > dirA = qrow->getTarget() ;
-          vector< vector<Angle> > offA = prow->getOffset() ;
-          vector< vector<Angle> > dirB = qrow->getTarget() ;
-          vector< vector<Angle> > offB = qrow->getOffset() ;
+          vector< vector<double> > dirA = pointingDir( prow ) ;
+          vector< vector<double> > dirB = pointingDir( qrow ) ;
           double t0 = getMidTime( prow->getTimeInterval() ).getMJD() ;
           double t1 = getMidTime( qrow->getTimeInterval() ).getMJD() ;
-          double t = startTime.getMJD() ;
-          double dt = ( t - t0 ) / ( t1 - t0 ) ;
-          //double da0 = dirA[0][0].get() ;
-          //double db0 = dirB[0][0].get() ;
-          //double da1 = dirA[0][1].get() ;
-          //double db1 = dirB[0][1].get() ;
-          double da0 = dirA[0][0].get() + offA[0][0].get() ;
-          double db0 = dirB[0][0].get() + offB[0][0].get() ;
-          double da1 = dirA[0][1].get() + offA[0][1].get() ;
-          double db1 = dirB[0][1].get() + offB[0][1].get() ;
+          double dt = ( tcen - t0 ) / ( t1 - t0 ) ;
+          double da0 = dirA[0][0] ;
+          double db0 = dirB[0][0] ;
+          double da1 = dirA[0][1] ;
+          double db1 = dirB[0][1] ;
           az = da0 + ( db0 - da0 ) * dt ;
           el = da1 + ( db1 - db0 ) * dt ;
           if ( prow->getNumTerm() > 0 && qrow->getNumTerm() > 1 ) {
-            //double ra0 = dirA[1][0].get() ;
-            //double rb0 = dirB[1][0].get() ;
-            //double ra1 = dirA[1][1].get() ;
-            //double rb1 = dirB[1][1].get() ;
-            double ra0 = dirA[1][0].get() + offA[1][0].get() ;
-            double rb0 = dirB[1][0].get() + offB[1][0].get() ;
-            double ra1 = dirA[1][1].get() + offA[1][1].get() ;
-            double rb1 = dirB[1][1].get() + offB[1][1].get() ;          
+            double ra0 = dirA[1][0] ;
+            double rb0 = dirB[1][0] ;
+            double ra1 = dirA[1][1] ;
+            double rb1 = dirB[1][1] ;
             srate[0] = ra0 + ( rb0 - ra0 ) * dt ;
             srate[1] = ra1 + ( rb1 - ra1 ) * dt ;
@@ -1661,11 +1619,10 @@
       else if ( prow->getUsePolynomials() == qrow->getUsePolynomials() ) {
         //logsink_->postLocally( LogMessage("numSample == numTerm",LogOrigin(className_,funcName,WHERE)) ) ;
+        tcen = 0.0 ;
         for ( int irow = row0 ; irow <= row1 ; irow++ ) {
           prow = (*prows)[irow] ;
           int numSample = prow->getNumSample() ;
           //logsink_->postLocally( LogMessage("numSample = "+String::toString(numSample),LogOrigin(className_,funcName,WHERE)) ) ;
-          //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
-          vector< vector<Angle> > dirA = prow->getTarget() ;
-          vector< vector<Angle> > offA = prow->getOffset() ;
+          vector< vector<double> > dirA = pointingDir( prow ) ;
           if ( prow->isSampledTimeIntervalExists() ) {
             //logsink_->postLocally( LogMessage("sampledTimeIntervalExists",LogOrigin(className_,funcName,WHERE)) ) ;
@@ -1674,8 +1631,7 @@
               //if ( tint.overlaps( stime[isam] ) ) {
               if ( tint.contains( stime[isam] ) ) {
-                //az += dirA[isam][0].get() ;
-                //el += dirA[isam][1].get() ;
-                az += dirA[isam][0].get() + offA[isam][0].get() ;
-                el += dirA[isam][1].get() + offA[isam][1].get() ;
+                az += dirA[isam][0] ;
+                el += dirA[isam][1] ;
+                tcen += getMidTime( stime[isam] ).getMJD() ;
                 ndir++ ;
               }
@@ -1692,8 +1648,6 @@
               //if ( tint.overlaps( stime ) ) {
               if ( tint.contains( stime ) ) {
-                //az += dirA[isam][0].get() ;
-                //el += dirA[isam][1].get() ;
-                az += dirA[isam][0].get() + offA[isam][0].get() ;
-                el += dirA[isam][1].get() + offA[isam][1].get() ;
+                az += dirA[isam][0] ;
+                el += dirA[isam][1] ;
                 tcen += getMidTime( stime ).getMJD() ;
                 ndir++ ;
@@ -1923,2 +1877,21 @@
   return angle ;
 }
+
+vector< vector<double> > ASDMReader::pointingDir( PointingRow *row ) 
+{
+  //vector< vector<Angle> > aDir = row->getPointingDirection() ;
+  vector< vector<Angle> > aDir = row->getTarget() ;
+  vector< vector<Angle> > aOff = row->getOffset() ;
+  unsigned int n = aDir.size() ;
+  unsigned int m = 0 ;
+  vector< vector<double> > dir( n ) ;
+  for ( unsigned int i = 0 ; i < n ; i++ ) {
+    m = aDir[i].size() ;
+    dir[i].resize( m ) ;
+    for ( unsigned int j = 0 ; j < m ; j++ ) {
+      //dir[i][j] = aDir[i][j].get() ;
+      dir[i][j] = aDir[i][j].get() + aOff[i][j].get() ;
+    }
+  }
+  return dir ;
+}
