#!/usr/bin/perl
#
# Create an artificial image showing stars in and around
#   the Big Dipper, with accurate positions and magnitudes
#   from the Bright Star Catalog.
#
# MWR 7/25/2017

use POSIX;

$debug = 1;

# this file comes from SIMBAD, has 17 stars, some faint
#   I've projected the stars around RA=182 Dec=+55
$proj_file = "./dipper.proj";
# this will be a file we can use as input to "pstar"
$field_file = "./dipper_pstar.dat";
# name of final image
$image_file = "./dipper.fts";

$temp_img = "./make_dipper.tmp";


# convert (xi, eta) to (row, col) for the stars
open(FIELD_FILE, ">$field_file") || die("can't create file $field_file");
open(PROJ_FILE, "$proj_file") || die("can't open file $proj_file");
while (<PROJ_FILE>) {
  $line = $_;
  @words = split(/\s+/, $line);
  
  $hr_id = $words[1];
  $xi = $words[2];
  $eta = $words[3];
  $mag = $words[4];

  $mag = pow($mag, 1.2);

  # compute a row and col from xi, eta, scaling to a reasonable size
  $scale = 1000.0;
  $row = 300 + $eta*$scale;
  $col = 300 + $xi*$scale;

  # print out one line per star, with format
  #    ID   row  col  mag
  printf FIELD_FILE " %5d  %7.1f  %7.1f  %6.3f \n", $hr_id, $row, $col, $mag;
}
close(PROJ_FILE);
close(FIELD_FILE);


# create a bunch of images which show ordinary Dipper Stars only
$num_images = 100;
if (0 == 1) {
		  for ($i = 0; $i < $num_images; $i++) {

			 $outfile = sprintf "dipper_%04d.fts", $i;

					 # run the "pstar" program to create the image
					 $nrow = 600;
					 $ncol = 700;
					 $cmd = "pstar nrow=$nrow ncol=$ncol starlist=$field_file ";
					 $cmd .= "   fwhm=2.0 sky=2000 photon ";
					 $cmd .= "   outfile=$temp_img ";
					 $cmd .= "   seed=$i ";
					 $ret = exec_cmd($cmd);


					 # now rotate the image by 180 degrees
					 $cmd = "rotate $temp_img pa=180 outfile=$outfile ";
					 $ret = exec_cmd($cmd);

		  }
}


# now, create one image showing Dipper plus transient source
$trans_row = 417;
$trans_col = 269;
$trans_mag = 2.0;
$trans_id  = 1;
$trans_file = "dipper_trans.dat";
$cmd = "cp $field_file $trans_file";
$ret = exec_cmd($cmd);
open(TRANS_FILE, ">>$trans_file") || die("can't append to $trans_file");
printf TRANS_FILE "  %5d  %7.1f  %7.1f  %6.3f \n", 
     $trans_id, $trans_row, $trans_col, $trans_mag;
close(TRANS_FILE);


  $i = 20;
  $outfile = sprintf "trans_%04d.fts", $i;

		  # run the "pstar" program to create the image
		  $nrow = 600;
		  $ncol = 700;
		  $cmd = "pstar nrow=$nrow ncol=$ncol starlist=$trans_file ";
		  $cmd .= "   fwhm=2.0 sky=2000 photon ";
		  $cmd .= "   outfile=$temp_img ";
        $cmd .= "   seed=$i ";
		  $ret = exec_cmd($cmd);


		  # now rotate the image by 180 degrees
		  $cmd = "rotate $temp_img pa=180 outfile=$outfile ";
		  $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);
}


