Commit 536b17af authored by brinn's avatar brinn

Add classes HDF5ArrayBlockParams and HDF5ArrayBlockParamsBuilder and primitive...

Add classes HDF5ArrayBlockParams and HDF5ArrayBlockParamsBuilder and primitive methods readMDArray() using it.
parent e5e3b55c
/*
* Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.hdf5;
import ch.systemsx.cisd.hdf5.exceptions.HDF5SpaceRankMismatch;
import hdf.hdf5lib.exceptions.HDF5JavaException;
/**
* A parameter class to specify the block and slice to read from or write to a multi-dimensional array.
*/
public final class HDF5ArrayBlockParams
{
int[] blockDimensions;
long[] offset;
long[] boundIndexArray;
IndexMap boundIndexMap;
int[] getBlockDimensions()
{
return blockDimensions;
}
long[] getOffset()
{
if (blockDimensions == null)
{
return null;
}
if (offset == null)
{
offset = new long[blockDimensions.length];
}
return offset;
}
long[] getBoundIndexArray()
{
return boundIndexArray;
}
IndexMap getBoundIndexMap()
{
return boundIndexMap;
}
boolean hasBlock()
{
return blockDimensions != null;
}
boolean hasSlice()
{
return boundIndexArray != null || boundIndexMap != null;
}
//
// Public interface
//
/**
* Sets an array block dimensions.
*
* @param dimensions The block dimensions.
*/
public HDF5ArrayBlockParams block(int... dimensions)
{
this.blockDimensions = dimensions;
return this;
}
/**
* Sets the array block indices.
*
* @param blockIndex The block index in each dimension (offset: multiply with the
* <var>blockDimensions</var> in the according dimension).
*/
public HDF5ArrayBlockParams index(long... blockIndex)
{
if (blockDimensions == null)
{
throw new HDF5JavaException("No block dimensions set");
}
if (blockDimensions.length != blockIndex.length)
{
throw new HDF5SpaceRankMismatch(blockDimensions.length, blockIndex.length);
}
offset = new long[blockDimensions.length];
for (int i = 0; i < offset.length; ++i)
{
offset[i] = blockIndex[i] * blockDimensions[i];
}
return this;
}
/**
* Sets the array block offset.
*
* @param offset The offset in the array to start reading from in each dimension.
*/
public HDF5ArrayBlockParams offset(long... offset)
{
if (blockDimensions == null)
{
throw new HDF5JavaException("No block dimensions set");
}
if (blockDimensions.length != offset.length)
{
throw new HDF5SpaceRankMismatch(blockDimensions.length, offset.length);
}
this.offset = offset;
return this;
}
/**
* Sets an array slice.
*
* @param boundIndices The array containing the values of the bound indices at the respective
* index positions, and -1 at the free index positions. For example an array of
* <code>new long[] { -1, -1, 5, -1, 7, -1 }</code> has 2 and 4 as bound indices and
* binds them to the values 5 and 7, respectively.
*/
public HDF5ArrayBlockParams slice(long... boundIndices)
{
boundIndexArray = boundIndices;
return this;
}
/**
* Sets an array slice.
*
* @param boundIndices The mapping of indices to index values which should be bound. For example
* a map of <code>new IndexMap().mapTo(2, 5).mapTo(4, 7)</code> has 2 and 4 as bound
* indices and binds them to the values 5 and 7, respectively.
*/
public HDF5ArrayBlockParams slice(IndexMap boundIndices)
{
boundIndexMap = boundIndices;
return this;
}
}
/*
* Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.hdf5;
/**
* A builder class for {@link HDF5ArrayBlockParams}.
*/
public class HDF5ArrayBlockParamsBuilder
{
/**
* Creates a parameter that reads the complete array.
*/
public static HDF5ArrayBlockParams array()
{
return new HDF5ArrayBlockParams();
}
/**
* Creates a parameter that reads an array block.
*
* @param dimensions The block dimensions.
*/
public static HDF5ArrayBlockParams block(int... dimensions)
{
final HDF5ArrayBlockParams params = new HDF5ArrayBlockParams();
params.blockDimensions = dimensions;
return params;
}
/**
* Creates a parameter that reads an array slice.
*
* @param boundIndices The array containing the values of the bound indices at the respective
* index positions, and -1 at the free index positions. For example an array of
* <code>new long[] { -1, -1, 5, -1, 7, -1 }</code> has 2 and 4 as bound indices and
* binds them to the values 5 and 7, respectively.
*/
public static HDF5ArrayBlockParams slice(long... boundIndices)
{
final HDF5ArrayBlockParams params = new HDF5ArrayBlockParams();
params.boundIndexArray = boundIndices;
return params;
}
/**
* Creates a parameter that reads an array slice.
*
* @param boundIndices The mapping of indices to index values which should be bound. For example
* a map of <code>new IndexMap().mapTo(2, 5).mapTo(4, 7)</code> has 2 and 4 as bound
* indices and binds them to the values 5 and 7, respectively.
*/
public static HDF5ArrayBlockParams slice(IndexMap boundIndices)
{
final HDF5ArrayBlockParams params = new HDF5ArrayBlockParams();
params.boundIndexMap = boundIndices;
return params;
}
}
......@@ -553,6 +553,22 @@ class HDF5ByteReader implements IHDF5ByteReader
return baseReader.runner.call(readCallable);
}
MDByteArray readMDArray(final HDF5DataSet dataSet)
{
assert dataSet != null;
baseReader.checkOpen();
final ICallableWithCleanUp<MDByteArray> readCallable = new ICallableWithCleanUp<MDByteArray>()
{
@Override
public MDByteArray call(ICleanUpRegistry registry)
{
return readByteMDArray(dataSet.getDataSetId(), registry);
}
};
return baseReader.runner.call(readCallable);
}
MDByteArray readByteMDArray(long dataSetId, ICleanUpRegistry registry)
{
try
......@@ -603,6 +619,72 @@ class HDF5ByteReader implements IHDF5ByteReader
}
}
@Override
public MDByteArray readMDArray(String objectPath, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(objectPath, params.getBoundIndexArray());
} else
{
return readMDArraySlice(objectPath, params.getBoundIndexMap());
}
}
return readMDArray(objectPath);
}
@Override
public MDByteArray readMDArray(HDF5DataSet dataSet, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(dataSet, params.getBoundIndexArray());
} else
{
return readMDArraySlice(dataSet, params.getBoundIndexMap());
}
}
return readMDArray(dataSet);
}
@Override
public MDByteArray readSlicedMDArrayBlock(String objectPath, int[] blockDimensions,
long[] blockNumber, IndexMap boundIndices)
......
......@@ -553,6 +553,22 @@ class HDF5DoubleReader implements IHDF5DoubleReader
return baseReader.runner.call(readCallable);
}
MDDoubleArray readMDArray(final HDF5DataSet dataSet)
{
assert dataSet != null;
baseReader.checkOpen();
final ICallableWithCleanUp<MDDoubleArray> readCallable = new ICallableWithCleanUp<MDDoubleArray>()
{
@Override
public MDDoubleArray call(ICleanUpRegistry registry)
{
return readDoubleMDArray(dataSet.getDataSetId(), registry);
}
};
return baseReader.runner.call(readCallable);
}
MDDoubleArray readDoubleMDArray(long dataSetId, ICleanUpRegistry registry)
{
try
......@@ -603,6 +619,72 @@ class HDF5DoubleReader implements IHDF5DoubleReader
}
}
@Override
public MDDoubleArray readMDArray(String objectPath, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(objectPath, params.getBoundIndexArray());
} else
{
return readMDArraySlice(objectPath, params.getBoundIndexMap());
}
}
return readMDArray(objectPath);
}
@Override
public MDDoubleArray readMDArray(HDF5DataSet dataSet, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(dataSet, params.getBoundIndexArray());
} else
{
return readMDArraySlice(dataSet, params.getBoundIndexMap());
}
}
return readMDArray(dataSet);
}
@Override
public MDDoubleArray readSlicedMDArrayBlock(String objectPath, int[] blockDimensions,
long[] blockNumber, IndexMap boundIndices)
......
......@@ -553,6 +553,22 @@ class HDF5FloatReader implements IHDF5FloatReader
return baseReader.runner.call(readCallable);
}
MDFloatArray readMDArray(final HDF5DataSet dataSet)
{
assert dataSet != null;
baseReader.checkOpen();
final ICallableWithCleanUp<MDFloatArray> readCallable = new ICallableWithCleanUp<MDFloatArray>()
{
@Override
public MDFloatArray call(ICleanUpRegistry registry)
{
return readFloatMDArray(dataSet.getDataSetId(), registry);
}
};
return baseReader.runner.call(readCallable);
}
MDFloatArray readFloatMDArray(long dataSetId, ICleanUpRegistry registry)
{
try
......@@ -603,6 +619,72 @@ class HDF5FloatReader implements IHDF5FloatReader
}
}
@Override
public MDFloatArray readMDArray(String objectPath, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(objectPath, params.getBoundIndexArray());
} else
{
return readMDArraySlice(objectPath, params.getBoundIndexMap());
}
}
return readMDArray(objectPath);
}
@Override
public MDFloatArray readMDArray(HDF5DataSet dataSet, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(dataSet, params.getBoundIndexArray());
} else
{
return readMDArraySlice(dataSet, params.getBoundIndexMap());
}
}
return readMDArray(dataSet);
}
@Override
public MDFloatArray readSlicedMDArrayBlock(String objectPath, int[] blockDimensions,
long[] blockNumber, IndexMap boundIndices)
......
......@@ -553,6 +553,22 @@ class HDF5IntReader implements IHDF5IntReader
return baseReader.runner.call(readCallable);
}
MDIntArray readMDArray(final HDF5DataSet dataSet)
{
assert dataSet != null;
baseReader.checkOpen();
final ICallableWithCleanUp<MDIntArray> readCallable = new ICallableWithCleanUp<MDIntArray>()
{
@Override
public MDIntArray call(ICleanUpRegistry registry)
{
return readIntMDArray(dataSet.getDataSetId(), registry);
}
};
return baseReader.runner.call(readCallable);
}
MDIntArray readIntMDArray(long dataSetId, ICleanUpRegistry registry)
{
try
......@@ -603,6 +619,72 @@ class HDF5IntReader implements IHDF5IntReader
}
}
@Override
public MDIntArray readMDArray(String objectPath, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(objectPath, params.getBoundIndexArray());
} else
{
return readMDArraySlice(objectPath, params.getBoundIndexMap());
}
}
return readMDArray(objectPath);
}
@Override
public MDIntArray readMDArray(HDF5DataSet dataSet, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else
{
return readSlicedMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexMap());
}
}
return readMDArrayBlockWithOffset(dataSet, params.getBlockDimensions(), params.getOffset());
}
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readMDArraySlice(dataSet, params.getBoundIndexArray());
} else
{
return readMDArraySlice(dataSet, params.getBoundIndexMap());
}
}
return readMDArray(dataSet);
}
@Override
public MDIntArray readSlicedMDArrayBlock(String objectPath, int[] blockDimensions,
long[] blockNumber, IndexMap boundIndices)
......
......@@ -553,6 +553,22 @@ class HDF5LongReader implements IHDF5LongReader
return baseReader.runner.call(readCallable);
}
MDLongArray readMDArray(final HDF5DataSet dataSet)
{
assert dataSet != null;
baseReader.checkOpen();
final ICallableWithCleanUp<MDLongArray> readCallable = new ICallableWithCleanUp<MDLongArray>()
{
@Override
public MDLongArray call(ICleanUpRegistry registry)
{
return readLongMDArray(dataSet.getDataSetId(), registry);
}
};
return baseReader.runner.call(readCallable);
}
MDLongArray readLongMDArray(long dataSetId, ICleanUpRegistry registry)
{
try
......@@ -603,6 +619,72 @@ class HDF5LongReader implements IHDF5LongReader
}
}
@Override
public MDLongArray readMDArray(String objectPath, HDF5ArrayBlockParams params)
{
if (params.hasBlock())
{
if (params.hasSlice())
{
if (params.getBoundIndexArray() != null)
{
return readSlicedMDArrayBlockWithOffset(objectPath, params.getBlockDimensions(), params.getOffset(), params.getBoundIndexArray());
} else