NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

[netCDFJava #SCH-111122]: Problem casting Array

Hi Philippe:

The most efficient solution:

  Array data = v.read();
  float[] fdata =(float []) data.get1DJavaArray(float.class);

this avoids copying if the data is already float.

However, you have a 1D array, not 2D. Since you are worried about efficiency, 
the fastest thing to do is to work with the 1D array (ie dont use a 2D array), 
and do the stride arithmetic yourself, so use fdata[j*nx+i] whenever you want 
f[j][i], where nx = data.getShape()[1]. Im assuming you know these are 2D 
arrays.

otherwise you have to allocate the 2D array and copy into it, in which case you 
might as well do

  Index ima = data.getIndex();
  int [] shape = data.getShape();
  for (int j = 0; j<shape[0];  j++)
    for (int i = 0; i<shape[1];  i++)
      f[j][i] = data.getFloat( ima.set(j,i)); 

Regards,

John

Ticket Details
==================
Ticket ID: SCH-111122
Department: Support netCDF Java
Priority: Normal
Status: Open


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