SEGY IO
推薦采用的IDE為Visual studio(VS),本文檔將介紹SEGY文件的讀取與寫入過程,即SEGY文件的復制。
因此,新建頭文件ReadSeismic.h與C++文件ReadSeismic.cpp,以及主函數main.cpp。
1 SEGY簡介
SEG-Y文件格式是SEG協會為存儲地震數據而制定的幾種標准之一。標准SEGY文件一般包括三部分:卷頭、道頭與地震道數據;
卷頭(File Header) | EBCDIC文件頭(3200字節) | 保存一些對地震數據體進行描述的信息 |
二進制文件頭(400字節) | 存儲描述SEGY文件的數據格式、采樣點數、采樣間隔、測量單位等信息 | |
道頭(Trace Header) | 240字節 | 存儲某一地震道對應的線號、道號、采樣點數、大地坐標等信息 |
地震道數據(Data) | 若干采樣點(1000、2000) | 是對地震信號的波形按一定時間間隔Δt進行取樣所得的一系列離散振幅值 |
SEGY文件由卷頭及n個地震道記錄組成:
SEGY樣本示例:
2 頭文件ReadSeismic.h的編寫及其規范
2.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) 備注、改動信息等
**********************************************************************/
2.2 調用需要的C/C++頭文件
//調用需要的C頭文件
#include<stdio.h> //C Language header file
#include<stdlib.h>
#include<string.h>
#include<math.h>
//調用需要的C++頭文件
#include<iostream> // C++ header file
#include<vector>
#include<algorithm>
2.3 調用需要的非標准庫頭文件
注意使用雙引號(" "
),並將這些頭文件添加到項目文件夾中。對於每個調用的非標准庫,需要給出必要的注釋說明。
#include"alloc.h" // 用於創建多維數組
#include"segy.h" // 包含segy與bhed結構體,用於提取卷頭和道頭中采集、存儲的信息
2.4 定義全局變量及命名空間
對於代碼中用到的物理量、常數等,使用#define
定義常量並加以說明,避免函數中出現意義不明的常量。
#define PI 3.141592654 //Constant Number Definition
#define EPS 0.0000001
using namespace std;
2.5 聲明函數
bool copySeismicData(const char *filenameInput, const char *filenameOutput); //Copy seismic data from Inputfile to Outputfile
3 C++文件ReadSeismic.cpp的編寫及其規范
3.1 必要的說明
/*************************************************************************************************************
Function: copySeismicData (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)備注、改動信息等
*************************************************************************************************************/
3.2 定義讀、寫函數
#include "ReadSeismic.h"
bool copySeismicData(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
(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
fopen()
的參數詳解:
fopen(const char *filename, const char *mode)
- filename -- 要打開的文件名稱;
- mode -- 文件訪問模式;
(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 = 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()
從給定流讀取數據到指針所指向的數組中;fread(*ptr, size, nmemb,*stream)
- ptr -- 指向帶有最小尺寸 size*nmemb 字節的內存塊的指針;
- size -- 要讀取的每個元素的大小,以字節為單位。
- nmemb -- 元素的個數,每個元素的大小為 size 字節;
- stream -- 指向 FILE 對象的指針。
-
fwrite(*ptr, size, nmemb,*stream)
參數與fread()
相同,把ptr
所指向的數組中的數據寫入到給定流stream
中; -
_fseeki64
的用法與fseek
相同,表示從文件指定位置偏移一定字節數;前者具有更高的兼容性; -
_ftelli64
與ftell
同理,返回給定流的當前文件位置;_fseeki64(*stream, offset, whence)
-
stream -- 指向 FILE 對象的指針;
-
offset -- 相對 whence 的偏移量,以字節為單位;
-
whence -- 表示開始添加偏移 offset 的位置。一般定義為
SEEK_SET
(文件開頭)、SEEK_CUR
(文件指針的當前位置)、SEEK_END
(文件末尾)三類常量。
-
(6)遍歷每一條地震道,讀、寫數據
dataInput=(float*)calloc(nt,sizeof(float)); // 分配nt(采樣點數)個元素所需的內存空間,用來存放單條地震道數據
for(int itrace= 0; itrace< ncdp; itrace++){
fread(&traceheader, sizetraceheader,1, fpinput); // 指針fpinput自第一道道頭開始移動,讀取數據到traceheader中
fread(dataInput,nt*sizeof(float),1,fpinput); // 指針fpinput移動到道頭末尾(數據開頭),讀入nt個采樣點的數據到dataInput中
fwrite(&traceheader, sizetraceheader, 1, fpoutput);// 寫入某一道頭
fwrite(dataInput, nt * sizeof(float), 1, fpoutput);// 寫入某一道地震數據
}//end for(int itrace= 0; itrace< ncdp; itrace++)
// 在每個循環末尾的"}"添加備注,便於尋找和區分
//分配內存calloc()與釋放內存free()配合使用,在寫操作完成后釋放內存
free(dataInput); // free data input pointer
calloc(num,size)
:在內存的動態存儲區中分配num
個長度為size
的連續空間,函數返回一個指向分配起始地址的指針;如果分配不成功,返回NULL
;malloc(size)
:功能與calloc()
相似,不同之處是malloc()
不會設置內存為零,而calloc()
會設置分配的內存為零。
4 主函數main.cpp及運行結果
#include"ReadSeismic.h"
void main(){
copySeismicData("OVT-BZ19-6-1_pc.segy","Output.segy");
}
運行主函數后,程序會讀入OVT-BZ19-6-1_pc.segy
,再寫入到Output.segy
,從而完成對Segy文件的復制。
完整代碼
I ReadSeismic.h
/**********************************************************************
* 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<stdio.h> // C Language header file
#include<stdlib.h>
#include<string.h>
#include<math.h>
//(2)調用需要的C++頭文件
#include<iostream> // C++ header file
#include<vector>
#include<algorithm>
//(3)調用需要的非標准庫頭文件
#include"alloc.h" // project header file
#include"segy.h"
//(4)定義全局常量
#define PI 3.141592654 // Constant Number Definition
#define EPS 0.0000001
//(5)聲明命名空間
using namespace std;
//(6)聲明函數名、輸入、輸出及其類型
bool copySeismicData(const char *filenameInput, const char *filenameOutput); //Copy seismic data from Inputfile to Outputfile
II ReadSeismic.cpp
/*****************************************************************************
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 copySeismicData(const char *filenameInput, const char *filenameOutput){
bhed fileheader; // file header
segy traceheader; // trace 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
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;
_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);
fwrite(&fileheader, sizefileheader, 1, fpoutput);
dataInput=(float*)calloc(nt,sizeof(float));
for(int itrace= 0; itrace< ncdp; itrace++){
fread(&traceheader, sizetraceheader,1, fpinput);
fread(dataInput,nt*sizeof(float),1,fpinput);
fwrite(&traceheader, sizetraceheader, 1, fpoutput);
fwrite(dataInput, nt * sizeof(float), 1, fpoutput);
}//end for(int itrace= 0; itrace< ncdp; itrace++)
free(dataInput); // free data input pointer
fclose(fpinput); //close input file pointer
fclose(fpoutput); //close output file pointer
return true;
}
III segy.h
typedef struct { /* segy - trace identification header */
int tracl; /* Trace sequence number within line
--numbers continue to increase if the
same line continues across multiple
SEG Y files.
byte# 1-4
*/
int tracr; /* Trace sequence number within SEG Y file
---each file starts with trace sequence
one
byte# 5-8
*/
int fldr; /* Original field record number
byte# 9-12
*/
int tracf; /* Trace number within original field record
byte# 13-16
*/
int ep; /* energy source point number
---Used when more than one record occurs
at the same effective surface location.
byte# 17-20
*/
int cdp; /* Ensemble number (i.e. CDP, CMP, CRP,...)
byte# 21-24
*/
int cdpt; /* trace number within the ensemble
---each ensemble starts with trace number one.
byte# 25-28
*/
short trid; /* trace identification code:
-1 = Other
0 = Unknown
1 = Seismic data
2 = Dead
3 = Dummy
4 = Time break
5 = Uphole
6 = Sweep
7 = Timing
8 = Water break
9 = Near-field gun signature
10 = Far-field gun signature
11 = Seismic pressure sensor
12 = Multicomponent seismic sensor
- Vertical component
13 = Multicomponent seismic sensor
- Cross-line component
14 = Multicomponent seismic sensor
- in-line component
15 = Rotated multicomponent seismic sensor
- Vertical component
16 = Rotated multicomponent seismic sensor
- Transverse component
17 = Rotated multicomponent seismic sensor
- Radial component
18 = Vibrator reaction mass
19 = Vibrator baseplate
20 = Vibrator estimated ground force
21 = Vibrator reference
22 = Time-velocity pairs
23 ... N = optional use
(maximum N = 32,767)
Following are CWP id flags:
109 = autocorrelation
110 = Fourier transformed - no packing
xr[0],xi[0], ..., xr[N-1],xi[N-1]
111 = Fourier transformed - unpacked Nyquist
xr[0],xi[0],...,xr[N/2],xi[N/2]
112 = Fourier transformed - packed Nyquist
even N:
xr[0],xr[N/2],xr[1],xi[1], ...,
xr[N/2 -1],xi[N/2 -1]
(note the exceptional second entry)
odd N:
xr[0],xr[(N-1)/2],xr[1],xi[1], ...,
xr[(N-1)/2 -1],xi[(N-1)/2 -1],xi[(N-1)/2]
(note the exceptional second & last entries)
113 = Complex signal in the time domain
xr[0],xi[0], ..., xr[N-1],xi[N-1]
114 = Fourier transformed - amplitude/phase
a[0],p[0], ..., a[N-1],p[N-1]
115 = Complex time signal - amplitude/phase
a[0],p[0], ..., a[N-1],p[N-1]
116 = Real part of complex trace from 0 to Nyquist
117 = Imag part of complex trace from 0 to Nyquist
118 = Amplitude of complex trace from 0 to Nyquist
119 = Phase of complex trace from 0 to Nyquist
121 = Wavenumber time domain (k-t)
122 = Wavenumber frequency (k-omega)
123 = Envelope of the complex time trace
124 = Phase of the complex time trace
125 = Frequency of the complex time trace
130 = Depth-Range (z-x) traces
201 = Seismic data packed to bytes (by supack1)
202 = Seismic data packed to 2 bytes (by supack2)
byte# 29-30
*/
short nvs; /* Number of vertically summed traces yielding
this trace. (1 is one trace,
2 is two summed traces, etc.)
byte# 31-32
*/
short nhs; /* Number of horizontally summed traces yielding
this trace. (1 is one trace
2 is two summed traces, etc.)
byte# 33-34
*/
short duse; /* Data use:
1 = Production
2 = Test
byte# 35-36
*/
int offset; /* Distance from the center of the source point
to the center of the receiver group
(negative if opposite to direction in which
the line was shot).
byte# 37-40
*/
int gelev; /* Receiver group elevation from sea level
(all elevations above the Vertical datum are
positive and below are negative).
byte# 41-44
*/
int selev; /* Surface elevation at source.
byte# 45-48
*/
int sdepth; /* Source depth below surface (a positive number).
byte# 49-52
*/
int gdel; /* Datum elevation at receiver group.
byte# 53-56
*/
int sdel; /* Datum elevation at source.
byte# 57-60
*/
int swdep; /* Water depth at source.
byte# 61-64
*/
int gwdep; /* Water depth at receiver group.
byte# 65-68
*/
short scalel; /* Scalar to be applied to the previous 7 entries
to give the real value.
Scalar = 1, +10, +100, +1000, +10000.
If positive, scalar is used as a multiplier,
if negative, scalar is used as a divisor.
byte# 69-70
*/
short scalco; /* Scalar to be applied to the next 4 entries
to give the real value.
Scalar = 1, +10, +100, +1000, +10000.
If positive, scalar is used as a multiplier,
if negative, scalar is used as a divisor.
byte# 71-72
*/
int sx; /* Source coordinate - X
byte# 73-76
*/
int sy; /* Source coordinate - Y
byte# 77-80
*/
int gx; /* Group coordinate - X
byte# 81-84
*/
int gy; /* Group coordinate - Y
byte# 85-88
*/
short counit; /* Coordinate units: (for previous 4 entries and
for the 7 entries before scalel)
1 = Length (meters or feet)
2 = Seconds of arc
3 = Decimal degrees
4 = Degrees, minutes, seconds (DMS)
In case 2, the X values are longitude and
the Y values are latitude, a positive value designates
the number of seconds east of Greenwich
or north of the equator
In case 4, to encode +-DDDMMSS
counit = +-DDD*10^4 + MM*10^2 + SS,
with scalco = 1. To encode +-DDDMMSS.ss
counit = +-DDD*10^6 + MM*10^4 + SS*10^2
with scalco = -100.
byte# 89-90
*/
short wevel; /* Weathering velocity.
byte# 91-92
*/
short swevel; /* Subweathering velocity.
byte# 93-94
*/
short sut; /* Uphole time at source in milliseconds.
byte# 95-96
*/
short gut; /* Uphole time at receiver group in milliseconds.
byte# 97-98
*/
short sstat; /* Source static correction in milliseconds.
byte# 99-100
*/
short gstat; /* Group static correction in milliseconds.
byte# 101-102
*/
short tstat; /* Total static applied in milliseconds.
(Zero if no static has been applied.)
byte# 103-104
*/
short laga; /* Lag time A, time in ms between end of 240-
byte trace identification header and time
break, positive if time break occurs after
end of header, time break is defined as
the initiation pulse which maybe recorded
on an auxiliary trace or as otherwise
specified by the recording system
byte# 105-106
*/
short lagb; /* lag time B, time in ms between the time break
and the initiation time of the energy source,
may be positive or negative
byte# 107-108
*/
short delrt; /* delay recording time, time in ms between
initiation time of energy source and time
when recording of data samples begins
(for deep water work if recording does not
start at zero time)
byte# 109-110
*/
short muts; /* mute time--start
byte# 111-112
*/
short mute; /* mute time--end
byte# 113-114
*/
unsigned short ns; /* number of samples in this trace
byte# 115-116
*/
unsigned short dt; /* sample interval; in micro-seconds
byte# 117-118
*/
short gain; /* gain type of field instruments code:
1 = fixed
2 = binary
3 = floating point
4 ---- N = optional use
byte# 119-120
*/
short igc; /* instrument gain constant
byte# 121-122
*/
short igi; /* instrument early or initial gain
byte# 123-124
*/
short corr; /* correlated:
1 = no
2 = yes
byte# 125-126
*/
short sfs; /* sweep frequency at start
byte# 127-128
*/
short sfe; /* sweep frequency at end
byte# 129-130
*/
short slen; /* sweep length in ms
byte# 131-132
*/
short styp; /* sweep type code:
1 = linear
2 = cos-squared
3 = other
byte# 133-134
*/
short stas; /* sweep trace length at start in ms
byte# 135-136
*/
short stae; /* sweep trace length at end in ms
byte# 137-138
*/
short tatyp; /* taper type: 1=linear, 2=cos^2, 3=other
byte# 139-140
*/
short afilf; /* alias filter frequency if used
byte# 141-142
*/
short afils; /* alias filter slope
byte# 143-144
*/
short nofilf; /* notch filter frequency if used
byte# 145-146
*/
short nofils; /* notch filter slope
byte# 147-148
*/
short lcf; /* low cut frequency if used
byte# 149-150
*/
short hcf; /* high cut frequncy if used
byte# 151-152
*/
short lcs; /* low cut slope
byte# 153-154
*/
short hcs; /* high cut slope
byte# 155-156
*/
short year; /* year data recorded
byte# 157-158
*/
short day; /* day of year
byte# 159-160
*/
short hour; /* hour of day (24 hour clock)
byte# 161-162
*/
short minute; /* minute of hour
byte# 163-164
*/
short sec; /* second of minute
byte# 165-166
*/
short timbas; /* time basis code:
1 = local
2 = GMT
3 = other
byte# 167-168
*/
short trwf; /* trace weighting factor, defined as 1/2^N
volts for the least sigificant bit
byte# 169-170
*/
short grnors; /* geophone group number of roll switch
position one
byte# 171-172
*/
short grnofr; /* geophone group number of trace one within
original field record
byte# 173-174
*/
short grnlof; /* geophone group number of last trace within
original field record
byte# 175-176
*/
short gaps; /* gap size (total number of groups dropped)
byte# 177-178
*/
short otrav; /* overtravel taper code:
1 = down (or behind)
2 = up (or ahead)
byte# 179-180
*/
short int unass[30]; /*unassigned--for optional info*/
}segy;
typedef struct { /* bhed - binary header */
char commend[3200];
int jobid; /* job identification number */
int lino; /* line number (only one line per reel) */
int reno; /* reel number */
short ntrpr; /* number of data traces per record */
short nart; /* number of auxiliary traces per record */
unsigned short hdt; /* sample interval in micro secs for this reel */
unsigned short dto; /* same for original field recording */
unsigned short hns; /* number of samples per trace for this reel */
unsigned short nso; /* same for original field recording */
short format; /* data sample format code:
1 = floating point, 4 byte (32 bits)
2 = fixed point, 4 byte (32 bits)
3 = fixed point, 2 byte (16 bits)
4 = fixed point w/gain code, 4 byte (32 bits)
5 = IEEE floating point, 4 byte (32 bits)
8 = two's complement integer, 1 byte (8 bits)
*/
short fold; /* CDP fold expected per CDP ensemble */
short tsort; /* trace sorting code:
1 = as recorded (no sorting)
2 = CDP ensemble
3 = single fold continuous profile
4 = horizontally stacked */
short vscode; /* vertical sum code:
1 = no sum
2 = two sum ...
N = N sum (N = 32,767) */
short hsfs; /* sweep frequency at start */
short hsfe; /* sweep frequency at end */
short hslen; /* sweep length (ms) */
short hstyp; /* sweep type code:
1 = linear
2 = parabolic
3 = exponential
4 = other */
short schn; /* trace number of sweep channel */
short hstas; /* sweep trace taper length at start if
tapered (the taper starts at zero time
and is effective for this length) */
short hstae; /* sweep trace taper length at end (the ending
taper starts at sweep length minus the taper
length at end) */
short htatyp; /* sweep trace taper type code:
1 = linear
2 = cos-squared
3 = other */
short hcorr; /* correlated data traces code:
1 = no
2 = yes */
short bgrcv; /* binary gain recovered code:
1 = yes
2 = no */
short rcvm; /* amplitude recovery method code:
1 = none
2 = spherical divergence
3 = AGC
4 = other */
short mfeet; /* measurement system code:
1 = meters
2 = feet */
short polyt; /* impulse signal polarity code:
1 = increase in pressure or upward
geophone case movement gives
negative number on tape
2 = increase in pressure or upward
geophone case movement gives
positive number on tape */
short vpol; /* vibratory polarity code:
code seismic signal lags pilot by
1 337.5 to 22.5 degrees
2 22.5 to 67.5 degrees
3 67.5 to 112.5 degrees
4 112.5 to 157.5 degrees
5 157.5 to 202.5 degrees
6 202.5 to 247.5 degrees
7 247.5 to 292.5 degrees
8 293.5 to 337.5 degrees */
short hunass[170]; /* unassigned */
} bhed;
IV alloc.h
void *alloc1 (size_t n1, size_t size);
void *realloc1 (void *v, size_t n1, size_t size);
void free1 (void *p);
void **alloc2 (size_t n1, size_t n2, size_t size);
void free2 (void **p);
int **alloc2int (size_t n1, size_t n2);
void free2int (int **p);
float **alloc2float (size_t n1, size_t n2);
void free2float (float **p);
double **alloc2double (size_t n1, size_t n2);
void free2double (double **p);
bool **alloc2bool(size_t n1, size_t n2);
void free2bool(bool **p);
V alloc.cpp
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void *alloc1 (size_t n1, size_t size)
{
void *p;
if ((p=malloc(n1*size))==NULL)
return NULL;
return p;
}
/* re-allocate a 1-d array */
void *realloc1(void *v, size_t n1, size_t size)
{
void *p;
if ((p=realloc(v,n1*size))==NULL)
return NULL;
return p;
}
/* free a 1-d array */
void free1 (void *p)
{
free(p);
}
/* allocate a 2-d array */
void **alloc2 (size_t n1, size_t n2, size_t size)
{
size_t i2;
void **p;
if ((p=(void**)malloc(n2*sizeof(void*)))==NULL)
return NULL;
if ((p[0]=(void*)malloc(n2*n1*size))==NULL) {
free(p);
return NULL;
}
for (i2=0; i2<n2; i2++)
p[i2] = (char*)p[0]+size*n1*i2;
return p;
}
/* free a 2-d array */
void free2 (void **p)
{
free(p[0]);
free(p);
}
/* allocate a 2-d array of ints */
int **alloc2int(size_t n1, size_t n2)
{
return (int**)alloc2(n1,n2,sizeof(int));
}
/* free a 2-d array of ints */
void free2int(int **p)
{
free2((void**)p);
}
/* allocate a 2-d array of floats */
float **alloc2float(size_t n1, size_t n2)
{
return (float**)alloc2(n1,n2,sizeof(float));
}
/* free a 2-d array of floats */
void free2float(float **p)
{
free2((void**)p);
}
/* allocate a 2-d array of doubles */
double **alloc2double(size_t n1, size_t n2)
{
return (double**)alloc2(n1,n2,sizeof(double));
}
/* free a 2-d array of doubles */
void free2double(double **p)
{
free2((void**)p);
}
/*allocate a 2-d array of bools*/
bool **alloc2bool(size_t n1,size_t n2)
{
return (bool**)alloc2(n1,n2,sizeof(bool));
}
/* free a 2-d array of bools */
void free2bool(bool **p){
free2((void**)p);
}
附錄
I SEGY文件卷頭與道頭中常用的屬性
屬性 | 變量名 | 位置/字節 | 備注 |
---|---|---|---|
采樣點 | hns | 卷頭 | 此卷中每道的采樣點數 |
格式 | format | 卷頭 | ![]() |
線號 | fldr | 道頭/9-12 | - |
道號 | cdp | 道頭/21-24 | - |
采樣點數 | nhs | 道頭/33-34 | - |
橫坐標 | sx | 道頭/73-76 | - |
縱坐標 | sy | 道頭/77-80 | - |
采樣點數 | ns | 道頭/115-116 | - |
采樣間隔 | dt | 道頭/117-118 | - |
增益 | gain | 道頭/119-120 | 1 = yes ; 2 = no |
II 文件打開模式
模式 | 描述 |
---|---|
"r" | 以只讀方式打開文件,該文件必須存在。 |
"r+" | 以可讀寫方式打開文件,該文件必須存在。 |
"rb" | 以只讀方式打開一個二進制文件,只允許讀數據。 |
"w" | 創建一個用於寫入的空文件。如果文件名稱與已存在的文件相同,則會刪除已有文件的內容,文件被視為一個新的空文件。 |
"w+" | 創建一個用於讀寫的空文件。 |
"wb" | 只寫打開或新建一個二進制文件,只允許寫數據。 |
"a" | 追加到一個文件;寫操作向文件末尾追加數據。如果文件不存在,則創建文件。 |
"a+" | 打開一個用於讀取和追加的文件。 |
"rw+" | 讀寫打開一個文本文件,允許讀和寫。 |