LCOV - code coverage report
Current view: top level - frmts/hdf5 - hdf5imagedataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 691 884 78.2 %
Date: 2025-02-20 10:14:44 Functions: 32 33 97.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Hierarchical Data Format Release 5 (HDF5)
       4             :  * Purpose:  Read subdatasets of HDF5 file.
       5             :  * Author:   Denis Nadeau <denis.nadeau@gmail.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
       9             :  * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #include "hdf5_api.h"
      15             : 
      16             : #include "cpl_float.h"
      17             : #include "cpl_string.h"
      18             : #include "gdal_frmts.h"
      19             : #include "gdal_pam.h"
      20             : #include "gdal_priv.h"
      21             : #include "gh5_convenience.h"
      22             : #include "hdf5dataset.h"
      23             : #include "hdf5drivercore.h"
      24             : #include "ogr_spatialref.h"
      25             : #include "../mem/memdataset.h"
      26             : 
      27             : #include <algorithm>
      28             : 
      29             : class HDF5ImageDataset final : public HDF5Dataset
      30             : {
      31             :     typedef enum
      32             :     {
      33             :         UNKNOWN_PRODUCT = 0,
      34             :         CSK_PRODUCT
      35             :     } Hdf5ProductType;
      36             : 
      37             :     typedef enum
      38             :     {
      39             :         PROD_UNKNOWN = 0,
      40             :         PROD_CSK_L0,
      41             :         PROD_CSK_L1A,
      42             :         PROD_CSK_L1B,
      43             :         PROD_CSK_L1C,
      44             :         PROD_CSK_L1D
      45             :     } HDF5CSKProductEnum;
      46             : 
      47             :     friend class HDF5ImageRasterBand;
      48             : 
      49             :     OGRSpatialReference m_oSRS{};
      50             :     OGRSpatialReference m_oGCPSRS{};
      51             :     std::vector<gdal::GCP> m_aoGCPs{};
      52             : 
      53             :     hsize_t *dims;
      54             :     hsize_t *maxdims;
      55             :     HDF5GroupObjects *poH5Objects;
      56             :     int ndims;
      57             :     int dimensions;
      58             :     hid_t dataset_id;
      59             :     hid_t dataspace_id;
      60             :     hid_t native;
      61             : #ifdef HDF5_HAVE_FLOAT16
      62             :     bool m_bConvertFromFloat16 = false;
      63             : #endif
      64             :     Hdf5ProductType iSubdatasetType;
      65             :     HDF5CSKProductEnum iCSKProductType;
      66             :     double adfGeoTransform[6];
      67             :     bool bHasGeoTransform;
      68             :     int m_nXIndex = -1;
      69             :     int m_nYIndex = -1;
      70             :     int m_nOtherDimIndex = -1;
      71             : 
      72             :     int m_nBlockXSize = 0;
      73             :     int m_nBlockYSize = 0;
      74             :     int m_nBandChunkSize = 1;  //! Number of bands in a chunk
      75             : 
      76             :     enum WholeBandChunkOptim
      77             :     {
      78             :         WBC_DETECTION_IN_PROGRESS,
      79             :         WBC_DISABLED,
      80             :         WBC_ENABLED,
      81             :     };
      82             : 
      83             :     //! Flag to detect if the read pattern of HDF5ImageRasterBand::IRasterIO()
      84             :     // is whole band after whole band.
      85             :     WholeBandChunkOptim m_eWholeBandChunkOptim = WBC_DETECTION_IN_PROGRESS;
      86             :     //! Value of nBand during last HDF5ImageRasterBand::IRasterIO() call
      87             :     int m_nLastRasterIOBand = -1;
      88             :     //! Value of nXOff during last HDF5ImageRasterBand::IRasterIO() call
      89             :     int m_nLastRasterIOXOff = -1;
      90             :     //! Value of nYOff during last HDF5ImageRasterBand::IRasterIO() call
      91             :     int m_nLastRasterIOYOff = -1;
      92             :     //! Value of nXSize during last HDF5ImageRasterBand::IRasterIO() call
      93             :     int m_nLastRasterIOXSize = -1;
      94             :     //! Value of nYSize during last HDF5ImageRasterBand::IRasterIO() call
      95             :     int m_nLastRasterIOYSize = -1;
      96             :     //! Value such that m_abyBandChunk represent band data in the range
      97             :     // [m_iCurrentBandChunk * m_nBandChunkSize, (m_iCurrentBandChunk+1) * m_nBandChunkSize[
      98             :     int m_iCurrentBandChunk = -1;
      99             :     //! Cached values (in native data type) for bands in the range
     100             :     // [m_iCurrentBandChunk * m_nBandChunkSize, (m_iCurrentBandChunk+1) * m_nBandChunkSize[
     101             :     std::vector<GByte> m_abyBandChunk{};
     102             : 
     103             :     CPLErr CreateODIMH5Projection();
     104             : 
     105             :   public:
     106             :     HDF5ImageDataset();
     107             :     virtual ~HDF5ImageDataset();
     108             : 
     109             :     CPLErr CreateProjections();
     110             :     static GDALDataset *Open(GDALOpenInfo *);
     111             :     static int Identify(GDALOpenInfo *);
     112             : 
     113             :     const OGRSpatialReference *GetSpatialRef() const override;
     114             :     virtual int GetGCPCount() override;
     115             :     const OGRSpatialReference *GetGCPSpatialRef() const override;
     116             :     virtual const GDAL_GCP *GetGCPs() override;
     117             :     virtual CPLErr GetGeoTransform(double *padfTransform) override;
     118             : 
     119             :     CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
     120             :                      int nYSize, void *pData, int nBufXSize, int nBufYSize,
     121             :                      GDALDataType eBufType, int nBandCount,
     122             :                      BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
     123             :                      GSpacing nLineSpace, GSpacing nBandSpace,
     124             :                      GDALRasterIOExtraArg *psExtraArg) override;
     125             : 
     126             :     const char *GetMetadataItem(const char *pszName,
     127             :                                 const char *pszDomain = "") override;
     128             : 
     129         159 :     Hdf5ProductType GetSubdatasetType() const
     130             :     {
     131         159 :         return iSubdatasetType;
     132             :     }
     133             : 
     134           6 :     HDF5CSKProductEnum GetCSKProductType() const
     135             :     {
     136           6 :         return iCSKProductType;
     137             :     }
     138             : 
     139         159 :     int IsComplexCSKL1A() const
     140             :     {
     141         165 :         return GetSubdatasetType() == CSK_PRODUCT &&
     142         165 :                GetCSKProductType() == PROD_CSK_L1A && ndims == 3;
     143             :     }
     144             : 
     145         632 :     int GetYIndex() const
     146             :     {
     147         632 :         return m_nYIndex;
     148             :     }
     149             : 
     150         772 :     int GetXIndex() const
     151             :     {
     152         772 :         return m_nXIndex;
     153             :     }
     154             : 
     155             :     /**
     156             :      * Identify if the subdataset has a known product format
     157             :      * It stores a product identifier in iSubdatasetType,
     158             :      * UNKNOWN_PRODUCT, if it isn't a recognizable format.
     159             :      */
     160             :     void IdentifyProductType();
     161             : 
     162             :     /**
     163             :      * Captures Geolocation information from a COSMO-SKYMED
     164             :      * file.
     165             :      * The geoid will always be WGS84
     166             :      * The projection type may be UTM or UPS, depending on the
     167             :      * latitude from the center of the image.
     168             :      * @param iProductType type of HDF5 subproduct, see HDF5CSKProduct
     169             :      */
     170             :     void CaptureCSKGeolocation(int iProductType);
     171             : 
     172             :     /**
     173             :      * Get Geotransform information for COSMO-SKYMED files
     174             :      * In case of success it stores the transformation
     175             :      * in adfGeoTransform. In case of failure it doesn't
     176             :      * modify adfGeoTransform
     177             :      * @param iProductType type of HDF5 subproduct, see HDF5CSKProduct
     178             :      */
     179             :     void CaptureCSKGeoTransform(int iProductType);
     180             : 
     181             :     /**
     182             :      * @param iProductType type of HDF5 subproduct, see HDF5CSKProduct
     183             :      */
     184             :     void CaptureCSKGCPs(int iProductType);
     185             : };
     186             : 
     187             : /************************************************************************/
     188             : /* ==================================================================== */
     189             : /*                              HDF5ImageDataset                        */
     190             : /* ==================================================================== */
     191             : /************************************************************************/
     192             : 
     193             : /************************************************************************/
     194             : /*                           HDF5ImageDataset()                         */
     195             : /************************************************************************/
     196          53 : HDF5ImageDataset::HDF5ImageDataset()
     197             :     : dims(nullptr), maxdims(nullptr), poH5Objects(nullptr), ndims(0),
     198             :       dimensions(0), dataset_id(-1), dataspace_id(-1), native(-1),
     199             :       iSubdatasetType(UNKNOWN_PRODUCT), iCSKProductType(PROD_UNKNOWN),
     200          53 :       bHasGeoTransform(false)
     201             : {
     202          53 :     m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
     203          53 :     m_oGCPSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
     204          53 :     adfGeoTransform[0] = 0.0;
     205          53 :     adfGeoTransform[1] = 1.0;
     206          53 :     adfGeoTransform[2] = 0.0;
     207          53 :     adfGeoTransform[3] = 0.0;
     208          53 :     adfGeoTransform[4] = 0.0;
     209          53 :     adfGeoTransform[5] = 1.0;
     210          53 : }
     211             : 
     212             : /************************************************************************/
     213             : /*                            ~HDF5ImageDataset()                       */
     214             : /************************************************************************/
     215         106 : HDF5ImageDataset::~HDF5ImageDataset()
     216             : {
     217             :     HDF5_GLOBAL_LOCK();
     218             : 
     219          53 :     FlushCache(true);
     220             : 
     221          53 :     if (dataset_id > 0)
     222          53 :         H5Dclose(dataset_id);
     223          53 :     if (dataspace_id > 0)
     224          53 :         H5Sclose(dataspace_id);
     225          53 :     if (native > 0)
     226          53 :         H5Tclose(native);
     227             : 
     228          53 :     CPLFree(dims);
     229          53 :     CPLFree(maxdims);
     230         106 : }
     231             : 
     232             : /************************************************************************/
     233             : /* ==================================================================== */
     234             : /*                            Hdf5imagerasterband                       */
     235             : /* ==================================================================== */
     236             : /************************************************************************/
     237             : class HDF5ImageRasterBand final : public GDALPamRasterBand
     238             : {
     239             :     friend class HDF5ImageDataset;
     240             : 
     241             :     bool bNoDataSet = false;
     242             :     double dfNoDataValue = -9999.0;
     243             :     bool m_bHasOffset = false;
     244             :     double m_dfOffset = 0.0;
     245             :     bool m_bHasScale = false;
     246             :     double m_dfScale = 1.0;
     247             :     int m_nIRasterIORecCounter = 0;
     248             : 
     249             :   public:
     250             :     HDF5ImageRasterBand(HDF5ImageDataset *, int, GDALDataType);
     251             :     virtual ~HDF5ImageRasterBand();
     252             : 
     253             :     virtual CPLErr IReadBlock(int, int, void *) override;
     254             :     virtual double GetNoDataValue(int *) override;
     255             :     virtual double GetOffset(int *) override;
     256             :     virtual double GetScale(int *) override;
     257             :     // virtual CPLErr IWriteBlock( int, int, void * );
     258             : 
     259             :     CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
     260             :                      int nYSize, void *pData, int nBufXSize, int nBufYSize,
     261             :                      GDALDataType eBufType, GSpacing nPixelSpace,
     262             :                      GSpacing nLineSpace,
     263             :                      GDALRasterIOExtraArg *psExtraArg) override;
     264             : };
     265             : 
     266             : /************************************************************************/
     267             : /*                        ~HDF5ImageRasterBand()                        */
     268             : /************************************************************************/
     269             : 
     270         230 : HDF5ImageRasterBand::~HDF5ImageRasterBand()
     271             : {
     272         230 : }
     273             : 
     274             : /************************************************************************/
     275             : /*                           HDF5ImageRasterBand()                      */
     276             : /************************************************************************/
     277         115 : HDF5ImageRasterBand::HDF5ImageRasterBand(HDF5ImageDataset *poDSIn, int nBandIn,
     278         115 :                                          GDALDataType eType)
     279             : {
     280         115 :     poDS = poDSIn;
     281         115 :     nBand = nBandIn;
     282         115 :     eDataType = eType;
     283         115 :     nBlockXSize = poDSIn->m_nBlockXSize;
     284         115 :     nBlockYSize = poDSIn->m_nBlockYSize;
     285             : 
     286             :     // netCDF convention for nodata
     287         115 :     bNoDataSet =
     288         115 :         GH5_FetchAttribute(poDSIn->dataset_id, "_FillValue", dfNoDataValue);
     289         115 :     if (!bNoDataSet)
     290         106 :         dfNoDataValue = -9999.0;
     291             : 
     292             :     // netCDF conventions for scale and offset
     293         115 :     m_bHasOffset =
     294         115 :         GH5_FetchAttribute(poDSIn->dataset_id, "add_offset", m_dfOffset);
     295         115 :     if (!m_bHasOffset)
     296         114 :         m_dfOffset = 0.0;
     297         115 :     m_bHasScale =
     298         115 :         GH5_FetchAttribute(poDSIn->dataset_id, "scale_factor", m_dfScale);
     299         115 :     if (!m_bHasScale)
     300         114 :         m_dfScale = 1.0;
     301         115 : }
     302             : 
     303             : /************************************************************************/
     304             : /*                           GetNoDataValue()                           */
     305             : /************************************************************************/
     306          48 : double HDF5ImageRasterBand::GetNoDataValue(int *pbSuccess)
     307             : 
     308             : {
     309          48 :     if (bNoDataSet)
     310             :     {
     311           3 :         if (pbSuccess)
     312           3 :             *pbSuccess = bNoDataSet;
     313             : 
     314           3 :         return dfNoDataValue;
     315             :     }
     316             : 
     317          45 :     return GDALPamRasterBand::GetNoDataValue(pbSuccess);
     318             : }
     319             : 
     320             : /************************************************************************/
     321             : /*                             GetOffset()                              */
     322             : /************************************************************************/
     323             : 
     324          22 : double HDF5ImageRasterBand::GetOffset(int *pbSuccess)
     325             : 
     326             : {
     327          22 :     if (m_bHasOffset)
     328             :     {
     329           1 :         if (pbSuccess)
     330           1 :             *pbSuccess = m_bHasOffset;
     331             : 
     332           1 :         return m_dfOffset;
     333             :     }
     334             : 
     335          21 :     return GDALPamRasterBand::GetOffset(pbSuccess);
     336             : }
     337             : 
     338             : /************************************************************************/
     339             : /*                             GetScale()                               */
     340             : /************************************************************************/
     341             : 
     342          22 : double HDF5ImageRasterBand::GetScale(int *pbSuccess)
     343             : 
     344             : {
     345          22 :     if (m_bHasScale)
     346             :     {
     347           1 :         if (pbSuccess)
     348           1 :             *pbSuccess = m_bHasScale;
     349             : 
     350           1 :         return m_dfScale;
     351             :     }
     352             : 
     353          21 :     return GDALPamRasterBand::GetScale(pbSuccess);
     354             : }
     355             : 
     356             : /************************************************************************/
     357             : /*                             IReadBlock()                             */
     358             : /************************************************************************/
     359         103 : CPLErr HDF5ImageRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
     360             :                                        void *pImage)
     361             : {
     362         103 :     HDF5ImageDataset *poGDS = static_cast<HDF5ImageDataset *>(poDS);
     363             : 
     364         103 :     memset(pImage, 0,
     365         103 :            static_cast<size_t>(nBlockXSize) * nBlockYSize *
     366         103 :                GDALGetDataTypeSizeBytes(eDataType));
     367             : 
     368         103 :     if (poGDS->eAccess == GA_Update)
     369             :     {
     370           0 :         return CE_None;
     371             :     }
     372             : 
     373         103 :     const int nXOff = nBlockXOff * nBlockXSize;
     374         103 :     const int nYOff = nBlockYOff * nBlockYSize;
     375         103 :     const int nXSize = std::min(nBlockXSize, nRasterXSize - nXOff);
     376         103 :     const int nYSize = std::min(nBlockYSize, nRasterYSize - nYOff);
     377         103 :     if (poGDS->m_eWholeBandChunkOptim == HDF5ImageDataset::WBC_ENABLED)
     378             :     {
     379             :         const bool bIsBandInterleavedData =
     380           2 :             poGDS->ndims == 3 && poGDS->m_nOtherDimIndex == 0 &&
     381           3 :             poGDS->GetYIndex() == 1 && poGDS->GetXIndex() == 2;
     382           1 :         if (poGDS->nBands == 1 || bIsBandInterleavedData)
     383             :         {
     384             :             GDALRasterIOExtraArg sExtraArg;
     385           1 :             INIT_RASTERIO_EXTRA_ARG(sExtraArg);
     386           1 :             const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
     387           2 :             return IRasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize, pImage,
     388             :                              nXSize, nYSize, eDataType, nDTSize,
     389           1 :                              static_cast<GSpacing>(nDTSize) * nBlockXSize,
     390           1 :                              &sExtraArg);
     391             :         }
     392             :     }
     393             : 
     394             :     HDF5_GLOBAL_LOCK();
     395             : 
     396         102 :     hsize_t count[3] = {0, 0, 0};
     397         102 :     H5OFFSET_TYPE offset[3] = {0, 0, 0};
     398         102 :     hsize_t col_dims[3] = {0, 0, 0};
     399         102 :     hsize_t rank = std::min(poGDS->ndims, 2);
     400             : 
     401         102 :     if (poGDS->ndims == 3)
     402             :     {
     403           1 :         rank = 3;
     404           1 :         offset[poGDS->m_nOtherDimIndex] = nBand - 1;
     405           1 :         count[poGDS->m_nOtherDimIndex] = 1;
     406           1 :         col_dims[poGDS->m_nOtherDimIndex] = 1;
     407             :     }
     408             : 
     409         102 :     const int nYIndex = poGDS->GetYIndex();
     410             :     // Blocksize may not be a multiple of imagesize.
     411         102 :     if (nYIndex >= 0)
     412             :     {
     413         101 :         offset[nYIndex] = nYOff;
     414         101 :         count[nYIndex] = nYSize;
     415             :     }
     416         102 :     offset[poGDS->GetXIndex()] = nXOff;
     417         102 :     count[poGDS->GetXIndex()] = nXSize;
     418             : 
     419             :     // Select block from file space.
     420         102 :     herr_t status = H5Sselect_hyperslab(poGDS->dataspace_id, H5S_SELECT_SET,
     421             :                                         offset, nullptr, count, nullptr);
     422         102 :     if (status < 0)
     423           0 :         return CE_Failure;
     424             : 
     425             :     // Create memory space to receive the data.
     426         102 :     if (nYIndex >= 0)
     427         101 :         col_dims[nYIndex] = nBlockYSize;
     428         102 :     col_dims[poGDS->GetXIndex()] = nBlockXSize;
     429             : 
     430             :     const hid_t memspace =
     431         102 :         H5Screate_simple(static_cast<int>(rank), col_dims, nullptr);
     432         102 :     H5OFFSET_TYPE mem_offset[3] = {0, 0, 0};
     433         102 :     status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offset, nullptr,
     434             :                                  count, nullptr);
     435         102 :     if (status < 0)
     436             :     {
     437           0 :         H5Sclose(memspace);
     438           0 :         return CE_Failure;
     439             :     }
     440             : 
     441         102 :     status = H5Dread(poGDS->dataset_id, poGDS->native, memspace,
     442             :                      poGDS->dataspace_id, H5P_DEFAULT, pImage);
     443             : 
     444         102 :     H5Sclose(memspace);
     445             : 
     446         102 :     if (status < 0)
     447             :     {
     448           0 :         CPLError(CE_Failure, CPLE_AppDefined, "H5Dread() failed for block.");
     449           0 :         return CE_Failure;
     450             :     }
     451             : 
     452             : #ifdef HDF5_HAVE_FLOAT16
     453             :     if (eDataType == GDT_Float32 && poGDS->m_bConvertFromFloat16)
     454             :     {
     455             :         for (size_t i = static_cast<size_t>(nBlockXSize) * nBlockYSize; i > 0;
     456             :              /* do nothing */)
     457             :         {
     458             :             --i;
     459             :             uint16_t nVal16;
     460             :             memcpy(&nVal16, static_cast<uint16_t *>(pImage) + i,
     461             :                    sizeof(nVal16));
     462             :             const uint32_t nVal32 = CPLHalfToFloat(nVal16);
     463             :             float fVal;
     464             :             memcpy(&fVal, &nVal32, sizeof(fVal));
     465             :             *(static_cast<float *>(pImage) + i) = fVal;
     466             :         }
     467             :     }
     468             :     else if (eDataType == GDT_CFloat32 && poGDS->m_bConvertFromFloat16)
     469             :     {
     470             :         for (size_t i = static_cast<size_t>(nBlockXSize) * nBlockYSize; i > 0;
     471             :              /* do nothing */)
     472             :         {
     473             :             --i;
     474             :             for (int j = 1; j >= 0; --j)
     475             :             {
     476             :                 uint16_t nVal16;
     477             :                 memcpy(&nVal16, static_cast<uint16_t *>(pImage) + 2 * i + j,
     478             :                        sizeof(nVal16));
     479             :                 const uint32_t nVal32 = CPLHalfToFloat(nVal16);
     480             :                 float fVal;
     481             :                 memcpy(&fVal, &nVal32, sizeof(fVal));
     482             :                 *(static_cast<float *>(pImage) + 2 * i + j) = fVal;
     483             :             }
     484             :         }
     485             :     }
     486             : #endif
     487             : 
     488         102 :     return CE_None;
     489             : }
     490             : 
     491             : /************************************************************************/
     492             : /*                             IRasterIO()                              */
     493             : /************************************************************************/
     494             : 
     495         311 : CPLErr HDF5ImageRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
     496             :                                       int nXSize, int nYSize, void *pData,
     497             :                                       int nBufXSize, int nBufYSize,
     498             :                                       GDALDataType eBufType,
     499             :                                       GSpacing nPixelSpace, GSpacing nLineSpace,
     500             :                                       GDALRasterIOExtraArg *psExtraArg)
     501             : 
     502             : {
     503         311 :     HDF5ImageDataset *poGDS = static_cast<HDF5ImageDataset *>(poDS);
     504             : 
     505             : #ifdef HDF5_HAVE_FLOAT16
     506             :     if (poGDS->m_bConvertFromFloat16)
     507             :     {
     508             :         return GDALPamRasterBand::IRasterIO(
     509             :             eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
     510             :             eBufType, nPixelSpace, nLineSpace, psExtraArg);
     511             :     }
     512             : #endif
     513             : 
     514             :     const bool bIsBandInterleavedData =
     515         450 :         poGDS->ndims == 3 && poGDS->m_nOtherDimIndex == 0 &&
     516         761 :         poGDS->GetYIndex() == 1 && poGDS->GetXIndex() == 2;
     517             : 
     518         311 :     const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
     519             : 
     520             :     // Try to detect if we read whole bands by chunks of whole lines
     521             :     // If so, then read and cache whole band (or group of m_nBandChunkSize bands)
     522             :     // to save HDF5 decompression.
     523         311 :     if (m_nIRasterIORecCounter == 0)
     524             :     {
     525         263 :         bool bInvalidateWholeBandChunkOptim = false;
     526         263 :         if (!(nXSize == nBufXSize && nYSize == nBufYSize))
     527             :         {
     528           1 :             bInvalidateWholeBandChunkOptim = true;
     529             :         }
     530             :         // Is the first request on band 1, line 0 and one or several full lines?
     531         262 :         else if (poGDS->m_eWholeBandChunkOptim !=
     532          70 :                      HDF5ImageDataset::WBC_ENABLED &&
     533          70 :                  nBand == 1 && nXOff == 0 && nYOff == 0 &&
     534          20 :                  nXSize == nRasterXSize)
     535             :         {
     536          19 :             poGDS->m_eWholeBandChunkOptim =
     537             :                 HDF5ImageDataset::WBC_DETECTION_IN_PROGRESS;
     538          19 :             poGDS->m_nLastRasterIOBand = 1;
     539          19 :             poGDS->m_nLastRasterIOXOff = nXOff;
     540          19 :             poGDS->m_nLastRasterIOYOff = nYOff;
     541          19 :             poGDS->m_nLastRasterIOXSize = nXSize;
     542          19 :             poGDS->m_nLastRasterIOYSize = nYSize;
     543             :         }
     544         243 :         else if (poGDS->m_eWholeBandChunkOptim ==
     545             :                  HDF5ImageDataset::WBC_DETECTION_IN_PROGRESS)
     546             :         {
     547          50 :             if (poGDS->m_nLastRasterIOBand == 1 && nBand == 1)
     548             :             {
     549             :                 // Is this request a continuation of the previous one?
     550          44 :                 if (nXOff == 0 && poGDS->m_nLastRasterIOXOff == 0 &&
     551          44 :                     nYOff == poGDS->m_nLastRasterIOYOff +
     552          44 :                                  poGDS->m_nLastRasterIOYSize &&
     553          43 :                     poGDS->m_nLastRasterIOXSize == nRasterXSize &&
     554          43 :                     nXSize == nRasterXSize)
     555             :                 {
     556          43 :                     poGDS->m_nLastRasterIOXOff = nXOff;
     557          43 :                     poGDS->m_nLastRasterIOYOff = nYOff;
     558          43 :                     poGDS->m_nLastRasterIOXSize = nXSize;
     559          43 :                     poGDS->m_nLastRasterIOYSize = nYSize;
     560             :                 }
     561             :                 else
     562             :                 {
     563           1 :                     bInvalidateWholeBandChunkOptim = true;
     564             :                 }
     565             :             }
     566           6 :             else if (poGDS->m_nLastRasterIOBand == 1 && nBand == 2)
     567             :             {
     568             :                 // Are we switching to band 2 while having fully read band 1?
     569           5 :                 if (nXOff == 0 && nYOff == 0 && nXSize == nRasterXSize &&
     570           5 :                     poGDS->m_nLastRasterIOXOff == 0 &&
     571           5 :                     poGDS->m_nLastRasterIOXSize == nRasterXSize &&
     572           5 :                     poGDS->m_nLastRasterIOYOff + poGDS->m_nLastRasterIOYSize ==
     573           5 :                         nRasterYSize)
     574             :                 {
     575          11 :                     if ((poGDS->m_nBandChunkSize > 1 ||
     576           5 :                          nBufYSize < nRasterYSize) &&
     577           1 :                         static_cast<int64_t>(poGDS->m_nBandChunkSize) *
     578           1 :                                 nRasterXSize * nRasterYSize * nDTSize <
     579           1 :                             CPLGetUsablePhysicalRAM() / 10)
     580             :                     {
     581           1 :                         poGDS->m_eWholeBandChunkOptim =
     582             :                             HDF5ImageDataset::WBC_ENABLED;
     583             :                     }
     584             :                     else
     585             :                     {
     586           3 :                         bInvalidateWholeBandChunkOptim = true;
     587             :                     }
     588             :                 }
     589             :                 else
     590             :                 {
     591           1 :                     bInvalidateWholeBandChunkOptim = true;
     592             :                 }
     593             :             }
     594             :             else
     595             :             {
     596           1 :                 bInvalidateWholeBandChunkOptim = true;
     597             :             }
     598             :         }
     599         263 :         if (bInvalidateWholeBandChunkOptim)
     600             :         {
     601           7 :             poGDS->m_eWholeBandChunkOptim = HDF5ImageDataset::WBC_DISABLED;
     602           7 :             poGDS->m_nLastRasterIOBand = -1;
     603           7 :             poGDS->m_nLastRasterIOXOff = -1;
     604           7 :             poGDS->m_nLastRasterIOYOff = -1;
     605           7 :             poGDS->m_nLastRasterIOXSize = -1;
     606           7 :             poGDS->m_nLastRasterIOYSize = -1;
     607             :         }
     608             :     }
     609             : 
     610         311 :     if (poGDS->m_eWholeBandChunkOptim == HDF5ImageDataset::WBC_ENABLED &&
     611         193 :         nXSize == nBufXSize && nYSize == nBufYSize)
     612             :     {
     613         193 :         if (poGDS->nBands == 1 || bIsBandInterleavedData)
     614             :         {
     615         193 :             if (poGDS->m_iCurrentBandChunk < 0)
     616           1 :                 CPLDebug("HDF5", "Using whole band chunk caching");
     617         193 :             const int iBandChunk = (nBand - 1) / poGDS->m_nBandChunkSize;
     618         193 :             if (iBandChunk != poGDS->m_iCurrentBandChunk)
     619             :             {
     620          15 :                 poGDS->m_abyBandChunk.resize(
     621          15 :                     static_cast<size_t>(poGDS->m_nBandChunkSize) *
     622          15 :                     nRasterXSize * nRasterYSize * nDTSize);
     623             : 
     624             :                 HDF5_GLOBAL_LOCK();
     625             : 
     626             :                 hsize_t count[3] = {
     627          30 :                     std::min(static_cast<hsize_t>(poGDS->nBands),
     628          30 :                              static_cast<hsize_t>(iBandChunk + 1) *
     629          15 :                                  poGDS->m_nBandChunkSize) -
     630          15 :                         static_cast<hsize_t>(iBandChunk) *
     631          15 :                             poGDS->m_nBandChunkSize,
     632          15 :                     static_cast<hsize_t>(nRasterYSize),
     633          15 :                     static_cast<hsize_t>(nRasterXSize)};
     634          15 :                 H5OFFSET_TYPE offset[3] = {
     635          15 :                     static_cast<H5OFFSET_TYPE>(iBandChunk) *
     636          15 :                         poGDS->m_nBandChunkSize,
     637             :                     static_cast<H5OFFSET_TYPE>(0),
     638          15 :                     static_cast<H5OFFSET_TYPE>(0)};
     639             :                 herr_t status =
     640          15 :                     H5Sselect_hyperslab(poGDS->dataspace_id, H5S_SELECT_SET,
     641             :                                         offset, nullptr, count, nullptr);
     642          15 :                 if (status < 0)
     643           0 :                     return CE_Failure;
     644             : 
     645             :                 const hid_t memspace =
     646          15 :                     H5Screate_simple(poGDS->ndims, count, nullptr);
     647          15 :                 H5OFFSET_TYPE mem_offset[3] = {0, 0, 0};
     648             :                 status =
     649          15 :                     H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offset,
     650             :                                         nullptr, count, nullptr);
     651          15 :                 if (status < 0)
     652             :                 {
     653           0 :                     H5Sclose(memspace);
     654           0 :                     return CE_Failure;
     655             :                 }
     656             : 
     657          15 :                 status = H5Dread(poGDS->dataset_id, poGDS->native, memspace,
     658             :                                  poGDS->dataspace_id, H5P_DEFAULT,
     659          15 :                                  poGDS->m_abyBandChunk.data());
     660             : 
     661          15 :                 H5Sclose(memspace);
     662             : 
     663          15 :                 if (status < 0)
     664             :                 {
     665           0 :                     CPLError(
     666             :                         CE_Failure, CPLE_AppDefined,
     667             :                         "HDF5ImageRasterBand::IRasterIO(): H5Dread() failed");
     668           0 :                     return CE_Failure;
     669             :                 }
     670             : 
     671          15 :                 poGDS->m_iCurrentBandChunk = iBandChunk;
     672             :             }
     673             : 
     674        1447 :             for (int iY = 0; iY < nYSize; ++iY)
     675             :             {
     676        2508 :                 GDALCopyWords(poGDS->m_abyBandChunk.data() +
     677        1254 :                                   static_cast<size_t>((nBand - 1) %
     678        1254 :                                                       poGDS->m_nBandChunkSize) *
     679        1254 :                                       nRasterYSize * nRasterXSize * nDTSize +
     680        1254 :                                   static_cast<size_t>(nYOff + iY) *
     681        1254 :                                       nRasterXSize * nDTSize +
     682        1254 :                                   nXOff * nDTSize,
     683             :                               eDataType, nDTSize,
     684             :                               static_cast<GByte *>(pData) +
     685        1254 :                                   static_cast<size_t>(iY) * nLineSpace,
     686             :                               eBufType, static_cast<int>(nPixelSpace), nXSize);
     687             :             }
     688         193 :             return CE_None;
     689             :         }
     690             :     }
     691             : 
     692             :     const bool bIsExpectedLayout =
     693         204 :         (bIsBandInterleavedData ||
     694         171 :          (poGDS->ndims == 2 && poGDS->GetYIndex() == 0 &&
     695          85 :           poGDS->GetXIndex() == 1));
     696         118 :     if (eRWFlag == GF_Read && bIsExpectedLayout && nXSize == nBufXSize &&
     697         116 :         nYSize == nBufYSize && eBufType == eDataType &&
     698          68 :         nPixelSpace == nDTSize && nLineSpace == nXSize * nPixelSpace)
     699             :     {
     700             :         HDF5_GLOBAL_LOCK();
     701             : 
     702          68 :         hsize_t count[3] = {1, static_cast<hsize_t>(nYSize),
     703          68 :                             static_cast<hsize_t>(nXSize)};
     704          68 :         H5OFFSET_TYPE offset[3] = {static_cast<H5OFFSET_TYPE>(nBand - 1),
     705          68 :                                    static_cast<H5OFFSET_TYPE>(nYOff),
     706          68 :                                    static_cast<H5OFFSET_TYPE>(nXOff)};
     707          68 :         if (poGDS->ndims == 2)
     708             :         {
     709          47 :             count[0] = count[1];
     710          47 :             count[1] = count[2];
     711             : 
     712          47 :             offset[0] = offset[1];
     713          47 :             offset[1] = offset[2];
     714             :         }
     715          68 :         herr_t status = H5Sselect_hyperslab(poGDS->dataspace_id, H5S_SELECT_SET,
     716             :                                             offset, nullptr, count, nullptr);
     717          68 :         if (status < 0)
     718           0 :             return CE_Failure;
     719             : 
     720          68 :         const hid_t memspace = H5Screate_simple(poGDS->ndims, count, nullptr);
     721          68 :         H5OFFSET_TYPE mem_offset[3] = {0, 0, 0};
     722          68 :         status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offset,
     723             :                                      nullptr, count, nullptr);
     724          68 :         if (status < 0)
     725             :         {
     726           0 :             H5Sclose(memspace);
     727           0 :             return CE_Failure;
     728             :         }
     729             : 
     730          68 :         status = H5Dread(poGDS->dataset_id, poGDS->native, memspace,
     731             :                          poGDS->dataspace_id, H5P_DEFAULT, pData);
     732             : 
     733          68 :         H5Sclose(memspace);
     734             : 
     735          68 :         if (status < 0)
     736             :         {
     737           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     738             :                      "HDF5ImageRasterBand::IRasterIO(): H5Dread() failed");
     739           0 :             return CE_Failure;
     740             :         }
     741             : 
     742          68 :         return CE_None;
     743             :     }
     744             : 
     745             :     // If the request is still small enough, try to read from libhdf5 with
     746             :     // the natural interleaving into a temporary MEMDataset, and then read
     747             :     // from it with the requested interleaving and data type.
     748          50 :     if (eRWFlag == GF_Read && bIsExpectedLayout && nXSize == nBufXSize &&
     749         100 :         nYSize == nBufYSize &&
     750          48 :         static_cast<GIntBig>(nXSize) * nYSize < CPLGetUsablePhysicalRAM() / 10)
     751             :     {
     752             :         auto poMemDS = std::unique_ptr<GDALDataset>(
     753          48 :             MEMDataset::Create("", nXSize, nYSize, 1, eDataType, nullptr));
     754          48 :         if (poMemDS)
     755             :         {
     756          48 :             void *pMemData = poMemDS->GetInternalHandle("MEMORY1");
     757          48 :             CPLAssert(pMemData);
     758             :             // Read from HDF5 into the temporary MEMDataset using the
     759             :             // natural interleaving of the HDF5 dataset
     760          48 :             ++m_nIRasterIORecCounter;
     761             :             CPLErr eErr =
     762          96 :                 IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pMemData,
     763             :                           nXSize, nYSize, eDataType, nDTSize,
     764          48 :                           static_cast<GSpacing>(nXSize) * nDTSize, psExtraArg);
     765          48 :             --m_nIRasterIORecCounter;
     766          48 :             if (eErr != CE_None)
     767             :             {
     768           0 :                 return CE_Failure;
     769             :             }
     770             :             // Copy to the final buffer using requested data type and spacings.
     771          48 :             return poMemDS->GetRasterBand(1)->RasterIO(
     772             :                 GF_Read, 0, 0, nXSize, nYSize, pData, nXSize, nYSize, eBufType,
     773          48 :                 nPixelSpace, nLineSpace, nullptr);
     774             :         }
     775             :     }
     776             : 
     777           2 :     return GDALPamRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
     778             :                                         pData, nBufXSize, nBufYSize, eBufType,
     779           2 :                                         nPixelSpace, nLineSpace, psExtraArg);
     780             : }
     781             : 
     782             : /************************************************************************/
     783             : /*                             IRasterIO()                              */
     784             : /************************************************************************/
     785             : 
     786          90 : CPLErr HDF5ImageDataset::IRasterIO(
     787             :     GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
     788             :     void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
     789             :     int nBandCount, BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
     790             :     GSpacing nLineSpace, GSpacing nBandSpace, GDALRasterIOExtraArg *psExtraArg)
     791             : 
     792             : {
     793             : #ifdef HDF5_HAVE_FLOAT16
     794             :     if (m_bConvertFromFloat16)
     795             :     {
     796             :         return HDF5Dataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
     797             :                                       pData, nBufXSize, nBufYSize, eBufType,
     798             :                                       nBandCount, panBandMap, nPixelSpace,
     799             :                                       nLineSpace, nBandSpace, psExtraArg);
     800             :     }
     801             : #endif
     802             : 
     803         134 :     const auto IsConsecutiveBands = [](const int *panVals, int nCount)
     804             :     {
     805         151 :         for (int i = 1; i < nCount; ++i)
     806             :         {
     807          19 :             if (panVals[i] != panVals[i - 1] + 1)
     808           2 :                 return false;
     809             :         }
     810         132 :         return true;
     811             :     };
     812             : 
     813          90 :     const auto eDT = GetRasterBand(1)->GetRasterDataType();
     814          90 :     const int nDTSize = GDALGetDataTypeSizeBytes(eDT);
     815             : 
     816             :     // Band-interleaved data and request
     817         176 :     const bool bIsBandInterleavedData = ndims == 3 && m_nOtherDimIndex == 0 &&
     818         266 :                                         GetYIndex() == 1 && GetXIndex() == 2;
     819          90 :     if (eRWFlag == GF_Read && bIsBandInterleavedData && nXSize == nBufXSize &&
     820          86 :         nYSize == nBufYSize && IsConsecutiveBands(panBandMap, nBandCount) &&
     821          84 :         eBufType == eDT && nPixelSpace == nDTSize &&
     822         180 :         nLineSpace == nXSize * nPixelSpace && nBandSpace == nYSize * nLineSpace)
     823             :     {
     824             :         HDF5_GLOBAL_LOCK();
     825             : 
     826          43 :         hsize_t count[3] = {static_cast<hsize_t>(nBandCount),
     827          43 :                             static_cast<hsize_t>(nYSize),
     828          43 :                             static_cast<hsize_t>(nXSize)};
     829             :         H5OFFSET_TYPE offset[3] = {
     830          43 :             static_cast<H5OFFSET_TYPE>(panBandMap[0] - 1),
     831          43 :             static_cast<H5OFFSET_TYPE>(nYOff),
     832          43 :             static_cast<H5OFFSET_TYPE>(nXOff)};
     833          43 :         herr_t status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET,
     834             :                                             offset, nullptr, count, nullptr);
     835          43 :         if (status < 0)
     836           0 :             return CE_Failure;
     837             : 
     838          43 :         const hid_t memspace = H5Screate_simple(ndims, count, nullptr);
     839          43 :         H5OFFSET_TYPE mem_offset[3] = {0, 0, 0};
     840          43 :         status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offset,
     841             :                                      nullptr, count, nullptr);
     842          43 :         if (status < 0)
     843             :         {
     844           0 :             H5Sclose(memspace);
     845           0 :             return CE_Failure;
     846             :         }
     847             : 
     848          43 :         status = H5Dread(dataset_id, native, memspace, dataspace_id,
     849             :                          H5P_DEFAULT, pData);
     850             : 
     851          43 :         H5Sclose(memspace);
     852             : 
     853          43 :         if (status < 0)
     854             :         {
     855           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     856             :                      "HDF5ImageDataset::IRasterIO(): H5Dread() failed");
     857           0 :             return CE_Failure;
     858             :         }
     859             : 
     860          43 :         return CE_None;
     861             :     }
     862             : 
     863             :     // Pixel-interleaved data and request
     864             : 
     865          51 :     const bool bIsPixelInterleaveData = ndims == 3 && m_nOtherDimIndex == 2 &&
     866          98 :                                         GetYIndex() == 0 && GetXIndex() == 1;
     867          47 :     if (eRWFlag == GF_Read && bIsPixelInterleaveData && nXSize == nBufXSize &&
     868           4 :         nYSize == nBufYSize && IsConsecutiveBands(panBandMap, nBandCount) &&
     869           4 :         eBufType == eDT && nBandSpace == nDTSize &&
     870          97 :         nPixelSpace == nBandCount * nBandSpace &&
     871           3 :         nLineSpace == nXSize * nPixelSpace)
     872             :     {
     873             :         HDF5_GLOBAL_LOCK();
     874             : 
     875           3 :         hsize_t count[3] = {static_cast<hsize_t>(nYSize),
     876           3 :                             static_cast<hsize_t>(nXSize),
     877           3 :                             static_cast<hsize_t>(nBandCount)};
     878             :         H5OFFSET_TYPE offset[3] = {
     879           3 :             static_cast<H5OFFSET_TYPE>(nYOff),
     880           3 :             static_cast<H5OFFSET_TYPE>(nXOff),
     881           3 :             static_cast<H5OFFSET_TYPE>(panBandMap[0] - 1)};
     882           3 :         herr_t status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET,
     883             :                                             offset, nullptr, count, nullptr);
     884           3 :         if (status < 0)
     885           0 :             return CE_Failure;
     886             : 
     887           3 :         const hid_t memspace = H5Screate_simple(ndims, count, nullptr);
     888           3 :         H5OFFSET_TYPE mem_offset[3] = {0, 0, 0};
     889           3 :         status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offset,
     890             :                                      nullptr, count, nullptr);
     891           3 :         if (status < 0)
     892             :         {
     893           0 :             H5Sclose(memspace);
     894           0 :             return CE_Failure;
     895             :         }
     896             : 
     897           3 :         status = H5Dread(dataset_id, native, memspace, dataspace_id,
     898             :                          H5P_DEFAULT, pData);
     899             : 
     900           3 :         H5Sclose(memspace);
     901             : 
     902           3 :         if (status < 0)
     903             :         {
     904           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     905             :                      "HDF5ImageDataset::IRasterIO(): H5Dread() failed");
     906           0 :             return CE_Failure;
     907             :         }
     908             : 
     909           3 :         return CE_None;
     910             :     }
     911             : 
     912             :     // If the request is still small enough, try to read from libhdf5 with
     913             :     // the natural interleaving into a temporary MEMDataset, and then read
     914             :     // from it with the requested interleaving and data type.
     915          44 :     if (eRWFlag == GF_Read &&
     916          44 :         (bIsBandInterleavedData || bIsPixelInterleaveData) &&
     917          88 :         nXSize == nBufXSize && nYSize == nBufYSize &&
     918         132 :         IsConsecutiveBands(panBandMap, nBandCount) &&
     919          43 :         static_cast<GIntBig>(nXSize) * nYSize <
     920          43 :             CPLGetUsablePhysicalRAM() / 10 / nBandCount)
     921             :     {
     922          43 :         const char *const apszOptions[] = {
     923          43 :             bIsPixelInterleaveData ? "INTERLEAVE=PIXEL" : nullptr, nullptr};
     924             :         auto poMemDS = std::unique_ptr<GDALDataset>(
     925          43 :             MEMDataset::Create("", nXSize, nYSize, nBandCount, eDT,
     926          43 :                                const_cast<char **>(apszOptions)));
     927          43 :         if (poMemDS)
     928             :         {
     929          43 :             void *pMemData = poMemDS->GetInternalHandle("MEMORY1");
     930          43 :             CPLAssert(pMemData);
     931             :             // Read from HDF5 into the temporary MEMDataset using the
     932             :             // natural interleaving of the HDF5 dataset
     933         129 :             if (IRasterIO(
     934             :                     eRWFlag, nXOff, nYOff, nXSize, nYSize, pMemData, nXSize,
     935             :                     nYSize, eDT, nBandCount, panBandMap,
     936           1 :                     bIsBandInterleavedData ? nDTSize : nDTSize * nBandCount,
     937             :                     bIsBandInterleavedData
     938          42 :                         ? static_cast<GSpacing>(nXSize) * nDTSize
     939           1 :                         : static_cast<GSpacing>(nXSize) * nDTSize * nBandCount,
     940             :                     bIsBandInterleavedData
     941          42 :                         ? static_cast<GSpacing>(nYSize) * nXSize * nDTSize
     942             :                         : nDTSize,
     943          43 :                     psExtraArg) != CE_None)
     944             :             {
     945           0 :                 return CE_Failure;
     946             :             }
     947             :             // Copy to the final buffer using requested data type and spacings.
     948          43 :             return poMemDS->RasterIO(GF_Read, 0, 0, nXSize, nYSize, pData,
     949             :                                      nXSize, nYSize, eBufType, nBandCount,
     950             :                                      nullptr, nPixelSpace, nLineSpace,
     951          43 :                                      nBandSpace, nullptr);
     952             :         }
     953             :     }
     954             : 
     955           1 :     return HDF5Dataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
     956             :                                   nBufXSize, nBufYSize, eBufType, nBandCount,
     957             :                                   panBandMap, nPixelSpace, nLineSpace,
     958           1 :                                   nBandSpace, psExtraArg);
     959             : }
     960             : 
     961             : /************************************************************************/
     962             : /*                                Open()                                */
     963             : /************************************************************************/
     964          53 : GDALDataset *HDF5ImageDataset::Open(GDALOpenInfo *poOpenInfo)
     965             : {
     966          53 :     if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "HDF5:"))
     967           0 :         return nullptr;
     968             : 
     969             :     HDF5_GLOBAL_LOCK();
     970             : 
     971             :     // Confirm the requested access is supported.
     972          53 :     if (poOpenInfo->eAccess == GA_Update)
     973             :     {
     974           0 :         ReportUpdateNotSupportedByDriver("HDF5");
     975           0 :         return nullptr;
     976             :     }
     977             : 
     978          53 :     HDF5ImageDataset *poDS = new HDF5ImageDataset();
     979             : 
     980             :     // Create a corresponding GDALDataset.
     981             :     char **papszName =
     982          53 :         CSLTokenizeString2(poOpenInfo->pszFilename, ":",
     983             :                            CSLT_HONOURSTRINGS | CSLT_PRESERVEESCAPES);
     984             : 
     985          53 :     if (!(CSLCount(papszName) == 3 || CSLCount(papszName) == 4))
     986             :     {
     987           0 :         CSLDestroy(papszName);
     988           0 :         delete poDS;
     989           0 :         return nullptr;
     990             :     }
     991             : 
     992          53 :     poDS->SetDescription(poOpenInfo->pszFilename);
     993             : 
     994             :     // Check for drive name in windows HDF5:"D:\...
     995         106 :     CPLString osSubdatasetName;
     996             : 
     997         106 :     CPLString osFilename(papszName[1]);
     998             : 
     999          53 :     if ((strlen(papszName[1]) == 1 && papszName[3] != nullptr) ||
    1000          53 :         (STARTS_WITH(papszName[1], "/vsicurl/http") && papszName[3] != nullptr))
    1001             :     {
    1002           0 :         osFilename += ":";
    1003           0 :         osFilename += papszName[2];
    1004           0 :         osSubdatasetName = papszName[3];
    1005             :     }
    1006             :     else
    1007             :     {
    1008          53 :         osSubdatasetName = papszName[2];
    1009             :     }
    1010             : 
    1011          53 :     poDS->SetSubdatasetName(osSubdatasetName);
    1012             : 
    1013          53 :     CSLDestroy(papszName);
    1014          53 :     papszName = nullptr;
    1015             : 
    1016          53 :     poDS->SetPhysicalFilename(osFilename);
    1017             : 
    1018             :     // Try opening the dataset.
    1019          53 :     poDS->m_hHDF5 = GDAL_HDF5Open(osFilename);
    1020          53 :     if (poDS->m_hHDF5 < 0)
    1021             :     {
    1022           0 :         delete poDS;
    1023           0 :         return nullptr;
    1024             :     }
    1025             : 
    1026          53 :     poDS->hGroupID = H5Gopen(poDS->m_hHDF5, "/");
    1027          53 :     if (poDS->hGroupID < 0)
    1028             :     {
    1029           0 :         delete poDS;
    1030           0 :         return nullptr;
    1031             :     }
    1032             : 
    1033             :     // This is an HDF5 file.
    1034          53 :     poDS->ReadGlobalAttributes(FALSE);
    1035             : 
    1036             :     // Create HDF5 Data Hierarchy in a link list.
    1037          53 :     poDS->poH5Objects = poDS->HDF5FindDatasetObjectsbyPath(poDS->poH5RootGroup,
    1038             :                                                            osSubdatasetName);
    1039             : 
    1040          53 :     if (poDS->poH5Objects == nullptr)
    1041             :     {
    1042           0 :         delete poDS;
    1043           0 :         return nullptr;
    1044             :     }
    1045             : 
    1046             :     // Retrieve HDF5 data information.
    1047          53 :     poDS->dataset_id = H5Dopen(poDS->m_hHDF5, poDS->poH5Objects->pszPath);
    1048          53 :     poDS->dataspace_id = H5Dget_space(poDS->dataset_id);
    1049          53 :     poDS->ndims = H5Sget_simple_extent_ndims(poDS->dataspace_id);
    1050          53 :     if (poDS->ndims <= 0)
    1051             :     {
    1052           0 :         delete poDS;
    1053           0 :         return nullptr;
    1054             :     }
    1055          53 :     poDS->dims =
    1056          53 :         static_cast<hsize_t *>(CPLCalloc(poDS->ndims, sizeof(hsize_t)));
    1057          53 :     poDS->maxdims =
    1058          53 :         static_cast<hsize_t *>(CPLCalloc(poDS->ndims, sizeof(hsize_t)));
    1059          53 :     poDS->dimensions = H5Sget_simple_extent_dims(poDS->dataspace_id, poDS->dims,
    1060             :                                                  poDS->maxdims);
    1061          53 :     auto datatype = H5Dget_type(poDS->dataset_id);
    1062          53 :     poDS->native = H5Tget_native_type(datatype, H5T_DIR_ASCEND);
    1063          53 :     H5Tclose(datatype);
    1064             : 
    1065          53 :     const auto eGDALDataType = poDS->GetDataType(poDS->native);
    1066          53 :     if (eGDALDataType == GDT_Unknown)
    1067             :     {
    1068           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Unhandled HDF5 data type");
    1069           0 :         delete poDS;
    1070           0 :         return nullptr;
    1071             :     }
    1072             : 
    1073             : #ifdef HDF5_HAVE_FLOAT16
    1074             :     if (H5Tequal(H5T_NATIVE_FLOAT16, poDS->native) ||
    1075             :         IsNativeCFloat16(poDS->native))
    1076             :     {
    1077             :         poDS->m_bConvertFromFloat16 = true;
    1078             :     }
    1079             : #endif
    1080             : 
    1081             :     // CSK code in IdentifyProductType() and CreateProjections()
    1082             :     // uses dataset metadata.
    1083          53 :     poDS->SetMetadata(poDS->m_aosMetadata.List());
    1084             : 
    1085             :     // Check if the hdf5 is a well known product type
    1086          53 :     poDS->IdentifyProductType();
    1087             : 
    1088          53 :     poDS->m_nYIndex = poDS->IsComplexCSKL1A() ? 0 : poDS->ndims - 2;
    1089          53 :     poDS->m_nXIndex = poDS->IsComplexCSKL1A() ? 1 : poDS->ndims - 1;
    1090             : 
    1091          53 :     if (poDS->IsComplexCSKL1A())
    1092             :     {
    1093           0 :         poDS->m_nOtherDimIndex = 2;
    1094             :     }
    1095          53 :     else if (poDS->ndims == 3)
    1096             :     {
    1097          10 :         poDS->m_nOtherDimIndex = 0;
    1098             :     }
    1099             : 
    1100          53 :     if (HDF5EOSParser::HasHDFEOS(poDS->hGroupID))
    1101             :     {
    1102          34 :         HDF5EOSParser oHDFEOSParser;
    1103          17 :         if (oHDFEOSParser.Parse(poDS->hGroupID))
    1104             :         {
    1105          17 :             CPLDebug("HDF5", "Successfully parsed HDFEOS metadata");
    1106          34 :             HDF5EOSParser::GridDataFieldMetadata oGridDataFieldMetadata;
    1107          34 :             HDF5EOSParser::SwathDataFieldMetadata oSwathDataFieldMetadata;
    1108          17 :             if (oHDFEOSParser.GetDataModel() ==
    1109           5 :                     HDF5EOSParser::DataModel::GRID &&
    1110           5 :                 oHDFEOSParser.GetGridDataFieldMetadata(
    1111          22 :                     osSubdatasetName.c_str(), oGridDataFieldMetadata) &&
    1112           5 :                 static_cast<int>(oGridDataFieldMetadata.aoDimensions.size()) ==
    1113           5 :                     poDS->ndims)
    1114             :             {
    1115           5 :                 int iDim = 0;
    1116          17 :                 for (const auto &oDim : oGridDataFieldMetadata.aoDimensions)
    1117             :                 {
    1118          12 :                     if (oDim.osName == "XDim")
    1119           5 :                         poDS->m_nXIndex = iDim;
    1120           7 :                     else if (oDim.osName == "YDim")
    1121           5 :                         poDS->m_nYIndex = iDim;
    1122             :                     else
    1123           2 :                         poDS->m_nOtherDimIndex = iDim;
    1124          12 :                     ++iDim;
    1125             :                 }
    1126             : 
    1127           5 :                 if (oGridDataFieldMetadata.poGridMetadata->GetGeoTransform(
    1128           5 :                         poDS->adfGeoTransform))
    1129           5 :                     poDS->bHasGeoTransform = true;
    1130             : 
    1131          10 :                 auto poSRS = oGridDataFieldMetadata.poGridMetadata->GetSRS();
    1132           5 :                 if (poSRS)
    1133           5 :                     poDS->m_oSRS = *(poSRS.get());
    1134             :             }
    1135          12 :             else if (oHDFEOSParser.GetDataModel() ==
    1136          12 :                          HDF5EOSParser::DataModel::SWATH &&
    1137          12 :                      oHDFEOSParser.GetSwathDataFieldMetadata(
    1138           6 :                          osSubdatasetName.c_str(), oSwathDataFieldMetadata) &&
    1139             :                      static_cast<int>(
    1140           6 :                          oSwathDataFieldMetadata.aoDimensions.size()) ==
    1141           6 :                          poDS->ndims &&
    1142          30 :                      oSwathDataFieldMetadata.iXDim >= 0 &&
    1143           6 :                      oSwathDataFieldMetadata.iYDim >= 0)
    1144             :             {
    1145           6 :                 poDS->m_nXIndex = oSwathDataFieldMetadata.iXDim;
    1146           6 :                 poDS->m_nYIndex = oSwathDataFieldMetadata.iYDim;
    1147           6 :                 poDS->m_nOtherDimIndex = oSwathDataFieldMetadata.iOtherDim;
    1148           6 :                 if (!oSwathDataFieldMetadata.osLongitudeSubdataset.empty())
    1149             :                 {
    1150             :                     // Arbitrary
    1151           6 :                     poDS->SetMetadataItem("SRS", SRS_WKT_WGS84_LAT_LONG,
    1152           6 :                                           "GEOLOCATION");
    1153           6 :                     poDS->SetMetadataItem(
    1154             :                         "X_DATASET",
    1155          12 :                         ("HDF5:\"" + osFilename +
    1156          12 :                          "\":" + oSwathDataFieldMetadata.osLongitudeSubdataset)
    1157             :                             .c_str(),
    1158           6 :                         "GEOLOCATION");
    1159           6 :                     poDS->SetMetadataItem("X_BAND", "1", "GEOLOCATION");
    1160           6 :                     poDS->SetMetadataItem(
    1161             :                         "Y_DATASET",
    1162          12 :                         ("HDF5:\"" + osFilename +
    1163          12 :                          "\":" + oSwathDataFieldMetadata.osLatitudeSubdataset)
    1164             :                             .c_str(),
    1165           6 :                         "GEOLOCATION");
    1166           6 :                     poDS->SetMetadataItem("Y_BAND", "1", "GEOLOCATION");
    1167           6 :                     poDS->SetMetadataItem(
    1168             :                         "PIXEL_OFFSET",
    1169             :                         CPLSPrintf("%d", oSwathDataFieldMetadata.nPixelOffset),
    1170           6 :                         "GEOLOCATION");
    1171           6 :                     poDS->SetMetadataItem(
    1172             :                         "PIXEL_STEP",
    1173             :                         CPLSPrintf("%d", oSwathDataFieldMetadata.nPixelStep),
    1174           6 :                         "GEOLOCATION");
    1175           6 :                     poDS->SetMetadataItem(
    1176             :                         "LINE_OFFSET",
    1177             :                         CPLSPrintf("%d", oSwathDataFieldMetadata.nLineOffset),
    1178           6 :                         "GEOLOCATION");
    1179           6 :                     poDS->SetMetadataItem(
    1180             :                         "LINE_STEP",
    1181             :                         CPLSPrintf("%d", oSwathDataFieldMetadata.nLineStep),
    1182           6 :                         "GEOLOCATION");
    1183             :                     // Not totally sure about that
    1184           6 :                     poDS->SetMetadataItem("GEOREFERENCING_CONVENTION",
    1185           6 :                                           "PIXEL_CENTER", "GEOLOCATION");
    1186             :                 }
    1187             :             }
    1188             :         }
    1189             :     }
    1190             : 
    1191          53 :     poDS->nRasterYSize =
    1192          53 :         poDS->GetYIndex() < 0
    1193          53 :             ? 1
    1194          52 :             : static_cast<int>(poDS->dims[poDS->GetYIndex()]);  // nRows
    1195          53 :     poDS->nRasterXSize =
    1196          53 :         static_cast<int>(poDS->dims[poDS->GetXIndex()]);  // nCols
    1197          53 :     int nBands = 1;
    1198          53 :     if (poDS->m_nOtherDimIndex >= 0)
    1199             :     {
    1200          10 :         nBands = static_cast<int>(poDS->dims[poDS->m_nOtherDimIndex]);
    1201             :     }
    1202             : 
    1203         106 :     CPLStringList aosMetadata;
    1204          53 :     std::map<std::string, CPLStringList> oMapBandSpecificMetadata;
    1205          53 :     if (poDS->poH5Objects->nType == H5G_DATASET)
    1206             :     {
    1207          53 :         HDF5Dataset::CreateMetadata(poDS->m_hHDF5, poDS->poH5Objects,
    1208             :                                     H5G_DATASET, false, aosMetadata);
    1209          53 :         if (nBands > 1 && poDS->nRasterXSize != nBands &&
    1210           9 :             poDS->nRasterYSize != nBands)
    1211             :         {
    1212             :             // Heuristics to detect non-scalar attributes, that are intended
    1213             :             // to be attached to a specific band.
    1214          18 :             const CPLStringList aosMetadataDup(aosMetadata);
    1215          12 :             for (const auto &[pszKey, pszValue] :
    1216          21 :                  cpl::IterateNameValue(aosMetadataDup))
    1217             :             {
    1218           6 :                 const hid_t hAttrID = H5Aopen_name(poDS->dataset_id, pszKey);
    1219           6 :                 const hid_t hAttrSpace = H5Aget_space(hAttrID);
    1220          11 :                 if (H5Sget_simple_extent_ndims(hAttrSpace) == 1 &&
    1221           5 :                     H5Sget_simple_extent_npoints(hAttrSpace) == nBands)
    1222             :                 {
    1223             :                     CPLStringList aosTokens(
    1224           8 :                         CSLTokenizeString2(pszValue, " ", 0));
    1225           4 :                     if (aosTokens.size() == nBands)
    1226             :                     {
    1227           8 :                         std::string osAttrName(pszKey);
    1228           6 :                         if (osAttrName.size() > strlen("_coefficients") &&
    1229           6 :                             osAttrName.substr(osAttrName.size() -
    1230           2 :                                               strlen("_coefficients")) ==
    1231             :                                 "_coefficients")
    1232             :                         {
    1233           1 :                             osAttrName.pop_back();
    1234             :                         }
    1235           5 :                         else if (osAttrName.size() > strlen("_wavelengths") &&
    1236           5 :                                  osAttrName.substr(osAttrName.size() -
    1237           2 :                                                    strlen("_wavelengths")) ==
    1238             :                                      "_wavelengths")
    1239             :                         {
    1240           1 :                             osAttrName.pop_back();
    1241             :                         }
    1242           3 :                         else if (osAttrName.size() > strlen("_list") &&
    1243           3 :                                  osAttrName.substr(osAttrName.size() -
    1244           1 :                                                    strlen("_list")) == "_list")
    1245             :                         {
    1246           1 :                             osAttrName.resize(osAttrName.size() -
    1247             :                                               strlen("_list"));
    1248             :                         }
    1249           4 :                         oMapBandSpecificMetadata[osAttrName] =
    1250           8 :                             std::move(aosTokens);
    1251           4 :                         aosMetadata.SetNameValue(pszKey, nullptr);
    1252             :                     }
    1253             :                 }
    1254           6 :                 H5Sclose(hAttrSpace);
    1255           6 :                 H5Aclose(hAttrID);
    1256             :             }
    1257             :         }
    1258             :     }
    1259             : 
    1260          53 :     poDS->m_nBlockXSize = poDS->GetRasterXSize();
    1261          53 :     poDS->m_nBlockYSize = 1;
    1262          53 :     poDS->m_nBandChunkSize = 1;
    1263             : 
    1264             :     // Check for chunksize and set it as the blocksize (optimizes read).
    1265          53 :     const hid_t listid = H5Dget_create_plist(poDS->dataset_id);
    1266          53 :     if (listid > 0)
    1267             :     {
    1268          53 :         if (H5Pget_layout(listid) == H5D_CHUNKED)
    1269             :         {
    1270          12 :             hsize_t panChunkDims[3] = {0, 0, 0};
    1271          12 :             const int nDimSize = H5Pget_chunk(listid, 3, panChunkDims);
    1272          12 :             CPL_IGNORE_RET_VAL(nDimSize);
    1273          12 :             CPLAssert(nDimSize == poDS->ndims);
    1274          12 :             poDS->m_nBlockXSize =
    1275          12 :                 static_cast<int>(panChunkDims[poDS->GetXIndex()]);
    1276          12 :             if (poDS->GetYIndex() >= 0)
    1277          12 :                 poDS->m_nBlockYSize =
    1278          12 :                     static_cast<int>(panChunkDims[poDS->GetYIndex()]);
    1279          12 :             if (nBands > 1)
    1280             :             {
    1281           3 :                 poDS->m_nBandChunkSize =
    1282           3 :                     static_cast<int>(panChunkDims[poDS->m_nOtherDimIndex]);
    1283             : 
    1284           3 :                 poDS->SetMetadataItem("BAND_CHUNK_SIZE",
    1285             :                                       CPLSPrintf("%d", poDS->m_nBandChunkSize),
    1286           3 :                                       "IMAGE_STRUCTURE");
    1287             :             }
    1288             :         }
    1289             : 
    1290          53 :         const int nFilters = H5Pget_nfilters(listid);
    1291          59 :         for (int i = 0; i < nFilters; ++i)
    1292             :         {
    1293           6 :             unsigned int flags = 0;
    1294           6 :             size_t cd_nelmts = 0;
    1295           6 :             char szName[64 + 1] = {0};
    1296           6 :             const auto eFilter = H5Pget_filter(listid, i, &flags, &cd_nelmts,
    1297             :                                                nullptr, 64, szName);
    1298           6 :             if (eFilter == H5Z_FILTER_DEFLATE)
    1299             :             {
    1300           5 :                 poDS->SetMetadataItem("COMPRESSION", "DEFLATE",
    1301           5 :                                       "IMAGE_STRUCTURE");
    1302             :             }
    1303           1 :             else if (eFilter == H5Z_FILTER_SZIP)
    1304             :             {
    1305           0 :                 poDS->SetMetadataItem("COMPRESSION", "SZIP", "IMAGE_STRUCTURE");
    1306             :             }
    1307             :         }
    1308             : 
    1309          53 :         H5Pclose(listid);
    1310             :     }
    1311             : 
    1312         168 :     for (int i = 0; i < nBands; i++)
    1313             :     {
    1314             :         HDF5ImageRasterBand *const poBand =
    1315         115 :             new HDF5ImageRasterBand(poDS, i + 1, eGDALDataType);
    1316             : 
    1317         115 :         poDS->SetBand(i + 1, poBand);
    1318             : 
    1319         115 :         if (poDS->poH5Objects->nType == H5G_DATASET)
    1320             :         {
    1321         115 :             poBand->SetMetadata(aosMetadata.List());
    1322         123 :             for (const auto &oIter : oMapBandSpecificMetadata)
    1323             :             {
    1324           8 :                 poBand->SetMetadataItem(oIter.first.c_str(), oIter.second[i]);
    1325             :             }
    1326             :         }
    1327             :     }
    1328             : 
    1329          53 :     if (!poDS->GetMetadata("GEOLOCATION"))
    1330          47 :         poDS->CreateProjections();
    1331             : 
    1332             :     // Setup/check for pam .aux.xml.
    1333          53 :     poDS->TryLoadXML();
    1334             : 
    1335             :     // Setup overviews.
    1336          53 :     poDS->oOvManager.Initialize(poDS, ":::VIRTUAL:::");
    1337             : 
    1338          53 :     return poDS;
    1339             : }
    1340             : 
    1341             : /************************************************************************/
    1342             : /*                   HDF5ImageDatasetDriverUnload()                     */
    1343             : /************************************************************************/
    1344             : 
    1345           6 : static void HDF5ImageDatasetDriverUnload(GDALDriver *)
    1346             : {
    1347           6 :     HDF5UnloadFileDriver();
    1348           6 : }
    1349             : 
    1350             : /************************************************************************/
    1351             : /*                        GDALRegister_HDF5Image()                      */
    1352             : /************************************************************************/
    1353          10 : void GDALRegister_HDF5Image()
    1354             : 
    1355             : {
    1356          10 :     if (!GDAL_CHECK_VERSION("HDF5Image driver"))
    1357           0 :         return;
    1358             : 
    1359          10 :     if (GDALGetDriverByName(HDF5_IMAGE_DRIVER_NAME) != nullptr)
    1360           0 :         return;
    1361             : 
    1362          10 :     GDALDriver *poDriver = new GDALDriver();
    1363             : 
    1364          10 :     HDF5ImageDriverSetCommonMetadata(poDriver);
    1365             : 
    1366          10 :     poDriver->pfnOpen = HDF5ImageDataset::Open;
    1367          10 :     poDriver->pfnUnloadDriver = HDF5ImageDatasetDriverUnload;
    1368             : 
    1369          10 :     GetGDALDriverManager()->RegisterDriver(poDriver);
    1370             : }
    1371             : 
    1372             : /************************************************************************/
    1373             : /*                       CreateODIMH5Projection()                       */
    1374             : /************************************************************************/
    1375             : 
    1376             : // Reference:
    1377             : //   http://www.knmi.nl/opera/opera3/OPERA_2008_03_WP2.1b_ODIM_H5_v2.1.pdf
    1378             : //
    1379             : // 4.3.2 where for geographically referenced image Groups
    1380             : // We don't use the where_xscale and where_yscale parameters, but recompute them
    1381             : // from the lower-left and upper-right coordinates. There's some difference.
    1382             : // As all those parameters are linked together, I'm not sure which one should be
    1383             : // considered as the reference.
    1384             : 
    1385           0 : CPLErr HDF5ImageDataset::CreateODIMH5Projection()
    1386             : {
    1387           0 :     const char *const pszProj4String = GetMetadataItem("where_projdef");
    1388           0 :     const char *const pszLL_lon = GetMetadataItem("where_LL_lon");
    1389           0 :     const char *const pszLL_lat = GetMetadataItem("where_LL_lat");
    1390           0 :     const char *const pszUR_lon = GetMetadataItem("where_UR_lon");
    1391           0 :     const char *const pszUR_lat = GetMetadataItem("where_UR_lat");
    1392           0 :     if (pszProj4String == nullptr || pszLL_lon == nullptr ||
    1393           0 :         pszLL_lat == nullptr || pszUR_lon == nullptr || pszUR_lat == nullptr)
    1394           0 :         return CE_Failure;
    1395             : 
    1396           0 :     m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    1397           0 :     if (m_oSRS.importFromProj4(pszProj4String) != OGRERR_NONE)
    1398           0 :         return CE_Failure;
    1399             : 
    1400           0 :     OGRSpatialReference oSRSWGS84;
    1401           0 :     oSRSWGS84.SetWellKnownGeogCS("WGS84");
    1402           0 :     oSRSWGS84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    1403             : 
    1404             :     OGRCoordinateTransformation *poCT =
    1405           0 :         OGRCreateCoordinateTransformation(&oSRSWGS84, &m_oSRS);
    1406           0 :     if (poCT == nullptr)
    1407           0 :         return CE_Failure;
    1408             : 
    1409             :     // Reproject corners from long,lat WGS84 to the target SRS.
    1410           0 :     double dfLLX = CPLAtof(pszLL_lon);
    1411           0 :     double dfLLY = CPLAtof(pszLL_lat);
    1412           0 :     double dfURX = CPLAtof(pszUR_lon);
    1413           0 :     double dfURY = CPLAtof(pszUR_lat);
    1414           0 :     if (!poCT->Transform(1, &dfLLX, &dfLLY) ||
    1415           0 :         !poCT->Transform(1, &dfURX, &dfURY))
    1416             :     {
    1417           0 :         delete poCT;
    1418           0 :         return CE_Failure;
    1419             :     }
    1420           0 :     delete poCT;
    1421             : 
    1422             :     // Compute the geotransform now.
    1423           0 :     const double dfPixelX = (dfURX - dfLLX) / nRasterXSize;
    1424           0 :     const double dfPixelY = (dfURY - dfLLY) / nRasterYSize;
    1425             : 
    1426           0 :     bHasGeoTransform = true;
    1427           0 :     adfGeoTransform[0] = dfLLX;
    1428           0 :     adfGeoTransform[1] = dfPixelX;
    1429           0 :     adfGeoTransform[2] = 0;
    1430           0 :     adfGeoTransform[3] = dfURY;
    1431           0 :     adfGeoTransform[4] = 0;
    1432           0 :     adfGeoTransform[5] = -dfPixelY;
    1433             : 
    1434           0 :     return CE_None;
    1435             : }
    1436             : 
    1437             : /************************************************************************/
    1438             : /*                         CreateProjections()                          */
    1439             : /************************************************************************/
    1440          47 : CPLErr HDF5ImageDataset::CreateProjections()
    1441             : {
    1442          47 :     switch (iSubdatasetType)
    1443             :     {
    1444           2 :         case CSK_PRODUCT:
    1445             :         {
    1446           2 :             int productType = PROD_UNKNOWN;
    1447             : 
    1448           2 :             if (GetMetadataItem("Product_Type") != nullptr)
    1449             :             {
    1450             :                 // Get the format's level.
    1451             :                 const char *osMissionLevel =
    1452           2 :                     HDF5Dataset::GetMetadataItem("Product_Type");
    1453             : 
    1454           2 :                 if (STARTS_WITH_CI(osMissionLevel, "RAW"))
    1455           0 :                     productType = PROD_CSK_L0;
    1456             : 
    1457           2 :                 if (STARTS_WITH_CI(osMissionLevel, "SSC"))
    1458           0 :                     productType = PROD_CSK_L1A;
    1459             : 
    1460           2 :                 if (STARTS_WITH_CI(osMissionLevel, "DGM"))
    1461           1 :                     productType = PROD_CSK_L1B;
    1462             : 
    1463           2 :                 if (STARTS_WITH_CI(osMissionLevel, "GEC"))
    1464           1 :                     productType = PROD_CSK_L1C;
    1465             : 
    1466           2 :                 if (STARTS_WITH_CI(osMissionLevel, "GTC"))
    1467           0 :                     productType = PROD_CSK_L1D;
    1468             :             }
    1469             : 
    1470           2 :             CaptureCSKGeoTransform(productType);
    1471           2 :             CaptureCSKGeolocation(productType);
    1472           2 :             CaptureCSKGCPs(productType);
    1473             : 
    1474           2 :             break;
    1475             :         }
    1476          45 :         case UNKNOWN_PRODUCT:
    1477             :         {
    1478          45 :             constexpr int NBGCPLAT = 100;
    1479          45 :             constexpr int NBGCPLON = 30;
    1480             : 
    1481          45 :             const int nDeltaLat = nRasterYSize / NBGCPLAT;
    1482          45 :             const int nDeltaLon = nRasterXSize / NBGCPLON;
    1483             : 
    1484          45 :             if (nDeltaLat == 0 || nDeltaLon == 0)
    1485          37 :                 return CE_None;
    1486             : 
    1487             :             // Create HDF5 Data Hierarchy in a link list.
    1488           8 :             poH5Objects = HDF5FindDatasetObjects(poH5RootGroup, "Latitude");
    1489           8 :             if (!poH5Objects)
    1490             :             {
    1491           8 :                 if (GetMetadataItem("where_projdef") != nullptr)
    1492           0 :                     return CreateODIMH5Projection();
    1493           8 :                 return CE_None;
    1494             :             }
    1495             : 
    1496             :             // The Latitude and Longitude arrays must have a rank of 2 to
    1497             :             // retrieve GCPs.
    1498           0 :             if (poH5Objects->nRank != 2 ||
    1499           0 :                 poH5Objects->paDims[0] != static_cast<size_t>(nRasterYSize) ||
    1500           0 :                 poH5Objects->paDims[1] != static_cast<size_t>(nRasterXSize))
    1501             :             {
    1502           0 :                 return CE_None;
    1503             :             }
    1504             : 
    1505             :             // Retrieve HDF5 data information.
    1506             :             const hid_t LatitudeDatasetID =
    1507           0 :                 H5Dopen(m_hHDF5, poH5Objects->pszPath);
    1508             :             // LatitudeDataspaceID = H5Dget_space(dataset_id);
    1509             : 
    1510           0 :             poH5Objects = HDF5FindDatasetObjects(poH5RootGroup, "Longitude");
    1511             :             // GCPs.
    1512           0 :             if (poH5Objects == nullptr || poH5Objects->nRank != 2 ||
    1513           0 :                 poH5Objects->paDims[0] != static_cast<size_t>(nRasterYSize) ||
    1514           0 :                 poH5Objects->paDims[1] != static_cast<size_t>(nRasterXSize))
    1515             :             {
    1516           0 :                 if (LatitudeDatasetID > 0)
    1517           0 :                     H5Dclose(LatitudeDatasetID);
    1518           0 :                 return CE_None;
    1519             :             }
    1520             : 
    1521             :             const hid_t LongitudeDatasetID =
    1522           0 :                 H5Dopen(m_hHDF5, poH5Objects->pszPath);
    1523             :             // LongitudeDataspaceID = H5Dget_space(dataset_id);
    1524             : 
    1525           0 :             if (LatitudeDatasetID > 0 && LongitudeDatasetID > 0)
    1526             :             {
    1527             :                 float *const Latitude =
    1528           0 :                     static_cast<float *>(VSI_MALLOC3_VERBOSE(
    1529             :                         nRasterYSize, nRasterXSize, sizeof(float)));
    1530             :                 float *const Longitude =
    1531           0 :                     static_cast<float *>(VSI_MALLOC3_VERBOSE(
    1532             :                         nRasterYSize, nRasterXSize, sizeof(float)));
    1533           0 :                 if (!Latitude || !Longitude)
    1534             :                 {
    1535           0 :                     CPLFree(Latitude);
    1536           0 :                     CPLFree(Longitude);
    1537           0 :                     H5Dclose(LatitudeDatasetID);
    1538           0 :                     H5Dclose(LongitudeDatasetID);
    1539           0 :                     return CE_Failure;
    1540             :                 }
    1541           0 :                 memset(Latitude, 0,
    1542           0 :                        static_cast<size_t>(nRasterXSize) * nRasterYSize *
    1543             :                            sizeof(float));
    1544           0 :                 memset(Longitude, 0,
    1545           0 :                        static_cast<size_t>(nRasterXSize) * nRasterYSize *
    1546             :                            sizeof(float));
    1547             : 
    1548             :                 // netCDF convention for nodata
    1549           0 :                 double dfLatNoData = 0;
    1550           0 :                 bool bHasLatNoData = GH5_FetchAttribute(
    1551             :                     LatitudeDatasetID, "_FillValue", dfLatNoData);
    1552             : 
    1553           0 :                 double dfLongNoData = 0;
    1554           0 :                 bool bHasLongNoData = GH5_FetchAttribute(
    1555             :                     LongitudeDatasetID, "_FillValue", dfLongNoData);
    1556             : 
    1557           0 :                 H5Dread(LatitudeDatasetID, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
    1558             :                         H5P_DEFAULT, Latitude);
    1559             : 
    1560           0 :                 H5Dread(LongitudeDatasetID, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
    1561             :                         H5P_DEFAULT, Longitude);
    1562             : 
    1563           0 :                 m_oSRS.Clear();
    1564           0 :                 m_oGCPSRS.SetWellKnownGeogCS("WGS84");
    1565             : 
    1566           0 :                 const int nYLimit =
    1567           0 :                     (static_cast<int>(nRasterYSize) / nDeltaLat) * nDeltaLat;
    1568           0 :                 const int nXLimit =
    1569           0 :                     (static_cast<int>(nRasterXSize) / nDeltaLon) * nDeltaLon;
    1570             : 
    1571             :                 // The original code in
    1572             :                 // https://trac.osgeo.org/gdal/changeset/8066 always add +180 to
    1573             :                 // the longitudes, but without justification I suspect this
    1574             :                 // might be due to handling products crossing the antimeridian.
    1575             :                 // Trying to do it just when needed through a heuristics.
    1576           0 :                 bool bHasLonNearMinus180 = false;
    1577           0 :                 bool bHasLonNearPlus180 = false;
    1578           0 :                 bool bHasLonNearZero = false;
    1579           0 :                 for (int j = 0; j < nYLimit; j += nDeltaLat)
    1580             :                 {
    1581           0 :                     for (int i = 0; i < nXLimit; i += nDeltaLon)
    1582             :                     {
    1583           0 :                         const int iGCP = j * nRasterXSize + i;
    1584           0 :                         if ((bHasLatNoData && static_cast<float>(dfLatNoData) ==
    1585           0 :                                                   Latitude[iGCP]) ||
    1586           0 :                             (bHasLongNoData &&
    1587           0 :                              static_cast<float>(dfLongNoData) ==
    1588           0 :                                  Longitude[iGCP]))
    1589           0 :                             continue;
    1590           0 :                         if (Longitude[iGCP] > 170 && Longitude[iGCP] <= 180)
    1591           0 :                             bHasLonNearPlus180 = true;
    1592           0 :                         if (Longitude[iGCP] < -170 && Longitude[iGCP] >= -180)
    1593           0 :                             bHasLonNearMinus180 = true;
    1594           0 :                         if (fabs(Longitude[iGCP]) < 90)
    1595           0 :                             bHasLonNearZero = true;
    1596             :                     }
    1597             :                 }
    1598             : 
    1599             :                 // Fill the GCPs list.
    1600             :                 const char *pszShiftGCP =
    1601           0 :                     CPLGetConfigOption("HDF5_SHIFT_GCPX_BY_180", nullptr);
    1602             :                 const bool bAdd180 =
    1603           0 :                     (bHasLonNearPlus180 && bHasLonNearMinus180 &&
    1604           0 :                      !bHasLonNearZero && pszShiftGCP == nullptr) ||
    1605           0 :                     (pszShiftGCP != nullptr && CPLTestBool(pszShiftGCP));
    1606             : 
    1607           0 :                 for (int j = 0; j < nYLimit; j += nDeltaLat)
    1608             :                 {
    1609           0 :                     for (int i = 0; i < nXLimit; i += nDeltaLon)
    1610             :                     {
    1611           0 :                         const int iGCP = j * nRasterXSize + i;
    1612           0 :                         if ((bHasLatNoData && static_cast<float>(dfLatNoData) ==
    1613           0 :                                                   Latitude[iGCP]) ||
    1614           0 :                             (bHasLongNoData &&
    1615           0 :                              static_cast<float>(dfLongNoData) ==
    1616           0 :                                  Longitude[iGCP]))
    1617           0 :                             continue;
    1618           0 :                         double dfGCPX = static_cast<double>(Longitude[iGCP]);
    1619           0 :                         if (bAdd180)
    1620           0 :                             dfGCPX += 180.0;
    1621           0 :                         const double dfGCPY =
    1622           0 :                             static_cast<double>(Latitude[iGCP]);
    1623             : 
    1624           0 :                         m_aoGCPs.emplace_back("", "", i + 0.5, j + 0.5, dfGCPX,
    1625           0 :                                               dfGCPY);
    1626             :                     }
    1627             :                 }
    1628             : 
    1629           0 :                 CPLFree(Latitude);
    1630           0 :                 CPLFree(Longitude);
    1631             :             }
    1632             : 
    1633           0 :             if (LatitudeDatasetID > 0)
    1634           0 :                 H5Dclose(LatitudeDatasetID);
    1635           0 :             if (LongitudeDatasetID > 0)
    1636           0 :                 H5Dclose(LongitudeDatasetID);
    1637             : 
    1638           0 :             break;
    1639             :         }
    1640             :     }
    1641             : 
    1642           2 :     return CE_None;
    1643             : }
    1644             : 
    1645             : /************************************************************************/
    1646             : /*                         GetMetadataItem()                            */
    1647             : /************************************************************************/
    1648             : 
    1649         120 : const char *HDF5ImageDataset::GetMetadataItem(const char *pszName,
    1650             :                                               const char *pszDomain)
    1651             : {
    1652         120 :     if (pszDomain && EQUAL(pszDomain, "__DEBUG__") &&
    1653          66 :         EQUAL(pszName, "WholeBandChunkOptim"))
    1654             :     {
    1655          66 :         switch (m_eWholeBandChunkOptim)
    1656             :         {
    1657           4 :             case WBC_DETECTION_IN_PROGRESS:
    1658           4 :                 return "DETECTION_IN_PROGRESS";
    1659           3 :             case WBC_DISABLED:
    1660           3 :                 return "DISABLED";
    1661          59 :             case WBC_ENABLED:
    1662          59 :                 return "ENABLED";
    1663             :         }
    1664             :     }
    1665          54 :     return GDALPamDataset::GetMetadataItem(pszName, pszDomain);
    1666             : }
    1667             : 
    1668             : /************************************************************************/
    1669             : /*                         GetSpatialRef()                              */
    1670             : /************************************************************************/
    1671             : 
    1672          12 : const OGRSpatialReference *HDF5ImageDataset::GetSpatialRef() const
    1673             : {
    1674          12 :     if (!m_oSRS.IsEmpty())
    1675          11 :         return &m_oSRS;
    1676           1 :     return GDALPamDataset::GetSpatialRef();
    1677             : }
    1678             : 
    1679             : /************************************************************************/
    1680             : /*                            GetGCPCount()                             */
    1681             : /************************************************************************/
    1682             : 
    1683           3 : int HDF5ImageDataset::GetGCPCount()
    1684             : 
    1685             : {
    1686           3 :     if (!m_aoGCPs.empty())
    1687           1 :         return static_cast<int>(m_aoGCPs.size());
    1688             : 
    1689           2 :     return GDALPamDataset::GetGCPCount();
    1690             : }
    1691             : 
    1692             : /************************************************************************/
    1693             : /*                        GetGCPSpatialRef()                            */
    1694             : /************************************************************************/
    1695             : 
    1696           1 : const OGRSpatialReference *HDF5ImageDataset::GetGCPSpatialRef() const
    1697             : 
    1698             : {
    1699           1 :     if (!m_aoGCPs.empty() && !m_oGCPSRS.IsEmpty())
    1700           1 :         return &m_oGCPSRS;
    1701             : 
    1702           0 :     return GDALPamDataset::GetGCPSpatialRef();
    1703             : }
    1704             : 
    1705             : /************************************************************************/
    1706             : /*                               GetGCPs()                              */
    1707             : /************************************************************************/
    1708             : 
    1709           2 : const GDAL_GCP *HDF5ImageDataset::GetGCPs()
    1710             : {
    1711           2 :     if (!m_aoGCPs.empty())
    1712           1 :         return gdal::GCP::c_ptr(m_aoGCPs);
    1713             : 
    1714           1 :     return GDALPamDataset::GetGCPs();
    1715             : }
    1716             : 
    1717             : /************************************************************************/
    1718             : /*                         GetGeoTransform()                            */
    1719             : /************************************************************************/
    1720             : 
    1721           6 : CPLErr HDF5ImageDataset::GetGeoTransform(double *padfTransform)
    1722             : {
    1723           6 :     if (bHasGeoTransform)
    1724             :     {
    1725           5 :         memcpy(padfTransform, adfGeoTransform, sizeof(double) * 6);
    1726           5 :         return CE_None;
    1727             :     }
    1728             : 
    1729           1 :     return GDALPamDataset::GetGeoTransform(padfTransform);
    1730             : }
    1731             : 
    1732             : /************************************************************************/
    1733             : /*                       IdentifyProductType()                          */
    1734             : /************************************************************************/
    1735             : 
    1736             : /**
    1737             :  * Identify if the subdataset has a known product format
    1738             :  * It stores a product identifier in iSubdatasetType,
    1739             :  * UNKNOWN_PRODUCT, if it isn't a recognizable format.
    1740             :  */
    1741          53 : void HDF5ImageDataset::IdentifyProductType()
    1742             : {
    1743          53 :     iSubdatasetType = UNKNOWN_PRODUCT;
    1744             : 
    1745             :     // COSMO-SKYMED
    1746             : 
    1747             :     // Get the Mission Id as a char *, because the field may not exist.
    1748          53 :     const char *const pszMissionId = HDF5Dataset::GetMetadataItem("Mission_ID");
    1749             : 
    1750             :     // If there is a Mission_ID field.
    1751          53 :     if (pszMissionId != nullptr && strstr(GetDescription(), "QLK") == nullptr)
    1752             :     {
    1753             :         // Check if the mission type is CSK, KMPS or CSG.
    1754             :         // KMPS: Komsat-5 is Korean mission with a SAR instrument.
    1755             :         // CSG: Cosmo Skymed 2nd Generation
    1756           2 :         if (EQUAL(pszMissionId, "CSK") || EQUAL(pszMissionId, "KMPS") ||
    1757           0 :             EQUAL(pszMissionId, "CSG"))
    1758             :         {
    1759           2 :             iSubdatasetType = CSK_PRODUCT;
    1760             : 
    1761           2 :             if (GetMetadataItem("Product_Type") != nullptr)
    1762             :             {
    1763             :                 // Get the format's level.
    1764             :                 const char *osMissionLevel =
    1765           2 :                     HDF5Dataset::GetMetadataItem("Product_Type");
    1766             : 
    1767           2 :                 if (STARTS_WITH_CI(osMissionLevel, "RAW"))
    1768           0 :                     iCSKProductType = PROD_CSK_L0;
    1769             : 
    1770           2 :                 if (STARTS_WITH_CI(osMissionLevel, "SCS"))
    1771           0 :                     iCSKProductType = PROD_CSK_L1A;
    1772             : 
    1773           2 :                 if (STARTS_WITH_CI(osMissionLevel, "DGM"))
    1774           1 :                     iCSKProductType = PROD_CSK_L1B;
    1775             : 
    1776           2 :                 if (STARTS_WITH_CI(osMissionLevel, "GEC"))
    1777           1 :                     iCSKProductType = PROD_CSK_L1C;
    1778             : 
    1779           2 :                 if (STARTS_WITH_CI(osMissionLevel, "GTC"))
    1780           0 :                     iCSKProductType = PROD_CSK_L1D;
    1781             :             }
    1782             :         }
    1783             :     }
    1784          53 : }
    1785             : 
    1786             : /************************************************************************/
    1787             : /*                       CaptureCSKGeolocation()                        */
    1788             : /************************************************************************/
    1789             : 
    1790             : /**
    1791             :  * Captures Geolocation information from a COSMO-SKYMED
    1792             :  * file.
    1793             :  * The geoid will always be WGS84
    1794             :  * The projection type may be UTM or UPS, depending on the
    1795             :  * latitude from the center of the image.
    1796             :  * @param iProductType type of CSK subproduct, see HDF5CSKProduct
    1797             :  */
    1798           2 : void HDF5ImageDataset::CaptureCSKGeolocation(int iProductType)
    1799             : {
    1800             :     // Set the ellipsoid to WGS84.
    1801           2 :     m_oSRS.SetWellKnownGeogCS("WGS84");
    1802             : 
    1803           2 :     if (iProductType == PROD_CSK_L1C || iProductType == PROD_CSK_L1D)
    1804             :     {
    1805           1 :         double *dfProjFalseEastNorth = nullptr;
    1806           1 :         double *dfProjScaleFactor = nullptr;
    1807           1 :         double *dfCenterCoord = nullptr;
    1808             : 
    1809             :         // Check if all the metadata attributes are present.
    1810           1 :         if (HDF5ReadDoubleAttr("Map Projection False East-North",
    1811           1 :                                &dfProjFalseEastNorth) == CE_Failure ||
    1812           1 :             HDF5ReadDoubleAttr("Map Projection Scale Factor",
    1813           1 :                                &dfProjScaleFactor) == CE_Failure ||
    1814           1 :             HDF5ReadDoubleAttr("Map Projection Centre", &dfCenterCoord) ==
    1815           2 :                 CE_Failure ||
    1816           1 :             GetMetadataItem("Projection_ID") == nullptr)
    1817             :         {
    1818           0 :             m_oSRS.Clear();
    1819           0 :             m_oGCPSRS.Clear();
    1820           0 :             CPLError(CE_Failure, CPLE_OpenFailed,
    1821             :                      "The CSK hdf5 file geolocation information is "
    1822             :                      "malformed");
    1823             :         }
    1824             :         else
    1825             :         {
    1826             :             // Fetch projection Type.
    1827           2 :             CPLString osProjectionID = GetMetadataItem("Projection_ID");
    1828             : 
    1829             :             // If the projection is UTM.
    1830           1 :             if (EQUAL(osProjectionID, "UTM"))
    1831             :             {
    1832             :                 // @TODO: use SetUTM
    1833           1 :                 m_oSRS.SetProjCS(SRS_PT_TRANSVERSE_MERCATOR);
    1834           1 :                 m_oSRS.SetTM(dfCenterCoord[0], dfCenterCoord[1],
    1835             :                              dfProjScaleFactor[0], dfProjFalseEastNorth[0],
    1836           1 :                              dfProjFalseEastNorth[1]);
    1837             :             }
    1838             :             else
    1839             :             {
    1840             :                 // TODO: No UPS projected files to test!
    1841             :                 // If the projection is UPS.
    1842           0 :                 if (EQUAL(osProjectionID, "UPS"))
    1843             :                 {
    1844           0 :                     m_oSRS.SetProjCS(SRS_PT_POLAR_STEREOGRAPHIC);
    1845           0 :                     m_oSRS.SetPS(dfCenterCoord[0], dfCenterCoord[1],
    1846             :                                  dfProjScaleFactor[0], dfProjFalseEastNorth[0],
    1847           0 :                                  dfProjFalseEastNorth[1]);
    1848             :                 }
    1849             :             }
    1850             : 
    1851           1 :             CPLFree(dfCenterCoord);
    1852           1 :             CPLFree(dfProjScaleFactor);
    1853           1 :             CPLFree(dfProjFalseEastNorth);
    1854           1 :         }
    1855             :     }
    1856             :     else
    1857             :     {
    1858           1 :         m_oGCPSRS = m_oSRS;
    1859             :     }
    1860           2 : }
    1861             : 
    1862             : /************************************************************************/
    1863             : /*                       CaptureCSKGeoTransform()                       */
    1864             : /************************************************************************/
    1865             : 
    1866             : /**
    1867             :  * Get Geotransform information for COSMO-SKYMED files
    1868             :  * In case of success it stores the transformation
    1869             :  * in adfGeoTransform. In case of failure it doesn't
    1870             :  * modify adfGeoTransform
    1871             :  * @param iProductType type of CSK subproduct, see HDF5CSKProduct
    1872             :  */
    1873           2 : void HDF5ImageDataset::CaptureCSKGeoTransform(int iProductType)
    1874             : {
    1875           2 :     const char *pszSubdatasetName = GetSubdatasetName();
    1876             : 
    1877           2 :     bHasGeoTransform = false;
    1878             :     // If the product level is not L1C or L1D then
    1879             :     // it doesn't have a valid projection.
    1880           2 :     if (iProductType == PROD_CSK_L1C || iProductType == PROD_CSK_L1D)
    1881             :     {
    1882             :         // If there is a subdataset.
    1883           1 :         if (pszSubdatasetName != nullptr)
    1884             :         {
    1885           2 :             CPLString osULPath = pszSubdatasetName;
    1886           1 :             osULPath += "/Top Left East-North";
    1887             : 
    1888           2 :             CPLString osLineSpacingPath = pszSubdatasetName;
    1889           1 :             osLineSpacingPath += "/Line Spacing";
    1890             : 
    1891           2 :             CPLString osColumnSpacingPath = pszSubdatasetName;
    1892           1 :             osColumnSpacingPath += "/Column Spacing";
    1893             : 
    1894           1 :             double *pdOutUL = nullptr;
    1895           1 :             double *pdLineSpacing = nullptr;
    1896           1 :             double *pdColumnSpacing = nullptr;
    1897             : 
    1898             :             // If it could find the attributes on the metadata.
    1899           1 :             if (HDF5ReadDoubleAttr(osULPath.c_str(), &pdOutUL) == CE_Failure ||
    1900           1 :                 HDF5ReadDoubleAttr(osLineSpacingPath.c_str(), &pdLineSpacing) ==
    1901           2 :                     CE_Failure ||
    1902           1 :                 HDF5ReadDoubleAttr(osColumnSpacingPath.c_str(),
    1903             :                                    &pdColumnSpacing) == CE_Failure)
    1904             :             {
    1905           0 :                 bHasGeoTransform = false;
    1906             :             }
    1907             :             else
    1908             :             {
    1909             :                 // geotransform[1] : width of pixel
    1910             :                 // geotransform[4] : rotational coefficient, zero for north up
    1911             :                 // images.
    1912             :                 // geotransform[2] : rotational coefficient, zero for north up
    1913             :                 // images.
    1914             :                 // geotransform[5] : height of pixel (but negative)
    1915             : 
    1916           1 :                 adfGeoTransform[0] = pdOutUL[0];
    1917           1 :                 adfGeoTransform[1] = pdLineSpacing[0];
    1918           1 :                 adfGeoTransform[2] = 0;
    1919           1 :                 adfGeoTransform[3] = pdOutUL[1];
    1920           1 :                 adfGeoTransform[4] = 0;
    1921           1 :                 adfGeoTransform[5] = -pdColumnSpacing[0];
    1922             : 
    1923           1 :                 CPLFree(pdOutUL);
    1924           1 :                 CPLFree(pdLineSpacing);
    1925           1 :                 CPLFree(pdColumnSpacing);
    1926             : 
    1927           1 :                 bHasGeoTransform = true;
    1928             :             }
    1929             :         }
    1930             :     }
    1931           2 : }
    1932             : 
    1933             : /************************************************************************/
    1934             : /*                          CaptureCSKGCPs()                            */
    1935             : /************************************************************************/
    1936             : 
    1937             : /**
    1938             :  * Retrieves and stores the GCPs from a COSMO-SKYMED dataset
    1939             :  * It only retrieves the GCPs for L0, L1A and L1B products
    1940             :  * for L1C and L1D products, geotransform is provided.
    1941             :  * The GCPs provided will be the Image's corners.
    1942             :  * @param iProductType type of CSK product @see HDF5CSKProductEnum
    1943             :  */
    1944           2 : void HDF5ImageDataset::CaptureCSKGCPs(int iProductType)
    1945             : {
    1946             :     // Only retrieve GCPs for L0,L1A and L1B products.
    1947           2 :     if (iProductType == PROD_CSK_L0 || iProductType == PROD_CSK_L1A ||
    1948             :         iProductType == PROD_CSK_L1B)
    1949             :     {
    1950          10 :         CPLString osCornerName[4];
    1951           1 :         double pdCornerPixel[4] = {0.0, 0.0, 0.0, 0.0};
    1952           1 :         double pdCornerLine[4] = {0.0, 0.0, 0.0, 0.0};
    1953             : 
    1954           1 :         const char *const pszSubdatasetName = GetSubdatasetName();
    1955             : 
    1956             :         // Load the subdataset name first.
    1957           5 :         for (int i = 0; i < 4; i++)
    1958           4 :             osCornerName[i] = pszSubdatasetName;
    1959             : 
    1960             :         // Load the attribute name, and raster coordinates for
    1961             :         // all the corners.
    1962           1 :         osCornerName[0] += "/Top Left Geodetic Coordinates";
    1963           1 :         pdCornerPixel[0] = 0;
    1964           1 :         pdCornerLine[0] = 0;
    1965             : 
    1966           1 :         osCornerName[1] += "/Top Right Geodetic Coordinates";
    1967           1 :         pdCornerPixel[1] = GetRasterXSize();
    1968           1 :         pdCornerLine[1] = 0;
    1969             : 
    1970           1 :         osCornerName[2] += "/Bottom Left Geodetic Coordinates";
    1971           1 :         pdCornerPixel[2] = 0;
    1972           1 :         pdCornerLine[2] = GetRasterYSize();
    1973             : 
    1974           1 :         osCornerName[3] += "/Bottom Right Geodetic Coordinates";
    1975           1 :         pdCornerPixel[3] = GetRasterXSize();
    1976           1 :         pdCornerLine[3] = GetRasterYSize();
    1977             : 
    1978             :         // For all the image's corners.
    1979           5 :         for (int i = 0; i < 4; i++)
    1980             :         {
    1981           4 :             double *pdCornerCoordinates = nullptr;
    1982             : 
    1983             :             // Retrieve the attributes.
    1984           4 :             if (HDF5ReadDoubleAttr(osCornerName[i].c_str(),
    1985           4 :                                    &pdCornerCoordinates) == CE_Failure)
    1986             :             {
    1987           0 :                 CPLError(CE_Failure, CPLE_OpenFailed,
    1988             :                          "Error retrieving CSK GCPs");
    1989           0 :                 m_aoGCPs.clear();
    1990           0 :                 break;
    1991             :             }
    1992             : 
    1993           0 :             m_aoGCPs.emplace_back(osCornerName[i].c_str(), "", pdCornerPixel[i],
    1994           4 :                                   pdCornerLine[i],
    1995           4 :                                   /* X = */ pdCornerCoordinates[1],
    1996             :                                   /* Y = */ pdCornerCoordinates[0],
    1997           4 :                                   /* Z = */ pdCornerCoordinates[2]);
    1998             : 
    1999             :             // Free the returned coordinates.
    2000           4 :             CPLFree(pdCornerCoordinates);
    2001             :         }
    2002             :     }
    2003           2 : }

Generated by: LCOV version 1.14