You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SEG-Y与CSV互转过程中缺失SAMPLE_ARRAY列的技术咨询

Fixing the Missing SAMPLE_ARRAY Column in SEG-Y to CSV Conversion

Hey there! The reason your generated CSV doesn't have the SAMPLE_ARRAY column is that your current code only extracts trace header metadata—the descriptive tags for each seismic trace—while the SAMPLE_ARRAY contains the actual seismic waveform data (the numerical samples that make up each trace), which isn't part of the trace headers. Let's fix this and also cover the full round-trip (CSV back to SEG-Y) workflow.

Step 1: Update SEG-Y to CSV Code to Include SAMPLE_ARRAY

We'll modify your script to pull both trace headers and raw sample data for each trace, then add that as a new column in your DataFrame. Since CSV files store text, we'll convert each sample array to a comma-separated string (adjustable if you need a different format):

import segyio
import numpy as np
import pandas as pd

def segy_to_csv(segy_filename, csv_filename):
    with segyio.open(segy_filename, ignore_geometry=True) as f:
        # Extract trace headers (same as your original logic)
        header_keys = segyio.tracefield.keys
        trace_headers = pd.DataFrame(
            index=range(1, f.tracecount + 1),
            columns=header_keys.keys()
        )
        for k, v in header_keys.items():
            trace_headers[k] = f.attributes(v)[:]
        
        # Extract actual seismic sample data for each trace
        sample_arrays = []
        for trace in f.trace:
            # Convert array to comma-separated string for CSV compatibility
            sample_str = ",".join(map(str, trace))
            sample_arrays.append(sample_str)
        
        # Add the sample array column to the DataFrame
        trace_headers["SAMPLE_ARRAY"] = sample_arrays
        
        # Export to CSV, keeping trace ID as an explicit column
        trace_headers.to_csv(csv_filename, index_label="TRACE_ID")
        print(f"Successfully exported to {csv_filename}")

if __name__ == '__main__':
    segy_to_csv('Seis.sgy', 'out_with_samples.csv')

Key Details:

  • f.trace[:] retrieves all seismic traces as a 2D array (shape: [number of traces, number of samples per trace]). We iterate through each trace to convert it to a string for CSV storage.
  • The TRACE_ID column ensures we can map each sample array back to its correct trace when reversing the process.

Step 2: Convert Edited CSV Back to SEG-Y

To turn your edited CSV back into a valid SEG-Y file, we'll read the CSV, parse the SAMPLE_ARRAY strings back into numerical arrays, and reuse metadata from the original SEG-Y to maintain compatibility:

def csv_to_segy(csv_filename, output_segy_filename, original_segy_filename):
    # Load the edited CSV
    df = pd.read_csv(csv_filename, index_col="TRACE_ID")
    
    # Open original SEG-Y to copy critical metadata (sample rate, data format, etc.)
    with segyio.open(original_segy_filename, ignore_geometry=True) as src:
        # Define specs for the new SEG-Y file
        spec = segyio.spec()
        spec.tracecount = len(df)
        spec.samples = src.samples
        spec.format = src.format
        
        with segyio.create(output_segy_filename, spec) as dst:
            # Copy the original text header (preserves file context)
            dst.text[0] = src.text[0]
            
            # Write trace headers and sample data
            header_keys = segyio.tracefield.keys
            for trace_id, row in df.iterrows():
                trace_idx = trace_id - 1  # SEG-Y uses 0-based internal indexing
                # Write header values
                for key in header_keys.keys():
                    dst.header[trace_idx][header_keys[key]] = row[key]
                
                # Parse SAMPLE_STRING back into a numerical array
                sample_array = np.array(row["SAMPLE_ARRAY"].split(","), dtype=np.float32)
                dst.trace[trace_idx] = sample_array
        
        print(f"Successfully converted back to {output_segy_filename}")

if __name__ == '__main__':
    csv_to_segy('out_with_samples.csv', 'Edited_Seis.sgy', 'Seis.sgy')

Important Tips:

  • When editing the CSV, avoid modifying the TRACE_ID column or altering the comma-separated structure of SAMPLE_ARRAY values—this ensures the reverse conversion works correctly.
  • We reuse the original SEG-Y's specs to guarantee the new file matches the original's data format, sample rate, and other critical properties.

内容的提问来源于stack exchange,提问作者Hanna Nabil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 14:42:36