-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.py
More file actions
executable file
·689 lines (592 loc) · 29.7 KB
/
Copy pathpublish.py
File metadata and controls
executable file
·689 lines (592 loc) · 29.7 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#!/usr/bin/env python3
'''
Thin wrapper around ESGF publishing software.
Use to publish CCCma datasets to ESGF.
https://esg-publisher.readthedocs.io/en/main/index.html
'''
import argparse
import json
import os
import requests
import shutil
import subprocess
import sys
import time
import yaml
from collections import OrderedDict
from datetime import datetime, UTC
from pystac_client import Client
from tools import (find_datasets, get_unique_param_values, match_params,
publication_checks, data_request_checks, get_dreq_validation_file)
from esgfsearch import search, show_params, parse_file_size_str, file_size_str
##############################################################################
DATE_FORMAT = '%d %b %Y, %H:%M:%S UTC'
def check_env(config):
'''check that correct env is active'''
if 'venv' in config:
env = config['venv']
env_realpath = os.path.realpath(env)
if do_cmds:
if 'VIRTUAL_ENV' not in os.environ:
raise ValueError('No venv is activated')
if os.environ['VIRTUAL_ENV'] != env_realpath:
cmd = 'source ' + os.path.join(env, 'bin/activate')
raise OSError(f'To run commands, first do:\n {cmd}')
elif 'conda env' in config:
env = config['conda env']
if do_cmds:
if 'CONDA_DEFAULT_ENV' not in os.environ:
raise ValueError('No conda env is activated')
if os.environ['CONDA_DEFAULT_ENV'] != env:
raise OSError('To run commands, first do:\n conda activate ' + env)
else:
raise Exception('Need to specify env to run publishing commands')
def exec_cmds(commands: list[str], cmd_args: dict, do_cmds: bool = True, retries: int = 0) -> list[dict]:
'''
Execute list of commands.
Checks return codes of commands and stops if a command fails.
Arguments
---------
commands: list[str]
List of command templates. Example of one command template:
"esgmapfile make --project {project} --outdir {mapfile_path} --directory {dataset_path}"
cmd_args: dict
Argument:value pairs to substitute into command templates. Example:
{'project': 'cmip7'}
do_cmds: bool
True ==> execute the commands
False ==> show the commands that would be executed, but don't execute them
retries: int
Number of times to retry a command if it fails.
retries = 0 ==> only try it once
Returns
-------
exit_status: int
Exit status of last command executed (0 = success).
'''
cmds = []
for cmd in commands:
cmds.append( cmd.format(**cmd_args) )
exit_status = None
attempt = 1
max_attempts = 1 + retries
start_time = time.time()
cmd_results = []
for cmd in cmds:
cmd_result = {'cmd': cmd}
cmd_results.append(cmd_result)
if do_cmds:
while attempt <= max_attempts:
if attempt > 1:
# Show message saying this is a retry
print(f'Returned exit status={exit_status}, retrying (attempt {attempt} of {max_attempts})')
print(cmd)
# Using subprocess.run works fine but the stdout is not seen by the user
# result = subprocess.run(cmd.split(), capture_output=True, text=True)
# exit_status = result.returncode
# Using subprocess.Popen allows user to see the stdout
result = subprocess.Popen(
cmd.split(),
stdout=sys.stdout, # preserves colour (if any) in the stdout
stderr=sys.stderr,
text=True,
)
result.communicate()
exit_status = result.returncode
cmd_result.update({'exit_status': exit_status, 'attempt': attempt})
if exit_status == 0:
# Command has succeeded, so exit the retry loop
break
else:
# Command failed
attempt += 1
if exit_status != 0:
# Command failed, so don't attempt any subsequent commands
break
else:
# Show command that would have been executed
print(cmd)
cmd_result.update({'exit_status': 'N/A', 'attempt': 0})
# Record time taken to complete the command (units: seconds)
cmd_result['time'] = time.time() - start_time
return cmd_results
def log_cmds(logfile: str, dataset_id: str, cmd_results: dict):
'''
Write success/fail status of commands.
'''
msg = [dataset_id]
for cmd_result in cmd_results:
msg += [cmd_result['cmd']]
cmd_result['time'] = str('%.4f' % cmd_result['time'])
details = 'exit_status: {exit_status}, attempts: {attempt}, time: {time} s'.format(**cmd_result)
if cmd_result['exit_status'] == 0:
msg += [f'SUCCESS - {details}']
else:
msg += [f'FAIL - {details}']
msg = '\n'.join(msg) + '\n'*2
with open(logfile, 'a') as f:
f.write(msg)
def parse_args():
parser = argparse.ArgumentParser(
description='Publish CCCma datasets to ESGF'
)
parser.add_argument('-c', '--config', type=str, default='config-datasets.yaml',
help='name of config file containing datasets to publish, default: %(default)s')
# Define different publishing actions as input flags
default_datasets_file = 'datasets.json'
actions = OrderedDict({
'datasets': {
'short': '-d',
'help': f'find datasets to publish and write info on them to json file (default: {default_datasets_file})'
},
'mapfile': {
'short': '-m',
'help': 'generate mapfiles'
},
'publish': {
'short': '-p',
'help': 'publish to ESGF'
},
'inventory': {
'short': '-i',
'help': 'do datasets inventory ' +
'(equivalent to this set of options: -d -nesgf -ndreq -nval -df inventory.json)'
}
})
for action, d in actions.items():
parser.add_argument(d['short'], f'--{action}', action='store_true', default=False, help=d['help'])
# Additional arguments
parser.add_argument('-dry', '--dry-run', action='store_true', default=False,
help='show commands but don\'t execute them')
parser.add_argument('-max', '--max-size', type=str,
help='maximum size of dataset to retain, examples: "1 GB", 1GB, 1G')
parser.add_argument('-min', '--min-size', type=str,
help='minimum size of dataset to retain, examples: "1 GB", 1GB, 1G')
parser.add_argument('-nxr', '--no-xarray', action='store_true', default=False,
help='use --no-xarray argument to esgpublish (prevents failure on large datasets)')
parser.add_argument('-df', '--datasets-file', type=str, default=default_datasets_file,
help='name of datasets output json file')
parser.add_argument('-nesgf', '--no-esgf-search', action='store_true', default=False,
help='turn off ESGF search that checks whether datasets are already published')
parser.add_argument('-ndreq', '--no-data-request', action='store_true', default=False,
help='turn off filtering based on the data request')
parser.add_argument('-nval', '--no-validation', action='store_true', default=False,
help='turn off checking of validation list (Stamp of Approval) - use with caution!')
parser.add_argument('-r', '--retries', type=int, default=0,
help='number of times to retry publishing command if it fails (default: 0)')
parser.add_argument('-s', '--start', type=int,
help='index to begin with in list of datasets (0 = first dataset)')
parser.add_argument('-n', '--number', type=int,
help='number of datasets to use from list of datasets (default: all)')
parser.add_argument('-id', '--dataset-ids', type=str,
help='dataset ids to use: comma-separated list, or file listing datasets')
parser.add_argument('-c7', '--cmip7-dev', action='store_true', default=False,
help='TEMPORARY option for use with -d for CMIP7 ESGF-NG publishing')
parser.add_argument('-api', '--api-method', type=int, default=3,
help='TEMPORARY specify how to use restful api to find out what datasets are already published')
args = parser.parse_args()
if not any([args.__dict__[action] for action in actions]):
print('Specify at least one of these options (invoke with -h for more info): ')
for action, d in actions.items():
print(f' {d["short"]}, --{action}')
sys.exit()
return args
def load_config_file(config_file: str) -> dict:
'''
Load yaml configuration file and return contents as dict.
'''
if not os.path.exists(config_file):
raise OSError('Config file not found: ' + config_file)
with open(config_file) as f:
config = yaml.safe_load(f)
print('Loaded ' + config_file)
return config
if __name__ == '__main__':
args = parse_args()
if args.datasets_file:
datasets_file = args.datasets_file
log_dir = 'logs'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
date_run = datetime.now(UTC).strftime('%Y%m%d_%H%M%SUTC')
logfile = os.path.join(log_dir, f'log_cmds_{date_run}.log')
qc_reports_dir = 'ccreport'
if not os.path.exists(qc_reports_dir):
os.makedirs(qc_reports_dir)
##############################################################################
# Load dataset configuration settings from config file
config_dat = load_config_file(args.config)
repo_path = os.environ['REPO_PATH']
if not os.path.exists(repo_path):
raise ValueError('Path to esgwrapper code repo is required, received: ' + repo_path)
project = config_dat['project']
# Load configuration settings for publishing commands
config_pub = load_config_file(os.path.join(repo_path, 'esg_ng', 'config-publisher.yaml'))
##############################################################################
if args.datasets or args.inventory:
# Determine datasets to publish, write them to datasets_file
search_esgf = not args.no_esgf_search
check_data_request = not args.no_data_request
do_validation = not args.no_validation
search_esgf_ng = False
if args.inventory:
search_esgf = False
check_data_request = False
do_validation = False
datasets_file = 'inventory.json'
if args.cmip7_dev:
search_esgf = False
check_data_request = False
do_validation = False
search_esgf_ng = True
get_size = True
if args.max_size:
max_size = parse_file_size_str(args.max_size)
if args.min_size:
min_size = parse_file_size_str(args.min_size)
base_paths = config_dat['paths'] # top-level paths to search at
dataset_paths = config_dat['datasets'] # datasets to search (dir path for some level in the DRS dir tree)
dataset_template = config_pub['DRS'][project]['dataset']
path_template = config_pub['DRS'][project]['path']
file_template = config_pub['DRS'][project]['file'] # not currently needed (but might be for some projects?)
datasets = {}
searched_base_paths = []
for base_path in base_paths:
if os.path.exists(base_path):
print('Searching path: ' + base_path)
searched_base_paths.append(base_path)
else:
print('Path not found: ' + base_path)
for dataset_path in dataset_paths:
d = find_datasets(project,
base_path, dataset_path,
dataset_template, path_template, file_template,
get_size=get_size)
datasets.update(d)
del d
print(f'Found {len(datasets)} datasets')
# Apply filters specified in config-datasets file
if config_dat['keep']:
print('Keeping datasets with these parameter values:')
show_params(config_dat['keep'], indent=' ')
keep = set()
for dataset_id, info in datasets.items():
matches = match_params(info['params'], config_dat['keep'])
if all(matches.values()):
keep.add(dataset_id)
n = len(datasets)
datasets = {s: datasets[s] for s in keep}
print(f' --> excluded {n-len(datasets)} datasets')
if config_dat['exclude']:
print('Excluding datasets with these parameter values:')
show_params(config_dat['exclude'], indent=' ')
exclude = set()
for dataset_id, info in datasets.items():
matches = match_params(info['params'], config_dat['exclude'])
if any(matches.values()):
exclude.add(dataset_id)
n = len(datasets)
datasets = {s: datasets[s] for s in datasets if s not in exclude}
print(f' --> excluded {n-len(datasets)} datasets')
print(f'Retained {len(datasets)} datasets')
# Filter based on dataset size
if args.max_size:
print(f'Keeping datasets with size up to {args.max_size} ({max_size} B)')
keep = set()
for dataset_id, info in datasets.items():
if info['size (bytes)'] <= max_size:
keep.add(dataset_id)
n = len(datasets)
datasets = {s: datasets[s] for s in keep}
print(f' --> excluded {n-len(datasets)} datasets')
if args.min_size:
print(f'Keeping datasets with size at least {args.min_size} ({min_size} B)')
keep = set()
for dataset_id, info in datasets.items():
if info['size (bytes)'] >= min_size:
keep.add(dataset_id)
n = len(datasets)
datasets = {s: datasets[s] for s in keep}
print(f' --> excluded {n-len(datasets)} datasets')
# Ensure datasets with size zero and/or no files are discarded
if get_size:
exclude = set()
for dataset_id, info in datasets.items():
if info['size (bytes)'] == 0 or info['no. of files'] == 0:
exclude.add(dataset_id)
n = len(datasets)
keep = [s for s in datasets if s not in exclude]
datasets = {s: datasets[s] for s in keep}
print(f' --> excluded {n-len(datasets)} datasets that had zero size and/or no valid files')
# Filter based on other criteria
if do_validation:
# Check stamp of approval and other validation criteria
validation_file = os.path.join(repo_path, 'input/validation_variables.json')
datasets = publication_checks(datasets, validation_file)
else:
print('WARNING: data validation (Stamp of Approval) filtering is off')
if check_data_request:
# Check which datasets are requested in the project's data request, exclude those that aren't
validation_file = get_dreq_validation_file(project, repo_path)
datasets = data_request_checks(datasets, validation_file)
else:
print('WARNING: data request filtering is off')
dataset_sep = '.'
dataset_parameters = [s.strip('{').strip('}') for s in dataset_template.split(dataset_sep)]
param_unique_values = get_unique_param_values(datasets, dataset_parameters)
if search_esgf:
# Check which datasets are already published
index_node = config_pub['search_esgf']['index_node']
print(f'Checking for already published datasets by searching {index_node}')
published_datasets = search(param_unique_values, dataset_parameters, project, index_node,
verbose=config_pub['search_esgf']['verbose'],
show_browser_url=config_pub['search_esgf']['show_browser_url'],
keep_params=None,
)
keep = set(datasets.keys()).difference(set(published_datasets))
n = len(datasets)
datasets = {s: datasets[s] for s in keep}
if len(datasets) == n:
print('None of the datasets are already published')
elif len(datasets) == 0:
print('All of the datasets are already published')
else:
print(f'Removed {n-len(datasets)} already-published datasets from publishing list (keeping {len(datasets)})')
if search_esgf_ng:
# # Check which datasets are already published by searching ESGF-NG.
# # TODO: figure out whether or not this should replace the above search_esgf call.
# index_node = config_pub['search_esgf_ng']['index_node']
# print(f'Checking for already published datasets by searching {index_node}')
# published_datasets = search_ng(param_unique_values, dataset_parameters, project, index_node,
# # verbose=config_pub['search_esgf']['verbose'],
# # show_browser_url=config_pub['search_esgf']['show_browser_url'],
# # keep_params=None,
# )
# Temporary option (Aug 2026) while figuring out best way to query ESGF-NG via api.
#
# The assumption here is that if a dataset is already published, a URL will exist of the form:
# https://discovery.{where}.esgf.io/collections/CMIP7/items/{dataset_id}
# Example:
# https://discovery.east.esgf.io/collections/CMIP7/items/MIP-DRS7.CMIP7.CMIP.CCCma.CanESM5-1.piControl.r1i1p2f1.glb.mon.uas.tavg-h10m-hxy-u.g120.v20190429
#
# Use this to determine what's already published.
if search_esgf:
raise Exception('this is a stopgap for proper ESGF search')
index_node = 'east'
# index_node = 'west'
keep = []
if args.api_method == 1:
for dataset_id in datasets:
url = f'https://discovery.{index_node}.esgf.io/collections/CMIP7/items/{dataset_id}'
response = requests.get(url).json()
# print(url)
if 'code' in response:
if response['code'] == "NotFoundError":
# Dataset was not found, therefore is not already published
keep.append(dataset_id)
# Example of returned json:
#{"code":"NotFoundError","description":"Item MIP-DRS7.CMIP7.CMIP.CCCma.CanESM5-1.piControl.r1i1p2f1.glb.mon.boovas.tavg-h10m-hxy-u.g120.v20190429 does not exist inside Collection CMIP7"}'
datasets = {s: datasets[s] for s in keep}
del keep
elif args.api_method == 2:
limit = 10000000000 # set large enough to get all datasets on ESGF
limit = str(limit)
url = f'https://discovery.east.esgf.io/collections/CMIP7/items?fields=id,properties.retracted&limit={limit}'
print(f'Checking for already-published datasets by searching:\n {url}')
response = requests.get(url).json()
outfile = 'published_datasets.json'
with open(outfile, 'w') as f:
json.dump(response, f, indent=2)
print('Wrote ' + outfile)
# Get list of published dataset id's
exclude = []
for d in response['features']:
dataset_id = d['id']
retracted = bool(d['properties']['retracted'])
if not retracted:
exclude.append(dataset_id)
datasets = {s: datasets[s] for s in datasets if s not in exclude}
del exclude
elif args.api_method == 3:
# Use STAC API
url_endpoint = f"https://discovery.{index_node}.esgf.io"
client = Client.open(url_endpoint)
print(f'Searching ESGF-NG using STAC API for endpoint: {url_endpoint}')
exclude = set()
for source_id in param_unique_values['source_id']:
for experiment_id in param_unique_values['experiment_id']:
query = {
'collections': [project.upper()],
'query': {
f'{project}:source_id': {"eq": source_id},
f'{project}:experiment_id': {"eq": experiment_id},
}
}
search = client.search(**query)
items = search.item_collection()
dataset_ids = [item.id for item in items]
print(f' Found {len(dataset_ids)} published datasets for source_id={source_id} and experiment_id={experiment_id}')
exclude.update(dataset_ids)
del dataset_ids
print(f' TOTAL no. of published datasets: {len(exclude)}')
datasets = {s: datasets[s] for s in datasets if s not in exclude}
exclude = sorted(exclude, key=str.lower)
outfile = 'published_datasets.txt'
w = '\n'.join(exclude) + '\n'
with open(outfile, 'w') as f:
f.write(w)
print(f'Wrote {outfile} listing {len(exclude)} published datasets')
del exclude
else:
raise ValueError(f'Which ad-hoc API method should be used?')
if len(datasets) == n:
print('None of the datasets are already published')
elif len(datasets) == 0:
print('All of the datasets are already published')
else:
print(f'Removed {n-len(datasets)} already-published datasets from publishing list (keeping {len(datasets)})')
datasets = OrderedDict({s : datasets[s] for s in sorted(datasets.keys(), key=str.lower)})
param_unique_values = get_unique_param_values(datasets, dataset_parameters)
if any([len(vals) > 0 for vals in param_unique_values.values()]):
print('Unique parameter values:')
for p in dataset_parameters:
print(f' {p} : ' + ', '.join(param_unique_values[p]))
out = OrderedDict({
'Header' : {
'date of search' : datetime.now(UTC).strftime(DATE_FORMAT),
'paths searched' : searched_base_paths,
'no. of datasets' : len(datasets),
'unique parameter values' : param_unique_values,
},
'datasets' : datasets
})
if get_size:
# Report total size of datasets
size = 0
for dataset_id, info in datasets.items():
size += info['size (bytes)']
total_size = file_size_str(size)
out['Header'].update({
'total size (all datasets)': total_size
})
msg = f'Total size of publishable datasets: {total_size}'
if args.inventory:
msg = f'Total size of inventoried datasets: {total_size}'
print(msg)
filepath = datasets_file
with open(filepath, 'w') as f:
json.dump(out, f, indent=4)
print(f'Wrote {filepath} with {len(datasets)} datasets')
# config_dat is not used after this point since all info on datasets to be published
# should be in the output json file datasets_file.
del config_dat
##############################################################################
if args.mapfile or args.publish:
# Load info on datasets to publish
filepath = datasets_file
with open(filepath, 'r') as f:
datasets = json.load(f)['datasets']
print('Loaded ' + filepath)
dataset_ids = sorted(datasets.keys(), key=str.lower)
if args.dataset_ids:
if os.path.exists(args.dataset_ids):
with open(args.dataset_ids) as f:
dataset_ids = f.readlines()
else:
dataset_ids = args.dataset_ids.split(',')
dataset_ids = [s.strip() for s in dataset_ids]
if args.start:
dataset_ids = dataset_ids[args.start:]
if args.number:
dataset_ids = dataset_ids[:args.number]
datasets = OrderedDict({s : datasets[s] for s in dataset_ids})
del dataset_ids
do_cmds = not args.dry_run
##############################################################################
if args.mapfile:
# Generate mapfiles. These are small files containing info about each dataset,
# including the checksums of its files.
# Check that correct env is activated
check_env(config_pub['mapfile'])
# Get info to construct mapfile paths
mapfile_path_template = config_pub['mapfile']['mapfile_subdir'][project]
mapfile_base_path = config_pub['mapfile']['mapfile_dir']
if not os.path.exists(mapfile_base_path):
os.makedirs(mapfile_base_path)
# Get mapfile template, used to determine if a mapfile already exists
dataset_template = config_pub['DRS'][project]['dataset']
mapfile_template = dataset_template + os.path.extsep + 'map'
# Example mapfile name for CMIP6:
# CMIP6.DCPP.CCCma.CanESM5.dcppB-forecast.s2022-r1i1p2f1.Amon.tas.gn.v20190429.map
# Get command template(s)
commands = config_pub['mapfile']['commands']
# Loop over datasets to create a mapfile for each one
n = len(datasets)
k = 0
for dataset_id, info in datasets.items():
k += 1
print(f'\nGenerating mapfile for dataset ({k} of {n}): {dataset_id} ({info["size (human readable)"]})')
cmd_args = {
'mapfile_path' : os.path.normpath(os.path.join(
mapfile_base_path, mapfile_path_template.format(**info['params'])
)),
'dataset_path' : info['path'],
'project' : project,
}
if not config_pub['mapfile']['clobber']:
filename = mapfile_template.format(**info['params'])
filepath = os.path.join(cmd_args['mapfile_path'], filename)
if os.path.exists(filepath):
print('Not overwriting existing mapfile: ' + filepath)
continue
# Run commands to generate mapfile for this dataset
cmd_results = exec_cmds(commands, cmd_args, do_cmds)
if do_cmds:
# Write logfile summarizing the results of commands
log_cmds(logfile, dataset_id, cmd_results)
##############################################################################
if args.publish:
# Publish to ESGF. This assumes that mapfiles have already been generated.
# Check that correct env is activated
check_env(config_pub['publish'])
# Get info to construct mapfile paths
mapfile_path_template = config_pub['mapfile']['mapfile_subdir'][project]
mapfile_base_path = config_pub['mapfile']['mapfile_dir']
# Get command template(s)
commands = config_pub['publish']['commands']
if args.no_xarray:
# Convenience option to add --no-xarray argument to esgpublish command.
# Intention of this wrapper is that publisher command(s) are set in config-publisher.yaml.
# However we only need to use --no-xarray for large datasets, so it's useful
# to be able to specify it as a command-line argument to publish.py instead
# of modifying config-publisher.yaml often (error-prone) or having more than
# one config-publisher.yaml (confusing). Can use in conjuntion with -min argument.
for k,cmd in enumerate(commands):
if cmd.startswith('esgpublish'):
commands[k] = cmd + ' --no-xarray'
# Loop over datasets to publish each one
n = len(datasets)
k = 0
for dataset_id, info in datasets.items():
k += 1
print(f'\nPublishing dataset ({k} of {n}): {dataset_id}')
# Find mapfile for this dataset
mapfile_path = os.path.join(mapfile_base_path, mapfile_path_template.format(**info['params']))
mapfile = dataset_id + os.path.extsep + 'map'
cmd_args = {
'mapfile' : os.path.normpath(os.path.join(mapfile_path, mapfile))
}
if not os.path.exists(cmd_args['mapfile']):
print('Mapfile not found: ' + cmd_args['mapfile'])
continue
# Run commands to publish this dataset
cmd_results = exec_cmds(commands, cmd_args, do_cmds, retries=args.retries)
if do_cmds:
# Write logfile summarizing the results of commands
log_cmds(logfile, dataset_id, cmd_results)
# If QC report output file was created, move it to a subdir
qc_report_file = f'{dataset_id}.ccreport'
if os.path.exists(qc_report_file):
shutil.move(qc_report_file, os.path.join(qc_reports_dir, qc_report_file))
if os.path.exists(logfile):
print(f'\nWrote logfile: {logfile}')