Conditions | 7 |
Total Lines | 52 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
26 | def main(): |
||
27 | '''Entry point for the script''' |
||
28 | |||
29 | parser = argparse.ArgumentParser(description='Converts between geographical coordinates, AACGM-v2, and MLT') |
||
30 | |||
31 | subparsers = parser.add_subparsers(title='Subcommands', prog='aacgmv2', dest='subcommand', |
||
32 | description='for help, run %(prog)s SUBCOMMAND -h') |
||
33 | subparsers.required = True |
||
34 | parser_convert = subparsers.add_parser('convert', help=('convert to/from geomagnetic coordinates. Input file must ' |
||
35 | 'have lines of the form "LAT LON ALT".')) |
||
36 | parser_convert_mlt = subparsers.add_parser('convert_mlt', help=('convert between magnetic local time (MLT) and ' |
||
37 | 'AACGM-v2 longitude. Input file must have a single ' |
||
38 | 'number on each line.')) |
||
39 | |||
40 | for p in [parser_convert, parser_convert_mlt]: |
||
41 | p.add_argument('-i', '--input', dest='file_in', metavar='FILE_IN', type=argparse.FileType('r'), |
||
42 | default=STDIN, help='input file (stdin if none specified)') |
||
43 | p.add_argument('-o', '--output', dest='file_out', metavar='FILE_OUT', type=argparse.FileType('wb'), |
||
44 | default=STDOUT, help='output file (stdout if none specified)') |
||
45 | |||
46 | parser_convert.add_argument('-d', '--date', dest='date', metavar='YYYYMMDD', |
||
47 | help='date for magnetic field model (1900-2020, default: today)') |
||
48 | parser_convert.add_argument('-v', '--a2g', dest='a2g', action='store_true', default=False, |
||
49 | help='invert - convert AACGM to geographic instead of geographic to AACGM') |
||
50 | parser_convert.add_argument('-t', '--trace', dest='trace', action='store_true', default=False, |
||
51 | help='use field-line tracing instead of coefficients') |
||
52 | parser_convert.add_argument('-a', '--allowtrace', dest='allowtrace', action='store_true', default=False, |
||
53 | help='automatically use field-line tracing above 2000 km') |
||
54 | parser_convert.add_argument('-b', '--badidea', dest='badidea', action='store_true', default=False, |
||
55 | help='allow use of coefficients above 2000 km (bad idea!)') |
||
56 | parser_convert.add_argument('-g', '--geocentric', dest='geocentric', action='store_true', default=False, |
||
57 | help='assume inputs are geocentric with Earth radius 6371.2 km') |
||
58 | |||
59 | parser_convert_mlt.add_argument('datetime', metavar='YYYYMMDDHHMMSS', help='date and time for conversion') |
||
60 | parser_convert_mlt.add_argument('-v', '--m2a', dest='m2a', action='store_true', default=False, |
||
61 | help='invert - convert MLT to AACGM longitude instead of AACGM longitude to MLT') |
||
62 | |||
63 | args = parser.parse_args() |
||
64 | |||
65 | array = np.loadtxt(args.file_in, ndmin=2) |
||
66 | |||
67 | if args.subcommand == 'convert': |
||
68 | date = dt.date.today() if args.date is None else dt.datetime.strptime(args.date, '%Y%m%d') |
||
69 | lats, lons, alts = aacgmv2.convert(array[:, 0], array[:, 1], array[:, 2], date=date, a2g=args.a2g, trace=args.trace, |
||
70 | allowtrace=args.allowtrace, badidea=args.badidea, geocentric=args.geocentric) |
||
71 | with args.file_out as f: |
||
72 | np.savetxt(f, np.column_stack((lats, lons, alts)), fmt='%.8f') |
||
73 | elif args.subcommand == 'convert_mlt': |
||
74 | datetime = dt.datetime.strptime(args.datetime, '%Y%m%d%H%M%S') |
||
75 | out = aacgmv2.convert_mlt(array, datetime, m2a=args.m2a) |
||
76 | with args.file_out as f: |
||
77 | np.savetxt(f, out, fmt='%.8f') |
||
78 | |||
82 |