-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,036 additions
and
172 deletions.
There are no files selected for viewing
Submodule PhaseNet
updated
from d5a17e to 4f36f9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# %% | ||
import json | ||
import multiprocessing as mp | ||
import os | ||
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed | ||
from datetime import datetime, timedelta, timezone | ||
from threading import Lock, Thread | ||
|
||
import fsspec | ||
import numpy as np | ||
import pandas as pd | ||
import pyproj | ||
from obspy import read_inventory | ||
from obspy.clients.fdsn import Client | ||
from sklearn.cluster import DBSCAN | ||
from tqdm import tqdm | ||
from args import parse_args | ||
from glob import glob | ||
|
||
|
||
def load_data(year, jday, data_path, root_path, bucket, protocol, token): | ||
|
||
fs = fsspec.filesystem(protocol, token=token) | ||
adloc_events_csv = f"{data_path}/{year:04d}/adloc_events_{jday:03d}.csv" | ||
adloc_picks_csv = f"{data_path}/{year:04d}/adloc_picks_{jday:03d}.csv" | ||
if protocol == "file": | ||
events = pd.read_csv(f"{root_path}/{adloc_events_csv}", parse_dates=["time"]) | ||
picks = pd.read_csv(f"{root_path}/{adloc_picks_csv}", parse_dates=["phase_time"]) | ||
else: | ||
with fs.open(f"{bucket}/{adloc_events_csv}", "r") as fp: | ||
events = pd.read_csv(fp, parse_dates=["time"]) | ||
with fs.open(f"{bucket}/{adloc_picks_csv}", "r") as fp: | ||
picks = pd.read_csv(fp, parse_dates=["phase_time"]) | ||
|
||
events["year"] = year | ||
events["jday"] = jday | ||
picks["year"] = year | ||
picks["jday"] = jday | ||
|
||
return events, picks | ||
|
||
|
||
# %% | ||
if __name__ == "__main__": | ||
|
||
args = parse_args() | ||
root_path = args.root_path | ||
region = args.region | ||
|
||
data_path = f"{region}/gamma" | ||
result_path = f"{region}/gamma" | ||
|
||
# %% | ||
# protocol = "gs" | ||
# token_json = f"{os.environ['HOME']}/.config/gcloud/application_default_credentials.json" | ||
# with open(token_json, "r") as fp: | ||
# token = json.load(fp) | ||
# fs = fsspec.filesystem(protocol, token=token) | ||
|
||
# %% | ||
event_csvs = sorted(glob(f"{root_path}/{data_path}/????/????.???.events.csv")) | ||
|
||
# %% | ||
events = [] | ||
picks = [] | ||
for event_csv in tqdm(event_csvs, desc="Load event csvs"): | ||
pick_csv = event_csv.replace("events.csv", "picks.csv") | ||
year, jday = event_csv.split("/")[-1].split(".")[:2] | ||
events_ = pd.read_csv(event_csv, dtype=str) | ||
picks_ = pd.read_csv(pick_csv, dtype=str) | ||
events_["year"] = year | ||
events_["jday"] = jday | ||
picks_["year"] = year | ||
picks_["jday"] = jday | ||
events.append(events_) | ||
picks.append(picks_) | ||
|
||
events = pd.concat(events, ignore_index=True) | ||
picks = pd.concat(picks, ignore_index=True) | ||
|
||
events["dummy_id"] = events["year"] + "." + events["jday"] + "." + events["event_index"] | ||
picks["dummy_id"] = picks["year"] + "." + picks["jday"] + "." + picks["event_index"] | ||
|
||
events["event_index"] = np.arange(len(events)) | ||
picks = picks.drop("event_index", axis=1) | ||
picks = picks.merge(events[["dummy_id", "event_index"]], on="dummy_id") | ||
|
||
events.drop(["year", "jday", "dummy_id"], axis=1, inplace=True) | ||
picks.drop(["year", "jday", "dummy_id"], axis=1, inplace=True) | ||
|
||
events.to_csv(f"{root_path}/{result_path}/gamma_events.csv", index=False) | ||
picks.to_csv(f"{root_path}/{result_path}/gamma_picks.csv", index=False) | ||
|
||
# %% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# %% | ||
import json | ||
import multiprocessing as mp | ||
import os | ||
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed | ||
from datetime import datetime, timedelta, timezone | ||
from threading import Lock, Thread | ||
|
||
import fsspec | ||
import numpy as np | ||
import pandas as pd | ||
import pyproj | ||
from obspy import read_inventory | ||
from obspy.clients.fdsn import Client | ||
from sklearn.cluster import DBSCAN | ||
from tqdm import tqdm | ||
from args import parse_args | ||
from glob import glob | ||
|
||
|
||
def scan_csv(year, root_path, fs=None, bucket=None, protocol="file"): | ||
# %% | ||
csv_list = [] | ||
if protocol != "file": | ||
jdays = fs.ls(f"{bucket}/{region}/{folder}/{year}") | ||
else: | ||
jdays = os.listdir(f"{root_path}/{region}/phasenet/picks/{year}/") | ||
|
||
for jday in jdays: | ||
if protocol != "file": | ||
csvs = fs.glob(f"{jday}/??/*.csv") | ||
else: | ||
csvs = glob(f"{root_path}/{region}/phasenet/picks/{year}/{jday}/??/*.csv") | ||
|
||
csv_list.extend([[year, jday, csv] for csv in csvs]) | ||
|
||
csvs = pd.DataFrame(csv_list, columns=["year", "jday", "csv"]) | ||
csv_file = f"{root_path}/{region}/phasenet/csv_list_{year}.csv" | ||
csvs.to_csv(csv_file, index=False) | ||
|
||
return csv_file | ||
|
||
|
||
# %% | ||
def read_csv(rows, region, year, jday, root_path, fs=None, bucket=None): | ||
|
||
picks = [] | ||
for i, row in rows.iterrows(): | ||
# if fs.info(row["csv"])["size"] == 0: | ||
# continue | ||
# with fs.open(row["csv"], "r") as f: | ||
# picks_ = pd.read_csv(f, dtype=str) | ||
if os.path.getsize(row["csv"]) == 0: | ||
continue | ||
with open(row["csv"], "r") as f: | ||
picks_ = pd.read_csv(f, dtype=str) | ||
picks.append(picks_) | ||
|
||
if len(picks) > 0: | ||
picks = pd.concat(picks, ignore_index=True) | ||
if not os.path.exists(f"{root_path}/{region}/phasenet/{year}"): | ||
os.makedirs(f"{root_path}/{region}/phasenet/{year}", exist_ok=True) | ||
picks.to_csv(f"{root_path}/{region}/phasenet/{year}/{year}.{jday}.csv", index=False) | ||
# fs.put( | ||
# f"{root_path}/{region}/phasenet/{year}/{jday}/{year}.{jday}.csv", | ||
# f"{bucket}/{region}/phasenet_merged/{year}/{year}.{jday}.csv", | ||
# ) | ||
else: | ||
with open(f"{root_path}/{region}/phasenet/{year}/{year}.{jday}.csv", "w") as f: | ||
f.write("") | ||
|
||
|
||
# %% | ||
if __name__ == "__main__": | ||
|
||
args = parse_args() | ||
root_path = args.root_path | ||
region = args.region | ||
|
||
data_path = f"{region}/phasenet/picks" | ||
result_path = f"{region}/phasenet" | ||
|
||
# %% | ||
# protocol = "gs" | ||
# token_json = f"{os.environ['HOME']}/.config/gcloud/application_default_credentials.json" | ||
# with open(token_json, "r") as fp: | ||
# token = json.load(fp) | ||
# fs = fsspec.filesystem(protocol, token=token) | ||
|
||
# %% | ||
years = os.listdir(f"{root_path}/{region}/phasenet/picks") | ||
|
||
for year in years: | ||
|
||
csv_list = scan_csv(year, root_path) | ||
|
||
# %% | ||
csv_list = pd.read_csv(csv_list, dtype=str) | ||
|
||
# for jday, csvs in csv_list.groupby("jday"): | ||
# read_csv(csvs, region, year, jday, root_path) | ||
# raise | ||
|
||
# ncpu = os.cpu_count() | ||
ncpu = 64 | ||
print(f"Number of processors: {ncpu}") | ||
csv_by_jday = csv_list.groupby("jday") | ||
pbar = tqdm(total=len(csv_by_jday), desc=f"Loading csv files (year {year})") | ||
|
||
# with mp.Pool(ncpu) as pool: | ||
ctx = mp.get_context("spawn") | ||
with ctx.Pool(ncpu) as pool: | ||
jobs = [] | ||
for jday, csvs in csv_by_jday: | ||
job = pool.apply_async( | ||
read_csv, (csvs, region, year, jday, root_path), callback=lambda _: pbar.update() | ||
) | ||
jobs.append(job) | ||
pool.close() | ||
pool.join() | ||
for job in jobs: | ||
output = job.get() | ||
if output: | ||
print(output) | ||
|
||
pbar.close() | ||
|
||
# %% | ||
csvs = glob(f"{root_path}/{region}/phasenet/????/????.???.csv") | ||
picks = [] | ||
for csv in tqdm(csvs, desc="Merge csv files"): | ||
picks.append(pd.read_csv(csv, dtype=str)) | ||
picks = pd.concat(picks, ignore_index=True) | ||
picks.to_csv(f"{root_path}/{region}/phasenet/phasenet_picks.csv", index=False) | ||
|
||
# %% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.