Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/ufo/operators/timeoper/ObsTimeOper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ufo/ObsOperatorBase.h"
#include "ufo/ObsTraits.h"
#include "ufo/operators/timeoper/ObsTimeOperUtil.h"
#include "ufo/ScopedDefaultGeoVaLFormatChange.h"

namespace ufo {

Expand All @@ -39,7 +40,8 @@ ObsTimeOper::ObsTimeOper(const ioda::ObsSpace & odb,
odb,
oops::validateAndDeserialize<ObsOperatorParametersWrapper>(
parameters.obsOperator.value()).operatorParameters)),
odb_(odb), timeWeights_(timeWeightCreate(odb, parameters))
odb_(odb), windowSub_(parameters.windowSub.value()),
timeWeights_(timeWeightCreate(odb, parameters))
{
oops::Log::trace() << "ObsTimeOper constructor start" << std::endl;

Expand All @@ -52,6 +54,9 @@ ObsTimeOper::ObsTimeOper(const ioda::ObsSpace & odb,
if (window == windowSub) {
ABORT("Time Interpolation of obs not implemented when assimilation window = subWindow");
}
if (window.toSeconds() % windowSub.toSeconds() != 0) {
ABORT("Time Interpolation of obs requires windowSub to divide the assimilation window");
}
oops::Log::trace() << "ObsTimeOper constructor done" << std::endl;
}

Expand All @@ -66,7 +71,15 @@ ObsTimeOper::~ObsTimeOper() {

ObsTimeOper::Locations_ ObsTimeOper::locations() const {
oops::Log::trace() << "ObsOperatorTime::locations start" << std::endl;
return actualoperator_->locations();
return timeOperLocations(odb_, windowSub_);
}

// -----------------------------------------------------------------------------

void ObsTimeOper::computeReducedVars(const oops::Variables &, GeoVaLs & geovals) const {
oops::Log::trace() << "ObsTimeOper::computeReducedVars start" << std::endl;
timeInterpolate(geovals, timeWeights_);
oops::Log::trace() << "ObsTimeOper::computeReducedVars done" << std::endl;
}

// -----------------------------------------------------------------------------
Expand All @@ -78,16 +91,10 @@ void ObsTimeOper::simulateObs(const GeoVaLs & gv, ioda::ObsVector & ovec,

oops::Log::debug() << gv << std::endl;

GeoVaLs gv1(gv);
GeoVaLs gv2(gv);

oops::Log::debug() << gv1 << std::endl;
oops::Log::debug() << gv2 << std::endl;

gv1 *= timeWeights_[0];
gv2 *= timeWeights_[1];
gv1 += gv2;
actualoperator_->simulateObs(gv1, ovec, ydiags, qc_flags);
GeoVaLs gvt(gv);
timeInterpolate(gvt, timeWeights_);
ScopedDefaultGeoVaLFormatChange change(gvt, GeoVaLFormat::REDUCED);
actualoperator_->simulateObs(gvt, ovec, ydiags, qc_flags);

oops::Log::trace() << "ObsTimeOper::simulateObs done " << std::endl;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ufo/operators/timeoper/ObsTimeOper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <vector>

#include "ioda/ObsDataVector.h"
#include "oops/util/Duration.h"
#include "oops/util/ObjectCounter.h"

#include "ufo/ObsOperatorBase.h"
Expand Down Expand Up @@ -54,13 +55,16 @@ class ObsTimeOper : public ObsOperatorBase,

Locations_ locations() const override;

void computeReducedVars(const oops::Variables & vars, GeoVaLs & geovals) const override;

// Other
const oops::Variables & requiredVars() const override {return actualoperator_->requiredVars();}

private:
void print(std::ostream &) const override;
std::unique_ptr<ObsOperatorBase> actualoperator_;
const ioda::ObsSpace& odb_;
util::Duration windowSub_;
std::vector<std::vector<float>> timeWeights_;
};

Expand Down
49 changes: 17 additions & 32 deletions src/ufo/operators/timeoper/ObsTimeOperTLAD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "ufo/GeoVaLs.h"
#include "ufo/operators/timeoper/ObsTimeOperUtil.h"
#include "ufo/ScopedDefaultGeoVaLFormatChange.h"

namespace ufo {

Expand Down Expand Up @@ -56,19 +57,10 @@ void ObsTimeOperTLAD::setTrajectory(const GeoVaLs & geovals,
// oops::Log::debug() << "ObsTimeOperTLAD::setTrajectory input geovals "
// << geovals << std::endl;

GeoVaLs gv1(geovals);
GeoVaLs gv2(geovals);

gv1 *= timeWeights_[0];
gv2 *= timeWeights_[1];
gv1 += gv2;

// oops::Log::debug() << "ObsTimeOperTLAD::setTrajectory final geovals gv1 "
// << gv1 << std::endl;

actualoperator_->setTrajectory(gv1, ydiags, qc_flags);

// oops::Log::debug() << gv1;
GeoVaLs gvt(geovals);
timeInterpolate(gvt, timeWeights_);
ScopedDefaultGeoVaLFormatChange change(gvt, GeoVaLFormat::REDUCED);
actualoperator_->setTrajectory(gvt, ydiags, qc_flags);

oops::Log::trace() << "ObsTimeOperTLAD::setTrajectory done" << std::endl;
}
Expand All @@ -81,17 +73,10 @@ void ObsTimeOperTLAD::simulateObsTL(const GeoVaLs & geovals, ioda::ObsVector & o
// oops::Log::debug() << "ObsTimeOperTLAD::setTrajectory input geovals "
// << geovals << std::endl;

GeoVaLs gv1(geovals);
GeoVaLs gv2(geovals);

gv1 *= timeWeights_[0];
gv2 *= timeWeights_[1];
gv1 += gv2;

// oops::Log::debug() << "ObsTimeOperTLAD::simulateObsTL final geovals gv1 "
// << gv1 << std::endl;

actualoperator_->simulateObsTL(gv1, ovec);
GeoVaLs gvt(geovals);
timeInterpolate(gvt, timeWeights_);
ScopedDefaultGeoVaLFormatChange change(gvt, GeoVaLFormat::REDUCED);
actualoperator_->simulateObsTL(gvt, ovec);

oops::Log::trace() << "ObsTimeOperTLAD::simulateObsTL done" << std::endl;
}
Expand All @@ -104,14 +89,14 @@ void ObsTimeOperTLAD::simulateObsAD(GeoVaLs & geovals, const ioda::ObsVector & o
// oops::Log::debug() << "ObsTimeOperTLAD::simulateObsAD input geovals "
// << geovals << std::endl;

actualoperator_->simulateObsAD(geovals, ovec);

GeoVaLs gv2(geovals);

geovals *= timeWeights_[0];
gv2 *= timeWeights_[1];

geovals += gv2;
GeoVaLs gvad(geovals);
gvad.zero();
timeInterpolate(gvad, timeWeights_); // allocates the reduced format
{
ScopedDefaultGeoVaLFormatChange change(gvad, GeoVaLFormat::REDUCED);
actualoperator_->simulateObsAD(gvad, ovec);
}
timeInterpolateAD(geovals, gvad, timeWeights_);

// oops::Log::debug() << "ObsTimeOperTLAD::simulateObsAD final geovals "
// << geovals << std::endl;
Expand Down
104 changes: 104 additions & 0 deletions src/ufo/operators/timeoper/ObsTimeOperUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@


#include <algorithm>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>

#include "ioda/ObsSpace.h"
#include "ioda/ObsVector.h"

#include "oops/base/Variables.h"
#include "oops/interface/SampledLocations.h"
#include "oops/util/DateTime.h"
#include "oops/util/Duration.h"
#include "oops/util/Logger.h"
#include "oops/util/missingValues.h"
#include "oops/util/Range.h"
#include "ufo/GeoVaLs.h"
#include "ufo/ObsTraits.h"
#include "ufo/operators/timeoper/ObsTimeOperParameters.h"
#include "ufo/operators/timeoper/ObsTimeOperUtil.h"
#include "ufo/SampledLocations.h"

namespace ufo {

Expand Down Expand Up @@ -82,6 +91,101 @@ std::vector<std::vector<float>> timeWeightCreate(const ioda::ObsSpace & odb_,
oops::Log::trace() << "TimeOperUtil::timeWeightCreate done" << std::endl;
return timeWeights;
}
// -----------------------------------------------------------------------------
oops::Locations<ObsTraits> timeOperLocations(const ioda::ObsSpace & odb,
const util::Duration & windowSub) {
const size_t nlocs = odb.nlocs();
std::vector<float> lons(nlocs), lats(nlocs);
std::vector<util::DateTime> times(nlocs);
odb.get_db("MetaData", "longitude", lons);
odb.get_db("MetaData", "latitude", lats);
odb.get_db("MetaData", "dateTime", times);

const util::DateTime windowBegin = odb.windowStart();
const int64_t windowSubSec = windowSub.toSeconds();
std::vector<float> pathLons, pathLats;
std::vector<util::DateTime> pathTimes;
std::vector<util::Range<size_t>> pathsGroupedByLocation(nlocs);
for (size_t jloc = 0; jloc < nlocs; ++jloc) {
const int64_t fromStart = (times[jloc] - windowBegin).toSeconds();
const util::DateTime before =
windowBegin + util::Duration((fromStart / windowSubSec) * windowSubSec);
const util::DateTime after = (before == times[jloc]) ? before : before + windowSub;
for (const util::DateTime & t : {before, after}) {
pathLons.push_back(lons[jloc]);
pathLats.push_back(lats[jloc]);
pathTimes.push_back(t);
}
pathsGroupedByLocation[jloc] = {2 * jloc, 2 * jloc + 2};
}
// GetValues ignores paths on an excluded window bound, so move them one second inside
const std::vector<bool> inWindow = odb.timeWindow().createTimeMask(pathTimes);
for (size_t jpath = 0; jpath < pathTimes.size(); ++jpath) {
if (!inWindow[jpath])
pathTimes[jpath] += util::Duration(pathTimes[jpath] <= windowBegin ? 1 : -1);
}
return oops::SampledLocations<ObsTraits>(
std::make_unique<SampledLocations>(pathLons, pathLats, pathTimes, odb.distribution(),
std::move(pathsGroupedByLocation)));
}

// -----------------------------------------------------------------------------
void timeInterpolate(GeoVaLs & gv, const std::vector<std::vector<float>> & timeWeights) {
const double missing = util::missingValue<double>();
const oops::Variables vars = gv.getVars();
oops::Variables newVars = vars;
newVars -= gv.getReducedVars();
std::vector<size_t> nlevs;
for (const auto & var : newVars) nlevs.push_back(gv.nlevs(var, GeoVaLFormat::SAMPLED));
gv.addReducedVars(newVars, nlevs);

std::vector<util::Range<size_t>> paths;
std::vector<double> before, after;
for (const auto & var : vars) {
if (gv.areReducedAndSampledFormatsAliased(var)) continue; // one path per location
gv.getProfileIndicesGroupedByLocation(var, paths, GeoVaLFormat::SAMPLED);
const size_t nlevs = gv.nlevs(var, GeoVaLFormat::SAMPLED);
for (size_t jloc = 0; jloc < paths.size(); ++jloc) {
ASSERT(paths[jloc].end - paths[jloc].begin == 2);
before.assign(nlevs, 0.0);
after.assign(nlevs, 0.0);
gv.getProfile(before, var, paths[jloc].begin, GeoVaLFormat::SAMPLED);
gv.getProfile(after, var, paths[jloc].begin + 1, GeoVaLFormat::SAMPLED);
for (size_t jlev = 0; jlev < before.size(); ++jlev) {
before[jlev] = (before[jlev] == missing || after[jlev] == missing) ? missing :
timeWeights[0][jloc] * before[jlev] + timeWeights[1][jloc] * after[jlev];
}
gv.putProfile(before, var, jloc, GeoVaLFormat::REDUCED);
}
}
}

// -----------------------------------------------------------------------------
void timeInterpolateAD(GeoVaLs & gv, const GeoVaLs & gvad,
const std::vector<std::vector<float>> & timeWeights) {
const double missing = util::missingValue<double>();
std::vector<util::Range<size_t>> paths;
std::vector<double> reduced, sampled;
for (const auto & var : gv.getVars()) {
gv.getProfileIndicesGroupedByLocation(var, paths, GeoVaLFormat::SAMPLED);
const size_t nlevs = gv.nlevs(var, GeoVaLFormat::SAMPLED);
for (size_t jloc = 0; jloc < paths.size(); ++jloc) {
reduced.assign(nlevs, 0.0);
gvad.getProfile(reduced, var, jloc, GeoVaLFormat::REDUCED);
const size_t npaths = paths[jloc].end - paths[jloc].begin;
for (size_t jp = 0; jp < npaths; ++jp) {
const double weight = (npaths == 1) ? 1.0 : timeWeights[jp][jloc];
sampled.assign(nlevs, 0.0);
gv.getProfile(sampled, var, paths[jloc].begin + jp, GeoVaLFormat::SAMPLED);
for (size_t jlev = 0; jlev < sampled.size(); ++jlev) {
if (reduced[jlev] != missing) sampled[jlev] += weight * reduced[jlev];
}
gv.putProfile(sampled, var, paths[jloc].begin + jp, GeoVaLFormat::SAMPLED);
}
}
}
}

// -----------------------------------------------------------------------------

} // namespace ufo
14 changes: 14 additions & 0 deletions src/ufo/operators/timeoper/ObsTimeOperUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@

#include "ioda/ObsVector.h"

#include "oops/base/Locations.h"
#include "oops/util/DateTime.h"
#include "oops/util/Duration.h"
#include "oops/util/Logger.h"

namespace ufo {

class GeoVaLs;
class ObsTimeOperParameters;
struct ObsTraits;

std::vector<std::vector<float>> timeWeightCreate(const ioda::ObsSpace & odb_,
const ObsTimeOperParameters & parameters);

/// Two interpolation paths per location, at the model states before and after the observation.
oops::Locations<ObsTraits> timeOperLocations(const ioda::ObsSpace & odb,
const util::Duration & windowSub);

/// Store in the reduced format the time-weighted sum of the two profiles sampling each location.
void timeInterpolate(GeoVaLs & gv, const std::vector<std::vector<float>> & timeWeights);

/// Adjoint of timeInterpolate: add the weighted reduced profiles of `gvad` to the sampled `gv`.
void timeInterpolateAD(GeoVaLs & gv, const GeoVaLs & gvad,
const std::vector<std::vector<float>> & timeWeights);

// -----------------------------------------------------------------------------

} // namespace ufo
Expand Down
15 changes: 15 additions & 0 deletions test/mains/TestObsTimeOper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* (C) Copyright 2026 UCAR.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*/

#include "../ufo/ObsTimeOper.h"
#include "oops/runs/Run.h"

int main(int argc, char ** argv) {
oops::Run run(argc, argv);
ufo::test::ObsTimeOper tests;
return run.execute(tests);
}
9 changes: 9 additions & 0 deletions test/testinput/unit_tests/operators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,15 @@ ufo_add_test( NAME linopr_timeoper
LABELS operators
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../../../ )
#
ufo_add_test( NAME opr_timeoper_two_states
ECBUILD
SOURCES ../../../mains/TestObsTimeOper.cc ../../../ufo/ObsTimeOper.h
ARGS "${CMAKE_CURRENT_SOURCE_DIR}/timeoper_two_states.yaml"
MPI 1
LIBS ufo
LABELS operators
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../../../ )
#
ufo_add_test( NAME opr_tropomi_no2
ECBUILD
COMMAND ${CMAKE_BINARY_DIR}/bin/test_ObsOperator.x
Expand Down
Loading