def main(): parser = argparse.ArgumentParser( description='Merge binary files by y-axis.') parser.add_argument('output', help='output json bin file') parser.add_argument('files', help='files to merge', nargs="+") parser.add_argument('-y', '--yspacing', help='spacing at y direction', type=float, required=True) args = parser.parse_args() ny = len(args.files) data = None bbox = None cnt = 0 for i in args.files: jd, d = load_bin(i) if cnt == 0: data = np.zeros([d.shape[0], ny, d.shape[1]], dtype=np.float) data[:, cnt, :] = d cnt += 1 bbox = jd['bbox'] bb = [bbox[0], bbox[1], 0.0, ny * args.yspacing, bbox[2], bbox[3]] save_bin(args.output, data, bbox=bb)
def main(): parser = argparse.ArgumentParser(description='Reshape 3D data.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') parser.add_argument('-s', '--size', help='number of nodes at x, y, z-direction', type=int, nargs=3, required=True) args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] n = args.size x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) z = np.linspace(b[4], b[5], s[2]) newx = np.linspace(b[0], b[1], n[0]) newy = np.linspace(b[2], b[3], n[1]) newz = np.linspace(b[4], b[5], n[2]) f = RegularGridInterpolator(points=(z, y, x), values=d, fill_value=None) z, y, x = np.meshgrid(newz, newy, newx, indexing='ij') data = f((z.ravel(), y.ravel(), x.ravel())) save_bin(args.output, data.reshape(n[::-1]), jd['bbox'])
def main(): parser = argparse.ArgumentParser( description='Load faults from txt file and create binary model.') parser.add_argument('faults', help='input txt file with faults') parser.add_argument('model', help='input bin file with model') parser.add_argument('out_faults', help='output binary file with faults') parser.add_argument('-w', '--width', help='width of fault', type=float, required=True) args = parser.parse_args() faults = load_faults(args.faults) jd, d = load_bin(args.model) bb = jd['bbox'] x0 = np.array([bb[0], bb[2], bb[4]]) x1 = np.array([bb[1], bb[3], bb[5]]) h = (x1 - x0) / np.array(d.shape[::-1]) print "H=", h data = np.full_like(d, 0.0) for f in faults.keys(): print 'Fault', f fill_fault(faults[f], data, x0, h, args.width) save_bin(args.out_faults, data, jd['bbox'])
def main(): parser = argparse.ArgumentParser(description='Reshape 2D data.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') parser.add_argument('-s', '--size', help='number of nodes at x, y-direction', type=int, nargs=2, required=True) args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] n = args.size x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) newx = np.linspace(b[0], b[1], n[0]) newy = np.linspace(b[2], b[3], n[1]) f = RectBivariateSpline(y, x, d, kx=1, ky=1) data = f(newy, newx) save_bin(args.output, data, jd['bbox'])
def main(): parser = argparse.ArgumentParser(description='Interpolate points 2d.') parser.add_argument('coords', help='coords file (x, y)') parser.add_argument('input', help='input files', nargs="+") args = parser.parse_args() c = np.loadtxt(args.coords) for i in args.input: jd, d = load_bin(i) b = jd['bbox'] s = jd['size'] x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) d = d.reshape((s[1], s[0])) f = RectBivariateSpline(y, x, d, kx=1, ky=1) data = [] if len(c.shape) == 1: data = f(c[1], c[0]) else: data = [[]] for j in range(c.shape[0]): data[0].append(f(c[j, 1], c[j, 0])[0][0]) s = '' for j in data[0]: s = '%s %s' % (s, j) print s
def main(): parser = argparse.ArgumentParser(description = 'Place 3D data (input1) inside other 3D data (input2).') parser.add_argument('input1', help='input1 file') parser.add_argument('input2', help='input2 file') parser.add_argument('output', help='output file') parser.add_argument('-i', '--index', help='index', type=int, nargs=3, required = True) args = parser.parse_args() jd1, d1 = load_bin(args.input1) jd2, d2 = load_bin(args.input2) bb = jd1['bbox'] i = args.index d2[i[2]:i[2]+d1.shape[0], i[1]:i[1]+d1.shape[1], i[0]:i[0]+d1.shape[2]] = d1 save_bin(args.output, d2, jd2['bbox'])
def main(): parser = argparse.ArgumentParser( description='Create 3D model from 2D maps.') parser.add_argument('output', help='output file') parser.add_argument('layers', help='layers files', nargs="+") parser.add_argument('-l', '--values', help='values for layers', type=file, required=True) parser.add_argument('-z', '--zcoords', help='values for z coordinates', type=str, required=True) args = parser.parse_args() layers = [] jd = [] for i in args.layers: j, d = load_layer(i) layers.append(d) jd.append(j) layers[0] += 1.0 layers[-1] -= 1.0 v = load_values(args.values, layers[0].shape) if len(v) != len(layers) - 1: raise argparse.ArgumentError( "number of layers and values should be the same") n = layers[0].shape t, z = load_bin(args.zcoords) data = np.zeros((z.shape[0], n[0], n[1])) for j in range(n[0]): print 'Procession %s of %s' % (j, n[0]) for i in range(n[1]): #for k in range(n[2]): for m in range(len(layers) - 1): l1 = layers[m][j][i] l2 = layers[m + 1][j][i] #print l1,l2, z[k] #if z[k] <= l1 and z[k] >= l2: #print 'AAA' # data[k][j][i] = v[m] # break data[:, j, i] += (data[:, j, i] <= 0.0) * ( (z[:, j, i] <= l1) & (z[:, j, i] >= l2)) * v[m][j, i] print 'Min max data %s %s' % (data.min(), data.max()) save_bin(args.output, data, jd[0]['bbox'] + [float(z.min()), float(z.max())])
def main(): parser = argparse.ArgumentParser(description = 'Show 2D binary data.') parser.add_argument('json_file', help='json data file') args = parser.parse_args() jdata, data = load_bin(args.json_file) plt.imshow(data, cmap='jet', aspect='auto') plt.colorbar() plt.show()
def main(): parser = argparse.ArgumentParser(description = 'Create data with constant value like existing array.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') parser.add_argument('-a', '--value', help='value', type=float, required = True) args = parser.parse_args() jd, data = load_bin(args.input) save_bin(args.output, np.full_like(data, args.value), jd['bbox'])
def load_values(path, shape): v = [] for i in path: d = None try: print i jd, d = load_bin(i.strip()) print 'Loading layer', i except: d = np.ones(shape) * float(i) print 'Creating layer', i v.append(d) return v
def main(): parser = argparse.ArgumentParser(description='Evaluate any expression.') parser.add_argument('expr', help='expression', type=str) parser.add_argument('output', help='output file') parser.add_argument('-a', help='a-parameter', type=str, required=True) parser.add_argument('-b', help='b-parameter', type=str) parser.add_argument('-c', help='c-parameter', type=str) parser.add_argument('-d', help='d-parameter', type=str) args = parser.parse_args() a = b = c = d = None jd, a = load_bin(args.a) if args.b: jd, b = load_bin(args.b) if args.c: jd, c = load_bin(args.c) if args.d: jd, d = load_bin(args.d) data = eval(args.expr) save_bin(args.output, data, jd['bbox'])
def main(): parser = argparse.ArgumentParser( description='Slice 3D data by one coordinate.') parser.add_argument('input', help='input file') parser.add_argument('-a', '--value', help='coordinate value', type=float, required=True) parser.add_argument('-c', '--coord', help='coordinate index -- 0, 1, or 2', type=int, required=True) parser.add_argument('output', help='output file') args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) z = np.linspace(b[4], b[5], s[2]) """ f = RegularGridInterpolator(points = (z, y, x), values = d, fill_value = None) c = np.loadtxt(args.coords) data = f((c[:,2], c[:,1], c[:,0])) for i in data: print i """ if args.coord == 1: if args.value >= b[2] and args.value <= b[3]: h = (b[3] - b[2]) / s[1] print "Y spacing", h ind = int((args.value - b[2]) / h) print "Y index", ind ind2 = ind + 1 y1 = y[ind] y2 = y[ind2] print "Interpolate between", y1, y2 c1 = (y2 - args.value) / (y2 - y1) c2 = 1.0 - c1 newd = d[:, ind, :] * c1 + d[:, ind2, :] * c2 save_bin(args.output, newd, [b[0], b[1], b[4], b[5]]) else: print "Invalid coord value:", args.coord
def main(): parser = argparse.ArgumentParser( description='Clip (limit) the values in an array.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') parser.add_argument('-b', '--bounds', help='bounds for values', type=two_floats, metavar='"min max"', required=True) args = parser.parse_args() jd, data = load_bin(args.input) save_bin(args.output, np.clip(data, args.bounds[0], args.bounds[1]), jd['bbox'])
def main(): parser = argparse.ArgumentParser( description='Show some info about binary file.') parser.add_argument('json_file', help='json data file') args = parser.parse_args() jdata, data = load_bin(args.json_file) bbox = jdata['bbox'] print 'Array size:', jdata['size'] print 'Bounding box:', jdata['bbox'] print 'Saving binary data to "%s"' % jdata['bin_data'] print 'Min/max value:', data.min(), data.max() print 'Spacing:', (bbox[1] - bbox[0]) / jdata['size'][0], ( bbox[3] - bbox[2]) / jdata['size'][1] print 'Comment:', jdata['comment'] print 'Command:', jdata['command']
def main(): parser = argparse.ArgumentParser( description='Scale all values at bin file.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') parser.add_argument('-s', '--scale', help='scale value', type=float, required=True) args = parser.parse_args() jd, data = load_bin(args.input) data *= args.scale save_bin(args.output, data, jd['bbox'])
def main(): parser = argparse.ArgumentParser(description='Set values between fault.') parser.add_argument('input', help='input file') parser.add_argument('f1', help='fault bottom') parser.add_argument('f2', help='fault top') parser.add_argument('output', help='output file') parser.add_argument('-v', '--value', help='fault value', type=float, required=True) args = parser.parse_args() jd, data = load_bin(args.input) bb = jd['bbox'] xl = np.linspace(bb[0], bb[1], data.shape[1]) yl = np.linspace(bb[2], bb[3], data.shape[0]) print bb f1 = np.loadtxt(args.f1) ff1 = interpolate.interp1d(f1[:, 0], f1[:, 1] + 150.0, bounds_error=False, fill_value='extrapolate') f2 = np.loadtxt(args.f2) ff2 = interpolate.interp1d(f2[:, 0], f2[:, 1] + 150.0, bounds_error=False, fill_value='extrapolate') rmin = min(f1[:, 0].min(), f2[:, 0].min()) rmax = max(f1[:, 0].max(), f2[:, 0].max()) for i in range(data.shape[1]): if xl[i] < rmin or xl[i] > rmax: continue y1 = ff1(xl[i]) y2 = ff2(xl[i]) for j in range(data.shape[0]): if yl[j] >= y1 and yl[j] <= y2: data[j, i] = args.value save_bin(args.output, data, jd['bbox'])
def main(): parser = argparse.ArgumentParser(description='Show 3D binary data.') parser.add_argument('json_file', help='json data file') args = parser.parse_args() jdata, data = load_bin(args.json_file) fig = plt.figure() ims = [] for k in range(data.shape[0]): im = plt.imshow(data[k], cmap='jet', animated=True, aspect='auto') ims.append([im]) ani = animation.ArtistAnimation(fig, ims, interval=100, blit=True, repeat_delay=1000) plt.show()
def main(): parser = argparse.ArgumentParser(description='Convert 3D data to VTK.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) z = np.linspace(b[4], b[5], s[2]) hx = x[1] - x[0] hy = y[1] - y[0] hz = z[1] - z[0] n = [s[0] - 1, s[1] - 1, s[2] - 1] newx = np.linspace(b[0] + hx / 2, b[1] - hx / 2, n[0]) newy = np.linspace(b[2] + hy / 2, b[3] - hy / 2, n[1]) newz = np.linspace(b[4] + hz / 2, b[5] - hz / 2, n[2]) f = RegularGridInterpolator(points=(z, y, x), values=d, fill_value=None) zv, yv, xv = np.meshgrid(newz, newy, newx, indexing='ij') data = f((zv.ravel(), yv.ravel(), xv.ravel())) #grid = tvtk.ImageData(spacing=(hx, hy, hz), origin=(b[0], b[2], b[4]), dimensions=s) grid = tvtk.RectilinearGrid(dimensions=s) grid.x_coordinates = x grid.y_coordinates = y grid.z_coordinates = z grid.cell_data.scalars = np.ravel(data, order='F') grid.cell_data.scalars.name = 'data' # Writes legacy ".vtk" format if filename ends with "vtk", otherwise # this will write data using the newer xml-based format. write_data(grid, args.output)
def main(): parser = argparse.ArgumentParser( description='Interpolate 3D data to points.') parser.add_argument('input', help='input file') parser.add_argument('coords', help='coords file (x y z format)') args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) z = np.linspace(b[4], b[5], s[2]) f = RegularGridInterpolator(points=(z, y, x), values=d, fill_value=None) c = np.loadtxt(args.coords) data = f((c[:, 2], c[:, 1], c[:, 0])) for i in data: print i
def main(): parser = argparse.ArgumentParser(description = 'Convert bin to segy.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] spacing = (b[5] - b[4]) / d.shape[0] x = np.linspace(b[0], b[1], d.shape[2]) y = np.linspace(b[2], b[3], d.shape[1]) xv, yv = np.meshgrid(x, y, sparse=False, indexing='ij') header = {} header['GroupX'] = xv.ravel() header['GroupY'] = yv.ravel() write_segy(args.output, d.reshape((d.shape[0], d.shape[1]*d.shape[2])), dt = spacing, trace_header_in = header)
def load_layer(path): jd, data = load_bin(path) return jd, data
def load_layer(path): jd, data = load_bin(path) #print 'Layer %s min max %s %s' % (path, data.min(), data.max()) #return data.reshape(size) return jd, data