ucar.nc2.units
Class SimpleUnit

java.lang.Object
  extended by ucar.nc2.units.SimpleUnit
Direct Known Subclasses:
DateUnit, TimeUnit

public class SimpleUnit
extends java.lang.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"

Version:
$Revision: 63 $ $Date: 2006-07-12 15:50:51 -0600 (Wed, 12 Jul 2006) $
Author:
caron

Field Summary
protected static Unit dateUnit
           
protected static boolean debugParse
           
protected static UnitFormat format
           
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(java.lang.String name)
          Create a SimpleUnit from the given name, catch Exceptions.
static SimpleUnit factoryWithExceptions(java.lang.String name)
          Create a SimpleUnit from the given name, allow Exceptions.
static double getConversionFactor(java.lang.String inputUnitString, java.lang.String outputUnitString)
          Get the conversion factor to convert unputUnit to outputUnit.
 Unit getUnit()
          Get underlying ucar.units.Unit.
 java.lang.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(java.lang.String unitString)
          Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other.
static boolean isCompatible(java.lang.String unitString1, java.lang.String unitString2)
          Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other.
static boolean isCompatibleWithExceptions(java.lang.String unitString1, java.lang.String unitString2)
          Return true if unitString1 is convertible to unitString2
static boolean isDateUnit(java.lang.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(java.lang.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(java.lang.String name)
           
 java.lang.String toString()
          Unit string representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

format

protected static UnitFormat format

secsUnit

protected static Unit secsUnit

dateUnit

protected static Unit dateUnit

debugParse

protected static boolean debugParse

uu

protected Unit uu
Constructor Detail

SimpleUnit

protected SimpleUnit()
for subclasses.

Method Detail

factory

public static SimpleUnit factory(java.lang.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(java.lang.String name)
                                        throws java.lang.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:
java.lang.Exception
See Also:
UnitFormat.parse(java.lang.String)

makeUnit

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

isCompatible

public static boolean isCompatible(java.lang.String unitString1,
                                   java.lang.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.


isCompatibleWithExceptions

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

Throws:
java.lang.Exception

isDateUnit

public static boolean isDateUnit(Unit uu)
Return true if this ucar.units.Unit is a Date.


isTimeUnit

public static boolean isTimeUnit(Unit uu)
Return true if this ucar.units.Unit is convertible to secs.


isDateUnit

public static boolean isDateUnit(java.lang.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]]]


isTimeUnit

public static boolean isTimeUnit(java.lang.String unitString)
Return true if the given unit is a time Unit, eg "seconds".


getConversionFactor

public static double getConversionFactor(java.lang.String inputUnitString,
                                         java.lang.String outputUnitString)
                                  throws ConversionException
Get the conversion factor to convert unputUnit to outputUnit.

Parameters:
inputUnitString -
outputUnitString -
Returns:
conversion factor
Throws:
ConversionException

toString

public java.lang.String toString()
Unit string representation.

Overrides:
toString in class java.lang.Object

getUnit

public Unit getUnit()
Get underlying ucar.units.Unit.


convertTo

public double convertTo(double value,
                        SimpleUnit outputUnit)
                 throws ConversionException
Convert given value of this unit to the new unit.

Throws:
ConversionException

isCompatible

public boolean isCompatible(java.lang.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.


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.


getUnitString

public java.lang.String getUnitString()
Extract the simple unit string (no number), eg "s" or "m".