### Start Training Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Starts the training process from a pre-trained model using specified directories and parameters. ```python python phasenet/train.py --model_dir=model/190703-214543/ --train_dir=test_data/npz --train_list=test_data/npz.csv --plot_figure --epochs=10 --batch_size=10 ``` -------------------------------- ### Import Libraries Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Imports necessary Python libraries for the project. ```python from gradio_client import Client import obspy import numpy as np import json import pandas as pd import matplotlib.pyplot as plt import json ``` -------------------------------- ### Install to 'phasenet' Virtual Environment Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Creates a new Conda environment named 'phasenet', installs dependencies, and activates the environment. ```bash conda env create -f env.yaml conda activate phasenet ``` -------------------------------- ### Get picks and prediction probabilities Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_fastapi.ipynb Requests both phase picks and prediction probabilities (time series) from the PhaseNet API and visualizes the results. ```python resp = requests.post(f'{PHASENET_API_URL}/predict_prob', json=req) print(resp) picks, preds = resp.json() preds = np.array(preds) print('Picks', picks) plt.figure() plt.subplot(211) plt.plot(data[:,-1], 'k', label="Z") plt.subplot(212) plt.plot(preds[0, :, 0, 1], label="P") plt.plot(preds[0, :, 0, 2], label="S") plt.legend() plt.show(); ``` -------------------------------- ### Install to Default Environment Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Installs the project dependencies into the base Conda environment using the provided env.yaml file. ```bash conda env update -f=env.yaml -n base ``` -------------------------------- ### Prepare Seismic Waveforms Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Reads seismic data using ObsPy and prepares it for plotting. ```python import obspy stream = obspy.read() stream.plot(); ``` -------------------------------- ### Loading Configuration and Data Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb Loads configuration, start times, interval, and station data from pickle files. ```python with open(config_file, "rb") as fp: config = pickle.load(fp) with open(datetime_file, "rb") as fp: tmp = pickle.load(fp) starttimes = tmp["starttimes"] interval = tmp["interval"] with open(station_file, "rb") as fp: stations = pickle.load(fp) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_fastapi.ipynb Imports essential Python libraries for data manipulation, plotting, seismic data handling, and API requests. ```python import os, sys import numpy as np import matplotlib.pyplot as plt import obspy import requests sys.path.insert(0, os.path.abspath("../")) ``` -------------------------------- ### Read Picks from JSON Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_batch_prediction.ipynb Opens and loads the 'picks.json' file into a Python list of dictionaries, then prints the first two entries. ```python with open(os.path.join(PROJECT_ROOT, "results/picks.json")) as fp: picks_json = json.load(fp) print(picks_json[1]) print(picks_json[0]) ``` -------------------------------- ### Import Libraries and Define Project Root Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_batch_prediction.ipynb Imports necessary libraries (pandas, json, os) and defines the project root directory for accessing results. ```python import pandas as pd import json import os PROJECT_ROOT = os.path.realpath(os.path.join(os.path.abspath(''), "..")) ``` -------------------------------- ### Define time window for data download Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Specifies the start and end times for fetching seismic data. ```python starttime = UTCDateTime("2019-07-04T17:00:00") endtime = UTCDateTime("2019-07-05T00:00:00") ``` -------------------------------- ### Write Waveform Data Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Writes the seismic waveform data to a miniSEED file. ```python stream.write("data.mseed", format="MSEED") ``` -------------------------------- ### Predict with mseed format (example 1) Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Command to perform batch prediction using mseed format data. ```bash python phasenet/predict.py --model=model/190703-214543 --data_list=test_data/mseed.csv --data_dir=test_data/mseed --format=mseed --amplitude --response_xml=test_data/stations.xml --batch_size=1 --sampling_rate=100 ``` -------------------------------- ### Predict P/S-phase Picks using PhaseNet Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Uses the PhaseNet model via Gradio client to predict P/S-phase picks. ```python model = "ai4eps/phasenet" # model = "http://127.0.0.1:7860" client = Client(model, serialize=True) _, picks_csv, picks_json = client.predict(["data.mseed"], json.dumps(data.tolist()), json.dumps([data_id])) ``` -------------------------------- ### Predict with mseed format (example 2) Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Another command for batch prediction using mseed format data, with a different data list. ```bash python phasenet/predict.py --model=model/190703-214543 --data_list=test_data/mseed2.csv --data_dir=test_data/mseed --format=mseed --amplitude --response_xml=test_data/stations.xml --batch_size=1 --sampling_rate=100 ``` -------------------------------- ### Extract 3-Component Data Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Extracts 3-component seismic data and associated metadata. ```python stream = stream.sort() assert(len(stream) == 3) data = [] for trace in stream: data.append(trace.data) data = np.array(data).T assert(data.shape[-1] == 3) data_id = stream[0].get_id()[:-1] timestamp = stream[0].stats.starttime.datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] ``` -------------------------------- ### Read and Process Picks from CSV Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_batch_prediction.ipynb Reads the 'picks.csv' file, processes the 'p_idx', 'p_prob', 's_idx', and 's_prob' columns by stripping brackets and splitting by comma, then prints the first two entries. ```python picks_csv = pd.read_csv(os.path.join(PROJECT_ROOT, "results/picks.csv"), sep="\t") picks_csv.loc[:, 'p_idx'] = picks_csv["p_idx"].apply(lambda x: x.strip("[]").split(",")) picks_csv.loc[:, 'p_prob'] = picks_csv["p_prob"].apply(lambda x: x.strip("[]").split(",")) picks_csv.loc[:, 's_idx'] = picks_csv["s_idx"].apply(lambda x: x.strip("[]").split(",")) picks_csv.loc[:, 's_prob'] = picks_csv["s_prob"].apply(lambda x: x.strip("[]").split(",")) print(picks_csv.iloc[1]) print(picks_csv.iloc[0]) ``` -------------------------------- ### Extract and format seismic data Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_fastapi.ipynb Extracts 3-component seismic data from an Obspy stream, formats it into a NumPy array, and captures metadata like data ID and timestamp. ```python ## Extract 3-component data stream = stream.sort() assert(len(stream) == 3) data = [] for trace in stream: data.append(trace.data) data = np.array(data).T assert(data.shape[-1] == 3) data_id = stream[0].get_id()[:-1] timestamp = stream[0].stats.starttime.datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] ``` -------------------------------- ### Process Prediction Results Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Loads the predicted picks from a JSON string into a pandas DataFrame. ```python picks = pd.read_json(picks_json) # picks = pd.read_csv(csv, parse_dates=["phase_time"]) # print(picks) ``` -------------------------------- ### Plot Waveforms with Picks Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_gradio.ipynb Plots the seismic waveforms and overlays the predicted P/S-phase picks with their scores. ```python fig, ax = plt.subplots(len(stream), 1, figsize=(10, 10)) for i, tr in enumerate(stream): ax[i].plot(tr.times(), tr.data, label=tr.stats.channel, c="k") for _, pick in picks.iterrows(): c = "blue" if pick["phase_type"] == "P" else "red" label = pick["phase_type"] if i == 0 else None ax[i].axvline((pick["phase_time"]-tr.stats.starttime.datetime).total_seconds(), c=c, label=label, alpha=pick["phase_score"]) ax[i].legend() ``` -------------------------------- ### Predict P/S-phase picks using PhaseNet API Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_fastapi.ipynb Sends seismic data to the PhaseNet API for P/S-phase pick prediction and prints the API response and the predicted picks. ```python req = {"id": [data_id], "timestamp": [timestamp], "vec": [data.tolist()]} resp = requests.post(f'{PHASENET_API_URL}/predict', json=req) print(resp) print('Picks', resp.json()) ``` -------------------------------- ### Install for Mac ARM Chip Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Creates a new Conda environment named 'phasenet' using env_mac.yaml for Mac ARM chips and activates it. ```bash conda env create -f env_mac.yaml conda activate phasenet ``` -------------------------------- ### Read HDF5 Catalog Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb A simple example of reading the '/catalog' group from an HDF5 file into a pandas DataFrame. ```python pd.read_hdf("data.h5", "/catalog") ``` -------------------------------- ### Configuration and Data Preparation Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb This code block sets up configuration parameters for data preparation, including location, time, seismic station details, and file paths. It then saves the configuration to a pickle file and generates a list of start times for data retrieval. ```python import obspy import os import pickle import datetime import time # Location pi = 3.1415926 degree2km = pi*6371/180 center = (-115.53, 32.98) #salton sea horizontal_degree = 0.5 vertical_degree = 0.5 zero_anchor = (center[0]-horizontal_degree, center[1]-vertical_degree) # Time starttime = obspy.UTCDateTime("2020-10-01T00:00") endtime = obspy.UTCDateTime("2020-10-01T00:03") ## not included # seismic stations network_list = "CI" # channel_list = "HNE,HNN,HNZ,HHE,HHN,HHZ,BHE,BHN,BHZ,EHE,EHN,EHZ" channel_list = "HHE,HHN,HHZ" config_file = "config.pkl" datetime_file = "datetimes.pkl" station_file = "stations.pkl" data_path = "./" station_list = "stations.csv" fname_list = "mseed.csv" if not os.path.exists(data_path): os.mkdir(data_path) ####### save config ######## config = {} config["center"] = center config["horizontal_degree"] = horizontal_degree config["vertical_degree"] = vertical_degree config["zero_anchor"] = zero_anchor config["xlim"] = [0, horizontal_degree*2*degree2km] config["ylim"] = [0, vertical_degree*2*degree2km] config["anchor"] = zero_anchor config["degree2km"] = degree2km config["starttime"] = starttime config["endtime"] = endtime config["networks"] = network_list config["channels"] = channel_list config["network_list"] = network_list config["channel_list"] = channel_list with open(config_file, "wb") as fp: pickle.dump(config, fp) one_day = datetime.timedelta(days=1) one_hour = datetime.timedelta(hours=1) one_minute = datetime.timedelta(minutes=1) invertal = 3*one_minute starttimes = [] tmp_start = starttime while tmp_start < endtime: starttimes.append(tmp_start) tmp_start += invertal with open(datetime_file, "wb") as fp: pickle.dump({"starttimes": starttimes, "interval": invertal}, fp) print(starttimes) ``` -------------------------------- ### Define PhaseNet API URL Source: https://github.com/ai4eps/phasenet/blob/main/docs/example_fastapi.ipynb Sets the URL for the PhaseNet API endpoint. Several options are commented out, with the active URL pointing to a cloud-hosted instance. ```python # PHASENET_API_URL = "http://127.0.0.1:8000" # PHASENET_API_URL = "http://phasenet.quakeflow.com" ## gcp # PHASENET_API_URL = "http://test.quakeflow.com:8001" ## local machine PHASENET_API_URL = "https://ai4eps-eqnet.hf.space" # PHASENET_API_URL = "http://131.215.74.195:8001" ## local machine ``` -------------------------------- ### Initialize FDSN client and data directory Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Sets up the SCEDC client and creates a directory to store downloaded miniseed files if it doesn't exist. ```python client = Client("SCEDC") data_dir = "mseed" if not os.path.exists(data_dir): os.makedirs(data_dir) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Imports required libraries for data handling, time operations, and FDSN client interaction. ```python import os import obspy from obspy import UTCDateTime from obspy.clients.fdsn import Client ``` -------------------------------- ### Run PhaseNet for prediction Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Executes PhaseNet in prediction mode using the downloaded data and a pre-trained model. ```bash python run.py --mode=pred --model_dir=model/190703-214543 --data_dir=demo/mseed --data_list=demo/fname.csv --output_dir=output --batch_size=20 --input_mseed ``` -------------------------------- ### Download Waveform Data Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Creates a 'data' directory if it doesn't exist and downloads waveform files based on the selected catalog. ```python if not os.path.exists("data"): os.mkdir("data") for fname in select_catalog["fname"]: download_blob("quakeflow", f"data/{fname}", f"data/{fname}") ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Clones the PhaseNet repository from GitHub and changes the current directory to the repository's root. ```bash git clone https://github.com/wayneweiqiang/PhaseNet.git cd PhaseNet ``` -------------------------------- ### Listing Miniseed Files Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb Lists miniseed files in the 'waveforms' directory and appends them to 'mseed.csv'. ```shell !echo "fname" > mseed.csv && cd waveforms && ls 2020* >> ../mseed.csv ``` -------------------------------- ### Download Combined Phases Catalog Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Downloads the combined_phases.csv catalog from Google Cloud Storage. ```python download_blob("quakeflow", "catalogs/combined_phases.csv", "combined_phases.csv") ``` -------------------------------- ### Create filename mapping file Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Generates a CSV file mapping miniseed filenames to their corresponding seismic components (East, North, Vertical). ```python with open("fname.csv", 'w') as fp: fp.write("fname,E,N,Z\n") fp.write("CCC.mseed,HHE,HHN,HHZ\n") fp.write("CLC.mseed,HHE,HHN,HHZ\n") ``` -------------------------------- ### Listing Waveform Files Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb Lists waveform files in the 'waveforms' directory and appends them to 'mseed_station.csv'. ```shell !echo "fname" > mseed_station.csv && cd waveforms && ls CI* >> ../mseed_station.csv ``` -------------------------------- ### Read and Sample Catalog Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Reads the downloaded CSV catalog into a pandas DataFrame and samples 100 rows. ```python catalogs = pd.read_csv("combined_phases.csv", sep="\t", dtype=str) select_catalog = catalogs.sample(100, random_state=123) select_catalog.to_csv("selected_phases.csv", sep="\t") ``` -------------------------------- ### Create HDF5 Group and Dataset Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb This snippet shows how to create an HDF5 file, define groups for catalog and data, and store NumPy arrays as datasets with metadata as attributes. ```python catalog = fp.create_group("/catalog") data = fp.create_group("/data") for fname in select_catalog["fname"]: meta = np.load(f"data/{fname}") ds = data.create_dataset(fname, data=meta["data"], dtype="float32") for k in meta: if k != "data": if meta[k].dtype.type is np.str_: ds.attrs[k] = str(meta[k]) else: ds.attrs[k] = meta[k] ``` -------------------------------- ### Download waveforms Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Fetches seismic waveform data for stations CCC and CLC for the specified time range and components. ```python CCC = client.get_waveforms("CI", "CCC", "*", "HHE,HHN,HHZ", starttime, endtime) CLC = client.get_waveforms("CI", "CLC", "*", "HHE,HHN,HHZ", starttime, endtime) ``` -------------------------------- ### Download Test Data Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Command to download and unzip the test data for PhaseNet. ```bash wget https://github.com/wayneweiqiang/PhaseNet/releases/download/test_data/test_data.zip unzip test_data.zip ``` -------------------------------- ### Run PhaseNet with plotting enabled Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Executes PhaseNet in prediction mode and additionally generates plots for waveform and predictions. ```bash python run.py --mode=pred --model_dir=model/190703-214543 --data_dir=demo/mseed --data_list=demo/fname.csv --output_dir=output --plot_figure --batch_size=20 --input_mseed ``` -------------------------------- ### Display filename mapping Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Prints the content of the generated CSV file to verify the filename mapping. ```bash !cat fname.csv ``` -------------------------------- ### Data Download and Conversion Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb Downloads seismic waveforms using Obspy, saves them as miniseed and SAC files, and logs file information. ```python waveform_dir = os.path.join(data_path, "waveforms") sac_dir = os.path.join(data_path, "sac") if not os.path.exists(waveform_dir): os.mkdir(waveform_dir) if not os.path.exists(sac_dir): os.mkdir(sac_dir) ####### Download data ######## client = Client("SCEDC") fname_sac = open("sac.csv", "w") fname_sac.write("fname\tE\tN\tZ\n") fp = open(fname_list, "w") fp.write("fname\n") # for i in idx: for i in range(len(starttimes)): starttime = starttimes[i] endtime = starttime + interval fname = "{}.mseed".format(starttime.datetime.strftime("%Y-%m-%dT%H:%M")) max_retry = 3 stream = obspy.Stream() print(f"{fname} download starts") for network in stations: for station in network: retry = 0 while retry < max_retry: try: tmp = client.get_waveforms(network.code, station.code, "*", config["channel_list"], starttime, endtime, attach_response=True) # tmp.remove_sensitivity() tmp.write(os.path.join(waveform_dir, f"{network.code}.{station.code}.{fname}")) line = f"{network.code}.{station.code}.{fname.rstrip('.mseed')}" for c in config["channel_list"].split(","): x = tmp.select(channel=c) x.write(os.path.join(sac_dir, f"{network.code}.{station.code}.{fname.rstrip('.mseed')}.{c}.sac"), format='SAC') line += f"\t{network.code}.{station.code}.{fname.rstrip('.mseed')}.{c}.sac" line += "\n" fname_sac.write(line) stream += tmp break except Exception as e: err = e retry += 1 time.sleep(1) continue if retry == max_retry: print(f"{fname}: MAX {max_retry} retries reached : {network.code}.{station.code} with error: {err}") if not os.path.exists(waveform_dir): os.makedirs(waveform_dir) stream.write(os.path.join(waveform_dir, fname)) print(f"{fname} download succeeds") fp.write(f"{fname}\n") fp.close() fname_sac.close() print(os.listdir(waveform_dir)) ``` -------------------------------- ### Save downloaded waveforms Source: https://github.com/ai4eps/phasenet/blob/main/demo/demo-obspy.ipynb Saves the downloaded waveform data to miniseed files in the specified directory. ```python CCC.write(os.path.join(data_dir, "CCC.mseed")) CLC.write(os.path.join(data_dir, "CLC.mseed")) ``` -------------------------------- ### Download Blob Function Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Defines a function to download a file from a Google Cloud Storage bucket. ```python from google.cloud import storage import os os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/weiqiang/.dotbot/cloud/quakeflow_wayne.json" def download_blob(bucket_name, source_blob_name, destination_file_name): storage_client = storage.Client() bucket = storage_client.bucket(bucket_name) blob = bucket.blob(source_blob_name) blob.download_to_filename(destination_file_name) print("Blob {} downloaded to {}.".format(source_blob_name, destination_file_name)) ``` -------------------------------- ### Predict script usage Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Help message detailing the optional arguments for the predict.py script. ```bash usage: predict.py [-h] [--batch_size BATCH_SIZE] [--model_dir MODEL_DIR] [--data_dir DATA_DIR] [--data_list DATA_LIST] [--hdf5_file HDF5_FILE] [--hdf5_group HDF5_GROUP] [--result_dir RESULT_DIR] [--result_fname RESULT_FNAME] [--min_p_prob MIN_P_PROB] [--min_s_prob MIN_S_PROB] [--mpd MPD] [--amplitude] [--format FORMAT] [--s3_url S3_URL] [--stations STATIONS] [--plot_figure] [--save_prob] optional arguments: -h, --help show this help message and exit --batch_size BATCH_SIZE batch size --model_dir MODEL_DIR Checkpoint directory (default: None) --data_dir DATA_DIR Input file directory --data_list DATA_LIST Input csv file --hdf5_file HDF5_FILE Input hdf5 file --hdf5_group HDF5_GROUP data group name in hdf5 file --result_dir RESULT_DIR Output directory --result_fname RESULT_FNAME Output file --min_p_prob MIN_P_PROB Probability threshold for P pick --min_s_prob MIN_S_PROB Probability threshold for S pick --mpd MPD Minimum peak distance --amplitude if return amplitude value --format FORMAT input format --stations STATIONS seismic station info --plot_figure If plot figure for test --save_prob If save result for test ``` -------------------------------- ### Displaying mseed.csv Content Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb Displays the content of the 'mseed.csv' file. ```shell !cat mseed.csv ``` -------------------------------- ### Import Libraries Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Imports necessary libraries for data manipulation and file handling. ```python import pandas as pd import h5py ``` -------------------------------- ### Displaying mseed_station.csv Content Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb Displays the content of the 'mseed_station.csv' file. ```shell !cat mseed_station.csv ``` -------------------------------- ### Open HDF5 File for Appending Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Opens an HDF5 file named 'data.h5' in append mode ('a') using the latest library version. ```python with h5py.File("data.h5", "a", libver='latest') as fp: ``` -------------------------------- ### Cleanup Command (Commented Out) Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb A commented-out command to remove generated files. ```python # !rm stations.xml stations.pkl datetimes.pkl config.pkl ``` -------------------------------- ### Read HDF5 Data and Attributes Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb This snippet demonstrates how to open an HDF5 file in read mode with SWMR enabled, list the keys in the 'data' group, and print the 'snr' attribute for each dataset. ```python with h5py.File("data.h5", "r", libver='latest', swmr=True) as fp: print(list(fp['data'].keys())) for k in fp['data'].keys(): print(fp[f"data/{k}"].attrs["snr"]) ``` -------------------------------- ### Inspect Metadata Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb This code iterates through metadata loaded from a file and prints the key, value, data type, length, and whether it has a length attribute. ```python for fname in select_catalog["fname"]: meta = np.load(f"data/{fname}") for k in meta: print(k, meta[k], meta[k].dtype,len(meta[k]), hasattr(meta[k], "__len__")) break ``` -------------------------------- ### Download and Process Station Information Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb This code block downloads seismic station information using the FDSN client, processes the inventory data, and saves station locations, components, and response values to a CSV file. It also includes plotting functionality for station locations. ```python from obspy.clients.fdsn import Client import matplotlib # matplotlib.use("agg") import matplotlib.pyplot as plt from collections import defaultdict import pandas as pd with open(config_file, "rb") as fp: config = pickle.load(fp) ####### Download stations ######## stations = Client("IRIS").get_stations(network = config["network_list"], station = "*", starttime=config["starttime"], endtime=config["endtime"], minlatitude=config["center"][1]-config["vertical_degree"], maxlatitude=config["center"][1]+config["vertical_degree"], minlongitude=config["center"][0]-config["horizontal_degree"], maxlongitude=config["center"][0]+config["horizontal_degree"], channel=config["channel_list"], level="response", filename=os.path.join(data_path, 'stations.xml')) stations = obspy.read_inventory(os.path.join(data_path, 'stations.xml')) print("Number of stations: {}".format(sum([len(x) for x in stations]))) # stations.plot('local', outfile="stations.png") ####### Save stations ######## station_locs = defaultdict(dict) for network in stations: for station in network: for chn in station: x = (chn.longitude - config["zero_anchor"][0])*config["degree2km"] y = (chn.latitude - config["zero_anchor"][1])*config["degree2km"] z = -chn.elevation / 1e3 #km sid = f"{network.code}.{station.code}.{chn.location_code}.{chn.code[:-1]}" if sid in station_locs: station_locs[sid]["component"] += f",{chn.code[-1]}" station_locs[sid]["response"] += f",{chn.response.instrument_sensitivity.value:.2f}" else: component = f"{chn.code[-1]}" response = f"{chn.response.instrument_sensitivity.value:.2f}" dtype = chn.response.instrument_sensitivity.input_units.lower() tmp_dict = {} tmp_dict["x(km)"], tmp_dict["y(km)"], tmp_dict["z(km)"] = x, y, z tmp_dict["lng"], tmp_dict["lat"], tmp_dict["elv(m)"] = chn.longitude, chn.latitude, chn.elevation tmp_dict["component"], tmp_dict["response"], tmp_dict["unit"] = component, response, dtype station_locs[sid] = tmp_dict station_locs = pd.DataFrame.from_dict(station_locs, orient='index') station_locs.to_csv(station_list, sep="\t", float_format="%.3f", index_label="station", columns=["x(km)", "y(km)", "z(km)", "lat", "lng", "elv(m)", "unit", "component", "response"]) # ####### Plot stations ######## plt.figure() plt.plot(station_locs["x(km)"], station_locs["y(km)"], "^", label="Stations") ``` -------------------------------- ### Save Catalog to HDF5 Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_numpy_mseed_data.ipynb Saves a pandas DataFrame catalog to an HDF5 file in 'w' (write) mode with table format and data columns enabled. ```python select_catalog.to_hdf('data.h5', '/catalog', mode='w', format='table', data_columns=True) ``` -------------------------------- ### Plotting Stations and Events Source: https://github.com/ai4eps/phasenet/blob/main/phasenet/test_data/prepare_mseed.ipynb This snippet plots the locations of stations and earthquakes, and saves the plot. ```python # plt.plot(catalog["x(km)"], catalog["y(km)"], "k.", label="Earthquakes") plt.xlabel("X (km)") plt.ylabel("Y (km)") plt.axis("scaled") plt.legend() plt.title(f"Number of stations: {len(station_locs)}") # plt.savefig(os.path.join(data_path, "stations_events.png")) plt.show() with open(station_file, "wb") as fp: pickle.dump(stations, fp) ``` -------------------------------- ### Predict with seismic array format Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Command to perform batch prediction for a seismic array, compatible with QuakeFlow. ```bash python phasenet/predict.py --model=model/190703-214543 --data_list=test_data/mseed_array.csv --data_dir=test_data/mseed_array --stations=test_data/stations.json --format=mseed_array --amplitude ``` -------------------------------- ### Predict with sac format Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Command to perform batch prediction using sac format data. ```bash python phasenet/predict.py --model=model/190703-214543 --data_list=test_data/sac.csv --data_dir=test_data/sac --format=sac --batch_size=1 ``` -------------------------------- ### Predict with hdf5 format Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Command to perform batch prediction using hdf5 format data. ```bash python phasenet/predict.py --model=model/190703-214543 --hdf5_file=test_data/data.h5 --hdf5_group=data --format=hdf5 ``` -------------------------------- ### Predict with numpy format Source: https://github.com/ai4eps/phasenet/blob/main/docs/README.md Command to perform batch prediction using numpy format data. ```bash python phasenet/predict.py --model=model/190703-214543 --data_list=test_data/npz.csv --data_dir=test_data/npz --format=numpy ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.