📄 cvt2segd.c
字号:
in_seisnet = fopen(inname_ptr[ifile], "rb"); if(in_seisnet == ((FILE *) NULL)) { perror(inname_ptr[ifile]); fprintf(stderr,"Unable to open input filename \"%s\"\n", inname_ptr[ifile]); fprintf(stderr,"Exiting %s\n", argv[0]); exit(EXIT_FAILURE); } /* * Determine the size of the data object. */ if( 0 != fseeko(in_seisnet, (off_t) 0, SEEK_END)) { perror(inname_ptr[ifile]); fprintf(stderr,"Unable to seek to end of input file.\n"); fprintf(stderr,"Will try to continue.\n"); } nbytes = ftello(in_seisnet); if(nbytes < 0) { perror(inname_ptr[ifile]); fprintf(stderr,"Unable to get input filesize.\n"); fprintf(stderr,"Will try to continue.\n"); } if( 0 != fseeko(in_seisnet, (off_t) 0, SEEK_SET)) { perror(inname_ptr[ifile]); fprintf(stderr,"Unable to rewind input file.\n"); fprintf(stderr,"Will try to continue.\n"); } /* * Read the SEISFILEHEADER structure. */ errno = 0; nread = fread((void *)&sh, sizeof(char), sizeof(SEISFILEHEADER), in_seisnet); if(nread < sizeof(SEISFILEHEADER)) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SEISFILEHEADER from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } /* * Make sure that we have opened a valid data object. */ if(sh.ID != SEISDATAOBJECTID) { fprintf(stderr, "File \"%s\" is apparently not a valid SeisNet object.\n", inname_ptr[ifile]); fprintf(stderr,"Skipping.\n"); (void) fclose(in_seisnet); continue; } /* * Read the SEISDATAPARMS structure */ errno = 0; nread = fread((void *)&sd, sizeof(char), sizeof(SEISDATAPARMS), in_seisnet); if(nread < sizeof(SEISDATAPARMS)) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SEISDATAPARMS from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } /* * Read the orginal SEG-D record header */ if(big_endian) u_swapends4((void *) &sh.nseghead, 1); if(sh.nseghead > 4) { errno = 0; seghead = (unsigned char *)malloc(sh.nseghead); if(seghead == ((unsigned char *) NULL)) { perror("malloc"); fprintf(stderr,"Unable to malloc %ul bytes. Exiting.\n", (unsigned long) sh.nseghead); exit(EXIT_FAILURE); } errno = 0; nread = fread((void *)seghead, sizeof(char), (size_t) sh.nseghead, in_seisnet); if(nread < (size_t) sh.nseghead) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SEGD file header from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } } else { fprintf(stderr,"SeisNet file \"%s\" doesn't contain a SEG-D file header ... skipping\n", inname_ptr[ifile]); (void) fclose(in_seisnet); continue; } /* change data format to IEEE float (8058) in SEG-D file header */ seghead[2] = 8*16; seghead[3] = 5*16 + 8; /* * Read in the location of the trace data */ if(big_endian) u_swapends4((void *) &sh.ntraces, 1); ntraces = sh.ntraces; fprintf(stderr,"Total number of traces (data + aux) in dataset is: %d\n", ntraces); nb = ntraces * sizeof(SEISTRACELOCATION); errno = 0; pl = (SEISTRACELOCATION *)malloc(nb); if(pl == ((SEISTRACELOCATION *) NULL)) { perror("malloc"); fprintf(stderr,"Unable to malloc %ul bytes. Exiting.\n", (unsigned long) nb); exit(EXIT_FAILURE); } errno = 0; nread = fread((void *)pl, sizeof(char), (size_t) nb, in_seisnet); if(nread < nb) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SEISTRACELOCATION from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } /* * Read the trace location information. Also, assign pointers for the SeisNet * trace headers, the orginal SEG-D trace headers, and the seismic traces. */ errno = 0; td = (TRACEDATA *)malloc(ntraces * sizeof(TRACEDATA)); if(td == ((TRACEDATA *) NULL)) { perror("malloc"); fprintf(stderr,"Unable to malloc %ul bytes. Exiting.\n", (unsigned long) (ntraces*sizeof(TRACEDATA))); exit(EXIT_FAILURE); } if(big_endian) u_swapends4((void *) &sd.m_nsegtrchead, 1); /* Read the trace headers and seismic data */ for(i=0; i<ntraces; i++) { /* Seek to correct location in the file. */ if(big_endian) u_swapends4((void *) &((pl+i)->trcoffset), 1); if(0 != fseeko(in_seisnet, (off_t) ((pl+i)->trcoffset), SEEK_SET)) { perror(inname_ptr[ifile]); perror("Exiting on seek failure.\n"); exit(EXIT_FAILURE); } /* Allocate memory to hold the SeisNet trace header. */ errno = 0; (td+i)->thdr = (SEISTRACEHEADER *)malloc(sizeof(SEISTRACEHEADER)); if( ((td+i)->thdr) == ((SEISTRACEHEADER *) NULL)) { perror("malloc"); fprintf(stderr,"Unable to malloc %ul bytes. Exiting.\n", (unsigned long) (sizeof(SEISTRACEHEADER))); exit(EXIT_FAILURE); } /* Assign a local pointer to the trace header for less typing below. */ thdr = (td+i)->thdr; /* Read the SeisNet trace header. */ errno = 0; nread = fread((void *)thdr, sizeof(char), sizeof(SEISTRACEHEADER), in_seisnet); if(nread < sizeof(SEISTRACEHEADER)) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SEISTRACEHEADER from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } /* Allocate memory to hold the orginal SEG-D trace header. */ (td+i)->segd = (unsigned char *)malloc(sd.m_nsegtrchead); if( ((td+i)->segd) == ((unsigned char *) NULL)) { perror("malloc"); fprintf(stderr,"Unable to malloc %ul bytes. Exiting.\n", (unsigned long) (sd.m_nsegtrchead)); exit(EXIT_FAILURE); } /* Read the orginal SEG-D trace header. */ errno = 0; nread = fread((void *)(td+i)->segd, sizeof(char), (size_t) sd.m_nsegtrchead, in_seisnet); if(nread < (size_t) sd.m_nsegtrchead) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SEGD trace header from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } /* Allocate memory to hold the seismic trace. */ if(big_endian) u_swapends4((void *) &(thdr->m_nsamp), 1); (td+i)->trace = (float *)malloc(sizeof(float) * thdr->m_nsamp); if( ((td+i)->trace) == ((float *) NULL)) { perror("malloc"); fprintf(stderr,"Unable to malloc %ul bytes. Exiting.\n", (unsigned long) (thdr->m_nsamp*sizeof(float))); exit(EXIT_FAILURE); } /* Read the seismic trace. */ errno = 0; nread = fread((void *)(td+i)->trace, sizeof(char) , sizeof(float) * thdr->m_nsamp, in_seisnet); if(nread < (size_t) (sizeof(float) * thdr->m_nsamp)) { perror(inname_ptr[ifile]); fprintf(stderr,"%s read of SeisNet trace data from \"%s\"\n", (nread < 0) ? "Bad" : "Short", inname_ptr[ifile]); fprintf(stderr,"Exiting.\n"); exit(EXIT_FAILURE); } /* make IEEE floats big-endian for output */ u_swapends4((void *) (td+i)->trace, thdr->m_nsamp); } /* Close the file so that another process can get to it. */ (void) fclose(in_seisnet); /* Write out SEG-D file header in ProMAX TOD format */ writeTODrec(out_segd, segd_outname, seghead, (size_t) sh.nseghead); /* write out SEG-D traces in ProMAX TOD format */ for(i=0; i<ntraces; i++) { /* Assign a local pointer to the SeisNet trace header. */ thdr = (td+i)->thdr; /* Assign a local pointer to the orginal SEG-D trace header. */ segd = (td+i)->segd; /* Assign a local pointer to point to the trace data. Remember that * the C language is zero-based. That is, the first trace has an index * of zero. */ trace = (td+i)->trace; /* write TOD prefix */ writeTODcount(out_segd, segd_outname, sd.m_nsegtrchead + thdr->m_nsamp*sizeof(float)); writeTODdata(out_segd, segd_outname, segd, (size_t) sd.m_nsegtrchead); writeTODdata(out_segd, segd_outname, trace, (size_t) thdr->m_nsamp*sizeof(float)); writeTODcount(out_segd, segd_outname, sd.m_nsegtrchead + thdr->m_nsamp*sizeof(float)); } /* write TOD file mark */ writeTODcount(out_segd, segd_outname, (size_t) 0); /* Free all the memory that was allocated above */ for(i=0; i<ntraces; i++) { free((void *) ((td+i)->thdr)); free((void *) ((td+i)->segd)); free((void *) ((td+i)->trace)); } free((void *) seghead); free((void *) pl); free((void *) td); } /* end of input file loop */ /* Exit the program. */ return EXIT_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -