#! /usr/bin/perl -Tw
#                              -*- Mode: Perl -*- 
# Copyright (C) 2002, Heiko Klein
# $Id$
# 
# File name       : traj.pl
# Description     : CGI-script to select directories
# 
# Author          : Heiko Klein
# Created On      : Tue Feb  5 16:33:16 2002
# 
# Last Modified By: Heiko Klein
# Last Modified On: Fri Nov 29 11:54:20 2002
# Update Count    : 108
# Status          : $State$
# 
# $Locker$
# $Log$
# 
eval 'exec perl -Tw -S "$0" "$@"'
    if 0;

$ENV{PATH} = '';

use strict;
use CGI qw(:standard);
#use CGI::Carp qw(fatalsToBrowser);
use vars qw($prgn $month $year $station $type $country $path $query %type
	    $file);

($prgn = $0) =~ s:.*/::;		# the script-name

%type = ( 1 => 'pre',
	  2 => 'all',
	  3 => 'anna3',
	  4 => 'anna4' );

$path = "/usr/local/dnmi/emep/htdocs";

$query = new CGI;

$month = $query->param('month');
$year = $query->param('year');
$station = $query->param('station');
$type = $query->param('type');

$month = check_digits($month);
$year = check_digits($year,4);

if (!$station) {
    error_message();
#    $station = 'NO01';
} else {
    if (! ($station =~ /^(\w{2})(\d+)$/)){
	$station = 'NO01';
    } else {
	$station = $1 . check_digits($2);
    }
    if (!$type) {
	$type = 1;
    } elsif (!($type =~ /^\d$/)) {
	$type = 1;
    }

    if (($type == 1) || ($type == 2)) {
	get_pic();
    } elsif ($type == 3) {
	get_sector_data('Lag');
    } elsif ($type == 4) {
	get_sector_data('Eul');
    } elsif ($type == 5) {
	get_trajectory_data();
    }
}

sub error_message {
    print header("text/html");
    print <<'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Trajectory data</TITLE>
</HEAD>
<BODY text="#00367E" bgcolor="#ffffff">
EOF
	print "<em>\n";
	print "Please, choose a station!\n";
    print "<br>\n";
	print <<'EOF';
</em>
</BODY>
</HTML>
EOF
}

sub get_pic {
    $type = $type{$type};
    if ($month == 0) { 
	$file = "/plot/trcross/$type-$year-$station.png";
    } else {
	$file = "/plot/trcross/$type-$year$month-$station.png";
    }
    
    print header("text/html");
    print <<'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Trajectory crossings</TITLE>
</HEAD>
<BODY text="#00367E" bgcolor="#ffffff">
The <b>units</b> are no. of times the trajectory has crossed the area (gridcell)
4 times per day in one month or one year. 
<br>
<p>
EOF
    unless (-r "$path$file") {
	print "<em>\n";
	print "Sorry, the selected station did not produce data in the selected year.\n";
	print "Please choose  another year/station.\n";
	print <<'EOF';
</em>
</BODY>
</HTML>
EOF
        return;
    } 
    print "<IMG SRC=\"$file\">\n";
    print <<'EOF';
</p>
To download the figure, please press <em>shift</em> 
and <em>click left mousebutton</em> 
EOF
    print "<a href=\"$file\">here</a>! \n";
    print <<'EOF';
<h4>Remark</h4>
In 1986, trajectory crossings for March-August have been calculated with 
modelled precipitation instead of observed precipitation. This should
 be considered when using the data.<BR>
The trajectory crossings of January 1985 begin at January 1 12GMT
 because meteorological data is missing for the first two terms of the month. 
<p>
</BODY>
</HTML>
EOF
}

sub get_sector_data {
    my $model = shift;
    my $file;
    if ($model eq 'Lag') {
	$file = "/data/sector1985-1996/sector-$station.dat";
    } else {
	$file = "/data/sector1997-2001/sector-$station.dat";
    }
    print header("text/html");
    if (! -r "$path$file") {
	print <<'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Trajectory crossings</TITLE>
</HEAD>
<BODY text="#00367E" bgcolor="#ffffff">
Sorry, the selected station did not produce data in the selected year. Please 
choose  another year/station.
</BODY>
</HTML>
EOF
        return;
    } 
    open (F, "$path$file")
	or die "Cannot open file $path$file: $!\n";
    print <<'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Trajectory crossings</TITLE>
</HEAD>
<BODY text="#00367E" bgcolor="#ffffff">
EOF
    print "<h4><a href=\"$file\">Download this data (Shift + Click)</a>".
	"</h4>\n";
    for (my $i = 0; $i < 6; $i++) {
	print scalar <F>,"<BR>\n";
    }
    print "<TABLE border=1 cellspacing=2>\n<TR>\n";
    print map {'<TH>'.$_.'</TH>'} split /\s+/, scalar <F>;
    print "\n</TR>\n";
    foreach my $line (<F>) {
	$line =~ s/^\s*//;
	print "<TR>\n";
	print map {'<TD ALIGN="right">'.$_.'</TD>'} split /\s+/, $line;
	print "\n</TR>\n";
    }
    print <<EOF;
</TABLE>
</BODY>
</HTML>
EOF
    close F;
}

sub get_trajectory_data {
    my $file = "/data/trajectory/traj-$year/trj-$year-$station.dat.zip";
    print header("text/html");
    if (! -r "$path$file") {
	print <<'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Trajectory crossings</TITLE>
</HEAD>
<BODY text="#00367E" bgcolor="#ffffff">
Sorry, the selected station did not produce data in the selected year. Please 
choose  another year/station.
</BODY>
</HTML>
EOF
        return;
    } 
    print <<'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Trajectory crossings</TITLE>
</HEAD>
<BODY text="#00367E" bgcolor="#ffffff">
EOF
    (my $shortfile = $file) =~ s:.*/::;
    print "<h4><a href=\"$file\">Download data (Shift + Click)".
	" $shortfile</h4></a>\n";
    print <<EOF;
</BODY>
</HTML>
EOF
}

#@
# DESCRIPTION: check, that value is \d{$length}
#              
# PARAMETER:   $value,$length
#              
# RETURN:      $value
#@
sub check_digits {
    my $value = shift;
    my $length = shift || 2;
    my $default = '01';
    if (!defined $value) {
	return $default;
    }
    $value = set_length ($length,$value);
    if (!($value =~ /(\d{$length})/)) {
	return $default;
    }
    return $1;
}


#@
# DESCRIPTION: set length of parameter but cutting of first letters
#              or adding 0
#              
# PARAMETER:   $length $param
#              
# RETURN:      $param
#@
sub set_length ($$) {
    my ($length, $param) = @_;
    # cut-off
    $param = substr $param,-$length;
    # add zero's
    while (length $param < $length) {
	$param = '0' . $param;
    }
    return $param;
}

