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 ucar.units.UnitdateReferenceUnitprotected static booleandebugParsestatic SimpleUnitkmUnitstatic SimpleUnitmeterUnitstatic SimpleUnitpressureUnitprotected static ucar.units.UnitsecsUnitprotected ucar.units.Unituu
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleUnit()for subclasses.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleconvertTo(double value, SimpleUnit outputUnit)Convert given value of this unit to the new unit.static SimpleUnitfactory(String name)Create a SimpleUnit from the given name, catch Exceptions.static SimpleUnitfactoryWithExceptions(String name)Create a SimpleUnit from the given name, allow Exceptions.StringgetCanonicalString()static doublegetConversionFactor(String inputUnitString, String outputUnitString)Get the conversion factor to convert inputUnit to outputUnit.StringgetImplementingClass()ucar.units.UnitgetUnit()Get underlying ucar.units.Unit.StringgetUnitString()Extract the simple unit string (no number), eg "s" or "m".doublegetValue()Extract the value, can only be called for ScaledUnit.booleanisCompatible(String unitString)Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other.static booleanisCompatible(String unitString1, String unitString2)Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other.static booleanisCompatibleWithExceptions(String unitString1, String unitString2)Return true if unitString1 is convertible to unitString2static booleanisDateUnit(String unitString)Return true if the given unit is convertible to a date Unit.static booleanisDateUnit(ucar.units.Unit uu)Return true if this ucar.units.Unit is a Date.static booleanisTimeUnit(String unitString)Return true if the given unit is a time Unit, eg "seconds".static booleanisTimeUnit(ucar.units.Unit uu)Return true if this ucar.units.Unit is convertible to secs.booleanisUnknownUnit()Is this an instance of an UnknownUnit?protected static ucar.units.UnitmakeUnit(String name)StringtoString()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 ucar.units.Unit secsUnit
-
dateReferenceUnit
protected static ucar.units.Unit dateReferenceUnit
-
debugParse
protected static boolean debugParse
-
uu
protected ucar.units.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 ucar.units.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:
ucar.units.UnitException- when date parser fails- See Also:
UnitFormat.parse(java.lang.String)
-
makeUnit
protected static ucar.units.Unit makeUnit(String name) throws ucar.units.UnitException
- Throws:
ucar.units.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 ucar.units.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:
ucar.units.UnitException- if units parsing fails
-
isDateUnit
public static boolean isDateUnit(ucar.units.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(ucar.units.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 ucar.units.Unit getUnit()
Get underlying ucar.units.Unit.- Returns:
- underlying ucar.units.Unit.
-
convertTo
public double convertTo(double value, SimpleUnit outputUnit) throws IllegalArgumentExceptionConvert 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()
-
-