File size: 3,982 Bytes
cbe8603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
python example_query_waveform.py \
  --db data/index/waveform_index.sqlite \
  --network BK \
  --station CMB \
  --starttime 2019-07-01T00:00:00 \
  --endtime 2019-07-01T01:00:00
"""
import argparse
import json
import sys
from pathlib import Path

import numpy as np

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from utils.waveform_index_api import query_and_merge, save_merged_to_npz


def main():
    parser = argparse.ArgumentParser(
        description="Example API client for querying and merging waveform data."
    )

    parser.add_argument(
        "--db",
        required=True,
        help="SQLite waveform index database, e.g. data/index/waveform_index.sqlite",
    )

    parser.add_argument(
        "--network",
        default="*",
        help='Network code or wildcard, e.g. "BK", "C*", "*".',
    )

    parser.add_argument(
        "--station",
        default="*",
        help='Station code or wildcard, e.g. "BDM", "BD*", "*".',
    )

    parser.add_argument(
        "--location",
        default="*",
        help='Location code or wildcard, e.g. "00", "--", "*".',
    )

    parser.add_argument(
        "--channel",
        default="*",
        help='Channel code or wildcard, e.g. "BHE", "BH*", "*Z", "*".',
    )

    parser.add_argument(
        "--starttime",
        required=True,
        help="Query start time, e.g. 2019-07-01T00:00:00",
    )

    parser.add_argument(
        "--endtime",
        required=True,
        help="Query end time, e.g. 2019-07-01T01:00:00",
    )

    parser.add_argument(
        "--fill_value",
        type=float,
        default=0.0,
        help="Fill value for gaps.",
    )

    parser.add_argument(
        "--limit",
        type=int,
        default=None,
        help="Optional limit for matched segments.",
    )

    parser.add_argument(
        "--output_npz",
        default=None,
        help="Optional output NPZ file.",
    )

    parser.add_argument(
        "--show_segments",
        action="store_true",
        help="Print segment details.",
    )

    args = parser.parse_args()

    merged, rows = query_and_merge(
        db_file=args.db,
        network=args.network,
        station=args.station,
        location=args.location,
        channel=args.channel,
        starttime=args.starttime,
        endtime=args.endtime,
        fill_value=args.fill_value,
        dtype=np.float32,
        limit=args.limit,
    )

    print("=" * 80)
    print("Query")
    print("=" * 80)
    print(f"network   : {args.network}")
    print(f"station   : {args.station}")
    print(f"location  : {args.location}")
    print(f"channel   : {args.channel}")
    print(f"starttime : {args.starttime}")
    print(f"endtime   : {args.endtime}")

    print()
    print("=" * 80)
    print("Matched segments")
    print("=" * 80)
    print(f"Segment count: {len(rows)}")

    if args.show_segments:
        for row in rows:
            print(
                f"{row['network']}.{row['station']}.{row['location']} "
                f"{row['channel']} "
                f"{row['starttime']} -> {row['endtime']} "
                f"sr={row['sampling_rate']} "
                f"npts={row['npts']} "
                f"path={row['dataset_path']} "
                f"h5={row['h5_file']}"
            )

    print()
    print("=" * 80)
    print("Merged waveforms")
    print("=" * 80)

    if len(merged) == 0:
        print("No waveform found.")
        return

    for key, item in merged.items():
        print(
            f"{key}: "
            f"shape={item['data'].shape}, "
            f"sr={item['sampling_rate']}, "
            f"filled_ratio={item['filled_ratio']:.4f}, "
            f"segments={len(item['segments'])}"
        )

    if args.output_npz:
        save_merged_to_npz(merged, args.output_npz)
        print()
        print(f"[OK] Saved merged waveform NPZ: {args.output_npz}")


if __name__ == "__main__":
    main()