📄 segywrite.c
字号:
/* Copyright (c) Colorado School of Mines, 2006.*//* All rights reserved. *//* SEGYWRITE: $Revision: 1.47 $ ; $Date: 2006/11/07 22:58:42 $ */#ifdef SUXDR /* begin if SUXDR */#include "su_xdr.h"#else /* else if not SUXDR */#include "su.h"#include "segy.h"#include "tapebhdr.h"#endif /* end if SUXDR */#include "tapesegy.h"#include "bheader.h"/*********************** self documentation **********************/char *sdoc[] = {" "," SEGYWRITE - write an SEG-Y tape "," "," segywrite <stdin tape= "," "," Required parameters: "," tape= tape device to use (see sudoc segyread) "," "," Optional parameter: "," verbose=0 silent operation "," =1 ; echo every 'vblock' traces "," vblock=50 echo every 'vblock' traces under verbose option "," buff=1 for buffered device (9-track reel tape drive) "," =0 possibly useful for 8mm EXABYTE drive "," conv=1 =0 don't convert to IBM format "," hfile=header ebcdic card image header file "," bfile=binary binary header file "," trmin=1 first trace to write "," trmax=INT_MAX last trace to write "," endian=1 =0 for little-endian machines (PC's, DEC,etc...)"," errmax=0 allowable number of consecutive tape IO errors "," format= override value of format in binary header file "," "," Note: The header files may be created with 'segyhdrs'. "," "," "," Note: For buff=1 (default) tape is accessed with 'write', for buff=0 "," tape is accessed with fwrite. Try the default setting of buff=1 "," for all tape types. "," Caveat: may be slow on an 8mm streaming (EXABYTE) tapedrive "," Warning: segyread or segywrite to 8mm tape is fragile. Allow time "," between successive reads and writes. "," Precaution: make sure tapedrive is set to read/write variable blocksize"," tapefiles. "," "," For more information, type: sudoc <segywrite> "," ",NULL};/* * Warning: may return the error message "efclose: fclose failed" * intermittently when segyreading/segywriting to 8mm EXABYTE tape, * even if actual segyread/segywrite is successful. However, this * may indicate that your tape drive has been set to a fixed block * size. Tape drives should be set to variable block size before reading * or writing tapes in the SEG-Y format. * * Credits: * SEP: Einar Kjartansson * CWP: Jack, Brian, Chris * : John Stockwell (added EXABYTE functionality) * Notes: * Brian's subroutine, float_to_ibm, for converting IEEE floating * point to IBM floating point is NOT portable and must be * altered for non-IEEE machines. See the subroutine notes below. * * On machines where shorts are not 2 bytes and/or ints are not * 4 bytes, routines to convert SEGY 16 bit and 32 bit integers * will be required. * * The program, segyhdrs, can be used to make the ascii and binary * files required by this code. *//**************** end self doc ***********************************//* typedefs */#ifdef SUXDR /* begin if SUXDR */#if defined(_CRAYMPP) /* begin if _CRAYMPP */typedef short fourbyte;#else /* else if SUXDR but not _CRAYMPP */typedef int fourbyte;#endif /* end if _CRAYMPP */#endif /* end if SUXDR *//* subroutine prototypes */#ifdef SUXDR /* begin if SUXDR */static void float_to_ibm(fourbyte *from, fourbyte *to, int n, int endian);#else /* if not SUXDR */static void float_to_ibm(int from[], int to[], int n, int endian);static void bhed_to_tapebhed(const bhed *bhptr, tapebhed *tapebhptr); static void segy_to_tapesegy(const segy *trptr, tapesegy *tapetrptr, size_t nsegy); /* globals */tapesegy tapetr;tapebhed tapebh;#endif /* end if SUXDR *//* globals */segy tr;bhed bh;intmain(int argc, char **argv){ cwp_String tape; /* name of raw tape device */ cwp_String hfile; /* name of ebcdic header file */ cwp_String bfile; /* name of binary header file */#ifdef SUXDR /* begin SUXDR */ int j; /* counter */ FILE *headerfp; /* file pointer to header file */#else /* else if not SUXDR */ FILE *pipefp; /* file pointer for popen read */#endif /* end if SUXDR */ FILE *tapefp=NULL; /* file pointer for tape */ FILE *binaryfp; /* file pointer for bfile */ int tapefd=0; /* file descriptor for tape buff=0 */ int i; /* counter */ int ns; /* number of data samples */ size_t nsegy; /* size of whole trace in bytes */ int itr; /* current trace number */ int trmax; /* last trace to write */ int trmin; /* first trace to write */ int verbose; /* echo every ... */ int vblock; /* ... vblock traces with verbose=1 */ int buff; /* buffered or unbuffered device */ int endian; /* =0 little endian; =1 big endian */ int conv; /* =1 IBM format =0 don't convert */ int errmax; /* max consecutive tape io errors */ int errcount = 0; /* counter for tape io errors */ int format = 0; /* tape format */ cwp_Bool format_set = cwp_false; /* tape format */#ifdef SUXDR /* begin if SUXDR */#if defined(CRAY) /* begin if defined CRAY */#if defined(_CRAYMPP) /* begin if defined _CRAYMPP */ fourbyte imone = -1; /* constant for Fortran linkage */ fourbyte fns; /* for Fortran CRAYMPP linkage */#else /* CRAY but not _CRAYMPP */ int ier; /* CRAY ibmfloat error flag */ fourbyte ione = -1; /* constant for Fortran linkage */#endif /* end if _CRAYMPP */#endif /* end if SUXDR and CRAY but not _CRAYMPP */ char ebcbuf[EBCBYTES+1];/* ebcdic data buffer */ char bhbuf[BNYBYTES]; /* binary reel header buffer */ char *trbuf; /* output trace buffer */ XDR bhed_xdr, bhbuf_xdr;/* for handling binary reel header */ XDR trhd_xdr; unsigned int trstart; /* "offset" of trhd stream buffer */#else /* not Cray and not SUXDR */ char cmdbuf[BUFSIZ]; /* dd command buffer */ char ebcbuf[EBCBYTES]; /* ebcdic data buffer */#endif /* end if SUXDR */ /* Initialize */ initargs(argc, argv); requestdoc(1); /* Get parameters */ MUSTGETPARSTRING("tape", &tape); if (!getparstring("hfile", &hfile)) hfile = "header"; if (!getparstring("bfile", &bfile)) bfile = "binary"; if (!getparint ("trmin", &trmin)) trmin = 1; if (!getparint ("trmax", &trmax)) trmax = INT_MAX; if (!getparint ("verbose", &verbose)) verbose = 0; if (!getparint ("vblock", &vblock)) vblock = 50; if (!getparint ("buff", &buff)) buff = 1; if (!getparint ("conv", &conv)) conv = 1; if (!getparint ("endian", &endian)) endian = 1; if (!getparint ("errmax", &errmax)) errmax = 0; if (getparint("format", &format)) format_set = cwp_true; /* Check parameters */ if (trmin < 1 || trmax < 1 || trmax < trmin) err("bad trmin/trmax values, trmin = %d, trmax = %d", trmin, trmax); /* Get first trace early to be sure that binary file is ready */ gettr(&tr); /* Open files - first the tape */ if (buff) tapefd = eopen(tape, O_WRONLY | O_CREAT | O_TRUNC, 0666); else tapefp = efopen(tape, "w"); if (verbose) warn("tape opened successfully ");#ifdef SUXDR /* begin SUXDR */ /* Open ascii header file */ headerfp = efopen(hfile, "r"); if (verbose) warn("header file opened successfully"); /* - binary header file */ binaryfp = efopen(bfile, "r"); xdrstdio_create(&bhed_xdr,binaryfp,XDR_DECODE); xdrmem_create(&bhbuf_xdr,bhbuf,BNYBYTES,XDR_ENCODE); if (verbose) warn("binary file opened successfully"); /* Read ascii header into buffer and blank newlines & nulls */ memset(&(ebcbuf[0]),0,EBCBYTES); for(i = 0; i<EBCBYTES; i += 80) { fgets(&(ebcbuf[i]),81, headerfp); j = (int) strlen(&(ebcbuf[i])); ebcbuf[i+j] = ' '; j--; if(ebcbuf[j] == '\n') ebcbuf[j] = ' '; } /* Convert to EBCDIC */ zebc(&(ebcbuf[0]),&(ebcbuf[0]),EBCBYTES); efclose(headerfp); if (verbose) warn("header file closed successfully"); /* Write ebcdic stream to tape */ if (buff) { if (EBCBYTES != write(tapefd, ebcbuf, EBCBYTES)) { if (verbose) warn("tape write error on ebcdic header"); if (++errcount > errmax) err("exceeded maximum io errors"); } else { /* Reset counter on successful tape IO */ errcount = 0; } } else { fwrite(ebcbuf, 1, EBCBYTES, tapefp); if (ferror(tapefp)) { if (verbose) warn("tape write error on ebcdic header"); if (++errcount > errmax) err("exceeded maximum io errors"); clearerr(tapefp); } else { /* Reset counter on successful tape IO */ errcount = 0; } } /* Read binary file into bh structure */ xdrbhdrsub(&bhed_xdr, &bh); /* update requisite field */ if (format_set) bh.format = format; /* manually set format */ bh.ntrpr = 1; /* one trace per record */#else /* not SUXDR */ /* - binary header file */ binaryfp = efopen(bfile, "r"); if (verbose) warn("binary file opened successfully"); /* Open pipe to use dd to convert ascii to ebcdic */ sprintf(cmdbuf, "dd if=%s conv=ebcdic cbs=80 obs=3200", hfile); pipefp = epopen(cmdbuf, "r"); /* Read ebcdic stream from pipe into buffer */ efread(ebcbuf, 1, EBCBYTES, pipefp); /* Write ebcdic stream to tape */ if (buff) { if (EBCBYTES != write(tapefd, ebcbuf, EBCBYTES)) { if (verbose) warn("tape write error on ebcdic header"); if (++errcount > errmax) err("exceeded maximum io errors"); } else { /* Reset counter on successful tape IO */ errcount = 0; } } else { fwrite(ebcbuf, 1, EBCBYTES, tapefp); if (ferror(tapefp)) { if (verbose) warn("tape write error on ebcdic header"); if (++errcount > errmax) err("exceeded maximum io errors"); clearerr(tapefp); } else { /* Reset counter on successful tape IO */ errcount = 0; } } /* Read binary file into bh structure */ efread((char *) &bh, 1, BNYBYTES, binaryfp); if (format_set) bh.format = format; /* manually set format */ if (bh.ntrpr == 0) bh.ntrpr = 1; /* one trace per record */#endif /* end if SUXDR */ /* Compute trace size (can't use HDRBYTES here!) */ ns = bh.hns; if (!ns) err("bh.hns not set in binary header"); nsegy = ns*4 + SEGY_HDRBYTES;#ifdef SUXDR /* begin SUXDR */ /* Convert from ints to shorts */ xdrbhdrsub(&bhbuf_xdr, &bh); /* Write binary structure to tape */ if (buff) { if (BNYBYTES != write(tapefd, bhbuf, BNYBYTES)) { if (verbose) warn("tape write error on binary header"); if (++errcount > errmax) err("exceeded maximum io errors"); } else { /* Reset counter on successful tape IO */ errcount = 0; } } else { fwrite(bhbuf, 1, BNYBYTES, tapefp); if (ferror(tapefp)) { if (verbose) warn("tape write error on binary header"); if (++errcount > errmax) err("exceeded maximum io errors"); clearerr(tapefp); } else { /* Reset counter on successful tape IO */ errcount = 0; } }#else /* not SUXDR */ /* if little endian (endian=0) swap bytes of binary header */ if (endian==0) for (i = 0; i < BHED_NKEYS; ++i) swapbhval(&bh,i); /* Convert from ints/shorts to bytes */ bhed_to_tapebhed(&bh, &tapebh); /* Write binary structure to tape */ if (buff) { if (BNYBYTES != write(tapefd, (char *) &tapebh, BNYBYTES)) { if (verbose) warn("tape write error on binary header"); if (++errcount > errmax) err("exceeded maximum io errors"); } else { /* Reset counter on successful tape IO */ errcount = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -