outseis.c 9.84 KB
Newer Older
Simone Butzer's avatar
Simone Butzer committed
1 2 3 4
/*------------------------------------------------------------------------
 * Copyright (C) 2015 For the list of authors, see file AUTHORS.
 *
 * This file is part of IFOS3D.
5
 *
Simone Butzer's avatar
Simone Butzer committed
6 7 8
 * IFOS3D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
9
 *
Simone Butzer's avatar
Simone Butzer committed
10 11 12 13
 * IFOS3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
Simone Butzer's avatar
Simone Butzer committed
15
 * You should have received a copy of the GNU General Public License
16
 * along with IFOS3D. See file COPYING and/or
Simone Butzer's avatar
Simone Butzer committed
17 18 19 20
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
21
 *   Write seismograms to disk
Simone Butzer's avatar
Simone Butzer committed
22 23 24 25 26 27 28 29 30
 *  ----------------------------------------------------------------------*/

#include "fd.h"
#include "segy.h"

/* ****************************  UNDER CONSTRUCTION !!!  ***************************** */


void  outseis(FILE *fp, FILE *fpdata, int comp, float **section,
31 32
              int **recpos, int **recpos_loc, int ntr, float **srcpos,
              int nsrc, int ns, int seis_form) {
Simone Butzer's avatar
Simone Butzer committed
33
	/* declaration of extern variables */
34
	extern int NDT;
Simone Butzer's avatar
Simone Butzer committed
35
	extern float  DX, DY, DZ, TIME, DT, REFREC[4];
36 37 38

	extern int LITTLEBIG;

Simone Butzer's avatar
Simone Butzer committed
39 40 41 42 43 44 45 46
	/* declaration of local variables */
	int i,j, * pint;
	segy tr;
	int tracl ;
	float xr, yr, zr, y, z, scalefac, tfloat;  /*x*/
	float XS=0.0, YS=0.0, ZS=0.0;
	const float scale=3.0;
	
47 48 49

	if (nsrc==1) {
		/* only if one source position is specified in SOURCE_FILE,
Simone Butzer's avatar
Simone Butzer committed
50
			source coordinates are written into trace header fields */
51 52
		XS=srcpos[1][1];
		YS=srcpos[2][1];
Simone Butzer's avatar
Simone Butzer committed
53
		ZS=srcpos[3][1];
54 55
	}

Simone Butzer's avatar
Simone Butzer committed
56
	scalefac=pow(10.0,scale);
57 58

	switch (seis_form) {
Simone Butzer's avatar
Simone Butzer committed
59
	case 0 :
60 61 62 63 64 65 66 67 68 69
	case 1 : /* SU ~ (IEEE)  */

		/* fprintf(stderr,"BEEN HERE !!!\n"); */
		for (tracl=1; tracl<=ntr; tracl++) {
			xr=recpos[1][recpos_loc[4][tracl]]*DX;
			yr=recpos[2][recpos_loc[4][tracl]]*DY;
			zr=recpos[3][recpos_loc[4][tracl]]*DZ;
			/*x=xr-REFREC[1];*/
			y=yr-REFREC[2];
			z=zr-REFREC[3];
Simone Butzer's avatar
Simone Butzer committed
70 71
			tr.tracl=recpos_loc[4][tracl];      /* trace sequence number within line */
			tr.ep=comp;
72
			//tr.cdp=recpos_loc[4][tracl];
Simone Butzer's avatar
Simone Butzer committed
73 74
			tr.trid=1;           /* trace identification code: 1=seismic*/
			tr.offset=iround(sqrt((XS-xr)*(XS-xr)
75 76
			                      +(YS-yr)*(YS-yr)
			                      +(ZS-zr)*(ZS-zr))*scalefac);
Simone Butzer's avatar
Simone Butzer committed
77 78 79
			tr.gelev=iround(yr*scalefac);
			tr.sdepth=iround(YS*scalefac);   /* source depth (positive) */

80 81 82
			/* angle between receiver position and reference point
			   (sperical coordinate system: used for tunnel geometry) */
			tr.gdel=iround(atan2(-y,z)*180.0*scalefac/PI);
Simone Butzer's avatar
Simone Butzer committed
83 84 85 86 87 88 89
			tr.gwdep=iround(sqrt(z*z+y*y)*scalefac);

			tr.scalel=(short)(-scale);
			tr.scalco=(short)(-scale);
			tr.sx=iround(XS*scalefac);  /* X source coordinate */
			tr.sy=iround(ZS*scalefac);  /* Z source coordinate */

90
			/* group coordinates */
Simone Butzer's avatar
Simone Butzer committed
91
			tr.gx=iround(xr*scalefac);
92
			tr.gy=iround(zr*scalefac);
Simone Butzer's avatar
Simone Butzer committed
93 94 95 96 97


			tr.ns=(unsigned short)ns; /* number of samples in this trace */
			tr.dt=(unsigned short)iround(((float)NDT*DT)*1.0e6); /* sample interval in micro-seconds */
			tr.d1=(float)(TIME/ns);        /* sample spacing for non-seismic data */
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
			tr.d1=0;
			tr.tracr=0	;	/* trace sequence number within reel */

			tr.fldr=0       ;	/* field record number */

			tr.tracf=0      ;	/* trace number within field record */

			tr.ep=0         ;	/* energy source point number */

			tr.cdpt=0       ;	/* trace number within CDP ensemble */


			tr.nvs=0       	;   /* number of vertically summed traces (see vscode
			   			in bhed structure) */

			tr.nhs=0       	;   /* number of horizontally summed traces (see vscode
			  			in bhed structure) */

			tr.duse=0     	;   /* data use:
						1 = production
						2 = test */

			tr.gdel=0      	; /* datum elevation at receiver group */

			tr.sdel=0      	; /* datum elevation at source */

			tr.gwdep=0     	; /* water depth at receiver group */

			tr.counit=0    	;   /* coordinate units code:
						for previous four entries
						1 = length (meters or feet)
						2 = seconds of arc (in this case, the
						X values are longitude and the Y values
						are latitude, a positive value designates
						the number of seconds east of Greenwich
						or north of the equator */

			tr.wevel=0     ;	/* weathering velocity */

			tr.swevel=0    ;	/* subweathering velocity */

			tr.sut=0       ;	/* uphole time at source */

			tr.gut=0       ;	/* uphole time at receiver group */

			tr.sstat=0     ;	/* source static correction */

			tr.gstat=0     ;	/* group static correction */

			tr.tstat=0     ;	/* total static applied */

			tr.laga=0      ; /* lag time A, time in ms between end of 240-
			   			byte trace identification header and time
			   			break, positive if time break occurs after
			   			end of header, time break is defined as
			   			the initiation pulse which maybe recorded
			   			on an auxiliary trace or as otherwise
			   			specified by the recording system */

			tr.lagb=0     	; /* lag time B, time in ms between the time break
			   			and the initiation time of the energy source,
			   			may be positive or negative */

			tr.delrt=0     	; /* delay recording time, time in ms between
			   			initiation time of energy source and time
			   			when recording of data samples begins
			   			(for deep water work if recording does not
			   			start at zero time) */

			tr.muts=0      ; /* mute time--start */

			tr.mute=0      ; /* mute time--end */

			tr.gain=0      ; /* gain type of field instruments code:
						1 = fixed
						2 = binary
						3 = floating point
						4 ---- N = optional use */

			tr.igc=0       ; /* instrument gain constant */

			tr.igi=0       ; /* instrument early or initial gain */

			tr.corr=0      ; /* correlated:
						1 = no
						2 = yes */

			tr.sfs=0       ; /* sweep frequency at start */

			tr.sfe=0       ; /* sweep frequency at end */

			tr.slen=0      ; /* sweep length in ms */

			tr.styp=0      ; /* sweep type code:
						1 = linear
						2 = cos-squared
						3 = other */

			tr.stas=0      	; /* sweep trace length at start in ms */

			tr.stae=0      	; /* sweep trace length at end in ms */

			tr.tatyp=0     	; /* taper type: 1=linear, 2=cos^2, 3=other */

			tr.afilf=0     	; /* alias filter frequency if used */

			tr.afils=0     	; /* alias filter slope */

			tr.nofilf=0    	; /* notch filter frequency if used */

			tr.nofils=0   	; /* notch filter slope */

			tr.lcf=0      	; /* low cut frequency if used */

			tr.hcf=0      	; /* high cut frequncy if used */

			tr.lcs=0       	; /* low cut slope */

			tr.hcs=0       	; /* high cut slope */

			tr.year=0      	; /* year data recorded */

			tr.day=0       	; /* day of year */

			tr.hour=0     	; /* hour of day (24 hour clock) */

			tr.minute=0    	; /* minute of hour */

			tr.sec=0       	; /* second of minute */

			tr.timbas=0    	; /* time basis code:
						1 = local
						2 = GMT
						3 = other */

			tr.trwf=0      	; /* trace weighting factor, defined as 1/2^N
			   			volts for the least sigificant bit */

			tr.grnors=0   	; /* geophone group number of roll switch
			   			position one */

			tr.grnofr=0    	; /* geophone group number of trace one within
			   			original field record */

			tr.grnlof=0    	; /* geophone group number of last trace within
			   			original field record */

			tr.gaps=0      	;  /* gap size (total number of groups dropped) */

			tr.otrav=0     	;  /* overtravel taper code:
						1 = down (or behind)
						2 = up (or ahead) */

			/* local assignments */

			tr.f1=0.0;	/* first sample location for non-seismic data */

			tr.d2=0.0;	/* sample spacing between traces */

			tr.f2=0.0;	/* first trace location */

			tr.ungpow=0.0;	/* negative of power used for dynamic
			   			range compression */

			tr.unscale=0.0;	/* reciprocal of scaling factor to normalize
			   			range */
			tr.ntr=0      ;   /* number of traces */

			tr.mark=0     ;

			for (j=1; j<=ns; j++) {
				tr.data[j]=section[tracl][j];
			}

Simone Butzer's avatar
Simone Butzer committed
272
			fwrite(&tr,240,1,fpdata);
273
			fwrite(&tr.data[1],4,ns,fpdata);
Simone Butzer's avatar
Simone Butzer committed
274
		}
275

Simone Butzer's avatar
Simone Butzer committed
276
		break;
277 278 279 280 281 282 283 284 285 286

	case 2 :


		for (j=1; j<=ns; j++) {     /*ASCII ONE COLUMN PER TRACE */
			for (i=1; i<=ntr; i++) {
				fprintf(fpdata,"%e\t", section[i][j]);
			}

			fprintf(fpdata,"\n");
Simone Butzer's avatar
Simone Butzer committed
287
		}
288 289 290



Simone Butzer's avatar
Simone Butzer committed
291 292 293
		break;

	case 3 :                             /*BINARY */
294 295 296
		if (!LITTLEBIG) { /* OUTPUT NATIVE FLOATS */
			for (i=1; i<=ntr; i++) for (j=1; j<=ns; j++) {
					fwrite(&section[i][j],sizeof(float),1,fpdata);
Simone Butzer's avatar
Simone Butzer committed
297
				}
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

		} else {
				/* SWAP FLOATS */
				for (i=1; i<=ntr; i++) for (j=1; j<=ns; j++) {
						tfloat=section[i][j];
						pint=(int *) &tfloat;
						*pint=((*pint>>24)&0xff)|((*pint&0xff)<<24)|((*pint>>8)&0xff00)|((*pint&0xff00)<<8);
							/* explanation:
							* (*pint>>24)&0xff shifts 3 bytes (3*8) to the right and takes with &=and only the least significant byte (other bytes 0) 0 0 0 1
							(*pint&0xff)<<24 sets the first 3 bytes to zero (takes only the least significant byte) than shifts 3 bytes to the left (0 0 0 1 -> 1 0 0 0) 
							(*pint>>8)&0xff00 shifts 1 byte to the right and than takes only 3rd byte (0 0 1 0)
							(*pint&0xff00)<<8 sets bytes 1,2,4 to zero and shifts 1 byte to the left (0 1 0 0)
							The |=or operator adds all for parts to the swaped float
							*/
						fwrite(&tfloat,sizeof(float),1,fpdata);
					}

Simone Butzer's avatar
Simone Butzer committed
315
		}
316

Simone Butzer's avatar
Simone Butzer committed
317 318 319
		break;

	default :
320
		fprintf(fp," Don't know data format for seismograms ! Choose SEIS_FORMAT 1-3 \n");
Simone Butzer's avatar
Simone Butzer committed
321 322 323 324
		fprintf(fp," No output written. ");
	}

	fclose(fpdata);
325 326


Simone Butzer's avatar
Simone Butzer committed
327
}