#!/usr/bin/perl

# PDL modules:
use PDL;
use PDL::Core;
use PDL::Basic;
use PDL::NiceSlice;
use PDL::ImageND;

$pattern = shift;
print "Got pattern: $pattern\n";
$slice = -1;
@files = glob $pattern;

print "Binning (choose 1,2,4,8...etc): ";
chomp($binning=<>);
print "\n";
print "Take Log10 of data? (0=no, 1=yes): ";
chomp($logscale=<>);
print "\n";

$n_files = $#files;
$count = 0;
foreach $filename (@files){
print  "------------------------------------------------------------------\n";
    print "Working on: $filename\n";
    $im = rfits $filename;
    $hdr = $im->gethdr;
    $im = float $im;
    $dims = pdl $im->dims;
    print "DIMS PDL : $dims\n";
    @dims = $dims->list;
	print "Image Dimensions are : @dims\n";
    if($dims[2] > 0){
    if($slice == -1){
        print "Enter slice number: ";
        chomp($slice = <>);
        print "\n";
	print "This has a z-dimension.... slicing plane [ z = $slice ]\n";
	$im = $im(:,:,$slice);
	$im = $im->reshape($dims[0],$dims[1]);
    }
    }
    &z_maker($im,$hdr,$binning,$filename,$logscale);
    ++$count;
print  "------------------------------------------------------------------\n";
}


########################################################

sub z_maker {
    my ($gle_im,$gle_hdr,$binning,$filename,$logscale) = @_;

    my $xdims0 = pdl $gle_im->dim(0);
    my $ydims0 = pdl $gle_im->dim(1);
    my $Nxpix = $xdims0/$binning;
    my $Nypix = $Nxpix*($ydims0/$xdims0);
    $Nxpix = $Nxpix->sclr;
    $Nypix = $Nypix->sclr;

    if($binning == 1){
	print "1x1 binning selected\n";
        print "Image size will be [Nx,Ny] = [$Nxpix , $Nypix]\n";
    }
    else {
	print "$binning x $binning binning selected...\n";
        print "Rebinning to [Nx,Ny] = [$Nxpix , $Nypix]\n";
    $gle_im = rebin($gle_im,$Nxpix,$Nypix);
    print "Final image dimensions: ";
    print $gle_im->info,"\n";
    }

    print "Final image dimensions: ";
    print $gle_im->info,"\n";

    if($logscale == 1){
    $minval = min($gle_im->where($gle_im > 0));
    ($gle_im->where($gle_im <= 0)) .= $minval; 
    $gle_im = log($gle_im)/log(10);
    # Dealing with any zeroes!
#    $gle_nans = $gle_im->where(!($gle_im >= 0));
#    $gle_nans .= -1;
    }
    my $xdims = pdl $gle_im->dim(0);
    my $ydims = pdl $gle_im->dim(1);
    my $line1 = "! nx $Nxpix ny $Nypix xmin 0 xmax $Nxpix ymin 0 ymax $Nypix";
    my $max = max($gle_im);
    my $min = min($gle_im);

    my $c = 0;
    my $string = "";
    my @bits = ();


    $_ = $filename;
    my $newbit = "_".$binning."x".$binning.".z";
    s/.fits.gz/$newbit/;
    s/.fits/$newbit/;
    my $gle_data = $_;

###############################################
    open(OUT,">$gle_data") || die "Cannot open $gle_data for writing!\n";
    print OUT "$line1\n";
###############################################
for($i=0;$i<$ydims;++$i){
    my @bits = ();
    my @bits =  list($gle_im(:,$i)->flat);
    print OUT "@bits\n";
}
###############################################
    close(OUT);
###############################################

    print "Done $gle_data\n";
  }
