Beispiel #1
0
        private void _getMinAndInterval(ucar.nc2.Variable var, string minMaxStr, out double interval, out double minVal, out double count)
        {
            ucar.ma2.Array dataArr    = _util.GetAllVariableData(var);
            bool           stepExists = false;

            interval = 1.0; minVal = double.MaxValue; count = 0;
            java.util.List ncVarAtt = var.getAttributes();
            for (int attCount = 0; attCount < ncVarAtt.size(); attCount++)
            {
                ucar.nc2.Attribute varAtt  = (ucar.nc2.Attribute)ncVarAtt.get(attCount);
                string             attName = varAtt.getName();
                if (attName == "step")
                {
                    java.lang.Number attVal = (java.lang.Number)varAtt.getValue(0);
                    interval   = attVal.doubleValue();
                    stepExists = true;
                }
            }

            double prevVal = 0.0;
            int    minCount = 0, maxCount = (int)dataArr.getSize() - 1; count = 0;

            if (!string.IsNullOrEmpty(minMaxStr))
            {
                minCount = Convert.ToInt32(minMaxStr.Split(':')[0]);
                maxCount = Convert.ToInt32(minMaxStr.Split(':')[1]);
                count    = maxCount - minCount + 1;
            }
            else
            {
                count = maxCount + 1;
            }

            for (int dCount = minCount; dCount <= maxCount; dCount++)
            {
                ucar.ma2.Index dIndex = dataArr.getIndex();
                double         data   = dataArr.getDouble(dIndex.set(dCount));
                if (minVal >= data)
                {
                    minVal = data;
                }
                if (!stepExists)
                {
                    if (dCount > 0)
                    {
                        prevVal  = dataArr.getDouble(dIndex.set(dCount - 1));
                        interval = data - prevVal;
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Converts a Java Number to a nullable double.
 /// </summary>
 /// <param name="n">Java Number instance</param>
 /// <returns>Nullable double</returns>
 public static double?ToNullableDouble(this java.lang.Number n)
 {
     return(n == null ? (double?)null : n.doubleValue());
 }
Beispiel #3
0
 /// <summary>
 /// Converts a Java Number instance to a nullable decimal.
 /// </summary>
 /// <param name="n">Java Number instance</param>
 /// <returns>Nullable decimal</returns>
 public static decimal?ToNullableDecimal(this java.lang.Number n)
 {
     return(n == null ? (decimal?)null : Convert.ToDecimal(n.doubleValue()));
 }
            public static double ToDouble(JavaNumber n)
            {
                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    if (bigDecimalValue.compareTo(MAX_DOUBLE) > 0
                        || bigDecimalValue.compareTo(MIN_DOUBLE) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return bigDecimalValue.doubleValue();
                }

                return n.doubleValue();
            }
Beispiel #5
0
        private void _getMinAndInterval(ucar.nc2.Variable ncDim, string minMaxStr, out double interval, out double minVal, out List <double> interpolatedVal, out List <double> originalVal, bool useDefinedDZ)
        {
            ucar.ma2.Array dataArr = _util.GetAllVariableData(ncDim);
            int            minCount = 0, maxCount = (int)dataArr.getSize() - 1;

            minVal = double.MaxValue;
            double maxVal = double.MinValue;

            double        prevVal   = 0.0;
            List <double> intervals = new List <double>();

            interpolatedVal = new List <double>();
            originalVal     = new List <double>();
            interval        = 0.0;

            if (!string.IsNullOrEmpty(minMaxStr))
            {
                minCount = Convert.ToInt32(minMaxStr.Split(':')[0]);
                maxCount = Convert.ToInt32(minMaxStr.Split(':')[1]);
            }

            java.util.List ncVarAtt   = ncDim.getAttributes();
            bool           stepExists = false;

            for (int attCount = 0; attCount < ncVarAtt.size(); attCount++)
            {
                ucar.nc2.Attribute varAtt  = (ucar.nc2.Attribute)ncVarAtt.get(attCount);
                string             attName = varAtt.getName();
                if (attName == "step")
                {
                    java.lang.Number attVal = (java.lang.Number)varAtt.getValue(0);
                    interval   = attVal.doubleValue();
                    stepExists = true;
                }
            }

            for (int dCount = minCount; dCount <= maxCount; dCount++)
            {
                ucar.ma2.Index dIndex = dataArr.getIndex();
                double         data   = dataArr.getDouble(dIndex.set(dCount));
                originalVal.Add(data);
                if (minVal >= data)
                {
                    minVal = data;
                }
                if (maxVal <= data)
                {
                    maxVal = data;
                }

                if (!stepExists)
                {
                    if (dCount > 0)
                    {
                        prevVal  = dataArr.getDouble(dIndex.set(dCount - 1));
                        interval = data - prevVal;
                        intervals.Add(interval);
                    }
                }
            }

            if (!stepExists)
            {
                if (intervals.Average() != interval)
                {
                    if (useDefinedDZ)
                    {
                        for (double min = minVal; min <= maxVal; min += Convert.ToDouble(_settings.DZ))
                        {
                            interpolatedVal.Add(min);
                        }
                        interval = Convert.ToDouble(_settings.DZ);
                    }
                    else
                    {
                        //generate a list of interpolatedVal
                        for (double min = minVal; min <= maxVal; min += intervals.Min())
                        {
                            interpolatedVal.Add(min);
                        }
                        interval = intervals.Min();
                    }
                }
            }
        }
            public static bool ToBoolean(JavaNumber n)
            {
                if (n is JavaInteger
                    || n is JavaShort
                    || n is JavaByte)
                {
                    return (n.intValue() != 0);
                }
                else if (n is JavaLong)
                {
                    return (n.longValue() != 0L);
                }
                else if (n is JavaDouble
                         || n is JavaFloat)
                {
                    return (n.doubleValue() != 0.0D);
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    return !bigDecimalValue.equals(BIG_DECIMAL_0);
                }

                return (n.intValue() != 0);
            }
            public static decimal ToDecimal(JavaNumber n)
            {
                if (n is JavaByte || n is JavaShort || n is JavaInteger)
                {
                    return new decimal(n.intValue());
                }
                else if (n is JavaLong)
                {
                    return new decimal(n.longValue());
                }
                else if (n is JavaFloat
                         || n is JavaDouble)
                {
                    return new decimal(n.doubleValue());
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    if (bigDecimalValue.compareTo(MAX_DECIMAL) > 0
                        || bigDecimalValue.compareTo(MIN_DECIMAL) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return decimal.Parse(bigDecimalValue.toPlainString());
                }

                return decimal.Parse(n.toString());
            }
            public static long ToBigInt(JavaNumber n)
            {
                if (n is JavaByte
                    || n is JavaShort
                    || n is JavaInteger
                    || n is JavaLong)
                {
                    return n.longValue();
                }

                const long max = JavaLong.MAX_VALUE;
                const long min = JavaLong.MIN_VALUE;

                if (n is JavaDouble || n is JavaFloat)
                {
                    double value = n.doubleValue();

                    if (JavaDouble.isNaN(value)
                        || JavaDouble.isInfinite(value)
                        || max < value
                        || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (long)value;
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    JavaBigInteger bi = bigDecimalValue.toBigInteger();

                    if (bi.compareTo(MAX_BIGINT) > 0
                        || bi.compareTo(MIN_BIGINT) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return bi.longValue();
                }

                return n.longValue();
            }
            public static sbyte ToTinyInt(JavaNumber n)
            {
                if (n is JavaByte)
                {
                    return (sbyte) n.intValue();
                }

                const int max = 127;
                const int min = -128;

                if (n is JavaShort
                    || n is JavaInteger)
                {
                    int value = n.intValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (sbyte) value;
                }
                else if (n is JavaLong)
                {
                    long value = n.longValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (sbyte)value;
                }
                else if (n is JavaDouble || n is JavaFloat)
                {
                    double value = n.doubleValue();

                    if (JavaDouble.isNaN(value)
                        || JavaDouble.isInfinite(value)
                        || max < value
                        || value < min)
                    {
                        throw NumericValueOutOfRange(n);
                    }

                    return (sbyte)value;
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    JavaBigInteger bi = bigDecimalValue.toBigInteger();

                    if (bi.compareTo(MAX_TINYINT) > 0
                        || bi.compareTo(MIN_TINYINT) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (sbyte) bi.intValue();
                }

                long longVaue = n.longValue();

                if (max < longVaue || longVaue < min)
                {
                    throw HsqlConvert.NumericValueOutOfRange(n);
                }

                return (sbyte)longVaue;
            }
            public static short ToSmallInt(JavaNumber n)
            {
                if (n is JavaByte
                    || n is JavaShort)
                {
                    return n.shortValue();
                }

                const int max = JavaShort.MAX_VALUE;
                const int min = JavaShort.MIN_VALUE;

                if (n is JavaInteger)
                {
                    int value = n.intValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (short) value;
                }

                if (n is JavaLong)
                {
                    long value = n.longValue();

                    if (max < value || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (short) value;
                }

                if (n is JavaDouble || n is JavaFloat)
                {
                    double value = n.doubleValue();

                    if (JavaDouble.isNaN(value)
                        || JavaDouble.isInfinite(value)
                        || max < value
                        || value < min)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return (short) value;
                }

                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    JavaBigInteger bi = bigDecimalValue.toBigInteger();

                    if (bi.compareTo(MAX_SMALLINT) > 0
                        || bi.compareTo(MIN_SMALLINT) < 0)
                    {
                        throw HsqlConvert.NumericValueOutOfRange(n);
                    }

                    return bi.shortValue();
                }

                long l = n.longValue();

                if (max < l || l < min)
                {
                    throw HsqlConvert.NumericValueOutOfRange(n);
                }

                return (short) l;
            }
            public static float ToReal(JavaNumber n)
            {
                JavaBigDecimal bigDecimalValue = n as JavaBigDecimal;

                if (bigDecimalValue != null)
                {
                    if (bigDecimalValue.compareTo(MAX_REAL) > 0
                        || bigDecimalValue.compareTo(MIN_REAL) < 0)
                    {
                        throw NumericValueOutOfRange(n);
                    }

                    return bigDecimalValue.floatValue();
                }

                double d = n.doubleValue();

                if (JavaFloat.MAX_VALUE < d
                    || JavaFloat.MIN_VALUE > d)
                {
                    throw HsqlConvert.NumericValueOutOfRange(n);
                }

                return (float) d;
            }
Beispiel #12
0
        public static string FormatNumber(java.lang.String format, IFormatProvider provider,
                                          java.lang.Number number)
        {
            if (provider != null)
            {
                if ((provider is System.Globalization.NumberFormatInfo) ||
                    null != provider.GetFormat(
                        typeof(System.Globalization.NumberFormatInfo)))
                {
                    throw new PlatformNotSupportedException("provider with a NumberFormatInfo");
                }
            }

            int len = format.length();

            bool explicit_width = false;
            int  width          = -1;

            if (len == 1)
            {
                width = 0;
            }
            else if (len > 1 && len <= 3)
            {
                int n10 = 0;
                int n01 = (int)(format.charAt(1) - '0');
                if (len > 2)
                {
                    n10 = n01;
                    n01 = (int)(format.charAt(2) - '0');
                }
                if (n01 >= 0 && n01 <= 9 && n10 >= 0 && n10 <= 9)
                {
                    width = n10 * 10 + n01;
                }
            }

            char c     = format.charAt(0);
            bool isInt = (number is java.lang.Byte || number is java.lang.Short ||
                          number is java.lang.Integer || number is java.lang.Long);
            char   pfx = (char)0;
            string sfx = "";

            switch (Char.ToUpperInvariant(c))
            {
            case 'X':
                if (width != 0)
                {
                    pfx = '0';
                }
                if (!isInt)
                {
                    c = (char)0;
                }
                else if (number is java.lang.Byte)
                {
                    number = java.lang.Integer.valueOf(number.byteValue() & 0xFF);
                }
                else if (number is java.lang.Short)
                {
                    number = java.lang.Integer.valueOf(number.shortValue() & 0xFFFF);
                }
                break;

            case 'D':
                c   = isInt ? 'd' : (char)0;
                pfx = '0';
                break;

            case 'N':
                if (isInt)
                {
                    c     = 'd';
                    sfx   = "." + new string('0', width);
                    width = 0;
                }
                else
                {
                    c = 'f';
                    explicit_width = true;      // set width even if zero
                }
                pfx = ',';
                break;

            case 'P':
                number = java.lang.Double.valueOf(number.doubleValue() * 100.0);
                pfx    = ',';
                sfx    = "%%";
                if (len == 1)
                {
                    width = 2;
                }
                goto case 'F';

            case 'F':
                c = 'f';
                goto case 'G';

            case 'E':
            case 'G':
                if (isInt)
                {
                    number = java.lang.Double.valueOf(number.doubleValue());
                    isInt  = false;
                }
                break;

            default:
                c = (char)0;
                break;
            }

            if (c != 0 && width != -1)
            {
                var format1 = "%";
                if (pfx != 0)
                {
                    format1 += pfx;
                }
                if (explicit_width || width != 0)
                {
                    if (!isInt)
                    {
                        format1 += ".";
                    }
                    format1 += width.ToString();
                }
                format1 += c + sfx;
                return(java.lang.String.format(format1, new object[] { number }));
            }

            return((string)(object)format);
        }