[cig-commits] [commit] pluggable: Add my replacement of asc2sac (bb6971b)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Wed Apr 9 08:55:20 PDT 2014


Repository : ssh://geoshell/specfem3d_globe

On branch  : pluggable
Link       : https://github.com/geodynamics/specfem3d_globe/compare/64e1b38f0c5ebb4056cce0b15d41c0b9f94ab6e5...099a4d330d5b173b21e51ad441f9f429e5d37842

>---------------------------------------------------------------

commit bb6971bf8fb49f721e0bd95b3055c65a468dc1bb
Author: Eh Tan <tan2 at earth.sinica.edu.tw>
Date:   Tue May 5 18:57:52 2009 +0000

    Add my replacement of asc2sac


>---------------------------------------------------------------

bb6971bf8fb49f721e0bd95b3055c65a468dc1bb
 UTILS/seis_process/Makefile      |  20 ++++++++
 UTILS/seis_process/asc2sac.c     | 105 +++++++++++++++++++++++++++++++++++++++
 UTILS/seis_process/ascii2sac.csh |   6 +--
 3 files changed, 128 insertions(+), 3 deletions(-)

diff --git a/UTILS/seis_process/Makefile b/UTILS/seis_process/Makefile
new file mode 100644
index 0000000..da9e95e
--- /dev/null
+++ b/UTILS/seis_process/Makefile
@@ -0,0 +1,20 @@
+# %License%
+
+
+
+## The environment variable SACAUX is required for running SAC.
+## We will assume SACAUX is already defined.
+
+## The SAC library on GPS machine is for SUN, not for linux
+## using my version for now
+SACAUX = /home/tan2/opt/sac-101.2/aux
+
+CC = gcc
+CFLAGS = -O2
+CPPFLAGS = -I$(SACAUX)/../include
+LDFLAGS = -L$(SACAUX)/../lib -lsacio
+
+asc2sac: asc2sac.c
+	$(CC) asc2sac.c -o asc2sac $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
+
+
diff --git a/UTILS/seis_process/asc2sac.c b/UTILS/seis_process/asc2sac.c
new file mode 100644
index 0000000..069538c
--- /dev/null
+++ b/UTILS/seis_process/asc2sac.c
@@ -0,0 +1,105 @@
+/* License
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <errno.h>
+#include "sac.h"
+
+int
+main(int argc, char *argv[])
+{
+    int npts, nerr, i;
+    int itmp;
+    float ftmp;
+    char *endptr, *str;
+    char *ascfn, *sacfn;
+    float *time, *data;
+    float a, b;
+    char buffer[255];
+    FILE *f;
+
+    if(argc < 4) {
+	fprintf(stderr, "%s ascii-file npts sac-file\n", argv[0]);
+	exit(1);
+    }
+
+    str = argv[2];
+    errno = 0;
+    npts = strtol(str, &endptr, 10);
+  
+    /* Check for various possible errors */
+    if ((errno == ERANGE && (npts == INT_MAX || npts == INT_MIN))
+	|| (errno != 0 && npts <= 0)) {
+	perror("strtol");
+	exit(EXIT_FAILURE);
+    }
+    
+    if (endptr == str) {
+	fprintf(stderr, "No npts were found\n");
+	exit(EXIT_FAILURE);
+    }
+    
+    /* If we got here, strtol() successfully parsed a number */
+    ascfn = argv[1];
+    sacfn = argv[3];
+
+    time = (float*) malloc(npts * sizeof(float));
+    data = (float*) malloc(npts * sizeof(float));
+
+    if(time == NULL || data == NULL) {
+        fprintf(stderr, "Out of memory\n");
+        exit(1);
+    }
+
+
+    /* reading ascii file */
+    f = fopen(ascfn, "r");
+    if(f == NULL) {
+	fprintf(stderr, "Cannot open file '%s' to read\n", ascfn);
+	exit(-1);
+    }
+  
+    for(i=0; i<npts; i++) {
+	fgets(buffer, 254, f);
+	if(sscanf(buffer, "%f %f\n", &a, &b) != 2) {
+	    fprintf(stderr, "error when reading file '%s'\n", ascfn);
+	    exit(-1);
+	}
+	time[i] = a;
+	data[i] = b;
+    }
+    fclose(f);
+    /* finished reading ascii file */
+
+    /* write SAC data usng SAC IO library */
+    nerr = 0;
+    newhdr();
+    setnhv("npts", &npts, &nerr, strlen("npts"));
+    itmp = 6;
+    setnhv("nvhdr", &itmp, &nerr, strlen("nvhdr"));
+    itmp = 0;
+    setlhv("leven", &itmp, &nerr, strlen("leven"));
+    ftmp = time[1] - time[0];
+    setfhv("delta", &ftmp, &nerr, strlen("delta"));
+    setfhv("b", &(time[0]), &nerr, strlen("b"));
+    setfhv("e", &(time[npts-1]), &nerr, strlen("e"));
+    setihv("iftype", "itime", &nerr, strlen("iftype"), strlen("itime"));
+    setihv("idep", "idisp", &nerr, strlen("idep"), strlen("idisp"));
+
+    if(nerr) {
+	fprintf(stderr, "error when setting header for '%s'\n", sacfn);
+        exit(-1);
+    }
+
+    wsac0(sacfn, time, data, &nerr, strlen(sacfn));
+
+    if(nerr) {
+	fprintf(stderr, "error when writing '%s'\n", sacfn);
+	exit(-1);
+    }
+
+    return 0;
+}
diff --git a/UTILS/seis_process/ascii2sac.csh b/UTILS/seis_process/ascii2sac.csh
index e0eeb91..704447f 100755
--- a/UTILS/seis_process/ascii2sac.csh
+++ b/UTILS/seis_process/ascii2sac.csh
@@ -3,7 +3,7 @@
 # Vala Hjorleifsdottir and Qinya Liu, Caltech, Jan 2007
 
 foreach file ($*)
-	echo $file
-  set nlines = `wc -l $file | awk '{print $1}'`
-  /opt/seismo-util/bin/asc2sac $file $nlines $file.sac
+  echo $file
+  set nlines = `cat $file | wc -l`
+  ./asc2sac $file $nlines $file.sac
 end



More information about the CIG-COMMITS mailing list