# module

import obspy

def peak_values(tr):
	if tr.stats.channel[1] != 'H':
		raise Exception("Record probably not in velocity (channel %s), other possibilities are not implemented yet." % tr.stats.channel)

	if tr.stats.channel[0] == 'H': # broad band
		tr.filter("highpass", freq=0.025) # lepsi vzit rohovou frekvenci pristroje
	else: # short period
		tr.filter("highpass", freq=1)
	tr.detrend(type='demean')

	acc = tr.copy()
	acc.differentiate()
	disp = tr.copy()
	disp.integrate()

	pgv = abs(tr.max())
	pga = abs(acc.max())

	disp.filter("highpass", freq=0.025) # lepsi vzit rohovou frekvenci pristroje
	pgd = abs(disp.max())

	acc_filt = acc.copy()
	vel_filt = tr.copy()
	acc_filt.filter("lowpass", freq=1)
	pga_0_1_Hz = abs(acc_filt.max())
	vel_filt.filter("lowpass", freq=1)
	pgv_0_1_Hz = abs(vel_filt.max())

	acc_filt = acc.copy()
	vel_filt = tr.copy()
	acc_filt.filter("bandpass", freqmin=1, freqmax=4)
	pga_1_4_Hz = abs(acc_filt.max())
	vel_filt.filter("bandpass", freqmin=1, freqmax=4)
	pgv_1_4_Hz = abs(vel_filt.max())

	acc_filt = acc.copy()
	vel_filt = tr.copy()
	acc_filt.filter("bandpass", freqmin=4, freqmax=16)
	pga_4_16_Hz = abs(acc_filt.max())
	vel_filt.filter("bandpass", freqmin=4, freqmax=16)
	pgv_4_16_Hz = abs(vel_filt.max())

	return (pga, pgv, pgd, pga_0_1_Hz, pgv_0_1_Hz, pga_1_4_Hz, pgv_1_4_Hz, pga_4_16_Hz, pgv_4_16_Hz)
