Search in sources :

Example 16 with Dimension

use of javax.measure.Dimension in project n2a by frothga.

the class ExportJob method printDimension.

public String printDimension(Unit<?> unit) {
    Dimension dimension = unit.getDimension();
    String result = dimensionsNML.get(dimension);
    // Not a standard NML dimension
    if (result == null)
        result = dimension.toString();
    return result;
}
Also used : Dimension(javax.measure.Dimension)

Example 17 with Dimension

use of javax.measure.Dimension in project n2a by frothga.

the class ExportJob method appendUnits.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void appendUnits(boolean assumeNML) {
    // Purge pure dimensions (added independent of specific unit) if they are standard and NML dimensions are available.
    if (assumeNML) {
        List<String> keys = new ArrayList<String>(dimensionsUsed.keySet());
        for (String key : keys) {
            Dimension d = dimensionsUsed.get(key);
            if (dimensionsNML.containsKey(d))
                dimensionsUsed.remove(key);
        }
    }
    for (Entry<Unit<?>, String> e : unitsUsed.entrySet()) {
        Unit<?> key = e.getKey();
        if (assumeNML && unitsNML.containsKey(key))
            continue;
        String symbol = e.getValue();
        Dimension dimension = key.getDimension();
        String dimensionName = dimensionsNML.get(dimension);
        if (// Not a standard NML dimension
        dimensionName == null) {
            dimensionName = dimension.toString();
            dimensionsUsed.put(dimensionName, dimension);
        } else if (// Is a standard NML dimension, so only add if don't have NML dimensions available.
        !assumeNML) {
            dimensionsUsed.put(dimensionName, dimension);
        }
        Element unit = addElement("Unit", elements);
        unit.setAttribute("symbol", symbol);
        unit.setAttribute("dimension", dimensionName);
        // Determine offset
        Unit systemUnit = key.getSystemUnit();
        UnitConverter converter = key.getConverterTo(systemUnit);
        double offset = converter.convert(Integer.valueOf(0)).doubleValue();
        if (offset != 0)
            unit.setAttribute("offset", String.valueOf(offset));
        // Determine power*scale
        double scale = converter.convert(Integer.valueOf(1)).doubleValue() - offset;
        int power = (int) Math.round(Math.log(scale) / Math.log(10));
        if (Math.abs(scale - Math.pow(10, power)) < epsilon) {
            unit.setAttribute("power", String.valueOf(power));
        } else {
            unit.setAttribute("scale", String.valueOf(scale));
        }
    }
    for (Entry<String, Dimension> d : dimensionsUsed.entrySet()) {
        String name = d.getKey();
        Dimension value = d.getValue();
        Element dimension = addElement("Dimension", elements);
        dimension.setAttribute("name", name);
        Map<? extends Dimension, Integer> bases = value.getBaseDimensions();
        if (bases == null) {
            Map<Dimension, Integer> temp = new HashMap<Dimension, Integer>();
            temp.put(value, 1);
            bases = temp;
        }
        for (Entry<? extends Dimension, Integer> e : bases.entrySet()) {
            String base = e.getKey().toString().substring(1, 2).toLowerCase();
            if (base.equals("θ"))
                base = "k";
            dimension.setAttribute(base, e.getValue().toString());
        }
    }
}
Also used : HashMap(java.util.HashMap) AccessElement(gov.sandia.n2a.language.AccessElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Dimension(javax.measure.Dimension) Unit(javax.measure.Unit) UnitConverter(javax.measure.UnitConverter)

Example 18 with Dimension

use of javax.measure.Dimension in project uom-se by unitsofmeasurement.

the class ProductUnit method getDimension.

@Override
public Dimension getDimension() {
    Dimension dimension = QuantityDimension.NONE;
    for (int i = 0; i < this.getUnitCount(); i++) {
        Unit<?> unit = this.getUnit(i);
        if (this.elements != null && unit.getDimension() != null) {
            Dimension d = unit.getDimension().pow(this.getUnitPow(i)).root(this.getUnitRoot(i));
            dimension = dimension.multiply(d);
        }
    }
    return dimension;
}
Also used : Dimension(javax.measure.Dimension) QuantityDimension(tec.uom.se.quantity.QuantityDimension)

Aggregations

Dimension (javax.measure.Dimension)18 Test (org.junit.jupiter.api.Test)7 Map (java.util.Map)3 QuantityDimension (tech.units.indriya.quantity.QuantityDimension)3 HashMap (java.util.HashMap)2 UnitConverter (javax.measure.UnitConverter)2 Fraction (org.apache.sis.math.Fraction)2 QuantityDimension (tec.uom.se.quantity.QuantityDimension)2 AccessElement (gov.sandia.n2a.language.AccessElement)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Unit (javax.measure.Unit)1 DependsOnMethod (org.apache.sis.test.DependsOnMethod)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 UnsupportedImplementationException (org.apache.sis.util.UnsupportedImplementationException)1 Test (org.junit.Test)1 Element (org.w3c.dom.Element)1