UFO
LapseRate.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 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 #include <fstream>
8 #include <iterator>
9 #include <string>
10 #include <vector>
11 
13 
14 #include "ioda/ObsSpace.h"
15 
16 #include "oops/base/Variables.h"
17 #include "oops/util/abor1_cpp.h"
18 #include "oops/util/Logger.h"
19 
20 #include "ufo/GeoVaLs.h"
21 #include "ufo/ObsDiagnostics.h"
22 
23 namespace ufo {
24 
26 
27 // -----------------------------------------------------------------------------
28 
29 LapseRate::LapseRate(const Parameters_ & parameters, const oops::Variables & vars)
30  : PredictorBase(parameters, vars),
31  order_(parameters.order.value().value_or(1))
32 {
33  if (parameters.order.value() != boost::none) {
34  // override the predictor name to distinguish between lapse_rate predictors of different orders
35  name() = name() + "_order_" + std::to_string(order_);
36  }
37 
38  // required variables
39  geovars_ += oops::Variables({"air_temperature",
40  "air_pressure",
41  "average_surface_temperature_within_field_of_view"});
42  if (vars.size() > 0) {
43  hdiags_ += oops::Variables({"transmittances_of_atmosphere_layer"}, vars.channels());
44  } else {
45  oops::Log::error() << "Channels size is ZERO !" << std::endl;
46  ABORT("Channels size is ZERO !");
47  }
48 
49  // This is a very preliminary method, please revisit
50  // more flexibilites are needed
51  const std::string & tlapse_file = parameters.tlapse;
52  std::ifstream infile(tlapse_file);
53  std::string nusis; // sensor/instrument/satellite
54  int nuchan; // channel number
55  float tlapse;
56 
57  if (infile.is_open()) {
58  while (!infile.eof()) {
59  infile >> nusis;
60  infile >> nuchan;
61  infile >> tlapse;
62  tlapmean_[nuchan] = tlapse;
63  }
64  infile.close();
65  } else {
66  oops::Log::error() << "Unable to open file : "
67  << tlapse_file << std::endl;
68  ABORT("Unable to open tlapse file ");
69  }
70 }
71 
72 // -----------------------------------------------------------------------------
73 
74 void LapseRate::compute(const ioda::ObsSpace & odb,
75  const GeoVaLs & geovals,
76  const ObsDiagnostics & ydiags,
77  ioda::ObsVector & out) const {
78  const std::size_t nvars = out.nvars();
79  const std::size_t nlocs = out.nlocs();
80 
81  // common vectors storage
82  std::vector <float> pred(nlocs, 0.0);
83 
84  // retrieve the average surface temperature
85  std::vector<float> tsavg5(nlocs, 0.0);
86  geovals.get(tsavg5, "average_surface_temperature_within_field_of_view");
87 
88  // Retrieve the transmittances_of_atmosphere_layer from Hdiag
89  std::vector<std::vector<std::vector<float>>> ptau5;
90  std::vector<std::vector<float>> tmpvar;
91 
92  std::string hdiags;
93  for (std::size_t jvar = 0; jvar < nvars; ++jvar) {
94  hdiags = "transmittances_of_atmosphere_layer_" + std::to_string(vars_.channels()[jvar]);
95  tmpvar.clear();
96  for (std::size_t js = 0; js < ydiags.nlevs(hdiags); ++js) {
97  ydiags.get(pred, hdiags, js);
98  tmpvar.push_back(pred);
99  }
100  ptau5.push_back(tmpvar);
101  }
102 
103  // Retrieve the temperature
104  std::vector<std::vector<float>> tvp;
105  std::size_t nlevs = geovals.nlevs("air_temperature");
106  for (std::size_t js = 0; js < nlevs; ++js) {
107  geovals.getAtLevel(pred, "air_temperature", js);
108  tvp.push_back(pred);
109  }
110  nlevs = geovals.nlevs("air_pressure");
111  float tlapchn;
112 
113  // sort out the tlapmean based on vars
114  std::vector<float> tlap;
115  for (std::size_t jvar = 0; jvar < nvars; ++jvar) {
116  auto it = tlapmean_.find(vars_.channels()[jvar]);
117  if (it != tlapmean_.end()) {
118  tlap.push_back(it->second);
119  } else {
120  oops::Log::error() << "Could not locate tlapemean for channel: " <<
121  vars_.channels()[jvar] << std::endl;
122  ABORT("Could not locate tlapemean value");
123  }
124  }
125 
126  for (std::size_t jloc = 0; jloc < nlocs; ++jloc) {
127  for (std::size_t jvar = 0; jvar < nvars; ++jvar) {
128  tlapchn = (ptau5[jvar][nlevs-2][jloc]-ptau5[jvar][nlevs-1][jloc])*
129  (tsavg5[jloc]-tvp[nlevs-2][jloc]);
130  for (std::size_t k = 1; k < nlevs-1; ++k) {
131  tlapchn = tlapchn+(ptau5[jvar][nlevs-k-2][jloc]-ptau5[jvar][nlevs-k-1][jloc])*
132  (tvp[nlevs-k][jloc]-tvp[nlevs-k-2][jloc]);
133  }
134  out[jloc*nvars+jvar] = pow((tlapchn - tlap[jvar]), order_);
135  }
136  }
137 }
138 
139 // -----------------------------------------------------------------------------
140 
141 } // namespace ufo
GeoVaLs: geophysical values at locations.
size_t nlevs(const std::string &var) const
Return number of levels for a specified variable.
Definition: GeoVaLs.cc:310
void getAtLevel(std::vector< double > &, const std::string &, const int) const
Get GeoVaLs at a specified level.
Definition: GeoVaLs.cc:330
void get(std::vector< double > &, const std::string &var) const
Get 2D GeoVaLs for variable var (fails for 3D GeoVaLs)
Definition: GeoVaLs.cc:359
std::map< int, float > tlapmean_
Definition: LapseRate.h:63
LapseRate(const Parameters_ &, const oops::Variables &)
Definition: LapseRate.cc:29
void compute(const ioda::ObsSpace &, const GeoVaLs &, const ObsDiagnostics &, ioda::ObsVector &) const override
compute the predictor
Definition: LapseRate.cc:74
Configuration parameters of the LapseRate predictor.
Definition: LapseRate.h:33
oops::OptionalParameter< int > order
Definition: LapseRate.h:44
oops::RequiredParameter< std::string > tlapse
Path to an input file.
Definition: LapseRate.h:38
void get(std::vector< float > &, const std::string &) const
size_t nlevs(const std::string &) const
std::string & name()
predictor name
Definition: PredictorBase.h:80
oops::Variables vars_
variables that will be bias-corrected using this predictor
Definition: PredictorBase.h:84
oops::Variables geovars_
required GeoVaLs
Definition: PredictorBase.h:85
oops::Variables hdiags_
required ObsDiagnostics
Definition: PredictorBase.h:86
integer function nlocs(this)
Return the number of observational locations in this Locations object.
Definition: RunCRTM.h:27
static PredictorMaker< LapseRate > makerFuncLapseRate_("lapse_rate")