Repository navigation
Expand file tree
/
Copy pathRadarCrossSection.cpp
More file actions
1370 lines (1245 loc) · 39.3 KB
/
Copy pathRadarCrossSection.cpp
File metadata and controls
1370 lines (1245 loc) · 39.3 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c++ -*- */
/****************************************************************************
***** *****
***** Classification: UNCLASSIFIED *****
***** Classified By: *****
***** Declassify On: *****
***** *****
****************************************************************************
*
*
* Developed by: Naval Research Laboratory, Tactical Electronic Warfare Div.
* EW Modeling & Simulation, Code 5773
* 4555 Overlook Ave.
* Washington, D.C. 20375-5339
*
* License for source code is in accompanying LICENSE.txt file. If you did
* not receive a LICENSE.txt with this code, email simdis@us.navy.mil.
*
* The U.S. Government retains all rights to use, duplicate, distribute,
* disclose, or release this software.
*
*/
#include <algorithm>
#include <string>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cfloat>
#include <limits>
#include "simCore/String/Tokenizer.h"
#include "simCore/String/Format.h"
#include "simCore/String/UtfUtils.h"
#include "simCore/String/Utils.h"
#include "simCore/String/ValidNumber.h"
#include "simCore/Calc/Interpolation.h"
#include "simCore/Calc/Angle.h"
#include "simNotify/Notify.h"
#include "simCore/EM/Decibel.h"
#include "simCore/EM/RadarCrossSection.h"
namespace
{
bool getTokenLine(std::istream &inFile, std::string &str, size_t *lineNumber)
{
// track line number for error reporting purposes
(*lineNumber)++;
return simCore::getStrippedLine(inFile, str);
}
bool getFirstToken(std::istream &inFile, std::string &token, size_t *lineNumber)
{
std::string str;
if (getTokenLine(inFile, str, lineNumber))
{
std::vector<std::string> vec;
simCore::stringTokenizer(vec, str);
if (!vec.empty())
{
token = vec[0];
return true;
}
}
return false;
}
/** Retrieve the RCS type from stream by inspecting contents for markers */
simCore::RCSType getRCSType(std::istream& is)
{
simCore::RCSType rv = simCore::NO_RCS;
if (!is) // early exit
return rv;
const std::istream::pos_type readPosition = is.tellg();
// Read the first line of the file
std::string str;
if (simCore::getStrippedLine(is, str))
{
// Trim, and find the first non-whitespace token
str.erase(str.find_last_not_of(" \n\r\t") + 1);
const size_t strStart = str.find_first_not_of(" \n\r\t");
const std::string typeStr = str.substr(strStart, str.find_first_of(" \n\r\t", strStart) - strStart);
// Compare token to determine file type
if (typeStr == "0")
rv = simCore::RCS_LUT;
else if (typeStr == "1")
rv = simCore::RCS_BLOOM;
else if (typeStr.find("%") != std::string::npos || typeStr.find("&") != std::string::npos)
rv = simCore::RCS_SADM;
else if (typeStr.find("#") != std::string::npos || typeStr.find("f(GHz)") != std::string::npos)
rv = simCore::RCS_XPATCH;
}
// go back to the beginning of file
is.seekg(readPosition);
return rv;
}
}
namespace simCore {
/* ************************************************************************ */
/* RCSTableUD Methods */
/* ************************************************************************ */
RCSTable::RCSTable()
: freq_(0.f),
elev_(0.f),
polarity_(POLARITY_UNKNOWN)
{}
/* ************************************************************************ */
float RCSTable::RCS(double azim) const
{
if (azMap_.empty())
return static_cast<float>(SMALL_RCS_SM);
AZIM_RCS_MAP::const_iterator iter;
if (azMap_.size() == 1)
{
iter = azMap_.begin();
return iter->second;
}
iter = azMap_.find(static_cast<float>(azim));
if (iter == azMap_.end())
{
iter = azMap_.lower_bound(static_cast<float>(azim));
if (iter != azMap_.end())
{
if (iter == azMap_.begin())
{
return iter->second;
}
else
{
float rcshi = iter->second;
float azimhi = iter->first;
--iter;
float rcslo = iter->second;
float azimlo = iter->first;
// make sure we are not interpolating unnecessarily
assert(azim > azimlo);
assert(azim < azimhi);
return linearInterpolate(rcslo, rcshi, azimlo, azim, azimhi);
}
}
else
{
// item not found, use rbegin
AZIM_RCS_MAP::const_reverse_iterator riter = azMap_.rbegin();
return riter->second;
}
}
return iter->second;
}
/* ************************************************************************ */
void RCSTable::setRCS(float azim, float rcs)
{
azMap_[azim] = rcs;
}
/* ************************************************************************ */
/* ELEVMAP Methods */
/* ************************************************************************ */
ELEVMAP::~ELEVMAP()
{
ELEV_RCSTABLE_MAP::iterator it = eMap.begin();
while (it != eMap.end())
{
delete it->second;
++it;
}
}
/* ************************************************************************ */
/* FREQMAP Methods */
/* ************************************************************************ */
FREQMAP::~FREQMAP()
{
FREQ_ELEV_MAP::iterator it = freqMap.begin();
while (it != freqMap.end())
{
delete it->second;
++it;
}
}
/* ************************************************************************ */
/* RCSLUT Methods */
/* ************************************************************************ */
RCSLUT::RCSLUT()
: RadarCrossSection(),
description_(""),
tableType_(RCS_LUT_TYPE),
functionType_(RCS_MEAN_FUNC),
modulation_(1.f),
mean_(0.),
median_(SMALL_DB_VAL),
min_(std::numeric_limits<float>::max()),
max_(-std::numeric_limits<float>::max()),
lastFreq_(-std::numeric_limits<float>::max()),
lastPolarity_(POLARITY_UNKNOWN)
{
for (unsigned int i = 0; i < 2; ++i)
{
lastElev_[i] = std::numeric_limits<float>::max();
loTable_[i] = nullptr;
hiTable_[i] = nullptr;
}
}
RCSLUT::~RCSLUT()
{
POLARITY_FREQ_ELEV_MAP::iterator it = rcsMap_.begin();
while (it != rcsMap_.end())
{
delete it->second;
++it;
}
}
RCSTable *RCSLUT::getTable_(float freq, float elev, PolarityType pol, bool create)
{
POLARITY_FREQ_ELEV_MAP::iterator pfeiter;
FREQ_ELEV_MAP::iterator feiter;
ELEV_RCSTABLE_MAP::iterator eiter;
RCSTable *rcsTable = nullptr;
FREQMAP* fm = nullptr;
ELEVMAP* em = nullptr;
// look for specified polarization
pfeiter = rcsMap_.find(pol);
if (pfeiter == rcsMap_.end())
{
if (create)
{
fm = new FREQMAP();
rcsMap_[pol] = fm;
em = new ELEVMAP();
fm->freqMap[freq] = em;
rcsTable = new RCSTable;
rcsTable->setFreq(freq);
rcsTable->setElev(elev);
rcsTable->setPolarity(pol);
em->eMap[elev] = rcsTable;
}
return rcsTable;
}
// look for specified frequency
fm = pfeiter->second;
feiter = fm->freqMap.find(freq);
if (feiter == fm->freqMap.end())
{
if (create)
{
em = new ELEVMAP();
fm->freqMap[freq] = em;
rcsTable = new RCSTable;
rcsTable->setFreq(freq);
rcsTable->setElev(elev);
rcsTable->setPolarity(pol);
em->eMap[elev] = rcsTable;
}
return rcsTable;
}
// look for specified elevation
em = feiter->second;
eiter = em->eMap.find(elev);
if (eiter == em->eMap.end())
{
if (create)
{
rcsTable = new RCSTable;
rcsTable->setFreq(freq);
rcsTable->setElev(elev);
rcsTable->setPolarity(pol);
em->eMap[elev] = rcsTable;
}
return rcsTable;
}
rcsTable = eiter->second;
return rcsTable;
}
float RCSLUT::calcTableRCS_(float freq, double azim, double elev, PolarityType pol)
{
float final_rcs = SMALL_RCS_SM;
if (rcsMap_.empty())
{
return final_rcs;
}
RCSTable *rcstabLo = nullptr;
RCSTable *rcstabHi = nullptr;
bool foundInCache = false;
if (simCore::areEqual(lastFreq_, freq, 0.1) && pol == lastPolarity_)
{
if (lastElev_[0] != std::numeric_limits<float>::max() && simCore::areAnglesEqual(elev, lastElev_[0], 1.0e-11))
{
rcstabLo = loTable_[0];
rcstabHi = hiTable_[0];
foundInCache = true;
}
else if (lastElev_[1] != std::numeric_limits<float>::max() && simCore::areAnglesEqual(elev, lastElev_[1], 1.0e-11))
{
// note, not promoting this to primary cache
rcstabLo = loTable_[1];
rcstabHi = hiTable_[1];
foundInCache = true;
}
}
if (!foundInCache)
{
// find tables in map
POLARITY_FREQ_ELEV_MAP::const_iterator pfeiter;
if (pol == POLARITY_UNKNOWN)
{
// unknown polarity, grab first one
pfeiter = rcsMap_.begin();
}
else
{
pfeiter = rcsMap_.find(pol);
if (pfeiter == rcsMap_.end())
{
return final_rcs;
}
}
// look for selected frequency
FREQMAP* fm = pfeiter->second;
FREQ_ELEV_MAP::const_iterator feiter = fm->freqMap.find(freq);
// choose closest
if (feiter == fm->freqMap.end())
{
feiter = fm->freqMap.lower_bound(freq);
if (feiter == fm->freqMap.end())
{
// grab the last one
--feiter;
}
else if (feiter != fm->freqMap.begin())
{
float maxFreq = feiter->first;
--feiter;
float minFreq = feiter->first;
if (fabs(freq-minFreq) > fabs(maxFreq-freq))
{
// max freq is closer to requested freq
++feiter;
}
}
}
// look for selected elev
ELEVMAP* emap = feiter->second;
ELEV_RCSTABLE_MAP::const_iterator eiter;
if (emap->eMap.size() == 1)
{
eiter = emap->eMap.begin();
rcstabLo = eiter->second;
rcstabHi = rcstabLo;
}
else
{
// using find here assumes that requests will not be made for elev values close to but not equal to key values, e.g. elev=30.00001
// such values will be interpolated; a comparison that uses simCore::areAnglesEqual could be better in such cases.
eiter = emap->eMap.find(static_cast<float>(elev));
if (eiter != emap->eMap.end())
{
// exact match found
rcstabLo = eiter->second;
rcstabHi = rcstabLo;
}
else
{
eiter = emap->eMap.lower_bound(static_cast<float>(elev));
if (eiter == emap->eMap.end())
{
// after last table, use rbegin
ELEV_RCSTABLE_MAP::const_reverse_iterator riter = emap->eMap.rbegin();
rcstabLo = riter->second;
rcstabHi = rcstabLo;
}
else if (eiter == emap->eMap.begin())
{
// before first table
rcstabLo = eiter->second;
rcstabHi = rcstabLo;
}
else
{
// in between two tables, need to interpolate
rcstabHi = eiter->second;
--eiter;
rcstabLo = eiter->second;
}
}
}
// cache two elev/table combinations, to support calling pattern used by 3D rcs calculation
if (simCore::areEqual(lastFreq_, freq, 0.1) && pol == lastPolarity_)
{
// cache backup elev/table cache
loTable_[1] = loTable_[0];
hiTable_[1] = hiTable_[0];
lastElev_[1] = lastElev_[0];
}
else
{
// freq or polarization changed, cached elev/table values are no longer valid
loTable_[1] = nullptr;
hiTable_[1] = nullptr;
lastElev_[1] = std::numeric_limits<float>::max();
}
// update cached primary freq/polarity/elev/table combination
loTable_[0] = rcstabLo;
hiTable_[0] = rcstabHi;
lastFreq_ = freq;
lastElev_[0] = static_cast<float>(elev);
lastPolarity_ = pol;
}
// either both tables are null, or both are non-null
assert((rcstabLo == nullptr && rcstabHi == nullptr) || (rcstabLo && rcstabHi));
if (rcstabLo == nullptr && rcstabHi == nullptr)
{
// we didn't find any matching tables
final_rcs = dB2Linear(mean_);
}
else if (rcstabLo == rcstabHi)
{
// only found one table
final_rcs = rcstabLo->RCS(azim);
}
else if (rcstabLo && rcstabHi)
{
// we need to interpolate
// we are pretty sure that the two tables cannot have different freqs
assert(rcstabLo->freq() == rcstabHi->freq());
// we are pretty sure that the elev does not match table values
assert(elev > rcstabLo->elev());
assert(elev < rcstabHi->elev());
final_rcs = linearInterpolate(rcstabLo->RCS(azim), rcstabHi->RCS(azim), rcstabLo->elev(), elev, rcstabHi->elev());
}
// these cases should not occur, assert is above
else if (rcstabLo)
final_rcs = rcstabLo->RCS(azim);
else if (rcstabHi)
final_rcs = rcstabHi->RCS(azim);
return final_rcs;
}
float RCSLUT::RCSdB(float freq, double azim, double elev, PolarityType pol)
{
return linear2dB(RCSsm(freq, azim, elev, pol));
}
float RCSLUT::RCSsm(float freq, double azim, double elev, PolarityType pol)
{
// convert incoming azimuth & elevation to correct units & limits
azim = angFix2PI(azim);
elev = angFixPI(elev);
switch (tableType_)
{
case RCS_LUT_TYPE:
// strictly a lookup table, return mean value
return calcTableRCS_(freq, azim, elev, pol);
case RCS_SYM_LUT_TYPE:
// symmetrical lookup table, return mean value
return calcTableRCS_(freq, fabs(angFixPI(azim)), elev, pol);
case RCS_DISTRIBUTION_FUNC_TYPE:
default: // UTILS::eRCS_DISTRIBUTION_FUNC_TYPE
{
const double rcs = calcTableRCS_(freq, azim, elev, pol);
// apply distribution to mean rcs value
switch (functionType_)
{
case RCS_MEAN_FUNC:
default: // RCS_MEAN_FUNC
// apply scintillation to mean value
return static_cast<float>(rcs + modulation_);
case RCS_GAUSSIAN_FUNC:
// apply Gaussian distribution to rcs value
return static_cast<float>(rcs + (modulation_ * gaussian_()));
case RCS_RAYLEIGH_FUNC:
{
// apply Rayleigh distribution to rcs value
// sqrt (sum of the squares of two gaussians)
double x = gaussian_();
double y = gaussian_();
return static_cast<float>(rcs + (modulation_ * (sqrt(square(x) + square(y)))));
}
case RCS_LOG_NORMAL_FUNC:
{
// apply log normal distribution to rcs value
// (log of Rayleigh)
double x = gaussian_();
double y = gaussian_();
return static_cast<float>(rcs + (modulation_ * (log10(sqrt(square(x) + square(y))))));
}
}
}
}
return SMALL_RCS_SM;
}
int RCSLUT::loadXPATCHRCSFile_(std::istream &inFile)
{
int rv = 1;
RCSTable *rcsTable = nullptr;
std::vector<float> medianVec;
float freq = 0;
float elev = 0;
float azim = 0;
float rcsVV = 0;
float rcsHV = 0;
float rcsVH = 0;
float rcsHH = 0;
double rcsVal;
std::vector<std::string> vec;
while (getTokens(inFile, vec))
{
size_t vecLen = vec.size();
// FR(GHz) inc-EL inc-AZ VV HV VH HH
if (vec[0] != "#" && vecLen == 7 && vec[3].find("VV") == std::string::npos)
{
// found valid data, mark return value
if (rv == 1)
{
// reset previous look up table data
reset_();
description_ = "XPATCH RCS";
}
rv = 0;
if (!isValidNumber(vec[0], freq))
{
SIM_ERROR << "Encountered invalid number for XPATCH frequency" << std::endl;
return 1;
}
// convert GHz to MHz
freq *= 1000.f;
if (!isValidNumber(vec[1], elev))
{
SIM_ERROR << "Encountered invalid number for XPATCH elevation" << std::endl;
return 1;
}
elev = static_cast<float>(DEG2RAD*elev);
if (!isValidNumber(vec[2], azim))
{
SIM_ERROR << "Encountered invalid number for XPATCH azimuth" << std::endl;
return 1;
}
azim = static_cast<float>(DEG2RAD*azim);
if (!isValidNumber(vec[3], rcsVal))
{
SIM_ERROR << "Encountered invalid number for XPATCH VV value" << std::endl;
return 1;
}
// use double for dB2Linear to avoid precision truncation
rcsVV = static_cast<float>(dB2Linear(rcsVal));
if (!isValidNumber(vec[4], rcsVal))
{
SIM_ERROR << "Encountered invalid number for XPATCH HV value" << std::endl;
return 1;
}
// use double for dB2Linear to avoid precision truncation
rcsHV = static_cast<float>(dB2Linear(rcsVal));
if (!isValidNumber(vec[5], rcsVal))
{
SIM_ERROR << "Encountered invalid number for XPATCH VH value" << std::endl;
return 1;
}
// use double for dB2Linear to avoid precision truncation
rcsVH = static_cast<float>(dB2Linear(rcsVal));
if (!isValidNumber(vec[6], rcsVal))
{
SIM_ERROR << "Encountered invalid number for XPATCH HH value" << std::endl;
return 1;
}
// use double for dB2Linear to avoid precision truncation
rcsHH = static_cast<float>(dB2Linear(rcsVal));
// calculate median, min, max and mean rcs values
medianVec.push_back(rcsVV);
min_ = sdkMin(min_, rcsVV);
max_ = sdkMax(max_, rcsVV);
mean_ += rcsVV;
// add data to LUT
rcsTable = getTable_(freq, elev, POLARITY_HORIZONTAL, true);
rcsTable->setRCS(azim, rcsHH);
rcsTable = getTable_(freq, elev, POLARITY_VERTICAL, true);
rcsTable->setRCS(azim, rcsVV);
rcsTable = getTable_(freq, elev, POLARITY_HORZVERT, true);
rcsTable->setRCS(azim, rcsHV);
rcsTable = getTable_(freq, elev, POLARITY_VERTHORZ, true);
rcsTable->setRCS(azim, rcsVH);
} // end of if vec
} // end of while
if (rv)
{
return rv;
}
computeStatistics_(&medianVec);
// So far, so good...
return 0;
}
int RCSLUT::loadRcsLutFile_(std::istream &inFile)
{
int numTables = 0;
int tableType;
size_t lineNumber = 0;
// Table Type
std::string st;
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing table type for RCS LUT on line: " << lineNumber << std::endl;
return 1;
}
if (!isValidNumber(st, tableType))
{
SIM_ERROR << "Encountered invalid number for RCS LUT table type on line: " << lineNumber << std::endl;
return 1;
}
if (tableType != 0)
{
SIM_ERROR << "Unsupported RCS File Format\n" << std::endl;
return 1;
}
// reset previous look up table data
reset_();
// rcs pattern title
if (!getTokenLine(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing pattern title for RCS LUT on line: " << lineNumber << std::endl;
return 1;
}
description_ = st;
// table type
lineNumber++;
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing table type for RCS LUT on line: " << lineNumber << std::endl;
return 1;
}
int intTabType = 0;
if (!isValidNumber(st, intTabType))
{
SIM_ERROR << "Encountered invalid number for RCS LUT table type on line: " << lineNumber << std::endl;
return 1;
}
if (intTabType < 0 || intTabType > 2)
{
SIM_ERROR << "Incorrect table type found for RCS LUT on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
tableType_ = static_cast<RCSTableType>(intTabType);
// distribution function type
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing distribution function for RCS LUT on line: " << lineNumber << std::endl;
return 1;
}
int intFuncType = 0;
if (!isValidNumber(st, intFuncType))
{
SIM_ERROR << "Encountered invalid number for RCS LUT distribution function on line: " << lineNumber << std::endl;
return 1;
}
if (intFuncType < 0 || intFuncType > 3)
{
SIM_ERROR << "Incorrect distribution function found for RCS LUT on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
functionType_ = static_cast<RCSFuncType>(intFuncType);
// scintillation modulation
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing scintillation modulation for RCS LUT on line: " << lineNumber << std::endl;
return 1;
}
if (!isValidNumber(st, modulation_))
{
SIM_ERROR << "Encountered invalid number for RCS LUT modulation on line: " << lineNumber << std::endl;
return 1;
}
if (modulation_ < 0)
{
SIM_ERROR << "Modulation value found for RCS LUT on line: " << lineNumber << " <" << st << ">, must be >= 0" << std::endl;
return 1;
}
// # tables
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing # tables for RCS LUT on line: " << lineNumber << std::endl;
return 1;
}
if (!isValidNumber(st, numTables))
{
SIM_ERROR << "Encountered invalid number for RCS LUT number of tables on line: " << lineNumber << std::endl;
return 1;
}
PolarityType pol=POLARITY_UNKNOWN;
float azim = 0;
float rcs = 0;
double val = 0;
std::vector<float> medianVec;
// read in remainder of data
for (int i = 0; i < numTables; i++)
{
// Frequency of rcs
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing RCS freq for RCS LUT # " << i+1 << " on line: " << lineNumber << std::endl;
return 1;
}
float freq = 0.f;
if (!isValidNumber(st, freq))
{
SIM_ERROR << "Encountered invalid number for RCS LUT frequency on line: " << lineNumber << std::endl;
return 1;
}
if (freq <= 0)
{
SIM_ERROR << "Incorrect frequency value found for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
// Elevation of rcs
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing elev for RCS LUT # " << i+1 << " on line: " << lineNumber << std::endl;
return 1;
}
float elev = 0.f;
if (!isValidNumber(st, elev))
{
SIM_ERROR << "Encountered invalid number for RCS LUT elevation on line: " << lineNumber << std::endl;
return 1;
}
elev = static_cast<float>(DEG2RAD*elev);
// polarization of pattern
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing polarity for RCS LUT # " << i+1 << " on line: " << lineNumber << std::endl;
return 1;
}
int intpol = 0;
if (!isValidNumber(st, intpol))
{
SIM_ERROR << "Encountered invalid number for RCS LUT polarity on line: " << lineNumber << std::endl;
return 1;
}
if (intpol < 0 || intpol > 8)
{
SIM_ERROR << "Incorrect polarity found for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
pol = static_cast<PolarityType>(intpol);
// # of (azim,rcs) pairs in table
if (!getFirstToken(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing (azim, rcs) table size for RCS LUT # " << i+1 << " on line: " << lineNumber << std::endl;
return 1;
}
int tableSize = 0;
if (!isValidNumber(st, tableSize))
{
SIM_ERROR << "Encountered invalid number for RCS LUT table size on line: " << lineNumber << std::endl;
return 1;
}
if (tableSize <= 0)
{
SIM_ERROR << "Incorrect table size found for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
// units for angle & rcs
if (!getTokenLine(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing angle and RCS units for RCS LUT # " << i+1 << " on line: " << lineNumber << std::endl;
return 1;
}
std::vector<std::string> unitVec;
stringTokenizer(unitVec, st);
if (unitVec.size() < 2)
{
SIM_ERROR << "Incorrect # tokens (>=2) " << unitVec.size() << " found with angle and RCS units for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
short angleUnits = 0;
if (!isValidNumber(unitVec[0], angleUnits))
{
SIM_ERROR << "Encountered invalid number for RCS LUT angle unit on line: " << lineNumber << std::endl;
return 1;
}
if (angleUnits < 0 || angleUnits > 1)
{
SIM_ERROR << "Incorrect angle unit found for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
short rcsUnits = 0;
if (!isValidNumber(unitVec[1], rcsUnits))
{
SIM_ERROR << "Encountered invalid number for RCS LUT rcs unit on line: " << lineNumber << std::endl;
return 1;
}
if (rcsUnits < 0 || rcsUnits > 1)
{
SIM_ERROR << "Incorrect RCS unit found for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
RCSTable *rcsTable = getTable_(freq, elev, pol, true);
// read in remainder of data
for (int ii = 0; ii < tableSize; ii++)
{
if (!getTokenLine(inFile, st, &lineNumber))
{
SIM_ERROR << "Error processing (azim, rcs) pair " << ii+1 << " for RCS LUT # " << i+1 << " on line: " << lineNumber << std::endl;
return 1;
}
std::vector<std::string> dataVec;
stringTokenizer(dataVec, st);
if (dataVec.size() < 2)
{
SIM_ERROR << "Incorrect # tokens (>=2) " << dataVec.size() << " found with (azim, rcs) pair " << ii+1 << " for RCS LUT # " << i+1 << " on line: " << lineNumber << " <" << st << ">" << std::endl;
return 1;
}
if (!isValidNumber(dataVec[0], azim))
{
SIM_ERROR << "Encountered invalid number for RCS LUT azimuth on line: " << lineNumber << std::endl;
return 1;
}
if (!isValidNumber(dataVec[1], val))
{
SIM_ERROR << "Encountered invalid number for RCS LUT value on line: " << lineNumber << std::endl;
return 1;
}
// verify angle units, force to rad
// we force all rcs units to be in m^2
// use double for dB2Linear to avoid precision truncation
rcs = static_cast<float>((rcsUnits == 1) ? dB2Linear(val) : val);
rcsTable->setRCS((angleUnits == 0) ? static_cast<float>(DEG2RAD*(azim)) : azim, rcs);
// calc statistics on data
min_ = sdkMin(min_, rcs);
max_ = sdkMax(max_, rcs);
mean_ += rcs;
medianVec.push_back(rcs);
}
} // end of for numTables
computeStatistics_(&medianVec);
// So far, so good...
return 0;
}
int RCSLUT::loadSADMRCSFile_(std::istream &inFile)
{
std::string str;
// remove SADM comments
do
{
getStrippedLine(inFile, str);
} while (str.find("%") != std::string::npos);
// Verify correct SADM file
if (str.find("&RCS") == std::string::npos)
{
SIM_ERROR << "Error processing SADM RCS file, did not find \"&RCS\" after comment section." << std::endl;
return 1;
}
float freq = 0.f;
PolarityType pol=POLARITY_UNKNOWN;
std::vector<float> medianVec;
// The SADM documentation explicitly states that:
//
// % There are THREE forms of this file:
// % 1. Version 1 contains total RCS vs azimuth and elevation
// % at each frequency and polarization. This file version
// % applies if the flag RCS_IQ is false or missing.
// % 2. Version 2 contains RCS at high sampling resolution at
// % each azimuth for a specified frequency and polarization.
// % This file version applies if the flag RCS_IQ_FLAG is true.
// % 3. Version 3 contains multiple RCS scatterers plus their
// % locations on the ship. This file version applies if the
// % flag RCS_MULTI_SCATTERERS is true.
// %
// %**********************************************************
std::vector<std::string> vec;
if (!getTokens(inFile, vec))
{
SIM_ERROR << "Error processing SADM RCS file" << std::endl;
return 1;
}
// Verify correct version of SADM file
// We only process version 1 of the file
if (vec.size() > 2 && stringCaseFind(vec[0], "RCS_IQ") != std::string::npos)
{
if (upperCase(vec[2]) == "T")
{
SIM_ERROR << "SADM version 2 RCS file is not supported" << std::endl;
return 1;
}
}
if (vec.size() > 2 && stringCaseFind(vec[0], "RCS_MULTI_SCATTERERS") != std::string::npos)
{
if (upperCase(vec[2]) == "T")
{
SIM_ERROR << "SADM version 3 RCS file is not supported" << std::endl;
return 1;
}
}
if (!vec.empty() && simCore::caseCompare(vec[0], "RCS_FREQ_INTERPOLATE") != 0)
{
if (!getTokens(inFile, vec))
{
SIM_ERROR << "Could not find RCS_FREQ_INTERPOLATE" << std::endl;
return 1;
}
}
// check for existing RCS, then reset
reset_();
description_ = "SADM RCS";
// verify next token is RCS_FREQ_INTERPOLATE
if (checkTokens_(vec.size(), 3, "RCS_FREQ_INTERPOLATE")) return 1;
// % The initial parameters that go into Version 1 of the file are:
// %
// % RCS_IQ = F
// % RCS_FREQ_INTERPOLATE = T/F
// % RCS_N_AZ =
// % RCS_N_EL =
// % RCS_N_FREQ =
// % RCS_EL = el1 el2 ...
// %
// % They are followed by the following values, repeated for
// % each frequency / polarization combination.
// %
// % RCS_FREQ = freq1
// % RCS_POL = H
// % RCS_ON_AXIS = on_axis_rcs @ freq1 & H pol
// % RCS_TABLE =