<jupyter_start><jupyter_text>Get events<jupyter_code>import wget

!rm *.ndk

url = 'http://www.ldeo.columbia.edu/~gcmt/projects/CMT/catalog/jan76_dec13.ndk'
wget.download( url )

years = range(6,18)
months  = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
for year in years:
    for month in months:
        url='http://www.ldeo.columbia.edu/~gcmt/projects/CMT/catalog/NEW_MONTHLY/20%02d/%s%02d.ndk' % (year, month, year)
        #print(url)
        wget.download( url )<jupyter_output><empty_output><jupyter_text>Parse ndk files to populate catalog<jupyter_code>import obspy
from obspy.geodetics.base import gps2dist_azimuth
from obspy.clients.syngine import Client
from obspy.taup import TauPyModel

cat = obspy.read_events('jan76_dec13.ndk')
cat = cat.filter("time > 1990-01-01T00:00",'time < 2006-01-01T00:00').filter("magnitude >= 5.8")

for year in years:
    for month in months:
        tmp = obspy.read_events('%s%02d.ndk' % (month, year))
        cat = cat + tmp.filter('magnitude >= 5.8')
        
print(cat)
from obspy.clients.fdsn.client import Client
from obspy.geodetics.base import gps2dist_azimuth
from sph_loc import sph_loc

import pandas as pd
import sqlite3

#Connect to Database
conn = sqlite3.connect("ss.db")
cur  = conn.cursor()

def save_dataframe(dataframe, if_exists = 'fail'):
    #dataframe.to_csv(file, index = False, mode = 'a', header = header)
    dataframe.to_sql("bouncepoints_full", conn, if_exists=if_exists,
                     dtype = {'ID': 'INTEGER',
                                'ID_Full:': "TEXT",
                                'Event': 'TEXT',
                                'Network': 'TEXT',
                                'Station':'TEXT',
                                'Range':'REAL',
                                'Azimuth':'REAL',
                                'BAzimuth':'REAL',
                                'EvtLat':'REAL',
                                'EvtLon':'REAL',
                                'EvtDep':'REAL',
                                'EvtMag':'REAL',
                                'EvtStr':'REAL',
                                'EvtDip':'REAL',
                                'EvtRak':'REAL',
                                'StaLat':'REAL',
                                'StaLon':'REAL',
                                'MidLat':'REAL',
                                'MidLon':'REAL',
                                'EvtTime': 'TEXT'}
                    )
    print('saved')
    
def init_dataframe():
    dataframe = pd.DataFrame(columns =['ID', 'ID_Full', 'Event', 'Network', 'Station', 'Range', 'Azimuth',
                                       'BAzimuth', 'EvtLat','EvtLon', 'EvtDep', 'EvtMag','EvtStr','EvtDip','EvtRak',
                                       'StaLat', 'StaLon', 'MidLat','MidLon', 'EvtTime'],
                            )
    
    return dataframe

df = init_dataframe()
    
save_dataframe(df, if_exists = 'replace')
    
irow = 0

client = Client('IRIS')

for evt in cat:

    etag = evt.event_descriptions[1].text
    
    etime = evt.preferred_origin().time
    elat  = evt.preferred_origin().latitude
    elon  = evt.preferred_origin().longitude
    edep  = evt.preferred_origin().depth/1000.0
    emag  = evt.preferred_magnitude().mag
    estr  = evt.focal_mechanisms[0].nodal_planes.nodal_plane_1.strike
    edip  = evt.focal_mechanisms[0].nodal_planes.nodal_plane_1.dip
    erak  = evt.focal_mechanisms[0].nodal_planes.nodal_plane_1.rake
    
    try:
        inv = client.get_stations(network='*', station='*', channel='LH?', level='station',
                                  startbefore = etime, endafter = etime+60*60,
                                  includerestricted = False, includeavailability=True)
    except:
        print('Request Error')
        continue
        
    for net in inv:
        for sta in net:
            if sta.restricted_status != 'open':
                continue
            
            slat = sta.latitude
            slon = sta.longitude
            
            delm, az, baz = gps2dist_azimuth(elat, elon, slat, slon)
            deldeg = delm/1000./111.11
            if deldeg > 55:
                mlat, mlon = sph_loc(elat,elon,deldeg/2.,az)
                
                if mlon > 180.:
                    mlon = mlon - 360.
                
                fullid = etag + '.' + net.code + '.' + sta.code
                df.loc[irow] = [irow, fullid, etag, net.code, sta.code, deldeg, az, baz, elat, elon, edep, emag, estr, edip, erak, slat, slon, mlat, mlon, str(etime)]
                irow += 1
                if irow % 1000 == 0:  #write and drop memory load
                    save_dataframe(df, 'append')
                    df = init_dataframe()

save_dataframe(df, 'append')

conn.close()<jupyter_output>saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
saved
save[...]
{'fraction_non_alphanumeric': 0.10966191865453921, 'fraction_numerical': 0.015960185344087868, 'mean_word_length': 1.9598781107160996, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '49224', 'license': 'MIT', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1880, 'n_words': 452, 'path': 'ss_precursors/Calculate_SS_Bounce_Points.ipynb', 'repo_name': 'nmancinelli/ss-precursors', 'size': '5827'}