Notebook to format picks for NonLinLoc runs

import numpy as np
import pandas as pd
import os

Notebook to format picks for NonLinLoc runs#

pd.read_pickle("../analyst_picks/pick_files/uw61500713_picks.pkl")
Channel Distance Azimuth Phase Arrival Time Status Residual Weight Agency
0 UW.BHW.EHZ. None 186.265786 P 2019-07-12 19:04:40.240 manual -0.08 0.13 UW
1 UW.BHW.EHZ. None 186.265786 S 2019-07-12 19:04:43.820 manual -0.03 0.13 UW
2 UW.LEOT.HNN. None 212.250402 S 2019-07-12 19:04:45.650 manual 0.27 0.13 UW
3 UW.EARN.ENN. None 184.597057 S 2019-07-12 19:04:45.660 manual 0.16 0.08 UW
4 UW.HTW.EHZ. None 107.868225 P 2019-07-12 19:04:41.310 manual 0.14 0.17 UW
5 UW.HTW.EHZ. None 107.868225 S 2019-07-12 19:04:45.160 manual -0.20 0.13 UW
6 UW.TOLT.ENZ. None 125.840942 P 2019-07-12 19:04:42.320 manual 0.02 0.13 UW
7 UW.TOLT.ENN. None 125.840942 S 2019-07-12 19:04:47.300 manual -0.07 0.08 UW
8 UW.TLW1.HNE. None 126.069173 S 2019-07-12 19:04:47.290 manual -0.12 0.17 UW
9 UW.JCW.EHZ. None 11.481605 P 2019-07-12 19:04:43.190 manual 0.17 0.17 UW
10 UW.JCW.ENN. None 11.481605 S 2019-07-12 19:04:48.670 manual 0.02 0.13 UW
11 UW.BERY.HHZ. None 353.740661 P 2019-07-12 19:04:44.060 manual 0.08 0.13 UW
12 UW.BERY.HHE. None 353.740661 S 2019-07-12 19:04:50.050 manual -0.31 0.13 UW
13 UW.GMW.EHZ. None 238.769249 P 2019-07-12 19:04:46.890 manual 0.00 0.03 UW
14 UW.GNW.HHZ. None 241.320785 P 2019-07-12 19:04:46.830 manual -0.06 0.07 UW
15 UW.GNW.HHN. None 241.320785 S 2019-07-12 19:04:55.220 manual -0.32 0.10 UW
16 UW.DOSE.HHN. None 257.595534 S 2019-07-12 19:04:55.950 manual -1.00 0.00 UW
17 UW.GPW.EHZ.01 None 66.335810 P 2019-07-12 19:04:48.880 manual 0.89 0.00 UW
18 UW.GPW.EHZ.01 None 66.335810 S 2019-07-12 19:04:57.550 manual 0.05 0.06 UW
19 UW.RPW.EHZ.01 None 30.103637 P 2019-07-12 19:04:48.500 manual 0.21 0.08 UW
20 UW.RPW.EHZ.01 None 30.103637 S 2019-07-12 19:04:57.850 manual -0.18 0.08 UW
21 UW.RVC.EHZ. None 177.640346 P 2019-07-12 19:04:52.230 manual 0.17 0.00 UW
22 UW.MBW.EHZ.01 None 5.243321 P 2019-07-12 19:04:52.550 manual 0.13 0.00 UW
23 CC.OBSR.BHZ. None 171.337641 P 2019-07-12 19:04:53.230 manual 0.48 0.00 UW
folder_path = '../analyst_picks/pick_files'

os.makedirs("./nll_picks", exist_ok=True)

with open("./nll_picks/NLL_picks.txt", "w") as f:         
    # Loop through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.startswith(".") == False and filename.endswith('_picks.pkl'):
            # Construct full file path
            file_path = os.path.join(folder_path, filename)

            # Read the .pkl file into a pandas DataFrame
            df = pd.read_pickle(file_path)

            # Get the part of the filename before '_picks.pkl'
            evid = filename.split('_picks.pkl')[0][2:]
            # Print or use the base_name and df as needed
            print(f"Loaded file: {filename}, base name: {evid}")
            # You can now do further processing with df and base_name

            # loop over lines in file
            f.writelines(f"PUBLIC_ID {evid}\n") # header line
            for ii, row in df.iterrows():
                # extract date and time
                sdate = row["Arrival Time"].strftime("%Y%m%d")
                stime = row["Arrival Time"].strftime("%H%M")
                ssec = row["Arrival Time"].second + row["Arrival Time"].microsecond/1e6
                station = row['Channel'].split('.')[1]
                channel = row['Channel'].split('.')[2]
                # component
                if len(channel) > 2:
                    comp = channel[2]
                elif row["Phase"] == "P":
                    comp = "Z"
                else:
                    comp = "E"

                # write to file
                f.write("%-6s %-4s %-4s %-1s %-6s %-1s %-8s %-4s %9.4f GAU %9.2e %9.2e %9.2e %9.2e %9.2e\n" %(
                    station, channel[0:2], comp, "?", row["Phase"], "?", sdate, stime, ssec, 0.1, -1, -1, -1, 1) )

            # blank record at end of event
            f.write("\n")
Loaded file: uw61500713_picks.pkl, base name: 61500713
Loaded file: uw61500718_picks.pkl, base name: 61500718
Loaded file: uw61500723_picks.pkl, base name: 61500723
Loaded file: uw61500728_picks.pkl, base name: 61500728
Loaded file: uw61500743_picks.pkl, base name: 61500743
Loaded file: uw61500748_picks.pkl, base name: 61500748
Loaded file: uw61500753_picks.pkl, base name: 61500753
Loaded file: uw61500758_picks.pkl, base name: 61500758
Loaded file: uw61500763_picks.pkl, base name: 61500763
Loaded file: uw61500768_picks.pkl, base name: 61500768
Loaded file: uw61500773_picks.pkl, base name: 61500773
Loaded file: uw61500778_picks.pkl, base name: 61500778
Loaded file: uw61535367_picks.pkl, base name: 61535367
Loaded file: uw61535372_picks.pkl, base name: 61535372
Loaded file: uw61535377_picks.pkl, base name: 61535377
Loaded file: uw61535382_picks.pkl, base name: 61535382
Loaded file: uw61535397_picks.pkl, base name: 61535397
Loaded file: uw61535412_picks.pkl, base name: 61535412
Loaded file: uw61535427_picks.pkl, base name: 61535427
Loaded file: uw61535432_picks.pkl, base name: 61535432
Loaded file: uw61535447_picks.pkl, base name: 61535447
Loaded file: uw61535452_picks.pkl, base name: 61535452
Loaded file: uw61535477_picks.pkl, base name: 61535477
Loaded file: uw61535497_picks.pkl, base name: 61535497
Loaded file: uw61535632_picks.pkl, base name: 61535632
Loaded file: uw61535657_picks.pkl, base name: 61535657
Loaded file: uw61535687_picks.pkl, base name: 61535687
Loaded file: uw61535812_picks.pkl, base name: 61535812
Loaded file: uw61536017_picks.pkl, base name: 61536017
Loaded file: uw61536062_picks.pkl, base name: 61536062
Loaded file: uw61536122_picks.pkl, base name: 61536122
Loaded file: uw61536131_picks.pkl, base name: 61536131
Loaded file: uw61536136_picks.pkl, base name: 61536136
Loaded file: uw61536697_picks.pkl, base name: 61536697
Loaded file: uw61536812_picks.pkl, base name: 61536812
Loaded file: uw61537187_picks.pkl, base name: 61537187