-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_fre_cmor_cli.py
More file actions
604 lines (483 loc) · 23.6 KB
/
test_fre_cmor_cli.py
File metadata and controls
604 lines (483 loc) · 23.6 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
"""
CLI Tests for fre cmor *
Tests the command-line-interface calls for tools in the fre cmor suite.
Each tool generally gets 3 tests:
- fre cmor $tool, checking for exit code 0 (fails if cli isn't configured right)
- fre cmor $tool --help, checking for exit code 0 (fails if the code doesn't run)
- fre cmor $tool --optionDNE, checking for exit code 2 (fails if cli isn't configured
right and thinks the tool has a --optionDNE option)
We also have a set of more complicated tests for testing the full set of
command-line args for fre cmor yaml and fre cmor run.
"""
from datetime import date
import json
import os
from pathlib import Path
import shutil
from click.testing import CliRunner
from fre import fre
runner = CliRunner()
# where are we? we're running pytest from the base directory of this repo
#ROOTDIR = 'fre/tests/test_files'
ROOTDIR = str( Path( fre.__file__ ).parent ) + '/tests/test_files'
# these unit tests should be more about the cli, rather than the workload
YYYYMMDD=date.today().strftime('%Y%m%d')
COPIED_NC_FILEPATH = f'{ROOTDIR}/ocean_sos_var_file/reduced_ocean_monthly_1x1deg.199301-199302.sosV2.nc'
ORIGINAL_NC_FILEPATH = f'{ROOTDIR}/ocean_sos_var_file/reduced_ocean_monthly_1x1deg.199301-199302.sos.nc'
def test_setup_test_files():
""" set-up test: copy and rename NetCDF file created in test_fre_cmor_run_subtool.py """
assert Path(ORIGINAL_NC_FILEPATH).exists()
if Path(COPIED_NC_FILEPATH).exists():
Path(COPIED_NC_FILEPATH).unlink()
assert not Path(COPIED_NC_FILEPATH).exists()
shutil.copy(Path(ORIGINAL_NC_FILEPATH), Path(COPIED_NC_FILEPATH))
assert Path(COPIED_NC_FILEPATH).exists()
# fre cmor
def test_cli_fre_cmor():
''' fre cmor '''
result = runner.invoke(fre.fre, args=["cmor"])
assert result.exit_code == 2
def test_cli_fre_cmor_help():
''' fre cmor --help '''
result = runner.invoke(fre.fre, args=["cmor", "--help"])
assert result.exit_code == 0
def test_cli_fre_cmor_help_and_debuglog():
''' fre -vv -l TEST_FOO_LOG.log cmor --help '''
if Path("TEST_FOO_LOG.log").exists():
Path("TEST_FOO_LOG.log").unlink()
assert not Path("TEST_FOO_LOG.log").exists()
result = runner.invoke(fre.fre, args=["-vv", "-l", "TEST_FOO_LOG.log", "cmor", "--help"])
assert result.exit_code == 0
assert Path("TEST_FOO_LOG.log").exists()
log_text_line_1='[ INFO: fre.py: fre] fre_file_handler added to base_fre_logger\n' # pylint: disable=line-too-long
log_text_line_2='[ DEBUG: fre.py: fre] click entry-point function call done.\n' # pylint: disable=line-too-long
with open( "TEST_FOO_LOG.log", 'r', encoding='utf-8') as log_text:
line_list=log_text.readlines()
assert log_text_line_1 in line_list[0]
assert log_text_line_2 in line_list[1]
Path("TEST_FOO_LOG.log").unlink()
def test_cli_fre_cmor_help_and_infolog():
''' fre -v -l TEST_FOO_LOG.log cmor --help '''
if Path("TEST_FOO_LOG.log").exists():
Path("TEST_FOO_LOG.log").unlink()
assert not Path("TEST_FOO_LOG.log").exists()
result = runner.invoke(fre.fre, args=["-v", "-l", "TEST_FOO_LOG.log", "cmor", "--help"])
assert result.exit_code == 0
assert Path("TEST_FOO_LOG.log").exists()
log_text_line_1='[ INFO: fre.py: fre] fre_file_handler added to base_fre_logger\n' # pylint: disable=line-too-long
with open( "TEST_FOO_LOG.log", 'r', encoding='utf-8') as log_text:
line_list=log_text.readlines()
assert log_text_line_1 in line_list[0]
Path("TEST_FOO_LOG.log").unlink()
def test_cli_fre_cmor_help_and_quietlog():
''' fre -q -l TEST_FOO_LOG.log cmor --help '''
if Path("TEST_FOO_LOG.log").exists():
Path("TEST_FOO_LOG.log").unlink()
assert not Path("TEST_FOO_LOG.log").exists()
result = runner.invoke(fre.fre, args=["-q", "-l", "TEST_FOO_LOG.log", "cmor", "--help"])
assert result.exit_code == 0
assert Path("TEST_FOO_LOG.log").exists()
with open( "TEST_FOO_LOG.log", 'r', encoding='utf-8') as log_text:
line_list=log_text.readlines()
assert line_list == []
Path("TEST_FOO_LOG.log").unlink()
def test_cli_fre_cmor_opt_dne():
''' fre cmor optionDNE '''
result = runner.invoke(fre.fre, args=["cmor", "optionDNE"])
assert result.exit_code == 2
# fre cmor yaml
def test_cli_fre_cmor_yaml():
''' fre cmor yaml '''
result = runner.invoke(fre.fre, args=["cmor", "yaml"])
assert result.exit_code == 2
def test_cli_fre_cmor_yaml_help():
''' fre cmor yaml --help '''
result = runner.invoke(fre.fre, args=["cmor", "yaml", "--help"])
assert result.exit_code == 0
def test_cli_fre_cmor_yaml_opt_dne():
''' fre cmor yaml optionDNE '''
result = runner.invoke(fre.fre, args=["cmor", "yaml", "optionDNE"])
assert result.exit_code == 2
TEST_AM5_YAML_PATH="fre/yamltools/tests/AM5_example/am5.yaml"
TEST_CMOR_YAML_PATH="fre/yamltools/tests/AM5_example/cmor_yamls/cmor.am5.yaml"
def test_cli_fre_cmor_yaml_case1():
''' fre cmor yaml --dry_run -y TEST_AM5_YAML_PATH ... --output FOO_cmor.yaml '''
# only pp_dir is needed by cmor_yamler; history/analysis dirs came from settings.yaml
# which is now deprecated for the cmor path
Path( os.path.expandvars(
'fre/tests/test_files/ascii_files/mock_archive/$USER/am5/am5f7b12r1/c96L65_am5f7b12r1_amip/' + \
'ncrc5.intel-prod-openmp/pp'
) ).mkdir(parents=True, exist_ok=True)
if Path('FOO_cmor.yaml').exists():
Path('FOO_cmor.yaml').unlink()
result = runner.invoke(fre.fre, args=["-v", "-v", "cmor", "yaml", "--dry_run",
"-y", TEST_AM5_YAML_PATH,
"-e", "c96L65_am5f7b12r1_amip",
"-p", "ncrc5.intel",
"-t", "prod-openmp",
"--output", "FOO_cmor.yaml" ])
assert all ( [ Path(TEST_AM5_YAML_PATH).exists(), # input, unparsed, model-yaml file
Path(TEST_CMOR_YAML_PATH).exists(), # input, unparsed, tool-yaml file
Path('FOO_cmor.yaml').exists(), #output, merged, parsed, model+tool yaml-file
result.exit_code == 0 ] )
# fre cmor run
def test_cli_fre_cmor_run():
''' fre cmor run '''
result = runner.invoke(fre.fre, args=["cmor", "run"])
assert result.exit_code == 2
def test_cli_fre_cmor_run_help():
''' fre cmor run --help '''
result = runner.invoke(fre.fre, args=["cmor", "run", "--help"])
assert result.exit_code == 0
def test_cli_fre_cmor_run_opt_dne():
''' fre cmor run optionDNE '''
result = runner.invoke(fre.fre, args=["cmor", "run", "optionDNE"])
assert result.exit_code == 2
def test_cli_fre_cmor_run_case1():
''' fre cmor run, test-use case '''
# explicit inputs to tool
indir = f'{ROOTDIR}/ocean_sos_var_file/'
varlist = f'{ROOTDIR}/varlist'
table_config = f'{ROOTDIR}/cmip6-cmor-tables/Tables/CMIP6_Omon.json'
exp_config = f'{ROOTDIR}/CMOR_input_example.json'
outdir = f'{ROOTDIR}/outdir'
grid_label = 'gr'
grid_desc = 'FOO_BAR_PLACEHOLD'
nom_res = '10000 km'
calendar='julian'
# determined by cmor_run_subtool
cmor_creates_dir = \
f'CMIP6/CMIP6/ISMIP6/PCMDI/PCMDI-test-1-0/piControl-withism/r3i1p1f1/Omon/sos/{grid_label}'
full_outputdir = \
f"{outdir}/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile = \
f"{full_outputdir}/sos_Omon_PCMDI-test-1-0_piControl-withism_r3i1p1f1_{grid_label}_199301-199302.nc"
# FYI/unneeded, this is mostly for reference
filename = 'reduced_ocean_monthly_1x1deg.199301-199302.sos.nc'
full_inputfile=f"{indir}/{filename}"
# clean up, lest we fool ourselves
if Path(full_outputfile).exists():
Path(full_outputfile).unlink()
result = runner.invoke(fre.fre, args = [ "-v", "-v",
"cmor", "run", "--run_one",
"--indir", indir,
"--varlist", varlist,
"--table_config", table_config,
"--exp_config", exp_config,
"--outdir", outdir,
"--calendar", calendar,
"--grid_label", grid_label,
"--grid_desc", grid_desc,
"--nom_res", nom_res ] )
assert all ( [ result.exit_code == 0,
Path(full_outputfile).exists(),
Path(full_inputfile).exists() ] )
def test_cli_fre_cmor_run_case2():
''' fre cmor run, test-use case '''
# explicit inputs to tool
indir = f'{ROOTDIR}/ocean_sos_var_file'
varlist = f'{ROOTDIR}/varlist_local_target_vars_differ'
table_config = f'{ROOTDIR}/cmip6-cmor-tables/Tables/CMIP6_Omon.json'
exp_config = f'{ROOTDIR}/CMOR_input_example.json'
outdir = f'{ROOTDIR}/outdir'
grid_label = 'gr'
grid_desc = 'FOO_BAR_PLACEHOLD'
nom_res = '10000 km'
calendar='julian'
# determined by cmor_run_subtool
cmor_creates_dir = \
f'CMIP6/CMIP6/ISMIP6/PCMDI/PCMDI-test-1-0/piControl-withism/r3i1p1f1/Omon/sos/{grid_label}'
full_outputdir = \
f"{outdir}/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile = \
f"{full_outputdir}/sos_Omon_PCMDI-test-1-0_piControl-withism_r3i1p1f1_{grid_label}_199301-199302.nc"
# FYI/unneeded, this is mostly for reference
filename = 'reduced_ocean_monthly_1x1deg.199301-199302.sosV2.nc'
full_inputfile=f"{indir}/{filename}"
# clean up, lest we fool ourselves
if Path(full_outputfile).exists():
Path(full_outputfile).unlink()
result = runner.invoke(fre.fre, args = ["-v", "-v",
"cmor", "run", "--run_one",
"--indir", indir,
"--varlist", varlist,
"--table_config", table_config,
"--exp_config", exp_config,
"--outdir", outdir,
"--calendar", calendar,
"--grid_label", grid_label,
"--grid_desc", grid_desc,
"--nom_res", nom_res ] )
assert all ( [ result.exit_code == 0,
Path(full_outputfile).exists(),
Path(full_inputfile).exists() ] )
# fre cmor find
def test_cli_fre_cmor_find():
''' fre cmor find '''
result = runner.invoke(fre.fre, args=["cmor", "find"])
assert result.exit_code == 2
def test_cli_fre_cmor_find_help():
''' fre cmor find --help '''
result = runner.invoke(fre.fre, args=["cmor", "find", "--help"])
assert result.exit_code == 0
def test_cli_fre_cmor_find_opt_dne():
''' fre cmor find optionDNE '''
result = runner.invoke(fre.fre, args=["cmor", "find", "optionDNE"])
assert result.exit_code == 2
def test_cli_fre_cmor_find_cmip6_case1():
''' fre cmor find, test-use case searching for variables in cmip6 tables '''
result = runner.invoke(fre.fre, args=["-v", "cmor", "find",
"--varlist", "fre/tests/test_files/varlist",
"--table_config_dir", "fre/tests/test_files/cmip6-cmor-tables/Tables"] )
assert result.exit_code == 0
def test_cli_fre_cmor_find_cmip6_case2():
''' fre cmor find, test-use case searching for variables in cmip6 tables '''
result = runner.invoke(fre.fre, args=["-v", "cmor", "find",
"--opt_var_name", "sos",
"--table_config_dir", "fre/tests/test_files/cmip6-cmor-tables/Tables"] )
assert result.exit_code == 0
def test_cli_fre_cmor_run_cmip7_case1():
''' fre cmor run, test-use case for cmip7 '''
# explicit inputs to tool
indir = f'{ROOTDIR}/ocean_sos_var_file/'
varlist = f'{ROOTDIR}/varlist'
table_config = f'{ROOTDIR}/cmip7-cmor-tables/tables/CMIP7_ocean.json'
exp_config = f'{ROOTDIR}/CMOR_CMIP7_input_example.json'
outdir = f'{ROOTDIR}/outdir'
grid_label = 'g99'
grid_desc = 'FOO_BAR_PLACEHOLD'
nom_res = '10000 km'
calendar='julian'
# determined by cmor_run_subtool
cmor_creates_dir = \
f'CMIP/CanESM6-MR/esm-piControl/r3i1p1f3/sos/tavg-u-hxy-sea/{grid_label}'
full_outputdir = \
f"{outdir}/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile = f"{full_outputdir}/" + \
f"sos_tavg-u-hxy-sea_mon_glb_{grid_label}_CanESM6-MR_esm-piControl_variant_idtime_range_199301-199302.nc"
# FYI/unneeded, this is mostly for reference
filename = 'reduced_ocean_monthly_1x1deg.199301-199302.sos.nc'
full_inputfile=f"{indir}/{filename}"
# clean up, lest we fool ourselves
if Path(full_outputfile).exists():
Path(full_outputfile).unlink()
result = runner.invoke(fre.fre, args = [ "-v", "-v",
"cmor", "run", "--run_one",
"--indir", indir,
"--varlist", varlist,
"--table_config", table_config,
"--exp_config", exp_config,
"--outdir", outdir,
"--calendar", calendar,
"--grid_label", grid_label,
"--grid_desc", grid_desc,
"--nom_res", nom_res ] )
assert all ( [ result.exit_code == 0,
Path(full_outputfile).exists(),
Path(full_inputfile).exists() ] )
def test_cli_fre_cmor_run_cmip7_case2():
''' fre cmor run, test-use case for cmip7 '''
# explicit inputs to tool
indir = f'{ROOTDIR}/ocean_sos_var_file/'
varlist = f'{ROOTDIR}/varlist_local_target_vars_differ'
table_config = f'{ROOTDIR}/cmip7-cmor-tables/tables/CMIP7_ocean.json'
exp_config = f'{ROOTDIR}/CMOR_CMIP7_input_example.json'
outdir = f'{ROOTDIR}/outdir'
grid_label = 'g99'
grid_desc = 'FOO_BAR_PLACEHOLD'
nom_res = '10000 km'
calendar='julian'
# determined by cmor_run_subtool
cmor_creates_dir = \
f'CMIP/CanESM6-MR/esm-piControl/r3i1p1f3/sos/tavg-u-hxy-sea/{grid_label}'
full_outputdir = \
f"{outdir}/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile = f"{full_outputdir}/" + \
f"sos_tavg-u-hxy-sea_mon_glb_{grid_label}_CanESM6-MR_esm-piControl_variant_idtime_range_199301-199302.nc"
# FYI/unneeded, this is mostly for reference
filename = 'reduced_ocean_monthly_1x1deg.199301-199302.sosV2.nc'
full_inputfile=f"{indir}/{filename}"
# clean up, lest we fool ourselves
if Path(full_outputfile).exists():
Path(full_outputfile).unlink()
result = runner.invoke(fre.fre, args = [ "-v", "-v",
"cmor", "run", "--run_one",
"--indir", indir,
"--varlist", varlist,
"--table_config", table_config,
"--exp_config", exp_config,
"--outdir", outdir,
"--calendar", calendar,
"--grid_label", grid_label,
"--grid_desc", grid_desc,
"--nom_res", nom_res ] )
assert all ( [ result.exit_code == 0,
Path(full_outputfile).exists(),
Path(full_inputfile).exists() ] )
# fre cmor config
def test_cli_fre_cmor_config():
''' fre cmor config '''
result = runner.invoke(fre.fre, args=["cmor", "config"])
assert result.exit_code == 2
def test_cli_fre_cmor_config_help():
''' fre cmor config --help '''
result = runner.invoke(fre.fre, args=["cmor", "config", "--help"])
assert result.exit_code == 0
def test_cli_fre_cmor_config_opt_dne():
''' fre cmor config optionDNE '''
result = runner.invoke(fre.fre, args=["cmor", "config", "optionDNE"])
assert result.exit_code == 2
def test_cli_fre_cmor_config_case1():
'''
fre cmor config -- generate a CMOR YAML config from a mock pp directory tree.
Uses the ocean_sos_var_file test data with a mock pp layout.
'''
# set up a mock pp directory tree that the writer can scan
mock_pp_dir = Path(f'{ROOTDIR}/mock_pp_writer')
comp_ts_dir = mock_pp_dir / 'ocean' / 'ts' / 'monthly' / '5yr'
comp_ts_dir.mkdir(parents=True, exist_ok=True)
# make an ice component dir with no chunk-dir to skip accordingly
(mock_pp_dir / 'ice' / 'ts' / 'monthly' ).mkdir(parents=True, exist_ok=True)
# make a land component dir with no ts dir to skip accordingly
(mock_pp_dir / 'land' / 'av').mkdir(parents=True, exist_ok=True)
# make an empty atmos component dir with no netcdf files to make sure we skip a dir with no nc files
(mock_pp_dir / 'atmos' / 'ts' / 'monthly' / '5yr').mkdir(parents=True, exist_ok=True)
# create random file that's not a directory in the pp_dir that we should skip over gracefully
(mock_pp_dir / 'foo.json').touch()
# put an av directory in to make sure we're nmot targeting that at the moment
(mock_pp_dir / 'ocean' / 'av').mkdir(parents=True, exist_ok=True)
# put an annual directory in to make sure we're not targeting that at the moment
(mock_pp_dir / 'ocean' / 'ts' / 'annual').mkdir(parents=True, exist_ok=True)
# symlink the test nc file into the mock tree
src_nc = Path(f'{ROOTDIR}/ocean_sos_var_file/reduced_ocean_monthly_1x1deg.199301-199302.sos.nc')
dst_nc = comp_ts_dir / src_nc.name
if dst_nc.exists() or dst_nc.is_symlink():
dst_nc.unlink()
dst_nc.symlink_to(src_nc.resolve())
varlist_out_dir = Path(f'{ROOTDIR}/mock_writer_varlists')
varlist_out_dir.mkdir(exist_ok=True)
# create an empty variable list of one we want to create. it should be remade.
(varlist_out_dir / 'CMIP6_CMIP6_Omon_ocean.list').touch()
assert (varlist_out_dir / 'CMIP6_CMIP6_Omon_ocean.list').exists(), 'pre-existing variable list failed to be created for tests'
output_yaml = Path(f'{ROOTDIR}/mock_writer_output.yaml')
output_data_dir = Path(f'{ROOTDIR}/mock_writer_outdir')
# clean up previous runs
for p in [output_yaml]:
if p.exists():
p.unlink()
# recreate the yaml to make sure it's recreated
output_yaml.touch()
result = runner.invoke(fre.fre, args=[
"-v", "-v",
"cmor", "config",
"--pp_dir", str(mock_pp_dir),
"--mip_tables_dir", f'{ROOTDIR}/cmip6-cmor-tables/Tables',
"--mip_era", "cmip6",
"--exp_config", f'{ROOTDIR}/CMOR_input_example.json',
"--output_yaml", str(output_yaml),
"--output_dir", str(output_data_dir),
"--varlist_dir", str(varlist_out_dir),
"--freq", "monthly",
"--chunk", "5yr",
"--grid", "gn",
"--overwrite"
])
assert result.exit_code == 0, f'config failed: {result.output}'
assert output_yaml.exists(), 'output YAML was not created'
assert (varlist_out_dir / 'CMIP6_CMIP6_Omon_ocean.list').exists(), 'CMIP6_CMIP6_Omon_ocean.list was not created for some reason'
# basic sanity: the written file should contain "cmor:" and "table_targets:"
yaml_text = output_yaml.read_text(encoding='utf-8')
assert 'cmor:' in yaml_text
assert 'table_targets:' in yaml_text
# clean up
if dst_nc.is_symlink():
dst_nc.unlink()
shutil.rmtree(mock_pp_dir, ignore_errors=True)
shutil.rmtree(varlist_out_dir, ignore_errors=True)
if output_yaml.exists():
output_yaml.unlink()
# fre cmor varlist
def test_cli_fre_cmor_varlist():
''' fre cmor varlist '''
result = runner.invoke(fre.fre, args=["cmor", "varlist"])
assert result.exit_code == 2
def test_cli_fre_cmor_varlist_help():
''' fre cmor varlist --help '''
result = runner.invoke(fre.fre, args=["cmor", "varlist", "--help"])
assert result.exit_code == 0
def test_cli_fre_cmor_varlist_opt_dne():
''' fre cmor varlist optionDNE '''
result = runner.invoke(fre.fre, args=["cmor", "varlist", "optionDNE"])
assert result.exit_code == 2
def test_cli_fre_cmor_varlist_no_table_filter():
'''
fre cmor varlist — no MIP table filter.
creates a variable list from the ocean_sos_var_file test data without a MIP table,
so both sos and sosV2 should appear.
'''
indir = f'{ROOTDIR}/ocean_sos_var_file'
output_varlist = Path(f'{ROOTDIR}/test_varlist_no_filter.json')
if output_varlist.exists():
output_varlist.unlink()
result = runner.invoke(fre.fre, args=[
"-v", "-v",
"cmor", "varlist",
"--dir_targ", indir,
"--output_variable_list", str(output_varlist)
])
assert result.exit_code == 0, f'varlist failed: {result.output}'
assert output_varlist.exists(), 'output variable list was not created'
with open(output_varlist, 'r', encoding='utf-8') as f:
var_list = json.load(f)
assert 'sos' in var_list
assert 'sosV2' in var_list
assert len(var_list) == 2
output_varlist.unlink()
def test_cli_fre_cmor_varlist_cmip6_table_filter():
'''
fre cmor varlist — with CMIP6 Omon MIP table filter.
only sos should survive; sosV2 is not in the CMIP6 Omon table.
'''
indir = f'{ROOTDIR}/ocean_sos_var_file'
mip_table = f'{ROOTDIR}/cmip6-cmor-tables/Tables/CMIP6_Omon.json'
output_varlist = Path(f'{ROOTDIR}/test_varlist_cmip6_filter.json')
if output_varlist.exists():
output_varlist.unlink()
result = runner.invoke(fre.fre, args=[
"-v", "-v",
"cmor", "varlist",
"--dir_targ", indir,
"--output_variable_list", str(output_varlist),
"--mip_table", mip_table
])
assert result.exit_code == 0, f'varlist failed: {result.output}'
assert output_varlist.exists(), 'output variable list was not created'
with open(output_varlist, 'r', encoding='utf-8') as f:
var_list = json.load(f)
assert 'sos' in var_list, 'sos should be in the CMIP6-filtered list'
assert 'sosV2' not in var_list, 'sosV2 should NOT be in the CMIP6-filtered list'
output_varlist.unlink()
def test_cli_fre_cmor_varlist_cmip7_table_filter():
'''
fre cmor varlist — with CMIP7 ocean MIP table filter.
sos should survive (sos_tavg-u-hxy-sea splits to sos); sosV2 should not.
'''
indir = f'{ROOTDIR}/ocean_sos_var_file'
mip_table = f'{ROOTDIR}/cmip7-cmor-tables/tables/CMIP7_ocean.json'
output_varlist = Path(f'{ROOTDIR}/test_varlist_cmip7_filter.json')
if output_varlist.exists():
output_varlist.unlink()
result = runner.invoke(fre.fre, args=[
"-v", "-v",
"cmor", "varlist",
"--dir_targ", indir,
"--output_variable_list", str(output_varlist),
"--mip_table", mip_table
])
assert result.exit_code == 0, f'varlist failed: {result.output}'
assert output_varlist.exists(), 'output variable list was not created'
with open(output_varlist, 'r', encoding='utf-8') as f:
var_list = json.load(f)
assert 'sos' in var_list, 'sos should be in the CMIP7-filtered list'
assert 'sosV2' not in var_list, 'sosV2 should NOT be in the CMIP7-filtered list'
output_varlist.unlink()