#!/bin/sh # filename: try_hour_avg # input: 2 columns: decimal.day, H2O2 air # output: averages of the above columns for every hour # doesn't report hourly average for missing hours # ddy H2O2 #159.538194 0.58 #159.565972 0.58 #159.593750 0.94 #159.621528 0.49 #159.649306 0.70 #160.461806 0.60 #160.496528 0.63 #160.524306 0.68 #160.552083 0.47 #160.579861 0.66 #160.607639 0.58 #160.635417 0.42 #160.670139 0.55 # first do some command-line checking if [ $# -ne 1 ]; then printf "usage: try_hour_avg file\n"; exit; fi if [ ! -f $1 ]; then printf "file: $1 does not exist\n"; exit; fi # prompt for output filename printf "\nPlease enter output filename:\n" read outname cat $1 | nawk ' BEGIN{ printf("%-12s %-12s %-22s\n", "Day_of_year", "H2O2 [ppbv]", "Number of data points") } { day[NR] = $1; value[NR] = $2 } # get hour and values, save in arrays END{ fh = 1/24 # fh = fraction hour = 0.04167 i = 1 frac = day[i] % 1 # get only the fractional part, e.g. 0.538194 inc = int(frac / fh) sh = int(day[i]) + (inc * fh) # sh = start hour while ( i < NR ) { while ( day[i] < (sh + fh) && i <= NR) { sum += value[i] count++ i++ } printf("%-12f %-12f %-22d\n", sh, sum/count, count) sum = 0 count = 0 frac = day[i] % 1 inc = int(frac / fh) sh = int(day[i]) + (inc * fh) } }' > $outname # end END printf "\noutput stored as file: $outname\n"