def test_convert_datetime_date(): lat_1, lon_1, alt_1 = aacgmv2.convert(60, 0, 300, dt.date(2013, 12, 1)) lat_2, lon_2, alt_2 = aacgmv2.convert(60, 0, 300, dt.datetime(2013, 12, 1, 0, 0, 0)) assert lat_1 == lat_2 assert lon_1 == lon_2 assert alt_1 == alt_2
def test_exception_maxalt(): with pytest.raises(ValueError): aacgmv2.convert(60, 0, 2001, dtObj) with pytest.raises(ValueError): aacgmv2.convert(60, 0, [300, 2001], dtObj) # the following should not raise exceptions aacgmv2.convert(60, 0, 2001, dtObj, trace=True) aacgmv2.convert(60, 0, 2001, dtObj, allowtrace=True) aacgmv2.convert(60, 0, 2001, dtObj, badidea=True)
def test_convert_result_values_geocentric(): lat_p, lon_p, alt_p = aacgmv2.convert(60, 0, 300, dtObj, geocentric=True) aacgmv2._aacgmv2.setDateTime(*date) lat_c, lon_c, alt_c = aacgmv2._aacgmv2.aacgmConvert(60, 0, 300, GEOCENTRIC) assert lat_p == lat_c assert lon_p == lon_c assert alt_p == alt_c
def test_convert_result_values_A2G_coeff(): lat_p, lon_p, alt_p = aacgmv2.convert(60, 0, 300, dtObj, a2g=True) aacgmv2._aacgmv2.setDateTime(*date) lat_c, lon_c, alt_c = aacgmv2._aacgmv2.aacgmConvert(60, 0, 300, A2G) assert lat_p == lat_c assert lon_p == lon_c assert alt_p == alt_c
def test_convert_result_values_G2A_trace(): lat_p, lon_p, alt_p = aacgmv2.convert(60, 0, 300, dtObj, trace=True) aacgmv2._aacgmv2.setDateTime(*date) lat_c, lon_c, alt_c = aacgmv2._aacgmv2.aacgmConvert( 60, 0, 300, G2A | TRACE) assert lat_p == lat_c assert lon_p == lon_c assert alt_p == alt_c
def test_convert_result_values_badidea(): lat, lon, alt = aacgmv2.convert(60, 0, [300, 5000], dtObj, badidea=True) aacgmv2._aacgmv2.setDateTime(*date) np.testing.assert_allclose( (lat[0], lon[0], alt[0]), aacgmv2._aacgmv2.aacgmConvert(60, 0, 300, BADIDEA), rtol=1e-5) np.testing.assert_allclose( (lat[1], lon[1], alt[1]), aacgmv2._aacgmv2.aacgmConvert(60, 0, 5000, BADIDEA), rtol=1e-5)
def test_convert_result_values_allowtrace(): lat, lon, alt = aacgmv2.convert(60, 0, [300, 5000], dtObj, allowtrace=True) aacgmv2._aacgmv2.setDateTime(*date) np.testing.assert_allclose( (lat[0], lon[0], alt[0]), aacgmv2._aacgmv2.aacgmConvert(60, 0, 300, ALLOWTRACE), rtol=1e-5) np.testing.assert_allclose( (lat[1], lon[1], alt[1]), aacgmv2._aacgmv2.aacgmConvert(60, 0, 5000, ALLOWTRACE), rtol=1e-5)
def test_output_type(): lat, lon, alt = aacgmv2.convert(60, 0, 300, dtObj) print(type(lat)) print(lat.shape) print(lat.size) assert isinstance(lat, np.ndarray) assert isinstance(lon, np.ndarray) assert isinstance(alt, np.ndarray) lat, lon, alt = aacgmv2.convert([60], [0], [300], dtObj) assert isinstance(lat, np.ndarray) assert isinstance(lon, np.ndarray) assert isinstance(alt, np.ndarray) lat, lon, alt = aacgmv2.convert([60, 61], [0, 0], [300, 300], dtObj) assert isinstance(lat, np.ndarray) assert isinstance(lon, np.ndarray) assert isinstance(alt, np.ndarray) lat, lon, alt = aacgmv2.convert([60, 61, 62], 0, 300, dtObj) assert isinstance(lat, np.ndarray) assert isinstance(lon, np.ndarray) assert isinstance(alt, np.ndarray) lat, lon, alt = aacgmv2.convert(np.array([60, 61, 62]), 0, 300, dtObj) assert isinstance(lat, np.ndarray) assert isinstance(lon, np.ndarray) assert isinstance(alt, np.ndarray) lat, lon, alt = aacgmv2.convert(np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, dtObj) assert isinstance(lat, np.ndarray) assert isinstance(lon, np.ndarray) assert isinstance(alt, np.ndarray)
def main(): '''Entry point for the script''' parser = argparse.ArgumentParser(description='Converts between geographical coordinates, AACGM-v2, and MLT') subparsers = parser.add_subparsers(title='Subcommands', prog='aacgmv2', dest='subcommand', description='for help, run %(prog)s SUBCOMMAND -h') subparsers.required = True parser_convert = subparsers.add_parser('convert', help=('convert to/from geomagnetic coordinates. Input file must ' 'have lines of the form "LAT LON ALT".')) parser_convert_mlt = subparsers.add_parser('convert_mlt', help=('convert between magnetic local time (MLT) and ' 'AACGM-v2 longitude. Input file must have a single ' 'number on each line.')) for p in [parser_convert, parser_convert_mlt]: p.add_argument('-i', '--input', dest='file_in', metavar='FILE_IN', type=argparse.FileType('r'), default=STDIN, help='input file (stdin if none specified)') p.add_argument('-o', '--output', dest='file_out', metavar='FILE_OUT', type=argparse.FileType('wb'), default=STDOUT, help='output file (stdout if none specified)') parser_convert.add_argument('-d', '--date', dest='date', metavar='YYYYMMDD', help='date for magnetic field model (1900-2020, default: today)') parser_convert.add_argument('-v', '--a2g', dest='a2g', action='store_true', default=False, help='invert - convert AACGM to geographic instead of geographic to AACGM') parser_convert.add_argument('-t', '--trace', dest='trace', action='store_true', default=False, help='use field-line tracing instead of coefficients') parser_convert.add_argument('-a', '--allowtrace', dest='allowtrace', action='store_true', default=False, help='automatically use field-line tracing above 2000 km') parser_convert.add_argument('-b', '--badidea', dest='badidea', action='store_true', default=False, help='allow use of coefficients above 2000 km (bad idea!)') parser_convert.add_argument('-g', '--geocentric', dest='geocentric', action='store_true', default=False, help='assume inputs are geocentric with Earth radius 6371.2 km') parser_convert_mlt.add_argument('datetime', metavar='YYYYMMDDHHMMSS', help='date and time for conversion') parser_convert_mlt.add_argument('-v', '--m2a', dest='m2a', action='store_true', default=False, help='invert - convert MLT to AACGM longitude instead of AACGM longitude to MLT') args = parser.parse_args() array = np.loadtxt(args.file_in, ndmin=2) if args.subcommand == 'convert': date = dt.date.today() if args.date is None else dt.datetime.strptime(args.date, '%Y%m%d') lats, lons, alts = aacgmv2.convert(array[:, 0], array[:, 1], array[:, 2], date=date, a2g=args.a2g, trace=args.trace, allowtrace=args.allowtrace, badidea=args.badidea, geocentric=args.geocentric) with args.file_out as f: np.savetxt(f, np.column_stack((lats, lons, alts)), fmt='%.8f') elif args.subcommand == 'convert_mlt': datetime = dt.datetime.strptime(args.datetime, '%Y%m%d%H%M%S') out = aacgmv2.convert_mlt(array, datetime, m2a=args.m2a) with args.file_out as f: np.savetxt(f, out, fmt='%.8f')
def test_output_shape_size(): lat, lon, alt = aacgmv2.convert(60, 0, 300, dtObj) assert lat.shape == tuple() assert lon.shape == tuple() assert alt.shape == tuple() assert lat.size == 1 assert lon.size == 1 assert alt.size == 1 lat, lon, alt = aacgmv2.convert([60], [0], [300], dtObj) assert lat.shape == (1, ) assert lon.shape == (1, ) assert alt.shape == (1, ) assert lat.size == 1 assert lon.size == 1 assert alt.size == 1 lat, lon, alt = aacgmv2.convert([60, 61], [0, 0], [300, 300], dtObj) assert lat.shape == (2, ) assert lon.shape == (2, ) assert alt.shape == (2, ) assert lat.size == 2 assert lon.size == 2 assert alt.size == 2 lat, lon, alt = aacgmv2.convert([60, 61, 62], 0, 300, dtObj) assert lat.shape == (3, ) assert lon.shape == (3, ) assert alt.shape == (3, ) assert lat.size == 3 assert lon.size == 3 assert alt.size == 3 lat, lon, alt = aacgmv2.convert(np.array([60, 61, 62]), 0, 300, dtObj) assert lat.shape == (3, ) assert lon.shape == (3, ) assert alt.shape == (3, ) assert lat.size == 3 assert lon.size == 3 assert alt.size == 3 lat, lon, alt = aacgmv2.convert(np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, dtObj) assert lat.shape == (2, 3) assert lon.shape == (2, 3) assert alt.shape == (2, 3) assert lat.size == 6 assert lon.size == 6 assert alt.size == 6
def test_convert_result_values_shape(): lat, lon, alt = aacgmv2.convert(np.array([[60, 61, 62], [63, 64, 65]]), 0, 300, dtObj) aacgmv2._aacgmv2.setDateTime(*date) np.testing.assert_allclose((lat[0, 0], lon[0, 0], alt[0, 0]), aacgmv2._aacgmv2.aacgmConvert(60, 0, 300, G2A), rtol=1e-5) np.testing.assert_allclose((lat[0, 1], lon[0, 1], alt[0, 1]), aacgmv2._aacgmv2.aacgmConvert(61, 0, 300, G2A), rtol=1e-5) np.testing.assert_allclose((lat[0, 2], lon[0, 2], alt[0, 2]), aacgmv2._aacgmv2.aacgmConvert(62, 0, 300, G2A), rtol=1e-5) np.testing.assert_allclose((lat[1, 0], lon[1, 0], alt[1, 0]), aacgmv2._aacgmv2.aacgmConvert(63, 0, 300, G2A), rtol=1e-5) np.testing.assert_allclose((lat[1, 1], lon[1, 1], alt[1, 1]), aacgmv2._aacgmv2.aacgmConvert(64, 0, 300, G2A), rtol=1e-5) np.testing.assert_allclose((lat[1, 2], lon[1, 2], alt[1, 2]), aacgmv2._aacgmv2.aacgmConvert(65, 0, 300, G2A), rtol=1e-5)
def test_forbidden(): np.testing.assert_equal(aacgmv2.convert(7, 0, 0), (np.nan, np.nan, np.nan))
def test_exception_lat90(): with pytest.raises(ValueError): aacgmv2.convert(91, 0, 300, dtObj) with pytest.raises(ValueError): aacgmv2.convert(-91, 0, 300, dtObj) with pytest.raises(ValueError): aacgmv2.convert([60, 91], 0, 300, dtObj) with pytest.raises(ValueError): aacgmv2.convert([60, -91], 0, 300, dtObj) # the following should not raise exceptions aacgmv2.convert(90, 0, 300, dtObj) aacgmv2.convert(-90, 0, 300, dtObj)
def test_warning_below_ground(): with pytest.warns(UserWarning): aacgmv2.convert(60, 0, -1, dtObj) with pytest.warns(UserWarning): aacgmv2.convert(60, 0, [300, -1], dtObj)