Nephstat

From TROPOS Wiki

Jump to: navigation, search
#!/usr/bin/perl
# Name: nephstat
# Desc: calculate performance statistics from a raw neph data file
# Call: nephstat infile [ > outfile ]
# Uses: data file written by TSI neph software
#  OUT: standard output
#  Rev: 970529 JAO translate awk version to perl
#       070129 JAO move background check math to after filtered air results
#                  are printed out, in case on background checks were done
#       070311 JAO allow specification of input files/types on command line,
#                  to allow processing cpd1-format files (na_, nc_, h__)

&DefineGlobals;
die $syntax unless $#ARGV >= 0;

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9\[\]]+=)(.*)/ && shift;
			# process any FOO=bar switches

if (-e $nc) {			# process nc_ file
  print "processing $nc\n" if $debug;
  @nc_avg = split( ",", `ave.pl all=1 noflag=1 $nc`);
  @nc_std = split( ",", `ave.pl all=1 noflag=1 stdev=1 $nc`);
  @aBbkg = @nc_avg[3..8];
  @sBbkg = @nc_std[3..8];
}

if (-e $h_) {			# process h__ file
  print "processing $h_\n" if $debug;
  @h__avg = split( ",", `ave.pl all=1 noflag=1 $h_`);
  @h__std = split( ",", `ave.pl all=1 noflag=1 stdev=1 $h_`);
  @aBair = @h__avg[7..12];
  @sBair = @h__std[7..12];
  &PrintResults( 1, $fmt_air, @aBair, @sBair);
  &PrintResults( 0, $fmt_bkg, @aBbkg, @sBbkg);
} elsif (-e $na) {		# process na_ file
  print "processing $na\n" if $debug;
  @na_avg = split( ",", `ave.pl all=1 noflag=1 $na`);
  @na_std = split( ",", `ave.pl all=1 noflag=1 stdev=1 $na`);
  @aBair = @na_avg[4..9];
  @sBair = @na_std[4..9];
  &PrintResults( 1, $fmt_air, @aBair, @sBair);
  &PrintResults( 0, $fmt_bkg, @aBbkg, @sBbkg);
} else {				# process input data written by TSI neph software
  print "processing TSI data file\n" if $debug;
  &ClearSums;
  while (<>) {
    tr/\n\r//d;			# strip record separator
    @F = split(",");	# split input line on commas
    if (/^D,N/) {		# normal data records
      for ($i = 0; $i < 6; $i++) {
        $aBair[$i] += $F[$i + 3];
        $sBair[$i] +=  $F[$i + 3] * $F[$i + 3];
      }
      $nAir += 1;
    }
    if (/^Z/) {			# zero records
      for ($i = 0; $i < 3; $i++) {
      	$x = $F[$i+1] - $F[$i+7];
        $aBbkg[$i] += $x;
        $sBbkg[$i] +=  $x * $x;
      }
      for ($i = 3; $i < 6; $i++) {
      	$x = $F[$i+1] - $F[$i+4]/2;
        $aBbkg[$i] += $x;
        $sBbkg[$i] +=  $x * $x;
      }
      $nBkg += 1;
    }
  }	# end while

  # print out statistics for filtered air
  if ($nAir) {
    grep( $_ /= $nAir, @aBair );
    for ($i=0; $i<6; $i++ ) {
      $sBair[$i] = $sBair[$i] / $nAir - $aBair[$i]**2;
      $sBair[$i] = ($sBair[$i]>0) ? sqrt( $sBair[$i] ) : 0;
    }
  }
  &PrintResults( 0, $fmt_air, @aBair, @sBair);
                       
  # print out statistics for background checks
  if ($nBkg) {
    grep( $_ /= $nBkg, @aBbkg );
    for ($i=0; $i<6; $i++ ) {
      $sBbkg[$i] = $sBbkg[$i] / $nBkg - $aBbkg[$i]**2;
      $sBbkg[$i] = ($sBbkg[$i]>0) ? sqrt( $sBbkg[$i] ) : 0;
    }
  }
  &PrintResults( 0, $fmt_bkg, @aBbkg, @sBbkg);

} # end if
exit 0;

sub ClearSums {
  @aBair = (0) x 6;
  @sBair = (0) x 6;
  @aBbkg = (0) x 6;
  @sBbkg = (0) x 6;
  $nAir = 0;
  $nBkg = 0;
};

sub PrintResults {
  my ($scale, $fmt, @val) = @_;
  $scale = 1E6 unless $scale==1;		# default is data in inverse meters
  grep( $_*=$scale, @val );				# scale input data
  printf( $fmt, @val );	
};

sub DefineGlobals {
  $fmt_air = "Total\tFiltered Air\tMean\tBlue\t%8.3f\t1/Mm\n" .
           "Total\tFiltered Air\tMean\tGreen\t%8.3f\t1/Mm\n" .
           "Total\tFiltered Air\tMean\tRed\t%8.3f\t1/Mm\n" .
           "Back\tFiltered Air\tMean\tBlue\t%8.3f\t1/Mm\n" .
           "Back\tFiltered Air\tMean\tGreen\t%8.3f\t1/Mm\n" .
           "Back\tFiltered Air\tMean\tRed\t%8.3f\t1/Mm\n" .
           "Total\tFiltered Air\tStdDev\tBlue\t%8.3f\t1/Mm\n" .
           "Total\tFiltered Air\tStdDev\tGreen\t%8.3f\t1/Mm\n" .
           "Total\tFiltered Air\tStdDev\tRed\t%8.3f\t1/Mm\n" .
           "Back\tFiltered Air\tStdDev\tBlue\t%8.3f\t1/Mm\n" .
           "Back\tFiltered Air\tStdDev\tGreen\t%8.3f\t1/Mm\n" .
           "Back\tFiltered Air\tStdDev\tRed\t%8.3f\t1/Mm\n";	
  $fmt_bkg = "Total\tBackground\tMean\tBlue\t%8.3f\t1/Mm\n" .
           "Total\tBackground\tMean\tGreen\t%8.3f\t1/Mm\n" .
           "Total\tBackground\tMean\tRed\t%8.3f\t1/Mm\n" .
           "Back\tBackground\tMean\tBlue\t%8.3f\t1/Mm\n" .
           "Back\tBackground\tMean\tGreen\t%8.3f\t1/Mm\n" .
           "Back\tBackground\tMean\tRed\t%8.3f\t1/Mm\n" .
           "Total\tBackground\tStdDev\tBlue\t%8.3f\t1/Mm\n" .
           "Total\tBackground\tStdDev\tGreen\t%8.3f\t1/Mm\n" .
           "Total\tBackground\tStdDev\tRed\t%8.3f\t1/Mm\n" .
           "Back\tBackground\tStdDev\tBlue\t%8.3f\t1/Mm\n" .
           "Back\tBackground\tStdDev\tGreen\t%8.3f\t1/Mm\n" .
           "Back\tBackground\tStdDev\tRed\t%8.3f\t1/Mm\n";
	$syntax = 
	  "Syntax: nephstat [nc=file] [na=file | h_=file | filelist]\n" .
	  " Input: na_-file, h__-file, file list, or standard input\n" .
	  "        nc       - name of file with nc_-format records\n" .
	  "        na       - name of file with na_-format records\n" .
	  "        h_      - name of file with h__-format records\n" .
	  "        filelist - list of files in TSI-format to process\n" .
	  "                   Uses standard input if filelist is omitted\n" .
	  "Output: standard output\n" .
	  " Notes: \n" .
	  "   Ver: $version\n";
};
Personal tools
Create a book