Plotting seismic noise. ObsPy, numpy, pandas, matplotlib are required.
import numpy as np
import matplotlib as mpl
import pandas as pd
import obspy as op
print("# numpy version = ",np.__version__)
print("# matplotlib version = ",mpl.__version__)
print("# pandas version = ",pd.__version__)
print("# numpy version = ",np.__version__)
print("# obspy version = ",op.__version__)
import matplotlib.pyplot as plt
from obspy import UTCDateTime
This example uses a 4.0-14.0 passband
band = "4.0-14.0"
This example uses the ftp directory
csv_dir = "http://ncedc.org/ftp/outgoing/taira/EastBay_seismic_noise"
This example uses BK.BRK.00.HHZ
net = "BK" # network ID
sta = "BRK" # station ID
#sta = "BKS"
loc = "00" # location ID
com = "HHZ" # channel ID
nslc = net+"."+sta+"."+loc+"."+com
print("# nslc = ", nslc)
This csv file includes seismic noise at several different frequency bands. Timestamp is UTC
csv_fi = csv_dir+"/"+net+"."+sta+"."+loc+"."+com+".csv"
This rs csv file includes 1-day median noise level (from 6am through 4pm at local time) at the 4.0-14.0 Hz. Timestamp is UTC
#http://ncedc.org/ftp/outgoing/taira/EastBay_seismic_noise/BK.BRK.00.HHZ_4.0-14.0_rs.csv
rs_csv_fi = csv_dir+"/"+net+"."+sta+"."+loc+"."+com+"_"+band+"_rs.csv"
This RS csv file is output from Thomas's code
rs_csv_data = pd.read_csv(rs_csv_fi,
sep=",", index_col=0)
#print(rs_csv_data)
rs_csv_data['time'] = pd.to_datetime(rs_csv_data.index)
#print(rs_csv_data['time'])
print(rs_csv_data)
csv_data = pd.read_csv(csv_fi,
sep=",", index_col=0)
csv_data['time'] = pd.to_datetime(csv_data.index)
print(csv_data)
not fully understand but I follow an example page from https://qiita.com/StingQian/items/0030ef9e3c23cce17c76 (in Japanese)
csv_data = csv_data.set_index('time')
csv_data.index = pd.to_datetime(csv_data.index)
csv_data.index
This function from Thomas's code. convert UTC to local time zone and then reampling with a moving mean process
def localize_tz_and_reindex(df, freq="15Min", time_zone = "Europe/Brussels"):
#return df.copy().tz_localize("UTC").dropna().tz_convert(time_zone).tz_localize(None).resample(freq).mean().to_frame()
return df.copy().tz_localize("UTC").dropna().tz_convert(time_zone).tz_localize(None).resample(freq).mean()
our time zone is "America/Los_angelse"
time_zone = 'America/Los_Angeles'
This 30-min-mean used in Thomas's code
csv_data_30min_mean = localize_tz_and_reindex(csv_data, "30Min", time_zone = time_zone)
rs_local = csv_data_30min_mean.copy().between_time("6:00", "16:00")
rs_local = rs_local.resample("1D" ).median().tshift(12, "H")
This is not include in Thomas's code. I added it so that we can plot resuls with local timestamp
rs_local.index = rs_local.index.tz_localize('America/Los_Angeles')
plt.figure(figsize=(20, 6))
# 1e+9 for conveting meter -> nanometer
#plt.plot(csv_data['time'], csv_data[band]*1e+9, label=com)
plt.plot(csv_data.index, csv_data[band]*1e+9, label=com)
plt.plot(rs_csv_data['time'], rs_csv_data[band]*1e+9, label="$\overline{"+com+"}$ (6h-16h)")
plt.plot(rs_local.index, rs_local[band]*1e+9, label="REDO $\overline{"+com+"}$ (6h-16h)")
#plt.plot(rs_local_test.index, rs_local_test[band]*1e+9, label="REDO $\overline{"+com+"}$ (6h-16h)")
#plt.ylim(0.75,3)
ymin = 0
ymax = np.nanpercentile(csv_data[band]*1e+9,95)*1.5
plt.ylim(ymin, ymax)
#2020-03-17 00:00":'BayArea shelter-in-place order' local time
plt.vlines(x=(UTCDateTime("2020-03-17 07:00").datetime), ymin=ymin, ymax=ymax, color="red", linewidth = 2.0, linestyle = "--")
#plt.xlim(csv_data['time'][0], csv_data['time'][-1])
#plt.xlim(csv_data.index[0], csv_data.index[-1])
plt.xlim(csv_data.index[43000], csv_data.index[45000])
#plt.xlim(csv_data.index[43000], csv_data.index[43500])
#plt.xlim(csv_data.index[43000], csv_data.index[43200])
plt.ylabel("Displacement (nm)")
plt.title("Local time plot Seismic Noise for "+nslc+" - Filter ["+band+"] Hz")
plt.legend(loc = "upper right")
plt.savefig(nslc+"_"+band+".png")
index=401 #
index=252 #
#rs_csv_data['time'][index]
#rs_local.index[index]
#rs_local[band][index]
#rs_csv_data[band][index]
rs_csv_data[band][index]-rs_local[band][index]