-
Notifications
You must be signed in to change notification settings - Fork 0
/
PamguardToSilbidoBinaries.py
60 lines (48 loc) · 3.13 KB
/
PamguardToSilbidoBinaries.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
'''Reads the Pamguard tonal detection json generated by R and then translates the
data to the Silbido .ann format for whistle and moan data.'''
from silbidopy.writeBinaries import *
from readRJSON import *
import subprocess
import argparse
import os
OUTPUT_EXTENSION = ".bin"
parser = argparse.ArgumentParser("PamguardToSilbidoBinaries",
description = "Given a directory containing .pgdf PAMGuard binary whistle-moan-annotation files, generate corresponding silbido binary annotation files. WARNING: Could execute arbitrary code if not handled carefully. Confer with README.md.")
parser.add_argument("source", help = "The directory containing the PAMGuard binaries to be converted. i.e. /path/to/pamguard/binaries", type = str)
parser.add_argument("destination", help = "The directory to which the silbido binaries will be written. i.e. path/to/output/folder", type = str)
parser.add_argument("rscript", help = "The path to the Rscript.exe file, the installation of R. i.e. /path/to/Rscript.exe", type = str)
parser.add_argument("-m", action="store_true", help = "If set, filters the PAMGuard annotations, removing all that do not meet criteria to be a blue-whale moan. (requires NumPy)", default = False)
parser.add_argument("-i", action="store_true", help = "If set, this flag specifies that the intermediate JSON file will be saved, by default, to './intermediary.json'.", default = False)
parser.add_argument("--intermediary", help = "The file to which the intermediate JSON file will be written to translate data from R to Python. i.e. /path/to/intermediary.json (This file will be DELETED if -i not set; default is './intermediary.json')", type = str, default = "./intermediary.json")
args = parser.parse_args()
print(f"Translating PAMGuard Binaries to intermediate file {args.intermediary}")
# Use R script to get all PAMGuard binaries into intermediate JSON file
# WARNING: ARBITRARY CODE COULD BE RUN
command_str = f"{args.rscript} PamguardBinariesToJson.R {args.source} {args.intermediary}"
if input(f"WARNING: command \"{command_str}\" will execute as a result of this script.\n Are you certain that you wish to procede? (y/n)") == 'y':
pass
else:
print("Execution halted by user.")
exit()
e = subprocess.call(command_str)
print(f"Reading Data from intermediary.")
# Read data from intermediary to Python
files = readAllFilesRJSON(args.intermediary)
# If the i flag is not set, delete the intermedate file
if not args.i:
print(f"Deleting intermediary {args.intermediary}")
os.remove(args.intermediary)
print(f"Writing silbido binaries to {args.destination}")
for file in files:
contours = file["data"]
if args.m:
import filterMoans
contours = filterMoans.processDCalls(contours, merge = True,
trim = True, final_rejection = True)
# This naming scheme names each file after its PAMGuard counterpart,
# only with a different extention instead of pgdf
output_full_path = (
args.destination + "/"
+ file["filename"].split("/")[-1].split(".")[0][-15:] + OUTPUT_EXTENSION
)
writeTimeFrequencyBinary(output_full_path, contours)