Exemple #1
0
 def test_readWrite(self):
     """
     Tests for SacIO read and write
     """
     sacfile = os.path.join(self.path, 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         t = SacIO()
         t.ReadSacFile(sacfile)
         self.assertEqual(t.GetHvalue('npts'), 100)
         self.assertEqual(t.GetHvalue("kcmpnm"), "Q       ")
         self.assertEqual(t.GetHvalue("kstnm"), "STA     ")
         t.SetHvalue("kstnm", "spiff")
         self.assertEqual(t.GetHvalue('kstnm'), 'spiff   ')
         t.WriteSacBinary(tempfile)
         self.assertEqual(os.stat(sacfile)[6], os.stat(tempfile)[6])
         self.assertEqual(os.path.exists(tempfile), True)
         t.ReadSacHeader(tempfile)
         self.assertEqual((t.hf is not None), True)
         t.SetHvalue("kstnm", "spoff")
         self.assertEqual(t.GetHvalue('kstnm'), 'spoff   ')
         t.WriteSacHeader(tempfile)
         t.SetHvalueInFile(tempfile, "kcmpnm", 'Z       ')
         self.assertEqual(t.GetHvalueFromFile(tempfile, "kcmpnm"),
                          'Z       ')
         self.assertEqual(
             SacIO(tempfile, headonly=True).GetHvalue('kcmpnm'), 'Z       ')
         self.assertEqual(t.IsValidSacFile(tempfile), True)
         self.assertEqual(t.IsValidXYSacFile(tempfile), False)
         self.assertEqual(SacIO().GetHvalueFromFile(sacfile, 'npts'), 100)
         self.assertEqual(SacIO(sacfile).GetHvalue('npts'), 100)
def updateSacHeader(sac,dir,latStaz,lonStaz,evla,evlo):

    # load sac trace, update headers, write and close
    tr = SacIO(dir + os.sep + sac)
    tr.SetHvalue('evla',evla)
    tr.SetHvalue('evlo',evlo)
    tr.SetHvalue('stla',latStaz)
    tr.SetHvalue('stlo',lonStaz)
    (distD,Az,Baz,distkm)=dlaz(evla,evlo,latStaz,lonStaz)
    tr.SetHvalue('dist',distkm)
    tr.SetHvalue('gcarc',distD)
    tr.SetHvalue('baz',Baz)
    tr.SetHvalue('az',Az)
    tr.WriteSacHeader(dir + os.sep + sac)

    return 1
Exemple #3
0
 def test_getdist(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         t = SacIO(tfile)
         t.SetHvalue('evla', 48.15)
         t.SetHvalue('evlo', 11.58333)
         t.SetHvalue('stla', -41.2869)
         t.SetHvalue('stlo', 174.7746)
         t.SetHvalue('lcalda', 1)
         t.WriteSacBinary(tempfile)
         t2 = SacIO(tempfile)
     b = np.array([18486532.5788 / 1000., 65.654154562, 305.975459869],
                  dtype=native_str('>f4'))
     self.assertEqual(t2.GetHvalue('dist'), b[0])
     self.assertEqual(t2.GetHvalue('az'), b[1])
     self.assertEqual(t2.GetHvalue('baz'), b[2])
def updateSacHeaderEida(sacFiles, sourceFileFormat, outFileFormat, dir,evla,evlo): 

   # this operation works only if the source seed file is a fseed
   if sourceFileFormat == "FSEED" and outFileFormat == "SAC":

      # open rdseed.station file
#     file = open(dir + "/rdseed.stations")


      # loop over sac files to find stations
      for sac in sacFiles:

          info = sac.split('.')
          staz = info[0]

          #loop over line of rdseed.stations
          file = open(dir + os.sep + "rdseed.stations")
          for line in file:
             a = line.split(' ')
             station = a[0]
             network = a[1]
             latStaz = eval(a[2])
             lonStaz = eval(a[3])
             # if stations are the same, upadte header and exit this loop
             if station == staz:
  
                # load sac trace, update headers, write and close
                tr = SacIO(dir + os.sep + sac)            
                tr.SetHvalue('evla',evla)
                tr.SetHvalue('evlo',evlo)
                tr.SetHvalue('stla',latStaz)
                tr.SetHvalue('stlo',lonStaz)
                (distD,Az,Baz,distkm)=dlaz(evla,evlo,latStaz,lonStaz)
                tr.SetHvalue('dist',distkm)
                tr.SetHvalue('gcarc',distD)
                tr.SetHvalue('baz',Baz)
                tr.SetHvalue('az',Az)
                #distkm=dlaz(evla,evlo,latStaz,lonStaz)
                tr.WriteSacHeader(dir + os.sep + sac)
                break
                 
   return sacFiles
Exemple #5
0
 def test_issue171(self):
     """
     Test for issue #171.
     """
     tr = read()[0]
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         tr.write(tempfile, format="SAC")
         trace = SacIO(tempfile)
         trace.SetHvalue('stel', 91.0)
         trace.WriteSacHeader(tempfile)
         trace = SacIO(tempfile)
Exemple #6
0
def export_sac(db, filename, pair, components, filterid,corr,ncorr=0,sac_format=None,maxlag=None,cc_sampling_rate=None):
    if sac_format is None:
        sac_format=get_config(db,"sac_format")
    if maxlag is None:
        maxlag = float(get_config(db,"maxlag"))
    if cc_sampling_rate is None:
        cc_sampling_rate = float(get_config(db,"cc_sampling_rate"))
    try:
        os.makedirs(os.path.split(filename)[0])
    except:
        pass
    filename += ".SAC"
    mytrace = Trace(data=corr)
    mytrace.stats['station'] = pair
    mytrace.stats['sampling_rate'] = cc_sampling_rate


    st = Stream(traces = [mytrace,])            
    st.write(filename,format='SAC')
    tr = SacIO(filename)
    if sac_format == "doublets":
        tr.SetHvalue('A',120)
    else:
        tr.SetHvalue('B',-maxlag)
        tr.SetHvalue('DEPMIN',np.min(corr))
        tr.SetHvalue('DEPMAX',np.max(corr))
        tr.SetHvalue('DEPMEN',np.mean(corr))
        tr.SetHvalue('SCALE',1)
        tr.SetHvalue('NPTS',len(corr))
    tr.WriteSacBinary(filename)
    del st, tr
    return
def write2sac(d, header, evla, evlo, evdp, mag, output):
    sacio = SacIO()
    sacio.fromarray(d)
    # set the date to today
    sacio.SetHvalue('nzyear', header['nzyear'])
    sacio.SetHvalue('nzjday', header['nzjday'])
    sacio.SetHvalue('delta', 0.02)
    sacio.SetHvalue('nzhour', header['nzhour'])
    sacio.SetHvalue('nzmin', header['nzmin'])
    sacio.SetHvalue('nzsec', header['nzsec'])
    sacio.SetHvalue('nzmsec', header['nzmsec'])
    sacio.SetHvalue('kstnm', header['stnm'])
    sacio.SetHvalue('stla', header['stla'])
    sacio.SetHvalue('stlo', header['stlo'])
    sacio.SetHvalue('kcmpnm', header['comp'])
    sacio.SetHvalue('evla', evla)
    sacio.SetHvalue('evlo', evlo)
    sacio.SetHvalue('evdp', evdp)
    sacio.SetHvalue('mag', mag)

    #dist = sacio.GetHvalue('dist')
    #print dist
    dist = gps2DistAzimuth(stla, stlo, evla, evlo)[0] / 1000.
    sacio.SetHvalue('dist', dist)

    #sacio.SetHvalue('knetwk','phones')
    #sacio.SetHvalue('kstnm',phone)
    #sacio.SetHvalue('kcmpnm',comp)
    #sacio.SetHvalue('kevnm',loc)
    #sacio.SetHvalue('kuser0',test + ' test')
    #sacio.SetHvalue('kuser0',version)
    #sacio.SetHvalue('kuser1',brand)
    #set the type of the dependent variable as acceleration nm/sec/sec
    #sacio.SetHvalue('idep',8)

    sacio.WriteSacBinary(output)
def write2sac(d, header, output):
    '''
    Function to write the data and header to sac files
    Inputs:
    d - data array
    header - dictionary of the header info
    output - filename of the output sac file
    '''
    sacio = SacIO()
    sacio.fromarray(d)
    # set the date
    t = header['record_time']
    sacio.SetHvalue('nzyear', t.year)
    sacio.SetHvalue('nzjday', t.julday)
    sacio.SetHvalue('delta', 1. / header['df'])
    sacio.SetHvalue('nzhour', t.hour)
    sacio.SetHvalue('nzmin', t.minute)
    sacio.SetHvalue('nzsec', t.second)
    sacio.SetHvalue('kstnm', header['stnm'])
    sacio.SetHvalue('stla', header['stla'])
    sacio.SetHvalue('stlo', header['stlo'])
    sacio.SetHvalue('stel', header['stel'])
    sacio.SetHvalue('kcmpnm', header['comp'])
    sacio.SetHvalue('evla', header['evla'])
    sacio.SetHvalue('evlo', header['evlo'])
    sacio.SetHvalue('o', header['o'])
    sacio.SetHvalue('mag', header['mag'])
    sacio.SetHvalue('o', header['o'])
    #TRUE if DIST AZ BAZ and GCARC are to be calculated from st event coordinates.
    sacio.SetHvalue('LCALDA', 1)

    #set the type of the dependent variable as acceleration nm/sec/sec
    #sacio.SetHvalue('idep',8)

    sacio.WriteSacBinary(output)