LCOV - code coverage report
Current view: top level - frmts/rcm - rcmdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 665 1033 64.4 %
Date: 2024-11-21 22:18:42 Functions: 23 28 82.1 %

          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           8 : RCMDataset::RCMDataset()
     966             : {
     967           8 :     adfGeoTransform[0] = 0.0;
     968           8 :     adfGeoTransform[1] = 1.0;
     969           8 :     adfGeoTransform[2] = 0.0;
     970           8 :     adfGeoTransform[3] = 0.0;
     971           8 :     adfGeoTransform[4] = 0.0;
     972           8 :     adfGeoTransform[5] = 1.0;
     973           8 : }
     974             : 
     975             : /************************************************************************/
     976             : /*                            ~RCMDataset()                             */
     977             : /************************************************************************/
     978             : 
     979          16 : RCMDataset::~RCMDataset()
     980             : 
     981             : {
     982           8 :     RCMDataset::FlushCache(true);
     983             : 
     984           8 :     if (nGCPCount > 0)
     985             :     {
     986           8 :         GDALDeinitGCPs(nGCPCount, pasGCPList);
     987           8 :         CPLFree(pasGCPList);
     988             :     }
     989             : 
     990           8 :     RCMDataset::CloseDependentDatasets();
     991             : 
     992           8 :     if (papszSubDatasets != nullptr)
     993           4 :         CSLDestroy(papszSubDatasets);
     994             : 
     995           8 :     if (papszExtraFiles != nullptr)
     996           8 :         CSLDestroy(papszExtraFiles);
     997             : 
     998           8 :     if (m_nfIncidenceAngleTable != nullptr)
     999           8 :         CPLFree(m_nfIncidenceAngleTable);
    1000          16 : }
    1001             : 
    1002             : /************************************************************************/
    1003             : /*                      CloseDependentDatasets()                        */
    1004             : /************************************************************************/
    1005             : 
    1006           8 : int RCMDataset::CloseDependentDatasets()
    1007             : {
    1008           8 :     int bHasDroppedRef = GDALPamDataset::CloseDependentDatasets();
    1009             : 
    1010           8 :     if (nBands != 0)
    1011           8 :         bHasDroppedRef = TRUE;
    1012             : 
    1013          24 :     for (int iBand = 0; iBand < nBands; iBand++)
    1014             :     {
    1015          16 :         delete papoBands[iBand];
    1016             :     }
    1017           8 :     nBands = 0;
    1018             : 
    1019           8 :     return bHasDroppedRef;
    1020             : }
    1021             : 
    1022             : /************************************************************************/
    1023             : /*                            GetFileList()                             */
    1024             : /************************************************************************/
    1025             : 
    1026           0 : char **RCMDataset::GetFileList()
    1027             : 
    1028             : {
    1029           0 :     char **papszFileList = GDALPamDataset::GetFileList();
    1030             : 
    1031           0 :     papszFileList = CSLInsertStrings(papszFileList, -1, papszExtraFiles);
    1032             : 
    1033           0 :     return papszFileList;
    1034             : }
    1035             : 
    1036             : /************************************************************************/
    1037             : /*                                Open()                                */
    1038             : /************************************************************************/
    1039             : 
    1040          10 : GDALDataset *RCMDataset::Open(GDALOpenInfo *poOpenInfo)
    1041             : 
    1042             : {
    1043             :     /* -------------------------------------------------------------------- */
    1044             :     /*      Is this a RCM Product.xml definition?                           */
    1045             :     /* -------------------------------------------------------------------- */
    1046          10 :     if (!RCMDatasetIdentify(poOpenInfo))
    1047             :     {
    1048           0 :         return nullptr;
    1049             :     }
    1050             : 
    1051             :     /* -------------------------------------------------------------------- */
    1052             :     /*        Get subdataset information, if relevant                       */
    1053             :     /* -------------------------------------------------------------------- */
    1054          10 :     const char *pszFilename = poOpenInfo->pszFilename;
    1055          10 :     eCalibration eCalib = None;
    1056             : 
    1057          10 :     if (STARTS_WITH_CI(pszFilename, szLayerCalibration) &&
    1058           6 :         pszFilename[strlen(szLayerCalibration)] == chLayerSeparator)
    1059             :     {
    1060             :         // The calibration name and filename begins after the hard coded layer
    1061             :         // name
    1062           6 :         pszFilename += strlen(szLayerCalibration) + 1;
    1063             : 
    1064           6 :         if (STARTS_WITH_CI(pszFilename, szBETA0))
    1065             :         {
    1066           1 :             eCalib = Beta0;
    1067             :         }
    1068           5 :         else if (STARTS_WITH_CI(pszFilename, szSIGMA0))
    1069             :         {
    1070           1 :             eCalib = Sigma0;
    1071             :         }
    1072           4 :         else if (STARTS_WITH_CI(pszFilename, szGAMMA) ||
    1073           3 :                  STARTS_WITH_CI(pszFilename, "GAMMA0"))  // Cover both situation
    1074             :         {
    1075           1 :             eCalib = Gamma;
    1076             :         }
    1077           3 :         else if (STARTS_WITH_CI(pszFilename, szUNCALIB))
    1078             :         {
    1079           2 :             eCalib = Uncalib;
    1080             :         }
    1081             :         else
    1082             :         {
    1083           1 :             CPLError(CE_Failure, CPLE_NotSupported,
    1084             :                      "Unsupported calibration type");
    1085           1 :             return nullptr;
    1086             :         }
    1087             : 
    1088             :         /* advance the pointer to the actual filename */
    1089          35 :         while (*pszFilename != '\0' && *pszFilename != ':')
    1090          30 :             pszFilename++;
    1091             : 
    1092           5 :         if (*pszFilename == ':')
    1093           5 :             pszFilename++;
    1094             : 
    1095             :         // need to redo the directory check:
    1096             :         // the GDALOpenInfo check would have failed because of the calibration
    1097             :         // string on the filename
    1098             :         VSIStatBufL sStat;
    1099           5 :         if (VSIStatL(pszFilename, &sStat) == 0)
    1100           4 :             poOpenInfo->bIsDirectory = VSI_ISDIR(sStat.st_mode);
    1101             :     }
    1102             : 
    1103          18 :     CPLString osMDFilename;
    1104           9 :     if (poOpenInfo->bIsDirectory)
    1105             :     {
    1106             :         /* Check for directory access when there is a product.xml file in the
    1107             :         directory. */
    1108           2 :         osMDFilename = CPLFormCIFilename(pszFilename, "product.xml", nullptr);
    1109             : 
    1110             :         VSIStatBufL sStat;
    1111           2 :         if (VSIStatL(osMDFilename, &sStat) != 0)
    1112             :         {
    1113             :             /* If not, check for directory extra 'metadata' access when there is
    1114             :             a product.xml file in the directory. */
    1115             :             osMDFilename =
    1116           1 :                 CPLFormCIFilename(pszFilename, GetMetadataProduct(), nullptr);
    1117             :         }
    1118             :     }
    1119             :     else
    1120           7 :         osMDFilename = pszFilename;
    1121             : 
    1122             :     /* -------------------------------------------------------------------- */
    1123             :     /*      Ingest the Product.xml file.                                    */
    1124             :     /* -------------------------------------------------------------------- */
    1125          18 :     CPLXMLTreeCloser psProduct(CPLParseXMLFile(osMDFilename));
    1126           9 :     if (!psProduct)
    1127           1 :         return nullptr;
    1128             : 
    1129             :     /* -------------------------------------------------------------------- */
    1130             :     /*      Confirm the requested access is supported.                      */
    1131             :     /* -------------------------------------------------------------------- */
    1132           8 :     if (poOpenInfo->eAccess == GA_Update)
    1133             :     {
    1134           0 :         CPLError(CE_Failure, CPLE_NotSupported,
    1135             :                  "ERROR: The RCM driver does not support update "
    1136             :                  "access to existing dataset.");
    1137           0 :         return nullptr;
    1138             :     }
    1139             : 
    1140             :     CPLXMLNode *psSceneAttributes =
    1141           8 :         CPLGetXMLNode(psProduct.get(), "=product.sceneAttributes");
    1142           8 :     if (psSceneAttributes == nullptr)
    1143             :     {
    1144           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
    1145             :                  "ERROR: Failed to find <sceneAttributes> in document.");
    1146           0 :         return nullptr;
    1147             :     }
    1148             : 
    1149           8 :     CPLXMLNode *psImageAttributes = CPLGetXMLNode(
    1150             :         psProduct.get(), "=product.sceneAttributes.imageAttributes");
    1151           8 :     if (psImageAttributes == nullptr)
    1152             :     {
    1153           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
    1154             :                  "ERROR: Failed to find <sceneAttributes.imageAttributes> in "
    1155             :                  "document.");
    1156           0 :         return nullptr;
    1157             :     }
    1158             : 
    1159             :     int numberOfEntries =
    1160           8 :         atoi(CPLGetXMLValue(psSceneAttributes, "numberOfEntries", "0"));
    1161           8 :     if (numberOfEntries != 1)
    1162             :     {
    1163           0 :         CPLError(CE_Failure, CPLE_OpenFailed,
    1164             :                  "ERROR: Only RCM with Complex Single-beam is supported.");
    1165           0 :         return nullptr;
    1166             :     }
    1167             : 
    1168             :     CPLXMLNode *psImageReferenceAttributes =
    1169           8 :         CPLGetXMLNode(psProduct.get(), "=product.imageReferenceAttributes");
    1170           8 :     if (psImageReferenceAttributes == nullptr)
    1171             :     {
    1172           0 :         CPLError(
    1173             :             CE_Failure, CPLE_OpenFailed,
    1174             :             "ERROR: Failed to find <imageReferenceAttributes> in document.");
    1175           0 :         return nullptr;
    1176             :     }
    1177             : 
    1178             :     CPLXMLNode *psImageGenerationParameters =
    1179           8 :         CPLGetXMLNode(psProduct.get(), "=product.imageGenerationParameters");
    1180           8 :     if (psImageGenerationParameters == nullptr)
    1181             :     {
    1182           0 :         CPLError(
    1183             :             CE_Failure, CPLE_OpenFailed,
    1184             :             "ERROR: Failed to find <imageGenerationParameters> in document.");
    1185           0 :         return nullptr;
    1186             :     }
    1187             : 
    1188             :     /* -------------------------------------------------------------------- */
    1189             :     /*      Create the dataset.                                             */
    1190             :     /* -------------------------------------------------------------------- */
    1191          16 :     auto poDS = std::make_unique<RCMDataset>();
    1192             : 
    1193           8 :     poDS->psProduct = std::move(psProduct);
    1194             : 
    1195             :     /* -------------------------------------------------------------------- */
    1196             :     /*      Get overall image information.                                  */
    1197             :     /* -------------------------------------------------------------------- */
    1198           8 :     poDS->nRasterXSize = atoi(CPLGetXMLValue(
    1199             :         psSceneAttributes, "imageAttributes.samplesPerLine", "-1"));
    1200           8 :     poDS->nRasterYSize = atoi(
    1201             :         CPLGetXMLValue(psSceneAttributes, "imageAttributes.numLines", "-1"));
    1202           8 :     if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize))
    1203             :     {
    1204           0 :         return nullptr;
    1205             :     }
    1206             : 
    1207             :     /* -------------------------------------------------------------------- */
    1208             :     /*      Check product type, as to determine if there are LUTs for       */
    1209             :     /*      calibration purposes.                                           */
    1210             :     /* -------------------------------------------------------------------- */
    1211             : 
    1212             :     const char *pszItem =
    1213           8 :         CPLGetXMLValue(psImageGenerationParameters,
    1214             :                        "generalProcessingInformation.productType", "UNK");
    1215           8 :     poDS->SetMetadataItem("PRODUCT_TYPE", pszItem);
    1216           8 :     const char *pszProductType = pszItem;
    1217             : 
    1218             :     pszItem =
    1219           8 :         CPLGetXMLValue(poDS->psProduct.get(), "=product.productId", "UNK");
    1220           8 :     poDS->SetMetadataItem("PRODUCT_ID", pszItem);
    1221             : 
    1222           8 :     pszItem = CPLGetXMLValue(
    1223           8 :         poDS->psProduct.get(),
    1224             :         "=product.securityAttributes.securityClassification", "UNK");
    1225           8 :     poDS->SetMetadataItem("SECURITY_CLASSIFICATION", pszItem);
    1226             : 
    1227             :     pszItem =
    1228           8 :         CPLGetXMLValue(poDS->psProduct.get(),
    1229             :                        "=product.sourceAttributes.polarizationDataMode", "UNK");
    1230           8 :     poDS->SetMetadataItem("POLARIZATION_DATA_MODE", pszItem);
    1231             : 
    1232           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1233             :                              "generalProcessingInformation.processingFacility",
    1234             :                              "UNK");
    1235           8 :     poDS->SetMetadataItem("PROCESSING_FACILITY", pszItem);
    1236             : 
    1237             :     pszItem =
    1238           8 :         CPLGetXMLValue(psImageGenerationParameters,
    1239             :                        "generalProcessingInformation.processingTime", "UNK");
    1240           8 :     poDS->SetMetadataItem("PROCESSING_TIME", pszItem);
    1241             : 
    1242           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1243             :                              "sarProcessingInformation.satelliteHeight", "UNK");
    1244           8 :     poDS->SetMetadataItem("SATELLITE_HEIGHT", pszItem);
    1245             : 
    1246           8 :     pszItem = CPLGetXMLValue(
    1247             :         psImageGenerationParameters,
    1248             :         "sarProcessingInformation.zeroDopplerTimeFirstLine", "UNK");
    1249           8 :     poDS->SetMetadataItem("FIRST_LINE_TIME", pszItem);
    1250             : 
    1251           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1252             :                              "sarProcessingInformation.zeroDopplerTimeLastLine",
    1253             :                              "UNK");
    1254           8 :     poDS->SetMetadataItem("LAST_LINE_TIME", pszItem);
    1255             : 
    1256           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1257             :                              "sarProcessingInformation.lutApplied", "");
    1258           8 :     poDS->SetMetadataItem("LUT_APPLIED", pszItem);
    1259             : 
    1260             :     /*---------------------------------------------------------------------
    1261             :            If true, a polarization dependent application LUT has been applied
    1262             :            for each polarization channel. Otherwise the same application LUT
    1263             :            has been applied for all polarization channels. Not applicable to
    1264             :            lookupTable = "Unity*" or if dataType = "Floating-Point".
    1265             :       --------------------------------------------------------------------- */
    1266           8 :     pszItem = CPLGetXMLValue(psImageGenerationParameters,
    1267             :                              "sarProcessingInformation.perPolarizationScaling",
    1268             :                              "false");
    1269           8 :     poDS->SetMetadataItem("PER_POLARIZATION_SCALING", pszItem);
    1270           8 :     if (EQUAL(pszItem, "true") || EQUAL(pszItem, "TRUE"))
    1271             :     {
    1272           8 :         poDS->bPerPolarizationScaling = TRUE;
    1273             :     }
    1274             : 
    1275             :     /* the following cases can be assumed to have no LUTs, as per
    1276             :      * RN-RP-51-2713, but also common sense
    1277             :      * SLC represents a SLant range georeferenced Complex product
    1278             :      * (i.e., equivalent to a Single-Look Complex product for RADARSAT-1 or
    1279             :      * RADARSAT-2). • GRD or GRC represent GRound range georeferenced Detected
    1280             :      * or Complex products (GRD is equivalent to an SGX, SCN or SCW product for
    1281             :      * RADARSAT1 or RADARSAT-2). • GCD or GCC represent GeoCoded Detected or
    1282             :      * Complex products (GCD is equivalent to an SSG or SPG product for
    1283             :      * RADARSAT-1 or RADARSAT-2).
    1284             :      */
    1285           8 :     bool bCanCalib = false;
    1286           8 :     if (!(STARTS_WITH_CI(pszProductType, "UNK") ||
    1287           8 :           STARTS_WITH_CI(pszProductType, "GCD") ||
    1288           8 :           STARTS_WITH_CI(pszProductType, "GCC")))
    1289             :     {
    1290           8 :         bCanCalib = true;
    1291             :     }
    1292             : 
    1293             :     /* -------------------------------------------------------------------- */
    1294             :     /*      Get dataType (so we can recognise complex data), and the        */
    1295             :     /*      bitsPerSample.                                                  */
    1296             :     /* -------------------------------------------------------------------- */
    1297           8 :     const char *pszSampleDataType = CPLGetXMLValue(
    1298             :         psImageReferenceAttributes, "rasterAttributes.sampleType", "");
    1299           8 :     poDS->SetMetadataItem("SAMPLE_TYPE", pszSampleDataType);
    1300             : 
    1301             :     /* Either Integer (16 bits) or Floating-Point (32 bits) */
    1302           8 :     const char *pszDataType = CPLGetXMLValue(psImageReferenceAttributes,
    1303             :                                              "rasterAttributes.dataType", "");
    1304           8 :     poDS->SetMetadataItem("DATA_TYPE", pszDataType);
    1305             : 
    1306             :     /* 2 entries for complex data 1 entry for magnitude detected data */
    1307           8 :     const char *pszBitsPerSample = CPLGetXMLValue(
    1308             :         psImageReferenceAttributes, "rasterAttributes.bitsPerSample", "");
    1309           8 :     const int nBitsPerSample = atoi(pszBitsPerSample);
    1310           8 :     poDS->SetMetadataItem("BITS_PER_SAMPLE", pszBitsPerSample);
    1311             : 
    1312             :     const char *pszSampledPixelSpacingTime =
    1313           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1314             :                        "rasterAttributes.sampledPixelSpacingTime", "UNK");
    1315           8 :     poDS->SetMetadataItem("SAMPLED_PIXEL_SPACING_TIME",
    1316           8 :                           pszSampledPixelSpacingTime);
    1317             : 
    1318             :     const char *pszSampledLineSpacingTime =
    1319           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1320             :                        "rasterAttributes.sampledLineSpacingTime", "UNK");
    1321           8 :     poDS->SetMetadataItem("SAMPLED_LINE_SPACING_TIME",
    1322           8 :                           pszSampledLineSpacingTime);
    1323             : 
    1324             :     GDALDataType eDataType;
    1325             : 
    1326           8 :     if (EQUAL(pszSampleDataType, "Mixed")) /* RCM MLC has Mixed sampleType */
    1327             :     {
    1328           1 :         poDS->isComplexData = false; /* RCM MLC is detected, non-complex */
    1329           1 :         if (nBitsPerSample == 32)
    1330             :         {
    1331           1 :             eDataType = GDT_Float32; /* 32 bits, check read block */
    1332           1 :             poDS->magnitudeBits = 32;
    1333             :         }
    1334             :         else
    1335             :         {
    1336           0 :             eDataType = GDT_UInt16; /* 16 bits, check read block */
    1337           0 :             poDS->magnitudeBits = 16;
    1338             :         }
    1339             :     }
    1340           7 :     else if (EQUAL(pszSampleDataType, "Complex"))
    1341             :     {
    1342           0 :         poDS->isComplexData = true;
    1343             :         /* Usually this is the same bits for both */
    1344           0 :         poDS->realBitsComplexData = nBitsPerSample;
    1345           0 :         poDS->imaginaryBitsComplexData = nBitsPerSample;
    1346             : 
    1347           0 :         if (nBitsPerSample == 32)
    1348             :         {
    1349           0 :             eDataType = GDT_CFloat32; /* 32 bits, check read block */
    1350             :         }
    1351             :         else
    1352             :         {
    1353           0 :             eDataType = GDT_CInt16; /* 16 bits, check read block */
    1354             :         }
    1355             :     }
    1356           7 :     else if (nBitsPerSample == 32 &&
    1357           0 :              EQUAL(pszSampleDataType, "Magnitude Detected"))
    1358             :     {
    1359             :         /* Actually, I don't need to test the 'dataType'=' Floating-Point', we
    1360             :          * know that a 32 bits per sample */
    1361           0 :         eDataType = GDT_Float32;
    1362           0 :         poDS->isComplexData = false;
    1363           0 :         poDS->magnitudeBits = 32;
    1364             :     }
    1365           7 :     else if (nBitsPerSample == 16 &&
    1366           7 :              EQUAL(pszSampleDataType, "Magnitude Detected"))
    1367             :     {
    1368             :         /* Actually, I don't need to test the 'dataType'=' Integer', we know
    1369             :          * that a 16 bits per sample */
    1370           7 :         eDataType = GDT_UInt16;
    1371           7 :         poDS->isComplexData = false;
    1372           7 :         poDS->magnitudeBits = 16;
    1373             :     }
    1374             :     else
    1375             :     {
    1376           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1377             :                  "ERROR: dataType=%s and bitsPerSample=%d are not a supported "
    1378             :                  "configuration.",
    1379             :                  pszDataType, nBitsPerSample);
    1380           0 :         return nullptr;
    1381             :     }
    1382             : 
    1383             :     /* Indicates whether pixel number (i.e., range) increases or decreases with
    1384             :     range time. For GCD and GCC products, this applies to intermediate ground
    1385             :     range image data prior to geocoding. */
    1386             :     const char *pszPixelTimeOrdering =
    1387           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1388             :                        "rasterAttributes.pixelTimeOrdering", "UNK");
    1389           8 :     poDS->SetMetadataItem("PIXEL_TIME_ORDERING", pszPixelTimeOrdering);
    1390             : 
    1391             :     /* Indicates whether line numbers (i.e., azimuth) increase or decrease with
    1392             :     azimuth time. For GCD and GCC products, this applies to intermediate ground
    1393             :     range image data prior to geocoding. */
    1394           8 :     const char *pszLineTimeOrdering = CPLGetXMLValue(
    1395             :         psImageReferenceAttributes, "rasterAttributes.lineTimeOrdering", "UNK");
    1396           8 :     poDS->SetMetadataItem("LINE_TIME_ORDERING", pszLineTimeOrdering);
    1397             : 
    1398             :     /* while we're at it, extract the pixel spacing information */
    1399             :     const char *pszPixelSpacing =
    1400           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1401             :                        "rasterAttributes.sampledPixelSpacing", "UNK");
    1402           8 :     poDS->SetMetadataItem("PIXEL_SPACING", pszPixelSpacing);
    1403             : 
    1404             :     const char *pszLineSpacing =
    1405           8 :         CPLGetXMLValue(psImageReferenceAttributes,
    1406             :                        "rasterAttributes.sampledLineSpacing", "UNK");
    1407           8 :     poDS->SetMetadataItem("LINE_SPACING", pszLineSpacing);
    1408             : 
    1409             :     /* -------------------------------------------------------------------- */
    1410             :     /*      Open each of the data files as a complex band.                  */
    1411             :     /* -------------------------------------------------------------------- */
    1412           8 :     char *pszBeta0LUT = nullptr;
    1413           8 :     char *pszGammaLUT = nullptr;
    1414           8 :     char *pszSigma0LUT = nullptr;
    1415             :     // Same file for all calibrations except the calibration is plit inside the
    1416             :     // XML
    1417          16 :     std::string osNoiseLevelsValues;
    1418             : 
    1419          16 :     const CPLString osPath = CPLGetPath(osMDFilename);
    1420             : 
    1421             :     /* Get a list of all polarizations */
    1422             :     CPLXMLNode *psSourceAttrs =
    1423           8 :         CPLGetXMLNode(poDS->psProduct.get(), "=product.sourceAttributes");
    1424           8 :     if (psSourceAttrs == nullptr)
    1425             :     {
    1426           0 :         CPLError(
    1427             :             CE_Failure, CPLE_OpenFailed,
    1428             :             "ERROR: RCM source attributes is missing. Please contact your data "
    1429             :             "provider for a corrected dataset.");
    1430           0 :         return nullptr;
    1431             :     }
    1432             : 
    1433           8 :     CPLXMLNode *psRadarParameters = CPLGetXMLNode(
    1434           8 :         poDS->psProduct.get(), "=product.sourceAttributes.radarParameters");
    1435           8 :     if (psRadarParameters == nullptr)
    1436             :     {
    1437           0 :         CPLError(
    1438             :             CE_Failure, CPLE_OpenFailed,
    1439             :             "ERROR: RCM radar parameters is missing. Please contact your data "
    1440             :             "provider for a corrected dataset.");
    1441           0 :         return nullptr;
    1442             :     }
    1443             : 
    1444             :     const char *pszPolarizations =
    1445           8 :         CPLGetXMLValue(psRadarParameters, "polarizations", "");
    1446           8 :     if (pszPolarizations == nullptr || strlen(pszPolarizations) == 0)
    1447             :     {
    1448           0 :         CPLError(
    1449             :             CE_Failure, CPLE_OpenFailed,
    1450             :             "ERROR: RCM polarizations list is missing. Please contact your "
    1451             :             "data provider for a corrected dataset.");
    1452           0 :         return nullptr;
    1453             :     }
    1454           8 :     poDS->SetMetadataItem("POLARIZATIONS", pszPolarizations);
    1455             : 
    1456             :     const char *psAcquisitionType =
    1457           8 :         CPLGetXMLValue(psRadarParameters, "acquisitionType", "UNK");
    1458           8 :     poDS->SetMetadataItem("ACQUISITION_TYPE", psAcquisitionType);
    1459             : 
    1460           8 :     const char *psBeams = CPLGetXMLValue(psRadarParameters, "beams", "UNK");
    1461           8 :     poDS->SetMetadataItem("BEAMS", psBeams);
    1462             : 
    1463             :     const CPLStringList aosPolarizationsGrids(
    1464          16 :         CSLTokenizeString2(pszPolarizations, " ", 0));
    1465          16 :     CPLStringList imageBandList;
    1466          16 :     CPLStringList imageBandFileList;
    1467           8 :     const int nPolarizationsGridCount = aosPolarizationsGrids.size();
    1468             : 
    1469             :     /* File names for full resolution IPDFs. For GeoTIFF format, one entry per
    1470             :     pole; For NITF 2.1 format, only one entry. */
    1471           8 :     bool bIsNITF = false;
    1472           8 :     const char *pszNITF_Filename = nullptr;
    1473           8 :     int imageBandFileCount = 0;
    1474           8 :     int imageBandCount = 0;
    1475             : 
    1476             :     /* Split the polarization string*/
    1477          24 :     auto iss = std::istringstream((CPLString(pszPolarizations)).c_str());
    1478          16 :     auto pol = std::string{};
    1479             :     /* Count number of polarizations*/
    1480          24 :     while (iss >> pol)
    1481          16 :         imageBandCount++;
    1482             : 
    1483           8 :     CPLXMLNode *psNode = psImageAttributes->psChild;
    1484         131 :     for (; psNode != nullptr; psNode = psNode->psNext)
    1485             :     {
    1486             :         /* Find the tif or ntf filename */
    1487         123 :         if (psNode->eType != CXT_Element || !(EQUAL(psNode->pszValue, "ipdf")))
    1488         107 :             continue;
    1489             : 
    1490             :         /* --------------------------------------------------------------------
    1491             :          */
    1492             :         /*      Fetch ipdf image. Could be either tif or ntf. */
    1493             :         /*      Replace / by \\ */
    1494             :         /* --------------------------------------------------------------------
    1495             :          */
    1496          17 :         const char *pszBasedFilename = CPLGetXMLValue(psNode, "", "");
    1497          17 :         if (*pszBasedFilename == '\0')
    1498           0 :             continue;
    1499             : 
    1500             :         /* Count number of image names within ipdf tag*/
    1501          17 :         imageBandFileCount++;
    1502             : 
    1503          17 :         CPLString pszUpperBasedFilename(CPLString(pszBasedFilename).toupper());
    1504             : 
    1505             :         const bool bEndsWithNTF =
    1506          34 :             strlen(pszUpperBasedFilename.c_str()) > 4 &&
    1507          17 :             EQUAL(pszUpperBasedFilename.c_str() +
    1508             :                       strlen(pszUpperBasedFilename.c_str()) - 4,
    1509             :                   ".NTF");
    1510             : 
    1511          17 :         if (bEndsWithNTF)
    1512             :         {
    1513             :             /* Found it! It would not exist one more */
    1514           0 :             bIsNITF = true;
    1515           0 :             pszNITF_Filename = pszBasedFilename;
    1516           0 :             break;
    1517             :         }
    1518             :         else
    1519             :         {
    1520             :             /* Keep adding polarizations filename according to the pole */
    1521          17 :             const char *pszPole = CPLGetXMLValue(psNode, "pole", "");
    1522          17 :             if (*pszPole == '\0')
    1523             :             {
    1524             :                 /* Guard against case when pole is a null string, skip it */
    1525           0 :                 imageBandFileCount--;
    1526           0 :                 continue;
    1527             :             }
    1528             : 
    1529          17 :             if (EQUAL(pszPole, "XC"))
    1530             :             {
    1531             :                 /* Skip RCM MLC's 3rd band file ##XC.tif */
    1532           1 :                 imageBandFileCount--;
    1533           1 :                 continue;
    1534             :             }
    1535             : 
    1536          16 :             imageBandList.AddString(CPLString(pszPole).toupper());
    1537          16 :             imageBandFileList.AddString(pszBasedFilename);
    1538             :         }
    1539             :     }
    1540             : 
    1541             :     /* -------------------------------------------------------------------- */
    1542             :     /*      Incidence Angle in a sub-folder                                 */
    1543             :     /*      called 'calibration' from the 'metadata' folder                 */
    1544             :     /* -------------------------------------------------------------------- */
    1545             : 
    1546           8 :     const char *pszIncidenceAngleFileName = CPLGetXMLValue(
    1547             :         psImageReferenceAttributes, "incidenceAngleFileName", "");
    1548             : 
    1549           8 :     if (pszIncidenceAngleFileName != nullptr)
    1550             :     {
    1551             :         CPLString osIncidenceAngleFilePath = CPLFormFilename(
    1552             :             CPLFormFilename(osPath, CALIBRATION_FOLDER, nullptr),
    1553          16 :             pszIncidenceAngleFileName, nullptr);
    1554             : 
    1555             :         /* Check if the file exist */
    1556           8 :         if (IsValidXMLFile(osIncidenceAngleFilePath))
    1557             :         {
    1558             :             CPLXMLTreeCloser psIncidenceAngle(
    1559          16 :                 CPLParseXMLFile(osIncidenceAngleFilePath));
    1560             : 
    1561           8 :             int pixelFirstLutValue = atoi(
    1562           8 :                 CPLGetXMLValue(psIncidenceAngle.get(),
    1563             :                                "=incidenceAngles.pixelFirstAnglesValue", "0"));
    1564             : 
    1565           8 :             const int stepSize = atoi(CPLGetXMLValue(
    1566           8 :                 psIncidenceAngle.get(), "=incidenceAngles.stepSize", "0"));
    1567             :             const int numberOfValues =
    1568           8 :                 atoi(CPLGetXMLValue(psIncidenceAngle.get(),
    1569             :                                     "=incidenceAngles.numberOfValues", "0"));
    1570             : 
    1571           8 :             if (!(stepSize == 0 || stepSize == INT_MIN ||
    1572             :                   numberOfValues == INT_MIN ||
    1573           8 :                   abs(numberOfValues) > INT_MAX / abs(stepSize)))
    1574             :             {
    1575             :                 /* Get the Pixel Per range */
    1576           8 :                 const int tableSize = abs(stepSize) * abs(numberOfValues);
    1577             : 
    1578          16 :                 CPLString angles;
    1579             :                 // Loop through all nodes with spaces
    1580             :                 CPLXMLNode *psNextNode =
    1581           8 :                     CPLGetXMLNode(psIncidenceAngle.get(), "=incidenceAngles");
    1582             : 
    1583             :                 CPLXMLNode *psNodeInc;
    1584         458 :                 for (psNodeInc = psNextNode->psChild; psNodeInc != nullptr;
    1585         450 :                      psNodeInc = psNodeInc->psNext)
    1586             :                 {
    1587         450 :                     if (EQUAL(psNodeInc->pszValue, "angles"))
    1588             :                     {
    1589         402 :                         if (angles.length() > 0)
    1590             :                         {
    1591         394 :                             angles.append(" "); /* separator */
    1592             :                         }
    1593             :                         const char *valAngle =
    1594         402 :                             CPLGetXMLValue(psNodeInc, "", "");
    1595         402 :                         angles.append(valAngle);
    1596             :                     }
    1597             :                 }
    1598             : 
    1599             :                 char **papszAngleList =
    1600           8 :                     CSLTokenizeString2(angles, " ", CSLT_HONOURSTRINGS);
    1601             : 
    1602             :                 /* Allocate the right LUT size according to the product range pixel
    1603             :                  */
    1604           8 :                 poDS->m_IncidenceAngleTableSize = tableSize;
    1605          16 :                 poDS->m_nfIncidenceAngleTable =
    1606           8 :                     InterpolateValues(papszAngleList, tableSize, stepSize,
    1607             :                                       numberOfValues, pixelFirstLutValue);
    1608             : 
    1609           8 :                 CSLDestroy(papszAngleList);
    1610             :             }
    1611             :         }
    1612             :     }
    1613             : 
    1614          24 :     for (int iPoleInx = 0; iPoleInx < nPolarizationsGridCount; iPoleInx++)
    1615             :     {
    1616             :         // Search for a specific band name
    1617             :         const CPLString pszPole =
    1618          16 :             CPLString(aosPolarizationsGrids[iPoleInx]).toupper();
    1619             : 
    1620             :         // Look if the NoiseLevel file xml exist for the
    1621          16 :         CPLXMLNode *psRefNode = psImageReferenceAttributes->psChild;
    1622         232 :         for (; psRefNode != nullptr; psRefNode = psRefNode->psNext)
    1623             :         {
    1624         216 :             if (EQUAL(psRefNode->pszValue, "noiseLevelFileName") && bCanCalib)
    1625             :             {
    1626             :                 /* Determine which incidence angle correction this is */
    1627             :                 const char *pszPoleToMatch =
    1628          32 :                     CPLGetXMLValue(psRefNode, "pole", "");
    1629             :                 const char *pszNoiseLevelFile =
    1630          32 :                     CPLGetXMLValue(psRefNode, "", "");
    1631             : 
    1632          32 :                 if (*pszPoleToMatch == '\0')
    1633          16 :                     continue;
    1634             : 
    1635          32 :                 if (EQUAL(pszPoleToMatch, "XC"))
    1636             :                     /* Skip noise for RCM MLC's 3rd band XC */
    1637           0 :                     continue;
    1638             : 
    1639          32 :                 if (EQUAL(pszNoiseLevelFile, ""))
    1640           0 :                     continue;
    1641             : 
    1642             :                 /* --------------------------------------------------------------------
    1643             :                  */
    1644             :                 /*      With RCM, LUT file is different per polarizarion (pole)
    1645             :                  */
    1646             :                 /*      The following code make sure to loop through all
    1647             :                  * possible       */
    1648             :                 /*      'noiseLevelFileName' and match the <ipdf 'pole' name */
    1649             :                 /* --------------------------------------------------------------------
    1650             :                  */
    1651          32 :                 if (pszPole.compare(pszPoleToMatch) != 0)
    1652             :                 {
    1653          16 :                     continue;
    1654             :                 }
    1655             : 
    1656             :                 /* --------------------------------------------------------------------
    1657             :                  */
    1658             :                 /*      LUT calibration is unique per pole in a sub-folder */
    1659             :                 /*      called 'calibration' from the 'metadata' folder */
    1660             :                 /* --------------------------------------------------------------------
    1661             :                  */
    1662             : 
    1663             :                 const CPLString oNoiseLevelPath = CPLFormFilename(
    1664             :                     CPLFormFilename(osPath, CALIBRATION_FOLDER, nullptr),
    1665          32 :                     pszNoiseLevelFile, nullptr);
    1666          16 :                 if (IsValidXMLFile(oNoiseLevelPath))
    1667             :                 {
    1668          16 :                     osNoiseLevelsValues = oNoiseLevelPath;
    1669             :                 }
    1670             :             }
    1671             :         }
    1672             : 
    1673             :         // Search again with different file
    1674          16 :         psRefNode = psImageReferenceAttributes->psChild;
    1675         232 :         for (; psRefNode != nullptr; psRefNode = psRefNode->psNext)
    1676             :         {
    1677         216 :             if (EQUAL(psRefNode->pszValue, "lookupTableFileName") && bCanCalib)
    1678             :             {
    1679             :                 /* Determine which incidence angle correction this is */
    1680             :                 const char *pszLUTType =
    1681         102 :                     CPLGetXMLValue(psRefNode, "sarCalibrationType", "");
    1682             :                 const char *pszPoleToMatch =
    1683         102 :                     CPLGetXMLValue(psRefNode, "pole", "");
    1684         102 :                 const char *pszLUTFile = CPLGetXMLValue(psRefNode, "", "");
    1685             : 
    1686         102 :                 if (*pszPoleToMatch == '\0')
    1687          54 :                     continue;
    1688             : 
    1689         102 :                 if (EQUAL(pszPoleToMatch, "XC"))
    1690             :                     /* Skip Calib for RCM MLC's 3rd band XC */
    1691           6 :                     continue;
    1692             : 
    1693          96 :                 if (*pszLUTType == '\0')
    1694           0 :                     continue;
    1695             : 
    1696          96 :                 if (EQUAL(pszLUTType, ""))
    1697           0 :                     continue;
    1698             : 
    1699             :                 /* --------------------------------------------------------------------
    1700             :                  */
    1701             :                 /*      With RCM, LUT file is different per polarizarion (pole)
    1702             :                  */
    1703             :                 /*      The following code make sure to loop through all
    1704             :                  * possible       */
    1705             :                 /*      'lookupTableFileName' and match the <ipdf 'pole' name */
    1706             :                 /* --------------------------------------------------------------------
    1707             :                  */
    1708          96 :                 if (pszPole.compare(pszPoleToMatch) != 0)
    1709             :                 {
    1710          48 :                     continue;
    1711             :                 }
    1712             : 
    1713             :                 /* --------------------------------------------------------------------
    1714             :                  */
    1715             :                 /*      LUT calibration is unique per pole in a sub-folder */
    1716             :                 /*      called 'calibration' from the 'metadata' folder */
    1717             :                 /* --------------------------------------------------------------------
    1718             :                  */
    1719             :                 const CPLString osLUTFilePath = CPLFormFilename(
    1720             :                     CPLFormFilename(osPath, CALIBRATION_FOLDER, nullptr),
    1721          96 :                     pszLUTFile, nullptr);
    1722             : 
    1723          64 :                 if (EQUAL(pszLUTType, "Beta Nought") &&
    1724          16 :                     IsValidXMLFile(osLUTFilePath))
    1725             :                 {
    1726          32 :                     poDS->papszExtraFiles =
    1727          16 :                         CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
    1728             : 
    1729             :                     CPLString pszBuf(
    1730          16 :                         FormatCalibration(szBETA0, osMDFilename.c_str()));
    1731          16 :                     CPLFree(pszBeta0LUT);
    1732          16 :                     pszBeta0LUT = VSIStrdup(osLUTFilePath);
    1733             : 
    1734             :                     const char *oldLut =
    1735          16 :                         poDS->GetMetadataItem("BETA_NOUGHT_LUT");
    1736          16 :                     if (oldLut == nullptr)
    1737             :                     {
    1738           8 :                         poDS->SetMetadataItem("BETA_NOUGHT_LUT", osLUTFilePath);
    1739             :                     }
    1740             :                     else
    1741             :                     {
    1742             :                         /* Keep adding LUT file for all bands, should be planty
    1743             :                          * of space */
    1744             :                         char *ptrConcatLut =
    1745           8 :                             static_cast<char *>(CPLMalloc(2048));
    1746           8 :                         ptrConcatLut[0] =
    1747             :                             '\0'; /* Just initialize the first byte */
    1748           8 :                         strcat(ptrConcatLut, oldLut);
    1749           8 :                         strcat(ptrConcatLut, ",");
    1750           8 :                         strcat(ptrConcatLut, osLUTFilePath);
    1751           8 :                         poDS->SetMetadataItem("BETA_NOUGHT_LUT", ptrConcatLut);
    1752           8 :                         CPLFree(ptrConcatLut);
    1753             :                     }
    1754             : 
    1755          32 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1756          16 :                         poDS->papszSubDatasets, "SUBDATASET_3_NAME", pszBuf);
    1757          16 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1758          16 :                         poDS->papszSubDatasets, "SUBDATASET_3_DESC",
    1759             :                         "Beta Nought calibrated");
    1760             :                 }
    1761          48 :                 else if (EQUAL(pszLUTType, "Sigma Nought") &&
    1762          16 :                          IsValidXMLFile(osLUTFilePath))
    1763             :                 {
    1764          32 :                     poDS->papszExtraFiles =
    1765          16 :                         CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
    1766             : 
    1767             :                     CPLString pszBuf(
    1768          16 :                         FormatCalibration(szSIGMA0, osMDFilename.c_str()));
    1769          16 :                     CPLFree(pszSigma0LUT);
    1770          16 :                     pszSigma0LUT = VSIStrdup(osLUTFilePath);
    1771             : 
    1772             :                     const char *oldLut =
    1773          16 :                         poDS->GetMetadataItem("SIGMA_NOUGHT_LUT");
    1774          16 :                     if (oldLut == nullptr)
    1775             :                     {
    1776          16 :                         poDS->SetMetadataItem("SIGMA_NOUGHT_LUT",
    1777           8 :                                               osLUTFilePath);
    1778             :                     }
    1779             :                     else
    1780             :                     {
    1781             :                         /* Keep adding LUT file for all bands, should be planty
    1782             :                          * of space */
    1783             :                         char *ptrConcatLut =
    1784           8 :                             static_cast<char *>(CPLMalloc(2048));
    1785           8 :                         ptrConcatLut[0] =
    1786             :                             '\0'; /* Just initialize the first byte */
    1787           8 :                         strcat(ptrConcatLut, oldLut);
    1788           8 :                         strcat(ptrConcatLut, ",");
    1789           8 :                         strcat(ptrConcatLut, osLUTFilePath);
    1790           8 :                         poDS->SetMetadataItem("SIGMA_NOUGHT_LUT", ptrConcatLut);
    1791           8 :                         CPLFree(ptrConcatLut);
    1792             :                     }
    1793             : 
    1794          32 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1795          16 :                         poDS->papszSubDatasets, "SUBDATASET_2_NAME", pszBuf);
    1796          16 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1797          16 :                         poDS->papszSubDatasets, "SUBDATASET_2_DESC",
    1798             :                         "Sigma Nought calibrated");
    1799             :                 }
    1800          32 :                 else if (EQUAL(pszLUTType, "Gamma") &&
    1801          16 :                          IsValidXMLFile(osLUTFilePath))
    1802             :                 {
    1803          32 :                     poDS->papszExtraFiles =
    1804          16 :                         CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
    1805             : 
    1806             :                     CPLString pszBuf(
    1807          16 :                         FormatCalibration(szGAMMA, osMDFilename.c_str()));
    1808          16 :                     CPLFree(pszGammaLUT);
    1809          16 :                     pszGammaLUT = VSIStrdup(osLUTFilePath);
    1810             : 
    1811          16 :                     const char *oldLut = poDS->GetMetadataItem("GAMMA_LUT");
    1812          16 :                     if (oldLut == nullptr)
    1813             :                     {
    1814           8 :                         poDS->SetMetadataItem("GAMMA_LUT", osLUTFilePath);
    1815             :                     }
    1816             :                     else
    1817             :                     {
    1818             :                         /* Keep adding LUT file for all bands, should be planty
    1819             :                          * of space */
    1820             :                         char *ptrConcatLut =
    1821           8 :                             static_cast<char *>(CPLMalloc(2048));
    1822           8 :                         ptrConcatLut[0] =
    1823             :                             '\0'; /* Just initialize the first byte */
    1824           8 :                         strcat(ptrConcatLut, oldLut);
    1825           8 :                         strcat(ptrConcatLut, ",");
    1826           8 :                         strcat(ptrConcatLut, osLUTFilePath);
    1827           8 :                         poDS->SetMetadataItem("GAMMA_LUT", ptrConcatLut);
    1828           8 :                         CPLFree(ptrConcatLut);
    1829             :                     }
    1830             : 
    1831          32 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1832          16 :                         poDS->papszSubDatasets, "SUBDATASET_4_NAME", pszBuf);
    1833          16 :                     poDS->papszSubDatasets = CSLSetNameValue(
    1834          16 :                         poDS->papszSubDatasets, "SUBDATASET_4_DESC",
    1835             :                         "Gamma calibrated");
    1836             :                 }
    1837             :             }
    1838             :         }
    1839             : 
    1840             :         /* --------------------------------------------------------------------
    1841             :          */
    1842             :         /*      Fetch ipdf image. Could be either tif or ntf. */
    1843             :         /*      Replace / by \\ */
    1844             :         /* --------------------------------------------------------------------
    1845             :          */
    1846             :         const char *pszBasedFilename;
    1847          16 :         if (bIsNITF)
    1848             :         {
    1849           0 :             pszBasedFilename = pszNITF_Filename;
    1850             :         }
    1851             :         else
    1852             :         {
    1853          16 :             const int bandPositionIndex = imageBandList.FindString(pszPole);
    1854          16 :             if (bandPositionIndex < 0)
    1855             :             {
    1856           0 :                 CPLFree(imageBandList);
    1857           0 :                 CPLFree(imageBandFileList);
    1858             : 
    1859           0 :                 CPLError(CE_Failure, CPLE_OpenFailed,
    1860             :                          "ERROR: RCM cannot find the polarization %s. Please "
    1861             :                          "contact your data provider for a corrected dataset",
    1862             :                          pszPole.c_str());
    1863           0 :                 return nullptr;
    1864             :             }
    1865             : 
    1866          16 :             pszBasedFilename = imageBandFileList[bandPositionIndex];
    1867             :         }
    1868             : 
    1869             :         /* --------------------------------------------------------------------
    1870             :          */
    1871             :         /*      Form full filename (path of product.xml + basename). */
    1872             :         /* --------------------------------------------------------------------
    1873             :          */
    1874             :         char *pszFullname =
    1875          16 :             CPLStrdup(CPLFormFilename(osPath, pszBasedFilename, nullptr));
    1876             : 
    1877             :         /* --------------------------------------------------------------------
    1878             :          */
    1879             :         /*      Try and open the file. */
    1880             :         /* --------------------------------------------------------------------
    1881             :          */
    1882             :         auto poBandFile = std::unique_ptr<GDALDataset>(GDALDataset::Open(
    1883          16 :             pszFullname, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR));
    1884          16 :         if (poBandFile == nullptr)
    1885             :         {
    1886           0 :             CPLFree(pszFullname);
    1887           0 :             continue;
    1888             :         }
    1889          16 :         if (poBandFile->GetRasterCount() == 0)
    1890             :         {
    1891           0 :             CPLFree(pszFullname);
    1892           0 :             continue;
    1893             :         }
    1894             : 
    1895          32 :         poDS->papszExtraFiles =
    1896          16 :             CSLAddString(poDS->papszExtraFiles, pszFullname);
    1897             : 
    1898             :         /* Some CFloat32 NITF files have nBitsPerSample incorrectly reported */
    1899             :         /* as 16, and get misinterpreted as CInt16.  Check the underlying NITF
    1900             :          */
    1901             :         /* and override if this is the case. */
    1902          16 :         if (poBandFile->GetRasterBand(1)->GetRasterDataType() == GDT_CFloat32)
    1903           0 :             eDataType = GDT_CFloat32;
    1904             : 
    1905             :         BandMappingRCM b =
    1906          16 :             checkBandFileMappingRCM(eDataType, poBandFile.get(), bIsNITF);
    1907          16 :         if (b == BANDERROR)
    1908             :         {
    1909           0 :             CPLFree(pszFullname);
    1910           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    1911             :                      "The underlying band files do not have an appropriate "
    1912             :                      "data type.");
    1913           0 :             return nullptr;
    1914             :         }
    1915          16 :         bool twoBandComplex = b == TWOBANDCOMPLEX;
    1916          16 :         bool isOneFilePerPol = (imageBandCount == imageBandFileCount);
    1917             : 
    1918             :         /* --------------------------------------------------------------------
    1919             :          */
    1920             :         /*      Create the band. */
    1921             :         /* --------------------------------------------------------------------
    1922             :          */
    1923          16 :         int bandNum = poDS->GetRasterCount() + 1;
    1924          16 :         if (eCalib == None || eCalib == Uncalib)
    1925             :         {
    1926             :             RCMRasterBand *poBand = new RCMRasterBand(
    1927          20 :                 poDS.get(), bandNum, eDataType, pszPole, poBandFile.release(),
    1928          20 :                 twoBandComplex, isOneFilePerPol, bIsNITF);
    1929             : 
    1930          10 :             poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
    1931             :         }
    1932             :         else
    1933             :         {
    1934           6 :             const char *pszLUT = nullptr;
    1935           6 :             switch (eCalib)
    1936             :             {
    1937           2 :                 case Sigma0:
    1938           2 :                     pszLUT = pszSigma0LUT;
    1939           2 :                     break;
    1940           2 :                 case Beta0:
    1941           2 :                     pszLUT = pszBeta0LUT;
    1942           2 :                     break;
    1943           2 :                 case Gamma:
    1944           2 :                     pszLUT = pszGammaLUT;
    1945           2 :                     break;
    1946           0 :                 default:
    1947             :                     /* we should bomb gracefully... */
    1948           0 :                     pszLUT = pszSigma0LUT;
    1949             :             }
    1950           6 :             if (!pszLUT)
    1951             :             {
    1952           0 :                 CPLFree(pszFullname);
    1953           0 :                 CPLError(CE_Failure, CPLE_AppDefined, "LUT missing.");
    1954           0 :                 return nullptr;
    1955             :             }
    1956             : 
    1957             :             // The variable 'osNoiseLevelsValues' is always the same for a ban
    1958             :             // name except the XML contains different calibration name
    1959           6 :             if (poDS->isComplexData)
    1960             :             {
    1961             :                 // If Complex, always 32 bits
    1962             :                 RCMCalibRasterBand *poBand = new RCMCalibRasterBand(
    1963           0 :                     poDS.get(), pszPole, GDT_Float32, poBandFile.release(),
    1964           0 :                     eCalib, pszLUT, osNoiseLevelsValues.c_str(), eDataType);
    1965           0 :                 poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
    1966             :             }
    1967             :             else
    1968             :             {
    1969             :                 // Whatever the datatype was previoulsy set
    1970             :                 RCMCalibRasterBand *poBand = new RCMCalibRasterBand(
    1971          12 :                     poDS.get(), pszPole, eDataType, poBandFile.release(),
    1972          12 :                     eCalib, pszLUT, osNoiseLevelsValues.c_str(), eDataType);
    1973           6 :                 poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
    1974             :             }
    1975             :         }
    1976             : 
    1977          16 :         CPLFree(pszFullname);
    1978             :     }
    1979             : 
    1980           8 :     if (poDS->papszSubDatasets != nullptr && eCalib == None)
    1981             :     {
    1982           4 :         CPLString pszBuf = FormatCalibration(szUNCALIB, osMDFilename.c_str());
    1983           4 :         poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
    1984             :                                                  "SUBDATASET_1_NAME", pszBuf);
    1985           4 :         poDS->papszSubDatasets =
    1986           4 :             CSLSetNameValue(poDS->papszSubDatasets, "SUBDATASET_1_DESC",
    1987             :                             "Uncalibrated digital numbers");
    1988             :     }
    1989           4 :     else if (poDS->papszSubDatasets != nullptr)
    1990             :     {
    1991           4 :         CSLDestroy(poDS->papszSubDatasets);
    1992           4 :         poDS->papszSubDatasets = nullptr;
    1993             :     }
    1994             : 
    1995             :     /* -------------------------------------------------------------------- */
    1996             :     /*      Set the appropriate MATRIX_REPRESENTATION.                      */
    1997             :     /* -------------------------------------------------------------------- */
    1998             : 
    1999           8 :     if (poDS->GetRasterCount() == 4 &&
    2000           0 :         (eDataType == GDT_CInt16 || eDataType == GDT_CFloat32))
    2001             :     {
    2002           0 :         poDS->SetMetadataItem("MATRIX_REPRESENTATION", "SCATTERING");
    2003             :     }
    2004             : 
    2005             :     /* -------------------------------------------------------------------- */
    2006             :     /*      Collect a few useful metadata items.                            */
    2007             :     /* -------------------------------------------------------------------- */
    2008             : 
    2009           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "satellite", "");
    2010           8 :     poDS->SetMetadataItem("SATELLITE_IDENTIFIER", pszItem);
    2011             : 
    2012           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "sensor", "");
    2013           8 :     poDS->SetMetadataItem("SENSOR_IDENTIFIER", pszItem);
    2014             : 
    2015             :     /* Get beam mode mnemonic */
    2016           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "beamMode", "UNK");
    2017           8 :     poDS->SetMetadataItem("BEAM_MODE", pszItem);
    2018             : 
    2019           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "beamModeMnemonic", "UNK");
    2020           8 :     poDS->SetMetadataItem("BEAM_MODE_MNEMONIC", pszItem);
    2021             : 
    2022           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "beamModeDefinitionId", "UNK");
    2023           8 :     poDS->SetMetadataItem("BEAM_MODE_DEFINITION_ID", pszItem);
    2024             : 
    2025           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "rawDataStartTime", "UNK");
    2026           8 :     poDS->SetMetadataItem("ACQUISITION_START_TIME", pszItem);
    2027             : 
    2028           8 :     pszItem = CPLGetXMLValue(psSourceAttrs, "inputDatasetFacilityId", "UNK");
    2029           8 :     poDS->SetMetadataItem("FACILITY_IDENTIFIER", pszItem);
    2030             : 
    2031           8 :     pszItem = CPLGetXMLValue(psSourceAttrs,
    2032             :                              "orbitAndAttitude.orbitInformation.passDirection",
    2033             :                              "UNK");
    2034           8 :     poDS->SetMetadataItem("ORBIT_DIRECTION", pszItem);
    2035           8 :     pszItem = CPLGetXMLValue(
    2036             :         psSourceAttrs, "orbitAndAttitude.orbitInformation.orbitDataSource",
    2037             :         "UNK");
    2038           8 :     poDS->SetMetadataItem("ORBIT_DATA_SOURCE", pszItem);
    2039           8 :     pszItem = CPLGetXMLValue(
    2040             :         psSourceAttrs, "orbitAndAttitude.orbitInformation.orbitDataFileName",
    2041             :         "UNK");
    2042           8 :     poDS->SetMetadataItem("ORBIT_DATA_FILE", pszItem);
    2043             : 
    2044             :     /* Get incidence angle information. DONE */
    2045           8 :     pszItem = CPLGetXMLValue(psSceneAttributes, "imageAttributes.incAngNearRng",
    2046             :                              "UNK");
    2047           8 :     poDS->SetMetadataItem("NEAR_RANGE_INCIDENCE_ANGLE", pszItem);
    2048             : 
    2049           8 :     pszItem = CPLGetXMLValue(psSceneAttributes, "imageAttributes.incAngFarRng",
    2050             :                              "UNK");
    2051           8 :     poDS->SetMetadataItem("FAR_RANGE_INCIDENCE_ANGLE", pszItem);
    2052             : 
    2053           8 :     pszItem = CPLGetXMLValue(psSceneAttributes,
    2054             :                              "imageAttributes.slantRangeNearEdge", "UNK");
    2055           8 :     poDS->SetMetadataItem("SLANT_RANGE_NEAR_EDGE", pszItem);
    2056             : 
    2057           8 :     pszItem = CPLGetXMLValue(psSceneAttributes,
    2058             :                              "imageAttributes.slantRangeFarEdge", "UNK");
    2059           8 :     poDS->SetMetadataItem("SLANT_RANGE_FAR_EDGE", pszItem);
    2060             : 
    2061             :     /*--------------------------------------------------------------------- */
    2062             :     /*      Collect Map projection/Geotransform information, if present.DONE */
    2063             :     /*      In RCM, there is no file that indicates                         */
    2064             :     /* -------------------------------------------------------------------- */
    2065           8 :     CPLXMLNode *psMapProjection = CPLGetXMLNode(
    2066             :         psImageReferenceAttributes, "geographicInformation.mapProjection");
    2067             : 
    2068           8 :     if (psMapProjection != nullptr)
    2069             :     {
    2070             :         CPLXMLNode *psPos =
    2071           0 :             CPLGetXMLNode(psMapProjection, "positioningInformation");
    2072             : 
    2073             :         pszItem =
    2074           0 :             CPLGetXMLValue(psMapProjection, "mapProjectionDescriptor", "UNK");
    2075           0 :         poDS->SetMetadataItem("MAP_PROJECTION_DESCRIPTOR", pszItem);
    2076             : 
    2077             :         pszItem =
    2078           0 :             CPLGetXMLValue(psMapProjection, "mapProjectionOrientation", "UNK");
    2079           0 :         poDS->SetMetadataItem("MAP_PROJECTION_ORIENTATION", pszItem);
    2080             : 
    2081           0 :         pszItem = CPLGetXMLValue(psMapProjection, "resamplingKernel", "UNK");
    2082           0 :         poDS->SetMetadataItem("RESAMPLING_KERNEL", pszItem);
    2083             : 
    2084           0 :         pszItem = CPLGetXMLValue(psMapProjection, "satelliteHeading", "UNK");
    2085           0 :         poDS->SetMetadataItem("SATELLITE_HEADING", pszItem);
    2086             : 
    2087           0 :         if (psPos != nullptr)
    2088             :         {
    2089           0 :             const double tl_x = CPLStrtod(
    2090             :                 CPLGetXMLValue(psPos, "upperLeftCorner.mapCoordinate.easting",
    2091             :                                "0.0"),
    2092             :                 nullptr);
    2093           0 :             const double tl_y = CPLStrtod(
    2094             :                 CPLGetXMLValue(psPos, "upperLeftCorner.mapCoordinate.northing",
    2095             :                                "0.0"),
    2096             :                 nullptr);
    2097           0 :             const double bl_x = CPLStrtod(
    2098             :                 CPLGetXMLValue(psPos, "lowerLeftCorner.mapCoordinate.easting",
    2099             :                                "0.0"),
    2100             :                 nullptr);
    2101           0 :             const double bl_y = CPLStrtod(
    2102             :                 CPLGetXMLValue(psPos, "lowerLeftCorner.mapCoordinate.northing",
    2103             :                                "0.0"),
    2104             :                 nullptr);
    2105           0 :             const double tr_x = CPLStrtod(
    2106             :                 CPLGetXMLValue(psPos, "upperRightCorner.mapCoordinate.easting",
    2107             :                                "0.0"),
    2108             :                 nullptr);
    2109           0 :             const double tr_y = CPLStrtod(
    2110             :                 CPLGetXMLValue(psPos, "upperRightCorner.mapCoordinate.northing",
    2111             :                                "0.0"),
    2112             :                 nullptr);
    2113           0 :             poDS->adfGeoTransform[1] = (tr_x - tl_x) / (poDS->nRasterXSize - 1);
    2114           0 :             poDS->adfGeoTransform[4] = (tr_y - tl_y) / (poDS->nRasterXSize - 1);
    2115           0 :             poDS->adfGeoTransform[2] = (bl_x - tl_x) / (poDS->nRasterYSize - 1);
    2116           0 :             poDS->adfGeoTransform[5] = (bl_y - tl_y) / (poDS->nRasterYSize - 1);
    2117           0 :             poDS->adfGeoTransform[0] = (tl_x - 0.5 * poDS->adfGeoTransform[1] -
    2118           0 :                                         0.5 * poDS->adfGeoTransform[2]);
    2119           0 :             poDS->adfGeoTransform[3] = (tl_y - 0.5 * poDS->adfGeoTransform[4] -
    2120           0 :                                         0.5 * poDS->adfGeoTransform[5]);
    2121             : 
    2122             :             /* Use bottom right pixel to test geotransform */
    2123           0 :             const double br_x = CPLStrtod(
    2124             :                 CPLGetXMLValue(psPos, "lowerRightCorner.mapCoordinate.easting",
    2125             :                                "0.0"),
    2126             :                 nullptr);
    2127           0 :             const double br_y = CPLStrtod(
    2128             :                 CPLGetXMLValue(psPos, "lowerRightCorner.mapCoordinate.northing",
    2129             :                                "0.0"),
    2130             :                 nullptr);
    2131             :             const double testx =
    2132           0 :                 poDS->adfGeoTransform[0] +
    2133           0 :                 poDS->adfGeoTransform[1] * (poDS->nRasterXSize - 0.5) +
    2134           0 :                 poDS->adfGeoTransform[2] * (poDS->nRasterYSize - 0.5);
    2135             :             const double testy =
    2136           0 :                 poDS->adfGeoTransform[3] +
    2137           0 :                 poDS->adfGeoTransform[4] * (poDS->nRasterXSize - 0.5) +
    2138           0 :                 poDS->adfGeoTransform[5] * (poDS->nRasterYSize - 0.5);
    2139             : 
    2140             :             /* Give 1/4 pixel numerical error leeway in calculating location
    2141             :             based on affine transform */
    2142           0 :             if ((fabs(testx - br_x) >
    2143           0 :                  fabs(0.25 *
    2144           0 :                       (poDS->adfGeoTransform[1] + poDS->adfGeoTransform[2]))) ||
    2145           0 :                 (fabs(testy - br_y) > fabs(0.25 * (poDS->adfGeoTransform[4] +
    2146           0 :                                                    poDS->adfGeoTransform[5]))))
    2147             :             {
    2148           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    2149             :                          "WARNING: Unexpected error in calculating affine "
    2150             :                          "transform: corner coordinates inconsistent.");
    2151             :             }
    2152             :             else
    2153             :             {
    2154           0 :                 poDS->bHaveGeoTransform = TRUE;
    2155             :             }
    2156             :         }
    2157             :     }
    2158             : 
    2159             :     /* -------------------------------------------------------------------- */
    2160             :     /*      Collect Projection String Information.DONE                      */
    2161             :     /* -------------------------------------------------------------------- */
    2162             :     CPLXMLNode *psEllipsoid =
    2163           8 :         CPLGetXMLNode(psImageReferenceAttributes,
    2164             :                       "geographicInformation.ellipsoidParameters");
    2165             : 
    2166           8 :     if (psEllipsoid != nullptr)
    2167             :     {
    2168          16 :         OGRSpatialReference oLL, oPrj;
    2169             : 
    2170             :         const char *pszGeodeticTerrainHeight =
    2171           8 :             CPLGetXMLValue(psEllipsoid, "geodeticTerrainHeight", "UNK");
    2172           8 :         poDS->SetMetadataItem("GEODETIC_TERRAIN_HEIGHT",
    2173           8 :                               pszGeodeticTerrainHeight);
    2174             : 
    2175             :         const char *pszEllipsoidName =
    2176           8 :             CPLGetXMLValue(psEllipsoid, "ellipsoidName", "");
    2177             :         double minor_axis =
    2178           8 :             CPLAtof(CPLGetXMLValue(psEllipsoid, "semiMinorAxis", "0.0"));
    2179             :         double major_axis =
    2180           8 :             CPLAtof(CPLGetXMLValue(psEllipsoid, "semiMajorAxis", "0.0"));
    2181             : 
    2182           8 :         if (EQUAL(pszEllipsoidName, "") || (minor_axis == 0.0) ||
    2183             :             (major_axis == 0.0))
    2184             :         {
    2185           0 :             oLL.SetWellKnownGeogCS("WGS84");
    2186           0 :             oPrj.SetWellKnownGeogCS("WGS84");
    2187             : 
    2188           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2189             :                      "WARNING: Incomplete ellipsoid "
    2190             :                      "information.  Using wgs-84 parameters.");
    2191             :         }
    2192           8 :         else if (EQUAL(pszEllipsoidName, "WGS84") ||
    2193           8 :                  EQUAL(pszEllipsoidName, "WGS 1984"))
    2194             :         {
    2195           8 :             oLL.SetWellKnownGeogCS("WGS84");
    2196           8 :             oPrj.SetWellKnownGeogCS("WGS84");
    2197             :         }
    2198             :         else
    2199             :         {
    2200           0 :             const double inv_flattening =
    2201           0 :                 major_axis / (major_axis - minor_axis);
    2202           0 :             oLL.SetGeogCS("", "", pszEllipsoidName, major_axis, inv_flattening);
    2203           0 :             oPrj.SetGeogCS("", "", pszEllipsoidName, major_axis,
    2204             :                            inv_flattening);
    2205             :         }
    2206             : 
    2207           8 :         if (psMapProjection != nullptr)
    2208             :         {
    2209             :             const char *pszProj =
    2210           0 :                 CPLGetXMLValue(psMapProjection, "mapProjectionDescriptor", "");
    2211           0 :             bool bUseProjInfo = false;
    2212             : 
    2213             :             CPLXMLNode *psUtmParams =
    2214           0 :                 CPLGetXMLNode(psMapProjection, "utmProjectionParameters");
    2215             : 
    2216             :             CPLXMLNode *psNspParams =
    2217           0 :                 CPLGetXMLNode(psMapProjection, "nspProjectionParameters");
    2218             : 
    2219           0 :             if ((psUtmParams != nullptr) && poDS->bHaveGeoTransform)
    2220             :             {
    2221             :                 /* double origEasting, origNorthing; */
    2222           0 :                 bool bNorth = true;
    2223             : 
    2224             :                 const int utmZone =
    2225           0 :                     atoi(CPLGetXMLValue(psUtmParams, "utmZone", ""));
    2226             :                 const char *pszHemisphere =
    2227           0 :                     CPLGetXMLValue(psUtmParams, "hemisphere", "");
    2228             : #if 0
    2229             :                 origEasting = CPLStrtod(CPLGetXMLValue(
    2230             :                     psUtmParams, "mapOriginFalseEasting", "0.0"), nullptr);
    2231             :                 origNorthing = CPLStrtod(CPLGetXMLValue(
    2232             :                     psUtmParams, "mapOriginFalseNorthing", "0.0"), nullptr);
    2233             : #endif
    2234           0 :                 if (STARTS_WITH_CI(pszHemisphere, "southern"))
    2235           0 :                     bNorth = FALSE;
    2236             : 
    2237           0 :                 if (STARTS_WITH_CI(pszProj, "UTM"))
    2238             :                 {
    2239           0 :                     oPrj.SetUTM(utmZone, bNorth);
    2240           0 :                     bUseProjInfo = true;
    2241             :                 }
    2242             :             }
    2243           0 :             else if ((psNspParams != nullptr) && poDS->bHaveGeoTransform)
    2244             :             {
    2245           0 :                 const double origEasting = CPLStrtod(
    2246             :                     CPLGetXMLValue(psNspParams, "mapOriginFalseEasting", "0.0"),
    2247             :                     nullptr);
    2248             :                 const double origNorthing =
    2249           0 :                     CPLStrtod(CPLGetXMLValue(psNspParams,
    2250             :                                              "mapOriginFalseNorthing", "0.0"),
    2251             :                               nullptr);
    2252           0 :                 const double copLong = CPLStrtod(
    2253             :                     CPLGetXMLValue(psNspParams, "centerOfProjectionLongitude",
    2254             :                                    "0.0"),
    2255             :                     nullptr);
    2256           0 :                 const double copLat = CPLStrtod(
    2257             :                     CPLGetXMLValue(psNspParams, "centerOfProjectionLatitude",
    2258             :                                    "0.0"),
    2259             :                     nullptr);
    2260           0 :                 const double sP1 = CPLStrtod(
    2261             :                     CPLGetXMLValue(psNspParams, "standardParallels1", "0.0"),
    2262             :                     nullptr);
    2263           0 :                 const double sP2 = CPLStrtod(
    2264             :                     CPLGetXMLValue(psNspParams, "standardParallels2", "0.0"),
    2265             :                     nullptr);
    2266             : 
    2267           0 :                 if (STARTS_WITH_CI(pszProj, "ARC"))
    2268             :                 {
    2269             :                     /* Albers Conical Equal Area */
    2270           0 :                     oPrj.SetACEA(sP1, sP2, copLat, copLong, origEasting,
    2271             :                                  origNorthing);
    2272           0 :                     bUseProjInfo = true;
    2273             :                 }
    2274           0 :                 else if (STARTS_WITH_CI(pszProj, "LCC"))
    2275             :                 {
    2276             :                     /* Lambert Conformal Conic */
    2277           0 :                     oPrj.SetLCC(sP1, sP2, copLat, copLong, origEasting,
    2278             :                                 origNorthing);
    2279           0 :                     bUseProjInfo = true;
    2280             :                 }
    2281           0 :                 else if (STARTS_WITH_CI(pszProj, "STPL"))
    2282             :                 {
    2283             :                     /* State Plate
    2284             :                     ASSUMPTIONS: "zone" in product.xml matches USGS
    2285             :                     definition as expected by ogr spatial reference; NAD83
    2286             :                     zones (versus NAD27) are assumed. */
    2287             : 
    2288             :                     const int nSPZone =
    2289           0 :                         atoi(CPLGetXMLValue(psNspParams, "zone", "1"));
    2290             : 
    2291           0 :                     oPrj.SetStatePlane(nSPZone, TRUE, nullptr, 0.0);
    2292           0 :                     bUseProjInfo = true;
    2293             :                 }
    2294             :             }
    2295             : 
    2296           0 :             if (bUseProjInfo)
    2297             :             {
    2298           0 :                 poDS->m_oSRS = std::move(oPrj);
    2299             :             }
    2300             :             else
    2301             :             {
    2302           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    2303             :                          "WARNING: Unable to interpret projection information; "
    2304             :                          "check mapProjection info in product.xml!");
    2305             :             }
    2306             :         }
    2307             : 
    2308           8 :         poDS->m_oGCPSRS = std::move(oLL);
    2309             :     }
    2310             : 
    2311             :     /* -------------------------------------------------------------------- */
    2312             :     /*      Collect GCPs.DONE */
    2313             :     /* -------------------------------------------------------------------- */
    2314           8 :     CPLXMLNode *psGeoGrid = CPLGetXMLNode(
    2315             :         psImageReferenceAttributes, "geographicInformation.geolocationGrid");
    2316             : 
    2317           8 :     if (psGeoGrid != nullptr)
    2318             :     {
    2319             :         /* count GCPs */
    2320           8 :         poDS->nGCPCount = 0;
    2321             : 
    2322         136 :         for (psNode = psGeoGrid->psChild; psNode != nullptr;
    2323         128 :              psNode = psNode->psNext)
    2324             :         {
    2325         128 :             if (EQUAL(psNode->pszValue, "imageTiePoint"))
    2326         128 :                 poDS->nGCPCount++;
    2327             :         }
    2328             : 
    2329          16 :         poDS->pasGCPList = reinterpret_cast<GDAL_GCP *>(
    2330           8 :             CPLCalloc(sizeof(GDAL_GCP), poDS->nGCPCount));
    2331             : 
    2332           8 :         poDS->nGCPCount = 0;
    2333             : 
    2334         136 :         for (psNode = psGeoGrid->psChild; psNode != nullptr;
    2335         128 :              psNode = psNode->psNext)
    2336             :         {
    2337         128 :             GDAL_GCP *psGCP = poDS->pasGCPList + poDS->nGCPCount;
    2338             : 
    2339         128 :             if (!EQUAL(psNode->pszValue, "imageTiePoint"))
    2340           0 :                 continue;
    2341             : 
    2342         128 :             poDS->nGCPCount++;
    2343             : 
    2344             :             char szID[32];
    2345         128 :             snprintf(szID, sizeof(szID), "%d", poDS->nGCPCount);
    2346         128 :             psGCP->pszId = CPLStrdup(szID);
    2347         128 :             psGCP->pszInfo = CPLStrdup("");
    2348         128 :             psGCP->dfGCPPixel =
    2349         128 :                 CPLAtof(CPLGetXMLValue(psNode, "imageCoordinate.pixel", "0"));
    2350         128 :             psGCP->dfGCPLine =
    2351         128 :                 CPLAtof(CPLGetXMLValue(psNode, "imageCoordinate.line", "0"));
    2352         128 :             psGCP->dfGCPX = CPLAtof(
    2353             :                 CPLGetXMLValue(psNode, "geodeticCoordinate.longitude", ""));
    2354         128 :             psGCP->dfGCPY = CPLAtof(
    2355             :                 CPLGetXMLValue(psNode, "geodeticCoordinate.latitude", ""));
    2356         128 :             psGCP->dfGCPZ = CPLAtof(
    2357             :                 CPLGetXMLValue(psNode, "geodeticCoordinate.height", ""));
    2358             :         }
    2359             :     }
    2360             : 
    2361           8 :     if (pszBeta0LUT)
    2362           8 :         CPLFree(pszBeta0LUT);
    2363           8 :     if (pszSigma0LUT)
    2364           8 :         CPLFree(pszSigma0LUT);
    2365           8 :     if (pszGammaLUT)
    2366           8 :         CPLFree(pszGammaLUT);
    2367             : 
    2368             :     /* -------------------------------------------------------------------- */
    2369             :     /*      Collect RPC.DONE */
    2370             :     /* -------------------------------------------------------------------- */
    2371           8 :     CPLXMLNode *psRationalFunctions = CPLGetXMLNode(
    2372             :         psImageReferenceAttributes, "geographicInformation.rationalFunctions");
    2373           8 :     if (psRationalFunctions != nullptr)
    2374             :     {
    2375           8 :         char **papszRPC = nullptr;
    2376             :         static const char *const apszXMLToGDALMapping[] = {
    2377             :             "biasError",
    2378             :             "ERR_BIAS",
    2379             :             "randomError",
    2380             :             "ERR_RAND",
    2381             :             //"lineFitQuality", "????",
    2382             :             //"pixelFitQuality", "????",
    2383             :             "lineOffset",
    2384             :             "LINE_OFF",
    2385             :             "pixelOffset",
    2386             :             "SAMP_OFF",
    2387             :             "latitudeOffset",
    2388             :             "LAT_OFF",
    2389             :             "longitudeOffset",
    2390             :             "LONG_OFF",
    2391             :             "heightOffset",
    2392             :             "HEIGHT_OFF",
    2393             :             "lineScale",
    2394             :             "LINE_SCALE",
    2395             :             "pixelScale",
    2396             :             "SAMP_SCALE",
    2397             :             "latitudeScale",
    2398             :             "LAT_SCALE",
    2399             :             "longitudeScale",
    2400             :             "LONG_SCALE",
    2401             :             "heightScale",
    2402             :             "HEIGHT_SCALE",
    2403             :             "lineNumeratorCoefficients",
    2404             :             "LINE_NUM_COEFF",
    2405             :             "lineDenominatorCoefficients",
    2406             :             "LINE_DEN_COEFF",
    2407             :             "pixelNumeratorCoefficients",
    2408             :             "SAMP_NUM_COEFF",
    2409             :             "pixelDenominatorCoefficients",
    2410             :             "SAMP_DEN_COEFF",
    2411             :         };
    2412         136 :         for (size_t i = 0; i < CPL_ARRAYSIZE(apszXMLToGDALMapping); i += 2)
    2413             :         {
    2414         256 :             const char *pszValue = CPLGetXMLValue(
    2415         128 :                 psRationalFunctions, apszXMLToGDALMapping[i], nullptr);
    2416         128 :             if (pszValue)
    2417         128 :                 papszRPC = CSLSetNameValue(
    2418         128 :                     papszRPC, apszXMLToGDALMapping[i + 1], pszValue);
    2419             :         }
    2420           8 :         poDS->GDALDataset::SetMetadata(papszRPC, "RPC");
    2421           8 :         CSLDestroy(papszRPC);
    2422             :     }
    2423             : 
    2424             :     /* -------------------------------------------------------------------- */
    2425             :     /*      Initialize any PAM information.                                 */
    2426             :     /* -------------------------------------------------------------------- */
    2427          16 :     CPLString osDescription;
    2428          16 :     CPLString osSubdatasetName;
    2429           8 :     bool useSubdatasets = true;
    2430             : 
    2431           8 :     switch (eCalib)
    2432             :     {
    2433           1 :         case Sigma0:
    2434             :         {
    2435           1 :             osSubdatasetName = szSIGMA0;
    2436           1 :             osDescription = FormatCalibration(szSIGMA0, osMDFilename.c_str());
    2437             :         }
    2438           1 :         break;
    2439           1 :         case Beta0:
    2440             :         {
    2441           1 :             osSubdatasetName = szBETA0;
    2442           1 :             osDescription = FormatCalibration(szBETA0, osMDFilename.c_str());
    2443             :         }
    2444           1 :         break;
    2445           1 :         case Gamma:
    2446             :         {
    2447           1 :             osSubdatasetName = szGAMMA;
    2448           1 :             osDescription = FormatCalibration(szGAMMA, osMDFilename.c_str());
    2449             :         }
    2450           1 :         break;
    2451           1 :         case Uncalib:
    2452             :         {
    2453           1 :             osSubdatasetName = szUNCALIB;
    2454           1 :             osDescription = FormatCalibration(szUNCALIB, osMDFilename.c_str());
    2455             :         }
    2456           1 :         break;
    2457           4 :         default:
    2458           4 :             osSubdatasetName = szUNCALIB;
    2459           4 :             osDescription = osMDFilename;
    2460           4 :             useSubdatasets = false;
    2461             :     }
    2462             : 
    2463           8 :     CPL_IGNORE_RET_VAL(osSubdatasetName);
    2464             : 
    2465           8 :     if (eCalib != None)
    2466           4 :         poDS->papszExtraFiles =
    2467           4 :             CSLAddString(poDS->papszExtraFiles, osMDFilename);
    2468             : 
    2469             :     /* -------------------------------------------------------------------- */
    2470             :     /*      Initialize any PAM information.                                 */
    2471             :     /* -------------------------------------------------------------------- */
    2472           8 :     poDS->SetDescription(osDescription);
    2473             : 
    2474           8 :     poDS->SetPhysicalFilename(osMDFilename);
    2475           8 :     poDS->SetSubdatasetName(osDescription);
    2476             : 
    2477           8 :     poDS->TryLoadXML();
    2478             : 
    2479             :     /* -------------------------------------------------------------------- */
    2480             :     /*      Check for overviews.                                            */
    2481             :     /* -------------------------------------------------------------------- */
    2482           8 :     if (useSubdatasets)
    2483           4 :         poDS->oOvManager.Initialize(poDS.get(), ":::VIRTUAL:::");
    2484             :     else
    2485           4 :         poDS->oOvManager.Initialize(poDS.get(), osMDFilename);
    2486             : 
    2487           8 :     return poDS.release();
    2488             : }
    2489             : 
    2490             : /************************************************************************/
    2491             : /*                            GetGCPCount()                             */
    2492             : /************************************************************************/
    2493             : 
    2494           6 : int RCMDataset::GetGCPCount()
    2495             : 
    2496             : {
    2497           6 :     return nGCPCount;
    2498             : }
    2499             : 
    2500             : /************************************************************************/
    2501             : /*                          GetGCPSpatialRef()                          */
    2502             : /************************************************************************/
    2503             : 
    2504           1 : const OGRSpatialReference *RCMDataset::GetGCPSpatialRef() const
    2505             : 
    2506             : {
    2507           1 :     return m_oGCPSRS.IsEmpty() || nGCPCount == 0 ? nullptr : &m_oGCPSRS;
    2508             : }
    2509             : 
    2510             : /************************************************************************/
    2511             : /*                               GetGCPs()                              */
    2512             : /************************************************************************/
    2513             : 
    2514           5 : const GDAL_GCP *RCMDataset::GetGCPs()
    2515             : 
    2516             : {
    2517           5 :     return pasGCPList;
    2518             : }
    2519             : 
    2520             : /************************************************************************/
    2521             : /*                          GetSpatialRef()                             */
    2522             : /************************************************************************/
    2523             : 
    2524           0 : const OGRSpatialReference *RCMDataset::GetSpatialRef() const
    2525             : 
    2526             : {
    2527           0 :     return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
    2528             : }
    2529             : 
    2530             : /************************************************************************/
    2531             : /*                          GetGeoTransform()                           */
    2532             : /************************************************************************/
    2533             : 
    2534           0 : CPLErr RCMDataset::GetGeoTransform(double *padfTransform)
    2535             : 
    2536             : {
    2537           0 :     memcpy(padfTransform, adfGeoTransform, sizeof(double) * 6);
    2538             : 
    2539           0 :     if (bHaveGeoTransform)
    2540             :     {
    2541           0 :         return CE_None;
    2542             :     }
    2543             : 
    2544           0 :     return CE_Failure;
    2545             : }
    2546             : 
    2547             : /************************************************************************/
    2548             : /*                      GetMetadataDomainList()                         */
    2549             : /************************************************************************/
    2550             : 
    2551           0 : char **RCMDataset::GetMetadataDomainList()
    2552             : {
    2553           0 :     return BuildMetadataDomainList(GDALDataset::GetMetadataDomainList(), TRUE,
    2554           0 :                                    "SUBDATASETS", nullptr);
    2555             : }
    2556             : 
    2557             : /************************************************************************/
    2558             : /*                            GetMetadata()                             */
    2559             : /************************************************************************/
    2560             : 
    2561           2 : char **RCMDataset::GetMetadata(const char *pszDomain)
    2562             : 
    2563             : {
    2564           2 :     if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "SUBDATASETS") &&
    2565           0 :         papszSubDatasets != nullptr)
    2566           0 :         return papszSubDatasets;
    2567             : 
    2568           2 :     return GDALDataset::GetMetadata(pszDomain);
    2569             : }
    2570             : 
    2571             : /************************************************************************/
    2572             : /*                         GDALRegister_RCM()                           */
    2573             : /************************************************************************/
    2574             : 
    2575        1595 : void GDALRegister_RCM()
    2576             : 
    2577             : {
    2578        1595 :     if (GDALGetDriverByName("RCM") != nullptr)
    2579             :     {
    2580         302 :         return;
    2581             :     }
    2582             : 
    2583        1293 :     GDALDriver *poDriver = new GDALDriver();
    2584        1293 :     RCMDriverSetCommonMetadata(poDriver);
    2585             : 
    2586        1293 :     poDriver->pfnOpen = RCMDataset::Open;
    2587             : 
    2588        1293 :     GetGDALDriverManager()->RegisterDriver(poDriver);
    2589             : }

Generated by: LCOV version 1.14