int minTileX = minX / tileWidth - (minX < 0 ? (-minX % tileWidth > 0 ? 1 : 0): 0);
int minTileY = minY / tileHeight - (minY < 0 ? (-minY % tileHeight > 0 ? 1 : 0): 0);
int maxTileX = maxX / tileWidth - (maxX < 0 ? (-maxX % tileWidth > 0 ? 1 : 0): 0);
int maxTileY = maxY / tileHeight - (maxY < 0 ? (-maxY % tileHeight > 0 ? 1 : 0): 0);
final Index matrixIndex = matrix.getIndex();
final int indexing[] = new int[numDimensions];
// Update the NetCDF array indexing to set values for a specific 2D slice
updateIndexing(indexing, gridCoverage);
// ----------------
// Fill data matrix
// ----------------
// Loop over bands using a RandomIter
final RandomIter data = RandomIterFactory.create(ri, null);
for (int tileY = minTileY; tileY <= maxTileY; tileY++) {
for (int tileX = minTileX; tileX <= maxTileX; tileX++) {
for (int trow = 0; trow < tileHeight; trow++) {
int j = (tileY * tileHeight) + trow;
if ((j >= minY) && (j <= maxY)) {
for (int tcol = 0; tcol < tileWidth; tcol++) {
int col = (tileX * tileWidth) + tcol;
if ((col >= minX) && (col <= maxX)) {
int k = col;
final int yPos = height - j + minY - 1;
// Simply setting lat and lon
indexing[numDimensions - 1] = k - minX;
indexing[numDimensions - 2] = yPos;
matrixIndex.set(indexing);
// Write data
switch (netCDFDataType) {
case BYTE:
byte sampleByte = (byte) data.getSampleFloat(k, j, 0);