#!/usr/bin/perl

# Script by J. Gutow 4/2006
# Converts MacMolPlt grid file to Gaussian .cube file.
# based on script which tested reading standard input.  So I can read a pipe.
#This script is distributed under the GNU Public License.  You are free to distribute this script to others.
#You may modify the script and add your name to the contributors list at the end of the script.  
#DO NOT REMOVE THE CONTRIBUTORS LIST.

my $filename = @ARGV[0];

my $count = 0;

open (INFILE, $filename) or die "Can't open $filename : $!";

#Slurp the file in
my @gridfile = <INFILE>;
close (INFILE);
#See if we got more than one line (=> crlf line breaks)
my $crlflinecount = @gridfile;
#print "Lines in crlf mode = $crlflinecount\n";
my @filelist = ();
#Split out the lines based on containing just cr.  Do file by sections.
while ($count < $crlflinecount){
    @sublines = split /\r\s*/,$gridfile[$count];
#    print "$_\n";
    my $returnlines = @sublines;
#    print "lines split out = $returnlines\n";
    my $sublinescount = 0;
    while ($sublinescount < $returnlines){
# Remove trailing line dividers.    
    chomp $sublines[$sublinescount];
#    print "$sublines[$sublinescount]\n";
    $sublinescount= $sublinescount+1;
    }
    @filelist = (@filelist,@sublines);
    $count = $count + 1;
    }
$count = 0;
#print "First 10 elements of new array:\n";
#while ($count < 10){
#    print "$filelist[$count]\n";
#    $count = $count + 1;
#    }

#Now we have lines from the file

#Now need to make a bunch of new lines, will just write to file as constructed.

#First will need to create new filename, which should be based on old file, but with .cube extension.
use File::Basename;
my ($name, $path, $suffix) = fileparse($filename,qr{\..*});
chomp $name;
chomp $path;
chomp $suffix;
#print"name: $name\n path: $path\n suffix: $suffix\n";

my $outfilename = "$path$name.cube";
#print "output file: $outfilename\n";

#open the file
open (OUTFILE,">$outfilename") or die "Can't open $outfilename: $!";

#print the top two lines to the file (comments)
print OUTFILE "$filelist[0]\n";
print OUTFILE "File modified from MacMolPlt grid file to Gaussian .cube by MacMolPlttocube.pl.\n";

#Now we parse the lines to make the necessary introductory lines.  Then the data can just be
#written as it is stored.
#line 3 is # of atoms (set to zero) and origin of grid.
print OUTFILE "0 $filelist[2]\n";
#line 4-6 are x, y and z grid (pts along axis, x-comp, y-comp, z-comp).
my @ngridpts = split / \s*/,$filelist[1];
my @gridincr = split / \s*/,$filelist[3];
# x-axis info
print OUTFILE "$ngridpts[0] $gridincr[0] 0.000000 0.000000\n";
# y-axis info
print OUTFILE "$ngridpts[1] 0.000000 $gridincr[1] 0.000000\n";
# z-axis info
print OUTFILE "$ngridpts[2] 0.000000 0.000000 $gridincr[2]\n";
#Now write the grid point values
$count = 4;
my $filelines = @filelist;
while ($count < $filelines) {
    print OUTFILE "$filelist[$count]\n";
    $count = $count +1;
    }
close (OUTFILE);

#Now compress and delete the uncompressed version.  I am not checking for success. Probably should.
readpipe "gzip $outfilename";
end;

#Contributors: Jonathan Gutow (original author)