Class ScannableUtils

java.lang.Object
gda.device.scannable.ScannableUtils

public final class ScannableUtils extends Object
A collection of tools for Scannables and packages which use them
  • Method Details

    • getHTMLFormattedCurrentPosition

      public static String getHTMLFormattedCurrentPosition(Scannable scannable) throws DeviceException
      Returns the current position of the Scannable as an HTML string. This is mainly used in gda.gui.utils.WatchPanel.

      Use this for pretty-printing Scannables.

      Parameters:
      scannable -
      Returns:
      string pretty representation of this scannable's position
      Throws:
      DeviceException
    • getFormattedCurrentPosition

      public static String getFormattedCurrentPosition(Scannable scannable) throws DeviceException
      Returns the current position of the Scannable as a string.

      Use this for pretty-printing Scannables.

      Parameters:
      scannable -
      Returns:
      string representation of this Scannable
      Throws:
      DeviceException
    • getFormattedCurrentPosition

      public static String getFormattedCurrentPosition(ScannableMotion scannable) throws DeviceException
      Throws:
      DeviceException
    • getFormattedCurrentPosition

      public static String getFormattedCurrentPosition(ScannableMotionUnits scannable) throws DeviceException
      Throws:
      DeviceException
    • getFormattedCurrentPosition

      public static String getFormattedCurrentPosition(Scannable scannable, String[] unitStringArray, Double[] offsetArray) throws DeviceException
      Returns the current position of the Scannable as a string including units.

      Use this for pretty-printing Scannables.

      Parameters:
      scannable -
      unitStringArray - may be null otherwise must match length of input and extra fields
      Returns:
      string representation of this Scannable
      Throws:
      DeviceException
    • getFormattedPosition

      public static String getFormattedPosition(Object position, Scannable scannable, String[] unitStringArray, Double[] offsetArray) throws DeviceException
      Returns the current position of the Scannable as a string including units.

      Use this for pretty-printing Scannables.

      Parameters:
      position -
      scannable -
      unitStringArray - may be null otherwise must match length of input and extra fields
      offsetArray -
      Returns:
      string representation of this Scannable
      Throws:
      DeviceException
    • getNumDecimalsArray

      public static int[] getNumDecimalsArray(Scannable scannable)
      Returns the number of decimal places for each field of the scannable, as an array
      Parameters:
      scannable - the scannable
      Returns:
      number of decimal places for each field
    • getNumDecimals

      public static int getNumDecimals(String outputFormat)
      Returns the number of decimals specified in the given output format
      Parameters:
      outputFormat -
      Returns:
      number of decimals
    • getFormattedCurrentPositionArray

      public static String[] getFormattedCurrentPositionArray(Object positionObject, int numberOfParts, String[] formats) throws DeviceException
      Breaks currentPositionObj into the required number of parts. For each part if formatString given return String.format(formatString, part) else if String return as as else return toString value
      Parameters:
      positionObject -
      numberOfParts -
      formats -
      Returns:
      array of position values taken from the currentPosition object
      Throws:
      DeviceException
    • getFormattedCurrentPositionArray

      public static String[] getFormattedCurrentPositionArray(Scannable scannable, Object position) throws DeviceException
      Throws:
      DeviceException
    • getFormattedCurrentPositionArray

      public static String[] getFormattedCurrentPositionArray(Scannable scannable) throws DeviceException
      Returns the current position of the Scannable as a formatted array of Strings.

      Use this for pretty-printing Scannables.

      Parameters:
      scannable -
      Returns:
      formatted array of Strings
      Throws:
      DeviceException
    • formatScannableWithChildren

      public static String formatScannableWithChildren(Scannable parent, Collection<Scannable> children, boolean includeParentPosition)
      Formats a scannable which contains child scannables, such as ScannableGroup invalid input: '&' CoupledScannable.
      Parameters:
      parent - Overall scannable
      children - Collection of child scannables
      includeParentPosition - Controls whether position of parent scannable should be included in output
      Returns:
      Name and position of parent and child scannables indented for terminal output.
    • getCurrentPositionArray

      public static double[] getCurrentPositionArray(Scannable scannable) throws DeviceException
      Returns the current position of the given Scannable as an array of doubles
      Parameters:
      scannable -
      Returns:
      the position as an array of doubles
      Throws:
      DeviceException
    • positionToArray

      public static double[] positionToArray(Object currentPositionObj, Scannable scannable) throws DeviceException
      Returns the current position of the given Scannable as an array of doubles. This will not call the scannable's getPosition() method.
      Parameters:
      currentPositionObj -
      scannable -
      Returns:
      the position as an array of doubles
      Throws:
      DeviceException
    • getCurrentPositionArray_InputsOnly

      public static double[] getCurrentPositionArray_InputsOnly(Scannable scannable) throws Exception
      Returns an array which is the current position, but with the 'extra' fields removed
      Parameters:
      scannable -
      Returns:
      the position as an array of doubles
      Throws:
      Exception
    • calculateNextPoint

      public static Object calculateNextPoint(Object previousPoint, Object step)
      Given a current position and an incremental move, calculates the target position.

      This assumes that both objects are of the same type and are one of: number, Java array of numbers, Jython array of numbers

      Parameters:
      previousPoint -
      step -
      Returns:
      the target position of the relative move
    • getNumberSteps

      public static int getNumberSteps(Scannable theScannable, Object start, Object stop, Object step) throws Exception
      Assuming the objects can be converted into doubles, this calculates the number of steps for the given Scannables based on the given start, stop and step values.
      Parameters:
      theScannable - Scannable
      start - Object
      stop - Object
      step - Object
      Returns:
      int
      Throws:
      Exception
    • getNumberSteps

      public static int getNumberSteps(Double startValue, Double stopValue, Double stepValue)
    • validate

      public static void validate(Scannable theScannable) throws ScannableUtils.ScannableValidationException
      Performs some basic validation of the given Scannable to check for internal consistency. This should be called at the end of every Scannable's constructor.

      If the validation fails then an exception with an explanatory message is thrown.

      Parameters:
      theScannable -
      Throws:
      ScannableUtils.ScannableValidationException
    • objectToDouble

      public static Double objectToDouble(Object value)
      Convert an object to a Double
      Number
      🡆 Result of doubleValue()
      String
      🡆 Try parsing as either double or quantity
      Quantity
      🡆 Value component - no unit conversions are made
      PyObject
      🡆 Attempt conversion to number or string and try again
      Object
      🡆 Call toString and try to parse the result
      null
      🡆 null
      If no conversion is possible return null to differentiate the case where "NaN" or Double.NaN is passed in.
      Parameters:
      value - Anything that might be useful as a number
      Returns:
      Some kind of double value - null if no conversion is possible
    • objectToArray

      public static Double[] objectToArray(Object position)
      Converts an object to an array of Doubles.
      If it is an array or list, each value is converted according to objectToDouble(Object) and returned in an array. Otherwise, attempt to convert the object to a single double and return an array of a single value.
      Parameters:
      position - Any object representing a double or sequence of doubles
      Returns:
      array of Doubles
    • toObjectArray

      public static Object[] toObjectArray(Object object)
      Converts an object to an array of Objects.
      If it is an array, or iterable (including PySequence), it is returned as an array. Otherwise, an array is returned with a single value. Unlike objectToArray(Object), this method does not attempt to convert objects to double values, but it does convert Jython objects to their Java equivalents.
      Parameters:
      object - any object to convert
      Returns:
      the object as an array of Java objects
    • convertToJava

      public static Object convertToJava(Object object)
      Parameters:
      object -
      Returns:
      null
      See Also:
    • convertToJava

      public static Object convertToJava(org.python.core.PyObject object)
      Converts a Jython PyObject into its Java equivalent if that is possible. This only works on the sorts of objects dealt with in the Jython environment i.e. Strings, integers, floats (doubles) and arrays of these.

      If this fails or cannot work for any reason then null is returned.

      Parameters:
      object -
      Returns:
      Java equivalent object
    • getScannableNames

      public static List<String> getScannableNames(List<? extends Scannable> scannables)
      Parameters:
      scannables -
      Returns:
      list of Names from the provided list of scannables/detectors
    • getScannableInputFieldNames

      public static List<String> getScannableInputFieldNames(List<Scannable> scannables)
      Parameters:
      scannables -
      Returns:
      list of InputNames from the provided list of scannables
    • getScannableExtraFieldNames

      public static List<String> getScannableExtraFieldNames(List<Scannable> scannables)
      Parameters:
      scannables -
      Returns:
      list of extraNames from the provided list of scannables
    • getScannableFieldNames

      public static List<String> getScannableFieldNames(List<Scannable> scannables)
      Parameters:
      scannables -
      Returns:
      list of input and extraNames from the provided list of scannables
    • getScannableFieldNames

      public static final List<String> getScannableFieldNames(Scannable scannable)
    • getDetectorFieldNames

      public static final List<String> getDetectorFieldNames(List<Detector> detectors)
      Parameters:
      detectors -
      Returns:
      list of names (extra names in case of CounterTimer) from the provided list of detectors
    • getDetectorFieldNames

      public static final List<String> getDetectorFieldNames(Detector detector)
      Parameters:
      detector -
      Returns:
      list of names for the detector (extra names in case of CounterTimer, otherwise just the detector name)
    • getScannablePosition

      public static String getScannablePosition(String name) throws DeviceException
      Call to get the position with unit as a string. The units, if any, will be added on without a space.
      Parameters:
      name -
      Returns:
      String position
      Throws:
      DeviceException
    • isScannableBusy

      public static boolean isScannableBusy(String scannableName)
      Returns of the scannable is busy if it can. Otherwise returns false without an exception being thrown.
      Parameters:
      scannableName -
      Returns:
      true if busy
    • getScannablePositionNoException

      public static String getScannablePositionNoException(String name)
      NOTE: Instead of giving an exception of the EpicsMonitor is not in a valid state, this methods returns the message from the exception.
      Parameters:
      name -
      Returns:
      value or exception message or null.
    • getExtraNamesFormats

      public static String[] getExtraNamesFormats(Scannable scannable)
      Returns an array of strings describing the formatting information for the output names of this scannable. I.e. it strips off the formatting strings for the input names.

      This is useful for adding detector data formatting information into the ScanDataPoint

      Parameters:
      scannable -
      Returns:
      String[]
    • prettyPrintScannable

      public static String prettyPrintScannable(Scannable theScannable)
    • prettyPrintScannable

      public static String prettyPrintScannable(Scannable obj, int paddingSize)
    • prettyPrintScannableGroup

      public static String prettyPrintScannableGroup(IScannableGroup args)
    • getSerializedScannableSnapshot

      public static String getSerializedScannableSnapshot(Scannable scn) throws Exception
      Serializes and encodes into Base64 a snapshot of a scannable
      Parameters:
      scn -
      Returns:
      Base64 encoded ScannableSnapshot
      Throws:
      Exception
    • deserializeScannableSnapshot

      public static ScannableSnapshot deserializeScannableSnapshot(String serialized) throws Exception
      Deserializes a Base64 encoded ScannableSnapshot
      Parameters:
      serialized -
      Returns:
      Deserialized snapshot
      Throws:
      Exception
    • getScannableUnits

      public static String getScannableUnits(Scannable scannable)
      Check to see if scannable has any units to return. If not, return null. Does not work with IScannableGroup because it returns an array of strings where as this only returns a single string Use getScannableUnitsArray(Scannable...) instead.
      Parameters:
      scannable -
      Returns:
      units
    • getScannableUnitsArray

      public static String[] getScannableUnitsArray(Scannable... scannables) throws DeviceException
      Return an array of scannable units which map on to the scannable field names.
      Parameters:
      scannables -
      Returns:
      units
      Throws:
      DeviceException