Code Duplication    Length = 24-28 lines in 2 locations

metpy/calc/tools.py 2 locations

@@ 390-417 (lines=28) @@
387
    above = broadcast_indicies(xp, minv2, ndim, axis)
388
    below = broadcast_indicies(xp, minv2 - 1, ndim, axis)
389
390
    # Calculate interpolation for each variable
391
    for i in range(len(args)):
392
        var = variables[i][below] + ((log_x_array - log_xp[below]) /
393
                                     (log_xp[above] - log_xp[below])) * (variables[i][above] -
394
                                                                         variables[i][below])
395
396
        # set to nan if extrap=set_nan
397
        if extrap == 'set_nan':
398
            var[minv == xp.shape[axis]] = np.nan
399
            var[minv == 0] = np.nan
400
401
        # Output to list
402
        ret.append(var)
403
    if len(ret) == 1:
404
        return ret[0]
405
    else:
406
        return ret
407
408
409
def broadcast_indicies(x, minv, ndim, axis):
410
    """Calculate index values to properly broadcast index array within data array.
411
412
    See usage in log_interp.
413
    """
414
    ret = []
415
    for dim in range(ndim):
416
        if dim == axis:
417
            ret.append(minv)
418
        else:
419
            broadcast_slice = [np.newaxis] * ndim
420
            broadcast_slice[dim] = slice(None)
@@ 364-387 (lines=24) @@
361
362
    # Log x and xp
363
    log_x_array = np.log(x_array)
364
    log_xp = np.log(xp[sorter])
365
366
    # Calculate value above interpolated value
367
    minv = np.apply_along_axis(np.searchsorted, axis, xp[sorter], x[sort_x])
368
    minv2 = np.copy(minv)
369
370
    # If extrap is none and data is out of bounds, raise value error
371
    if ((np.max(minv) == xp.shape[axis]) or (np.min(minv) == 0)) and extrap == 'none':
372
        raise ValueError('Interpolation point out of data bounds encountered')
373
374
    # Warn if interpolated values are outside data bounds, will make these the values
375
    # at end of data range.
376
    if np.max(minv) == xp.shape[axis]:
377
        warnings.warn('Interpolation point out of data bounds encountered')
378
        minv2[minv == xp.shape[axis]] = xp.shape[axis] - 1
379
    if np.max(minv) == 0:
380
        warnings.warn('Interpolation point out of data bounds encountered')
381
        minv2[minv == 0] = 1
382
383
    # Create empty output list
384
    ret = []
385
386
    # Get indicies for broadcasting arrays
387
    above = broadcast_indicies(xp, minv2, ndim, axis)
388
    below = broadcast_indicies(xp, minv2 - 1, ndim, axis)
389
390
    # Calculate interpolation for each variable