-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSources.cpp
More file actions
303 lines (255 loc) · 10.4 KB
/
Copy pathSources.cpp
File metadata and controls
303 lines (255 loc) · 10.4 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
//
// Created by Raffaele Montella on 07/12/20.
//
#include "Sources.hpp"
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
#include <iomanip>
#include "JulianDate.hpp"
// for convenience
using json = nlohmann::json;
Sources::Sources() {
// Logger configuration
logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("WaComM"));
LOG4CPLUS_DEBUG(logger, "Empty Sources");
}
void Sources::loadFromNamelist(string &fileName) {
LOG4CPLUS_DEBUG(logger, "Reading from namelist:" + fileName);
std::ifstream infile(fileName);
std::string line;
while (std::getline(infile, line))
{
// Trim the line
line = Utils::trim(line," \t\r");
// Select the section parser
if (line == "&ems") {
// s starts with prefix &
namelistParseEms(infile);
}
}
}
void Sources::namelistParseEms(ifstream &infile) {
std::string line;
// Start the input/output section
while (std::getline(infile, line))
{
// Trim the line
line = Utils::trim(line, " \t\r");
// Check for the end of the section
if (line=="/") { break; }
vector<string> keyValues;
Utils::tokenize(line,'=',keyValues);
if (keyValues.size()==2) {
string key=Utils::trim(keyValues.at(0)," \t\r");
if (key == "nsources") {
int nSources = stoi(keyValues.at(1));
for (int idx=0;idx<nSources;idx++) {
this->push_back(Source());
}
} else if (key == "id_source") {
// The list of the input files
vector<string> idSources;
Utils::tokenize(keyValues.at(1),',',idSources);
int idx=0;
for(string idSource: idSources) {
idSource=Utils::trim(idSource," '");
this->at(idx).Id(idSource);
idx++;
}
} else if (key == "i_source") {
// The list of the input files
vector<string> iSources;
Utils::tokenize(keyValues.at(1), ',', iSources);
int idx = 0;
for (string iSource: iSources) {
iSource = Utils::trim(iSource, " '");
this->at(idx).I(stod(iSource));
idx++;
}
} else if (key == "j_source") {
// The list of the input files
vector<string> jSources;
Utils::tokenize(keyValues.at(1), ',', jSources);
int idx = 0;
for (string jSource: jSources) {
jSource = Utils::trim(jSource, " '");
this->at(idx).J(stod(jSource));
idx++;
}
} else if (key == "k_source") {
// The list of the input files
vector<string> kSources;
Utils::tokenize(keyValues.at(1), ',', kSources);
int idx = 0;
for (string kSource: kSources) {
kSource = Utils::trim(kSource, " '");
this->at(idx).K(stod(kSource));
idx++;
}
} else if (key == "nPartsPerHour") {
// The list of the input files
vector<string> nPartsPerHours;
Utils::tokenize(keyValues.at(1), ',', nPartsPerHours);
int idx = 0;
for (string nPartsPerHour: nPartsPerHours) {
nPartsPerHour = Utils::trim(nPartsPerHour, " '");
this->at(idx).ParticlesPerHour(stoi(nPartsPerHour));
idx++;
}
} else if (key == "mode") {
// The list of the input files
vector<string> modes;
Utils::tokenize(keyValues.at(1), ',', modes);
int idx = 0;
for (string mode: modes) {
mode = Utils::trim(mode, " '");
this->at(idx).Mode(stoi(mode));
idx++;
}
} else if (key == "source_start") {
// The list of the input files
vector<string> sourceStarts;
Utils::tokenize(keyValues.at(1), ',', sourceStarts);
int idx = 0;
for (string sourceStart: sourceStarts) {
sourceStart = Utils::trim(sourceStart, " '");
this->at(idx).Start(stof(sourceStart));
idx++;
}
} else if (key == "source_end") {
// The list of the input files
vector<string> sourceEnds;
Utils::tokenize(keyValues.at(1), ',', sourceEnds);
int idx = 0;
for (string sourceEnd: sourceEnds) {
sourceEnd = Utils::trim(sourceEnd, " '");
this->at(idx).End(stof(sourceEnd));
idx++;
}
}
}
}
}
void Sources::saveAsJson(string &fileName, shared_ptr<OceanModelAdapter> oceanModelAdapter)
{
json features = json::array();
for (auto source:*this) {
if (source.J() < 0 || source.I() < 0 || source.K() > 0) {
continue;
}
json coordinates = json::array();
double dep, lat, lon;
oceanModelAdapter->kji2deplatlon(source.K(), source.J(), source.I(), dep, lat, lon);
if (dep == 1e37 || lat == 1e37 || lon == 1e37) {
continue;
}
coordinates.push_back(lon);
coordinates.push_back(lat);
Calendar calStart, calEnd;
JulianDate::fromModJulian(source.Start(), calStart);
JulianDate::fromModJulian(source.End(), calEnd);
json properties = {
{ "id", source.Id()},
{ "k", source.K()},
{ "j", source.J()},
{ "i", source.I()},
{ "start", calStart.asNCEPdate()},
{ "end", calEnd.asNCEPdate()},
{ "particlesPerHour", source.ParticlesPerHour()},
{ "mode", source.Mode()},
{ "depth", dep}
};
json geometry = {
{ "type", "Point"},
{ "coordinates", coordinates }
};
json feature = {
{"type", "Feature"},
{"properties", properties},
{"geometry", geometry}
};
features.push_back(feature);
}
json featureCollection = {
{"type", "FeatureCollection"},
{"features", features}
};
// write prettified JSON to another file
std::ofstream o(fileName);
o << std::setw(4) << featureCollection << std::endl;
}
Sources::~Sources() {
}
void Sources::loadFromJson(string &fileName, shared_ptr<OceanModelAdapter> oceanModelAdapter) {
LOG4CPLUS_INFO(logger, "Reading from json:" << fileName);
std::ifstream infile(fileName);
try {
json featureCollection;
infile >> featureCollection;
if (featureCollection.contains("features") && featureCollection["features"].is_array()) {
int count = 1;
for (auto feature:featureCollection["features"]) {
double k = 0, j = 1e37, i = 1e37;
double startOceanTime = -1, endOceanTime = -1;
double dep = 0;
int mode = 1, particlesPerHour = 100;
string id = "source_" + to_string(count);
if (feature.contains("properties")) {
auto properties = feature["properties"];
if (properties.contains("id")) { id = properties["id"]; }
if (properties.contains("start")) {
if (properties["start"] < 0){
startOceanTime = -1;
}
else{
Calendar cal(properties["start"]);
startOceanTime = JulianDate::toModJulian(cal);
}
}
if (properties.contains("end")) {
if (properties["end"] < 0){
endOceanTime = -1;
}
else{
Calendar cal(properties["end"]);
endOceanTime = JulianDate::toModJulian(cal);
}
}
if (properties.contains("k")) { k = atof(to_string(properties["k"]).c_str()); }
if (properties.contains("j")) { j = atof(to_string(properties["j"]).c_str()); }
if (properties.contains("i")) { i = atof(to_string(properties["i"]).c_str()); }
if (properties.contains("dep")) { dep = atof(to_string(properties["dep"]).c_str()); }
if (properties.contains("particlesPerHour")) {
particlesPerHour = stoi(to_string(properties["particlesPerHour"]));
}
}
// convert lat/lon and depth in k,j,i
if (j == 1e37 || i == 1e37) {
double lat = 1e37, lon = 1e37;
if (feature.contains("geometry")) {
auto geometry = feature["geometry"];
if (geometry.contains("type") && geometry["type"] == "Point") {
if (geometry.contains("coordinates") && geometry["coordinates"].is_array()) {
auto coordinates = geometry["coordinates"];
if (coordinates.size() >= 2) {
lon = coordinates[0];
lat = coordinates[1];
if (coordinates.size() == 3 && dep == 0) {
dep = coordinates[2];
}
oceanModelAdapter->deplatlon2kji(dep, lat, lon, k, j, i);
//cout << "k:" << k << " j:" << j << " i:" << i;
}
}
}
}
}
this->push_back(Source(id, k, j, i, startOceanTime, endOceanTime, particlesPerHour, mode));
count++;
}
}
} catch (const nlohmann::json::parse_error& e) {
LOG4CPLUS_ERROR(logger,e.what());
}
}