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