#!/usr/bin/perl -w

# This script combines individual daily output files from condense_hkt_day.pl
# into yearly files.

# Norman Kuring		 7-Dec-2011	Initial development

use strict;
use lib '/web/aquarius/scripts';
use ReadTelemMeta qw(get_param_specs);
use Utility qw(read_dir isleap);

my (%pindex, %ctg);

foreach my $t ( qw( sp aq ) ){

  # Get parameter information as a hash of arrays.
  #                        [    0   ,     1      ,  2  , 3, 4, 5, 6,    7      ]
  # $pindex{TYPE}{PARAM_ID}[GROUP_ID,GROUP_COLUMN,UNITS,RL,YL,YH,RH,DESCRIPTION]
  $pindex{$t} = { get_param_specs($t) };

  my @params = sort keys %{ $pindex{$t} };

  for(my $i=0; $i<@params; $i++){
    push @{ $ctg{$t}{ $pindex{$t}{ $params[$i] }[0] } }, $params[$i];
  }
}

my $dir = '/web/aquarius/metadata/condensed_hkt';

my @years = read_dir($dir,'^\d{4}$');

foreach my $y (@years){

  my $daysinyear = isleap($y) ? 366 : 365;

  my @days = ("001" .. "$daysinyear");

  foreach my $t ( qw( sp aq ) ){

    my @ctg = keys %{ $ctg{$t} };

    my $ty = uc $t;

    foreach my $g ( @ctg ){
      my $out = "$dir/$y/Q$y.day.${ty}_$g.txt";
      open O,'>',"$out.$^T.$$" or die "Could not create $out.$^T.$$ ($!)";
      my @in = map "$dir/$y/$_/Q$y$_.day.${ty}_$g.txt",@days;
      for(my $i=0;$i<@in;$i++){
        if(open F,'<',$in[$i]){
          my @recs = <F>;
          close F;
          my $numrecs = @recs;
          if($numrecs == 5){
            print O @recs;
          }
          else{
            warn "Unexpected number of records ($numrecs) in $in[$i]\n";
            print O "N\nN\nN\nN\nN\n";
          }
        }
        else{
          print O "N\nN\nN\nN\nN\n";
        }
      }
      close O;
      rename "$out.$^T.$$",$out;
    }
  }
}
