将gprMax的out文件转化为DZT文件

文章介绍了如何使用Python的h5py库读取和操作HDF5文件,包括获取基本参数、核心数据以及创建包含头信息和数据的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,获取out文件中的基本参数

import h5py
def open_list_Attribute(file_path):
    file = h5py.File(file_path, 'r')
    list_h = list(file.attrs.keys())
    return file, list_h
def get_parameter(file_path):
    file,list_Attribute = open_list_Attribute(file_path)
    len_list = len(list_Attribute)
    parameter =[]
    for i in range(0, len_list):
        parameter.append(file.attrs[list_Attribute[i]])

    iteratation = parameter[0]
    dt = parameter[2]
    d_run = parameter[3]
    dx = d_run[0]
    dy = d_run[1]
    dz = d_run[2]
    nrx = parameter[5]
    nsrc = parameter[6]
    n_run = parameter[7]
    nx = n_run[0]
    ny = n_run[1]
    nz = n_run[2]
    rxsteps = parameter[8]
    srcsteps = parameter[9]
    return iteratation, dt, dx, dy, dz, nrx, nsrc, nx, ny, nz, rxsteps, srcsteps

2,获取核心数据

import h5py

def get_catalog(file_path):

    file = h5py.File(file_path, 'r')
    catalog = list(file.keys())

    sub_catalog =[]
    for i in range(0, len(catalog)):
        sub_catalog.append(list(file[catalog[i]]))

    ssub_catalog = []
    for i in range(0, len(sub_catalog)):
        ssub_catalog.append(list(file[catalog[i]][sub_catalog[i][0]]))
    return file, catalog, sub_catalog, ssub_catalog
def get_data(file_path):
    file, catalog, sub_catalog, ssub_catalog = get_catalog(file_path)
    # print(catalog, sub_catalog, ssub_catalog)

    rc = list(file[catalog[0]][sub_catalog[0][0]][ssub_catalog[0][0]].shape)
    if len(rc) < 2 :
        data = np.zeros((len(ssub_catalog[0]), rc[0]))
    else:
        data = np.zeros((len(ssub_catalog[0]), rc[0], rc[1]))
    # print(f'数据的shape{data.shape}')
    # print('----------------------------------------------------------')
    for i in range(0, len(ssub_catalog[0])):
        j = 0
        for datas in file[catalog[0]][sub_catalog[0][0]][ssub_catalog[0][i]]:
            data[i][j] = datas
            j = j + 1
        i = i + 1
    return data

3,创建头和写数据

class readhead():
    _fields_ = []
#写1024个字节的头和数据
def write():
    iteratation, dt, dx, dy, dz, nrx, nsrc, nx, ny, nz, rxsteps, srcsteps = get_parameter(outname)
    rh = readhead()
    rh.tag = 2047
    rh.Header_size = 1024
    rh.nsamp = int(iteratation/10)
    rh.bits = 32
    rh.zero = 0
    rh.sps = 0.0  # 1.0 / dx;
    rh.spm = 1.0 / dx
    rh.mpm = 0
    rh.position = 0
    rh.range = rh.nsamp * dt*10e9
    rh.npass = 0
    b = bitarray(32)
    b[:] = 0
    rh.offset_to_range_gain = 0
    rh.nrgain = 0
    rh.offset_to_text = 512
    rh.size_of_text = 0
    rh.offset_to_proc_hist = 128
    rh.size_of_proc_hist = 35
    rh.nchan = 1
    rh.epsr = 1.0
    rh.top = 0.0
    rh.depth = 15
    rh.reserved =b'0'
    rh.dtype = b'0'
    rh.antname1 = b'S'
    rh.antname2 = b'2'
    rh.antname3 = b'7'
    rh.antname4 = b'0'
    rh.antname5 = b'M'
    rh.antname6 = b'H'
    rh.antname7 = b'z'
    rh.antname8_14 = b' '
    rh.chanmask = 0
    rh.name1 = b'0'
    rh.name2 = b'0'
    rh.name3 = b'1'
    rh.name4_12 = b' '
    rh.chksum = 0
    rh.variable = b'0'

    with open(filename, 'wb') as f:
        rh.tag = struct.pack('@H', rh.tag)
        f.write(rh.tag)

        rh.Header_size = struct.pack('@H', rh.Header_size)
        f.write(rh.Header_size)

        rh.nsamp = struct.pack('<H', rh.nsamp)
        f.write(rh.nsamp)

        rh.bits = struct.pack('<H', rh.bits)
        f.write(rh.bits)

        rh.zero = struct.pack('<H', rh.zero)
        f.write(rh.zero)

        rh.sps = struct.pack('<f', rh.sps)
        f.write(rh.sps)

        rh.spm = struct.pack('<f', rh.spm)
        f.write(rh.spm)

        rh.mpm = struct.pack('<f', rh.mpm)
        f.write(rh.mpm)

        rh.position = struct.pack('<f', rh.position)
        f.write(rh.position)

        rh.range = struct.pack('<f', rh.range)
        f.write(rh.range)

        rh.npass = struct.pack('<H', rh.npass)
        f.write(rh.npass)

        f.write(b)

        f.write(b)

        rh.offset_to_range_gain = struct.pack('<H',rh.offset_to_range_gain)
        f.write(rh.offset_to_range_gain)

        rh.nrgain = struct.pack('<H', rh.nrgain)
        f.write(rh.nrgain)

        rh.offset_to_text = struct.pack('<H', rh.offset_to_text)
        f.write(rh.offset_to_text)

        rh.size_of_text = struct.pack('<H', rh.size_of_text)
        f.write(rh.size_of_text)

        rh.offset_to_proc_hist = struct.pack('<H', rh.offset_to_proc_hist)
        f.write(rh.offset_to_proc_hist)

        rh.size_of_proc_hist = struct.pack('<H', rh.size_of_proc_hist)
        f.write(rh.size_of_proc_hist)

        rh.nchan = struct.pack('<H', rh.nchan)
        f.write(rh.nchan)

        rh.epsr = struct.pack('<f', rh.epsr)
        f.write(rh.epsr)

        rh.top = struct.pack('<f', rh.top)
        f.write(rh.top)

        rh.depth = struct.pack('<f', rh.depth)
        f.write(rh.depth)

        rh.reserved = struct.pack('<c', rh.reserved)
        for i in range(0, 31):
            f.write(rh.reserved)

        rh.dtype = struct.pack('<c', rh.dtype)
        f.write(rh.dtype)

        rh.antname1 = struct.pack('<c', rh.antname1)
        f.write(rh.antname1)

        rh.antname2 = struct.pack('<c', rh.antname2)
        f.write(rh.antname2)

        rh.antname2 = struct.pack('<c', rh.antname2)
        f.write(rh.antname2)

        rh.antname3 = struct.pack('<c', rh.antname3)
        f.write(rh.antname3)

        rh.antname4 = struct.pack('<c', rh.antname4)
        f.write(rh.antname4)

        rh.antname5 = struct.pack('<c', rh.antname5)
        f.write(rh.antname5)

        rh.antname6 = struct.pack('<c', rh.antname6)
        f.write(rh.antname6)

        rh.antname7 = struct.pack('<c', rh.antname7)
        f.write(rh.antname7)

        rh.antname8_14 = struct.pack('<c', rh.antname8_14)
        for i in range(0,7):
            f.write(rh.antname8_14)

        rh.chanmask = struct.pack('<H', rh.chanmask)
        f.write(rh.chanmask)

        rh.name1 = struct.pack('<c', rh.name1)
        f.write(rh.name1)

        rh.name2 = struct.pack('<c', rh.name2)
        f.write(rh.name2)

        rh.name3 = struct.pack('<c', rh.name3)
        f.write(rh.name3)

        rh.name4_12 = struct.pack('<c', rh.name4_12)
        for i in range(0, 8):
            f.write(rh.name4_12)

        rh.chksum = struct.pack('<H', rh.chksum)
        f.write(rh.chksum)
        rh.variable = struct.pack('<c', rh.variable)
        for i in range(0, 896):
            f.write(rh.variable)

        data = get_data(file_path)
        data_Ex = np.squeeze(data[2:3, :, :])
        Data = data_Ex*10e5
        Data_ceil = np.ceil(Data)
        Data_ceil = Data_ceil.astype(int)
        dataset = np.zeros((int(Data_ceil.shape[0]/10),Data_ceil.shape[1]))
        print(Data_ceil.shape)
        for i in range(0, Data_ceil.shape[1]):
            for j in range(0, int(Data_ceil.shape[0]/10)):
                dataset[j:j+1, i:i+1] = Data_ceil[j*10:j*10+1, i:i+1]
                datarun = int(dataset[j, i])
                datarun = struct.pack('@i',datarun)
                f.write(datarun)
        print(Data_ceil.max())
        print(dt)
        print(iteratation)
    f.close()

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值