Package ucar.nc2.dt.ugrid.utils
Class AsaMath
- java.lang.Object
-
- ucar.nc2.dt.ugrid.utils.AsaMath
-
public class AsaMath extends Object
Utilities for common mathematical operations.
-
-
Constructor Summary
Constructors Constructor Description AsaMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanalmostEqual(double val1, double val2, double tolerance)static booleanalmostEqual(float val1, float val2, float tolerance)static doubleaverageDouble(double[] vals)Averages the values in vals ignoring any Double.NaN values.static doubleaverageDouble(double[] vals, double ignoreVal)Averages the values in vals ignoring any ignoreVal values.static floataverageFloat(float[] vals)Averages the values in vals ignoring any Float.NaN values.static floataverageFloat(float[] vals, float ignoreVal)Averages the values in vals ignoring any ignoreVal values.static doublecalcDirection(double u, double v)Calculates direction from U and Vstatic doublecalcSpeed(double u, double v)Calculates speed from U and Vstatic 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 Vstatic float[]calculateModes(float[] numbers, Float fillVal)Returns the statistical mode of the given dataset denoted bynumberswhile ignoring entries whose values matchfillVal.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 directionstatic longgetTimeInMilliseconds(double time, String units)Calculates a time fromtimein the specified units.static booleanisNumeric(Object value)Determines if the specified value can be parsed as adoublestatic doubleroundDouble(double value, int precision)Rounds value to the number of decimal points indicated by precision.static floatroundFloat(float value, int precision)Rounds value to the number of decimal points indicated by precision.static doubleroundToNearest(double value, double nearest, int roundDirection)Roundsvalueto the closest multiple ofnearest.static booleansameSign(double val1, double val2)
-
-
-
Field Detail
-
RAD_2_DEG
public static final double RAD_2_DEG
Constant for converting from radians to degrees- See Also:
- Constant Field Values
-
DEG_2_RAD
public static final double DEG_2_RAD
Constant for converting from degrees to radians;- See Also:
- Constant Field Values
-
-
Method Detail
-
calcSpeed
public static double calcSpeed(double u, double v)Calculates speed from U and V- Parameters:
u- the u componentv- 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 componentv- 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 componentv- 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 componentsvs- 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 fromtimein the specified units. Valid values forunitsare: "hour", "hours", "hrs", "hr", "minute", "minutes", "mins", or "min"- Parameters:
time- adoubletime in millisecondsunits- theStringunits to convert the given time to- Returns:
- a
longvalue representing the number of minutes or hours intimedependant on the units specified
-
isNumeric
public static boolean isNumeric(Object value)
Determines if the specified value can be parsed as adouble- Parameters:
value- anObjectto be parsed- Returns:
trueif the specified object can be parsed as adouble;falseotherwise
-
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 averagedignoreVal- 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 averagedignoreVal- 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 bynumberswhile ignoring entries whose values matchfillVal. If multiple values yeild the same frequency of occurance, each of them will be returned.- Parameters:
numbers- afloatarray of numbersfillVal- the "filling" value withinnumbersto 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)Roundsvalueto the closest multiple ofnearest. TheroundDirectionparameter 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.55The 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 toroundDirection- the direction to force rounding.0uses natural rounding,<0always rounds up,>0always 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)
-
-