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.
Tom Rink wrote:
Hi, How can skip or stride reading be done in the Java implementation of NetCDF? Is there an equivalent operation? Tom Rink
Hi Tom:I seem to have overlooked implementing a stride-based read, probably because the original (version 1) library doesnt have one. I will add this to the to-do list.
Meanwhile, you can get a strided subset of an Array, which will be equivilent except that you probably have to read more than you need:
Suppose you have a 3D Array, and you want to access it with stride 2, 1, and 3 in the three dimensions. The following will give you a logical view of the data with those strides, using the same backing data as the original:
Array data = var.read(); int[] shape = data.getShape(); Array dataS;Range[] ranges = new Range[] { new Range(0,shape[0]-1, 2), null, new Range(0,shape[2]-1, 3) };
try { dataS = data.section( ranges); } catch (InvalidRangeException e) { System.out.println("testStride failed == "+ e); }Note that the null second Range means “use all with stride one”. You can make a copy to get just the subset you want, so that the original data memory can be freed up:
dataS = dataS.copy(); // now is a separate data array