AMolecularSQLManager.cc
Go to the documentation of this file.
1 
9 #include <sstream>
10 #include <boost/lexical_cast.hpp>
11 
12 #include <atm/AMolecularSQLManager.h>
13 #include <fwk/CentralConfig.h>
14 #include <det/Detector.h>
15 #include <utl/ErrorLogger.h>
16 #include <utl/TimeStamp.h>
17 #include <utl/Reader.h>
18 #include <utl/AugerException.h>
19 #include <utl/StringCompare.h>
20 
21 using namespace std;
22 using namespace atm;
23 using namespace det;
24 using namespace utl;
25 
26 
27 REGISTER_A_MANAGER("AMolecularSQLManager", AMolecularSQLManager);
28 
29 
31 AMolecularSQLManager::GetDBResFundamental(const string& tableName,
32  const string& columnName,
33  const IndexMap& componentIndex)
34  const
35 {
36  const utl::TimeStamp detTime = Detector::GetInstance().GetTime();
37 
38  ostringstream query;
39 
40  // aerosol table is a special case, as it requires checking software version
41  if (tableName == "molecular") {
42 
43  // for the molecular table, the profile_type must be specified
44  //
45  if (componentIndex.find("profile_type_id") == componentIndex.end()) {
46  ERROR("Request for data in molecular table does not specify the profile_type_id.");
47  throw DataNotFoundInDBException("molecular", "profile_type_id", componentIndex,
48  boost::lexical_cast<string>(Detector::GetInstance().GetTime()));
49  }
50 
51  // Use the fast query for GDAS and weather station date. This query DOES
52  // work for all profile types in the NEW database Atm_Molecular_1_A, but NOT
53  // for the monthly models in the OLD database Atm_Molecular_0_A.
54 
55  const string profileTypeName = componentIndex.find("profile_type_name")->second;
56  if (StringEquivalent(profileTypeName, "weather station")) {
57 
58  if (StringEquivalent(fDatabaseSoftwareVersion, "default")) {
59  // do NOT check software version
60  query
61  << "SELECT molecular_id "
62  "FROM molecular "
63  "WHERE start_time = "
64  "(SELECT MAX(start_time) FROM molecular "
65  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
66  "AND start_time <= \"" << detTime.GetGPSSecond() << "\") "
67  "AND profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
68  "ORDER BY last_modified DESC LIMIT 1";
69  } else {
70  // DO check software version
71  query
72  << "SELECT molecular_id "
73  "FROM molecular, software "
74  "WHERE start_time = "
75  "(SELECT MAX(start_time) FROM molecular "
76  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
77  "AND molecular.software_id = software.software_id "
78  "AND software.software_version = \"" << fDatabaseSoftwareVersion << "\" "
79  "AND start_time <= \"" << detTime.GetGPSSecond() << "\") "
80  "AND profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
81  "ORDER BY molecular.last_modified DESC LIMIT 1";
82  }
83 
84  } else if (StringEquivalent(profileTypeName, "Malargue GDAS data")) {
85 
86  if (StringEquivalent(fDatabaseSoftwareVersion, "default")) {
87  // do NOT check software version
88  query
89  << "SELECT molecular_id "
90  "FROM molecular "
91  "WHERE start_time = "
92  "(SELECT MAX(start_time) FROM molecular "
93  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
94  "AND start_time <= \"" << detTime.GetGPSSecond() << "\" "
95  "AND end_time >= \"" << detTime.GetGPSSecond() << "\") "
96  "AND profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
97  "ORDER BY last_modified DESC LIMIT 1";
98  } else {
99  // DO check software version
100  query
101  << "SELECT molecular_id "
102  "FROM molecular, software "
103  "WHERE start_time = "
104  "(SELECT MAX(start_time) FROM molecular "
105  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
106  "AND molecular.software_id = software.software_id "
107  "AND software.software_version = \"" << fDatabaseSoftwareVersion << "\" "
108  "AND start_time <= \"" << detTime.GetGPSSecond() << "\" "
109  "AND end_time >= \"" << detTime.GetGPSSecond() << "\") "
110  "AND profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
111  "ORDER BY molecular.last_modified DESC LIMIT 1";
112  }
113 
114  } else {
115 
116  if (StringEquivalent(fDatabaseSoftwareVersion, "default")) {
117  // do NOT check software version
118  query
119  << "SELECT molecular_id "
120  "FROM molecular "
121  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
122  "AND start_time <= \"" << detTime.GetGPSSecond() << "\" "
123  "AND (end_time > \"" << detTime.GetGPSSecond() << "\" OR end_time IS NULL) "
124  "ORDER BY last_modified DESC, start_time DESC LIMIT 1";
125  } else {
126  // DO check software version
127  query
128  << "SELECT molecular_id "
129  "FROM molecular, software "
130  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
131  "AND start_time <= \"" << detTime.GetGPSSecond() << "\" "
132  "AND (end_time > \"" << detTime.GetGPSSecond() << "\" OR end_time IS NULL) "
133  "AND molecular.software_id = software.software_id "
134  "AND software.software_version = \"" << fDatabaseSoftwareVersion << "\" "
135  "ORDER BY molecular.last_modified DESC, start_time DESC LIMIT 1";
136  }
137 
138  } // end check profile name
139 
140  } else if (tableName == "zone") {
141 
142  // special case of column id -- this has to be fetched from the molecular_zone table
143  if (columnName == "zone_id") {
144  query
145  << "SELECT " << columnName << " "
146  "FROM molecular_zone "
147  "WHERE molecular_id = " << componentIndex.find("molecular_id")->second;
148  } else {
149  query
150  << "SELECT " << columnName << " "
151  "FROM zone "
152  "WHERE zone_id = " << componentIndex.find("zone_id")->second;
153  }
154 
155  } else {
156 
157  // issue queries in form :
158  // SELECT columnName FROM tableName WHERE
159  // componentIndex[0].first = componentIndex[0].second AND
160  // componentIndex[1].first = componentIndex[1].second
161  // (etc)
162  //
163  query
164  << "SELECT " << columnName << " "
165  "FROM " << tableName << " "
166  "WHERE " << MergeIndexMap(componentIndex);
167 
168  }
169 
170  return Query(query, QueryInfoMessage(tableName, columnName));
171 }
172 
173 
175 AMolecularSQLManager::GetDBResVector(const string& tableName,
176  const string& columnName,
177  const IndexMap& componentIndex)
178  const
179 {
180 
181  const utl::TimeStamp detTime = Detector::GetInstance().GetTime();
182 
183  // special case: get all zones valid for a particular molecular_id
184  //
185  if (tableName == "zone") {
186 
187  // index map must specify the molecular_id
188  if (componentIndex.find("molecular_id") == componentIndex.end()) {
189  ERROR("no molecular_id specified when looking up zone names");
190  throw DataNotFoundInDBException("zone", "molecular_id", componentIndex,
191  boost::lexical_cast<string>(Detector::GetInstance().GetTime()));
192  }
193 
194  ostringstream query;
195  query
196  << "SELECT zone." << columnName << " FROM zone, molecular_zone "
197  "WHERE " << MergeIndexMap(componentIndex) << " "
198  "AND molecular_zone.zone_id = zone.zone_id";
199 
200  return Query(query, string("vector from ") + QueryInfoMessage(tableName, columnName));
201 
202  } else if (tableName == "molecular") {
203 
204  // special case (hack): get all the zones for this timestamp. This is done in rather a
205  // hackish way, since the molecular db is not filled properly (ie. the zone tables
206  // are not used in the way they were originally intended).
207 
208  ostringstream query;
209 
210  if (StringEquivalent(fDatabaseSoftwareVersion, "default")) {
211  // do NOT check software version
212  query
213  << "SELECT molecular_id "
214  "FROM molecular "
215  "WHERE start_time = "
216  "(SELECT MAX(start_time) FROM molecular "
217  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
218  "AND start_time <= \"" << detTime.GetGPSSecond() << "\") "
219  "AND profile_type_id = \"" << componentIndex.find("profile_type_id")->second << '"';
220  } else {
221  // DO check software version
222  query
223  << "SELECT molecular_id "
224  "FROM molecular, software "
225  "WHERE start_time = (SELECT MAX(start_time) FROM molecular "
226  "WHERE profile_type_id = \"" << componentIndex.find("profile_type_id")->second << "\" "
227  "AND molecular.software_id = software.software_id "
228  "AND software.software_version = \"" << fDatabaseSoftwareVersion << "\" "
229  "AND start_time <= \"" << detTime.GetGPSSecond() << "\") "
230  "AND profile_type_id = \"" << componentIndex.find("profile_type_id")->second << '"';
231  }
232 
233  return Query(query, string("vector from ") + QueryInfoMessage(tableName, columnName));
234 
235  } else {
236 
237  ostringstream query;
238  query
239  << "SELECT " << columnName << " "
240  "FROM " << tableName << " "
241  "WHERE " << MergeIndexMap(componentIndex);
242 
243  return Query(query, string("vector from ") + QueryInfoMessage(tableName, columnName));
244 
245  }
246 }
247 
248 
250 AMolecularSQLManager::GetDBDataCheckTranspose(vector<double>& returnData,
251  const string& tableName,
252  const string& columnName,
253  const IndexMap& componentIndex)
254  const
255 {
256  const unsigned int n = FindComponent<unsigned int>("transpose", 0, componentIndex);
257  if (!n)
258  return VSQLManager::GetDBData(returnData, tableName, columnName, componentIndex);
259 
260  IndexMap newComponentIndex = componentIndex;
261  newComponentIndex.erase("transpose");
262 
263  ostringstream query;
264  query
265  << "SELECT " << columnName << " "
266  "FROM " << tableName << " "
267  "WHERE " << MergeIndexMap(newComponentIndex);
268 
269  if (Query(query, string("transposed vector from ") + QueryInfoMessage(tableName, columnName)) != eFound)
270  return eNotFound;
271 
272  return FetchRow(returnData);
273 }
Manager for molecular portion of atmospheric monitoring database.
bool StringEquivalent(const std::string &a, const std::string &b, Predicate p)
Utility to compare strings for equivalence. It takes a predicate to determine the equivalence of indi...
Definition: StringCompare.h:38
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
Exception to use in case requested data not found in the database with detailed printout.
#define REGISTER_A_MANAGER(_name_, _Type_)
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.