Thread: SAC reading binary files

Started: 2008-07-25 21:17:42
Last activity: 2008-07-28 17:46:31
Topics: SAC Help
Ryan P Cary
2008-07-25 21:17:42
Hi,
I am attempting to read in sac binary files without using the rsac1 routine in c. My goal is to get the floating point amplitudes into an array, and I am having trouble understanding the correct binary offset in the sac files to get to the amplitudes. The online sac manual states that the "FIRST DATA SECTION" starts at word 158, implying that the following c code should work:

FILE * f;
float fx;
int i;

f=fopen("83.0325.22.27.33.RED.4.sac","r");

fseek(f, 54,SEEK_SET);//jump 54 bytes
for (i=0; i < 200; i++)
{

fread(&fx,sizeof(float),1,f);

printf("%f\n",fx);

}

However this seems to print junk data; I can validate what should be printed by calling rsac1.

So my questions for you are:

1. What is the correct offset to read in amplitudes?
2. What (if any) is the spacing between the data? I ask because the manual page mentions "contents:

* dependent variable
* amplitude
* real component "

but I can't find any description on the ordering/structur/size in bytes of these data.


Thank you in advance for your effort,

-Ryan Cary


  • Chad Trabant
    2008-07-28 16:26:49

    Hi Ryan,

    The sac2mseed converter does not use the SAC library, you'll find the C source
    code here:

    http://www.iris.edu/pub/programs/converters/

    cheers,
    Chad

    Ryan P Cary wrote:
    Hi,
    I am attempting to read in sac binary files without using the rsac1 routine in c. My goal is to get the floating point amplitudes into an array, and I am having trouble understanding the correct binary offset in the sac files to get to the amplitudes. The online sac manual states that the "FIRST DATA SECTION" starts at word 158, implying that the following c code should work:

    FILE * f;
    float fx;
    int i;

    f=fopen("83.0325.22.27.33.RED.4.sac","r");

    fseek(f, 54,SEEK_SET);//jump 54 bytes
    for (i=0; i < 200; i++)
    {

    fread(&fx,sizeof(float),1,f);

    printf("%f\n",fx);

    }

    However this seems to print junk data; I can validate what should be printed by calling rsac1.

    So my questions for you are:

    1. What is the correct offset to read in amplitudes?
    2. What (if any) is the spacing between the data? I ask because the manual page mentions "contents:

    * dependent variable
    * amplitude
    * real component "

    but I can't find any description on the ordering/structur/size in bytes of these data.


    Thank you in advance for your effort,

    -Ryan Cary

    _______________________________________________
    sac-help mailing list
    sac-help<at>iris.washington.edu
    http://www.iris.washington.edu/mailman/listinfo/sac-help



  • Philip Crotwell
    2008-07-28 17:46:31
    The attached files work for reading/writing in C without using the sac library.

    Philip

    On Fri, Jul 25, 2008 at 5:17 PM, Ryan P Cary <RPCary<at>lbl.gov> wrote:
    Hi,
    I am attempting to read in sac binary files without using the rsac1 routine in c. My goal is to get the floating point amplitudes into an array, and I am having trouble understanding the correct binary offset in the sac files to get to the amplitudes. The online sac manual states that the "FIRST DATA SECTION" starts at word 158, implying that the following c code should work:

    FILE * f;
    float fx;
    int i;

    f=fopen("83.0325.22.27.33.RED.4.sac","r");

    fseek(f, 54,SEEK_SET);//jump 54 bytes
    for (i=0; i < 200; i++)
    {

    fread(&fx,sizeof(float),1,f);

    printf("%f\n",fx);

    }

    However this seems to print junk data; I can validate what should be printed by calling rsac1.

    So my questions for you are:

    1. What is the correct offset to read in amplitudes?
    2. What (if any) is the spacing between the data? I ask because the manual page mentions "contents:

    * dependent variable
    * amplitude
    * real component "

    but I can't find any description on the ordering/structur/size in bytes of these data.


    Thank you in advance for your effort,

    -Ryan Cary

    _______________________________________________
    sac-help mailing list
    sac-help<at>iris.washington.edu
    http://www.iris.washington.edu/mailman/listinfo/sac-help

    struct sac {
    float delta, depmin, depmax, scale, odelta;
    float b, e, o, a, internal1;
    float t0, t1, t2, t3, t4;
    float t5, t6, t7, t8, t9;
    float f, resp0, resp1, resp2, resp3;
    float resp4, resp5, resp6, resp7, resp8;
    float resp9, stla, stlo, stel, stdp;
    float evla, evlo, evel, evdp, unused1;
    float user0, user1, user2, user3, user4;
    float user5, user6, user7, user8, user9;
    float dist, az, baz, gcarc, internal2;
    float internal3, depmen, cmpaz, cmpinc, unused2;
    float unused3, unused4, unused5, unused6, unused7;
    float unused8, unused9, unused10, unused11, unused12;
    long nzyear, nzjday, nzhour, nzmin, nzsec;
    long nzmsec, internal4, internal5, internal6, npts;
    long internal7, internal8, unused13, unused14, unused15;
    long iftype, idep, iztype, unused16, iinst;
    long istreg, ievreg, ievtyp, iqual, isynth;
    long unused17, unused18, unused19, unused20, unused21;
    long unused22, unused23, unused24, unused25, unused26;
    long leven, lpspol, lovrok, lcalda, unused27;
    char kstnm[8], kevnm[16];
    char khole[8], ko[8], ka[8];
    char kt0[8], kt1[8], kt2[8];
    char kt3[8], kt4[8], kt5[8];
    char kt6[8], kt7[8], kt8[8];
    char kt9[8], kf[8], kuser0[8];
    char kuser1[8], kuser2[8], kcmpnm[8];
    char knetwk[8], kdatrd[8], kinst[8];
    };

    static struct sac sac_null = {
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345., -12345., -12345., -12345., -12345.,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    -12345, -12345, -12345, -12345, -12345,
    { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
    { '-','1','2','3','4','5',' ',' ' }
    };

    /* defines for logical data types */
    #define TRUE 1
    #define FALSE 0

    /* defines for enumerated data types */
    #define IREAL 0
    #define ITIME 1
    #define IRLIM 2
    #define IAMPH 3
    #define IXY 4
    #define IUNKN 5
    #define IDISP 6
    #define IVEL 7
    #define IACC 8
    #define IB 9
    #define IDAY 10
    #define IO 11
    #define IA 12
    #define IT0 13
    #define IT1 14
    #define IT2 15
    #define IT3 16
    #define IT4 17
    #define IT5 18
    #define IT6 19
    #define IT7 20
    #define IT8 21
    #define IT9 22
    #define IRADNV 23
    #define ITANNV 24
    #define IRADEV 25
    #define ITANEV 26
    #define INORTH 27
    #define IEAST 28
    #define IHORZA 29
    #define IDOWN 30
    #define IUP 31
    #define ILLLBB 32
    #define IWWSN1 33
    #define IWWSN2 34
    #define IHGLP 35
    #define ISRO 36
    #define INUCL 37
    #define IPREN 38
    #define IPOSTN 39
    #define IQUAKE 40
    #define IPREQ 41
    #define IPOSTQ 42
    #define ICHEM 43
    #define IOTHER 44
    #define IGOOD 45
    #define IGLCH 46
    #define IDROP 47
    #define ILOWSN 48
    #define IRLDTA 49
    #define IVOLTS 50
    #define INIV51 51
    #define INIV52 52
    #define INIV53 53
    #define INIV54 54
    #define INIV55 55
    #define INIV56 56
    #define INIV57 57
    #define INIV58 58
    #define INIV59 59
    #define INIV60 60

    #define FCS "%15.7f%15.7f%15.7f%15.7f%15.7f\n"
    #define ICS "%10d%10d%10d%10d%10d\n"
    #define CCS1 "%-8.8s%-8.8s%-8.8s\n"
    #define CCS2 "%-8.8s%-16.16s\n"#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include "sac.h"

    /* cc readsac.c -g -o readsac -lm*/

    /*
    Template for c codes that use sac files. This just reads in a sac file,
    copies the array, and writes it out into a new sac file. We #include
    several header files that aren't needed but might be when the template
    is modified.
    */

    main(int argc, char* argv[])
    {
    FILE *sacfile;
    struct sac head;
    char insacfile[256];
    char outsacfile[256];
    float indata[150000], outdata[150000];
    int i;

    if (argc != 3) {
    (void) printf("Usage:%s insacfile outsacfile \n",argv[0]);
    return 1;
    } else {
    (void) strcpy(insacfile,&argv[1][0]);
    (void) strcpy(outsacfile,&argv[2][0]);
    (void) printf("infile %s, outfile %s\n",insacfile,outsacfile);
    }


    /* Read sac file */
    sacfile = fopen(insacfile,"r");
    (void) fread(&head, 1 , sizeof(head), sacfile);
    (void) fread( indata, sizeof(float), head.npts, sacfile);
    (void) fclose(sacfile);

    /* Set out data points to in data points. */
    for (i=0;i<head.npts;i++) {
    outdata[i] = indata[i];
    }

    /* Write sac file */
    sacfile = fopen(outsacfile,"w");
    (void) fwrite(&head, 1 , sizeof(head), sacfile);
    (void) fwrite( outdata, sizeof(float), head.npts, sacfile);
    (void) fclose(sacfile);

    return(0);
    }
Page built 08:21:19 | v.b'108166