#!/usr/bin/perl
#
# Given one big list of SNe, break it into 
#    several small output files, based on the
#    redshift of the supernovae
#
#          0.00 < z < 0.05      "near"
#          0.05 < z < 0.50      "middle"
#          0.50 < z < 5.00      "distant"
#              unknown z        "noz"
#
# MWR 10/19/2007

$debug = 1;

$output_base = "./snlist_";

$file = sprintf "%s%s", $output_base, "noz.dat"; 
open(NOZ_FILE, ">$file") || die("can't open file $file");
$file = sprintf "%s%s", $output_base, "near.dat"; 
open(NEAR_FILE, ">$file") || die("can't open file $file");
$file = sprintf "%s%s", $output_base, "middle.dat"; 
open(MIDDLE_FILE, ">$file") || die("can't open file $file");
$file = sprintf "%s%s", $output_base, "distant.dat"; 
open(DISTANT_FILE, ">$file") || die("can't open file $file");


while (<STDIN>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
  chomp($line);

  $z_str = substr($line, 92, 7);
  if ($debug > 0) {
    printf "next z is ..%s.. ", $z_str;
  }
  if ($z_str =~ /\?/) {
    if ($debug > 0) {
      printf " skipping uncert \n";
    }
    next;
  }
  if (($z_str =~ /[a-z]/) || ($z_str =~ /[A-Z]/)) {
    if ($debug > 0) {
      printf " skipping nonumeric \n";
    }
    next;
  }
  if ($z_str <= 0.0001) {
    if ($debug > 0) {
      printf " noz \n";
    }
    printf NOZ_FILE "%s\n", $line;

  }
  if (($z_str > 0.0001) && ($z_str <= 0.05)) {
    if ($debug > 0) {
      printf " near \n";
    }
    printf NEAR_FILE "%s\n", $line;

  }
  if (($z_str > 0.05) && ($z_str <= 0.50)) {
    if ($debug > 0) {
      printf " middle \n";
    }
    printf MIDDLE_FILE "%s\n", $line;

  }
  if (($z_str > 0.50) && ($z_str <= 5.00)) {
    if ($debug > 0) {
      printf " distant \n";
    }
    printf DISTANT_FILE "%s\n", $line;

  }


}

close(DISTANT_FILE);
close(MIDDLE_FILE);
close(NEAR_FILE);
close(NOZ_FILE);



exit 0;
