From b0371808b0f0848e271b03e0417976d2f3184f4d Mon Sep 17 00:00:00 2001
From: Jan Thorbecke <janth@xs4all.nl>
Date: Thu, 7 Dec 2017 07:53:00 +0100
Subject: [PATCH] adding src_txt file to read in areal shot locations

---
 fdelmodc/fdelmodc.c      |  1 +
 fdelmodc/getParameters.c | 44 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/fdelmodc/fdelmodc.c b/fdelmodc/fdelmodc.c
index 899f78c..66b19a5 100644
--- a/fdelmodc/fdelmodc.c
+++ b/fdelmodc/fdelmodc.c
@@ -138,6 +138,7 @@ char *sdoc[] = {
 "   dzshot=0 .......... if nshot > 1: z-shift in shot locations",
 "   xsrca= ............ defines source array x-positions",
 "   zsrca= ............ defines source array z-positions",
+"   src_txt=........... text file with source coordinates. Col 1: x, Col. 2: z",
 "   wav_random=1 ...... 1 generates (band limited by fmax) noise signatures ",
 "   fmax=from_src ..... maximum frequency in wavelet",
 "   src_multiwav=0 .... use traces in file_src as areal source",
diff --git a/fdelmodc/getParameters.c b/fdelmodc/getParameters.c
index 0c5d4e1..7c65270 100644
--- a/fdelmodc/getParameters.c
+++ b/fdelmodc/getParameters.c
@@ -39,7 +39,7 @@ int getParameters(modPar *mod, recPar *rec, snaPar *sna, wavPar *wav, srcPar *sr
 {
 	int isnapmax1, isnapmax2, isnapmax, sna_nrsna;
 	int n1, n2, nx, nz, nsrc, ix, axis, ioPz, is0, optn;
-	int idzshot, idxshot;
+	int idzshot, idxshot, nsrctext;
 	int src_ix0, src_iz0, src_ix1, src_iz1;
 	int disable_check;
 	float cp_min, cp_max, cs_min, cs_max, ro_min, ro_max;
@@ -63,6 +63,8 @@ int getParameters(modPar *mod, recPar *rec, snaPar *sna, wavPar *wav, srcPar *sr
 	int is,ntraces,length_random;
 	float rand;
 	char *src_positions, tmpname[1024];
+	char* src_txt;
+	FILE *fp;
 
 	if (!getparint("verbose",&verbose)) verbose=0;
 	if (!getparint("disable_check",&disable_check)) disable_check=0;
@@ -584,6 +586,9 @@ int getParameters(modPar *mod, recPar *rec, snaPar *sna, wavPar *wav, srcPar *sr
 		verr("Number of sources in array xsrca (%d), zsrca(%d) are not equal",nxsrc, nzsrc);
 	}
 
+	/* source positions defined through txt file */
+   	if (!getparstring("src_txt",&src_txt)) src_txt=NULL;
+
 	/* check if sources on a circle are defined */
 	
 	if (getparfloat("rsrc", &rsrc)) {
@@ -729,17 +734,45 @@ int getParameters(modPar *mod, recPar *rec, snaPar *sna, wavPar *wav, srcPar *sr
 		}
 
 	}
-	else if (nxsrc != 0) {
+	else if ( (nxsrc != 0) || (src_txt != NULL) ) {
 		/* source array is defined */
-		nsrc=nxsrc;
+	    if (src_txt!=NULL) {
+    	    /* Sources from a Text File */
+            /* Open text file */
+		    nsrctext=0;
+            fp=fopen(src_txt,"r");
+            assert(fp!=NULL);
+            /* Get number of lines */
+            while (!feof(fp)) if (fgetc(fp)=='\n') nsrctext++;
+            fseek(fp,-1,SEEK_CUR);
+            if (fgetc(fp)!='\n') nsrctext++; /* Checks if last line terminated by /n */
+            if (verbose) vmess("Number of sources in src_txt file: %d",nsrctext);
+            rewind(fp);
+		    nsrc=nsrctext;
+        }
+        else {
+		    nsrc=nxsrc;
+        }
+		/* Allocate arrays */
 		src->x = (int *)malloc(nsrc*sizeof(int));
 		src->z = (int *)malloc(nsrc*sizeof(int));
 		src->tbeg = (float *)malloc(nsrc*sizeof(float));
 		src->tend = (float *)malloc(nsrc*sizeof(float));
 		xsrca = (float *)malloc(nsrc*sizeof(float));
 		zsrca = (float *)malloc(nsrc*sizeof(float));
-		getparfloat("xsrca", xsrca);
-		getparfloat("zsrca", zsrca);
+	    if (src_txt!=NULL) {
+			/* Read in source coordinates */
+			for (i=0;i<nsrc;i++) {
+				if (fscanf(fp,"%e %e\n",&xsrca[i],&zsrca[i])!=2) vmess("Source Text File: Can not parse coordinates on line %d.",i);
+			}
+			/* Close file */
+			fclose(fp);
+        }
+		else {
+			getparfloat("xsrca", xsrca);
+			getparfloat("zsrca", zsrca);
+        }
+		/* Process coordinates */
 		for (is=0; is<nsrc; is++) {
 			src->x[is] = NINT((xsrca[is]-sub_x0)/dx);
 			src->z[is] = NINT((zsrca[is]-sub_z0)/dz);
@@ -747,6 +780,7 @@ int getParameters(modPar *mod, recPar *rec, snaPar *sna, wavPar *wav, srcPar *sr
 			src->tend[is] = (wav->nt-1)*wav->dt;
 			if (verbose>3) fprintf(stderr,"Source Array: xsrc[%d]=%f zsrc=%f\n", is, xsrca[is], zsrca[is]);
 		}
+
 		src->random = 1;
 		wav->nsamp = (size_t *)malloc((nsrc+1)*sizeof(size_t));
 		if (wav->random) {
-- 
GitLab