#!/usr/bin/perl
#
# Starting with a bunch of FITS images, create
# an animated GIF movie of them in order.
# 
# MWR 4/14/2006

$debug = 1;

# clean up temporary files when done?
$cleanup = 0;

# pattern which will match the FITS file names
#$file_glob = "./QQ_00?.fit";
$file_glob = "./dipper_00[012]?.fts";
# give temp files names that start with this
$temp_prefix = "SSS_anim_";

# the typical sky level 
$sky_level = 2000;

# the typical sky sigma
$sky_sigma = 44;

# this sets roughly the pixel level
#    of the top of the greyscale -- 
#    everything higher is pure black
$top_pix_val = 5000;

# invert the image, to yield black stars on white background?
$invert_flag = 0;

# skip this many images between each image
#    we include in the movie
#    (note bash has limit of 109 args)
#    use skip_number=0 to include all images
$skip_number = 0;

# change this value if you want to scale the image up or down in size
$scale_factor = 1;

# gifmerge control parameters
#    delay between frames, in 1/100 of a sec
$gifmerge_delay = 50;
#    name of output file
$gifmerge_outfile = "make_anim_gif.gif";


$image_index = 0;
$include_count = 0;
@files = glob($file_glob);
foreach $file (@files) {
  if ($debug > 0) {
    printf "next is file ..$file.. \n";
  }

  if (($image_index % ($skip_number + 1)) == 0) {
    if ($debug > 0) {
      printf " will include file $file ..\n";
    }
  }
  else {
    if ($debug > 0) {
      printf " will skip    file $file ..\n";
    }
    $image_index++;
    next;
  }
    
  # make a copy of the FITS image
  $temp_fits = sprintf "%s.fit", $temp_prefix;
  $cmd = "cp $file $temp_fits ";
  exec_cmd($cmd);
  
  # subtract the sky value
  $cmd = "sub $temp_fits const=$sky_level ";
  exec_cmd($cmd);

  # add a constant to shift most of the noise above zero 
  $cmd = "add $temp_fits const=$sky_sigma ";
  exec_cmd($cmd);
 
  # clip all negative values to zero, and all values
  #    greater than the top_pix_val to the top_pix_val
  $cmd = "clip $temp_fits min=0 vmin=0 ";
  $cmd .= " max=$top_pix_val vmax=$top_pix_val ";
  exec_cmd($cmd);

  if ($invert_flag == 1) {
    # multiply by negative one, to invert image values
    $cmd = "mul $temp_fits const=-1 ";
    exec_cmd($cmd);

    # now add the top_pix_val, so the range of values
    #    runs from zero to top_pix_val
    $cmd = "add $temp_fits const=$top_pix_val ";
    exec_cmd($cmd);
  }

  # rescale the image so that it runs from 0 to 255
  $cmd = "mul $temp_fits const=255 div=$top_pix_val ";
  exec_cmd($cmd);

  # run the conversion programs to turn FITS into GIF
  $temp_pnm = sprintf "%s.pnm", $temp_prefix;
  $temp_pnm2 = sprintf "%s_2.pnm", $temp_prefix;
  $temp_jpg = sprintf "%s.jpeg", $temp_prefix;
  $temp_gif[$include_count] = sprintf "%s_%04d.gif", $temp_prefix, $image_index;
  $cmd = "fitstopnm -min 0 -max 255 $temp_fits > $temp_pnm ";
  exec_cmd($cmd);
  # if the scale factor is not 1, we scale up/down the image here
  if ($scale_factor != 1) {
    $cmd = "pnmscale 2 $temp_pnm > $temp_pnm2 ";
  }
  else {
    $cmd = "cp $temp_pnm $temp_pnm2"
  }
  exec_cmd($cmd);

  #
  # add text to the image
  # get the time for this image from the addheader.log file
  if (0 == 1) {
    $orig_file = sprintf "pluto-%03d.fit", $image_index + 1;
    printf " orig_file is ..%s.. \n", $orig_file;
    $cmd = "grep $orig_file addheader.log | awk '{ print \$7 }'";
    $time_string = exec_cmd($cmd);
    chomp($time_string);
    $time_string =~ s/:/\ /g;
  } 
  else {
    $time_string = sprintf "%4d", $image_index + 1;
  }
  $cmd = "convert -font helvetica -fill gold -pointsize 48 ";
  $cmd .= "    -draw \"text 50,50 '$time_string'  \" ";
  $cmd .= "  $temp_pnm2 $temp_pnm";
  exec_cmd($cmd);
  $cmd = "cp $temp_pnm $temp_pnm2";
  exec_cmd($cmd);
  #
  $cmd = "pnmtojpeg $temp_pnm2 > $temp_jpg ";
  exec_cmd($cmd);
  $cmd = "convert $temp_jpg $temp_gif[$include_count] ";
  exec_cmd($cmd);

  $image_index++;
  $include_count++;
}

#############################################################
# The final step: run gifmerge to create an animate GIF
$cmd = " ~/bin/gifmerge -$gifmerge_delay ";
for ($i = 0; $i < $include_count; $i++) {
  $cmd .= " $temp_gif[$i] ";
}
$cmd .= " > $gifmerge_outfile ";
exec_cmd($cmd);


# clean up temporary files
if ($cleanup == 1) {
  @remove_files = glob("$temp_prefix*");
  foreach $remove_file (@remove_files) {
    unlink($remove_file);
  }
}


exit 0;



##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }


  return($ret);
}


