#############################################################################
# Copyright (c) 2002-2021 MBARI
# Monterey Bay Aquarium Research Institute, all rights reserved.
#############################################################################
import os
import time
import unittest

import numpy

from LCM.TethysLcmTypes import DoubleArray
from supervisor.ExportLogs import ExportLogs


HOME_DIR = os.path.expanduser("~")
PROJ_DIR = os.path.dirname(os.path.abspath("backseat_app.py"))

class ExportLogsTestCase(unittest.TestCase):

    def test_find_logs(self):
        # from supervisor.ExportLogs import ExportLogs

        export_data = ExportLogs()
        export_data.find_logs(PROJ_DIR + '/test/data/Fall-CANON-2019')

    def test_write_netcdf(self):
        # from supervisor.ExportLogs import ExportLogs

        export_data = ExportLogs()
        export_data.read_log(PROJ_DIR + '/test/data/lcmlog.00')
        export_data.write_netcdf(PROJ_DIR + '/test/data/test')

    def test_write_netcdf_ndarray(self):
        data = numpy.array([[0.1, 1.1, 2.1, 3.1],
                            [4.1, 5.1, 6.1, 7.1],
                            [8.1, 9.1, 10.1, 11.1]])

        export_data = ExportLogs()

        channel = 'TEST'
        double_item = DoubleArray()
        double_item.name = 'foo'
        double_item.unit = 'n/a'
        double_item.nDim = data.ndim
        double_item.shape = data.shape
        double_item.data = list(data.flat)
        double_item.size = data.size
        double_item.valid = True

        export_data.unpack_item(channel, double_item, timestamp=time.time())
        export_data.write_netcdf(PROJ_DIR + '/test/data/test')

    def test_write_hdf5(self):
        export_data = ExportLogs()
        export_data.read_log(PROJ_DIR + '/test/data/lcmlog.00')
        export_data.write_hdf5(PROJ_DIR + '/test/data/test')

    def test_match_log(self):
        log = '20191003T192852'
        destination_root = HOME_DIR + '/tmp/photon/test/Logs'

        export_data = ExportLogs()
        match = export_data.match_log(log, destination_root)


if __name__ == '__main__':
    unittest.main()
