import module

In [1]:
from obspy.core.event import read_events
import pandas as pd
import os
import sys

pandas dataframe

In [2]:
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)
Empty 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]
Index: []

Input file and output file

In [3]:
# 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)
In [4]:
#evid = "73584926" # ALV
#evid = "73730876"
#evid = "73728431"

#evid = "73716646"
#vid = "73719205"

function to get mag info

In [5]:
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)
In [6]:
print(df)
Empty 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]
Index: []
In [7]:
for evid in df_loc['evid']:
    #print(evid)
    df2 = mag_catalog(evid=str(evid), df=df)
    df = df2.copy()
1 Event(s) in Catalog:
2020-03-09T02:59:08.860000Z | +40.392, -125.094 | 5.77 Mw | manual
<ipython-input-5-92aa675cf91c>:15: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  df = df.append({'evid': str(evid)}, ignore_index=True)
1 Event(s) in Catalog:
2020-03-18T13:09:31.000000Z | +40.751, -112.078 | 5.7 Mw | manual
<ipython-input-5-92aa675cf91c>:15: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  df = df.append({'evid': str(evid)}, ignore_index=True)
1 Event(s) in Catalog:
2020-03-18T22:08:20.920000Z | +40.347, -124.456 | 5.21 Mw | manual
<ipython-input-5-92aa675cf91c>:15: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  df = df.append({'evid': str(evid)}, ignore_index=True)
In [8]:
#df = mag_catalog(evid=evid, df=df)

check output df

In [9]:
print(df)
       evid    Mw    Ml    Md Mw_errors Ml_errors Md_errors  \
0  73351710  5.77  5.65  5.58      None     0.201     0.245   
1  73355550   5.7   NaN   NaN      None       NaN       NaN   
2  73355700  5.21  5.03  5.23      None     0.396      0.22   

  Mw_evaluation_mode Ml_evaluation_mode Md_evaluation_mode  \
0             manual             manual             manual   
1             manual                NaN                NaN   
2             manual          automatic             manual   

  Mw_evaluation_status Ml_evaluation_status Md_evaluation_status  \
0             reviewed             reviewed             reviewed   
1             reviewed                  NaN                  NaN   
2             reviewed          preliminary             reviewed   

                   origin_time      evlat       evlon evdp_km evmag  
0  2020-03-09T02:59:08.860000Z  40.391667 -125.093667    3.19  5.58  
1  2020-03-18T13:09:31.000000Z     40.751    -112.078    11.7   5.7  
2  2020-03-18T22:08:20.920000Z    40.3475 -124.456167   28.61  5.03  
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

save as csv

In [10]:
df.to_csv(csv_fi)
In [ ]:
    
In [ ]:
 
In [ ]: