ucar.nc2.units
Class SimpleUnit

java.lang.Object
  extended by 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

  1. scientific units, which are factors of the fundamental dimensions such as length, time, mass, etc
  2. dates, represented as "n units of time since reference date" eg "1203 days since 1970-01-01 00:00:00"

Author:
caron

Field Summary
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
protected SimpleUnit()
          for subclasses.
 
Method Summary
 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 unitString2
static 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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
Constructor Detail

SimpleUnit

protected SimpleUnit()
for subclasses.

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 Exception
Create a SimpleUnit from the given name, allow Exceptions.

Parameters:
name - parse this name to create a unit.
Returns:
SimpleUnit, DateUnit, or TimeUnit
Throws:
Exception - when date parser fails
See Also:
UnitFormat.parse(java.lang.String)

makeUnit

protected static Unit makeUnit(String name)
                        throws Exception
Throws:
Exception

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 unit
unitString2 - compare this unit
Returns:
true if the 2 units are compatible

isCompatibleWithExceptions

public static boolean isCompatibleWithExceptions(String unitString1,
                                                 String unitString2)
                                          throws Exception
Return true if unitString1 is convertible to unitString2

Parameters:
unitString1 - compare this unit
unitString2 - compare this unit
Returns:
true if the 2 units are compatible
Throws:
Exception - 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 form
outputUnitString - outputUnit in string form
Returns:
conversion factor
Throws:
IllegalArgumentException - if not convertible

toString

public String toString()
Unit string representation.

Overrides:
toString in class Object

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 unit
outputUnit - 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()


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.