-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.py
311 lines (278 loc) · 9.25 KB
/
cli.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
"""Command line interface to the IMAP Data Access API.
This module serves as a command line utility to invoke the IMAP Data Access API.
It provides the ability to interact with the Science Data Center (SDC)
by querying, downloading, and uploading files to the data center.
Use
---
imap-data-access <command> [<args>]
imap-data-access --help
imap-data-access download <file_path>
imap-data-access query <query-parameters>
imap-data-access upload <file_path>
"""
import argparse
import logging
import os
from pathlib import Path
import imap_data_access
def _download_parser(args: argparse.Namespace):
"""Download a file from the IMAP SDC.
Parameters
----------
args : argparse.Namespace
An object containing the parsed arguments and their values
"""
try:
output_path = imap_data_access.download(args.file_path)
print(f"Successfully downloaded the file to: {output_path}")
except imap_data_access.io.IMAPDataAccessError as e:
print(e)
def _print_query_results_table(query_results: list[dict]):
"""Print the query results in a table.
Parameters
----------
query_results : list
A list of dictionaries containing the query results
"""
num_files = len(query_results)
print(f"Found [{num_files}] matching files")
if num_files == 0:
return
format_string = "{:<10}|{:<10}|{:<10}|{:<10}|{:<10}|{:<7}|{:<50}|"
# Add hyphens for a separator between header and data
hyphens = "-" * 113 + "|"
print(hyphens)
header = [
"Instrument",
"Data Level",
"Descriptor",
"Start Date",
"Repointing",
"Version",
"Filename",
]
print(format_string.format(*header))
print(hyphens)
# Print data
for item in query_results:
values = [
item["instrument"],
item["data_level"],
item["descriptor"],
item["start_date"],
# Repointing is optional, so use an empty string if not present
item["repointing"] or "",
item["version"],
os.path.basename(item["file_path"]),
]
print(format_string.format(*values))
# Close the table
print(hyphens)
def _query_parser(args: argparse.Namespace):
"""Query the IMAP SDC.
Parameters
----------
args : argparse.Namespace
An object containing the parsed arguments and their values
"""
# Filter to get the arguments of interest from the namespace
valid_args = [
"instrument",
"data_level",
"descriptor",
"start_date",
"repointing",
"version",
"extension",
]
query_params = {
key: value
for key, value in vars(args).items()
if key in valid_args and value is not None
}
try:
query_results = imap_data_access.query(**query_params)
except imap_data_access.io.IMAPDataAccessError as e:
print(e)
return
if args.output_format == "table":
_print_query_results_table(query_results)
elif args.output_format == "json":
# Dump the content directly
print(query_results)
def _upload_parser(args: argparse.Namespace):
"""Upload a file to the IMAP SDC.
Parameters
----------
args : argparse.Namespace
An object containing the parsed arguments and their values
"""
try:
imap_data_access.upload(args.file_path)
print("Successfully uploaded the file to the IMAP SDC")
except imap_data_access.io.IMAPDataAccessError as e:
print(e)
def main():
"""Parse the command line arguments.
Run the command line interface to the IMAP Data Access API.
"""
api_key_help = (
"API key to authenticate with the IMAP SDC. "
"This can also be set using the IMAP_API_KEY environment variable. "
"It is only necessary for uploading files."
)
data_dir_help = (
"Directory to use for reading and writing IMAP data. "
"The default is a 'data/' folder in the "
"current working directory. This can also be "
"set using the IMAP_DATA_DIR environment variable."
)
description = (
"This command line program accesses the IMAP SDC APIs to query, download, "
"and upload data files."
)
download_help = (
"Download a file from the IMAP SDC to the locally configured data directory. "
)
file_path_help = (
"This must be the full path to the file."
"\nE.g. imap/mag/l0/2025/01/imap_mag_l0_raw_20250101_v001.pkts"
)
query_help = (
"Query the IMAP SDC for files matching the query parameters. "
"The query parameters are optional, but at least one must be provided."
)
upload_help = (
"Upload a file to the IMAP SDC. This must be the full path to the file."
"\nE.g. imap/mag/l0/2025/01/imap_mag_l0_raw_20250101_v001.pkts"
)
url_help = (
"URL of the IMAP SDC API. "
"The default is https://api.dev.imap-mission.com. This can also be "
"set using the IMAP_DATA_ACCESS_URL environment variable."
)
parser = argparse.ArgumentParser(prog="imap-data-access", description=description)
parser.add_argument(
"--version",
action="version",
version=f"%(prog)s {imap_data_access.__version__}",
)
parser.add_argument("--api-key", type=str, required=False, help=api_key_help)
parser.add_argument("--data-dir", type=Path, required=False, help=data_dir_help)
parser.add_argument("--url", type=str, required=False, help=url_help)
# Logging level
parser.add_argument(
"--debug",
help="Print lots of debugging statements",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
default=logging.WARNING,
)
parser.add_argument(
"-v",
"--verbose",
help="Add verbose output",
action="store_const",
dest="loglevel",
const=logging.INFO,
)
# Download command
subparsers = parser.add_subparsers(required=True)
parser_download = subparsers.add_parser(
"download", help=download_help, description=download_help
)
parser_download.add_argument("file_path", type=Path, help=file_path_help)
parser_download.set_defaults(func=_download_parser)
# Query command (with optional arguments)
query_parser = subparsers.add_parser(
"query", help=query_help, description=query_help
)
query_parser.add_argument(
"--instrument",
type=str,
required=False,
help="Name of the instrument",
choices=[
"codice",
"glows",
"hi",
"hit",
"idex",
"lo",
"mag",
"swapi",
"swe",
"ultra",
],
)
query_parser.add_argument(
"--data-level",
type=str,
required=False,
help="Data level of the product (l0, l1a, l2, etc.)",
)
query_parser.add_argument(
"--descriptor",
type=str,
required=False,
help="Descriptor of the product (raw, burst, etc.)",
)
query_parser.add_argument(
"--start-date", type=str, required=False, help="Start date in YYYYMMDD format"
)
query_parser.add_argument(
"--end-date", type=str, required=False, help="End date in YYYYMMDD format"
)
query_parser.add_argument(
"--repointing", type=int, required=False, help="Repointing number (int)"
)
query_parser.add_argument(
"--version",
type=str,
required=False,
help="Version of the product in the format 'v001'",
)
query_parser.add_argument(
"--extension", type=str, required=False, help="File extension (cdf, pkts)"
)
query_parser.add_argument(
"--output-format",
type=str,
required=False,
help="How to format the output, default is 'table'",
choices=["table", "json"],
default="table",
)
query_parser.set_defaults(func=_query_parser)
# Upload command
parser_upload = subparsers.add_parser(
"upload", help=upload_help, description=upload_help
)
parser_upload.add_argument("file_path", type=Path, help=file_path_help)
parser_upload.set_defaults(func=_upload_parser)
# Parse the arguments and set the values
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
if args.data_dir:
# We got an explicit data directory from the command line
data_path = args.data_dir.resolve()
if not data_path.exists():
raise ValueError(f"Data directory {args.data_dir} does not exist")
# Set the data directory to the user-supplied value
imap_data_access.config["DATA_DIR"] = data_path
if args.url:
# We got an explicit url from the command line
imap_data_access.config["DATA_ACCESS_URL"] = args.url
if args.api_key:
# We got an explicit api key from the command line
imap_data_access.config["API_KEY"] = args.api_key
# Now process through the respective function for the invoked command
# (set with set_defaults on the subparsers above)
try:
args.func(args)
except Exception as e:
parser.error(e)
if __name__ == "__main__":
main()