Reputation: 589
I have a binary file with a 3600 byte header, 13483 traces (columns) each with a 240 byte header. I want to skip the headers and read the data values into a matrix.
I'm able to get some values out of the file but the seismicDataNH[50][40] to seismicDataNH[50][54] should be [13, 17, 12, 5, 19, 51, 29, -118, -127, -127, -50, 126, 126, 126, -32] which is not what I get.
I'm not sure I understand fread() correctly, does it read the file as one long row of values, or as multiple line? I'm assuming one long row, maybe that's why it doesn't work.
Here's the code I wrote to read the file:
#include <iostream>
using namespace std;
#define N_SAMP 1990
#define M_TR 13483
char tempArray [N_SAMP*M_TR];
char seismicData[1990][13483];
char seismicDataNH[1750][13483];
int main()
{
FILE*seismicFile;
seismicFile = fopen("NVGT-88-06.sgy","rb");
if (seismicFile!=NULL)
{
fseek(seismicFile, 3600*sizeof(char), SEEK_CUR);
fread(tempArray, sizeof(char), N_SAMP*M_TR, seismicFile);
puts("\n\nRead File successfully");
int c = 0;
for (int in=0; in<N_SAMP; in++)
{
for (int im=0; im<M_TR; im++)
{
seismicData[in][im] = tempArray[c];
c++;
}
}
puts("\nStored in matrix");
// Make matrix values without header values
for (int in=240; in < N_SAMP; in++)
{
for(int im=0; im < M_TR; im++)
{
seismicDataNH[in-240][im] = seismicData[in][im];
}
}
puts("Removed header");
puts("Test values: \n");
for (int it = 40; it<55; it++)
{
printf("%d\n", seismicDataNH[50][it]);
}
fclose(seismicFile);
}
return 0;
}
and here's the data file (.sgy) if someone wants to have a look at it: https://www.dropbox.com/s/y8aa99yqhfyacc8/NVGT-88-06.sgy?dl=0
Upvotes: 0
Views: 352
Reputation: 16243
From your description, there are 13483 consecutive blocks of 1990 bytes (including 240 for the header).
That means you have the for loop nesting and array indexing the wrong way around.
Change the array definitions to :
char seismicData[M_TR][N_SAMP];
char seismicDataNH[M_TR][N_SAMP-240];
And the two nested for loops to :
for (int im=0; im<M_TR; im++)
{
for (int in=0; in<N_SAMP; in++)
{
seismicData[im][in] = tempArray[c];
c++;
}
}
resp. :
for(int im=0; im < M_TR; im++)
{
for (int in=240; in < N_SAMP; in++)
{
seismicDataNH[im][in-240] = seismicData[im][in];
}
}
Keep the final for loop (that prints the data) as it is.
That should give you the expected output (it does for me).
Upvotes: 2