Loading [MathJax]/extensions/tex2jax.js
OOPS
All Classes Namespaces Files Functions Variables Typedefs Macros Pages
ObsSpaceQG.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  * (C) Copyright 2017-2019 UCAR.
4  *
5  * This software is licensed under the terms of the Apache Licence Version 2.0
6  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7  * In applying this licence, ECMWF does not waive the privileges and immunities
8  * granted to it by virtue of its status as an intergovernmental organisation nor
9  * does it submit to any jurisdiction.
10  */
11 
12 #include "model/ObsSpaceQG.h"
13 
14 #include <map>
15 #include <string>
16 #include <utility>
17 
18 #include "atlas/array.h"
19 #include "atlas/field.h"
20 #include "eckit/config/Configuration.h"
21 #include "eckit/exception/Exceptions.h"
22 #include "eckit/geometry/Sphere.h"
23 
24 #include "oops/util/abor1_cpp.h"
25 #include "oops/util/DateTime.h"
26 #include "oops/util/Duration.h"
27 #include "oops/util/Logger.h"
28 
29 using atlas::array::make_view;
30 
31 namespace qg {
32 // -----------------------------------------------------------------------------
33 // initialization for the static map
34 std::map < std::string, F90odb > ObsSpaceQG::theObsFileRegister_;
36 
37 // -----------------------------------------------------------------------------
38 
39 ObsSpaceQG::ObsSpaceQG(const Parameters_ & params, const eckit::mpi::Comm & comm,
40  const util::DateTime & bgn, const util::DateTime & end,
41  const eckit::mpi::Comm & timeComm)
42  : oops::ObsSpaceBase(params, comm, bgn, end), obsname_(params.obsType),
43  winbgn_(bgn), winend_(end), obsvars_()
44 {
45  typedef std::map< std::string, F90odb >::iterator otiter;
46 
47  eckit::LocalConfiguration fileconf = params.toConfiguration();
48  std::string ofin("-");
49  if (params.obsdatain.value() != boost::none) {
50  ofin = params.obsdatain.value()->obsfile;
51  }
52  std::string ofout("-");
53  if (params.obsdataout.value() != boost::none) {
54  ofout = params.obsdataout.value()->obsfile;
55  if (timeComm.size() > 1) {
56  std::ostringstream ss;
57  ss << "_" << timeComm.rank();
58  std::size_t found = ofout.find_last_of(".");
59  if (found == std::string::npos) found = ofout.length();
60  std::string fileout = ofout.insert(found, ss.str());
61  fileconf.set("obsdataout.obsfile", fileout);
62  }
63  }
64  oops::Log::trace() << "ObsSpaceQG: Obs files are: " << ofin << " and " << ofout << std::endl;
65  std::string ref = ofin + ofout;
66  if (ref == "--") {
67  ABORT("Underspecified observation files.");
68  }
69 
70  ref = ref + bgn.toString() + end.toString();
71  otiter it = theObsFileRegister_.find(ref);
72  if ( it == theObsFileRegister_.end() ) {
73  // Open new file
74  oops::Log::trace() << "ObsSpaceQG::getHelper: " << "Opening " << ref << std::endl;
75  qg_obsdb_setup_f90(key_, fileconf, bgn, end);
77  } else {
78  // File already open
79  oops::Log::trace() << "ObsSpaceQG::getHelper: " << ref << " already opened." << std::endl;
80  key_ = it->second;
81  }
83 
84  // Set variables simulated for different obstypes
85  if (obsname_ == "Stream") obsvars_.push_back("Stream");
86  if (obsname_ == "WSpeed") obsvars_.push_back("WSpeed");
87  if (obsname_ == "Wind") {
88  obsvars_.push_back("Uwind");
89  obsvars_.push_back("Vwind");
90  }
91 
92  // Generate locations etc... if required
93  if (params.generate.value() != boost::none) {
94  const ObsGenerateParameters &gParams = *params.generate.value();
95  const util::Duration first(gParams.begin);
96  const util::DateTime start(winbgn_ + first);
97  const util::Duration freq(gParams.obsPeriod);
98  int nobstimes = 0;
99  util::DateTime now(start);
100  while (now <= winend_) {
101  ++nobstimes;
102  now += freq;
103  }
104  int iobs;
105  qg_obsdb_generate_f90(key_, obsname_.size(), obsname_.c_str(), gParams.toConfiguration(),
106  start, freq, nobstimes, iobs);
107  }
108 }
109 
110 // -----------------------------------------------------------------------------
111 
113 
114 // -----------------------------------------------------------------------------
115 
116 void ObsSpaceQG::save() const {
117  ASSERT(theObsFileCount_ > 0);
119  if (theObsFileCount_ == 0) {
120  theObsFileRegister_.clear();
122  }
123 }
124 
125 // -----------------------------------------------------------------------------
126 
127 void ObsSpaceQG::getdb(const std::string & col, int & keyData) const {
128  qg_obsdb_get_f90(key_, obsname_.size(), obsname_.c_str(), col.size(), col.c_str(), keyData);
129 }
130 
131 // -----------------------------------------------------------------------------
132 
133 void ObsSpaceQG::putdb(const std::string & col, const int & keyData) const {
134  qg_obsdb_put_f90(key_, obsname_.size(), obsname_.c_str(), col.size(), col.c_str(), keyData);
135 }
136 
137 // -----------------------------------------------------------------------------
138 
139 std::unique_ptr<LocationsQG> ObsSpaceQG::locations() const {
140  atlas::FieldSet fields;
141  std::vector<util::DateTime> times;
142  qg_obsdb_locations_f90(key_, obsname_.size(), obsname_.c_str(), fields.get(), times);
143  return std::unique_ptr<LocationsQG>(new LocationsQG(fields, std::move(times)));
144 }
145 
146 // -----------------------------------------------------------------------------
147 
148 int ObsSpaceQG::nobs() const {
149  int iobs;
150  qg_obsdb_nobs_f90(key_, obsname_.size(), obsname_.c_str(), iobs);
151  return iobs;
152 }
153 // -----------------------------------------------------------------------------
154 
155 // -----------------------------------------------------------------------------
157  return ObsIteratorQG(*this->locations(), 0);
158 }
159 // -----------------------------------------------------------------------------
161  return ObsIteratorQG(*this->locations(), this->nobs());
162 }
163 // -----------------------------------------------------------------------------
164 
165 void ObsSpaceQG::print(std::ostream & os) const {
166  os << "ObsSpace for " << obsname_ << ", " << this->nobs() << " obs";
167 }
168 
169 // -----------------------------------------------------------------------------
170 
171 } // namespace qg
void push_back(const std::string &)
LocationsQG class to handle locations for QG model.
Definition: LocationsQG.h:36
Options controlling generation of artificial observations.
Definition: ObsSpaceQG.h:48
oops::RequiredParameter< util::Duration > obsPeriod
Definition: ObsSpaceQG.h:53
oops::RequiredParameter< util::Duration > begin
Definition: ObsSpaceQG.h:52
Iterator over all observations.
Definition: ObsIteratorQG.h:28
void print(std::ostream &) const
Definition: ObsSpaceQG.cc:165
oops::Variables obsvars_
Definition: ObsSpaceQG.h:125
const std::string obsname_
Definition: ObsSpaceQG.h:122
const util::DateTime winend_
Definition: ObsSpaceQG.h:124
const util::DateTime winbgn_
Definition: ObsSpaceQG.h:123
void getdb(const std::string &, int &) const
read data or metadata
Definition: ObsSpaceQG.cc:127
static int theObsFileCount_
Definition: ObsSpaceQG.h:129
std::unique_ptr< LocationsQG > locations() const
create locations for the whole time window
Definition: ObsSpaceQG.cc:139
ObsIteratorQG end() const
iterator to the observation past-the-last
Definition: ObsSpaceQG.cc:160
void putdb(const std::string &, const int &) const
save data or metadata
Definition: ObsSpaceQG.cc:133
void save() const
save and close file
Definition: ObsSpaceQG.cc:116
ObsIteratorQG begin() const
iterator to the first observation
Definition: ObsSpaceQG.cc:156
ObsSpaceQG(const Parameters_ &, const eckit::mpi::Comm &, const util::DateTime &, const util::DateTime &, const eckit::mpi::Comm &)
create full ObsSpace (read or generate data)
Definition: ObsSpaceQG.cc:39
static std::map< std::string, F90odb > theObsFileRegister_
Definition: ObsSpaceQG.h:128
int nobs() const
return number of observations (unique locations)
Definition: ObsSpaceQG.cc:148
Configuration parameters for the QG model's ObsSpace.
Definition: ObsSpaceQG.h:61
oops::OptionalParameter< ObsDataParameters > obsdataout
File to which to save observations and analysis.
Definition: ObsSpaceQG.h:70
oops::OptionalParameter< ObsDataParameters > obsdatain
File from which to load observations.
Definition: ObsSpaceQG.h:68
oops::OptionalParameter< ObsGenerateParameters > generate
Options controlling generation of artificial observations.
Definition: ObsSpaceQG.h:72
The namespace for the main oops code.
The namespace for the qg model.
void qg_obsdb_put_f90(const F90odb &, const int &, const char *, const int &, const char *, const F90ovec &)
void qg_obsdb_get_f90(const F90odb &, const int &, const char *, const int &, const char *, const F90ovec &)
void qg_obsdb_delete_f90(F90odb &)
void qg_obsdb_setup_f90(F90odb &, const eckit::Configuration &, const util::DateTime &, const util::DateTime &)
void qg_obsdb_generate_f90(const F90odb &, const int &, const char *, const eckit::Configuration &, const util::DateTime &, const util::Duration &, const int &, int &)
void qg_obsdb_nobs_f90(const F90odb &, const int &, const char *, int &)
void qg_obsdb_locations_f90(const F90odb &, const int &, const char *, atlas::field::FieldSetImpl *, std::vector< util::DateTime > &)