Kirk Wythers wrote:
Sorry for the "not sure where to even begin" nature of this email, but I
am stuck. I am trying to put together a aggregating script that takes
daily climate data and produces monthly averages. For example, the input
file has the form:
year month doy tmax tmin par precip NH4 NO3 O3
CO2 V1 V2 V3 V4
1949 1 1 0 -2.78 440.16 0 0 0 0 0 0
0 0 0
[ SNIP ]
1949 2 36 -2.78 -12.22 489.31 0 0 0 0 0
0 0 0 0
I need to step through 50 years of data, returning a single line of
monthly averages. SOmething like:
year month doy tmax tmin par precip NH4
NO3 O3 CO2 V1 V2 V3 V4
1949 1 15 5.45 -3.1 346 1.3
0 0 0 0 0 0 0 0
1949 2 61 4.6 1.2 421 2.1
0 0 0 0 0 0 0 0
$ echo "year month doy tmax tmin par precip NH4 NO3 O3
CO2 V1 V2 V3 V4
1949 1 1 0 -2.78 440.16 0 0 0 0 0 0 0 0
0
1949 1 2 0.56 -6.67 501.21 0 0 0 0 0 0 0
0 0
1949 1 3 -1.11 -9.44 374.2 0 0 0 0 0 0 0
0 0
1949 1 4 1.67 -3.89 180.04 0.03 0 0 0 0 0
0 0 0
1949 1 5 3.89 -11.11 225.07 0.33 0 0 0 0 0
0 0 0
1949 1 6 3.33 -11.67 190.57 0 0 0 0 0 0 0
0 0
1949 1 7 8.89 1.67 358.3 0 0 0 0 0 0 0
0 0
1949 1 8 2.78 -1.11 447.57 0 0 0 0 0 0 0
0 0
1949 1 9 1.11 -6.67 185.17 0 0 0 0 0 0 0
0 0
1949 1 10 -6.11 -8.33 402.95 0.03 0 0 0 0 0
0 0 0
1949 1 11 -2.78 -7.78 438.61 0 0 0 0 0 0 0
0 0
1949 1 12 -2.22 -6.67 197.34 0 0 0 0 0 0 0
0 0
1949 1 13 0 -3.89 194.77 0 0 0 0 0 0 0
0 0
1949 1 14 -1.11 -3.89 428.67 0 0 0 0 0 0 0
0 0
1949 1 15 5.56 -1.11 196.74 0.61 0 0 0 0 0
0 0 0
1949 1 16 5.56 -6.67 198.01 0.05 0 0 0 0 0
0 0 0
1949 1 17 -5.56 -7.78 403.66 0.1 0 0 0 0 0
0 0 0
1949 1 18 -3.89 -11.67 205.6 0.3 0 0 0 0 0
0 0 0
1949 1 19 -3.89 -16.11 217.64 0.33 0 0 0 0 0
0 0 0
1949 1 20 -10 -16.67 477.62 0.03 0 0 0 0 0
0 0 0
1949 1 21 -1.67 -10 213.43 0.13 0 0 0 0 0
0 0 0
1949 1 22 -6.11 -10.56 514.38 0.03 0 0 0 0 0
0 0 0
1949 1 23 -3.33 -8.33 221.18 1.14 0 0 0 0 0
0 0 0
1949 1 24 -1.11 -6.67 222.15 0.33 0 0 0 0 0
0 0 0
1949 1 25 -6.67 -11.11 366.21 0.08 0 0 0 0 0
0 0 0
1949 1 26 -3.89 -11.11 353.45 0.1 0 0 0 0 0
0 0 0
1949 1 27 -1.67 -6.67 230.49 0 0 0 0 0 0 0
0 0
1949 1 28 -2.78 -14.44 369.51 1.73 0 0 0 0 0
0 0 0
1949 1 29 -13.33 -19.44 324.27 0 0 0 0 0 0
0 0 0
1949 1 30 -6.11 -18.33 479.95 0 0 0 0 0 0
0 0 0
1949 1 31 -6.11 -16.67 455.25 0.03 0 0 0 0 0
0 0 0
1949 2 32 -6.67 -15 508.9 0 0 0 0 0 0 0
0 0
1949 2 33 -10.56 -18.89 709.76 0 0 0 0 0 0
0 0 0
1949 2 34 -6.11 -13.89 253.29 0.05 0 0 0 0 0
0 0 0
1949 2 35 -3.33 -7.22 411 0.28 0 0 0 0 0 0
0 0
1949 2 36 -2.78 -12.22 489.31 0 0 0 0 0 0
0 0 0" | perl -ne'
next unless /^\d/; # skip headers
my ( $year, $mon, $day, @fields ) = split;
my $key = sprintf "%04d%02d", $year, $mon;
for ( 0 .. $#fields ) {
$data{ $key }{ total }[ $_ ] += $fields[ $_ ];
}
$data{ $key }{ count }++;
END {
for my $key ( sort keys %data ) {
print join( "\t", unpack( "a4a2", $key ), map $_ ? sprintf( "%.2f", $_ /
$data{ $key }{ count } ) : 0, @{ $data{ $key }{ total } } ), "\n";
}
}
'
1949 01 -1.81 -8.89 323.04 0.17 0 0 0 0
0 0 0 0
1949 02 -5.89 -13.44 474.45 0.07 0 0 0 0
0 0 0 0
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/