#!/usr/bin/env python # # Script to modify a c.in file # Chris LeBlanc, Dalhousie University, 2004 # # Dependencies: Rayinvr, c2v, Python >= 2.3, Bash or similar shell # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU program 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 # program General Public License for more details. # # You should have received a copy of the GNU program General Public # License along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys from optparse import OptionParser usage = """ Script for manipulation of Rayinvr c.in format velocity files (not v.in format, use c2v and v2c to convert between the two). This script is useful for moving the depth of a layer, or setting all the depth nodes to be used for inversion. Not currently able to modify the velocity nodes (because these are easy enough to modify by hand). This example adds 1 km to each depth node in layer 4, but leaves the last column as it was originally. The output is written to c_mod.in by default: c_modify.py --layer 4 --shift 1 Or as short options (with input and output set): c_modify.py -l 4 -s 1 -i c.in -o c_mod.in Or to set all nodes in layer 4 for inversion: c_modify.py --layer 4 --shift 1 --invert Or to set all nodes in layer 4 to have no shift and not be inverted (regarless of whether input is set to be inverted or not): c_modify.py --layer 4 --no-invert """ def getCmdLineArgs(): parser = OptionParser() parser.add_option("-i", "--input", dest="inFile", \ help="Specify input file in the Rayinvr 'c.in' format [c.in].", \ type="string", metavar="INPUT", default="c.in") parser.add_option("-o", "--output", dest="outFile", \ help="Specify output filename (in c.in format) [c_mod.in].", \ type="string", metavar="OUTPUT", default="c_mod.in") parser.add_option("-l", "--layer", dest="layer", \ help="Layer number to modify, mandatory", \ type="int", metavar="LAYER", default=0) parser.add_option("-s", "--shift", dest="shift", \ help="Shift to apply to depth values of specified layer.", \ type="float", metavar="SHIFT", default=0.0) parser.add_option("--invert", action="store_true", dest="invert", \ help="Set all depth nodes to be inverted (number 1 " + \ "in the last column). Default is to leave it as is.") parser.add_option("--no-invert", action="store_true", dest="noInvert", \ help="Set all depth nodes to NOT be inverted (number 0 " + \ "in the last column). Default is to leave it as is.") return parser.parse_args() if __name__ == "__main__": # getting the command line options from the parser (options,args)= getCmdLineArgs() # seeing if the single mandatory option is present if not options.layer: print "Error: must define a layer to adjust.\n" + \ "Type 'c_modify.py --help' to see all options available." sys.exit(usage) velModel = [] for line in file(options.inFile, "r").readlines(): velModel.append([line]) adjustedModel = [] adjustLayer = False for line in velModel: for text in line: fields = text.split() # evaluating if it is the proper depth layer: if (fields[0] == "B") and (fields[1] == str(options.layer)): adjustLayer = True adjustedModel.append(text) continue elif (fields[0] == ("B")) and (fields[1] != str(options.layer)): adjustLayer = False elif (fields[0] == ("V")): adjustLayer = False # if reading the nodes of interest, replace with modified values if adjustLayer: offset = float(fields[0]) depth = float(fields[1]) invert = float(fields[2]) # if the invert option is used, set all nodes to be inverted (1) if options.invert: invert = 1 elif options.noInvert: invert = 0 adjustedModel.append("%7.2f %4.2f %d\n" % (offset, depth + options.shift, invert)) # otherwise just write out original values (to a new list) else: adjustedModel.append(text) # writing out the file outFileObj = open(options.outFile, "w") for line in adjustedModel: outFileObj.write(line) outFileObj.close()