319866696bb3ad196ea268c91240693560406e1b
[spider.git] / perl / Minimuf.pm
1 #!/usr/bin/perl -w
2 # A perl Minimuf calculator, nicked from the minimuf program written in
3 # C.
4 #
5 # Translated and modified for my own purposes by Dirk Koopman G1TLH
6 #
7 # as fixed by Steve Franke K9AN
8 #
9 # Copyright (c) 1999 Dirk Koopman G1TLH
10 #
11 # The original copyright:-
12 #/***********************************************************************
13 # *                                                                     *
14 # * Copyright (c) David L. Mills 1994-1998                              *
15 # *                                                                     *
16 # * Permission to use, copy, modify, and distribute this software and   *
17 # * its documentation for any purpose and without fee is hereby         *
18 # * granted, provided that the above copyright notice appears in all    *
19 # * copies and that both the copyright notice and this permission       *
20 # * notice appear in supporting documentation, and that the name        *
21 # * University of Delaware not be used in advertising or publicity      *
22 # * pertaining to distribution of the software without specific,        *
23 # * written prior permission.  The University of Delaware makes no      *
24 # * representations about the suitability this software for any         *
25 # * purpose. It is provided "as is" without express or implied          *
26 # * warranty.                                                           *
27 # *                                                                     *
28 # ***********************************************************************
29 #
30 # MINIMUF 3.5 from QST December 1982
31 # (originally in BASIC)
32 #
33 # $Id$
34 #
35 #
36
37 package Minimuf;
38
39 use POSIX;
40
41 require Exporter;
42 @ISA = qw(Exporter);
43 @EXPORT = qw($pi $d2r $r2d $halfpi $pi2 $VOFL $R $hE $hF $GAMMA $LN10
44                     $MINBETA $BOLTZ $NTEMP $DELTAF $MPATH $GLOSS $SLOSS
45             $noise);
46
47 use strict;
48
49 use vars qw($VERSION $BRANCH);
50 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
51 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
52 $main::build += $VERSION;
53 $main::branch += $BRANCH;
54
55 use vars qw($pi $d2r $r2d $halfpi $pi2 $VOFL $R $hE $hF $GAMMA $LN10
56                     $MINBETA $BOLTZ $NTEMP $DELTAF $MPATH $GLOSS $SLOSS
57             $noise);
58  
59 $pi = 3.141592653589;
60 $d2r = ($pi/180);
61 $r2d = (180/$pi);
62 $halfpi = $pi/2;
63 $pi2 = $pi*2;
64 $VOFL = 2.9979250e8;                    # velocity of light
65 $R = 6371.2;                                    # radius of the Earth (km)  
66 $hE = 110;                                              # mean height of E layer (km) 
67 $hF = 320;                                              # mean height of F layer (km) 
68 $GAMMA = 1.42;                                  # geomagnetic constant 
69 $LN10 = 2.302585;                               # natural logarithm of 10 
70 $MINBETA = (10 * $d2r);                 # min elevation angle (rad) 
71 $BOLTZ = 1.380622e-23;                  # Boltzmann's constant 
72 $NTEMP = 290;                                   # receiver noise temperature (K) 
73 $DELTAF = 2500;                                 # communication bandwidth (Hz) 
74 $MPATH = 3;                                             # multipath threshold (dB) 
75 $GLOSS = 3;                                             # ground-reflection loss (dB) 
76 $SLOSS = 10;                                    # excess system loss 
77 $noise = 10 * log10($BOLTZ * $NTEMP * $DELTAF) + 30;
78
79 # basic SGN function
80 sub SGN
81 {
82         my $x = shift;
83         return 0 if $x == 0;
84         return ($x > 0) ? 1 : -1;
85 }
86
87 #
88 # MINIMUF 3.5 (From QST December 1982, originally in BASIC)
89 #
90
91 sub minimuf
92 {
93         my $flux = shift;               # 10-cm solar flux 
94         my $month = shift;              # month of year (1 - 12) 
95         my $day = shift;                # day of month (1 - 31) 
96         my $hour = shift;               # hour of day (utc) (0 - 23) 
97         my $lat1 = shift;               # transmitter latitude (deg n) 
98         my $lon1 = shift;               # transmitter longitude (deg w) 
99         my $lat2 = shift;               # receiver latitude (deg n) 
100         my $lon2 = shift;               # receiver longitude (deg w) 
101         
102         my $ssn;                # sunspot number dervived from flux 
103         my $muf;                # maximum usable frequency 
104         my $dist;               # path angle (rad) 
105         my ($a, $p, $q);                # unfathomable local variables 
106         my ($y1, $y2, $y3);
107         my ($t, $t4, $t9);
108         my ($g0, $g8);
109         my ($k1, $k6, $k8, $k9);
110         my ($m9, $c0);
111         my ($ftemp, $gtemp);    # volatile temps 
112         
113         # Determine geometry and invariant coefficients
114         $ssn = spots($flux);
115         $ftemp = sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) *
116             cos($lon2 - $lon1);
117         $ftemp = -1 if ($ftemp < -1);
118         $ftemp = 1 if ($ftemp > 1);
119         $dist = acos($ftemp);
120         $k6 = 1.59 * $dist;
121         $k6 = 1 if ($k6 < 1);
122         $p = sin($lat2);
123         $q = cos($lat2);
124         $a = (sin($lat1) - $p * cos($dist)) / ($q * sin($dist));
125         $y1 = 0.0172 * (10 + ($month - 1) * 30.4 + $day);
126         $y2 = 0.409 * cos($y1);
127         $ftemp = 2.5 * $dist / $k6;
128         $ftemp = $halfpi if ($ftemp > $halfpi);
129         $ftemp = sin($ftemp);
130         $m9 = 1 + 2.5 * $ftemp * sqrt($ftemp);
131         $muf = 100;
132
133         # Loop along path
134         for ($k1 = 1 / (2 * $k6); $k1 <= 1 - 1 / (2 * $k6); $k1 += abs(0.9999 - 1 / $k6)) {
135                 $gtemp = $dist * $k1;
136                 $ftemp = $p * cos($gtemp) + $q * sin($gtemp) * $a;
137                 $ftemp = -1 if ($ftemp < -1);
138                 $ftemp = 1 if ($ftemp > 1);
139                 $y3 = $halfpi - acos($ftemp);
140                 $ftemp = (cos($gtemp) - $ftemp * $p) / ($q * sqrt(1 - $ftemp * $ftemp));
141                 $ftemp = -1 if ($ftemp < -1);
142                 $ftemp = 1 if ($ftemp > 1);
143                 $ftemp = $lon2 + SGN(sin($lon1 - $lon2)) * acos($ftemp);
144                 $ftemp += $pi2 if ($ftemp < 0);
145                 $ftemp -= $pi2 if ($ftemp >= $pi2);
146                 $ftemp = 3.82 * $ftemp + 12 + 0.13 * (sin($y1) + 1.2 * sin(2 * $y1));
147                 $k8 = $ftemp - 12 * (1 + SGN($ftemp - 24)) * SGN(abs($ftemp - 24));
148                 if (cos($y3 + $y2) <= -0.26) {
149                         $k9 = 0;
150                         $g0 = 0;
151                 } else {
152                         $ftemp = (-0.26 + sin($y2) * sin($y3)) / (cos($y2) * cos($y3) + 0.001);
153                         $k9 = 12 - atan($ftemp / sqrt(abs(1 - $ftemp * $ftemp))) * 7.639437;
154                         $t = $k8 - $k9 / 2 + 12 * (1 - SGN($k8 - $k9 / 2)) * SGN(abs($k8 - $k9 / 2));
155                         $t4 = $k8 + $k9 / 2 - 12 * (1 + SGN($k8 + $k9 / 2 - 24)) * SGN(abs($k8 + $k9 / 2 - 24));
156                         $c0 = abs(cos($y3 + $y2));
157                         $t9 = 9.7 * pow($c0, 9.6);
158                         $t9 = 0.1 if ($t9 < 0.1);
159                         
160                         $g8 = $pi * $t9 / $k9;
161                         if (($t4 < $t && ($hour - $t4) * ($t - $hour) > 0.) || ($t4 >= $t && ($hour - $t) * ($t4 - $hour) <= 0)) {
162                                 $ftemp = $hour + 12 * (1 + SGN($t4 - $hour)) * SGN(abs($t4 - $hour));
163                                 $ftemp = ($t4 - $ftemp) / 2;
164                                 $g0 = $c0 * ($g8 * (exp(-$k9 / $t9) + 1)) * exp($ftemp) / (1 + $g8 * $g8);
165                         } else {
166                                 $ftemp = $hour + 12 * (1 + SGN($t - $hour)) * SGN(abs($t - $hour));
167                                 $gtemp = $pi * ($ftemp - $t) / $k9;
168                                 $ftemp = ($t - $ftemp) / $t9;
169                                 $g0 = $c0 * (sin($gtemp) + $g8 * (exp($ftemp) - cos($gtemp))) / (1 + $g8 * $g8);
170                                 $ftemp = $c0 * ($g8 * (exp(-$k9 / $t9) + 1)) * exp(($k9 - 24) / 2) / (1 + $g8 * $g8);
171                                 $g0 = $ftemp if ($g0 < $ftemp);
172                         }
173                 }
174                 $ftemp = (1 + $ssn / 250) * $m9 * sqrt(6 + 58 * sqrt($g0));
175                 $ftemp *= 1 - 0.1 * exp(($k9 - 24) / 3);
176                 $ftemp *= 1 + 0.1 * (1 - SGN($lat1) * SGN($lat2));
177                 $ftemp *= 1 - 0.1 * (1 + SGN(abs(sin($y3)) - cos($y3)));
178                 $muf = $ftemp if ($ftemp < $muf);
179         }
180         return $muf;
181 }
182
183 #
184 # spots(flux) - Routine to map solar flux to sunspot number.
185 #
186 # THis routine was done by eyeball and graph on p. 22-6 of the 1991
187 # ARRL Handbook. The nice curve fitting was done using Mathematica.
188
189 sub spots
190 {
191         my $flux = shift; # 10-cm solar flux 
192         my $ftemp;                      # double temp 
193
194         return 0 if ($flux < 65);
195         if ($flux < 110) {
196                 $ftemp = $flux - 200.6;
197                 $ftemp = 108.36 - .005896 * $ftemp * $ftemp;
198         } elsif ($flux < 213) {
199                 $ftemp = 60 + 1.0680 * ($flux - 110);
200         } else {
201                 $ftemp = $flux - 652.9;
202                 $ftemp = 384.0 - 0.0011059 * $ftemp * $ftemp;
203         }
204         return $ftemp;
205 }
206
207 # ion - determine paratmeters for hop h
208 #
209 # This routine determines the reflection zones for each hop along the
210 # path and computes the minimum F-layer MUF, maximum E-layer MUF,
211 # ionospheric absorption factor and day/night flags for the entire
212 # path.
213
214 sub ion
215 {
216         my $h = shift;                          # hop index
217         my $d = shift;                          # path angle (rad)
218         my $fcF = shift;                        # F-layer critical frequency 
219         my $ssn = shift;            # current sunspot number
220         my $lat1 = shift;
221         my $lon1 = shift;
222         my $b1 = shift;
223         my $b2 = shift;
224         my $lats = shift;
225         my $lons = shift;
226         
227         # various refs to arrays
228     my $daynight = shift;               # ref to daynight array one per hop
229         my $mufE = shift;
230         my $mufF = shift;
231         my $absorp = shift;
232         
233         my $beta;               # elevation angle (rad) 
234         my $psi;                # sun zenith angle (rad) 
235         my $dhop;               # hop angle / 2 (rad) 
236         my $dist;               # path angle (rad) 
237         my $phiF;               # F-layer angle of incidence (rad) 
238         my $phiE;               # E-layer angle of incidence (rad) 
239         my $fcE;                # E-layer critical frequency (MHz) 
240         my $ftemp;              # double temp 
241     
242
243         # Determine the path geometry, E-layer angle of incidence and
244         # minimum F-layer MUF. The F-layer MUF is determined from the
245         # F-layer critical frequency previously calculated by MINIMUF
246         # 3.5 and the secant law and so depends only on the F-layer
247         # angle of incidence. This is somewhat of a crock; however,
248         # doing it with MINIMUF 3.5 on a hop-by-hop basis results in
249         # rather serious errors.
250          
251
252         $dhop = $d / ($h * 2);
253         $beta = atan((cos($dhop) - $R / ($R + $hF)) / sin($dhop));
254         $ftemp = $R * cos($beta) / ($R + $hE);
255         $phiE = atan($ftemp / sqrt(1 - $ftemp * $ftemp));
256         $ftemp = $R * cos($beta) / ($R + $hF);
257         $phiF = atan($ftemp / sqrt(1 - $ftemp * $ftemp));
258         $absorp->[$h] = $mufE->[$h] = $daynight->[$h] = 0;
259         $mufF->[$h] = $fcF / cos($phiF);;
260         for ($dist = $dhop; $dist < $d; $dist += $dhop * 2) {
261
262                 # Calculate the E-layer critical frequency and MUF.
263                  
264                 $fcE = 0;
265                 $psi = zenith($dist, $lat1, $lon1, $b1, $b2, $lats, $lons);
266                 $ftemp = cos($psi);
267                 $fcE = .9 * pow((180. + 1.44 * $ssn) * $ftemp, .25) if ($ftemp > 0);
268                 $fcE = .005 * $ssn if ($fcE < .005 * $ssn);
269                 $ftemp = $fcE / cos($phiE);
270                 $mufE->[$h] = $ftemp if ($ftemp > $mufE->[$h]);
271
272                 # Calculate ionospheric absorption coefficient and
273                 # day/night indicators. Note that some hops along a
274                 # path can be in daytime and others in nighttime.
275
276                 $ftemp = $psi;
277                 if ($ftemp > 100.8 * $d2r) {
278                         $ftemp = 100.8 * $d2r;
279                         $daynight->[$h] |= 2;
280                 } else {
281                         $daynight->[$h] |= 1;
282                 }
283                 $ftemp = cos(90. / 100.8 * $ftemp);
284                 $ftemp = 0. if ($ftemp < 0.);
285                 $ftemp = (1. + .0037 * $ssn) * pow($ftemp, 1.3);
286                 $ftemp = .1 if ($ftemp < .1);
287                 $absorp->[$h] += $ftemp;
288         }
289 }
290
291
292 #
293 # pathloss(freq, hop) - Compute receive power for given path.
294 #
295 # This routine determines which of the three ray paths determined
296 # previously are usable. It returns the hop index of the best of these
297 # or zero if none are found.
298
299 sub pathloss
300 {
301         my $hop = shift;                        # minimum hops 
302         my $freq = shift;                       # frequency
303     my $txpower = shift || 20;  # transmit power 
304     my $rsens = shift || -123;  # receiver sensitivity
305     my $antgain = shift || 0;   # antenna gain
306                 
307     my $daynight = shift;               # ref to daynight array one per hop
308     my $beta = shift;
309         my $path = shift;
310         my $mufF = shift;
311         my $mufE = shift;
312     my $absorp = shift;
313     my $dB2 = shift;
314                         
315         my $h;                                          # hop number 
316         my $level;                                      # max signal (dBm) 
317         my $signal;                                     # receive signal (dBm) 
318         my $ftemp;                                      # double temp 
319         my $j;                                          # index temp 
320
321         #
322         # Calculate signal and noise for all hops. The noise level is
323         # -140 dBm for a receiver bandwidth of 2500 Hz and noise
324         # temperature 290 K. The receiver sensitivity is assumed -123
325         # dBm (0.15 V at 50 Ohm for 10 dB S/N). Paths where the signal
326         # is less than the noise or when the frequency exceeds the F-
327         # layer MUF are considered unusable.
328          
329         $level = $noise;
330         $j = 0;
331         for ($h = $hop; $h < $hop + 3; $h++) {
332                 $daynight->[$h] &= ~(4 | 8 | 16);
333                 if ($freq < 0.85 * $mufF->[$h]) {
334
335                         # Transmit power (dBm)
336                          
337                         $signal = $txpower + $antgain + 30;
338
339                         # Path loss
340                          
341                         $signal -= 32.44 + 20 * log10($path->[$h] * $freq) + $SLOSS;
342
343                         # Ionospheric loss
344                          
345                         $ftemp = $R * cos($beta->[$h]) / ($R + $hE);
346                         $ftemp = atan($ftemp / sqrt(1 - $ftemp * $ftemp));
347                         $signal -= 677.2 * $absorp->[$h] / cos($ftemp) / (pow(($freq + $GAMMA), 1.98) + 10.2);
348
349                         # Ground reflection loss
350                          
351                         $signal -= $h * $GLOSS;
352                         $dB2->[$h] = $signal;
353
354                         # Paths where the signal is greater than the
355                         # noise, but less than the receiver sensitivity
356                         # are marked 's'. Paths below the E-layer MUF
357                         # are marked 'e'. When comparing for maximum
358                         # signal, The signal for these paths is reduced
359                         # by 3 dB so they will be used only as a last
360                         # resort.
361                          
362                         
363                         $daynight->[$h] |= 4 if ($signal < $rsens);
364                         if ($freq < $mufE->[$h]) {
365                                 $daynight->[$h] |= 8;
366                                 $signal -= $MPATH;
367                         }
368                         if ($signal > $level) {
369                                 $level = $signal;
370                                 $j = $h;
371                         }
372                 }
373         }
374
375         # We have found the best path. If this path is less than 3 dB
376         # above the RMS sum of the other paths, the path is marked 'm'.
377          
378         return 0 if ($j == 0);
379
380         $ftemp = 0;
381         for ($h = $hop; $h < $hop + 3; $h++) {
382                 $ftemp += exp(2 / 10 * $dB2->[$h] * $LN10) if ($h != $j);
383         }
384         $ftemp = 10 / 2 * log10($ftemp);
385         $daynight->[$j] |= 16 if ($level < $ftemp + $MPATH);
386         
387         return $j;
388 }
389
390 # zenith(dist) - Determine sun zenith angle at reflection zone.
391
392 sub zenith
393 {
394         my $dist = shift;                       # path angle 
395         my $txlat = shift;          # tx latitude (rad)
396         my $txlong = shift;         # tx longitude (rad)
397         my $txbearing = shift;      # tx bearing
398         my $pathangle = shift;      # 'b1'
399         my $lats = shift;           # subsolar latitude
400         my $lons = shift;           # subsolar longitude
401                                 
402         my ($latr, $lonr);                      # reflection zone coordinates (rad) 
403         my $thetar;                                     # reflection zone angle (rad) 
404         my $psi;                                        # sun zenith angle (rad) 
405
406         # Calculate reflection zone coordinates.
407          
408         $latr = acos(cos($dist) * sin($txlat) + sin($dist) * cos($txlat) * cos($txbearing));
409         $latr += $pi if ($latr < 0);
410         $latr = $halfpi - $latr;
411         $lonr = acos((cos($dist) - sin($latr) * sin($txlat)) / (cos($latr) * cos($txlat)));
412         $lonr += $pi if ($lonr < 0);
413         $lonr = - $lonr if ($pathangle < 0);
414         $lonr = $txlong - $lonr;
415         $lonr -= $pi2 if ($lonr >= $pi);
416         $lonr += $pi2 if ($lonr <= -$pi);
417         $thetar = $lons - $lonr;
418         $thetar = $pi2 - $thetar if ($thetar > $pi);
419         $thetar -= $pi2 if ($thetar < - $pi);
420         
421         # Calculate sun zenith angle.
422          
423         $psi = acos(sin($latr) * sin($lats) + cos($latr) * cos($lats) * cos($thetar));
424         $psi += $pi if ($psi < 0);
425         return($psi);
426 }
427
428 #  official minimuf version of display          
429 sub dsx
430 {
431         my $h = shift;
432         my $rsens = shift;
433         my $dB2 = shift;
434         my $daynight = shift;
435         
436         my $c1;
437         my $c2;
438
439         return "       " unless $h;
440         
441         if (($daynight->[$h] & 3) == 3) {
442                 $c1 = 'x';
443         } elsif ($daynight->[$h] & 1) {
444                 $c1 = 'j';
445         } elsif ($daynight->[$h] & 2) {
446                 $c1 = 'n';
447         }
448         if ($daynight->[$h] & 4) {
449                 $c2 = 's';
450         } elsif ($daynight->[$h] & 16) {
451                 $c2 = 'm';
452         } else {
453                 $c2 = ' ';
454         }
455     return sprintf("%4.0f%s%1d%s", $dB2->[$h] - $rsens, $c1, $h, $c2)
456 }               
457
458 #  my version
459 sub ds
460 {
461         my $h = shift;
462         my $rsens = shift;
463         my $dB2 = shift;
464         my $daynight = shift;
465         
466         my $c2;
467
468         return "    " unless $h;
469         
470         if ($daynight->[$h] & 4) {
471                 $c2 = 's';
472         } elsif ($daynight->[$h] & 16) {
473                 $c2 = 'm';
474         } else {
475                 $c2 = ' ';
476         }
477         my $l = $dB2->[$h] - $rsens;
478         my $s = int $l / 6;
479         $s = 9 if $s > 9;
480         $s = 0 if $s < 0;
481     my $plus = (($l / 6) >= $s + 0.5) ? '+' : ' ';
482         
483     return "$c2". "S$s$plus";
484 }               
485
486 1;