package MpmasEDICRiverFlows;

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 "EdicRiverFlows0";
}

sub required {
	my $self = shift;
	return ($self->config_entry("LANDSCAPE_MODEL") == 1 );
}
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("EDICINFLOWEXPECT")->transform($dbh,2);

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

	$head->add_row("THE EDIC EXPECTED (AVERAGE) INFLOWS FILE");
	$head->add_row("1","INFLOWS");
	$head->add_empty_rows(1);
	$head->add_row("", "Number of Rows", scalar @sortedinflows );
	$head->add_row("", "Number of Columns", $self->config_entry("IRRIGMONTH") + 1 );
	$head->add_empty_rows(2);
	$head->add_comment_row("","","","Months");
	$head->add_comment_row("","Inflow ID", "Inflow MPMAS ID", @sortedmonths);
	for my $inf (0..$#sortedinflows) {
	      my $infid = $sortedinflows[$inf];
	      my @row;
	      for my $m (@sortedmonths) {
		      push @row, sprintf("%.4f",$flowtab->query([$infid, $m],3));
	      }
	      $head->add_row("",$infid, $inf, @row);
	}
	$self->add_block($head);
}
1;