#! /usr/bin/perl -w
#                              -*- Mode: Perl -*- 
# Copyright (C) 2002, Heiko Klein
# $Id$
# 
# File name       : create_station_object.pl
# Description     : quickhack to read station-files and country-files
#                   and create station.obj and country.obj
# 
# Author          : Heiko Klein
# Created On      : Wed Feb  6 11:26:38 2002
# 
# Last Modified By: Heiko Klein
# Last Modified On: Fri Nov 29 11:27:43 2002
# Update Count    : 21
# Status          : $State$
# 
# $Locker$
# $Log$
# 
eval 'exec perl -w -S "$0" "$@"'
    if 0;

use strict;
use Data::Dumper;
use vars qw($prgn %Country %Station);


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

open (C, "stations/areas")
    or die "Cannot read stations/areas: $!\n";
foreach my $line (<C>) {
    chomp $line;
    my ($code, $id, $long, $short) = split (/;/, $line);
    $Country{$code}->{'Long'} = $long;
    $Country{$code}->{'Short'} = $short;
    $Country{$code}->{'Id'} = $id;
}
close C;

my %countrystation;
foreach my $year (1997..2006) {
    open (S, "stations/stat$year.txt")
	or die "Cannot open stations/stat$year.txt: $!\n";
    foreach my $line (<S>) {
	chomp $line;
	# I like starting at the end
	my $name = substr $line, 19;
	my $id = substr $line, 16,3;
	my $code = substr $line, 13,3;
	my $y = substr $line, 6,7;
	my $x = substr $line, 0,6;
	foreach my $el ($name, $id, $code, $y, $x) {
	    # $el is reference to elements
	    $el =~ s/^\s+//;
	    $el =~ s/\s+$//;
	}
	$countrystation{$code}->{$id} = 1;
	$Station{$code.$id}->{Country} = $code;
	$Station{$code.$id}->{Name} = $name;
	$Station{$code.$id}->{GridX} = $x;
	$Station{$code.$id}->{GridY} = $y;
	my $fullyear = ($year < 50) ? ($year+2000) : ($year+1900);
	push @{ $Station{$code.$id}->{Yearlist} }, $fullyear;
    }
    close S;
}
foreach my $code (keys %Country) {
    @{ $Country{$code}->{Stationlist} } = (keys %{ $countrystation{$code} });
}
    
open (F, ">station.obj")
    or die "Cannot write station.obj: $!\n";
print F Dumper \%Station;
close F;
open (F, ">country.obj")
    or die "Cannot write country.obj: $!\n";
print F Dumper \%Country;
close F;

