IODA Bundle
DatetimeVariable.cpp
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 NOAA/NWS/NCEP/EMC
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 <iostream>
9 #include <iomanip>
10 #include <ostream>
11 #include <vector>
12 
13 #include "eckit/exception/Exceptions.h"
14 
15 #include "DatetimeVariable.h"
16 
17 
18 namespace
19 {
20  namespace ConfKeys
21  {
22  const char* Year = "year";
23  const char* Month = "month";
24  const char* Day = "day";
25  const char* Hour = "hour";
26  const char* Minute = "minute";
27  const char* Second = "second";
28  const char* HoursFromUtc = "hoursFromUtc";
29  const char* Utc = "isUTC"; // deprecated
30  } // namespace ConfKeys
31 } // namespace
32 
33 
34 namespace Ingester
35 {
36  DatetimeVariable::DatetimeVariable(const eckit::Configuration& conf) :
37  yearKey_(conf.getString(ConfKeys::Year)),
38  monthKey_(conf.getString(ConfKeys::Month)),
39  dayKey_(conf.getString(ConfKeys::Day)),
40  hourKey_(conf.getString(ConfKeys::Hour)),
41  minuteKey_(conf.getString(ConfKeys::Minute)),
42  secondKey_(""),
43  hoursFromUtc_(0)
44  {
45  if (conf.has(ConfKeys::Second))
46  {
47  secondKey_ = conf.getString(ConfKeys::Second);
48  }
49 
50  if (conf.has(ConfKeys::HoursFromUtc))
51  {
53  }
54 
55  if (conf.has(ConfKeys::Utc))
56  {
57  std::cout << "WARNING: usage of " \
58  << ConfKeys::Utc \
59  << " in datetime is depricated!" \
60  << std::endl;
61  std::cout << "Use the optional parameter " << ConfKeys::HoursFromUtc << " instead.";
62  }
63  }
64 
65  std::shared_ptr<DataObject> DatetimeVariable::exportData(const BufrDataMap& map)
66  {
67  checkKeys(map);
68 
69  auto datetimes = std::vector<std::string>();
70 
71  datetimes.reserve(map.at(yearKey_).size());
72  for (unsigned int idx = 0; idx < map.at(yearKey_).size(); idx++)
73  {
74  // YYYY-MM-DDThh:mm:ssZ
75  std::ostringstream datetimeStr;
76  datetimeStr << std::setfill('0')
77  << std::setw(4) << map.at(yearKey_)(idx) << "-" \
78  << std::setw(2) << map.at(monthKey_)(idx) << "-" \
79  << std::setw(2) << map.at(dayKey_)(idx) << "T" \
80  << std::setw(2) << map.at(hourKey_)(idx) - hoursFromUtc_ << ":" \
81  << std::setw(2) << map.at(minuteKey_)(idx) << ":";
82 
83  if (!secondKey_.empty())
84  {
85  datetimeStr << std::setw(2) << map.at(secondKey_)(idx);
86  }
87  else
88  {
89  datetimeStr << std::setw(2) << 0;
90  }
91 
92  datetimeStr << "Z";
93 
94  datetimes.push_back(datetimeStr.str());
95  }
96 
97  return std::make_shared<StrVecDataObject>(datetimes);
98  }
99 
101  {
102  std::vector<std::string> requiredKeys = {yearKey_,
103  monthKey_,
104  dayKey_,
105  hourKey_,
106  minuteKey_};
107 
108  if (!secondKey_.empty())
109  {
110  requiredKeys.push_back(secondKey_);
111  }
112 
113  std::stringstream errStr;
114  errStr << "Mnemonic ";
115 
116  bool isKeyMissing = false;
117  for (auto key : requiredKeys)
118  {
119  if (map.find(key) == map.end())
120  {
121  isKeyMissing = true;
122  errStr << key;
123  break;
124  }
125  }
126 
127  errStr << " could not be found during export of datetime object.";
128 
129  if (isKeyMissing)
130  {
131  throw eckit::BadParameter(errStr.str());
132  }
133  }
134 } // namespace Ingester
const std::string monthKey_
Mnemonic for month.
int hoursFromUtc_
Hours to offset from UTC (optional)
DatetimeVariable(const eckit::Configuration &conf)
const std::string hourKey_
Mnemonic for hour.
std::string secondKey_
Mnemonic for second (optional)
const std::string yearKey_
Mnemonic for year.
std::shared_ptr< DataObject > exportData(const BufrDataMap &map) final
Get the configured mnemonics and turn them into datetime strings.
const std::string dayKey_
Mnemonic for day.
void checkKeys(const BufrDataMap &map)
makes sure the bufr data map has all the required keys.
const std::string minuteKey_
Mnemonic for minute.
IngesterArrayMap BufrDataMap
Definition: BufrTypes.h:21