<gh_stars>1-10
#!/usr/bin/env python

"""
@package ion_functions.test.pH_functions
@file ion_functions/test/pH_functions.py
@author <NAME>
@brief Unit tests for pH_functions module
"""

from nose.plugins.attrib import attr
from ion_functions.test.base_test import BaseUnitTestCase

import numpy as np
from ion_functions.data import ph_functions as ph


@attr('UNIT', group='func')
class TestpHFunctionsUnit(BaseUnitTestCase):
    def setUp(self):
        """
        Test values for the PHWATER unit tests, based on test strings in the
        DPS and available on Alfresco. Note, had to recompute output values
        using original Matlab code as those provided in DPS do not match the
        output calculated from those input strings.

        OOI (2012). Data Product Specification for pH of Seawater. Document
            Control Number 1341-00510. https://alfresco.oceanobservatories.org/
            (See: Company Home >> OOI >> Controlled >> 1000 System Level >>
            1341-00510_Data_Product_SPEC_PHWATER_OOI.pdf)

        Implemented by <NAME>, April 2013
        Corrected by <NAME>, January 2014 to allow for testing of
            vectorized code.
        """
        # reagent bag calibration factors
        self.ea434 = 17533.
        self.eb434 = 2229.
        self.ea578 = 101.
        self.eb578 = 38502.
        self.ind_slp = 0.9698
        self.ind_off = 0.2484
        self.salinity = np.array([30., 30., 30., 30., 30.,
                                  32., 32., 32., 32., 32.,
                                  35., 35., 35., 35., 35.])

        # raw data strings (already converted from ASCIIHEX) provided by the DPS
        self.raw = np.array([
            [10, 3480086699, 2530, 3288, 1435, 2484, 2228, 3289, 1435, 2490, 2234,
             3286, 1439, 2485, 2235, 3287, 1439, 2487, 2238, 3289, 1439, 2487, 2238,
             3290, 1432, 2486, 2208, 3289, 1265, 2484, 1630, 3283, 943, 2482, 797,
             3289, 663, 2485, 336, 3286, 512, 2481, 178, 3282, 451, 2479, 136, 3288,
             449, 2486, 142, 3285, 478, 2480, 173, 3290, 530, 2488, 238, 3288, 600,
             2481, 329, 3287, 675, 2483, 443, 3286, 756, 2486, 582, 3288, 836, 2480,
             732, 3288, 913, 2484, 894, 3291, 983, 2484, 1047, 3288, 1043, 2485, 1199,
             3287, 1103, 2479, 1341, 3291, 1156, 2489, 1469, 3287, 1198, 2480, 1577,
             3292, 1236, 2480, 1675, 3286, 1266, 2480, 1760, 3287, 1295, 2485, 1834,
             0, 3374, 2528],
            [10, 3480086999, 2530, 3285, 1509, 2480, 2341, 3284, 1510, 2484, 2341,
             3288, 1507, 2482, 2341, 3289, 1506, 2480, 2339, 3294, 1513, 2487, 2345,
             3285, 1498, 2477, 2324, 3286, 1276, 2478, 1885, 3285, 857, 2477, 1140,
             3287, 545, 2478, 639, 3289, 397, 2479, 423, 3282, 346, 2482, 354, 3289,
             350, 2480, 358, 3287, 387, 2482, 399, 3287, 446, 2484, 478, 3293, 522,
             2482, 581, 3289, 607, 2480, 702, 3288, 695, 2483, 842, 3289, 789, 2479,
             984, 3285, 879, 2479, 1132, 3288, 962, 2486, 1283, 3286, 1035, 2480,
             1406, 3291, 1104, 2476, 1532, 3281, 1160, 2479, 1640, 3289, 1215, 2482,
             1741, 3289, 1259, 2481, 1827, 3290, 1295, 2481, 1900, 3288, 1326, 2477,
             1964, 0, 3374, 2530],
            [10, 3480087299, 2527, 3291, 1581, 2480, 2457, 3290, 1585, 2481, 2462,
             3289, 1583, 2481, 2461, 3290, 1582, 2486, 2462, 3290, 1581, 2480, 2460,
             3286, 1579, 2477, 2449, 3286, 1331, 2478, 1979, 3285, 895, 2476, 1218,
             3289, 571, 2481, 701, 3284, 413, 2474, 472, 3290, 364, 2477, 400, 3293,
             369, 2481, 405, 3284, 404, 2477, 447, 3288, 467, 2476, 528, 3290, 541,
             2485, 632, 3290, 624, 2478, 757, 3290, 721, 2477, 898, 3287, 814, 2479,
             1049, 3288, 900, 2482, 1191, 3292, 995, 2481, 1339, 3289, 1069, 2478,
             1475, 3289, 1140, 2479, 1601, 3288, 1206, 2480, 1718, 3291, 1258, 2482,
             1815, 3291, 1307, 2483, 1907, 3291, 1344, 2482, 1984, 3289, 1381, 2480,
             2049, 0, 3373, 2526],
            [10, 3480087599, 2530, 3284, 1630, 2477, 2538, 3289, 1633, 2479, 2539,
             3288, 1635, 2480, 2541, 3286, 1636, 2476, 2543, 3288, 1635, 2479, 2543,
             3290, 1632, 2482, 2530, 3284, 1391, 2480, 2085, 3286, 915, 2475, 1252,
             3286, 572, 2483, 704, 3289, 411, 2479, 471, 3286, 368, 2481, 409, 3282,
             378, 2480, 416, 3290, 422, 2483, 469, 3281, 482, 2478, 551, 3289, 561,
             2479, 663, 3289, 651, 2479, 791, 3291, 743, 2480, 934, 3294, 841, 2484,
             1087, 3287, 932, 2473, 1235, 3285, 1020, 2479, 1387, 3285, 1100, 2479,
             1527, 3292, 1173, 2486, 1656, 3288, 1238, 2482, 1776, 3281, 1298, 2476,
             1880, 3285, 1345, 2478, 1971, 3285, 1388, 2478, 2050, 3290, 1425, 2479,
             2122, 0, 3374, 2527],
            [10, 3480087899, 2526, 3283, 1705, 2478, 2658, 3290, 1706, 2474, 2654,
             3287, 1706, 2479, 2654, 3289, 1707, 2483, 2660, 3287, 1710, 2480, 2657,
             3288, 1702, 2482, 2644, 3286, 1449, 2481, 2174, 3289, 951, 2485, 1303,
             3289, 589, 2487, 728, 3287, 432, 2478, 489, 3286, 382, 2473, 415, 3283,
             388, 2480, 426, 3291, 436, 2480, 482, 3286, 503, 2480, 573, 3290, 585,
             2474, 684, 3288, 675, 2484, 823, 3285, 772, 2477, 972, 3289, 878, 2480,
             1136, 3289, 973, 2483, 1295, 3291, 1065, 2482, 1446, 3286, 1151, 2489,
             1604, 3287, 1220, 2481, 1732, 3289, 1293, 2477, 1853, 3286, 1352, 2486,
             1959, 3291, 1402, 2484, 2059, 3289, 1448, 2482, 2141, 3294, 1484, 2485,
             2211, 0, 3373, 2529],
            [10, 3480086699, 2530, 3288, 1435, 2484, 2228, 3289, 1435, 2490, 2234,
             3286, 1439, 2485, 2235, 3287, 1439, 2487, 2238, 3289, 1439, 2487, 2238,
             3290, 1432, 2486, 2208, 3289, 1265, 2484, 1630, 3283, 943, 2482, 797,
             3289, 663, 2485, 336, 3286, 512, 2481, 178, 3282, 451, 2479, 136, 3288,
             449, 2486, 142, 3285, 478, 2480, 173, 3290, 530, 2488, 238, 3288, 600,
             2481, 329, 3287, 675, 2483, 443, 3286, 756, 2486, 582, 3288, 836, 2480,
             732, 3288, 913, 2484, 894, 3291, 983, 2484, 1047, 3288, 1043, 2485, 1199,
             3287, 1103, 2479, 1341, 3291, 1156, 2489, 1469, 3287, 1198, 2480, 1577,
             3292, 1236, 2480, 1675, 3286, 1266, 2480, 1760, 3287, 1295, 2485, 1834,
             0, 3374, 2528],
            [10, 3480086999, 2530, 3285, 1509, 2480, 2341, 3284, 1510, 2484, 2341,
             3288, 1507, 2482, 2341, 3289, 1506, 2480, 2339, 3294, 1513, 2487, 2345,
             3285, 1498, 2477, 2324, 3286, 1276, 2478, 1885, 3285, 857, 2477, 1140,
             3287, 545, 2478, 639, 3289, 397, 2479, 423, 3282, 346, 2482, 354, 3289,
             350, 2480, 358, 3287, 387, 2482, 399, 3287, 446, 2484, 478, 3293, 522,
             2482, 581, 3289, 607, 2480, 702, 3288, 695, 2483, 842, 3289, 789, 2479,
             984, 3285, 879, 2479, 1132, 3288, 962, 2486, 1283, 3286, 1035, 2480,
             1406, 3291, 1104, 2476, 1532, 3281, 1160, 2479, 1640, 3289, 1215, 2482,
             1741, 3289, 1259, 2481, 1827, 3290, 1295, 2481, 1900, 3288, 1326, 2477,
             1964, 0, 3374, 2530],
            [10, 3480087299, 2527, 3291, 1581, 2480, 2457, 3290, 1585, 2481, 2462,
             3289, 1583, 2481, 2461, 3290, 1582, 2486, 2462, 3290, 1581, 2480, 2460,
             3286, 1579, 2477, 2449, 3286, 1331, 2478, 1979, 3285, 895, 2476, 1218,
             3289, 571, 2481, 701, 3284, 413, 2474, 472, 3290, 364, 2477, 400, 3293,
             369, 2481, 405, 3284, 404, 2477, 447, 3288, 467, 2476, 528, 3290, 541,
             2485, 632, 3290, 624, 2478, 757, 3290, 721, 2477, 898, 3287, 814, 2479,
             1049, 3288, 900, 2482, 1191, 3292, 995, 2481, 1339, 3289, 1069, 2478,
             1475, 3289, 1140, 2479, 1601, 3288, 1206, 2480, 1718, 3291, 1258, 2482,
             1815, 3291, 1307, 2483, 1907, 3291, 1344, 2482, 1984, 3289, 1381, 2480,
             2049, 0, 3373, 2526],
            [10, 3480087599, 2530, 3284, 1630, 2477, 2538, 3289, 1633, 2479, 2539,
             3288, 1635, 2480, 2541, 3286, 1636, 2476, 2543, 3288, 1635, 2479, 2543,
             3290, 1632, 2482, 2530, 3284, 1391, 2480, 2085, 3286, 915, 2475, 1252,
             3286, 572, 2483, 704, 3289, 411, 2479, 471, 3286, 368, 2481, 409, 3282,
             378, 2480, 416, 3290, 422, 2483, 469, 3281, 482, 2478, 551, 3289, 561,
             2479, 663, 3289, 651, 2479, 791, 3291, 743, 2480, 934, 3294, 841, 2484,
             1087, 3287, 932, 2473, 1235, 3285, 1020, 2479, 1387, 3285, 1100, 2479,
             1527, 3292, 1173, 2486, 1656, 3288, 1238, 2482, 1776, 3281, 1298, 2476,
             1880, 3285, 1345, 2478, 1971, 3285, 1388, 2478, 2050, 3290, 1425, 2479,
             2122, 0, 3374, 2527],
            [10, 3480087899, 2526, 3283, 1705, 2478, 2658, 3290, 1706, 2474, 2654,
             3287, 1706, 2479, 2654, 3289, 1707, 2483, 2660, 3287, 1710, 2480, 2657,
             3288, 1702, 2482, 2644, 3286, 1449, 2481, 2174, 3289, 951, 2485, 1303,
             3289, 589, 2487, 728, 3287, 432, 2478, 489, 3286, 382, 2473, 415, 3283,
             388, 2480, 426, 3291, 436, 2480, 482, 3286, 503, 2480, 573, 3290, 585,
             2474, 684, 3288, 675, 2484, 823, 3285, 772, 2477, 972, 3289, 878, 2480,
             1136, 3289, 973, 2483, 1295, 3291, 1065, 2482, 1446, 3286, 1151, 2489,
             1604, 3287, 1220, 2481, 1732, 3289, 1293, 2477, 1853, 3286, 1352, 2486,
             1959, 3291, 1402, 2484, 2059, 3289, 1448, 2482, 2141, 3294, 1484, 2485,
             2211, 0, 3373, 2529],
            [10, 3480086699, 2530, 3288, 1435, 2484, 2228, 3289, 1435, 2490, 2234,
             3286, 1439, 2485, 2235, 3287, 1439, 2487, 2238, 3289, 1439, 2487, 2238,
             3290, 1432, 2486, 2208, 3289, 1265, 2484, 1630, 3283, 943, 2482, 797,
             3289, 663, 2485, 336, 3286, 512, 2481, 178, 3282, 451, 2479, 136, 3288,
             449, 2486, 142, 3285, 478, 2480, 173, 3290, 530, 2488, 238, 3288, 600,
             2481, 329, 3287, 675, 2483, 443, 3286, 756, 2486, 582, 3288, 836, 2480,
             732, 3288, 913, 2484, 894, 3291, 983, 2484, 1047, 3288, 1043, 2485, 1199,
             3287, 1103, 2479, 1341, 3291, 1156, 2489, 1469, 3287, 1198, 2480, 1577,
             3292, 1236, 2480, 1675, 3286, 1266, 2480, 1760, 3287, 1295, 2485, 1834,
             0, 3374, 2528],
            [10, 3480086999, 2530, 3285, 1509, 2480, 2341, 3284, 1510, 2484, 2341,
             3288, 1507, 2482, 2341, 3289, 1506, 2480, 2339, 3294, 1513, 2487, 2345,
             3285, 1498, 2477, 2324, 3286, 1276, 2478, 1885, 3285, 857, 2477, 1140,
             3287, 545, 2478, 639, 3289, 397, 2479, 423, 3282, 346, 2482, 354, 3289,
             350, 2480, 358, 3287, 387, 2482, 399, 3287, 446, 2484, 478, 3293, 522,
             2482, 581, 3289, 607, 2480, 702, 3288, 695, 2483, 842, 3289, 789, 2479,
             984, 3285, 879, 2479, 1132, 3288, 962, 2486, 1283, 3286, 1035, 2480,
             1406, 3291, 1104, 2476, 1532, 3281, 1160, 2479, 1640, 3289, 1215, 2482,
             1741, 3289, 1259, 2481, 1827, 3290, 1295, 2481, 1900, 3288, 1326, 2477,
             1964, 0, 3374, 2530],
            [10, 3480087299, 2527, 3291, 1581, 2480, 2457, 3290, 1585, 2481, 2462,
             3289, 1583, 2481, 2461, 3290, 1582, 2486, 2462, 3290, 1581, 2480, 2460,
             3286, 1579, 2477, 2449, 3286, 1331, 2478, 1979, 3285, 895, 2476, 1218,
             3289, 571, 2481, 701, 3284, 413, 2474, 472, 3290, 364, 2477, 400, 3293,
             369, 2481, 405, 3284, 404, 2477, 447, 3288, 467, 2476, 528, 3290, 541,
             2485, 632, 3290, 624, 2478, 757, 3290, 721, 2477, 898, 3287, 814, 2479,
             1049, 3288, 900, 2482, 1191, 3292, 995, 2481, 1339, 3289, 1069, 2478,
             1475, 3289, 1140, 2479, 1601, 3288, 1206, 2480, 1718, 3291, 1258, 2482,
             1815, 3291, 1307, 2483, 1907, 3291, 1344, 2482, 1984, 3289, 1381, 2480,
             2049, 0, 3373, 2526],
            [10, 3480087599, 2530, 3284, 1630, 2477, 2538, 3289, 1633, 2479, 2539,
             3288, 1635, 2480, 2541, 3286, 1636, 2476, 2543, 3288, 1635, 2479, 2543,
             3290, 1632, 2482, 2530, 3284, 1391, 2480, 2085, 3286, 915, 2475, 1252,
             3286, 572, 2483, 704, 3289, 411, 2479, 471, 3286, 368, 2481, 409, 3282,
             378, 2480, 416, 3290, 422, 2483, 469, 3281, 482, 2478, 551, 3289, 561,
             2479, 663, 3289, 651, 2479, 791, 3291, 743, 2480, 934, 3294, 841, 2484,
             1087, 3287, 932, 2473, 1235, 3285, 1020, 2479, 1387, 3285, 1100, 2479,
             1527, 3292, 1173, 2486, 1656, 3288, 1238, 2482, 1776, 3281, 1298, 2476,
             1880, 3285, 1345, 2478, 1971, 3285, 1388, 2478, 2050, 3290, 1425, 2479,
             2122, 0, 3374, 2527],
            [10, 3480087899, 2526, 3283, 1705, 2478, 2658, 3290, 1706, 2474, 2654,
             3287, 1706, 2479, 2654, 3289, 1707, 2483, 2660, 3287, 1710, 2480, 2657,
             3288, 1702, 2482, 2644, 3286, 1449, 2481, 2174, 3289, 951, 2485, 1303,
             3289, 589, 2487, 728, 3287, 432, 2478, 489, 3286, 382, 2473, 415, 3283,
             388, 2480, 426, 3291, 436, 2480, 482, 3286, 503, 2480, 573, 3290, 585,
             2474, 684, 3288, 675, 2484, 823, 3285, 772, 2477, 972, 3289, 878, 2480,
             1136, 3289, 973, 2483, 1295, 3291, 1065, 2482, 1446, 3286, 1151, 2489,
             1604, 3287, 1220, 2481, 1732, 3289, 1293, 2477, 1853, 3286, 1352, 2486,
             1959, 3291, 1402, 2484, 2059, 3289, 1448, 2482, 2141, 3294, 1484, 2485,
             2211, 0, 3373, 2529]
        ])

        # setup expected output arrays
        self.vbatt = np.array([12.3560, 12.3560, 12.3523, 12.3560, 12.3523,
                               12.3560, 12.3560, 12.3523, 12.3560, 12.3523,
                               12.3560, 12.3560, 12.3523, 12.3560, 12.3523])
        self.therm = np.array([0.9927, 0.9479, 1.0375, 1.0151, 0.9703,
                               0.9927, 0.9479, 1.0375, 1.0151, 0.9703,
                               0.9927, 0.9479, 1.0375, 1.0151, 0.9703])
        self.pH = np.array([8.3381, 8.2042, 8.1884, 8.1796, 8.1801,
                            8.3340, 8.1993, 8.1842, 8.1754, 8.1759,
                            8.3279, 8.1930, 8.1779, 8.1691, 8.1696])

        # parse raw into test arrays
        self.tstart = self.raw[:, 2]            # thermistor reading at start of readings
        self.ref = self.raw[:, 3:(3+16)]        # reference measurements
        self.light = self.raw[:, 19:(19+92)]    # light measurements
        self.braw = self.raw[:, 112]            # raw battery voltage counts
        self.traw = self.raw[:, 113]            # thermistor reading at start of readings

    def test_ph_singles(self):
        """
        Test ability of ph_calc_phwater to process a single pH measurement, one
        measurement at a time.
        """
        # determine the number of records and create the output arrays
        nRec = self.ref.shape[0]
        bout = np.zeros(nRec, dtype=np.float)
        tout = np.zeros(nRec, dtype=np.float)
        a434 = np.zeros((nRec, 23), dtype=np.float)
        a578 = np.zeros((nRec, 23), dtype=np.float)
        pout = np.zeros(nRec, dtype=np.float)

        # index through the records, calculating pH one record at a time
        for iRec in range(nRec):
            # compute the battery voltage, final temperature in deg_C and pH,
            # record by record.
            bout[iRec] = ph.ph_battery(self.braw[iRec])
            a434[iRec, :] = ph.ph_434_intensity(self.light[iRec, :])
            a578[iRec, :] = ph.ph_578_intensity(self.light[iRec, :])
            tout[iRec] = ph.ph_thermistor(self.traw[iRec])
            pout[iRec] = ph.ph_calc_phwater(self.ref[iRec, :], self.light[iRec, :],
                                            tout[iRec], self.ea434, self.eb434,
                                            self.ea578, self.eb578, self.ind_slp,
                                            self.ind_off, self.salinity[iRec])

        # test above output where records were processed one at a time
        np.testing.assert_array_almost_equal(bout, self.vbatt, 4)
        np.testing.assert_array_almost_equal(tout, self.therm, 4)
        np.testing.assert_array_almost_equal(pout, self.pH, 4)

    def test_ph_multiples(self):
        """
        Test ability of ph_calc_phwater to process multiple pH measurements
        in a single block.
        """
        bout = ph.ph_battery(self.braw)
        tout = ph.ph_thermistor(self.traw)
        a434 = ph.ph_434_intensity(self.light)  # no unit tests, just checking to see if they work
        print a434
        a578 = ph.ph_578_intensity(self.light)
        print a578

        # reset calibration values to an array, replicating how ION will pass
        # the data when processing blocks of values.
        ea434 = np.ones(15) * self.ea434
        eb434 = np.ones(15) * self.eb434
        ea578 = np.ones(15) * self.ea578
        eb578 = np.ones(15) * self.eb578
        ind_slp = np.ones(15) * self.ind_slp
        ind_off = np.ones(15) * self.ind_off

        # test the function
        pout = ph.ph_calc_phwater(self.ref, self.light, tout, ea434, eb434,
                                  ea578, eb578, ind_slp, ind_off, self.salinity)

        # test above output where records were processed one at a time
        np.testing.assert_array_almost_equal(bout, self.vbatt, 4)
        np.testing.assert_array_almost_equal(tout, self.therm, 4)
        np.testing.assert_array_almost_equal(pout, self.pH, 4)

#!/usr/bin/env python

"""
@package ion_functions.test.pH_functions
@file ion_functions/test/pH_functions.py
@author <NAME>
@brief Unit tests for pH_functions module
"""

from nose.plugins.attrib import attr
from ion_functions.test.base_test import BaseUnitTestCase

import numpy as np
from ion_functions.data import ph_functions as ph


@attr('UNIT', group='func')
class TestpHFunctionsUnit(BaseUnitTestCase):
    def setUp(self):
        """
        Test values for the PHWATER unit tests, based on test strings in the
        DPS and available on Alfresco. Note, had to recompute output values
        using original Matlab code as those provided in DPS do not match the
        output calculated from those input strings.

        OOI (2012). Data Product Specification for pH of Seawater. Document
            Control Number 1341-00510. https://alfresco.oceanobservatories.org/
            (See: Company Home >> OOI >> Controlled >> 1000 System Level >>
            1341-00510_Data_Product_SPEC_PHWATER_OOI.pdf)

        Implemented by <NAME>, April 2013
        Corrected by <NAME>, January 2014 to allow for testing of
            vectorized code.
        """
        # reagent bag calibration factors
        self.ea434 = 17533.
        self.eb434 = 2229.
        self.ea578 = 101.
        self.eb578 = 38502.
        self.ind_slp = 0.9698
        self.ind_off = 0.2484
        self.salinity = np.array([30., 30., 30., 30., 30.,
                                  32., 32., 32., 32., 32.,
                                  35., 35., 35., 35., 35.])

        # raw data strings (already converted from ASCIIHEX) provided by the DPS
        self.raw = np.array([
            [10, 3480086699, 2530, 3288, 1435, 2484, 2228, 3289, 1435, 2490, 2234,
             3286, 1439, 2485, 2235, 3287, 1439, 2487, 2238, 3289, 1439, 2487, 2238,
             3290, 1432, 2486, 2208, 3289, 1265, 2484, 1630, 3283, 943, 2482, 797,
             3289, 663, 2485, 336, 3286, 512, 2481, 178, 3282, 451, 2479, 136, 3288,
             449, 2486, 142, 3285, 478, 2480, 173, 3290, 530, 2488, 238, 3288, 600,
             2481, 329, 3287, 675, 2483, 443, 3286, 756, 2486, 582, 3288, 836, 2480,
             732, 3288, 913, 2484, 894, 3291, 983, 2484, 1047, 3288, 1043, 2485, 1199,
             3287, 1103, 2479, 1341, 3291, 1156, 2489, 1469, 3287, 1198, 2480, 1577,
             3292, 1236, 2480, 1675, 3286, 1266, 2480, 1760, 3287, 1295, 2485, 1834,
             0, 3374, 2528],
            [10, 3480086999, 2530, 3285, 1509, 2480, 2341, 3284, 1510, 2484, 2341,
             3288, 1507, 2482, 2341, 3289, 1506, 2480, 2339, 3294, 1513, 2487, 2345,
             3285, 1498, 2477, 2324, 3286, 1276, 2478, 1885, 3285, 857, 2477, 1140,
             3287, 545, 2478, 639, 3289, 397, 2479, 423, 3282, 346, 2482, 354, 3289,
             350, 2480, 358, 3287, 387, 2482, 399, 3287, 446, 2484, 478, 3293, 522,
             2482, 581, 3289, 607, 2480, 702, 3288, 695, 2483, 842, 3289, 789, 2479,
             984, 3285, 879, 2479, 1132, 3288, 962, 2486, 1283, 3286, 1035, 2480,
             1406, 3291, 1104, 2476, 1532, 3281, 1160, 2479, 1640, 3289, 1215, 2482,
             1741, 3289, 1259, 2481, 1827, 3290, 1295, 2481, 1900, 3288, 1326, 2477,
             1964, 0, 3374, 2530],
            [10, 3480087299, 2527, 3291, 1581, 2480, 2457, 3290, 1585, 2481, 2462,
             3289, 1583, 2481, 2461, 3290, 1582, 2486, 2462, 3290, 1581, 2480, 2460,
             3286, 1579, 2477, 2449, 3286, 1331, 2478, 1979, 3285, 895, 2476, 1218,
             3289, 571, 2481, 701, 3284, 413, 2474, 472, 3290, 364, 2477, 400, 3293,
             369, 2481, 405, 3284, 404, 2477, 447, 3288, 467, 2476, 528, 3290, 541,
             2485, 632, 3290, 624, 2478, 757, 3290, 721, 2477, 898, 3287, 814, 2479,
             1049, 3288, 900, 2482, 1191, 3292, 995, 2481, 1339, 3289, 1069, 2478,
             1475, 3289, 1140, 2479, 1601, 3288, 1206, 2480, 1718, 3291, 1258, 2482,
             1815, 3291, 1307, 2483, 1907, 3291, 1344, 2482, 1984, 3289, 1381, 2480,
             2049, 0, 3373, 2526],
            [10, 3480087599, 2530, 3284, 1630, 2477, 2538, 3289, 1633, 2479, 2539,
             3288, 1635, 2480, 2541, 3286, 1636, 2476, 2543, 3288, 1635, 2479, 2543,
             3290, 1632, 2482, 2530, 3284, 1391, 2480, 2085, 3286, 915, 2475, 1252,
             3286, 572, 2483, 704, 3289, 411, 2479, 471, 3286, 368, 2481, 409, 3282,
             378, 2480, 416, 3290, 422, 2483, 469, 3281, 482, 2478, 551, 3289, 561,
             2479, 663, 3289, 651, 2479, 791, 3291, 743, 2480, 934, 3294, 841, 2484,
             1087, 3287, 932, 2473, 1235, 3285, 1020, 2479, 1387, 3285, 1100, 2479,
             1527, 3292, 1173, 2486, 1656, 3288, 1238, 2482, 1776, 3281, 1298, 2476,
             1880, 3285, 1345, 2478, 1971, 3285, 1388, 2478, 2050, 3290, 1425, 2479,
             2122, 0, 3374, 2527],
            [10, 3480087899, 2526, 3283, 1705, 2478, 2658, 3290, 1706, 2474, 2654,
             3287, 1706, 2479, 2654, 3289, 1707, 2483, 2660, 3287, 1710, 2480, 2657,
             3288, 1702, 2482, 2644, 3286, 1449, 2481, 2174, 3289, 951, 2485, 1303,
             3289, 589, 2487, 728, 3287, 432, 2478, 489, 3286, 382, 2473, 415, 3283,
             388, 2480, 426, 3291, 436, 2480, 482, 3286, 503, 2480, 573, 3290, 585,
             2474, 684, 3288, 675, 2484, 823, 3285, 772, 2477, 972, 3289, 878, 2480,
             1136, 3289, 973, 2483, 1295, 3291, 1065, 2482, 1446, 3286, 1151, 2489,
             1604, 3287, 1220, 2481, 1732, 3289, 1293, 2477, 1853, 3286, 1352, 2486,
             1959, 3291, 1402, 2484, 2059, 3289, 1448, 2482, 2141, 3294, 1484, 2485,
             2211, 0, 3373, 2529],
            [10, 3480086699, 2530, 3288, 1435, 2484, 2228, 3289, 1435, 2490, 2234,
             3286, 1439, 2485, 2235, 3287, 1439, 2487, 2238, 3289, 1439, 2487, 2238,
             3290, 1432, 2486, 2208, 3289, 1265, 2484, 1630, 3283, 943, 2482, 797,
             3289, 663, 2485, 336, 3286, 512, 2481, 178, 3282, 451, 2479, 136, 3288,
             449, 2486, 142, 3285, 478, 2480, 173, 3290, 530, 2488, 238, 3288, 600,
             2481, 329, 3287, 675, 2483, 443, 3286, 756, 2486, 582, 3288, 836, 2480,
             732, 3288, 913, 2484, 894, 3291, 983, 2484, 1047, 3288, 1043, 2485, 1199,
             3287, 1103, 2479, 1341, 3291, 1156, 2489, 1469, 3287, 1198, 2480, 1577,
             3292, 1236, 2480, 1675, 3286, 1266, 2480, 1760, 3287, 1295, 2485, 1834,
             0, 3374, 2528],
            [10, 3480086999, 2530, 3285, 1509, 2480, 2341, 3284, 1510, 2484, 2341,
             3288, 1507, 2482, 2341, 3289, 1506, 2480, 2339, 3294, 1513, 2487, 2345,
             3285, 1498, 2477, 2324, 3286, 1276, 2478, 1885, 3285, 857, 2477, 1140,
             3287, 545, 2478, 639, 3289, 397, 2479, 423, 3282, 346, 2482, 354, 3289,
             350, 2480, 358, 3287, 387, 2482, 399, 3287, 446, 2484, 478, 3293, 522,
             2482, 581, 3289, 607, 2480, 702, 3288, 695, 2483, 842, 3289, 789, 2479,
             984, 3285, 879, 2479, 1132, 3288, 962, 2486, 1283, 3286, 1035, 2480,
             1406, 3291, 1104, 2476, 1532, 3281, 1160, 2479, 1640, 3289, 1215, 2482,
             1741, 3289, 1259, 2481, 1827, 3290, 1295, 2481, 1900, 3288, 1326, 2477,
             1964, 0, 3374, 2530],
            [10, 3480087299, 2527, 3291, 1581, 2480, 2457, 3290, 1585, 2481, 2462,
             3289, 1583, 2481, 2461, 3290, 1582, 2486, 2462, 3290, 1581, 2480, 2460,
             3286, 1579, 2477, 2449, 3286, 1331, 2478, 1979, 3285, 895, 2476, 1218,
             3289, 571, 2481, 701, 3284, 413, 2474, 472, 3290, 364, 2477, 400, 3293,
             369, 2481, 405, 3284, 404, 2477, 447, 3288, 467, 2476, 528, 3290, 541,
             2485, 632, 3290, 624, 2478, 757, 3290, 721, 2477, 898, 3287, 814, 2479,
             1049, 3288, 900, 2482, 1191, 3292, 995, 2481, 1339, 3289, 1069, 2478,
             1475, 3289, 1140, 2479, 1601, 3288, 1206, 2480, 1718, 3291, 1258, 2482,
             1815, 3291, 1307, 2483, 1907, 3291, 1344, 2482, 1984, 3289, 1381, 2480,
             2049, 0, 3373, 2526],
            [10, 3480087599, 2530, 3284, 1630, 2477, 2538, 3289, 1633, 2479, 2539,
             3288, 1635, 2480, 2541, 3286, 1636, 2476, 2543, 3288, 1635, 2479, 2543,
             3290, 1632, 2482, 2530, 3284, 1391, 2480, 2085, 3286, 915, 2475, 1252,
             3286, 572, 2483, 704, 3289, 411, 2479, 471, 3286, 368, 2481, 409, 3282,
             378, 2480, 416, 3290, 422, 2483, 469, 3281, 482, 2478, 551, 3289, 561,
             2479, 663, 3289, 651, 2479, 791, 3291, 743, 2480, 934, 3294, 841, 2484,
             1087, 3287, 932, 2473, 1235, 3285, 1020, 2479, 1387, 3285, 1100, 2479,
             1527, 3292, 1173, 2486, 1656, 3288, 1238, 2482, 1776, 3281, 1298, 2476,
             1880, 3285, 1345, 2478, 1971, 3285, 1388, 2478, 2050, 3290, 1425, 2479,
             2122, 0, 3374, 2527],
            [10, 3480087899, 2526, 3283, 1705, 2478, 2658, 3290, 1706, 2474, 2654,
             3287, 1706, 2479, 2654, 3289, 1707, 2483, 2660, 3287, 1710, 2480, 2657,
             3288, 1702, 2482, 2644, 3286, 1449, 2481, 2174, 3289, 951, 2485, 1303,
             3289, 589, 2487, 728, 3287, 432, 2478, 489, 3286, 382, 2473, 415, 3283,
             388, 2480, 426, 3291, 436, 2480, 482, 3286, 503, 2480, 573, 3290, 585,
             2474, 684, 3288, 675, 2484, 823, 3285, 772, 2477, 972, 3289, 878, 2480,
             1136, 3289, 973, 2483, 1295, 3291, 1065, 2482, 1446, 3286, 1151, 2489,
             1604, 3287, 1220, 2481, 1732, 3289, 1293, 2477, 1853, 3286, 1352, 2486,
             1959, 3291, 1402, 2484, 2059, 3289, 1448, 2482, 2141, 3294, 1484, 2485,
             2211, 0, 3373, 2529],
            [10, 3480086699, 2530, 3288, 1435, 2484, 2228, 3289, 1435, 2490, 2234,
             3286, 1439, 2485, 2235, 3287, 1439, 2487, 2238, 3289, 1439, 2487, 2238,
             3290, 1432, 2486, 2208, 3289, 1265, 2484, 1630, 3283, 943, 2482, 797,
             3289, 663, 2485, 336, 3286, 512, 2481, 178, 3282, 451, 2479, 136, 3288,
             449, 2486, 142, 3285, 478, 2480, 173, 3290, 530, 2488, 238, 3288, 600,
             2481, 329, 3287, 675, 2483, 443, 3286, 756, 2486, 582, 3288, 836, 2480,
             732, 3288, 913, 2484, 894, 3291, 983, 2484, 1047, 3288, 1043, 2485, 1199,
             3287, 1103, 2479, 1341, 3291, 1156, 2489, 1469, 3287, 1198, 2480, 1577,
             3292, 1236, 2480, 1675, 3286, 1266, 2480, 1760, 3287, 1295, 2485, 1834,
             0, 3374, 2528],
            [10, 3480086999, 2530, 3285, 1509, 2480, 2341, 3284, 1510, 2484, 2341,
             3288, 1507, 2482, 2341, 3289, 1506, 2480, 2339, 3294, 1513, 2487, 2345,
             3285, 1498, 2477, 2324, 3286, 1276, 2478, 1885, 3285, 857, 2477, 1140,
             3287, 545, 2478, 639, 3289, 397, 2479, 423, 3282, 346, 2482, 354, 3289,
             350, 2480, 358, 3287, 387, 2482, 399, 3287, 446, 2484, 478, 3293, 522,
             2482, 581, 3289, 607, 2480, 702, 3288, 695, 2483, 842, 3289, 789, 2479,
             984, 3285, 879, 2479, 1132, 3288, 962, 2486, 1283, 3286, 1035, 2480,
             1406, 3291, 1104, 2476, 1532, 3281, 1160, 2479, 1640, 3289, 1215, 2482,
             1741, 3289, 1259, 2481, 1827, 3290, 1295, 2481, 1900, 3288, 1326, 2477,
             1964, 0, 3374, 2530],
            [10, 3480087299, 2527, 3291, 1581, 2480, 2457, 3290, 1585, 2481, 2462,
             3289, 1583, 2481, 2461, 3290, 1582, 2486, 2462, 3290, 1581, 2480, 2460,
             3286, 1579, 2477, 2449, 3286, 1331, 2478, 1979, 3285, 895, 2476, 1218,
             3289, 571, 2481, 701, 3284, 413, 2474, 472, 3290, 364, 2477, 400, 3293,
             369, 2481, 405, 3284, 404, 2477, 447, 3288, 467, 2476, 528, 3290, 541,
             2485, 632, 3290, 624, 2478, 757, 3290, 721, 2477, 898, 3287, 814, 2479,
             1049, 3288, 900, 2482, 1191, 3292, 995, 2481, 1339, 3289, 1069, 2478,
             1475, 3289, 1140, 2479, 1601, 3288, 1206, 2480, 1718, 3291, 1258, 2482,
             1815, 3291, 1307, 2483, 1907, 3291, 1344, 2482, 1984, 3289, 1381, 2480,
             2049, 0, 3373, 2526],
            [10, 3480087599, 2530, 3284, 1630, 2477, 2538, 3289, 1633, 2479, 2539,
             3288, 1635, 2480, 2541, 3286, 1636, 2476, 2543, 3288, 1635, 2479, 2543,
             3290, 1632, 2482, 2530, 3284, 1391, 2480, 2085, 3286, 915, 2475, 1252,
             3286, 572, 2483, 704, 3289, 411, 2479, 471, 3286, 368, 2481, 409, 3282,
             378, 2480, 416, 3290, 422, 2483, 469, 3281, 482, 2478, 551, 3289, 561,
             2479, 663, 3289, 651, 2479, 791, 3291, 743, 2480, 934, 3294, 841, 2484,
             1087, 3287, 932, 2473, 1235, 3285, 1020, 2479, 1387, 3285, 1100, 2479,
             1527, 3292, 1173, 2486, 1656, 3288, 1238, 2482, 1776, 3281, 1298, 2476,
             1880, 3285, 1345, 2478, 1971, 3285, 1388, 2478, 2050, 3290, 1425, 2479,
             2122, 0, 3374, 2527],
            [10, 3480087899, 2526, 3283, 1705, 2478, 2658, 3290, 1706, 2474, 2654,
             3287, 1706, 2479, 2654, 3289, 1707, 2483, 2660, 3287, 1710, 2480, 2657,
             3288, 1702, 2482, 2644, 3286, 1449, 2481, 2174, 3289, 951, 2485, 1303,
             3289, 589, 2487, 728, 3287, 432, 2478, 489, 3286, 382, 2473, 415, 3283,
             388, 2480, 426, 3291, 436, 2480, 482, 3286, 503, 2480, 573, 3290, 585,
             2474, 684, 3288, 675, 2484, 823, 3285, 772, 2477, 972, 3289, 878, 2480,
             1136, 3289, 973, 2483, 1295, 3291, 1065, 2482, 1446, 3286, 1151, 2489,
             1604, 3287, 1220, 2481, 1732, 3289, 1293, 2477, 1853, 3286, 1352, 2486,
             1959, 3291, 1402, 2484, 2059, 3289, 1448, 2482, 2141, 3294, 1484, 2485,
             2211, 0, 3373, 2529]
        ])

        # setup expected output arrays
        self.vbatt = np.array([12.3560, 12.3560, 12.3523, 12.3560, 12.3523,
                               12.3560, 12.3560, 12.3523, 12.3560, 12.3523,
                               12.3560, 12.3560, 12.3523, 12.3560, 12.3523])
        self.therm = np.array([0.9927, 0.9479, 1.0375, 1.0151, 0.9703,
                               0.9927, 0.9479, 1.0375, 1.0151, 0.9703,
                               0.9927, 0.9479, 1.0375, 1.0151, 0.9703])
        self.pH = np.array([8.3381, 8.2042, 8.1884, 8.1796, 8.1801,
                            8.3340, 8.1993, 8.1842, 8.1754, 8.1759,
                            8.3279, 8.1930, 8.1779, 8.1691, 8.1696])

        # parse raw into test arrays
        self.tstart = self.raw[:, 2]            # thermistor reading at start of readings
        self.ref = self.raw[:, 3:(3+16)]        # reference measurements
        self.light = self.raw[:, 19:(19+92)]    # light measurements
        self.braw = self.raw[:, 112]            # raw battery voltage counts
        self.traw = self.raw[:, 113]            # thermistor reading at start of readings

    def test_ph_singles(self):
        """
        Test ability of ph_calc_phwater to process a single pH measurement, one
        measurement at a time.
        """
        # determine the number of records and create the output arrays
        nRec = self.ref.shape[0]
        bout = np.zeros(nRec, dtype=np.float)
        tout = np.zeros(nRec, dtype=np.float)
        a434 = np.zeros((nRec, 23), dtype=np.float)
        a578 = np.zeros((nRec, 23), dtype=np.float)
        pout = np.zeros(nRec, dtype=np.float)

        # index through the records, calculating pH one record at a time
        for iRec in range(nRec):
            # compute the battery voltage, final temperature in deg_C and pH,
            # record by record.
            bout[iRec] = ph.ph_battery(self.braw[iRec])
            a434[iRec, :] = ph.ph_434_intensity(self.light[iRec, :])
            a578[iRec, :] = ph.ph_578_intensity(self.light[iRec, :])
            tout[iRec] = ph.ph_thermistor(self.traw[iRec])
            pout[iRec] = ph.ph_calc_phwater(self.ref[iRec, :], self.light[iRec, :],
                                            tout[iRec], self.ea434, self.eb434,
                                            self.ea578, self.eb578, self.ind_slp,
                                            self.ind_off, self.salinity[iRec])

        # test above output where records were processed one at a time
        np.testing.assert_array_almost_equal(bout, self.vbatt, 4)
        np.testing.assert_array_almost_equal(tout, self.therm, 4)
        np.testing.assert_array_almost_equal(pout, self.pH, 4)

    def test_ph_multiples(self):
        """
        Test ability of ph_calc_phwater to process multiple pH measurements
        in a single block.
        """
        bout = ph.ph_battery(self.braw)
        tout = ph.ph_thermistor(self.traw)
        a434 = ph.ph_434_intensity(self.light)  # no unit tests, just checking to see if they work
        print a434
        a578 = ph.ph_578_intensity(self.light)
        print a578

        # reset calibration values to an array, replicating how ION will pass
        # the data when processing blocks of values.
        ea434 = np.ones(15) * self.ea434
        eb434 = np.ones(15) * self.eb434
        ea578 = np.ones(15) * self.ea578
        eb578 = np.ones(15) * self.eb578
        ind_slp = np.ones(15) * self.ind_slp
        ind_off = np.ones(15) * self.ind_off

        # test the function
        pout = ph.ph_calc_phwater(self.ref, self.light, tout, ea434, eb434,
                                  ea578, eb578, ind_slp, ind_off, self.salinity)

        # test above output where records were processed one at a time
        np.testing.assert_array_almost_equal(bout, self.vbatt, 4)
        np.testing.assert_array_almost_equal(tout, self.therm, 4)
        np.testing.assert_array_almost_equal(pout, self.pH, 4)
#!/usr/bin/env python @package ion_functions.test.pH_functions @file ion_functions/test/pH_functions.py @author <NAME> @brief Unit tests for pH_functions module Test values for the PHWATER unit tests, based on test strings in the         DPS and available on Alfresco. Note, had to recompute output values         using original Matlab code as those provided in DPS do not match the         output calculated from those input strings.          OOI (2012). Data Product Specification for pH of Seawater. Document             Control Number 1341-00510. https://alfresco.oceanobservatories.org/             (See: Company Home >> OOI >> Controlled >> 1000 System Level >>             1341-00510_Data_Product_SPEC_PHWATER_OOI.pdf)          Implemented by <NAME>, April 2013         Corrected by <NAME>, January 2014 to allow for testing of             vectorized code. # reagent bag calibration factors # raw data strings (already converted from ASCIIHEX) provided by the DPS # setup expected output arrays # parse raw into test arrays # thermistor reading at start of readings # reference measurements # light measurements # raw battery voltage counts # thermistor reading at start of readings Test ability of ph_calc_phwater to process a single pH measurement, one         measurement at a time. # determine the number of records and create the output arrays # index through the records, calculating pH one record at a time # compute the battery voltage, final temperature in deg_C and pH, # record by record. # test above output where records were processed one at a time Test ability of ph_calc_phwater to process multiple pH measurements         in a single block. # no unit tests, just checking to see if they work # reset calibration values to an array, replicating how ION will pass # the data when processing blocks of values. # test the function # test above output where records were processed one at a time