예제 #1
0
 /**
  * Associate the specified unit to with quantity.
  *
  * @param unit        Unit to add. Non-null.
  * @param isBaseUnit  True if this is the base unit, false otherwise.
  *                    If more than one unit is added as base unit, the
  *                    last one added will have this role. If no units are
  *                    added as base unit, the first unit added will have
  *                    this role.
  */
 internal void addUnit(WitsmlUnit unit, bool isBaseUnit)
 {
     //Debug.Assert(unit != null : "unit cannot be null";
     if (isBaseUnit)
     {
         units.Insert(0, unit);
     }
     else
     {
         units.Add(unit);
     }
     //units.Add(isBaseUnit ? 0 : units.size(), unit);
 }
예제 #2
0
        /**
         * Convert a specified value between two units.
         *
         * @param fromUnit  Unit to convert from. Non-null.
         * @param toUnit    Unit to convert to. Non-null.
         * @param value     Value to convert.
         * @return          Converted value.
         * @throws ArgumentException  If fromUnit or toUnit is null.
         */
        public double convert(WitsmlUnit fromUnit, WitsmlUnit toUnit, double value)
        {
            if (fromUnit == null)
            {
                throw new ArgumentException("fromUnit cannot be null");
            }

            if (toUnit == null)
            {
                throw new ArgumentException("toUnit cannot be null");
            }

            double baseValue = fromUnit.toBase(value);

            return(toUnit.fromBase(baseValue));
        }
예제 #3
0
        /**
         * Find the base unit of the specified unit.
         *
         * @param unit  Unit to find base unit for. Non-null.
         * @return      Requested base unit. Never null.
         * @throws ArgumentException  If unit is null.
         */
        public WitsmlUnit findBaseUnit(WitsmlUnit unit)
        {
            if (unit == null)
            {
                throw new ArgumentException("unit cannot be null");
            }

            foreach (WitsmlQuantity quantity in quantities)
            {
                foreach (WitsmlUnit u in quantity.getUnits())
                {
                    if (u.Equals(unit))
                    {
                        return(quantity.getBaseUnit());
                    }
                }
            }

            // Not found
            //Debug.Assert(false : "Impossible, as all units originates from the manager";
            return(null);
        }
예제 #4
0
        /**
         * Convert the specified value instance to base unit
         * if possible.
         *
         * @param value  Value to convert. Non-null.
         * @return       Converted value. If the value cannot be converted
         *               for some reason, a copy of the argument is returned.
         *               Never null.
         * @throws ArgumentException  If value is null.
         */
        public Value toBaseUnit(Value value)
        {
            if (value == null)
            {
                throw new ArgumentException("value cannot be null");
            }

            Double v          = value.getValue().Value;
            String unitSymbol = value.getUnit();

            if (unitSymbol != null)
            {
                WitsmlUnit unit = findUnit(unitSymbol);
                if (unit != null)
                {
                    WitsmlUnit baseUnit = findBaseUnit(unit);
                    v          = convert(unit, baseUnit, v);
                    unitSymbol = baseUnit.getSymbol();
                }
            }

            return(new Value(v, unitSymbol));
        }
예제 #5
0
        /**
         * Load all quantity and unit information from the local "units.txt" file.
         */
        private void load()
        {
            String fileName = "witsmlUnitDict.xml";
            //String packageName = Assembly.GetExecutingAssembly().GetName().Name;// getClass().getPackage().getName();
            //String packageLocation = packageName.Replace ('.', '/');
            //String filePath = "/" + packageLocation + "/" + fileName;

            // Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fileName );
            Assembly ass    = this.GetType().Assembly;
            Stream   stream = ass.GetManifestResourceStream(ass.GetName().Name + ".nwitsml.units." + fileName);
            List <WitsmlQuantity> hasBaseUnit = new List <WitsmlQuantity>();


            //InputStream stream = WitsmlUnitManager.class.getResourceAsStream(filePath);

            //SAXBuilder builder = new SAXBuilder();

            try {
                XDocument  document    = XDocument.Load(stream);     // builder.build(stream);
                XElement   rootElement = document.Root;
                XNamespace @namespace  = rootElement.Name.Namespace; //.getNamespace();

                XElement unitDefinitionsElement = rootElement.Element(@namespace + "UnitsDefinition" /*,@namespace*/);
                var      children = unitDefinitionsElement.Elements(@namespace + "UnitOfMeasure" /*, @namespace*/);
                foreach (Object child in children)
                {
                    XElement unitOfMeasureElement = (XElement)child;

                    List <WitsmlQuantity> quantitiesForUnit = new List <WitsmlQuantity>();

                    //
                    // Extract the BaseUnit element with its Description memeber
                    //
                    XElement baseUnitElement = unitOfMeasureElement.Element(@namespace + "BaseUnit" /*, @namespace*/);
                    bool     isBaseUnit      = baseUnitElement != null;

                    String quantityDescription = baseUnitElement != null && baseUnitElement.Element(@namespace + "Description") != null? //bugfix 9-25-10
                                                 baseUnitElement.Element(@namespace + "Description").Value.Trim()
                                                     : null;

                    //
                    // Identify all the quantities this unit appears in
                    //
                    var quantityTypeElements = unitOfMeasureElement.Elements(@namespace + "QuantityType" /*,@namespace*/);
                    foreach (Object child2 in quantityTypeElements)
                    {
                        XElement       quantityTypeElement = (XElement)child2;
                        String         quantityName        = quantityTypeElement.Value.Trim(); //.getTextTrim();
                        WitsmlQuantity quantity            = findOrCreateQuantity(quantityName,
                                                                                  quantityDescription);
                        quantitiesForUnit.Add(quantity);

                        // DEBUG
                        if (isBaseUnit && hasBaseUnit.Contains(quantity))
                        {
                            Console.WriteLine(
                                //System.out.println(
                                "ALREADY BASE: " + quantity.getName());
                        }

                        if (isBaseUnit)
                        {
                            hasBaseUnit.Add(quantity);
                        }
                    }

                    String unitName = unitOfMeasureElement.Element(@namespace + "Name" /*, @namespace*/).Value.Trim();

                    String unitSymbol = unitOfMeasureElement.Element(@namespace + "CatalogSymbol" /*,@namespace*/).Value.Trim();

                    XElement conversionElement = unitOfMeasureElement.Element("ConversionToBaseUnit" /*,@namespace*/);


                    double a = 1.0;
                    double b = 0.0;
                    double c = 0.0;
                    double d = 1.0;

                    if (conversionElement != null)
                    {
                        String   factorText      = conversionElement.Element(@namespace + "Factor" /*,@namespace*/).Value.Trim();
                        XElement fractionElement = conversionElement.Element(@namespace + "Fraction" /*,@namespace*/);
                        XElement formulaElement  = conversionElement.Element(@namespace + "Formula" /*,@namespace*/);

                        if (factorText != null)
                        {
                            try {
                                a = Double.Parse(factorText);
                            }
                            catch (FormatException exception) {
                                //Debug.Assert(false : "Invalid numeric value: " + factorText;
                            }
                        }
                        else if (fractionElement != null)
                        {
                            String numeratorText   = fractionElement.Element(@namespace + "Numerator" /*,@namespace*/).Value.Trim();
                            String denominatorText = fractionElement.Element(@namespace + "Denominator" /*,@namespace*/).Value.Trim();

                            try {
                                double numerator   = Double.Parse(numeratorText);
                                double denominator = Double.Parse(denominatorText);

                                a = numerator / denominator;
                            }
                            catch (FormatException exception) {
                                //Debug.Assert(false : "Invalid numeric value: " + numeratorText + "/" + denominatorText;
                            }
                        }
                        else if (formulaElement != null)
                        {
                            String aText = formulaElement.Element(@namespace + "A" /*, @namespace*/).Value.Trim();
                            String bText = formulaElement.Element(@namespace + "B" /*, @namespace*/).Value.Trim();
                            String cText = formulaElement.Element(@namespace + "C" /*, @namespace*/).Value.Trim();
                            String dText = formulaElement.Element(@namespace + "D" /*, @namespace*/).Value.Trim();

                            try {
                                a = Double.Parse(aText);
                                b = Double.Parse(bText);
                                c = Double.Parse(cText);
                                d = Double.Parse(dText);
                            }
                            catch (FormatException exception) {
                                //Debug.Assert(false : "Invalid numeric value: " + aText + "," + bText + "," + cText + "," + dText;
                            }
                        }
                    }

                    WitsmlUnit unit = new WitsmlUnit(unitName, unitSymbol,
                                                     a, b, c, d);

                    foreach (WitsmlQuantity quantity in quantitiesForUnit)
                    {
                        quantity.addUnit(unit, isBaseUnit);
                    }
                }
            }
            catch (IOException exception) {
                //Debug.Assert(false : "Parse error: " + filePath;
            }
            catch (Exception /*JDOMException*/ exception) {
                //Debug.Assert(false : "Parse error: " + filePath;
            }


            foreach (WitsmlQuantity q in quantities)
            {
                if (!hasBaseUnit.Contains(q))
                {
                    Console.WriteLine(
                        //System.out.println(
                        "NO BASE UNIT: " + q.getName());
                }
            }
        }