From fd93e2fb56b7061f12d43c3ef3ade20469b50561 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20J=C3=BCrges?= <jurges@astron.nl>
Date: Mon, 21 Jan 2019 09:31:10 +0000
Subject: [PATCH] SW-546:  Change the offset type to int64_t

Offsets can also be negative.  A uint64_t obviously cannot accomodate negative
values.
---
 RTCP/Cobalt/OutputProc/src/TBB_Dipole.cc | 39 +++++++++++++-----------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/RTCP/Cobalt/OutputProc/src/TBB_Dipole.cc b/RTCP/Cobalt/OutputProc/src/TBB_Dipole.cc
index dfde6975bd7..35d49f9b012 100644
--- a/RTCP/Cobalt/OutputProc/src/TBB_Dipole.cc
+++ b/RTCP/Cobalt/OutputProc/src/TBB_Dipole.cc
@@ -295,9 +295,9 @@ namespace LOFAR
        * but this makes flagging lost packets more complicated than how we deal with it using appendFlags() below.
        * Also, fake out-of-order occurs when data is dumped (incorrectly) across the frozen TBB write pointer (LOFAR control bug).
        */
-      if (frame.header.time < itsDumpInfo.itsTime0 ||
-          ((frame.header.time == itsDumpInfo.itsTime0)
-              && (sliceNr < itsDumpInfo.itsSliceNr0)))
+      if(frame.header.time < itsDumpInfo.itsTime0
+      || ((frame.header.time == itsDumpInfo.itsTime0)
+          && (sliceNr < itsDumpInfo.itsSliceNr0)))
       {
           logErrorRateLimited(&itsLastLogErrorTime, "TBB: received an "
               "out-of-order packet!  Current start time of first frame for "
@@ -316,18 +316,22 @@ namespace LOFAR
        * actually falls half-way).
        *
        * Calculate the offset of the frame start from the time stamp of the
-       * first received frame.
+       * first received frame.  Use 64 bit integers so that negative numbers
+       * can be handled.
        */
-      uint64_t offset = frame.header.time - itsDumpInfo.itsTime0;
-      LOG_INFO_STR("TBBDipole:  offset = frame.header.time - itsDumpInfo.itsTime0"
+      int64_t offset{frame.header.time};
+      offset -= itsDumpInfo.itsTime0;
+      LOG_INFO_STR("TBB:  offset = frame.header.time - itsDumpInfo.itsTime0"
           << offset << ", " << frame.header.time << ", " << itsDumpInfo.itsTime0);
+
       /**
-       * Convert the offset in [s] to samples containing a complex numbers of
-       * two 16 bit integers.  sampleFreq is in MHz.
+       * Convert the offset in [s] to the total number of samples (containing a
+       * complex number of two 16 bit integers).  sampleFreq is in MHz.
        */
-      offset *= (frame.header.sampleFreq * 1000000U);
-      LOG_INFO_STR("TBBDipole:  offset *= (frame.header.sampleFreq * 1000000U)"
+      offset *= (frame.header.sampleFreq * 1000000);
+      LOG_INFO_STR("TBB:  offset *= (frame.header.sampleFreq * 1000000)"
           << offset << ", " << static_cast< uint32_t >(frame.header.sampleFreq));
+
       /**
        * Divide the offset, i.e. the number of samples by the size of an FFT
        * block.  The result is the index of the raw voltage sampling window
@@ -344,7 +348,7 @@ namespace LOFAR
        * the begin of a voltage window is also the begin of a frequency window.
        */
       offset /= SPECTRAL_TRANSFORM_SIZE;
-      LOG_INFO_STR("TBBDipole:  offset /= SPECTRAL_TRANSFORM_SIZE"
+      LOG_INFO_STR("TBB:  offset /= SPECTRAL_TRANSFORM_SIZE"
           << offset << ", " << SPECTRAL_TRANSFORM_SIZE);
 
       /**
@@ -359,7 +363,7 @@ namespace LOFAR
        */
       if(frame.header.sampleFreq == 200)
       {
-        uint32_t time0{itsDumpInfo.itsTime0};
+        int64_t time0{itsDumpInfo.itsTime0};
         /**
          * Is the current frame not the first frame and is its time stamp an
          * even number?
@@ -383,15 +387,16 @@ namespace LOFAR
         offset += (frame.header.time - time0) / 2; // 1 extra slice every 2 seconds
       }
 
-      LOG_INFO_STR("TBBDipole:  offset after even second correction = "
-          << offset);
+      LOG_INFO_STR("TBB:  offset after even second correction = " << offset);
+
       /**
        * Add to the offset
        * Attention!
        * Both slice numbers are already divided by 1024!
        */
-      offset += sliceNr - itsDumpInfo.itsSliceNr0;
-      LOG_INFO_STR("TBBDipole:  offset += sliceNr - itsDumpInfo.itsSliceNr0"
+      offset += sliceNr;
+      offset -= itsDumpInfo.itsSliceNr0;
+      LOG_INFO_STR("TBB:  offset += sliceNr - itsDumpInfo.itsSliceNr0"
           << offset << ", " << sliceNr << ", " << itsDumpInfo.itsSliceNr0
           << ", " << SPECTRAL_TRANSFORM_SIZE);
 
@@ -451,7 +456,7 @@ namespace LOFAR
 
       // TODO 20181127: do we have enough reserved space to write all subbands in one file?
       // Since we are writing around HDF5, there is no need to lock. Resize the HDF5 dataset in the destructor.
-      LOG_INFO_STR("TBBDipole:  offset passed to pwrite:"
+      LOG_INFO_STR("TBB:  offset passed to pwrite:"
           << offset << ", offset casted for pwrite = " << (off_t)(offset * 2 * sizeof(int16_t)));
       pwrite(itsDumpInfo.itsRawFile->fd, frame.payload.data, frame.header.nOfSamplesPerFrame * 2 * sizeof(int16_t),
              (off_t)(offset * 2 * sizeof(int16_t)));
-- 
GitLab