Passed
Pull Request — master (#60)
by Angeline
01:20
created

TestCAACGMV2.test_convert_G2A_TRACE()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 18
rs 9.65
c 0
b 0
f 0
cc 2
nop 1
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import datetime as dt
5
import numpy as np
6
import pytest
7
8
import aacgmv2
9
10
11
class TestCAACGMV2:
12
    def setup(self):
13
        """Runs before every method to create a clean testing setup"""
14
        self.date_args = [(2014, 3, 22, 3, 11, 0), (2018, 1, 1, 0, 0, 0)]
15
        self.long_date = [2014, 3, 22, 3, 11, 0]
16
        self.mlat = None
17
        self.mlon = None
18
        self.rshell = None
19
        self.mlt = None
20
        self.lat_in = [45.5, 60]
21
        self.lon_in = [-23.5, 0]
22
        self.alt_in = [1135, 300]
23
        self.code = {'G2A': aacgmv2._aacgmv2.G2A, 'A2G': aacgmv2._aacgmv2.A2G,
24
                     'TG2A': aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
25
                     'TA2G': aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE}
26
        self.lat_comp = {'G2A': [48.1902, 58.2194], 'A2G': [30.7550, 50.4371],
27
                         'TG2A': [48.1954, 58.2189], 'TA2G': [30.7661, 50.4410]}
28
        self.lon_comp = {'G2A': [57.7505, 80.7282], 'A2G': [-94.1724, -77.5323],
29
                         'TG2A': [57.7456, 80.7362],
30
                         'TA2G': [-94.1727, -77.5440]}
31
        self.r_comp = {'G2A': [1.1775, 1.0457], 'A2G': [1133.6246, 305.7308],
32
                       'TG2A': [1.1775, 1.0457], 'TA2G': [1133.6282, 305.7322]}
33
34
    def teardown(self):
35
        """Runs after every method to clean up previous testing"""
36
        del self.date_args, self.long_date, self.mlat, self.mlon, self.mlt
37
        del self.lat_in, self.lon_in, self.alt_in, self.lat_comp, self.lon_comp
38
        del self.r_comp, self.code
39
40
    @pytest.mark.parametrize('mattr,val', [(aacgmv2._aacgmv2.G2A, 0),
41
                                           (aacgmv2._aacgmv2.A2G, 1),
42
                                           (aacgmv2._aacgmv2.TRACE, 2),
43
                                           (aacgmv2._aacgmv2.ALLOWTRACE, 4),
44
                                           (aacgmv2._aacgmv2.BADIDEA, 8),
45
                                           (aacgmv2._aacgmv2.GEOCENTRIC, 16)])
46
    def test_constants(self, mattr, val):
47
        """Test module constants"""
48
        np.testing.assert_equal(mattr, val)
49
50
    @pytest.mark.parametrize('idate', [0, 1])
51
    def test_set_datetime(self, idate):
52
        """Test set_datetime"""
53
        self.mlt = aacgmv2._aacgmv2.set_datetime(*self.date_args[idate])
54
        assert self.mlt is None
55
56
    def test_fail_set_datetime(self):
57
        """Test unsuccessful set_datetime"""
58
        self.long_date[0] = 1013
59
        with pytest.raises(RuntimeError):
60
            aacgmv2._aacgmv2.set_datetime(*self.long_date)
61
62
    @pytest.mark.parametrize('idate,ckey', [(0, 'G2A'), (1, 'G2A'),
63
                                            (0, 'A2G'), (1, 'A2G'),
64
                                            (0, 'TG2A'), (1, 'TG2A'),
65
                                            (0, 'TA2G'), (1, 'TA2G')])
66
    def test_convert(self, idate, ckey):
67
        """Test convert from geographic to magnetic coordinates"""
68
        aacgmv2._aacgmv2.set_datetime(*self.date_args[idate])
69
        (self.mlat, self.mlon,
70
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[idate],
71
                                                 self.lon_in[idate],
72
                                                 self.alt_in[idate],
73
                                                 self.code[ckey])
74
        np.testing.assert_almost_equal(self.mlat, self.lat_comp[ckey][idate],
75
                                       decimal=4)
76
        np.testing.assert_almost_equal(self.mlon, self.lon_comp[ckey][idate],
77
                                       decimal=4)
78
        np.testing.assert_almost_equal(self.rshell, self.r_comp[ckey][idate],
79
                                       decimal=4)
80
81
    @pytest.mark.parametrize('ckey', ['G2A', 'A2G', 'TG2A', 'TA2G'])
82
    def test_convert_arr(self, ckey):
83
        """Test convert_arr using from magnetic to geodetic coordinates"""
84
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
85
        (self.mlat, self.mlon, self.rshell,
86
         bad_ind) = aacgmv2._aacgmv2.convert_arr(self.lat_in, self.lon_in,
87
                                                 self.alt_in,
88
                                                 self.code[ckey])
89
90
        np.testing.assert_equal(len(self.mlat), len(self.lat_in))
91
        np.testing.assert_almost_equal(self.mlat[0], self.lat_comp[ckey][0],
92
                                       decimal=4)
93
        np.testing.assert_almost_equal(self.mlon[0], self.lon_comp[ckey][0],
94
                                       decimal=4)
95
        np.testing.assert_almost_equal(self.rshell[0], self.r_comp[ckey][0],
96
                                       decimal=4)
97
        np.testing.assert_equal(bad_ind[0], -1)
98
99
    def test_forbidden(self):
100
        """Test convert failure"""
101
        self.lat_in[0] = 7
102
        with pytest.raises(RuntimeError):
103
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 0,
104
                                     aacgmv2._aacgmv2.G2A)
105
106
    def test_convert_high_denied(self):
107
        """Test for failure when converting to high altitude geodetic to
108
        magnetic coordinates"""
109
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
110
        with pytest.raises(RuntimeError):
111
            aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0], 5500,
112
                                     aacgmv2._aacgmv2.G2A)
113
114
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
115
                             [(aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE,
116
                               59.9753, 57.7294, 1.8626),
117
                              (aacgmv2._aacgmv2.G2A
118
                               + aacgmv2._aacgmv2.ALLOWTRACE, 59.9753, 57.7294,
119
                               1.8626),
120
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.BADIDEA,
121
                               58.7286, 56.4296, 1.8626)])
122
    def test_convert_high(self, code, lat_comp, lon_comp, r_comp):
123
        """Test convert from high altitude geodetic to magnetic coordinates"""
124
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
125
        (self.mlat, self.mlon,
126
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
127
                                                 5500, code)
128
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
129
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
130
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
131
132
    @pytest.mark.parametrize('code,lat_comp,lon_comp,r_comp',
133
                             [(aacgmv2._aacgmv2.G2A
134
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
135
                               1.1781),
136
                              (aacgmv2._aacgmv2.G2A
137
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3784, 57.7844,
138
                               1.1781),
139
                              (aacgmv2._aacgmv2.A2G
140
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6117, -94.1724,
141
                               1135.0000),
142
                              (aacgmv2._aacgmv2.G2A + aacgmv2._aacgmv2.TRACE
143
                               + aacgmv2._aacgmv2.GEOCENTRIC, 48.3836, 57.7793,
144
                               1.1781),
145
                              (aacgmv2._aacgmv2.A2G + aacgmv2._aacgmv2.TRACE
146
                               + aacgmv2._aacgmv2.GEOCENTRIC, 30.6227, -94.1727,
147
                               1135.0000)])
148
    def test_convert_geocentric(self, code, lat_comp, lon_comp, r_comp):
149
        """Test convert for different code inputs with geocentric coords"""
150
        aacgmv2._aacgmv2.set_datetime(*self.date_args[0])
151
        (self.mlat, self.mlon,
152
         self.rshell) = aacgmv2._aacgmv2.convert(self.lat_in[0], self.lon_in[0],
153
                                                 self.alt_in[0], code)
154
        np.testing.assert_almost_equal(self.mlat, lat_comp, decimal=4)
155
        np.testing.assert_almost_equal(self.mlon, lon_comp, decimal=4)
156
        np.testing.assert_almost_equal(self.rshell, r_comp, decimal=4)
157
158
    @pytest.mark.parametrize('marg,mlt_comp',
159
                             [(12.0, -153.6033), (25.0, 41.3967),
160
                              (-1.0, 11.3967)])
161
    def test_inv_mlt_convert(self, marg, mlt_comp):
162
        """Test MLT inversion"""
163
        self.long_date = list(self.long_date)
164
        self.long_date.append(marg)
165
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert(*self.long_date)
166
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
167
168
    @pytest.mark.parametrize('marg,mlt_comp',
169
                             [(12.0, -153.6033), (25.0, 41.3967),
170
                              (-1.0, 11.3967)])
171
    def test_inv_mlt_convert_yrsec(self, marg, mlt_comp):
172
        """Test MLT inversion with year and seconds of year"""
173
        dtime = dt.datetime(*self.long_date)
174
        soy = (int(dtime.strftime("%j")) - 1) * 86400 + dtime.hour * 3600 \
175
            + dtime.minute * 60 + dtime.second
176
177
        self.mlon = aacgmv2._aacgmv2.inv_mlt_convert_yrsec(dtime.year, soy,
178
                                                           marg)
179
180
        np.testing.assert_almost_equal(self.mlon, mlt_comp, decimal=4)
181
182
        del dtime, soy
183
184
    @pytest.mark.parametrize('marg,mlt_comp',
185
                             [(270.0, 16.2402), (80.0, 3.5736),
186
                              (-90.0, 16.2402)])
187
    def test_mlt_convert(self, marg, mlt_comp):
188
        """Test MLT calculation with different longitudes"""
189
        mlt_args = list(self.long_date)
190
        mlt_args.append(marg)
191
        self.mlt = aacgmv2._aacgmv2.mlt_convert(*mlt_args)
192
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
193
194
    @pytest.mark.parametrize('marg,mlt_comp',
195
                             [(270.0, 16.2402), (80.0, 3.5736),
196
                              (-90.0, 16.2402)])
197
    def test_mlt_convert_yrsec(self, marg, mlt_comp):
198
        """Test MLT calculation using year and seconds of year"""
199
        dtime = dt.datetime(*self.long_date)
200
        soy = (int(dtime.strftime("%j")) - 1) * 86400 + dtime.hour * 3600 \
201
            + dtime.minute * 60 + dtime.second
202
203
        self.mlt = aacgmv2._aacgmv2.mlt_convert_yrsec(dtime.year, soy, marg)
204
205
        np.testing.assert_almost_equal(self.mlt, mlt_comp, decimal=4)
206
207
        del dtime, soy
208