def test_get_aacgm_coord_arr_datetime_date(self): """Test array AACGMV2 calculation with date and datetime input""" self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, self.ddate, self.method) self.ref = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, self.dtime, self.method) self.evaluate_output()
def test_get_aacgm_coord_arr_datetime_date(self): """Test array AACGMV2 calculation with date and datetime input""" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [300], self.ddate) mlat_2, mlon_2, mlt_2 = aacgmv2.get_aacgm_coord_arr([60], [0], [300], self.dtime) np.testing.assert_almost_equal(self.mlat_out, mlat_2, decimal=6) np.testing.assert_almost_equal(self.mlon_out, mlon_2, decimal=6) np.testing.assert_almost_equal(self.mlt_out, mlt_2, decimal=6) del mlat_2, mlon_2, mlt_2
def test_get_aacgm_coord_arr_mlat_failure(self): """Test error return for co-latitudes above 90 for an array""" import logbook lerr = u"unrealistic latitude" with logbook.TestHandler() as handler: with pytest.raises(AssertionError): aacgmv2.get_aacgm_coord_arr([91, 60, -91], 0, 300, self.dtime) if not handler.has_error(lerr): raise AssertionError() handler.close()
def test_get_aacgm_coord_arr_datetime_date(self): """Test array AACGMV2 calculation with date and datetime input""" self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, self.ddate, self.method) self.ref = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, self.dtime, self.method) assert len(self.out) == len(self.ref) assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) for oo in self.out] for i, oo in enumerate(self.out): np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol)
def test_get_aacgm_coord_arr_arr(self): """Test array AACGMV2 calculation for an array""" self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in), np.array(self.lon_in), np.array(self.alt_in), self.dtime, self.method) self.evaluate_output()
def test_get_aacgm_coord_arr_list_single(self): """Test array AACGMV2 calculation for list input of single values""" self.out = aacgmv2.get_aacgm_coord_arr([self.lat_in[0]], [self.lon_in[0]], [self.alt_in[0]], self.dtime, self.method) self.evaluate_output(ind=0)
def test_get_aacgm_coord_arr_mult_failure(self): """Test aacgm_coord_arr failure with multi-dim array input""" with pytest.raises(ValueError): (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr( np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, self.dtime)
def test_get_aacgm_coord_arr_arr(self): """Test array AACGMV2 calculation for an array""" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr(np.array([60, 61]), np.array([0, 0]), np.array([300, 300]), self.dtime) if not isinstance(self.mlat_out, np.ndarray): raise AssertionError() if not isinstance(self.mlon_out, np.ndarray): raise AssertionError() if not isinstance(self.mlt_out, np.ndarray): raise AssertionError() if not (self.mlt_out.shape == self.mlon_out.shape and self.mlat_out.shape == self.mlt_out.shape and self.mlt_out.shape == (2,)): raise AssertionError() np.testing.assert_allclose(self.mlat_out, [58.22474610, 59.31648007], rtol=1e-4) np.testing.assert_allclose(self.mlon_out, [81.17611033, 81.62281360], rtol=1e-4) np.testing.assert_allclose(self.mlt_out, [0.18891995, 0.21870017], rtol=1e-4)
def test_get_aacgm_coord_arr_mult_arr_mix(self): """Test array AACGMV2 calculation for a multi-dim array and floats""" mlat_in = np.array([[60, 61, 62], [63, 64, 65]]) (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr(mlat_in, 0, 300, self.dtime) if not isinstance(self.mlat_out, np.ndarray): raise AssertionError() if not isinstance(self.mlon_out, np.ndarray): raise AssertionError() if not isinstance(self.mlt_out, np.ndarray): raise AssertionError() if not (self.mlt_out.shape == self.mlon_out.shape and self.mlat_out.shape == self.mlt_out.shape and self.mlt_out.shape == (2, 3)): raise AssertionError() np.testing.assert_allclose(self.mlat_out, [[58.2247461, 59.3164801, 60.4008651], [61.4780560, 62.5481858, 63.6113609]], rtol=1e-4) np.testing.assert_allclose(self.mlon_out, [[81.1761103, 81.6228136, 82.0969646], [82.6013918, 83.1393547, 83.7146224]], rtol=1e-4) np.testing.assert_allclose(self.mlt_out, [[0.18891995, 0.21870017, 0.25031024], [0.28393872, 0.31980291, 0.35815409]], rtol=1e-4) del mlat_in
def test_get_aacgm_coord_arr_arr_unequal(self): """Test array AACGMV2 calculation for unequal arrays""" mlat_in = np.array([[60, 61, 62], [63, 64, 65]]) (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr(mlat_in, np.array([0]), np.array([300]), self.dtime) if not isinstance(self.mlat_out, np.ndarray): raise AssertionError() if not isinstance(self.mlon_out, np.ndarray): raise AssertionError() if not isinstance(self.mlt_out, np.ndarray): raise AssertionError() if not (self.mlt_out.shape == self.mlon_out.shape and self.mlat_out.shape == self.mlt_out.shape and self.mlt_out.shape == (2, 3)): raise AssertionError() np.testing.assert_allclose(self.mlat_out, [[58.2247, 59.3165, 60.4009], [61.4781, 62.5482, 63.6114]], rtol=1e-3) np.testing.assert_allclose(self.mlon_out, [[81.1761, 81.6228, 82.0970], [82.6014, 83.1394, 83.7146]], rtol=1e-3) np.testing.assert_allclose(self.mlt_out, [[0.1889, 0.2187, 0.2503], [0.2839, 0.3198, 0.3582]], rtol=1e-3) del mlat_in
def test_get_aacgm_coord_arr_arr_single(self): """Test array AACGMV2 calculation for array with a single value""" self.out = aacgmv2.get_aacgm_coord_arr(np.array([self.lat_in[0]]), np.array([self.lon_in[0]]), np.array([self.alt_in[0]]), self.dtime, self.method) self.evaluate_output(ind=0)
def test_get_aacgm_coord_arr_mlat_failure(self): """Test error return for co-latitudes above 90 for an array""" self.lat_in = [91, 60, -91] with pytest.raises(ValueError): self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0], self.alt_in[0], self.dtime, self.method)
def test_get_aacgm_coord_arr_location_failure(self): """Test array AACGMV2 calculation with a bad location""" self.out = aacgmv2.get_aacgm_coord_arr([0], [0], [0], self.dtime, self.method) np.testing.assert_equal(len(self.out), len(self.ref)) assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] assert np.any([np.isnan(oo) for oo in self.out])
def test_get_aacgm_coord_arr_badidea(self): """Test array AACGMV2 calculation for BADIDEA""" self.method = "|".join([self.method, "BADIDEA"]) self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], [3000.0], self.dtime, self.method) self.ref = [[64.3481], [83.2885], [0.3306]] self.evaluate_output()
def test_get_aacgm_coord_arr_single_val(self): """Test array AACGMV2 calculation for a single value""" self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], self.alt_in[0], self.dtime, self.method) assert len(self.out) == len(self.ref) assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] for i, oo in enumerate(self.out): np.testing.assert_allclose(oo, [self.ref[i][0]], rtol=self.rtol)
def test_get_aacgm_coord_arr_maxalt_failure(self): """For an array, test failure for an altitude too high for coefficients""" method = "" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [2001], self.dtime, method=method) if not np.all([np.isnan(self.mlat_out), np.isnan(self.mlon_out), np.isnan(self.mlt_out)]): raise AssertionError() del method
def test_get_aacgm_coord_arr_badidea(self): """Test array AACGMV2 calculation for BADIDEA""" method = "BADIDEA" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [3000], self.dtime, method=method) np.testing.assert_allclose(self.mlat_out, [64.35677791], rtol=1e-3) np.testing.assert_allclose(self.mlon_out, [83.30272053], rtol=1e-3) np.testing.assert_allclose(self.mlt_out, [0.33069397], rtol=1e-3) del method
def mlat(self, lats, longs, alts, stamps): """ Calculates geomagnetic coordinates """ mlats, mlongs, mlts = aacgmv2.get_aacgm_coord_arr( lats, longs, alts, stamps[0]) temp_inds = np.nonzero(np.isnan(mlats)) mlats[temp_inds] = lats[temp_inds] mlongs[temp_inds] = longs[temp_inds] zeros = np.zeros_like(lats) mlts[temp_inds] = zeros[temp_inds] return (mlats, mlongs, mlts)
def test_get_aacgm_coord_arr_arr_mix(self): """Test array AACGMV2 calculation for an array and floats""" self.out = aacgmv2.get_aacgm_coord_arr(np.array(self.lat_in), self.lon_in[0], self.alt_in[0], self.dtime, self.method) assert len(self.out) == len(self.ref) assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) for oo in self.out] for i, oo in enumerate(self.out): np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol)
def test_get_aacgm_coord_arr_maxalt_failure(self): """test aacgm_coord_arr failure for an altitude too high for coeff""" self.method = "" self.alt_in = [2001 for ll in self.lat_in] self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, self.dtime, self.method) assert len(self.out) == len(self.ref) assert [isinstance(oo, np.ndarray) and len(oo) == len(self.lat_in) for oo in self.out] assert np.all(np.isnan(np.array(self.out)))
def test_warning_below_ground_get_aacgm_coord_arr(self): """ Test that a warning is issued if altitude is below zero""" import logbook lwarn = u"conversion not intended for altitudes < 0 km" with logbook.TestHandler() as handler: (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [-1], self.dtime) if not handler.has_warning(lwarn): raise AssertionError() handler.close()
def test_get_aacgm_coord_arr_badidea(self): """Test array AACGMV2 calculation for BADIDEA""" self.method = "|".join([self.method, "BADIDEA"]) self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in[0], self.lon_in[0], [3000.0], self.dtime, self.method) assert len(self.out) == len(self.ref) assert [isinstance(oo, np.ndarray) and len(oo) == 1 for oo in self.out] self.ref = [64.3481, 83.2885, 0.3306] for i, oo in enumerate(self.out): np.testing.assert_allclose(oo, self.ref[i], rtol=self.rtol)
def test_get_aacgm_coord_arr_location_failure(self): """Test array AACGMV2 calculation with a bad location""" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr([0], [0], [0], self.dtime) if not isinstance(self.mlat_out, np.ndarray): raise AssertionError() if not isinstance(self.mlon_out, np.ndarray): raise AssertionError() if not isinstance(self.mlt_out, np.ndarray): raise AssertionError() if not (self.mlt_out.shape == self.mlon_out.shape and self.mlat_out.shape == self.mlt_out.shape and self.mlt_out.shape == (1,)): raise AssertionError() if not np.all([np.isnan(self.mlat_out), np.isnan(self.mlon_out), np.isnan(self.mlt_out)]): raise AssertionError()
def test_get_aacgm_coord_arr_single_val(self): """Test array AACGMV2 calculation for a single value""" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr(60, 0, 300, self.dtime) if not isinstance(self.mlat_out, np.ndarray): raise AssertionError() if not isinstance(self.mlon_out, np.ndarray): raise AssertionError() if not isinstance(self.mlt_out, np.ndarray): raise AssertionError() if not (self.mlt_out.shape == self.mlon_out.shape and self.mlat_out.shape == self.mlt_out.shape and self.mlt_out.shape == (1,)): raise AssertionError() np.testing.assert_allclose(self.mlat_out, [58.22474610], rtol=1e-4) np.testing.assert_allclose(self.mlon_out, [81.17611033], rtol=1e-4) np.testing.assert_allclose(self.mlt_out, [0.18891995], rtol=1e-4)
def test_get_aacgm_coord_arr_arr_single(self): """Test array AACGMV2 calculation for array with a single value""" (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr(np.array([60]), np.array([0]), np.array([300]), self.dtime) if not isinstance(self.mlat_out, np.ndarray): raise AssertionError() if not isinstance(self.mlon_out, np.ndarray): raise AssertionError() if not isinstance(self.mlt_out, np.ndarray): raise AssertionError() if not (self.mlt_out.shape == self.mlon_out.shape and self.mlat_out.shape == self.mlt_out.shape and self.mlt_out.shape == (1,)): raise AssertionError() np.testing.assert_almost_equal(self.mlat_out, 58.2247, decimal=4) np.testing.assert_almost_equal(self.mlon_out, 81.1761, decimal=4) np.testing.assert_almost_equal(self.mlt_out, 0.1889, decimal=4)
def test_get_aacgm_coord_arr_list(self): """Test array AACGMV2 calculation for list input""" self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, self.dtime, self.method) self.evaluate_output()
def test_get_aacgm_coord_arr_time_failure(self): """Test array AACGMV2 calculation with a bad time""" with pytest.raises(AssertionError): (self.mlat_out, self.mlon_out, self.mlt_out) = aacgmv2.get_aacgm_coord_arr([60], [0], [300], None)
def test_get_aacgm_coord_arr_list_mix(self): """Test array AACGMV2 calculation for a list and floats""" self.out = aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in[0], self.alt_in[0], self.dtime, self.method) self.evaluate_output()
def test_get_aacgm_coord_arr_time_failure(self): """Test array AACGMV2 calculation with a bad time""" with pytest.raises(ValueError): aacgmv2.get_aacgm_coord_arr(self.lat_in, self.lon_in, self.alt_in, None, self.method)