UFO
ProfileConsistencyChecks.cc
Go to the documentation of this file.
1 /*
2  * (C) Crown copyright 2020, Met Office
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #include <algorithm>
9 #include <cmath>
10 #include <cstring>
11 #include <iomanip>
12 #include <limits>
13 #include <vector>
14 
15 #include "eckit/config/Configuration.h"
16 
17 #include "ioda/ObsDataVector.h"
18 #include "ioda/ObsSpace.h"
19 
20 #include "oops/interface/ObsFilter.h"
21 #include "oops/util/abor1_cpp.h"
22 #include "oops/util/Logger.h"
23 
27 
34 
35 namespace ufo {
36 
37  // -----------------------------------------------------------------------------
38 
40  (ioda::ObsSpace & obsdb,
41  const eckit::Configuration & config,
42  std::shared_ptr<ioda::ObsDataVector<int> > flags,
43  std::shared_ptr<ioda::ObsDataVector<float> > obserr)
44  : FilterBase(obsdb, config, flags, obserr)
45  {
47  options_->deserialize(config);
48 
49  allvars_ += Variables(filtervars_, "HofX");
50 
51  // Throw exception if expected configuration option is missing.
52  // It is essential for observations to be grouped according to (e.g.) station ID
53  // (unless there is only one profile in the sample, which would be very unusual)
54  if (obsdb.obs_group_var().empty())
55  throw eckit::BadParameter("group variable is empty.", Here());
56  }
57 
58  // -----------------------------------------------------------------------------
59 
61 
62  // -----------------------------------------------------------------------------
63 
64  void ProfileConsistencyChecks::applyFilter(const std::vector<bool> & apply,
65  const Variables & filtervars,
66  std::vector<std::vector<bool>> & flagged) const
67  {
68  print(oops::Log::trace());
69 
70  const int nlocs = static_cast <int> (obsdb_.nlocs());
71  const int nprofs = static_cast <int> (obsdb_.nrecs());
72 
73  // Handles data in entire sample
74  EntireSampleDataHandler entireSampleDataHandler(obsdb_,
75  *options_);
76 
77  // Determines indices of profile's observations in entire sample
78  ProfileIndices profileIndices(obsdb_,
79  *options_,
80  apply);
81 
82  // Handles individual profile data
83  ProfileDataHandler profileDataHandler(obsdb_,
84  *options_,
85  entireSampleDataHandler,
86  profileIndices);
87 
88  // (Optionally) validates check results against OPS values
89  ProfileCheckValidator profileCheckValidator(*options_,
90  profileDataHandler);
91 
92  // Applies checks to each profile
93  ProfileChecker profileChecker(*options_,
94  profileIndices,
95  profileDataHandler,
96  profileCheckValidator);
97 
98  // Loop over profiles
99  oops::Log::debug() << "Starting loop over profiles..." << std::endl;
100 
101  for (int jprof = 0; jprof < nprofs; ++jprof) {
102  oops::Log::debug() << "Profile " << (jprof + 1) << " / " << nprofs << std::endl;
103 
104  // Determine indices in entire sample that correspond to this profile
105  profileIndices.determineProfileIndices();
106 
107  // Reset contents of profile data handler
108  profileDataHandler.reset();
109 
110  // Print station ID if requested
111  if (options_->PrintStationID.value()) {
112  const std::vector <std::string> &station_ID =
113  profileDataHandler.get<std::string>(ufo::VariableNames::station_ID);
114  if (!station_ID.empty())
115  oops::Log::debug() << "Station ID: " << station_ID[0] << std::endl;
116  }
117 
118  // Run checks
119  profileChecker.runChecks();
120 
121  // After all checks have run, set final report flags in this profile
122  profileDataHandler.setFinalReportFlags();
123 
124  // Modify 'flagged' vector for each filter variable based on check results
125  profileDataHandler.setFlagged(filtervars.nvars(), flagged);
126 
127  // If any variables in the current profile were modified by the checks,
128  // the equivalent variables in the entire sample are set to the modified values.
129  profileDataHandler.updateEntireSampleData();
130 
131  // Optionally compare check results with OPS values
132  if (options_->compareWithOPS.value() && profileChecker.getBasicCheckResult()) {
133  profileCheckValidator.validate();
134  nMismatches_.emplace_back(profileCheckValidator.getMismatches());
135  }
136  }
137 
138  // Write out any quantities that may have changed to obsdb
139  entireSampleDataHandler.writeQuantitiesToObsdb();
140 
141  oops::Log::debug() << "... Finished loop over profiles" << std::endl;
142  oops::Log::debug() << std::endl;
143  }
144 
145  // -----------------------------------------------------------------------------
146 
147  void ProfileConsistencyChecks::print(std::ostream & os) const {
148  os << "ProfileConsistencyChecks: config = " << config_ << std::endl;
149  }
150 
151  // -----------------------------------------------------------------------------
152 
153 } // namespace ufo
ufo::Variables::nvars
size_t nvars() const
Definition: Variables.cc:104
ufo::ProfileDataHandler
Retrieve and store data for individual profiles. To do this, first the vector of values in the entire...
Definition: ProfileDataHandler.h:40
ufo::ProfileConsistencyChecks::applyFilter
void applyFilter(const std::vector< bool > &, const Variables &, std::vector< std::vector< bool >> &) const override
Definition: ProfileConsistencyChecks.cc:64
ufo::ProfileConsistencyChecks::~ProfileConsistencyChecks
~ProfileConsistencyChecks()
Definition: ProfileConsistencyChecks.cc:60
ufo::ProfileIndices
Determine indices of observations making up individual profiles. The indices are computed with respec...
Definition: ProfileIndices.h:39
ufo::Variables
Definition: src/ufo/filters/Variables.h:24
ufo::ProfileCheckValidator
Profile QC check validator.
Definition: ProfileCheckValidator.h:27
ufo_radiancerttov_utils_mod::debug
logical, public debug
Definition: ufo_radiancerttov_utils_mod.F90:100
ufo::FilterBase::obsdb_
ioda::ObsSpace & obsdb_
Definition: FilterBase.h:59
ProfileConsistencyCheckParameters.h
ProfileIndices.h
getScalarOrFilterData.h
ufo::FilterBase
FilterBase: Base class for UFO QC filters.
Definition: FilterBase.h:42
ufo
Definition: RunCRTM.h:27
ufo::ProfileChecker
Profile QC checker.
Definition: ProfileChecker.h:29
ufo::ProfileConsistencyChecks::ProfileConsistencyChecks
ProfileConsistencyChecks(ioda::ObsSpace &, const eckit::Configuration &, std::shared_ptr< ioda::ObsDataVector< int > >, std::shared_ptr< ioda::ObsDataVector< float > >)
Definition: ProfileConsistencyChecks.cc:40
ufo::FilterBase::allvars_
ufo::Variables allvars_
Definition: FilterBase.h:63
ufo::ProfileDataHandler::setFinalReportFlags
void setFinalReportFlags()
Set final report flags based on the NumAnyErrors counter.
Definition: ProfileDataHandler.cc:74
ufo::ProfileIndices::determineProfileIndices
void determineProfileIndices()
Determine indices in entire sample for this profile.
Definition: ProfileIndices.cc:40
ProfileDataHandler.h
ufo::ProfileConsistencyChecks::nMismatches_
std::vector< int > nMismatches_
Definition: src/ufo/filters/ProfileConsistencyChecks.h:108
ufo::ProfileDataHandler::updateEntireSampleData
void updateEntireSampleData()
Definition: ProfileDataHandler.cc:44
EntireSampleDataHandler.h
ProfileChecker.h
ufo::EntireSampleDataHandler
Retrieve and store data for entire sample. This class uses lazy loading; vectors of variables are ret...
Definition: EntireSampleDataHandler.h:42
ufo::EntireSampleDataHandler::writeQuantitiesToObsdb
void writeQuantitiesToObsdb()
Definition: EntireSampleDataHandler.cc:20
ufo::ProfileConsistencyCheckParameters
Options controlling the operation of the ProfileConsistencyChecks filter.
Definition: ProfileConsistencyCheckParameters.h:33
ufo::ProfileChecker::runChecks
void runChecks()
Run all checks requested.
Definition: ProfileChecker.cc:49
ufo::FilterBase::filtervars_
ufo::Variables filtervars_
Definition: FilterBase.h:64
ProfileCheckValidator.h
ioda::ObsDataVector< int >
ufo::ProfileCheckValidator::validate
void validate()
Validate check results against OPS values.
Definition: ProfileCheckValidator.cc:200
ufo::FilterBase::config_
const eckit::LocalConfiguration config_
Definition: FilterBase.h:60
ProfileConsistencyChecks.h
ufo::ProfileConsistencyChecks::options_
std::unique_ptr< ProfileConsistencyCheckParameters > options_
Configurable options.
Definition: src/ufo/filters/ProfileConsistencyChecks.h:104
VariableNames.h
ufo::ProfileCheckValidator::getMismatches
int getMismatches() const
Get number of mismatches between values produced in this code and the OPS equivalents.
Definition: ProfileCheckValidator.h:36
ufo::ProfileDataHandler::get
std::vector< T > & get(const std::string &fullname)
Definition: ProfileDataHandler.h:53
ufo::ProfileDataHandler::setFlagged
void setFlagged(const size_t nvars, std::vector< std::vector< bool >> &flagged)
Definition: ProfileDataHandler.cc:87
ufo::VariableNames::station_ID
static constexpr const char *const station_ID
Definition: VariableNames.h:80
ufo::ProfileChecker::getBasicCheckResult
bool getBasicCheckResult()
Get basic check result.
Definition: ProfileChecker.h:40
ufo::ProfileDataHandler::reset
void reset()
Definition: ProfileDataHandler.cc:22
ufo::ProfileConsistencyChecks::print
void print(std::ostream &) const override
Definition: ProfileConsistencyChecks.cc:147