forked from SWOT-Confluence/combine_data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine_data.py
More file actions
314 lines (262 loc) · 11 KB
/
combine_data.py
File metadata and controls
314 lines (262 loc) · 11 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
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
312
313
314
"""Combine JSON Data Files
This script combines the continent-level data files produced by datagen into
single, global-level files.
The following files are combined and created:
- basin.json
- cycle_passes.json
- hivdisets.json
- metrosets.json
- passes.json
- reach_node.json
- reaches.json
- s3_list.json
- sicsets.json
Files are written out to directory referenced by 'datadir' command line argument.
"""
# Example deployment
# expanded
# not expanded
# docker run -v /storage/repos/setfinder/testing:/data combine_data -d /data -c expanded_continent.json
# Standard imports
import argparse
from collections import OrderedDict
import datetime
import glob
import json
import logging
import pathlib
import re
import sys
import os
# Third-party imports
import boto3
import botocore
import fnmatch
CONTINENTS = [
{ "af" : [1] },
{ "as" : [4, 3] },
{ "eu" : [2] },
{ "na" : [7, 8, 9] },
{ "oc" : [5] },
{ "sa" : [6] }
]
def create_args():
"""Create and return argparser with arguments."""
arg_parser = argparse.ArgumentParser(description="Combine continent JSON files")
arg_parser.add_argument("-c",
"--contfile",
type=str,
default="continent.json",
help="Name of continent.json file.")
arg_parser.add_argument("-d",
"--datadir",
type=str,
help="Path to directory that contains datagen data.")
arg_parser.add_argument("-u",
"--uploadbucket",
type=str,
help="Name of S3 bucket to upload JSON files to.")
arg_parser.add_argument("-k",
"--bucketkey",
type=str,
help="Name of prefix to upload JSON files to.")
arg_parser.add_argument("-s",
"--sword_version",
type=str,
help="version of sword to use.")
arg_parser.add_argument("-x",
"--delete",
help="Indicate if continent-level JSON files should be deleted.",
action="store_true")
arg_parser.add_argument("-e",
"--expanded",
help="Indicate we are looking for expanded set files.",
action="store_true")
return arg_parser
def get_logger():
"""Return a formatted logger object."""
# Create a Logger object and set log level
logger = logging.getLogger(__name__)
if not logger.hasHandlers():
logger.setLevel(logging.DEBUG)
# Create a handler to console and set level
console_handler = logging.StreamHandler()
# Create a formatter and add it to the handler
console_format = logging.Formatter("%(asctime)s - %(module)s - %(levelname)s : %(message)s")
console_handler.setFormatter(console_format)
# Add handlers to logger
logger.addHandler(console_handler)
# Return logger
return logger
def combine_continents(continents, data_dir, sword_version, expanded, logger):
"""Combine continent-level data in to global data.
Parameters
----------
continents: list
List of continents to combine
data_dir: pathlib.Path
Path to datagen directory
json_dict: dict
Dictionary of global data lists
logger: logger
Logger instance to use for logging statements
Returns
-------
dict
Dictionary of global data lists
"""
out_dict = {}
continent_json = []
for continent in continents:
if expanded:
all_continent_files = glob.glob(os.path.join(data_dir, f'expanded_reaches_of_interest_{continent}.json'))
else:
all_continent_files = glob.glob(os.path.join(data_dir, f'*_{continent}.json'))
if all_continent_files and not expanded:
key = all_continent_files[0].split("_")[-1].split(".")[0]
for element in CONTINENTS:
for c, i in element.items():
if c == key:
continent_json.append({key: i})
if not expanded:
all_continent_files = [i for i in all_continent_files if not os.path.basename(i).startswith('expanded') ]
for continent_file in all_continent_files:
with open(continent_file) as jf:
data = json.load(jf)
global_file_basename = os.path.basename(continent_file).replace(f'_{continent}.json', '')
if global_file_basename not in list(out_dict.keys()):
out_dict[global_file_basename] = []
out_dict[global_file_basename].extend(data)
if not expanded and os.path.basename(continent_file).startswith('reaches'):
if 'basin' not in list(out_dict.keys()):
out_dict['basin'] = []
logger.info('making basin from %s', continent_file)
base_reaches = [reach_data["reach_id"] for reach_data in data]
basin_ids = list(set([str(reach)[:4] for reach in base_reaches]))
basin_data = [create_basin_data(data_dir, basin_id, base_reaches, sword_version) for basin_id in basin_ids]
out_dict['basin'].extend(basin_data)
reaches_json_list = []
for a_key in list(out_dict.keys()):
outpath = os.path.join(data_dir, a_key + '.json')
reaches_json_list.append(outpath)
with open(outpath, 'w') as jf:
json.dump(out_dict[a_key], jf, indent=2)
logger.info(f"Written: {outpath}.")
if not expanded:
c_file = os.path.join(data_dir, 'continent.json')
reaches_json_list.append(c_file)
with open(c_file, 'w') as jf:
json.dump(continent_json, jf, indent=2)
logger.info(f"Written: {c_file}")
ssc_json_data = combine_ssc(data_dir=data_dir, logger = logger)
if len(ssc_json_data) > 0:
ssc_json = os.path.join(data_dir,"ssc_hls_list.json")
with open(ssc_json, "w") as jf:
json.dump(ssc_json_data, jf, indent=2)
reaches_json_list.append(ssc_json)
logger.info(f"Written: %s", ssc_json)
else:
logger.info("No SSC JSON written.")
return reaches_json_list
def combine_ssc(data_dir: str, logger):
"""Combine SSC input data into a single file."""
ssc_input_data = glob.glob(os.path.join(data_dir, "ssc", "*.json"))
ssc_json_data = {}
for ssc_input in ssc_input_data:
with open(ssc_input) as jf:
data = json.load(jf)
for key, value in data.items():
short_key = key[:-10]
if short_key in ssc_json_data:
# Use set for deduplication during merge
ssc_json_data[short_key].update(value)
else:
# Store as set for efficient deduplication
ssc_json_data[short_key] = set(value)
# Convert sets back to lists only at the end
single_entry_list = [{k: list(v)} for k, v in ssc_json_data.items()]
return single_entry_list
def create_basin_data(data_dir, basin_id, base_reaches, sword_version):
continent_codes = { '1': "af", '2': "eu", '3': "as", '4': "as", '5': "oc", '6': "sa", '7': "na", '8': "na", '9':"na" }
sword_filepath = os.path.join(data_dir, "sword", f"{continent_codes[str(basin_id)[0]]}_sword_v{sword_version}_patch.nc")
if os.path.exists(sword_filepath):
sword_file = os.path.basename(sword_filepath)
else:
sword_file = f"{continent_codes[str(basin_id)[0]]}_sword_v{sword_version}.nc"
return {
"basin_id": basin_id,
"reach_id": [reach_id for reach_id in base_reaches if str(reach_id).startswith(str(basin_id))],
"sword": sword_file,
"sos": f"{continent_codes[str(basin_id)[0]]}_sword_v{sword_version}_SOS_priors.nc"
}
def upload(json_file_list, upload_bucket, bucket_key, input_dir, expanded, logger):
"""Upload JSON files to S3 bucket."""
s3 = boto3.client("s3")
try:
for json_file in json_file_list:
json_file = pathlib.Path(json_file)
if json_file.name == "expanded_reaches_of_interest.json": continue
# Upload under bucket key
s3.upload_file(str(json_file),
upload_bucket,
f"{bucket_key}/{json_file.name}",
ExtraArgs={"ServerSideEncryption": "aws:kms"})
logger.info(f"Uploaded {json_file} to {upload_bucket}/{bucket_key}.")
# Upload to root of bucket
s3.upload_file(str(json_file),
upload_bucket,
json_file.name,
ExtraArgs={"ServerSideEncryption": "aws:kms"})
logger.info(f"Uploaded {json_file} to {upload_bucket}.")
# Upload expanded reaches of interest
expanded_roi = pathlib.Path(input_dir).joinpath("expanded_reaches_of_interest.json")
if expanded_roi.exists():
s3.upload_file(str(expanded_roi),
upload_bucket,
expanded_roi.name,
ExtraArgs={"ServerSideEncryption": "aws:kms"})
logger.info(f"Uploaded {expanded_roi} to {upload_bucket}.")
if not expanded and expanded_roi.exists():
s3.upload_file(str(expanded_roi),
upload_bucket,
f"{bucket_key}/{expanded_roi.name}",
ExtraArgs={"ServerSideEncryption": "aws:kms"})
logger.info(f"Uploaded {expanded_roi} to {upload_bucket}/{bucket_key}.")
except botocore.exceptions.ClientError as e:
raise e
def combine_data():
"""Combine continent-level JSON files into global files."""
start = datetime.datetime.now()
# Get logger
logger = get_logger()
# Command line arguments
arg_parser = create_args()
args = arg_parser.parse_args()
for arg in vars(args):
logger.info("%s: %s", arg, getattr(args, arg))
# Load continents
continents = [
"af",
"as",
"eu",
"na",
"oc",
"sa"
]
# Combine continent-level data
json_file_list = combine_continents(continents, args.datadir, args.sword_version, args.expanded, logger)
# Check for lakeflow data
viable_lakes = pathlib.Path(args.datadir).joinpath("lakeflow", "viable", "viable_locations.csv")
if viable_lakes.exists(): json_file_list.append(str(viable_lakes))
# Upload JSON files to S3
if args.uploadbucket:
try:
upload(json_file_list, args.uploadbucket, args.bucketkey, args.datadir, args.expanded, logger)
except botocore.exceptions.ClientError as e:
logger.error(e)
logger.info("System exiting.")
sys.exit(1)
end = datetime.datetime.now()
logger.info(f"Execution time: {end - start}")
if __name__ == "__main__":
combine_data()