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 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)
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
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