| Conditions | 12 |
| Total Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 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 Rayleigh.get_reflectance() 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 | #!/usr/bin/env python |
||
| 173 | def get_reflectance(self, sun_zenith, sat_zenith, azidiff, bandname, |
||
| 174 | redband=None): |
||
| 175 | """Get the reflectance from the three sun-sat angles.""" |
||
| 176 | # Get wavelength in nm for band: |
||
| 177 | wvl = self.get_effective_wavelength(bandname) * 1000.0 |
||
| 178 | rayl, wvl_coord, azid_coord, satz_sec_coord, sunz_sec_coord = \ |
||
| 179 | self.get_reflectance_lut() |
||
| 180 | |||
| 181 | # force dask arrays |
||
| 182 | compute = False |
||
| 183 | if HAVE_DASK and not isinstance(sun_zenith, Array): |
||
| 184 | print("NO") |
||
| 185 | compute = True |
||
| 186 | sun_zenith = from_array(sun_zenith, chunks=sun_zenith.shape) |
||
| 187 | sat_zenith = from_array(sat_zenith, chunks=sat_zenith.shape) |
||
| 188 | azidiff = from_array(azidiff, chunks=azidiff.shape) |
||
| 189 | if redband is not None: |
||
| 190 | redband = from_array(redband, chunks=redband.shape) |
||
| 191 | |||
| 192 | clip_angle = np.rad2deg(np.arccos(1. / sunz_sec_coord.max())) |
||
| 193 | sun_zenith = clip(sun_zenith, 0, clip_angle) |
||
| 194 | sunzsec = 1. / np.cos(np.deg2rad(sun_zenith)) |
||
| 195 | clip_angle = np.rad2deg(np.arccos(1. / satz_sec_coord.max())) |
||
| 196 | sat_zenith = clip(sat_zenith, 0, clip_angle) |
||
| 197 | satzsec = 1. / np.cos(np.deg2rad(sat_zenith)) |
||
| 198 | |||
| 199 | shape = sun_zenith.shape |
||
| 200 | |||
| 201 | if not(wvl_coord.min() < wvl < wvl_coord.max()): |
||
| 202 | LOG.warning( |
||
| 203 | "Effective wavelength for band %s outside 400-800 nm range!", |
||
| 204 | str(bandname)) |
||
| 205 | LOG.info( |
||
| 206 | "Set the rayleigh/aerosol reflectance contribution to zero!") |
||
| 207 | if HAVE_DASK: |
||
| 208 | chunks = sun_zenith.chunks if redband is None else redband.chunks |
||
| 209 | res = zeros(shape, chunks=chunks) |
||
| 210 | return res.compute() if compute else res |
||
| 211 | else: |
||
| 212 | return zeros(shape) |
||
| 213 | |||
| 214 | idx = np.searchsorted(wvl_coord, wvl) |
||
| 215 | wvl1 = wvl_coord[idx - 1] |
||
| 216 | wvl2 = wvl_coord[idx] |
||
| 217 | |||
| 218 | fac = (wvl2 - wvl) / (wvl2 - wvl1) |
||
| 219 | raylwvl = fac * rayl[idx - 1, :, :, :] + (1 - fac) * rayl[idx, :, :, :] |
||
| 220 | tic = time.time() |
||
| 221 | |||
| 222 | smin = [sunz_sec_coord[0], azid_coord[0], satz_sec_coord[0]] |
||
| 223 | smax = [sunz_sec_coord[-1], azid_coord[-1], satz_sec_coord[-1]] |
||
| 224 | orders = [ |
||
| 225 | len(sunz_sec_coord), len(azid_coord), len(satz_sec_coord)] |
||
| 226 | minterp = MultilinearInterpolator(smin, smax, orders) |
||
| 227 | |||
| 228 | f_3d_grid = raylwvl |
||
| 229 | minterp.set_values(np.atleast_2d(f_3d_grid.ravel())) |
||
| 230 | |||
| 231 | def _do_interp(minterp, sunzsec, azidiff, satzsec): |
||
| 232 | print(sunzsec.shape, sunzsec.size) |
||
| 233 | print(azidiff.shape, azidiff.size) |
||
| 234 | print(satzsec.shape, satzsec.size) |
||
| 235 | interp_points2 = np.vstack((sunzsec.ravel(), |
||
| 236 | 180 - azidiff.ravel(), |
||
| 237 | satzsec.ravel())) |
||
| 238 | res = minterp(interp_points2) |
||
| 239 | return res.reshape(sunzsec.shape) |
||
| 240 | |||
| 241 | if HAVE_DASK: |
||
| 242 | print("About to map blocks") |
||
| 243 | print(sunzsec.shape, sunzsec.size, sunzsec.chunks) |
||
| 244 | print(azidiff.shape, azidiff.size, azidiff.chunks) |
||
| 245 | print(satzsec.shape, satzsec.size, satzsec.chunks) |
||
| 246 | ipn = map_blocks(_do_interp, minterp, sunzsec, azidiff, |
||
| 247 | satzsec, dtype=raylwvl.dtype, |
||
| 248 | chunks=azidiff.chunks) |
||
| 249 | else: |
||
| 250 | ipn = _do_interp(minterp, sunzsec, azidiff, satzsec) |
||
| 251 | |||
| 252 | LOG.debug("Time - Interpolation: {0:f}".format(time.time() - tic)) |
||
| 253 | |||
| 254 | ipn *= 100 |
||
| 255 | res = ipn |
||
| 256 | if redband is not None: |
||
| 257 | res = where(redband < 20., res, |
||
| 258 | (1 - (redband - 20) / 80) * res) |
||
| 259 | |||
| 260 | res = clip(res, 0, 100) |
||
| 261 | if compute: |
||
| 262 | res = res.compute() |
||
| 263 | return res |
||
| 264 | |||
| 308 |