import os
import unittest
from datetime import date

import numpy as np

from sdi.binary import read

class TestReadMeta(unittest.TestCase):
    """ Test reading of metadata from binary files against values 
    seen in DepthPic GUI.
    """

    def setUp(self):
        self.test_dir = os.path.dirname(__file__)
        self.test_data = {
           '06081001.bin':{'draft': 0.37, 
                'tide': 0.0, 
                'spdos': 1477.1,
                'date': date(2006, 8, 10),
            },
           '07050823.bin':{'draft': 0.40, 
                'tide': 0.0, 
                'spdos': 1492.0,
                'date': date(2007,5,8)
            },
            '08091852.bin':{'draft': 0.46, 
                'tide': 0.0, 
                'spdos': 1499.3,
                'date': date(2008,9,18)
            },
            '09112303.bin':{'draft': 0.46, 
                'tide': 0.0, 
                'spdos': 1471.0,
                'date': date(2009,11,23)
            },
        }

    def test_read_metadata(self):
        """ Test that metadata matches what can be seen in DepthPic GUI. 
        Note: Only a limited number of fields are visible in DepthPic.
        """

        for name, metadata in self.test_data.iteritems():
            filename = os.path.join(self.test_dir, 'files', name)
            d = read(filename)
            f200 = max(d['frequencies'].keys())
            
            self.assertEqual(d['date'], metadata['date'])
            self.assertAlmostEqual(np.unique(d['frequencies'][f200]['draft']), metadata['draft'], places=2)
            self.assertAlmostEqual(np.round(np.unique(d['frequencies'][f200]['spdos']),1), metadata['spdos'], places=1)
            self.assertAlmostEqual(np.unique(d['frequencies'][f200]['tide']), metadata['tide'], places=2)


if __name__ == '__main__':
    unittest.main()
@@ -44,16 +44,28 @@
 ort date
+%0Aimport json
 %0A%0Aimport
@@ -105,16 +105,25 @@
 ort read
+, Dataset
 %0A%0Aclass 
@@ -1041,16 +1041,33 @@
 metadata
+_against_depthpic
 (self):%0A
@@ -1726,16 +1726,16 @@
 aces=1)%0A
-
         
@@ -1834,16 +1834,1060 @@
 es=2)%0A%0A%0A
+    def test_read_metadata_against_json(self):%0A        %22%22%22 Test that metadata against selected values exported from the %0A        binary files using the depreciated reader in pyhat %0A        (https://github.com/twdb/pyhat). The pyhat reader has been in use %0A        for several years and in the absense of an absolute way to test %0A        the reader this is a reasonable sanity check.%0A        %22%22%22%0A%0A        for root, dirs, files in os.walk(os.path.join(self.test_dir, 'files')):%0A            for filename in files:%0A                if filename.endswith('.bin'):%0A                    jsonfile = os.path.join(root, filename.replace('.bin','.json'))%0A                    with open(jsonfile) as f:%0A                        j = json.load(f)%0A                    d = Dataset(os.path.join(root, filename))%0A                    d.parse()%0A                    self.assertEqual(d.version, str(j%5B'version'%5D))%0A                    for a,b in zip(sorted(d.frequencies.keys()), sorted(j%5B'frequencies'%5D)):%0A                        self.assertAlmostEqual(a, b, places=1)%0A%0A%0A
 if __nam

add tests against metadata exported to json using depreciated pyhat sdi reader