def test_convert_bool_to_bit(self, bool_dict, method_code): """Test conversion from Boolean code to bit""" self.ref_code = method_code self.set_c_code() self.out = aacgmv2.convert_bool_to_bit(**bool_dict) np.testing.assert_equal(self.out, self.c_method_code)
def test_convert_bool_to_bit(self, bool_dict, method_code): """Test conversion from Boolean code to bit""" if hasattr(aacgmv2._aacgmv2, method_code.upper()): self.c_method_code = getattr(aacgmv2._aacgmv2, method_code.upper()) else: raise ValueError('cannot find method in C code: {:}'.format( method_code)) assert aacgmv2.convert_bool_to_bit(**bool_dict) == self.c_method_code
def main(): """Entry point for the script""" desc = 'Converts between geographical coordinates, AACGM-v2, and MLT' parser = argparse.ArgumentParser(description=desc) desc = 'for help, run %(prog)s SUBCOMMAND -h' subparsers = parser.add_subparsers(title='Subcommands', prog='aacgmv2', dest='subcommand', description=desc) subparsers.required = True desc = 'convert to/from geomagnetic coordinates. Input file must have lines' desc += 'of the form "LAT LON ALT".' parser_convert = subparsers.add_parser('convert', help=(desc)) desc = 'convert between magnetic local time (MLT) and AACGM-v2 longitude. ' desc += 'Input file must have a single number on each line.' parser_convert_mlt = subparsers.add_parser('convert_mlt', help=(desc)) desc = 'input file (stdin if none specified)' for pp in [parser_convert, parser_convert_mlt]: pp.add_argument('-i', '--input', dest='file_in', metavar='FILE_IN', type=argparse.FileType('r'), default=STDIN, help=desc) pp.add_argument('-o', '--output', dest='file_out', metavar='FILE_OUT', type=argparse.FileType('wb'), default=STDOUT, help='output file (stdout if none specified)') desc = 'date for magnetic field model (1900-2020, default: today)' parser_convert.add_argument('-d', '--date', dest='date', metavar='YYYYMMDD', help=desc) desc = 'invert - convert AACGM to geographic instead of geographic to AACGM' parser_convert.add_argument('-v', '--a2g', dest='a2g', action='store_true', default=False, help=desc) desc = 'use field-line tracing instead of coefficients' parser_convert.add_argument('-t', '--trace', dest='trace', action='store_true', default=False, help=desc) desc = 'automatically use field-line tracing above 2000 km' parser_convert.add_argument('-a', '--allowtrace', dest='allowtrace', action='store_true', default=False, help=desc) desc = 'allow use of coefficients above 2000 km (bad idea!)' parser_convert.add_argument('-b', '--badidea', dest='badidea', action='store_true', default=False, help=desc) desc = 'assume inputs are geocentric with Earth radius 6371.2 km' parser_convert.add_argument('-g', '--geocentric', dest='geocentric', action='store_true', default=False, help=desc) parser_convert_mlt.add_argument('datetime', metavar='YYYYMMDDHHMMSS', help='date and time for conversion') desc = 'invert - convert MLT to AACGM longitude instead of AACGM longitude' desc += ' to MLT' parser_convert_mlt.add_argument('-v', '--m2a', dest='m2a', action='store_true', default=False, help=desc) 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') code = aacgmv2.convert_bool_to_bit(a2g=args.a2g, trace=args.trace, allowtrace=args.allowtrace, badidea=args.badidea, geocentric=args.geocentric) lats, lons, alts = aacgmv2.convert_latlon_arr(array[:, 0], array[:, 1], array[:, 2], dtime=date, method_code=code) np.savetxt(args.file_out, np.column_stack((lats, lons, alts)), fmt='%.8f') elif args.subcommand == 'convert_mlt': dtime = dt.datetime.strptime(args.datetime, '%Y%m%d%H%M%S') out = np.array(aacgmv2.convert_mlt(array[:, 0], dtime, m2a=args.m2a)) if len(out.shape) == 0: out = np.array([out]) np.savetxt(args.file_out, out, fmt='%.8f')
def convert(lat, lon, alt, date=None, a2g=False, trace=False, allowtrace=False, badidea=False, geocentric=False): """Converts between geomagnetic coordinates and AACGM coordinates Parameters ------------ lat : (float) Input latitude in degrees N (code specifies type of latitude) lon : (float) Input longitude in degrees E (code specifies type of longitude) alt : (float) Altitude above the surface of the earth in km date : (datetime) Datetime for magnetic field a2g : (bool) True for AACGM-v2 to geographic (geodetic), False otherwise (default=False) trace : (bool) If True, use field-line tracing, not coefficients (default=False) allowtrace : (bool) If True, use trace only above 2000 km (default=False) badidea : (bool) If True, use coefficients above 2000 km (default=False) geocentric : (bool) True for geodetic, False for geocentric w/RE=6371.2 (default=False) Returns ------- lat_out : (float) Output latitude in degrees N lon_out : (float) Output longitude in degrees E """ import aacgmv2 if (np.array(alt).max() > 2000 and not trace and not allowtrace and not badidea): estr = 'coefficients are not valid for altitudes above 2000 km. You' estr += ' must either use field-line tracing (trace=True ' estr += 'or allowtrace=True) or indicate you know this is a bad idea' estr += ' (badidea=True)' logging.error(estr) raise ValueError(estr) # construct a code from the boolian flags bit_code = aacgmv2.convert_bool_to_bit(a2g=a2g, trace=trace, allowtrace=allowtrace, badidea=badidea, geocentric=geocentric) # convert location lat_out, lon_out, _ = aacgmv2.convert_latlon_arr(lat, lon, alt, date, code=bit_code) return lat_out, lon_out
def test_convert_bool_to_bit_geocentric(self): """Test conversion from string code to bit GEOCENTRIC""" if(aacgmv2.convert_bool_to_bit(geocentric=True) != aacgmv2._aacgmv2.GEOCENTRIC): raise AssertionError()
def test_convert_bool_to_bit_badidea(self): """Test conversion from string code to bit BADIDEA""" if(aacgmv2.convert_bool_to_bit(badidea=True) != aacgmv2._aacgmv2.BADIDEA): raise AssertionError()
def test_convert_bool_to_bit_allowtrace(self): """Test conversion from string code to bit ALLOWTRACE""" if(aacgmv2.convert_bool_to_bit(allowtrace=True) != aacgmv2._aacgmv2.ALLOWTRACE): raise AssertionError()
def test_convert_bool_to_bit_trace(self): """Test conversion from string code to bit TRACE""" if aacgmv2.convert_bool_to_bit(trace=True) != aacgmv2._aacgmv2.TRACE: raise AssertionError()
def test_convert_bool_to_bit_a2g(self): """Test conversion from string code to bit A2G""" if aacgmv2.convert_bool_to_bit(a2g=True) != aacgmv2._aacgmv2.A2G: raise AssertionError()
def test_convert_bool_to_bit_g2a(self): """Test conversion from string code to bit G2A""" if aacgmv2.convert_bool_to_bit() != aacgmv2._aacgmv2.G2A: raise AssertionError()