Search in sources :

Example 1 with StandardUnitFormat

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

the class InternalUnitDefinition method getInstance.

/**
 * Creation date: (4/15/2004 1:39:26 PM)
 * @return cbit.vcell.units.InternalUnitDefinition
 * @param InternalUnitDefinition cbit.vcell.units.InternalUnitDefinition
 * @deprecated please don't use, we should hide the underlying implementation (CellML translator needs it now)
 */
static InternalUnitDefinition getInstance(Unit ucarUnit) {
    // 
    for (int i = 0; i < defs.size(); i++) {
        InternalUnitDefinition temp = (InternalUnitDefinition) defs.get(i);
        if (equivalent(ucarUnit, temp.getUcarUnit())) {
            return temp;
        }
    }
    // 
    try {
        if (ucarUnit instanceof ScaledUnit) {
            java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(ucarUnit.toString(), " ");
            double scale = 1;
            int cnt = 0;
            String nonNum = null;
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                try {
                    scale *= Double.parseDouble(token);
                    cnt++;
                } catch (NumberFormatException e) {
                    nonNum = token;
                    break;
                }
            }
            double roundedScale = round(scale);
            if (roundedScale != scale || cnt > 1) {
                String newSymbol = String.valueOf(roundedScale);
                if (nonNum != null) {
                    newSymbol += " " + nonNum;
                }
                StandardUnitFormat standardUnitFormat = new StandardUnitFormat(new java.io.ByteArrayInputStream(newSymbol.trim().getBytes()));
                ucar.units_vcell.UnitDB unitDB = ucar.units_vcell.UnitSystemManager.instance().getUnitDB();
                ucarUnit = standardUnitFormat.unitSpec(unitDB);
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + ucarUnit.toString() + ": " + e.getMessage());
    } catch (UnitException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + ucarUnit.toString() + ": " + e.getMessage());
    }
    // 
    return new InternalUnitDefinition(ucarUnit);
}
Also used : ScaledUnit(ucar.units_vcell.ScaledUnit) UnitException(ucar.units_vcell.UnitException) StandardUnitFormat(ucar.units_vcell.StandardUnitFormat) ParseException(ucar.units_vcell.ParseException)

Example 2 with StandardUnitFormat

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

the class InternalUnitDefinition method getInstance.

static InternalUnitDefinition getInstance(String symbol) {
    if (symbol == null) {
        throw new IllegalArgumentException("symbol was null");
    }
    if (TBD_SYMBOL.equals(symbol)) {
        return UNIT_TBD;
    }
    if (symbol.equalsIgnoreCase("dimensionless")) {
        return UNIT_DIMENSIONLESS;
    }
    Unit dUnit;
    try {
        StandardUnitFormat standardUnitFormat = new StandardUnitFormat(new java.io.ByteArrayInputStream(symbol.trim().getBytes()));
        ucar.units_vcell.UnitDB unitDB = ucar.units_vcell.UnitSystemManager.instance().getUnitDB();
        dUnit = standardUnitFormat.unitSpec(unitDB);
        ucar.units_vcell.Factor[] factors = dUnit.getDerivedUnit().getDimension().getFactors();
        for (int i = 0; i < factors.length; i++) {
            if (factors[i].getBase() instanceof ucar.units_vcell.UnknownUnit) {
                throw new VCUnitException("invalid symbol '" + factors[i].getBase() + "'");
            }
        }
    } catch (ucar.units_vcell.ParseException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + symbol + ": " + e.getMessage());
    } catch (UnitException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + symbol + ": " + e.getMessage());
    }
    return getInstance(dUnit);
}
Also used : UnitException(ucar.units_vcell.UnitException) BaseUnit(ucar.units_vcell.BaseUnit) TimeScaleUnit(ucar.units_vcell.TimeScaleUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit) StandardUnitFormat(ucar.units_vcell.StandardUnitFormat) Factor(ucar.units_vcell.Factor) ParseException(ucar.units_vcell.ParseException)

Aggregations

ParseException (ucar.units_vcell.ParseException)2 ScaledUnit (ucar.units_vcell.ScaledUnit)2 StandardUnitFormat (ucar.units_vcell.StandardUnitFormat)2 UnitException (ucar.units_vcell.UnitException)2 BaseUnit (ucar.units_vcell.BaseUnit)1 Factor (ucar.units_vcell.Factor)1 OffsetUnit (ucar.units_vcell.OffsetUnit)1 TimeScaleUnit (ucar.units_vcell.TimeScaleUnit)1 Unit (ucar.units_vcell.Unit)1