#!/usr/bin/env ruby ### wxbaddata --- Utility checking for bad data in Open3600 files. ## Copyright 2006 by Dave Pearson ## $Revision: 1.2 $ ## ## wxbaddata is free software distributed under the terms of the GNU General ## Public Licence, version 2. For details see the file COPYING. # We're going to use long options. require "getoptlong" # Set the default parameters. $params = { :field => 18, :help => false, :licence => false, :maxdiff => 5, } # Print the help screen. def printHelp print "wxbaddata v#{/(\d+\.\d+)/.match( '$Revision: 1.2 $' )[ 1 ]} Copyright 2006 by Dave Pearson http://www.davep.org/ Supported command line options: -f --field Specify which field to average. (default is 18 -- the pressure field). -m --maxdiff Specify the maximum difference allowed. (default is 5). -h --help Display this help. " end # Print the licence. def printLicence print "wxbaddata - Utility checking for bad data in Open3600 files. Copyright (C) 2006 Dave Pearson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. " end # Get the arguments from the command line. begin GetoptLong.new().set_options( [ "--field", "-f", GetoptLong::REQUIRED_ARGUMENT ], [ "--maxdiff", "-m", GetoptLong::REQUIRED_ARGUMENT ], [ "--help", "-h", GetoptLong::NO_ARGUMENT ] ).each {|name, value| $params[ name.gsub( /^--/, "" ).intern ] = value } rescue GetoptLong::Error printHelp() exit 1 end # The need for help overrides everything else if $params[ :help ] printHelp() elsif $params[ :licence ] # As does the need for the legal mumbojumbo printLicence() else # Ensure the numeric parameters are numeric. $params[ :maxdiff ] = $params[ :maxdiff ].to_f $params[ :field ] = $params[ :field ].to_i # For each file given on the command line (or from stdin if no files were # given)... ( ARGV.length == 0 ? ( "-" ) : ARGV ).each do |file| # Init the previous value. prev_value = -1 # Process the file. Note that "-" means process stdin. ( file == "-" ? $stdin : File.open( file ) ).each do |line| # Split the line into fields. fields = line.chomp.split( " " ) # Does the field we're after exist? if fields.length >= $params[ :field ] # Get the value. value = fields[ $params[ :field ] - 1 ].to_f # Not got previous value? if prev_value == -1 then prev_value = value end # Does it differ too much? if ( value - prev_value ).abs() > $params[ :maxdiff ] puts line else prev_value = value end end end end end # All's well that ends well. exit 0 ### wxbaddata ends here