package MpmasEDICRiverFlowTimeSeries;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Carp ;
use Data::Dumper;
use CtUtilities qw(queryhash extqueryhash get_unique);
use MpmasInputfile;
use MpmasInputBlock;
use IndexedTable;
our @ISA = qw(MpmasInputfile);

sub filename {
	my $self = shift;
	return "RiverFlows_FullTimeSeries0";
}

sub required {
	my $self = shift;

	return ($self->config_entry("LANDSCAPE_MODEL") == 1 and $self->config_entry("OFFDYNFLOW") == 0 );
}
sub prepare_shared {

}

sub prepare_special {
	my $self = shift;	
	my @sortedinflows = @{$self->config_entry("SORTEDINFLOWS")};
	my @sortedmonths = @{$self->config_entry("SORTEDIRRIGMONTHS")};
	
	my $dbh = $self->get_dbh;
	my $flowtab = $self->config_entry("EDICINFLOWACTUAL")->transform($dbh, 3);

	my $head = MpmasInputBlock->new(2, 10);

	$head->add_row("THE EDIC INFLOWS (TIME SERIES) FILE");
	$head->add_row("1","INFLOWS");
	$head->add_empty_rows(1);

	$head->add_row("","Earliest year", $self->config_entry("STARTYEAR") -1 );
	$head->add_row("","Number of years",  $self->config_entry("ENDYEAR") - $self->config_entry("STARTYEAR")   + 3) ;
	$head->add_row("","Number of inflows", scalar @sortedinflows);
	$head->add_row("","Is Northern Hemisphere ( 0 / 1 )", 		 $self->config_entry("NORTHERN"));

	$head->add_empty_rows(2);
	$head->add_comment_row("","","","","Months");
	$head->add_comment_row("", "Inflow ID", "Inflow MPMAS ID", "Year", @sortedmonths);
	for my $inf (0..$#sortedinflows) {
	      my $infid = $sortedinflows[$inf];
	      for my $y ($self->config_entry("STARTYEAR") - 1 ..$self->config_entry("ENDYEAR") + 1) {
		      my @row;
		      for my $m (@sortedmonths) {
			      push @row, sprintf("%.2f",$flowtab->query([$infid, $y, $m],4));
		      }
		      $head->add_row("",$infid, $inf,$y, @row);
	      }
	      $head->add_empty_rows(2);
	}
	$self->add_block($head);
}
1;
