#!/usr/bin/perl 

# script to create a google earth script (.KML) to show wind speed during the 2004 Hurricane Frances. By arlduc (arlduc@amnh.org)

my ($one, $two, $three, $four, $five, $six, $seven, $eight, $north, $south, $east, $west, $frameCtr);

$north = "90";
$south = "-90";
$east = "180";
$west = "-180";
$frameCtr = 1;

open ( DEPC_IN, "modis_chlor_granule.txt");
open ( SCRIPT_OUT, ">NASA_SVS_OceanGranules_DuringKatrina.kml" );

#lines to print out script headers
printf SCRIPT_OUT ("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <kml xmlns=\"http://earth.google.com/kml/2.1\"> \n\n");
printf SCRIPT_OUT ("kml script by Arlene Ducao, Science Bulletins, AMNH \narlduc@amnh.org\n\n");

printf SCRIPT_OUT ("<Folder> \n<open>1</open>");
printf SCRIPT_OUT ("<name> MODIS Observation Granules during Katrina</name> \n");
printf SCRIPT_OUT ("<description>images from http://svs.gsfc.nasa.gov/vis/a000000/a003300/a003326/index.html</description> \n\n");


#while-loop to go through each line of the dataset
while (defined($line = <DEPC_IN>)) {
	$line =~ s/^\s+//;
	($one, $two, $three, $four, $five, $six, $seven, $eight) = split(/\s+/, $line);

	#if struc to check for border coordinates
	if ($one eq "latmin" ) {
		printf STDOUT ("%s\n", $one);
		$south = $two;		
	}
	elsif ($one eq "latmax" ) {
		printf STDOUT ("%s\n", $one);
		$north = $two;		
	}
	elsif ($one eq "lonmin" ) {
		printf STDOUT ("%s\n", $one);
		$west = $two;		
	}
	elsif ($one eq "lonmax" ) {
		printf STDOUT ("%s\n", $one);
		$east = $two;		
	}
	elsif ($one eq "frametime") {
		printf SCRIPT_OUT ("<GroundOverlay>\n");
		printf SCRIPT_OUT ("<name> %s-%s-%sT%s:%s:%sZ </name>\n", $three, $four, $five, $six, $seven, $eight);
		printf SCRIPT_OUT ("<visibility>1</visibility>\n");
		printf SCRIPT_OUT ("<Icon>\n");
		
		if (($frameCtr%10) == $frameCtr) {
			printf SCRIPT_OUT ("http://svs.gsfc.nasa.gov/vis/a000000/a003300/a003326/frames/1024x512/modis_chlor_granule.000%s.png \n", $two);
		}

		elsif (($frameCtr%100) == $frameCtr) {
			printf SCRIPT_OUT ("http://svs.gsfc.nasa.gov/vis/a000000/a003300/a003326/frames/1024x512/modis_chlor_granule.00%s.png \n", $two);
		}

		elsif  (($frameCtr%1000) == $frameCtr) {
			printf SCRIPT_OUT ("http://svs.gsfc.nasa.gov/vis/a000000/a003300/a003326/frames/1024x512/modis_chlor_granule.0%s.png", $two);
		}

	  	printf SCRIPT_OUT ("</Icon>\n");
	  	printf SCRIPT_OUT ("<LatLonBox>\n");
		printf SCRIPT_OUT ("<north>%s</north>\n", $north);
		printf SCRIPT_OUT ("<south>%s</south>\n", $south);
		printf SCRIPT_OUT ("<east>%s</east>\n", $east);
		printf SCRIPT_OUT ("<west>%s</west>\n", $west);
		printf SCRIPT_OUT ("<rotation>0</rotation>\n");
	  	printf SCRIPT_OUT ("</LatLonBox>\n");
	  	printf SCRIPT_OUT ("<TimeSpan>\n");
		printf SCRIPT_OUT ("<begin> %s-%s-%sT%s:%s:%sZ </begin>\n", $three, $four, $five, $six, $seven, $eight);
		#printf SCRIPT_OUT ("<end>2004-12-31</end>\n");
	  	printf SCRIPT_OUT ("</TimeSpan>\n");
		printf SCRIPT_OUT ("</GroundOverlay>\n\n\n\n");	
	}
	$frameCtr++;

}

printf SCRIPT_OUT ("</Folder> \n");
printf SCRIPT_OUT ("</kml>");

printf STDOUT ("written to specified KML file.\n");
