ucar.nc2.iosp
Class IndexChunker

java.lang.Object
  extended by ucar.nc2.iosp.IndexChunker

public class IndexChunker
extends Object

Iterator to read/write subsets of a multidimensional array, finding the contiguous chunks. The iteration is monotonic in both src and dest positions.

Example for Integers:

  int[] read( IndexChunker index, int[] src) {
    int[] dest = new int[index.getTotalNelems()];
    while (index.hasNext()) {
      Indexer2.Chunk chunk = index.next();
      System.arraycopy(src, chunk.getSrcElem(), dest, chunk.getDestElem(), chunk.getNelems());
    }
    return dest;
  }

  int[] read( IndexChunker index, RandomAccessFile raf, long start_pos) {
    int[] dest = new int[index.getTotalNelems()];
    while (index.hasNext()) {
      Indexer2.Chunk chunk = index.next();
      raf.seek( start_pos + chunk.getSrcElem() * 4);
      raf.readInt(dest, chunk.getDestElem(), chunk.getNelems());
    }
    return dest;
  }

 // note src and dest misnamed
  void write( IndexChunker index, int[] src, RandomAccessFile raf, long start_pos) {
    while (index.hasNext()) {
      Indexer2.Chunk chunk = index.next();
      raf.seek( start_pos + chunk.getSrcElem() * 4);
      raf.writeInt(src, chunk.getDestElem(), chunk.getNelems());
    }
  }
 

Since:
Jan 2, 2008
Author:
caron

Nested Class Summary
static class IndexChunker.Chunk
          A chunk of data that is contiguous in both the source and destination.
 
Constructor Summary
IndexChunker(int[] srcShape, Section wantSection)
          Constructor
 
Method Summary
 long getTotalNelems()
          Get total number of elements in wantSection
 boolean hasNext()
          If there are more chunks to process
 IndexChunker.Chunk next()
          Get the next chunk
protected static String printa(int[] a)
           
protected static void printa(String name, int[] a)
           
protected static void printl(String name, long[] a)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndexChunker

public IndexChunker(int[] srcShape,
                    Section wantSection)
             throws InvalidRangeException
Constructor

Parameters:
srcShape - the shape of the source, eg Variable.getShape()
wantSection - the wanted section in srcShape, ie must be sibset of srcShape.
Throws:
InvalidRangeException - if wantSection is incorrect
Method Detail

getTotalNelems

public long getTotalNelems()
Get total number of elements in wantSection

Returns:
total number of elements in wantSection

hasNext

public boolean hasNext()
If there are more chunks to process

Returns:
true if there are more chunks to process

next

public IndexChunker.Chunk next()
Get the next chunk

Returns:
the next chunk

toString

public String toString()
Overrides:
toString in class Object

printa

protected static String printa(int[] a)

printa

protected static void printa(String name,
                             int[] a)

printl

protected static void printl(String name,
                             long[] a)


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.