Conditions | 6 |
Total Lines | 78 |
Code Lines | 66 |
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 | #!/usr/bin/env python |
||
47 | def main(): |
||
48 | logging.basicConfig(level=logging.WARN, |
||
49 | format="[%(levelname)-8s] (%(asctime)s) " |
||
50 | "%(filename)s:%(lineno)d %(message)s", |
||
51 | datefmt="%Y-%m-%d %H:%M:%S %z") |
||
52 | parser = ap.ArgumentParser() |
||
53 | parser.add_argument("file", help="The input HDF5 file.", |
||
54 | default="SCI_NL__1PYDPA20100203_031030_000060632086_00319_41455_0002.ch1.h5") |
||
55 | parser.add_argument("-C", "--cat", help="The categories to extract, either a " |
||
56 | "single number or a comma-separated list of numbers (default: %(default)s)", |
||
57 | default="26,27") |
||
58 | parser.add_argument("-c", "--clus", help="The spectral clusters to extract, either a " |
||
59 | "single number or a comma-separated list of numbers (default: %(default)s)", |
||
60 | default="2,3,4") |
||
61 | parser.add_argument("-z", "--solar_id", default="D0", |
||
62 | choices=["D0", "D1", "D2", "E0", "E1", "A0", "A1", "N1", "N2", "N3", "N4", "N5"], |
||
63 | help="The solar reference ID to extract (default: %(default)s).") |
||
64 | loglevels = parser.add_mutually_exclusive_group() |
||
65 | loglevels.add_argument("-q", "--quiet", action="store_true", default=False, |
||
66 | help="less output, same as --loglevel=ERROR (default: %(default)s)") |
||
67 | loglevels.add_argument("-v", "--verbose", action="store_true", default=False, |
||
68 | help="verbose output, same as --loglevel=INFO (default: %(default)s)") |
||
69 | loglevels.add_argument("-l", "--loglevel", default="WARNING", |
||
70 | choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
||
71 | help="change the loglevel (default: %(default)s)") |
||
72 | args = parser.parse_args() |
||
73 | if args.quiet: |
||
74 | logging.getLogger().setLevel(logging.ERROR) |
||
75 | elif args.verbose: |
||
76 | logging.getLogger().setLevel(logging.INFO) |
||
77 | else: |
||
78 | logging.getLogger().setLevel(args.loglevel) |
||
79 | |||
80 | cats = [n for n in map(int, args.cat.split(','))] |
||
81 | cl_ids = [n - 1 for n in map(int, args.clus.split(','))] |
||
82 | logging.debug("categories: %s", cats) |
||
83 | logging.debug("cluster ids: %s", cl_ids) |
||
84 | |||
85 | hf = h5py.File(args.file, "r") |
||
86 | |||
87 | mlt_idxs = np.array([], dtype=int) |
||
88 | for cat in cats: |
||
89 | meas_cats = hf.get("/ADS/STATES")["meas_cat"] |
||
90 | mlt_idxs = np.append(mlt_idxs, np.where(meas_cats == cat)[0]) |
||
91 | logging.info("limb state indexes: %s", mlt_idxs) |
||
92 | |||
93 | for sid, lstate_id in enumerate(sorted(mlt_idxs)): |
||
94 | logging.info("processing limb state nr. %s (%s)...", lstate_id, sid) |
||
95 | slsc = slvl1c.scia_limb_scan() |
||
96 | # read and continue to the next state if reading failed |
||
97 | if slsc.read_from_hdf5(hf, lstate_id, sid, cl_ids): |
||
98 | continue |
||
99 | logging.debug("final shapes: %s (wls), %s (signal)", |
||
100 | slsc.wls.shape, slsc.limb_data["rad"].shape) |
||
101 | filename = "SCIA_limb_{0:04d}{1:02d}{2:02d}_{3:02d}{4:02d}{5:02d}_{6}_{7}_{8:05d}".format( |
||
102 | slsc.date[0], slsc.date[1], slsc.date[2], |
||
103 | slsc.date[3], slsc.date[4], slsc.date[5], |
||
104 | slsc.orbit_state[3], slsc.orbit_state[4], |
||
105 | slsc.orbit_state[0]) |
||
106 | |||
107 | slsc.write_to_textfile("{0}.dat".format(filename)) |
||
108 | logging.info("limb state nr. %s written to %s", |
||
109 | lstate_id, "{0}.dat".format(filename)) |
||
110 | slsc.write_to_mpl_binary("{0}.l_mpl_binary".format(filename)) |
||
111 | logging.info("limb state nr. %s written to %s", |
||
112 | lstate_id, "{0}.l_mpl_binary".format(filename)) |
||
113 | del slsc |
||
114 | |||
115 | sol = slvl1c.scia_solar() |
||
116 | sol.read_from_hdf5(hf, args.solar_id) |
||
117 | sol_filename = ("SCIA_solar_{0:%Y%m%d}_{1:%H%M%S}_{2}_{3:05d}".format( |
||
118 | sol.time, sol.time, sol.solar_id, sol.orbit)) |
||
119 | sol.write_to_textfile("{0}.dat".format(sol_filename)) |
||
120 | logging.info("solar reference %s written to %s", |
||
121 | sol.solar_id, "{0}.dat".format(sol_filename)) |
||
122 | del sol |
||
123 | |||
124 | hf.close() |
||
125 | |||
129 |