#!/usr/bin/perl 

# script to create a google earth script (.KML) of summed Annual NPP data available from the University of Maryland Global Land Cover Facility. By arlduc (arlduc@amnh.org)

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

$north = "90";
$south = "-90";
$east = "180";
$west = "-180";
$month = 11;
$year = 1;

open ( SCRIPT_OUT, ">Ozone_AMNH.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> Global Ozone</name> \n");
printf SCRIPT_OUT ("<description>images from http://sciencebulletins.amnh.org/earth/v/ozone.20050214/</description> \n\n");


#while-loop to go through each line of the dataset
do  {
		
		if ($month == 13) {$month = 1;}

		printf SCRIPT_OUT ("<GroundOverlay>\n");
		if ($month%10 == $month) {
			printf SCRIPT_OUT ("<name> 200%s-0%s </name>\n", $year, $month);
		}
		else {	printf SCRIPT_OUT ("<name> 200%s-%s </name>\n", $year, $month); }

		printf SCRIPT_OUT ("<visibility>1</visibility>\n");
		printf SCRIPT_OUT ("<Icon>\n");
		if ($month%10 == $month) {
			printf SCRIPT_OUT ("http://www.arlduc.org/3dAnim/AMNH/googleEarth/ozone/sourceimages/for2005/ga0%s0%s.tif\n", $year, $month);
		}
		else {	printf SCRIPT_OUT ("http://www.arlduc.org/3dAnim/AMNH/googleEarth/ozone/sourceimages/for2005/ga0%s%s.tif\n", $year, $month); }
	  	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");
		if ($month%10 == $month) {
			printf SCRIPT_OUT ("<begin> 200%s-0%s </begin>\n", $year, $month);
		}
		else {	printf SCRIPT_OUT ("<begin> 200%s-%s </begin>\n", $year, $month); }
	  	printf SCRIPT_OUT ("</TimeSpan>\n");
		printf SCRIPT_OUT ("</GroundOverlay>\n\n\n\n");	
	
		if ($month == 12) { $year++; }
		$month++;
		
} until (($year == 5) && ($month == 2));

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

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