#!/usr/bin/perl -w
#
# Script to convert range of z values (center of bilayer becomes 0)
#

use strict;
use warnings;

use List::Util qw( min max );


# Open input files in read mode
open INPUTFILE, "<", $ARGV[0] or die $!;
open INPUTFILE2, "<", $ARGV[1] or die $!;
open INPUTFILE3, "<", $ARGV[2] or die $!;
open INPUTFILE4, "<", $ARGV[3] or die $!;
open INPUTFILE5, "<", $ARGV[4] or die $!;
open INPUTFILE6, "<", $ARGV[5] or die $!;

# Open output file in write mode
open OUTPUTFILE, ">", $ARGV[6] or die $!;

# Read the input file line by line and split in two arrays:
my @z1;
my @density1;
my @z2;
my @density2;
my @z3;
my @density3;
my @z4;
my @density4;
my @z5;
my @density5;
my @z6;
my @density6;

while(my $line = <INPUTFILE>){
    chomp $line;
    my @linearray = split(" ", $line);
#	print "@linearray \t";
    push(@z1, $linearray[0]);
    push(@density1, $linearray[1]);
}
my $numelements1=@z1;
#print "@z1 ";
#print "@density1 ";


my $i = 0;
while(my $line = <INPUTFILE2>){
    chomp $line;
    my @linearray = split(" ", $line);
        #print $line;
	push(@z2, $linearray[0]);
        push(@density2, $linearray[1]);
        $i++;
}
my $numelements2=@density2;

$i = 0;
while($z1[$i] != $z2[0]){
        unshift(@density2,0);
	$i++;
}
while($z1[$i] != $z2[$i]){
        push(@density2,0);
        $numelements2=@density2;
                $i++;
}

$i = 0;
while(my $line = <INPUTFILE3>){
    chomp $line;
    my @linearray = split(" ", $line);
	push(@z3, $linearray[0]);
	push(@density3, $linearray[1]);
	$i++;
}
my $numelements3=@density3;

$i = 0;
while($z1[$i] != $z3[0]){
        unshift(@density3,0);
        $i++;
}
while($z1[$i] != $z3[$i]){
        push(@density3,0);
        $numelements3=@density3;
                $i++;
}

$i = 0;
while(my $line = <INPUTFILE4>){
    chomp $line;
    my @linearray = split(" ", $line);
	push(@z4, $linearray[0]);
	push(@density4, $linearray[1]);
	$i++;
}
my $numelements4=@z4;

$i = 0;
while($z1[$i] != $z4[0]){
        unshift(@density4,0);
        $i++;
}
while($z1[$i] != $z4[$i]){
        push(@density4,0);
        $numelements4=@density4;
                $i++;
}


$i = 0;
while(my $line = <INPUTFILE5>){
    chomp $line;
    my @linearray = split(" ", $line);
        push(@z5, $linearray[0]);
        push(@density5, $linearray[1]);
        $i++;
}
my $numelements5=@z5;

$i = 0;
while($z1[$i] != $z5[0]){
        unshift(@density5,0);
        $i++;
}
while($z1[$i] != $z5[$i]){
        push(@density5,0);
        $numelements5=@density5;
                $i++;
}

$i = 0;
while(my $line = <INPUTFILE6>){
    chomp $line;
    my @linearray = split(" ", $line);
        push(@z6, $linearray[0]);
        push(@density6, $linearray[1]);
        $i++;
}
my $numelements6=@z6;

#print join(", ", @density6);
#print "\n";
$i = 0;
while($z1[$i] != $z6[0]){
        unshift(@density6,0);
        $i++;
}
#$i = 0;
while($numelements1 != $numelements6){
	push(@density6,0);
        $numelements6=@density6;
                $i++;
}

my $min = min @z1;
my $max = max @z1;
my $range = $max - $min;
my $diff = $range / 2; 

print OUTPUTFILE "\@type xy \n";
print OUTPUTFILE "\@title \"Density\" \n";
print OUTPUTFILE "\@xaxis label \"z (angstroms)\" \n";
print OUTPUTFILE "\@yaxis label \"Electron density\" \n";
print OUTPUTFILE "\@ s0 legend \"POPC headgroup\" \n";
print OUTPUTFILE "\@ s1 legend \"POPC tails\" \n";
print OUTPUTFILE "\@ s2 legend \"POPC glycerol\" \n";
print OUTPUTFILE "\@ s3 legend \"CHOL headgroup\" \n";
print OUTPUTFILE "\@ s4 legend \"CHOL hydrocarbon chain\" \n";
print OUTPUTFILE "\@ s5 legend \"D301\" \n";
print OUTPUTFILE "\@ legend 0.9, 0.8 \n";
#print OUTPUTFILE "\@ world -45 0 45 0.9 \n";
print OUTPUTFILE "\@ WORLD XMIN -45 \n";
print OUTPUTFILE "\@ WORLD XMAX 45 \n";
print OUTPUTFILE "\@ WORLD YMIN 0 \n";
print OUTPUTFILE "\@ WORLD YMAX 0.5 \n";
print OUTPUTFILE "\@ AUTOSCALE ONREAD NONE \n";



for (my $j = 0; $j < $numelements1; $j++){
	$z1[$j] = $j - $diff;
	print OUTPUTFILE "$z1[$j] \t $density1[$j] \t $density2[$j] \t $density3[$j] \t $density4[$j] \t $density5[$j] \t $density6[$j] \n";
}

# Close the input and output files
close INPUTFILE;
close OUTPUTFILE;


