[cig-commits] [commit] pluggable: add get_Sac_header.c to read SAC header from command line (ff3481c)

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


Repository : ssh://geoshell/specfem3d_globe

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

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

commit ff3481c46a56b561b7af6cae3e47ce3277698645
Author: Eh Tan <tan2 at earth.sinica.edu.tw>
Date:   Tue May 5 20:24:53 2009 +0000

    add get_Sac_header.c to read SAC header from command line


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

ff3481c46a56b561b7af6cae3e47ce3277698645
 UTILS/seis_process/Makefile         |  5 ++
 UTILS/seis_process/get_sac_header.c | 91 +++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git a/UTILS/seis_process/Makefile b/UTILS/seis_process/Makefile
index da9e95e..3894017 100644
--- a/UTILS/seis_process/Makefile
+++ b/UTILS/seis_process/Makefile
@@ -14,7 +14,12 @@ CFLAGS = -O2
 CPPFLAGS = -I$(SACAUX)/../include
 LDFLAGS = -L$(SACAUX)/../lib -lsacio
 
+all: asc2sac get_sac_header
+
 asc2sac: asc2sac.c
 	$(CC) asc2sac.c -o asc2sac $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
 
+get_sac_header: get_sac_header.c
+	$(CC) get_sac_header.c -o get_sac_header $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
+
 
diff --git a/UTILS/seis_process/get_sac_header.c b/UTILS/seis_process/get_sac_header.c
new file mode 100644
index 0000000..5918b42
--- /dev/null
+++ b/UTILS/seis_process/get_sac_header.c
@@ -0,0 +1,91 @@
+/* License
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "sac.h"
+
+int
+main(int argc, char *argv[])
+{
+    int i;
+    long int max, nlen, nerr;
+    float yarray[1], beg, del;
+    char *sacfn;
+
+    if(argc < 3) {
+	fprintf(stderr, "usage: %s sac-file var1 [var2 ...]\n", argv[0]);
+	exit(1);
+    }
+
+    /* Read evenly spaced SAC file */
+    /* The library does not allow reading header only, so we need to
+     * read in some data as well... */
+    sacfn = argv[1];
+    max = 1;
+    nerr = 0;
+    rsac1(sacfn, yarray, &nlen, &beg, &del, &max, &nerr, strlen(sacfn));
+    if(nerr && (nerr != -803)) {
+        /* -803 means the file is longer than requested size. Ignored. */
+	fprintf(stderr, "error when reading '%s', err=%d\n", sacfn, nerr);
+	exit(-1);
+    }
+
+    /* Get header values */
+    for(i=2; i<argc; i++) {
+        long int itmp;
+        float ftmp;
+        char ctmp[33];
+        char c;
+
+        /* the 1st character must be ASCII */
+        c = argv[i][0];
+        if(c < 'A' || c > 'z') {
+            fprintf(stderr, "argument '%s' is not a valid SAC header\n", argv[i]);
+            exit(1);
+        }
+        fprintf(stderr, "argument is '%s'\n", argv[i]);
+
+        /* the first character of SAC header indicated its type */
+        switch(tolower(c)) {
+        case 'n':
+            /* integer type */
+            getnhv(argv[i], &itmp, &nerr, strlen(argv[i]));
+            printf("%10d", itmp);
+            break;
+        case 'i':
+            /* enumerated type */
+            getihv(argv[i], ctmp, &nerr, strlen(argv[i]), strlen(ctmp));
+            printf("%s", ctmp);
+            break;
+        case 'l':
+            /* logical type */
+            getlhv(argv[i], &itmp, &nerr, strlen(argv[i]));
+            printf("%d", itmp);
+            break;
+        case 'k':
+            /* char string type */
+            getkhv(argv[i], ctmp, &nerr, strlen(argv[i]), strlen(ctmp));
+            printf("%s", ctmp);
+            break;
+        default:
+            /* float type */
+            getfhv(argv[i], &ftmp, &nerr, strlen(argv[i]));
+            printf("%12.6g", ftmp);
+            break;
+        }
+
+        if(nerr) {
+            fprintf(stderr, "error when reading header of '%s'\n", sacfn);
+            exit(-1);
+        }
+
+        /* each field is separated by at least two spaces */
+        printf("  ");
+    }
+
+    printf("\n");
+
+    return 0;
+}



More information about the CIG-COMMITS mailing list