# 3xt2png-multi-delay.pl Version 3
# Plot signal strength and show areas where combined signals fall
# outside a median current generation ATSC receiver's equalizer range

use GD;

# Open input and output file. 
# Add support for command line arguments in future versions
open (MULTXY, "delay_xy.txt") or die "Can't open delay_xy.txt! \n"; 
# open (TESTIMG,">xyimagtest.txt") or die "Can't open xyimagtest.txt! \n";

print "Converting SPLAT Multi XY data to an image file....\n";

# Initialize variable
$tx1offset = 0;
# Repeater delay = time to travel 26.61 km (88.76 microseconds) plus repeater delay 
$tx2offset = 95.76;

$idx = 1;
$ymax = 1200;
$ymin = 1200;
$xmax = 1200;
$xmin = 1200;
$width = 1;
$height = 1;
$count = 0;

# Read the processed SPLAT text file, 
# Obtain the corners and size of the image from the first two lines
# Extract the latitude x 1200, longitude x 1200 and field strength date from following lines
# Convert lat/long data into Y and X coordinates and field strength into a color
# Create GDImage from X, Y and color data
while (<MULTXY>) {
    $temp = $_;
    chomp($temp);	
    if ($idx > 2) {
        @data = split /\s/,  ($temp);
        $x = $xmax - $data[1];
        $y = $ymax - $data[0];
        $fs1 = $data[2];
        $fs2 = $data[3];
        $tx1delay = $data[4] + $tx1offset ;
        $tx2delay = $data[5] + $tx2offset ;
	if ($fs1 >= $fs2) {$delay = ( $tx2delay - $tx1delay ); } else { $delay = ( $tx1delay - $tx2delay ); };

#     print TESTIMG $data[0]." ".$data[1]." ".$data[2]." ".$x." ".$y." ".$fs." ".$temp;
        if (($fs1 < 41) && ($fs2 < 41)) {
            $im->setPixel($x,$y,$gray);
        }       
        elsif (($fs1 >= 88) || ($fs2 >= 88)) {
            $im->setPixel($x,$y,$bright_green);
        }
        elsif (($fs1 >= 78) || ($fs2 >= 78)) {
            $im->setPixel($x,$y,$green_yellow);
        }
        elsif (($fs1 >= 68) || ($fs2 >= 68)) {
            $im->setPixel($x,$y,$yellow);
        }
        elsif (($fs1 >= 58) || ($fs2 >= 58)) {
            $im->setPixel($x,$y,$light_orange);
        }
        elsif (($fs1 >= 48) || ($fs2 >= 48)) {
            $im->setPixel($x,$y,$dark_orange);
        }
        else {
            $im->setPixel($x,$y,$red);
        };
	if (($fs1 >= 41) || ($fs2 >= 41)) {
           if ((( $fs1 >= $fs2) && (($fs1 - $fs2) < 15.5 )) || (($fs2 >= $fs1) && (($fs2 - $fs1) < 15.5 )))  {
	       if  ( ( ($delay) <= -70 ) && 
	             ( ( $fs1 - $fs2) < 15.0 ) ) {$im->setPixel($x,$y,$magenta)};  		  
	       if  ( ( ( ($delay) > -70 ) && ( ($delay) <= -60 ) ) &&
	             ( ( $fs1 - $fs2) < 11.3  ) ) {$im->setPixel($x,$y,$magenta)};  		  
	       if  ( ( ( ($delay) > -60 ) && ( ($delay) <= -50 ) ) &&
	             ( ( $fs1 - $fs2) < 8.8  ) ) {$im->setPixel($x,$y,$magenta)};  		  
	       if  ( ( ( ($delay) > -50 ) && ( ($delay) <= -40 ) ) &&
	             ( ( $fs1 - $fs2) < 7.5  ) ) {$im->setPixel($x,$y,$violet)};  		  
	       if  ( ( ( ($delay) > -40 ) && ( ($delay) <= -30 ) ) &&
	             ( ( $fs1 - $fs2) < 6.0  ) ) {$im->setPixel($x,$y,$violet)};  		  
	       if  ( ( ( ($delay) > -30 ) && ( ($delay) <= -25 ) ) &&
	             ( ( $fs1 - $fs2) < 5.3 ) ) {$im->setPixel($x,$y,$blue)};  		  
	       if  ( ( ( ($delay) > -25 ) && ( ($delay) <= -20 ) ) &&
	             ( ( $fs1 - $fs2) < 4.5 ) ) {$im->setPixel($x,$y,$blue)};  		  
	       if  ( ( ( ($delay) > -20 ) && ( ($delay) <= -15 ) ) &&
	             ( ( $fs1 - $fs2) < 3.5 ) ) {$im->setPixel($x,$y,$light_blue)};  		  
	       if  ( ( ( ($delay) > -15 ) && ( ($delay) <= -9 ) ) &&
	             ( ( $fs1 - $fs2) < 2.5 ) ) {$im->setPixel($x,$y,$light_blue)};  		  
	       if  ( ( ( ($delay) > -9 ) && ( ($delay) <= -8 ) ) &&
	             ( ( $fs1 - $fs2) < 2.0 ) ) {$im->setPixel($x,$y,$teal)};  		  
#	       if  ( ( ( ($delay) > -8 ) && ( ($delay) <= -5 ) ) &&
#	             ( ( $fs1 - $fs2) < 1.5) ) {$im->setPixel($x,$y,$teal)};  		  
#	       if  ( ( ( ($delay) > -5 ) && ( ($delay) <= -3 ) ) &&
#	             ( ( $fs1 - $fs2) < 1.3) ) {$im->setPixel($x,$y,$teal)};
#	       if  ( ( ( ($delay) > -3 ) && ( ($delay) <= -2 ) ) &&
#		             ( ( $fs1 - $fs2) < 1.2) ) {$im->setPixel($x,$y,$teal)};  		  
#	       if  ( ( ( ($delay) > -2 ) && ( ($delay) <= -1 ) ) &&
#	             ( ( $fs1 - $fs2) < 0.7) ) {$im->setPixel($x,$y,$cyan)};  		  
#	       if  ( ( ( ($delay) > -1 ) && ( ($delay) <= 2 ) ) &&
#	             ( ( $fs1 - $fs2) < 0.5) ) {$im->setPixel($x,$y,$cyan)};  		  
#	       if  ( ( ( ($delay) > 2 ) && ( ($delay) <= 3 ) ) &&
#	             ( ( $fs1 - $fs2) < 0.7) ) {$im->setPixel($x,$y,$cyan)};  		  
#	       if  ( ( ( ($delay) > 3 ) && ( ($delay) <= 7 ) ) &&
#	             ( ( $fs1 - $fs2) < 0.8) ) {$im->setPixel($x,$y,$cyan)};  		  
#	       if  ( ( ( ($delay) > 7 ) && ( ($delay) <= 8 ) ) &&
#	             ( ( $fs1 - $fs2) < 1.0) ) {$im->setPixel($x,$y,$cyan)};  		  
#	       if  ( ( ( ($delay) > 8 ) && ( ($delay) <= 20 ) ) &&
#	             ( ( $fs1 - $fs2) < 1.3) ) {$im->setPixel($x,$y,$teal)};  		  
#	       if  ( ( ( ($delay) > 20 ) && ( ($delay) <= 25 ) ) &&
#	             ( ( $fs1 - $fs2) < 1.5) ) {$im->setPixel($x,$y,$teal)};  		  
	       if  ( ( ( ($delay) > 25 ) && ( ($delay) <= 30 ) ) &&
	             ( ( $fs1 - $fs2) < 1.8) ) {$im->setPixel($x,$y,$teal)};  		  
	       if  ( ( ( ($delay) > 30 ) && ( ($delay) <= 40 ) ) &&
	             ( ( $fs1 - $fs2) < 2.3) ) {$im->setPixel($x,$y,$light_blue)};
	       if  ( ( ( ($delay) > 40 ) && ( ($delay) <= 50 ) ) &&
	             ( ( $fs1 - $fs2) < 2.5) ) {$im->setPixel($x,$y,$blue)};
	       if  ( ( ( ($delay) > 50 ) && ( ($delay) <= 70 ) ) &&
	             ( ( $fs1 - $fs2) < 11.5) ) {$im->setPixel($x,$y,$violet)};
	       if  ( ( ( ($delay) > 70 ) && ( ($delay) <= 80 ) ) &&
	             ( ( $fs1 - $fs2) < 15.0) ) {$im->setPixel($x,$y,$magenta)};
	       if  ( ( ( $delay ) > 80 ) &&
	             ( ( $fs1 - $fs2) < 15.0 ) ) {$im->setPixel($x,$y,$magenta)};
           };
	};
    }
    elsif ($idx == 1) {
        @corners = split /,\s/, $temp;
        $xmax = 1200 * $corners[1];
        $xmin = 1200 * $corners[2];
        $width = $xmax - $xmin;
        print "  width = ".$width."\n";
    }
    else {
        @corners = split /,\s/, $temp;
        $ymax = 1200 * $corners[1];
        $ymin = 1200 * $corners[2];
        $height = $ymax - $ymin;
        print "  height = ".$height."\n";
#       Create blank image
        $im = new GD::Image($width, $height);
# Define colors
        $gray = $im->colorAllocate(128,128,128);
        $white = $im->colorAllocate(255,255,255);
        $black = $im->colorAllocate(0,0,0);
     
        $cyan = $im->colorAllocate(0,255,255);
        $teal = $im->colorAllocate(30,222,222);
        $light_blue = $im->colorAllocate(128,128,255);
        $blue = $im->colorAllocate(0,0,255);
        $violet = $im->colorAllocate(160,0,255);
        $magenta = $im->colorAllocate(255,0,255);

        $bright_green = $im->colorAllocate(0,255,0);
        $green_yellow = $im->colorAllocate(192,255,0);
        $yellow = $im->colorAllocate(255,255,0);
        $light_orange = $im->colorAllocate(255,220,0);
        $dark_orange = $im->colorAllocate(235,114,0);
        $red = $im->colorAllocate(255,0,0);
#        $im->transparent($gray);
    };
$idx++;
};
# Close input file
close SPLATXY;

# Display status information
print "\n";
print $idx." records processed. \n";
print 'xmin = '.$xmin.'  xmax = '.$xmax.'  ymin = '.$ymin.'  ymax = '.$ymax."\n";
print 'height = '.$height.'  width = '.$width."\n";

#Write PNG image file
$pngdata = $im->png(6);
open (PNGPLOT,">pngmultiplot-RF198-7us-proc-delay.png") or die "Can't open pngmultiplot-RF198-7us-proc-delay.png! \n";
binmode PNGPLOT;
print PNGPLOT $pngdata;
close PNGPLOT;

#Write KML file
open (KMLFILE,">pngmultiplot-RF198-7us-proc-delay.kml") or die "Can't open pngmultiplot-RF198-7us-proc-delay.kml! \n";
print KMLFILE '<?xml version="1.0" encoding="UTF-8"?>'."\n";
print KMLFILE '<kml xmlns="http://www.opengis.net/kml/2.2">'."\n";
print KMLFILE '  <Folder>'."\n";
print KMLFILE '    <name>SPLAT!</name>'."\n";
print KMLFILE '    <description>Longley-Rice signal and interference map</description>'."\n";
print KMLFILE '    <GroundOverlay>'."\n";
print KMLFILE '      <name>SPLAT! Signal Strength Overlay</name>'."\n";
print KMLFILE '      <Icon>'."\n";
print KMLFILE '        <href>pngmultiplot-RF198-7us-proc-delay.png</href>'."\n";
print KMLFILE '      </Icon>'."\n";
print KMLFILE '      <LatLonBox>'."\n";
print KMLFILE '        <north>'.(($ymax) / 1200).'</north>'."\n";
print KMLFILE '        <south>'.(($ymin) / 1200).'</south>'."\n";
print KMLFILE '        <east>'.-(($xmin) / 1200).'</east>'."\n";
print KMLFILE '        <west>'.-(($xmax) / 1200).'</west>'."\n";
print KMLFILE '        <rotation>-0.000000</rotation>'."\n";
print KMLFILE '      </LatLonBox>'."\n";
print KMLFILE '    </GroundOverlay>'."\n";
print KMLFILE '  </Folder>'."\n";
print KMLFILE '</kml>'."\n";
close KMLFILE;

# close TESTIMG;
exit;