Find the position where the value is half of the peak
771007
def position_half_h(pslit, cpix, backw=4):
    """"""

    # Find the first peak to the right of cpix
    next_peak = simple_prot(pslit, cpix)

    if next_peak is None:
        raise ValueError

    dis_peak = next_peak - cpix

    wpos2 = cpix - dis_peak
    wpos1 = wpos2 - backw

    # Compute background in a window of width backw
    # in a position simetrical to the peak
    # around cpix
    left_background = pslit[wpos1:wpos2].min()

    # height of the peak
    height = pslit[next_peak] - left_background

    half_height = left_background + 0.5 * height

    # Position at halg peak, linear interpolation
    vv = pslit[wpos1:next_peak+1] - half_height

    res1, =  numpy.nonzero(numpy.diff(vv > 0))
    i1 = res1[0]

    xint = wpos1 + i1 + (0 - vv[i1]) / (vv[i1+1] - vv[i1])

    return xint, next_peak, wpos1, wpos2, left_background, half_height
530933
function calculateStopPositions(stop1, stop2, count) {
    var good = function (values, unit) {
        return { values: values, unit: unit };
    };
    var bad = function (warning) {
        return { values: false, unit: '', warning: warning };
    };

    // Exit early if either value is calc()
    if (stop1 && isCalc(stop1.positionNode) || stop2 && isCalc(stop2.positionNode)) {
        return bad(errorStopPosition);
    }

    var startPos, endPos, baseUnit;
    var hasStartPos = true;
    var hasEndPos = true;

    var pos1 = stop1 && unitValue(stop1.positionNode);
    var pos2 = stop2 && unitValue(stop2.positionNode);
    if (!pos1) {
        hasStartPos = false;
        pos1 = { number: '0', unit: '%' };
    } else {
        count++;
    }
    if (!pos2) {
        hasEndPos = false;
        pos2 = { number: '100', unit: '%' };
    } else {
        count++;
    }

    // Check if missing stops can be calculated
    if (pos1.unit !== pos2.unit) {
        return bad(errorStopPosition);
    }
    startPos = +pos1.number || 0;
    endPos = +pos2.number || 0;
    baseUnit = pos1.unit;

    // Generate as many missing positions as required
    var positions = midRange(startPos, endPos, count);
    // Take off any known positions
    if (hasStartPos) {
        positions.shift();
    }
    if (hasEndPos) {
        positions.pop();
    }

    return good(positions, baseUnit);
}
501036
def _at(self, t):
        """"""
        pos, vel = terra(self.latitude.radians, self.longitude.radians,
                         self.elevation.au, t.gast)
        pos = einsum('ij...,j...->i...', t.MT, pos)
        vel = einsum('ij...,j...->i...', t.MT, vel)
        if self.x:
            R = rot_y(self.x * ASEC2RAD)
            pos = einsum('ij...,j...->i...', R, pos)
        if self.y:
            R = rot_x(self.y * ASEC2RAD)
            pos = einsum('ij...,j...->i...', R, pos)
        # TODO: also rotate velocity

        return pos, vel, pos, None
663440
def _findNearest(arr, value):
    
    arr = np.array(arr)
    # find nearest value in array
    idx = (abs(arr-value)).argmin()
    return arr[idx]
1163015
def find_gt(a, x):
    """"""
    i = bs.bisect_right(a, x)
    if i != len(a): return i
    raise ValueError
504185
function locationOf(element, array, start, end) {
  start = start || 0;
  end = end || array.length;
  var pivot = parseInt(start + (end - start) / 2, 10);
  if (end-start <= 1 || array[pivot] === element) return pivot;
  if (array[pivot] < element) {
    return locationOf(element, array, pivot, end);
  } else {
    return locationOf(element, array, start, pivot);
  }
}
234160
def midpoint(self) -> float:
        
        return (self.bid + self.ask) / 2 if self.hasBidAsk() else nan
1118620
def findLowerElevation(self, source, world):
        ''''''
        x, y = source
        currentRadius = 1
        maxRadius = 40
        lowestElevation = world.layers['elevation'].data[y, x]
        destination = []
        notFound = True
        isWrapped = False
        wrapped = []

        while notFound and currentRadius <= maxRadius:
            for cx in range(-currentRadius, currentRadius + 1):
                for cy in range(-currentRadius, currentRadius + 1):
                    rx, ry = x + cx, y + cy

                    # are we within bounds?
                    if not self.wrap and not world.contains((rx, ry)):
                        continue

                    # are we within a circle?
                    if not in_circle(currentRadius, x, y, rx, ry):
                        continue

                    rx, ry = overflow(rx, world.width), overflow(ry,
                                                                 world.height)

                    # if utilities.outOfBounds([x+cx, y+cy], self.size):
                    #                        print "Fixed:",x ,y,  rx, ry

                    elevation = world.layers['elevation'].data[ry, rx]
                    # have we found a lower elevation?
                    if elevation < lowestElevation:
                        lowestElevation = elevation
                        destination = [rx, ry]
                        notFound = False
                        if not world.contains((x + cx, y + cy)):
                            wrapped.append(destination)

            currentRadius += 1

        if destination in wrapped:
            isWrapped = True
        # print "Wrapped lower elevation found:", rx, ry, "!"
        return isWrapped, destination
170153
private long findAfter(long offset) {
    if (size == 0) {
      return -1;
    }

    int low  = 0;
    int high = size-1;

    while (low <= high) {
      int mid = low + ((high - low) / 2);

      long i = buffer.readLong(mid * ENTRY_SIZE);
      if (i < offset) {
        low = mid + 1;
      } else if (i > offset) {
        high = mid - 1;
      } else {
        return mid;
      }
    }

    return (low < high) ? low + 1 : high + 1;
  }
296393
def getTopLeft(self):
        
        return (float(self.get_x()), float(self.get_y())+ float(self.get_height()))
572769
def getAltitudeAtPoint(self, latty, longy):
        ''''''
        #check the bounds
        if self.startlongitude > 0 and (longy < self.startlongitude or longy > self.endlongitude):
            return -1
        if self.startlongitude < 0 and (longy > self.startlongitude or longy < self.endlongitude):
            return -1
        if self.startlatitude > 0 and (latty < self.startlatitude or longy > self.endlatitude):
            return -1
        if self.startlatitude < 0 and (latty > self.startlatitude or longy < self.endlatitude):
            return -1

        x = numpy.abs((latty - self.startlatitude)/self.deltalatitude)
        y = numpy.abs((longy - self.startlongitude)/self.deltalongitude)

        #do some interpolation
        # print("x,y", x, y)
        x_int = int(x)
        x_frac = x - int(x)
        y_int = int(y)
        y_frac = y - int(y)
        #print("frac", x_int, x_frac, y_int, y_frac)
        value00 = self.data[x_int, y_int]
        value10 = self.data[x_int+1, y_int]
        value01 = self.data[x_int, y_int+1]
        value11 = self.data[x_int+1, y_int+1]
        #print("values ", value00, value10, value01, value11)

        #check for null values
        if value00 == -99999:
            value00 = 0
        if value10 == -99999:
            value10 = 0
        if value01 == -99999:
            value01 = 0
        if value11 == -99999:
            value11 = 0

        value1 = self._avg(value00, value10, x_frac)
        value2 = self._avg(value01, value11, x_frac)
        value  = self._avg(value1,  value2, y_frac)

        return value
437973
def near_to_position(self, position, max_distance):
        ''''''
        end = self.ref_end_pos()
        return self.POS <= position <= end or abs(position - self.POS) <= max_distance or abs(position - end) <= max_distance
744806
def peaks(samples):
    
    max_index = numpy.argmax(samples)
    max_value = samples[max_index]

    min_index = numpy.argmin(samples)
    min_value = samples[min_index]

    if min_index < max_index:
        return (min_value, max_value)
    else:
        return (max_value, min_value)
1351087
function findClosestPointIndexWithinRadius(points, x, y, radiusThreshold) {
    let closestIdx;
    let closestDist = Number.MAX_VALUE;
    points.forEach((d, idx) => {
      let distSqrd = Math.pow(d.x - x, 2) + Math.pow(d.y - y, 2); // changeto: (d.x - x)**2 + (d.y - y)**2;
      if (distSqrd < closestDist && distSqrd < radiusThreshold) {
        closestIdx = idx;
        closestDist = distSqrd;
      }
    });
    return closestIdx;
  }
1113587
def find_range(self, interval):
        """"""
        return self.find(self.tree, interval, self.start, self.end)
32933
def semimajor(P,mstar=1):
    
    return ((P*DAY/2/np.pi)**2*G*mstar*MSUN)**(1./3)/AU
1371554
function (a, b) {
            if (!b.pos || !a.pos) {
                return (a.pos ? -Infinity : Infinity);
            }
            var result = b.pos.z - a.pos.z;
            return (result ? result : (b.pos.y - a.pos.y));
        }