#!/usr/bin/perl
#
# compute scaling factors for pictures of each planet
#
# MWR 9/24/2016

$debug = 1;
$do_it = 0;

$num_obj = 0;
# this is the desired scale
$scale_name = "small";
$want_scale = 100.0;

$infile = "size.txt";
open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  @words = split(/\s+/, $line);

  $name[$num_obj] = $words[0];
  $phys_diam[$num_obj] = $words[1];
  $pix_diam[$num_obj] = $words[2];
  if ($debug > 0) {
    printf " name %15s  phys_km %8.0f  pix %8.0f  \n", 
          $name[$num_obj], $phys_diam[$num_obj], $pix_diam[$num_obj];
  }

  # compute current scale (km/pix)
  $cur_scale = $phys_diam[$num_obj] / $pix_diam[$num_obj];
  # compute factor by which we must multiply current image
  $factor = $cur_scale / $want_scale;
  $perc_factor = $factor*100.0;

  if ($debug > 0) {
    printf " must mult %15s  by %6.3f percent \n", 
        $name[$num_obj], $perc_factor;
  }

  # call this the 
  $orig_name = sprintf "%s_orig.jpg", $name[$num_obj];
  $new_name = sprintf "%s/%s_%s.jpg", $scale_name, $name[$num_obj], $scale_name;
  $cmd = sprintf "convert $orig_name -geometry %.1f%sx%.1f%s $new_name",
             $perc_factor, "%", $perc_factor, "%";
  if ($debug > 0) {
    printf "cmd is ..%s.. \n", $cmd;
  }

  if ($do_it == 1) {
    $ret = `$cmd`;
  }
  
  $num_obj++;



}
close(INFILE);



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


