Conditions | 7 |
Total Lines | 94 |
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 | # Copyright (c) 2016 MetPy Developers. |
||
133 | @exporter.export |
||
134 | def interpolate(x, y, z, interp_type='linear', hres=50000, |
||
135 | minimum_neighbors=3, gamma=0.25, kappa_star=5.052, |
||
136 | search_radius=None, rbf_func='linear', rbf_smooth=0): |
||
137 | r"""Interpolate given (x,y), observation (z) pairs to a grid based on given parameters. |
||
138 | |||
139 | Parameters |
||
140 | ---------- |
||
141 | x: array_like |
||
142 | x coordinate |
||
143 | y: array_like |
||
144 | y coordinate |
||
145 | z: array_like |
||
146 | observation value |
||
147 | interp_type: str |
||
148 | What type of interpolation to use. Available options include: |
||
149 | 1) "linear", "nearest", "cubic", or "rbf" from Scipy.interpolate. |
||
150 | 2) "natural_neighbor", "barnes", or "cressman" from Metpy.mapping . |
||
151 | Default "linear". |
||
152 | hres: float |
||
153 | The horizontal resolution of the generated grid. Default 50000 meters. |
||
154 | minimum_neighbors: int |
||
155 | Minimum number of neighbors needed to perform barnes or cressman interpolation for a |
||
156 | point. Default is 3. |
||
157 | gamma: float |
||
158 | Adjustable smoothing parameter for the barnes interpolation. Default 0.25. |
||
159 | kappa_star: float |
||
160 | Response parameter for barnes interpolation, specified nondimensionally |
||
161 | in terms of the Nyquist. Default 5.052 |
||
162 | search_radius: float |
||
163 | A search radius to use for the barnes and cressman interpolation schemes. |
||
164 | If search_radius is not specified, it will default to the average spacing of |
||
165 | observations. |
||
166 | rbf_func: str |
||
167 | Specifies which function to use for Rbf interpolation. |
||
168 | Options include: 'multiquadric', 'inverse', 'gaussian', 'linear', 'cubic', |
||
169 | 'quintic', and 'thin_plate'. Defualt 'linear'. See scipy.interpolate.Rbf for more |
||
170 | information. |
||
171 | rbf_smooth: float |
||
172 | Smoothing value applied to rbf interpolation. Higher values result in more smoothing. |
||
173 | |||
174 | Returns |
||
175 | ------- |
||
176 | grid_x: (N, 2) ndarray |
||
177 | Meshgrid for the resulting interpolation in the x dimension |
||
178 | grid_y: (N, 2) ndarray |
||
179 | Meshgrid for the resulting interpolation in the y dimension ndarray |
||
180 | img: (M, N) ndarray |
||
181 | 2-dimensional array representing the interpolated values for each grid. |
||
182 | |||
183 | """ |
||
184 | grid_x, grid_y = points.generate_grid(hres, points.get_boundary_coords(x, y)) |
||
185 | |||
186 | if interp_type in ['linear', 'nearest', 'cubic']: |
||
187 | points_zip = np.array(list(zip(x, y))) |
||
188 | img = griddata(points_zip, z, (grid_x, grid_y), method=interp_type) |
||
189 | |||
190 | elif interp_type == 'natural_neighbor': |
||
191 | img = interpolation.natural_neighbor(x, y, z, grid_x, grid_y) |
||
192 | |||
193 | elif interp_type in ['cressman', 'barnes']: |
||
194 | ave_spacing = np.mean((cdist(list(zip(x, y)), list(zip(x, y))))) |
||
195 | |||
196 | if search_radius is None: |
||
197 | search_radius = ave_spacing |
||
198 | |||
199 | if interp_type == 'cressman': |
||
200 | img = interpolation.inverse_distance(x, y, z, grid_x, grid_y, search_radius, |
||
201 | min_neighbors=minimum_neighbors, |
||
202 | kind=interp_type) |
||
203 | else: |
||
204 | kappa = calc_kappa(ave_spacing, kappa_star) |
||
205 | img = interpolation.inverse_distance(x, y, z, grid_x, grid_y, search_radius, |
||
206 | gamma, kappa, min_neighbors=minimum_neighbors, |
||
207 | kind=interp_type) |
||
208 | |||
209 | elif interp_type == 'rbf': |
||
210 | # 3-dimensional support not yet included. |
||
211 | # Assign a zero to each z dimension for observations. |
||
212 | h = np.zeros((len(x))) |
||
213 | |||
214 | rbfi = Rbf(x, y, h, z, function=rbf_func, smooth=rbf_smooth) |
||
215 | |||
216 | # 3-dimensional support not yet included. |
||
217 | # Assign a zero to each z dimension grid cell position. |
||
218 | hi = np.zeros(grid_x.shape) |
||
219 | img = rbfi(grid_x, grid_y, hi) |
||
220 | |||
221 | else: |
||
222 | raise ValueError('Interpolation option not available. ' |
||
223 | 'Try: linear, nearest, cubic, natural_neighbor, ' |
||
224 | 'barnes, cressman, rbf') |
||
225 | |||
226 | return grid_x, grid_y, img |
||
227 |