| Conditions | 11 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 planck() 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 |
||
| 117 | def planck(wave, temp, wavelength=True): |
||
| 118 | """The Planck radiation or Blackbody radiation as a function of wavelength |
||
| 119 | or wavenumber. SI units. |
||
| 120 | _planck(wave, temperature, wavelength=True) |
||
| 121 | wave = Wavelength/wavenumber or a sequence of wavelengths/wavenumbers (m or m^-1) |
||
| 122 | temp = Temperature (scalar) or a sequence of temperatures (K) |
||
| 123 | |||
| 124 | |||
| 125 | Output: Wavelength space: The spectral radiance per meter (not micron!) |
||
| 126 | Unit = W/m^2 sr^-1 m^-1 |
||
| 127 | |||
| 128 | Wavenumber space: The spectral radiance in Watts per square meter |
||
| 129 | per steradian per m-1: |
||
| 130 | Unit = W/m^2 sr^-1 (m^-1)^-1 = W/m sr^-1 |
||
| 131 | |||
| 132 | Converting from SI units to mW/m^2 sr^-1 (cm^-1)^-1: |
||
| 133 | 1.0 W/m^2 sr^-1 (m^-1)^-1 = 0.1 mW/m^2 sr^-1 (cm^-1)^-1 |
||
| 134 | |||
| 135 | """ |
||
| 136 | units = ['wavelengths', 'wavenumbers'] |
||
| 137 | if wavelength: |
||
| 138 | LOG.debug("Using {0} when calculating the Blackbody radiance".format( |
||
| 139 | units[(wavelength == True) - 1])) |
||
| 140 | |||
| 141 | if np.isscalar(temp): |
||
| 142 | temperature = np.array([temp, ], dtype='float64') |
||
| 143 | else: |
||
| 144 | temperature = np.array(temp, dtype='float64') |
||
| 145 | |||
| 146 | shape = temperature.shape |
||
| 147 | if np.isscalar(wave): |
||
| 148 | wln = np.array([wave, ], dtype='float64') |
||
| 149 | else: |
||
| 150 | wln = np.array(wave, dtype='float64') |
||
| 151 | |||
| 152 | if wavelength: |
||
| 153 | const = 2 * H_PLANCK * C_SPEED ** 2 |
||
| 154 | nom = const / wln ** 5 |
||
| 155 | arg1 = H_PLANCK * C_SPEED / (K_BOLTZMANN * wln) |
||
| 156 | else: |
||
| 157 | nom = 2 * H_PLANCK * (C_SPEED ** 2) * (wln ** 3) |
||
| 158 | arg1 = H_PLANCK * C_SPEED * wln / K_BOLTZMANN |
||
| 159 | |||
| 160 | arg2 = np.where(np.greater(np.abs(temperature), EPSILON), |
||
| 161 | np.array(1. / temperature), -9).reshape(-1, 1) |
||
| 162 | arg2 = np.ma.masked_array(arg2, mask=arg2 == -9) |
||
| 163 | LOG.debug("Max and min - arg1: %s %s", str(arg1.max()), str(arg1.min())) |
||
| 164 | LOG.debug("Max and min - arg2: %s %s", str(arg2.max()), str(arg2.min())) |
||
| 165 | try: |
||
| 166 | exp_arg = np.multiply(arg1.astype('float32'), arg2.astype('float32')) |
||
| 167 | except MemoryError: |
||
| 168 | LOG.warning(("Dimensions used in numpy.multiply probably reached " |
||
| 169 | "limit!\n" |
||
| 170 | "Make sure the Radiance<->Tb table has been created " |
||
| 171 | "and try running again")) |
||
| 172 | raise |
||
| 173 | |||
| 174 | LOG.debug("Max and min before exp: %s %s", str(exp_arg.max()), |
||
| 175 | str(exp_arg.min())) |
||
| 176 | if exp_arg.min() < 0: |
||
| 177 | LOG.warning("Something is fishy: \n" + |
||
| 178 | "\tDenominator might be zero or negative in radiance derivation:") |
||
| 179 | dubious = np.where(exp_arg < 0)[0] |
||
| 180 | LOG.warning( |
||
| 181 | "Number of items having dubious values: " + str(dubious.shape[0])) |
||
| 182 | |||
| 183 | denom = np.exp(exp_arg) - 1 |
||
| 184 | rad = nom / denom |
||
| 185 | radshape = rad.shape |
||
| 186 | if wln.shape[0] == 1: |
||
| 187 | if temperature.shape[0] == 1: |
||
| 188 | return rad[0, 0] |
||
| 189 | else: |
||
| 190 | return rad[:, 0].reshape(shape) |
||
| 191 | else: |
||
| 192 | if temperature.shape[0] == 1: |
||
| 193 | return rad[0, :] |
||
| 194 | else: |
||
| 195 | if len(shape) == 1: |
||
| 196 | return np.reshape(rad, (shape[0], radshape[1])) |
||
| 197 | else: |
||
| 198 | return np.reshape(rad, (shape[0], shape[1], radshape[1])) |
||
| 199 | |||
| 233 |