#!/usr/bin/perl
#
# Read as many "calc_sn_mags.out" files as can be found,
#    and gather all the information into a single file.
#    The format of the file will be one stanza per passband,
#    with stanzas separated by two blank lines:
#
#    # band     JD             mag     
#      B     24455800.53212    12.843
#      B     24455800.53212    12.843
#      B     24455800.53212    12.843
#      B     24455800.53212    12.843
#
#     
#      V     24455800.53212    12.843
#      V     24455800.53212    12.843
#
#               etc.
#      
# MWR 8/31/2011

$debug = 0;

$num_lines = 0;
@passband_list = ("B", "V", "R", "I");
$output_file = "sn2013ej_mags.out";
printf "# sending output to $output_file \n";

$topdir = "~/z1/ritobs/";
$globstr = sprintf "%s/?????_201[3]", $topdir;
@dirglob = glob("$globstr");
foreach $dir (@dirglob) {
  if ($debug > 0) {
    printf  "next dir is ..%s.. \n", $dir;
  }

  $snfile = sprintf "%s/work/pff/calc_sn_mags.out", $dir;
  if (!-f $snfile) {
    next;
  }
  if ($debug > 0) {
    printf " next file is %s \n", $snfile;
  }

  # read the information from the file
  open(SNFILE, "$snfile") || die("can't open file $snfile"); 
  while (<SNFILE>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    if ($line !~ / SN /) {
      next;
    }
    @words = split(/\s+/, $line);
    if ($#words != 11) {
      printf " error, %s bad line %s \n", $snfile, $line;
      exit(1);
    }
    $passband_array[$num_lines] = $words[2];
    $snmag_array[$num_lines] = $words[4];
    $jd_array[$num_lines] = $words[11];
    if ($debug > 0) {
      printf " %d  %s %6.3f %12.5f \n", $num_lines, 
             $passband_array[$num_lines],
             $snmag_array[$num_lines],
             $jd_array[$num_lines];
    }
    $num_lines++;

  }
  close(SNFILE);

}


#####
#  Now, we loop over the passbands
#      Print out information for each passband to stdout
#
open(OUTPUT_FILE, ">$output_file") || die("can't open file $output_file");
printf OUTPUT_FILE  "#%-4s %12s %7s \n", "band", "JD", "SN_mag";

foreach $passband (@passband_list) {
  if ($debug > 0) {
    printf "next passband is %s \n", $passband;
  }
  for ($i = 0; $i < $num_lines; $i++) {
     if ($passband_array[$i] eq $passband) {
       if ($debug > 0) {
             printf "%-5s %12.5f %7.4f \n", $passband, 
                  $jd_array[$i], $snmag_array[$i];
       }
       printf OUTPUT_FILE "%-5s %12.5f %7.4f \n", $passband, 
           $jd_array[$i], $snmag_array[$i];
     }
  }
  printf OUTPUT_FILE "\n\n";

}
close(OUTPUT_FILE);





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




