#!/usr/bin/perl
#-------------------------------------------------------------------------------
#Purpose     : have a quick look at a fits file
#Author      : Robert Greimel (greimel@uvastro.phys.uvic.ca,greimel@ing.iac.es)
#Last Change : 17-Oct-1999
#-------------------------------------------------------------------------------
#use lib "/data2/usr/greimel/PRV/reduction/scripts-0.13";
use PGPLOT;
use PDL;
#use PDL::Lite;
#use PDL::IO::Misc;

# check usage
if ($#ARGV != 0)
  {die("usage: quicklook.perl <datafile>\n");}

# check if file exists
if (! -e $ARGV[0]) {die "\nfile $ARGV[0] does not exist ... bailing out\n\n";}

# load image
$image = rfits ($dir.$ARGV[0]);
$header = $image->gethdr;

$file = (split /\//,$ARGV[0])[-1];
$obj = ${$header}{"OBJECT"};
$obj =~ s/'//g;
print "$file : $obj\n";

$na  = ${$header}{"NAXIS"};
print "This image has $na axis\n";

if ( $na == 1 )
  {
  $na1 = ${$header}{"NAXIS1"};
  print "image dimension: $na1\n";

  $max = max($image);
  $min = min($image);
  print "image value range: $min - $max\n";

  pgbegin(0,"/xserve",1,1);
  pgenv(1,$na1,$min,$max,0,0);
  pglabel('Pixel','Intensity',$file." :: ".$obj);
  @yvals=$image->list;
  #pgpoint($na1,[1..$na1],\@yvals,17);
  pgline($na1,[1..$na1],\@yvals);
  pgend;
  }
elsif ( $na == 2 )
  {
  $na1 = ${$header}{"NAXIS1"};
  $na2 = ${$header}{"NAXIS2"};
  print "image dimensions: $na1 x $na2\n";

  #lets convert this to unsigned ...
  $slice = ushort($image->slice("*,*"));

  $max = max($slice);
  $min = min($slice);
  print "image value range: $min - $max\n";

  pgbegin(0,"/xserve",1,1);
  imag($slice);
  pgend;
  }
else
  {
  print "Sorry - I don't know how to handle this number of axis."
  }
