#!/usr/bin/perl

# This script selects stars from the MIPS calibrator list based on a simple
# set of rules that includes filter choice, flux range, and date range.  The
# calibrator must have a flux between the values specified and be visible
# during the entire date range specified.


use Time::Local;


########### set rules here ####################################
#
# Useful flux ranges for calibrators (includes confusion noise)
# BAND   |-------bright stds.--------|   |---------full range--------|
# (um)   20-SIGMA in 3S   SATUR. in 3S   3-SIGMA in 10S   SATUR. in 1S
#  24      0.01 Jy          1.4 Jy         7e-4 Jy          5.6 Jy
#  70      0.07 Jy          1.3 Jy         6e-3 Jy          5.3 Jy
# 160      0.20 Jy          2.0 Jy         3e-2 Jy          8.0 Jy
#
$flux1 = 7e-4;		# minimum flux, in Jy
$flux2 = 5.6;		# maximum flux, in Jy
$filter = 0;	# 0 = 24um, 1 = 70um, 2 = 160um
# timelocal fields are sec, min, hr, day, month (0-offset), year
$date1 = timelocal (0, 0, 0, 17, 10, 2003);	# start of required window
$date2 = timelocal (0, 0, 0, 12, 11, 2003);	# end of required window
###############################################################


# set parameters

$file = "mips-calibrators.txt";
@filter_name = (24, 70, 160);


# print output header
printf "%10s%9s%11s%10s%5s%6s%7s%22s\n", "ID", "type", "RA",
   "DEC", "Band", "flux", "bkgd.", "visibility";
printf "%10s%9s%11s%10s%5s%6s%7s%22s\n", "<br>", "<br>", "(2000)",
   "(2000)", "(um)", "(mJy)", "MJy/sr", "<br>";


# open file and skip 2-line header

open (FILE, $file) or die "Cannot open $file: $!";
<FILE>; <FILE>;


# read file and print out results as we go

while (<FILE>) {
   ($hdnum, $name, $type, $ra, $dec, $v, $k, $flux[0], $flux[1], $flux[2],
      $bkgd[0], $bkgd[1], $bkgd[2], $visibility) = split;
   ($temp1, $temp2) = split ("-", $visibility);
   ($year, $month, $day) = split ("/", $temp1);
   $day1 = timelocal (0, 0, 0, $day, $month-1, $year);
   ($year, $month, $day) = split ("/", $temp2);
   $day2 = timelocal (0, 0, 0, $day, $month-1, $year);
   if ($day1 > $date1) {next;}
   if ($day2 < $date2) {next;}
   if ($flux[$filter] < $flux1) {next;}
   if ($flux[$filter] > $flux2) {next;}
   printf "%10s%9s%11s%10s%5s%6.0f%7.1f%22s\n", $hdnum, $type, $ra, $dec,
      $filter_name[$filter], $flux[$filter]*1e3, $bkgd[$filter], $visibility;
}

close (FILE);
