UFO
Locations.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2020 UCAR
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 "ufo/Locations.h"
9 
10 #include <utility>
11 #include <vector>
12 
13 #include "eckit/config/Configuration.h"
14 #include "eckit/exception/Exceptions.h"
15 
16 #include "ioda/ObsSpace.h"
17 
18 #include "oops/mpi/mpi.h"
19 #include "oops/util/DateTime.h"
20 #include "oops/util/Logger.h"
21 
22 namespace ufo {
23 
24 // -------------------------------------------------------------------------------------------------
25 
26 Locations::Locations(const std::vector<float> & lons, const std::vector<float> & lats,
27  const std::vector<util::DateTime> & times,
28  std::shared_ptr<const ioda::Distribution> dist)
29  : dist_(std::move(dist)), lons_(lons), lats_(lats), times_(times) {
30  ASSERT(lons_.size() == lats_.size());
31  ASSERT(lats_.size() == times_.size());
32  oops::Log::trace() << "ufo::Locations::Locations done" << std::endl;
33 }
34 
35 // -------------------------------------------------------------------------------------------------
36 /*! UFO Locations Constructor with Configuration
37  *
38  * \details This constructor can be used to generate user-specified
39  * and/or random locations for use with interpolation or other tests
40  *
41  * To generate random locations, the relevant parameters specified in
42  * **StateTest.Locations** section of the config file are:
43  *
44  * * **lats** user-specified latitudes (degrees)
45  * * **lons** user-specified longitudes (degrees)
46  * * **Nrandom** number of random locations desired
47  * * **random_seed** (optional) random seed for reproducibility of results
48  *
49  * \date May, 2018 Created (M. Miesch, JCSDA)
50  *
51  * \sa ufo::ufo_locs_create() ufo::ufo_loc_test() test::testStateInterpolation()
52  *
53  */
54 
55 Locations::Locations(const eckit::Configuration & conf,
56  const eckit::mpi::Comm & comm) {
57  const eckit::LocalConfiguration obsconf(conf, "obs space");
58  const util::DateTime bgn = util::DateTime(conf.getString("window begin"));
59  const util::DateTime end = util::DateTime(conf.getString("window end"));
60  ioda::ObsTopLevelParameters obsparams;
61  obsparams.validateAndDeserialize(obsconf);
62 
63  ioda::ObsSpace obspace(obsparams, comm, bgn, end, oops::mpi::myself());
64  const size_t nlocs = obspace.nlocs();
65  dist_ = obspace.distribution();
66 
67  lats_.resize(nlocs);
68  lons_.resize(nlocs);
69  times_.resize(nlocs);
70 
71  obspace.get_db("MetaData", "latitude", lats_);
72  obspace.get_db("MetaData", "longitude", lons_);
73  obspace.get_db("MetaData", "datetime", times_);
74 }
75 
76 // -------------------------------------------------------------------------------------------------
78  lons_.insert(lons_.end(), other.lons_.begin(), other.lons_.end());
79  lats_.insert(lats_.end(), other.lats_.begin(), other.lats_.end());
80  times_.insert(times_.end(), other.times_.begin(), other.times_.end());
81  return *this;
82 }
83 
84 // -------------------------------------------------------------------------------------------------
85 std::vector<bool> Locations::isInTimeWindow(const util::DateTime & t1,
86  const util::DateTime & t2) const {
87  std::vector<bool> isIn(times_.size(), false);
88  for (size_t ii = 0; ii < times_.size(); ++ii) {
89  if (t1 < times_[ii] && times_[ii] <= t2) isIn[ii] = true;
90  }
91  return isIn;
92 }
93 
94 // -------------------------------------------------------------------------------------------------
95 
96 size_t Locations::size() const {
97  return lats_.size();
98 }
99 
100 // -------------------------------------------------------------------------------------------------
101 
102 void Locations::print(std::ostream & os) const {
103  os << "Lat/lon/time locations: " << size() << " locations on this task " << std::endl;
104 }
105 
106 // -------------------------------------------------------------------------------------------------
107 
108 } // namespace ufo
Locations class to handle simple lat-lon-time locations.
size_t size() const
size of locations
Definition: Locations.cc:96
std::shared_ptr< const ioda::Distribution > dist_
std::vector< bool > isInTimeWindow(const util::DateTime &t1, const util::DateTime &t2) const
find which observations are in the (t1, t2] time window
Definition: Locations.cc:85
Locations & operator+=(const Locations &)
append locations with more locations
Definition: Locations.cc:77
std::vector< float > lats_
longitudes on current MPI task
std::vector< util::DateTime > times_
latitudes on current MPI task
Locations(const std::vector< float > &lons, const std::vector< float > &lats, const std::vector< util::DateTime > &times, std::shared_ptr< const ioda::Distribution >)
constructor from passed lons, lats, times
Definition: Locations.cc:26
void print(std::ostream &os) const override
Definition: Locations.cc:102
std::vector< float > lons_
observations MPI distribution
integer function nlocs(this)
Return the number of observational locations in this Locations object.
Definition: RunCRTM.h:27