#include <QtGui>
#include <segy.h>
#include <util.h>
SEGY::SEGY()
{
_F = NULL;
_ByteLineName = 90;
_LineNameLength = 10;
_ByteCDP = 1;
_ByteSP = 17;
_ByteX = 73;
_ByteY = 77;
_ByteInline = 9;
_ByteXline = 21;
_MinInline = _MaxInline = _MinXline = _MaxXline = 0;
_Corner1_X = _Corner1_Y = _Corner2_X = _Corner2_Y = _Corner3_X = _Corner3_Y = 0;
_FirstCDP = _FirstSP = _FirstX = _FirstY = 0;
_LastCDP = _LastSP = _LastX = _LastY = 0;
_CDPInc = _SPInc = 0;
_MinInline_Trace = _MaxInline_Trace = _MinXline_Trace = _MaxXline_Trace = 0;
}
bool SEGY::OpenFile(char* _Filename)
{
long long l;
if(_F)
fclose(_F);
_F = fopen64(_Filename, "rb");
if(_F)
{
fseeko64(_F,0,SEEK_END);
l = ftell(_F);
l = ftello64(_F);
fseek(_F,0,SEEK_SET);
fread(_EbcdicHeader,1,3200,_F);
fread(_BinaryHeader,1, 400,_F);
_SampleInterval = i2(_BinaryHeader, 17)/1000000.;
_TotalSamples = i2(_BinaryHeader, 21);
_DataFormat = i2(_BinaryHeader, 25);
_EndTime = _SampleInterval * _TotalSamples * 1000;
if((_SampleInterval <= 0) || (_TotalSamples <= 0) || ((_DataFormat < 1) || (_DataFormat > 6)) ) goto ERR;
if (_DataFormat == 3)
_SampleRate = 2;
else if(_DataFormat == 6)
_SampleRate = 1;
else
_SampleRate = 4;
_TRL = 240 + _TotalSamples * _SampleRate;
if(_TRL <= 240)
goto ERR;
_TotalTraces = (l-3600)/(_TRL);
return true;
ERR:
fclose(_F);
_F = NULL;
return false;
}
else
{
return false;
}
}
void SEGY::ShowEbcdicHeader(QPlainTextEdit *ptxtEbcdic)
{
if(_F==NULL) return;
unsigned char str[128];
int n;
QString qstr;
memset(str, 0, 128);
ptxtEbcdic->clear();
for(n=0; n<40; n++)
{
memcpy(str, (_EbcdicHeader + n * 80), 80);
ebasd(str, str);
qstr.sprintf("%s", str);
ptxtEbcdic->appendPlainText(qstr);
}
ptxtEbcdic->verticalScrollBar()->setValue(0);
}
void SEGY::ShowBinaryHeader(QPlainTextEdit *ptxtBinary)
{
if(_F==NULL) return;
ptxtBinary->clear();
QString qstr;
int byte = 1;
ptxtBinary->insertPlainText("Job identification number :......................" + QString::number(i4(_BinaryHeader, 1)) + "\n");
ptxtBinary->insertPlainText("Line number :...................................." + QString::number(i4(_BinaryHeader, 5)) + "\n");
ptxtBinary->insertPlainText("Reel number :...................................." + QString::number(i4(_BinaryHeader, 9)) + "\n");
ptxtBinary->insertPlainText("# data traces per record :......................." + QString::number(i2(_BinaryHeader, 13)) + "\n");
ptxtBinary->insertPlainText("# aux traces per record :......................." + QString::number(i2(_BinaryHeader, 15)) + "\n");
ptxtBinary->insertPlainText("Sample interval (microseconds) for reel :........" + QString::number(i2(_BinaryHeader, 17)) + "\n");
ptxtBinary->insertPlainText("Sample interval (microseconds) for field :......." + QString::number(i2(_BinaryHeader, 19)) + "\n");
ptxtBinary->insertPlainText("Number samples per data trace for reel :........." + QString::number(i2(_BinaryHeader, 21)) + "\n");
ptxtBinary->insertPlainText("Number samples per datat trace for field :......." + QString::number(i2(_BinaryHeader, 23)) + "\n");
ptxtBinary->insertPlainText("Data sample format code :........................" + QString::number(i2(_BinaryHeader, 25)) + "\n");
ptxtBinary->insertPlainText("CDP fold :......................................." + QString::number(i2(_BinaryHeader, 27)) + "\n");
ptxtBinary->insertPlainText("Trace sorting code :............................." + QString::number(i2(_BinaryHeader, 29)) + "\n");
ptxtBinary->insertPlainText("Vertical sum code :.............................." + QString::number(i2(_BinaryHeader, 31)) + "\n");
ptxtBinary->insertPlainText("Sweep frequency at start :......................." + QString::number(i2(_BinaryHeader, 33)) + "\n");
ptxtBinary->insertPlainText("Sweep frequency at end :........................." + QString::number(i2(_BinaryHeader, 35)) + "\n");
ptxtBinary->insertPlainText("Sweep length (milliseconds) :...................." + QString::number(i2(_BinaryHeader, 37)) + "\n");
ptxtBinary->insertPlainText("Sweep type code :................................" + QString::number(i2(_BinaryHeader, 39)) + "\n");
ptxtBinary->insertPlainText("Trace number of sweep channel :.................." + QString::number(i2(_BinaryHeader, 41)) + "\n");
ptxtBinary->insertPlainText("Sweep trace taper length at start (ms) :........." + QString::number(i2(_BinaryHeader, 43)) + "\n");
ptxtBinary->insertPlainText("Sweep trace taper length at end (ms) :........." + QString::number(i2(_BinaryHeader, 45)) + "\n");
ptxtBinary->insertPlainText("Taper type :....................................." + QString::number(i2(_BinaryHeader, 47)) + "\n");
ptxtBinary->insertPlainText("Corellated data traces :........................." + QString::number(i2(_BinaryHeader, 49)) + "\n");
ptxtBinary->insertPlainText("Binary gain recoverd :..........................." + QString::number(i2(_BinaryHeader, 51)) + "\n");
ptxtBinary->insertPlainText("Amplitude recovery method :......................" + QString::number(i2(_BinaryHeader, 53)) + "\n");
ptxtBinary->insertPlainText("Measuriment system (1-m / 2-feet) :.............." + QString::number(i2(_BinaryHeader, 55)) + "\n");
ptxtBinary->insertPlainText("Impulse signal :................................." + QString::number(i2(_BinaryHeader, 57)) + "\n");
ptxtBinary->insertPlainText("Vibratory polarity code :........................" + QString::number(i2(_BinaryHeader, 59)) + "\n\n");
for(int i=1; i<=3; i++)
{
qstr.sprintf("%3d %-15d\t",byte,i4(_BinaryHeader,byte));
ptxtBinary->insertPlainText(qstr);
byte += 4;
}
for(int i=1; i<=15; i++)
{
ptxtBinary->insertPlainText("\n");
for(int j=1; j<=3; j++)
{
qstr.sprintf("%3d %-15d\t",byte,i2(_BinaryHeader,byte));
ptxtBinary->insertPlainText(qstr);
byte += 2;
}
}
}
float SEGY::GetSample(int idx)
{
float smp;
int* xmp = (int*)&smp;
float* dat = (float*)(_INPTRC+240);
short* int2ptr = (short*) dat;
int* int4ptr = (int*) dat;
signed char* int1ptr = (signed char*) dat;
if(_DataFormat==1)
{
smp = dat[idx];
ibm2ieee(&smp,1);
}
else if(_DataFormat==2)
{
smp = swapi4(int4ptr[idx]);
}
else if(_DataFormat==3)
{
smp = swapi2(int2ptr[idx]);
}
else if(_DataFormat==4)
{
smp = dat[idx];
}
else if(_DataFormat==5)
{
*xmp = swapi4(int4ptr[idx]);
}
else if(_DataFormat==6)
{
smp = int1ptr[idx];
}
else
smp = 0;
return smp;
}
bool SEGY::ReadTrace(long long n)
{
long long l;
if(_F == NULL) return false;
if(n == 0) return false;
if(n < 1 || n > _TotalTraces)
{
printf("Bad Trace Number");
return false;
}
fseeko64(_F, 3600+(n-1)*_TRL, SEEK_SET);
l = fread(_INPTRC, 1, _TRL, _F );
_CurrentTrace = n;
for(n=0; n < _TotalSamples; n++)
_Data[n] = GetSample(n);
return true;
}
void SEGY::ShowTraceHeader(long long _TraceNo, QPlainTextEdit *ptxtTrace)
{
ptxtTrace->clear();
if(!ReadTrace(_TraceNo)) return;
if(_F==NULL) return;
ptxtTrace->insertPlainText("Trace # " +