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

$debug = 0;

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

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

# first, we generate the list of files to be reduced
@files = glob("iyuma-*.fit");
$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/\./_/;
  printf "now working on $file = $base\n";


  # measure the sky on this frame
  `sky $file`;

  # now run the "stars" command to find objects 
  $cmd = "stars $file minsig=10 minfwhm=1.2 maxfwhm=5.0 minsharp=-1.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=4 aper=5  readnoise=15 gain=2.2 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;
  
