#!/usr/bin/python # Script to combine two ascii datafiles (2 columns, ie: "[shot](or FFID) [traveltime]") into one, # matching the first column of each. It is intended to be used for combining data "hunks" that # are sampled at the same rate (and offset),but may not necessarily be sequential (merging these # by hand would be tedious). # # # 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 import fileinput if len(sys.argv) != 4: print(""" Usage is: 'columnMerge.py [input1] [input2] [output]' Used to merge sequential with non-sequential ascii data and write output to a file. The input data must be of (at least) 2 columns each. For example: input1 (X1 Y1), and input2 (X2 Y2) yields output (X1 Y1 Y2). Lines that dont have matching first columns are discarded. Example, Input 1: 1 0 2 0 3 0 4 0 5 0 Input 2: 1 1.188 15.504 2 1.188 15.510 3 1.188 15.516 4 1.189 15.521 5 1.190 15.527 Output: 1 0 1.188 15.504 2 0 1.188 15.510 3 0 1.188 15.516 4 0 1.189 15.521 5 0 1.190 15.527 """); sys.exit() inFile1, inFile2, outFile = sys.argv[1:4] sequentialHash = {} #Checking of input data, should be 2 columns of numbers (depth, velocity), #lines that are not 2 columns are skipped. for line in fileinput.input(inFile2): fields = line.split() if len(fields) >= 2: idx = fields[0] #creation of smelly hash table sequentialHash[idx] = fields[1:] outFileObj = open(outFile, "w") for line in fileinput.input(inFile1): fields = line.strip().split() if len(fields) >= 2 and sequentialHash.has_key(fields[0]): outStr = "" for s in fields: outStr += s + " " for s in sequentialHash[fields[0]]: outStr += s + " " #uncomment the following line to print to stdout: #sys.stdout.write("%s\n" %(outStr)) outFileObj.write("%s\n" %(outStr)) outFileObj.close()