Class AsaMath

java.lang.Object
ucar.nc2.dt.ugrid.utils.AsaMath

public class AsaMath extends Object
Utilities for common mathematical operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Constant for converting from degrees to radians;
    static final double
    Constant for converting from radians to degrees
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    almostEqual(double val1, double val2, double tolerance)
     
    static boolean
    almostEqual(float val1, float val2, float tolerance)
     
    static double
    averageDouble(double[] vals)
    Averages the values in vals ignoring any Double.NaN values.
    static double
    averageDouble(double[] vals, double ignoreVal)
    Averages the values in vals ignoring any ignoreVal values.
    static float
    averageFloat(float[] vals)
    Averages the values in vals ignoring any Float.NaN values.
    static float
    averageFloat(float[] vals, float ignoreVal)
    Averages the values in vals ignoring any ignoreVal values.
    static double
    calcDirection(double u, double v)
    Calculates direction from U and V
    static double
    calcSpeed(double u, double v)
    Calculates speed from U and V
    static double[][]
    calcSpeedDirection(double[] us, double[] vs)
    Calculates speeds and directions from an array of U and V pairs.
    static double[]
    calcSpeedDirection(double u, double v)
    Calculates both speed and direction from U and V
    static float[]
    calculateModes(float[] numbers, Float fillVal)
    Returns the statistical mode of the given dataset denoted by numbers while ignoring entries whose values match fillVal.
    static double[][]
    calcUVFromSpeedDir(double[] speeds, double[] directions)
    Calculates Us and Vs from arrays of speeds and directions.
    static double[]
    calcUVFromSpeedDir(double speed, double direction)
    Calulates U and V from speed and direction
    static long
    getTimeInMilliseconds(double time, String units)
    Calculates a time from time in the specified units.
    static boolean
    Determines if the specified value can be parsed as a double
    static double
    roundDouble(double value, int precision)
    Rounds value to the number of decimal points indicated by precision.
    static float
    roundFloat(float value, int precision)
    Rounds value to the number of decimal points indicated by precision.
    static double
    roundToNearest(double value, double nearest, int roundDirection)
    Rounds value to the closest multiple of nearest.
    static boolean
    sameSign(double val1, double val2)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • RAD_2_DEG

      public static final double RAD_2_DEG
      Constant for converting from radians to degrees
      See Also:
    • DEG_2_RAD

      public static final double DEG_2_RAD
      Constant for converting from degrees to radians;
      See Also:
  • Constructor Details

    • AsaMath

      public AsaMath()
  • Method Details

    • calcSpeed

      public static double calcSpeed(double u, double v)
      Calculates speed from U and V
      Parameters:
      u - the u component
      v - the v component
      Returns:
      the speed
    • calcDirection

      public static double calcDirection(double u, double v)
      Calculates direction from U and V
      Parameters:
      u - the u component
      v - the v component
      Returns:
      the direction
    • calcSpeedDirection

      public static double[] calcSpeedDirection(double u, double v)
      Calculates both speed and direction from U and V
      Parameters:
      u - the u component
      v - the v component
      Returns:
      a double[] where where [0]==speed and [1]==direction
    • calcSpeedDirection

      public static double[][] calcSpeedDirection(double[] us, double[] vs)
      Calculates speeds and directions from an array of U and V pairs. The arrays MUST be of the same length.
      Parameters:
      us - the array of u components
      vs - the array of v components
      Returns:
      a double[][] where [0]==speed[] and [1]==direction[]
    • calcUVFromSpeedDir

      public static double[] calcUVFromSpeedDir(double speed, double direction)
      Calulates U and V from speed and direction
      Parameters:
      speed -
      direction -
      Returns:
      a double[] where [0]==U and [1]==V
    • calcUVFromSpeedDir

      public static double[][] calcUVFromSpeedDir(double[] speeds, double[] directions)
      Calculates Us and Vs from arrays of speeds and directions. Arrays MUST be the same length.
      Parameters:
      speeds -
      directions -
      Returns:
      a double[][] where [0]==U[] and [1]==V[]
    • getTimeInMilliseconds

      public static long getTimeInMilliseconds(double time, String units)
      Calculates a time from time in the specified units. Valid values for units are: "hour", "hours", "hrs", "hr", "minute", "minutes", "mins", or "min"
      Parameters:
      time - a double time in milliseconds
      units - the String units to convert the given time to
      Returns:
      a long value representing the number of minutes or hours in time dependant on the units specified
    • isNumeric

      public static boolean isNumeric(Object value)
      Determines if the specified value can be parsed as a double
      Parameters:
      value - an Object to be parsed
      Returns:
      true if the specified object can be parsed as a double; false otherwise
    • roundDouble

      public static double roundDouble(double value, int precision)
      Rounds value to the number of decimal points indicated by precision.
      Parameters:
      value -
      precision -
      Returns:
    • roundFloat

      public static float roundFloat(float value, int precision)
      Rounds value to the number of decimal points indicated by precision.
      Parameters:
      value -
      precision -
      Returns:
    • averageDouble

      public static double averageDouble(double[] vals)
      Averages the values in vals ignoring any Double.NaN values.
      Parameters:
      vals - the values to be averaged
      Returns:
      Double.NaN if vals is null or all values are Double.NaN, otherwise the average of vals
    • averageDouble

      public static double averageDouble(double[] vals, double ignoreVal)
      Averages the values in vals ignoring any ignoreVal values.
      Parameters:
      vals - the values to be averaged
      ignoreVal - the value to ignore
      Returns:
      Double.NaN if vals is null or all values are ignoreVal, otherwise the average of vals
    • averageFloat

      public static float averageFloat(float[] vals)
      Averages the values in vals ignoring any Float.NaN values.
      Parameters:
      vals - the values to be averaged
      Returns:
      Float.NaN if vals is null or all values are Float.NaN, otherwise the average of vals
    • averageFloat

      public static float averageFloat(float[] vals, float ignoreVal)
      Averages the values in vals ignoring any ignoreVal values.
      Parameters:
      vals - the values to be averaged
      ignoreVal - the value to ignore
      Returns:
      Float.NaN if vals is null or all values are ignoreVal, otherwise the average of vals
    • calculateModes

      public static float[] calculateModes(float[] numbers, Float fillVal)
      Returns the statistical mode of the given dataset denoted by numbers while ignoring entries whose values match fillVal. If multiple values yeild the same frequency of occurance, each of them will be returned.
      Parameters:
      numbers - a float array of numbers
      fillVal - the "filling" value within numbers to be ignored in determining a statistical mode.
      Returns:
      an array of values which share the same frequency as the highest occuring value from the array numbers
    • roundToNearest

      public static double roundToNearest(double value, double nearest, int roundDirection)
      Rounds value to the closest multiple of nearest. The roundDirection parameter indicates which direction to force rounding. A '0' rounds naturally (up for values > 0.5, down for values < 0.5). A number > 0 forces a round up, while < 0 forces a round down.

      Examples:

      The code
      double val = roundToNearest(102.536, 0.05, 1);
      results in val having a value of: 102.55

      The code
      double val = roundToNearest(102.536, 50, -1);
      results in val having a value of: 100

      Parameters:
      value - the input value.
      nearest - the value of the nearest number to round to
      roundDirection - the direction to force rounding. 0 uses natural rounding, <0 always rounds up, >0 always rounds down.
      Returns:
      the rounded number
    • almostEqual

      public static boolean almostEqual(double val1, double val2, double tolerance)
    • almostEqual

      public static boolean almostEqual(float val1, float val2, float tolerance)
    • sameSign

      public static boolean sameSign(double val1, double val2)