日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C/C++實現segy文件的讀取詳解_C 語言

作者:GeoFXR ? 更新時間: 2022-05-18 編程語言

本文檔將介紹SEGY的讀取與寫入過程,其中包括IBM與PC兩種數據格式的轉換。

程序將調用IEEE2IBM.cpp文件完成IBM與PC格式的互相轉換。

新建頭文件ReadSeismic.h與C++文件ReadSeismic.cpp,以及主程序main.cpp。

1 頭文件ReadSeismic.h的編寫及其規范

1.1 程序描述、調用、聲明、定義

/**********************************************************************

 * Copyright(C) 2018,Company All Rights Reserved   (1)版權說明
   *
 * @file    : ReadSeismic.cpp                      (2) 文件名
   *
 * @brief   :  實現地震數據的讀、寫操作                 (3) 該文件主要功能簡介
   *
 * @version : 1.0                                  (4) 版本信息
   *
 * @author  : Fan XinRan                           (5) 創建作者
   *
 * @date    : 2022/2/8 星期二                       (6) 創建時間
   *
 * Others  :                                       (7) 備注、改動信息等
   **********************************************************************/
  
//調用需要的C頭文件
#include   //C Language header file  
#include
#include
#include

//調用需要的C++頭文件
#include  // C++ header file
#include
#include

//調用非標準庫
#include"alloc.h"   // 用于創建多維數組
#include"segy.h"   // 包含segy與bhed結構體,用于提取卷頭和道頭中采集、存儲的信息

// 定義全局變量及命名空間
#define PI 3.141592654   //Constant Number Definition
#define EPS 0.0000001

using namespace std;

1.2 聲明函數

unsigned short exchangeLowHigh16(unsigned short Data_temp);//16位高低位轉換函數  short占2字節,2*8
unsigned int exchangeLowHigh32(unsigned int Data_temp); //32位高低位轉換函數  4*8
float ibm2pc(unsigned int Data_temp);      //IBM轉PC數據
unsigned int pc2ibm(float input);          //PC轉IBM數據
float ieee2pc(unsigned int Data_temp);   //IEEE轉為PC

void trace_ibm2pc(float *data_output, int *data_input, int nt); //地震道數據由IBM轉換為PC格式
void trace_pc2ibm(float *data_input, int *data_output, int nt); //地震道數據由PC轉換為IBM格式

bool copySeismicDataIBM(const char *filenameInput, const char *filenameOutput); //Copy seismic data from Inputfile to Outputfile

1.3完整代碼

/**********************************************************************

 * Copyright(C) 2018,Company All Rights Reserved   
   *
 * @file    : ReadSeismic.cpp                      
   *
 * @brief   :  實現地震數據的讀、寫操作                 
   *
 * @version : 1.0                                  
   *
 * @author  : Fan XinRan                          
   *
 * @date    : 2022/2/8 星期二                       
   *
 * Others  :                                       
   **********************************************************************/

//(1)調用需要的C頭文件
#include   // C Language header file  
#include
#include
#include

//(2)調用需要的C++頭文件
#include  // C++ header file
#include
#include

//(3)調用需要的非標準庫頭文件
#include"alloc.h"   // project header file 
#include"segy.h"
#include

//(4)定義全局常量
#define PI 3.141592654   // Constant Number Definition
#define EPS 0.0000001

//(5)聲明命名空間
using namespace std;

//(6)聲明函數名、輸入、輸出及其類型
unsigned short exchangeLowHigh16(unsigned short Data_temp);//16位高低位轉換函數  short占2字節,2*8
unsigned int exchangeLowHigh32(unsigned int Data_temp); //32位高低位轉換函數  4*8
float ibm2pc(unsigned int Data_temp);      //IBM轉PC數據
unsigned int pc2ibm(float input);          //PC轉IBM數據
float ieee2pc(unsigned int Data_temp);   //IEEE轉為PC

void trace_ibm2pc(float *data_output, int *data_input, int nt); //地震道數據由IBM轉換為PC格式
void trace_pc2ibm(float *data_input, int *data_output, int nt); //地震道數據由PC轉換為IBM格式

bool copySeismicDataIBM(const char *filenameInput, const char *filenameOutput); //Copy seismic data from Inputfile to Outputfile

2 C++文件ReadSeismic.cpp的編寫及其規范

2.1 必要的說明

/*************************************************************************************************************

 Function:       copySeismicDataIBM                                                (1)函數名
 Description:    copy segy file from input data to output data                     (2)簡要描述其功能
 Input:          
                 const char *filenameInput [in]    input filename (.segy)          (3)輸入變量及輸入文件類型
 Output:         
                 const char  *filenameOutput[out]  output filename (.segy)         (4)輸出變量及輸出文件類型
 Return:         
             bool  true    program success
			 bool  false   program failed                                          (5)返回值及其說明
 Author:     Fan XinRan                                                            (6)創建作者
 Date  :     2022/2/8                                                              (7)創建時間
 Others:                                                                           (8)備注、改動信息等
 
*************************************************************************************************************/

2.2 定義讀、寫函數

#include "ReadSeismic.h"
bool copySeismicDataIBM(const char *filenameInput, const char *filenameOutput){

    //實現代碼        
    ...

}

(1)定義待使用的結構體變量、數值型變量

bhed?與?segy均為定義在segy.h中的結構體(structure),分別包含了二進制卷頭信息與道頭信息,使用成員訪問運算符(.)獲取其內容;

使用unsigned int?聲明整型變量,使用long long?或__int64聲明SEGY文件字節數、地震道字節數,防止數據量超出范圍,并且盡可能初始化變量。

bhed fileheader;        // file header  卷頭
segy traceheader;       // trace header  道頭
unsigned int nt=0;        // number of sample  采樣點數
unsigned int sizefileheader=sizeof(fileheader);   // size of fileheader;
unsigned int sizetraceheader=sizeof(traceheader);  // size of traceheader;

unsigned int ncdp = 0;      // number of cdp  道數
long long  size_file = 0;   //size of input file
long long  size_trace = 0;  //size of per-trace

(2)新建指針變量

在讀、寫地震道數據這一任務中,需要用到輸入指針、輸出指針、地震道數據指針、道頭指針以及一個臨時指針變量,共五個指針變量。

FILE *fpinput = NULL;   // input file pointer
FILE *fpoutput = NULL;   // output file pointer
float *dataInput = NULL;  // input data pointer
segy *traceheaderArray = NULL; // traceheader pointer
int* temp = NULL;  // temp pointer 

之前的任務中fread(dataInput[itrace],nt * sizeof(float),1,fpinput)

整個讀、寫流程為:fpinput-->讀取nt個數據-->dataInput[itrace]-->寫入nt個數據-->fpoutput

本次任務中需要對數據進行變換,流程變為:fpinput-->讀取nt個數據-->temp-->IBM/PC轉換-->dataInput[itrace]-->寫入nt個數據-->fpoutput

(3)打開輸入、輸出文件指針

fpinput = fopen(filenameInput, "rb");  //open input file pointer 
fpoutput = fopen(filenameOutput,"wb");  //open output file pointer

//讀寫操作
...
    
//fopen()與fclose()成對出現,在對文件的操作完成后切記關閉文件
fclose(fpinput);  //close input file pointer
fclose(fpoutput); //close output file pointer

(4)判斷文件打開情況

if(fpinput==NULL){                                            //如果文件指針為NULL
	printf("Cannot open %s file\n", filenameInput);           //打印“文件打開失敗”
	return false;                                             //結束程序
}

if(fpoutput==NULL){
	printf("Cannot open %s file\n", filenameOutput);
	return false;
}

(5)讀取/計算卷、道信息

fread(&fileheader,sizefileheader,1,fpinput); // 從輸入流(fpinput)中讀取卷頭信息到指定地址---->fileheader

nt = exchangeLowHigh16(fileheader.hns) // 高低位轉換

_fseeki64(fpinput,0,SEEK_END);   // 從文件末尾偏移這個結構體0個長度給文件指針fpinput,即fpinput此時指向文件尾

size_file = _ftelli64(fpinput);  // 返回當前文件位置,即文件總字節數
size_trace = nt*sizeof(float)+sizetraceheader;  // 每一道的字節數 = 采樣點字節數+道頭字節數

ncdp = (size_file - (long long)sizefileheader)/size_trace; // 道數 = (文件總字節數 - 卷頭字節數)/每一道的字節數

_fseeki64(fpinput,sizefileheader,SEEK_SET); // 從文件開頭偏移sizefileheader(卷頭字節數)個長度給指針fpinput,即fpinput此時指向第一道的開始
fwrite(&fileheader, sizefileheader, 1, fpoutput); // 先寫入卷頭
  • fread()?從給定流讀取數據到指針所指向的數組中;
  • fwrite(*ptr, size, nmemb,*stream)?參數與fread()相同,把ptr所指向的數組中的數據寫入到給定流stream中;
  • _fseeki64的用法與fseek相同,表示從文件指定位置偏移一定字節數;前者具有更高的兼容性;
  • _ftelli64ftell同理,返回給定流的當前文件位置;
  • exchangeLowHigh16()完成short型的高低位轉換,int型的高低位轉換使用exchangeLowHigh64()

(6)遍歷每一條地震道,讀、寫數據

dataInput=alloc2float(nt,ncdp); // 分配nt*ncdp(采樣點數×道數)所需的內存空間,用來存放二維地震道數據
//其中,alloc2float是卸載alloc.h中的函數,創建一個float型的二維數組
//dataInput為二級指針,可簡記為dataInput指向每一行的開頭

memset(dataInput[0], 0, nt*ncdp * sizeof(float)); // 從第一行的開頭開始,將內存塊中nt*ncdp個字符賦值為0
// dataInput指向每行開頭,而dataInput[0]則為整個二維數組的起始位置

// 在內存的動態存儲區中分配ncdp個長度為sizetraceheader的連續空間
traceheaderArray = (segy*)calloc(ncdp,sizetraceheader);

temp = (int*)calloc(nt,sizeof(int));

//逐道讀取道頭與地震道數據
for(int itrace = 0; itrace < ncdp; itrace++){
    fread(&traceheaderArray[itrace],sizetraceheader,1,fpinput); // &traceheaderArray[itrace]為第itrace道的地址,讀取該道頭信息
    fread(temp,nt * sizeof(float),1,fpinput); // 讀取nt個采樣點的信息并將結果指向temp
    
    // 使用trace_ibm2pc將temp位置后nt個采樣點的數據,進行IBM-->PC的轉換,結果指向每一道開頭(dataInput[itrace])
    trace_ibm2pc(dataInput[itrace], temp, nt); 
    
}//end for(int itrace = 0; itrace < ncdp; itrace++)

//逐道寫入道頭與地震道數據
for (int itrace = 0; itrace < ncdp; itrace++) {
    fwrite(&traceheaderArray[itrace], sizetraceheader, 1, fpoutput); // 寫入該道頭信息
    
    // 使用trace_pc2ibm將temp位置后nt個采樣點的數據,進行PC-->IBM的轉換,結果指向每一道開頭(dataInput[itrace])
    trace_pc2ibm(dataInput[itrace],temp,nt); 
    
    fwrite(temp, nt * sizeof(int), 1, fpoutput); // 以IBM的格式存回fpoutput
}//end for(int itrace = 0; itrace < ncdp; itrace++)
 // 在每個循環末尾的"}"添加備注,便于尋找和區分

//在寫操作完成后釋放內存
free(temp); // free temp pointer
free(traceheaderArray); // free traceheader pointer
free2float(dataInput);  // free data input pointer
  • calloc(num,size):在內存的動態存儲區中分配num個長度為size的連續空間,函數返回一個指向分配起始地址的指針;如果分配不成功,返回NULL;
  • malloc(size):功能與calloc()相似,不同之處是malloc()?不會將內存值初始化為0,而?calloc()會將新申請的內存填充0。

2.3完整代碼

/*****************************************************************************
 Function:       CopySeismicData                                              
 Description:    copy segy file from input data to output data                   
 Input:          
                 const char *filenameInput [in]    input filename (.segy)         
 Output:         
                 const char  *filenameOutput[out]  output filename (.segy)        
 Return:         
             bool  true    program success
			 bool  false   program failed                                         
 Author:     Fan XinRan                                                         
 Date  :     2022/2/8                                                             
 Others:                                                                           
*****************************************************************************/
#include "ReadSeismic.h"

bool copySeismicDataIBM(const char *filenameInput, const char *filenameOutput){

	segy traceheader;       // trace header 
	bhed fileheader;        // file header
	unsigned int nt = 0;        // number of sample
	unsigned int sizetraceheader = sizeof(traceheader);  // size of traceheader;
	unsigned int sizefileheader = sizeof(fileheader);   // size of fileheader;
	unsigned int ncdp = 0;     // number of cdp
	long long   size_file = 0;  //size of input file
	long long  size_trace = 0;  //size of per-trace

	FILE *fpinput = NULL;   // input file pointer
	FILE *fpoutput = NULL;   //output file pointer
	float **dataInput = NULL;  //input data pointer
	segy *traceheaderArray = NULL; //
	int* temp = NULL;

	fpinput = fopen(filenameInput, "rb");  //open input file pointer 
	fpoutput = fopen(filenameOutput, "wb");  //open output file pointer


	if (fpinput == NULL) {
		printf("Cannot open %s file\n", filenameInput);
		return false;
	}

	if (fpoutput == NULL) {
		printf("Cannot open %s file\n", filenameOutput);
		return false;
	}

	fread(&fileheader, sizefileheader, 1, fpinput);

	nt = fileheader.hns;
	nt = exchangeLowHigh16(fileheader.hns);
	_fseeki64(fpinput, 0, SEEK_END);
	size_file = _ftelli64(fpinput);
	size_trace = nt * sizeof(float) + sizetraceheader;

	ncdp = (size_file - (long long)sizefileheader) / size_trace;

	_fseeki64(fpinput, sizefileheader, SEEK_SET);


	dataInput = alloc2float(nt, ncdp);
	memset(dataInput[0], 0, nt*ncdp * sizeof(float));
	traceheaderArray = (segy*)calloc(ncdp, sizetraceheader);

	temp = (int*)calloc(nt,sizeof(int));


	fwrite(&fileheader,sizefileheader,1, fpoutput);
	for (int itrace = 0; itrace < ncdp; itrace++) {

		fread(&traceheaderArray[itrace], sizetraceheader, 1, fpinput);
		fread(temp, nt * sizeof(int), 1, fpinput);
		trace_ibm2pc(dataInput[itrace], temp, nt);
	}//end for(int itrace = 0; itrace < ncdp; itrace++)

	for (int itrace = 0; itrace < ncdp; itrace++) {
		fwrite(&traceheaderArray[itrace], sizetraceheader, 1, fpoutput);
		trace_pc2ibm(dataInput[itrace],temp,nt);
		fwrite(temp, nt * sizeof(int), 1, fpoutput);
	}//end for(int itrace = 0; itrace < ncdp; itrace++)


	free(temp);
	free(traceheaderArray);
	free2float(dataInput);  // free data input pointer
	fclose(fpoutput); //close output file pointer
	fclose(fpinput);  //close input file pointer
	return true;
}

3 主函數main.cpp及運行結果

#include"ReadSeismic.h"

void main(){

	copySeismicDataIBM("Azi6-Ang35-BZ19-6-1.segy","Outputibm.segy");

}

運行主函數后,程序會讀入Azi6-Ang35-BZ19-6-1.segy,這是一個IBM格式的數據。再寫入到Outputibm.segy,從而完成對SEGY文件的復制。

原文鏈接:https://www.cnblogs.com/GeophysicsWorker/p/16012141.html

欄目分類
最近更新