Passed
Pull Request — develop (#77)
by Angeline
01:21
created

test_utils_aacgmv2.TestUtilsAACGMV2.setup_method()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import datetime as dt
2
import numpy as np
3
import pytest
4
5
from aacgmv2 import utils
6
7
8
class TestUtilsAACGMV2(object):
9
    """Unit tests for the utility functions."""
10
    def setup_method(self):
11
        """Run before every method to create a clean testing setup."""
12
        self.rtol = 1.0e-4
13
        self.out = None
14
15
    def teardown_method(self):
16
        """Run after every method to clean up previous testing."""
17
        del self.rtol, self.out
18
19
    @pytest.mark.parametrize('year,ref', [(1880, [-179.1494, -23.0801]),
20
                                          (2015, [-179.2004, -23.0431])])
21
    def test_subsol(self, year, ref):
22
        """Test the subsolar calculation.
23
24
        Parameters
25
        ----------
26
        year : int
27
            Input year
28
        ref : list
29
            Expected output
30
31
        """
32
        self.out = utils.subsol(year, 1, 0.0)
33
        np.testing.assert_allclose(self.out, ref, rtol=self.rtol)
34
35
    @pytest.mark.parametrize('year', [(1500), (2110)])
36
    def test_subsol_raises_time_range(self, year):
37
        """Test the routine failure for out-of-range dates.
38
39
        Parameters
40
        ----------
41
        year : int
42
            Input year
43
44
        """
45
        with pytest.raises(ValueError, match="subsol valid between 1601-2100"):
46
            self.out = utils.subsol(year, 1, 0.0)
47
48
    @pytest.mark.parametrize('year,ref',
49
                             [(1500, [0.141408, -0.48357, 0.86381]),
50
                              (2015, [0.050281, -0.16057, 0.98574]),
51
                              (2110, [0.027069, -0.08006, 0.99642])])
52
    def test_igrf_dipole_axis(self, year, ref):
53
        """Test the IGRF dipole axis calculation.
54
55
        Parameters
56
        ----------
57
        year : int
58
            Input year
59
        ref : list
60
            Expected output
61
62
        """
63
        self.out = utils.igrf_dipole_axis(dt.datetime(year, 1, 1))
64
65
        np.testing.assert_allclose(self.out, ref, rtol=self.rtol)
66
67
    @pytest.mark.parametrize('gc_lat,gd_lat,mult',
68
                             [(45.0, 45.1924, False),
69
                              ([45.0, -45.0], [45.1924, -45.1924], True),
70
                              (np.array([45.0, -45.0]),
71
                               np.array([45.1924, -45.1924]), True)])
72
    def test_gc2gd_lat(self, gc_lat, gd_lat, mult):
73
        """Test the geocentric to geodetic conversion.
74
75
        Parameters
76
        ----------
77
        gc_lat : float, list, array
78
            Geocentric latitude
79
        gd_lat : float, list, array
80
            Geodetic latitude
81
        mult : bool
82
            Specify whether or not the input/output is a float
83
84
        """
85
        self.out = utils.gc2gd_lat(gc_lat)
86
87
        if mult:
88
            np.testing.assert_allclose(self.out, gd_lat, rtol=self.rtol)
89
        else:
90
            np.testing.assert_almost_equal(self.out, gd_lat, decimal=4)
91