|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Compute a set of contour plots using diags (diags.py). |
| 4 | +# First argument: --datadir=<data location> - with subdirectories cam_output and obs_atmos and baseline. |
| 5 | +# These have sample model output, observation data, and "baseline" output which we should match. |
| 6 | +# However, the graphical output (png files) may not match in manner suitable for automated testing. |
| 7 | +# So the return value only depends on the numerical values in the .nc files. |
| 8 | +# Second argument: '--keep=True' to keep (don't delete) output files* |
| 9 | +# No attempt is made to clean up the diagnostics' cache files, which are generally in /tmp. |
| 10 | + |
| 11 | +print 'Test 2: Diagnostic contour plots ... ', |
| 12 | + |
| 13 | +from metrics.common.utilities import * |
| 14 | +from pprint import pprint |
| 15 | +import sys, os, shutil, tempfile, subprocess |
| 16 | +import cdms2, numpy |
| 17 | +pth = os.path.join(os.path.dirname(__file__),"..") |
| 18 | +sys.path.append(pth) |
| 19 | +import checkimage |
| 20 | +import argparse |
| 21 | + |
| 22 | +p = argparse.ArgumentParser(description="Basic gm testing code for vcs") |
| 23 | +p.add_argument("--datadir", dest="datadir", help="root directory for model and obs data") |
| 24 | +p.add_argument("--baseline", dest="baseline", help="directory with baseline files for comparing results") |
| 25 | +p.add_argument("--keep", dest="keep", help="Iff True, will keep computed png and nc files") |
| 26 | +args = p.parse_args(sys.argv[1:]) |
| 27 | + |
| 28 | +def closeness( varname, filename, pathout, baselinepath, rtol, atol ): |
| 29 | + fname = os.path.join( pathout, filename ) |
| 30 | + baselinefname = os.path.join( baselinepath, filename ) |
| 31 | + f = cdms2.open( fname ) |
| 32 | + g = cdms2.open( baselinefname ) |
| 33 | + fvar = f(varname) |
| 34 | + gvar = g(varname) |
| 35 | + close = numpy.ma.allclose( fvar, gvar, rtol=rtol, atol=atol ) |
| 36 | + if close: |
| 37 | + print "fvar and gvar are close for", varname |
| 38 | + else: |
| 39 | + print "fvar and gvar differ for", varname |
| 40 | + print "max difference", (fvar-gvar).max() |
| 41 | + print "min difference", (fvar-gvar).min() |
| 42 | + f.close() |
| 43 | + g.close() |
| 44 | + return close |
| 45 | + |
| 46 | +datadir = args.datadir |
| 47 | +path1 = os.path.join( datadir, 'cam_output' ) |
| 48 | +path2 = os.path.join( datadir, 'obs_atmos' ) |
| 49 | +baselinepath = args.baseline |
| 50 | +pathout = tempfile.mkdtemp() |
| 51 | + |
| 52 | +diagstr = "diags --outputdir '%s' --model path=%s,climos=no --obs path=%s,filter=\"f_contains('NCEP')\",climos=yes --varopts 850 --package AMWG --set 5 --var T --seasons ANN" % (pathout,path1,path2) |
| 53 | +# nonstandard, suitable for testing: |
| 54 | +#diagstr = "diags --outputdir '%s' --model path=%s,climos=yes --obs path=%s,filter=\"f_contains('NCEP')\",climos=yes --varopts 850 --package AMWG --set 5 --var T --seasons ANN" % (pathout,os.path.join(datadir,'cam_output_climo'),path2) |
| 55 | +proc = subprocess.Popen([diagstr],shell=True) |
| 56 | +proc_status = proc.wait() |
| 57 | +if proc_status!=0: |
| 58 | + raise DiagError("diags run failed") |
| 59 | + |
| 60 | +# Test of graphics (png) file match: |
| 61 | +# This just looks at combined plot, aka summary plot, which is a compound of three plots. |
| 62 | +filename = 'set5_Global_ANN_T-combined.png' |
| 63 | +fname = os.path.join( pathout, filename ) |
| 64 | +baselinefname = os.path.join( baselinepath, filename ) |
| 65 | +threshold = 1.0e6 |
| 66 | +graphics_result = checkimage.check_result_image( fname, baselinefname, threshold ) |
| 67 | +print "Graphics file",fname,"match difference:",graphics_result |
| 68 | + |
| 69 | +# Test of NetCDF data (nc) file match: |
| 70 | +rtol = 1.0e-3 |
| 71 | +atol = 1.0e-2 # suitable for temperatures |
| 72 | +filename = 'T_ANN_at_850_mbar_(1)_None.nc' |
| 73 | +varname = 'dv_T_lp_ANN_ft0_None_None' |
| 74 | +close1 = closeness( varname, filename, pathout, baselinepath, rtol, atol ) |
| 75 | +filename = 'T_ANN_at_850_mbar_(2)_None.nc' |
| 76 | +varname = 'dv_T_lp_ANN_ft1_None_None' |
| 77 | +close2 = closeness( varname, filename, pathout, baselinepath, rtol, atol ) |
| 78 | +filename = 'T_ANN_at_850_mbar_(1)-(2)_None,_None.nc' |
| 79 | +varname = 'dv_T_lp_ANN_ft0_None_None_dv_T_lp_ANN_ft1_None_None' |
| 80 | +close12 = closeness( varname, filename, pathout, baselinepath, rtol, atol ) |
| 81 | +close = close1 and close2 and close12 |
| 82 | + |
| 83 | +if args.keep is True: |
| 84 | + print "saving output in",pathout |
| 85 | +else: |
| 86 | + shutil.rmtree(pathout) |
| 87 | + |
| 88 | +# The exit value depends on numerical values in the NetCDF file, not on the plot. |
| 89 | +sys.exit( close ) |
0 commit comments