<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2017, AGB
# Full license can be found in License.md
# -----------------------------------------------------------------------------
"""Tests the vorticity instrument sub-module."""

import datetime as dt
from io import StringIO
import logging
import numpy as np
import os
import platform
import unittest

win_list = ["win32", "cygwin", "windows"]
if platform.system().lower() not in win_list:
    import filecmp

import ocbpy
import ocbpy.instruments.vort as ocb_ivort


class TestVortLogWarnings(unittest.TestCase):
    """Unit tests for the vorticity instrument logging warnings."""

    def setUp(self):
        """Initialize the test environment."""

        self.ocb_dir = os.path.dirname(ocbpy.__file__)
        self.test_file = os.path.join(self.ocb_dir, "tests", "test_data",
                                      "test_vort")
        self.test_ocb = os.path.join(self.ocb_dir, "tests", "test_data",
                                     "test_north_circle")
        self.temp_output = os.path.join(self.ocb_dir, "tests", "test_data",
                                        "temp_vort")
        self.assertTrue(os.path.isfile(self.test_file))

        self.lwarn = u''
        self.lout = u''
        self.log_capture = StringIO()
        ocbpy.logger.addHandler(logging.StreamHandler(self.log_capture))
        ocbpy.logger.setLevel(logging.WARNING)
        return

    def tearDown(self):
        """Tear down the test environment."""
        if os.path.isfile(self.temp_output):
            os.remove(self.temp_output)

        del self.test_file, self.temp_output, self.test_ocb, self.ocb_dir
        del self.lwarn, self.lout, self.log_capture
        return

    def test_vort2ascii_ocb_wrong_hemi(self):
        """Test the vorticity failure of choosing the wrong hemisphere."""

        self.lwarn = u'No southern hemisphere data in file'
        ocb_ivort.vort2ascii_ocb(self.test_file, self.temp_output,
                                 ocbfile=self.test_ocb, instrument='image',
                                 hemisphere=-1)
        self.lout = self.log_capture.getvalue()

        # Test logging error message
        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        return

    def test_vort_unexpected_line(self):
        """Testing vorticity catch for file loading."""

        # Initalize the vorticity run with different test files
        for val in [(u'unexpected line encountered when number of entries', 1),
                    (u'unexpected line encountered for a data block', -1)]:
            with self.subTest(val=val):
                # Initalize the warning
                self.lwarn = val[0]

                # Create the bad file
                with open(self.temp_output, 'w') as fout:
                    with open(self.test_file, 'r') as fin:
                        data = fin.readlines()
                    data.pop(val[1])
                    fout.write(''.join(data))

                # Load the bad file
                data = ocb_ivort.load_vorticity_ascii_data(self.temp_output)
                self.lout = self.log_capture.getvalue()

                # Test logging error message and data output
                self.assertTrue(self.lout.find(self.lwarn) >= 0)
                self.assertIsNone(data)
        return


class TestVort2AsciiMethods(unittest.TestCase):
    """Test the vorticity instrument ASCII functions."""

    def setUp(self):
        """Initialize the testing set up."""

        self.ocb_dir = os.path.dirname(ocbpy.__file__)
        self.test_ocb = os.path.join(self.ocb_dir, "tests", "test_data",
                                     "test_north_circle")
        self.test_file = os.path.join(self.ocb_dir, "tests", "test_data",
                                      "test_hemi_vort")
        self.test_eq_file = os.path.join(self.ocb_dir, "tests", "test_data",
                                         "test_eq_hemi_vort")
        self.test_empty = os.path.join(self.ocb_dir, "tests", "test_data",
                                       "test_empty")
        self.test_output_north = os.path.join(self.ocb_dir, "tests",
                                              "test_data", "out_vort")
        self.test_output_south = os.path.join(self.ocb_dir, "tests",
                                              "test_data", "out_south_vort")
        self.temp_output = os.path.join(self.ocb_dir, "tests", "test_data",
                                        "temp_vort")
        self.test_vals = {'CENTRE_MLAT': 67.27, 'DAY': 5, 'MLT': 3.127,
                          'UTH': 13.65, 'VORTICITY': 0.0020967, 'YEAR': 2000,
                          'DATETIME': dt.datetime(2000, 5, 5, 13, 39, 00),
                          'MONTH': 5}
        self.assertTrue(os.path.isfile(self.test_file))
        return

    def tearDown(self):
        """Clean up the test environment."""
        if os.path.isfile(self.temp_output):
            os.remove(self.temp_output)

        del self.test_file, self.temp_output, self.test_ocb, self.ocb_dir
        del self.test_output_north, self.test_output_south, self.test_eq_file
        del self.test_empty
        return

    def test_deprecated_kwargs(self):
        """Test DeprecationWarning raised for deprecated kwarg input."""
        # Set the deprecated keyword arguments with standard values
        dep_inputs = {"min_sectors": 7, "rcent_dev": 8.0, "max_r": 23.0,
                      "min_r": 10.0}

        # Cycle through the keyword arguments that should raise a warning
        for dkey in dep_inputs.keys():
            kwargs = {dkey: dep_inputs[dkey]}
            with self.subTest(kwargs=kwargs):
                ocb_ivort.vort2ascii_ocb(self.test_file, self.temp_output,
                                         ocbfile=self.test_ocb,
                                         instrument="image", hemisphere=1,
                                         **kwargs)
        return

    def test_vort2ascii_ocb(self):
        """Test vorticity conversion with different hemispheres and methods."""
        # Initialize the subTest input
        subtests = [(self.test_file, self.test_output_north,
                     {"ocbfile": self.test_ocb, "instrument": "image",
                      "hemisphere": 1}),
                    (self.test_file, self.test_output_south,
                     {"ocbfile": self.test_ocb, "instrument": "image",
                      "hemisphere": -1}),
                    (self.test_eq_file, self.test_output_south,
                     {"ocbfile": self.test_ocb, "instrument": "image"}),
                    (self.test_file, self.test_output_north,
                     {"ocb": ocbpy.ocboundary.OCBoundary(
                         filename=self.test_ocb, instrument='image',
                         hemisphere=1)})]

        # Initalize the vorticity run with different test files
        for val in subtests:<commit_msg>TST: added vort unit tests<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2017, AGB
# Full license can be found in License.md
# -----------------------------------------------------------------------------
"""Tests the vorticity instrument sub-module."""

import datetime as dt
from io import StringIO
import logging
import numpy as np
import os
import platform
import unittest

win_list = ["win32", "cygwin", "windows"]
if platform.system().lower() not in win_list:
    import filecmp

import ocbpy
import ocbpy.instruments.vort as ocb_ivort


class TestVortLogWarnings(unittest.TestCase):
    """Unit tests for the vorticity instrument logging warnings."""

    def setUp(self):
        """Initialize the test environment."""

        self.ocb_dir = os.path.dirname(ocbpy.__file__)
        self.test_file = os.path.join(self.ocb_dir, "tests", "test_data",
                                      "test_vort")
        self.test_ocb = os.path.join(self.ocb_dir, "tests", "test_data",
                                     "test_north_circle")
        self.temp_output = os.path.join(self.ocb_dir, "tests", "test_data",
                                        "temp_vort")
        self.assertTrue(os.path.isfile(self.test_file))

        self.lwarn = u''
        self.lout = u''
        self.log_capture = StringIO()
        ocbpy.logger.addHandler(logging.StreamHandler(self.log_capture))
        ocbpy.logger.setLevel(logging.WARNING)
        return

    def tearDown(self):
        """Tear down the test environment."""
        if os.path.isfile(self.temp_output):
            os.remove(self.temp_output)

        del self.test_file, self.temp_output, self.test_ocb, self.ocb_dir
        del self.lwarn, self.lout, self.log_capture
        return

    def test_vort2ascii_ocb_wrong_hemi(self):
        """Test the vorticity failure of choosing the wrong hemisphere."""

        self.lwarn = u'No southern hemisphere data in file'
        ocb_ivort.vort2ascii_ocb(self.test_file, self.temp_output,
                                 ocbfile=self.test_ocb, instrument='image',
                                 hemisphere=-1)
        self.lout = self.log_capture.getvalue()

        # Test logging error message
        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        return

    def test_vort_unexpected_line(self):
        """Testing vorticity catch for file loading."""

        # Initalize the vorticity run with different test files
        for val in [(u'unexpected line encountered when number of entries', 1),
                    (u'unexpected line encountered for a data block', -1)]:
            with self.subTest(val=val):
                # Initalize the warning
                self.lwarn = val[0]

                # Create the bad file
                with open(self.temp_output, 'w') as fout:
                    with open(self.test_file, 'r') as fin:
                        data = fin.readlines()
                    data.pop(val[1])
                    fout.write(''.join(data))

                # Load the bad file
                data = ocb_ivort.load_vorticity_ascii_data(self.temp_output)
                self.lout = self.log_capture.getvalue()

                # Test logging error message and data output
                self.assertTrue(self.lout.find(self.lwarn) >= 0)
                self.assertIsNone(data)
        return


class TestVort2AsciiMethods(unittest.TestCase):
    """Test the vorticity instrument ASCII functions."""

    def setUp(self):
        """Initialize the testing set up."""

        self.ocb_dir = os.path.dirname(ocbpy.__file__)
        self.test_ocb = os.path.join(self.ocb_dir, "tests", "test_data",
                                     "test_north_circle")
        self.test_eab = os.path.join(self.ocb_dir, "tests", "test_data",
                                     "test_north_eab")
        self.test_file = os.path.join(self.ocb_dir, "tests", "test_data",
                                      "test_hemi_vort")
        self.test_eq_file = os.path.join(self.ocb_dir, "tests", "test_data",
                                         "test_eq_hemi_vort")
        self.test_empty = os.path.join(self.ocb_dir, "tests", "test_data",
                                       "test_empty")
        self.test_output_north = os.path.join(self.ocb_dir, "tests",
                                              "test_data", "out_vort")
        self.test_output_dual = os.path.join(self.ocb_dir, "tests",
                                             "test_data", "out_dual_vort")
        self.test_output_south = os.path.join(self.ocb_dir, "tests",
                                              "test_data", "out_south_vort")
        self.temp_output = os.path.join(self.ocb_dir, "tests", "test_data",
                                        "temp_vort")
        self.test_vals = {'CENTRE_MLAT': 67.27, 'DAY': 5, 'MLT': 3.127,
                          'UTH': 13.65, 'VORTICITY': 0.0020967, 'YEAR': 2000,
                          'DATETIME': dt.datetime(2000, 5, 5, 13, 39, 00),
                          'MONTH': 5}
        self.assertTrue(os.path.isfile(self.test_file))
        return

    def tearDown(self):
        """Clean up the test environment."""
        if os.path.isfile(self.temp_output):
            os.remove(self.temp_output)

        del self.test_file, self.temp_output, self.test_ocb, self.ocb_dir
        del self.test_output_north, self.test_output_south, self.test_eq_file
        del self.test_empty
        return

    def test_deprecated_kwargs(self):
        """Test DeprecationWarning raised for deprecated kwarg input."""
        # Set the deprecated keyword arguments with standard values
        dep_inputs = {"min_sectors": 7, "rcent_dev": 8.0, "max_r": 23.0,
                      "min_r": 10.0}

        # Cycle through the keyword arguments that should raise a warning
        for dkey in dep_inputs.keys():
            kwargs = {dkey: dep_inputs[dkey]}
            with self.subTest(kwargs=kwargs):
                ocb_ivort.vort2ascii_ocb(self.test_file, self.temp_output,
                                         ocbfile=self.test_ocb,
                                         instrument="image", hemisphere=1,
                                         **kwargs)
        return

    def test_vort2ascii_ocb(self):
        """Test vorticity conversion with different hemispheres and methods."""
        # Initialize the subTest input
        subtests = [(self.test_file, self.test_output_north,
                     {"ocbfile": self.test_ocb, "instrument": "image",
                      "hemisphere": 1}),
                    (self.test_file, self.test_output_south,
                     {"ocbfile": self.test_ocb, "instrument": "image",
                      "hemisphere": -1}),
                    (self.test_eq_file, self.test_output_south,
                     {"ocbfile": self.test_ocb, "instrument": "image"}),
                    (self.test_file, self.test_output_north,
                     {"ocb": ocbpy.OCBoundary(
                         filename=self.test_ocb, instrument='image',
                         hemisphere=1)}),
                    (self.test_file, self.test_output_north,
                     {"ocb": ocbpy.EABoundary(
                         filename=self.test_ocb, instrument='image',
                         hemisphere=1, boundary_lat=74.0)}),
                    (self.test_file, self.test_output_dual,
                     {"ocb": ocbpy.DualBoundary(
                         ocb_filename=self.test_ocb, ocb_instrument='image',
                         eab_filename=self.test_eab, eab_instrument='image',
                         hemisphere=1)})]

        # Initalize the vorticity run with different test files
        for val in subtests:
{'fraction_non_alphanumeric': 0.1158751696065129, 'fraction_numerical': 0.00983717774762551, 'mean_word_length': 1.716734242535938, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2326056', 'n_tokens_mistral': 4442, 'n_tokens_neox': 4135, 'n_words': 904}