-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADFParser.c
350 lines (311 loc) · 12.6 KB
/
ADFParser.c
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
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "ADFParser.h"
enum ADFPatternGainUnit stringToGainUnit(const char* str) {
if(strncmp(str, "DBI", 3)) {
return ADF_PATTERN_GAIN_UNIT_DBI;
}
else if(strncmp(str, "DBD", 3)) {
return ADF_PATTERN_GAIN_UNIT_DBD;
}
else if(strncmp(str, "DBR", 3)) {
return ADF_PATTERN_GAIN_UNIT_DBR;
}
else if(strncmp(str, "LIN", 3)) {
return ADF_PATTERN_GAIN_UNIT_LIN;
}
else {
printf("Unrecognized gain unit, defaulting to dBi\n");
return ADF_PATTERN_GAIN_UNIT_DBI;
}
}
enum ADFPatternCutType stringToCutType(const char* str) {
if(str[0] == 'H') {
return ADF_PATTERN_CUT_HORIZONTAL;
}
else if(str[0] == 'V') {
return ADF_PATTERN_CUT_VERTICAL;
}
else if(str[0] == 'A' && str[1] == 'Z') {
return ADF_PATTERN_CUT_AZIMUTH;
}
else if(str[0] == 'E' && str[1] == 'L') {
return ADF_PATTERN_CUT_ELEVATION;
}
else {
return ADF_PATTERN_CUT_PHI;
}
}
enum ADFPatternPolarizationType stringToPolarizationType(const char* str) {
if(strncmp(str, "H/H", 3)) {
return ADF_PATTERN_POLARIZATION_HORIZONTAL_HORIZONTAL;
}
else if(strncmp(str, "H/V", 3)) {
return ADF_PATTERN_POLARIZATION_HORIZONTAL_VERTICAL;
}
else if(strncmp(str, "V/V", 3)) {
return ADF_PATTERN_POLARIZATION_VERTICAL_VERTICAL;
}
else if(strncmp(str, "V/H", 3)) {
return ADF_PATTERN_POLARIZATION_VERTICAL_HORIZONTAL;
}
else if(strncmp(str, "SLR", 3)) {
return ADF_PATTERN_POLARIZATION_SLANT_RIGHT;
}
else if(strncmp(str, "SLL", 3)) {
return ADF_PATTERN_POLARIZATION_SLANT_LEFT;
}
else if(strncmp(str, "RCP", 3)) {
return ADF_PATTERN_POLARIZATION_RIGHT_HAND;
}
else if(strncmp(str, "LCP", 3)) {
return ADF_PATTERN_POLARIZATION_LEFT_HAND;
}
else if(strncmp(str, "ETH", 3)) {
return ADF_PATTERN_POLARIZATION_E_THETA;
}
else if(strncmp(str, "EPH", 3)) {
return ADF_PATTERN_POLARIZATION_E_PHI;
}
else {
printf("Unknown polarization type \"%s\", defaulting to H/H\n", str);
return ADF_PATTERN_POLARIZATION_HORIZONTAL_HORIZONTAL;
}
}
bool startsWith(const char* a, const char* b) {
return strncmp(a, b, strlen(b) - 1) == 0;
}
bool copyStringProperty(const char* property, const char** data, char* dest, int size) {
if(startsWith(*data, property)) {
const char* strStart = strchr(*data, ',') + 1;
const char* strEnd = strchr(*data, '\r');
if(strEnd - strStart <= size) {
printf("The property \"%s\" is too long\n", property);
strEnd = strStart + size;
}
strncpy(dest, strStart, strEnd - strStart);
*data = strchr(*data, '\n') + 1;
return true;
}
else {
return false;
}
}
bool copyMultiPartStringProperty(const char* property, const char** data, char** output) {
bool hasProperty = false;
char buffer[800] = {0};
int bufferLen = 0;
int propLen = strlen(property);
char* numberedProperty = (char*)malloc(propLen + 2);
strcpy(numberedProperty, property);
char num = '1';
numberedProperty[propLen + 1] = num;
while(copyStringProperty(numberedProperty, data, &buffer[bufferLen], 800 - bufferLen)) {
num++;
numberedProperty[propLen + 1] = num;
bufferLen = strlen(buffer);
hasProperty = true;
}
if(hasProperty) {
*output = (char*)malloc(bufferLen + 1);
strcpy(*output, buffer);
return true;
}
else {
*output = NULL;
return false;
}
}
bool copyIntProperty(const char* property, const char** data, int* dest) {
if(startsWith(*data, property)){
const char* intStart = strchr(*data, ',') + 1;
char* intEnd;
double tempInt = strtol(intStart, &intEnd, 10);
if(intEnd == intStart) {
//Didn't read a number
printf("Error in ADF file\n");
return false;
}
*dest = tempInt;
*data = strchr(*data, '\n') + 1;
return true;
}
else {
return false;
}
}
bool copyRatioProperty(const char* property, const char** data, int* numerator, int* denominator) {
if(startsWith(*data, property)){
const char* numeratorStart = strchr(*data, ',') + 1;
char* numeratorEnd;
int tempNumerator = strtol(numeratorStart, &numeratorEnd, 10);
if(numeratorEnd == numeratorStart) {
//Didn't read a number
printf("Error in ADF file\n");
return false;
}
const char* denominatorStart = strchr(*data, '/') + 1;
char* denominatorEnd;
int tempDenominator = strtol(denominatorStart, &denominatorEnd, 10);
if(denominatorStart == denominatorEnd) {
printf("Error in ADF file\n");
return false;
}
*numerator = tempNumerator;
*denominator = tempDenominator;
*data = strchr(*data, '\n') + 1;
return true;
}
else {
return false;
}
}
bool copyDoubleProperty(const char* property, const char** data, double* dest) {
if(startsWith(*data, property)){
const char* doubleStart = strchr(*data, ',') + 1;
char* doubleEnd;
double tempDouble = strtod(doubleStart, &doubleEnd);
if(doubleEnd == doubleStart) {
//Didn't read a number
printf("Error in ADF file\n");
return false;
}
*dest = tempDouble;
*data = strchr(*data, '\n') + 1;
return true;
}
else {
return false;
}
}
bool copyDoubleWithToleranceProperty(const char* property, const char** data, double* val, double* tolerance) {
if(startsWith(*data, property)){
const char* valStart = strchr(*data, ',') + 1;
char* valEnd;
double tempVal = strtod(valStart, &valEnd);
if(valEnd == valStart) {
//Didn't read a number
printf("Error in ADF file\n");
return false;
}
const char* endOfLine = strchr(valEnd, '\n');
const char* toleranceStart = strchr(valEnd, ',') + 1;
//The tolerance portion is optional
if(toleranceStart < endOfLine) {
char* toleranceEnd;
double tempTolerance = strtod(toleranceStart, &toleranceEnd);
if(toleranceEnd == toleranceStart) {
//Didn't read a number
printf("Error in ADF file\n");
return false;
}
*tolerance = tempTolerance;
}
*val = tempVal;
*data = endOfLine + 1;
return true;
}
else {
return false;
}
}
struct ADFPattern parseADFPattern(const char* data, int len) {
const char* current = data;
struct ADFPattern pattern;
copyStringProperty("REVNUM", ¤t, pattern.revisionNumber, 34);
copyStringProperty("REVDAT", ¤t, pattern.revisionDate, 8);
copyMultiPartStringProperty("COMNT", ¤t, &pattern.comment);
copyStringProperty("ANTMAN", ¤t, pattern.antennaManufacturer, 34);
copyStringProperty("MODNUM", ¤t, pattern.modelNumber, 34);
copyStringProperty("PATNUM", ¤t, pattern.patternIDNumber, 34);
copyRatioProperty("FILNUM", ¤t, &pattern.patternFileNumber, &pattern.patternFileNumberOutOf);
copyStringProperty("FEDORN", ¤t, pattern.feedOrientation, 5);
copyMultiPartStringProperty("DESCR", ¤t, &pattern.description);
copyStringProperty("DTDATA", ¤t, pattern.dateOfData, 8);
copyDoubleProperty("LOWFRQ", ¤t, &pattern.lowFrequency);
copyDoubleProperty("HGHFRQ", ¤t, &pattern.highFrequency);
//Parse gain units
if(startsWith(current, "GUNITS")) {
const char* maxGainUnitStart = strchr(current, ',') + 1;
pattern.maxGainUnit = stringToGainUnit(maxGainUnitStart);
const char* gainDataUnitStart = strchr(current, '/');
pattern.gainDataUnit = stringToGainUnit(gainDataUnitStart);
current = strchr(current, '\n') + 1;
}
copyDoubleProperty("LWGAIN", ¤t, &pattern.lowBandGain);
copyDoubleProperty("MDGAIN", ¤t, &pattern.midBandGain);
copyDoubleProperty("HGGAIN", ¤t, &pattern.highBandGain);
copyDoubleWithToleranceProperty("AZWIDT", ¤t, &pattern.azimuthBeamWidth, &pattern.azimuthBeamWidthTolerance);
copyDoubleWithToleranceProperty("ELWIDT", ¤t, &pattern.elevationBeamWidth, &pattern.elevationBeamWidthTolerance);
copyStringProperty("CONTYP", ¤t, pattern.connectorType, 72);
copyDoubleProperty("ATVSWR", ¤t, &pattern.voltageStandingWaveRatio);
copyDoubleProperty("FRTOBA", ¤t, &pattern.frontToBackRatio);
copyDoubleWithToleranceProperty("ELTILT", ¤t, &pattern.electricalDownTilt, &pattern.electricalDownTiltTolerance);
copyDoubleProperty("RADCTR", ¤t, &pattern.radiationCenter);
copyDoubleProperty("POTOPO", ¤t, &pattern.portToPortIsolation);
copyDoubleProperty("MAXPOW", ¤t, &pattern.maximumInputPower);
copyDoubleProperty("ANTLEN", ¤t, &pattern.antennaLength);
copyDoubleProperty("ANTWID", ¤t, &pattern.antennaWidth);
copyDoubleProperty("ANTDEP", ¤t, &pattern.antennaDepth);
copyDoubleProperty("ANTWGT", ¤t, &pattern.antennaWeight);
copyStringProperty("PATTYP", ¤t, pattern.patternType, 16);
copyIntProperty("NOFREQ", ¤t, &pattern.numberOfFrequencies);
pattern.frequencies = (struct ADFPatternFrequency*)malloc(sizeof(struct ADFPatternFrequency) * pattern.numberOfFrequencies);
for(int i = 0; i < pattern.numberOfFrequencies; i++) {
struct ADFPatternFrequency* freq = &pattern.frequencies[i];
copyDoubleProperty("PATFRE", ¤t, &freq->patternFrequency);
copyIntProperty("NUMCUT", ¤t, &freq->numberOfPatternCuts);
freq->patternCuts = (struct ADFPatternCut*)malloc(sizeof(struct ADFPatternCut) * freq->numberOfPatternCuts);
for(int j = 0; j < freq->numberOfPatternCuts; j++) {
struct ADFPatternCut* cut = &freq->patternCuts[j];
//Pattern cut type
char cutTypeBuffer[3];
copyStringProperty("PATCUT", ¤t, cutTypeBuffer, 3);
cut->patternCut = stringToCutType(cutTypeBuffer);
if(cut->patternCut == ADF_PATTERN_CUT_PHI) {
char* endInt;
cut->phiCutAngle = strtol(cutTypeBuffer, &endInt, 10);
if(endInt == cutTypeBuffer) {
printf("There should have been a angle, defaulting to 0\n");
}
}
//Polarization
char polarizationBuff[7];
copyStringProperty("POLARI", ¤t, polarizationBuff, 7);
cut->polarizationOfAntenna = stringToPolarizationType(polarizationBuff);
cut->polarizationOfSource = stringToPolarizationType(&polarizationBuff[3]);
copyIntProperty("NUPOIN", ¤t, &cut->numberOfDataPoints);
copyDoubleWithToleranceProperty("FSTLST", ¤t, &cut->firstAngleOfPatternData, &cut->lastAngleOfPatternData);
copyStringProperty("XORIEN", ¤t, cut->xAxisOrientation, 45);
copyStringProperty("YORIEN", ¤t, cut->yAxisOrientation, 45);
copyStringProperty("ZORIEN", ¤t, cut->zAxisOrientation, 45);
cut->dataPoints = (struct ADFPatternDataPoint*)malloc(sizeof(struct ADFPatternDataPoint) * cut->numberOfDataPoints);
for(int k = 0; k < cut->numberOfDataPoints; k++) {
cut->dataPoints[k].angleOfObservation = strtof(current, NULL);
current = strchr(current, ',') + 1;
cut->dataPoints[k].magnitudeResponse = strtof(current, NULL);
//Phase response is not required, so check if it is there
if(strchr(current, ',') < strchr(current, '\n')) {
current = strchr(current, ',') + 1;
cut->dataPoints[k].phaseResponse = strtof(current, NULL);
}
current = strchr(current, '\n') + 1;
}
for(int k = 0; k < cut->numberOfDataPoints; k++) {
printf("%.2f, %.2f, %.2f\n", cut->dataPoints[k].angleOfObservation, cut->dataPoints[k].magnitudeResponse, cut->dataPoints[k].phaseResponse);
}
}
}
return pattern;
}
void freeADFPattern(struct ADFPattern pattern) {
if(pattern.comment != NULL) {
free(pattern.comment);
}
if(pattern.description != NULL) {
free(pattern.description);
}
}