#!/usr/bin/perl -w # name of script: eddy4.pl # Time-stamp: ($a, $intv) = @ARGV; open (INFILE, $a) or die "Could not open the file: $a ($!)\n"; $out = "covresults_" . $intv . ".txt"; open (OUTFILE, ">$out") or die "Could not open the outfile. ($!)\n"; $intv = $intv * 60 * 4 ; # $intv is in minutes $cntr=1; print OUTFILE "hr, temp, Cov\n"; while (defined ($text = )) { chomp $text; (undef,undef,$HRMN,undef,undef,undef,$uzIn,undef,undef,undef,$CO2_in, undef,undef,$temp,undef) = split(/[,\s+]/,$text); unless($CO2_in == 99999 || $CO2_in == -99999 || $uzIn == 99999 || $uzIn == -99999 || $temp == 99999 || $temp == -99999){ push(@CO2, $CO2_in); push(@wind, $uzIn); push(@temp, $temp); if(!($cntr % $intv)){ $co2mean = &calcMean(@CO2); $windmean = &calcMean(@wind); $tempmean = &calcMean(@temp); $thecov = &cov(\@CO2, $co2mean, \@wind, $windmean); print OUTFILE "$HRMN,$tempmean,$thecov\n"; @CO2 = (); @temp = (); @wind = (); } $cntr++; } } close (INFILE) || die "Could not close the file: $a"; close (OUTFILE); sub calcMean { my $tmp = 0; # @_ is the passed array; foreach my $i (@_) { $tmp = $tmp + $i; } return $tmp / @_ ; # @_ (here, a list in a scalar context) is equal to ($#_ + 1) } sub cov { # call as &cov(\@CO2, $co2mean, \@wind, $windmean) my($xref, $xmean, $yref, $ymean) = @_; my $result = 0; # for (my $i = 0; $i < @$xref; $i++){ foreach my $i (0..(@$xref-1)){ $result += ($xref->[$i] - $xmean) * ($yref->[$i] - $ymean) } return $result / (@$xref - 1); }