This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Scheller, Robert M. wrote:
Dear John Caron: I am a grad student at UW-Madison and I have been attempting to work with a suite of VEMAP climate data files in the netCDF format. Although the User's Manual for NetCDF Java has been helpful, I am utterly stuck. It appears fairly simple to create the NetcdFile class. However, I want only a very small subsection of the data. It appears that ma2.Index would provide this function but I haven't been able to make the logical leap from creating the NetcdFile class to creating a MultiArray that I can index on. Specifically, it isn't clear to me how to use the MultiArray read() functionson a netcdfile.
Hi Rob: you dont directly manipulate Index, the read() is done on the Variable to get an in-memory Array, see example below.
Also, what is the constructor for the Range
class? Thanks very much for any information, advise orreferences you could provide.
see http://www.unidata.ucar.edu/packages/netcdf-java/ and print out the "user documentation".
there's also online javadoc there, you can look up the Range class Sincerely,
Rob Scheller Robert M. Scheller Dept. Forest Ecology & Management University of Wisconsin - Madison 1630 Linden Dr., Madison, WI 53706 w: 608.265.6321 Visit my webpage! http://landscape.forest.wisc.edu/staff/rob/ Forest Landscape Ecology Lab http://landscape.forest.wisc.edu
Here's an example that 1) reads a 2x2 subset of an array and then 2) reads the entire array and subsets it.
package test.nc2;
import ucar.ma2.*;
import ucar.nc2.*;
import java.io.*;
public class ExampleRead {
public static void main(String [] args) {
new ExampleRead();
}
public ExampleRead() {
try {
NetcdfFile ncfile = new
NetcdfFile("E:/metapps/src/test/nc2/mydata.nc");
System.out.println( "ncfile "+ ncfile);
Variable temp = ncfile.findVariable("temperature");
System.out.println( "temperature "+ temp);
// read subset
int[] origin = {1,3};// start at index [1,3]
int[] shape = {2,2}; // 2x2 array
Array As = temp.read( origin, shape);
System.out.println( "physical subset");
printArray( As);
// read all the data and logically subset it
Array A = temp.read();
Range[] r = new Range[2];
r[0] = new Range(1,2);
r[1] = new Range(3,4);
Array Ap = A.section( r); // logical subset
System.out.println( "logical subset");
printArray( Ap);
} catch (InvalidRangeException e) {
System.err.println("ERROR reading file "+e);
return;
} catch (IOException e) {
System.err.println("ERROR reading file "+e);
return;
}
}
private void printArray( Array a) {
IndexIterator iter = a.getIndexIterator();
while (iter.hasNext())
System.out.println( iter.getDoubleNext()+ ",");
}
}