def addNetDemo(fSrcDir, static_path): STATIC_PATH = static_path sDenDir = 'networks' fDenDir = os.path.join(STATIC_PATH, sDenDir) mkfile(fDenDir, 0) # updateSql() # 删除旧数据并更新数据库 all_files = [] all_paths = [] all_files, all_paths = show_path(fSrcDir, all_files, all_paths) for i in range(0, len(all_files)): file = all_files[i] path = all_paths[i] if file.count('.') >= 6: dayCount = countDay_1OfYear(datetime.date.today()) (NetCode, StaCode, LocCode, ChCode, DataCode, nYear, nDay) = file.split('.') if (len(NetCode) <= 2 and len(StaCode) <= 5 and len(LocCode) <= 2 and len(ChCode) <= 3 and DataCode == 'D' and len(nYear) <= 4 and len(nDay) <= 3 and int(nDay) == dayCount): net = Network(NetCode, NetCode, fSrcDir, sDenDir, 3).get_or_create_Network() sta = Station(net, StaCode, StaCode).get_or_create_Station() cDigitizerInfo = DigitizerInfo('TDE-324', '10Vpp', '100Hz', 'Linear') (bRet, AD, gain, rate, filter) = cDigitizerInfo.getDigitizerInfo() if not bRet: print('Digitizer not found!') continue cSensorInfo = SensorInfo('TMA-33') (bRet, sensor, sensorinfo) = cSensorInfo.getSensorInfo() if not bRet: print('Sensor not found!') continue adsensor = ADSensor(filter, sensorinfo).get_ADSensor() sta_adsensor = Sta_ADSensor( sta, adsensor).get_or_create_Sta_ADSensor() ch = Channel(sta_adsensor, LocCode, ChCode).get_or_create_CH() sDenDir2 = sDenDir + '/' + NetCode fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) sDenDir2 = sDenDir2 + '/' + StaCode fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) sDenDir2 = sDenDir2 + '/' + nYear fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) sDenDir2 = sDenDir2 + '/' + nDay fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) from obspy import read # from obspy.io.xseed import Parser from obspy.signal import PPSD from obspy.imaging.cm import pqlx try: st = read(path) except Exception as ex: print('%s数据读取错误\n' % file, ex) continue ChName = NetCode + '.' + StaCode + '.' + LocCode + '.' + ChCode + '.' + nYear + '.' + nDay outfile1 = fDenDir + '/' + ChName + '.day_wave.png' outfile2 = fDenDir + '/' + ChName + '.day_wave.low_pass_0.2Hz.png' outfile3 = fDenDir + '/' + ChName + '.day_wave.high_pass_0.2Hz.png' outfile4 = fDenDir + '/' + ChName + '.ppsd.png' outfile5 = fDenDir + '/' + ChName + '.spectrogram.png' print(NetCode, StaCode, LocCode, ChCode, DataCode, nYear, nDay) st.plot(size=(1600, 1200), tick_format='%I:%M:%p', type="dayplot", interval=30, right_vertical_labels=True, vertical_scaling_range=st[0].data.std() * 20, one_tick_per_line=True, color=["r", "b", "g"], show_y_UTC_label=True, title=ChName, time_offset=8, outfile=outfile1) st2 = st.copy() st.filter("lowpass", freq=0.2, corners=2) st.plot(size=(1600, 1200), tick_format='%I:%M:%p', type="dayplot", interval=30, right_vertical_labels=True, vertical_scaling_range=st[0].data.std() * 20, one_tick_per_line=True, color=["r", "b", "g"], show_y_UTC_label=True, title=ChName + '.low_pass 0.2Hz', time_offset=8, outfile=outfile2) st2.filter("highpass", freq=0.2) st2.plot( size=(1600, 1200), tick_format='%I:%M:%p', type="dayplot", interval=30, right_vertical_labels=True, vertical_scaling_range=st2[0].data.std() * 20, one_tick_per_line=True, color=["r", "b", "g"], show_y_UTC_label=True, # events={"min_magnitude": 5}, title=ChName + '.high_pass 0.2Hz', time_offset=8, outfile=outfile3) paz = {} paz['zeros'] = [] paz['zeros'] = Zeros(sensorinfo).getZero() paz['poles'] = [] paz['poles'] = Poles(sensorinfo).getPole() if 2000 <= cSensorInfo.getField('IMainType', sensor) <= 3000: paz['zeros'].append(complex(0., 0)) paz['gain'] = cSensorInfo.getField('IGainNormalization', sensorinfo) paz['sensitivity'] = cSensorInfo.getField('IGain', sensorinfo) \ * cDigitizerInfo.getField('sensitivity', filter) print(paz) st = read(path) # print(st) ppsd = PPSD(st[0].stats, paz) ppsd.add(st) # print(ppsd.times_data) # print('len=',len(ppsd.times_data),ppsd.times_data[0][0],ppsd.times_data[0][1]) ppsd.plot(outfile4, xaxis_frequency=True, cmap=pqlx) ppsd.plot_spectrogram(filename=outfile5, cmap='CMRmap_r') if cSensorInfo.getField('IMainType', sensor) < 2000: outfile6 = fDenDir + '/' + ChName + '.1-2s.sp.png' ppsd.plot_temporal(1.414, filename=outfile6) elif 2000 <= cSensorInfo.getField('IMainType', sensor) < 3000: # 加速度模式) outfile6 = fDenDir + '/' + ChName + '.1-2Hz.sp.png' ppsd.plot_temporal(.707, filename=outfile6) fBlankTime = 0. for i in range(1, len(ppsd.times_data)): # 1个整时间段说明未丢数 dt = (ppsd.times_data[i][0] - ppsd.times_data[i - 1][1]) if dt < 0: print(dt, ppsd.times_data[i][0], ppsd.times_data[i - 1][1]) else: fBlankTime += dt runrate = 1.0 - fBlankTime / 86400. date = datetime.date(ppsd.times_data[0][0].year, ppsd.times_data[0][0].month, ppsd.times_data[0][0].day) DayData(ch, date, runrate).set_or_create_Day_data() else: print(file, "Name is error.")
print( 'Run as: python3 %s "NN.SSSS.LL.CCC" [-plot=spec,temp,site,ppsd] [http://localhost:8080/] ' % argv[0]) print( ' or: python3 %s "NN.SSSS.LL.CCC" [-plot=spec,temp,site,ppsd] [data (e.g. mseed)] [metadata (e.g. fdsn.xml)]' % argv[0]) stream._cleanup() ids = [] for trace in stream: if trace.id in ids: continue ids += [trace.id] ppsd = PPSD(trace.stats, inventory, db_bins=(-200, -50, .5), period_step_octaves=0.125 / 2., ppsd_length=min([ int(trace.stats.npts / 4 / trace.stats.sampling_rate), 800 ])) ppsd.add(stream) if "site" in argv[-2]: sitemap(inventory) if "spec" in argv[-2]: ppsd.plot_spectrogram() if "temp" in argv[-2]: ppsd.plot_temporal([10, 0.1, 1]) ppsd.plot(show_mode=True, cmap=pqlx)
def addNetDemo(fSrcDir,nNetMode=1,sensortype='TMA-33'): # 建立顶层根目录 STATIC_PATH = os.path.join(os.path.dirname(__file__), 'static') sDenDir = 'networks' fDenDir = os.path.join(STATIC_PATH, sDenDir) mkfile(fDenDir, 0) file_list = [] path_list = [] (file_list,path_list) = show_path (fSrcDir,file_list,path_list) for i in range(len(file_list)): file = file_list[i] path = path_list[i] dayCount = countDay_1OfYear(datetime.date.today()) num = file.count('.') if (num >= 6): (NetCode,StaCode,LocCode,ChCode,DataCode,nYear,nDay) = file.split('.') # print(NetCode, StaCode, LocCode, ChCode, DataCode, nYear, nDay) # print(len(NetCode), len(StaCode), len(LocCode), len(ChCode), len(DataCode), len(nYear), len(nDay)) if (len(NetCode)<=2 and len(StaCode)<=5 and len(LocCode)<=2 and len(ChCode)<=3 and DataCode=='D' and len(nYear)<=4 and len(nDay)<=3 and int(nDay) == dayCount): net = get_or_create_Network(NetCode,NetCode,fSrcDir,sDenDir,nNetMode) sta = get_or_create_Station(net,StaCode,StaCode) (bRet,AD,gain,rate,filter) = get_DigitizerInfo('TDE-324','10Vpp','100Hz','Linear') if bRet==False: continue (bRet,sensor,sensorinfo) = get_SensorInfo(sensortype, '0-200Hz', '1.0225V/M/S**2') if bRet==False: continue ADSensor = get_or_create_ADSensor(filter,sensorinfo) StaADSensor = get_or_create_Sta_ADSensor(sta,ADSensor) ch = get_or_create_CH(StaADSensor,LocCode, ChCode) # 以上,添加1个台站的逻辑确实很复杂 sDenDir2 = sDenDir + '/' + NetCode fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) sDenDir2 = sDenDir2 + '/' + StaCode fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) sDenDir2 = sDenDir2 + '/' + nYear fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) sDenDir2 = sDenDir2 + '/' + nDay fDenDir = os.path.join(STATIC_PATH, sDenDir2) mkfile(fDenDir, 0) from obspy import read #from obspy.io.xseed import Parser from obspy.signal import PPSD from obspy.imaging.cm import pqlx try: st = read(path) except Exception as ex: print('读取数据错误\n', ex) continue ChName = NetCode + '.' + StaCode + '.' + LocCode + '.' + ChCode + '.' + nYear + '.' + nDay outfile1 = fDenDir + '/' + ChName + '.day_wave.png' outfile2 = fDenDir + '/' + ChName + '.day_wave.low_pass_0.2Hz.png' outfile3 = fDenDir + '/' + ChName + '.day_wave.high_pass_0.2Hz.png' outfile4 = fDenDir + '/' + ChName + '.ppsd.png' outfile5 = fDenDir + '/' + ChName + '.spectrogram.png' print(NetCode, StaCode, LocCode, ChCode, DataCode, nYear, nDay) st.plot(size=(1600, 1200), tick_format='%I:%M:%p', type="dayplot", interval=30, right_vertical_labels=True, vertical_scaling_range=st[0].data.std() * 20, one_tick_per_line=True, color=["r", "b", "g"], show_y_UTC_label=True, title=ChName,time_offset=8, outfile=outfile1) st2 = st.copy() st.filter("lowpass", freq=0.2, corners=2) st.plot(size=(1600, 1200), tick_format='%I:%M:%p', type="dayplot", interval=30, right_vertical_labels=True, vertical_scaling_range=st[0].data.std() * 20, one_tick_per_line=True, color=["r", "b", "g"], show_y_UTC_label=True, title=ChName + '.low_pass 0.2Hz',time_offset=8, outfile=outfile2) st2.filter("highpass", freq=0.2) st2.plot(size=(1600, 1200), tick_format='%I:%M:%p', type="dayplot", interval=30, right_vertical_labels=True, vertical_scaling_range=st2[0].data.std() * 20,one_tick_per_line=True, color=["r", "b", "g"], show_y_UTC_label=True, # events={"min_magnitude": 5}, title=ChName+ '.high_pass 0.2Hz', time_offset=8, outfile=outfile3) paz = {} paz['zeros'] = [] for zero in Zero.objects.filter(sensor_info=sensorinfo).order_by('id'): paz['zeros'].append(complex(zero.fReal, zero.fImag)) paz['poles'] = [] for pole in Pole.objects.filter(sensor_info=sensorinfo).order_by('id'): paz['poles'].append(complex(pole.fReal, pole.fImag)) if (2000<=sensor.IMainType and sensor.IMainType<3000): # 加速度模式 paz['zeros'].append(complex(0.,0)) paz['gain'] = sensorinfo.IGainNormalization paz['sensitivity'] = sensorinfo.IGain * filter.sensitivity #print(paz) st = read(path) #print(st) ppsd = PPSD(st[0].stats, paz) ppsd.add(st) #print(ppsd.times_data) #print('len=',len(ppsd.times_data),ppsd.times_data[0][0],ppsd.times_data[0][1]) ppsd.plot(outfile4, xaxis_frequency=True, cmap=pqlx) ppsd.plot_spectrogram(filename=outfile5, cmap='CMRmap_r') if (sensor.IMainType<2000): outfile6 = fDenDir + '/' + ChName + '.1-2s.sp.png' ppsd.plot_temporal(1.414, filename=outfile6) elif (2000<=sensor.IMainType and sensor.IMainType<3000): # 加速度模式) outfile6 = fDenDir + '/' + ChName + '.1-2Hz.sp.png' ppsd.plot_temporal(.707, filename=outfile6) fBlankTime = 0. for i in range(1,len(ppsd.times_data)): # 1个整时间段说明未丢数 dt = (ppsd.times_data[i][0] - ppsd.times_data[i-1][1]) if (dt < 0): print(dt,ppsd.times_data[i][0],ppsd.times_data[i-1][1]) else: fBlankTime += dt runrate = 1.0 - fBlankTime / 86400. date = datetime.date(ppsd.times_data[0][0].year,ppsd.times_data[0][0].month,ppsd.times_data[0][0].day) set_or_create_Day_data(ch,date,runrate) else: print(file , "Name is error.")
tr.remove_response(inventory=inv, pre_filt=pre_filt, output="DISP", water_level=60, plot=True) from obspy.io.xseed import Parser from obspy.signal import PPSD st = read("https://examples.obspy.org/BW.KW1..EHZ.D.2011.038") tr = st.select(id="BW.KW1..EHZ")[0] inv = read_inventory("https://examples.obspy.org/BW_KW1.xml") ppsd = PPSD(tr.stats, metadata=inv) ppsd.add(st) ppsd.plot() ppsd.plot_temporal([0.1, 1, 10]) ppsd.plot_spectrogram() from obspy.imaging.cm import obspy_sequential from obspy.signal.tf_misfit import cwt st = obspy.read() tr = st[0] npts = tr.stats.npts dt = tr.stats.delta t = np.linspace(0, dt * npts, npts) f_min = 1 f_max = 50 scalogram = cwt(tr.data, dt, 8, f_min, f_max)