On 8/23/07, Kirk Wythers <[EMAIL PROTECTED]> 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
> 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
>
> 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
>
> etc....
>
> So far I can not seem to get past reading in the data and printing it
> out with something like:
>
> #! /usr/bin/perl -w
> use strict;
>
> $, = ' '; # set output field separator
> $\ = "\n"; # set output record separator
>
> my %days = map +($_, 1),
>    qw/ 15 46 76 107 137 168 229 259 290 321 351 /;
>
> while (<>) {
>    my @data = split;
>    print @data;
> }
>
>
> Thanks in advance
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>

assuming your data is sorted this should work

#!/usr/bin/perl

use strict;
use warnings;

my @key    = qw<year month>;
my @head   = (@key, 'doy');
my @sum    = qw<tmax tmin par precip NH4 NO3 O3 CO2 V1 V2 V3 V4>;
my @fields = (@head, @sum);
my $fmt    = "%4d %4d %10.2f %8.2f %8.2f %5.2f %4d %7d %7d %7d %7d %7d
%7d %7d\n";

my $old;
my $n;
my %store;

my $average = sub {
        $store{$_} /= $n for @sum;
        printf $fmt, @[EMAIL PROTECTED], @sum};
        $n = 0;
        @[EMAIL PROTECTED] = ('') x @head;
        @[EMAIL PROTECTED]  = (0)  x @sum;
};
while (<DATA>) {
        chomp;
        my %rec;
        @[EMAIL PROTECTED] = split;
        my $cur       = "@[EMAIL PROTECTED]";
        $average->() if defined $old and $old ne $cur;
        $old          = $cur;
        @[EMAIL PROTECTED] = @[EMAIL PROTECTED];
        $store{$_}   += $rec{$_} for @sum;
        $n++;
}
$average->();

__DATA__
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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to