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);
}
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);
}
Aggregations