use strict;
use warnings;
use Chart::Gnuplot;
use List::Util qw(sum);


my $sta = shift;

my $l;
my @tab_line;
my @x = ();
my @y1= ();
my @err1= ();
my @x2 = ();
my @y2= ();
my @err2= ();
my @x3 = ();
my @y3= ();
my @err3= ();
my $dataSet3;
my $dataSet4;
my $dataSet5;
my @date_soln;


# on va recuperer la liste des soln de la station pour faire des barres verticales
open (SOLN, "soln.snx");
my @solns = <SOLN>;
my $sta_court=substr($sta,0,4);
my @soln_sta=grep(/$sta_court/,@solns); # on recupere les lignes de la station
foreach (@soln_sta)
{
	my @tab=split(' |:',$_);
	if ($tab[13]!=0)
	{	
		my $date;
		if ($tab[12]>20)
		{
			$date=1900+$tab[12]+$tab[13]/365;
		}
		else
		{
			$date=2000+$tab[12]+$tab[13]/365;
		}
		push @date_soln, $date;
	}

}

my $title0 = $sta." residuals (mm)";

my $multiChart = Chart::Gnuplot->new(
    output => $sta.".png",
    title  => $title0,
);

my @charts = ();

##############NORD
open (REF, $sta.".DN");
$l=<REF>;
@tab_line = split(' ',$l);

$l=<REF>;
while (defined($l=<REF>))
{
	
	@tab_line = split ' ',$l;
	push @x, $tab_line[0];		
	push @y1, $tab_line[1];
	push @err1, $tab_line[2];
}
# Calcul et affichage des moyenne des valeurs : 
#my $moyenne1=sum(@y1)/@y1;
#print "Moyenne ".$title0." :  ".$moyenne1."\n";


# Create chart object and specify the properties of the chart
$charts[0][0] = Chart::Gnuplot->new(
    ylabel => "NORTH",
	plotbg    =>  "white",
	grid   => "on",
	xtics     => {minor => 3},
	ytics    => {minor => 0.1},
    minorgrid => 'on',
	yrange => [-15,15],	
	
);

 


# Create dataset object and specify the properties of the dataset
my $dataSet = Chart::Gnuplot::DataSet->new(
    xdata => \@x,
    ydata => [\@y1,\@err1],
    title => "NORTH",
    style => "yerrorlines",  
	color => "forest-green",  
    width => 0.1,
    
);

my $dataSet2 = Chart::Gnuplot::DataSet->new(
    xdata => \@x,
    ydata => \@y1,
    style => "lines",  
	color => "black",  
    
);
$charts[0][0]->add2d($dataSet);
$charts[0][0]->add2d($dataSet2);




 ##############EST
open (REF2, $sta.".DE");
$l=<REF2>;
@tab_line = split(' ',$l);

$l=<REF2>;
while (defined($l=<REF2>))
{
	
	@tab_line = split ' ',$l;
	push @x2, $tab_line[0];		
	push @y2, $tab_line[1];
	push @err2, $tab_line[2];
}
# Calcul et affichage des moyenne des valeurs : 
#my $moyenne1=sum(@y1)/@y1;
#print "Moyenne ".$title0." :  ".$moyenne1."\n";


# Create chart object and specify the properties of the chart
$charts[1][0] = Chart::Gnuplot->new(
    ylabel => "EAST",
	grid   => "on",
	plotbg    =>  "white",
	xtics     => {minor => 3},
	ytics    => {minor => 0.1},
    minorgrid => 'on',
	yrange => [-15,15],	
);

 


# Create dataset object and specify the properties of the dataset
$dataSet = Chart::Gnuplot::DataSet->new(
    xdata => \@x2,
    ydata => [\@y2,\@err2],
    title => "EAST",
    style => "yerrorlines",  
	color => "forest-green",  
    width => 0.1,
    
);

$dataSet2 = Chart::Gnuplot::DataSet->new(
    xdata => \@x,
    ydata => \@y2,
    style => "lines",  
	color => "black",  
    
);
$charts[1][0]->add2d($dataSet);
$charts[1][0]->add2d($dataSet2);



##############Up
open (REF3, $sta.".DH");
$l=<REF3>;
@tab_line = split(' ',$l);

$l=<REF3>;
while (defined($l=<REF3>))
{
	
	@tab_line = split ' ',$l;
	push @x3, $tab_line[0];		
	push @y3, $tab_line[1];
	push @err3, $tab_line[2];
}
# Calcul et affichage des moyenne des valeurs : 
#my $moyenne1=sum(@y1)/@y1;
#print "Moyenne ".$title0." :  ".$moyenne1."\n";


# Create chart object and specify the properties of the chart
$charts[2][0] = Chart::Gnuplot->new(
    ylabel => "UP",
	grid   => "on",
	plotbg    =>  "white",
	xtics     => {minor => 3},
	ytics    => {minor => 0.1},
    minorgrid => 'on',
	yrange => [-25,25],	
);

 


# Create dataset object and specify the properties of the dataset
$dataSet = Chart::Gnuplot::DataSet->new(
    xdata => \@x3,
    ydata => [\@y3,\@err3],
    title => "UP",
    style => "yerrorlines",  
	color => "forest-green",  
    width => 0.1,
    
);
$dataSet2 = Chart::Gnuplot::DataSet->new(
    xdata => \@x,
    ydata => \@y3,
    style => "lines",  
	color => "black",  
    
);
$charts[2][0]->add2d($dataSet);
$charts[2][0]->add2d($dataSet2);

#on ajoute les soln
foreach (@date_soln)
{	
	
	# Add a line
	$charts[0][0]->line(
    from => "$_, -15",
    to   => "$_, 15",
	color => "dark-red",
	);
	$charts[1][0]->line(
    from => "$_, -15",
    to   => "$_, 15",
	color => "dark-red",
	);
	$charts[2][0]->line(
    from => "$_, -25",
    to   => "$_, 25",
	color => "dark-red",
   
);

}
# Plot the multplot chart
$multiChart->multiplot(\@charts);

if ($x[0]<2014 && $x[-1]>2014) # si on a des données après 2014
{
	@x = ();
	@y1= ();
	@err1= ();
	@x2 = ();
	@y2= ();
	@err2= ();
	@x3 = ();
	@y3= ();
	@err3= ();
	
	
	
	my $multiChart2 = Chart::Gnuplot->new(
		output => $sta."_2014-2019.png",
		title  => $title0,
	);

	my @charts = ();

	##############NORD
	open (REF, $sta.".DN");
	$l=<REF>;
	@tab_line = split(' ',$l);

	$l=<REF>;
	while (defined($l=<REF>))
	{
		
		@tab_line = split ' ',$l;
		push @x, $tab_line[0];		
		push @y1, $tab_line[1];
		push @err1, $tab_line[2];
	}
	# Calcul et affichage des moyenne des valeurs : 
	#my $moyenne1=sum(@y1)/@y1;
	#print "Moyenne ".$title0." :  ".$moyenne1."\n";


	# Create chart object and specify the properties of the chart
	$charts[0][0] = Chart::Gnuplot->new(
		ylabel => "NORTH",
		grid   => "on",
		plotbg    =>  "white",
		xtics     => {minor => 3},
		ytics    => {minor => 0.1},
		minorgrid => 'on',
		yrange => [-15,15],	
		xrange => [2014,"*"],
	);

	 


	# Create dataset object and specify the properties of the dataset
	my $dataSet = Chart::Gnuplot::DataSet->new(
		xdata => \@x,
		ydata => [\@y1,\@err1],
		title => "NORTH",
		style => "yerrorlines",  
		color => "forest-green",  
		width => 0.1,
		
	);

	my $dataSet2 = Chart::Gnuplot::DataSet->new(
		xdata => \@x,
		ydata => \@y1,
		style => "lines",  
		color => "black",  
		
	);
	$charts[0][0]->add2d($dataSet);
	$charts[0][0]->add2d($dataSet2);

	 ##############EST
	open (REF2, $sta.".DE");
	$l=<REF2>;
	@tab_line = split(' ',$l);

	$l=<REF2>;
	while (defined($l=<REF2>))
	{
		
		@tab_line = split ' ',$l;
		push @x2, $tab_line[0];		
		push @y2, $tab_line[1];
		push @err2, $tab_line[2];
	}
	# Calcul et affichage des moyenne des valeurs : 
	#my $moyenne1=sum(@y1)/@y1;
	#print "Moyenne ".$title0." :  ".$moyenne1."\n";


	# Create chart object and specify the properties of the chart
	$charts[1][0] = Chart::Gnuplot->new(
		ylabel => "EAST",
		plotbg    =>  "white",
		grid   => "on",
		xtics     => {minor => 3},
		ytics    => {minor => 0.1},
		minorgrid => 'on',
		yrange => [-15,15],	
		xrange => [2014,"*"],
	);

	 


	# Create dataset object and specify the properties of the dataset
	$dataSet = Chart::Gnuplot::DataSet->new(
		xdata => \@x2,
		ydata => [\@y2,\@err2],
		title => "EAST",
		style => "yerrorlines",  
		color => "forest-green",  
		width => 0.1,
		
	);

	$dataSet2 = Chart::Gnuplot::DataSet->new(
		xdata => \@x,
		ydata => \@y2,
		style => "lines",  
		color => "black",  
		
	);
	$charts[1][0]->add2d($dataSet);
	$charts[1][0]->add2d($dataSet2);



	##############Up
	open (REF3, $sta.".DH");
	$l=<REF3>;
	@tab_line = split(' ',$l);

	$l=<REF3>;
	while (defined($l=<REF3>))
	{
		
		@tab_line = split ' ',$l;
		push @x3, $tab_line[0];		
		push @y3, $tab_line[1];
		push @err3, $tab_line[2];
	}
	# Calcul et affichage des moyenne des valeurs : 
	#my $moyenne1=sum(@y1)/@y1;
	#print "Moyenne ".$title0." :  ".$moyenne1."\n";


	# Create chart object and specify the properties of the chart
	$charts[2][0] = Chart::Gnuplot->new(
		ylabel => "UP",
		grid   => "on",
		plotbg    =>  "white",
		xtics     => {minor => 3},
		ytics    => {minor => 0.1},
		minorgrid => 'on',
		yrange => [-25,25],	
		xrange => [2014,"*"],
	);

	 


	# Create dataset object and specify the properties of the dataset
	$dataSet = Chart::Gnuplot::DataSet->new(
		xdata => \@x3,
		ydata => [\@y3,\@err3],
		title => "UP",
		style => "yerrorlines",  
		color => "forest-green",  
		width => 0.1,
		
	);
	$dataSet2 = Chart::Gnuplot::DataSet->new(
		xdata => \@x,
		ydata => \@y3,
		style => "lines",  
		color => "black",  
		
	);
	$charts[2][0]->add2d($dataSet);
	$charts[2][0]->add2d($dataSet2);
	
	foreach (@date_soln)
	{	
	
	# Add a line
	$charts[0][0]->line(
    from => "$_, -15",
    to   => "$_, 15",
	color => "dark-red",
	);
	$charts[1][0]->line(
    from => "$_, -15",
    to   => "$_, 15",
	color => "dark-red",
	);
	$charts[2][0]->line(
    from => "$_, -20",
    to   => "$_, 20",
	color => "dark-red",
   
	);
	}
	# Plot the multplot chart
	$multiChart2->multiplot(\@charts);
}
