Search in sources :

Example 1 with StandardUnitDB

use of ucar.units_vcell.StandardUnitDB in project vcell by virtualcell.

the class VCUnitTranslator method processCellMLUnit.

// derives the ucar unit equivalent to a single cellml 'unit'
// part of the recursive retrieval of CellML units.
private static Unit processCellMLUnit(Element cellUnit, Namespace sNamespace, Namespace sAttNamespace, VCUnitSystem vcUnitSystem) {
    Unit unit = null;
    UnitName uName;
    String kind = cellUnit.getAttributeValue(CELLMLTags.units, sAttNamespace);
    if (kind == null || kind.length() == 0) {
        throw new RuntimeException("No kind specified for the CellML unit >>> 2");
    }
    String exp = cellUnit.getAttributeValue(CELLMLTags.exponent, sAttNamespace);
    String scaleStr = cellUnit.getAttributeValue(CELLMLTags.prefix, sAttNamespace);
    int scale;
    try {
        if (scaleStr == null || scaleStr.length() == 0) {
            scale = 0;
        } else {
            scale = Integer.parseInt(scaleStr);
        }
    } catch (NumberFormatException e) {
        // bad practice but no better way.
        scale = CELLMLTags.prefixToScale(scaleStr);
    }
    String multiplier = cellUnit.getAttributeValue(CELLMLTags.mult, sAttNamespace);
    String offset = cellUnit.getAttributeValue(CELLMLTags.offset, sAttNamespace);
    try {
        StandardUnitDB unitDB = StandardUnitDB.instance();
        if (kind.equals(CELLMLTags.noDimUnit)) {
            // special treatment. ignore all the other attributes. Not used in computing total unit.
            unit = DerivedUnitImpl.DIMENSIONLESS;
            uName = unit.getUnitName();
        } else if (CELLMLTags.isCellBaseUnit(kind)) {
            unit = unitDB.get(kind);
            uName = UnitName.newUnitName(kind);
        } else {
            Element owner = (Element) cellUnit.getParent().getParent();
            String ownerName = owner.getAttributeValue(CELLMLTags.name, sAttNamespace);
            // check if already added.
            VCUnitDefinition unitDef = getMatchingCellMLUnitDef(owner, sAttNamespace, kind, vcUnitSystem);
            if (unitDef != null) {
                unit = unitDef.getUcarUnit();
            } else {
                // recursive block. assumes model level units are added before component level ones,
                // so no need to recurse through those as well.
                @SuppressWarnings("unchecked") ArrayList<Element> siblings = new ArrayList<Element>(owner.getChildren(CELLMLTags.UNITS, sNamespace));
                Element cellUnitDef = null;
                for (Element cuElement : siblings) {
                    cellUnitDef = cuElement;
                    if (cellUnitDef.getAttributeValue(CELLMLTags.name, sAttNamespace).equals(kind)) {
                        break;
                    } else {
                        cellUnitDef = null;
                    }
                }
                if (cellUnitDef != null) {
                    unitDef = CellMLToVCUnit(cellUnitDef, sNamespace, sAttNamespace, vcUnitSystem);
                } else {
                    System.err.println("Unit definition: " + kind + " is missing. >>> 3");
                    return null;
                }
            }
            uName = UnitName.newUnitName(kind);
        }
        if (exp != null && Integer.parseInt(exp) != 1) {
            unit = unit.raiseTo(new RationalNumber(Integer.parseInt(exp)));
        }
        if (scale != 0) {
            unit = new ScaledUnit(Double.parseDouble("1.0e" + scale), unit, uName);
        }
        if (multiplier != null && Double.parseDouble(multiplier) != 1) {
            unit = new ScaledUnit(Double.parseDouble(multiplier), unit, uName);
        }
        if (offset != null && Double.parseDouble(offset) != 0) {
            unit = new OffsetUnit(unit, Double.parseDouble(offset), uName);
        }
    } catch (UnitException e) {
        e.printStackTrace();
        throw new cbit.vcell.units.VCUnitException("Unable to set unit value: " + e.getMessage());
    }
    return unit;
}
Also used : OffsetUnit(ucar.units_vcell.OffsetUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) StandardUnitDB(ucar.units_vcell.StandardUnitDB) Element(org.jdom.Element) UnitException(ucar.units_vcell.UnitException) ArrayList(java.util.ArrayList) UnitName(ucar.units_vcell.UnitName) BaseUnit(ucar.units_vcell.BaseUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) RationalNumber(ucar.units_vcell.RationalNumber)

Aggregations

VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)1 ArrayList (java.util.ArrayList)1 Element (org.jdom.Element)1 BaseUnit (ucar.units_vcell.BaseUnit)1 OffsetUnit (ucar.units_vcell.OffsetUnit)1 RationalNumber (ucar.units_vcell.RationalNumber)1 ScaledUnit (ucar.units_vcell.ScaledUnit)1 StandardUnitDB (ucar.units_vcell.StandardUnitDB)1 Unit (ucar.units_vcell.Unit)1 UnitException (ucar.units_vcell.UnitException)1 UnitName (ucar.units_vcell.UnitName)1