    001/*****************************************************************************
    002 * Copyright by The HDF Group.                                               *
    003 * Copyright by the Board of Trustees of the University of Illinois.         *
    004 * All rights reserved.                                                      *
    005 *                                                                           *
    006 * This file is part of the HDF Java Products distribution.                  *
    007 * The full copyright notice, including terms governing use, modification,   *
    008 * and redistribution, is contained in the files COPYING and Copyright.html. *
    009 * COPYING can be found at the root of the source code distribution tree.    *
    010 * Or, see https://support.hdfgroup.org/products/licenses.html               *
    011 * If you do not have access to either file, you may request a copy from     *
    012 * help@hdfgroup.org.                                                        *
    013 ****************************************************************************/
    014
    015package hdf.object.h5;
    016
    017import hdf.hdf5lib.H5;
    018import hdf.hdf5lib.HDF5Constants;
    019import hdf.hdf5lib.exceptions.HDF5Exception;
    020
    021public final class H5Utils {
    022
    023    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(H5Utils.class);
    024
    025    /**
    026     * Set up a hyperslab selection within a dataset.
    027     *
    028     * @param did
    029     *            IN dataset ID
    030     * @param dsetDims
    031     *            IN dimensions
    032     * @param startDims
    033     *            IN start dimensions
    034     * @param selectedStride
    035     *            IN selected stride values
    036     * @param selectedDims
    037     *            IN selected dimensions
    038     * @param spaceIDs
    039     *            IN/OUT memory and file space IDs -- spaceIDs[0]=mspace, spaceIDs[1]=fspace
    040     *
    041     * @return total number of data points selected
    042     *
    043     * @throws HDF5Exception
    044     *             If there is an error at the HDF5 library level.
    045     */
    046    public static final long selectHyperslab(long did, long[] dsetDims, long[] startDims, long[] selectedStride,
    047            long[] selectedDims, long[] spaceIDs) throws HDF5Exception {
    048        if (dsetDims == null) {
    049            log.debug("selectHyperslab(): dsetDims is null");
    050            return -1;
    051        }
    052
    053        int rank = dsetDims.length;
    054        if ((startDims != null) && (startDims.length != rank)) {
    055            log.debug("selectHyperslab(): startDims rank didn't match dsetDims rank");
    056            return -1;
    057        }
    058        if ((selectedStride != null) && (selectedStride.length != rank)) {
    059            log.debug("selectHyperslab(): selectedStride rank didn't match startDims rank");
    060            return -1;
    061        }
    062        if ((selectedDims != null) && (selectedDims.length != rank)) {
    063            log.debug("selectHyperslab(): selectedDims rank didn't match startDims rank");
    064            return -1;
    065        }
    066
    067        long lsize = 1;
    068
    069        boolean isAllSelected = true;
    070        for (int i = 0; i < rank; i++) {
    071            if (selectedDims != null) {
    072                lsize *= selectedDims[i];
    073                if (selectedDims[i] < dsetDims[i]) {
    074                    isAllSelected = false;
    075                }
    076            }
    077        }
    078
    079        log.trace("selectHyperslab(): isAllSelected={}", isAllSelected);
    080
    081        if (isAllSelected) {
    082            spaceIDs[0] = HDF5Constants.H5S_ALL;
    083            spaceIDs[1] = HDF5Constants.H5S_ALL;
    084        }
    085        else {
    086            spaceIDs[1] = H5.H5Dget_space(did);
    087
    088            // When a 1D dataspace is used for a chunked dataset, reading is very slow.
    089            //
    090            // It is a known problem within the HDF5 library.
    091            // mspace = H5.H5Screate_simple(1, lsize, null);
    092            spaceIDs[0] = H5.H5Screate_simple(rank, selectedDims, null);
    093            H5.H5Sselect_hyperslab(spaceIDs[1], HDF5Constants.H5S_SELECT_SET, startDims, selectedStride, selectedDims, null);
    094        }
    095
    096        return lsize;
    097    }
    098
    099    public static final long getTotalSelectedSpacePoints(long did, long[] dsetDims, long[] startDims,
    100            long[] selectedStride, long[] selectedDims, long[] spaceIDs) throws HDF5Exception {
    101        long totalSelectedSpacePoints = selectHyperslab(did, dsetDims, startDims, selectedStride, selectedDims, spaceIDs);
    102
    103        log.trace("getTotalSelectedSpacePoints(): selected {} points in dataset's dataspace", totalSelectedSpacePoints);
    104
    105        if (totalSelectedSpacePoints == 0) {
    106            log.debug("getTotalSelectedSpacePoints(): No data to read. Dataset or selected subset is empty.");
    107            throw new HDF5Exception("No data to read.\nEither the dataset or the selected subset is empty.");
    108        }
    109
    110        if (totalSelectedSpacePoints < Integer.MIN_VALUE || totalSelectedSpacePoints > Integer.MAX_VALUE) {
    111            log.debug("getTotalSelectedSpacePoints(): totalSelectedSpacePoints outside valid Java int range; unsafe cast");
    112            throw new HDF5Exception("Invalid int size");
    113        }
    114
    115        if (log.isDebugEnabled()) {
    116            // check is storage space is allocated
    117            try {
    118                long ssize = H5.H5Dget_storage_size(did);
    119                log.trace("getTotalSelectedSpacePoints(): Storage space allocated = {} bytes", ssize);
    120            }
    121            catch (Exception ex) {
    122                log.debug("getTotalSelectedSpacePoints(): check if storage space is allocated:", ex);
    123            }
    124        }
    125
    126        return totalSelectedSpacePoints;
    127    }
    128
    129}