~ubuntu-branches/ubuntu/wily/gsw/wily

« back to all changes in this revision

Viewing changes to gsw/test/test_tuples.py

  • Committer: Package Import Robot
  • Author(s): Alastair McKinstry
  • Date: 2012-12-17 19:12:21 UTC
  • Revision ID: package-import@ubuntu.com-20121217191221-k7d89tsyikdycbra
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
"""Unit check for standard profiles for the Gibbs Sea Water python package."""
 
4
 
 
5
import os
 
6
import sys
 
7
import unittest
 
8
import functools  # Requires python 2.5.
 
9
import numpy as np
 
10
 
 
11
import gsw
 
12
from gsw.utilities import Dict2Struc
 
13
 
 
14
 
 
15
# Read data file with check value profiles
 
16
datadir = os.path.join(os.path.dirname(gsw.utilities.__file__), 'data')
 
17
fname = 'gsw_cv_v3_0.npz'
 
18
cv = Dict2Struc(np.load(os.path.join(datadir, fname)))
 
19
cf = Dict2Struc(np.load(os.path.join(datadir, 'gsw_cf_.npz')))
 
20
 
 
21
# derivatives.py
 
22
#CT_first_derivatives=('SA', 'pt'),  # NOTE: TI, BUG
 
23
#CT_second_derivatives=('SA', 'pt'),  # NOTE: TI FIXME BUG
 
24
#enthalpy_first_derivatives=('SA', 'CT', 'p'), FIXME name match
 
25
#enthalpy_second_derivatives=('SA', 'CT', 'p'), FIXME name match
 
26
#entropy_first_derivatives=('SA', 'CT'),  #NOTE: TI FIXME name match
 
27
#entropy_second_derivatives=('SA', 'pt'),  #NOTE: TI FIXME name match
 
28
#pt_first_derivatives':,  #NOTE: TI FIXME name match
 
29
#pt_second_derivatives= #NOTE: TI FIXME name match
 
30
 
 
31
# Make aliases for some values to be used as arguments
 
32
cv.entropy_chck_cast = cv.entropy_from_CT
 
33
cv.Abs_Pressure_from_p_chck_cast = cv.Abs_Pressure_from_p
 
34
cv.depth_chck_cast = cv.depth_from_z
 
35
cv.C_chck_cast = cv.C_from_SP
 
36
cv.pt_chck_cast = cv.pt_from_t
 
37
cv.z_chck_cast = cv.z_from_p
 
38
cv.SR_chck_cast = cv.SR_from_SP
 
39
cv.pr_chck_cast = cv.pr
 
40
cv.p_shallow_chck_cast = cv.p_chck_cast_shallow
 
41
cv.p_deep_chck_cast = cv.p_chck_cast_deep
 
42
cv.rho_chck_cast = cv.rho_CT_exact
 
43
cv.R_cf_chck_cast = cf.R
 
44
cv.rho_cf_chck_cast = cf.rho
 
45
 
 
46
def generic_test(self, func=None, argnames=None):
 
47
    """Generic test function, to be specialized by functools.partial"""
 
48
    # Transform argument names to name convention in cv dataset
 
49
    args = [getattr(cv, a + '_chck_cast') for a in argnames]
 
50
    # Perform the function call
 
51
    out = getattr(gsw, func)(*args)
 
52
    # FIXME: Testing just the first output!
 
53
    # TODO: Create the tuples and compare all together.
 
54
    # Check that the maximal error is less than the given tolerance
 
55
    if isinstance(out, tuple):
 
56
        out = out[0]
 
57
    maxdiff = np.nanmax(abs(out - getattr(cv, func)))
 
58
    try:
 
59
        self.assertTrue(maxdiff < getattr(cv, func + '_ca'))
 
60
    except AssertionError, e:
 
61
        raise AssertionError("Error in %s %s" % (func, e.args))
 
62
 
 
63
 
 
64
# Dictionary of functions with corresponding test methods
 
65
function_test = {}
 
66
for f in function_arguments:
 
67
    function_test[f] = functools.partial(generic_test,
 
68
                      func=f, argnames=function_arguments[f])
 
69
 
 
70
 
 
71
# Auto-generated TestCase
 
72
class Test_profiles(unittest.TestCase):
 
73
 
 
74
    for f in function_test:
 
75
        method_def = ("test_" + f +
 
76
            " = lambda self: function_test['" + f + "'](self)")
 
77
        #print method_def
 
78
        exec(method_def)
 
79
 
 
80
 
 
81
if __name__ == '__main__':
 
82
    # A more verbose output
 
83
    suite = unittest.TestLoader().loadTestsFromTestCase(Test_profiles)
 
84
    a = unittest.TextTestRunner(verbosity=2).run(suite)
 
85
    if a.errors or a.failures:
 
86
        sys.exit(256)
 
87
    #unittest.main()
 
88
 
 
89
[gsw_cf.CT_SA, gsw_cf.CT_pt] = gsw_CT_first_derivatives(gsw_cv.SA_chck_cast,gsw_cf.pt);
 
90
[gsw_cf.ICT_first_deriv] = find(abs(gsw_cv.CT_SA - gsw_cf.CT_SA) >= gsw_cv.CT_SA_ca | ...
 
91
    (gsw_cv.CT_pt - gsw_cf.CT_pt) >= gsw_cv.CT_pt_ca);
 
92
if ~isempty(gsw_cf.ICT_first_deriv)
 
93
    fprintf(2,'gsw_CT_first_derivatives:   Failed\n');
 
94
    gsw_cf.gsw_chks = 0;
 
95
end
 
 
b'\\ No newline at end of file'