#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 26 16:18:56 2023

@author: gmi522
"""
import matplotlib 
import matplotlib.pyplot as plt
from obspy.clients.fdsn import Client
from obspy import UTCDateTime

nDays = 1
T_START = 0     # length in seconds of data to plot before origin time
T_END = 60*60*24*nDays    # length in seconds of data to plot after origin time
Fhigh = 1.   

#   now get some station meta data, esp. lat & long 
namStationList = ['R6C8A',   # MMU
                  'S53C6',   # Beswick 
                  'R9098',   # Astley 
                  'R36CB']   # Cheadle Hulme 
namStationLabelList = ["Manchester Metropolitan University, Oxford Rd", 
                       "Connell 6th Form College, Beswick", 
                       "St Marys' High School, Astley", 
                       "St James' High School, Cheadle Hulme"]
netStation = 'AM'
locStation = '00'
chaStation = 'EHZ'      #   vertical component of geophone 
client = Client(base_url='https://data.raspberryshake.org')

#   define the time window
EQ_TIME = "2023-02-03T00:00:00"
dtEvent = UTCDateTime(EQ_TIME)
t1start = dtEvent + T_START
t1end = dtEvent + T_END

#   loop through the stations 
print("Getting responses...")
iPlot = 0     
for namStation in namStationList:
    print(namStation)
    #   get the response for this station
    inv = client.get_stations(t1start, 
                               network=netStation, 
                               station=namStation, 
                               location=locStation,
                      channel=chaStation, level="response")
    
    fig1, ax1 = plt.subplots(1, 1, figsize=(6,5))
    fig2, ax2 = plt.subplots(1, 1, figsize=(6,5))
    inv.plot_response(0.01, axes=[ax1, ax2], show=False)
    for child in ax1.get_children():
        if isinstance(child, matplotlib.text.Annotation):
            child.remove()
    ax1.grid(True) 
    ax1.set_xlim(0.05, 50.)
    ax1.set_xlabel('Frequency (Hz)')
    ax1.set_ylabel('Amplitude')
    ax1.set_title(namStation + '\n' + namStationLabelList[iPlot], loc='left')
    outfile='plotResponse_paper_' + namStation + '.png'
    fig1.savefig(outfile, dpi=300)
    iPlot += 1 
    