#!/usr/bin/perl -w
# call: csv2fanndat.pl outfile1.dat outfile2.dat infile.csv
# this script converts spot_num.txt into fann data format and
# multiplies in/output with 1/200
use strict;
my @tmpinput;
my @tmpoutput;
my %year;
my @data1;
my @data2;
my $outfile1=shift;
my $outfile2=shift;
<>; # remove first line
while (<>) {
	my ($year, $mon, $ssn, $dev) = split(/\s+/);
	if (defined $year{$year}) {
		$year{$year}+=($ssn/12);
	} else {
		$year{$year}=0;
	}
}
@tmpinput = map {$_/200} sort {$a<=>$b} keys %year; # rescale
@tmpoutput =map {$year{$_}/200} sort {$a<=>$b} keys %year;
foreach my $i (0 .. $#tmpinput) {
	print "year:".200*$tmpinput[$i]," activity:",200*$tmpoutput[$i],"\n";
}
my $len = $#tmpinput;
my $cutpos = int (2*$len/3);
my $OUT1; open($OUT1, ">$outfile1") || die "could not read '$outfile1'\n";
my $OUT2; open($OUT2, ">$outfile2") || die "could not read '$outfile2'\n";
my $prev_years=30; my $predicted_years=1; my $years=$prev_years+$predicted_years;
print $OUT1 join(" ", $cutpos - $years, $prev_years, $predicted_years), "\n"; # header
print $OUT2 join(" ", $len - $cutpos - $years , $prev_years, $predicted_years), "\n"; # header
print $OUT1 join("\n", map { 
	join(" ", @tmpoutput[$_-$years .. $_-$predicted_years-1]), # input
	join(" ", @tmpoutput[$_-$predicted_years .. $_-1]) # output
} ($years+1 .. $cutpos)), "\n";
print $OUT2 join("\n", map { 
	join(" ", @tmpoutput[$_-$years .. $_-$predicted_years-1]), # input
	join(" ", @tmpoutput[$_-$predicted_years .. $_-1]) # output
} ($cutpos+$years+1 .. $len)), "\n";
close($OUT1); close($OUT2);

