concatFiles.pl

#!/usr/bin/perl

# written by Nan Rosenbloom
# July 2010


$fyr = 899;
$lyr = 903;
$nyrs = $lyr - $fyr + 1;
$caseid = "b40.mh6ka.1deg.002.cpl.ha2x3h";
$outid = "b40mh6ka1d002_3h";
$indir = "/hf_input/dailyFiles/";
$outdir = "/hf_input/monthlyFiles/";

@monList  = ("-01","-02","-03","-04","-05","-06","-07","-08","-09","-10","-11","-12");
@ndays    = (  31,   28,   31,   30,   31,   30,   31,   31,   30,   31,   30,   31);

$cy=$fyr;

while ($cy <= $lyr) {
  $ctr=0;               # month counter
  foreach $m (@monList) {
        $iYr = $cy;
        $use_prnt  = printYear($iYr);

        $ndays = $ndays[$ctr];
        $ifile   = $indir.$caseid.".".$use_prnt.$m."-01.nc";
        $ofile   = $outdir.$outid.".".$use_prnt.$m.".nc";

        print("$ifile\n");
        print("$ofile\n");

         print("year = $use_prnt; month = $m; $ndays = $ndays\n");
         print("ncrcat \-O \-n $ndays,2,1 $ifile $ofile");
        system("ncrcat \-O \-n $ndays,2,1 $ifile $ofile");
        $ctr++;
  }
  $cy++;
}


sub printYear
{
  local($iyr) = @_;
  if    ($iyr <    10)                { $y = "000".$iyr; }
  elsif ($iyr >=   10 && $iyr <  100) { $y =  "00".$iyr; }
  elsif ($iyr >=  100 && $iyr < 1000) { $y =   "0".$iyr; }
  elsif ($iyr >= 1000)                { $y =       $iyr; }
  return($y);
}