def test_reading_and_writing_different_byteorders(self): """ Writing different byte orders should not change """ # This file is little endian. file = os.path.join(self.path, '1.su_first_trace') with NamedTemporaryFile() as tf: outfile = tf.name # The following should both work. su = _read_su(file) data = su.traces[0].data # Also read the original file. with open(file, 'rb') as f: org_data = f.read() self.assertEqual(su.endian, '<') # Write it little endian. su.write(outfile, endian='<') with open(outfile, 'rb') as f: new_data = f.read() self.assertEqual(org_data, new_data) su2 = _read_su(outfile) self.assertEqual(su2.endian, '<') np.testing.assert_array_equal(data, su2.traces[0].data) # Write it big endian. su.write(outfile, endian='>') with open(outfile, 'rb') as f: new_data = f.read() self.assertFalse(org_data == new_data) su3 = _read_su(outfile) self.assertEqual(su3.endian, '>') np.testing.assert_array_equal(data, su3.traces[0].data)
def test_reading_and_writing_different_byteorders(self): """ Writing different byte orders should not change """ # This file is little endian. file = os.path.join(self.path, "1.su_first_trace") with NamedTemporaryFile() as tf: outfile = tf.name # The following should both work. su = _read_su(file) data = su.traces[0].data # Also read the original file. with open(file, "rb") as f: org_data = f.read() self.assertEqual(su.endian, "<") # Write it little endian. su.write(outfile, endian="<") with open(outfile, "rb") as f: new_data = f.read() self.assertEqual(org_data, new_data) su2 = _read_su(outfile) self.assertEqual(su2.endian, "<") np.testing.assert_array_equal(data, su2.traces[0].data) # Write it big endian. su.write(outfile, endian=">") with open(outfile, "rb") as f: new_data = f.read() self.assertFalse(org_data == new_data) su3 = _read_su(outfile) self.assertEqual(su3.endian, ">") np.testing.assert_array_equal(data, su3.traces[0].data)
def read_su2d(data): """Reads a Seismic Unix data file input: data: is a string with your data name output: sdata: matrix where each column represents a seismic trace header: a dictionary with header words""" st = _read_su(data) #Get information from traces ns = len(st.traces[0].data) tracl = len(st.traces) #number of traces dt = (st.traces[0].header.sample_interval_in_ms_for_this_trace ) / 1000000 # sampling rate in seconds ft = (dt * ns) - dt #final time in seconds header = {"dt": dt, "ns": ns, "tracl": tracl} #Write the data into a matrix dado = np.zeros((tracl, ns)) #empty matrix where I write the data i = 0 while i < tracl: dado[i][:] = st.traces[i].data i = i + 1 sdata = np.transpose(dado) return (sdata, header)
def test_enforcing_byteorders_while_reading(self): """ Tests whether or not enforcing the byte order while reading and writing does something and works at all. Using the wrong byte order will most likely raise an Exception. """ # This file is little endian. file = os.path.join(self.path, '1.su_first_trace') # The following should both work. su = _read_su(file) self.assertEqual(su.endian, '<') su = _read_su(file, endian='<') self.assertEqual(su.endian, '<') # The following not because it will unpack the header and try to unpack # the number of data samples specified there which will of course not # correct. self.assertRaises(SEGYTraceReadingError, _read_su, file, endian='>')
def test_enforcing_byteorders_while_reading(self): """ Tests whether or not enforcing the byte order while reading and writing does something and works at all. Using the wrong byte order will most likely raise an Exception. """ # This file is little endian. file = os.path.join(self.path, "1.su_first_trace") # The following should both work. su = _read_su(file) self.assertEqual(su.endian, "<") su = _read_su(file, endian="<") self.assertEqual(su.endian, "<") # The following not because it will unpack the header and try to unpack # the number of data samples specified there which will of course not # correct. self.assertRaises(SEGYTraceReadingError, _read_su, file, endian=">")
def test_read_bytes_io(self): """ Tests reading from BytesIO instances. """ # 1 filename = os.path.join(self.path, '1.su_first_trace') with open(filename, 'rb') as fp: data = fp.read() st = _read_su(io.BytesIO(data)) self.assertEqual(len(st.traces[0].data), 8000)
def test_read_bytes_io(self): """ Tests reading from BytesIO instances. """ # 1 filename = os.path.join(self.path, "1.su_first_trace") with open(filename, "rb") as fp: data = fp.read() st = _read_su(io.BytesIO(data)) self.assertEqual(len(st.traces[0].data), 8000)
def test_unpacking_su_data(self): """ Unpacks data and compares them to data unpacked by Madagascar. """ # This file has the same data as 1.sgy_first_trace. file = os.path.join(self.path, '1.su_first_trace') data_file = os.path.join(self.path, '1.sgy_first_trace.npy') su = _read_su(file) data = su.traces[0].data # The data is written as integer so it is also converted to float32. correct_data = np.require(np.load(data_file).ravel(), np.float32) # Compare both. np.testing.assert_array_equal(correct_data, data)
def test_unpacking_su_data(self): """ Unpacks data and compares them to data unpacked by Madagascar. """ # This file has the same data as 1.sgy_first_trace. file = os.path.join(self.path, "1.su_first_trace") data_file = os.path.join(self.path, "1.sgy_first_trace.npy") su = _read_su(file) data = su.traces[0].data # The data is written as integer so it is also converted to float32. correct_data = np.require(np.load(data_file).ravel(), np.float32) # Compare both. np.testing.assert_array_equal(correct_data, data)
def test_read_and_write_su(self): """ Reading and writing a SU file should not change it. """ file = os.path.join(self.path, '1.su_first_trace') # Read the original file once. with open(file, 'rb') as f: org_data = f.read() with NamedTemporaryFile() as tf: outfile = tf.name # Read the SU file. su = _read_su(file) # Write it. su.write(outfile) with open(outfile, 'rb') as f: new_data = f.read() # Should be identical! self.assertEqual(org_data, new_data)
def test_read_and_write_su(self): """ Reading and writing a SU file should not change it. """ file = os.path.join(self.path, "1.su_first_trace") # Read the original file once. with open(file, "rb") as f: org_data = f.read() with NamedTemporaryFile() as tf: outfile = tf.name # Read the SU file. su = _read_su(file) # Write it. su.write(outfile) with open(outfile, "rb") as f: new_data = f.read() # Should be identical! self.assertEqual(org_data, new_data)
def handleMsg(msgJ): """Process the message in msgJ. Parameters: msgJ: dict Dictionary with command sent from client Returns: string JSON string with command response Commands are of the form: {'cmd' : 'getCCC', 'param0': 'param0val', ...} Response is a string of the form (note that JSON is picky that keys and strings should be enclosed in double quotes: '{"cmd" : "getCmd", "cmd" : "<response>"}' {'cmd':'getHello'} -> {"cmd":"getHello", "hello": "world"} {'cmd':'getSegyHdrs', filename: f} -> {"cmd":"getSegyHdrs", "segyhdrs": {ns:nsamps, dt:dt: hdrs:[hdr1, hdr2...]}} FIXME FIXME - this currently returns "segy", not "ensemble" as the key WARNING - you must call getSegyHdrs first flo and fhi are optional. If they are not present, no filtering {'cmd':'getEnsemble', filename:f, ensemble:n, [flo:flo, fhi: fhi]} -> {"cmd":"getEnsemble", "segy": {ns:nsamps, dt:dt: traces:[trc1, trc2...]}} """ print('msgJ: {}'.format(msgJ)) if msgJ['cmd'].lower() == 'getsegyhdrs': filename = msgJ['filename'] print('getting segyhdr >{}<, filename: {}'.format(msgJ, filename)) t0 =datetime.now() if segy.filename != filename: # new file - open it try: s = _read_su(filename, endian='>', headonly=True) segy.filename = filename segy.segyfile = s except: ret = json.dumps({"cmd":"readSegy", "error": "Error reading file {}".format(filename)}) return ret print("ntrcs = {}".format(len(segy.segyfile.traces))) hdrs = [segy.getTrc(i, headonly=True) for i in range(len(segy.segyfile.traces))] nsamps = segy.segyfile.traces[0].header.number_of_samples_in_this_trace dt = segy.segyfile.traces[0].header.sample_interval_in_ms_for_this_trace/(1000.*1000.) segy.nsamps = nsamps segy.dt = dt segy.hdrs = hdrs ret = json.dumps({"cmd": "getSegyHdrs", "segyHdrs" : json.dumps({"dt":dt, "ns":nsamps, "filename": segy.filename, "hdrs":hdrs})}) return ret if msgJ['cmd'].lower() == 'getnmo': # assumes getSegyHdrs called already. needed? print('nmo getting ens', msgJ) if segy.segyfile is None: ret = json.dumps({"cmd":"getNMO", "error": "Error doing NMO: call getSegyHdrs first."}) return ret try: vnmo = msgJ['vnmo'] tnmo = msgJ['tnmo'] print('got nmo', vnmo, tnmo) except: vnmo = 'vnmo=2000' tnmo = 'tnmo=0' try: ens = int(msgJ['ensemble']) try: # open a tmp file tmpf = tempfile.NamedTemporaryFile(delete=False) # output print('opened', tmpf.name) # and the segy input file with open(msgJ['filename'], 'rb') as sf: # input # and do the nmo p1 = sp.Popen(['suwind', 'key=cdp', 'min={}'.format(ens), 'max={}'.format(ens)], stdin=sf, stdout=sp.PIPE) p2 = sp.Popen(['sugain', "tpow=1.5"], stdin=p1.stdout, stdout=sp.PIPE) p3 = sp.Popen(['sunmo', vnmo, tnmo], stdin=p2.stdout, stdout=tmpf) print('p2 ok') p1.stdout.close() p2.stdout.close() out,err = p3.communicate() print('suwind/sugain/nmo', out, err) #print('nmo call', ret) tmpf.close() # nmo segy file nsegy = Segy() nsegy.filename = tmpf.name nsegy.segyfile = _read_su(tmpf.name, headonly=False) nmontrcs = len(nsegy.segyfile.traces) #print('nmo ntrcs', nmontrcs) nmotrcs = [nsegy.getTrc(i, headonly=False, trctype='seismic') for i in range(nmontrcs)] # delete the tmp file #os.unlink(tmpf.name) print('nmo trcs', len(nmotrcs)) except: print('err nmo', ens) ret = json.dumps({"cmd":"getNMO", "error": "Error performing NMO"}) return ret ntrc = len(nmotrcs) except: print('err ens', msgJ) ret = json.dumps({"cmd":"getNMO", "error": "Error reading ensemble number"}) return ret print("ens = {} ntrc={}".format(ens, len(nmotrcs))) # dt/nsamps could change from the original due to decimation dt = nmotrcs[0]["dt"] nsamps = nmotrcs[0]["nsamps"] print('dt, nsamps', dt, nsamps) #print(json.dumps(traces[0])) ret = json.dumps({"cmd": "getNMO", "NMO" : json.dumps({"dt":dt, "ns":nsamps, "filename": nsegy.filename, "traces":nmotrcs})}) return ret if msgJ['cmd'].lower() == 'getvelan': if segy.segyfile is None: ret = json.dumps({"cmd":"getEnsemble", "error": "Error reading ensemble"}) return ret try: ens = int(msgJ['ensemble']) print('in velan', ens) except: print('no ens') return json.dumps({"cmd":"getVelan", "error": "Error reading ensemble number"}) try: dv = msgJ['dv'] fv = msgJ['fv'] nv = msgJ['nv'] except: fv=1500 dv=100 nv=50 dvstr = "dv={}".format(dv) fvstr = "fv={}".format(fv) nvstr = "nv={}".format(nv) tmpf = tempfile.NamedTemporaryFile(delete=False) # output with open(segy.filename, 'rb') as sf:# input #tmpfname = tmpf.name p1 = sp.Popen(['suwind', 'key=cdp', 'min={}'.format(ens), 'max={}'.format(ens)], stdin=sf, stdout=sp.PIPE) p2 = sp.Popen(['sugain', "tpow=1.5"], stdin=p1.stdout, stdout=sp.PIPE) p3 = sp.Popen(['suvelan', dvstr, fvstr, nvstr], stdin=p2.stdout, stdout=tmpf) print('p3 ok') p1.stdout.close() p2.stdout.close() out,err = p3.communicate() print('suwind/sugain/velan', out, err) #ret = sp.call(['suvelan', dvstr, fvstr, nvstr], stdin=sf, stdout=tmpf) #print('wrote suvelan file', ret, tmpf.name) tmpf.close() vsegy = Segy() vsegy.filename=tmpf.name vsegy.segyfile = _read_su(tmpf.name, headonly=False) vtrcs = [vsegy.getTrc(i, headonly=False, trctype='velocity', v0=fv, dv=dv) for i in range(len(vsegy.segyfile.traces)) if vsegy.segyfile.traces[i].header.ensemble_number == ens] print('nvel trcs', len(vtrcs)) dt = vtrcs[0]["dt"] nsamps = vtrcs[0]["nsamps"] print('dt, nsamps', dt, nsamps) #print(json.dumps(traces[0])) ret = json.dumps({"cmd": "getVelan", "velan" : json.dumps({"dt":dt, "ns":nsamps, "fv":fv, "dv":dv, "nv":nv, "filename": vsegy.filename, "traces":vtrcs})}) #ret = json.dumps({"cmd": "velan", "velan": "test"}) return ret if msgJ["cmd"].lower() == "gethello": ret = json.dumps({"cmd": "hello", "hello": "world"}) return ret