|
27 | 27 |
|
28 | 28 | #include "orca-jedi/geometry/Geometry.h"
|
29 | 29 | #include "orca-jedi/state/State.h"
|
| 30 | +#include "orca-jedi/increment/Increment.h" |
30 | 31 |
|
31 | 32 | #include "orca-jedi/interpolator/Interpolator.h"
|
32 | 33 |
|
@@ -177,6 +178,149 @@ template void Interpolator::executeInterpolation<float>(
|
177 | 178 | const std::vector<bool> & mask,
|
178 | 179 | std::vector<double>::iterator& iter) const;
|
179 | 180 |
|
| 181 | +void Interpolator::apply(const oops::Variables& vars, const Increment& inc, |
| 182 | + const std::vector<bool> & mask, |
| 183 | + std::vector<double>& result) const { |
| 184 | + |
| 185 | + // DJL question can the atlas templates help? |
| 186 | + |
| 187 | + // input is inc output is result |
| 188 | + |
| 189 | + const size_t nvars = vars.size(); |
| 190 | + |
| 191 | + for (size_t j=0; j < nvars; ++j) { |
| 192 | + if (!inc.variables().has(vars[j])) { |
| 193 | + std::stringstream err_stream; |
| 194 | + err_stream << "orcamodel::Interpolator::apply varname \" " |
| 195 | + << "\" " << vars[j] |
| 196 | + << " not found in the model increment." << std::endl; |
| 197 | + err_stream << " add the variable to the increment variables and " |
| 198 | + << "add a mapping from the geometry to that variable." |
| 199 | + << std::endl; |
| 200 | + throw eckit::BadParameter(err_stream.str(), Here()); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + const std::vector<size_t> varSizes = |
| 205 | + inc.geometry()->variableSizes(vars); |
| 206 | + size_t nvals = 0; |
| 207 | + for (size_t jvar=0; jvar < nvars; ++jvar) nvals += nlocs_ * varSizes[jvar]; |
| 208 | + result.resize(nvals); |
| 209 | + |
| 210 | + std::size_t out_idx = 0; |
| 211 | + for (size_t jvar=0; jvar < nvars; ++jvar) { |
| 212 | + auto gv_varname = vars[jvar].name(); |
| 213 | + atlas::Field tgt_field = atlasObsFuncSpace_.createField<double>( |
| 214 | + atlas::option::name(gv_varname) | |
| 215 | + atlas::option::levels(varSizes[jvar])); |
| 216 | + interpolator_.execute(inc.incrementFields()[gv_varname], tgt_field); |
| 217 | + auto field_view = atlas::array::make_view<double, 2>(tgt_field); |
| 218 | + atlas::field::MissingValue mv(inc.incrementFields()[gv_varname]); |
| 219 | + bool has_mv = static_cast<bool>(mv); |
| 220 | + for (std::size_t klev=0; klev < varSizes[jvar]; ++klev) { |
| 221 | + for (std::size_t iloc=0; iloc < nlocs_; iloc++) { |
| 222 | + if (has_mv && mv(field_view(iloc, klev))) { |
| 223 | + result[out_idx] = util::missingValue<double>(); |
| 224 | + } else { |
| 225 | + result[out_idx] = field_view(iloc, klev); |
| 226 | + } |
| 227 | + ++out_idx; |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +void Interpolator::applyAD(const oops::Variables& vars, Increment& inc, |
| 234 | + const std::vector<bool> & mask, |
| 235 | + const std::vector<double> & resultin) const |
| 236 | +{ |
| 237 | + |
| 238 | + // input is resultin output is inc |
| 239 | + |
| 240 | + oops::Log::trace() << "orcamodel::Interpolator::applyAD start " |
| 241 | + << std::endl; |
| 242 | + |
| 243 | + oops::Log::debug() << "DJL ** Interpolator::applyAD this needs checking **" << std::endl; |
| 244 | + |
| 245 | +// ** Not sure what I'm doing yet - Just trying to do the opposite of applyAD |
| 246 | + |
| 247 | + const size_t nvars = vars.size(); |
| 248 | + |
| 249 | + for (size_t j=0; j < nvars; ++j) { |
| 250 | + if (!inc.variables().has(vars[j])) { |
| 251 | + std::stringstream err_stream; |
| 252 | + err_stream << "orcamodel::Interpolator::apply varname \" " |
| 253 | + << "\" " << vars[j] |
| 254 | + << " not found in the model increment." << std::endl; |
| 255 | + err_stream << " add the variable to the increment variables and " |
| 256 | + << "add a mapping from the geometry to that variable." |
| 257 | + << std::endl; |
| 258 | + throw eckit::BadParameter(err_stream.str(), Here()); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + const std::vector<size_t> varSizes = |
| 263 | + inc.geometry()->variableSizes(vars); |
| 264 | + size_t nvals = 0; |
| 265 | + |
| 266 | +// boost::uuids::uuid uuid = boost::uuids::random_generator()(); |
| 267 | +// std::shared_ptr<const Geometry> geom = inc.geometry(); |
| 268 | +// writeGenFieldsToFile("applyADpre"+ boost::uuids::to_string(uuid) +".nc", *geom, inc.validTime(), inc.incrementFields()); |
| 269 | + |
| 270 | + for (size_t jvar=0; jvar < nvars; ++jvar) nvals += nlocs_ * varSizes[jvar]; |
| 271 | +// result.resize(nvals); |
| 272 | + |
| 273 | + std::size_t out_idx = 0; |
| 274 | + for (size_t jvar=0; jvar < nvars; ++jvar) { |
| 275 | + oops::Log::debug() << "DJL ** jvar " << jvar << " " << nvars |
| 276 | + << "varSizes " << varSizes[jvar] |
| 277 | + << std::endl; |
| 278 | + auto gv_varname = vars[jvar].name(); |
| 279 | +// atlas::Field tgt_field = atlasObsFuncSpace_.createField<double>( |
| 280 | +// atlas::option::name(gv_varname) | |
| 281 | +// atlas::option::levels(varSizes[jvar])); |
| 282 | +// atlas::Field incField = inc.incrementFields()[jvar]; |
| 283 | + |
| 284 | + auto tgt_field = atlasObsFuncSpace_.createField<double>( |
| 285 | + atlas::option::name(gv_varname) | |
| 286 | + atlas::option::levels(varSizes[jvar])); |
| 287 | + |
| 288 | + auto field_view = atlas::array::make_view<double, 2>(tgt_field); |
| 289 | +// field_view.assign(0.0); |
| 290 | + |
| 291 | + for (std::size_t klev=0; klev < varSizes[jvar]; ++klev) { |
| 292 | + for (std::size_t iloc=0; iloc < nlocs_; iloc++) { |
| 293 | +// if (has_mv && mv(field_view(iloc, klev))) { |
| 294 | +//// result[out_idx] = util::missingValue(result[out_idx]); |
| 295 | +// } else { |
| 296 | + oops::Log::debug() << "DJL iloc " << iloc << " klev " << klev << " out_idx " |
| 297 | + << out_idx << " resultin[out_idx] " << resultin[out_idx] << std::endl; |
| 298 | + field_view(iloc, klev) = resultin[out_idx]; |
| 299 | +// } |
| 300 | + ++out_idx; |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + |
| 305 | +// atlas::field::MissingValue mv(inc.incrementFields()[gv_varname]); |
| 306 | +// bool has_mv = static_cast<bool>(mv); |
| 307 | + interpolator_.execute_adjoint(inc.incrementFields()[gv_varname], tgt_field); |
| 308 | + |
| 309 | + } // jvar |
| 310 | + |
| 311 | + // DJL write to file |
| 312 | + |
| 313 | +// boost::uuids::uuid uuid = boost::uuids::random_generator()(); |
| 314 | +// std::shared_ptr<const Geometry> geom = inc.geometry(); |
| 315 | +// std::ostringstream out; |
| 316 | +// out << std::setfill('0') << std::setw(6) << uuid; |
| 317 | +// writeGenFieldsToFile("applyAD"+ boost::uuids::to_string(uuid) +".nc", *geom, inc.validTime(), inc.incrementFields()); |
| 318 | +// fileCounter_++; |
| 319 | + |
| 320 | + oops::Log::trace() << "orcamodel::Interpolator::applyAD done " |
| 321 | + << std::endl; |
| 322 | +} |
| 323 | + |
180 | 324 | void Interpolator::print(std::ostream & os) const {
|
181 | 325 | os << "orcamodel::Interpolator: " << std::endl;
|
182 | 326 | os << " Obs function space " << atlasObsFuncSpace_ << std::endl;
|
|
0 commit comments