Package ucar.nc2.units
Class SimpleUnit
- java.lang.Object
-
- ucar.nc2.units.SimpleUnit
-
- Direct Known Subclasses:
TimeUnit
public class SimpleUnit extends Object
Convenience routines on top of ucar.units package. The ucar.units package handles- scientific units, which are factors of the fundamental dimensions such as length, time, mass, etc
- dates, represented as "n units of time since reference date" eg "1203 days since 1970-01-01 00:00:00"
-
-
Field Summary
Fields Modifier and Type Field Description protected static Unit
dateReferenceUnit
protected static boolean
debugParse
static SimpleUnit
kmUnit
static SimpleUnit
meterUnit
static SimpleUnit
pressureUnit
protected static Unit
secsUnit
protected Unit
uu
-
Constructor Summary
Constructors Modifier Constructor Description protected
SimpleUnit()
for subclasses.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
convertTo(double value, SimpleUnit outputUnit)
Convert given value of this unit to the new unit.static SimpleUnit
factory(String name)
Create a SimpleUnit from the given name, catch Exceptions.static SimpleUnit
factoryWithExceptions(String name)
Create a SimpleUnit from the given name, allow Exceptions.String
getCanonicalString()
static double
getConversionFactor(String inputUnitString, String outputUnitString)
Get the conversion factor to convert inputUnit to outputUnit.String
getImplementingClass()
Unit
getUnit()
Get underlying ucar.units.Unit.String
getUnitString()
Extract the simple unit string (no number), eg "s" or "m".double
getValue()
Extract the value, can only be called for ScaledUnit.boolean
isCompatible(String unitString)
Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other.static boolean
isCompatible(String unitString1, String unitString2)
Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other.static boolean
isCompatibleWithExceptions(String unitString1, String unitString2)
Return true if unitString1 is convertible to unitString2static boolean
isDateUnit(String unitString)
Return true if the given unit is convertible to a date Unit.static boolean
isDateUnit(Unit uu)
Return true if this ucar.units.Unit is a Date.static boolean
isTimeUnit(String unitString)
Return true if the given unit is a time Unit, eg "seconds".static boolean
isTimeUnit(Unit uu)
Return true if this ucar.units.Unit is convertible to secs.boolean
isUnknownUnit()
Is this an instance of an UnknownUnit?protected static Unit
makeUnit(String name)
String
toString()
Unit string representation.
-
-
-
Field Detail
-
kmUnit
public static final SimpleUnit kmUnit
-
meterUnit
public static final SimpleUnit meterUnit
-
pressureUnit
public static final SimpleUnit pressureUnit
-
secsUnit
protected static Unit secsUnit
-
dateReferenceUnit
protected static Unit dateReferenceUnit
-
debugParse
protected static boolean debugParse
-
uu
protected Unit uu
-
-
Method Detail
-
factory
public static SimpleUnit factory(String name)
Create a SimpleUnit from the given name, catch Exceptions.- Parameters:
name
- parse this name to create a unit.- Returns:
- SimpleUnit, DateUnit, TimeUnit, or null if failed
- See Also:
UnitFormat.parse(java.lang.String)
-
factoryWithExceptions
public static SimpleUnit factoryWithExceptions(String name) throws UnitException
Create a SimpleUnit from the given name, allow Exceptions.- Parameters:
name
- parse this name to create a unit.- Returns:
- SimpleUnit, DateUnit, or TimeUnit
- Throws:
UnitException
- when date parser fails- See Also:
UnitFormat.parse(java.lang.String)
-
makeUnit
protected static Unit makeUnit(String name) throws UnitException
- Throws:
UnitException
-
isCompatible
public static boolean isCompatible(String unitString1, String unitString2)
Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other. If either unit string is illegal, return false.- Parameters:
unitString1
- compare this unitunitString2
- compare this unit- Returns:
- true if the 2 units are compatible
-
isCompatibleWithExceptions
public static boolean isCompatibleWithExceptions(String unitString1, String unitString2) throws UnitException
Return true if unitString1 is convertible to unitString2- Parameters:
unitString1
- compare this unitunitString2
- compare this unit- Returns:
- true if the 2 units are compatible
- Throws:
UnitException
- if units parsing fails
-
isDateUnit
public static boolean isDateUnit(Unit uu)
Return true if this ucar.units.Unit is a Date.- Parameters:
uu
- check this Unit- Returns:
- true if its a Date
-
isTimeUnit
public static boolean isTimeUnit(Unit uu)
Return true if this ucar.units.Unit is convertible to secs.- Parameters:
uu
- check this Unit- Returns:
- true if its a Time
-
isDateUnit
public static boolean isDateUnit(String unitString)
Return true if the given unit is convertible to a date Unit. allowed format is something like:[-]Y[Y[Y[Y]]]-MM-DD[(T| )hh[:mm[:ss[.sss*]]][ [+|-]hh[[:]mm]]]
- Parameters:
unitString
- check this unit string- Returns:
- true if its a Date
-
isTimeUnit
public static boolean isTimeUnit(String unitString)
Return true if the given unit is a time Unit, eg "seconds".- Parameters:
unitString
- check this unit string- Returns:
- true if its a Time
-
getConversionFactor
public static double getConversionFactor(String inputUnitString, String outputUnitString) throws IllegalArgumentException
Get the conversion factor to convert inputUnit to outputUnit.- Parameters:
inputUnitString
- inputUnit in string formoutputUnitString
- outputUnit in string form- Returns:
- conversion factor
- Throws:
IllegalArgumentException
- if not convertible
-
getUnit
public Unit getUnit()
Get underlying ucar.units.Unit.- Returns:
- underlying ucar.units.Unit.
-
convertTo
public double convertTo(double value, SimpleUnit outputUnit) throws IllegalArgumentException
Convert given value of this unit to the new unit.- Parameters:
value
- value in this unitoutputUnit
- convert to this unit- Returns:
- value in outputUnit
- Throws:
IllegalArgumentException
- if outputUnit not convertible from this unit
-
isCompatible
public boolean isCompatible(String unitString)
Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other. If either unit string is illegal, return false.- Parameters:
unitString
- check if this is compatible with unitString- Returns:
- true if compatible
-
isUnknownUnit
public boolean isUnknownUnit()
Is this an instance of an UnknownUnit?- Returns:
- true if an instance of an UnknownUnit
-
getValue
public double getValue()
Extract the value, can only be called for ScaledUnit.- Returns:
- value of this unit if ScaledUnit, else NaN
-
getUnitString
public String getUnitString()
Extract the simple unit string (no number), eg "s" or "m".- Returns:
- unit string with no value
-
getCanonicalString
public String getCanonicalString()
-
getImplementingClass
public String getImplementingClass()
-
-