#!/usr/bin/perl 
###############################################################################
# PERL script to make a new catalogue which only contains spectroscopic 
# observations for regions above a specified completeness (i.e. fraction of 
# objects with spectra that have been identified).  Input is a full 2QZ 
# catalogue file.  The completeness is based on the fraction of observed 
# objects identified in each sector.  
#
# Written by Scott Croom
#
# 05/03/03 SMC (v1.1): Allow upper and lower limit on sector completeness.
#
################################################################################
#                                                                              #
#          LICENCE                                                             #
#                                                                              #
#          This program is free software; you can redistribute it and/or       #
#          modify it under the terms of the GNU General Public License         #
#          as published by the Free Software Foundation; either version 2      #
#          of the License, or (at your option) any later version.              #
#                                                                              #
#          This program is distributed in the hope that it will be useful,     #
#          but WITHOUT ANY WARRANTY; without even the implied warranty of      #
#          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       #
#          GNU General Public License for more details.                        #
#                                                                              #
#          You should have received a copy of the GNU General Public License   #
#          along with this program; if not, write to the Free Software         #
#          Foundation, Inc., 59 Temple Place - Suite 330,                      #
#          Boston, MA  02111-1307, USA.                                        #
#                                                                              #
################################################################################
#
# Pragmas:
use diagnostics;
# 
# Modules:
use TWOQZ_CAT;
#
# Version control:
$version=1.1;
printf ("make_comp_cat.pl version %3.1f\n\n",$version);
#
# get command line arguments:
if (scalar(@ARGV) == 4) {
    $catfile = $ARGV[0];
    $lower_completeness_limit = $ARGV[1]; 
    $upper_completeness_limit = $ARGV[2]; 
    $allflag = $ARGV[3];
    $outfile = "new.cat";
}
elsif (scalar(@ARGV) == 5) {
    $catfile = $ARGV[0];
    $lower_completeness_limit = $ARGV[1]; 
    $upper_completeness_limit = $ARGV[2]; 
    $allflag = $ARGV[3];
    $outfile = $ARGV[4];
}
else {
    print "Usage: make_comp_cat.pl <cat_file> <lower> <upper> all/notall [<output_file>]\n\n";
    print "Upper and lower completeness values should be specified as percentages\n";
    print "specify all/notall:\n";
    print "all    - Output all objects including unobserved objects.\n";
    print "         In this case all objects will be in the output catalogue\n";
    print "         file but only the ones with complete sectors will have\n";
    print "         redshifts and IDs.\n";
    print "notall - Only output objects in complete sectors\n";
    print "output file is optional\n";
    exit;
}
#
# specify limitign completeness:
$qlim=20; # only use quality 1 IDs.
#
# define completeness to be fractional rather than percentage:
$lower_completeness_limit = $lower_completeness_limit/100.0; 
$upper_completeness_limit = $upper_completeness_limit/100.0; 
#
# Read catalogue file and find the completeness in each sector:
$nlines=0;
open(CAT, $catfile);
while ($line = <CAT>) {
    chomp($line); # get rid of trailing new line.
    rline($line); # read catalogue entry
    $nobjects{$sector}++;
    if ($nobs > 0) {
	$nobserved{$sector}++;
	if ($zq1 < $qlim) {
	    $nidentified{$sector}++;
	}
    }
    $nlines++;
}
close(CAT);
print "$nlines lines read from $catfile\n";
#
# Next, for each sector find the completeness:
foreach $key (keys %nobjects) {
    $completeness{$key}=0.0;
    if (exists $nobserved{$key}) {
	if (exists $nidentified{$key}) {
	    $completeness{$key}=$nidentified{$key}/$nobserved{$key};
	}
    }
}
#
# lastly read in the catalogue file again and write those objects which
# are in good sectors to a new catalogue file:
open(CAT, $catfile);
open(OUT, "> ".$outfile);
$nobj=0;
$nlines=0;
while ($line = <CAT>) {
    chomp($line); # get rid of trailing new line.
    rline($line); # read catalogue entry
    $output=0;
# if the sector completeness is less than the input limit, empty all the
# observational entries for that object:
    if ($completeness{$sector} < $lower_completeness_limit || $completeness{$sector} > $upper_completeness_limit) {
	$nobs=0;
	$z1=0;
	$zq1=0;
	$id1="-";
	$date1="-";
	$fobs1=0;
	$fibre1=0;
	$sn1=0;
	$z2=0;
	$zq2=0;
	$id2="-";
	$date2="-";
	$fobs2=0;
	$fibre2=0;
	$sn2=0;
    }
    else {
	if ($nobs > 0) {
	    $nobj++;
	    $output=1;
	}
    }
# write the new data to the output file:
    $line=wline();
    if ($allflag eq "all") {$output=1;}
    if ($output) {
	print OUT "$line\n"; 
	$nlines++;
    }
}
close(CAT);
close(OUT);
print "$nlines lines written to $outfile, including $nobj object IDs\n";
$lower_completeness_limit = $lower_completeness_limit*100.0; 
$upper_completeness_limit = $upper_completeness_limit*100.0; 
print "in regions with completeness $lower_completeness_limit\% to $upper_completeness_limit\%\n";

###############################################################################
