#!/bin/sh # nightly_means_stdev # RTH ??/??/06? # Batch processes all the photometry output, and calculates std. dev. since the # ghetto photometry program I was using didn't do it for me. Also does the # nightly means # RTH 11/01/07 Version 2 # I'm trapped in the office and redoing it as a real world useful example # Reads in original comma separated data, output from photometry reductions datadir="./Orig_data" for i in `ls $datadir/2005_*I*` # Builds our list of input files do # A note on regular expressions - they frequently look like you have # crazy ninja-like abilities because of all the slashes, but they can be # broken down. Take this one for example: sed 's/.*\///g'` # sed 's/.*\///g' == sed 's/ .*\/ //g # Substitute # . == match ANY character # * == zero or more of the preceding thing, even wildcards (such as .) # \/ == backslash "deactivates" characters, interpreting them literally # must be done because we use the forward slash to separate things # // == replace the previous thing with nothing # g == do it "globally" for every occurance sed finds # In the final sed statement, /_//2 is used to delete 2nd occurance of _ outp=`echo $i | sed 's/.*\///g'` echo " Original = $i" echo "sed 's/.*\///g' = $outp" outp=`echo $outp | sed 's/.csv/.dat/g'` echo "sed 's/.csv/.dat/g' = $outp" outp=`echo $outp | sed 's/_//2'` echo "sed 's/_//2' = $outp" awk 'BEGIN {FS=","} {if(NR!=1) print $1-2450000,$2}' $i > $outp done # Remove vestigial files from previous runs, if they exist. # This is a low-down cheap hack because I'm lazy, and forget the # switch for 'test' and I hate error messages in my output. echo "Laziness is next to Godliness" > G_means.txt rm ?_means.txt # Please don't yell at me or tell Jon I'm averaging these data like this, # I know I should average each 3 measurements in a set & get stddev, then # add the errors properly. But its an example! for j in `ls *_I*.dat` do # NR is Number of Records, i.e. how many lines were read in. # Again note that you basically write C code inside of {}, which is nice awk '{ jdtot+=$1; magtot+=$2; sqsum+=$2^2; } END { jdavg = jdtot/NR; magavg=magtot/NR; sumsq=magtot*magtot; W=sumsq/NR; Q=(sqsum-W)/(NR-1); stddev=sqrt(Q); printf("%f %f %f\n",jdavg,magavg,stddev); }' $j >> I_means.txt # >> appends to a file, creating it if it doesn't exist. If we didn't # delete ?_means.txt at the beginning, the file would get messy quick. done