from obspy.core.event import read_events
import pandas as pd
import os
import sys
df = pd.DataFrame(columns=['evid', 'Mw', 'Ml', 'Md', 'Mw_errors', 'Ml_errors', 'Md_errors',
'Mw_evaluation_mode', 'Ml_evaluation_mode', 'Md_evaluation_mode',
'Mw_evaluation_status', 'Ml_evaluation_status', 'Md_evaluation_status',
'origin_time', 'evlat', 'evlon', 'evdp_km', 'evmag'])
print(df)
# cat SJBPK_hypoDD_stY1984_etY2019_poly1_Mmin1.0.out | awk '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13}' >! SJBPK_hypoDD_stY1984_etY2019_poly1_Mmin1.0.out.NCSSformat2
data_dir = "http://ncedc.org/ftp/outgoing/taira/CCT/NEHRP"
#data_dir = "./"
# input csv. "," separtion file
loc_fi = data_dir+"/catalog.out_test.csv"
#loc_fi = data_dir+"/catalog.out.csv"
#loc_fi = data_dir+"/catalog_m1.out.csv"
# output csv
csv_fi = "test_mag.csv"
#loc_fi = sys.argv[1]
#csv_fi = sys.argv[2]
df_loc= pd.read_csv(loc_fi,
sep=",",names=["time1", "time2", "lat", "lon", "dep", "mag","mag_type", "Nst", "Gap", "Clo", "RMS", "SRC", "evid"],header=None)
#print(df_loc)
#evid = "73584926" # ALV
#evid = "73730876"
#evid = "73728431"
#evid = "73716646"
#vid = "73719205"
def mag_catalog(evid, df):
#cat = read_events("fdsnws-event_2022-05-09T00_30_43Z.xml")
#cat = read_events("http://service.ncedc.org/fdsnws/event/1/query?catalog=NCSS&eventid=73584926&includeallmagnitudes=true&includearrivals=false&includemechanisms=false&orderby=time&format=xml")
cat = read_events("http://service.ncedc.org/fdsnws/event/1/query?catalog=NCSS&eventid="+evid+"&includeallmagnitudes=true&includearrivals=false&includemechanisms=false&orderby=time&format=xml")
print(cat)
event = cat[0]
origin = event.origins[0]
origin_time = origin.time
evlat = origin.latitude
evlon = origin.longitude
evdp_km = origin.depth / 1000
evmag = event.magnitudes[0].mag
#print(evid)
df = df.append({'evid': str(evid)}, ignore_index=True)
index = df[df["evid"] == evid].index[0]
df.at[index, 'origin_time'] = origin_time
df.at[index, 'evlat'] = evlat
df.at[index, 'evlon'] = evlon
df.at[index, 'evdp_km'] = evdp_km
df.at[index, 'evmag'] = evmag
#print(df)
str_out = ""+evid
# looking all mag info.
for magnitude in cat.events[0]['magnitudes']:
magnitude_type = magnitude.magnitude_type
mag = magnitude.mag
mag_errors = magnitude.mag_errors.uncertainty
evaluation_mode = magnitude.evaluation_mode
evaluation_status = magnitude.evaluation_status
str_out = str_out+","+magnitude_type+","+str(mag)+","+str(mag_errors)+","+evaluation_mode+","+evaluation_status
#index= df.query('evid == '+evid).index[0]
index = df[df["evid"] == evid].index[0]
#print("# index = ", index)
#print(evid+","+magnitude_type+","+str(mag)+","+str(mag_errors)+","+evaluation_mode+","+evaluation_status)
#dfObj = dfObj.append({'User_ID': 23, 'UserName': 'Riti', 'Action': 'Login'}, ignore_index=True)
#dfObj = dfObj.append({'evid': str(evid), magnitude_type: mag }, ignore_index=True)
df.at[index, magnitude_type] = mag
df.at[index, magnitude_type+'_errors'] = mag_errors
df.at[index, magnitude_type+'_evaluation_mode'] = evaluation_mode
df.at[index, magnitude_type+'_evaluation_status'] = evaluation_status
return df
#print(str_out)
print(df)
for evid in df_loc['evid']:
#print(evid)
df2 = mag_catalog(evid=str(evid), df=df)
df = df2.copy()
#df = mag_catalog(evid=evid, df=df)
print(df)
df.to_csv(csv_fi)