📄 suwrite.c
字号:
/* Copyright (c) Colorado School of Mines, 2006.*//* All rights reserved. *//***************************************************************************SUWRITE: write SU output stream***************************************************************************code by J.E. Anderson, Mobil/CWP (June, 1993)modified by J.E. Anderson, April, 1995, updated trace header words and internal documentation***************************************************************************/#include "cwpsegy.h"#include "cpromax.h"#include <pwd.h>#if defined SOLARIS#elseextern FILE *popen(char *cmnd,char *type);#endifvoid init_suwrite_(int *lensav, int *itooltype);void exec_suwrite_(float *trace, int *ihead, float *rhead);static void suwrite_putsu(FILE *fp, cwpsegy *tp, long *itr, int *nsegy);static FILE *suwrite_open(int ichoose, char *cmnd, char *datafile,char *cmndfile);static void suwrite_hmap_2su(cwpsegy *tr, int *h);static void suwrite_hdrinit(int *fmt, int *ind);static void suwrite_geth(int *h,int *fmt, int *ind, int *ihead, float *rhead);static char *suwrite_stringPrep(char *s);BEGINPARMSFILE *fp;int ichoose;char *cmnd;char *cmndfile;char *datafile;int input_numsmp;int input_samprat;long first;cwpsegy *tr;long itr;int nsegy;int *fmt;int *ind;int *h;ENDPARMS(parms)void init_suwrite_(int *lensav, int *itooltype)/***************************************************************************initialization phase for SUWRITE***************************************************************************/{ char *temp; exParGetInt("CHOOSE1",&parms->ichoose); exParGetString("CMND",&parms->cmnd); exParGetString("CMNDFILE",&temp); parms->cmndfile = suwrite_stringPrep(temp); exParGetString("DATAFILE",&temp); parms->datafile = suwrite_stringPrep(temp); parms->input_numsmp = globalRuntime->numsmp; parms->input_samprat = globalRuntime->samprat; parms->tr = (cwpsegy *)malloc(sizeof(cwpsegy)); parms->fmt = (int *)malloc(120*sizeof(int)); parms->ind = (int *)malloc(120*sizeof(int)); parms->h = (int *)malloc(120*sizeof(int)); parms->first = 0; parms->itr = 0; suwrite_hdrinit(parms->fmt,parms->ind); *lensav = NPARMS(parms); *itooltype = ISIMPLE; return;}void exec_suwrite_(float *trace, int *ihead, float *rhead)/***************************************************************************execution phase for SUWRITE***************************************************************************/{ int j; cwpsegy *tr; if (globalRuntime->cleanup) { if(parms->ichoose==0) { j=pclose(parms->fp); }else{ j=fclose(parms->fp); } (void) free(parms->h); (void) free(parms->ind); (void) free(parms->fmt); (void) free(parms->tr); return; } tr=parms->tr; if(parms->first==0) parms->fp=suwrite_open(parms->ichoose,parms->cmnd, parms->datafile,parms->cmndfile); parms->first++; suwrite_geth(parms->h,parms->fmt, parms->ind,ihead,rhead); suwrite_hmap_2su(tr, parms->h); tr->tracl=parms->first; tr->ns=parms->input_numsmp; tr->dt=1000*parms->input_samprat; tr->d2=1; for(j=0;j<parms->input_numsmp;j++) tr->data[j]=trace[j]; suwrite_putsu(parms->fp,tr,&parms->itr,&parms->nsegy); return;}FILE *suwrite_open(int ichoose,char *cmnd,char *datafile,char *cmndfile)/**************************************************************************************return a FILE pointer to output stream whether from Unix command, file, or command fileint ichoose 0 write to output pipe 1 write to output data file 2 write to output command file (input to suget)char *cmnd command setting up output pipechar *datafile output data filechar *cmndfile name of command file piping output on file descriptor given for argument 1 (usually for use with suget)************************************************************************/{ FILE *fp; int pfd[2],j,k; char fdstr[10],prog[50]; if(ichoose==0) { fprintf(stderr,"\nSUWRITE Pipe command:\n %s\n",cmnd); fp=popen(cmnd,"w"); if(fp==NULL) exErrFatal("suwrite_open: Could not open output pipe to %s\n",cmnd); return(fp); } if(ichoose==1) { fp=fopen(datafile,"w"); if(fp==NULL) exErrFatal("suwrite_open: Could not open output file %s\n",datafile); return (fp); } j=0; k=-1; while(cmndfile[j]!='\0'){ if(cmndfile[j]=='/') k=j; j++; } k++; j=0; do{ prog[j]=cmndfile[j+k]; j++; }while(cmndfile[j+k]!='\0'); if(pipe(pfd)==-1) { fprintf(stderr,"suwrite_open: Could not open pipe.\n"); return(NULL); } switch(fork()){ case -1: fprintf(stderr,"suwrite_open: Fork failed to create child.\n"); return(NULL); case 0: if(close(pfd[1])==-1) fprintf(stderr,"suwrite_open: Non-fatal close error in child.\n");/* fprintf(stderr,"suwrite_open: File descriptor passed from parent = %d\n",pfd[0]); */ sprintf(fdstr,"%d",pfd[0]); execlp(cmndfile,prog,fdstr,NULL); fprintf(stderr,"suwrite_open: Failed to execute command file %s. Path = %s.\n", prog,cmndfile); return(NULL); } if(close(pfd[0])==-1) fprintf(stderr,"suwrite_open: Non-fatal close error in parent.\n");/* fprintf(stderr,"suwrite_open: Parent will write on file descriptor %d\n",pfd[1]); */ fp=fdopen(pfd[1],"w"); return(fp);}void suwrite_hdrinit(int *fmt, int *ind)/************************************************************************initialize trace header map from ProMAX to SU in terms of format and indecies************************************************************************/{ int j; j=0; while( (sumap[j].suoffs>-1) && (sumap[j].suoffs<240) ) { if(hdrExists(sumap[j].pkey)) { fmt[j]=hdrFormat(sumap[j].pkey); ind[j]=hdrIndex(sumap[j].pkey); fprintf(stderr," output SU hdr=%s input ProMAX hdr=%s\n", sumap[j].sukey,sumap[j].pkey); }else{ fmt[j]=HDRUNDEFINED; ind[j]=-1; } j++; } return;}void suwrite_geth(int *h, int *fmt, int *ind, int *ihead, float *rhead)/************************************************************************extract ProMAX trace header values into h array************************************************************************/{ int j; j=0; while( (sumap[j].suoffs>-1) && (sumap[j].suoffs<240) ) { if(fmt[j]==HDRINT) { h[j]=ihead[ind[j]]; }else if(fmt[j]==HDRFLOAT) { h[j]=rhead[ind[j]]; }else{ h[j]=0; } j++; } return; }void suwrite_hmap_2su(cwpsegy *tr, int *h)/************************************************************************move header values into SU header from h array************************************************************************/{tr->tracl = h[0];tr->tracr = h[1];tr->fldr = h[2];tr->tracf = h[3];tr->ep = h[4];tr->cdp = h[5];tr->cdpt = h[6];tr->trid = h[7];tr->nvs = h[8];tr->nhs = h[9];tr->duse = h[10];tr->offset = h[11];tr->gelev = h[12];tr->selev = h[13];tr->sdepth = h[14];tr->gdel = h[15];tr->sdel = h[16];tr->swdep = h[17];tr->gwdep = h[18];tr->scalel = 1;tr->scalco = 1;tr->sx = h[21];tr->sy = h[22];tr->gx = h[23];tr->gy = h[24];tr->counit = h[25];tr->wevel = h[26];tr->swevel = h[27];tr->sut = h[28];tr->gut = h[29];tr->sstat = h[30];tr->gstat = h[31];tr->tstat = h[32];tr->laga = h[33];tr->lagb = h[34];tr->delrt = h[35];tr->muts = h[36];tr->mute = h[37];tr->ns = globalRuntime->numsmp;tr->dt = 1000*globalRuntime->samprat;tr->gain = h[40];tr->igc = h[41];tr->igi = h[42];tr->corr = h[43];tr->sfs = h[44];tr->sfe = h[45];tr->slen = h[46];tr->styp = h[47];tr->stas = h[48];tr->stae = h[49];tr->tatyp = h[50];tr->afilf = h[51];tr->afils = h[52];tr->nofilf = h[53];tr->nofils = h[54];tr->lcf = h[55];tr->hcf = h[56];tr->lcs = h[57];tr->hcs = h[58];tr->year = h[59];tr->day = h[60];tr->hour = h[61];tr->minute = h[62];tr->sec = h[63];tr->timbas = h[64];tr->trwf = h[65];tr->grnors = h[66];tr->grnofr = h[67];tr->grnlof = h[68];tr->gaps = h[69];tr->otrav = h[70];tr->d1 = globalRuntime->samprat;tr->f1 = 0;tr->d2 = 1;tr->f2 = 0;tr->ungpow = h[75];tr->unscale= h[76];tr->mark = h[77];tr->unass[0] = h[78];tr->unass[1] = h[79];tr->unass[2] = h[80];tr->unass[3] = h[81];tr->unass[4] = h[82];tr->unass[5] = h[83];tr->unass[6] = h[84];tr->unass[7] = h[85];tr->unass[8] = h[86];tr->unass[9] = h[87];tr->unass[10] = h[88];tr->unass[11] = h[89];tr->unass[12] = h[90];tr->unass[13] = h[91];tr->unass[14] = h[92];tr->unass[15] = h[93];tr->unass[16] = h[94];return;}void suwrite_putsu(FILE *fp, cwpsegy *tp, long *itr, int *nsegy)/************************************************************************suwrite_putsu - put a segy trace on a file by descriptor (based on fputtr but made reentrant)************************************************************************/{ if (*itr== 0) *nsegy = HDRBYTES + sizeof(float) * tp->ns; (void) fwrite(tp, 1, *nsegy, fp); *itr++; return;}char *suwrite_stringPrep(char *s)/*************************************************************************************returns properly prepared version of user-entered string for decoding a file name(allows user to get away with hitting a return in field for file name or an extra space)char *s input string**************************************************************************************/{ int n,j,c; char *ss;/*********************************** left justify string***********************************/ n=strlen(s); for(j=0;j<n;j++) { c=s[j]; if(!isspace(c)) break; } n=n-j; ss=(char *)malloc( (n+1)*sizeof(char)); for(c=0;c<n;c++) ss[c]=s[c+j]; ss[n]='\0'; /**************************************************** convert any remaining white space to nulls****************************************************/ for(j=0;j<n;j++) { c=ss[j]; if(isspace(c)) ss[j]='\0'; } return (ss);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -