Re: [netcdf-java] indexing problems with sliced array

On 7/19/2012 11:22 AM, Smit, Christine E. (GSFC-610.2)[TELOPHASE CORP] wrote:
I'm doing a double slice on a three-dimensional variable to get a 1-D slice and 
then trying to read out the values. Unfortunately, indexing seems to give me 
values from the full original array rather than the slice I'm working with.

Array twoD = array.slice(0, 1);
Array oneD = twoD.slice(0,1);
System.out.println(oneD.getDouble(0));
System.out.println(oneD);
System.out.println(array.getDouble(0));

This is what I see:

178.0
-9999 -9999 -9999 81 66 93 103 -9999 -9999 -9999 -9999 -9999
178.0

I would expect the first print statement to print -9999 since that is the first 
element of the slice I'm working on. So this kind of looks like a bug to me. 
How do I get the first element of my slice rather than of the entire parent 
array?
Hi Christine:

It look s ok to me. heres an example you can modify. if you see a problem send me the program.

  public void testSlice3D() throws InvalidRangeException {
    Array a = Array.makeArray(DataType.DOUBLE, 1000, 0.0, 1.0);
    Array a3 = a.reshape(new int[] {10,10,10});

    NCdumpW.printArray(a3);

    Array twoD = a3.slice(0,1);
    Array oneD = twoD.slice(0,1);

    System.out.printf("%n%f%n", oneD.getDouble(0));
    System.out.printf("%s%n", oneD);
    System.out.printf("%f%n%n", a3.getDouble(0));
    NCdumpW.printArray(oneD);
   }

Note the signature for slice:

  /**
   * Create a new Array using same backing store as this Array, by
* fixing the specified dimension at the specified index value. This reduces rank by 1.
   *
   * @param dim   which dimension to fix
   * @param value at what index value
   * @return a new Array
   */
  public Array slice(int dim, int value);

John



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