-
Notifications
You must be signed in to change notification settings - Fork 6
/
ooi_processing.py
124 lines (113 loc) · 3.57 KB
/
ooi_processing.py
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
import argparse
import datetime
import logging
import os
import sys
from ooipy.request import hydrophone_request
from create_spectrogram import create_spec_name, save_spectrogram
def save_ooi_spectrograms(
start_time,
end_time,
segment_length=datetime.timedelta(minutes=5),
node="PC01A",
output_dir="spectrograms",
nfft=256,
):
"""
Creates spectrograms for each time segment in the specified time range.
Args:
`start_time`: `datetime.datetime`
`end_time`: `datetime.datetime`
`segment_length`: `datetime.timedelta`
`node`: One of the preset OOI nodes.
`output_dir`: Path to the output directory.
`nfft`: The number of data points used in each block for the FFT. A power 2 is most efficient.
Returns:
None
"""
while start_time < end_time:
segment_end = min(start_time + segment_length, end_time)
hydrophone_data = hydrophone_request.get_acoustic_data(
start_time, segment_end, node, verbose=True
)
if hydrophone_data is None:
logging.info(f"Could not get data from {start_time} to {segment_end}")
start_time = segment_end
continue
datestr = start_time.strftime("%Y-%m-%dT%H-%M-%S-%f")[:-3]
wav_name = f"{datestr}.wav"
hydrophone_data.wav_write(wav_name)
spec_fname = create_spec_name(wav_name, output_dir)
save_spectrogram(wav_name, spec_fname, nfft)
os.remove(wav_name)
start_time = segment_end
if __name__ == "__main__":
logging.basicConfig(
format="%(levelname)s:%(message)s", stream=sys.stdout, level=logging.INFO
)
parser = argparse.ArgumentParser(
description="Creates spectrogram for each segment."
)
parser.add_argument(
"--node",
help="Alphanumeric node id (e.g. PC01A)",
default="PC01A",
choices=[
"LJ01D",
"LJ01A",
"PC01A",
"PC03A",
"LJ01C",
"LJ03A",
"AXABA1",
"AXCC1",
"AXEC2",
"HYS14",
"HYSB1",
],
)
parser.add_argument(
"-s",
"--start_time",
help="Start time formatted as Y-m-dTH-M-S",
)
parser.add_argument(
"-e",
"--end_time",
help="End time formatted as Y-m-dTH-M-S",
)
parser.add_argument(
"-l",
"--segment_length",
type=float,
default=5,
help="Segment length in minutes. Default is %(default)s.",
)
parser.add_argument(
"-o",
"--output",
default="spectrograms",
help="Path to the output directory for spectrograms. Default is %(default)s.",
)
parser.add_argument(
"-n",
"--nfft",
type=int,
default=256,
help="The number of data points used in each block for the FFT. A power 2 is most efficient. Default is %(default)s.",
)
args = parser.parse_args()
if args.end_time is None:
end_time = datetime.datetime.combine(
datetime.datetime.today(), datetime.datetime.min.time()
)
else:
end_time = datetime.datetime.strptime(args.end_time, "%Y-%m-%dT%H-%M-%S")
if args.start_time is None:
start_time = end_time - datetime.timedelta(days=1)
else:
start_time = datetime.datetime.strptime(args.start_time, "%Y-%m-%dT%H-%M-%S")
segment_length = datetime.timedelta(minutes=args.segment_length)
save_ooi_spectrograms(
start_time, end_time, segment_length, args.node, args.output, args.nfft
)