-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.cpp
4519 lines (3424 loc) · 102 KB
/
main.cpp
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
/****************************************************************************\
* Signal Server 1.3.8: Server optimised SPLAT! by Alex Farrant *
******************************************************************************
* SPLAT! Project started in 1997 by John A. Magliacane, KD2BD *
* *
******************************************************************************
* Please consult the SPLAT! documentation for a complete list of *
* individuals who have contributed to this project. *
******************************************************************************
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License or any later *
* version. *
* *
* This program is distributed in the hope that it will useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
******************************************************************************
* g++ -Wall -O3 -s -lm -fomit-frame-pointer itm.cpp hata.cpp cost.cpp fspl.cpp main.cpp -o ss *
\****************************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#define GAMMA 2.5
#define MAXPAGES 64
#define ARRAYSIZE 76810
#define IPPD 1200
#ifndef PI
#define PI 3.141592653589793
#endif
#ifndef TWOPI
#define TWOPI 6.283185307179586
#endif
#ifndef HALFPI
#define HALFPI 1.570796326794896
#endif
#define DEG2RAD 1.74532925199e-02
#define EARTHRADIUS 20902230.97
#define METERS_PER_MILE 1609.344
#define METERS_PER_FOOT 0.3048
#define KM_PER_MILE 1.609344
#define FOUR_THIRDS 1.3333333333333
char string[255], sdf_path[255], udt_file[255], opened=0, gpsav=0, ss_name[16],
ss_version[6], dashes[80];
double earthradius, max_range=0.0, forced_erp, dpp, ppd,
fzone_clearance=0.6, forced_freq, clutter, lat,lon,txh,tercon,terdic,
north,east,south,west;
int min_north=90, max_north=-90, min_west=360, max_west=-1, ippd, mpi,
max_elevation=-32768, min_elevation=32768, bzerror, contour_threshold,
pred,pblue,pgreen,ter,multiplier=256,debug=0,loops=64,jgets=0, MAXRAD;
unsigned char got_elevation_pattern, got_azimuth_pattern, metric=0, dbm=0;
struct site { double lat;
double lon;
float alt;
char name[50];
char filename[255];
} site;
struct path { double lat[ARRAYSIZE];
double lon[ARRAYSIZE];
double elevation[ARRAYSIZE];
double distance[ARRAYSIZE];
int length;
} path;
struct dem { int min_north;
int max_north;
int min_west;
int max_west;
int max_el;
int min_el;
short data[IPPD][IPPD];
unsigned char mask[IPPD][IPPD];
unsigned char signal[IPPD][IPPD];
} dem[MAXPAGES];
struct LR { double eps_dielect;
double sgm_conductivity;
double eno_ns_surfref;
double frq_mhz;
double conf;
double rel;
double erp;
int radio_climate;
int pol;
float antenna_pattern[361][1001];
} LR;
struct region { unsigned char color[128][3];
int level[128];
int levels;
} region;
double elev[ARRAYSIZE+10];
void point_to_point(double elev[], double tht_m, double rht_m,
double eps_dielect, double sgm_conductivity, double eno_ns_surfref,
double frq_mhz, int radio_climate, int pol, double conf,
double rel, double &dbloss, char *strmode, int &errnum);
double HataLinkdB(float f,float h_B, float h_M, float d, int mode);
double CostHataLinkdB(float f,float h_B, float h_M, float d);
double FsplLinkdB(float f, float d);
double ked(double freq, double elev[], double rxh, double dkm);
double arccos(double x, double y)
{
/* This function implements the arc cosine function,
returning a value between 0 and TWOPI. */
double result=0.0;
if (y>0.0)
result=acos(x/y);
if (y<0.0)
result=PI+acos(x/y);
return result;
}
int ReduceAngle(double angle)
{
/* This function normalizes the argument to
an integer angle between 0 and 180 degrees */
double temp;
temp=acos(cos(angle*DEG2RAD));
return (int)rint(temp/DEG2RAD);
}
double LonDiff(double lon1, double lon2)
{
/* This function returns the short path longitudinal
difference between longitude1 and longitude2
as an angle between -180.0 and +180.0 degrees.
If lon1 is west of lon2, the result is positive.
If lon1 is east of lon2, the result is negative. */
double diff;
diff=lon1-lon2;
if (diff<=-180.0)
diff+=360.0;
if (diff>=180.0)
diff-=360.0;
return diff;
}
char *dec2dms(double decimal)
{
/* Converts decimal degrees to degrees, minutes, seconds,
(DMS) and returns the result as a character string. */
char sign;
int degrees, minutes, seconds;
double a, b, c, d;
if (decimal<0.0)
{
decimal=-decimal;
sign=-1;
}
else
sign=1;
a=floor(decimal);
b=60.0*(decimal-a);
c=floor(b);
d=60.0*(b-c);
degrees=(int)a;
minutes=(int)c;
seconds=(int)d;
if (seconds<0)
seconds=0;
if (seconds>59)
seconds=59;
string[0]=0;
snprintf(string,250,"%d%c %d\' %d\"", degrees*sign, 176, minutes, seconds);
return (string);
}
int PutMask(double lat, double lon, int value)
{
/* Lines, text, markings, and coverage areas are stored in a
mask that is combined with topology data when topographic
maps are generated by ss. This function sets and resets
bits in the mask based on the latitude and longitude of the
area pointed to. */
int x, y, indx;
char found;
for (indx=0, found=0; indx<MAXPAGES && found==0;)
{
x=(int)rint(ppd*(lat-dem[indx].min_north));
y=mpi-(int)rint(ppd*(LonDiff(dem[indx].max_west,lon)));
if (x>=0 && x<=mpi && y>=0 && y<=mpi)
found=1;
else
indx++;
}
if (found)
{
dem[indx].mask[x][y]=value;
return ((int)dem[indx].mask[x][y]);
}
else
return -1;
}
int OrMask(double lat, double lon, int value)
{
/* Lines, text, markings, and coverage areas are stored in a
mask that is combined with topology data when topographic
maps are generated by ss. This function sets bits in
the mask based on the latitude and longitude of the area
pointed to. */
int x, y, indx;
char found;
for (indx=0, found=0; indx<MAXPAGES && found==0;)
{
x=(int)rint(ppd*(lat-dem[indx].min_north));
y=mpi-(int)rint(ppd*(LonDiff(dem[indx].max_west,lon)));
if (x>=0 && x<=mpi && y>=0 && y<=mpi)
found=1;
else
indx++;
}
if (found)
{
dem[indx].mask[x][y]|=value;
return ((int)dem[indx].mask[x][y]);
}
else
return -1;
}
int GetMask(double lat, double lon)
{
/* This function returns the mask bits based on the latitude
and longitude given. */
return (OrMask(lat,lon,0));
}
int PutSignal(double lat, double lon, unsigned char signal)
{
/* This function writes a signal level (0-255)
at the specified location for later recall. */
int x, y, indx;
char found;
for (indx=0, found=0; indx<MAXPAGES && found==0;)
{
x=(int)rint(ppd*(lat-dem[indx].min_north));
y=mpi-(int)rint(ppd*(LonDiff(dem[indx].max_west,lon)));
if (x>=0 && x<=mpi && y>=0 && y<=mpi)
found=1;
else
indx++;
}
if (found)
{
dem[indx].signal[x][y]=signal;
return (dem[indx].signal[x][y]);
}
else
return 0;
}
unsigned char GetSignal(double lat, double lon)
{
/* This function reads the signal level (0-255) at the
specified location that was previously written by the
complimentary PutSignal() function. */
int x, y, indx;
char found;
for (indx=0, found=0; indx<MAXPAGES && found==0;)
{
x=(int)rint(ppd*(lat-dem[indx].min_north));
y=mpi-(int)rint(ppd*(LonDiff(dem[indx].max_west,lon)));
if (x>=0 && x<=mpi && y>=0 && y<=mpi)
found=1;
else
indx++;
}
if (found)
return (dem[indx].signal[x][y]);
else
return 0;
}
double GetElevation(struct site location)
{
/* This function returns the elevation (in feet) of any location
represented by the digital elevation model data in memory.
Function returns -5000.0 for locations not found in memory. */
char found;
int x, y, indx;
double elevation;
for (indx=0, found=0; indx<MAXPAGES && found==0;)
{
x=(int)rint(ppd*(location.lat-dem[indx].min_north));
y=mpi-(int)rint(ppd*(LonDiff(dem[indx].max_west,location.lon)));
if (x>=0 && x<=mpi && y>=0 && y<=mpi)
found=1;
else
indx++;
}
if (found)
elevation=3.28084*dem[indx].data[x][y];
else
elevation=-5000.0;
return elevation;
}
int AddElevation(double lat, double lon, double height)
{
/* This function adds a user-defined terrain feature
(in meters AGL) to the digital elevation model data
in memory. Does nothing and returns 0 for locations
not found in memory. */
char found;
int x, y, indx;
for (indx=0, found=0; indx<MAXPAGES && found==0;)
{
x=(int)rint(ppd*(lat-dem[indx].min_north));
y=mpi-(int)rint(ppd*(LonDiff(dem[indx].max_west,lon)));
if (x>=0 && x<=mpi && y>=0 && y<=mpi)
found=1;
else
indx++;
}
if (found)
dem[indx].data[x][y]+=(short)rint(height);
return found;
}
double Distance(struct site site1, struct site site2)
{
/* This function returns the great circle distance
in miles between any two site locations. */
double lat1, lon1, lat2, lon2, distance;
lat1=site1.lat*DEG2RAD;
lon1=site1.lon*DEG2RAD;
lat2=site2.lat*DEG2RAD;
lon2=site2.lon*DEG2RAD;
distance=3959.0*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((lon1)-(lon2)));
return distance;
}
double Azimuth(struct site source, struct site destination)
{
/* This function returns the azimuth (in degrees) to the
destination as seen from the location of the source. */
double dest_lat, dest_lon, src_lat, src_lon,
beta, azimuth, diff, num, den, fraction;
dest_lat=destination.lat*DEG2RAD;
dest_lon=destination.lon*DEG2RAD;
src_lat=source.lat*DEG2RAD;
src_lon=source.lon*DEG2RAD;
/* Calculate Surface Distance */
beta=acos(sin(src_lat)*sin(dest_lat)+cos(src_lat)*cos(dest_lat)*cos(src_lon-dest_lon));
/* Calculate Azimuth */
num=sin(dest_lat)-(sin(src_lat)*cos(beta));
den=cos(src_lat)*sin(beta);
fraction=num/den;
/* Trap potential problems in acos() due to rounding */
if (fraction>=1.0)
fraction=1.0;
if (fraction<=-1.0)
fraction=-1.0;
/* Calculate azimuth */
azimuth=acos(fraction);
/* Reference it to True North */
diff=dest_lon-src_lon;
if (diff<=-PI)
diff+=TWOPI;
if (diff>=PI)
diff-=TWOPI;
if (diff>0.0)
azimuth=TWOPI-azimuth;
return (azimuth/DEG2RAD);
}
double ElevationAngle(struct site source, struct site destination)
{
/* This function returns the angle of elevation (in degrees)
of the destination as seen from the source location.
A positive result represents an angle of elevation (uptilt),
while a negative result represents an angle of depression
(downtilt), as referenced to a normal to the center of
the earth. */
register double a, b, dx;
a=GetElevation(destination)+destination.alt+earthradius;
b=GetElevation(source)+source.alt+earthradius;
dx=5280.0*Distance(source,destination);
/* Apply the Law of Cosines */
return ((180.0*(acos(((b*b)+(dx*dx)-(a*a))/(2.0*b*dx)))/PI)-90.0);
}
void ReadPath(struct site source, struct site destination)
{
/* This function generates a sequence of latitude and
longitude positions between source and destination
locations along a great circle path, and stores
elevation and distance information for points
along that path in the "path" structure. */
int c;
double azimuth, distance, lat1, lon1, beta, den, num,
lat2, lon2, total_distance, dx, dy, path_length,
miles_per_sample, samples_per_radian=68755.0;
struct site tempsite;
lat1=source.lat*DEG2RAD;
lon1=source.lon*DEG2RAD;
lat2=destination.lat*DEG2RAD;
lon2=destination.lon*DEG2RAD;
samples_per_radian=ppd*57.295833;
azimuth=Azimuth(source,destination)*DEG2RAD;
total_distance=Distance(source,destination);
if (total_distance>(30.0/ppd))
{
dx=samples_per_radian*acos(cos(lon1-lon2));
dy=samples_per_radian*acos(cos(lat1-lat2));
path_length=sqrt((dx*dx)+(dy*dy));
miles_per_sample=total_distance/path_length;
}
else
{
c=0;
dx=0.0;
dy=0.0;
path_length=0.0;
miles_per_sample=0.0;
total_distance=0.0;
lat1=lat1/DEG2RAD;
lon1=lon1/DEG2RAD;
path.lat[c]=lat1;
path.lon[c]=lon1;
path.elevation[c]=GetElevation(source);
path.distance[c]=0.0;
}
for (distance=0.0, c=0; (total_distance!=0.0 && distance<=total_distance && c<ARRAYSIZE); c++, distance=miles_per_sample*(double)c)
{
beta=distance/3959.0;
lat2=asin(sin(lat1)*cos(beta)+cos(azimuth)*sin(beta)*cos(lat1));
num=cos(beta)-(sin(lat1)*sin(lat2));
den=cos(lat1)*cos(lat2);
if (azimuth==0.0 && (beta>HALFPI-lat1))
lon2=lon1+PI;
else if (azimuth==HALFPI && (beta>HALFPI+lat1))
lon2=lon1+PI;
else if (fabs(num/den)>1.0)
lon2=lon1;
else
{
if ((PI-azimuth)>=0.0)
lon2=lon1-arccos(num,den);
else
lon2=lon1+arccos(num,den);
}
while (lon2<0.0)
lon2+=TWOPI;
while (lon2>TWOPI)
lon2-=TWOPI;
lat2=lat2/DEG2RAD;
lon2=lon2/DEG2RAD;
path.lat[c]=lat2;
path.lon[c]=lon2;
tempsite.lat=lat2;
tempsite.lon=lon2;
path.elevation[c]=GetElevation(tempsite);
path.distance[c]=distance;
}
/* Make sure exact destination point is recorded at path.length-1 */
if (c<ARRAYSIZE)
{
path.lat[c]=destination.lat;
path.lon[c]=destination.lon;
path.elevation[c]=GetElevation(destination);
path.distance[c]=total_distance;
c++;
}
if (c<ARRAYSIZE)
path.length=c;
else
path.length=ARRAYSIZE-1;
}
double ElevationAngle2(struct site source, struct site destination, double er)
{
/* This function returns the angle of elevation (in degrees)
of the destination as seen from the source location, UNLESS
the path between the sites is obstructed, in which case, the
elevation angle to the first obstruction is returned instead.
"er" represents the earth radius. */
int x;
char block=0;
double source_alt, destination_alt, cos_xmtr_angle,
cos_test_angle, test_alt, elevation, distance,
source_alt2, first_obstruction_angle=0.0;
struct path temp;
temp=path;
ReadPath(source,destination);
distance=5280.0*Distance(source,destination);
source_alt=er+source.alt+GetElevation(source);
destination_alt=er+destination.alt+GetElevation(destination);
source_alt2=source_alt*source_alt;
/* Calculate the cosine of the elevation angle of the
destination (receiver) as seen by the source (transmitter). */
cos_xmtr_angle=((source_alt2)+(distance*distance)-(destination_alt*destination_alt))/(2.0*source_alt*distance);
/* Test all points in between source and destination locations to
see if the angle to a topographic feature generates a higher
elevation angle than that produced by the destination. Begin
at the source since we're interested in identifying the FIRST
obstruction along the path between source and destination. */
for (x=2, block=0; x<path.length && block==0; x++)
{
distance=5280.0*path.distance[x];
test_alt=earthradius+(path.elevation[x]==0.0?path.elevation[x]:path.elevation[x]+clutter);
cos_test_angle=((source_alt2)+(distance*distance)-(test_alt*test_alt))/(2.0*source_alt*distance);
/* Compare these two angles to determine if
an obstruction exists. Since we're comparing
the cosines of these angles rather than
the angles themselves, the sense of the
following "if" statement is reversed from
what it would be if the angles themselves
were compared. */
if (cos_xmtr_angle>=cos_test_angle)
{
block=1;
first_obstruction_angle=((acos(cos_test_angle))/DEG2RAD)-90.0;
}
}
if (block)
elevation=first_obstruction_angle;
else
elevation=((acos(cos_xmtr_angle))/DEG2RAD)-90.0;
path=temp;
return elevation;
}
double AverageTerrain(struct site source, double azimuthx, double start_distance, double end_distance)
{
/* This function returns the average terrain calculated in
the direction of "azimuth" (degrees) between "start_distance"
and "end_distance" (miles) from the source location. If
the terrain is all water (non-critical error), -5000.0 is
returned. If not enough SDF data has been loaded into
memory to complete the survey (critical error), then
-9999.0 is returned. */
int c, samples, endpoint;
double beta, lat1, lon1, lat2, lon2, num, den, azimuth, terrain=0.0;
struct site destination;
lat1=source.lat*DEG2RAD;
lon1=source.lon*DEG2RAD;
/* Generate a path of elevations between the source
location and the remote location provided. */
beta=end_distance/3959.0;
azimuth=DEG2RAD*azimuthx;
lat2=asin(sin(lat1)*cos(beta)+cos(azimuth)*sin(beta)*cos(lat1));
num=cos(beta)-(sin(lat1)*sin(lat2));
den=cos(lat1)*cos(lat2);
if (azimuth==0.0 && (beta>HALFPI-lat1))
lon2=lon1+PI;
else if (azimuth==HALFPI && (beta>HALFPI+lat1))
lon2=lon1+PI;
else if (fabs(num/den)>1.0)
lon2=lon1;
else
{
if ((PI-azimuth)>=0.0)
lon2=lon1-arccos(num,den);
else
lon2=lon1+arccos(num,den);
}
while (lon2<0.0)
lon2+=TWOPI;
while (lon2>TWOPI)
lon2-=TWOPI;
lat2=lat2/DEG2RAD;
lon2=lon2/DEG2RAD;
destination.lat=lat2;
destination.lon=lon2;
/* If SDF data is missing for the endpoint of
the radial, then the average terrain cannot
be accurately calculated. Return -9999.0 */
if (GetElevation(destination)<-4999.0)
return (-9999.0);
else
{
ReadPath(source,destination);
endpoint=path.length;
/* Shrink the length of the radial if the
outermost portion is not over U.S. land. */
for (c=endpoint-1; c>=0 && path.elevation[c]==0.0; c--);
endpoint=c+1;
for (c=0, samples=0; c<endpoint; c++)
{
if (path.distance[c]>=start_distance)
{
terrain+=(path.elevation[c]==0.0?path.elevation[c]:path.elevation[c]+clutter);
samples++;
}
}
if (samples==0)
terrain=-5000.0; /* No land */
else
terrain=(terrain/(double)samples);
return terrain;
}
}
double haat(struct site antenna)
{
/* This function returns the antenna's Height Above Average
Terrain (HAAT) based on FCC Part 73.313(d). If a critical
error occurs, such as a lack of SDF data to complete the
survey, -5000.0 is returned. */
int azi, c;
char error=0;
double terrain, avg_terrain, haat, sum=0.0;
/* Calculate the average terrain between 2 and 10 miles
from the antenna site at azimuths of 0, 45, 90, 135,
180, 225, 270, and 315 degrees. */
for (c=0, azi=0; azi<=315 && error==0; azi+=45)
{
terrain=AverageTerrain(antenna, (double)azi, 2.0, 10.0);
if (terrain<-9998.0) /* SDF data is missing */
error=1;
if (terrain>-4999.0) /* It's land, not water */
{
sum+=terrain; /* Sum of averages */
c++;
}
}
if (error)
return -5000.0;
else
{
avg_terrain=(sum/(double)c);
haat=(antenna.alt+GetElevation(antenna))-avg_terrain;
return haat;
}
}
double ReadBearing(char *input)
{
/* This function takes numeric input in the form of a character
string, and returns an equivalent bearing in degrees as a
decimal number (double). The input may either be expressed
in decimal format (40.139722) or degree, minute, second
format (40 08 23). This function also safely handles
extra spaces found either leading, trailing, or
embedded within the numbers expressed in the
input string. Decimal seconds are permitted. */
double seconds, bearing=0.0;
char string[20];
int a, b, length, degrees, minutes;
/* Copy "input" to "string", and ignore any extra
spaces that might be present in the process. */
string[0]=0;
length=strlen(input);
for (a=0, b=0; a<length && a<18; a++)
{
if ((input[a]!=32 && input[a]!='\n') || (input[a]==32 && input[a+1]!=32 && input[a+1]!='\n' && b!=0))
{
string[b]=input[a];
b++;
}
}
string[b]=0;
/* Count number of spaces in the clean string. */
length=strlen(string);
for (a=0, b=0; a<length; a++)
if (string[a]==32)
b++;
if (b==0) /* Decimal Format (40.139722) */
sscanf(string,"%lf",&bearing);
if (b==2) /* Degree, Minute, Second Format (40 08 23.xx) */
{
sscanf(string,"%d %d %lf",°rees, &minutes, &seconds);
bearing=fabs((double)degrees);
bearing+=fabs(((double)minutes)/60.0);
bearing+=fabs(seconds/3600.0);
if ((degrees<0) || (minutes<0) || (seconds<0.0))
bearing=-bearing;
}
/* Anything else returns a 0.0 */
if (bearing>360.0 || bearing<-360.0)
bearing=0.0;
return bearing;
}
void LoadPAT(char *filename)
{
/* This function reads and processes antenna pattern (.az
and .el) files that correspond in name to previously
loaded ss .lrp files. */
int a, b, w, x, y, z, last_index, next_index, span;
char string[255], azfile[255], elfile[255], *pointer=NULL, *s=NULL;
float az, xx, elevation, amplitude, rotation, valid1, valid2,
delta, azimuth[361], azimuth_pattern[361], el_pattern[10001],
elevation_pattern[361][1001], slant_angle[361], tilt,
mechanical_tilt=0.0, tilt_azimuth, tilt_increment, sum;
FILE *fd=NULL;
unsigned char read_count[10001];
for (x=0; filename[x]!='.' && filename[x]!=0 && x<250; x++)
{
azfile[x]=filename[x];
elfile[x]=filename[x];
}
azfile[x]='.';
azfile[x+1]='a';
azfile[x+2]='z';
azfile[x+3]=0;
elfile[x]='.';
elfile[x+1]='e';
elfile[x+2]='l';
elfile[x+3]=0;
rotation=0.0;
got_azimuth_pattern=0;
got_elevation_pattern=0;
/* Load .az antenna pattern file */
fd=fopen(azfile,"r");
if (fd!=NULL)
{
/* Clear azimuth pattern array */
for (x=0; x<=360; x++)
{
azimuth[x]=0.0;
read_count[x]=0;
}
/* Read azimuth pattern rotation
in degrees measured clockwise
from true North. */
s=fgets(string,254,fd);
pointer=strchr(string,';');
if (pointer!=NULL)
*pointer=0;
sscanf(string,"%f",&rotation);
/* Read azimuth (degrees) and corresponding
normalized field radiation pattern amplitude
(0.0 to 1.0) until EOF is reached. */
s=fgets(string,254,fd);
pointer=strchr(string,';');
if (pointer!=NULL)
*pointer=0;
sscanf(string,"%f %f",&az, &litude);
do
{
x=(int)rintf(az);
if (x>=0 && x<=360 && fd!=NULL)
{
azimuth[x]+=amplitude;
read_count[x]++;
}
s=fgets(string,254,fd);
pointer=strchr(string,';');
if (pointer!=NULL)
*pointer=0;
sscanf(string,"%f %f",&az, &litude);
} while (feof(fd)==0);
fclose(fd);
/* Handle 0=360 degree ambiguity */
if ((read_count[0]==0) && (read_count[360]!=0))
{
read_count[0]=read_count[360];
azimuth[0]=azimuth[360];
}
if ((read_count[0]!=0) && (read_count[360]==0))
{
read_count[360]=read_count[0];
azimuth[360]=azimuth[0];
}
/* Average pattern values in case more than
one was read for each degree of azimuth. */
for (x=0; x<=360; x++)
{
if (read_count[x]>1)
azimuth[x]/=(float)read_count[x];
}
/* Interpolate missing azimuths
to completely fill the array */
last_index=-1;
next_index=-1;
for (x=0; x<=360; x++)
{
if (read_count[x]!=0)
{
if (last_index==-1)
last_index=x;