#!/usr/bin/perl

# Filename: avhrr.perl.cgi
# 

######################################################################
# Necessary Variables:                                               #
# The following variables should be set to define the locations      #
# and URLs of various files, as explained in the documentation.      #

	$CalCMD = "/usr/bin/cal";  # En: PATH of cal.
	                           # Fr: Chemin de cal.

	$SSI = 1;  # 0 NO SSI (print <HTML><BODY>...</HTML>)
	           # 1 SSI (En: print only Calandar / Fr: N'affiche que le calendrier)

	$Border = 1; # En: Size of table Border.
	             # Fr: Taille des Tables.

	$Days = "Su Mo Tu We Th Fr Sa";  # En: Days
	#$Days = "Di Lu Ma Me Je Ve Sa"; # Fr: Jours en Francais

# Nothing Below this line needs to be altered!                       #
######################################################################

############################################################
# GET INPUT FROM THE CALENDAR HYPERLINKS
(@args) = split(/&/, $ENV{'QUERY_STRING'});
foreach $arg (@args) {
   ($arg, $values) = split(/=/, $arg);
   $values =~ tr/+/ /;
$FORMY{$arg} = $values;
}
############################################################

############################################################
# GET THE INPUT FROM THE SUBMIT BUTTON
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;

   if ($allow_html != 1) {
      $value =~ s/<([^>]|\n)*>//g;
   }

   $FORM{$name} = $value;
}
############################################################


# Open file to get the last day look at by the user
{
$dayfile = "/home/fake/public_html/AVHRR/dayinfo";
open DAYFILE, "< $dayfile" or die "Couldn't open $file";
local @/ = undef;
@dayinfo = <DAYFILE>;
close DAYFILE;
}

# Decides the fate of $dayinfo[1]
if ( $FORMY{'Mon2'} eq 'January') { }
elsif ( $dayinfo[1] eq 'February') { }
elsif ( $dayinfo[1] eq 'March') { }
elsif ( $dayinfo[1] eq 'April') { }
elsif ( $dayinfo[1] eq 'May') { }
elsif ( $dayinfo[1] eq 'June') { }
elsif ( $dayinfo[1] eq 'July') { }
elsif ( $dayinfo[1] eq 'August') { }
elsif ( $dayinfo[1] eq 'September') { }
elsif ( $dayinfo[1] eq 'October') { }
elsif ( $dayinfo[1] eq 'November') { }
elsif ( $dayinfo[1] eq 'December') { }
#else { $dayinfo[1] ='0' }

# Open file to get information about the month, day, and time of the lastest available image
{
$filenew = "/home/fake/AVHRR/AUTOMATE/TASK1/fileinfo";
open PARAMFILE, "< $filenew" or die "Couldn't open $filenew";
#local $/ = undef; 
@params = <PARAMFILE>;
close PARAMFILE;
}

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

#####################################################################################
# Assign parameters a value
# Define the month to be used
if ( $FORM{MONTH} ) {
      $mon = $FORM{MONTH};
}
elsif ( $FORMY{Mon} ) {
      $mon = $FORMY{Mon};
}
elsif ( $FORMY{'Mon2'} ) {
if ( $FORMY{'Mon2'} eq 'January') { $mon = 'February'; }
elsif ( $FORMY{'Mon2'} eq 'February') { $mon = 'March'; }
elsif ( $FORMY{'Mon2'} eq 'March') { $mon = 'April'; }
elsif ( $FORMY{'Mon2'} eq 'April') { $mon = 'May'; }
elsif ( $FORMY{'Mon2'} eq 'May') { $mon = 'June'; }
elsif ( $FORMY{'Mon2'} eq 'June') { $mon = 'July'; }
elsif ( $FORMY{'Mon2'} eq 'July') { $mon = 'August'; }
elsif ( $FORMY{'Mon2'} eq 'August') { $mon = 'September'; }
elsif ( $FORMY{'Mon2'} eq 'September') { $mon = 'October'; }
elsif ( $FORMY{'Mon2'} eq 'October') { $mon = 'November'; }
elsif ( $FORMY{'Mon2'} eq 'November') { $mon = 'December'; }
elsif ( $FORMY{'Mon2'} eq 'December') { $mon = 'January';}
}
elsif ( $FORMY{'Mon1'} ) {
if ( $FORMY{'Mon1'} eq 'December') { $mon = 'February'; }
elsif ( $FORMY{'Mon1'} eq 'January') { $mon = 'March'; }
elsif ( $FORMY{'Mon1'} eq 'February') { $mon = 'April'; }
elsif ( $FORMY{'Mon1'} eq 'March') { $mon = 'May'; }
elsif ( $FORMY{'Mon1'} eq 'April') { $mon = 'June'; }
elsif ( $FORMY{'Mon1'} eq 'May') { $mon = 'July'; }
elsif ( $FORMY{'Mon1'} eq 'June') { $mon = 'August'; }
elsif ( $FORMY{'Mon1'} eq 'July') { $mon = 'September'; }
elsif ( $FORMY{'Mon1'} eq 'August') { $mon = 'October'; }
elsif ( $FORMY{'Mon1'} eq 'September') { $mon = 'November'; }
elsif ( $FORMY{'Mon1'} eq 'October') { $mon = 'December'; }
elsif ( $FORMY{'Mon1'} eq 'November') { $mon = 'January'; }
}
else {
      $mon = $params[1];
}
# 
if ( $mon eq $params[1] ) { }
else {
# Eliminates a space at the end of character if one exists
chomp $mon;
if ( $mon eq 'December') { $mon = 'Dec'; }
elsif ( $mon eq 'January') { $mon = 'Jan'; }
elsif ( $mon eq 'February') { $mon = 'Feb'; }
elsif ( $mon eq 'March') { $mon = 'Mar'; }
elsif ( $mon eq 'April') { $mon = 'Apr'; }
elsif ( $mon eq 'May') { $mon = 'May'; }
elsif ( $mon eq 'June') { $mon = 'Jun'; }
elsif ( $mon eq 'July') { $mon = 'Jul'; }
elsif ( $mon eq 'August') { $mon = 'Aug'; }
elsif ( $mon eq 'September') { $mon = 'Sep'; }
elsif ( $mon eq 'October') { $mon = 'Oct'; }
elsif ( $mon eq 'November') { $mon = 'Nov'; }
}
# Eliminates a space at the end of character if one exists
chomp $mon;

# Define the day to be used
if ( $FORMY{'Day'} ) {
	$dy=$FORMY{'Day'};
}
elsif ( $dayinfo[0] ) {
        if ( $FORM{'MONTH'} ) {
	   $dy=$dayinfo[0];
        }
        else {
           $dy=$params[2];
        }  
}
else {
	$dy=$params[2];
}

#sets the correct current available time based on output from automated codar file grabber
if ( $dy eq $params[2] ) { }
else {
  # Eliminates any spaces at the end of these variables
  chomp $dy;
  if ( length($dy) < 2 ) {
     if ( $dy < 10 ) {
    	$dy = "0$dy";
     }
  }
}
# Eliminates any spaces at the end of these variables
chomp $dy;

# Define the year to be used
# Setting the current time
($sec,$min,$hour,$mday,$monn,$year,$wday,$yday,$isdst) = localtime(time);
if ( $FORM{'YEAR'} ) {
	$yr=$FORM{'YEAR'};
}
elsif ( $FORMY{'Year'} ) {
	$yr=$FORMY{'Year'};
}
else {
        $yr=$params[0];
        chomp $yr;
#	$yr=$year+'1900';
}
# Decide which option button will be shown on web site
if ( $yr eq '2000' ) {
	$y1='SELECTED';
}
elsif ( $yr eq '2001' ) {
	$y2='SELECTED';
}
elsif ( $yr eq '2002' ) {
	$y3='SELECTED';
}
elsif ( $yr eq '2003' ) {
	$y4='SELECTED';
}
elsif ( $yr eq '2004' ) {
	$y5='SELECTED';
}





#####################################################################################
# DEFINES THE NUMBER FOR THE MONTH
if ( $mon eq 'Jan') {
   $numon = '1';
   $s1='SELECTED';
}
elsif ( $mon eq 'Feb') {
   $numon = '2';
   $s2='SELECTED';
}
elsif ( $mon eq 'Mar') {
   $numon = '3';
   $s3='SELECTED';
}
elsif ( $mon eq 'Apr') {
   $numon = '4';
   $s4='SELECTED';
}
elsif ( $mon eq 'May') {
   $numon = '5';
   $s5='SELECTED';
}
elsif ( $mon eq 'Jun') {
   $numon = '6';
   $s6='SELECTED';
}
elsif ( $mon eq 'Jul') {
   $numon = '7';
   $s7='SELECTED';
}
elsif ( $mon eq 'Aug') {
   $numon = '8';
   $s8='SELECTED';
}
elsif ( $mon eq 'Sep') {
   $numon = '9';
   $s9='SELECTED';
}
elsif ( $mon eq 'Oct') {
   $numon = '10';
   $s10='SELECTED';
}
elsif ( $mon eq 'Nov') {
   $numon = '11';
   $s11='SELECTED';
}
elsif ( $mon eq 'Dec') {
   $numon = '12';
   $s12='SELECTED';
}


#####################################################################################
print "Content-type: text/html\n\n";

# Print the HTML HEADER INFO
print <<EndOfHTML;
<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<meta name="Author" content="Todd Fake">
	</head>

	<body text="black" bgcolor="white" link="#0000ee" vlink="#551a8b" alink="red">
		<table width="797" cellspacing="0" cellpadding="0" border="0">
			<tr height="27">
				<td colspan="3" height="27">
					<center>
					<font size="5" face="Arial,Helvetica"><b><a href="http://www.nopp.uconn.edu" target="new">FRONT: NOPP Project at UCONN</a></b></font></center><br>
				</td>
				<td height="27"></td>
			</tr>
			<tr height="27">
				<td colspan="3" height="27">
					<center>
						<font size="5" face="Arial,Helvetica" color="#3333ff">Sea Surface Temperature</font></center>
				</td>
				<td height="27"></td>
			</tr>
			<tr height="19">
				<td colspan="3" valign="top" height="19">
					<center>
						<hr noshade width="100%">
					</center>
				</td>
				<td height="19"></td>
			</tr>
			<tr height="347">
				<td colspan="3" valign="top" height="347">

<center>
<FORM METHOD="POST" ACTION="avhrr.pl.cgi">

<table BORDER=0 valign="TOP">
<tr>
<td VALIGN=TOP WIDTH=180>

EndOfHTML

#########################################################################################################
#        BEGINING OF CALENDAR SECTION

	if (! $SSI) { print "<HTML><HEAD><TITLE>Calendar</TITLE></HEAD><BODY><H1>Calendar</H1>\n"; }
	print &RetCalFirst($CalCMD, $Query_String, $Border);
	print &RetCalSecond($CalCMD, $Query_String, $Border);
	print &RetCal($CalCMD, $Query_String, $Border);
	if (! $SSI) { print "<BODY></HTML>"; }

# En: Sub routine return calendar.
# Fr: Sous-routine retournant le calendrier.

sub RetCalFirst {
	my($CalCMD, $Args, $Border) = @_;
	my($ret, @Result, $Month, $dy, $line, $no);

	if ( -x "$CalCMD" ) {

		if (($Args !~ /^\d\d?-\d\d\d\d$/) || ($Args eq "")) {
			$Args = "";
			@date = localtime(time); 
			$Today = $date[3];
		} else {
			$Args =~ s/-/ /g;
		}
	        
                if ( $numon eq '1' ) {
			$numon1 = '11';
                    # Adjustments to handle year change
                    if ( $FORMY{'Mon1'} ) {
                       $yr1 = $yr;
                    }
                    elsif ( $FORMY{'Mon2'} ) {
                       $yr1 = $yr;  
                    }
                    else { 
                       $yr1 = $yr-1;
                    }
  		}     
         	elsif ( $numon eq '2' ) {
			$numon1 = '12';
                    # Adjustments to handle year change
                    if ( $FORMY{'Mon1'} ) {
                       $yr1 = $yr;
                    }
                    elsif ( $FORMY{'Mon2'} ) {
                       $yr1 = $yr-1;  
                    }
                    else { 
                       $yr1 = $yr-1;
                    }
  		}
		else {
			$numon1 = $numon-'2';
		    $yr1 = $yr;
		}
                open(CAL, "$CalCMD $numon1 $yr1|");
		#open(CAL, "$CalCMD $Args|");
		
                @Result = <CAL>;
		close(CAL);

		$Month = $Result[0]; 
                shift(@Result);
		$Month =~ s/\s\s\s*//g;
		$ret = "<TABLE Border=\"$Border\"><CAPTION ALIGN=\"TOP\"><B>$Month</B></CAPTION>\n<TR>";

		shift(@Result);
		foreach $dy (split(/ /, $Days)) {
			$ret .= "<TH>$dy</TH>";
		}
		$ret .= "</TR>\n";

		foreach $line (@Result) {
			$line =~ s/\n//;
			$line =~ s/   /ccs/g;
			$line =~ s/  /sc/g;
			$line =~ s/ /s/g;
			$line =~ s/^s//g;
			$line =~ s/ss/s/g;
			$line =~ s/c//g;
			$ret .= "<TR>";
                    # Loops through the days in the month and decides what values will be included in the link
			foreach $no (split(/s/, $line)) {
					$ret .= "<TD><a href=http://nopp.dms.uconn.edu/AVHRR/avhrr.pl.cgi?Day=$no&Year=$yr1\&Mon1=$Month\>$no\</a></TD>";
			}
			$ret .= "</TR>\n";
		}
		$ret .= "</TABLE>\n";
	} else {
		$ret = "Cannot find cal on this system.";
	}
        $ret .= "</td><td VALIGN=TOP WIDTH=180>\n";
	return $ret;

}
#########################################################################################################

sub RetCalSecond {
	my($CalCMD, $Args, $Border) = @_;
	my($ret, @Result, $Month, $dy, $line, $no);

	if ( -x "$CalCMD" ) {

		if (($Args !~ /^\d\d?-\d\d\d\d$/) || ($Args eq "")) {
			$Args = "";
			@date = localtime(time); 
			$Today = $date[3];
		} else {
			$Args =~ s/-/ /g;
		}
	        
                if ( $numon eq '1' ) {
			$numon1 = '12';
                   # Adjustments to handle year change
                   if ( $FORMY{'Mon1'} ) {
                       $yr2 = $yr;
                    }
                    elsif ( $FORMY{'Mon2'} ) {
                       $yr2 = $yr;  
                    }
                    else { 
                       $yr2 = $yr-'1';
                    }
  		}
                elsif ( $numon eq '2' ) {
                   $numon1 = '1';
                   if ( $FORMY{'Mon1'} ) {
                       $yr2 = $yr+'1';
                   }
                   else { 
                       $yr2 = $yr;
                    }
                }             
		else {
			$numon1 = $numon-'1';
                    $yr2 = $yr;
		}
                open(CAL, "$CalCMD $numon1 $yr2|");
		#open(CAL, "$CalCMD $Args|");
		
                @Result = <CAL>;
		close(CAL);

		$Month = $Result[0]; 
                shift(@Result);
		$Month =~ s/\s\s\s*//g;
		$ret = "<TABLE Border=\"$Border\"><CAPTION ALIGN=\"TOP\"><B>$Month</B></CAPTION>\n<TR>";

		shift(@Result);
		foreach $dy (split(/ /, $Days)) {
			$ret .= "<TH>$dy</TH>";
		}
		$ret .= "</TR>\n";

		foreach $line (@Result) {
			$line =~ s/\n//;
			$line =~ s/   /ccs/g;
			$line =~ s/  /sc/g;
			$line =~ s/ /s/g;
			$line =~ s/^s//g;
			$line =~ s/ss/s/g;
			$line =~ s/c//g;
			$ret .= "<TR>";
                    # Loops through the days in the month and decides what values will be included in the link
			foreach $no (split(/s/, $line)) {
					$ret .= "<TD><a href=http://nopp.dms.uconn.edu/AVHRR/avhrr.pl.cgi?Day=$no&Year=$yr2\&Mon2=$Month\>$no\</a></TD>";
			}
			$ret .= "</TR>\n";
		}
		$ret .= "</TABLE>\n";
	} else {
		$ret = "Cannot find cal on this system.";
	}
        $ret .= "</td><td VALIGN=TOP WIDTH=180>\n";
	return $ret;

}

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

sub RetCal {
	my($CalCMD, $Args, $Border) = @_;
	my($ret, @Result, $Month, $dy, $line, $no);

	if ( -x "$CalCMD" ) {

		if (($Args !~ /^\d\d?-\d\d\d\d$/) || ($Args eq "")) {
			$Args = "";
			@date = localtime(time); 
			$Today = $date[3];
		} else {
			$Args =~ s/-/ /g;
		}

                if ( $numon eq '1' ) {
                   # Adjustments to handle year change
                   if ( $FORMY{'Mon1'} ) {
                       $yr3 = $yr+1;
                    }
                    elsif ( $FORMY{'Mon2'} ) {
                       $yr3 = $yr+1;  
                    }
                    else { 
                       $yr3 = $yr;
                       # Resets the Year For Looking at December and November 
                       # When the first Month is January of the next Year
                       if ( $FORMY{Mon} ) {
                          $yr=$yr3;
                       }
                       elsif ( $dayinfo[1] eq 'December' ) {
                          if ( $FORM{MONTH} ) {
                            $yr=$yr3-'1';
                          }
                       }
                       elsif ( $dayinfo[1] eq 'November' ) {
                          if ( $FORM{MONTH} ) {
                            $yr=$yr3-'1';
                          }
                       }
                       else {
                          $yr=$yr3;
                       }
                    }
  		}
                elsif ( $numon eq '2' ) {
                   if ( $FORMY{'Mon1'} ) {
                       $yr3 = $yr+1;
                    }
                    else { 
                       $yr3 = $yr;
                       # Resets the Year For Looking at December and November 
                       # When the first Month is January of the next Year
                       if ( $FORMY{Mon} ) {
                          $yr=$yr3;
                       }
                       elsif ( $FORMY{Mon2} ) {
                          $yr=$yr3;
                       }
                       elsif ( $dayinfo[1] eq 'December' ) {
                          if ( $FORM{MONTH} ) {
                            $yr=$yr3-'1';
                          }
                       }
                       else {
                          $yr=$yr3;
                       }
                    }
                }
                else {
                    $yr3 = $yr;
                }
                # Decide which option button will be shown on web site
		if ( $yr3 eq '2000' ) {
		   $y1='SELECTED';
		}
	 	elsif ( $yr3 eq '2001' ) {
		   $y2='SELECTED';
 		}
	 	elsif ( $yr3 eq '2002' ) {
		   $y3='SELECTED';
		}               
	 	elsif ( $yr3 eq '2003' ) {
		   $y3='SELECTED';
		}               
	 	elsif ( $yr3 eq '2004' ) {
		   $y3='SELECTED';
		}               
       
                open(CAL, "$CalCMD $numon $yr3|");
		#open(CAL, "$CalCMD $Args|");
		
                @Result = <CAL>;
		close(CAL);

		$Month = $Result[0]; 
                shift(@Result);
		$Month =~ s/\s\s\s*//g;
		$ret = "<TABLE Border=\"$Border\"><CAPTION ALIGN=\"TOP\"><B>$Month</B></CAPTION>\n<TR>";

		shift(@Result);
		foreach $dy (split(/ /, $Days)) {
			$ret .= "<TH>$dy</TH>";
		}
		$ret .= "</TR>\n";

		foreach $line (@Result) {
			$line =~ s/\n//;
			$line =~ s/   /ccs/g;
			$line =~ s/  /sc/g;
			$line =~ s/ /s/g;
			$line =~ s/^s//g;
			$line =~ s/ss/s/g;
			$line =~ s/c//g;
			$ret .= "<TR>";
                    # Loops through the days in the month and decides what values will be included in the link
			foreach $no (split(/s/, $line)) {
				#if ($no == $Today) {
					#$ret .= "<TD><B><a href=http://nopp.dms.uconn.edu/AVHRR/avhrr.pl.cgi?Day=$no&Hour=$hr\&Year=$yr3\&Mon=$mon\>$no\</a></B></TD>";
                        #}
				  #else {
					$ret .= "<TD><a href=http://nopp.dms.uconn.edu/AVHRR/avhrr.pl.cgi?Day=$no&Year=$yr3\&Mon=$mon\>$no\</a></TD>";
				#}
			}
			$ret .= "</TR>\n";
		}
		$ret .= "</TABLE>\n";
	} else {
		$ret = "Cannot find cal on this system.";
	}
        $ret .= "</td></tr></table>\n";
	return $ret;
}



#                         END OF CALENDAR SECTION
#########################################################################################################

# Create HTML for web viewing

#print "Content-type:text/html\n\n";


# Decide which options will display on web based on latest image
if ( $params[1] ) {
  print <<ENDEOF
       Month:
       <SELECT NAME="MONTH">
       <OPTION $s1\>January
       <OPTION $s2\>February
       <OPTION $s3\>March
       <OPTION $s4\>April
       <OPTION $s5\>May 
       <OPTION $s6\>June
       <OPTION $s7\>July
       <OPTION $s8\>August
       <OPTION $s9\>September
       <OPTION $s10\>October
       <OPTION $s11\>November
       <OPTION $s12\>December
       </SELECT>
       Year:
       <SELECT NAME="YEAR">
       <OPTION $y1\>2000
       <OPTION $y2\>2001
       <OPTION $y3\>2002
       <OPTION $y4\>2003
       <OPTION $y5\>2004
       </SELECT>
  
      <INPUT TYPE="submit" NAME="submitname" VALUE="Submit">
    
</FORM>
</center>

<hr>

<center>
ENDEOF
}


######################################################################################################################
# Sets the proper month for images based on user input from web
if ( $FORMY{'Mon2'} ) {
if ( $FORMY{'Mon2'} eq 'January') { $moon = 'Jan'; }
elsif ( $FORMY{'Mon2'} eq 'February') { $moon = 'Feb'; }
elsif ( $FORMY{'Mon2'} eq 'March') { $moon = 'Mar'; }
elsif ( $FORMY{'Mon2'} eq 'April') { $moon = 'Apr'; }
elsif ( $FORMY{'Mon2'} eq 'May') { $moon = 'May'; }
elsif ( $FORMY{'Mon2'} eq 'June') { $moon = 'Jun'; }
elsif ( $FORMY{'Mon2'} eq 'July') { $moon = 'Jul'; }
elsif ( $FORMY{'Mon2'} eq 'August') { $moon = 'Aug'; }
elsif ( $FORMY{'Mon2'} eq 'September') { $moon = 'Sep'; }
elsif ( $FORMY{'Mon2'} eq 'October') { $moon = 'Oct'; }
elsif ( $FORMY{'Mon2'} eq 'November') { $moon = 'Nov'; }
elsif ( $FORMY{'Mon2'} eq 'December') { $moon = 'Dec';}
}
elsif ( $FORMY{'Mon1'} ) {
if ( $FORMY{'Mon1'} eq 'December') { $moon = 'Dec'; }
elsif ( $FORMY{'Mon1'} eq 'January') { $moon = 'Jan'; }
elsif ( $FORMY{'Mon1'} eq 'February') { $moon = 'Feb'; }
elsif ( $FORMY{'Mon1'} eq 'March') { $moon = 'Mar'; }
elsif ( $FORMY{'Mon1'} eq 'April') { $moon = 'Apr'; }
elsif ( $FORMY{'Mon1'} eq 'May') { $moon = 'May'; }
elsif ( $FORMY{'Mon1'} eq 'June') { $moon = 'Jun'; }
elsif ( $FORMY{'Mon1'} eq 'July') { $moon = 'Jul'; }
elsif ( $FORMY{'Mon1'} eq 'August') { $moon = 'Aug'; }
elsif ( $FORMY{'Mon1'} eq 'September') { $moon = 'Sep'; }
elsif ( $FORMY{'Mon1'} eq 'October') { $moon = 'Oct'; }
elsif ( $FORMY{'Mon1'} eq 'November') { $moon = 'Nov'; }
}
else { $moon = $mon }

# Gets the files present in the requested directory
$thisdir = "/home/fake/public_html/AVHRR/pictures/$yr\/$moon\/$dy\/";
opendir THISDIR, "$thisdir" or die print "<font size=6><b><blink>No Images exist for $moon $dy $yr!</blink></b></font></center></tr></td></TABLE>";
@allfiles = readdir THISDIR;
closedir THISDIR;
# print "@allfiles\n";

# Finds the number of figures in this directory
$numfigs=scalar(@allfiles);
$numfigs=$numfigs - 2;

# Prints Html Code to display images
if ( $numfigs eq '0' ) {
   print "<font size=6><b><blink>No Images exist for $moon $dy $yr!</blink></b></font></center></tr></td></TABLE>";
}
else {
  print "<font size=6>Images From $moon $dy $yr</font><p>";
  if ( $numfigs eq '1' ) {
print <<EOFF;
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></center>
EOFF
  }
  elsif ( $numfigs eq '2' ) {
print <<EOFF;
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a>
</td></tr></table></center>
EOFF
  }
  elsif ( $numfigs eq '3' ) {
print <<EOFF;
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table>
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table></center>
EOFF
  }
  elsif ( $numfigs eq '4' ) {
print <<EOFF;
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table>
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[5]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[5]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table></center>
EOFF
  }
  elsif ( $numfigs eq '5' ) {
print <<EOFF;
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table>
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[5]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[5]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[6]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[6]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></center>
EOFF
  }
elsif ( $numfigs eq '6' ) {
print <<EOFF;
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[2]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[3]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table>
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[4]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[5]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[5]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table>
    <table BORDER=0 valign="TOP"><tr><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[6]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[6]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td><td VALIGN=TOP WIDTH=180>
    <a href="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[7]"><img SRC="http://nopp.dms.uconn.edu/AVHRR/pictures/$yr\/$moon\/$dy\/$allfiles[7]" Width=400 Height=300 align=CENTER alt="click to enlarge"></a></td></tr></table></center>
EOFF
  }
}

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


print <<EEFOF;
<br>&nbsp;</td>
				<td height="80"></td>
			</tr>
			<tr height="19">
				<td colspan="5" height="19">
					<center>
						<hr noshade width="100%">
					</center>
				</td>
				<td height="19"></td>
			</tr>
		</table>
		<font size="2"><i>Please send comments to:</i> <a href="mailto:web\@nopp.dms.uconn.edu">web\@nopp.dms.uconn.edu</a><br>
		</font>
	<center>
	<img src="http://nopp.dms.uconn.edu/counter/counter.pl.cgi" align=abscenter><font size="1">visitors since Nov. 16 2000</font>
	</center>
	</body>

</html>



EEFOF




# Saves data that allows time to be changed for previous months and not just current month
open(MYOUTFILE, ">dayinfo"); #open for write, overwrite
if ( $FORM{'MONTH'} ) {
   if ( $dayinfo[1] =~ $mon ) { 
     print MYOUTFILE "$dy\n";
     close(MYOUTFILE);
   }
   else {
     print MYOUTFILE "$dy\n";
     print MYOUTFILE "$dayinfo[1]";
     close(MYOUTFILE);
   }
}
else { print MYOUTFILE "$dy\n"; 
   if ( $FORMY{'Mon1'} ) { print MYOUTFILE $FORMY{'Mon1'}; }
   elsif ( $FORMY{'Mon2'} ) { print MYOUTFILE $FORMY{'Mon2'}; }
   close(MYOUTFILE);
}


