-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoft_api_tester.py
More file actions
70 lines (59 loc) · 2.36 KB
/
soft_api_tester.py
File metadata and controls
70 lines (59 loc) · 2.36 KB
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
# -------------------------------------------------------------------------------
# Name: soft_api_tester.py
# Purpose: Tester for retrieving and decoding Feb '24 DVT SOFT data from the Sofar API
#
# Author: towynlin
#
# Copyright: (c) 2024 Sofar Ocean
# License: Apache License, Version 2.0
# -------------------------------------------------------------------------------
import argparse
from iso8601 import parse_date
from lib.api_functions import fetch_and_decode_soft_data
from lib.plotting_functions import plot_beta2_json_channels
from lib.script_functions import (
get_plot_handles_for_channels,
add_plot_arg_from_handles,
get_channels_from_args,
)
import logging
# Configure logging (this is a basic configuration, adjust as needed)
logging.basicConfig(level=logging.INFO)
SOFT_DATA_CHANNELS = [
"Temperature[ºC]",
]
soft_plot_handles = get_plot_handles_for_channels(SOFT_DATA_CHANNELS)
def convert_to_iso8601(date_str):
try:
parsed_date = parse_date(date_str)
return parsed_date.strftime("%Y-%m-%dT%H:%M:%SZ")
except Exception:
raise ValueError("Invalid date/time format")
def main():
parser = argparse.ArgumentParser(
description="Retrieve and decode SOFT data from the Sofar API."
)
parser.add_argument("spotter_id", type=str, help="Spotter ID")
parser.add_argument("api_token", type=str, help="API Token")
parser.add_argument(
"-s", "--start_date", type=convert_to_iso8601, help="Start date (optional)"
)
parser.add_argument(
"-e", "--end_date", type=convert_to_iso8601, help="End date (optional)"
)
add_plot_arg_from_handles(parser, soft_plot_handles)
args = parser.parse_args()
channels_to_plot = get_channels_from_args(args.plot_channels, soft_plot_handles)
print(channels_to_plot)
try:
print(f"Fetching SOFT data from sensor-data API...")
decoded_api_response = fetch_and_decode_soft_data(
args.spotter_id, args.api_token, args.start_date, args.end_date
)
print(f"Retrieved {len(decoded_api_response['data'])} samples.")
print(f"Plotting channels {channels_to_plot}")
plot_beta2_json_channels(decoded_api_response, channels_to_plot)
except Exception as e:
logging.error(f"Failed to retrieve or decode data: {e}", exc_info=True)
if __name__ == "__main__":
main()