LCOV - code coverage report
Current view: top level - frmts/hdf5 - s104dataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 140 179 78.2 %
Date: 2025-02-20 10:14:44 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Hierarchical Data Format Release 5 (HDF5)
       4             :  * Purpose:  Read S104 datasets.
       5             :  * Author:   Even Rouault <even dot rouault at spatialys dot com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2023, Even Rouault <even dot rouault at spatialys dot com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "cpl_port.h"
      14             : #include "hdf5dataset.h"
      15             : #include "hdf5drivercore.h"
      16             : #include "gh5_convenience.h"
      17             : #include "rat.h"
      18             : #include "s100.h"
      19             : 
      20             : #include "gdal_priv.h"
      21             : #include "gdal_proxy.h"
      22             : #include "gdal_rat.h"
      23             : 
      24             : #include <limits>
      25             : 
      26             : /************************************************************************/
      27             : /*                             S104Dataset                              */
      28             : /************************************************************************/
      29             : 
      30             : class S104Dataset final : public S100BaseDataset
      31             : {
      32             :   public:
      33           4 :     explicit S104Dataset(const std::string &osFilename)
      34           4 :         : S100BaseDataset(osFilename)
      35             :     {
      36           4 :     }
      37             : 
      38             :     static GDALDataset *Open(GDALOpenInfo *);
      39             : };
      40             : 
      41             : /************************************************************************/
      42             : /*                            S104RasterBand                            */
      43             : /************************************************************************/
      44             : 
      45             : class S104RasterBand final : public GDALProxyRasterBand
      46             : {
      47             :     friend class S104Dataset;
      48             :     std::unique_ptr<GDALDataset> m_poDS{};
      49             :     GDALRasterBand *m_poUnderlyingBand = nullptr;
      50             :     std::string m_osUnitType{};
      51             :     std::unique_ptr<GDALRasterAttributeTable> m_poRAT{};
      52             : 
      53             :   public:
      54           4 :     explicit S104RasterBand(std::unique_ptr<GDALDataset> &&poDSIn)
      55           8 :         : m_poDS(std::move(poDSIn)),
      56           4 :           m_poUnderlyingBand(m_poDS->GetRasterBand(1))
      57             :     {
      58           4 :         eDataType = m_poUnderlyingBand->GetRasterDataType();
      59           4 :         m_poUnderlyingBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
      60           4 :     }
      61             : 
      62             :     GDALRasterBand *
      63           8 :     RefUnderlyingRasterBand(bool /*bForceOpen*/ = true) const override
      64             :     {
      65           8 :         return m_poUnderlyingBand;
      66             :     }
      67             : 
      68           2 :     const char *GetUnitType() override
      69             :     {
      70           2 :         return m_osUnitType.c_str();
      71             :     }
      72             : 
      73           1 :     GDALRasterAttributeTable *GetDefaultRAT() override
      74             :     {
      75           1 :         return m_poRAT.get();
      76             :     }
      77             : };
      78             : 
      79             : /************************************************************************/
      80             : /*                                Open()                                */
      81             : /************************************************************************/
      82             : 
      83           5 : GDALDataset *S104Dataset::Open(GDALOpenInfo *poOpenInfo)
      84             : 
      85             : {
      86             :     // Confirm that this appears to be a S104 file.
      87           5 :     if (!S104DatasetIdentify(poOpenInfo))
      88           0 :         return nullptr;
      89             : 
      90             :     HDF5_GLOBAL_LOCK();
      91             : 
      92           5 :     if (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER)
      93             :     {
      94           1 :         return HDF5Dataset::OpenMultiDim(poOpenInfo);
      95             :     }
      96             : 
      97             :     // Confirm the requested access is supported.
      98           4 :     if (poOpenInfo->eAccess == GA_Update)
      99             :     {
     100           0 :         ReportUpdateNotSupportedByDriver("S104");
     101           0 :         return nullptr;
     102             :     }
     103             : 
     104           8 :     std::string osFilename(poOpenInfo->pszFilename);
     105           8 :     std::string osGroup;
     106           4 :     if (STARTS_WITH(poOpenInfo->pszFilename, "S104:"))
     107             :     {
     108             :         const CPLStringList aosTokens(
     109           3 :             CSLTokenizeString2(poOpenInfo->pszFilename, ":",
     110           3 :                                CSLT_HONOURSTRINGS | CSLT_PRESERVEESCAPES));
     111             : 
     112           3 :         if (aosTokens.size() == 2)
     113             :         {
     114           0 :             osFilename = aosTokens[1];
     115             :         }
     116           3 :         else if (aosTokens.size() == 3)
     117             :         {
     118           3 :             osFilename = aosTokens[1];
     119           3 :             osGroup = aosTokens[2];
     120             :         }
     121             :         else
     122             :         {
     123           0 :             return nullptr;
     124             :         }
     125             :     }
     126             : 
     127           8 :     auto poDS = std::make_unique<S104Dataset>(osFilename);
     128           4 :     if (!poDS->Init())
     129           0 :         return nullptr;
     130             : 
     131           4 :     const auto &poRootGroup = poDS->m_poRootGroup;
     132             : 
     133          12 :     auto poWaterLevel = poRootGroup->OpenGroup("WaterLevel");
     134           4 :     if (!poWaterLevel)
     135             :     {
     136           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Cannot find /WaterLevel group");
     137           0 :         return nullptr;
     138             :     }
     139             : 
     140          12 :     auto poDataCodingFormat = poWaterLevel->GetAttribute("dataCodingFormat");
     141           8 :     if (!poDataCodingFormat ||
     142           4 :         poDataCodingFormat->GetDataType().GetClass() != GEDTC_NUMERIC)
     143             :     {
     144           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     145             :                  "Cannot find /WaterLevel/dataCodingFormat attribute");
     146           0 :         return nullptr;
     147             :     }
     148           4 :     const int nDataCodingFormat = poDataCodingFormat->ReadAsInt();
     149           4 :     if (nDataCodingFormat != 2)
     150             :     {
     151           0 :         CPLError(CE_Failure, CPLE_NotSupported,
     152             :                  "dataCodingFormat=%d is not supported by the S104 driver",
     153             :                  nDataCodingFormat);
     154           0 :         return nullptr;
     155             :     }
     156             : 
     157             :     // Read additional metadata
     158          12 :     for (const char *pszAttrName :
     159          16 :          {"methodWaterLevelProduct", "minDatasetHeight", "maxDatasetHeight"})
     160             :     {
     161          36 :         auto poAttr = poWaterLevel->GetAttribute(pszAttrName);
     162          12 :         if (poAttr)
     163             :         {
     164           8 :             const char *pszVal = poAttr->ReadAsString();
     165           8 :             if (pszVal)
     166             :             {
     167           8 :                 poDS->GDALDataset::SetMetadataItem(pszAttrName, pszVal);
     168             :             }
     169             :         }
     170             :     }
     171             : 
     172          12 :     auto poWaterLevel01 = poWaterLevel->OpenGroup("WaterLevel.01");
     173           4 :     if (!poWaterLevel01)
     174             :     {
     175           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     176             :                  "Cannot find /WaterLevel/WaterLevel.01 group");
     177           0 :         return nullptr;
     178             :     }
     179             : 
     180             :     // Read additional metadata
     181          16 :     for (const char *pszAttrName :
     182             :          {"timeRecordInterval", "dateTimeOfFirstRecord", "dateTimeOfLastRecord",
     183          20 :           "numberOfTimes"})
     184             :     {
     185          48 :         auto poAttr = poWaterLevel01->GetAttribute(pszAttrName);
     186          16 :         if (poAttr)
     187             :         {
     188          16 :             const char *pszVal = poAttr->ReadAsString();
     189          16 :             if (pszVal)
     190             :             {
     191          16 :                 poDS->GDALDataset::SetMetadataItem(pszAttrName, pszVal);
     192             :             }
     193             :         }
     194             :     }
     195             : 
     196           8 :     if (auto poStartSequence = poWaterLevel01->GetAttribute("startSequence"))
     197             :     {
     198           4 :         const char *pszStartSequence = poStartSequence->ReadAsString();
     199           4 :         if (pszStartSequence && !EQUAL(pszStartSequence, "0,0"))
     200             :         {
     201           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     202             :                      "startSequence (=%s) != 0,0 is not supported",
     203             :                      pszStartSequence);
     204           0 :             return nullptr;
     205             :         }
     206             :     }
     207             : 
     208           4 :     if (!S100GetNumPointsLongitudinalLatitudinal(
     209           4 :             poWaterLevel01.get(), poDS->nRasterXSize, poDS->nRasterYSize))
     210             :     {
     211           0 :         return nullptr;
     212             :     }
     213             : 
     214           4 :     const bool bNorthUp = CPLTestBool(
     215           4 :         CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "NORTH_UP", "YES"));
     216             : 
     217             :     // Compute geotransform
     218           4 :     poDS->m_bHasGT = S100GetGeoTransform(poWaterLevel01.get(),
     219           4 :                                          poDS->m_adfGeoTransform, bNorthUp);
     220             : 
     221           4 :     if (osGroup.empty())
     222             :     {
     223           2 :         const auto aosGroupNames = poWaterLevel01->GetGroupNames();
     224           1 :         int iSubDS = 1;
     225           2 :         for (const auto &osSubGroup : aosGroupNames)
     226             :         {
     227           2 :             if (auto poSubGroup = poWaterLevel01->OpenGroup(osSubGroup))
     228             :             {
     229           1 :                 poDS->GDALDataset::SetMetadataItem(
     230             :                     CPLSPrintf("SUBDATASET_%d_NAME", iSubDS),
     231             :                     CPLSPrintf("S104:\"%s\":%s", osFilename.c_str(),
     232             :                                osSubGroup.c_str()),
     233             :                     "SUBDATASETS");
     234           2 :                 std::string osSubDSDesc = "Values for group ";
     235           1 :                 osSubDSDesc += osSubGroup;
     236           2 :                 const auto poTimePoint = poSubGroup->GetAttribute("timePoint");
     237           1 :                 if (poTimePoint)
     238             :                 {
     239           1 :                     const char *pszVal = poTimePoint->ReadAsString();
     240           1 :                     if (pszVal)
     241             :                     {
     242           1 :                         osSubDSDesc = "Values at timestamp ";
     243           1 :                         osSubDSDesc += pszVal;
     244             :                     }
     245             :                 }
     246           1 :                 poDS->GDALDataset::SetMetadataItem(
     247             :                     CPLSPrintf("SUBDATASET_%d_DESC", iSubDS),
     248             :                     osSubDSDesc.c_str(), "SUBDATASETS");
     249           1 :                 ++iSubDS;
     250             :             }
     251             :         }
     252             :     }
     253             :     else
     254             :     {
     255           3 :         auto poGroup = poWaterLevel01->OpenGroup(osGroup);
     256           3 :         if (!poGroup)
     257             :         {
     258           1 :             CPLError(CE_Failure, CPLE_AppDefined,
     259             :                      "Cannot find /WaterLevel/WaterLevel.01/%s group",
     260             :                      osGroup.c_str());
     261           1 :             return nullptr;
     262             :         }
     263             : 
     264           4 :         auto poValuesArray = poGroup->OpenMDArray("values");
     265           2 :         if (!poValuesArray)
     266             :         {
     267           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     268             :                      "Cannot find /WaterLevel/WaterLevel.01/%s/values array",
     269             :                      osGroup.c_str());
     270           0 :             return nullptr;
     271             :         }
     272             : 
     273           2 :         if (poValuesArray->GetDimensionCount() != 2)
     274             :         {
     275           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     276             :                      "Wrong dimension count for %s",
     277           0 :                      poValuesArray->GetFullName().c_str());
     278           0 :             return nullptr;
     279             :         }
     280             : 
     281           2 :         const auto &oType = poValuesArray->GetDataType();
     282           2 :         if (oType.GetClass() != GEDTC_COMPOUND)
     283             :         {
     284           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Wrong data type for %s",
     285           0 :                      poValuesArray->GetFullName().c_str());
     286           0 :             return nullptr;
     287             :         }
     288             : 
     289           2 :         const auto &oComponents = oType.GetComponents();
     290           2 :         if (oComponents.size() != 2 ||
     291           4 :             oComponents[0]->GetName() != "waterLevelHeight" ||
     292           2 :             oComponents[0]->GetType().GetNumericDataType() != GDT_Float32 ||
     293           6 :             oComponents[1]->GetName() != "waterLevelTrend" ||
     294           2 :             (oComponents[1]->GetType().GetNumericDataType() != GDT_Byte &&
     295             :              // In theory should be Byte, but 104US00_ches_dcf2_20190606T12Z.h5 uses Int32
     296           0 :              oComponents[1]->GetType().GetNumericDataType() != GDT_Int32))
     297             :         {
     298           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Wrong data type for %s",
     299           0 :                      poValuesArray->GetFullName().c_str());
     300           0 :             return nullptr;
     301             :         }
     302             : 
     303           2 :         const auto &apoDims = poValuesArray->GetDimensions();
     304           2 :         if (apoDims[0]->GetSize() != static_cast<unsigned>(poDS->nRasterYSize))
     305             :         {
     306           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     307             :                      "numPointsLatitudinal(=%d) doesn't match first dimension "
     308             :                      "size of %s (=%d)",
     309           0 :                      poDS->nRasterYSize, poValuesArray->GetFullName().c_str(),
     310           0 :                      static_cast<int>(apoDims[0]->GetSize()));
     311           0 :             return nullptr;
     312             :         }
     313           2 :         if (apoDims[1]->GetSize() != static_cast<unsigned>(poDS->nRasterXSize))
     314             :         {
     315           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     316             :                      "numPointsLongitudinal(=%d) doesn't match second "
     317             :                      "dimension size of %s (=%d)",
     318           0 :                      poDS->nRasterXSize, poValuesArray->GetFullName().c_str(),
     319           0 :                      static_cast<int>(apoDims[1]->GetSize()));
     320           0 :             return nullptr;
     321             :         }
     322             : 
     323           2 :         if (bNorthUp)
     324           1 :             poValuesArray = poValuesArray->GetView("[::-1,...]");
     325             : 
     326             :         // Create waterLevelHeight band
     327             :         auto poWaterLevelHeight =
     328           6 :             poValuesArray->GetView("[\"waterLevelHeight\"]");
     329             :         auto poWaterLevelHeightDS = std::unique_ptr<GDALDataset>(
     330           4 :             poWaterLevelHeight->AsClassicDataset(1, 0));
     331             :         auto poWaterLevelHeightBand =
     332           4 :             std::make_unique<S104RasterBand>(std::move(poWaterLevelHeightDS));
     333           2 :         poWaterLevelHeightBand->SetDescription("waterLevelHeight");
     334           2 :         poWaterLevelHeightBand->m_osUnitType = "metre";
     335           2 :         poDS->SetBand(1, poWaterLevelHeightBand.release());
     336             : 
     337             :         // Create waterLevelTrend band
     338             :         auto poWaterLevelTrend =
     339           6 :             poValuesArray->GetView("[\"waterLevelTrend\"]");
     340             :         auto poWaterLevelTrendDS = std::unique_ptr<GDALDataset>(
     341           4 :             poWaterLevelTrend->AsClassicDataset(1, 0));
     342             :         auto poWaterLevelTrendBand =
     343           4 :             std::make_unique<S104RasterBand>(std::move(poWaterLevelTrendDS));
     344           2 :         poWaterLevelTrendBand->SetDescription("waterLevelTrend");
     345             : 
     346             :         // From D-5.3 Water Level Trend of S-101 v1.1 spec
     347           4 :         auto poRAT = std::make_unique<GDALDefaultRasterAttributeTable>();
     348           2 :         poRAT->CreateColumn("code", GFT_Integer, GFU_MinMax);
     349           2 :         poRAT->CreateColumn("label", GFT_String, GFU_Generic);
     350           2 :         poRAT->CreateColumn("definition", GFT_String, GFU_Generic);
     351             : 
     352             :         const struct
     353             :         {
     354             :             int nCode;
     355             :             const char *pszLabel;
     356             :             const char *pszDefinition;
     357           2 :         } aoRatValues[] = {
     358             :             {0, "Nodata", "No data"},
     359             :             {1, "Decreasing", "Becoming smaller in magnitude"},
     360             :             {2, "Increasing", "Becoming larger in magnitude"},
     361             :             {3, "Steady", "Constant"},
     362             :         };
     363             : 
     364           2 :         int iRow = 0;
     365          10 :         for (const auto &oRecord : aoRatValues)
     366             :         {
     367           8 :             int iCol = 0;
     368           8 :             poRAT->SetValue(iRow, iCol++, oRecord.nCode);
     369           8 :             poRAT->SetValue(iRow, iCol++, oRecord.pszLabel);
     370           8 :             poRAT->SetValue(iRow, iCol++, oRecord.pszDefinition);
     371           8 :             ++iRow;
     372             :         }
     373             : 
     374           2 :         poWaterLevelTrendBand->m_poRAT = std::move(poRAT);
     375             : 
     376           2 :         poDS->SetBand(2, poWaterLevelTrendBand.release());
     377             :     }
     378             : 
     379           3 :     poDS->GDALDataset::SetMetadataItem(GDALMD_AREA_OR_POINT, GDALMD_AOP_POINT);
     380             : 
     381             :     // Setup/check for pam .aux.xml.
     382           3 :     poDS->SetDescription(osFilename.c_str());
     383           3 :     poDS->TryLoadXML();
     384             : 
     385             :     // Setup overviews.
     386           3 :     poDS->oOvManager.Initialize(poDS.get(), osFilename.c_str());
     387             : 
     388           3 :     return poDS.release();
     389             : }
     390             : 
     391             : /************************************************************************/
     392             : /*                      S104DatasetDriverUnload()                       */
     393             : /************************************************************************/
     394             : 
     395           6 : static void S104DatasetDriverUnload(GDALDriver *)
     396             : {
     397           6 :     HDF5UnloadFileDriver();
     398           6 : }
     399             : 
     400             : /************************************************************************/
     401             : /*                         GDALRegister_S104()                          */
     402             : /************************************************************************/
     403          10 : void GDALRegister_S104()
     404             : 
     405             : {
     406          10 :     if (!GDAL_CHECK_VERSION("S104"))
     407           0 :         return;
     408             : 
     409          10 :     if (GDALGetDriverByName(S104_DRIVER_NAME) != nullptr)
     410           0 :         return;
     411             : 
     412          10 :     GDALDriver *poDriver = new GDALDriver();
     413             : 
     414          10 :     S104DriverSetCommonMetadata(poDriver);
     415          10 :     poDriver->pfnOpen = S104Dataset::Open;
     416          10 :     poDriver->pfnUnloadDriver = S104DatasetDriverUnload;
     417             : 
     418          10 :     GetGDALDriverManager()->RegisterDriver(poDriver);
     419             : }

Generated by: LCOV version 1.14