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

Hi Christine:

Sorry, ignore previous post.

Array.getDouble() is really a protected method to get at the direct storage underneath. You dont want to use it.

To access the individual array elements, you need to cast to ArrayDouble.D1 or ArrayDouble.D2, and use theget() call.

Heres an example that should explain it:

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

    System.out.printf("%n%s%n", NCdumpW.printArray(a3, "test a3", null));
    Array a2 = a3.slice(0,1);

System.out.printf("%n%s%n", NCdumpW.printArray(a2, "a3.slice(0,1)", null));

    Array a1 = a2.slice(0,1);

System.out.printf("%n%s%n%n", NCdumpW.printArray(a1, "a2.slice(0,1)", null));

    ArrayDouble.D2 twoD = (ArrayDouble.D2) a2;
    System.out.printf("wrong= %f%n", a2.getDouble(0));
    System.out.printf("right= %f%n", twoD.get(0, 0));

    ArrayDouble.D1 oneD = (ArrayDouble.D1) a1;
    System.out.printf("wrong= %f%n", a1.getDouble(0));
    System.out.printf("right= %f%n", oneD.get(0));

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);

This is what I see:

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

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 


Dr. Christine Smit
NASA Goddard, Building 32, N126-12
Code 610.2

netcdf-java mailing list
For list information or to unsubscribe, visit:

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