[cig-commits] [commit] pluggable: Removed silly shell script ascii2sac.csh (its functionality has been incorporated into "asc2sac.c"). (6cf4045)

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


Repository : ssh://geoshell/specfem3d_globe

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

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

commit 6cf4045cb104f1a76a9a70995a5f503067537cbc
Author: Leif Strand <leif at geodynamics.org>
Date:   Wed May 6 20:24:08 2009 +0000

    Removed silly shell script ascii2sac.csh (its functionality has been
    incorporated into "asc2sac.c").


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

6cf4045cb104f1a76a9a70995a5f503067537cbc
 UTILS/seis_process/asc2sac.c         | 116 ++++++++++++++++++-----------------
 UTILS/seis_process/ascii2sac.csh     |   9 ---
 UTILS/seis_process/process_syn.pl.in |   2 +-
 3 files changed, 60 insertions(+), 67 deletions(-)

diff --git a/UTILS/seis_process/asc2sac.c b/UTILS/seis_process/asc2sac.c
index 455ed69..b55b4b3 100644
--- a/UTILS/seis_process/asc2sac.c
+++ b/UTILS/seis_process/asc2sac.c
@@ -24,77 +24,53 @@
 #include <stdlib.h>
 #include <limits.h>
 #include <errno.h>
-#include "sac.h"
+#include "sacio.h"
 
-int
-main(int argc, char *argv[])
-{
-    long int npts, nerr, i;
+void asc2sac(char *ascfn) {
+    long int npts, max_npts, nerr, nconv, len;
     long int itmp;
     float ftmp;
-    char *endptr, *str;
-    char *ascfn, *sacfn;
-    float *time, *data;
+    char *sacfn;
+    float *time, *data, a, b;
     FILE *f;
     
-    if(argc < 4) {
-        fprintf(stderr,
-                "Converting evenly spaced time series ASCII file to SAC binary\n"
-                "usage: %s ascii-file npts sac-file\n\n"
-                "ascii-file: name of the input file. The file must have two columns,\n"
-                "            1st column must be evenly spaced.\n"
-                "npts:       number of lines in the input file.\n"
-                "sac-file:   name of the output file.\n",
-                argv[0]);
-        exit(1);
-    }
-
-    str = argv[2];
-    errno = 0;
-    endptr = str;
-    npts = strtol(str, &endptr, 10);
-
-    /* Check for various possible errors */
-    if (errno || (*endptr != 0)) {
-        fprintf(stderr, "Error when converting '%s' to integer\n", str);
-        exit(-1);
-    }
-
-    if (npts <= 0) {
-        fprintf(stderr, "npts must be positive\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) {
+    sacfn = (char *)malloc(strlen(ascfn) + strlen(".sac") + 1);
+    if (!sacfn) {
         fprintf(stderr, "Out of memory\n");
         exit(1);
     }
-
+    strcpy(sacfn, ascfn);
+    strcat(sacfn, ".sac");
 
     /* reading ascii file */
     f = fopen(ascfn, "r");
     if(f == NULL) {
 	fprintf(stderr, "Cannot open file '%s' to read\n", ascfn);
-	exit(-1);
+	exit(1);
     }
     
-    for(i=0; i<npts; i++) {
-        char buffer[255];
-        float a, b;
-        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 = data = 0;
+    max_npts = 0;
+    for (npts = 0; (nconv = fscanf(f, "%f %f\n", &a, &b)) == 2; ++npts) {
+        if (nconv != 2) {
+            fprintf(stderr, "error while reading file '%s'\n", ascfn);
+            exit(1);
+        }
+        if (npts >= max_npts) {
+            max_npts = max_npts ? 2 * max_npts : 1024;
+            time = (float*) realloc(time, max_npts * sizeof(float));
+            data = (float*) realloc(data, max_npts * sizeof(float));
+            if(time == NULL || data == NULL) {
+                fprintf(stderr, "Out of memory\n");
+                exit(1);
+            }
         }
-        time[i] = a;
-        data[i] = b;
+        time[npts] = a;
+        data[npts] = b;
+    }
+    if (nconv != EOF || ferror(f)) {
+        fprintf(stderr, "error while reading file '%s' (on or near line %d)\n", ascfn, npts);
+	exit(1);
     }
     fclose(f);
     /* finished reading ascii file */
@@ -114,14 +90,40 @@ main(int argc, char *argv[])
 
     if(nerr) {
 	fprintf(stderr, "error when setting header for '%s'\n", sacfn);
-        exit(-1);
+        exit(1);
     }
 
     wsac0(sacfn, time, data, &nerr, strlen(sacfn));
 
     if(nerr) {
         fprintf(stderr, "error when writing '%s'\n", sacfn);
-        exit(-1);
+        exit(1);
+    }
+    
+    free(time);
+    free(data);
+    free(sacfn);
+
+    return;
+}
+
+
+int main(int argc, char *argv[]) {
+    int i;
+     
+    if (argc < 2) {
+        fprintf(stderr,
+                "usage: %s FILE...\n"
+                "\n"
+                "    Converts ASCII time series files to SAC binary files.\n"
+                "    Input files must have two columns:  t, f(t).\n"
+                "\n",
+                argv[0]);
+        exit(1);
+    }
+    
+    for (i = 1; i < argc; ++i) {
+        asc2sac(argv[i]);
     }
     
     return 0;
diff --git a/UTILS/seis_process/ascii2sac.csh b/UTILS/seis_process/ascii2sac.csh
deleted file mode 100755
index 704447f..0000000
--- a/UTILS/seis_process/ascii2sac.csh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/csh -f
-
-# Vala Hjorleifsdottir and Qinya Liu, Caltech, Jan 2007
-
-foreach file ($*)
-  echo $file
-  set nlines = `cat $file | wc -l`
-  ./asc2sac $file $nlines $file.sac
-end
diff --git a/UTILS/seis_process/process_syn.pl.in b/UTILS/seis_process/process_syn.pl.in
index 906e0d9..c233ccf 100755
--- a/UTILS/seis_process/process_syn.pl.in
+++ b/UTILS/seis_process/process_syn.pl.in
@@ -68,7 +68,7 @@ $eps = 0.1;
 
 $saclst="$sacaux/../bin/saclst";
 $phtimes="phtimes";
-$asc2sac="@prefix@/bin/ascii2sac.csh";
+$asc2sac="@prefix@/bin/asc2sac";
 if (! -e $saclst)  {die(" No $saclst file\n");}
 #if (! -e $phtimes) {die("No $phtimes file\n");}
 if (! -e $asc2sac) {die("No $asc2sac file\n");}



More information about the CIG-COMMITS mailing list