Use in Fortran Programs

Time series data can be generated in your own fortran (and C ) programs and output in SAC binary format.  Many of my programs use this capability since it is useful to compare data and synthetic seismograms in the same format.  It also useful to read SAC binary files to perform more specialized functions that SAC does not include.

This is accomplished by called the following IO routines available in the SAC library, "sac.a".

In addition to these routines, there are a number of subroutines that access or define each SAC header variable.  See the SAC manual for details.

An Example

Here is an f77 program that constructs a gaussian time function an outputs the result in SAC binary format.
 
 
Source Code
Explanation
c ------ gauss.f program to create gaussian time function
      CHARACTER*90 KNAME
      CHARACTER*3 ANS
      dimension YARRAY(8192)
      CHARACTER*80 PASNAME
*      Create the far field time function --------------------
      WRITE(*,100)
100   FORMAT('INPUT time width and time shift of gaussian pulse:')
      READ(*,*) twidth,tshift
      alpha=2.2/twidth
      WRITE(*,101)
101   FORMAT('INPUT DT AND NPTS WANTED:')
      READ(*,*) DT,NPOINT
      DO 10 I=1,NPOINT
      TM=DT*(I-1) - twidth - tshift
      EGG=alpha*alpha*tm*tm
      IF(EGG.GT.25.) EGG=25.
      YARRAY(I)=EXP(-EGG)
C     YARRAY(I)=-2.*TM*ALPHA*ALPHA*EXP(-EGG)
10    CONTINUE
C---------- NOW CREATE A HEADER IN SAC FORMAT.---------------------
C
C - Set all header fields to their default values.
C
      CALL NEWHDR
C
C - USER ACTION: Shown below are incomplete statements used
C                to define each of the SAC header fields.
C                Using your own variables, complete the statements
C                which define the SAC header fields that you
C                want to be in the file.  Delete the remaining lines.
C
C - The definitions for each of these variables can be found in the
C   appendix to the SAC Command Reference Manual.
C
C - Fields which begin with an "L" are logical fields and must
C   be either .TRUE. or .FALSE.
C - Fields which begin with an "N" are integer fields.
C - Fields which begin with an "I" are also integers, but can only
C   be set to a limited number of values, each corresponding
C   to a specific condition.  See the above mentioned appendix.
C - Fields which begin with a "K" are character variables of
C   length 8 unless otherwise noted.
C - All other fields are real.
C
C - The following fields are required:
C
      NPTS=NPOINT
        B=0.
        DELTA=DT
        E=B+FLOAT(NPTS-1)*DELTA
      CALL SETFHV('B',B,NERR)
      CALL SETFHV('E',E,NERR)
      CALL SETNHV('NPTS',NPTS,NERR)
      CALL SETLHV('LEVEN',.TRUE.,NERR)
      CALL SETFHV('DELTA',DELTA,NERR)
      CALL SETIHV('IZTYPE','IB',NERR)
      CALL SETLHV('LPSPOL',.TRUE.,NERR)
      CALL SETIHV('IEVTYP','IUNKN',NERR)
      CALL SETIHV('IFTYPE','ITIME',NERR)
      CALL SETIHV('IDEP','IDISP',NERR)
      CALL SETLHV('LOVROK',.TRUE.,NERR)
C
      KNAME='gauss.tfunc'
C
C - WRITE THE FILE TO DISK.
C
      CALL WSAC1(KNAME,YARRAY,npts,b,delta,NERR)
C
      WRITE(1,200) KNAME
200   FORMAT(1X,'SAC FILE ',A32,' HAS BEEN WRITTEN TO DISK')
99    CONTINUE
      STOP
      END
 

 
 
 
 
 
 
 
 
 
 
 

calculate the gaussian function
 
 
 
 
 
 
 
 
 

Set up a new SAC header
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Set header variables needed
 
 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 

use wsac1 to create an evenly spaced binary file
 
 
 
 
 
 
 
 
 
 



Here is the make file used to compile this program:

FFLAGS = -g
MYBIN  = /yourdirectory/bin
LIB    = $(SACDIR)/lib

gauss: gauss.f
 f77 $(FFLAGS) -o $(MYBIN)/gauss gauss.f $(LIB)/sac.a


Important Note:

At this time (3/05/01) CERI does not have a generally available copy of the SAC library, "sac.a" or "sacio.a", that can be used in compiling fortran or C programs on SUN Solaris systems.  A copy is available at /tetemeko/local/sac/lib/sac.a if you need to compile programs with SAC I/O routines.


Return



last modified 3/06/01