-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathst2csv.cpp
More file actions
357 lines (314 loc) · 11 KB
/
Copy pathst2csv.cpp
File metadata and controls
357 lines (314 loc) · 11 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
// test_rtcm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#define MAXFIELD 100
#define PI 3.1415926535897932 /* pi */
#define D2R (PI/180.0) /* deg to rad */
#define R2D (180.0/PI) /* rad to deg */
static int parse_fields(char* const buffer, char** val, char key)
{
char* p, * q;
int n = 0;
/* parse fields */
for (p = buffer; *p && n < MAXFIELD; p = q + 1) {
if (p == NULL) break;
if ((q = strchr(p, key)) || (q = strchr(p, '\n')) || (q = strchr(p, '\r'))) {
val[n++] = p; *q = '\0';
}
else break;
}
return n;
}
static void set_output_file_name(const char* fname, const char* key, char* outfname)
{
char filename[255] = { 0 }, outfilename[255] = { 0 };
strcpy(filename, fname);
char* temp = strrchr(filename, '.');
if (temp) temp[0] = '\0';
sprintf(outfname, "%s-%s", filename, key);
}
static FILE* set_output_file(const char* fname, const char* key)
{
char filename[255] = { 0 };
set_output_file_name(fname, key, filename);
return fopen(filename, "w");
}
struct mount_t
{
std::string name;
double lat;
double lon;
int freq;
std::string sys;
std::string rcv;
std::string country;
std::string antenna;
};
static void read_st(const char* fname, std::map<std::string, mount_t>& mMount)
{
FILE* fLOG = fopen(fname, "rt");
if (fLOG ==NULL) return;
FILE* fOUT = set_output_file(fname, "-out.json");
char buffer[255] = { 0 };
char* val[MAXFIELD];
int line_index = 0;
int count = 0;
while (fLOG && !feof(fLOG))
{
fgets(buffer, sizeof(buffer), fLOG);
if (!(buffer[0] == 'S' && buffer[1] == 'T' && buffer[2] == 'R')) continue;
int num = parse_fields(buffer, val, ';');
if (num < 14) continue;
std::string name = std::string(val[1]);
int freq = atoi(val[5]);
std::string sys = std::string(val[6]);
std::string country = std::string(val[8]);
double lat = atof(val[9]);
double lon = atof(val[10]);
std::string rcv = std::string(val[13]);
mount_t mount;
mount.freq = freq;
mount.country = country;
mount.sys = sys;
mount.rcv = rcv;
mount.lat = lat;
mount.lon = lon;
mount.name = name;
mMount[name] = mount;
if (fOUT)
{
if (count == 0)
{
fprintf(fOUT, "[");
}
else
{
fprintf(fOUT, ",");
}
std::string new_name;
if (name.length() > 4)
new_name = name.substr(name.length() - 4);
else
{
for (int i = name.length(); i < 4; ++i)
new_name += "0";
new_name += name;
}
transform(new_name.begin(), new_name.end(), new_name.begin(), ::toupper);
std::string new_country;
if (country.length() > 3)
new_country = country.substr(country.length() - 3);
else
{
for (int i = country.length(); i < 3; ++i)
new_country += "0";
new_country += country;
}
fprintf(fOUT, "{\"no\":%i,\"host\":\"ntrip.info\",\"port\":\"2101\",\"mountpoint\": \"%s\",\"username\": \"user\",\"password\": \"password\",\"name\": \"00%s000%s\",\"type\": \"http\"}", count, name.c_str(), new_country.c_str(), new_name.c_str());
}
++count;
}
if (fLOG) fclose(fLOG);
if (fOUT)
{
if (count > 0)
{
fprintf(fOUT, "]");
}
fclose(fOUT);
}
return;
}
int read_csv(const char* fname, std::map<std::string, mount_t>& mMount)
{
FILE* fLOG = fopen(fname, "rt");
if (fLOG == NULL) return 0;
FILE* fOUT = set_output_file(fname, "-out.json");
char buffer[255] = { 0 };
char* val[MAXFIELD];
int line_index = 0;
int count = 0;
while (fLOG && !feof(fLOG) && fgets(buffer, sizeof(buffer), fLOG))
{
++line_index;
if (line_index < 2) continue;
int num = parse_fields(buffer, val, ',');
if (num < 2) continue;
std::string country = std::string(val[0]);
std::string name = std::string(val[1]);
double lat = (num > 2) ? atof(val[2]) : 0;
double lon = (num > 3) ? atof(val[3]) : 0;
mount_t mount;
mount.lat = lat;
mount.lon = lon;
mount.name = name;
mount.country = country;
if (num > 4) mount.rcv = std::string(val[4]);
//if (num > 5) mount.antenna = std::string(val[5]);
mMount[name] = mount;
if (fOUT)
{
std::string new_name;
if (name.length() > 4)
new_name = name.substr(name.length() - 4);
else
{
for (int i = name.length(); i < 4; ++i)
new_name += "0";
new_name += name;
}
transform(new_name.begin(), new_name.end(), new_name.begin(), ::toupper);
std::string new_country;
if (country.length() > 3)
new_country = country.substr(country.length() - 3);
else
{
for (int i = country.length(); i < 3; ++i)
new_country += "0";
new_country += country;
}
fprintf(fOUT, "{\"no\":%i,\"host\":\"ntrip.info\",\"port\":\"2101\",\"mountpoint\": \"%s\",\"username\": \"user\",\"password\": \"password\",\"name\": \"00%s000%s\,\"type\": \"http\"},", count, name.c_str(), new_country.c_str(), new_name.c_str());
}
++count;
}
if (fLOG) fclose(fLOG);
if (fOUT)
{
if (count > 0)
{
fprintf(fOUT, "]");
}
fclose(fOUT);
}
return (int)mMount.size();
}
static double lat2local(double lat, double* lat2north)
{
double f_WGS84 = (1.0 / 298.257223563);
double e2WGS84 = (2.0 * f_WGS84 - f_WGS84 * f_WGS84);
double slat = sin(lat);
double clat = cos(lat);
double one_e2_slat2 = 1.0 - e2WGS84 * slat * slat;
double Rn = 6378137.0 / sqrt(one_e2_slat2);
double Rm = Rn * (1.0 - e2WGS84) / (one_e2_slat2);
*lat2north = Rm;
return Rn * clat;
}
int diff_csv(const char* fname1, const char* fname2)
{
std::map<std::string, mount_t> mMount1;
std::map<std::string, mount_t> mMount2;
read_csv(fname1, mMount1);
read_csv(fname2, mMount2);
for (std::map<std::string, mount_t>::iterator pM1 = mMount1.begin(); pM1 != mMount1.end(); ++pM1)
{
if (fabs(pM1->second.lat) < 0.001 && fabs(pM1->second.lon) < 0.001) continue;
double bestD = -1;
double l2n = 0;
double l2e = lat2local(pM1->second.lat * 3.1415926 / 180.0, &l2n);
std::string bestM;
for (std::map<std::string, mount_t>::iterator pM2 = mMount2.begin(); pM2 != mMount2.end(); ++pM2)
{
if (fabs(pM2->second.lat) < 0.001 && fabs(pM2->second.lon) < 0.001) continue;
double dB = pM1->second.lat - pM2->second.lat;
double dL = pM1->second.lon - pM2->second.lon;
double dN = dB * 3.1415926 / 180.0 * l2n;
double dE = dL * 3.1415926 / 180.0 * l2e;
double currD = sqrt(dN * dN + dE * dE);
if (bestD < 0 || currD < bestD)
{
bestD = currD;
bestM = pM2->first;
}
}
if (bestD < 0)
{
}
else
{
std::string name;
if (pM1->first.length() > 4)
name = pM1->first.substr(pM1->first.length() - 4);
else
{
for (int i = pM1->first.length(); i < 4; ++i)
name += "0";
name += pM1->first;
}
transform(name.begin(), name.end(), name.begin(),::toupper);
std::string country;
if (pM1->second.country.length() > 3)
country = pM1->second.country.substr(pM1->second.country.length() - 3);
else
{
for (int i = pM1->second.country.length(); i < 3; ++i)
country += "0";
country += pM1->second.country;
}
printf("%s,%s,%7.2f,%7.2f,%s,%7.2f,%s,name=00%s000%s&mountpoint=%s'\n", pM1->second.country.c_str(), pM1->first.c_str(), pM1->second.lat, pM1->second.lon, pM1->second.rcv.c_str(), bestD / 1000.0, bestM.c_str(), country.c_str(), name.c_str(), pM1->first.c_str());
}
}
return 0;
}
int exclude_csv(const char* fname1, const char* fname2)
{
std::map<std::string, mount_t> mMount1;
std::map<std::string, mount_t> mMount2;
read_csv(fname1, mMount1);
read_csv(fname2, mMount2);
FILE* fOUT = set_output_file(fname1, "-exclude.csv");
for (std::map<std::string, mount_t>::iterator pM1 = mMount1.begin(); pM1 != mMount1.end(); ++pM1)
{
std::map<std::string, mount_t>::iterator pM2 = mMount2.find(pM1->first);
if (fOUT) fprintf(fOUT,"%s,%8.3f,%8.3f,%s,%s,%s,%c\n", pM1->first.c_str(), pM1->second.lat, pM1->second.lon, pM1->second.country.c_str(), pM1->second.antenna.c_str(), pM1->second.rcv.c_str(), pM2 == mMount2.end() ? ' ' : '*');
}
if (fOUT) fclose(fOUT);
return 0;
}
int main(int argc, const char* argv[])
{
if (argc > 2)
{
if (argc > 3 && strstr(argv[1], "diffcsv"))
{
diff_csv(argv[2], argv[3]);
}
else if (argc > 3 && strstr(argv[1], "exclude"))
{
exclude_csv(argv[2], argv[3]);
}
else if (argc > 3 && strstr(argv[1], "diffblh"))
{
double blh1[3] = { atof(argv[2]) * D2R, atof(argv[3]) * D2R, atof(argv[4]) };
double blh2[3] = { atof(argv[5]) * D2R, atof(argv[6]) * D2R, atof(argv[7]) };
double l2n = 0;
double l2e = lat2local(blh1[0], &l2n);
double dB = blh2[0] - blh1[0];
double dL = blh2[1] - blh1[1];
double dN = dB * l2n;
double dE = dL * l2e;
double dU = blh2[2] - blh1[2];
printf("%10.4f,%10.4f,%10.4f\n", dN, dE, dU);
}
}
else if (argc > 1)
{
std::map<std::string, mount_t> mMount;
read_st(argv[1], mMount);
if (mMount.size() > 0)
{
FILE* fOUT1 = set_output_file(argv[1], "-st.csv");
if (fOUT1) fprintf(fOUT1, "country,name,lat,lon,receiver type,constellation\n");
for (std::map<std::string, mount_t>::iterator pMount = mMount.begin(); pMount != mMount.end(); ++pMount)
{
if (fOUT1) fprintf(fOUT1, "%s,%s,%8.4f,%8.4f,%s,%s\n", pMount->second.country.c_str(), pMount->first.c_str(), pMount->second.lat, pMount->second.lon, pMount->second.rcv.c_str(), pMount->second.sys.c_str());
}
if (fOUT1) fclose(fOUT1);
}
}
return 0;
}