From 38fe7095fba449907df83ab65b82a2e98fa8cf85 Mon Sep 17 00:00:00 2001
From: Mattia Mancini <mancini@astron.nl>
Date: Fri, 12 Apr 2024 10:39:02 +0000
Subject: [PATCH] Make predict and apply beam use cached time directions

---
 CHANGELOG.md             |   2 +-
 CMakeLists.txt           |   2 +-
 docker/ubuntu_20_04_base | 116 ++++++++++++++++++-------------------
 docker/ubuntu_22_04_base | 122 +++++++++++++++++++--------------------
 steps/ApplyBeam.cc       |   4 ++
 steps/OnePredict.cc      |   4 ++
 6 files changed, 129 insertions(+), 121 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a70e857dc..8ba42540c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,7 @@
 ## Next release
 
 ### Improvements
-- DP3 now requires EveryBeam v0.5.4
+- DP3 now requires EveryBeam v0.5.8
 
 ## [6.0] - 2023-08-11
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2f8a3cced..f6e84ee97 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -249,7 +249,7 @@ include_directories(${AOFLAGGER_INCLUDE_DIR})
 # make it somewhat more explicit
 find_package(EveryBeam NO_MODULE)
 if(${EVERYBEAM_FOUND})
-  if(${EVERYBEAM_VERSION} VERSION_LESS "0.5.4" OR ${EVERYBEAM_VERSION}
+  if(${EVERYBEAM_VERSION} VERSION_LESS "0.5.8" OR ${EVERYBEAM_VERSION}
                                                   VERSION_GREATER_EQUAL "0.6.0")
     message(
       FATAL_ERROR
diff --git a/docker/ubuntu_20_04_base b/docker/ubuntu_20_04_base
index 5f20ff4d9..72c6856b4 100644
--- a/docker/ubuntu_20_04_base
+++ b/docker/ubuntu_20_04_base
@@ -2,83 +2,83 @@ FROM ubuntu:20.04
 
 # TODO: needs to be bumped before next DP3 release
 # ENV IDG_VERSION=0.8
-ENV EVERYBEAM_VERSION=v0.5.7
+ENV EVERYBEAM_VERSION=v0.5.8
 ENV IDG_VERSION=6b61c038883ad3f807d20047c4f9e1a1f0b8d98a
 ENV AOFLAGGER_VERSION=65d5fba4f4c12797386d3fd9cd76734956a8b233
 
 RUN export DEBIAN_FRONTEND="noninteractive" && \
 	apt-get update && \
 	apt-get install -y \
-		bison \
-		build-essential \
-		casacore-dev \
-		casacore-tools \
-		cmake \
-		doxygen \
-		flex \
-		gfortran \
-		git \
-		libarmadillo-dev \
-		libboost-date-time-dev \
-		libboost-filesystem-dev \
-		libboost-program-options-dev \
-		libboost-python-dev \
-		libboost-system-dev \
-		libboost-test-dev \
-		libcfitsio-dev \
-		libfftw3-dev \
-		libgtkmm-3.0-dev \
-		libhdf5-serial-dev \
-		liblua5.3-dev \
-		libpng-dev \
-		ninja-build \
-		pkg-config \
-		pybind11-dev \
-		python3-dev \
-		python3-numpy \
-		python3-pip \
-		wcslib-dev \
-		wget \
+	bison \
+	build-essential \
+	casacore-dev \
+	casacore-tools \
+	cmake \
+	doxygen \
+	flex \
+	gfortran \
+	git \
+	libarmadillo-dev \
+	libboost-date-time-dev \
+	libboost-filesystem-dev \
+	libboost-program-options-dev \
+	libboost-python-dev \
+	libboost-system-dev \
+	libboost-test-dev \
+	libcfitsio-dev \
+	libfftw3-dev \
+	libgtkmm-3.0-dev \
+	libhdf5-serial-dev \
+	liblua5.3-dev \
+	libpng-dev \
+	ninja-build \
+	pkg-config \
+	pybind11-dev \
+	python3-dev \
+	python3-numpy \
+	python3-pip \
+	wcslib-dev \
+	wget \
 	&& \
 	rm -rf /var/lib/apt/lists/*
 # Build aoflagger3
 RUN mkdir /aoflagger && cd /aoflagger \
-    && git clone https://gitlab.com/aroffringa/aoflagger.git src \
-    && ( cd src/ && git checkout ${AOFLAGGER_VERSION} ) \
-    && mkdir build && cd build \
-    && cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src \
-    && ninja install \
-    && cd / && rm -rf aoflagger
+	&& git clone https://gitlab.com/aroffringa/aoflagger.git src \
+	&& ( cd src/ && git checkout ${AOFLAGGER_VERSION} ) \
+	&& mkdir build && cd build \
+	&& cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src \
+	&& ninja install \
+	&& cd / && rm -rf aoflagger
 # Build IDG
 # (PORTABLE: it may run on a different node than where it was compiled)
 RUN mkdir /idg && cd /idg \
-    && git clone https://git.astron.nl/RD/idg.git src \
-    && ( cd src/ && git checkout ${IDG_VERSION} ) \
-    && mkdir build && cd build \
-    && cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DPORTABLE=ON ../src \
-    && ninja install \
-    && cd / && rm -rf idg
+	&& git clone https://git.astron.nl/RD/idg.git src \
+	&& ( cd src/ && git checkout ${IDG_VERSION} ) \
+	&& mkdir build && cd build \
+	&& cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DPORTABLE=ON ../src \
+	&& ninja install \
+	&& cd / && rm -rf idg
 # Build EveryBeam
 RUN mkdir /everybeam && cd /everybeam \
-    && git clone https://git.astron.nl/RD/EveryBeam.git src \
-    && ( cd src/ && git checkout ${EVERYBEAM_VERSION} ) \
-    && mkdir build && cd build \
-    && cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src -DPYTHON_EXECUTABLE=/usr/bin/python3 \
-    && ninja install \
-    && cd / && rm -rf everybeam
+	&& git clone https://git.astron.nl/RD/EveryBeam.git src \
+	&& ( cd src/ && git checkout ${EVERYBEAM_VERSION} ) \
+	&& mkdir build && cd build \
+	&& cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src -DPYTHON_EXECUTABLE=/usr/bin/python3 \
+	&& ninja install \
+	&& cd / && rm -rf everybeam
 # Install WSRT Measures (extra casacore data, for integration tests)
 # Note: The file on the ftp site is updated daily. When warnings regarding leap
 # seconds appear, ignore them or regenerate the docker image.
 RUN wget -nv -O /WSRT_Measures.ztar ftp://ftp.astron.nl/outgoing/Measures/WSRT_Measures.ztar \
-    && cd /var/lib/casacore/data \
-    && tar xfz /WSRT_Measures.ztar \
-    && rm /WSRT_Measures.ztar
+	&& cd /var/lib/casacore/data \
+	&& tar xfz /WSRT_Measures.ztar \
+	&& rm /WSRT_Measures.ztar
 # Install pip dependencies
 RUN pip3 install \
-		autosemver==0.5.5 \
-		gcovr \
-		h5py \
-		jsonschema2rst==0.1.0 \
-		sphinx \
-		sphinx-rtd-theme \
+	autosemver==0.5.5 \
+	gcovr \
+	h5py \
+	jsonschema2rst==0.1.0 \
+	sphinx \
+	sphinx-rtd-theme \
 	;
diff --git a/docker/ubuntu_22_04_base b/docker/ubuntu_22_04_base
index f3134dd23..901fc3e83 100644
--- a/docker/ubuntu_22_04_base
+++ b/docker/ubuntu_22_04_base
@@ -2,85 +2,85 @@ FROM ubuntu:22.04
 
 # TODO: needs to be bumped before next DP3 release
 # ENV IDG_VERSION=0.8
-ENV EVERYBEAM_VERSION=v0.5.7
+ENV EVERYBEAM_VERSION=v0.5.8
 ENV IDG_VERSION=6b61c038883ad3f807d20047c4f9e1a1f0b8d98a
 ENV AOFLAGGER_VERSION=65d5fba4f4c12797386d3fd9cd76734956a8b233
 
 RUN export DEBIAN_FRONTEND="noninteractive" && \
 	apt-get update && \
-    apt-get install -y \
-		bison \
-		build-essential \
-		casacore-dev \
-		casacore-tools \
-		clang-format-14 \
-		cmake \
-		doxygen \
-		flex \
-		gcovr \
-		gfortran \
-		git \
-		libarmadillo-dev \
-		libboost-date-time-dev \
-		libboost-filesystem-dev \
-		libboost-program-options-dev \
-		libboost-python-dev \
-		libboost-system-dev \
-		libboost-test-dev \
-		libcfitsio-dev \
-		libfftw3-dev \
-		libgtkmm-3.0-dev \
-		libhdf5-serial-dev \
-		liblua5.3-dev \
-		libpng-dev \
-		ninja-build \
-		pkg-config \
-		pybind11-dev \
-		python3-dev \
-		python3-numpy \
-		python3-pip \
-		wcslib-dev \
-		wget \
+	apt-get install -y \
+	bison \
+	build-essential \
+	casacore-dev \
+	casacore-tools \
+	clang-format-14 \
+	cmake \
+	doxygen \
+	flex \
+	gcovr \
+	gfortran \
+	git \
+	libarmadillo-dev \
+	libboost-date-time-dev \
+	libboost-filesystem-dev \
+	libboost-program-options-dev \
+	libboost-python-dev \
+	libboost-system-dev \
+	libboost-test-dev \
+	libcfitsio-dev \
+	libfftw3-dev \
+	libgtkmm-3.0-dev \
+	libhdf5-serial-dev \
+	liblua5.3-dev \
+	libpng-dev \
+	ninja-build \
+	pkg-config \
+	pybind11-dev \
+	python3-dev \
+	python3-numpy \
+	python3-pip \
+	wcslib-dev \
+	wget \
 	&& \
 	rm -rf /var/lib/apt/lists/*
 # Build aoflagger3
 RUN mkdir /aoflagger && cd /aoflagger \
-    && git clone https://gitlab.com/aroffringa/aoflagger.git src \
-    && ( cd src/ && git checkout ${AOFLAGGER_VERSION} ) \
-    && mkdir build && cd build \
-    && cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src \
-    && ninja install \
-    && cd / && rm -rf aoflagger
+	&& git clone https://gitlab.com/aroffringa/aoflagger.git src \
+	&& ( cd src/ && git checkout ${AOFLAGGER_VERSION} ) \
+	&& mkdir build && cd build \
+	&& cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src \
+	&& ninja install \
+	&& cd / && rm -rf aoflagger
 # Build IDG
 # (PORTABLE: it may run on a different node than where it was compiled)
 RUN mkdir /idg && cd /idg \
-    && git clone https://git.astron.nl/RD/idg.git src \
-    && ( cd src/ && git checkout ${IDG_VERSION} ) \
-    && mkdir build && cd build \
-    && cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DPORTABLE=ON ../src \
-    && ninja install \
-    && cd / && rm -rf idg
+	&& git clone https://git.astron.nl/RD/idg.git src \
+	&& ( cd src/ && git checkout ${IDG_VERSION} ) \
+	&& mkdir build && cd build \
+	&& cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DPORTABLE=ON ../src \
+	&& ninja install \
+	&& cd / && rm -rf idg
 # Build EveryBeam
 RUN mkdir /everybeam && cd /everybeam \
-    && git clone https://git.astron.nl/RD/EveryBeam.git src \
-    && ( cd src/ && git checkout ${EVERYBEAM_VERSION} ) \
-    && mkdir build && cd build \
-    && cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src -DPYTHON_EXECUTABLE=/usr/bin/python3 \
-    && ninja install \
-    && cd / && rm -rf everybeam
+	&& git clone https://git.astron.nl/RD/EveryBeam.git src \
+	&& ( cd src/ && git checkout ${EVERYBEAM_VERSION} ) \
+	&& mkdir build && cd build \
+	&& cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr ../src -DPYTHON_EXECUTABLE=/usr/bin/python3 \
+	&& ninja install \
+	&& cd / && rm -rf everybeam
 # Install WSRT Measures (extra casacore data, for integration tests)
 # Note: The file on the ftp site is updated daily. When warnings regarding leap
 # seconds appear, ignore them or regenerate the docker image.
 RUN wget -nv -O /WSRT_Measures.ztar ftp://ftp.astron.nl/outgoing/Measures/WSRT_Measures.ztar \
-    && cd /var/lib/casacore/data \
-    && tar xfz /WSRT_Measures.ztar \
-    && rm /WSRT_Measures.ztar
+	&& cd /var/lib/casacore/data \
+	&& tar xfz /WSRT_Measures.ztar \
+	&& rm /WSRT_Measures.ztar
 # Install pip dependencies
 RUN pip3 install \
-		black~=24.0 \
-		cmake-format \
-		h5py \
-		pytest \
-		sphinx \
-		sphinx-rtd-theme \
+	black~=24.0 \
+	cmake-format \
+	h5py \
+	pytest \
+	sphinx \
+	sphinx-rtd-theme \
 	;
diff --git a/steps/ApplyBeam.cc b/steps/ApplyBeam.cc
index 9ef1f24d9..605c47206 100644
--- a/steps/ApplyBeam.cc
+++ b/steps/ApplyBeam.cc
@@ -170,6 +170,8 @@ void ApplyBeam::updateInfo(const DPInfo& infoIn) {
         MDirection::Ref(MDirection::ITRF, itsMeasFrames[thread]));
     telescopes_[thread] = base::GetTelescope(
         info().msName(), itsElementResponseModel, itsUseChannelFreq);
+
+    telescopes_[thread]->SetTime(info().startTime());
   }
 }
 
@@ -231,6 +233,8 @@ bool ApplyBeam::processMultithreaded(std::unique_ptr<base::DPBuffer> buffer,
       srcdir = dir2Itrf(itsDirection, itsMeasConverters[threadIter]);
   }
 
+  telescopes_[thread]->SetTime(time);
+
   if (undoInputBeam) {
     // A beam was previously applied to this MS, and a different direction
     // was asked this time. 'Undo' applying the input beam.
diff --git a/steps/OnePredict.cc b/steps/OnePredict.cc
index 8817738e2..2ca0e4736 100644
--- a/steps/OnePredict.cc
+++ b/steps/OnePredict.cc
@@ -631,6 +631,10 @@ void OnePredict::PredictSourceRange(
   const size_t n_channels = info().nchan();
   const size_t n_buffer_correlations = stokes_i_only_ ? 1 : info().ncorr();
 
+  if (apply_beam_) {
+    telescope_->SetTime(time);
+  }
+
   aocommon::xt::UTensor<std::complex<double>, 3> model_data(
       {n_baselines, n_channels, n_buffer_correlations},
       std::complex<double>(0.0, 0.0));
-- 
GitLab