#!/usr/bin/perl
#
# find and measure stars in each frame, generating .coo and .pht files

$debug = 1;

# set to 1 to see pictures, one frame at a time
$interactive = 0;

# set this to be the pattern of the images you want to measure
@files = glob("m51-long??.fit");

# parameters which affect the finding of stars
#   minimum permitted FWHM for a real star (pixels)
$minfwhm = 2.0;
#   maximum permitted FWHM for a real star (pixels)
$maxfwhm = 7.0;
#   a real star must be at least this many sigma above noise in sky 
$minsig = 10.0;

##########################################################################
################### probably no need to modify below this line ###########

# set to 1 to use "masked" versions of images
$use_masked = 0;

# set to 1 to restrict search to the box shown below
$use_box = 0;
$box_num = 2;
$cmd = "box 2 cr=162 cc=255 nr=275 nc=510";
$retval = `$cmd`;
if (($debug > 0) && ($use_box == 1)) {
  printf "using a box $cmd ... \n";
}

# first, we generate the list of files to be reduced
#@files = glob("sdss*-[02][04][39].fit");
#if ($interactive == 0) {
#  @files = glob("tx*.fit");
#}
#else {
#  @files = glob("tx*?[02][39].fit");
#}
#@files = glob("tres3-?[02][39].fit");
#@files = glob("amcvn-?[26][3].fit");
#@files = glob("iyuma-?[26][3].fit");
#@files = glob("iyuma-*.fit");
#@files = glob("her0_*I.fit");
#@files = glob("her0-0?5I.fit");
#if ($use_masked == 0) {
#  @files = glob("mvlyr-???.fit");
  #@files = glob("mvlyr-?50.fit");
#}
#else {
#  @files = glob("masked/tres*-*.fit");
  #@files = glob("masked/tresi_013I.fit");
  #@files = glob("masked/foo*.fit");
#}
#@files = glob("tresher0-0[012]3*.fit");
#@files2 = glob("henden_?-????.fit");
#@files3 = glob("elis_?-????.fit");
#@files1 = glob("naka_[abcd]-020c.fit");
#@files1 = glob("rxc-00[456].fit");
#@files2 = glob("be87_?.???");
#@files2 = glob("be87_?.???");
$nframes = 0;
foreach $file (@files) {
#foreach $file (@files1, @files2, @files3) {
  $frame_array[$nframes] = $file;
  $nframes++;
}

# now process each frame in turn
foreach $file (@frame_array) {
  $base = $file;
  $base =~ s/\./_/;
  if ($use_masked == 1) {
    $base =~ s/masked\///;
  }
  printf "now working on $file = $base\n";

 if (1 == 1) {

  $cmd = "sky $file bin=5";
  $ret = exec_cmd($cmd);
  # normal in-focus stars
  #$cmd = "stars $file minsig=5 minfwhm=1.0 maxfwhm=4.0 minsharp=-1.0 maxsharp=1.0 minround=-1.0 maxround=1.0 movemax=2 outfile=$base.coo";
  # for out-of-focus stars which look like donuts
  $cmd = "stars $file minsig=$minsig minfwhm=$minfwhm maxfwhm=$maxfwhm minsharp=-5.0 maxsharp=1.0 minround=-1.0 maxround=1.0 movemax=2 outfile=$base.coo";
  if ($use_box == 1) {
    $cmd .= " box=$box_num ";
  }
  if ($interactive == 1) {
    $cmd .= " verbose show ";
    system("tv $file zoom=1 z=sky-100 l=500 zoom=1; sleep 2");

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

  if ($interactive == 1) {
    printf "skipping phot because interactive \n";
  }
  else {

    $cmd = "phot $file infile=$base.coo outfile=$base.pht aper=3 aper=5 aper=7 ";
    $cmd .= " saturate=30000 readnoise=15 gain=2.5 empscatter";
    if ($debug > 0) {
      printf "cmd is ..$cmd.. \n";
    }
    `$cmd`;
   }

   if ($interactive == 1) {
     printf "\nhit return to continue: \n";
     $a = <STDIN>;
     `killall -1 tv`;
   }
 
 
}


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


