[cig-commits] r14872 - seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process
tan2 at geodynamics.org
tan2 at geodynamics.org
Tue May 5 11:57:53 PDT 2009
Author: tan2
Date: 2009-05-05 11:57:52 -0700 (Tue, 05 May 2009)
New Revision: 14872
Added:
seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/Makefile
seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/asc2sac.c
Modified:
seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/ascii2sac.csh
Log:
Add my replacement of asc2sac
Added: seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/Makefile
===================================================================
--- seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/Makefile (rev 0)
+++ seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/Makefile 2009-05-05 18:57:52 UTC (rev 14872)
@@ -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)
+
+
Added: seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/asc2sac.c
===================================================================
--- seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/asc2sac.c (rev 0)
+++ seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/asc2sac.c 2009-05-05 18:57:52 UTC (rev 14872)
@@ -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;
+}
Modified: seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/ascii2sac.csh
===================================================================
--- seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/ascii2sac.csh 2009-05-05 18:32:29 UTC (rev 14871)
+++ seismo/3D/SPECFEM3D_GLOBE/branches/pluggable/UTILS/seis_process/ascii2sac.csh 2009-05-05 18:57:52 UTC (rev 14872)
@@ -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