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