Skip to content

Commit

Permalink
Implement get_value, so analysis branch and this branch can be merged…
Browse files Browse the repository at this point in the history
… to master
  • Loading branch information
PaulKGrimes committed Jun 16, 2020
1 parent 75152ae commit 0eec23c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/graspfile/grid.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""This is the module for manipulating grid files containing one or more field cuts from TICRA Tools, GRASP and CHAMP
"""

import configparser
try:
import configparser
except ImportError:
import ConfigParser as configparser

import graspfile.numpy_utilities as numpy_utilities
import numpy


Expand Down Expand Up @@ -134,6 +138,21 @@ def positions_1d(self):
return (numpy.linspace(self.grid_min_x, self.grid_max_x, self.grid_n_x),
numpy.linspace(self.grid_min_y, self.grid_max_y, self.grid_n_y))

def get_value(self, xv, yv):
"""Return the value of the field at the nearest point to xv, yv
Args:
xv: float containing the x coordinate of the point to get.
yv: float containing the y coordinate of the point to get.
Returns:
ndarray: containing self.field_components values of the field at xv, yv"""
x_vals, y_vals = self.positions_1d

nx = numpy_utilities.find_nearest_idx(x_vals, xv)
ny = numpy_utilities.find_nearest_idx(y_vals, yv)

return self.field[nx, ny, :]

def radius_grid(self, center=None):
"""Return an array holding the radii of each point from the beam centre.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_analysis_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ def filled_grasp_field(filled_grasp_grid):
return filled_grasp_grid.fields[0]


def test_get_value(filled_grasp_field):
"""Test getting a value from the field"""
x_vals, y_vals = filled_grasp_field.positions_1d

# Test values of xv, yv in x_vals, y_vals
xv = x_vals[int(len(x_vals)/3)]
yv = y_vals[int(2*len(y_vals)/3)]

value = filled_grasp_field.get_value(xv, yv)

assert len(value) == filled_grasp_field.field_components

# Test values of xv, yv not in x_vals, y_vals
xv = (x_vals[int(len(x_vals)/3)] + x_vals[int(len(x_vals)/3)+1])/2
yv = (y_vals[int(2*len(y_vals)/3)] + y_vals[int(2*len(y_vals)/3)-1])/2

value = filled_grasp_field.get_value(xv, yv)

assert len(value) == filled_grasp_field.field_components


def test_finding_peak(filled_grasp_field):
"""Test find the peak of a grasp field"""
x_peak, y_peak = ga.find_peak(filled_grasp_field)
Expand Down

0 comments on commit 0eec23c

Please sign in to comment.