LCOV - code coverage report
Current view: top level - frmts/rcm - rcmdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 665 1030 64.6 %
Date: 2025-08-01 10:10:57 Functions: 22 27 81.5 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  DRDC Ottawa GEOINT
       4             :  * Purpose:  Radarsat Constellation Mission - XML Products (product.xml) driver
       5             :  * Author:   Roberto Caron, MDA
       6             :  *           on behalf of DRDC Ottawa
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2020, DRDC Ottawa
      10             :  *
      11             :  * Based on the RS2 Dataset Class
      12             :  *
      13             :  * SPDX-License-Identifier: MIT
      14             :  ****************************************************************************/
      15             : 
      16             : #include <time.h>
      17             : #include <stdio.h>
      18             : #include <sstream>
      19             : 
      20             : #include "cpl_minixml.h"
      21             : #include "gdal_frmts.h"
      22             : #include "gdal_pam.h"
      23             : #include "ogr_spatialref.h"
      24             : #include "rcmdataset.h"
      25             : #include "rcmdrivercore.h"
      26             : 
      27             : #include <limits>
      28             : 
      29             : constexpr int max_space_for_string = 32;
      30             : 
      31             : /* RCM has a special folder that contains all LUT, Incidence Angle and Noise
      32             :  * Level files */
      33             : constexpr const char *CALIBRATION_FOLDER = "calibration";
      34             : 
      35             : /*** Function to format calibration for unique identification for Layer Name
      36             :  * ***/
      37             : /*
      38             :  *  RCM_CALIB : { SIGMA0 | GAMMA0 | BETA0 | UNCALIB } : product.xml full path
      39             :  */
      40          56 : inline CPLString FormatCalibration(const char *pszCalibName,
      41             :                                    const char *pszFilename)
      42             : {
      43          56 :     CPLString ptr;
      44             : 
      45             :     // Always begin by the layer calibration name
      46          56 :     ptr.append(szLayerCalibration);
      47             : 
      48             :     // A separator is needed before concat calibration name
      49          56 :     ptr += chLayerSeparator;
      50             :     // Add calibration name
      51          56 :     ptr.append(pszCalibName);
      52             : 
      53             :     // A separator is needed before concat full filename name
      54          56 :     ptr += chLayerSeparator;
      55             :     // Add full filename name
      56          56 :     ptr.append(pszFilename);
      57             : 
      58             :     /* return calibration format */
      59          56 :     return ptr;
      60             : }
      61             : 
      62             : /*** Function to test for valid LUT files ***/
      63          72 : static bool IsValidXMLFile(const char *pszPath)
      64             : {
      65             :     /* Return true for valid xml file, false otherwise */
      66          72 :     CPLXMLTreeCloser psLut(CPLParseXMLFile(pszPath));
      67             : 
      68          72 :     if (psLut.get() == nullptr)
      69             :     {
      70           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
      71             :                  "ERROR: Failed to open the LUT file %s", pszPath);
      72             :     }
      73             : 
      74         144 :     return psLut.get() != nullptr;
      75             : }
      76             : 
      77          14 : static double *InterpolateValues(CSLConstList papszList, int tableSize,
      78             :                                  int stepSize, int numberOfValues,
      79             :                                  int pixelFirstLutValue)
      80             : {
      81             :     /* Allocate the right LUT size according to the product range pixel */
      82             :     double *table =
      83          14 :         static_cast<double *>(VSI_CALLOC_VERBOSE(sizeof(double), tableSize));
      84          14 :     if (!table)
      85           0 :         return nullptr;
      86             : 
      87          14 :     if (stepSize <= 0)
      88             :     {
      89             :         /* When negative, the range of pixel is calculated from the opposite
      90             :          * starting from the end of gains array */
      91             :         /* Just step the range with positive value */
      92          14 :         const int positiveStepSize = abs(stepSize);
      93             : 
      94          14 :         int k = 0;
      95             : 
      96          14 :         if (positiveStepSize == 1)
      97             :         {
      98             :             // Be fast and just copy the values because all gain values
      99             :             // represent all image wide pixel
     100             :             /* Start at the end position and store in the opposite */
     101           0 :             for (int i = pixelFirstLutValue; i >= 0; i--)
     102             :             {
     103           0 :                 const double value = CPLAtof(papszList[i]);
     104           0 :                 table[k++] = value;
     105             :             }
     106             :         }
     107             :         else
     108             :         {
     109             : 
     110             :             /* Interpolation between 2 numbers */
     111         422 :             for (int i = numberOfValues - 1; i >= 0; i--)
     112             :             {
     113             :                 // We will consider the same value to cover the case that we
     114             :                 // will hit the last pixel
     115         408 :                 double valueFrom = CPLAtof(papszList[i]);
     116         408 :                 double valueTo = valueFrom;
     117             : 
     118         408 :                 if (i > 0)
     119             :                 {
     120             :                     // We have room to pick the previous number to interpolate
     121             :                     // with
     122         394 :                     valueTo = CPLAtof(papszList[i - 1]);
     123             :                 }
     124             : 
     125             :                 // If the valueFrom minus ValueTo equal 0, it means to finish
     126             :                 // off with the same number until the end of the table size
     127         408 :                 double interp = (valueTo - valueFrom) / positiveStepSize;
     128             : 
     129             :                 // Always begin with the value FROM found
     130         408 :                 table[k++] = valueFrom;
     131             : 
     132             :                 // Then add interpolation, don't forget. The stepSize is
     133             :                 // actually counting our valueFrom number thus we add
     134             :                 // interpolation until the last step - 1
     135      116341 :                 for (int j = 0; j < positiveStepSize - 1; j++)
     136             :                 {
     137      115933 :                     valueFrom += interp;
     138      115933 :                     table[k++] = valueFrom;
     139             :                 }
     140             :             }
     141             :         }
     142             :     }
     143             :     else
     144             :     {
     145             :         /* When positive, the range of pixel is calculated from the beginning of
     146             :          * gains array */
     147           0 :         if (stepSize == 1)
     148             :         {
     149             :             // Be fast and just copy the values because all gain values
     150             :             // represent all image wide pixel
     151           0 :             for (int i = 0; i < numberOfValues; i++)
     152             :             {
     153           0 :                 const double value = CPLAtof(papszList[i]);
     154           0 :                 table[i] = value;
     155             :             }
     156             :         }
     157             :         else
     158             :         {
     159             :             /* Interpolation between 2 numbers */
     160           0 :             int k = 0;
     161           0 :             for (int i = 0; i < numberOfValues; i++)
     162             :             {
     163             :                 // We will consider the same value to cover the case that we
     164             :                 // will hit the last pixel
     165           0 :                 double valueFrom = CPLAtof(papszList[i]);
     166           0 :                 double valueTo = valueFrom;
     167             : 
     168           0 :                 if (i < (numberOfValues)-1)
     169             :                 {
     170             :                     // We have room to pick the next number to interpolate with
     171           0 :                     valueTo = CPLAtof(papszList[i + 1]);
     172             :                 }
     173             : 
     174             :                 // If the valueFrom minus ValueTo equal 0, it means to finish
     175             :                 // off with the same number until the end of the table size
     176           0 :                 double interp = (valueTo - valueFrom) / stepSize;
     177             : 
     178             :                 // Always begin with the value FROM found
     179           0 :                 table[k++] = valueFrom;
     180             : 
     181             :                 // Then add interpolation, don't forget. The stepSize is
     182             :                 // actually counting our valueFrom number thus we add
     183             :                 // interpolation until the last step - 1
     184           0 :                 for (int j = 0; j < stepSize - 1; j++)
     185             :                 {
     186           0 :                     valueFrom += interp;
     187           0 :                     table[k++] = valueFrom;
     188             :                 }
     189             :             }
     190             :         }
     191             :     }
     192             : 
     193          14 :     return table;
     194             : }
     195             : 
     196             : /*** check that the referenced dataset for each band has the
     197             : correct data type and returns whether a 2 band I+Q dataset should
     198             : be mapped onto a single complex band.
     199             : Returns BANDERROR for error, STRAIGHT for 1:1 mapping, TWOBANDCOMPLEX for 2
     200             : bands -> 1 complex band
     201             : */
     202             : typedef enum
     203             : {
     204             :     BANDERROR,
     205             :     STRAIGHT,
     206             :     TWOBANDCOMPLEX
     207             : } BandMappingRCM;
     208             : 
     209          16 : static BandMappingRCM checkBandFileMappingRCM(GDALDataType dataType,
     210             :                                               GDALDataset *poBandFile,
     211             :                                               bool isNITF)
     212             : {
     213             : 
     214          16 :     GDALRasterBand *band1 = poBandFile->GetRasterBand(1);
     215          16 :     GDALDataType bandfileType = band1->GetRasterDataType();
     216             :     // if there is one band and it has the same datatype, the band file gets
     217             :     // passed straight through
     218          16 :     if ((poBandFile->GetRasterCount() == 1 ||
     219          16 :          poBandFile->GetRasterCount() == 4) &&
     220             :         dataType == bandfileType)
     221          16 :         return STRAIGHT;
     222             : 
     223             :     // if the band file has 2 bands, they should represent I+Q
     224             :     // and be a compatible data type
     225           0 :     if (poBandFile->GetRasterCount() == 2 && GDALDataTypeIsComplex(dataType))
     226             :     {
     227           0 :         GDALRasterBand *band2 = poBandFile->GetRasterBand(2);
     228             : 
     229           0 :         if (bandfileType != band2->GetRasterDataType())
     230           0 :             return BANDERROR;  // both bands must be same datatype
     231             : 
     232             :         // check compatible types - there are 4 complex types in GDAL
     233           0 :         if ((dataType == GDT_CInt16 && bandfileType == GDT_Int16) ||
     234           0 :             (dataType == GDT_CInt32 && bandfileType == GDT_Int32) ||
     235           0 :             (dataType == GDT_CFloat32 && bandfileType == GDT_Float32) ||
     236           0 :             (dataType == GDT_CFloat64 && bandfileType == GDT_Float64))
     237           0 :             return TWOBANDCOMPLEX;
     238             : 
     239           0 :         if ((dataType == GDT_CInt16 && bandfileType == GDT_CInt16) ||
     240           0 :             (dataType == GDT_CInt32 && bandfileType == GDT_CInt32) ||
     241           0 :             (dataType == GDT_CFloat32 && bandfileType == GDT_CFloat32) ||
     242           0 :             (dataType == GDT_CFloat64 && bandfileType == GDT_CFloat64))
     243           0 :             return TWOBANDCOMPLEX;
     244             :     }
     245             : 
     246           0 :     if (isNITF)
     247             :     {
     248           0 :         return STRAIGHT;
     249             :     }
     250             : 
     251           0 :     return BANDERROR;  // don't accept any other combinations
     252             : }
     253             : 
     254             : /************************************************************************/
     255             : /*                            RCMRasterBand                             */
     256             : /************************************************************************/
     257             : 
     258          10 : RCMRasterBand::RCMRasterBand(RCMDataset *poDSIn, int nBandIn,
     259             :                              GDALDataType eDataTypeIn, const char *pszPole,
     260             :                              GDALDataset *poBandFileIn, bool bTwoBandComplex,
     261          10 :                              bool isOneFilePerPolIn, bool isNITFIn)
     262             :     : poBandFile(poBandFileIn), poRCMDataset(poDSIn),
     263             :       twoBandComplex(bTwoBandComplex), isOneFilePerPol(isOneFilePerPolIn),
     264          10 :       isNITF(isNITFIn)
     265             : {
     266          10 :     poDS = poDSIn;
     267          10 :     this->nBand = nBandIn;
     268          10 :     eDataType = eDataTypeIn;
     269             : 
     270             :     /*Check image type, whether there is one file per polarization or
     271             :      *one file containing all polarizations*/
     272          10 :     if (this->isOneFilePerPol)
     273             :     {
     274          10 :         poBand = poBandFile->GetRasterBand(1);
     275             :     }
     276             :     else
     277             :     {
     278           0 :         poBand = poBandFile->GetRasterBand(this->nBand);
     279             :     }
     280             : 
     281          10 :     poBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
     282             : 
     283          10 :     if (pszPole != nullptr && strlen(pszPole) != 0)
     284             :     {
     285          10 :         SetMetadataItem("POLARIMETRIC_INTERP", pszPole);
     286             :     }
     287          10 : }
     288             : 
     289             : /************************************************************************/
     290             : /*                            RCMRasterBand()                            */
     291             : /************************************************************************/
     292             : 
     293          20 : RCMRasterBand::~RCMRasterBand()
     294             : 
     295             : {
     296          10 :     if (poBandFile != nullptr)
     297          10 :         GDALClose(reinterpret_cast<GDALRasterBandH>(poBandFile));
     298          20 : }
     299             : 
     300             : /************************************************************************/
     301             : /*                             IReadBlock()                             */
     302             : /************************************************************************/
     303             : 
     304          13 : CPLErr RCMRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
     305             : 
     306             : {
     307          13 :     int nRequestXSize = 0;
     308          13 :     int nRequestYSize = 0;
     309          13 :     GetActualBlockSize(nBlockXOff, nBlockYOff, &nRequestXSize, &nRequestYSize);
     310             : 
     311             :     // Zero initial partial right-most and bottom-most blocks
     312          13 :     if (nRequestXSize < nBlockXSize || nRequestYSize < nBlockYSize)
     313             :     {
     314           1 :         memset(pImage, 0,
     315           1 :                static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
     316           1 :                    nBlockXSize * nBlockYSize);
     317             :     }
     318             : 
     319          13 :     int dataTypeSize = GDALGetDataTypeSizeBytes(eDataType);
     320             :     GDALDataType bandFileType =
     321          13 :         poBandFile->GetRasterBand(1)->GetRasterDataType();
     322          13 :     int bandFileSize = GDALGetDataTypeSizeBytes(bandFileType);
     323             : 
     324             :     // case: 2 bands representing I+Q -> one complex band
     325          13 :     if (twoBandComplex && !this->isNITF)
     326             :     {
     327             :         // int bandFileSize = GDALGetDataTypeSizeBytes(bandFileType);
     328             :         // this data type is the complex version of the band file
     329             :         // Roberto: don't check that for the moment: CPLAssert(dataTypeSize ==
     330             :         // bandFileSize * 2);
     331             : 
     332             :         return
     333             :             // I and Q from each band are pixel-interleaved into this complex
     334             :             // band
     335           0 :             poBandFile->RasterIO(
     336           0 :                 GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     337             :                 nRequestXSize, nRequestYSize, pImage, nRequestXSize,
     338             :                 nRequestYSize, bandFileType, 2, nullptr, dataTypeSize,
     339           0 :                 static_cast<GSpacing>(dataTypeSize) * nBlockXSize, bandFileSize,
     340           0 :                 nullptr);
     341             :     }
     342          13 :     else if (twoBandComplex && this->isNITF)
     343             :     {
     344           0 :         return poBand->RasterIO(
     345           0 :             GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     346             :             nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
     347           0 :             eDataType, 0, static_cast<GSpacing>(dataTypeSize) * nBlockXSize,
     348           0 :             nullptr);
     349             :     }
     350             : 
     351          13 :     if (poRCMDataset->IsComplexData())
     352             :     {
     353             :         // this data type is the complex version of the band file
     354             :         // Roberto: don't check that for the moment: CPLAssert(dataTypeSize ==
     355             :         // bandFileSize * 2);
     356             :         return
     357             :             // I and Q from each band are pixel-interleaved into this complex
     358             :             // band
     359           0 :             poBandFile->RasterIO(
     360           0 :                 GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     361             :                 nRequestXSize, nRequestYSize, pImage, nRequestXSize,
     362             :                 nRequestYSize, bandFileType, 2, nullptr, dataTypeSize,
     363           0 :                 static_cast<GSpacing>(dataTypeSize) * nBlockXSize, bandFileSize,
     364           0 :                 nullptr);
     365             :     }
     366             : 
     367             :     // case: band file == this band
     368             :     // NOTE: if the underlying band is opened with the NITF driver, it may
     369             :     // combine 2 band I+Q -> complex band
     370          13 :     else if (poBandFile->GetRasterBand(1)->GetRasterDataType() == eDataType)
     371             :     {
     372          26 :         return poBand->RasterIO(
     373          13 :             GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     374             :             nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
     375          13 :             eDataType, 0, static_cast<GSpacing>(dataTypeSize) * nBlockXSize,
     376          13 :             nullptr);
     377             :     }
     378             :     else
     379             :     {
     380           0 :         CPLAssert(FALSE);
     381             :         return CE_Failure;
     382             :     }
     383             : }
     384             : 
     385             : /************************************************************************/
     386             : /*                            ReadLUT()                                 */
     387             : /************************************************************************/
     388             : /* Read the provided LUT in to m_ndTable                                */
     389             : /* 1. The gains list spans the range extent covered by all              */
     390             : /*    beams(if applicable).                                             */
     391             : /* 2. The mapping between the entry of gains                            */
     392             : /*    list and the range sample index is : the range sample             */
     393             : /*    index = gains entry index * stepSize + pixelFirstLutValue,        */
     394             : /*    where the gains entry index starts with ‘0’.For ScanSAR SLC,      */
     395             : /*    the range sample index refers to the index on the COPG            */
     396             : /************************************************************************/
     397           6 : void RCMCalibRasterBand::ReadLUT()
     398             : {
     399             : 
     400             :     char bandNumber[12];
     401           6 :     snprintf(bandNumber, sizeof(bandNumber), "%d", poDS->GetRasterCount() + 1);
     402             : 
     403           6 :     CPLXMLTreeCloser psLUT(CPLParseXMLFile(m_pszLUTFile));
     404           6 :     if (!psLUT)
     405           0 :         return;
     406             : 
     407           6 :     this->m_nfOffset =
     408           6 :         CPLAtof(CPLGetXMLValue(psLUT.get(), "=lut.offset", "0.0"));
     409             : 
     410           6 :     this->pixelFirstLutValue =
     411           6 :         atoi(CPLGetXMLValue(psLUT.get(), "=lut.pixelFirstLutValue", "0"));
     412             : 
     413           6 :     this->stepSize = atoi(CPLGetXMLValue(psLUT.get(), "=lut.stepSize", "0"));
     414             : 
     415           6 :     this->numberOfValues =
     416           6 :         atoi(CPLGetXMLValue(psLUT.get(), "=lut.numberOfValues", "0"));
     417             : 
     418           6 :     if (this->numberOfValues <= 0)
     419             :     {
     420           0 :         CPLError(
     421             :             CE_Failure, CPLE_NotSupported,
     422             :             "ERROR: The RCM driver does not support the LUT Number Of Values  "
     423             :             "equal or lower than zero.");
     424           0 :         return;
     425             :     }
     426             : 
     427             :     const CPLStringList aosLUTList(
     428           6 :         CSLTokenizeString2(CPLGetXMLValue(psLUT.get(), "=lut.gains", ""), " ",
     429           6 :                            CSLT_HONOURSTRINGS));
     430             : 
     431           6 :     if (this->stepSize <= 0)
     432             :     {
     433           6 :         if (this->pixelFirstLutValue <= 0)
     434             :         {
     435           0 :             CPLError(
     436             :                 CE_Failure, CPLE_NotSupported,
     437             :                 "ERROR: The RCM driver does not support LUT Pixel First Lut "
     438             :                 "Value equal or lower than zero when the product is "
     439             :                 "descending.");
     440           0 :             return;
     441             :         }
     442             :     }
     443             : 
     444             :     /* Get the Pixel Per range */
     445           6 :     if (this->stepSize == 0 || this->stepSize == INT_MIN ||
     446           6 :         this->numberOfValues == INT_MIN ||
     447           6 :         abs(this->stepSize) > INT_MAX / abs(this->numberOfValues))
     448             :     {
     449           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     450             :                  "Bad values of stepSize / numberOfValues");
     451           0 :         return;
     452             :     }
     453             : 
     454           6 :     this->m_nTableSize = abs(this->stepSize) * abs(this->numberOfValues);
     455             : 
     456           6 :     if (this->m_nTableSize < this->m_poBandDataset->GetRasterXSize())
     457             :     {
     458           0 :         CPLError(
     459             :             CE_Failure, CPLE_NotSupported,
     460             :             "ERROR: The RCM driver does not support range of LUT gain values "
     461             :             "lower than the full image pixel range.");
     462           0 :         return;
     463             :     }
     464             : 
     465             :     // Avoid excessive memory allocation
     466           6 :     if (this->m_nTableSize > 1000 * 1000)
     467             :     {
     468           0 :         CPLError(CE_Failure, CPLE_NotSupported, "Too many elements in LUT: %d",
     469             :                  this->m_nTableSize);
     470           0 :         return;
     471             :     }
     472             : 
     473             :     /* Allocate the right LUT size according to the product range pixel */
     474           6 :     this->m_nfTable =
     475           6 :         InterpolateValues(aosLUTList.List(), this->m_nTableSize, this->stepSize,
     476             :                           this->numberOfValues, this->pixelFirstLutValue);
     477           6 :     if (!this->m_nfTable)
     478           0 :         return;
     479             : 
     480             :     // 32 max + space
     481             :     char *lut_gains = static_cast<char *>(
     482           6 :         VSI_CALLOC_VERBOSE(this->m_nTableSize, max_space_for_string));
     483           6 :     if (!lut_gains)
     484           0 :         return;
     485             : 
     486      107496 :     for (int i = 0; i < this->m_nTableSize; i++)
     487             :     {
     488             :         char lut[max_space_for_string];
     489             :         // 6.123004711900930e+04  %e Scientific annotation
     490      107490 :         snprintf(lut, sizeof(lut), "%e ", this->m_nfTable[i]);
     491      107490 :         strcat(lut_gains, lut);
     492             :     }
     493             : 
     494           6 :     poDS->SetMetadataItem(CPLString("LUT_GAINS_").append(bandNumber).c_str(),
     495           6 :                           lut_gains);
     496             :     // Can free this because the function SetMetadataItem takes a copy
     497           6 :     CPLFree(lut_gains);
     498             : 
     499             :     char snum[256];
     500           6 :     if (this->m_eCalib == eCalibration::Sigma0)
     501             :     {
     502           2 :         poDS->SetMetadataItem(CPLString("LUT_TYPE_").append(bandNumber).c_str(),
     503           2 :                               "SIGMA0");
     504             :     }
     505           4 :     else if (this->m_eCalib == eCalibration::Beta0)
     506             :     {
     507           2 :         poDS->SetMetadataItem(CPLString("LUT_TYPE_").append(bandNumber).c_str(),
     508           2 :                               "BETA0");
     509             :     }
     510           2 :     else if (this->m_eCalib == eCalibration::Gamma)
     511             :     {
     512           2 :         poDS->SetMetadataItem(CPLString("LUT_TYPE_").append(bandNumber).c_str(),
     513           2 :                               "GAMMA");
     514             :     }
     515           6 :     snprintf(snum, sizeof(snum), "%d", this->m_nTableSize);
     516           6 :     poDS->SetMetadataItem(CPLString("LUT_SIZE_").append(bandNumber).c_str(),
     517           6 :                           snum);
     518           6 :     snprintf(snum, sizeof(snum), "%f", this->m_nfOffset);
     519           6 :     poDS->SetMetadataItem(CPLString("LUT_OFFSET_").append(bandNumber).c_str(),
     520           6 :                           snum);
     521             : }
     522             : 
     523             : /************************************************************************/
     524             : /*                            ReadNoiseLevels()                         */
     525             : /************************************************************************/
     526             : /* Read the provided LUT in to m_nfTableNoiseLevels                     */
     527             : /* 1. The gains list spans the range extent covered by all              */
     528             : /*    beams(if applicable).                                             */
     529             : /* 2. The mapping between the entry of gains                            */
     530             : /*    list and the range sample index is : the range sample             */
     531             : /*    index = gains entry index * stepSize + pixelFirstLutValue,        */
     532             : /*    where the gains entry index starts with ‘0’.For ScanSAR SLC,      */
     533             : /*    the range sample index refers to the index on the COPG            */
     534             : /************************************************************************/
     535           6 : void RCMCalibRasterBand::ReadNoiseLevels()
     536             : {
     537             : 
     538           6 :     this->m_nfTableNoiseLevels = nullptr;
     539             : 
     540           6 :     if (this->m_pszNoiseLevelsFile == nullptr)
     541             :     {
     542           0 :         return;
     543             :     }
     544             : 
     545             :     char bandNumber[12];
     546           6 :     snprintf(bandNumber, sizeof(bandNumber), "%d", poDS->GetRasterCount() + 1);
     547             : 
     548           6 :     CPLXMLTreeCloser psNoiseLevels(CPLParseXMLFile(this->m_pszNoiseLevelsFile));
     549           6 :     if (!psNoiseLevels)
     550           0 :         return;
     551             : 
     552             :     // Load Beta Nought, Sigma Nought, Gamma noise levels
     553             :     // Loop through all nodes with spaces
     554             :     CPLXMLNode *psReferenceNoiseLevelNode =
     555           6 :         CPLGetXMLNode(psNoiseLevels.get(), "=noiseLevels");
     556           6 :     if (!psReferenceNoiseLevelNode)
     557           0 :         return;
     558             : 
     559             :     CPLXMLNode *psNodeInc;
     560         234 :     for (psNodeInc = psReferenceNoiseLevelNode->psChild; psNodeInc != nullptr;
     561         228 :          psNodeInc = psNodeInc->psNext)
     562             :     {
     563         228 :         if (EQUAL(psNodeInc->pszValue, "referenceNoiseLevel"))
     564             :         {
     565             :             CPLXMLNode *psCalibType =
     566          18 :                 CPLGetXMLNode(psNodeInc, "sarCalibrationType");
     567             :             CPLXMLNode *psPixelFirstNoiseValue =
     568          18 :                 CPLGetXMLNode(psNodeInc, "pixelFirstNoiseValue");
     569          18 :             CPLXMLNode *psStepSize = CPLGetXMLNode(psNodeInc, "stepSize");
     570             :             CPLXMLNode *psNumberOfValues =
     571          18 :                 CPLGetXMLNode(psNodeInc, "numberOfValues");
     572             :             CPLXMLNode *psNoiseLevelValues =
     573          18 :                 CPLGetXMLNode(psNodeInc, "noiseLevelValues");
     574             : 
     575          18 :             if (psCalibType != nullptr && psPixelFirstNoiseValue != nullptr &&
     576          18 :                 psStepSize != nullptr && psNumberOfValues != nullptr &&
     577             :                 psNoiseLevelValues != nullptr)
     578             :             {
     579          18 :                 const char *calibType = CPLGetXMLValue(psCalibType, "", "");
     580          18 :                 this->pixelFirstLutValueNoiseLevels =
     581          18 :                     atoi(CPLGetXMLValue(psPixelFirstNoiseValue, "", "0"));
     582          18 :                 this->stepSizeNoiseLevels =
     583          18 :                     atoi(CPLGetXMLValue(psStepSize, "", "0"));
     584          18 :                 this->numberOfValuesNoiseLevels =
     585          18 :                     atoi(CPLGetXMLValue(psNumberOfValues, "", "0"));
     586             :                 const char *noiseLevelValues =
     587          18 :                     CPLGetXMLValue(psNoiseLevelValues, "", "");
     588          18 :                 if (this->stepSizeNoiseLevels > 0 &&
     589           0 :                     this->numberOfValuesNoiseLevels != INT_MIN &&
     590           0 :                     abs(this->numberOfValuesNoiseLevels) <
     591           0 :                         INT_MAX / this->stepSizeNoiseLevels)
     592             :                 {
     593           0 :                     char **papszNoiseLevelList = CSLTokenizeString2(
     594             :                         noiseLevelValues, " ", CSLT_HONOURSTRINGS);
     595             :                     /* Get the Pixel Per range */
     596           0 :                     this->m_nTableNoiseLevelsSize =
     597           0 :                         abs(this->stepSizeNoiseLevels) *
     598           0 :                         abs(this->numberOfValuesNoiseLevels);
     599             : 
     600           0 :                     if ((EQUAL(calibType, "Beta Nought") &&
     601           0 :                          this->m_eCalib == Beta0) ||
     602           0 :                         (EQUAL(calibType, "Sigma Nought") &&
     603           0 :                          this->m_eCalib == Sigma0) ||
     604           0 :                         (EQUAL(calibType, "Gamma") && this->m_eCalib == Gamma))
     605             :                     {
     606             :                         /* Allocate the right Noise Levels size according to the
     607             :                          * product range pixel */
     608           0 :                         this->m_nfTableNoiseLevels = InterpolateValues(
     609             :                             papszNoiseLevelList, this->m_nTableNoiseLevelsSize,
     610             :                             this->stepSizeNoiseLevels,
     611             :                             this->numberOfValuesNoiseLevels,
     612             :                             this->pixelFirstLutValueNoiseLevels);
     613             :                     }
     614             : 
     615           0 :                     CSLDestroy(papszNoiseLevelList);
     616             :                 }
     617             : 
     618          18 :                 if (this->m_nfTableNoiseLevels != nullptr)
     619             :                 {
     620           0 :                     break;  // We are done
     621             :                 }
     622             :             }
     623             :         }
     624             :     }
     625             : }
     626             : 
     627             : /************************************************************************/
     628             : /*                        RCMCalibRasterBand()                          */
     629             : /************************************************************************/
     630             : 
     631           6 : RCMCalibRasterBand::RCMCalibRasterBand(
     632             :     RCMDataset *poDataset, const char *pszPolarization, GDALDataType eType,
     633             :     GDALDataset *poBandDataset, eCalibration eCalib, const char *pszLUT,
     634           6 :     const char *pszNoiseLevels, GDALDataType eOriginalType)
     635             :     : m_eCalib(eCalib), m_poBandDataset(poBandDataset),
     636          12 :       m_eOriginalType(eOriginalType), m_pszLUTFile(VSIStrdup(pszLUT)),
     637           6 :       m_pszNoiseLevelsFile(VSIStrdup(pszNoiseLevels))
     638             : {
     639           6 :     this->poDS = poDataset;
     640             : 
     641           6 :     if (pszPolarization != nullptr && strlen(pszPolarization) != 0)
     642             :     {
     643           6 :         SetMetadataItem("POLARIMETRIC_INTERP", pszPolarization);
     644             :     }
     645             : 
     646           6 :     if ((eType == GDT_CInt16) || (eType == GDT_CFloat32))
     647           0 :         this->eDataType = GDT_CFloat32;
     648             :     else
     649           6 :         this->eDataType = GDT_Float32;
     650             : 
     651           6 :     GDALRasterBand *poRasterBand = poBandDataset->GetRasterBand(1);
     652           6 :     poRasterBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
     653             : 
     654           6 :     ReadLUT();
     655           6 :     ReadNoiseLevels();
     656           6 : }
     657             : 
     658             : /************************************************************************/
     659             : /*                       ~RCMCalibRasterBand()                          */
     660             : /************************************************************************/
     661             : 
     662          12 : RCMCalibRasterBand::~RCMCalibRasterBand()
     663             : {
     664           6 :     CPLFree(m_nfTable);
     665           6 :     CPLFree(m_nfTableNoiseLevels);
     666           6 :     CPLFree(m_pszLUTFile);
     667           6 :     CPLFree(m_pszNoiseLevelsFile);
     668           6 :     GDALClose(m_poBandDataset);
     669          12 : }
     670             : 
     671             : /************************************************************************/
     672             : /*                        IReadBlock()                                  */
     673             : /************************************************************************/
     674             : 
     675           0 : CPLErr RCMCalibRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
     676             :                                       void *pImage)
     677             : {
     678             :     CPLErr eErr;
     679           0 :     int nRequestXSize = 0;
     680           0 :     int nRequestYSize = 0;
     681           0 :     GetActualBlockSize(nBlockXOff, nBlockYOff, &nRequestXSize, &nRequestYSize);
     682             : 
     683             :     // Zero initial partial right-most and bottom-most blocks
     684           0 :     if (nRequestXSize < nBlockXSize || nRequestYSize < nBlockYSize)
     685             :     {
     686           0 :         memset(pImage, 0,
     687           0 :                static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
     688           0 :                    nBlockXSize * nBlockYSize);
     689             :     }
     690             : 
     691           0 :     if (this->m_eOriginalType == GDT_CInt16)
     692             :     {
     693             :         /* read in complex values */
     694             :         GInt16 *panImageTmp = static_cast<GInt16 *>(
     695           0 :             VSI_MALLOC3_VERBOSE(nBlockXSize, nBlockYSize,
     696             :                                 GDALGetDataTypeSizeBytes(m_eOriginalType)));
     697           0 :         if (!panImageTmp)
     698           0 :             return CE_Failure;
     699             : 
     700           0 :         if (m_poBandDataset->GetRasterCount() == 2)
     701             :         {
     702           0 :             eErr = m_poBandDataset->RasterIO(
     703           0 :                 GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     704             :                 nRequestXSize, nRequestYSize, panImageTmp, nRequestXSize,
     705             :                 nRequestYSize, this->m_eOriginalType, 2, nullptr, 4,
     706           0 :                 nBlockXSize * 4, 4, nullptr);
     707             : 
     708             :             /*
     709             :             eErr = m_poBandDataset->RasterIO(GF_Read,
     710             :                 nBlockXOff * nBlockXSize,
     711             :                 nBlockYOff * nBlockYSize,
     712             :                 nRequestXSize, nRequestYSize,
     713             :                 panImageTmp, nRequestXSize, nRequestYSize,
     714             :                 GDT_Int32,
     715             :                 2, nullptr, 4, nBlockXSize * 4, 2, nullptr);
     716             :             */
     717             :         }
     718             :         else
     719             :         {
     720           0 :             eErr = m_poBandDataset->RasterIO(
     721           0 :                 GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     722             :                 nRequestXSize, nRequestYSize, panImageTmp, nRequestXSize,
     723             :                 nRequestYSize, this->m_eOriginalType, 1, nullptr, 4,
     724           0 :                 nBlockXSize * 4, 0, nullptr);
     725             : 
     726             :             /*
     727             :             eErr =
     728             :                 m_poBandDataset->RasterIO(GF_Read,
     729             :                     nBlockXOff * nBlockXSize,
     730             :                     nBlockYOff * nBlockYSize,
     731             :                     nRequestXSize, nRequestYSize,
     732             :                     panImageTmp, nRequestXSize, nRequestYSize,
     733             :                     GDT_UInt32,
     734             :                     1, nullptr, 4, nBlockXSize * 4, 0, nullptr);
     735             :             */
     736             : 
     737             : #ifdef CPL_LSB
     738             :             /* First, undo the 32bit swap. */
     739           0 :             GDALSwapWords(pImage, 4, nBlockXSize * nBlockYSize, 4);
     740             : 
     741             :             /* Then apply 16 bit swap. */
     742           0 :             GDALSwapWords(pImage, 2, nBlockXSize * nBlockYSize * 2, 2);
     743             : #endif
     744             :         }
     745             : 
     746             :         /* calibrate the complex values */
     747           0 :         for (int i = 0; i < nRequestYSize; i++)
     748             :         {
     749           0 :             for (int j = 0; j < nRequestXSize; j++)
     750             :             {
     751             :                 /* calculate pixel offset in memory*/
     752           0 :                 const int nPixOff = 2 * (i * nBlockXSize + j);
     753           0 :                 const int nTruePixOff = (i * nBlockXSize) + j;
     754             : 
     755             :                 // Formula for Complex Q+J
     756           0 :                 const float real = static_cast<float>(panImageTmp[nPixOff]);
     757           0 :                 const float img = static_cast<float>(panImageTmp[nPixOff + 1]);
     758           0 :                 const float digitalValue = (real * real) + (img * img);
     759           0 :                 const float lutValue =
     760           0 :                     static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
     761           0 :                 const float calibValue = digitalValue / (lutValue * lutValue);
     762             : 
     763           0 :                 reinterpret_cast<float *>(pImage)[nTruePixOff] = calibValue;
     764             :             }
     765             :         }
     766             : 
     767           0 :         CPLFree(panImageTmp);
     768             :     }
     769             : 
     770             :     // If the underlying file is NITF CFloat32
     771           0 :     else if (this->m_eOriginalType == GDT_CFloat32 ||
     772           0 :              this->m_eOriginalType == GDT_CFloat64)
     773             :     {
     774             :         /* read in complex values */
     775             :         const int dataTypeSize =
     776           0 :             GDALGetDataTypeSizeBytes(this->m_eOriginalType);
     777           0 :         const GDALDataType bandFileType = this->m_eOriginalType;
     778           0 :         const int bandFileDataTypeSize = GDALGetDataTypeSizeBytes(bandFileType);
     779             : 
     780             :         /* read the original image complex values in a temporary image space */
     781           0 :         float *pafImageTmp = static_cast<float *>(VSI_MALLOC3_VERBOSE(
     782             :             nBlockXSize, nBlockYSize, 2 * bandFileDataTypeSize));
     783           0 :         if (!pafImageTmp)
     784           0 :             return CE_Failure;
     785             : 
     786             :         eErr =
     787             :             // I and Q from each band are pixel-interleaved into this complex
     788             :             // band
     789           0 :             m_poBandDataset->RasterIO(
     790           0 :                 GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     791             :                 nRequestXSize, nRequestYSize, pafImageTmp, nRequestXSize,
     792             :                 nRequestYSize, bandFileType, 2, nullptr, dataTypeSize,
     793           0 :                 static_cast<GSpacing>(dataTypeSize) * nBlockXSize,
     794             :                 bandFileDataTypeSize, nullptr);
     795             : 
     796             :         /* calibrate the complex values */
     797           0 :         for (int i = 0; i < nRequestYSize; i++)
     798             :         {
     799           0 :             for (int j = 0; j < nRequestXSize; j++)
     800             :             {
     801             :                 /* calculate pixel offset in memory*/
     802           0 :                 const int nPixOff = 2 * (i * nBlockXSize + j);
     803           0 :                 const int nTruePixOff = (i * nBlockXSize) + j;
     804             : 
     805             :                 // Formula for Complex Q+J
     806           0 :                 const float real = pafImageTmp[nPixOff];
     807           0 :                 const float img = pafImageTmp[nPixOff + 1];
     808           0 :                 const float digitalValue = (real * real) + (img * img);
     809           0 :                 const float lutValue =
     810           0 :                     static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
     811           0 :                 const float calibValue = digitalValue / (lutValue * lutValue);
     812             : 
     813           0 :                 reinterpret_cast<float *>(pImage)[nTruePixOff] = calibValue;
     814             :             }
     815             :         }
     816             : 
     817           0 :         CPLFree(pafImageTmp);
     818             :     }
     819             : 
     820           0 :     else if (this->m_eOriginalType == GDT_Float32)
     821             :     {
     822             :         /* A Float32 is actual 4 bytes */
     823           0 :         eErr = m_poBandDataset->RasterIO(
     824           0 :             GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     825             :             nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
     826           0 :             GDT_Float32, 1, nullptr, 4, nBlockXSize * 4, 0, nullptr);
     827             : 
     828             :         /* iterate over detected values */
     829           0 :         for (int i = 0; i < nRequestYSize; i++)
     830             :         {
     831           0 :             for (int j = 0; j < nRequestXSize; j++)
     832             :             {
     833           0 :                 int nPixOff = (i * nBlockXSize) + j;
     834             : 
     835             :                 /* For detected products, in order to convert the digital number
     836             :                 of a given range sample to a calibrated value, the digital value
     837             :                 is first squared, then the offset(B) is added and the result is
     838             :                 divided by the gains value(A) corresponding to the range sample.
     839             :                 RCM-SP-53-0419  Issue 2/5:  January 2, 2018  Page 7-56 */
     840           0 :                 const float digitalValue =
     841           0 :                     reinterpret_cast<float *>(pImage)[nPixOff];
     842           0 :                 const float A =
     843           0 :                     static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
     844           0 :                 reinterpret_cast<float *>(pImage)[nPixOff] =
     845           0 :                     ((digitalValue * digitalValue) +
     846           0 :                      static_cast<float>(this->m_nfOffset)) /
     847             :                     A;
     848             :             }
     849             :         }
     850             :     }
     851             : 
     852           0 :     else if (this->m_eOriginalType == GDT_Float64)
     853             :     {
     854             :         /* A Float64 is actual 8 bytes */
     855           0 :         eErr = m_poBandDataset->RasterIO(
     856           0 :             GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     857             :             nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
     858           0 :             GDT_Float64, 1, nullptr, 8, nBlockXSize * 8, 0, nullptr);
     859             : 
     860             :         /* iterate over detected values */
     861           0 :         for (int i = 0; i < nRequestYSize; i++)
     862             :         {
     863           0 :             for (int j = 0; j < nRequestXSize; j++)
     864             :             {
     865           0 :                 int nPixOff = (i * nBlockXSize) + j;
     866             : 
     867             :                 /* For detected products, in order to convert the digital number
     868             :                 of a given range sample to a calibrated value, the digital value
     869             :                 is first squared, then the offset(B) is added and the result is
     870             :                 divided by the gains value(A) corresponding to the range sample.
     871             :                 RCM-SP-53-0419  Issue 2/5:  January 2, 2018  Page 7-56 */
     872           0 :                 const float digitalValue =
     873           0 :                     reinterpret_cast<float *>(pImage)[nPixOff];
     874           0 :                 const float A =
     875           0 :                     static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
     876           0 :                 reinterpret_cast<float *>(pImage)[nPixOff] =
     877           0 :                     ((digitalValue * digitalValue) +
     878           0 :                      static_cast<float>(this->m_nfOffset)) /
     879             :                     A;
     880             :             }
     881             :         }
     882             :     }
     883             : 
     884           0 :     else if (this->m_eOriginalType == GDT_UInt16)
     885             :     {
     886             :         /* read in detected values */
     887           0 :         GUInt16 *panImageTmp = static_cast<GUInt16 *>(VSI_MALLOC3_VERBOSE(
     888             :             nBlockXSize, nBlockYSize, GDALGetDataTypeSizeBytes(GDT_UInt16)));
     889           0 :         if (!panImageTmp)
     890           0 :             return CE_Failure;
     891           0 :         eErr = m_poBandDataset->RasterIO(
     892           0 :             GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     893             :             nRequestXSize, nRequestYSize, panImageTmp, nRequestXSize,
     894           0 :             nRequestYSize, GDT_UInt16, 1, nullptr, 2, nBlockXSize * 2, 0,
     895             :             nullptr);
     896             : 
     897             :         /* iterate over detected values */
     898           0 :         for (int i = 0; i < nRequestYSize; i++)
     899             :         {
     900           0 :             for (int j = 0; j < nRequestXSize; j++)
     901             :             {
     902           0 :                 const int nPixOff = (i * nBlockXSize) + j;
     903             : 
     904           0 :                 const float digitalValue =
     905           0 :                     static_cast<float>(panImageTmp[nPixOff]);
     906           0 :                 const float A =
     907           0 :                     static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
     908           0 :                 reinterpret_cast<float *>(pImage)[nPixOff] =
     909           0 :                     ((digitalValue * digitalValue) +
     910           0 :                      static_cast<float>(this->m_nfOffset)) /
     911             :                     A;
     912             :             }
     913             :         }
     914           0 :         CPLFree(panImageTmp);
     915             :     } /* Ticket #2104: Support for ScanSAR products */
     916             : 
     917           0 :     else if (this->m_eOriginalType == GDT_Byte)
     918             :     {
     919             :         GByte *pabyImageTmp =
     920           0 :             static_cast<GByte *>(VSI_MALLOC2_VERBOSE(nBlockXSize, nBlockYSize));
     921           0 :         if (!pabyImageTmp)
     922           0 :             return CE_Failure;
     923           0 :         eErr = m_poBandDataset->RasterIO(
     924           0 :             GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
     925             :             nRequestXSize, nRequestYSize, pabyImageTmp, nRequestXSize,
     926           0 :             nRequestYSize, GDT_Byte, 1, nullptr, 1, nBlockXSize, 0, nullptr);
     927             : 
     928             :         /* iterate over detected values */
     929           0 :         for (int i = 0; i < nRequestYSize; i++)
     930             :         {
     931           0 :             for (int j = 0; j < nRequestXSize; j++)
     932             :             {
     933           0 :                 const int nPixOff = (i * nBlockXSize) + j;
     934             : 
     935           0 :                 const float digitalValue =
     936           0 :                     static_cast<float>(pabyImageTmp[nPixOff]);
     937           0 :                 const float A =
     938           0 :                     static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
     939           0 :                 reinterpret_cast<float *>(pImage)[nPixOff] =
     940           0 :                     ((digitalValue * digitalValue) +
     941           0 :                      static_cast<float>(this->m_nfOffset)) /
     942             :                     A;
     943             :             }
     944             :         }
     945           0 :         CPLFree(pabyImageTmp);
     946             :     }
     947             :     else
     948             :     {
     949           0 :         CPLAssert(FALSE);
     950             :         return CE_Failure;
     951             :     }
     952           0 :     return eErr;
     953             : }
     954             : 
     955             : /************************************************************************/
     956             : /* ==================================================================== */
     957             : /*                              RCMDataset                              */
     958             : /* ==================================================================== */
     959             : /************************************************************************/
     960             : 
     961             : /************************************************************************/
     962             : /*                            ~RCMDataset()                             */
     963             : /************************************************************************/
     964             : 
     965          16 : RCMDataset::~RCMDataset()
     966             : 
     967             : {
     968           8 :     RCMDataset::FlushCache(true);
     969             : 
     970           8 :     if (nGCPCount > 0)
     971             :     {
     972           8 :         GDALDeinitGCPs(nGCPCount, pasGCPList);
     973           8 :         CPLFree(pasGCPList);
     974             :     }
     975             : 
     976           8 :     RCMDataset::CloseDependentDatasets();
     977             : 
     978           8 :     if (papszSubDatasets != nullptr)
     979           4 :         CSLDestroy(papszSubDatasets);
     980             : 
     981           8 :     if (papszExtraFiles != nullptr)
     982           8 :         CSLDestroy(papszExtraFiles);
     983             : 
     984           8 :     if (m_nfIncidenceAngleTable != nullptr)
     985           8 :         CPLFree(m_nfIncidenceAngleTable);
     986          16 : }
     987             : 
     988             : /************************************************************************/
     989             : /*                      CloseDependentDatasets()                        */
     990             : /************************************************************************/
     991             : 
     992           8 : int RCMDataset::CloseDependentDatasets()
     993             : {
     994           8 :     int bHasDroppedRef = GDALPamDataset::CloseDependentDatasets();
     995             : 
     996           8 :     if (nBands != 0)
     997           8 :         bHasDroppedRef = TRUE;
     998             : 
     999          24 :     for (int iBand = 0; iBand < nBands; iBand++)
    1000             :     {
    1001          16 :         delete papoBands[iBand];
    1002             :     }
    1003           8 :     nBands = 0;
    1004             : 
    1005           8 :     return bHasDroppedRef;
    1006             : }
    1007             : 
    1008             : /************************************************************************/
    1009             : /*                            GetFileList()                             */
    1010             : /************************************************************************/
    1011             : 
    1012           0 : char **RCMDataset::GetFileList()
    1013             : 
    1014             : {
    1015           0 :     char **papszFileList = GDALPamDataset::GetFileList();
    1016             : 
    1017           0 :     papszFileList = CSLInsertStrings(papszFileList, -1, papszExtraFiles);
    1018             : 
    1019           0 :     return papszFileList;
    1020             : }
    1021             : 
    1022             : /************************************************************************/
    1023             : /*                                Open()                                */
    1024             : /************************************************************************/
    1025             : 
    1026          10 : GDALDataset *RCMDataset::Open(GDALOpenInfo *poOpenInfo)
    1027             : 
    1028             : {
    1029             :     /* -------------------------------------------------------------------- */
    1030             :     /*      Is this a RCM Product.xml definition?                           */
    1031             :     /* -------------------------------------------------------------------- */
    1032          10 :     if (!RCMDatasetIdentify(poOpenInfo))
    1033             :     {
    1034           0 :         return nullptr;
    1035             :     }
    1036             : 
    1037             :     /* -------------------------------------------------------------------- */
    1038             :     /*        Get subdataset information, if relevant                       */
    1039             :     /* -------------------------------------------------------------------- */
    1040          10 :     const char *pszFilename = poOpenInfo->pszFilename;
    1041          10 :     eCalibration eCalib = None;
    1042             : 
    1043          10 :     if (STARTS_WITH_CI(pszFilename, szLayerCalibration) &&
    1044           6 :         pszFilename[strlen(szLayerCalibration)] == chLayerSeparator)
    1045             :     {
    1046             :         // The calibration name and filename begins after the hard coded layer
    1047             :         // name
    1048           6 :         pszFilename += strlen(szLayerCalibration) + 1;
    1049             : 
    1050           6 :         if (STARTS_WITH_CI(pszFilename, szBETA0))
    1051             :         {
    1052           1 :             eCalib = Beta0;
    1053             :         }
    1054           5 :         else if (STARTS_WITH_CI(pszFilename, szSIGMA0))
    1055             :         {
    1056           1 :             eCalib = Sigma0;
    1057             :         }
    1058           4 :         else if (STARTS_WITH_CI(pszFilename, szGAMMA) ||
    1059           3 :                  STARTS_WITH_CI(pszFilename, "GAMMA0"))  // Cover both situation
    1060             :         {
    1061           1 :             eCalib = Gamma;
    1062             :         }
    1063           3 :         else if (STARTS_WITH_CI(pszFilename, szUNCALIB))
    1064             :         {
    1065           2 :             eCalib = Uncalib;
    1066             :         }
    1067             :         else
    1068             :         {
    1069           1 :             CPLError(CE_Failure, CPLE_NotSupported,
    1070             :                      "Unsupported calibration type");
    1071           1 :             return nullptr;
    1072             :         }
    1073             : 
    1074             :         /* advance the pointer to the actual filename */
    1075          35 :         while (*pszFilename != '\0' && *pszFilename != ':')
    1076          30 :             pszFilename++;
    1077             : 
    1078           5 :         if (*pszFilename == ':')
    1079           5 :             pszFilename++;
    1080             : 
    1081             :         // need to redo the directory check:
    1082             :         // the GDALOpenInfo check would have failed because of the calibration
    1083             :         // string on the filename
    1084             :         VSIStatBufL sStat;
    1085           5 :         if (VSIStatL(pszFilename, &sStat) == 0)
    1086           4 :             poOpenInfo->bIsDirectory = VSI_ISDIR(sStat.st_mode);
    1087             :     }
    1088             : 
    1089          18 :     CPLString osMDFilename;
    1090           9 :     if (poOpenInfo->bIsDirectory)
    1091             :     {
    1092             :         /* Check for directory access when there is a product.xml file in the
    1093             :         directory. */
    1094             :         osMDFilename =
    1095           2 :             CPLFormCIFilenameSafe(pszFilename, "product.xml", nullptr);
    1096             : 
    1097             :         VSIStatBufL sStat;
    1098           2 :         if (VSIStatL(osMDFilename, &sStat) != 0)
    1099             :         {
    1100             :             /* If not, check for directory extra 'metadata' access when there is
    1101             :             a product.xml file in the directory. */
    1102           1 :             osMDFilename = CPLFormCIFilenameSafe(pszFilename,
    1103           2 :                                                  GetMetadataProduct(), nullptr);
    1104             :         }
    1105             :     }
    1106             :     else
    1107           7 :         osMDFilename = pszFilename;
    1108             : 
    1109             :     /* -------------------------------------------------------------------- */
    1110             :     /*      Ingest the Product.xml file.                                    */
    1111             :     /* -------------------------------------------------------------------- */
    1112          18 :     CPLXMLTreeCloser psProduct(CPLParseXMLFile(osMDFilename));
    1113           9 :     if (!psProduct)
    1114           1 :         return nullptr;
    1115             : 
    1116             :     /* -------------------------------------------------------------------- */
    1117             :     /*      Confirm the requested access is supported.                      */
    1118             :     /* -------------------------------------------------------------------- */
    1119           8 :     if (poOpenInfo->eAccess == GA_Update)
    1120             :     {
    1121           0 :         ReportUpdateNotSupportedByDriver("RCM");
    1122           0 :         return nullptr;
    1123             :     }
    1124             : 
    1125             :     CPLXMLNode *psSceneAttributes =
    1126           8 :         CPLGetXMLNode(psProduct.get(), "=product.sceneAttributes");
    1127           8 :     if (psSceneAttributes == nullptr)
    1128             :     {
    1129           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
    1130             :                  "ERROR: Failed to find <sceneAttributes> in document.");
    1131           0 :         return nullptr;
    1132             :     }
    1133             : 
    1134           8 :     CPLXMLNode *psImageAttributes = CPLGetXMLNode(
    1135             :         psProduct.get(), "=product.sceneAttributes.imageAttributes");
    1136           8 :     if (psImageAttributes == nullptr)
    1137             :     {
    1138           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
    1139             :                  "ERROR: Failed to find <sceneAttributes.imageAttributes> in "
    1140             :                  "document.");
    1141           0 :         return nullptr;
    1142             :     }
    1143             : 
    1144             :     int numberOfEntries =
    1145           8 :         atoi(CPLGetXMLValue(psSceneAttributes, "numberOfEntries", "0"));
    1146           8 :     if (numberOfEntries != 1)
    1147             :     {
    1148           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
    1149             :                  "ERROR: Only RCM with Complex Single-beam is supported.");
    1150           0 :         return nullptr;
    1151             :     }
    1152             : 
    1153             :     CPLXMLNode *psImageReferenceAttributes =
    1154           8 :         CPLGetXMLNode(psProduct.get(), "=product.imageReferenceAttributes");
    1155           8 :     if (psImageReferenceAttributes == nullptr)
    1156             :     {
    1157           0 :         CPLError(
    1158             :             CE_Failure, CPLE_OpenFailed,
    1159             :             "ERROR: Failed to find <imageReferenceAttributes> in document.");
    1160           0 :         return nullptr;
    1161             :     }
    1162             : 
    1163             :     CPLXMLNode *psImageGenerationParameters =
    1164           8 :         CPLGetXMLNode(psProduct.get(), "=product.imageGenerationParameters");
    1165           8 :     if (psImageGenerationParameters == nullptr)
    1166             :     {
    1167           0 :         CPLError(
    1168             :             CE_Failure, CPLE_OpenFailed,
    1169             :             "ERROR: Failed to find <imageGenerationParameters> in document.");
    1170           0 :         return nullptr;
    1171             :     }
    1172             : 
    1173             :     /* -------------------------------------------------------------------- */
    1174             :     /*      Create the dataset.                                             */
    1175             :     /* -------------------------------------------------------------------- */
    1176          16 :     auto poDS = std::make_unique<RCMDataset>();
    1177             : 
    1178           8 :     poDS->psProduct = std::move(psProduct);
    1179             : 
    1180             :     /* -------------------------------------------------------------------- */
    1181             :     /*      Get overall image information.                                  */
    1182             :     /* -------------------------------------------------------------------- */
    1183           8 :     poDS->nRasterXSize = atoi(CPLGetXMLValue(
    1184             :         psSceneAttributes, "imageAttributes.samplesPerLine", "-1"));
    1185           8 :     poDS->nRasterYSize = atoi(
    1186             :         CPLGetXMLValue(psSceneAttributes, "imageAttributes.numLines", "-1"));
    1187           8 :     if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize))
    1188             :     {
    1189           0 :         return nullptr;
    1190             :     }
    1191             : 
    1192             :     /* -------------------------------------------------------------------- */
    1193             :     /*      Check product type, as to determine if there are LUTs for       */
    1194             :     /*      calibration purposes.                                           */
    1195             :     /* -------------------------------------------------------------------- */
    1196             : 
    1197             :     const char *pszItem =
    1198           8 :         CPLGetXMLValue(psImageGenerationParameters,
    1199             :                        "generalProcessingInformation.productType", "UNK");
    1200           8 :     poDS->SetMetadataItem("PRODUCT_TYPE", pszItem);
    1201           8 :     const char *pszProductType = pszItem;
    1202             : 
    1203             :     pszItem =
    1204           8 :         CPLGetXMLValue(poDS->psProduct.get(), "=product.productId", "UNK");
    1205           8 :     poDS->SetMetadataItem("PRODUCT_ID", pszItem);
    1206             : 
    1207           8 :     pszItem = CPLGetXMLValue(
    1208           8 :         poDS->psProduct.get(),
    1209             :         "=product.securityAttributes.securityClassification", "UNK");
    1210           8 :     poDS->SetMetadataItem("SECURITY_CLASSIFICATION", pszItem);
    1211             : 
    1212             :     pszItem =
    1213           8 :         CPLGetXMLValue(poDS->psProduct.get(),
    1214             :                        "=product.sourceAttributes.polarizationDataMode", "UNK");
    1215           8 :     poDS->SetMetadataItem("POLARIZATION_DATA_MODE", pszItem);
    1216             : 
    1217           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1218             :                              "generalProcessingInformation.processingFacility",
    1219             :                              "UNK");
    1220           8 :     poDS->SetMetadataItem("PROCESSING_FACILITY", pszItem);
    1221             : 
    1222             :     pszItem =
    1223           8 :         CPLGetXMLValue(psImageGenerationParameters,
    1224             :                        "generalProcessingInformation.processingTime", "UNK");
    1225           8 :     poDS->SetMetadataItem("PROCESSING_TIME", pszItem);
    1226             : 
    1227           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1228             :                              "sarProcessingInformation.satelliteHeight", "UNK");
    1229           8 :     poDS->SetMetadataItem("SATELLITE_HEIGHT", pszItem);
    1230             : 
    1231           8 :     pszItem = CPLGetXMLValue(
    1232             :         psImageGenerationParameters,
    1233             :         "sarProcessingInformation.zeroDopplerTimeFirstLine", "UNK");
    1234           8 :     poDS->SetMetadataItem("FIRST_LINE_TIME", pszItem);
    1235             : 
    1236           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1237             :                              "sarProcessingInformation.zeroDopplerTimeLastLine",
    1238             :                              "UNK");
    1239           8 :     poDS->SetMetadataItem("LAST_LINE_TIME", pszItem);
    1240             : 
    1241           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1242             :                              "sarProcessingInformation.lutApplied", "");
    1243           8 :     poDS->SetMetadataItem("LUT_APPLIED", pszItem);
    1244             : 
    1245             :     /*---------------------------------------------------------------------
    1246             :            If true, a polarization dependent application LUT has been applied
    1247             :            for each polarization channel. Otherwise the same application LUT
    1248             :            has been applied for all polarization channels. Not applicable to
    1249             :            lookupTable = "Unity*" or if dataType = "Floating-Point".
    1250             :       --------------------------------------------------------------------- */
    1251           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1252             :                              "sarProcessingInformation.perPolarizationScaling",
    1253             :                              "false");
    1254           8 :     poDS->SetMetadataItem("PER_POLARIZATION_SCALING", pszItem);
    1255           8 :     if (EQUAL(pszItem, "true") || EQUAL(pszItem, "TRUE"))
    1256             :     {
    1257           8 :         poDS->bPerPolarizationScaling = TRUE;
    1258             :     }
    1259             : 
    1260             :     /* the following cases can be assumed to have no LUTs, as per
    1261             :      * RN-RP-51-2713, but also common sense
    1262             :      * SLC represents a SLant range georeferenced Complex product
    1263             :      * (i.e., equivalent to a Single-Look Complex product for RADARSAT-1 or
    1264             :      * RADARSAT-2). • GRD or GRC represent GRound range georeferenced Detected
    1265             :      * or Complex products (GRD is equivalent to an SGX, SCN or SCW product for
    1266             :      * RADARSAT1 or RADARSAT-2). • GCD or GCC represent GeoCoded Detected or
    1267             :      * Complex products (GCD is equivalent to an SSG or SPG product for
    1268             :      * RADARSAT-1 or RADARSAT-2).
    1269             :      */
    1270           8 :     bool bCanCalib = false;
    1271           8 :     if (!(STARTS_WITH_CI(pszProductType, "UNK") ||
    1272           8 :           STARTS_WITH_CI(pszProductType, "GCD") ||
    1273           8 :           STARTS_WITH_CI(pszProductType, "GCC")))
    1274             :     {
    1275           8 :         bCanCalib = true;
    1276             :     }
    1277             : 
    1278             :     /* -------------------------------------------------------------------- */
    1279             :     /*      Get dataType (so we can recognise complex data), and the        */
    1280             :     /*      bitsPerSample.                                                  */
    1281             :     /* -------------------------------------------------------------------- */
    1282           8 :     const char *pszSampleDataType = CPLGetXMLValue(
    1283             :         psImageReferenceAttributes, "rasterAttributes.sampleType", "");
    1284           8 :     poDS->SetMetadataItem("SAMPLE_TYPE", pszSampleDataType);
    1285             : 
    1286             :     /* Either Integer (16 bits) or Floating-Point (32 bits) */
    1287           8 :     const char *pszDataType = CPLGetXMLValue(psImageReferenceAttributes,
    1288             :                                              "rasterAttributes.dataType", "");
    1289           8 :     poDS->SetMetadataItem("DATA_TYPE", pszDataType);
    1290             : 
    1291             :     /* 2 entries for complex data 1 entry for magnitude detected data */
    1292           8 :     const char *pszBitsPerSample = CPLGetXMLValue(
    1293             :         psImageReferenceAttributes, "rasterAttributes.bitsPerSample", "");
    1294           8 :     const int nBitsPerSample = atoi(pszBitsPerSample);
    1295           8 :     poDS->SetMetadataItem("BITS_PER_SAMPLE", pszBitsPerSample);
    1296             : 
    1297             :     const char *pszSampledPixelSpacingTime =
    1298           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1299             :                        "rasterAttributes.sampledPixelSpacingTime", "UNK");
    1300           8 :     poDS->SetMetadataItem("SAMPLED_PIXEL_SPACING_TIME",
    1301           8 :                           pszSampledPixelSpacingTime);
    1302             : 
    1303             :     const char *pszSampledLineSpacingTime =
    1304           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1305             :                        "rasterAttributes.sampledLineSpacingTime", "UNK");
    1306           8 :     poDS->SetMetadataItem("SAMPLED_LINE_SPACING_TIME",
    1307           8 :                           pszSampledLineSpacingTime);
    1308             : 
    1309             :     GDALDataType eDataType;
    1310             : 
    1311           8 :     if (EQUAL(pszSampleDataType, "Mixed")) /* RCM MLC has Mixed sampleType */
    1312             :     {
    1313           1 :         poDS->isComplexData = false; /* RCM MLC is detected, non-complex */
    1314           1 :         if (nBitsPerSample == 32)
    1315             :         {
    1316           1 :             eDataType = GDT_Float32; /* 32 bits, check read block */
    1317           1 :             poDS->magnitudeBits = 32;
    1318             :         }
    1319             :         else
    1320             :         {
    1321           0 :             eDataType = GDT_UInt16; /* 16 bits, check read block */
    1322           0 :             poDS->magnitudeBits = 16;
    1323             :         }
    1324             :     }
    1325           7 :     else if (EQUAL(pszSampleDataType, "Complex"))
    1326             :     {
    1327           0 :         poDS->isComplexData = true;
    1328             :         /* Usually this is the same bits for both */
    1329           0 :         poDS->realBitsComplexData = nBitsPerSample;
    1330           0 :         poDS->imaginaryBitsComplexData = nBitsPerSample;
    1331             : 
    1332           0 :         if (nBitsPerSample == 32)
    1333             :         {
    1334           0 :             eDataType = GDT_CFloat32; /* 32 bits, check read block */
    1335             :         }
    1336             :         else
    1337             :         {
    1338           0 :             eDataType = GDT_CInt16; /* 16 bits, check read block */
    1339             :         }
    1340             :     }
    1341           7 :     else if (nBitsPerSample == 32 &&
    1342           0 :              EQUAL(pszSampleDataType, "Magnitude Detected"))
    1343             :     {
    1344             :         /* Actually, I don't need to test the 'dataType'=' Floating-Point', we
    1345             :          * know that a 32 bits per sample */
    1346           0 :         eDataType = GDT_Float32;
    1347           0 :         poDS->isComplexData = false;
    1348           0 :         poDS->magnitudeBits = 32;
    1349             :     }
    1350           7 :     else if (nBitsPerSample == 16 &&
    1351           7 :              EQUAL(pszSampleDataType, "Magnitude Detected"))
    1352             :     {
    1353             :         /* Actually, I don't need to test the 'dataType'=' Integer', we know
    1354             :          * that a 16 bits per sample */
    1355           7 :         eDataType = GDT_UInt16;
    1356           7 :         poDS->isComplexData = false;
    1357           7 :         poDS->magnitudeBits = 16;
    1358             :     }
    1359             :     else
    1360             :     {
    1361           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1362             :                  "ERROR: dataType=%s and bitsPerSample=%d are not a supported "
    1363             :                  "configuration.",
    1364             :                  pszDataType, nBitsPerSample);
    1365           0 :         return nullptr;
    1366             :     }
    1367             : 
    1368             :     /* Indicates whether pixel number (i.e., range) increases or decreases with
    1369             :     range time. For GCD and GCC products, this applies to intermediate ground
    1370             :     range image data prior to geocoding. */
    1371             :     const char *pszPixelTimeOrdering =
    1372           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1373             :                        "rasterAttributes.pixelTimeOrdering", "UNK");
    1374           8 :     poDS->SetMetadataItem("PIXEL_TIME_ORDERING", pszPixelTimeOrdering);
    1375             : 
    1376             :     /* Indicates whether line numbers (i.e., azimuth) increase or decrease with
    1377             :     azimuth time. For GCD and GCC products, this applies to intermediate ground
    1378             :     range image data prior to geocoding. */
    1379           8 :     const char *pszLineTimeOrdering = CPLGetXMLValue(
    1380             :         psImageReferenceAttributes, "rasterAttributes.lineTimeOrdering", "UNK");
    1381           8 :     poDS->SetMetadataItem("LINE_TIME_ORDERING", pszLineTimeOrdering);
    1382             : 
    1383             :     /* while we're at it, extract the pixel spacing information */
    1384             :     const char *pszPixelSpacing =
    1385           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1386             :                        "rasterAttributes.sampledPixelSpacing", "UNK");
    1387           8 :     poDS->SetMetadataItem("PIXEL_SPACING", pszPixelSpacing);
    1388             : 
    1389             :     const char *pszLineSpacing =
    1390           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1391             :                        "rasterAttributes.sampledLineSpacing", "UNK");
    1392           8 :     poDS->SetMetadataItem("LINE_SPACING", pszLineSpacing);
    1393             : 
    1394             :     /* -------------------------------------------------------------------- */
    1395             :     /*      Open each of the data files as a complex band.                  */
    1396             :     /* -------------------------------------------------------------------- */
    1397           8 :     char *pszBeta0LUT = nullptr;
    1398           8 :     char *pszGammaLUT = nullptr;
    1399           8 :     char *pszSigma0LUT = nullptr;
    1400             :     // Same file for all calibrations except the calibration is plit inside the
    1401             :     // XML
    1402          16 :     std::string osNoiseLevelsValues;
    1403             : 
    1404          16 :     const CPLString osPath = CPLGetPathSafe(osMDFilename);
    1405             : 
    1406             :     /* Get a list of all polarizations */
    1407             :     CPLXMLNode *psSourceAttrs =
    1408           8 :         CPLGetXMLNode(poDS->psProduct.get(), "=product.sourceAttributes");
    1409           8 :     if (psSourceAttrs == nullptr)
    1410             :     {
    1411           0 :         CPLError(
    1412             :             CE_Failure, CPLE_OpenFailed,
    1413             :             "ERROR: RCM source attributes is missing. Please contact your data "
    1414             :             "provider for a corrected dataset.");
    1415           0 :         return nullptr;
    1416             :     }
    1417             : 
    1418           8 :     CPLXMLNode *psRadarParameters = CPLGetXMLNode(
    1419           8 :         poDS->psProduct.get(), "=product.sourceAttributes.radarParameters");
    1420           8 :     if (psRadarParameters == nullptr)
    1421             :     {
    1422           0 :         CPLError(
    1423             :             CE_Failure, CPLE_OpenFailed,
    1424             :             "ERROR: RCM radar parameters is missing. Please contact your data "
    1425             :             "provider for a corrected dataset.");
    1426           0 :         return nullptr;
    1427             :     }
    1428             : 
    1429             :     const char *pszPolarizations =
    1430           8 :         CPLGetXMLValue(psRadarParameters, "polarizations", "");
    1431           8 :     if (pszPolarizations == nullptr || strlen(pszPolarizations) == 0)
    1432             :     {
    1433           0 :         CPLError(
    1434             :             CE_Failure, CPLE_OpenFailed,
    1435             :             "ERROR: RCM polarizations list is missing. Please contact your "
    1436             :             "data provider for a corrected dataset.");
    1437           0 :         return nullptr;
    1438             :     }
    1439           8 :     poDS->SetMetadataItem("POLARIZATIONS", pszPolarizations);
    1440             : 
    1441             :     const char *psAcquisitionType =
    1442           8 :         CPLGetXMLValue(psRadarParameters, "acquisitionType", "UNK");
    1443           8 :     poDS->SetMetadataItem("ACQUISITION_TYPE", psAcquisitionType);
    1444             : 
    1445           8 :     const char *psBeams = CPLGetXMLValue(psRadarParameters, "beams", "UNK");
    1446           8 :     poDS->SetMetadataItem("BEAMS", psBeams);
    1447             : 
    1448             :     const CPLStringList aosPolarizationsGrids(
    1449          16 :         CSLTokenizeString2(pszPolarizations, " ", 0));
    1450          16 :     CPLStringList imageBandList;
    1451          16 :     CPLStringList imageBandFileList;
    1452           8 :     const int nPolarizationsGridCount = aosPolarizationsGrids.size();
    1453             : 
    1454             :     /* File names for full resolution IPDFs. For GeoTIFF format, one entry per
    1455             :     pole; For NITF 2.1 format, only one entry. */
    1456           8 :     bool bIsNITF = false;
    1457           8 :     const char *pszNITF_Filename = nullptr;
    1458           8 :     int imageBandFileCount = 0;
    1459           8 :     int imageBandCount = 0;
    1460             : 
    1461             :     /* Split the polarization string*/
    1462          24 :     auto iss = std::istringstream((CPLString(pszPolarizations)).c_str());
    1463          16 :     auto pol = std::string{};
    1464             :     /* Count number of polarizations*/
    1465          24 :     while (iss >> pol)
    1466          16 :         imageBandCount++;
    1467             : 
    1468           8 :     CPLXMLNode *psNode = psImageAttributes->psChild;
    1469         131 :     for (; psNode != nullptr; psNode = psNode->psNext)
    1470             :     {
    1471             :         /* Find the tif or ntf filename */
    1472         123 :         if (psNode->eType != CXT_Element || !(EQUAL(psNode->pszValue, "ipdf")))
    1473         107 :             continue;
    1474             : 
    1475             :         /* --------------------------------------------------------------------
    1476             :          */
    1477             :         /*      Fetch ipdf image. Could be either tif or ntf. */
    1478             :         /*      Replace / by \\ */
    1479             :         /* --------------------------------------------------------------------
    1480             :          */
    1481          17 :         const char *pszBasedFilename = CPLGetXMLValue(psNode, "", "");
    1482          17 :         if (*pszBasedFilename == '\0')
    1483           0 :             continue;
    1484             : 
    1485             :         /* Count number of image names within ipdf tag*/
    1486          17 :         imageBandFileCount++;
    1487             : 
    1488          17 :         CPLString pszUpperBasedFilename(CPLString(pszBasedFilename).toupper());
    1489             : 
    1490             :         const bool bEndsWithNTF =
    1491          34 :             strlen(pszUpperBasedFilename.c_str()) > 4 &&
    1492          17 :             EQUAL(pszUpperBasedFilename.c_str() +
    1493             :                       strlen(pszUpperBasedFilename.c_str()) - 4,
    1494             :                   ".NTF");
    1495             : 
    1496          17 :         if (bEndsWithNTF)
    1497             :         {
    1498             :             /* Found it! It would not exist one more */
    1499           0 :             bIsNITF = true;
    1500           0 :             pszNITF_Filename = pszBasedFilename;
    1501           0 :             break;
    1502             :         }
    1503             :         else
    1504             :         {
    1505             :             /* Keep adding polarizations filename according to the pole */
    1506          17 :             const char *pszPole = CPLGetXMLValue(psNode, "pole", "");
    1507          17 :             if (*pszPole == '\0')
    1508             :             {
    1509             :                 /* Guard against case when pole is a null string, skip it */
    1510           0 :                 imageBandFileCount--;
    1511           0 :                 continue;
    1512             :             }
    1513             : 
    1514          17 :             if (EQUAL(pszPole, "XC"))
    1515             :             {
    1516             :                 /* Skip RCM MLC's 3rd band file ##XC.tif */
    1517           1 :                 imageBandFileCount--;
    1518           1 :                 continue;
    1519             :             }
    1520             : 
    1521          16 :             imageBandList.AddString(CPLString(pszPole).toupper());
    1522          16 :             imageBandFileList.AddString(pszBasedFilename);
    1523             :         }
    1524             :     }
    1525             : 
    1526             :     /* -------------------------------------------------------------------- */
    1527             :     /*      Incidence Angle in a sub-folder                                 */
    1528             :     /*      called 'calibration' from the 'metadata' folder                 */
    1529             :     /* -------------------------------------------------------------------- */
    1530             : 
    1531           8 :     const char *pszIncidenceAngleFileName = CPLGetXMLValue(
    1532             :         psImageReferenceAttributes, "incidenceAngleFileName", "");
    1533             : 
    1534           8 :     if (pszIncidenceAngleFileName != nullptr)
    1535             :     {
    1536          16 :         CPLString osIncidenceAngleFilePath = CPLFormFilenameSafe(
    1537           8 :             CPLFormFilenameSafe(osPath, CALIBRATION_FOLDER, nullptr).c_str(),
    1538          16 :             pszIncidenceAngleFileName, nullptr);
    1539             : 
    1540             :         /* Check if the file exist */
    1541           8 :         if (IsValidXMLFile(osIncidenceAngleFilePath))
    1542             :         {
    1543             :             CPLXMLTreeCloser psIncidenceAngle(
    1544          16 :                 CPLParseXMLFile(osIncidenceAngleFilePath));
    1545             : 
    1546           8 :             int pixelFirstLutValue = atoi(
    1547           8 :                 CPLGetXMLValue(psIncidenceAngle.get(),
    1548             :                                "=incidenceAngles.pixelFirstAnglesValue", "0"));
    1549             : 
    1550           8 :             const int stepSize = atoi(CPLGetXMLValue(
    1551           8 :                 psIncidenceAngle.get(), "=incidenceAngles.stepSize", "0"));
    1552             :             const int numberOfValues =
    1553           8 :                 atoi(CPLGetXMLValue(psIncidenceAngle.get(),
    1554             :                                     "=incidenceAngles.numberOfValues", "0"));
    1555             : 
    1556           8 :             if (!(stepSize == 0 || stepSize == INT_MIN ||
    1557             :                   numberOfValues == INT_MIN ||
    1558           8 :                   abs(numberOfValues) > INT_MAX / abs(stepSize)))
    1559             :             {
    1560             :                 /* Get the Pixel Per range */
    1561           8 :                 const int tableSize = abs(stepSize) * abs(numberOfValues);
    1562             : 
    1563          16 :                 CPLString angles;
    1564             :                 // Loop through all nodes with spaces
    1565             :                 CPLXMLNode *psNextNode =
    1566           8 :                     CPLGetXMLNode(psIncidenceAngle.get(), "=incidenceAngles");
    1567             : 
    1568             :                 CPLXMLNode *psNodeInc;
    1569         458 :                 for (psNodeInc = psNextNode->psChild; psNodeInc != nullptr;
    1570         450 :                      psNodeInc = psNodeInc->psNext)
    1571             :                 {
    1572         450 :                     if (EQUAL(psNodeInc->pszValue, "angles"))
    1573             :                     {
    1574         402 :                         if (angles.length() > 0)
    1575             :                         {
    1576         394 :                             angles.append(" "); /* separator */
    1577             :                         }
    1578             :                         const char *valAngle =
    1579         402 :                             CPLGetXMLValue(psNodeInc, "", "");
    1580         402 :                         angles.append(valAngle);
    1581             :                     }
    1582             :                 }
    1583             : 
    1584             :                 char **papszAngleList =
    1585           8 :                     CSLTokenizeString2(angles, " ", CSLT_HONOURSTRINGS);
    1586             : 
    1587             :                 /* Allocate the right LUT size according to the product range pixel
    1588             :                  */
    1589           8 :                 poDS->m_IncidenceAngleTableSize = tableSize;
    1590          16 :                 poDS->m_nfIncidenceAngleTable =
    1591           8 :                     InterpolateValues(papszAngleList, tableSize, stepSize,
    1592             :                                       numberOfValues, pixelFirstLutValue);
    1593             : 
    1594           8 :                 CSLDestroy(papszAngleList);
    1595             :             }
    1596             :         }
    1597             :     }
    1598             : 
    1599          24 :     for (int iPoleInx = 0; iPoleInx < nPolarizationsGridCount; iPoleInx++)
    1600             :     {
    1601             :         // Search for a specific band name
    1602             :         const CPLString pszPole =
    1603          16 :             CPLString(aosPolarizationsGrids[iPoleInx]).toupper();
    1604             : 
    1605             :         // Look if the NoiseLevel file xml exist for the
    1606          16 :         CPLXMLNode *psRefNode = psImageReferenceAttributes->psChild;
    1607         232 :         for (; psRefNode != nullptr; psRefNode = psRefNode->psNext)
    1608             :         {
    1609         216 :             if (EQUAL(psRefNode->pszValue, "noiseLevelFileName") && bCanCalib)
    1610             :             {
    1611             :                 /* Determine which incidence angle correction this is */
    1612             :                 const char *pszPoleToMatch =
    1613          32 :                     CPLGetXMLValue(psRefNode, "pole", "");
    1614             :                 const char *pszNoiseLevelFile =
    1615          32 :                     CPLGetXMLValue(psRefNode, "", "");
    1616             : 
    1617          32 :                 if (*pszPoleToMatch == '\0')
    1618          16 :                     continue;
    1619             : 
    1620          32 :                 if (EQUAL(pszPoleToMatch, "XC"))
    1621             :                     /* Skip noise for RCM MLC's 3rd band XC */
    1622           0 :                     continue;
    1623             : 
    1624          32 :                 if (EQUAL(pszNoiseLevelFile, ""))
    1625           0 :                     continue;
    1626             : 
    1627             :                 /* --------------------------------------------------------------------
    1628             :                  */
    1629             :                 /*      With RCM, LUT file is different per polarizarion (pole)
    1630             :                  */
    1631             :                 /*      The following code make sure to loop through all
    1632             :                  * possible       */
    1633             :                 /*      'noiseLevelFileName' and match the <ipdf 'pole' name */
    1634             :                 /* --------------------------------------------------------------------
    1635             :                  */
    1636          32 :                 if (pszPole.compare(pszPoleToMatch) != 0)
    1637             :                 {
    1638          16 :                     continue;
    1639             :                 }
    1640             : 
    1641             :                 /* --------------------------------------------------------------------
    1642             :                  */
    1643             :                 /*      LUT calibration is unique per pole in a sub-folder */
    1644             :                 /*      called 'calibration' from the 'metadata' folder */
    1645             :                 /* --------------------------------------------------------------------
    1646             :                  */
    1647             : 
    1648          32 :                 CPLString oNoiseLevelPath = CPLFormFilenameSafe(
    1649          16 :                     CPLFormFilenameSafe(osPath, CALIBRATION_FOLDER, nullptr)
    1650             :                         .c_str(),
    1651          32 :                     pszNoiseLevelFile, nullptr);
    1652          16 :                 if (IsValidXMLFile(oNoiseLevelPath))
    1653             :                 {
    1654          16 :                     osNoiseLevelsValues = std::move(oNoiseLevelPath);
    1655             :                 }
    1656             :             }
    1657             :         }
    1658             : 
    1659             :         // Search again with different file
    1660          16 :         psRefNode = psImageReferenceAttributes->psChild;
    1661         232 :         for (; psRefNode != nullptr; psRefNode = psRefNode->psNext)
    1662             :         {
    1663         216 :             if (EQUAL(psRefNode->pszValue, "lookupTableFileName") && bCanCalib)
    1664             :             {
    1665             :                 /* Determine which incidence angle correction this is */
    1666             :                 const char *pszLUTType =
    1667         102 :                     CPLGetXMLValue(psRefNode, "sarCalibrationType", "");
    1668             :                 const char *pszPoleToMatch =
    1669         102 :                     CPLGetXMLValue(psRefNode, "pole", "");
    1670         102 :                 const char *pszLUTFile = CPLGetXMLValue(psRefNode, "", "");
    1671             : 
    1672         102 :                 if (*pszPoleToMatch == '\0')
    1673          54 :                     continue;
    1674             : 
    1675         102 :                 if (EQUAL(pszPoleToMatch, "XC"))
    1676             :                     /* Skip Calib for RCM MLC's 3rd band XC */
    1677           6 :                     continue;
    1678             : 
    1679          96 :                 if (*pszLUTType == '\0')
    1680           0 :                     continue;
    1681             : 
    1682          96 :                 if (EQUAL(pszLUTType, ""))
    1683           0 :                     continue;
    1684             : 
    1685             :                 /* --------------------------------------------------------------------
    1686             :                  */
    1687             :                 /*      With RCM, LUT file is different per polarizarion (pole)
    1688             :                  */
    1689             :                 /*      The following code make sure to loop through all
    1690             :                  * possible       */
    1691             :                 /*      'lookupTableFileName' and match the <ipdf 'pole' name */
    1692             :                 /* --------------------------------------------------------------------
    1693             :                  */
    1694          96 :                 if (pszPole.compare(pszPoleToMatch) != 0)
    1695             :                 {
    1696          48 :                     continue;
    1697             :                 }
    1698             : 
    1699             :                 /* --------------------------------------------------------------------
    1700             :                  */
    1701             :                 /*      LUT calibration is unique per pole in a sub-folder */
    1702             :                 /*      called 'calibration' from the 'metadata' folder */
    1703             :                 /* --------------------------------------------------------------------
    1704             :                  */
    1705          96 :                 const CPLString osLUTFilePath = CPLFormFilenameSafe(
    1706          48 :                     CPLFormFilenameSafe(osPath, CALIBRATION_FOLDER, nullptr)
    1707             :                         .c_str(),
    1708          96 :                     pszLUTFile, nullptr);
    1709             : 
    1710          64 :                 if (EQUAL(pszLUTType, "Beta Nought") &&
    1711          16 :                     IsValidXMLFile(osLUTFilePath))
    1712             :                 {
    1713          32 :                     poDS->papszExtraFiles =
    1714          16 :                         CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
    1715             : 
    1716             :                     CPLString pszBuf(
    1717          16 :                         FormatCalibration(szBETA0, osMDFilename.c_str()));
    1718          16 :                     CPLFree(pszBeta0LUT);
    1719          16 :                     pszBeta0LUT = VSIStrdup(osLUTFilePath);
    1720             : 
    1721             :                     const char *oldLut =
    1722          16 :                         poDS->GetMetadataItem("BETA_NOUGHT_LUT");
    1723          16 :                     if (oldLut == nullptr)
    1724             :                     {
    1725           8 :                         poDS->SetMetadataItem("BETA_NOUGHT_LUT", osLUTFilePath);
    1726             :                     }
    1727             :                     else
    1728             :                     {
    1729             :                         /* Keep adding LUT file for all bands, should be planty
    1730             :                          * of space */
    1731             :                         char *ptrConcatLut =
    1732           8 :                             static_cast<char *>(CPLMalloc(2048));
    1733           8 :                         ptrConcatLut[0] =
    1734             :                             '\0'; /* Just initialize the first byte */
    1735           8 :                         strcat(ptrConcatLut, oldLut);
    1736           8 :                         strcat(ptrConcatLut, ",");
    1737           8 :                         strcat(ptrConcatLut, osLUTFilePath);
    1738           8 :                         poDS->SetMetadataItem("BETA_NOUGHT_LUT", ptrConcatLut);
    1739           8 :                         CPLFree(ptrConcatLut);
    1740             :                     }
    1741             : 
    1742          32 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1743          16 :                         poDS->papszSubDatasets, "SUBDATASET_3_NAME", pszBuf);
    1744          16 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1745          16 :                         poDS->papszSubDatasets, "SUBDATASET_3_DESC",
    1746             :                         "Beta Nought calibrated");
    1747             :                 }
    1748          48 :                 else if (EQUAL(pszLUTType, "Sigma Nought") &&
    1749          16 :                          IsValidXMLFile(osLUTFilePath))
    1750             :                 {
    1751          32 :                     poDS->papszExtraFiles =
    1752          16 :                         CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
    1753             : 
    1754             :                     CPLString pszBuf(
    1755          16 :                         FormatCalibration(szSIGMA0, osMDFilename.c_str()));
    1756          16 :                     CPLFree(pszSigma0LUT);
    1757          16 :                     pszSigma0LUT = VSIStrdup(osLUTFilePath);
    1758             : 
    1759             :                     const char *oldLut =
    1760          16 :                         poDS->GetMetadataItem("SIGMA_NOUGHT_LUT");
    1761          16 :                     if (oldLut == nullptr)
    1762             :                     {
    1763          16 :                         poDS->SetMetadataItem("SIGMA_NOUGHT_LUT",
    1764           8 :                                               osLUTFilePath);
    1765             :                     }
    1766             :                     else
    1767             :                     {
    1768             :                         /* Keep adding LUT file for all bands, should be planty
    1769             :                          * of space */
    1770             :                         char *ptrConcatLut =
    1771           8 :                             static_cast<char *>(CPLMalloc(2048));
    1772           8 :                         ptrConcatLut[0] =
    1773             :                             '\0'; /* Just initialize the first byte */
    1774           8 :                         strcat(ptrConcatLut, oldLut);
    1775           8 :                         strcat(ptrConcatLut, ",");
    1776           8 :                         strcat(ptrConcatLut, osLUTFilePath);
    1777           8 :                         poDS->SetMetadataItem("SIGMA_NOUGHT_LUT", ptrConcatLut);
    1778           8 :                         CPLFree(ptrConcatLut);
    1779             :                     }
    1780             : 
    1781          32 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1782          16 :                         poDS->papszSubDatasets, "SUBDATASET_2_NAME", pszBuf);
    1783          16 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1784          16 :                         poDS->papszSubDatasets, "SUBDATASET_2_DESC",
    1785             :                         "Sigma Nought calibrated");
    1786             :                 }
    1787          32 :                 else if (EQUAL(pszLUTType, "Gamma") &&
    1788          16 :                          IsValidXMLFile(osLUTFilePath))
    1789             :                 {
    1790          32 :                     poDS->papszExtraFiles =
    1791          16 :                         CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
    1792             : 
    1793             :                     CPLString pszBuf(
    1794          16 :                         FormatCalibration(szGAMMA, osMDFilename.c_str()));
    1795          16 :                     CPLFree(pszGammaLUT);
    1796          16 :                     pszGammaLUT = VSIStrdup(osLUTFilePath);
    1797             : 
    1798          16 :                     const char *oldLut = poDS->GetMetadataItem("GAMMA_LUT");
    1799          16 :                     if (oldLut == nullptr)
    1800             :                     {
    1801           8 :                         poDS->SetMetadataItem("GAMMA_LUT", osLUTFilePath);
    1802             :                     }
    1803             :                     else
    1804             :                     {
    1805             :                         /* Keep adding LUT file for all bands, should be planty
    1806             :                          * of space */
    1807             :                         char *ptrConcatLut =
    1808           8 :                             static_cast<char *>(CPLMalloc(2048));
    1809           8 :                         ptrConcatLut[0] =
    1810             :                             '\0'; /* Just initialize the first byte */
    1811           8 :                         strcat(ptrConcatLut, oldLut);
    1812           8 :                         strcat(ptrConcatLut, ",");
    1813           8 :                         strcat(ptrConcatLut, osLUTFilePath);
    1814           8 :                         poDS->SetMetadataItem("GAMMA_LUT", ptrConcatLut);
    1815           8 :                         CPLFree(ptrConcatLut);
    1816             :                     }
    1817             : 
    1818          32 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1819          16 :                         poDS->papszSubDatasets, "SUBDATASET_4_NAME", pszBuf);
    1820          16 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1821          16 :                         poDS->papszSubDatasets, "SUBDATASET_4_DESC",
    1822             :                         "Gamma calibrated");
    1823             :                 }
    1824             :             }
    1825             :         }
    1826             : 
    1827             :         /* --------------------------------------------------------------------
    1828             :          */
    1829             :         /*      Fetch ipdf image. Could be either tif or ntf. */
    1830             :         /*      Replace / by \\ */
    1831             :         /* --------------------------------------------------------------------
    1832             :          */
    1833             :         const char *pszBasedFilename;
    1834          16 :         if (bIsNITF)
    1835             :         {
    1836           0 :             pszBasedFilename = pszNITF_Filename;
    1837             :         }
    1838             :         else
    1839             :         {
    1840          16 :             const int bandPositionIndex = imageBandList.FindString(pszPole);
    1841          16 :             if (bandPositionIndex < 0)
    1842             :             {
    1843           0 :                 CPLFree(imageBandList);
    1844           0 :                 CPLFree(imageBandFileList);
    1845             : 
    1846           0 :                 CPLError(CE_Failure, CPLE_OpenFailed,
    1847             :                          "ERROR: RCM cannot find the polarization %s. Please "
    1848             :                          "contact your data provider for a corrected dataset",
    1849             :                          pszPole.c_str());
    1850           0 :                 return nullptr;
    1851             :             }
    1852             : 
    1853          16 :             pszBasedFilename = imageBandFileList[bandPositionIndex];
    1854             :         }
    1855             : 
    1856             :         /* --------------------------------------------------------------------
    1857             :          */
    1858             :         /*      Form full filename (path of product.xml + basename). */
    1859             :         /* --------------------------------------------------------------------
    1860             :          */
    1861          16 :         char *pszFullname = CPLStrdup(
    1862          32 :             CPLFormFilenameSafe(osPath, pszBasedFilename, nullptr).c_str());
    1863             : 
    1864             :         /* --------------------------------------------------------------------
    1865             :          */
    1866             :         /*      Try and open the file. */
    1867             :         /* --------------------------------------------------------------------
    1868             :          */
    1869             :         auto poBandFile = std::unique_ptr<GDALDataset>(GDALDataset::Open(
    1870          16 :             pszFullname, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR));
    1871          16 :         if (poBandFile == nullptr)
    1872             :         {
    1873           0 :             CPLFree(pszFullname);
    1874           0 :             continue;
    1875             :         }
    1876          16 :         if (poBandFile->GetRasterCount() == 0)
    1877             :         {
    1878           0 :             CPLFree(pszFullname);
    1879           0 :             continue;
    1880             :         }
    1881             : 
    1882          32 :         poDS->papszExtraFiles =
    1883          16 :             CSLAddString(poDS->papszExtraFiles, pszFullname);
    1884             : 
    1885             :         /* Some CFloat32 NITF files have nBitsPerSample incorrectly reported */
    1886             :         /* as 16, and get misinterpreted as CInt16.  Check the underlying NITF
    1887             :          */
    1888             :         /* and override if this is the case. */
    1889          16 :         if (poBandFile->GetRasterBand(1)->GetRasterDataType() == GDT_CFloat32)
    1890           0 :             eDataType = GDT_CFloat32;
    1891             : 
    1892             :         BandMappingRCM b =
    1893          16 :             checkBandFileMappingRCM(eDataType, poBandFile.get(), bIsNITF);
    1894          16 :         if (b == BANDERROR)
    1895             :         {
    1896           0 :             CPLFree(pszFullname);
    1897           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    1898             :                      "The underlying band files do not have an appropriate "
    1899             :                      "data type.");
    1900           0 :             return nullptr;
    1901             :         }
    1902          16 :         bool twoBandComplex = b == TWOBANDCOMPLEX;
    1903          16 :         bool isOneFilePerPol = (imageBandCount == imageBandFileCount);
    1904             : 
    1905             :         /* --------------------------------------------------------------------
    1906             :          */
    1907             :         /*      Create the band. */
    1908             :         /* --------------------------------------------------------------------
    1909             :          */
    1910          16 :         int bandNum = poDS->GetRasterCount() + 1;
    1911          16 :         if (eCalib == None || eCalib == Uncalib)
    1912             :         {
    1913             :             RCMRasterBand *poBand = new RCMRasterBand(
    1914          20 :                 poDS.get(), bandNum, eDataType, pszPole, poBandFile.release(),
    1915          20 :                 twoBandComplex, isOneFilePerPol, bIsNITF);
    1916             : 
    1917          10 :             poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
    1918             :         }
    1919             :         else
    1920             :         {
    1921           6 :             const char *pszLUT = nullptr;
    1922           6 :             switch (eCalib)
    1923             :             {
    1924           2 :                 case Sigma0:
    1925           2 :                     pszLUT = pszSigma0LUT;
    1926           2 :                     break;
    1927           2 :                 case Beta0:
    1928           2 :                     pszLUT = pszBeta0LUT;
    1929           2 :                     break;
    1930           2 :                 case Gamma:
    1931           2 :                     pszLUT = pszGammaLUT;
    1932           2 :                     break;
    1933           0 :                 default:
    1934             :                     /* we should bomb gracefully... */
    1935           0 :                     pszLUT = pszSigma0LUT;
    1936             :             }
    1937           6 :             if (!pszLUT)
    1938             :             {
    1939           0 :                 CPLFree(pszFullname);
    1940           0 :                 CPLError(CE_Failure, CPLE_AppDefined, "LUT missing.");
    1941           0 :                 return nullptr;
    1942             :             }
    1943             : 
    1944             :             // The variable 'osNoiseLevelsValues' is always the same for a ban
    1945             :             // name except the XML contains different calibration name
    1946           6 :             if (poDS->isComplexData)
    1947             :             {
    1948             :                 // If Complex, always 32 bits
    1949             :                 RCMCalibRasterBand *poBand = new RCMCalibRasterBand(
    1950           0 :                     poDS.get(), pszPole, GDT_Float32, poBandFile.release(),
    1951           0 :                     eCalib, pszLUT, osNoiseLevelsValues.c_str(), eDataType);
    1952           0 :                 poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
    1953             :             }
    1954             :             else
    1955             :             {
    1956             :                 // Whatever the datatype was previoulsy set
    1957             :                 RCMCalibRasterBand *poBand = new RCMCalibRasterBand(
    1958          12 :                     poDS.get(), pszPole, eDataType, poBandFile.release(),
    1959          12 :                     eCalib, pszLUT, osNoiseLevelsValues.c_str(), eDataType);
    1960           6 :                 poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
    1961             :             }
    1962             :         }
    1963             : 
    1964          16 :         CPLFree(pszFullname);
    1965             :     }
    1966             : 
    1967           8 :     if (poDS->papszSubDatasets != nullptr && eCalib == None)
    1968             :     {
    1969           4 :         CPLString pszBuf = FormatCalibration(szUNCALIB, osMDFilename.c_str());
    1970           4 :         poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
    1971             :                                                  "SUBDATASET_1_NAME", pszBuf);
    1972           4 :         poDS->papszSubDatasets =
    1973           4 :             CSLSetNameValue(poDS->papszSubDatasets, "SUBDATASET_1_DESC",
    1974             :                             "Uncalibrated digital numbers");
    1975             :     }
    1976           4 :     else if (poDS->papszSubDatasets != nullptr)
    1977             :     {
    1978           4 :         CSLDestroy(poDS->papszSubDatasets);
    1979           4 :         poDS->papszSubDatasets = nullptr;
    1980             :     }
    1981             : 
    1982             :     /* -------------------------------------------------------------------- */
    1983             :     /*      Set the appropriate MATRIX_REPRESENTATION.                      */
    1984             :     /* -------------------------------------------------------------------- */
    1985             : 
    1986           8 :     if (poDS->GetRasterCount() == 4 &&
    1987           0 :         (eDataType == GDT_CInt16 || eDataType == GDT_CFloat32))
    1988             :     {
    1989           0 :         poDS->SetMetadataItem("MATRIX_REPRESENTATION", "SCATTERING");
    1990             :     }
    1991             : 
    1992             :     /* -------------------------------------------------------------------- */
    1993             :     /*      Collect a few useful metadata items.                            */
    1994             :     /* -------------------------------------------------------------------- */
    1995             : 
    1996           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "satellite", "");
    1997           8 :     poDS->SetMetadataItem("SATELLITE_IDENTIFIER", pszItem);
    1998             : 
    1999           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "sensor", "");
    2000           8 :     poDS->SetMetadataItem("SENSOR_IDENTIFIER", pszItem);
    2001             : 
    2002             :     /* Get beam mode mnemonic */
    2003           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "beamMode", "UNK");
    2004           8 :     poDS->SetMetadataItem("BEAM_MODE", pszItem);
    2005             : 
    2006           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "beamModeMnemonic", "UNK");
    2007           8 :     poDS->SetMetadataItem("BEAM_MODE_MNEMONIC", pszItem);
    2008             : 
    2009           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "beamModeDefinitionId", "UNK");
    2010           8 :     poDS->SetMetadataItem("BEAM_MODE_DEFINITION_ID", pszItem);
    2011             : 
    2012           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "rawDataStartTime", "UNK");
    2013           8 :     poDS->SetMetadataItem("ACQUISITION_START_TIME", pszItem);
    2014             : 
    2015           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "inputDatasetFacilityId", "UNK");
    2016           8 :     poDS->SetMetadataItem("FACILITY_IDENTIFIER", pszItem);
    2017             : 
    2018           8 :     pszItem = CPLGetXMLValue(psSourceAttrs,
    2019             :                              "orbitAndAttitude.orbitInformation.passDirection",
    2020             :                              "UNK");
    2021           8 :     poDS->SetMetadataItem("ORBIT_DIRECTION", pszItem);
    2022           8 :     pszItem = CPLGetXMLValue(
    2023             :         psSourceAttrs, "orbitAndAttitude.orbitInformation.orbitDataSource",
    2024             :         "UNK");
    2025           8 :     poDS->SetMetadataItem("ORBIT_DATA_SOURCE", pszItem);
    2026           8 :     pszItem = CPLGetXMLValue(
    2027             :         psSourceAttrs, "orbitAndAttitude.orbitInformation.orbitDataFileName",
    2028             :         "UNK");
    2029           8 :     poDS->SetMetadataItem("ORBIT_DATA_FILE", pszItem);
    2030             : 
    2031             :     /* Get incidence angle information. DONE */
    2032           8 :     pszItem = CPLGetXMLValue(psSceneAttributes, "imageAttributes.incAngNearRng",
    2033             :                              "UNK");
    2034           8 :     poDS->SetMetadataItem("NEAR_RANGE_INCIDENCE_ANGLE", pszItem);
    2035             : 
    2036           8 :     pszItem = CPLGetXMLValue(psSceneAttributes, "imageAttributes.incAngFarRng",
    2037             :                              "UNK");
    2038           8 :     poDS->SetMetadataItem("FAR_RANGE_INCIDENCE_ANGLE", pszItem);
    2039             : 
    2040           8 :     pszItem = CPLGetXMLValue(psSceneAttributes,
    2041             :                              "imageAttributes.slantRangeNearEdge", "UNK");
    2042           8 :     poDS->SetMetadataItem("SLANT_RANGE_NEAR_EDGE", pszItem);
    2043             : 
    2044           8 :     pszItem = CPLGetXMLValue(psSceneAttributes,
    2045             :                              "imageAttributes.slantRangeFarEdge", "UNK");
    2046           8 :     poDS->SetMetadataItem("SLANT_RANGE_FAR_EDGE", pszItem);
    2047             : 
    2048             :     /*--------------------------------------------------------------------- */
    2049             :     /*      Collect Map projection/Geotransform information, if present.DONE */
    2050             :     /*      In RCM, there is no file that indicates                         */
    2051             :     /* -------------------------------------------------------------------- */
    2052           8 :     CPLXMLNode *psMapProjection = CPLGetXMLNode(
    2053             :         psImageReferenceAttributes, "geographicInformation.mapProjection");
    2054             : 
    2055           8 :     if (psMapProjection != nullptr)
    2056             :     {
    2057             :         CPLXMLNode *psPos =
    2058           0 :             CPLGetXMLNode(psMapProjection, "positioningInformation");
    2059             : 
    2060             :         pszItem =
    2061           0 :             CPLGetXMLValue(psMapProjection, "mapProjectionDescriptor", "UNK");
    2062           0 :         poDS->SetMetadataItem("MAP_PROJECTION_DESCRIPTOR", pszItem);
    2063             : 
    2064             :         pszItem =
    2065           0 :             CPLGetXMLValue(psMapProjection, "mapProjectionOrientation", "UNK");
    2066           0 :         poDS->SetMetadataItem("MAP_PROJECTION_ORIENTATION", pszItem);
    2067             : 
    2068           0 :         pszItem = CPLGetXMLValue(psMapProjection, "resamplingKernel", "UNK");
    2069           0 :         poDS->SetMetadataItem("RESAMPLING_KERNEL", pszItem);
    2070             : 
    2071           0 :         pszItem = CPLGetXMLValue(psMapProjection, "satelliteHeading", "UNK");
    2072           0 :         poDS->SetMetadataItem("SATELLITE_HEADING", pszItem);
    2073             : 
    2074           0 :         if (psPos != nullptr)
    2075             :         {
    2076           0 :             const double tl_x = CPLStrtod(
    2077             :                 CPLGetXMLValue(psPos, "upperLeftCorner.mapCoordinate.easting",
    2078             :                                "0.0"),
    2079             :                 nullptr);
    2080           0 :             const double tl_y = CPLStrtod(
    2081             :                 CPLGetXMLValue(psPos, "upperLeftCorner.mapCoordinate.northing",
    2082             :                                "0.0"),
    2083             :                 nullptr);
    2084           0 :             const double bl_x = CPLStrtod(
    2085             :                 CPLGetXMLValue(psPos, "lowerLeftCorner.mapCoordinate.easting",
    2086             :                                "0.0"),
    2087             :                 nullptr);
    2088           0 :             const double bl_y = CPLStrtod(
    2089             :                 CPLGetXMLValue(psPos, "lowerLeftCorner.mapCoordinate.northing",
    2090             :                                "0.0"),
    2091             :                 nullptr);
    2092           0 :             const double tr_x = CPLStrtod(
    2093             :                 CPLGetXMLValue(psPos, "upperRightCorner.mapCoordinate.easting",
    2094             :                                "0.0"),
    2095             :                 nullptr);
    2096           0 :             const double tr_y = CPLStrtod(
    2097             :                 CPLGetXMLValue(psPos, "upperRightCorner.mapCoordinate.northing",
    2098             :                                "0.0"),
    2099             :                 nullptr);
    2100           0 :             poDS->m_gt[1] = (tr_x - tl_x) / (poDS->nRasterXSize - 1);
    2101           0 :             poDS->m_gt[4] = (tr_y - tl_y) / (poDS->nRasterXSize - 1);
    2102           0 :             poDS->m_gt[2] = (bl_x - tl_x) / (poDS->nRasterYSize - 1);
    2103           0 :             poDS->m_gt[5] = (bl_y - tl_y) / (poDS->nRasterYSize - 1);
    2104           0 :             poDS->m_gt[0] = (tl_x - 0.5 * poDS->m_gt[1] - 0.5 * poDS->m_gt[2]);
    2105           0 :             poDS->m_gt[3] = (tl_y - 0.5 * poDS->m_gt[4] - 0.5 * poDS->m_gt[5]);
    2106             : 
    2107             :             /* Use bottom right pixel to test geotransform */
    2108           0 :             const double br_x = CPLStrtod(
    2109             :                 CPLGetXMLValue(psPos, "lowerRightCorner.mapCoordinate.easting",
    2110             :                                "0.0"),
    2111             :                 nullptr);
    2112           0 :             const double br_y = CPLStrtod(
    2113             :                 CPLGetXMLValue(psPos, "lowerRightCorner.mapCoordinate.northing",
    2114             :                                "0.0"),
    2115             :                 nullptr);
    2116           0 :             const double testx = poDS->m_gt[0] +
    2117           0 :                                  poDS->m_gt[1] * (poDS->nRasterXSize - 0.5) +
    2118           0 :                                  poDS->m_gt[2] * (poDS->nRasterYSize - 0.5);
    2119           0 :             const double testy = poDS->m_gt[3] +
    2120           0 :                                  poDS->m_gt[4] * (poDS->nRasterXSize - 0.5) +
    2121           0 :                                  poDS->m_gt[5] * (poDS->nRasterYSize - 0.5);
    2122             : 
    2123             :             /* Give 1/4 pixel numerical error leeway in calculating location
    2124             :             based on affine transform */
    2125           0 :             if ((fabs(testx - br_x) >
    2126           0 :                  fabs(0.25 * (poDS->m_gt[1] + poDS->m_gt[2]))) ||
    2127           0 :                 (fabs(testy - br_y) >
    2128           0 :                  fabs(0.25 * (poDS->m_gt[4] + poDS->m_gt[5]))))
    2129             :             {
    2130           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    2131             :                          "WARNING: Unexpected error in calculating affine "
    2132             :                          "transform: corner coordinates inconsistent.");
    2133             :             }
    2134             :             else
    2135             :             {
    2136           0 :                 poDS->bHaveGeoTransform = TRUE;
    2137             :             }
    2138             :         }
    2139             :     }
    2140             : 
    2141             :     /* -------------------------------------------------------------------- */
    2142             :     /*      Collect Projection String Information.DONE                      */
    2143             :     /* -------------------------------------------------------------------- */
    2144             :     CPLXMLNode *psEllipsoid =
    2145           8 :         CPLGetXMLNode(psImageReferenceAttributes,
    2146             :                       "geographicInformation.ellipsoidParameters");
    2147             : 
    2148           8 :     if (psEllipsoid != nullptr)
    2149             :     {
    2150          16 :         OGRSpatialReference oLL, oPrj;
    2151             : 
    2152             :         const char *pszGeodeticTerrainHeight =
    2153           8 :             CPLGetXMLValue(psEllipsoid, "geodeticTerrainHeight", "UNK");
    2154           8 :         poDS->SetMetadataItem("GEODETIC_TERRAIN_HEIGHT",
    2155           8 :                               pszGeodeticTerrainHeight);
    2156             : 
    2157             :         const char *pszEllipsoidName =
    2158           8 :             CPLGetXMLValue(psEllipsoid, "ellipsoidName", "");
    2159             :         double minor_axis =
    2160           8 :             CPLAtof(CPLGetXMLValue(psEllipsoid, "semiMinorAxis", "0.0"));
    2161             :         double major_axis =
    2162           8 :             CPLAtof(CPLGetXMLValue(psEllipsoid, "semiMajorAxis", "0.0"));
    2163             : 
    2164           8 :         if (EQUAL(pszEllipsoidName, "") || (minor_axis == 0.0) ||
    2165             :             (major_axis == 0.0))
    2166             :         {
    2167           0 :             oLL.SetWellKnownGeogCS("WGS84");
    2168           0 :             oPrj.SetWellKnownGeogCS("WGS84");
    2169             : 
    2170           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2171             :                      "WARNING: Incomplete ellipsoid "
    2172             :                      "information.  Using wgs-84 parameters.");
    2173             :         }
    2174           8 :         else if (EQUAL(pszEllipsoidName, "WGS84") ||
    2175           8 :                  EQUAL(pszEllipsoidName, "WGS 1984"))
    2176             :         {
    2177           8 :             oLL.SetWellKnownGeogCS("WGS84");
    2178           8 :             oPrj.SetWellKnownGeogCS("WGS84");
    2179             :         }
    2180             :         else
    2181             :         {
    2182           0 :             const double inv_flattening =
    2183           0 :                 major_axis / (major_axis - minor_axis);
    2184           0 :             oLL.SetGeogCS("", "", pszEllipsoidName, major_axis, inv_flattening);
    2185           0 :             oPrj.SetGeogCS("", "", pszEllipsoidName, major_axis,
    2186             :                            inv_flattening);
    2187             :         }
    2188             : 
    2189           8 :         if (psMapProjection != nullptr)
    2190             :         {
    2191             :             const char *pszProj =
    2192           0 :                 CPLGetXMLValue(psMapProjection, "mapProjectionDescriptor", "");
    2193           0 :             bool bUseProjInfo = false;
    2194             : 
    2195             :             CPLXMLNode *psUtmParams =
    2196           0 :                 CPLGetXMLNode(psMapProjection, "utmProjectionParameters");
    2197             : 
    2198             :             CPLXMLNode *psNspParams =
    2199           0 :                 CPLGetXMLNode(psMapProjection, "nspProjectionParameters");
    2200             : 
    2201           0 :             if ((psUtmParams != nullptr) && poDS->bHaveGeoTransform)
    2202             :             {
    2203             :                 /* double origEasting, origNorthing; */
    2204           0 :                 bool bNorth = true;
    2205             : 
    2206             :                 const int utmZone =
    2207           0 :                     atoi(CPLGetXMLValue(psUtmParams, "utmZone", ""));
    2208             :                 const char *pszHemisphere =
    2209           0 :                     CPLGetXMLValue(psUtmParams, "hemisphere", "");
    2210             : #if 0
    2211             :                 origEasting = CPLStrtod(CPLGetXMLValue(
    2212             :                     psUtmParams, "mapOriginFalseEasting", "0.0"), nullptr);
    2213             :                 origNorthing = CPLStrtod(CPLGetXMLValue(
    2214             :                     psUtmParams, "mapOriginFalseNorthing", "0.0"), nullptr);
    2215             : #endif
    2216           0 :                 if (STARTS_WITH_CI(pszHemisphere, "southern"))
    2217           0 :                     bNorth = FALSE;
    2218             : 
    2219           0 :                 if (STARTS_WITH_CI(pszProj, "UTM"))
    2220             :                 {
    2221           0 :                     oPrj.SetUTM(utmZone, bNorth);
    2222           0 :                     bUseProjInfo = true;
    2223             :                 }
    2224             :             }
    2225           0 :             else if ((psNspParams != nullptr) && poDS->bHaveGeoTransform)
    2226             :             {
    2227           0 :                 const double origEasting = CPLStrtod(
    2228             :                     CPLGetXMLValue(psNspParams, "mapOriginFalseEasting", "0.0"),
    2229             :                     nullptr);
    2230             :                 const double origNorthing =
    2231           0 :                     CPLStrtod(CPLGetXMLValue(psNspParams,
    2232             :                                              "mapOriginFalseNorthing", "0.0"),
    2233             :                               nullptr);
    2234           0 :                 const double copLong = CPLStrtod(
    2235             :                     CPLGetXMLValue(psNspParams, "centerOfProjectionLongitude",
    2236             :                                    "0.0"),
    2237             :                     nullptr);
    2238           0 :                 const double copLat = CPLStrtod(
    2239             :                     CPLGetXMLValue(psNspParams, "centerOfProjectionLatitude",
    2240             :                                    "0.0"),
    2241             :                     nullptr);
    2242           0 :                 const double sP1 = CPLStrtod(
    2243             :                     CPLGetXMLValue(psNspParams, "standardParallels1", "0.0"),
    2244             :                     nullptr);
    2245           0 :                 const double sP2 = CPLStrtod(
    2246             :                     CPLGetXMLValue(psNspParams, "standardParallels2", "0.0"),
    2247             :                     nullptr);
    2248             : 
    2249           0 :                 if (STARTS_WITH_CI(pszProj, "ARC"))
    2250             :                 {
    2251             :                     /* Albers Conical Equal Area */
    2252           0 :                     oPrj.SetACEA(sP1, sP2, copLat, copLong, origEasting,
    2253             :                                  origNorthing);
    2254           0 :                     bUseProjInfo = true;
    2255             :                 }
    2256           0 :                 else if (STARTS_WITH_CI(pszProj, "LCC"))
    2257             :                 {
    2258             :                     /* Lambert Conformal Conic */
    2259           0 :                     oPrj.SetLCC(sP1, sP2, copLat, copLong, origEasting,
    2260             :                                 origNorthing);
    2261           0 :                     bUseProjInfo = true;
    2262             :                 }
    2263           0 :                 else if (STARTS_WITH_CI(pszProj, "STPL"))
    2264             :                 {
    2265             :                     /* State Plate
    2266             :                     ASSUMPTIONS: "zone" in product.xml matches USGS
    2267             :                     definition as expected by ogr spatial reference; NAD83
    2268             :                     zones (versus NAD27) are assumed. */
    2269             : 
    2270             :                     const int nSPZone =
    2271           0 :                         atoi(CPLGetXMLValue(psNspParams, "zone", "1"));
    2272             : 
    2273           0 :                     oPrj.SetStatePlane(nSPZone, TRUE, nullptr, 0.0);
    2274           0 :                     bUseProjInfo = true;
    2275             :                 }
    2276             :             }
    2277             : 
    2278           0 :             if (bUseProjInfo)
    2279             :             {
    2280           0 :                 poDS->m_oSRS = std::move(oPrj);
    2281             :             }
    2282             :             else
    2283             :             {
    2284           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    2285             :                          "WARNING: Unable to interpret projection information; "
    2286             :                          "check mapProjection info in product.xml!");
    2287             :             }
    2288             :         }
    2289             : 
    2290           8 :         poDS->m_oGCPSRS = std::move(oLL);
    2291             :     }
    2292             : 
    2293             :     /* -------------------------------------------------------------------- */
    2294             :     /*      Collect GCPs.DONE */
    2295             :     /* -------------------------------------------------------------------- */
    2296           8 :     CPLXMLNode *psGeoGrid = CPLGetXMLNode(
    2297             :         psImageReferenceAttributes, "geographicInformation.geolocationGrid");
    2298             : 
    2299           8 :     if (psGeoGrid != nullptr)
    2300             :     {
    2301             :         /* count GCPs */
    2302           8 :         poDS->nGCPCount = 0;
    2303             : 
    2304         136 :         for (psNode = psGeoGrid->psChild; psNode != nullptr;
    2305         128 :              psNode = psNode->psNext)
    2306             :         {
    2307         128 :             if (EQUAL(psNode->pszValue, "imageTiePoint"))
    2308         128 :                 poDS->nGCPCount++;
    2309             :         }
    2310             : 
    2311          16 :         poDS->pasGCPList = reinterpret_cast<GDAL_GCP *>(
    2312           8 :             CPLCalloc(sizeof(GDAL_GCP), poDS->nGCPCount));
    2313             : 
    2314           8 :         poDS->nGCPCount = 0;
    2315             : 
    2316         136 :         for (psNode = psGeoGrid->psChild; psNode != nullptr;
    2317         128 :              psNode = psNode->psNext)
    2318             :         {
    2319         128 :             GDAL_GCP *psGCP = poDS->pasGCPList + poDS->nGCPCount;
    2320             : 
    2321         128 :             if (!EQUAL(psNode->pszValue, "imageTiePoint"))
    2322           0 :                 continue;
    2323             : 
    2324         128 :             poDS->nGCPCount++;
    2325             : 
    2326             :             char szID[32];
    2327         128 :             snprintf(szID, sizeof(szID), "%d", poDS->nGCPCount);
    2328         128 :             psGCP->pszId = CPLStrdup(szID);
    2329         128 :             psGCP->pszInfo = CPLStrdup("");
    2330         128 :             psGCP->dfGCPPixel =
    2331         128 :                 CPLAtof(CPLGetXMLValue(psNode, "imageCoordinate.pixel", "0"));
    2332         128 :             psGCP->dfGCPLine =
    2333         128 :                 CPLAtof(CPLGetXMLValue(psNode, "imageCoordinate.line", "0"));
    2334         128 :             psGCP->dfGCPX = CPLAtof(
    2335             :                 CPLGetXMLValue(psNode, "geodeticCoordinate.longitude", ""));
    2336         128 :             psGCP->dfGCPY = CPLAtof(
    2337             :                 CPLGetXMLValue(psNode, "geodeticCoordinate.latitude", ""));
    2338         128 :             psGCP->dfGCPZ = CPLAtof(
    2339             :                 CPLGetXMLValue(psNode, "geodeticCoordinate.height", ""));
    2340             :         }
    2341             :     }
    2342             : 
    2343           8 :     if (pszBeta0LUT)
    2344           8 :         CPLFree(pszBeta0LUT);
    2345           8 :     if (pszSigma0LUT)
    2346           8 :         CPLFree(pszSigma0LUT);
    2347           8 :     if (pszGammaLUT)
    2348           8 :         CPLFree(pszGammaLUT);
    2349             : 
    2350             :     /* -------------------------------------------------------------------- */
    2351             :     /*      Collect RPC.DONE */
    2352             :     /* -------------------------------------------------------------------- */
    2353           8 :     CPLXMLNode *psRationalFunctions = CPLGetXMLNode(
    2354             :         psImageReferenceAttributes, "geographicInformation.rationalFunctions");
    2355           8 :     if (psRationalFunctions != nullptr)
    2356             :     {
    2357           8 :         char **papszRPC = nullptr;
    2358             :         static const char *const apszXMLToGDALMapping[] = {
    2359             :             "biasError",
    2360             :             "ERR_BIAS",
    2361             :             "randomError",
    2362             :             "ERR_RAND",
    2363             :             //"lineFitQuality", "????",
    2364             :             //"pixelFitQuality", "????",
    2365             :             "lineOffset",
    2366             :             "LINE_OFF",
    2367             :             "pixelOffset",
    2368             :             "SAMP_OFF",
    2369             :             "latitudeOffset",
    2370             :             "LAT_OFF",
    2371             :             "longitudeOffset",
    2372             :             "LONG_OFF",
    2373             :             "heightOffset",
    2374             :             "HEIGHT_OFF",
    2375             :             "lineScale",
    2376             :             "LINE_SCALE",
    2377             :             "pixelScale",
    2378             :             "SAMP_SCALE",
    2379             :             "latitudeScale",
    2380             :             "LAT_SCALE",
    2381             :             "longitudeScale",
    2382             :             "LONG_SCALE",
    2383             :             "heightScale",
    2384             :             "HEIGHT_SCALE",
    2385             :             "lineNumeratorCoefficients",
    2386             :             "LINE_NUM_COEFF",
    2387             :             "lineDenominatorCoefficients",
    2388             :             "LINE_DEN_COEFF",
    2389             :             "pixelNumeratorCoefficients",
    2390             :             "SAMP_NUM_COEFF",
    2391             :             "pixelDenominatorCoefficients",
    2392             :             "SAMP_DEN_COEFF",
    2393             :         };
    2394         136 :         for (size_t i = 0; i < CPL_ARRAYSIZE(apszXMLToGDALMapping); i += 2)
    2395             :         {
    2396         256 :             const char *pszValue = CPLGetXMLValue(
    2397         128 :                 psRationalFunctions, apszXMLToGDALMapping[i], nullptr);
    2398         128 :             if (pszValue)
    2399         128 :                 papszRPC = CSLSetNameValue(
    2400         128 :                     papszRPC, apszXMLToGDALMapping[i + 1], pszValue);
    2401             :         }
    2402           8 :         poDS->GDALDataset::SetMetadata(papszRPC, "RPC");
    2403           8 :         CSLDestroy(papszRPC);
    2404             :     }
    2405             : 
    2406             :     /* -------------------------------------------------------------------- */
    2407             :     /*      Initialize any PAM information.                                 */
    2408             :     /* -------------------------------------------------------------------- */
    2409          16 :     CPLString osDescription;
    2410          16 :     CPLString osSubdatasetName;
    2411           8 :     bool useSubdatasets = true;
    2412             : 
    2413           8 :     switch (eCalib)
    2414             :     {
    2415           1 :         case Sigma0:
    2416             :         {
    2417           1 :             osSubdatasetName = szSIGMA0;
    2418           1 :             osDescription = FormatCalibration(szSIGMA0, osMDFilename.c_str());
    2419             :         }
    2420           1 :         break;
    2421           1 :         case Beta0:
    2422             :         {
    2423           1 :             osSubdatasetName = szBETA0;
    2424           1 :             osDescription = FormatCalibration(szBETA0, osMDFilename.c_str());
    2425             :         }
    2426           1 :         break;
    2427           1 :         case Gamma:
    2428             :         {
    2429           1 :             osSubdatasetName = szGAMMA;
    2430           1 :             osDescription = FormatCalibration(szGAMMA, osMDFilename.c_str());
    2431             :         }
    2432           1 :         break;
    2433           1 :         case Uncalib:
    2434             :         {
    2435           1 :             osSubdatasetName = szUNCALIB;
    2436           1 :             osDescription = FormatCalibration(szUNCALIB, osMDFilename.c_str());
    2437             :         }
    2438           1 :         break;
    2439           4 :         default:
    2440           4 :             osSubdatasetName = szUNCALIB;
    2441           4 :             osDescription = osMDFilename;
    2442           4 :             useSubdatasets = false;
    2443             :     }
    2444             : 
    2445           8 :     CPL_IGNORE_RET_VAL(osSubdatasetName);
    2446             : 
    2447           8 :     if (eCalib != None)
    2448           4 :         poDS->papszExtraFiles =
    2449           4 :             CSLAddString(poDS->papszExtraFiles, osMDFilename);
    2450             : 
    2451             :     /* -------------------------------------------------------------------- */
    2452             :     /*      Initialize any PAM information.                                 */
    2453             :     /* -------------------------------------------------------------------- */
    2454           8 :     poDS->SetDescription(osDescription);
    2455             : 
    2456           8 :     poDS->SetPhysicalFilename(osMDFilename);
    2457           8 :     poDS->SetSubdatasetName(osDescription);
    2458             : 
    2459           8 :     poDS->TryLoadXML();
    2460             : 
    2461             :     /* -------------------------------------------------------------------- */
    2462             :     /*      Check for overviews.                                            */
    2463             :     /* -------------------------------------------------------------------- */
    2464           8 :     if (useSubdatasets)
    2465           4 :         poDS->oOvManager.Initialize(poDS.get(), ":::VIRTUAL:::");
    2466             :     else
    2467           4 :         poDS->oOvManager.Initialize(poDS.get(), osMDFilename);
    2468             : 
    2469           8 :     return poDS.release();
    2470             : }
    2471             : 
    2472             : /************************************************************************/
    2473             : /*                            GetGCPCount()                             */
    2474             : /************************************************************************/
    2475             : 
    2476           6 : int RCMDataset::GetGCPCount()
    2477             : 
    2478             : {
    2479           6 :     return nGCPCount;
    2480             : }
    2481             : 
    2482             : /************************************************************************/
    2483             : /*                          GetGCPSpatialRef()                          */
    2484             : /************************************************************************/
    2485             : 
    2486           1 : const OGRSpatialReference *RCMDataset::GetGCPSpatialRef() const
    2487             : 
    2488             : {
    2489           1 :     return m_oGCPSRS.IsEmpty() || nGCPCount == 0 ? nullptr : &m_oGCPSRS;
    2490             : }
    2491             : 
    2492             : /************************************************************************/
    2493             : /*                               GetGCPs()                              */
    2494             : /************************************************************************/
    2495             : 
    2496           5 : const GDAL_GCP *RCMDataset::GetGCPs()
    2497             : 
    2498             : {
    2499           5 :     return pasGCPList;
    2500             : }
    2501             : 
    2502             : /************************************************************************/
    2503             : /*                          GetSpatialRef()                             */
    2504             : /************************************************************************/
    2505             : 
    2506           0 : const OGRSpatialReference *RCMDataset::GetSpatialRef() const
    2507             : 
    2508             : {
    2509           0 :     return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
    2510             : }
    2511             : 
    2512             : /************************************************************************/
    2513             : /*                          GetGeoTransform()                           */
    2514             : /************************************************************************/
    2515             : 
    2516           0 : CPLErr RCMDataset::GetGeoTransform(GDALGeoTransform &gt) const
    2517             : 
    2518             : {
    2519           0 :     gt = m_gt;
    2520             : 
    2521           0 :     if (bHaveGeoTransform)
    2522             :     {
    2523           0 :         return CE_None;
    2524             :     }
    2525             : 
    2526           0 :     return CE_Failure;
    2527             : }
    2528             : 
    2529             : /************************************************************************/
    2530             : /*                      GetMetadataDomainList()                         */
    2531             : /************************************************************************/
    2532             : 
    2533           0 : char **RCMDataset::GetMetadataDomainList()
    2534             : {
    2535           0 :     return BuildMetadataDomainList(GDALDataset::GetMetadataDomainList(), TRUE,
    2536           0 :                                    "SUBDATASETS", nullptr);
    2537             : }
    2538             : 
    2539             : /************************************************************************/
    2540             : /*                            GetMetadata()                             */
    2541             : /************************************************************************/
    2542             : 
    2543           2 : char **RCMDataset::GetMetadata(const char *pszDomain)
    2544             : 
    2545             : {
    2546           2 :     if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "SUBDATASETS") &&
    2547           0 :         papszSubDatasets != nullptr)
    2548           0 :         return papszSubDatasets;
    2549             : 
    2550           2 :     return GDALDataset::GetMetadata(pszDomain);
    2551             : }
    2552             : 
    2553             : /************************************************************************/
    2554             : /*                         GDALRegister_RCM()                           */
    2555             : /************************************************************************/
    2556             : 
    2557        1961 : void GDALRegister_RCM()
    2558             : 
    2559             : {
    2560        1961 :     if (GDALGetDriverByName("RCM") != nullptr)
    2561             :     {
    2562         283 :         return;
    2563             :     }
    2564             : 
    2565        1678 :     GDALDriver *poDriver = new GDALDriver();
    2566        1678 :     RCMDriverSetCommonMetadata(poDriver);
    2567             : 
    2568        1678 :     poDriver->pfnOpen = RCMDataset::Open;
    2569             : 
    2570        1678 :     GetGDALDriverManager()->RegisterDriver(poDriver);
    2571             : }

Generated by: LCOV version 1.14