Issue #2906 added UnitMapper to convert between udunits and UCUM

(cherry picked from commit 4649b903fdaec93a047ac16578363b7340559b00 [formerly 9ed58058fa] [formerly 76fb531270 [formerly 6d8b4cd70e] [formerly 9a5c4142bb] [formerly 9ed58058fa [formerly 9a5c4142bb [formerly 0ca12403511c6375ddc2a3c7f1dd0ba9a30ff113]]]])


Former-commit-id: c54f87d99e [formerly b2666b3fa6] [formerly 2a5a946d64] [formerly ae670e22f4 [formerly 2a5a946d64 [formerly b211b2ab576090b227b0ecd42a251efe570af1fe]]]
Former-commit-id: ae670e22f4
Former-commit-id: aff8e86d5f8084d81842d13a9d1070206e18c78b [formerly 3e11a6db71]
Former-commit-id: 7e8f7c86c3
This commit is contained in:
Brian Clements 2014-04-02 15:35:42 -05:00 committed by Steve Harris
parent d79f9fef4b
commit 14767f29b5
4 changed files with 274 additions and 3 deletions

View file

@ -5,7 +5,9 @@ Bundle-SymbolicName: com.raytheon.uf.common.units
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: RAYTHEON
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: javax.measure;bundle-version="1.0.0";visibility:=reexport,
com.raytheon.uf.common.util;bundle-version="1.12.1174",
org.apache.commons.beanutils;bundle-version="1.8.3"
Require-Bundle: javax.measure;visibility:=reexport,
com.raytheon.uf.common.util,
org.apache.commons.beanutils,
com.raytheon.uf.common.localization,
com.raytheon.uf.common.status
Export-Package: com.raytheon.uf.common.units

View file

@ -0,0 +1,71 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.uf.common.units;
/**
* Exception thrown when unit mapper is unable to process textual unit
* representations
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Apr 02, 2014 2906 bclement Initial creation
*
* </pre>
*
* @author bclement
* @version 1.0
*/
public class UnitLookupException extends Exception {
private static final long serialVersionUID = 1484062925782118997L;
/**
*
*/
public UnitLookupException() {
}
/**
* @param message
*/
public UnitLookupException(String message) {
super(message);
}
/**
* @param cause
*/
public UnitLookupException(Throwable cause) {
super(cause);
}
/**
* @param message
* @param cause
*/
public UnitLookupException(String message, Throwable cause) {
super(message, cause);
}
}

View file

@ -0,0 +1,157 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.uf.common.units;
import java.util.HashSet;
import java.util.Set;
import javax.measure.unit.Unit;
import javax.xml.bind.JAXBException;
import com.raytheon.uf.common.localization.IPathManager;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.localization.PathManagerFactory;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.util.mapping.Mapper;
import com.raytheon.uf.common.util.mapping.MultipleMappingException;
/**
* Provide mapping of textual unit representations. The base set is defined by
* javax.measure and UCUM. As well as providing name mapping it is also possible
* to map from an alias to a Unit object.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Apr 02, 2014 2906 bclement Initial creation
*
* </pre>
*
* @author bclement
* @version 1.0
*/
public class UnitMapper extends Mapper {
private static final transient IUFStatusHandler statusHandler = UFStatus
.getHandler(UnitMapper.class);
private UnitMapper() {
IPathManager pathMgr = PathManagerFactory.getPathManager();
// read in the namespace map
LocalizationFile[] files = pathMgr.listStaticFiles("unit"
+ IPathManager.SEPARATOR + "alias", new String[] { ".xml" },
true, true);
for (LocalizationFile file : files) {
try {
addAliasList(file.getFile());
} catch (JAXBException e) {
statusHandler.error(
"Error reading unit aliases: " + file.getName()
+ " has been ignored.", e);
}
}
}
/**
* same functionality as lookupBaseNames but maps each baseName to a Unit
* object.
*
* @param alias
* @param namespace
* @return
* @throws UnitLookupException
*/
public Set<Unit<?>> lookupUnits(String alias, String namespace)
throws UnitLookupException {
Set<String> baseNames = super.lookupBaseNames(alias, namespace);
Set<Unit<?>> result = new HashSet<Unit<?>>(
(int) (baseNames.size() / 0.75) + 1, 0.75f);
for (String baseName : baseNames) {
result.add(getBaseUnit(baseName));
}
return result;
}
/**
* Return all base units that are compatible with targetUnit that have alias
*
* @param alias
* @param namespace
* @param targetUnit
* @return empty set if none are found
* @throws UnitLookupException
*/
public Set<Unit<?>> lookupCompatibleUnits(String alias, String namespace,
Unit<?> targetUnit) throws UnitLookupException {
Set<String> baseNames = super.lookupBaseNames(alias, namespace);
Set<Unit<?>> rval = new HashSet<Unit<?>>(baseNames.size());
for (String baseName : baseNames) {
Unit<?> baseUnit = getBaseUnit(baseName);
if (baseUnit.isCompatible(targetUnit)) {
rval.add(baseUnit);
}
}
return rval;
}
/**
* same functionality as lookupBaseName but maps the baseName to a Parameter
* Object.
*
* @param alias
* @param namespace
* @return
* @throws MultipleMappingException
* @throws UnitLookupException
*/
public Unit<?> lookupUnit(String alias, String namespace)
throws MultipleMappingException, UnitLookupException {
String baseName = super.lookupBaseName(alias, namespace);
return getBaseUnit(baseName);
}
/**
* get a Unit object for a baseName, no mapping is done, this is provided
* here for convenience.
*
* @param baseName
* @return
* @throws UnitLookupException
*/
public Unit<?> getBaseUnit(String baseName) throws UnitLookupException {
try {
return Unit.valueOf(baseName);
} catch (Exception e) {
throw new UnitLookupException("Unable to convert to base units: "
+ baseName, e);
}
}
private static final UnitMapper instance = new UnitMapper();
public static UnitMapper getInstance() {
return instance;
}
}

View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
This_software_was_developed_and_/_or_modified_by_Raytheon_Company,
pursuant_to_Contract_DG133W-05-CQ-1067_with_the_US_Government.
U.S._EXPORT_CONTROLLED_TECHNICAL_DATA
This_software_product_contains_export-restricted_data_whose
export/transfer/disclosure_is_restricted_by_U.S._law._Dissemination
to_non-U.S._persons_whether_in_the_United_States_or_abroad_requires
an_export_license_or_other_authorization.
Contractor_Name:________Raytheon_Company
Contractor_Address:_____6825_Pine_Street,_Suite_340
________________________Mail_Stop_B8
________________________Omaha,_NE_68106
________________________402.291.0100
See_the_AWIPS_II_Master_Rights_File_("Master_Rights_File.pdf")_for
further_licensing_information.
-->
<aliasList namespace="udunits">
<alias base="Pa*s^-1">kg m-1 s-3</alias>
<alias base="mm*s^-1">0.0010 m s-1</alias>
<alias base="mm">0.0010 m</alias>
<alias base="s^-1">s-1</alias>
<alias base="K*s^-1">K s-1</alias>
<alias base="m*s^-1">m s-1</alias>
<alias base="degree_angle">0.017453292519943295 rad</alias>
<alias base="J*kg^-1">m2 s-2</alias>
<alias base="%">0.01</alias>
<alias base="kg*m^-2">kg m-2</alias>
<!-- millimeters are equivalent for precipitation measurements -->
<alias base="mm">kg m-2</alias>
<alias base="°">Degree</alias>
<alias base="min">Minute</alias>
<alias base="h">Hour</alias>
<alias base="day">Day</alias>
<alias base="month">Month</alias>
<alias base="year">Year</alias>
<alias base="J*m^-2">J m-2</alias>
</aliasList>