pynemo_unit_test.py 4.39 KB
Newer Older
thopri's avatar
thopri committed
1 2 3 4 5 6 7 8 9 10
# -*- coding: utf-8 -*-
"""
Set of test functions to test PyNEMO functionality.

"""
from subprocess import Popen, PIPE
from netCDF4 import Dataset
import numpy as np
import glob
import os
11
from pynemo.unit_tests import UT_config as config
thopri's avatar
thopri committed
12

13
# generate test data by import test gen script and executing main function
thopri's avatar
thopri committed
14
# TODO: Maybe simplify this, as this import imports other scripts and is abit clunky.
15
from pynemo.unit_tests import test_gen as tg
16
gen_data = tg._main()
thopri's avatar
thopri committed
17
# if a non zero is return than the grid and data generation has failed.
18
if gen_data != 0:
thopri's avatar
thopri committed
19
    raise Exception('DONT PANIC: Input grid and boundary data generation failed')
thopri's avatar
thopri committed
20

21
# run PyNEMO with test data
thopri's avatar
thopri committed
22
# generate list of namelist.bdy files to run
23
namelist_files = glob.glob(config.unit_dir+'namelist*')
thopri's avatar
thopri committed
24
for n in namelist_files:
thopri's avatar
thopri committed
25
    # run each of the namelist files
thopri's avatar
thopri committed
26
    stdout, stderr = Popen(['pynemo', '-s', n], stdout=PIPE, stderr=PIPE,
27
                       universal_newlines=True).communicate()
thopri's avatar
thopri committed
28 29 30 31
    # check to see if PyNEMO ran correctly, no execution time in stdout is indication of this.
    if 'Execution Time' not in stdout:
        print(stderr)
        raise Exception('DONT PANIC: Test Run '+str(n)+'  Failed')
32

thopri's avatar
thopri committed
33 34 35
# TODO: Learn about parameterising the tests so that different parameters can be checked
#       with same code. Rather than having similar test functions repeated.

36
# perform tests
thopri's avatar
thopri committed
37
def test_temp():
38
    test_files = glob.glob(config.output_dir+'*bdyT*')
39 40
    if len(test_files) == 0:
        raise Exception('DONT PANIC: no temperature test files found')
thopri's avatar
thopri committed
41 42 43 44 45 46 47 48 49
    for t in test_files:
        results = Dataset(t) # open results
        temp = results['thetao'][:]
        results.close()
        temp_ = np.ma.masked_array(temp,temp == -32767.0)
        assert abs(temp_[temp_!=0.0].mean() - 15) <= 0.001
        assert abs(temp_[temp_ != 0.0].max() - 15) <= 0.001
        assert abs(temp_[temp_ != 0.0].min() - 15) <= 0.001

thopri's avatar
thopri committed
50
def test_salinty():
51
    test_files = glob.glob(config.output_dir+'*bdyT*')
52 53
    if len(test_files) == 0:
        raise Exception('DONT PANIC: no salinity test files found')
thopri's avatar
thopri committed
54 55 56 57 58 59 60 61
    for t in test_files:
        results = Dataset(t)  # open results
        sal = results['so'][:]
        results.close()
        sal_ = np.ma.masked_array(sal,sal == -32767.0)
        assert abs(sal_[sal_!=0.0].mean() - 35) <= 0.001
        assert abs(sal_[sal_ != 0.0].max() - 35) <= 0.001
        assert abs(sal_[sal_ != 0.0].min() - 35) <= 0.001
thopri's avatar
thopri committed
62

63 64 65
# TODO: add in checking so that settings in the bdy file are checked to see if
#  U and V and SSH tests are required. e.g. ln_dyn2d is set to true.

66
def test_ssh():
67
    test_files = glob.glob(config.output_dir+'*bdyT*')
68 69 70 71 72 73 74 75 76 77 78 79
    if len(test_files) == 0:
        raise Exception('DONT PANIC: no SSH test files found')
    for t in test_files:
        results = Dataset(t)  # open results
        ssh = results['zos'][:]
        results.close()
        ssh_ = np.ma.masked_array(ssh,ssh == -32767.0)
        assert abs(ssh_[ssh_!=0.0].mean() - 1.0) <= 0.001
        assert abs(ssh_[ssh_ != 0.0].max() - 1.0) <= 0.001
        assert abs(ssh_[ssh_ != 0.0].min() - 1.0) <= 0.001

def test_U():
80
    test_files = glob.glob(config.output_dir+'*bdyU*')
81 82 83 84 85 86 87 88 89 90 91 92
    if len(test_files) == 0:
        raise Exception('DONT PANIC: no U current test files found')
    for t in test_files:
        results = Dataset(t)  # open results
        U = results['uo'][:]
        results.close()
        U_ = np.ma.masked_array(U,U == -32767.0)
        assert abs(U_[U_!=0.0].mean() - 0.5) <= 0.001
        assert abs(U_[U_ != 0.0].max() - 0.5) <= 0.001
        assert abs(U_[U_ != 0.0].min() - 0.5) <= 0.001

def test_V():
93
    test_files = glob.glob(config.output_dir+'*bdyV*')
94 95 96 97 98 99 100 101 102 103 104
    if len(test_files) == 0:
        raise Exception('DONT PANIC: no V current test files found')
    for t in test_files:
        results = Dataset(t)  # open results
        V = results['vo'][:]
        results.close()
        V_ = np.ma.masked_array(V,V == -32767.0)
        assert abs(V_[V_!=0.0].mean() - 0.5) <= 0.001
        assert abs(V_[V_ != 0.0].max() - 0.5) <= 0.001
        assert abs(V_[V_ != 0.0].min() - 0.5) <= 0.001

105
# clean up test I/O
106
def test_rm_out():
107
    files = glob.glob(config.output_dir+'*')
108 109
    for f in files:
        os.remove(f)
110
    files = glob.glob(config.output_dir+'*')
111
    assert len(files) == 0
112

thopri's avatar
thopri committed
113

114
def test_rm_in():
115
    files = glob.glob(config.output_dir+'*')
116 117
    for f in files:
        os.remove(f)
118
    files = glob.glob(config.output_dir+'*')
119
    assert len(files) == 0