Conditions | 10 |
Total Lines | 81 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Tests | 55 |
CRAP Score | 10.0138 |
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:
Complex classes like sciapy.level1c.scia_limb_txt.read_from_textfile() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
38 | 1 | def read_from_textfile(self, filename): |
|
39 | """SCIAMACHY level 1c limb scan text import |
||
40 | |||
41 | Parameters |
||
42 | ---------- |
||
43 | filename : str |
||
44 | The (plain ascii) table text filename to read the data from. |
||
45 | |||
46 | Returns |
||
47 | ------- |
||
48 | nothing |
||
49 | """ |
||
50 | 1 | import numpy.lib.recfunctions as rfn |
|
51 | 1 | if hasattr(filename, 'seek'): |
|
52 | f = filename |
||
53 | else: |
||
54 | 1 | f = open(filename, 'rb') |
|
55 | 1 | h_list = [] |
|
56 | 1 | nh = int(f.readline()) |
|
57 | 1 | for _ in range(nh): |
|
58 | 1 | h_list.append(bytes(f.readline()).decode().rstrip()) |
|
59 | 1 | self.textheader_length = nh |
|
60 | 1 | self.textheader = '\n'.join(h_list) |
|
61 | 1 | self.parse_textheader() |
|
62 | 1 | self.nalt, self.npix = np.fromstring(f.readline(), dtype=_int_type, sep=' ') |
|
63 | |||
64 | 1 | self.orbit_state = np.fromstring(f.readline(), dtype=_int_type, sep=' ') |
|
65 | 1 | (self.orbit, self.state_in_orbit, self.state_id, |
|
66 | self.profiles_per_state, self.profile_in_state) = self.orbit_state |
||
67 | 1 | self.date = np.fromstring(f.readline(), dtype=_int_type, sep=' ') |
|
68 | # pre-set the limb_data |
||
69 | 1 | if self._limb_data_dtype is None: |
|
70 | 1 | self._limb_data_dtype = _limb_data_dtype[:] |
|
71 | 1 | self.limb_data = np.zeros((self.nalt), dtype=self._limb_data_dtype) |
|
72 | 1 | if nh > 27: |
|
73 | 1 | self.limb_data["sub_sat_lat"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
74 | 1 | self.limb_data["sub_sat_lon"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
75 | 1 | if nh > 29: |
|
76 | 1 | self.orbit_phase = np.fromstring(f.readline(), dtype=_float_type, sep=' ')[0] |
|
77 | 1 | self.cent_lat_lon = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
78 | |||
79 | 1 | self.limb_data["tp_lat"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
80 | 1 | self.limb_data["tp_lon"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
81 | 1 | self.limb_data["tp_alt"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
82 | 1 | self.limb_data["tp_sza"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
83 | 1 | self.limb_data["tp_saa"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
84 | 1 | self.limb_data["tp_los"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
85 | 1 | self.limb_data["toa_sza"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
86 | 1 | self.limb_data["toa_saa"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
87 | 1 | self.limb_data["toa_los"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
88 | 1 | self.limb_data["sat_sza"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
89 | 1 | self.limb_data["sat_saa"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
90 | 1 | self.limb_data["sat_los"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
91 | 1 | self.limb_data["sat_alt"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
92 | 1 | self.limb_data["earth_rad"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
93 | |||
94 | 1 | tmp_list = [] |
|
95 | 1 | for _ in range(self.npix): |
|
96 | 1 | input = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
97 | 1 | self.wls.append(input[0]) |
|
98 | 1 | tmp_list.append(input[1:]) |
|
99 | 1 | tmp_rad_arr = np.asarray(tmp_list).reshape(self.npix, self.nalt).transpose() |
|
100 | 1 | tmp_list[:] = [] |
|
101 | 1 | line = f.readline().strip() |
|
102 | 1 | if bytes(line) == b"ERRORS": |
|
103 | 1 | for _ in range(self.npix): |
|
104 | 1 | input = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
|
105 | 1 | tmp_list.append(input[1:]) |
|
106 | else: |
||
107 | for _ in range(self.npix): |
||
108 | tmp_list.append(np.zeros(self.nalt)) |
||
109 | 1 | tmp_err_arr = np.asarray(tmp_list).reshape(self.npix, self.nalt).transpose() |
|
110 | |||
111 | # save to limb_data recarray |
||
112 | 1 | rads = np.rec.fromarrays([tmp_rad_arr], |
|
113 | dtype=np.dtype([("rad", 'f4', (self.npix,))])) |
||
114 | 1 | errs = np.rec.fromarrays([tmp_err_arr], |
|
115 | dtype=np.dtype([("err", 'f4', (self.npix,))])) |
||
116 | 1 | self.limb_data = rfn.merge_arrays([self.limb_data, rads, errs], |
|
117 | usemask=False, asrecarray=True, flatten=True) |
||
118 | 1 | self._limb_data_dtype = self.limb_data.dtype |
|
119 | |||
182 |