#!/usr/bin/perl -w

use strict;

#  Program to extract daily Min/Max Temperature and Precipitation
#  from GHCN data data sets for a year padded with one month before
#  and after.  Reduces the global data set to only stations in Colorado,
#  Kansas and Nebraska and McCook WBAN station
#
#  Willem A. Schreuder
#  October 3, 2012

(@ARGV==1) || die "Usage: $0 <year>\n";
my $yr = shift @ARGV;
my $yr0 = $yr-1;
my $yr1 = $yr+1;
my $prev = ','.$yr0.'12';
my $next = ','.$yr1.'01';

my $stn = '^USC0005|^USC0014|^USC0025|^USW00094040';

unlink 'GHCN';

#  December of the previous year
print `zgrep -E 'PRCP|TMIN|TMAX' /pm/raid7/t/NCDC/GHCN/by_year/$yr0.csv.gz | grep -E '$stn' | grep $prev >> GHCN`;
#  Current year
print `zgrep -E 'PRCP|TMIN|TMAX' /pm/raid7/t/NCDC/GHCN/by_year/$yr.csv.gz | grep -E '$stn' >> GHCN`;
#  January of the next year
print `zgrep -E 'PRCP|TMIN|TMAX' /pm/raid7/t/NCDC/GHCN/by_year/$yr1.csv.gz | grep -E '$stn' | grep $next >> GHCN`;
