<commit_before>package physical.flow

import com.github.nscala_time.time.Imports._
import grizzled.slf4j.Logging
import io.FlowReader
import locals.Constants
import maths.RandomNumberGenerator
import maths.interpolation.Interpolator
import physical.{GeoCoordinate, Velocity}

import scala.collection.mutable


class FlowController(var flow: Flow, val randomNumbers: RandomNumberGenerator) extends Logging {

  val SizeOfQueue = 2
  val flowDataQueue = mutable.Queue.empty[Array[FlowPolygon]]
  val interpolator = new Interpolator(flow.dimensions)


  def getVelocityOfCoordinate(coordinate: GeoCoordinate, future: DateTime, now: DateTime, timeStep: Int): Velocity = {
    require(future >= now, "flow field information for this period is not loaded into memory")

    val currentVelocity = getVelocityOfCoordinate(coordinate, isFuture = false)
    val futureVelocity = getVelocityOfCoordinate(coordinate, isFuture = true)

    if (future == now) return currentVelocity
    if (future == now.plusSeconds(timeStep)) return futureVelocity
    if (future > now && future < now.plusSeconds(timeStep)) {

      val period: Duration = new Duration(future, now)
      val divisor = 1 / timeStep.toDouble
      val ratioA = period.toStandardSeconds.getSeconds
      val ratioB = timeStep - ratioA
      return currentVelocity * (ratioA * divisor) + futureVelocity * (ratioB * divisor)
    }
    new Velocity
  }

  /*  def getVelocityOfCoordinate(coordinate: GeoCoordinate, isFuture: Boolean): Velocity = {

      var index: Int = 0
      var bumped: Boolean = false
      var bumpedCoordinate: GeoCoordinate = new GeoCoordinate()
      //var coord = coordinate

      val flowPolygons: Array[FlowPolygon] = if (isFuture) flowDataQueue.last else flowDataQueue.head
      //logger.debug("Flow polygons is size " + flowPolygons.length)
      //logger.debug("Coordinate is " + coordinate)

      try {
        index = getIndexOfPolygon(coordinate)
        debug("Found the index " + index)
        //logger.debug("flow lr")
        //val polygon = flowPolygons(index)
      } catch {
        case e: IllegalArgumentException =>
          warn("Have to bump the coordinate " + coordinate)
          bumpedCoordinate = bumpCoordinate(coordinate)

          try {
            index = getIndexOfPolygon(bumpedCoordinate)
            bumped = true
          } catch {
            case e: IllegalArgumentException =>
              error("The coordinate " + bumpedCoordinate + " is not in the flow field")
              return new Velocity()
          }
      }
      debug("The velocity is " + flowPolygons(index).velocity)
      if (!flowPolygons(index).velocity.isUndefined) {
        if (bumped) {
          interpolator.interpolate(bumpedCoordinate, flowPolygons, index)
        } else {
          interpolator.interpolate(coordinate, flowPolygons, index)
        }
      } else {
        new Velocity()
      }

    }*/

  def getVelocityOfCoordinate(coordinate: GeoCoordinate, isFuture: Boolean): Velocity = {
    var index: Int = 0
    var bumped: Boolean = false
    var velocity: Velocity = new Velocity()
    //var bumpedCoordinate: GeoCoordinate = new GeoCoordinate()

    val flowPolygons: Array[FlowPolygon] = if (isFuture) flowDataQueue.last else flowDataQueue.head
    val polygonIndex = getIndexOfPolygon(coordinate)
    val velocityAtCentroid = flowPolygons(polygonIndex).velocity
    if (index == Constants.LightWeightException.CoordinateNotFoundException) {
      val bumpedCoordinate = bumpCoordinate(coordinate)
      index = getIndexOfPolygon(bumpedCoordinate)
      velocity = interpolator.interpolate(bumpedCoordinate, flowPolygons, index)
    } else {
      velocity = interpolator.interpolate(coordinate, flowPolygons, index)
    }

    if (velocity.isUndefined) velocityAtCentroid else velocity

  }

  def getIndexOfPolygon(coordinate: GeoCoordinate): Int = {
    debug("The flow: " + flow.dimensions.latitudeBoundary)
    require(flow.dimensions.latitudeBoundary.contains(coordinate.latitude) && flow.dimensions.longitudeBoundary.contains(coordinate.longitude))

    val lat1 = correctNegativeCoordinate(coordinate.latitude)
    val lon1 = correctNegativeCoordinate(coordinate.longitude)
    val lat2 = correctNegativeCoordinate(flow.dimensions.latitudeBoundary.start)
    val lon2 = correctNegativeCoordinate(flow.dimensions.longitudeBoundary.start)

    val x = ((lat1 - lat2) / flow.dimensions.cellSize.cell.width).toInt + 1
    val y = ((lon1 - lon2) / flow.dimensions.cellSize.cell.width).toInt + 1
    val z = ensureDepthIsInRange((coordinate.depth / flow.dimensions.cellSize.cell.depth).toInt)

    flow.dimensions.cellSize.width * x + y + z * flow.dimensions.cellSize.layerCellCount
  }

  def correctNegativeCoordinate(value: Double): Double = {
    if (value < 0) value + 180 else value
  }

  def ensureDepthIsInRange(value: Int): Int = {
    if (value < 0) 0 else if (value > flow.dimensions.cellSize.depth) flow.dimensions.cellSize.depth else value
  }
<commit_msg>Fixed the index typo<commit_after>package physical.flow

import com.github.nscala_time.time.Imports._
import grizzled.slf4j.Logging
import io.FlowReader
import locals.Constants
import maths.RandomNumberGenerator
import maths.interpolation.Interpolator
import physical.{GeoCoordinate, Velocity}

import scala.collection.mutable


class FlowController(var flow: Flow, val randomNumbers: RandomNumberGenerator) extends Logging {

  val SizeOfQueue = 2
  val flowDataQueue = mutable.Queue.empty[Array[FlowPolygon]]
  val interpolator = new Interpolator(flow.dimensions)


  def getVelocityOfCoordinate(coordinate: GeoCoordinate, future: DateTime, now: DateTime, timeStep: Int): Velocity = {
    require(future >= now, "flow field information for this period is not loaded into memory")

    val currentVelocity = getVelocityOfCoordinate(coordinate, isFuture = false)
    val futureVelocity = getVelocityOfCoordinate(coordinate, isFuture = true)

    if (future == now) return currentVelocity
    if (future == now.plusSeconds(timeStep)) return futureVelocity
    if (future > now && future < now.plusSeconds(timeStep)) {

      val period: Duration = new Duration(future, now)
      val divisor = 1 / timeStep.toDouble
      val ratioA = period.toStandardSeconds.getSeconds
      val ratioB = timeStep - ratioA
      return currentVelocity * (ratioA * divisor) + futureVelocity * (ratioB * divisor)
    }
    new Velocity
  }

  /*  def getVelocityOfCoordinate(coordinate: GeoCoordinate, isFuture: Boolean): Velocity = {

      var index: Int = 0
      var bumped: Boolean = false
      var bumpedCoordinate: GeoCoordinate = new GeoCoordinate()
      //var coord = coordinate

      val flowPolygons: Array[FlowPolygon] = if (isFuture) flowDataQueue.last else flowDataQueue.head
      //logger.debug("Flow polygons is size " + flowPolygons.length)
      //logger.debug("Coordinate is " + coordinate)

      try {
        index = getIndexOfPolygon(coordinate)
        debug("Found the index " + index)
        //logger.debug("flow lr")
        //val polygon = flowPolygons(index)
      } catch {
        case e: IllegalArgumentException =>
          warn("Have to bump the coordinate " + coordinate)
          bumpedCoordinate = bumpCoordinate(coordinate)

          try {
            index = getIndexOfPolygon(bumpedCoordinate)
            bumped = true
          } catch {
            case e: IllegalArgumentException =>
              error("The coordinate " + bumpedCoordinate + " is not in the flow field")
              return new Velocity()
          }
      }
      debug("The velocity is " + flowPolygons(index).velocity)
      if (!flowPolygons(index).velocity.isUndefined) {
        if (bumped) {
          interpolator.interpolate(bumpedCoordinate, flowPolygons, index)
        } else {
          interpolator.interpolate(coordinate, flowPolygons, index)
        }
      } else {
        new Velocity()
      }

    }*/

  def getVelocityOfCoordinate(coordinate: GeoCoordinate, isFuture: Boolean): Velocity = {
    // var index: Int = 0
    var bumped: Boolean = false
    var velocity: Velocity = new Velocity()
    //var bumpedCoordinate: GeoCoordinate = new GeoCoordinate()

    val flowPolygons: Array[FlowPolygon] = if (isFuture) flowDataQueue.last else flowDataQueue.head
    var index = getIndexOfPolygon(coordinate)
    val velocityAtCentroid = flowPolygons(index).velocity
    if (index == Constants.LightWeightException.CoordinateNotFoundException) {
      val bumpedCoordinate = bumpCoordinate(coordinate)
      index = getIndexOfPolygon(bumpedCoordinate)
      velocity = interpolator.interpolate(bumpedCoordinate, flowPolygons, index)
    } else {
      velocity = interpolator.interpolate(coordinate, flowPolygons, index)
    }

    if (velocity.isUndefined) velocityAtCentroid else velocity

  }

  def getIndexOfPolygon(coordinate: GeoCoordinate): Int = {
    if (flow.dimensions.latitudeBoundary.contains(coordinate.latitude) && flow.dimensions.longitudeBoundary.contains(coordinate.longitude)) {

      val lat1 = correctNegativeCoordinate(coordinate.latitude)
      val lon1 = correctNegativeCoordinate(coordinate.longitude)
      val lat2 = correctNegativeCoordinate(flow.dimensions.latitudeBoundary.start)
      val lon2 = correctNegativeCoordinate(flow.dimensions.longitudeBoundary.start)

      val x = ((lat1 - lat2) / flow.dimensions.cellSize.cell.width).toInt + 1
      val y = ((lon1 - lon2) / flow.dimensions.cellSize.cell.width).toInt + 1
      val z = ensureDepthIsInRange((coordinate.depth / flow.dimensions.cellSize.cell.depth).toInt)

      flow.dimensions.cellSize.width * x + y + z * flow.dimensions.cellSize.layerCellCount
    } else {
      Constants.LightWeightException.CoordinateNotFoundException
    }
  }

  def correctNegativeCoordinate(value: Double): Double = {
    if (value < 0) value + 180 else value
  }

  def ensureDepthIsInRange(value: Int): Int = {
    if (value < 0) 0 else if (value > flow.dimensions.cellSize.depth) flow.dimensions.cellSize.depth else value
  }

{'fraction_non_alphanumeric': 0.0906821132527714, 'fraction_numerical': 0.00419454708878458, 'mean_word_length': 3.452645620275678, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7583216', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2832, 'n_words': 685}