Skip to content

Commit ab0ba4d

Browse files
Modify export_csv to select data to load
Refactor code
1 parent bd23410 commit ab0ba4d

2 files changed

Lines changed: 77 additions & 111 deletions

File tree

include/gsql_gsm/igc_operators.h

Lines changed: 68 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include <curl/curl.h>
1212
#include <gsql_gsm/gsm_inmemory_db.h>
1313
#include <gsql_gsm/json_operators.h>
14-
14+
#include <yaucl/strings/serializers.h>
15+
#include <unordered_map>
1516
inline double to_degrees(double radians)
1617
{
1718
return radians * (180.0 / M_PI);
@@ -102,8 +103,6 @@ int calculate_lift(gsm_inmemory_db &db, int &bFixesIterator, int &iterator)
102103
int previousId;
103104
int previousAltitude = '\0';
104105
bool newLiftSeries = true;
105-
//TODO:delete
106-
int counter = 0;
107106
for(auto &bFix : db.O[bFixesIterator].phi["b_fix"])
108107
{
109108
int altitude;
@@ -137,7 +136,6 @@ int calculate_lift(gsm_inmemory_db &db, int &bFixesIterator, int &iterator)
137136
scoresLift.emplace_back(1.0);
138137
tablePhiLift.emplace_back(bFix.id);
139138
scoresLift.emplace_back(1.0);
140-
counter++;
141139
create_fast(db, ++iterator, {"lift"}, {std::to_string(diffAltitude)}, {scoresLift},
142140
{{"b_fix", {tablePhiLift}}});
143141
tablePhiLiftSeries.emplace_back(iterator);
@@ -157,7 +155,6 @@ int calculate_lift(gsm_inmemory_db &db, int &bFixesIterator, int &iterator)
157155
previousId = bFix.id;
158156
}
159157
create_fast(db, ++iterator, {"lifts"}, {}, {scoresLifts}, {{"lift_series", {tablePhiLifts}}});
160-
std::cout << "count of lifts added:" << counter << '\n';
161158
return iterator;
162159
}
163160

@@ -266,15 +263,15 @@ int vcweather_load(gsm_inmemory_db &db, int &iterator, double lat, double lon, s
266263
std::string geoHashString = geohash_encode(lat, lon);
267264
int result;
268265
std::string fileName = "_" + geoHashString + "_" + std::to_string(start) + ".json";
269-
std::string vcPath = "/home/neo/gsm_gsql/json_files/vc" + fileName;
266+
std::string vcPath = "json_files/vc" + fileName;
270267
if(std::filesystem::exists(vcPath))
271268
{
272-
std::string path = "/home/neo/gsm_gsql/json_files/vc" + fileName;
269+
std::string path = "json_files/vc" + fileName;
273270
result = load_jsonEllXiFile(db, path, iterator, {"currentConditions"}, "weather_vc");
274271
}
275272
else
276273
{
277-
std::ifstream fileAnother("/home/neo/gsm_gsql/api_keys/visualcrossing.txt");
274+
std::ifstream fileAnother("api_keys/visualcrossing.txt");
278275
std::string visualCrossingApiKey;
279276
fileAnother >> visualCrossingApiKey;
280277

@@ -288,7 +285,7 @@ int vcweather_load(gsm_inmemory_db &db, int &iterator, double lat, double lon, s
288285
std::string url = vc.str();
289286
std::string weatherString = weather_curl(url);
290287

291-
std::ofstream vcFile("/home/neo/gsm_gsql/json_files/vc" + fileName);
288+
std::ofstream vcFile("json_files/vc" + fileName);
292289
vcFile << weatherString;
293290
result = load_jsonEllXiData(db, weatherString, iterator, {"currentConditions"}, "weather_vc");
294291
}
@@ -300,25 +297,25 @@ std::vector<int> weather_load(gsm_inmemory_db &db, int &iterator, double lat, do
300297
std::string geoHashString = geohash_encode(lat, lon);
301298
std::vector<int> result;
302299
std::string fileName = "_" + geoHashString + "_" + std::to_string(start) + ".json";
303-
std::string owPath = "/home/neo/gsm_gsql/json_files/ow" + fileName;
304-
std::string vcPath = "/home/neo/gsm_gsql/json_files/vc" + fileName;
300+
std::string owPath = "json_files/ow" + fileName;
301+
std::string vcPath = "json_files/vc" + fileName;
305302
if(std::filesystem::exists(owPath) || std::filesystem::exists(vcPath))
306303
{
307-
std::string path = "/home/neo/gsm_gsql/json_files/ow" + fileName;
304+
std::string path = "json_files/ow" + fileName;
308305
result.push_back(iterator);
309306
load_jsonfile(db, path, iterator, "0", "weather_ow");
310307

311-
path = "/home/neo/gsm_gsql/json_files/vc" + fileName;
308+
path = "json_files/vc" + fileName;
312309
result.push_back(iterator);
313310
load_jsonfile(db, path, iterator, "currentConditions", "weather_vc");
314311
}
315312
else
316313
{
317-
std::ifstream file("/home/neo/gsm_gsql/api_keys/openweather.txt");
314+
std::ifstream file("api_keys/openweather.txt");
318315
std::string openWeatherApiKey;
319316
file >> openWeatherApiKey;
320317

321-
std::ifstream fileAnother("/home/neo/gsm_gsql/api_keys/visualcrossing.txt");
318+
std::ifstream fileAnother("api_keys/visualcrossing.txt");
322319
std::string visualCrossingApiKey;
323320
fileAnother >> visualCrossingApiKey;
324321

@@ -342,7 +339,7 @@ std::vector<int> weather_load(gsm_inmemory_db &db, int &iterator, double lat, do
342339
std::string weatherString = weather_curl(url);
343340
std::cout << weatherString << std::endl;
344341

345-
std::ofstream owFile("/home/neo/gsm_gsql/json_files/ow" + fileName);
342+
std::ofstream owFile("json_files/ow" + fileName);
346343
owFile << weatherString;
347344
result.push_back(iterator);
348345
load_jsondata(db, weatherString, iterator, "weather_ow", "0");
@@ -352,7 +349,7 @@ std::vector<int> weather_load(gsm_inmemory_db &db, int &iterator, double lat, do
352349
weatherString = weather_curl(url);
353350
std::cout << weatherString << std::endl;
354351

355-
std::ofstream vcFile("/home/neo/gsm_gsql/json_files/vc" + fileName);
352+
std::ofstream vcFile("json_files/vc" + fileName);
356353
vcFile << weatherString;
357354
result.push_back(iterator);
358355
load_jsondata(db, weatherString, iterator, "weather_vc", "currentConditions");
@@ -411,127 +408,99 @@ int generate_weatherbucket(gsm_inmemory_db& db, int bFixesIterator, int& iterato
411408
return ghIterator;
412409
}
413410

414-
/*
415-
* OW FILE NAME: ow_{geohash}_{unixtime}.json
416-
* WHAT TO EXTRACT:
417-
* json -> list -> 0 -> main -> [temp,..]
418-
* -> wind -> [speed,...]
419-
* -> clouds -> [all]
420-
* 1. in json
421-
*
422-
*
423-
* VC FILE NAME: vc_{geohash}_{unixtime}.json
424-
* WHAT TO EXTRACT:
425-
* everything from currentConditions
426-
* json -> currentConditions -> [temp,wind,...]
427-
*/
428-
429-
//VC
430-
431-
void export_csv(gsm_inmemory_db& db, int &iterator, int geoHashesIterator, int liftsIterator, gsm_db_indices& idx, std::string csvFileName)
411+
void export_csv(gsm_inmemory_db& db,
412+
int &iterator,
413+
int geoHashesIterator,
414+
int liftsIterator,
415+
gsm_db_indices& idx,
416+
std::string csvFileName,
417+
const std::set<std::string>& left = {},
418+
const std::set<std::string>& right = {},
419+
const std::set<std::string>& additional = {})
432420
{
433-
std::string headers;
434421
bool getHeaders = true;
435-
std::string fn = "/home/neo/gsm_gsql/csv_files/" + csvFileName;
422+
std::string fn = "csv_files/" + csvFileName;
436423
std::ofstream fileOut(fn);
437-
//TODO:delete
438-
int counter = 0;
439-
int sumOfBfix = 0;
440-
int sumOfLiftSizes = 0;
441-
std::vector<std::string> ignoreIgc = {}; // "latitude_double", "longitude_double", "unix_time"
424+
std::unordered_map<std::string, std::string> row;
425+
std::vector<std::string> header{left.begin(), left.end()};
426+
header.insert(header.end(), right.begin(), right.end());
427+
header.insert(header.end(), additional.begin(), additional.end());
442428

443429
for(auto& [geoHashString, geoHashVector] : db.O[geoHashesIterator].phi)
444430
{
445-
std::string weatherLine = "";
446431
std::vector<int> bFixes;
447432

448433
for(auto& geoHash : geoHashVector)
449434
{
450435
size_t weather = db.O[geoHash.id].phi["weather"].at(0).id;
451-
if(getHeaders)
452-
{
453-
headers += db.O[weather].ell[0];
454-
headers += ',';
455-
}
456-
weatherLine += std::to_string(weather);
457-
weatherLine += ',';
436+
if (left.contains(db.O[weather].ell[0]))
437+
row[db.O[weather].ell[0]] = std::to_string(weather);
458438

459439
for(int i = 1; i < db.O[weather].ell.size(); i++)
460440
{
461-
weatherLine += db.O[weather].xi[i];
462-
weatherLine += ',';
463441
if(getHeaders)
464442
{
465-
headers += db.O[weather].ell[i];
466-
headers += ',';
443+
if (left.contains(db.O[weather].ell[i]))
444+
row[db.O[weather].ell[i]] = (db.O[weather].xi[i]);
467445
}
468446
}
469447

470448
for(auto& bFix : db.O[geoHash.id].phi["b_fix"])
471449
{
472450
bool isInLift = false;
473-
sumOfBfix++;
474-
451+
bool isBeginLift = false;
475452
for(auto& lift_series : db.O[liftsIterator].phi["lift_series"])
476453
{
477-
//TODO:delete
478-
size_t dst = idx.containedBy.addUniqueStateOrGetExisting(db.O[lift_series.id].phi["lift"][0].id);
479-
for(auto const& it : idx.containedBy.outgoingEdgesById2(idx.containedBy.addUniqueStateOrGetExisting(bFix.id)))
480-
{
481-
if(it.second == dst)
454+
size_t count = 0;
455+
for (auto& liftPart : db.O[lift_series.id].phi["lift"]) {
456+
size_t dst = idx.containedBy.addUniqueStateOrGetExisting(liftPart.id);
457+
for(auto const& it : idx.containedBy.outgoingEdgesById2(idx.containedBy.addUniqueStateOrGetExisting(bFix.id)))
482458
{
483-
//TODO:delete
484-
counter++;
485-
isInLift = true;
459+
if(it.second == dst)
460+
{
461+
isInLift = true;
462+
if(count == 0)
463+
isBeginLift = true;
464+
}
465+
if(isInLift)
466+
break;
486467
}
468+
if(isInLift)
469+
break;
470+
count++;
487471
}
488472
if(isInLift)
489473
break;
490474
}
491-
492-
std::string csvLine = weatherLine;
493475
for(int i = 1; i < db.O[bFix.id].ell.size(); i++)
494476
{
495-
if(std::find(ignoreIgc.begin(), ignoreIgc.end(), db.O[bFix.id].ell[i]) != ignoreIgc.end())
496-
continue;
497-
csvLine += db.O[bFix.id].xi[i];
498-
csvLine += ',';
499-
if(getHeaders)
500-
{
501-
headers += db.O[bFix.id].ell[i];
502-
headers += ',';
503-
}
477+
if (right.contains(db.O[bFix.id].ell[i]))
478+
row[db.O[bFix.id].ell[i]] =(db.O[bFix.id].xi[i]);
504479
}
505-
506-
headers += "lift";
507-
csvLine += (isInLift ? '1' : '0');
508-
480+
row["lift"] = (isInLift ? "1" : "0");
481+
row["isbeginlift"] = (isBeginLift ? "1" : "0");
509482
if(getHeaders)
510483
{
511-
fileOut << headers << '\n';
484+
for (auto it = header.begin(), en = header.end(); it != en; )
485+
{
486+
fileOut << *it;
487+
it++;
488+
if (it != en)
489+
fileOut << ",";
490+
}
491+
fileOut << '\n';
512492
getHeaders = false;
513493
}
514-
fileOut << csvLine << '\n';
515-
}
516-
}
517-
}
518-
//TODO:delete
519-
std::cout << "counter_export_csv:" << counter << '\n';
520-
std::cout << "sum of bfix in geohash:" << sumOfBfix << '\n';
521-
int liftInLiftSeries = 0;
522-
std::set<size_t> bfigzIdset;
523-
for(auto &lifto_series : db.O[liftsIterator].phi["lift_series"])
524-
{
525-
for(auto& lifto : db.O[lifto_series.id].phi["lift"])
526-
{
527-
liftInLiftSeries++;
528-
for(auto& bfigz : db.O[lifto.id].phi["b_fix"])
529-
{
530-
bfigzIdset.insert(bfigz.id);
494+
for (auto it = header.begin(), en = header.end(); it != en; )
495+
{
496+
fileOut << row[*it];
497+
it++;
498+
if (it != en)
499+
fileOut << ",";
500+
}
501+
fileOut << '\n';
531502
}
532503
}
533504
}
534-
std::cout << "lift in lift series count:" << liftInLiftSeries << '\n';
535-
std::cout << "unique bfix.id in each lift object:" << bfigzIdset.size() << '\n';
536505
}
537506
#endif //GSM_GSQL_IGC_OPERATORS_H

main.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
#include "submodules/yaucl/submodules/rapidcsv/src/rapidcsv.h"
1313
#include <nlohmann/json.hpp>
1414
#include <filesystem>
15-
#include <curl/curl.h>
16-
1715
#include <chrono>
18-
#include "sys/types.h"
19-
#include "sys/sysinfo.h"
16+
2017

2118
using json = nlohmann::json;
2219

@@ -211,11 +208,9 @@ int main() {
211208
// global iterator keeping track of gsm ids
212209
int iterator = 0;
213210

214-
std::string csvPath = "/home/neo/gsm_gsql/csv_files/";
215-
std::string jsonPath = "/home/neo/gsm_gsql/json_files/vc_6dk65ff_1673719200.json";
216-
std::string igcPath = "/home/neo/gsm_gsql/igc_files/10k.igc";
217-
218-
//load_jsonEllXiFile(db, jsonPath, iterator, {"currentConditions"}, "weather");
211+
std::string csvPath = "csv_files/";
212+
std::string jsonPath = "json_files/vc_6dk65ff_1673719200.json";
213+
std::string igcPath = "igc_files/long2.igc";
219214

220215
int bFixesIterator = load_igcfile(db, igcPath, iterator);
221216
int liftsIterator = calculate_lift(db, bFixesIterator, iterator);
@@ -243,10 +238,12 @@ int main() {
243238
idx.valid_data();
244239

245240
std::string csvOutputFileName = "lift10k.csv";
246-
export_csv(db, iterator, geoHashesIterator, liftsIterator, idx, csvOutputFileName);
241+
export_csv(db, iterator, geoHashesIterator, liftsIterator, idx, csvOutputFileName,
242+
{"cloudcover", "dew", "feelslike", "humidity", "precip", "precipprob", "preciptype", "pressure", "severerisk", "snow", "snowdepth", "solarenergy", "solarradiation", "temp", "uvindex", "visibility", "weather_vc", "winddir", "windgust", "windspeed"},
243+
{"bearing_rate", "latitude_double", "longitude_double", "pressure_altitude", "unix_time"},
244+
{"lift","isbeginlift"});
247245

248246
// Dumping the db into a XML format
249-
dump_to_xml(db, idx, "/home/neo/gsm_gsql/question_mark.xml");
250-
std::cout << "Hello, World!" << std::endl;
247+
dump_to_xml(db, idx, "question_mark.xml");
251248
return 0;
252249
}

0 commit comments

Comments
 (0)