#!/usr/bin/perl -w
$| = 1;

# Load necessary modules.
use strict;
use lib '.';
use props;
use LWP::Simple;

# Assign global variables.
use vars qw(
        $query
        $op
        $showutc
        $showssn
        $showk
        $k
        $ssn
        $path
);

# Create a new CGI object.
$query = new CGI;

# Get the input.
$op = $query->param('op') || '';
$showutc = $query->param('showutc');
$showssn = $query->param('showssn');
$showk = $query->param('showk');
$k = $query->param('k');
$ssn = $query->param('ssn');
$path = $query->param('path');


if (!$path) {$path="s";}


my $wcypath = "datas/";
my $progname ="80mband.cgi";

my $utctime = gmtime;
my ($dayname, $month, $day, $time, $year, $res) = split (/\ /, $utctime);
   if ($day eq ""){$day = $time; $time = $year; $year = $res;}
my ($std,$min,$sek) = split (/\:/, $time);
my $monthnr = 0;

if ($month eq "Jan") {$monthnr=1;}
if ($month eq "Feb") {$monthnr=2;}
if ($month eq "Mar") {$monthnr=3;}
if ($month eq "Apr") {$monthnr=4;}
if ($month eq "May") {$monthnr=5;}
if ($month eq "Jun") {$monthnr=6;}
if ($month eq "Jul") {$monthnr=7;}
if ($month eq "Aug") {$monthnr=8;}
if ($month eq "Sep") {$monthnr=9;}
if ($month eq "Oct") {$monthnr=10;}
if ($month eq "Nov") {$monthnr=11;}
if ($month eq "Dec") {$monthnr=12;}

if ($day == 1 && $std == 0)
   {
   if ($monthnr==1)
      {
      $monthnr=12;
      $year--;
      }
   else {$monthnr--; }
   }

my $wcym ="";
if ($monthnr<10) {$wcym = "0" . $monthnr; }
else {$wcym = "$monthnr";}
my $monthpic = $wcym;

##### WCY-Datei prügen und ggf. neu runterladen

my $lastdowndat = (stat "dk0wcydat.txt")[9];
my $timenow = time;

my $difftime = ($timenow - $lastdowndat);


if ($difftime > 3600 )
   {
   getstore("http://dk0wcy.de/magnetogram/index.html", "dk0wcydat.txt");

   }

############## dk0wcydat.txt auslesen und umwandeln in wcydatas.txt

open (FC, "<dk0wcydat.txt");
chop (my @dk0wcydatas = <FC>);
close (FC);

my $dka=0;
my $dkk=0;
my $dkr=10;
my $dki=0;


for (my $w=0; $w<@dk0wcydatas; $w++)
   {
   ### A-Wert auslesen
   if ($w==29)
      {
      $dka=substr($dk0wcydatas[$w],65,1);
      my $tt1=substr($dk0wcydatas[$w],66,1);
      if ( int $tt1 eq $tt1) { $dka=($dka*10)+$tt1;}
      }


   ### K-Wert auslesen
   if ($w==30)
      {
      $dkk=substr($dk0wcydatas[$w],65,1);
      }


   ### R-Werte bzw SSN-Wert auslesen
   if ($w==39)
      {
      $dkr=substr($dk0wcydatas[$w],65,1);
      my $tt1=substr($dk0wcydatas[$w],66,1);
      my $tt2=substr($dk0wcydatas[$w],67,1);
      if ( int $tt1 eq $tt1 ) { $dkr=($dkr*10)+$tt1;}
      if ( int $tt2 eq $tt2 ) { $dkr=($dkr*10)+$tt2;}
      }


   ### SFI-Wert auslesen
   if ($w==44)
      {
      $dki=substr($dk0wcydatas[$w],51,1);
      my $tt1=substr($dk0wcydatas[$w],52,1);
      my $tt2=substr($dk0wcydatas[$w],53,1);
      if ( int $tt1 eq $tt1 ) { $dki=($dki*10)+$tt1;}
      if ( int $tt2 eq $tt2 ) { $dki=($dki*10)+$tt2;}

      }

   }

my $nday=0;
if ($day<10) {$nday = "0".$day;} else {$nday=$day;}
my $newdata="-,".$nday.$month.substr($year,2,2).",".(($std*60)+$min).",-,-,-,-,".$dkk.",-,".$dka.",-,-,-,-,".$dkr.",".$dki;

open (FH, "> wcydatas.txt");
print FH "$newdata\n";
close(FH);


############## wcydatas.txt auswerten

open (FD, "<wcydatas.txt");
chop (my @wcydatas = <FD>);
close(FD);

my $max = 0;
my $lastwcy ="";
for (my $x=0;$x<@wcydatas; $x++)
   {
   $max = $x;
   $lastwcy = "$wcydatas[$x]";

   }

   my @datas = split (/,/, $lastwcy);

   my $datum = $datas[1];
   my $zeit = $datas[2];
   my $ki = $datas[7];
   my $ai = $datas[9];
   my $ri = $datas[14];
   my $sfi = $datas[15];

   my $mina = $zeit % 60 ;
   my $tmp = $zeit - $mina;
   my $zeit2 = $tmp/60;

   my ($wmin,$wstd,$showdate);

   if ($mina < 10 ) {$wmin = "0" . $mina;}
   else {$wmin = "$mina";}

   if ($zeit2 < 10) { $wstd = "0" . $zeit2;}
   else {$wstd = $zeit2;}

   my $wday = substr($datum,0,2);
   my $wmonth = ucfirst(lc(substr($datum,2,3)));
   my $wyear = "20" . substr($datum,5,2);
   $showdate = $wday .".". $wmonth ." ". $wyear;

   my $logger = "DK0WCY";


###################


my $std1=$std;
my $back = "";
my $forward = "";

#sysopen (FG, "condx.txt", O_RDONLY);
#chomp (my @condx = <FG>);
#close(FG);
#
#my ($cpara0,$setk) = split (/\=/, $condx[0]);
#my ($cpara1,$setssn) = split (/\=/, $condx[1]);

my $setssn = $ri;
my $setk = $ki;


if ($op eq "set")
   {
   if (!$k or !$ssn) { }
   else
      {
      $setk = $k;
      $setssn = $ssn;
      open (FH, "> condx.txt");
      print FH "k=$k\n";
      print FH "ssn=$ssn\n";
      close (FH);
      }

   }

#   if (!$setk) {$setk = $ki;}

my $orissn=$setssn;
my $dossn = 0;
if (!$showssn) {$dossn = $setssn;$showssn=$setssn;}
else {$dossn = $showssn;}
my $dok = 1;

#if ((!$showk) && (!$setk)) {$dok = $ki; $showk = 3;}
if (!$showk) { $dok = $setk;$showk=$setk;}
else {$dok = $showk;}

if ($dok==10) { $dok=0;}

my $stemp1 = 0;

   if ($dossn<101)
      {
      for (my $z=0;$z<10;$z++)
         {
         $stemp1 += 10;
         if ($stemp1>$dossn && ($stemp1-$dossn)<11)
            {
            if (($stemp1-$dossn)<6) {$dossn = $stemp1;}
            else
               {
               if (($stemp1-$dossn)<11) {$dossn = ($stemp1-10);}
               }
            }
         }
      }

   if ($dossn>100 && $dossn<201)
      {
      $stemp1 += 100;
      for (my $z=0;$z<4;$z++)
         {
         $stemp1 += 25;
         if ($stemp1>$dossn && ($stemp1-$dossn)<26)
            {
            if (($stemp1-$dossn)<13) {$dossn = $stemp1;}
            else
               {
               if (($stemp1-$dossn)<26) {$dossn = ($stemp1-25);}
               }
            }
         }
      }

   if ($dossn>200 && $dossn<301)
      {
      $stemp1 += 200;
      for (my $z=0;$z<2;$z++)
         {
         $stemp1 += 50;
         if ($stemp1>$dossn && ($stemp1-$dossn)<51)
            {
            if (($stemp1-$dossn)<25) {$dossn = $stemp1;}
            else
               {
               if (($stemp1-$dossn)<51) {$dossn = ($stemp1-50);}
               }
            }
         }
      }
   if ($dossn>300) {$dossn=300;}
   my $stemp2 = 0;

   if ($orissn<101)
      {
      for (my $z=0;$z<10;$z++)
         {
         $stemp2 += 10;
         if ($stemp2>$orissn && ($stemp2-$orissn)<11)
            {
            if (($stemp2-$orissn)<6) {$orissn = $stemp2;}
            else
               {
               if (($stemp2-$orissn)<11) {$orissn = ($stemp2-10);}
               }
            }
         }
      }

   if ($orissn>100 && $orissn<201)
      {
      $stemp2 += 100;
      for (my $z=0;$z<4;$z++)
         {
         $stemp2 += 25;
         if ($stemp2>$orissn && ($stemp2-$orissn)<26)
            {
            if (($stemp2-$orissn)<13) {$orissn = $stemp2;}
            else
               {
               if (($stemp2-$orissn)<26) {$orissn = ($stemp2-25);}
               }
            }
         }
      }

   if ($orissn>200 && $orissn<301)
      {
      $stemp2 += 200;
      for (my $z=0;$z<2;$z++)
         {
         $stemp2 += 50;
         if ($stemp2>$orissn && ($stemp2-$orissn)<51)
            {
            if (($stemp2-$orissn)<25) {$orissn = $stemp2;}
            else
               {
               if (($stemp2-$orissn)<51) {$orissn = ($stemp2-50);}
               }
            }
         }
      }
   if ($orissn>300) {$orissn=300;}





   my $top = 5+ int(rand(30) + 1);

   my $rtime = time;
   my $rzeit = localtime $rtime;
   my ($rdayname, $rmonth, $rday, $rmtime, $ryear, $rres) = split (/\ /, $rzeit);
      if ($rday eq ""){$rday = $rmtime; $rmtime = $ryear; $ryear = $rres;}
   if (length($rday)<2) { $rday = "0" . $rday;}

   my ($rstd,$rmin,$rsek) = split (/\:/, $rmtime);

   my $rest = (((60-$rmin)*60)-$rsek+$top);

   if ($rest<30) {$rest=60+$top;}
   if ($rest>2400) {$rest=$rest-2400;}

if (!$showutc) {$showutc=$std;}
   else {$std = $showutc; $showutc=$std1;$rest = 300;}

   if ($op eq "set") { $rest = 10; }

   my $tpath="";
   if ($path eq "s") {$tpath="/shortpath/";}
   else {$tpath="/longpath/";}

   my $band1  = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/shortpath/" .$std ."UT-28MHz.gif";
   my $band1l = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/longpath/" .$std ."UT-28MHz.gif";
   my $band2  = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/shortpath/" .$std ."UT-21MHz.gif";
   my $band2l = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/longpath/" .$std ."UT-21MHz.gif";
   my $band3  = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/shortpath/" .$std ."UT-14MHz.gif";
   my $band3l = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/longpath/" .$std ."UT-14MHz.gif";
   my $band4  = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/shortpath/" .$std ."UT-07MHz.gif";
   my $band4l = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/longpath/" .$std ."UT-07MHz.gif";
   my $band5  = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/shortpath/" .$std ."UT-03MHz.gif";
   my $band5l = "http://dxpics.df0cg.de/gifs/".$monthpic."/k".$dok."/ssn".$dossn. "/longpath/" .$std ."UT-03MHz.gif";

   if ($dok==0) {$dok=10;}

print <<"HTML";
Content-type: text/html\n
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>DX-Propagation - DF0CG</title>
<meta http-equiv="Content-language" CONTENT="de">
<meta http-equiv="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<link href="http://dxprop.df0cg.de/style.css" rel="stylesheet" type="text/css">
<meta http-equiv="refresh" content="$rest;URL=$progname">
</head>
<body>
<table width="960" align="center">
<tr>
<td align="center">
</td>
</tr>
<tr>
<td>
<table align="center" width="100%">
<tr>
<td align="center"><a href="dxprop.cgi">
<img src="$band5" alt="" title="80m Band - 3.5MHz - $std:00 UT - K:$dok - SSN:$dossn"></a><br>
<a class="textsmall">80m - 3.5MHz - Shortpath</a>
<br>
</td>
<td align="center"><a href="dxprop.cgi">
<img src="$band5l" alt="" title="80m Band - 3.5MHz - $std:00 UT - K:$dok - SSN:$dossn"></a><br>
<a class="textsmall">80m - 3.5MHz - Longpath</a>
<br>
</td>
</tr>
</table><br>
<table width="400" align="center">
<tr>
<td align="center" valign="top">
<form method="post" action="$progname">
<div class="sidetopic">DX-Propagation at DF0CG/DR1A</div>
<div class="textsmall">$logger : SFI=$sfi SSN(R)=$setssn K=$setk A=$ai
HTML

   my $alter= $wday. "." .$wmonth. " " .$wyear. ", ".$wstd.".".$wmin."UT";

print <<"HTML";
($alter) <input type="submit" style="font-size: 6.5pt" value="set">
<input type="hidden" name="showk" value="$setk">
</div></form>
<table width="70%" align="center">
<tr>
HTML

 for (my $x=0; $x<24;$x++)
    {
    my $temp="";
    if ($x<10) {$temp ="0" . $x;}
    else {$temp ="$x";}
    if ($x==12)
       {
       print <<"HTML";
</tr>
<tr>
HTML
       }

    my $bgc = "#FFFFFF";
    if ($temp eq $std)
     {
     $bgc = "FF0000";


     if ($x<1) {$back = "23"; $forward = "01";}
     if ($x>22) {$back = "22"; $forward = "00";}
     if ($x>0 && $x<23)
       {
       my $tback = ($x-1);
       if ($tback<10) {$back = "0" . $tback;}
       else {$back ="$tback";}
       my $tfor = ($x+1);
       if ($tfor<10) {$forward = "0" . $tfor;}
       else {$forward = "$tfor";}

       }
     }
    if ($temp eq $std1)
       {
       if ($temp eq $std) {$bgc = "00FF00";}
       else { $bgc = "CCCCCC";}
       }

    print <<"HTML";
<td height="15" bgcolor="$bgc" align="center" class="textsmall">
<a href="$progname?showutc=$temp;showk=$dok;showssn=$dossn;path=$path"><strong>$temp</strong></a>
</td>
HTML

    }


 print <<"HTML";
</tr>
</table>
<table width="70%">
<tr>
<td align="left" class="text">
<a href="$progname?showutc=$back;showk=$dok;showssn=$dossn;path=$path" title="1h -"><strong> <<< </strong></a>
</td>
<td class="textsmall" align="center">
<table>
<tr>
<td align="left" class="textsmall">
K-Index:
</td>
HTML

if ($dok==10) {$dok=0;}

for (my $w=0;$w<10;$w++)
   {

    my $bgc = "FFFFFF";
    if ($w eq $dok)
     {
     $bgc = "FF0000";
     }

    if ($w eq $setk)
    {
    if ($dok eq $setk) {$bgc = "00FF00";}
    else { $bgc = "CCCCCC";}
    }

    my $temp54 = $w;
    if ($w==0) { $temp54 ="10";}

   print <<"HTML";
<td align="center" class="textsmall" bgcolor="$bgc" width="10">
<a href="$progname?showutc=$std;showk=$temp54;showssn=$dossn;path=$path"><strong>$w</strong></a>
</td>
HTML

   }

   if ($dok==0) {$dok=10;}

print <<"HTML";
</tr>
</table>
</td>
<td align="right" class="text">
<a href="$progname?showutc=$forward;showk=$dok;showssn=$dossn;path=$path" title="1h +"><strong> >>> </strong></a>
</td>
</tr>
</table>
<table>
<tr>
<td align="center" class="textsmall">SSN:
</td>
HTML

my $tssn = 0;
for (my $v=0;$v<17;$v++)
   {

    if ($v<11) {$tssn = $v*10;}
    if ($v>10 && $v<15) {$tssn = 100+(($v-10)*25);}
    if ($v>14) {$tssn = 200+(($v-14)*50);}

    my $bgc = "FFFFFF";
    if ($tssn eq $dossn)
     {
     $bgc = "FF0000";
     }

    if ($tssn eq $orissn)
    {
    if ($dossn eq $orissn) {$bgc = "00FF00";}
    else { $bgc = "CCCCCC";}
    }
   print <<"HTML";
<td align="center" class="textsmall" bgcolor="$bgc">
<a href="$progname?showutc=$std;showk=$dok;showssn=$tssn;path=$path"><strong>$tssn</strong></a>
</td>
HTML

   }

print <<"HTML";
</tr>
</table>
HTML

my $bgp1 = "00FF00";
my $bgp2 = "FF0000";

if ($path eq "s") {$bgp1="00FF00"; $bgp2="FF0000";}
else { $bgp1="FF0000"; $bgp2="00FF00";}


print <<"HTML";
<a class="textsmall"><br>
</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<br>
<table align="center">
<tr>
<td width="60" height="20" align="right"><strong>S/N Ratio:</strong></td>
<td width="65" height="20" align="center" bgcolor="999999" class="textsmall"> >= 0 dB</td>
<td width="65" height="20" align="center" bgcolor="4C33FF" class="textsmall">1-6 dB</td>
<td width="65" height="20" align="center" bgcolor="99CCFF" class="textsmall">7-12 dB</td>
<td width="65" height="20" align="center" bgcolor="66FFFF" class="textsmall">13-17 dB</td>
<td width="65" height="20" align="center" bgcolor="CCE5E5" class="textsmall">18-22 dB</td>
<td width="65" height="20" align="center" bgcolor="33FF00" class="textsmall">23-27 dB</td>
<td width="65" height="20" align="center" bgcolor="CCFF33" class="textsmall">28-32 dB</td>
<td width="65" height="20" align="center" bgcolor="FFFF22" class="textsmall">33-37 dB</td>
<td width="65" height="20" align="center" bgcolor="FFCC33" class="textsmall">38-42 dB</td>
<td width="65" height="20" align="center" bgcolor="FF4D33" class="textsmall">42-47 dB</td>
<td width="65" height="20" align="center" bgcolor="FFB2B2" class="textsmall"> > 47 dB</td>
</tr>
</table>
<br>
</td>
</tr>
<tr>
<td class="textsmall" align="center">
DJ6ET\@DF0CG Version 11 - February 2019<br>
<em>Maps generated by HamCAP v1.5 by VE3NEA</em><br><br>
Thanks to <a href="http://www.dk0wcy.de" target="_blank">Aurora-Beacon DK0WCY</a> using their datas to display these maps<br>
<br>
<table width="350" align="center">
<tr>
<td width="150" align="right" class="textsmall">Online-Besucher: <script language="JavaScript" type="text/Javascript" src="http://www.df0cg.de/cgi-bin/df0cg/dxprop-user/uo.cgi?out=js"> </script> </td>
<td width="50">&nbsp;</td>
<td width="150" align="left" class="textsmall">Besucher gesamt: <script language="JavaScript1.1" src="http://www.df0cg.de/cgi-bin/df0cg/dxprop-counter/ebcount.cgi?action=count&out=js"></script></td>
</tr>
</table>
<script language="JavaScript1.1" src="http://www.df0cg.de/cgi-bin/df0cg/user_counter/ebcount.cgi?action=count"></script>
</td>
</tr>
</table>
</body>
</html>

HTML

#