#!/usr/bin/perl
#
# Create animated GIFs showing motion of spectral lines for
#   various amplitudes of motion for Arcturus

$debug = 1;

# for max motion 10,000 m/s
@files = ("sun_arc_spec_b.png", "sun_arc_spec_b2.png", "sun_arc_spec_b3.png",
          "sun_arc_spec_b4.png", "sun_arc_spec_b5.png", "sun_arc_spec_b4.png",
          "sun_arc_spec_b3.png", "sun_arc_spec_b2.png");
@gif_files = ();
foreach $file (@files) {
  $gif_file = $file;
  $gif_file =~ s/.png/.gif/;
  push (@gif_files, $gif_file);
  $cmd = "convert -flatten -background white $file $gif_file";
  $ret = exec_cmd($cmd);
}
$cmd = "gifmerge -l5 -200 ";
foreach $gif_file (@gif_files) {
  $cmd .= " $gif_file ";
}
$cmd .= " > sun_spec_anim_b.gif";
$ret = exec_cmd($cmd);


# for max motion 1,000 m/s
@files = ("sun_arc_spec_c.png", "sun_arc_spec_c2.png", "sun_arc_spec_c3.png",
          "sun_arc_spec_c4.png", "sun_arc_spec_c5.png", "sun_arc_spec_c4.png",
          "sun_arc_spec_c3.png", "sun_arc_spec_c2.png");
@gif_files = ();
foreach $file (@files) {
  $gif_file = $file;
  $gif_file =~ s/.png/.gif/;
  push (@gif_files, $gif_file);
  $cmd = "convert -flatten -background white $file $gif_file";
  $ret = exec_cmd($cmd);
}
$cmd = "gifmerge -l5 -100 ";
foreach $gif_file (@gif_files) {
  $cmd .= " $gif_file ";
}
$cmd .= " > sun_spec_anim_c.gif";
$ret = exec_cmd($cmd);



# for max motion 1,000 m/s and zoomed in
@files = ("sun_arc_spec_d.png", "sun_arc_spec_d2.png", "sun_arc_spec_d3.png",
          "sun_arc_spec_d4.png", "sun_arc_spec_d5.png", "sun_arc_spec_d4.png",
          "sun_arc_spec_d3.png", "sun_arc_spec_d2.png");
@gif_files = ();
foreach $file (@files) {
  $gif_file = $file;
  $gif_file =~ s/.png/.gif/;
  push (@gif_files, $gif_file);
  $cmd = "convert -flatten -background white $file $gif_file";
  $ret = exec_cmd($cmd);
}
$cmd = "gifmerge -l5 -100 ";
foreach $gif_file (@gif_files) {
  $cmd .= " $gif_file ";
}
$cmd .= " > sun_spec_anim_d.gif";
$ret = exec_cmd($cmd);



# for max motion 100 m/s and zoomed in
@files = ("sun_arc_spec_e.png", "sun_arc_spec_e2.png", "sun_arc_spec_e3.png",
          "sun_arc_spec_e4.png", "sun_arc_spec_e5.png", "sun_arc_spec_e4.png",
          "sun_arc_spec_e3.png", "sun_arc_spec_e2.png");
@gif_files = ();
foreach $file (@files) {
  $gif_file = $file;
  $gif_file =~ s/.png/.gif/;
  push (@gif_files, $gif_file);
  $cmd = "convert -flatten -background white $file $gif_file";
  $ret = exec_cmd($cmd);
}
$cmd = "gifmerge -l5 -100 ";
foreach $gif_file (@gif_files) {
  $cmd .= " $gif_file ";
}
$cmd .= " > sun_spec_anim_e.gif";
$ret = exec_cmd($cmd);




# for max motion 10 m/s and zoomed in
@files = ("sun_arc_spec_f.png", "sun_arc_spec_f2.png", "sun_arc_spec_f3.png",
          "sun_arc_spec_f4.png", "sun_arc_spec_f5.png", "sun_arc_spec_f4.png",
          "sun_arc_spec_f3.png", "sun_arc_spec_f2.png");
@gif_files = ();
foreach $file (@files) {
  $gif_file = $file;
  $gif_file =~ s/.png/.gif/;
  push (@gif_files, $gif_file);
  $cmd = "convert -flatten -background white $file $gif_file";
  $ret = exec_cmd($cmd);
}
$cmd = "gifmerge -l5 -100 ";
foreach $gif_file (@gif_files) {
  $cmd .= " $gif_file ";
}
$cmd .= " > sun_spec_anim_f.gif";
$ret = exec_cmd($cmd);






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);
}


