C/C++语言读取SEGY文件笔记(一)
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+" | 读写打开一个文本文件,允许读和写。 |