-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathContourLibrary.cc
More file actions
359 lines (293 loc) · 10.9 KB
/
ContourLibrary.cc
File metadata and controls
359 lines (293 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* (C) Copyright 1996-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
/*! \file ContourLibrary.cc
\brief Implementation of the Template class ContourLibrary.
Magics Team - ECMWF 2010
Started: Fri 16-Jul-2010
Changes:
*/
#include "ContourLibrary.h"
#include "Layer.h"
#include "MetaData.h"
#include "Value.h"
#include "VisDefInfo.h"
using namespace magics;
VisDefInfoBase* ContourLibrary::info_ = 0;
ContourLibrary::ContourLibrary() {}
ContourLibrary::~ContourLibrary() {}
/*!
Class information are given to the output-stream.
*/
void ContourLibrary::print(ostream& out) const {
out << "ContourLibrary[";
out << "]";
}
// sete meta["shortName"] =the meta dat to be collected
void ContourLibrary::askId(MetaDataCollector& meta) {
meta["observationDiagnostic"] = "";
}
bool ContourLibrary::checkId(MetaDataCollector& metaId, MetaDataCollector& metaKey) {
// Obstat
if (metaId["observationDiagnostic"] != "") {
if (!setInfoObject("ObstatGrib")) {
return false;
}
}
else {
return false;
}
for (unsigned int i = 0; i < info_->keys().size(); i++) {
metaKey[info_->keys().at(i)] = "";
if (metaId["observationDiagnostic"] != "") {
MetaDataAttribute attr;
attr.setType(MetaDataAttribute::NumberType);
metaKey.setAttribute(info_->keys().at(i), attr);
}
}
return true;
}
// se the map to set the contour!
void ContourLibrary::getStyle(MetaDataCollector& meta, MagDef& attributes, StyleEntry&) {
MagLog::dev() << "ContourLibrary::set-->" << endl;
// Obstat
if (info_) {
for (map<string, string>::iterator it = meta.begin(); it != meta.end(); it++) {
MagLog::dev() << it->first << "--> " << it->second << endl;
}
info_->getAttributes(meta, attributes);
}
}
bool ContourLibrary::setInfoObject(string type) {
if (info_ && info_->type() != type) {
delete info_;
info_ = 0;
}
if (!info_) {
info_ = VisDefInfoFactory::makeItem(type);
}
if (!info_)
return false;
else if (info_->isLoaded())
return true;
else
return false;
}
#include "MagConfig.h"
void EcChartData::callback(const string& name, const Value& value) {
int iname = atoi(name.c_str());
ValueMap object = value.get_value<ValueMap>();
data_.insert(make_pair(iname, map<string, string>()));
for (auto entry = object.begin(); entry != object.end(); ++entry) {
data_[iname].insert(make_pair(entry->first, convert(entry->second)));
}
}
map<string, string> EcChartData::getMap(const int key) {
return data_[key];
}
void EcChartSetData::callback(const string& name, const Value& value) {
ValueList values = value.get_value<ValueList>();
data_.insert(make_pair(name, vector<int>()));
for (unsigned int i = 0; i < values.size(); i++) {
data_[name].push_back(int(values[i]));
}
}
bool EcChartSetData::hasKey(const string& key) {
return data_.find(key) != data_.end();
}
vector<int> EcChartSetData::getSet(const string& key) {
return data_[key];
}
EcChartLibrary::EcChartLibrary() : contours_("contours"), default_set_("default") {
keys_.push_back("paramId");
keys_.push_back("units");
keys_.push_back("typeOfLevel");
keys_.push_back("level");
keys_.push_back("marsClass");
keys_.push_back("marsType");
keys_.push_back("marsStream");
for (int i = 0; i < keys_.size(); i++) {
index_.insert(make_pair(keys_[i], EcChartSetData(keys_[i])));
}
}
EcChartLibrary::~EcChartLibrary() {}
void EcChartLibrary::setCriteria(MetaDataCollector& request, const string& criteria) {
request[criteria] = "";
MetaDataAttribute attribute;
attribute.setSource(MetaDataAttribute::GribApiSource);
request.setAttribute(criteria, attribute);
}
void EcChartLibrary::askId(MetaDataCollector& request) {
// main keywords
setCriteria(request, "paramId");
setCriteria(request, "typeOfLevel");
setCriteria(request, "level");
setCriteria(request, "units");
// auxiliary keywords
setCriteria(request, "stepRange");
setCriteria(request, "number");
setCriteria(request, "marsClass");
setCriteria(request, "marsType");
setCriteria(request, "marsStream");
}
// se the map to set the contour!
void EcChartLibrary::getStyle(MetaDataCollector& data, MagDef& contour, StyleEntry&) {
// find the best contour definition
vector<int>::iterator it;
vector<int> result_set;
map<string, EcChartSetData>::iterator ikey;
// initial result set is units's contours (OBLIGATORY)
ikey = index_.find("units");
// if (data["units"]=="" || !ikey->second.hasKey(data["units"])) return;
result_set = vector<int>(ikey->second.getSet(data["units"]));
// interset with paramId's contours (OPTIONAL)
if (result_set.size() > 1) {
ikey = index_.find("paramId");
if (data["paramId"] != "" && ikey->second.hasKey(data["paramId"])) {
vector<int> first_set = vector<int>(result_set);
vector<int> second_set = vector<int>(ikey->second.getSet(data["paramId"]));
it = std ::set_intersection(first_set.begin(), first_set.end(), second_set.begin(), second_set.end(),
result_set.begin());
result_set.resize(it - result_set.begin());
if (result_set.size() == 0)
result_set = first_set;
}
}
// interset with eccharts layers's default contours (OPTIONAL)
if (result_set.size() > 1 && default_set_.hasKey("default")) {
vector<int> first_set = vector<int>(result_set);
vector<int> second_set = vector<int>(default_set_.getSet("default"));
it = std ::set_intersection(first_set.begin(), first_set.end(), second_set.begin(), second_set.end(),
result_set.begin());
result_set.resize(it - result_set.begin());
if (result_set.size() == 0)
result_set = first_set;
}
// for each GRIB key (excluding paramId and units that have just been checked, OPTIONAL)
for (int i = 2; i < keys_.size() && result_set.size() > 1; i++) {
// result set is intersection with GRIB key contours
string key = keys_[i];
ikey = index_.find(key);
if (data[key] != "" && ikey->second.hasKey(data[key])) {
vector<int> first_set = vector<int>(result_set);
vector<int> second_set = vector<int>(ikey->second.getSet(data[key]));
it = std ::set_intersection(first_set.begin(), first_set.end(), second_set.begin(), second_set.end(),
result_set.begin());
result_set.resize(it - result_set.begin());
// if intersection is empty, restore previous result set
if (result_set.size() == 0)
result_set = first_set;
}
}
// Is there at least one contour left?
if (result_set.size() > 0) {
// set the contour parameters
map<string, string> cont = contours_.getMap(result_set[0]);
for (map<string, string>::const_iterator i = cont.begin(); i != cont.end(); ++i) {
contour[i->first] = i->second;
MagLog::debug() << " EcChartLibrary::getAttributes contour[" << i->first << "]= " << i->second << endl;
}
}
else
MagLog::info() << " EcChartLibrary::getAttributes: NO CONTOUR MATCHED!" << endl;
}
void EcChartLibrary::print(ostream&) const {}
StyleLibrary* WebLibrary::styles_ = 0;
WebLibrary::WebLibrary() {
if (!styles_) {
styles_ = new StyleLibrary(library_path_);
}
}
WebLibrary::~WebLibrary() {}
// set the meta data to be collected
void WebLibrary::askId(MetaDataCollector& request) {
// main keywords
std::set<string> criteria;
criteria.insert("units");
criteria.insert("parameterUnits"); // for grib
styles_->getCriteria(criteria);
for (auto c = criteria.begin(); c != criteria.end(); ++c) {
setCriteria(request, *c);
// cout << " asking for " << *c << endl;
}
}
void WebLibrary::setCriteria(MetaDataCollector& request, const string& criteria) {
request[criteria] = "";
MetaDataAttribute attribute;
attribute.setSource(MetaDataAttribute::GribApiSource);
request.setAttribute(criteria, attribute);
}
// set the map to set the contour!
void WebLibrary::getStyle(MetaDataCollector& data, MagDef& contour, StyleEntry& info) {
MagDef style;
if (styles_->findStyle(data, style, info)) {
contour = style;
}
else {
styles_->findStyle("default", contour);
// for (auto s = contour.begin(); s != contour.end(); ++s)
// cout << s->first << "--->" << s->second << endl;
}
}
void WebLibrary::getStyle(const string& name, MagDef& info) {
styles_->findStyle(name, info);
}
// set the map to set the contour!
void WebLibrary::getScaling(MetaDataCollector& data, double& scaling, double& offset) {
MagDef values;
StyleEntry info;
scaling = 1;
offset = 0;
static bool debug;
static bool first = true;
if ( first ) {
first = false;
debug = (getEnvVariable("MAGICS_STYLES_DEBUG") != "");
}
// cout << "SCALING" << endl;
vector<string> keywords = {"preferred_units", "prefered_units"};
auto unit = data.find("units");
if (unit == data.end())
unit = data.find("parameterUnits");
if (unit == data.end())
return;
if (debug) cout << " Found Unit " << unit->second << endl;
bool found = styles_->findStyle(data, values, info);
if (!found) {
MagLog::debug() << "Cannot find style" << endl;
return;
}
if (debug) {
cout << " TRYING to scale " << unit->second << endl;
for (auto x = values.begin(); x != values.end(); ++x)
cout << x->first << "--->" << x->second << endl;
}
MagDef::iterator need;
for (auto key = keywords.begin(); key != keywords.end(); ++key) {
need = values.find(*key);
if (need != values.end())
break;
}
if (need == values.end())
return;
UnitsLibrary converter;
string whitespaces(" \t\f\v\n\r");
string clean = unit->second;
std::size_t pos = clean.find_last_not_of(whitespaces);
if (pos != std::string::npos) {
// cout << "clean" << pos << endl;
clean = clean.substr(0, pos + 1);
}
// str is all whitespace
// cout << "CLEAN " << clean << ": " << clean.size() << endl;
converter.find(need->second, clean, scaling, offset);
MagLog::debug() << "Need " << need->second << " get " << unit->second << "--->APPLY " << scaling << " and "
<< offset << endl;
}
void WebLibrary::print(ostream&) const {}