#! /usr/bin/perl -Tw
#                              -*- Mode: Perl -*- 
# Copyright (C) 2002, Heiko Klein
# $Id$
# 
# File name       : traj_select.pl
# Description     : 
# 
# Author          : Heiko Klein
# Created On      : Wed Feb  6 14:08:58 2002
# 
# Last Modified By: Heiko Klein
# Last Modified On: Fri Nov 29 14:14:25 2002
# Update Count    : 73
# Status          : $State$
# 
# $Locker$
# $Log$
# 
eval 'exec perl -Tw -S "$0" "$@"'
    if 0;

use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

use vars qw($prgn %Country %Station $query $country $station $type 
	    $year $month @Month);

($prgn = $0) =~ s:.*/::;		# the script-name
@Month = qw(All January February March April May June July August September
	    October November December);

get_country_station ();
$query = new CGI;
$country = $query->param('country');

print_head();
if (!$country) {
    country_select ();
} else {
    select_all ();
}
print_foot();    


sub print_head {
    print header(-type => "text/html");
    print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>EMEP Trajectory Selector</TITLE>
<style type="text/css">
<!--
body { background-color: #FFFFFF; }
//-->
</style>
</HEAD>
<BODY>
EOF
}

sub print_foot {
    print <<EOF;
</BODY>
</HTML>
EOF
}

sub select_all {
    my $countryname = $Country{$country}->{Short};
    print <<EOF;
<FORM METHOD="GET" ACTION="traj.pl" TARGET="main_down" ENCTYPE="application/x-www-form-urlencoded">
<TABLE BORDER=0 CELLPADDING=0 cellspacing=0 width=100%>
<TR>
    <TD><B>Station: </B>    </TD>
    <TD>
	<SELECT NAME="station" SIZE="1">
EOF
    foreach my $id (sort {$Station{$a}->{Name} cmp
				$Station{$b}->{Name}} 
		    map {$country.$_} @{$Country{$country}->{Stationlist}}) { 
	my $statid = $id;
	print '<OPTION VALUE="',$id,'"> ',$Station{$id}->{Name},"\n";
    }
    print <<EOF;
    </SELECT>
    </TD>
    <TD>
<font size=5><A HREF="$prgn">Country: </A> $countryname &nbsp;&nbsp;</font>
    </TD>
<TR>
<TR>
<TD>
<B>Year: </B>
</TD>
<TD>
<SELECT NAME="year" SIZE="1">
EOF
    foreach my $year (1985..2001) {
	print "<OPTION>$year\n";
    }
    print <<EOF;
</SELECT>
&nbsp; &nbsp;
<B>Month: </B>
<SELECT NAME="month" SIZE="1">
EOF
    foreach my $month (0..$#Month) {
	print "<OPTION VALUE=\"$month\"> $Month[$month]\n";
    }
    print <<EOF;
</SELECT>
</TD>
</TR>
<TR>
<TD>
<B>Type: </B>
</TD>
<TD>
<SELECT NAME="type" SIZE="1">
EOF
    my %types = (1 => 'Trajectory Crossings (precipitation days) (png)',
		 2 => 'Trajectory Crossings (all days) (png)',
		 3 => 'Daily Sector Values (1985-1996, 150km Grid) (ASCII)',
		 4 => 'Daily Sector Values (1997-2001,  50km Grid) (ASCII)',
		 5 => '96h Trajectories (ASCII)');
		 
    foreach my $type (sort keys %types) {
	print "<OPTION VALUE=\"$type\">$types{$type}\n";
    }
    print <<EOF;
</SELECT>
</TD>
<TD>
<INPUT TYPE=submit>
</TD>
</TR>
</TABLE>
</FORM>
<HR>
EOF
}

sub country_select {
    print <<EOF;
<FORM METHOD="GET" ACTION="$prgn" ENCTYPE="application/x-www-form-urlencoded">
<font size=5>Country:&nbsp; </font>
<SELECT NAME="country" SIZE="1">
EOF
    foreach my $code (sort {$Country{$a}->{Short} cmp 
				$Country{$b}->{Short}} keys %Country) {
	if ($Country{$code}->{Stationlist}->[0]) {
	    print '<OPTION VALUE="',$code,'"> ',$Country{$code}->{Short},"\n";
	}
    }
    print <<EOF;
</SELECT>
<INPUT TYPE=submit>
</FORM>
<HR>
EOF
}


sub get_country_station {
    %Country = %{ read_objects ("country.obj") };
    %Station = %{ read_objects ("station.obj") };
}

#@
# DESCRIPTION: read a perl obj. file from disk (i.e. written by Dumper)
#              
# PARAMETER:   $file
#              
# RETURN:      $ref_to_object
#@
sub read_objects ($) {
    my $file = shift;
    open (F, "$file")
	or die "Cannot open $file: $!\n";
    local $/ = undef; # slurp mode
    my $obj = <F>;
    close F;
    if ($obj =~ /(.*)/s) {
	$obj = $1; # untaint this, I'm reading a local file
    }
    my $VAR1;
    eval "$obj"
	or die "Cannot eval $obj: $!\n";
    return $VAR1;
}

