#!/usr/bin/perl -w
#
#  PURPOSE: THE PERL SCRIPT lmp_scp_dup.pl ELIMINATES DUPLICATE
#  OBSERVATIONS FROM SATELLITE CLOUD PRODUCT (SCP) DATA.  THIS 
#  SCRIPT IS CALLED IN SUBROUTINE SCPEDIT.F OF CODE LMP_SCPPREP.F.
#
########################################################################  

$iflag=0;
$low_date=0;
$high_date=0;

########  OPEN THE OUTPUT FILE
open (OUTFILE, "> prepack.SCP.txt") || die "Error opening output file: $!\n";

HEADER:  while (<>) {
   chomp;
   $iflag++;
   if ($iflag == 1){
      $low_date =  $_;
      print "HEADER: low date = $low_date\n";
   }
   if ($iflag == 2){
      $high_date =  $_;
      print "HEADER: high date = $high_date\n";
   }
   if ($iflag == 3){
      print OUTFILE "$_\n";
      last HEADER;
   }
}
#######  STOP IF INCORRECT INPUTS  ############
if ($low_date > 320000 or $high_date > 320000 ){
   print "$low_date and $high_date ERROR: not read properly\n";
   close OUTFILE;
   exit(5);
}
#######################################################

PRINT: while (<>) {
   chomp;
   if ($_ eq 'ZZZZ' ) {
      print OUTFILE "$_\n";
      close OUTFILE;
      exit(0);
   }
   $everything = $_;
   my $middle_date = substr($everything, 6, 6);

    if ($middle_date <= $high_date && $middle_date >= $low_date){
      print OUTFILE "$everything\n";
    }

###print OUTFILE "$middle_date\n";

} 

###close OUTFILE;
###########close INFILE;
###exit(0);

