Skip to content

Commit

Permalink
check for input/output files appropriately, things look like theyre p…
Browse files Browse the repository at this point in the history
…assing nicely now

these tests wont pass on the pipeline unforutnately... dependence on cmip6-data-tables
... dependence on a netcdf file that has megabytes assoc with it
... dont want to blow up repo size... can we do an ncgen + cdl for the metadata?
  • Loading branch information
ilaflott committed Sep 13, 2024
1 parent eed40f6 commit a2b447c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion fre/cmor/cmor_mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def gfdl_to_pcmdi_var( proj_table_vars, var_lst, dir2cmor, gfdl_var, time_arr,

# copy ps also, if it's there
nc_ps_file_work = ''
nc_ps_file = nc_fls[i].replace(f'.{gfdl_var}.nc', '.ps.nc')
nc_ps_file = nc_fls[i].replace(f'.{gfdl_var}.nc', '.ps.nc')
if os.path.exists(nc_ps_file):
print(f"(gfdl_to_pcmdi_var) nc_ps_file = {nc_ps_file}")
nc_ps_file_work = nc_file_work.replace(f'.{gfdl_var}.nc', '.ps.nc')
Expand Down
47 changes: 30 additions & 17 deletions fre/cmor/tests/test_cmor_run_subtool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
''' tests for fre.cmor._cmor_run_subtool '''
import subprocess
import datetime
import pathlib
from pathlib import Path
from datetime import date

import fre
Expand All @@ -17,9 +19,28 @@
exp_config = f'{rootdir}/CMOR_input_example.json'
outdir = f'{rootdir}/outdir'

# determined by cmor_run_subtool
YYYYMMDD=date.today().strftime('%Y%m%d')
cmor_creates_dir='CMIP6/CMIP6/ISMIP6/PCMDI/PCMDI-test-1-0/piControl-withism/r3i1p1f1/Omon/sos/gn'
# why does this have "fre" at the end of it?
full_outputdir = \
f"{outdir}fre/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile=f"{full_outputdir}/sos_Omon_PCMDI-test-1-0_piControl-withism_r3i1p1f1_gn_199307-199807.nc"

# FYI
filename = 'ocean_monthly_1x1deg.199301-199712.sos.nc' # unneeded, this is mostly for reference
full_inputfile=f"{indir}/{filename}"




def test_fre_cmor_run(capfd):
''' fre cmor run, test-use case '''

# clean up, lest we fool outselves
if Path(full_outputfile).exists():
Path(full_outputfile).unlink()

fre.cmor._cmor_run_subtool(
indir = indir,
varlist = varlist,
Expand All @@ -29,31 +50,23 @@ def test_fre_cmor_run(capfd):
)
# success condition tricky... tool doesnt return anything really... ?
# TODO think about returns and success conditions
assert True
assert all( [ Path(full_outputfile).exists(),
Path(full_inputfile).exists() ] )
out, err = capfd.readouterr()

# determined by cmor_run_subtool
YYYYMMDD=date.today().strftime('%Y%m%d')
cmor_creates_dir='CMIP6/CMIP6/ISMIP6/PCMDI/PCMDI-test-1-0/piControl-withism/r3i1p1f1/Omon/sos/gn'
full_outputdir = \
f"{outdir}/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile=f"{full_outputdir}/sos_Omon_PCMDI-test-1-0_piControl-withism_r3i1p1f1_gn_199307-199807.nc"

# FYI
filename = 'ocean_monthly_1x1deg.199301-199712.sos.nc' # unneeded, this is mostly for reference
full_inputfile=f"{indir}/{filename}"

def test_fre_cmor_run_output_compare(capfd):
''' I/O comparison of prev test-use case '''
print(f'full_outputfile={full_outputfile}')
print(f'full_inputfile={full_inputfile}')

nccmp_cmd= [ "nccmp", "-f", "-m", "-g", "-d",
f"{full_inputfile}",
f"{full_outputfile}" ]
print(f"via subprocess, running {' '.join(nccmp_cmd)}")
result = subprocess.run( nccmp_cmd
,
shell=True
result = subprocess.run( ' '.join(nccmp_cmd),
shell=True,
check=False
)
#subprocess.run(["rm", "-rf", f"{outdir}/CMIP6/CMIP6/"])
#assert result.returncode == 1
assert False
assert result.returncode == 1
out, err = capfd.readouterr()
48 changes: 29 additions & 19 deletions fre/tests/test_fre_cmor_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env python3
''' test "fre cmor" calls '''

import datetime
from datetime import date
import pathlib
from pathlib import Path
import subprocess
import netCDF4 as nc

Expand Down Expand Up @@ -42,39 +46,45 @@ def test_cli_fre_cmor_run_opt_dne():
result = runner.invoke(fre.fre, args=["cmor", "run", "optionDNE"])
assert result.exit_code == 2

##def test_cli_fre_cmor_run_case1(capfd):
def test_cli_fre_cmor_run_case1(capsys):
def test_cli_fre_cmor_run_case1(capfd):
''' fre cmor run, test-use case '''


# where are we? we're running pytest from the base directory of this repo
rootdir = 'fre/tests/test_files'

# 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'

# indir = 'fre/tests/test_files'
# varlist = 'fre/tests/test_files/varlist'
# table_config = 'fre/tests/test_files/cmip6-cmor-tables/Tables/CMIP6_Omon.json'
# exp_config = 'fre/tests/test_files/CMOR_input_example.json'
# outdir = 'fre/tests/test_files/outdir'
# determined by cmor_run_subtool
YYYYMMDD=date.today().strftime('%Y%m%d')
cmor_creates_dir = \
'CMIP6/CMIP6/ISMIP6/PCMDI/PCMDI-test-1-0/piControl-withism/r3i1p1f1/Omon/sos/gn'
# why does this have "fre" at the end of it?
full_outputdir = \
f"{outdir}fre/{cmor_creates_dir}/v{YYYYMMDD}"
full_outputfile = \
f"{full_outputdir}/sos_Omon_PCMDI-test-1-0_piControl-withism_r3i1p1f1_gn_199307-199807.nc"

# FYI
filename = 'ocean_monthly_1x1deg.199301-199712.sos.nc' # unneeded, this is mostly for reference
full_inputfile=f"{indir}/{filename}"

# clean up, lest we fool outselves
if Path(full_outputfile).exists():
Path(full_outputfile).unlink()

#subprocess.run(["mkdir", "-p", outdir+'/tmp'])
result = runner.invoke(fre.fre, args = ["cmor", "run",
"--indir", indir,
"--varlist", varlist,
"--table_config", table_config,
"--exp_config", exp_config,
"--outdir", outdir])
#assert False
#out, err = capfd.readouterr()
out, err = capsys.readouterr()
#print(out)
#print(err)

assert result.exit_code == 0

assert False

assert all ( [ result.exit_code == 0,
Path(full_outputfile).exists(),
Path(full_inputfile).exists() ] )
out, err = capfd.readouterr()

0 comments on commit a2b447c

Please sign in to comment.