Re: [idvdevelopers] Bug in ucar.unidata.data.DataUtil

Guys - oops... the method I sent in the original report was not
quite right.    This is what Ghansham and I believe the toFloatArray()
method should look like.   The further change is near the beginning,
setting fromClass.  Previously, the method was basing the originating
class type on the destination class type, which we know is float!

Previous two lines:

        Object dst       = arr.get1DJavaArray(float.class);
        Class  fromClass = dst.getClass().getComponentType();


    /**
     * Get the 1D values for an array as floats.
     *
     * @param arr   Array of values
     * @return  float representation
     */
    public static float[] toFloatArray(Array arr) {
        Class fromClass = arr.getElementType();
        if (fromClass.equals(float.class)) {
            //It should always be a float
            return (float[]) dst;
        } else {
            float[] values = new float[(int) arr.getSize()];
            boolean isUnsigned = arr.isUnsigned();
            if (fromClass.equals(byte.class)) {
                byte[] fromArray = (byte[]) dst;
                for (int i = 0; i < fromArray.length; ++i) {
                    if (isUnsigned) {
                        values[i] = (int) fromArray[i] & 0xFF;
                    } else {
                        values[i] = fromArray[i];
                    }
                }
            } else if (fromClass.equals(short.class)) {
                short[] fromArray = (short[]) dst;
                for (int i = 0; i < fromArray.length; ++i) {
                    if (isUnsigned) {
                        values[i] = (int) fromArray[i] & 0xFFFF;
                    } else {
                        values[i] = fromArray[i];
                    }
                }
            } else if (fromClass.equals(int.class)) {
                int[] fromArray = (int[]) dst;
                for (int i = 0; i < fromArray.length; ++i) {
                    if (isUnsigned) {
                        values[i] = (long) fromArray[i] & 0xFFFFFFFF;
                    } else {
                        values[i] = fromArray[i];
                    }
                }
            } else if (fromClass.equals(double.class)) {
                double[] fromArray = (double[]) dst;
                for (int i = 0; i < fromArray.length; ++i) {
                    values[i] = (float) fromArray[i];
                }
            } else {
                throw new IllegalArgumentException("Unknown array type:"
                        + fromClass.getName());
            }
            return values;
        }

    }

--
Tommy Jasmin
Space Science and Engineering Center
University of Wisconsin, Madison
1225 West Dayton Street, Madison, WI 53706



  • 2011 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the idvdevelopers archives: