# 3xt2png-multi-3tx.pl 
# Plot signal strength from 3 transmitters and show strongest signal,
# delay between transmitters and 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_xy3.txt") or die "Can't open delay_xy3.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;
$tx2offset = 95.76;
$tx3offset = 85.1;

$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 = $_;
    if ($idx > 2) {
        @data = split /\s/, $temp;
        $x = $xmax - $data[1];
        $y = $ymax - $data[0];
        $fs1 = $data[2];
        $fs2 = $data[3];
#        $fs3 = -999;
        $fs3 = $data[4];
        $tx1delay = $data[5] + $tx1offset ;
        $tx2delay = $data[6] + $tx2offset ;
        $tx3delay = $data[7] + $tx3offset ;

#     print TESTIMG $data[0]." ".$data[1]." ".$data[2]." ".$x." ".$y." ".$fs." ".$temp;
#     set default (transparent) color for maxfs.png, besttx.png, eqfail.png, maxechodelay.png
	if ( ( ( $fs1 < 41 ) && ( $fs2 < 41 ) ) && ( $fs3 < 41) ) {
            $im->setPixel($x,$y,$gray);
            $im2->setPixel($x,$y,$gray2); 
            $im3->setPixel($x,$y,$gray3); 
            $im4->setPixel($x,$y,$gray4);	   
            $im5->setPixel($x,$y,$gray5);	   

        } else {
	    if ( $fs1 > $fs2 ) {
	      $fsA = $fs1;
	      $txAdelay = $tx1delay;
	      $fsB = $fs2;
	      $txBdelay = $tx2delay;
              $im2->setPixel($x,$y,$besttx1);
	    } else {
	      $fsA = $fs2;
	      $txAdelay = $tx2delay;
	      $fsB = $fs1; 
	      $txBdelay = $tx1delay;
              $im2->setPixel($x,$y,$besttx2); 
	    };
	    if ( $fs3 > $fsA ) {
	      $fsC = $fsB;
	      $txCdelay = $txBdelay;
	      $fsB = $fsA;
	      $txBdelay = $txAdelay;
	      $fsA = $fs3;
	      $txAdelay = $tx3delay;
              $im2->setPixel($x,$y,$besttx3); 
	    } else {
	      if ( fs3 < fsB ) {
		$fsC = $fs3;
		$txCdelay = $tx3delay;
	      } else {
		$fsC = $fsB;
		$txCdelay = $txBdelay;
		$fsB = $fs3;
		$txBdelay = $tx3delay;
	      };
            };
	    if ( $fsA >= 98 ) { $im->setPixel($x,$y,$dbfs98);
              } elsif ( $fsA >= 88 ) { $im->setPixel($x,$y,$dbfs88);
              } elsif ( $fsA >= 78 ) { $im->setPixel($x,$y,$dbfs78);
              } elsif ( $fsA >= 68 ) { $im->setPixel($x,$y,$dbfs68);
              } elsif ( $fsA >= 58 ) { $im->setPixel($x,$y,$dbfs58);
              } elsif ( $fsA >= 48 ) { $im->setPixel($x,$y,$dbfs48);
            } else  { $im->setPixel($x,$y,$dbfs41);
            };
            if ( ( $fsA < 41 ) || ( $fsA - $fsB > 15 ) ) {
               $im3->setPixel($x,$y,$gray3);
            } elsif ( $fsA - $fsB < 15) {   
              $fsdiffAB = ( $fsA - $fsB );
              $delaydiffBA = $txBdelay - $txAdelay;
              if ( $delaydiffBA <= -65 ) {
                  $im3->setPixel($x,$y,$eqfailpre2); 
              } elsif ( $delaydiffBA <= -55 ) {
                  if ( $fsdiffAB <= 11.3 ) { 
                    $im3->setPixel($x,$y,$eqfailpre2); 
                    };
              } elsif ( $delaydiffBA <= -45 ) {
                  if ( $fsdiffAB <= 8.8 ) { 
                    $im3->setPixel($x,$y,$eqfailpre2); 
                    };
              } elsif ( $delaydiffBA <= -35 ) {
                  if ( $fsdiffAB <= 7.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffBA <= -27.5 ) {
                  if ( $fsdiffAB <= 6.0 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffBA <= -22.5 ) {
                  if ( $fsdiffAB <= 5.3 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffBA <= -17.5 ) {
                  if ( $fsdiffAB <= 4.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffBA <= -12.5 ) {
                  if ( $fsdiffAB <= 2.8 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffBA <= -8.5 ) {
                  if ( $fsdiffAB <= 2.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffBA <= -7.5 ) {
                  if ( $fsdiffAB <= 2.0 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= -4.5 ) {
                  if ( $fsdiffAB <= 1.5 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= -1.5 ) {
                  if ( $fsdiffAB <= 1.2 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= 8.5 ) {
                  if ( $fsdiffAB <= 1.0 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= 22.5 ) {
                  if ( $fsdiffAB <= 1.3 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= 27.5 ) {
                  if ( $fsdiffAB <= 1.5 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= 30 ) {
                  if ( $fsdiffAB <= 1.75 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= 40 ) {
                  if ( $fsdiffAB <= 2.0 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffBA <= 60 ) {
                  if ( $fsdiffAB <= 2.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpost); 
                    };
              } elsif ( $delaydiffBA <= 65 ) {
                  if ( $fsdiffAB <= 7.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpost); 
                    };
              } elsif ( $delaydiffBA <= 75 ) {
                  if ( $fsdiffAB <= 11.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpost2); 
                    };
              } elsif ( $delaydiffBA > 75 ) {
                  if ( $fsdiffAB < 15.0 ) { 
                    $im3->setPixel($x,$y,$eqfailpost2); 
                    };
              } else {
                  $im3->setPixel($x,$y,$gray3); 
              };
            } else {
                $im3->setPixel($x,$y,$gray3); 
            }; 
            if ( ( $fsA < 41 ) ||  ( $fsA - $fsC > 15 ) ) {
               $im3->setPixel($x,$y,$gray3);
            } elsif ( $fsA - $fsC < 15) {   
              $fsdiffAC = ( $fsA - $fsC );
              $delaydiffCA = $txCdelay - $txAdelay;
              if ( $delaydiffCA <= -65 ) {
                  $im3->setPixel($x,$y,$eqfailpre2); 
              } elsif ( $delaydiffCA <= -55 ) {
                  if ( $fsdiffAC <= 11.3 ) { 
                    $im3->setPixel($x,$y,$eqfailpre2); 
                    };
              } elsif ( $delaydiffCA <= -45 ) {
                  if ( $fsdiffAC <= 8.8 ) { 
                    $im3->setPixel($x,$y,$eqfailpre2); 
                    };
              } elsif ( $delaydiffCA <= -35 ) {
                  if ( $fsdiffAC <= 7.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffCA <= -27.5 ) {
                  if ( $fsdiffAC <= 6.0 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffCA <= -22.5 ) {
                  if ( $fsdiffAC <= 5.3 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffCA <= -17.5 ) {
                  if ( $fsdiffAC <= 4.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffCA <= -12.5 ) {
                  if ( $fsdiffAC <= 2.8 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffCA <= -8.5 ) {
                  if ( $fsdiffAC <= 2.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpre); 
                    };
              } elsif ( $delaydiffCA <= -7.5 ) {
                  if ( $fsdiffAC <= 2.0 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= -4.5 ) {
                  if ( $fsdiffAC <= 1.5 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= -1.5 ) {
                  if ( $fsdiffAC <= 1.2 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= 8.5 ) {
                  if ( $fsdiffAC <= 1.0 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= 22.5 ) {
                  if ( $fsdiffAC <= 1.3 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= 27.5 ) {
                  if ( $fsdiffAC <= 1.5 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= 30 ) {
                  if ( $fsdiffAC <= 1.75 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= 40 ) {
                  if ( $fsdiffAC <= 2.0 ) { 
                    $im3->setPixel($x,$y,$eqfailmid); 
                    };
              } elsif ( $delaydiffCA <= 60 ) {
                  if ( $fsdiffAC <= 2.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpost); 
                    };
              } elsif ( $delaydiffCA <= 65 ) {
                  if ( $fsdiffAC <= 7.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpost); 
                    };
              } elsif ( $delaydiffCA <= 75 ) {
                  if ( $fsdiffAC <= 11.5 ) { 
                    $im3->setPixel($x,$y,$eqfailpost2); 
                    };
              } elsif ( $delaydiffCA > 75 ) {
                  if ( $fsdiffAC < 15.0 ) { 
                    $im3->setPixel($x,$y,$eqfailpost2); 
                    };
              } else {
                  $im3->setPixel($x,$y,$gray3);
              };   
            } else {
                $im3->setPixel($x,$y,$gray3); 
            };
            if ( $fsA <= 41 ) {
	      $im4->setPixel($x,$y,$gray4);
	    } else {  
              if ( ( ( $txBdelay - $txAdelay ) >= 80 ) && ( ($fsA - $fsB) < 15 ) ) {
                $im4->setPixel($x,$y,$postecho80);
	        } elsif ( ( ( $txBdelay - $txAdelay ) <= -70 )  && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$preecho70);
	        } elsif ( ( ( $txBdelay - $txAdelay ) <= -45 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$preecho45);
	        } elsif ( ( ( $txBdelay - $txAdelay ) <= -30 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$preecho30);
	        } elsif ( ( ( $txBdelay - $txAdelay ) <= -10 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$preecho10);
	        } elsif ( ( ( $txBdelay - $txAdelay ) <= -2 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$preecho2);
	        } elsif ( ( ( $txBdelay - $txAdelay ) >= 5 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$postecho5);
	        } elsif ( ( ( $txBdelay - $txAdelay ) >= 10 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$postecho10);
	        } elsif ( ( ( $txBdelay - $txAdelay ) >= 45 ) && ( ($fsA - $fsB) < 15 ) ) {  
                $im4->setPixel($x,$y,$postecho45);
	      } else {  
                if ( ($fsA - $fsB) < 15 ) { $im4->setPixel($x,$y,$echo0);
                } else { $im4->setPixel($x,$y,$gray4); } 
              };
            };
            if ( ( $fsA - $fsB ) > 15 ) { $im5->setPixel($x,$y,$fsdiff15);
              } elsif ( ( $fsA - $fsB ) >= 12 ) { $im5->setPixel($x,$y,$fsdiff12);
              } elsif ( ( $fsA - $fsB ) >= 9 ) { $im5->setPixel($x,$y,$fsdiff9);
              } elsif ( ( $fsA - $fsB ) >= 6 ) { $im5->setPixel($x,$y,$fsdiff6);
              } elsif ( ( $fsA - $fsB ) >= 3 ) { $im5->setPixel($x,$y,$fsdiff3);
              } elsif ( ( $fsA - $fsB ) >= 2 ) { $im5->setPixel($x,$y,$fsdiff2);
            } else { $im5->setPixel($x,$y,$fsdiff0);
            };
        };
    }
    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 images - maxfs.png, besttx.png, eqfail.png, maxechodelay.png
        $im = new GD::Image->newTrueColor($width, $height);
        $im2 = new GD::Image->newTrueColor($width, $height);
        $im3 = new GD::Image->newTrueColor($width, $height);
        $im4 = new GD::Image->newTrueColor($width, $height);
        $im5 = new GD::Image->newTrueColor($width, $height);

# Define background colors
        $gray = $im->colorAllocate(0,0,0);
        $gray2 = $im2->colorAllocate(0,0,0);
        $gray3 = $im3->colorAllocate(0,0,0);
        $gray4 = $im4->colorAllocate(0,0,0);
        $gray5 = $im5->colorAllocate(0,0,0);
     
# Colors for maxechodelay.png
        $preecho70 = $im4->colorAllocate(197,220,20); # Yellow-green   
        $preecho45 = $im4->colorAllocate(64,255,64);  # green    
        $preecho30 = $im4->colorAllocate(0,255,192);  # cyan
        $preecho10 = $im4->colorAllocate(96,96,255);  # light blue
        $preecho2 = $im4->colorAllocate(192,128,255); # magenta
        $echo0 = $im4->colorAllocate(255,255,255);    # white
        $postecho5 = $im4->colorAllocate(255,182,192); # pink
        $postecho10 = $im4->colorAllocate(255,64,64);  # red
        $postecho45 = $im4->colorAllocate(255,192,64); # orange
        $postecho80 = $im4->colorAllocate(255,255,64); # yellow
    
# Colors for eqfail
        $eqfailpre2 = $im3->colorAllocate(255,182,182); # pink 
        $eqfailpre = $im3->colorAllocate(252,214,41); # gold 
        $eqfailmid = $im3->colorAllocate(0,255,192); # cyan 
        $eqfailpost = $im3->colorAllocate(192,96,255); # violet 
        $eqfailpost2 = $im3->colorAllocate(0,0,255); # blue 

# Colors for best transmitter besttx,png (strongest signal)
        $besttx1 = $im2->colorAllocate(0,255,0); # green
        $besttx2 = $im2->colorAllocate(0,0,255); # blue
        $besttx3 = $im2->colorAllocate(220,80,255); # lavender
        $besttx4 = $im2->colorAllocate(255,0,0); # red

# Colors for max signal
        $dbfs98 = $im->colorAllocate(0,192,0);   # dark greeen
        $dbfs88 = $im->colorAllocate(64,255,64); # light green
        $dbfs78 = $im->colorAllocate(192,255,0); # green yellow
        $dbfs68 = $im->colorAllocate(255,255,0); # yellow
        $dbfs58 = $im->colorAllocate(255,220,0); # light orange
        $dbfs48 = $im->colorAllocate(235,114,0); # dark orange
        $dbfs41 = $im->colorAllocate(255,0,0);   # red

# Colors for A to B signal difference 
        $fsdiff15 = $im5->colorAllocate(0,192,0);   # dark greeen
        $fsdiff12 = $im5->colorAllocate(64,255,64); # light green
        $fsdiff9 = $im5->colorAllocate(192,255,0); # green yellow
        $fsdiff6 = $im5->colorAllocate(255,255,0); # yellow
        $fsdiff3 = $im5->colorAllocate(255,220,0); # light orange
        $fsdiff2 = $im5->colorAllocate(235,114,0); # dark orange
        $fsdiff0 = $im5->colorAllocate(255,0,0);   # red

# Set transparency
        $im5->transparent($gray5);
        $im4->transparent($gray4);
        $im3->transparent($gray3);
        $im2->transparent($gray2);
        $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,">maxfs.png") or die "Can't open maxfs.png! \n";
binmode PNGPLOT;
print PNGPLOT $pngdata;
close PNGPLOT;
$pngdata = 0;
$pngdata = $im2->png(6);
open (PNGPLOT,">besttx.png") or die "Can't open besttx.png! \n";
binmode PNGPLOT;
print PNGPLOT $pngdata;
close PNGPLOT;
$pngdata = 0;
$pngdata = $im3->png(6);
open (PNGPLOT,">eqfail.png") or die "Can't open eqfail.png! \n";
binmode PNGPLOT;
print PNGPLOT $pngdata;
close PNGPLOT;
$pngdata = 0;
$pngdata = $im4->png(6);
open (PNGPLOT,">maxechodelay.png") or die "Can't open maxechodelay.png! \n";
binmode PNGPLOT;
print PNGPLOT $pngdata;
close PNGPLOT;
$pngdata = 0;
$pngdata = $im5->png(6);
open (PNGPLOT,">fsABdiff.png") or die "Can't open fsABdiff.png! \n";
binmode PNGPLOT;
print PNGPLOT $pngdata;
close PNGPLOT;


#Write KML file
open (KMLFILE,">sigdelay.kml") or die "Can't open sigdelay.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>maxfs.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>'.-(($xmax) / 1200).'</east>'."\n";
print KMLFILE '        <west>'.-(($xmin) / 1200).'</west>'."\n";
print KMLFILE '        <rotation>-0.000000</rotation>'."\n";
print KMLFILE '      </LatLonBox>'."\n";
print KMLFILE '    </GroundOverlay>'."\n";
print KMLFILE '    <GroundOverlay>'."\n";
print KMLFILE '      <name>SPLAT! Best transmitter Overlay</name>'."\n";
print KMLFILE '      <Icon>'."\n";
print KMLFILE '        <href>besttx.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>'.-(($xmax) / 1200).'</east>'."\n";
print KMLFILE '        <west>'.-(($xmin) / 1200).'</west>'."\n";
print KMLFILE '        <rotation>-0.000000</rotation>'."\n";
print KMLFILE '      </LatLonBox>'."\n";
print KMLFILE '    </GroundOverlay>'."\n";
print KMLFILE '    <GroundOverlay>'."\n";
print KMLFILE '      <name>SPLAT! Equalizer Fail Overlay</name>'."\n";
print KMLFILE '      <Icon>'."\n";
print KMLFILE '        <href>eqfail.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>'.-(($xmax) / 1200).'</east>'."\n";
print KMLFILE '        <west>'.-(($xmin) / 1200).'</west>'."\n";
print KMLFILE '        <rotation>-0.000000</rotation>'."\n";
print KMLFILE '      </LatLonBox>'."\n";
print KMLFILE '    </GroundOverlay>'."\n";
print KMLFILE '    <GroundOverlay>'."\n";
print KMLFILE '      <name>SPLAT! Signal Maximum Echo Delay Overlay</name>'."\n";
print KMLFILE '      <Icon>'."\n";
print KMLFILE '        <href>maxechodelay.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>'.-(($xmax) / 1200).'</east>'."\n";
print KMLFILE '        <west>'.-(($xmin) / 1200).'</west>'."\n";
print KMLFILE '        <rotation>-0.000000</rotation>'."\n";
print KMLFILE '      </LatLonBox>'."\n";
print KMLFILE '    </GroundOverlay>'."\n";
print KMLFILE '    <GroundOverlay>'."\n";
print KMLFILE '      <name>SPLAT! Field strength difference - 2 strongest signals</name>'."\n";
print KMLFILE '      <Icon>'."\n";
print KMLFILE '        <href>fsABdiff.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>'.-(($xmax) / 1200).'</east>'."\n";
print KMLFILE '        <west>'.-(($xmin) / 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;