#!/usr/bin/perl -w

# avhrr_level_1.pl serves the level 1 J. Bisagni's avhrr data.  
# It will display the data for the month specified.  Passed 
# parameters include:
#	configuration file 
#		(e.g. /home/rgroman/scripts/avhrr_level.config)
#	month (e.g. 11)
#	image type (e.g. f)
#	year (e.g. 2001)
#	data directory (e.g.  /data/satellite/avhrr/globec_std/1993)

$version = "May 14, 2001, V1.03";

# Modified May 14, 2002.  V1.03.  Change output code in URL of
#	image link to use the passed value of $data_dir so that
#	the code works for data in other subdirectories.  Update
#	documentation to include this passed parameter.  It was
#	missing.  Fix sendmessage routine so text is sent with
#	mail.  The file name mistakenly included the ">" symbol.
# Modified May 9, 2002. V1.02.  Change so magic constants come from
#	config file.  Change so config file name comes from command
#	line.  Change configuration file to use := syntax.  rcg
# Modified May 9, 2002. V1.01.  Add output formatting for time to
#	hundreths place.  Edit long lines into mulpiple lines in this file.
#	Replace file to gif conversion approach to using DODS URL with
#	magic constants currently defined within this program.  Change
#	format of comment lines.
#	rcg
# Original version, V1.00, August 27, 2001

# Robert C. Groman

# Assumptions:
#	1. Data are stored in a directory tree given in the 
# configuration file with the parameter of dir_root.  The file is 
# passed as first argument.
#	2. Data have file names in the following form:
#		<image type><two digit year><3 digit year day> \
#		<HHMMSS>.<file type>.<optional compressed, Z>
# These files are located in the year sub-directory specified in 
# the passed argument list
#	3. File types to process are specified in the configuration 
# file with the parameter of file_types, as for example:
#			file_types=std,std.Z,img1
#	4. The format of the data directory passed parameter is
#		 /data/satellite/avhrr/globec_std/1993
# so that I can extract out the "globec_std" or whatever, string
# to use elsewhere.

$date = scalar localtime;

($configuration_file, $month, $image_type, $year, $data_dir) = @ARGV;

$| = 1;
$error = "&x";
$warning = "#";
$data_dir_type_string_default = 'globec_std';

@month = (' ','January','February','March',
		'April','May','June','July',
		'August','September','October',
		'November','December');

print STDOUT ("#Program $0 Version: $version\n");
print STDOUT ("#   Configuration file is $configuration_file\n");

@required = (
	"arraysizes",
	"date_month_low", "date_day_low",
	"date_month_high", "date_day_high",
	"dir_root",
	"file_types",
	"latlow", "lathigh",
	"lonlow", "lonhigh",
	"script_root",
	"xrange", "yrange"
	);

&read_configuration_file($configuration_file);

&make_log_entry (">>>>>>>Begin Program=$0",
	"Date of run=$date\tVersion=$version",
	"Configuration file=$configuration_file");
$okay = "yes";

if (exists $config_param{'debug'} and defined $config_param{'debug'}) {
	$debug = $config_param{'debug'};
}
else {
	$debug = 'no';
}
#print STDOUT ("debug=$debug\n");

print STDOUT ("#For the month of ", $month[$month], 
	".  Year is ", $year,
	".  Image type is ", $image_type, 
	".\n#   Data from ", $data_dir, ".\n");
&make_log_entry ("Month=$month", "Image type=$image_type", 
	"Year=$year","Data directory=$data_dir");

$data_dir_type_string = $data_dir;
$data_dir_type_string =~ s/.*\/(\w{1,})\/\d\d\d\d\s{0,}?/$1/;

unless (defined $data_dir_type_string) {
	&make_log_entry ("Data directory type string not defined",
		"For data directory=$data_dir");
	&sendmessage($error,"Data directory type string not defind",
		"For data directory $data_dir");
	exit;
}

if ($data_dir eq $data_dir_type_string) {
	&make_log_entry (
	 "Could not extract out data directory type string from=$data_dir",
	 "Use default value=$data_dir_type_string_default");
	$data_dir_type_string = $data_dir_type_string_default;
}

&make_log_entry ("Data directory type string=$data_dir_type_string");

for ($i=0; $i<=$#required; $i++) {
	unless (exists $config_param{$required[$i]} ) { 
	   $okay="no"; 
	   &sendmessage ($error,
	   	"$required[$i] is missing from configuration ",
	   	"file=$configuration_file");
	   &make_log_entry (
		"$required[$i] is missing from configuration file",
	   	"file=$configuration_file");
	}
	else {
		print STDOUT (
			"#  $required[$i]=$config_param{$required[$i]}\n")
			if $debug eq 'yes';
		&make_log_entry (
			"$required[$i]=$config_param{$required[$i]}");
	}
}
if ( $okay eq "no") { 
	&sendmessage ($error,
	  "One or more parameters are missing from the configuration file",
	  "Cannot continue.");
	&make_log_entry (
	   "One or more parameters missing from configuration file=$configuration_file");
	exit;
}

#Magic constants - get from config file

$date_month_low = $config_param{'date_month_low'};
$date_day_low = $config_param{'date_day_low'};

$date_month_high = $config_param{'date_month_high'};
$date_day_high = $config_param{'date_day_high'};

$xrange = $config_param{'xrange'};
$yrange = $config_param{'yrange'};

$lonlow = $config_param{'lonlow'};
$lonhigh = $config_param{'lonhigh'};

$latlow = $config_param{'latlow'};
$lathigh = $config_param{'lathigh'};

$arraysizes = $config_param{'arraysizes'};

&process_data;

undef $error;
undef $warning;
undef $version;

&make_log_entry ("<<<<<<<End of $0");

exit;

#------------------------------------------------------------------------
sub read_configuration_file {

# Open and read configuration file specified as first passed parameter 
# $_[0]. Return the contents of the file as the hash array %config_param.
# Lines beginning with "#" are treated as comments.  It is assumed
# that the indirect file contains lines as

#	parameter = value

# and this information is stored as 

#	$config_param{"parameter"} = value

my $filename = $_[0];
my ($parameter, $value);

#print STDOUT ("#**debug, indirect filename=$filename\n");

unless (defined $filename ) {
	&sendmessage ($error,
		"Could not open configuration file",
		"File name not specified.  Cannot continue.");
	exit;
}
unless (open CONFIG_FILE, $filename) {
	&sendmessage ($error,
		"Could not open configuration file=$filename",
		"Error code=$!.  Cannot continue.");
	exit;
}
while (<CONFIG_FILE>) {
	chomp;
	s/\s//g;
	if (m/^#/) { next;}
	unless ( m/\S+/ ) {next;}
	unless ( m/:=/ ) {
		&sendmessage ($warning,
			"No :equal sign in line of config file=$filename",
			"Line is=$_");
		next;
	}
#	print STDOUT ("***debug, config input line=$_\n");
	($parameter, $value) = split /:=/;
	$config_param{$parameter} = $value;
#	print STDOUT ("#**debug, config_param{$parameter}=",
#		$config_param{$parameter}, "\n");
}
close CONFIG_FILE;
}

#------------------------------------------------------------------------

sub sendmessage {

#Send a message to the user.
#The message sent will be in the strings $_[0] and $_[1]

my ( @args, $mailfile, $message0, $message1, $prefix, $status,$who);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

$prefix=$_[0];
$message0=$_[1];
$message1=$_[2];
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
if ($year >= 100 and $year <= 1000) {$year = $year + 1900}
if ($year < 1900) { $year = $year + 2000; }
$mon++;
if ($mon < 10) { $mon = "0" . $mon; }
if ($mday < 10) {$mday = "0" . $mday; }
if ($hour < 10) { $hour = "0" . $hour; }
if ($min < 10) { $min = "0" . $min; }
if ($sec < 10) { $sec = "0" . $sec; }
undef $wday;
undef $isdst;
$mailfile="/tmp/sendmess" . $year . $yday . $hour . $min . $sec . ".tmp";

if ( open TEMPFILE, ">$mailfile") {
	print TEMPFILE ("Message from $0\n");
	if ( exists $ENV{'REMOTE_HOST'} ) {$who=$ENV{'REMOTE_HOST'} ; }
	elsif (exists $ENV{'REMOTE_ADDR'} ) {$who=$ENV{'REMOTE_ADDR'} ; }
	else {$who="not available"; }
	print TEMPFILE (" Date of message: $year/$mon/$mday $hour:$min\n");
	print TEMPFILE (" From: $who\n");
	print TEMPFILE (" $message0\n");
	print TEMPFILE (" $message1\n");
	close TEMPFILE;
	`cat $mailfile`;
	$status = 
	  `/bin/mail -s "Probem with $0" dmo\@globec.whoi.edu <$mailfile`;
	unlink $mailfile;
}

print STDOUT ($prefix," $message0\n");
print STDOUT ($prefix," $message1\n");
print STDOUT ($prefix," Above message from $0\n");
print STDOUT ($prefix," Date of message: $year/$mon/$mday $hour:$min\n");
undef $status;
return 0;
}


#-----------------------------------------------------------------------
sub make_log_entry {

# Make a log entry with @_ if the log file $config_param{"logfile"} 
# exists.  Each entry in @_ are separated by tabs.

my ($login, @month, $temp);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
if ($year >= 100 and $year <= 1000) {$year = $year + 1900}
if ($year < 1900) { $year = $year + 2000; }
$mon++;
if ($mon < 10) { $mon = "0" . $mon; }
if ($mday < 10) {$mday = "0" . $mday; }
if ($hour < 10) { $hour = "0" . $hour; }
if ($min < 10) { $min = "0" . $min; }
if ($sec < 10) { $sec = "0" . $sec; }

if (exists $config_param{"logfile"} ) {
	$temp = ">>" . $config_param{'logfile'};
	unless ( open LOGFILE, $temp ) {
		&sendmessage ($warning, "Could not open logfile=$temp",
			"Error code=$!");
	}
	else {
		$login = getlogin || (getpwuid($<))[0] || "Intruder!!";
		$login="\tusername=" . $login;
		print LOGFILE ($year, "/" . $mon . "/" . $mday . ":" . 
			$hour . $min . "." . $sec, $login);
		foreach (@_) {
			print LOGFILE ("\t", $_);
		}
		print LOGFILE ("\n");
		close LOGFILE;
	}
}
return;
}

#-------------------------------------------------------

sub process_data {

# Process the level 1 for the avhrr data

my (
	@files, $file_pattern, $file_type, @file_types, 
	$i, $numb_files, $time,
	@valid_files, $year_day);

unless (chdir $data_dir) {
	&sendmessage ($error, "Could not change directory to $data_dir", 
		"Cannot continue");
	return "No good";
}
&make_log_entry ("Change directory=$data_dir");

print STDOUT ("#***debug, month=$month\n") if $debug eq 'yes';

@file_types = split ",", $config_param{"file_types"};
if ($#file_types < 0) {@file_types = ($config_param{"file_types"}) };

print STDOUT ("#***debug, config_param{'file_types'}=",
		"$config_param{'file_types'}\n")
	if $debug eq 'yes';

foreach $file_type (@file_types) {
	$file_pattern = $image_type . '*' .  '\.' . $file_type . '*';
	&make_log_entry ("Collecting files with pattern=$file_pattern");
	push @files, glob ($file_pattern);
}
$numb_files = @files;
&make_log_entry ("Number of files to process=$numb_files");

# Now get the file names for those within this month

foreach $file (@files) {
	if (&whatmonth($file) == $month) {
		push @valid_files, $file;
		print STDOUT ("#***debug, month=$month, ",
			"whatmonth($file)=", &whatmonth($file),"\n") 
				if $debug eq 'yes';
	}
}

print STDOUT ("status[width=11]\tyrday_utc\ttime\n");

foreach $file_type (@file_types) {
	unless (exists $config_param{"status_$file_type"} and defined
		$config_param{"status_$file_type"} ) {
		&make_log_entry(
		   "No status definition exists for file type=$file_type");
		&sendmessage($warning,"No status definition exists for file type $file_type",
			"Skipping this type");
		next;
	}
	foreach $file (@valid_files) {
		if ($file =~ m/$file_type/) {
			$year_day = $file;
			$year_day =~ s/\D\d\d(\d\d\d)(\d\d\d\d)(\d\d)\.$file_type.*/$1/;
			$time = $2;
			$seconds = $3;
			if (defined $time and defined $seconds) {
				print STDOUT ("#***debug, seconds=$seconds\n") 
					if $debug eq 'yes';
				$seconds = $seconds / 60;
				$seconds = sprintf "%5.2f", $seconds;
				$time = $time + $seconds;
			}
			else {
				&make_log_entry(
				   "No time could be determined for file=$file");
				$time = "nd";
			}
			$time = &format_time($time);
			print STDOUT ($config_param{"status_$file_type"},"\t",
				$year_day,"\t",
				'<a href="http://rodan.smast.umassd.edu/cgi-bin/',
				'idl-cmdln/IDL.cgi?',
				'variable=dsp_band_1&date_year_low=',$year,
				'&date_month_low=', $date_month_low,
				'&date_day_low=', $date_day_low,
				'&date_year_high=',$year,
				'&date_month_high=', $date_month_high, 
				'&date_day_high=', $date_day_high,
				'&parentURL',
				'=http://rodan.smast.umassd.edu/cgi-bin/nph-ff/',
				'catalog/gom_std.catalog',
				'&xrange=', $xrange,
				'&yrange=',$yrange,
				'&lonlow=', $lonlow,
				'&lonhigh=', $lonhigh,
				'&latlow=', $latlow,
				'&lathigh=', $lathigh,
				'&arraysizes=', $arraysizes,
				'&FillValue=&orientation=machine',
				'&dodstype=grid&script=dods_gif',
				'&banner=/home/httpd/html/DODSview/default.html',
				'&sequencevar=time&',
				'&&url=http://rodan.smast.umassd.edu/cgi-bin',
				'/nph-dsp/',
				$data_dir_type_string,
				'/',
				$year,
				'/',
				$file,
				'">', $time, '</a>', "\n");
		}
	}
}
return 1;
}

#------------------------------------------------------------------

sub whatmonth {

# Given the file in $_[0], parse the name and see what month 
# it is in.  Return value of the month it is in.

# Assume the year to be $year

my (@days, $month, $yrday, $total_days);

@days = (0, 31, 28, 31,30,31,30,31,31,30,31,30,31);

$yrday = $_[0];
$yrday =~ s/\D\d\d(\d\d\d)\d{1,6}\..*/$1/;

if ($yrday eq $_[0]) {
	&make_log_entry("Could not parse for month in file=$file");
	return 0;
}

# Determine leap year

if ( $year/4 == int ($year/4) and $year/100 != int ($year/100) ) { 
	$days[2] = 29 }
if ( $year/4 == int ($year/4) and $year/100 == int ($year/100)  
	and $year/400 == int ($year/400) ) { $days[2] = 29 }

$total_days = 31;
$month = "nd";
for ($i=2; $i <=13; $i++) {
	$month = $i - 1;
	if ( $yrday >  $total_days) {
		$total_days = $total_days + $days[$i];
	}
	else {
		last;
	}
}

print STDOUT ("#***debug, whatmonth return=$month\n") if $debug eq 'yes';
return $month;
}

#----------------------------------------------------------------------

sub format_time{

# Format the time value to the hundreths place adding leading zeros
# as necessary.  If value contains nd, e.g. "ng", leave alone.

my $i;
my $time = $_[0];

if ($time =~ m/nd/ ) {return $time}

unless ($time =~ m/\./) { $time = $time . '.00'}

unless ($time =~ m/\d{0,4}\.\d\d/ ) { $time = $time . '0'}

for ($i = 1; $i<= 4; $i++) {
	if ($time =~ m/\d\d\d\d\.{0,1}/) {return $time}
	$time = '0' . $time;
}
print STDOUT ("#***debug, logic error in format_time routine",
	"time=$time\n");
return $time;
}

#----------------------------------------------------------------------

