LCOV - code coverage report
Current view: top level - frmts/netcdf - netcdfdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 4958 5863 84.6 %
Date: 2025-06-28 21:28:23 Functions: 156 163 95.7 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  netCDF read/write Driver
       4             :  * Purpose:  GDAL bindings over netCDF library.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *           Even Rouault <even.rouault at spatialys.com>
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2004, Frank Warmerdam
      10             :  * Copyright (c) 2007-2016, Even Rouault <even.rouault at spatialys.com>
      11             :  * Copyright (c) 2010, Kyle Shannon <kyle at pobox dot com>
      12             :  * Copyright (c) 2021, CLS
      13             :  *
      14             :  * SPDX-License-Identifier: MIT
      15             :  ****************************************************************************/
      16             : 
      17             : #include "cpl_port.h"
      18             : 
      19             : #include <array>
      20             : #include <cassert>
      21             : #include <cctype>
      22             : #include <cerrno>
      23             : #include <climits>
      24             : #include <cmath>
      25             : #include <cstdio>
      26             : #include <cstdlib>
      27             : #include <cstring>
      28             : #include <ctime>
      29             : #include <algorithm>
      30             : #include <limits>
      31             : #include <map>
      32             : #include <mutex>
      33             : #include <set>
      34             : #include <queue>
      35             : #include <string>
      36             : #include <tuple>
      37             : #include <utility>
      38             : #include <vector>
      39             : 
      40             : // Must be included after standard includes, otherwise VS2015 fails when
      41             : // including <ctime>
      42             : #include "netcdfdataset.h"
      43             : #include "netcdfdrivercore.h"
      44             : #include "netcdfsg.h"
      45             : #include "netcdfuffd.h"
      46             : 
      47             : #include "netcdf_mem.h"
      48             : 
      49             : #include "cpl_conv.h"
      50             : #include "cpl_error.h"
      51             : #include "cpl_float.h"
      52             : #include "cpl_json.h"
      53             : #include "cpl_minixml.h"
      54             : #include "cpl_multiproc.h"
      55             : #include "cpl_progress.h"
      56             : #include "cpl_time.h"
      57             : #include "gdal.h"
      58             : #include "gdal_frmts.h"
      59             : #include "gdal_priv_templates.hpp"
      60             : #include "ogr_core.h"
      61             : #include "ogr_srs_api.h"
      62             : 
      63             : // netCDF 4.8 switched to expecting filenames in UTF-8 on Windows
      64             : // But with netCDF 4.9 and https://github.com/Unidata/netcdf-c/pull/2277/,
      65             : // this is apparently back to expecting filenames in current codepage...
      66             : // Detect netCDF 4.8 with NC_ENCZARR
      67             : // Detect netCDF 4.9 with NC_NOATTCREORD
      68             : #if defined(NC_ENCZARR) && !defined(NC_NOATTCREORD)
      69             : #define NETCDF_USES_UTF8
      70             : #endif
      71             : 
      72             : // Internal function declarations.
      73             : 
      74             : static bool NCDFIsGDALVersionGTE(const char *pszVersion, int nTarget);
      75             : 
      76             : static void
      77             : NCDFAddGDALHistory(int fpImage, const char *pszFilename, bool bWriteGDALVersion,
      78             :                    bool bWriteGDALHistory, const char *pszOldHist,
      79             :                    const char *pszFunctionName,
      80             :                    const char *pszCFVersion = GDAL_DEFAULT_NCDF_CONVENTIONS);
      81             : 
      82             : static void NCDFAddHistory(int fpImage, const char *pszAddHist,
      83             :                            const char *pszOldHist);
      84             : 
      85             : static CPLErr NCDFSafeStrcat(char **ppszDest, const char *pszSrc,
      86             :                              size_t *nDestSize);
      87             : 
      88             : // Var / attribute helper functions.
      89             : static CPLErr NCDFPutAttr(int nCdfId, int nVarId, const char *pszAttrName,
      90             :                           const char *pszValue);
      91             : 
      92             : // Replace this where used.
      93             : static CPLErr NCDFGet1DVar(int nCdfId, int nVarId, char **pszValue);
      94             : static CPLErr NCDFPut1DVar(int nCdfId, int nVarId, const char *pszValue);
      95             : 
      96             : // Replace this where used.
      97             : static char **NCDFTokenizeArray(const char *pszValue);
      98             : static void CopyMetadata(GDALDataset *poSrcDS, GDALRasterBand *poSrcBand,
      99             :                          GDALRasterBand *poDstBand, int fpImage, int CDFVarID,
     100             :                          const char *pszMatchPrefix = nullptr);
     101             : 
     102             : // NetCDF-4 groups helper functions.
     103             : // They all work also for NetCDF-3 files which are considered as
     104             : // NetCDF-4 file with only one group.
     105             : static CPLErr NCDFOpenSubDataset(int nCdfId, const char *pszSubdatasetName,
     106             :                                  int *pnGroupId, int *pnVarId);
     107             : static CPLErr NCDFGetVisibleDims(int nGroupId, int *pnDims, int **ppanDimIds);
     108             : static CPLErr NCDFGetSubGroups(int nGroupId, int *pnSubGroups,
     109             :                                int **ppanSubGroupIds);
     110             : static CPLErr NCDFGetGroupFullName(int nGroupId, char **ppszFullName,
     111             :                                    bool bNC3Compat = true);
     112             : static CPLErr NCDFGetVarFullName(int nGroupId, int nVarId, char **ppszFullName,
     113             :                                  bool bNC3Compat = true);
     114             : static CPLErr NCDFGetRootGroup(int nStartGroupId, int *pnRootGroupId);
     115             : 
     116             : static CPLErr NCDFResolveVarFullName(int nStartGroupId, const char *pszVar,
     117             :                                      char **ppszFullName,
     118             :                                      bool bMandatory = false);
     119             : static CPLErr NCDFResolveAttInt(int nStartGroupId, int nStartVarId,
     120             :                                 const char *pszAtt, int *pnAtt,
     121             :                                 bool bMandatory = false);
     122             : static CPLErr NCDFGetCoordAndBoundVarFullNames(int nCdfId, char ***ppapszVars);
     123             : 
     124             : // Uncomment this for more debug output.
     125             : // #define NCDF_DEBUG 1
     126             : 
     127             : CPLMutex *hNCMutex = nullptr;
     128             : 
     129             : // Workaround https://github.com/OSGeo/gdal/issues/6253
     130             : // Having 2 netCDF handles on the same file doesn't work in a multi-threaded
     131             : // way. Apparently having the same handle works better (this is OK since
     132             : // we have a global mutex on the netCDF library)
     133             : static std::map<std::string, int> goMapNameToNetCDFId;
     134             : static std::map<int, std::pair<std::string, int>> goMapNetCDFIdToKeyAndCount;
     135             : 
     136         667 : int GDAL_nc_open(const char *pszFilename, int nMode, int *pID)
     137             : {
     138        1334 :     std::string osKey(pszFilename);
     139         667 :     osKey += "#####";
     140         667 :     osKey += std::to_string(nMode);
     141         667 :     auto oIter = goMapNameToNetCDFId.find(osKey);
     142         667 :     if (oIter == goMapNameToNetCDFId.end())
     143             :     {
     144         618 :         int ret = nc_open(pszFilename, nMode, pID);
     145         618 :         if (ret != NC_NOERR)
     146           3 :             return ret;
     147         615 :         goMapNameToNetCDFId[osKey] = *pID;
     148         615 :         goMapNetCDFIdToKeyAndCount[*pID] =
     149        1230 :             std::pair<std::string, int>(osKey, 1);
     150         615 :         return ret;
     151             :     }
     152             :     else
     153             :     {
     154          49 :         *pID = oIter->second;
     155          49 :         goMapNetCDFIdToKeyAndCount[oIter->second].second++;
     156          49 :         return NC_NOERR;
     157             :     }
     158             : }
     159             : 
     160         923 : int GDAL_nc_close(int cdfid)
     161             : {
     162         923 :     int ret = NC_NOERR;
     163         923 :     auto oIter = goMapNetCDFIdToKeyAndCount.find(cdfid);
     164         923 :     if (oIter != goMapNetCDFIdToKeyAndCount.end())
     165             :     {
     166         664 :         if (--oIter->second.second == 0)
     167             :         {
     168         615 :             ret = nc_close(cdfid);
     169         615 :             goMapNameToNetCDFId.erase(oIter->second.first);
     170         615 :             goMapNetCDFIdToKeyAndCount.erase(oIter);
     171             :         }
     172             :     }
     173             :     else
     174             :     {
     175             :         // we can go here if file opened with nc_open_mem() or nc_create()
     176         259 :         ret = nc_close(cdfid);
     177             :     }
     178         923 :     return ret;
     179             : }
     180             : 
     181             : /************************************************************************/
     182             : /* ==================================================================== */
     183             : /*                         netCDFRasterBand                             */
     184             : /* ==================================================================== */
     185             : /************************************************************************/
     186             : 
     187             : class netCDFRasterBand final : public GDALPamRasterBand
     188             : {
     189             :     friend class netCDFDataset;
     190             : 
     191             :     nc_type nc_datatype;
     192             :     int cdfid;
     193             :     int nZId;
     194             :     int nZDim;
     195             :     int nLevel;
     196             :     int nBandXPos;
     197             :     int nBandYPos;
     198             :     int *panBandZPos;
     199             :     int *panBandZLev;
     200             :     bool m_bNoDataSet = false;
     201             :     double m_dfNoDataValue = 0;
     202             :     bool m_bNoDataSetAsInt64 = false;
     203             :     int64_t m_nNodataValueInt64 = 0;
     204             :     bool m_bNoDataSetAsUInt64 = false;
     205             :     uint64_t m_nNodataValueUInt64 = 0;
     206             :     bool bValidRangeValid = false;
     207             :     double adfValidRange[2]{0, 0};
     208             :     bool m_bHaveScale = false;
     209             :     bool m_bHaveOffset = false;
     210             :     double m_dfScale = 1;
     211             :     double m_dfOffset = 0;
     212             :     CPLString m_osUnitType{};
     213             :     bool bSignedData;
     214             :     bool bCheckLongitude;
     215             :     bool m_bCreateMetadataFromOtherVarsDone = false;
     216             : 
     217             :     void CreateMetadataFromAttributes();
     218             :     void CreateMetadataFromOtherVars();
     219             : 
     220             :     template <class T>
     221             :     void CheckData(void *pImage, void *pImageNC, size_t nTmpBlockXSize,
     222             :                    size_t nTmpBlockYSize, bool bCheckIsNan = false);
     223             :     template <class T>
     224             :     void CheckDataCpx(void *pImage, void *pImageNC, size_t nTmpBlockXSize,
     225             :                       size_t nTmpBlockYSize, bool bCheckIsNan = false);
     226             :     void SetBlockSize();
     227             : 
     228             :     bool FetchNetcdfChunk(size_t xstart, size_t ystart, void *pImage);
     229             : 
     230             :     void SetNoDataValueNoUpdate(double dfNoData);
     231             :     void SetNoDataValueNoUpdate(int64_t nNoData);
     232             :     void SetNoDataValueNoUpdate(uint64_t nNoData);
     233             : 
     234             :     void SetOffsetNoUpdate(double dfVal);
     235             :     void SetScaleNoUpdate(double dfVal);
     236             :     void SetUnitTypeNoUpdate(const char *pszNewValue);
     237             : 
     238             :   protected:
     239             :     CPLXMLNode *SerializeToXML(const char *pszUnused) override;
     240             : 
     241             :   public:
     242             :     struct CONSTRUCTOR_OPEN
     243             :     {
     244             :     };
     245             : 
     246             :     struct CONSTRUCTOR_CREATE
     247             :     {
     248             :     };
     249             : 
     250             :     netCDFRasterBand(const CONSTRUCTOR_OPEN &, netCDFDataset *poDS,
     251             :                      int nGroupId, int nZId, int nZDim, int nLevel,
     252             :                      const int *panBandZLen, const int *panBandPos, int nBand);
     253             :     netCDFRasterBand(const CONSTRUCTOR_CREATE &, netCDFDataset *poDS,
     254             :                      GDALDataType eType, int nBand, bool bSigned = true,
     255             :                      const char *pszBandName = nullptr,
     256             :                      const char *pszLongName = nullptr, int nZId = -1,
     257             :                      int nZDim = 2, int nLevel = 0,
     258             :                      const int *panBandZLev = nullptr,
     259             :                      const int *panBandZPos = nullptr,
     260             :                      const int *paDimIds = nullptr);
     261             :     virtual ~netCDFRasterBand();
     262             : 
     263             :     virtual double GetNoDataValue(int *) override;
     264             :     virtual int64_t GetNoDataValueAsInt64(int *pbSuccess = nullptr) override;
     265             :     virtual uint64_t GetNoDataValueAsUInt64(int *pbSuccess = nullptr) override;
     266             :     virtual CPLErr SetNoDataValue(double) override;
     267             :     virtual CPLErr SetNoDataValueAsInt64(int64_t nNoData) override;
     268             :     virtual CPLErr SetNoDataValueAsUInt64(uint64_t nNoData) override;
     269             :     // virtual CPLErr DeleteNoDataValue();
     270             :     virtual double GetOffset(int *) override;
     271             :     virtual CPLErr SetOffset(double) override;
     272             :     virtual double GetScale(int *) override;
     273             :     virtual CPLErr SetScale(double) override;
     274             :     virtual const char *GetUnitType() override;
     275             :     virtual CPLErr SetUnitType(const char *) override;
     276             :     virtual CPLErr IReadBlock(int, int, void *) override;
     277             :     virtual CPLErr IWriteBlock(int, int, void *) override;
     278             : 
     279             :     char **GetMetadata(const char *pszDomain = "") override;
     280             :     const char *GetMetadataItem(const char *pszName,
     281             :                                 const char *pszDomain = "") override;
     282             : 
     283             :     virtual CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
     284             :                                    const char *pszDomain = "") override;
     285             :     virtual CPLErr SetMetadata(char **papszMD,
     286             :                                const char *pszDomain = "") override;
     287             : };
     288             : 
     289             : /************************************************************************/
     290             : /*                          netCDFRasterBand()                          */
     291             : /************************************************************************/
     292             : 
     293         472 : netCDFRasterBand::netCDFRasterBand(const netCDFRasterBand::CONSTRUCTOR_OPEN &,
     294             :                                    netCDFDataset *poNCDFDS, int nGroupId,
     295             :                                    int nZIdIn, int nZDimIn, int nLevelIn,
     296             :                                    const int *panBandZLevIn,
     297         472 :                                    const int *panBandZPosIn, int nBandIn)
     298             :     : nc_datatype(NC_NAT), cdfid(nGroupId), nZId(nZIdIn), nZDim(nZDimIn),
     299         472 :       nLevel(nLevelIn), nBandXPos(panBandZPosIn[0]),
     300         472 :       nBandYPos(nZDim == 1 ? -1 : panBandZPosIn[1]), panBandZPos(nullptr),
     301             :       panBandZLev(nullptr),
     302             :       bSignedData(true),  // Default signed, except for Byte.
     303         944 :       bCheckLongitude(false)
     304             : {
     305         472 :     poDS = poNCDFDS;
     306         472 :     nBand = nBandIn;
     307             : 
     308             :     // Take care of all other dimensions.
     309         472 :     if (nZDim > 2)
     310             :     {
     311         168 :         panBandZPos = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     312         168 :         panBandZLev = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     313             : 
     314         462 :         for (int i = 0; i < nZDim - 2; i++)
     315             :         {
     316         294 :             panBandZPos[i] = panBandZPosIn[i + 2];
     317         294 :             panBandZLev[i] = panBandZLevIn[i];
     318             :         }
     319             :     }
     320             : 
     321         472 :     nRasterXSize = poDS->GetRasterXSize();
     322         472 :     nRasterYSize = poDS->GetRasterYSize();
     323         472 :     nBlockXSize = poDS->GetRasterXSize();
     324         472 :     nBlockYSize = 1;
     325             : 
     326             :     // Get the type of the "z" variable, our target raster array.
     327         472 :     if (nc_inq_var(cdfid, nZId, nullptr, &nc_datatype, nullptr, nullptr,
     328         472 :                    nullptr) != NC_NOERR)
     329             :     {
     330           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Error in nc_var_inq() on 'z'.");
     331           0 :         return;
     332             :     }
     333             : 
     334         472 :     if (NCDFIsUserDefinedType(cdfid, nc_datatype))
     335             :     {
     336             :         // First enquire and check that the number of fields is 2
     337             :         size_t nfields, compoundsize;
     338           5 :         if (nc_inq_compound(cdfid, nc_datatype, nullptr, &compoundsize,
     339           5 :                             &nfields) != NC_NOERR)
     340             :         {
     341           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     342             :                      "Error in nc_inq_compound() on 'z'.");
     343           0 :             return;
     344             :         }
     345             : 
     346           5 :         if (nfields != 2)
     347             :         {
     348           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     349             :                      "Unsupported data type encountered in nc_inq_compound() "
     350             :                      "on 'z'.");
     351           0 :             return;
     352             :         }
     353             : 
     354             :         // Now check that that two types are the same in the struct.
     355             :         nc_type field_type1, field_type2;
     356             :         int field_dims1, field_dims2;
     357           5 :         if (nc_inq_compound_field(cdfid, nc_datatype, 0, nullptr, nullptr,
     358             :                                   &field_type1, &field_dims1,
     359           5 :                                   nullptr) != NC_NOERR)
     360             :         {
     361           0 :             CPLError(
     362             :                 CE_Failure, CPLE_AppDefined,
     363             :                 "Error in querying Field 1 in nc_inq_compound_field() on 'z'.");
     364           0 :             return;
     365             :         }
     366             : 
     367           5 :         if (nc_inq_compound_field(cdfid, nc_datatype, 0, nullptr, nullptr,
     368             :                                   &field_type2, &field_dims2,
     369           5 :                                   nullptr) != NC_NOERR)
     370             :         {
     371           0 :             CPLError(
     372             :                 CE_Failure, CPLE_AppDefined,
     373             :                 "Error in querying Field 2 in nc_inq_compound_field() on 'z'.");
     374           0 :             return;
     375             :         }
     376             : 
     377           5 :         if ((field_type1 != field_type2) || (field_dims1 != field_dims2) ||
     378           5 :             (field_dims1 != 0))
     379             :         {
     380           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     381             :                      "Error in interpreting compound data type on 'z'.");
     382           0 :             return;
     383             :         }
     384             : 
     385           5 :         if (field_type1 == NC_SHORT)
     386           0 :             eDataType = GDT_CInt16;
     387           5 :         else if (field_type1 == NC_INT)
     388           0 :             eDataType = GDT_CInt32;
     389           5 :         else if (field_type1 == NC_FLOAT)
     390           4 :             eDataType = GDT_CFloat32;
     391           1 :         else if (field_type1 == NC_DOUBLE)
     392           1 :             eDataType = GDT_CFloat64;
     393             :         else
     394             :         {
     395           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     396             :                      "Unsupported netCDF compound data type encountered.");
     397           0 :             return;
     398             :         }
     399             :     }
     400             :     else
     401             :     {
     402         467 :         if (nc_datatype == NC_BYTE)
     403         145 :             eDataType = GDT_Byte;
     404         322 :         else if (nc_datatype == NC_CHAR)
     405           0 :             eDataType = GDT_Byte;
     406         322 :         else if (nc_datatype == NC_SHORT)
     407          41 :             eDataType = GDT_Int16;
     408         281 :         else if (nc_datatype == NC_INT)
     409          89 :             eDataType = GDT_Int32;
     410         192 :         else if (nc_datatype == NC_FLOAT)
     411         115 :             eDataType = GDT_Float32;
     412          77 :         else if (nc_datatype == NC_DOUBLE)
     413          40 :             eDataType = GDT_Float64;
     414          37 :         else if (nc_datatype == NC_UBYTE)
     415          15 :             eDataType = GDT_Byte;
     416          22 :         else if (nc_datatype == NC_USHORT)
     417           4 :             eDataType = GDT_UInt16;
     418          18 :         else if (nc_datatype == NC_UINT)
     419           3 :             eDataType = GDT_UInt32;
     420          15 :         else if (nc_datatype == NC_INT64)
     421           8 :             eDataType = GDT_Int64;
     422           7 :         else if (nc_datatype == NC_UINT64)
     423           7 :             eDataType = GDT_UInt64;
     424             :         else
     425             :         {
     426           0 :             if (nBand == 1)
     427           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
     428             :                          "Unsupported netCDF datatype (%d), treat as Float32.",
     429           0 :                          static_cast<int>(nc_datatype));
     430           0 :             eDataType = GDT_Float32;
     431           0 :             nc_datatype = NC_FLOAT;
     432             :         }
     433             :     }
     434             : 
     435             :     // Find and set No Data for this variable.
     436         472 :     nc_type atttype = NC_NAT;
     437         472 :     size_t attlen = 0;
     438         472 :     const char *pszNoValueName = nullptr;
     439             : 
     440             :     // Find attribute name, either _FillValue or missing_value.
     441         472 :     int status = nc_inq_att(cdfid, nZId, NCDF_FillValue, &atttype, &attlen);
     442         472 :     if (status == NC_NOERR)
     443             :     {
     444         248 :         pszNoValueName = NCDF_FillValue;
     445             :     }
     446             :     else
     447             :     {
     448         224 :         status = nc_inq_att(cdfid, nZId, "missing_value", &atttype, &attlen);
     449         224 :         if (status == NC_NOERR)
     450             :         {
     451          12 :             pszNoValueName = "missing_value";
     452             :         }
     453             :     }
     454             : 
     455             :     // Fetch missing value.
     456         472 :     double dfNoData = 0.0;
     457         472 :     bool bGotNoData = false;
     458         472 :     int64_t nNoDataAsInt64 = 0;
     459         472 :     bool bGotNoDataAsInt64 = false;
     460         472 :     uint64_t nNoDataAsUInt64 = 0;
     461         472 :     bool bGotNoDataAsUInt64 = false;
     462         472 :     if (status == NC_NOERR)
     463             :     {
     464         260 :         nc_type nAttrType = NC_NAT;
     465         260 :         size_t nAttrLen = 0;
     466         260 :         status = nc_inq_att(cdfid, nZId, pszNoValueName, &nAttrType, &nAttrLen);
     467         260 :         if (status == NC_NOERR && nAttrLen == 1 && nAttrType == NC_INT64)
     468             :         {
     469             :             long long v;
     470           7 :             nc_get_att_longlong(cdfid, nZId, pszNoValueName, &v);
     471           7 :             bGotNoData = true;
     472           7 :             bGotNoDataAsInt64 = true;
     473           7 :             nNoDataAsInt64 = static_cast<int64_t>(v);
     474             :         }
     475         253 :         else if (status == NC_NOERR && nAttrLen == 1 && nAttrType == NC_UINT64)
     476             :         {
     477             :             unsigned long long v;
     478           7 :             nc_get_att_ulonglong(cdfid, nZId, pszNoValueName, &v);
     479           7 :             bGotNoData = true;
     480           7 :             bGotNoDataAsUInt64 = true;
     481           7 :             nNoDataAsUInt64 = static_cast<uint64_t>(v);
     482             :         }
     483         246 :         else if (NCDFGetAttr(cdfid, nZId, pszNoValueName, &dfNoData) == CE_None)
     484             :         {
     485         245 :             bGotNoData = true;
     486             :         }
     487             :     }
     488             : 
     489             :     // If NoData was not found, use the default value, but for non-Byte types
     490             :     // as it is not recommended:
     491             :     // https://www.unidata.ucar.edu/software/netcdf/docs/attribute_conventions.html
     492         472 :     nc_type vartype = NC_NAT;
     493         472 :     if (!bGotNoData)
     494             :     {
     495         213 :         nc_inq_vartype(cdfid, nZId, &vartype);
     496         213 :         if (vartype == NC_INT64)
     497             :         {
     498             :             nNoDataAsInt64 =
     499           1 :                 NCDFGetDefaultNoDataValueAsInt64(cdfid, nZId, bGotNoData);
     500           1 :             bGotNoDataAsInt64 = bGotNoData;
     501             :         }
     502         212 :         else if (vartype == NC_UINT64)
     503             :         {
     504             :             nNoDataAsUInt64 =
     505           0 :                 NCDFGetDefaultNoDataValueAsUInt64(cdfid, nZId, bGotNoData);
     506           0 :             bGotNoDataAsUInt64 = bGotNoData;
     507             :         }
     508         212 :         else if (vartype != NC_CHAR && vartype != NC_BYTE &&
     509          90 :                  vartype != NC_UBYTE)
     510             :         {
     511          81 :             dfNoData =
     512          81 :                 NCDFGetDefaultNoDataValue(cdfid, nZId, vartype, bGotNoData);
     513          81 :             if (bGotNoData)
     514             :             {
     515          70 :                 CPLDebug("GDAL_netCDF",
     516             :                          "did not get nodata value for variable #%d, using "
     517             :                          "default %f",
     518             :                          nZId, dfNoData);
     519             :             }
     520             :         }
     521             :     }
     522             : 
     523         472 :     bool bHasUnderscoreUnsignedAttr = false;
     524         472 :     bool bUnderscoreUnsignedAttrVal = false;
     525             :     {
     526         472 :         char *pszTemp = nullptr;
     527         472 :         if (NCDFGetAttr(cdfid, nZId, "_Unsigned", &pszTemp) == CE_None)
     528             :         {
     529         137 :             if (EQUAL(pszTemp, "true"))
     530             :             {
     531         129 :                 bHasUnderscoreUnsignedAttr = true;
     532         129 :                 bUnderscoreUnsignedAttrVal = true;
     533             :             }
     534           8 :             else if (EQUAL(pszTemp, "false"))
     535             :             {
     536           8 :                 bHasUnderscoreUnsignedAttr = true;
     537           8 :                 bUnderscoreUnsignedAttrVal = false;
     538             :             }
     539         137 :             CPLFree(pszTemp);
     540             :         }
     541             :     }
     542             : 
     543             :     // Look for valid_range or valid_min/valid_max.
     544             : 
     545             :     // First look for valid_range.
     546         472 :     if (CPLFetchBool(poNCDFDS->GetOpenOptions(), "HONOUR_VALID_RANGE", true))
     547             :     {
     548         470 :         char *pszValidRange = nullptr;
     549         470 :         if (NCDFGetAttr(cdfid, nZId, "valid_range", &pszValidRange) ==
     550         130 :                 CE_None &&
     551         600 :             pszValidRange[0] == '{' &&
     552         130 :             pszValidRange[strlen(pszValidRange) - 1] == '}')
     553             :         {
     554             :             const std::string osValidRange =
     555         390 :                 std::string(pszValidRange).substr(1, strlen(pszValidRange) - 2);
     556             :             const CPLStringList aosValidRange(
     557         260 :                 CSLTokenizeString2(osValidRange.c_str(), ",", 0));
     558         130 :             if (aosValidRange.size() == 2 &&
     559         260 :                 CPLGetValueType(aosValidRange[0]) != CPL_VALUE_STRING &&
     560         130 :                 CPLGetValueType(aosValidRange[1]) != CPL_VALUE_STRING)
     561             :             {
     562         130 :                 bValidRangeValid = true;
     563         130 :                 adfValidRange[0] = CPLAtof(aosValidRange[0]);
     564         130 :                 adfValidRange[1] = CPLAtof(aosValidRange[1]);
     565             :             }
     566             :         }
     567         470 :         CPLFree(pszValidRange);
     568             : 
     569             :         // If not found look for valid_min and valid_max.
     570         470 :         if (!bValidRangeValid)
     571             :         {
     572         340 :             double dfMin = 0;
     573         340 :             double dfMax = 0;
     574         355 :             if (NCDFGetAttr(cdfid, nZId, "valid_min", &dfMin) == CE_None &&
     575          15 :                 NCDFGetAttr(cdfid, nZId, "valid_max", &dfMax) == CE_None)
     576             :             {
     577           8 :                 adfValidRange[0] = dfMin;
     578           8 :                 adfValidRange[1] = dfMax;
     579           8 :                 bValidRangeValid = true;
     580             :             }
     581             :         }
     582             : 
     583         470 :         if (bValidRangeValid &&
     584         138 :             (adfValidRange[0] < 0 || adfValidRange[1] < 0) &&
     585          17 :             nc_datatype == NC_SHORT && bHasUnderscoreUnsignedAttr &&
     586             :             bUnderscoreUnsignedAttrVal)
     587             :         {
     588           2 :             if (adfValidRange[0] < 0)
     589           0 :                 adfValidRange[0] += 65536;
     590           2 :             if (adfValidRange[1] < 0)
     591           2 :                 adfValidRange[1] += 65536;
     592           2 :             if (adfValidRange[0] <= adfValidRange[1])
     593             :             {
     594             :                 // Updating metadata item
     595           2 :                 GDALPamRasterBand::SetMetadataItem(
     596             :                     "valid_range",
     597           2 :                     CPLSPrintf("{%d,%d}", static_cast<int>(adfValidRange[0]),
     598           2 :                                static_cast<int>(adfValidRange[1])));
     599             :             }
     600             :         }
     601             : 
     602         470 :         if (bValidRangeValid && adfValidRange[0] > adfValidRange[1])
     603             :         {
     604           0 :             CPLError(CE_Warning, CPLE_AppDefined,
     605             :                      "netCDFDataset::valid_range: min > max:\n"
     606             :                      "  min: %lf\n  max: %lf\n",
     607             :                      adfValidRange[0], adfValidRange[1]);
     608           0 :             bValidRangeValid = false;
     609           0 :             adfValidRange[0] = 0.0;
     610           0 :             adfValidRange[1] = 0.0;
     611             :         }
     612             :     }
     613             : 
     614             :     // Special For Byte Bands: check for signed/unsigned byte.
     615         472 :     if (nc_datatype == NC_BYTE)
     616             :     {
     617             :         // netcdf uses signed byte by default, but GDAL uses unsigned by default
     618             :         // This may cause unexpected results, but is needed for back-compat.
     619         145 :         if (poNCDFDS->bIsGdalFile)
     620         124 :             bSignedData = false;
     621             :         else
     622          21 :             bSignedData = true;
     623             : 
     624             :         // For NC4 format NC_BYTE is (normally) signed, NC_UBYTE is unsigned.
     625             :         // But in case a NC3 file was converted automatically and has hints
     626             :         // that it is unsigned, take them into account
     627         145 :         if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     628             :         {
     629           3 :             bSignedData = true;
     630             :         }
     631             : 
     632             :         // If we got valid_range, test for signed/unsigned range.
     633             :         // https://docs.unidata.ucar.edu/netcdf-c/current/attribute_conventions.html
     634         145 :         if (bValidRangeValid)
     635             :         {
     636             :             // If we got valid_range={0,255}, treat as unsigned.
     637         126 :             if (adfValidRange[0] == 0 && adfValidRange[1] == 255)
     638             :             {
     639         118 :                 bSignedData = false;
     640             :                 // Reset valid_range.
     641         118 :                 bValidRangeValid = false;
     642             :             }
     643             :             // If we got valid_range={-128,127}, treat as signed.
     644           8 :             else if (adfValidRange[0] == -128 && adfValidRange[1] == 127)
     645             :             {
     646           8 :                 bSignedData = true;
     647             :                 // Reset valid_range.
     648           8 :                 bValidRangeValid = false;
     649             :             }
     650             :         }
     651             :         // Else test for _Unsigned.
     652             :         // https://docs.unidata.ucar.edu/nug/current/best_practices.html
     653             :         else
     654             :         {
     655          19 :             if (bHasUnderscoreUnsignedAttr)
     656           7 :                 bSignedData = !bUnderscoreUnsignedAttrVal;
     657             :         }
     658             : 
     659         145 :         if (bSignedData)
     660             :         {
     661          20 :             eDataType = GDT_Int8;
     662             :         }
     663         125 :         else if (dfNoData < 0)
     664             :         {
     665             :             // Fix nodata value as it was stored signed.
     666           6 :             dfNoData += 256;
     667           6 :             if (pszNoValueName)
     668             :             {
     669             :                 // Updating metadata item
     670           6 :                 GDALPamRasterBand::SetMetadataItem(
     671             :                     pszNoValueName,
     672             :                     CPLSPrintf("%d", static_cast<int>(dfNoData)));
     673             :             }
     674             :         }
     675             :     }
     676         327 :     else if (nc_datatype == NC_SHORT)
     677             :     {
     678          41 :         if (bHasUnderscoreUnsignedAttr)
     679             :         {
     680           4 :             bSignedData = !bUnderscoreUnsignedAttrVal;
     681           4 :             if (!bSignedData)
     682           4 :                 eDataType = GDT_UInt16;
     683             :         }
     684             : 
     685             :         // Fix nodata value as it was stored signed.
     686          41 :         if (!bSignedData && dfNoData < 0)
     687             :         {
     688           4 :             dfNoData += 65536;
     689           4 :             if (pszNoValueName)
     690             :             {
     691             :                 // Updating metadata item
     692           4 :                 GDALPamRasterBand::SetMetadataItem(
     693             :                     pszNoValueName,
     694             :                     CPLSPrintf("%d", static_cast<int>(dfNoData)));
     695             :             }
     696             :         }
     697             :     }
     698             : 
     699         286 :     else if (nc_datatype == NC_UBYTE || nc_datatype == NC_USHORT ||
     700         267 :              nc_datatype == NC_UINT || nc_datatype == NC_UINT64)
     701             :     {
     702          29 :         bSignedData = false;
     703             :     }
     704             : 
     705         472 :     CPLDebug("GDAL_netCDF", "netcdf type=%d gdal type=%d signedByte=%d",
     706         472 :              nc_datatype, eDataType, static_cast<int>(bSignedData));
     707             : 
     708         472 :     if (bGotNoData)
     709             :     {
     710             :         // Set nodata value.
     711         330 :         if (bGotNoDataAsInt64)
     712             :         {
     713           8 :             if (eDataType == GDT_Int64)
     714             :             {
     715           8 :                 SetNoDataValueNoUpdate(nNoDataAsInt64);
     716             :             }
     717           0 :             else if (eDataType == GDT_UInt64 && nNoDataAsInt64 >= 0)
     718             :             {
     719           0 :                 SetNoDataValueNoUpdate(static_cast<uint64_t>(nNoDataAsInt64));
     720             :             }
     721             :             else
     722             :             {
     723           0 :                 SetNoDataValueNoUpdate(static_cast<double>(nNoDataAsInt64));
     724             :             }
     725             :         }
     726         322 :         else if (bGotNoDataAsUInt64)
     727             :         {
     728           7 :             if (eDataType == GDT_UInt64)
     729             :             {
     730           7 :                 SetNoDataValueNoUpdate(nNoDataAsUInt64);
     731             :             }
     732           0 :             else if (eDataType == GDT_Int64 &&
     733             :                      nNoDataAsUInt64 <=
     734           0 :                          static_cast<uint64_t>(
     735           0 :                              std::numeric_limits<int64_t>::max()))
     736             :             {
     737           0 :                 SetNoDataValueNoUpdate(static_cast<int64_t>(nNoDataAsUInt64));
     738             :             }
     739             :             else
     740             :             {
     741           0 :                 SetNoDataValueNoUpdate(static_cast<double>(nNoDataAsUInt64));
     742             :             }
     743             :         }
     744             :         else
     745             :         {
     746             : #ifdef NCDF_DEBUG
     747             :             CPLDebug("GDAL_netCDF", "SetNoDataValue(%f) read", dfNoData);
     748             : #endif
     749         315 :             if (eDataType == GDT_Int64 && GDALIsValueExactAs<int64_t>(dfNoData))
     750             :             {
     751           0 :                 SetNoDataValueNoUpdate(static_cast<int64_t>(dfNoData));
     752             :             }
     753         315 :             else if (eDataType == GDT_UInt64 &&
     754           0 :                      GDALIsValueExactAs<uint64_t>(dfNoData))
     755             :             {
     756           0 :                 SetNoDataValueNoUpdate(static_cast<uint64_t>(dfNoData));
     757             :             }
     758             :             else
     759             :             {
     760         315 :                 SetNoDataValueNoUpdate(dfNoData);
     761             :             }
     762             :         }
     763             :     }
     764             : 
     765         472 :     CreateMetadataFromAttributes();
     766             : 
     767             :     // Attempt to fetch the scale_factor and add_offset attributes for the
     768             :     // variable and set them.  If these values are not available, set
     769             :     // offset to 0 and scale to 1.
     770         472 :     if (nc_inq_attid(cdfid, nZId, CF_ADD_OFFSET, nullptr) == NC_NOERR)
     771             :     {
     772          16 :         double dfOffset = 0;
     773          16 :         status = nc_get_att_double(cdfid, nZId, CF_ADD_OFFSET, &dfOffset);
     774          16 :         CPLDebug("GDAL_netCDF", "got add_offset=%.16g, status=%d", dfOffset,
     775             :                  status);
     776          16 :         SetOffsetNoUpdate(dfOffset);
     777             :     }
     778             : 
     779         472 :     bool bHasScale = false;
     780         472 :     if (nc_inq_attid(cdfid, nZId, CF_SCALE_FACTOR, nullptr) == NC_NOERR)
     781             :     {
     782          20 :         bHasScale = true;
     783          20 :         double dfScale = 1;
     784          20 :         status = nc_get_att_double(cdfid, nZId, CF_SCALE_FACTOR, &dfScale);
     785          20 :         CPLDebug("GDAL_netCDF", "got scale_factor=%.16g, status=%d", dfScale,
     786             :                  status);
     787          20 :         SetScaleNoUpdate(dfScale);
     788             :     }
     789             : 
     790          12 :     if (bValidRangeValid && GDALDataTypeIsInteger(eDataType) &&
     791           4 :         eDataType != GDT_Int64 && eDataType != GDT_UInt64 &&
     792           4 :         (std::fabs(std::round(adfValidRange[0]) - adfValidRange[0]) > 1e-5 ||
     793         484 :          std::fabs(std::round(adfValidRange[1]) - adfValidRange[1]) > 1e-5) &&
     794           1 :         CSLFetchNameValue(poNCDFDS->GetOpenOptions(), "HONOUR_VALID_RANGE") ==
     795             :             nullptr)
     796             :     {
     797           1 :         CPLError(CE_Warning, CPLE_AppDefined,
     798             :                  "validity range = %f, %f contains floating-point values, "
     799             :                  "whereas data type is integer. valid_range is thus likely "
     800             :                  "wrong%s. Ignoring it.",
     801             :                  adfValidRange[0], adfValidRange[1],
     802             :                  bHasScale ? " (likely scaled using scale_factor/add_factor "
     803             :                              "whereas it should be using the packed data type)"
     804             :                            : "");
     805           1 :         bValidRangeValid = false;
     806           1 :         adfValidRange[0] = 0.0;
     807           1 :         adfValidRange[1] = 0.0;
     808             :     }
     809             : 
     810             :     // Should we check for longitude values > 360?
     811         472 :     bCheckLongitude =
     812         944 :         CPLTestBool(CPLGetConfigOption("GDAL_NETCDF_CENTERLONG_180", "YES")) &&
     813         472 :         NCDFIsVarLongitude(cdfid, nZId, nullptr);
     814             : 
     815             :     // Attempt to fetch the units attribute for the variable and set it.
     816         472 :     SetUnitTypeNoUpdate(netCDFRasterBand::GetMetadataItem(CF_UNITS));
     817             : 
     818         472 :     SetBlockSize();
     819             : }
     820             : 
     821         654 : void netCDFRasterBand::SetBlockSize()
     822             : {
     823             :     // Check for variable chunking (netcdf-4 only).
     824             :     // GDAL block size should be set to hdf5 chunk size.
     825         654 :     int nTmpFormat = 0;
     826         654 :     int status = nc_inq_format(cdfid, &nTmpFormat);
     827         654 :     NetCDFFormatEnum eTmpFormat = static_cast<NetCDFFormatEnum>(nTmpFormat);
     828         654 :     if ((status == NC_NOERR) &&
     829         561 :         (eTmpFormat == NCDF_FORMAT_NC4 || eTmpFormat == NCDF_FORMAT_NC4C))
     830             :     {
     831         109 :         size_t chunksize[MAX_NC_DIMS] = {};
     832             :         // Check for chunksize and set it as the blocksize (optimizes read).
     833         109 :         status = nc_inq_var_chunking(cdfid, nZId, &nTmpFormat, chunksize);
     834         109 :         if ((status == NC_NOERR) && (nTmpFormat == NC_CHUNKED))
     835             :         {
     836          13 :             nBlockXSize = (int)chunksize[nZDim - 1];
     837          13 :             if (nZDim >= 2)
     838          13 :                 nBlockYSize = (int)chunksize[nZDim - 2];
     839             :             else
     840           0 :                 nBlockYSize = 1;
     841             :         }
     842             :     }
     843             : 
     844             :     // Deal with bottom-up datasets and nBlockYSize != 1.
     845         654 :     auto poGDS = static_cast<netCDFDataset *>(poDS);
     846         654 :     if (poGDS->bBottomUp && nBlockYSize != 1 && poGDS->poChunkCache == nullptr)
     847             :     {
     848           5 :         if (poGDS->eAccess == GA_ReadOnly)
     849             :         {
     850             :             // Try to cache 1 or 2 'rows' of netCDF chunks along the whole
     851             :             // width of the raster
     852           5 :             size_t nChunks =
     853           5 :                 static_cast<size_t>(DIV_ROUND_UP(nRasterXSize, nBlockXSize));
     854           5 :             if ((nRasterYSize % nBlockYSize) != 0)
     855           1 :                 nChunks *= 2;
     856             :             const size_t nChunkSize =
     857           5 :                 static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
     858           5 :                 nBlockXSize * nBlockYSize;
     859           5 :             constexpr size_t MAX_CACHE_SIZE = 100 * 1024 * 1024;
     860           5 :             nChunks = std::min(nChunks, MAX_CACHE_SIZE / nChunkSize);
     861           5 :             if (nChunks)
     862             :             {
     863           5 :                 poGDS->poChunkCache.reset(
     864           5 :                     new netCDFDataset::ChunkCacheType(nChunks));
     865             :             }
     866             :         }
     867             :         else
     868             :         {
     869           0 :             nBlockYSize = 1;
     870             :         }
     871             :     }
     872         654 : }
     873             : 
     874             : // Constructor in create mode.
     875             : // If nZId and following variables are not passed, the band will have 2
     876             : // dimensions.
     877             : // TODO: Get metadata, missing val from band #1 if nZDim > 2.
     878         182 : netCDFRasterBand::netCDFRasterBand(
     879             :     const netCDFRasterBand::CONSTRUCTOR_CREATE &, netCDFDataset *poNCDFDS,
     880             :     const GDALDataType eTypeIn, int nBandIn, bool bSigned,
     881             :     const char *pszBandName, const char *pszLongName, int nZIdIn, int nZDimIn,
     882             :     int nLevelIn, const int *panBandZLevIn, const int *panBandZPosIn,
     883         182 :     const int *paDimIds)
     884         182 :     : nc_datatype(NC_NAT), cdfid(poNCDFDS->GetCDFID()), nZId(nZIdIn),
     885             :       nZDim(nZDimIn), nLevel(nLevelIn), nBandXPos(1), nBandYPos(0),
     886             :       panBandZPos(nullptr), panBandZLev(nullptr), bSignedData(bSigned),
     887         182 :       bCheckLongitude(false), m_bCreateMetadataFromOtherVarsDone(true)
     888             : {
     889         182 :     poDS = poNCDFDS;
     890         182 :     nBand = nBandIn;
     891             : 
     892         182 :     nRasterXSize = poDS->GetRasterXSize();
     893         182 :     nRasterYSize = poDS->GetRasterYSize();
     894         182 :     nBlockXSize = poDS->GetRasterXSize();
     895         182 :     nBlockYSize = 1;
     896             : 
     897         182 :     if (poDS->GetAccess() != GA_Update)
     898             :     {
     899           0 :         CPLError(CE_Failure, CPLE_NotSupported,
     900             :                  "Dataset is not in update mode, "
     901             :                  "wrong netCDFRasterBand constructor");
     902           0 :         return;
     903             :     }
     904             : 
     905             :     // Take care of all other dimensions.
     906         182 :     if (nZDim > 2 && paDimIds != nullptr)
     907             :     {
     908          27 :         nBandXPos = panBandZPosIn[0];
     909          27 :         nBandYPos = panBandZPosIn[1];
     910             : 
     911          27 :         panBandZPos = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     912          27 :         panBandZLev = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     913             : 
     914          76 :         for (int i = 0; i < nZDim - 2; i++)
     915             :         {
     916          49 :             panBandZPos[i] = panBandZPosIn[i + 2];
     917          49 :             panBandZLev[i] = panBandZLevIn[i];
     918             :         }
     919             :     }
     920             : 
     921             :     // Get the type of the "z" variable, our target raster array.
     922         182 :     eDataType = eTypeIn;
     923             : 
     924         182 :     switch (eDataType)
     925             :     {
     926          77 :         case GDT_Byte:
     927          77 :             nc_datatype = NC_BYTE;
     928             :             // NC_UBYTE (unsigned byte) is only available for NC4.
     929          77 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     930           3 :                 nc_datatype = NC_UBYTE;
     931          77 :             break;
     932           7 :         case GDT_Int8:
     933           7 :             nc_datatype = NC_BYTE;
     934           7 :             break;
     935          11 :         case GDT_Int16:
     936          11 :             nc_datatype = NC_SHORT;
     937          11 :             break;
     938          24 :         case GDT_Int32:
     939          24 :             nc_datatype = NC_INT;
     940          24 :             break;
     941          13 :         case GDT_Float32:
     942          13 :             nc_datatype = NC_FLOAT;
     943          13 :             break;
     944           8 :         case GDT_Float64:
     945           8 :             nc_datatype = NC_DOUBLE;
     946           8 :             break;
     947           7 :         case GDT_Int64:
     948           7 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     949             :             {
     950           7 :                 nc_datatype = NC_INT64;
     951             :             }
     952             :             else
     953             :             {
     954           0 :                 if (nBand == 1)
     955           0 :                     CPLError(
     956             :                         CE_Warning, CPLE_AppDefined,
     957             :                         "Unsupported GDAL datatype %s, treat as NC_DOUBLE.",
     958             :                         "Int64");
     959           0 :                 nc_datatype = NC_DOUBLE;
     960           0 :                 eDataType = GDT_Float64;
     961             :             }
     962           7 :             break;
     963           7 :         case GDT_UInt64:
     964           7 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     965             :             {
     966           7 :                 nc_datatype = NC_UINT64;
     967             :             }
     968             :             else
     969             :             {
     970           0 :                 if (nBand == 1)
     971           0 :                     CPLError(
     972             :                         CE_Warning, CPLE_AppDefined,
     973             :                         "Unsupported GDAL datatype %s, treat as NC_DOUBLE.",
     974             :                         "UInt64");
     975           0 :                 nc_datatype = NC_DOUBLE;
     976           0 :                 eDataType = GDT_Float64;
     977             :             }
     978           7 :             break;
     979           6 :         case GDT_UInt16:
     980           6 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     981             :             {
     982           6 :                 nc_datatype = NC_USHORT;
     983           6 :                 break;
     984             :             }
     985             :             [[fallthrough]];
     986             :         case GDT_UInt32:
     987           6 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     988             :             {
     989           6 :                 nc_datatype = NC_UINT;
     990           6 :                 break;
     991             :             }
     992             :             [[fallthrough]];
     993             :         default:
     994          16 :             if (nBand == 1)
     995           8 :                 CPLError(CE_Warning, CPLE_AppDefined,
     996             :                          "Unsupported GDAL datatype (%d), treat as NC_FLOAT.",
     997           8 :                          static_cast<int>(eDataType));
     998          16 :             nc_datatype = NC_FLOAT;
     999          16 :             eDataType = GDT_Float32;
    1000          16 :             break;
    1001             :     }
    1002             : 
    1003             :     // Define the variable if necessary (if nZId == -1).
    1004         182 :     bool bDefineVar = false;
    1005             : 
    1006         182 :     if (nZId == -1)
    1007             :     {
    1008         160 :         bDefineVar = true;
    1009             : 
    1010             :         // Make sure we are in define mode.
    1011         160 :         static_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1012             : 
    1013             :         char szTempPrivate[256 + 1];
    1014         160 :         const char *pszTemp = nullptr;
    1015         160 :         if (!pszBandName || EQUAL(pszBandName, ""))
    1016             :         {
    1017         138 :             snprintf(szTempPrivate, sizeof(szTempPrivate), "Band%d", nBand);
    1018         138 :             pszTemp = szTempPrivate;
    1019             :         }
    1020             :         else
    1021             :         {
    1022          22 :             pszTemp = pszBandName;
    1023             :         }
    1024             : 
    1025             :         int status;
    1026         160 :         if (nZDim > 2 && paDimIds != nullptr)
    1027             :         {
    1028           5 :             status =
    1029           5 :                 nc_def_var(cdfid, pszTemp, nc_datatype, nZDim, paDimIds, &nZId);
    1030             :         }
    1031             :         else
    1032             :         {
    1033         155 :             int anBandDims[2] = {poNCDFDS->nYDimID, poNCDFDS->nXDimID};
    1034             :             status =
    1035         155 :                 nc_def_var(cdfid, pszTemp, nc_datatype, 2, anBandDims, &nZId);
    1036             :         }
    1037         160 :         NCDF_ERR(status);
    1038         160 :         CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d) id=%d", cdfid, pszTemp,
    1039             :                  nc_datatype, nZId);
    1040             : 
    1041         160 :         if (!pszLongName || EQUAL(pszLongName, ""))
    1042             :         {
    1043         153 :             snprintf(szTempPrivate, sizeof(szTempPrivate),
    1044             :                      "GDAL Band Number %d", nBand);
    1045         153 :             pszTemp = szTempPrivate;
    1046             :         }
    1047             :         else
    1048             :         {
    1049           7 :             pszTemp = pszLongName;
    1050             :         }
    1051             :         status =
    1052         160 :             nc_put_att_text(cdfid, nZId, CF_LNG_NAME, strlen(pszTemp), pszTemp);
    1053         160 :         NCDF_ERR(status);
    1054             : 
    1055         160 :         poNCDFDS->DefVarDeflate(nZId, true);
    1056             :     }
    1057             : 
    1058             :     // For Byte data add signed/unsigned info.
    1059         182 :     if (eDataType == GDT_Byte || eDataType == GDT_Int8)
    1060             :     {
    1061          84 :         if (bDefineVar)
    1062             :         {
    1063             :             // Only add attributes if creating variable.
    1064             :             // For unsigned NC_BYTE (except NC4 format),
    1065             :             // add valid_range and _Unsigned ( defined in CF-1 and NUG ).
    1066          76 :             if (nc_datatype == NC_BYTE && poNCDFDS->eFormat != NCDF_FORMAT_NC4)
    1067             :             {
    1068          73 :                 CPLDebug("GDAL_netCDF",
    1069             :                          "adding valid_range attributes for Byte Band");
    1070          73 :                 short l_adfValidRange[2] = {0, 0};
    1071             :                 int status;
    1072          73 :                 if (bSignedData || eDataType == GDT_Int8)
    1073             :                 {
    1074           7 :                     l_adfValidRange[0] = -128;
    1075           7 :                     l_adfValidRange[1] = 127;
    1076           7 :                     status =
    1077           7 :                         nc_put_att_text(cdfid, nZId, "_Unsigned", 5, "false");
    1078             :                 }
    1079             :                 else
    1080             :                 {
    1081          66 :                     l_adfValidRange[0] = 0;
    1082          66 :                     l_adfValidRange[1] = 255;
    1083             :                     status =
    1084          66 :                         nc_put_att_text(cdfid, nZId, "_Unsigned", 4, "true");
    1085             :                 }
    1086          73 :                 NCDF_ERR(status);
    1087          73 :                 status = nc_put_att_short(cdfid, nZId, "valid_range", NC_SHORT,
    1088             :                                           2, l_adfValidRange);
    1089          73 :                 NCDF_ERR(status);
    1090             :             }
    1091             :         }
    1092             :     }
    1093             : 
    1094         182 :     if (nc_datatype != NC_BYTE && nc_datatype != NC_CHAR &&
    1095         101 :         nc_datatype != NC_UBYTE)
    1096             :     {
    1097             :         // Set default nodata.
    1098          98 :         bool bIgnored = false;
    1099             :         double dfNoData =
    1100          98 :             NCDFGetDefaultNoDataValue(cdfid, nZId, nc_datatype, bIgnored);
    1101             : #ifdef NCDF_DEBUG
    1102             :         CPLDebug("GDAL_netCDF", "SetNoDataValue(%f) default", dfNoData);
    1103             : #endif
    1104          98 :         netCDFRasterBand::SetNoDataValue(dfNoData);
    1105             :     }
    1106             : 
    1107         182 :     SetBlockSize();
    1108             : }
    1109             : 
    1110             : /************************************************************************/
    1111             : /*                         ~netCDFRasterBand()                          */
    1112             : /************************************************************************/
    1113             : 
    1114        1308 : netCDFRasterBand::~netCDFRasterBand()
    1115             : {
    1116         654 :     netCDFRasterBand::FlushCache(true);
    1117         654 :     CPLFree(panBandZPos);
    1118         654 :     CPLFree(panBandZLev);
    1119        1308 : }
    1120             : 
    1121             : /************************************************************************/
    1122             : /*                          GetMetadata()                               */
    1123             : /************************************************************************/
    1124             : 
    1125          50 : char **netCDFRasterBand::GetMetadata(const char *pszDomain)
    1126             : {
    1127          50 :     if (!m_bCreateMetadataFromOtherVarsDone)
    1128          48 :         CreateMetadataFromOtherVars();
    1129          50 :     return GDALPamRasterBand::GetMetadata(pszDomain);
    1130             : }
    1131             : 
    1132             : /************************************************************************/
    1133             : /*                        GetMetadataItem()                             */
    1134             : /************************************************************************/
    1135             : 
    1136         546 : const char *netCDFRasterBand::GetMetadataItem(const char *pszName,
    1137             :                                               const char *pszDomain)
    1138             : {
    1139         546 :     if (!m_bCreateMetadataFromOtherVarsDone &&
    1140         530 :         STARTS_WITH(pszName, "NETCDF_DIM_") &&
    1141           1 :         (!pszDomain || pszDomain[0] == 0))
    1142           1 :         CreateMetadataFromOtherVars();
    1143         546 :     return GDALPamRasterBand::GetMetadataItem(pszName, pszDomain);
    1144             : }
    1145             : 
    1146             : /************************************************************************/
    1147             : /*                        SetMetadataItem()                             */
    1148             : /************************************************************************/
    1149             : 
    1150           7 : CPLErr netCDFRasterBand::SetMetadataItem(const char *pszName,
    1151             :                                          const char *pszValue,
    1152             :                                          const char *pszDomain)
    1153             : {
    1154           9 :     if (GetAccess() == GA_Update &&
    1155           9 :         (pszDomain == nullptr || pszDomain[0] == '\0') && pszValue != nullptr)
    1156             :     {
    1157             :         // Same logic as in CopyMetadata()
    1158             : 
    1159           2 :         const char *const papszIgnoreBand[] = {
    1160             :             CF_ADD_OFFSET,  CF_SCALE_FACTOR, "valid_range", "_Unsigned",
    1161             :             NCDF_FillValue, "coordinates",   nullptr};
    1162             :         // Do not copy varname, stats, NETCDF_DIM_*, nodata
    1163             :         // and items in papszIgnoreBand.
    1164           6 :         if (STARTS_WITH(pszName, "NETCDF_VARNAME") ||
    1165           2 :             STARTS_WITH(pszName, "STATISTICS_") ||
    1166           2 :             STARTS_WITH(pszName, "NETCDF_DIM_") ||
    1167           2 :             STARTS_WITH(pszName, "missing_value") ||
    1168           6 :             STARTS_WITH(pszName, "_FillValue") ||
    1169           2 :             CSLFindString(papszIgnoreBand, pszName) != -1)
    1170             :         {
    1171             :             // do nothing
    1172             :         }
    1173             :         else
    1174             :         {
    1175           2 :             cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1176             : 
    1177           2 :             if (!NCDFPutAttr(cdfid, nZId, pszName, pszValue))
    1178           2 :                 return CE_Failure;
    1179             :         }
    1180             :     }
    1181             : 
    1182           5 :     return GDALPamRasterBand::SetMetadataItem(pszName, pszValue, pszDomain);
    1183             : }
    1184             : 
    1185             : /************************************************************************/
    1186             : /*                          SetMetadata()                               */
    1187             : /************************************************************************/
    1188             : 
    1189           2 : CPLErr netCDFRasterBand::SetMetadata(char **papszMD, const char *pszDomain)
    1190             : {
    1191           4 :     if (GetAccess() == GA_Update &&
    1192           2 :         (pszDomain == nullptr || pszDomain[0] == '\0'))
    1193             :     {
    1194             :         // We don't handle metadata item removal for now
    1195           4 :         for (const char *const *papszIter = papszMD; papszIter && *papszIter;
    1196             :              ++papszIter)
    1197             :         {
    1198           2 :             char *pszName = nullptr;
    1199           2 :             const char *pszValue = CPLParseNameValue(*papszIter, &pszName);
    1200           2 :             if (pszName && pszValue)
    1201           2 :                 SetMetadataItem(pszName, pszValue);
    1202           2 :             CPLFree(pszName);
    1203             :         }
    1204             :     }
    1205           2 :     return GDALPamRasterBand::SetMetadata(papszMD, pszDomain);
    1206             : }
    1207             : 
    1208             : /************************************************************************/
    1209             : /*                             GetOffset()                              */
    1210             : /************************************************************************/
    1211          50 : double netCDFRasterBand::GetOffset(int *pbSuccess)
    1212             : {
    1213          50 :     if (pbSuccess != nullptr)
    1214          45 :         *pbSuccess = static_cast<int>(m_bHaveOffset);
    1215             : 
    1216          50 :     return m_dfOffset;
    1217             : }
    1218             : 
    1219             : /************************************************************************/
    1220             : /*                             SetOffset()                              */
    1221             : /************************************************************************/
    1222           1 : CPLErr netCDFRasterBand::SetOffset(double dfNewOffset)
    1223             : {
    1224           2 :     CPLMutexHolderD(&hNCMutex);
    1225             : 
    1226             :     // Write value if in update mode.
    1227           1 :     if (poDS->GetAccess() == GA_Update)
    1228             :     {
    1229             :         // Make sure we are in define mode.
    1230           1 :         static_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1231             : 
    1232           1 :         const int status = nc_put_att_double(cdfid, nZId, CF_ADD_OFFSET,
    1233             :                                              NC_DOUBLE, 1, &dfNewOffset);
    1234             : 
    1235           1 :         NCDF_ERR(status);
    1236           1 :         if (status == NC_NOERR)
    1237             :         {
    1238           1 :             SetOffsetNoUpdate(dfNewOffset);
    1239           1 :             return CE_None;
    1240             :         }
    1241             : 
    1242           0 :         return CE_Failure;
    1243             :     }
    1244             : 
    1245           0 :     SetOffsetNoUpdate(dfNewOffset);
    1246           0 :     return CE_None;
    1247             : }
    1248             : 
    1249             : /************************************************************************/
    1250             : /*                         SetOffsetNoUpdate()                          */
    1251             : /************************************************************************/
    1252          17 : void netCDFRasterBand::SetOffsetNoUpdate(double dfVal)
    1253             : {
    1254          17 :     m_dfOffset = dfVal;
    1255          17 :     m_bHaveOffset = true;
    1256          17 : }
    1257             : 
    1258             : /************************************************************************/
    1259             : /*                              GetScale()                              */
    1260             : /************************************************************************/
    1261          50 : double netCDFRasterBand::GetScale(int *pbSuccess)
    1262             : {
    1263          50 :     if (pbSuccess != nullptr)
    1264          45 :         *pbSuccess = static_cast<int>(m_bHaveScale);
    1265             : 
    1266          50 :     return m_dfScale;
    1267             : }
    1268             : 
    1269             : /************************************************************************/
    1270             : /*                              SetScale()                              */
    1271             : /************************************************************************/
    1272           1 : CPLErr netCDFRasterBand::SetScale(double dfNewScale)
    1273             : {
    1274           2 :     CPLMutexHolderD(&hNCMutex);
    1275             : 
    1276             :     // Write value if in update mode.
    1277           1 :     if (poDS->GetAccess() == GA_Update)
    1278             :     {
    1279             :         // Make sure we are in define mode.
    1280           1 :         static_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1281             : 
    1282           1 :         const int status = nc_put_att_double(cdfid, nZId, CF_SCALE_FACTOR,
    1283             :                                              NC_DOUBLE, 1, &dfNewScale);
    1284             : 
    1285           1 :         NCDF_ERR(status);
    1286           1 :         if (status == NC_NOERR)
    1287             :         {
    1288           1 :             SetScaleNoUpdate(dfNewScale);
    1289           1 :             return CE_None;
    1290             :         }
    1291             : 
    1292           0 :         return CE_Failure;
    1293             :     }
    1294             : 
    1295           0 :     SetScaleNoUpdate(dfNewScale);
    1296           0 :     return CE_None;
    1297             : }
    1298             : 
    1299             : /************************************************************************/
    1300             : /*                         SetScaleNoUpdate()                           */
    1301             : /************************************************************************/
    1302          21 : void netCDFRasterBand::SetScaleNoUpdate(double dfVal)
    1303             : {
    1304          21 :     m_dfScale = dfVal;
    1305          21 :     m_bHaveScale = true;
    1306          21 : }
    1307             : 
    1308             : /************************************************************************/
    1309             : /*                            GetUnitType()                             */
    1310             : /************************************************************************/
    1311             : 
    1312          22 : const char *netCDFRasterBand::GetUnitType()
    1313             : 
    1314             : {
    1315          22 :     if (!m_osUnitType.empty())
    1316           6 :         return m_osUnitType;
    1317             : 
    1318          16 :     return GDALRasterBand::GetUnitType();
    1319             : }
    1320             : 
    1321             : /************************************************************************/
    1322             : /*                           SetUnitType()                              */
    1323             : /************************************************************************/
    1324             : 
    1325           1 : CPLErr netCDFRasterBand::SetUnitType(const char *pszNewValue)
    1326             : 
    1327             : {
    1328           2 :     CPLMutexHolderD(&hNCMutex);
    1329             : 
    1330           2 :     const std::string osUnitType = (pszNewValue != nullptr ? pszNewValue : "");
    1331             : 
    1332           1 :     if (!osUnitType.empty())
    1333             :     {
    1334             :         // Write value if in update mode.
    1335           1 :         if (poDS->GetAccess() == GA_Update)
    1336             :         {
    1337             :             // Make sure we are in define mode.
    1338           1 :             static_cast<netCDFDataset *>(poDS)->SetDefineMode(TRUE);
    1339             : 
    1340           1 :             const int status = nc_put_att_text(
    1341             :                 cdfid, nZId, CF_UNITS, osUnitType.size(), osUnitType.c_str());
    1342             : 
    1343           1 :             NCDF_ERR(status);
    1344           1 :             if (status == NC_NOERR)
    1345             :             {
    1346           1 :                 SetUnitTypeNoUpdate(pszNewValue);
    1347           1 :                 return CE_None;
    1348             :             }
    1349             : 
    1350           0 :             return CE_Failure;
    1351             :         }
    1352             :     }
    1353             : 
    1354           0 :     SetUnitTypeNoUpdate(pszNewValue);
    1355             : 
    1356           0 :     return CE_None;
    1357             : }
    1358             : 
    1359             : /************************************************************************/
    1360             : /*                       SetUnitTypeNoUpdate()                          */
    1361             : /************************************************************************/
    1362             : 
    1363         473 : void netCDFRasterBand::SetUnitTypeNoUpdate(const char *pszNewValue)
    1364             : {
    1365         473 :     m_osUnitType = (pszNewValue != nullptr ? pszNewValue : "");
    1366         473 : }
    1367             : 
    1368             : /************************************************************************/
    1369             : /*                           GetNoDataValue()                           */
    1370             : /************************************************************************/
    1371             : 
    1372         155 : double netCDFRasterBand::GetNoDataValue(int *pbSuccess)
    1373             : 
    1374             : {
    1375         155 :     if (m_bNoDataSetAsInt64)
    1376             :     {
    1377           0 :         if (pbSuccess)
    1378           0 :             *pbSuccess = TRUE;
    1379           0 :         return GDALGetNoDataValueCastToDouble(m_nNodataValueInt64);
    1380             :     }
    1381             : 
    1382         155 :     if (m_bNoDataSetAsUInt64)
    1383             :     {
    1384           0 :         if (pbSuccess)
    1385           0 :             *pbSuccess = TRUE;
    1386           0 :         return GDALGetNoDataValueCastToDouble(m_nNodataValueUInt64);
    1387             :     }
    1388             : 
    1389         155 :     if (m_bNoDataSet)
    1390             :     {
    1391         118 :         if (pbSuccess)
    1392         102 :             *pbSuccess = TRUE;
    1393         118 :         return m_dfNoDataValue;
    1394             :     }
    1395             : 
    1396          37 :     return GDALPamRasterBand::GetNoDataValue(pbSuccess);
    1397             : }
    1398             : 
    1399             : /************************************************************************/
    1400             : /*                        GetNoDataValueAsInt64()                       */
    1401             : /************************************************************************/
    1402             : 
    1403           4 : int64_t netCDFRasterBand::GetNoDataValueAsInt64(int *pbSuccess)
    1404             : 
    1405             : {
    1406           4 :     if (m_bNoDataSetAsInt64)
    1407             :     {
    1408           4 :         if (pbSuccess)
    1409           4 :             *pbSuccess = TRUE;
    1410             : 
    1411           4 :         return m_nNodataValueInt64;
    1412             :     }
    1413             : 
    1414           0 :     return GDALPamRasterBand::GetNoDataValueAsInt64(pbSuccess);
    1415             : }
    1416             : 
    1417             : /************************************************************************/
    1418             : /*                        GetNoDataValueAsUInt64()                      */
    1419             : /************************************************************************/
    1420             : 
    1421           4 : uint64_t netCDFRasterBand::GetNoDataValueAsUInt64(int *pbSuccess)
    1422             : 
    1423             : {
    1424           4 :     if (m_bNoDataSetAsUInt64)
    1425             :     {
    1426           4 :         if (pbSuccess)
    1427           4 :             *pbSuccess = TRUE;
    1428             : 
    1429           4 :         return m_nNodataValueUInt64;
    1430             :     }
    1431             : 
    1432           0 :     return GDALPamRasterBand::GetNoDataValueAsUInt64(pbSuccess);
    1433             : }
    1434             : 
    1435             : /************************************************************************/
    1436             : /*                           SetNoDataValue()                           */
    1437             : /************************************************************************/
    1438             : 
    1439         134 : CPLErr netCDFRasterBand::SetNoDataValue(double dfNoData)
    1440             : 
    1441             : {
    1442         268 :     CPLMutexHolderD(&hNCMutex);
    1443             : 
    1444             :     // If already set to new value, don't do anything.
    1445         134 :     if (m_bNoDataSet && CPLIsEqual(dfNoData, m_dfNoDataValue))
    1446          19 :         return CE_None;
    1447             : 
    1448             :     // Write value if in update mode.
    1449         115 :     if (poDS->GetAccess() == GA_Update)
    1450             :     {
    1451             :         // netcdf-4 does not allow to set _FillValue after leaving define mode,
    1452             :         // but it is ok if variable has not been written to, so only print
    1453             :         // debug. See bug #4484.
    1454         125 :         if (m_bNoDataSet &&
    1455          10 :             !reinterpret_cast<netCDFDataset *>(poDS)->GetDefineMode())
    1456             :         {
    1457           0 :             CPLDebug("GDAL_netCDF",
    1458             :                      "Setting NoDataValue to %.17g (previously set to %.17g) "
    1459             :                      "but file is no longer in define mode (id #%d, band #%d)",
    1460             :                      dfNoData, m_dfNoDataValue, cdfid, nBand);
    1461             :         }
    1462             : #ifdef NCDF_DEBUG
    1463             :         else
    1464             :         {
    1465             :             CPLDebug("GDAL_netCDF",
    1466             :                      "Setting NoDataValue to %.17g (id #%d, band #%d)",
    1467             :                      dfNoData, cdfid, nBand);
    1468             :         }
    1469             : #endif
    1470             :         // Make sure we are in define mode.
    1471         115 :         reinterpret_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1472             : 
    1473             :         int status;
    1474         115 :         if (eDataType == GDT_Byte)
    1475             :         {
    1476           6 :             if (bSignedData)
    1477             :             {
    1478           0 :                 signed char cNoDataValue = static_cast<signed char>(dfNoData);
    1479           0 :                 status = nc_put_att_schar(cdfid, nZId, NCDF_FillValue,
    1480             :                                           nc_datatype, 1, &cNoDataValue);
    1481             :             }
    1482             :             else
    1483             :             {
    1484           6 :                 const unsigned char ucNoDataValue =
    1485           6 :                     static_cast<unsigned char>(dfNoData);
    1486           6 :                 status = nc_put_att_uchar(cdfid, nZId, NCDF_FillValue,
    1487             :                                           nc_datatype, 1, &ucNoDataValue);
    1488             :             }
    1489             :         }
    1490         109 :         else if (eDataType == GDT_Int16)
    1491             :         {
    1492          14 :             short nsNoDataValue = static_cast<short>(dfNoData);
    1493          14 :             status = nc_put_att_short(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1494             :                                       1, &nsNoDataValue);
    1495             :         }
    1496          95 :         else if (eDataType == GDT_Int32)
    1497             :         {
    1498          27 :             int nNoDataValue = static_cast<int>(dfNoData);
    1499          27 :             status = nc_put_att_int(cdfid, nZId, NCDF_FillValue, nc_datatype, 1,
    1500             :                                     &nNoDataValue);
    1501             :         }
    1502          68 :         else if (eDataType == GDT_Float32)
    1503             :         {
    1504          31 :             float fNoDataValue = static_cast<float>(dfNoData);
    1505          31 :             status = nc_put_att_float(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1506             :                                       1, &fNoDataValue);
    1507             :         }
    1508          37 :         else if (eDataType == GDT_UInt16 &&
    1509           6 :                  reinterpret_cast<netCDFDataset *>(poDS)->eFormat ==
    1510             :                      NCDF_FORMAT_NC4)
    1511             :         {
    1512           6 :             unsigned short usNoDataValue =
    1513           6 :                 static_cast<unsigned short>(dfNoData);
    1514           6 :             status = nc_put_att_ushort(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1515           6 :                                        1, &usNoDataValue);
    1516             :         }
    1517          31 :         else if (eDataType == GDT_UInt32 &&
    1518           7 :                  reinterpret_cast<netCDFDataset *>(poDS)->eFormat ==
    1519             :                      NCDF_FORMAT_NC4)
    1520             :         {
    1521           7 :             unsigned int unNoDataValue = static_cast<unsigned int>(dfNoData);
    1522           7 :             status = nc_put_att_uint(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1523           7 :                                      1, &unNoDataValue);
    1524             :         }
    1525             :         else
    1526             :         {
    1527          24 :             status = nc_put_att_double(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1528             :                                        1, &dfNoData);
    1529             :         }
    1530             : 
    1531         115 :         NCDF_ERR(status);
    1532             : 
    1533             :         // Update status if write worked.
    1534         115 :         if (status == NC_NOERR)
    1535             :         {
    1536         115 :             SetNoDataValueNoUpdate(dfNoData);
    1537         115 :             return CE_None;
    1538             :         }
    1539             : 
    1540           0 :         return CE_Failure;
    1541             :     }
    1542             : 
    1543           0 :     SetNoDataValueNoUpdate(dfNoData);
    1544           0 :     return CE_None;
    1545             : }
    1546             : 
    1547             : /************************************************************************/
    1548             : /*                       SetNoDataValueNoUpdate()                       */
    1549             : /************************************************************************/
    1550             : 
    1551         430 : void netCDFRasterBand::SetNoDataValueNoUpdate(double dfNoData)
    1552             : {
    1553         430 :     m_dfNoDataValue = dfNoData;
    1554         430 :     m_bNoDataSet = true;
    1555         430 :     m_bNoDataSetAsInt64 = false;
    1556         430 :     m_bNoDataSetAsUInt64 = false;
    1557         430 : }
    1558             : 
    1559             : /************************************************************************/
    1560             : /*                        SetNoDataValueAsInt64()                       */
    1561             : /************************************************************************/
    1562             : 
    1563           3 : CPLErr netCDFRasterBand::SetNoDataValueAsInt64(int64_t nNoData)
    1564             : 
    1565             : {
    1566           6 :     CPLMutexHolderD(&hNCMutex);
    1567             : 
    1568             :     // If already set to new value, don't do anything.
    1569           3 :     if (m_bNoDataSetAsInt64 && nNoData == m_nNodataValueInt64)
    1570           0 :         return CE_None;
    1571             : 
    1572             :     // Write value if in update mode.
    1573           3 :     if (poDS->GetAccess() == GA_Update)
    1574             :     {
    1575             :         // netcdf-4 does not allow to set NCDF_FillValue after leaving define mode,
    1576             :         // but it is ok if variable has not been written to, so only print
    1577             :         // debug. See bug #4484.
    1578           3 :         if (m_bNoDataSetAsInt64 &&
    1579           0 :             !reinterpret_cast<netCDFDataset *>(poDS)->GetDefineMode())
    1580             :         {
    1581           0 :             CPLDebug("GDAL_netCDF",
    1582             :                      "Setting NoDataValue to " CPL_FRMT_GIB
    1583             :                      " (previously set to " CPL_FRMT_GIB ") "
    1584             :                      "but file is no longer in define mode (id #%d, band #%d)",
    1585             :                      static_cast<GIntBig>(nNoData),
    1586           0 :                      static_cast<GIntBig>(m_nNodataValueInt64), cdfid, nBand);
    1587             :         }
    1588             : #ifdef NCDF_DEBUG
    1589             :         else
    1590             :         {
    1591             :             CPLDebug("GDAL_netCDF",
    1592             :                      "Setting NoDataValue to " CPL_FRMT_GIB
    1593             :                      " (id #%d, band #%d)",
    1594             :                      static_cast<GIntBig>(nNoData), cdfid, nBand);
    1595             :         }
    1596             : #endif
    1597             :         // Make sure we are in define mode.
    1598           3 :         reinterpret_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1599             : 
    1600             :         int status;
    1601           3 :         if (eDataType == GDT_Int64 &&
    1602           3 :             reinterpret_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    1603             :         {
    1604           3 :             long long tmp = static_cast<long long>(nNoData);
    1605           3 :             status = nc_put_att_longlong(cdfid, nZId, NCDF_FillValue,
    1606           3 :                                          nc_datatype, 1, &tmp);
    1607             :         }
    1608             :         else
    1609             :         {
    1610           0 :             double dfNoData = static_cast<double>(nNoData);
    1611           0 :             status = nc_put_att_double(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1612             :                                        1, &dfNoData);
    1613             :         }
    1614             : 
    1615           3 :         NCDF_ERR(status);
    1616             : 
    1617             :         // Update status if write worked.
    1618           3 :         if (status == NC_NOERR)
    1619             :         {
    1620           3 :             SetNoDataValueNoUpdate(nNoData);
    1621           3 :             return CE_None;
    1622             :         }
    1623             : 
    1624           0 :         return CE_Failure;
    1625             :     }
    1626             : 
    1627           0 :     SetNoDataValueNoUpdate(nNoData);
    1628           0 :     return CE_None;
    1629             : }
    1630             : 
    1631             : /************************************************************************/
    1632             : /*                       SetNoDataValueNoUpdate()                       */
    1633             : /************************************************************************/
    1634             : 
    1635          11 : void netCDFRasterBand::SetNoDataValueNoUpdate(int64_t nNoData)
    1636             : {
    1637          11 :     m_nNodataValueInt64 = nNoData;
    1638          11 :     m_bNoDataSet = false;
    1639          11 :     m_bNoDataSetAsInt64 = true;
    1640          11 :     m_bNoDataSetAsUInt64 = false;
    1641          11 : }
    1642             : 
    1643             : /************************************************************************/
    1644             : /*                        SetNoDataValueAsUInt64()                      */
    1645             : /************************************************************************/
    1646             : 
    1647           3 : CPLErr netCDFRasterBand::SetNoDataValueAsUInt64(uint64_t nNoData)
    1648             : 
    1649             : {
    1650           6 :     CPLMutexHolderD(&hNCMutex);
    1651             : 
    1652             :     // If already set to new value, don't do anything.
    1653           3 :     if (m_bNoDataSetAsUInt64 && nNoData == m_nNodataValueUInt64)
    1654           0 :         return CE_None;
    1655             : 
    1656             :     // Write value if in update mode.
    1657           3 :     if (poDS->GetAccess() == GA_Update)
    1658             :     {
    1659             :         // netcdf-4 does not allow to set _FillValue after leaving define mode,
    1660             :         // but it is ok if variable has not been written to, so only print
    1661             :         // debug. See bug #4484.
    1662           3 :         if (m_bNoDataSetAsUInt64 &&
    1663           0 :             !reinterpret_cast<netCDFDataset *>(poDS)->GetDefineMode())
    1664             :         {
    1665           0 :             CPLDebug("GDAL_netCDF",
    1666             :                      "Setting NoDataValue to " CPL_FRMT_GUIB
    1667             :                      " (previously set to " CPL_FRMT_GUIB ") "
    1668             :                      "but file is no longer in define mode (id #%d, band #%d)",
    1669             :                      static_cast<GUIntBig>(nNoData),
    1670           0 :                      static_cast<GUIntBig>(m_nNodataValueUInt64), cdfid, nBand);
    1671             :         }
    1672             : #ifdef NCDF_DEBUG
    1673             :         else
    1674             :         {
    1675             :             CPLDebug("GDAL_netCDF",
    1676             :                      "Setting NoDataValue to " CPL_FRMT_GUIB
    1677             :                      " (id #%d, band #%d)",
    1678             :                      static_cast<GUIntBig>(nNoData), cdfid, nBand);
    1679             :         }
    1680             : #endif
    1681             :         // Make sure we are in define mode.
    1682           3 :         reinterpret_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1683             : 
    1684             :         int status;
    1685           3 :         if (eDataType == GDT_UInt64 &&
    1686           3 :             reinterpret_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    1687             :         {
    1688           3 :             unsigned long long tmp = static_cast<long long>(nNoData);
    1689           3 :             status = nc_put_att_ulonglong(cdfid, nZId, NCDF_FillValue,
    1690           3 :                                           nc_datatype, 1, &tmp);
    1691             :         }
    1692             :         else
    1693             :         {
    1694           0 :             double dfNoData = static_cast<double>(nNoData);
    1695           0 :             status = nc_put_att_double(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1696             :                                        1, &dfNoData);
    1697             :         }
    1698             : 
    1699           3 :         NCDF_ERR(status);
    1700             : 
    1701             :         // Update status if write worked.
    1702           3 :         if (status == NC_NOERR)
    1703             :         {
    1704           3 :             SetNoDataValueNoUpdate(nNoData);
    1705           3 :             return CE_None;
    1706             :         }
    1707             : 
    1708           0 :         return CE_Failure;
    1709             :     }
    1710             : 
    1711           0 :     SetNoDataValueNoUpdate(nNoData);
    1712           0 :     return CE_None;
    1713             : }
    1714             : 
    1715             : /************************************************************************/
    1716             : /*                       SetNoDataValueNoUpdate()                       */
    1717             : /************************************************************************/
    1718             : 
    1719          10 : void netCDFRasterBand::SetNoDataValueNoUpdate(uint64_t nNoData)
    1720             : {
    1721          10 :     m_nNodataValueUInt64 = nNoData;
    1722          10 :     m_bNoDataSet = false;
    1723          10 :     m_bNoDataSetAsInt64 = false;
    1724          10 :     m_bNoDataSetAsUInt64 = true;
    1725          10 : }
    1726             : 
    1727             : /************************************************************************/
    1728             : /*                        DeleteNoDataValue()                           */
    1729             : /************************************************************************/
    1730             : 
    1731             : #ifdef notdef
    1732             : CPLErr netCDFRasterBand::DeleteNoDataValue()
    1733             : 
    1734             : {
    1735             :     CPLMutexHolderD(&hNCMutex);
    1736             : 
    1737             :     if (!bNoDataSet)
    1738             :         return CE_None;
    1739             : 
    1740             :     // Write value if in update mode.
    1741             :     if (poDS->GetAccess() == GA_Update)
    1742             :     {
    1743             :         // Make sure we are in define mode.
    1744             :         static_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1745             : 
    1746             :         status = nc_del_att(cdfid, nZId, NCDF_FillValue);
    1747             : 
    1748             :         NCDF_ERR(status);
    1749             : 
    1750             :         // Update status if write worked.
    1751             :         if (status == NC_NOERR)
    1752             :         {
    1753             :             dfNoDataValue = 0.0;
    1754             :             bNoDataSet = false;
    1755             :             return CE_None;
    1756             :         }
    1757             : 
    1758             :         return CE_Failure;
    1759             :     }
    1760             : 
    1761             :     dfNoDataValue = 0.0;
    1762             :     bNoDataSet = false;
    1763             :     return CE_None;
    1764             : }
    1765             : #endif
    1766             : 
    1767             : /************************************************************************/
    1768             : /*                           SerializeToXML()                           */
    1769             : /************************************************************************/
    1770             : 
    1771           5 : CPLXMLNode *netCDFRasterBand::SerializeToXML(const char * /* pszUnused */)
    1772             : {
    1773             :     // Overridden from GDALPamDataset to add only band histogram
    1774             :     // and statistics. See bug #4244.
    1775           5 :     if (psPam == nullptr)
    1776           0 :         return nullptr;
    1777             : 
    1778             :     // Setup root node and attributes.
    1779             :     CPLXMLNode *psTree =
    1780           5 :         CPLCreateXMLNode(nullptr, CXT_Element, "PAMRasterBand");
    1781             : 
    1782           5 :     if (GetBand() > 0)
    1783             :     {
    1784          10 :         CPLString oFmt;
    1785           5 :         CPLSetXMLValue(psTree, "#band", oFmt.Printf("%d", GetBand()));
    1786             :     }
    1787             : 
    1788             :     // Histograms.
    1789           5 :     if (psPam->psSavedHistograms != nullptr)
    1790           1 :         CPLAddXMLChild(psTree, CPLCloneXMLTree(psPam->psSavedHistograms));
    1791             : 
    1792             :     // Metadata (statistics only).
    1793           5 :     GDALMultiDomainMetadata oMDMDStats;
    1794           5 :     const char *papszMDStats[] = {"STATISTICS_MINIMUM", "STATISTICS_MAXIMUM",
    1795             :                                   "STATISTICS_MEAN", "STATISTICS_STDDEV",
    1796             :                                   nullptr};
    1797          25 :     for (int i = 0; i < CSLCount(papszMDStats); i++)
    1798             :     {
    1799          20 :         const char *pszMDI = GetMetadataItem(papszMDStats[i]);
    1800          20 :         if (pszMDI)
    1801           4 :             oMDMDStats.SetMetadataItem(papszMDStats[i], pszMDI);
    1802             :     }
    1803           5 :     CPLXMLNode *psMD = oMDMDStats.Serialize();
    1804             : 
    1805           5 :     if (psMD != nullptr)
    1806             :     {
    1807           1 :         if (psMD->psChild == nullptr)
    1808           0 :             CPLDestroyXMLNode(psMD);
    1809             :         else
    1810           1 :             CPLAddXMLChild(psTree, psMD);
    1811             :     }
    1812             : 
    1813             :     // We don't want to return anything if we had no metadata to attach.
    1814           5 :     if (psTree->psChild == nullptr || psTree->psChild->psNext == nullptr)
    1815             :     {
    1816           3 :         CPLDestroyXMLNode(psTree);
    1817           3 :         psTree = nullptr;
    1818             :     }
    1819             : 
    1820           5 :     return psTree;
    1821             : }
    1822             : 
    1823             : /************************************************************************/
    1824             : /*               Get1DVariableIndexedByDimension()                      */
    1825             : /************************************************************************/
    1826             : 
    1827          79 : static int Get1DVariableIndexedByDimension(int cdfid, int nDimId,
    1828             :                                            const char *pszDimName,
    1829             :                                            bool bVerboseError, int *pnGroupID)
    1830             : {
    1831          79 :     *pnGroupID = -1;
    1832          79 :     int nVarID = -1;
    1833             :     // First try to find a variable whose name is identical to the dimension
    1834             :     // name, and check that it is indeed indexed by this dimension
    1835          79 :     if (NCDFResolveVar(cdfid, pszDimName, pnGroupID, &nVarID) == CE_None)
    1836             :     {
    1837          65 :         int nDimCountOfVariable = 0;
    1838          65 :         nc_inq_varndims(*pnGroupID, nVarID, &nDimCountOfVariable);
    1839          65 :         if (nDimCountOfVariable == 1)
    1840             :         {
    1841          65 :             int nDimIdOfVariable = -1;
    1842          65 :             nc_inq_vardimid(*pnGroupID, nVarID, &nDimIdOfVariable);
    1843          65 :             if (nDimIdOfVariable == nDimId)
    1844             :             {
    1845          65 :                 return nVarID;
    1846             :             }
    1847             :         }
    1848             :     }
    1849             : 
    1850             :     // Otherwise iterate over the variables to find potential candidates
    1851             :     // TODO: should be modified to search also in other groups using the same
    1852             :     //       logic than in NCDFResolveVar(), but maybe not needed if it's a
    1853             :     //       very rare case? and I think this is not CF compliant.
    1854          14 :     int nvars = 0;
    1855          14 :     CPL_IGNORE_RET_VAL(nc_inq(cdfid, nullptr, &nvars, nullptr, nullptr));
    1856             : 
    1857          14 :     int nCountCandidateVars = 0;
    1858          14 :     int nCandidateVarID = -1;
    1859          65 :     for (int k = 0; k < nvars; k++)
    1860             :     {
    1861          51 :         int nDimCountOfVariable = 0;
    1862          51 :         nc_inq_varndims(cdfid, k, &nDimCountOfVariable);
    1863          51 :         if (nDimCountOfVariable == 1)
    1864             :         {
    1865          27 :             int nDimIdOfVariable = -1;
    1866          27 :             nc_inq_vardimid(cdfid, k, &nDimIdOfVariable);
    1867          27 :             if (nDimIdOfVariable == nDimId)
    1868             :             {
    1869           7 :                 nCountCandidateVars++;
    1870           7 :                 nCandidateVarID = k;
    1871             :             }
    1872             :         }
    1873             :     }
    1874          14 :     if (nCountCandidateVars > 1)
    1875             :     {
    1876           1 :         if (bVerboseError)
    1877             :         {
    1878           1 :             CPLError(CE_Warning, CPLE_AppDefined,
    1879             :                      "Several 1D variables are indexed by dimension %s",
    1880             :                      pszDimName);
    1881             :         }
    1882           1 :         *pnGroupID = -1;
    1883           1 :         return -1;
    1884             :     }
    1885          13 :     else if (nCandidateVarID < 0)
    1886             :     {
    1887           8 :         if (bVerboseError)
    1888             :         {
    1889           8 :             CPLError(CE_Warning, CPLE_AppDefined,
    1890             :                      "No 1D variable is indexed by dimension %s", pszDimName);
    1891             :         }
    1892             :     }
    1893          13 :     *pnGroupID = cdfid;
    1894          13 :     return nCandidateVarID;
    1895             : }
    1896             : 
    1897             : /************************************************************************/
    1898             : /*                      CreateMetadataFromAttributes()                  */
    1899             : /************************************************************************/
    1900             : 
    1901         472 : void netCDFRasterBand::CreateMetadataFromAttributes()
    1902             : {
    1903         472 :     char szVarName[NC_MAX_NAME + 1] = {};
    1904         472 :     int status = nc_inq_varname(cdfid, nZId, szVarName);
    1905         472 :     NCDF_ERR(status);
    1906             : 
    1907         472 :     GDALPamRasterBand::SetMetadataItem("NETCDF_VARNAME", szVarName);
    1908             : 
    1909             :     // Get attribute metadata.
    1910         472 :     int nAtt = 0;
    1911         472 :     NCDF_ERR(nc_inq_varnatts(cdfid, nZId, &nAtt));
    1912             : 
    1913        2018 :     for (int i = 0; i < nAtt; i++)
    1914             :     {
    1915        1546 :         char szMetaName[NC_MAX_NAME + 1] = {};
    1916        1546 :         status = nc_inq_attname(cdfid, nZId, i, szMetaName);
    1917        1546 :         if (status != NC_NOERR)
    1918          12 :             continue;
    1919             : 
    1920        1546 :         if (GDALPamRasterBand::GetMetadataItem(szMetaName) != nullptr)
    1921             :         {
    1922          12 :             continue;
    1923             :         }
    1924             : 
    1925        1534 :         char *pszMetaValue = nullptr;
    1926        1534 :         if (NCDFGetAttr(cdfid, nZId, szMetaName, &pszMetaValue) == CE_None)
    1927             :         {
    1928        1534 :             GDALPamRasterBand::SetMetadataItem(szMetaName, pszMetaValue);
    1929             :         }
    1930             :         else
    1931             :         {
    1932           0 :             CPLDebug("GDAL_netCDF", "invalid Band metadata %s", szMetaName);
    1933             :         }
    1934             : 
    1935        1534 :         if (pszMetaValue)
    1936             :         {
    1937        1534 :             CPLFree(pszMetaValue);
    1938        1534 :             pszMetaValue = nullptr;
    1939             :         }
    1940             :     }
    1941         472 : }
    1942             : 
    1943             : /************************************************************************/
    1944             : /*                      CreateMetadataFromOtherVars()                   */
    1945             : /************************************************************************/
    1946             : 
    1947          49 : void netCDFRasterBand::CreateMetadataFromOtherVars()
    1948             : 
    1949             : {
    1950          49 :     CPLAssert(!m_bCreateMetadataFromOtherVarsDone);
    1951          49 :     m_bCreateMetadataFromOtherVarsDone = true;
    1952             : 
    1953          49 :     netCDFDataset *l_poDS = reinterpret_cast<netCDFDataset *>(poDS);
    1954          49 :     const int nPamFlagsBackup = l_poDS->nPamFlags;
    1955             : 
    1956             :     // Compute all dimensions from Band number and save in Metadata.
    1957          49 :     int nd = 0;
    1958          49 :     nc_inq_varndims(cdfid, nZId, &nd);
    1959             :     // Compute multidimention band position.
    1960             :     //
    1961             :     // BandPosition = (Total - sum(PastBandLevels) - 1)/sum(remainingLevels)
    1962             :     // if Data[2,3,4,x,y]
    1963             :     //
    1964             :     //  BandPos0 = (nBand) / (3*4)
    1965             :     //  BandPos1 = (nBand - BandPos0*(3*4)) / (4)
    1966             :     //  BandPos2 = (nBand - BandPos0*(3*4)) % (4)
    1967             : 
    1968          49 :     int Sum = 1;
    1969          49 :     if (nd == 3)
    1970             :     {
    1971           5 :         Sum *= panBandZLev[0];
    1972             :     }
    1973             : 
    1974             :     // Loop over non-spatial dimensions.
    1975          49 :     int Taken = 0;
    1976             : 
    1977          89 :     for (int i = 0; i < nd - 2; i++)
    1978             :     {
    1979             :         int result;
    1980          40 :         if (i != nd - 2 - 1)
    1981             :         {
    1982          18 :             Sum = 1;
    1983          37 :             for (int j = i + 1; j < nd - 2; j++)
    1984             :             {
    1985          19 :                 Sum *= panBandZLev[j];
    1986             :             }
    1987          18 :             result = static_cast<int>((nLevel - Taken) / Sum);
    1988             :         }
    1989             :         else
    1990             :         {
    1991          22 :             result = static_cast<int>((nLevel - Taken) % Sum);
    1992             :         }
    1993             : 
    1994          40 :         char szName[NC_MAX_NAME + 1] = {};
    1995          40 :         snprintf(szName, sizeof(szName), "%s",
    1996          40 :                  l_poDS->papszDimName[l_poDS->m_anDimIds[panBandZPos[i]]]);
    1997             : 
    1998             :         char szMetaName[NC_MAX_NAME + 1 + 32];
    1999          40 :         snprintf(szMetaName, sizeof(szMetaName), "NETCDF_DIM_%s", szName);
    2000             : 
    2001          40 :         const int nGroupID = l_poDS->m_anExtraDimGroupIds[i];
    2002          40 :         const int nVarID = l_poDS->m_anExtraDimVarIds[i];
    2003          40 :         if (nVarID < 0)
    2004             :         {
    2005           2 :             GDALPamRasterBand::SetMetadataItem(szMetaName,
    2006             :                                                CPLSPrintf("%d", result + 1));
    2007             :         }
    2008             :         else
    2009             :         {
    2010             :             // TODO: Make sure all the status checks make sense.
    2011             : 
    2012          38 :             nc_type nVarType = NC_NAT;
    2013          38 :             /* status = */ nc_inq_vartype(nGroupID, nVarID, &nVarType);
    2014             : 
    2015          38 :             int nDims = 0;
    2016          38 :             /* status = */ nc_inq_varndims(nGroupID, nVarID, &nDims);
    2017             : 
    2018          38 :             char szMetaTemp[256] = {};
    2019          38 :             if (nDims == 1)
    2020             :             {
    2021          38 :                 size_t count[1] = {1};
    2022          38 :                 size_t start[1] = {static_cast<size_t>(result)};
    2023             : 
    2024          38 :                 switch (nVarType)
    2025             :                 {
    2026           0 :                     case NC_BYTE:
    2027             :                         // TODO: Check for signed/unsigned byte.
    2028             :                         signed char cData;
    2029           0 :                         /* status = */ nc_get_vara_schar(nGroupID, nVarID,
    2030             :                                                          start, count, &cData);
    2031           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", cData);
    2032           0 :                         break;
    2033           0 :                     case NC_SHORT:
    2034             :                         short sData;
    2035           0 :                         /* status = */ nc_get_vara_short(nGroupID, nVarID,
    2036             :                                                          start, count, &sData);
    2037           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", sData);
    2038           0 :                         break;
    2039          19 :                     case NC_INT:
    2040             :                     {
    2041             :                         int nData;
    2042          19 :                         /* status = */ nc_get_vara_int(nGroupID, nVarID, start,
    2043             :                                                        count, &nData);
    2044          19 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", nData);
    2045          19 :                         break;
    2046             :                     }
    2047           0 :                     case NC_FLOAT:
    2048             :                         float fData;
    2049           0 :                         /* status = */ nc_get_vara_float(nGroupID, nVarID,
    2050             :                                                          start, count, &fData);
    2051           0 :                         CPLsnprintf(szMetaTemp, sizeof(szMetaTemp), "%.8g",
    2052             :                                     fData);
    2053           0 :                         break;
    2054          18 :                     case NC_DOUBLE:
    2055             :                         double dfData;
    2056          18 :                         /* status = */ nc_get_vara_double(
    2057             :                             nGroupID, nVarID, start, count, &dfData);
    2058          18 :                         CPLsnprintf(szMetaTemp, sizeof(szMetaTemp), "%.16g",
    2059             :                                     dfData);
    2060          18 :                         break;
    2061           0 :                     case NC_UBYTE:
    2062             :                         unsigned char ucData;
    2063           0 :                         /* status = */ nc_get_vara_uchar(nGroupID, nVarID,
    2064             :                                                          start, count, &ucData);
    2065           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%u", ucData);
    2066           0 :                         break;
    2067           0 :                     case NC_USHORT:
    2068             :                         unsigned short usData;
    2069           0 :                         /* status = */ nc_get_vara_ushort(
    2070             :                             nGroupID, nVarID, start, count, &usData);
    2071           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%u", usData);
    2072           0 :                         break;
    2073           0 :                     case NC_UINT:
    2074             :                     {
    2075             :                         unsigned int unData;
    2076           0 :                         /* status = */ nc_get_vara_uint(nGroupID, nVarID, start,
    2077             :                                                         count, &unData);
    2078           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%u", unData);
    2079           0 :                         break;
    2080             :                     }
    2081           1 :                     case NC_INT64:
    2082             :                     {
    2083             :                         long long nData;
    2084           1 :                         /* status = */ nc_get_vara_longlong(
    2085             :                             nGroupID, nVarID, start, count, &nData);
    2086           1 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), CPL_FRMT_GIB,
    2087             :                                  nData);
    2088           1 :                         break;
    2089             :                     }
    2090           0 :                     case NC_UINT64:
    2091             :                     {
    2092             :                         unsigned long long unData;
    2093           0 :                         /* status = */ nc_get_vara_ulonglong(
    2094             :                             nGroupID, nVarID, start, count, &unData);
    2095           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), CPL_FRMT_GUIB,
    2096             :                                  unData);
    2097           0 :                         break;
    2098             :                     }
    2099           0 :                     default:
    2100           0 :                         CPLDebug("GDAL_netCDF", "invalid dim %s, type=%d",
    2101             :                                  szMetaTemp, nVarType);
    2102           0 :                         break;
    2103             :                 }
    2104             :             }
    2105             :             else
    2106             :             {
    2107           0 :                 snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", result + 1);
    2108             :             }
    2109             : 
    2110             :             // Save dimension value.
    2111             :             // NOTE: removed #original_units as not part of CF-1.
    2112             : 
    2113          38 :             GDALPamRasterBand::SetMetadataItem(szMetaName, szMetaTemp);
    2114             :         }
    2115             : 
    2116             :         // Avoid int32 overflow. Perhaps something more sensible to do here ?
    2117          40 :         if (result > 0 && Sum > INT_MAX / result)
    2118           0 :             break;
    2119          40 :         if (Taken > INT_MAX - result * Sum)
    2120           0 :             break;
    2121             : 
    2122          40 :         Taken += result * Sum;
    2123             :     }  // End loop non-spatial dimensions.
    2124             : 
    2125          49 :     l_poDS->nPamFlags = nPamFlagsBackup;
    2126          49 : }
    2127             : 
    2128             : /************************************************************************/
    2129             : /*                             CheckData()                              */
    2130             : /************************************************************************/
    2131             : template <class T>
    2132        5732 : void netCDFRasterBand::CheckData(void *pImage, void *pImageNC,
    2133             :                                  size_t nTmpBlockXSize, size_t nTmpBlockYSize,
    2134             :                                  bool bCheckIsNan)
    2135             : {
    2136        5732 :     CPLAssert(pImage != nullptr && pImageNC != nullptr);
    2137             : 
    2138             :     // If this block is not a full block (in the x axis), we need to re-arrange
    2139             :     // the data this is because partial blocks are not arranged the same way in
    2140             :     // netcdf and gdal.
    2141        5732 :     if (nTmpBlockXSize != static_cast<size_t>(nBlockXSize))
    2142             :     {
    2143           6 :         T *ptrWrite = static_cast<T *>(pImage);
    2144           6 :         T *ptrRead = static_cast<T *>(pImageNC);
    2145          29 :         for (size_t j = 0; j < nTmpBlockYSize;
    2146          23 :              j++, ptrWrite += nBlockXSize, ptrRead += nTmpBlockXSize)
    2147             :         {
    2148          23 :             memmove(ptrWrite, ptrRead, nTmpBlockXSize * sizeof(T));
    2149             :         }
    2150             :     }
    2151             : 
    2152             :     // Is valid data checking needed or requested?
    2153        5732 :     if (bValidRangeValid || bCheckIsNan)
    2154             :     {
    2155        1265 :         T *ptrImage = static_cast<T *>(pImage);
    2156        2584 :         for (size_t j = 0; j < nTmpBlockYSize; j++)
    2157             :         {
    2158             :             // k moves along the gdal block, skipping the out-of-range pixels.
    2159        1319 :             size_t k = j * nBlockXSize;
    2160       96938 :             for (size_t i = 0; i < nTmpBlockXSize; i++, k++)
    2161             :             {
    2162             :                 // Check for nodata and nan.
    2163       95619 :                 if (CPLIsEqual((double)ptrImage[k], m_dfNoDataValue))
    2164        6301 :                     continue;
    2165       89318 :                 if (bCheckIsNan && std::isnan((double)ptrImage[k]))
    2166             :                 {
    2167        5737 :                     ptrImage[k] = (T)m_dfNoDataValue;
    2168        5737 :                     continue;
    2169             :                 }
    2170             :                 // Check for valid_range.
    2171       83581 :                 if (bValidRangeValid)
    2172             :                 {
    2173       40986 :                     if (((adfValidRange[0] != m_dfNoDataValue) &&
    2174       40986 :                          (ptrImage[k] < (T)adfValidRange[0])) ||
    2175       40983 :                         ((adfValidRange[1] != m_dfNoDataValue) &&
    2176       40983 :                          (ptrImage[k] > (T)adfValidRange[1])))
    2177             :                     {
    2178           4 :                         ptrImage[k] = (T)m_dfNoDataValue;
    2179             :                     }
    2180             :                 }
    2181             :             }
    2182             :         }
    2183             :     }
    2184             : 
    2185             :     // If minimum longitude is > 180, subtract 360 from all.
    2186             :     // If not, disable checking for further calls (check just once).
    2187             :     // Only check first and last block elements since lon must be monotonic.
    2188        5732 :     const bool bIsSigned = std::numeric_limits<T>::is_signed;
    2189        5419 :     if (bCheckLongitude && bIsSigned &&
    2190           9 :         !CPLIsEqual((double)((T *)pImage)[0], m_dfNoDataValue) &&
    2191           8 :         !CPLIsEqual((double)((T *)pImage)[nTmpBlockXSize - 1],
    2192        2714 :                     m_dfNoDataValue) &&
    2193           8 :         std::min(((T *)pImage)[0], ((T *)pImage)[nTmpBlockXSize - 1]) > 180.0)
    2194             :     {
    2195           0 :         T *ptrImage = static_cast<T *>(pImage);
    2196           0 :         for (size_t j = 0; j < nTmpBlockYSize; j++)
    2197             :         {
    2198           0 :             size_t k = j * nBlockXSize;
    2199           0 :             for (size_t i = 0; i < nTmpBlockXSize; i++, k++)
    2200             :             {
    2201           0 :                 if (!CPLIsEqual((double)ptrImage[k], m_dfNoDataValue))
    2202           0 :                     ptrImage[k] = static_cast<T>(ptrImage[k] - 360);
    2203             :             }
    2204             :         }
    2205             :     }
    2206             :     else
    2207             :     {
    2208        5732 :         bCheckLongitude = false;
    2209             :     }
    2210        5732 : }
    2211             : 
    2212             : /************************************************************************/
    2213             : /*                             CheckDataCpx()                              */
    2214             : /************************************************************************/
    2215             : template <class T>
    2216          25 : void netCDFRasterBand::CheckDataCpx(void *pImage, void *pImageNC,
    2217             :                                     size_t nTmpBlockXSize,
    2218             :                                     size_t nTmpBlockYSize, bool bCheckIsNan)
    2219             : {
    2220          25 :     CPLAssert(pImage != nullptr && pImageNC != nullptr);
    2221             : 
    2222             :     // If this block is not a full block (in the x axis), we need to re-arrange
    2223             :     // the data this is because partial blocks are not arranged the same way in
    2224             :     // netcdf and gdal.
    2225          25 :     if (nTmpBlockXSize != static_cast<size_t>(nBlockXSize))
    2226             :     {
    2227           0 :         T *ptrWrite = static_cast<T *>(pImage);
    2228           0 :         T *ptrRead = static_cast<T *>(pImageNC);
    2229           0 :         for (size_t j = 0; j < nTmpBlockYSize; j++,
    2230           0 :                     ptrWrite += (2 * nBlockXSize),
    2231           0 :                     ptrRead += (2 * nTmpBlockXSize))
    2232             :         {
    2233           0 :             memmove(ptrWrite, ptrRead, nTmpBlockXSize * sizeof(T) * 2);
    2234             :         }
    2235             :     }
    2236             : 
    2237             :     // Is valid data checking needed or requested?
    2238          25 :     if (bValidRangeValid || bCheckIsNan)
    2239             :     {
    2240           0 :         T *ptrImage = static_cast<T *>(pImage);
    2241           0 :         for (size_t j = 0; j < nTmpBlockYSize; j++)
    2242             :         {
    2243             :             // k moves along the gdal block, skipping the out-of-range pixels.
    2244           0 :             size_t k = 2 * j * nBlockXSize;
    2245           0 :             for (size_t i = 0; i < (2 * nTmpBlockXSize); i++, k++)
    2246             :             {
    2247             :                 // Check for nodata and nan.
    2248           0 :                 if (CPLIsEqual((double)ptrImage[k], m_dfNoDataValue))
    2249           0 :                     continue;
    2250           0 :                 if (bCheckIsNan && std::isnan((double)ptrImage[k]))
    2251             :                 {
    2252           0 :                     ptrImage[k] = (T)m_dfNoDataValue;
    2253           0 :                     continue;
    2254             :                 }
    2255             :                 // Check for valid_range.
    2256           0 :                 if (bValidRangeValid)
    2257             :                 {
    2258           0 :                     if (((adfValidRange[0] != m_dfNoDataValue) &&
    2259           0 :                          (ptrImage[k] < (T)adfValidRange[0])) ||
    2260           0 :                         ((adfValidRange[1] != m_dfNoDataValue) &&
    2261           0 :                          (ptrImage[k] > (T)adfValidRange[1])))
    2262             :                     {
    2263           0 :                         ptrImage[k] = (T)m_dfNoDataValue;
    2264             :                     }
    2265             :                 }
    2266             :             }
    2267             :         }
    2268             :     }
    2269          25 : }
    2270             : 
    2271             : /************************************************************************/
    2272             : /*                         FetchNetcdfChunk()                           */
    2273             : /************************************************************************/
    2274             : 
    2275        5757 : bool netCDFRasterBand::FetchNetcdfChunk(size_t xstart, size_t ystart,
    2276             :                                         void *pImage)
    2277             : {
    2278        5757 :     size_t start[MAX_NC_DIMS] = {};
    2279        5757 :     size_t edge[MAX_NC_DIMS] = {};
    2280             : 
    2281        5757 :     start[nBandXPos] = xstart;
    2282        5757 :     edge[nBandXPos] = nBlockXSize;
    2283        5757 :     if ((start[nBandXPos] + edge[nBandXPos]) > (size_t)nRasterXSize)
    2284           6 :         edge[nBandXPos] = nRasterXSize - start[nBandXPos];
    2285        5757 :     if (nBandYPos >= 0)
    2286             :     {
    2287        5753 :         start[nBandYPos] = ystart;
    2288        5753 :         edge[nBandYPos] = nBlockYSize;
    2289        5753 :         if ((start[nBandYPos] + edge[nBandYPos]) > (size_t)nRasterYSize)
    2290           4 :             edge[nBandYPos] = nRasterYSize - start[nBandYPos];
    2291             :     }
    2292        5757 :     const size_t nYChunkSize = nBandYPos < 0 ? 1 : edge[nBandYPos];
    2293             : 
    2294             : #ifdef NCDF_DEBUG
    2295             :     CPLDebug("GDAL_netCDF", "start={%ld,%ld} edge={%ld,%ld} bBottomUp=%d",
    2296             :              start[nBandXPos], nBandYPos < 0 ? 0 : start[nBandYPos],
    2297             :              edge[nBandXPos], nYChunkSize, ((netCDFDataset *)poDS)->bBottomUp);
    2298             : #endif
    2299             : 
    2300        5757 :     int nd = 0;
    2301        5757 :     nc_inq_varndims(cdfid, nZId, &nd);
    2302        5757 :     if (nd == 3)
    2303             :     {
    2304        1078 :         start[panBandZPos[0]] = nLevel;  // z
    2305        1078 :         edge[panBandZPos[0]] = 1;
    2306             :     }
    2307             : 
    2308             :     // Compute multidimention band position.
    2309             :     //
    2310             :     // BandPosition = (Total - sum(PastBandLevels) - 1)/sum(remainingLevels)
    2311             :     // if Data[2,3,4,x,y]
    2312             :     //
    2313             :     //  BandPos0 = (nBand) / (3*4)
    2314             :     //  BandPos1 = (nBand - (3*4)) / (4)
    2315             :     //  BandPos2 = (nBand - (3*4)) % (4)
    2316        5757 :     if (nd > 3)
    2317             :     {
    2318         160 :         int Sum = -1;
    2319         160 :         int Taken = 0;
    2320         480 :         for (int i = 0; i < nd - 2; i++)
    2321             :         {
    2322         320 :             if (i != nd - 2 - 1)
    2323             :             {
    2324         160 :                 Sum = 1;
    2325         320 :                 for (int j = i + 1; j < nd - 2; j++)
    2326             :                 {
    2327         160 :                     Sum *= panBandZLev[j];
    2328             :                 }
    2329         160 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) / Sum);
    2330         160 :                 edge[panBandZPos[i]] = 1;
    2331             :             }
    2332             :             else
    2333             :             {
    2334         160 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) % Sum);
    2335         160 :                 edge[panBandZPos[i]] = 1;
    2336             :             }
    2337         320 :             Taken += static_cast<int>(start[panBandZPos[i]]) * Sum;
    2338             :         }
    2339             :     }
    2340             : 
    2341             :     // Make sure we are in data mode.
    2342        5757 :     static_cast<netCDFDataset *>(poDS)->SetDefineMode(false);
    2343             : 
    2344             :     // If this block is not a full block in the x axis, we need to
    2345             :     // re-arrange the data because partial blocks are not arranged the
    2346             :     // same way in netcdf and gdal, so we first we read the netcdf data at
    2347             :     // the end of the gdal block buffer then re-arrange rows in CheckData().
    2348        5757 :     void *pImageNC = pImage;
    2349        5757 :     if (edge[nBandXPos] != static_cast<size_t>(nBlockXSize))
    2350             :     {
    2351           6 :         pImageNC = static_cast<GByte *>(pImage) +
    2352           6 :                    ((static_cast<size_t>(nBlockXSize) * nBlockYSize -
    2353          12 :                      edge[nBandXPos] * nYChunkSize) *
    2354           6 :                     GDALGetDataTypeSizeBytes(eDataType));
    2355             :     }
    2356             : 
    2357             :     // Read data according to type.
    2358             :     int status;
    2359        5757 :     if (eDataType == GDT_Byte)
    2360             :     {
    2361        3005 :         if (bSignedData)
    2362             :         {
    2363           0 :             status = nc_get_vara_schar(cdfid, nZId, start, edge,
    2364             :                                        static_cast<signed char *>(pImageNC));
    2365           0 :             if (status == NC_NOERR)
    2366           0 :                 CheckData<signed char>(pImage, pImageNC, edge[nBandXPos],
    2367             :                                        nYChunkSize, false);
    2368             :         }
    2369             :         else
    2370             :         {
    2371        3005 :             status = nc_get_vara_uchar(cdfid, nZId, start, edge,
    2372             :                                        static_cast<unsigned char *>(pImageNC));
    2373        3005 :             if (status == NC_NOERR)
    2374        3005 :                 CheckData<unsigned char>(pImage, pImageNC, edge[nBandXPos],
    2375             :                                          nYChunkSize, false);
    2376             :         }
    2377             :     }
    2378        2752 :     else if (eDataType == GDT_Int8)
    2379             :     {
    2380          60 :         status = nc_get_vara_schar(cdfid, nZId, start, edge,
    2381             :                                    static_cast<signed char *>(pImageNC));
    2382          60 :         if (status == NC_NOERR)
    2383          60 :             CheckData<signed char>(pImage, pImageNC, edge[nBandXPos],
    2384             :                                    nYChunkSize, false);
    2385             :     }
    2386        2692 :     else if (nc_datatype == NC_SHORT)
    2387             :     {
    2388         465 :         status = nc_get_vara_short(cdfid, nZId, start, edge,
    2389             :                                    static_cast<short *>(pImageNC));
    2390         465 :         if (status == NC_NOERR)
    2391             :         {
    2392         465 :             if (eDataType == GDT_Int16)
    2393             :             {
    2394         462 :                 CheckData<GInt16>(pImage, pImageNC, edge[nBandXPos],
    2395             :                                   nYChunkSize, false);
    2396             :             }
    2397             :             else
    2398             :             {
    2399           3 :                 CheckData<GUInt16>(pImage, pImageNC, edge[nBandXPos],
    2400             :                                    nYChunkSize, false);
    2401             :             }
    2402             :         }
    2403             :     }
    2404        2227 :     else if (eDataType == GDT_Int32)
    2405             :     {
    2406             : #if SIZEOF_UNSIGNED_LONG == 4
    2407             :         status = nc_get_vara_long(cdfid, nZId, start, edge,
    2408             :                                   static_cast<long *>(pImageNC));
    2409             :         if (status == NC_NOERR)
    2410             :             CheckData<long>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2411             :                             false);
    2412             : #else
    2413         912 :         status = nc_get_vara_int(cdfid, nZId, start, edge,
    2414             :                                  static_cast<int *>(pImageNC));
    2415         912 :         if (status == NC_NOERR)
    2416         912 :             CheckData<int>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2417             :                            false);
    2418             : #endif
    2419             :     }
    2420        1315 :     else if (eDataType == GDT_Float32)
    2421             :     {
    2422        1178 :         status = nc_get_vara_float(cdfid, nZId, start, edge,
    2423             :                                    static_cast<float *>(pImageNC));
    2424        1178 :         if (status == NC_NOERR)
    2425        1178 :             CheckData<float>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2426             :                              true);
    2427             :     }
    2428         137 :     else if (eDataType == GDT_Float64)
    2429             :     {
    2430          86 :         status = nc_get_vara_double(cdfid, nZId, start, edge,
    2431             :                                     static_cast<double *>(pImageNC));
    2432          86 :         if (status == NC_NOERR)
    2433          86 :             CheckData<double>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2434             :                               true);
    2435             :     }
    2436          51 :     else if (eDataType == GDT_UInt16)
    2437             :     {
    2438           6 :         status = nc_get_vara_ushort(cdfid, nZId, start, edge,
    2439             :                                     static_cast<unsigned short *>(pImageNC));
    2440           6 :         if (status == NC_NOERR)
    2441           6 :             CheckData<unsigned short>(pImage, pImageNC, edge[nBandXPos],
    2442             :                                       nYChunkSize, false);
    2443             :     }
    2444          45 :     else if (eDataType == GDT_UInt32)
    2445             :     {
    2446           6 :         status = nc_get_vara_uint(cdfid, nZId, start, edge,
    2447             :                                   static_cast<unsigned int *>(pImageNC));
    2448           6 :         if (status == NC_NOERR)
    2449           6 :             CheckData<unsigned int>(pImage, pImageNC, edge[nBandXPos],
    2450             :                                     nYChunkSize, false);
    2451             :     }
    2452          39 :     else if (eDataType == GDT_Int64)
    2453             :     {
    2454           7 :         status = nc_get_vara_longlong(cdfid, nZId, start, edge,
    2455             :                                       static_cast<long long *>(pImageNC));
    2456           7 :         if (status == NC_NOERR)
    2457           7 :             CheckData<std::int64_t>(pImage, pImageNC, edge[nBandXPos],
    2458             :                                     nYChunkSize, false);
    2459             :     }
    2460          32 :     else if (eDataType == GDT_UInt64)
    2461             :     {
    2462             :         status =
    2463           7 :             nc_get_vara_ulonglong(cdfid, nZId, start, edge,
    2464             :                                   static_cast<unsigned long long *>(pImageNC));
    2465           7 :         if (status == NC_NOERR)
    2466           7 :             CheckData<std::uint64_t>(pImage, pImageNC, edge[nBandXPos],
    2467             :                                      nYChunkSize, false);
    2468             :     }
    2469          25 :     else if (eDataType == GDT_CInt16)
    2470             :     {
    2471           0 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2472           0 :         if (status == NC_NOERR)
    2473           0 :             CheckDataCpx<short>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2474             :                                 false);
    2475             :     }
    2476          25 :     else if (eDataType == GDT_CInt32)
    2477             :     {
    2478           0 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2479           0 :         if (status == NC_NOERR)
    2480           0 :             CheckDataCpx<int>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2481             :                               false);
    2482             :     }
    2483          25 :     else if (eDataType == GDT_CFloat32)
    2484             :     {
    2485          20 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2486          20 :         if (status == NC_NOERR)
    2487          20 :             CheckDataCpx<float>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2488             :                                 false);
    2489             :     }
    2490           5 :     else if (eDataType == GDT_CFloat64)
    2491             :     {
    2492           5 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2493           5 :         if (status == NC_NOERR)
    2494           5 :             CheckDataCpx<double>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2495             :                                  false);
    2496             :     }
    2497             : 
    2498             :     else
    2499           0 :         status = NC_EBADTYPE;
    2500             : 
    2501        5757 :     if (status != NC_NOERR)
    2502             :     {
    2503           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    2504             :                  "netCDF chunk fetch failed: #%d (%s)", status,
    2505             :                  nc_strerror(status));
    2506           0 :         return false;
    2507             :     }
    2508        5757 :     return true;
    2509             : }
    2510             : 
    2511             : /************************************************************************/
    2512             : /*                             IReadBlock()                             */
    2513             : /************************************************************************/
    2514             : 
    2515        5757 : CPLErr netCDFRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
    2516             :                                     void *pImage)
    2517             : 
    2518             : {
    2519       11514 :     CPLMutexHolderD(&hNCMutex);
    2520             : 
    2521             :     // Locate X, Y and Z position in the array.
    2522             : 
    2523        5757 :     size_t xstart = static_cast<size_t>(nBlockXOff) * nBlockXSize;
    2524        5757 :     size_t ystart = 0;
    2525             : 
    2526             :     // Check y order.
    2527        5757 :     if (nBandYPos >= 0)
    2528             :     {
    2529        5753 :         auto poGDS = static_cast<netCDFDataset *>(poDS);
    2530        5753 :         if (poGDS->bBottomUp)
    2531             :         {
    2532        4838 :             if (nBlockYSize == 1)
    2533             :             {
    2534        4825 :                 ystart = nRasterYSize - 1 - nBlockYOff;
    2535             :             }
    2536             :             else
    2537             :             {
    2538             :                 // in GDAL space
    2539          13 :                 ystart = static_cast<size_t>(nBlockYOff) * nBlockYSize;
    2540             :                 const size_t yend =
    2541          26 :                     std::min(ystart + nBlockYSize - 1,
    2542          13 :                              static_cast<size_t>(nRasterYSize - 1));
    2543             :                 // in netCDF space
    2544          13 :                 const size_t nFirstChunkLine = nRasterYSize - 1 - yend;
    2545          13 :                 const size_t nLastChunkLine = nRasterYSize - 1 - ystart;
    2546          13 :                 const size_t nFirstChunkBlock = nFirstChunkLine / nBlockYSize;
    2547          13 :                 const size_t nLastChunkBlock = nLastChunkLine / nBlockYSize;
    2548             : 
    2549             :                 const auto firstKey = netCDFDataset::ChunkKey(
    2550          13 :                     nBlockXOff, nFirstChunkBlock, nBand);
    2551             :                 const auto secondKey =
    2552          13 :                     netCDFDataset::ChunkKey(nBlockXOff, nLastChunkBlock, nBand);
    2553             : 
    2554             :                 // Retrieve data from the one or 2 needed netCDF chunks
    2555          13 :                 std::shared_ptr<std::vector<GByte>> firstChunk;
    2556          13 :                 std::shared_ptr<std::vector<GByte>> secondChunk;
    2557          13 :                 if (poGDS->poChunkCache)
    2558             :                 {
    2559          13 :                     poGDS->poChunkCache->tryGet(firstKey, firstChunk);
    2560          13 :                     if (firstKey != secondKey)
    2561           6 :                         poGDS->poChunkCache->tryGet(secondKey, secondChunk);
    2562             :                 }
    2563             :                 const size_t nChunkLineSize =
    2564          13 :                     static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
    2565          13 :                     nBlockXSize;
    2566          13 :                 const size_t nChunkSize = nChunkLineSize * nBlockYSize;
    2567          13 :                 if (!firstChunk)
    2568             :                 {
    2569          11 :                     firstChunk.reset(new std::vector<GByte>(nChunkSize));
    2570          11 :                     if (!FetchNetcdfChunk(xstart,
    2571          11 :                                           nFirstChunkBlock * nBlockYSize,
    2572          11 :                                           firstChunk.get()->data()))
    2573           0 :                         return CE_Failure;
    2574          11 :                     if (poGDS->poChunkCache)
    2575          11 :                         poGDS->poChunkCache->insert(firstKey, firstChunk);
    2576             :                 }
    2577          13 :                 if (!secondChunk && firstKey != secondKey)
    2578             :                 {
    2579           2 :                     secondChunk.reset(new std::vector<GByte>(nChunkSize));
    2580           2 :                     if (!FetchNetcdfChunk(xstart, nLastChunkBlock * nBlockYSize,
    2581           2 :                                           secondChunk.get()->data()))
    2582           0 :                         return CE_Failure;
    2583           2 :                     if (poGDS->poChunkCache)
    2584           2 :                         poGDS->poChunkCache->insert(secondKey, secondChunk);
    2585             :                 }
    2586             : 
    2587             :                 // Assemble netCDF chunks into GDAL block
    2588          13 :                 GByte *pabyImage = static_cast<GByte *>(pImage);
    2589          13 :                 const size_t nFirstChunkBlockLine =
    2590          13 :                     nFirstChunkBlock * nBlockYSize;
    2591          13 :                 const size_t nLastChunkBlockLine =
    2592          13 :                     nLastChunkBlock * nBlockYSize;
    2593         146 :                 for (size_t iLine = ystart; iLine <= yend; iLine++)
    2594             :                 {
    2595         133 :                     const size_t nLineFromBottom = nRasterYSize - 1 - iLine;
    2596         133 :                     const size_t nChunkY = nLineFromBottom / nBlockYSize;
    2597         133 :                     if (nChunkY == nFirstChunkBlock)
    2598             :                     {
    2599         121 :                         memcpy(pabyImage + nChunkLineSize * (iLine - ystart),
    2600         121 :                                firstChunk.get()->data() +
    2601         121 :                                    (nLineFromBottom - nFirstChunkBlockLine) *
    2602             :                                        nChunkLineSize,
    2603             :                                nChunkLineSize);
    2604             :                     }
    2605             :                     else
    2606             :                     {
    2607          12 :                         CPLAssert(nChunkY == nLastChunkBlock);
    2608          12 :                         assert(secondChunk);
    2609          12 :                         memcpy(pabyImage + nChunkLineSize * (iLine - ystart),
    2610          12 :                                secondChunk.get()->data() +
    2611          12 :                                    (nLineFromBottom - nLastChunkBlockLine) *
    2612             :                                        nChunkLineSize,
    2613             :                                nChunkLineSize);
    2614             :                     }
    2615             :                 }
    2616          13 :                 return CE_None;
    2617             :             }
    2618             :         }
    2619             :         else
    2620             :         {
    2621         915 :             ystart = static_cast<size_t>(nBlockYOff) * nBlockYSize;
    2622             :         }
    2623             :     }
    2624             : 
    2625        5744 :     return FetchNetcdfChunk(xstart, ystart, pImage) ? CE_None : CE_Failure;
    2626             : }
    2627             : 
    2628             : /************************************************************************/
    2629             : /*                             IWriteBlock()                            */
    2630             : /************************************************************************/
    2631             : 
    2632        6401 : CPLErr netCDFRasterBand::IWriteBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
    2633             :                                      void *pImage)
    2634             : {
    2635       12802 :     CPLMutexHolderD(&hNCMutex);
    2636             : 
    2637             : #ifdef NCDF_DEBUG
    2638             :     if (nBlockYOff == 0 || (nBlockYOff == nRasterYSize - 1))
    2639             :         CPLDebug("GDAL_netCDF",
    2640             :                  "netCDFRasterBand::IWriteBlock( %d, %d, ...) nBand=%d",
    2641             :                  nBlockXOff, nBlockYOff, nBand);
    2642             : #endif
    2643             : 
    2644        6401 :     int nd = 0;
    2645        6401 :     nc_inq_varndims(cdfid, nZId, &nd);
    2646             : 
    2647             :     // Locate X, Y and Z position in the array.
    2648             : 
    2649             :     size_t start[MAX_NC_DIMS];
    2650        6401 :     memset(start, 0, sizeof(start));
    2651        6401 :     start[nBandXPos] = static_cast<size_t>(nBlockXOff) * nBlockXSize;
    2652             : 
    2653             :     // check y order.
    2654        6401 :     if (static_cast<netCDFDataset *>(poDS)->bBottomUp)
    2655             :     {
    2656        6377 :         if (nBlockYSize == 1)
    2657             :         {
    2658        6377 :             start[nBandYPos] = nRasterYSize - 1 - nBlockYOff;
    2659             :         }
    2660             :         else
    2661             :         {
    2662           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2663             :                      "nBlockYSize = %d, only 1 supported when "
    2664             :                      "writing bottom-up dataset",
    2665             :                      nBlockYSize);
    2666           0 :             return CE_Failure;
    2667             :         }
    2668             :     }
    2669             :     else
    2670             :     {
    2671          24 :         start[nBandYPos] = static_cast<size_t>(nBlockYOff) * nBlockYSize;  // y
    2672             :     }
    2673             : 
    2674        6401 :     size_t edge[MAX_NC_DIMS] = {};
    2675             : 
    2676        6401 :     edge[nBandXPos] = nBlockXSize;
    2677        6401 :     if ((start[nBandXPos] + edge[nBandXPos]) > (size_t)nRasterXSize)
    2678           0 :         edge[nBandXPos] = nRasterXSize - start[nBandXPos];
    2679        6401 :     edge[nBandYPos] = nBlockYSize;
    2680        6401 :     if ((start[nBandYPos] + edge[nBandYPos]) > (size_t)nRasterYSize)
    2681           0 :         edge[nBandYPos] = nRasterYSize - start[nBandYPos];
    2682             : 
    2683        6401 :     if (nd == 3)
    2684             :     {
    2685         610 :         start[panBandZPos[0]] = nLevel;  // z
    2686         610 :         edge[panBandZPos[0]] = 1;
    2687             :     }
    2688             : 
    2689             :     // Compute multidimention band position.
    2690             :     //
    2691             :     // BandPosition = (Total - sum(PastBandLevels) - 1)/sum(remainingLevels)
    2692             :     // if Data[2,3,4,x,y]
    2693             :     //
    2694             :     //  BandPos0 = (nBand) / (3*4)
    2695             :     //  BandPos1 = (nBand - (3*4)) / (4)
    2696             :     //  BandPos2 = (nBand - (3*4)) % (4)
    2697        6401 :     if (nd > 3)
    2698             :     {
    2699         178 :         int Sum = -1;
    2700         178 :         int Taken = 0;
    2701         534 :         for (int i = 0; i < nd - 2; i++)
    2702             :         {
    2703         356 :             if (i != nd - 2 - 1)
    2704             :             {
    2705         178 :                 Sum = 1;
    2706         356 :                 for (int j = i + 1; j < nd - 2; j++)
    2707             :                 {
    2708         178 :                     Sum *= panBandZLev[j];
    2709             :                 }
    2710         178 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) / Sum);
    2711         178 :                 edge[panBandZPos[i]] = 1;
    2712             :             }
    2713             :             else
    2714             :             {
    2715         178 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) % Sum);
    2716         178 :                 edge[panBandZPos[i]] = 1;
    2717             :             }
    2718         356 :             Taken += static_cast<int>(start[panBandZPos[i]]) * Sum;
    2719             :         }
    2720             :     }
    2721             : 
    2722             :     // Make sure we are in data mode.
    2723        6401 :     static_cast<netCDFDataset *>(poDS)->SetDefineMode(false);
    2724             : 
    2725             :     // Copy data according to type.
    2726        6401 :     int status = 0;
    2727        6401 :     if (eDataType == GDT_Byte)
    2728             :     {
    2729        5842 :         if (bSignedData)
    2730           0 :             status = nc_put_vara_schar(cdfid, nZId, start, edge,
    2731             :                                        static_cast<signed char *>(pImage));
    2732             :         else
    2733        5842 :             status = nc_put_vara_uchar(cdfid, nZId, start, edge,
    2734             :                                        static_cast<unsigned char *>(pImage));
    2735             :     }
    2736         559 :     else if (eDataType == GDT_Int8)
    2737             :     {
    2738          40 :         status = nc_put_vara_schar(cdfid, nZId, start, edge,
    2739             :                                    static_cast<signed char *>(pImage));
    2740             :     }
    2741         519 :     else if (nc_datatype == NC_SHORT)
    2742             :     {
    2743         101 :         status = nc_put_vara_short(cdfid, nZId, start, edge,
    2744             :                                    static_cast<short *>(pImage));
    2745             :     }
    2746         418 :     else if (eDataType == GDT_Int32)
    2747             :     {
    2748         210 :         status = nc_put_vara_int(cdfid, nZId, start, edge,
    2749             :                                  static_cast<int *>(pImage));
    2750             :     }
    2751         208 :     else if (eDataType == GDT_Float32)
    2752             :     {
    2753         128 :         status = nc_put_vara_float(cdfid, nZId, start, edge,
    2754             :                                    static_cast<float *>(pImage));
    2755             :     }
    2756          80 :     else if (eDataType == GDT_Float64)
    2757             :     {
    2758          50 :         status = nc_put_vara_double(cdfid, nZId, start, edge,
    2759             :                                     static_cast<double *>(pImage));
    2760             :     }
    2761          30 :     else if (eDataType == GDT_UInt16 &&
    2762          12 :              static_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2763             :     {
    2764          12 :         status = nc_put_vara_ushort(cdfid, nZId, start, edge,
    2765             :                                     static_cast<unsigned short *>(pImage));
    2766             :     }
    2767          18 :     else if (eDataType == GDT_UInt32 &&
    2768          12 :              static_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2769             :     {
    2770          12 :         status = nc_put_vara_uint(cdfid, nZId, start, edge,
    2771             :                                   static_cast<unsigned int *>(pImage));
    2772             :     }
    2773           6 :     else if (eDataType == GDT_UInt64 &&
    2774           3 :              static_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2775             :     {
    2776           3 :         status =
    2777           3 :             nc_put_vara_ulonglong(cdfid, nZId, start, edge,
    2778             :                                   static_cast<unsigned long long *>(pImage));
    2779             :     }
    2780           3 :     else if (eDataType == GDT_Int64 &&
    2781           3 :              static_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2782             :     {
    2783           3 :         status = nc_put_vara_longlong(cdfid, nZId, start, edge,
    2784             :                                       static_cast<long long *>(pImage));
    2785             :     }
    2786             :     else
    2787             :     {
    2788           0 :         CPLError(CE_Failure, CPLE_NotSupported,
    2789             :                  "The NetCDF driver does not support GDAL data type %d",
    2790           0 :                  eDataType);
    2791           0 :         status = NC_EBADTYPE;
    2792             :     }
    2793        6401 :     NCDF_ERR(status);
    2794             : 
    2795        6401 :     if (status != NC_NOERR)
    2796             :     {
    2797           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    2798             :                  "netCDF scanline write failed: %s", nc_strerror(status));
    2799           0 :         return CE_Failure;
    2800             :     }
    2801             : 
    2802        6401 :     return CE_None;
    2803             : }
    2804             : 
    2805             : /************************************************************************/
    2806             : /* ==================================================================== */
    2807             : /*                              netCDFDataset                           */
    2808             : /* ==================================================================== */
    2809             : /************************************************************************/
    2810             : 
    2811             : /************************************************************************/
    2812             : /*                           netCDFDataset()                            */
    2813             : /************************************************************************/
    2814             : 
    2815        1004 : netCDFDataset::netCDFDataset()
    2816             :     :
    2817             : // Basic dataset vars.
    2818             : #ifdef ENABLE_NCDUMP
    2819             :       bFileToDestroyAtClosing(false),
    2820             : #endif
    2821             :       cdfid(-1), nSubDatasets(0), papszSubDatasets(nullptr),
    2822             :       papszMetadata(nullptr), bBottomUp(true), eFormat(NCDF_FORMAT_NONE),
    2823             :       bIsGdalFile(false), bIsGdalCfFile(false), pszCFProjection(nullptr),
    2824             :       pszCFCoordinates(nullptr), nCFVersion(1.6), bSGSupport(false),
    2825        1004 :       eMultipleLayerBehavior(SINGLE_LAYER), logCount(0), vcdf(this, cdfid),
    2826        1004 :       GeometryScribe(vcdf, this->generateLogName()),
    2827        1004 :       FieldScribe(vcdf, this->generateLogName()),
    2828        2008 :       bufManager(CPLGetUsablePhysicalRAM() / 5),
    2829             : 
    2830             :       // projection/GT.
    2831             :       nXDimID(-1), nYDimID(-1), bIsProjected(false),
    2832             :       bIsGeographic(false),  // Can be not projected, and also not geographic
    2833             :       // State vars.
    2834             :       bDefineMode(true), bAddedGridMappingRef(false),
    2835             : 
    2836             :       // Create vars.
    2837             :       papszCreationOptions(nullptr), eCompress(NCDF_COMPRESS_NONE),
    2838             :       nZLevel(NCDF_DEFLATE_LEVEL), bChunking(false), nCreateMode(NC_CLOBBER),
    2839        3012 :       bSignedData(true)
    2840             : {
    2841        1004 :     m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2842             : 
    2843             :     // Set buffers
    2844        1004 :     bufManager.addBuffer(&(GeometryScribe.getMemBuffer()));
    2845        1004 :     bufManager.addBuffer(&(FieldScribe.getMemBuffer()));
    2846        1004 : }
    2847             : 
    2848             : /************************************************************************/
    2849             : /*                           ~netCDFDataset()                           */
    2850             : /************************************************************************/
    2851             : 
    2852        1934 : netCDFDataset::~netCDFDataset()
    2853             : 
    2854             : {
    2855        1004 :     netCDFDataset::Close();
    2856        1934 : }
    2857             : 
    2858             : /************************************************************************/
    2859             : /*                              Close()                                 */
    2860             : /************************************************************************/
    2861             : 
    2862        1750 : CPLErr netCDFDataset::Close()
    2863             : {
    2864        1750 :     CPLErr eErr = CE_None;
    2865        1750 :     if (nOpenFlags != OPEN_FLAGS_CLOSED)
    2866             :     {
    2867        2008 :         CPLMutexHolderD(&hNCMutex);
    2868             : 
    2869             : #ifdef NCDF_DEBUG
    2870             :         CPLDebug("GDAL_netCDF",
    2871             :                  "netCDFDataset::~netCDFDataset(), cdfid=%d filename=%s", cdfid,
    2872             :                  osFilename.c_str());
    2873             : #endif
    2874             : 
    2875             :         // Write data related to geotransform
    2876        1235 :         if (GetAccess() == GA_Update && !m_bAddedProjectionVarsData &&
    2877         231 :             (m_bHasProjection || m_bHasGeoTransform))
    2878             :         {
    2879             :             // Ensure projection is written if GeoTransform OR Projection are
    2880             :             // missing.
    2881          37 :             if (!m_bAddedProjectionVarsDefs)
    2882             :             {
    2883           2 :                 AddProjectionVars(true, nullptr, nullptr);
    2884             :             }
    2885          37 :             AddProjectionVars(false, nullptr, nullptr);
    2886             :         }
    2887             : 
    2888        1004 :         if (netCDFDataset::FlushCache(true) != CE_None)
    2889           0 :             eErr = CE_Failure;
    2890             : 
    2891        1004 :         if (GetAccess() == GA_Update && !SGCommitPendingTransaction())
    2892           0 :             eErr = CE_Failure;
    2893             : 
    2894        1006 :         for (size_t i = 0; i < apoVectorDatasets.size(); i++)
    2895           2 :             delete apoVectorDatasets[i];
    2896             : 
    2897             :         // Make sure projection variable is written to band variable.
    2898        1004 :         if (GetAccess() == GA_Update && !bAddedGridMappingRef)
    2899             :         {
    2900         248 :             if (!AddGridMappingRef())
    2901           0 :                 eErr = CE_Failure;
    2902             :         }
    2903             : 
    2904        1004 :         CSLDestroy(papszMetadata);
    2905        1004 :         CSLDestroy(papszSubDatasets);
    2906        1004 :         CSLDestroy(papszCreationOptions);
    2907             : 
    2908        1004 :         CPLFree(pszCFProjection);
    2909             : 
    2910        1004 :         if (cdfid > 0)
    2911             :         {
    2912             : #ifdef NCDF_DEBUG
    2913             :             CPLDebug("GDAL_netCDF", "calling nc_close( %d)", cdfid);
    2914             : #endif
    2915         658 :             int status = GDAL_nc_close(cdfid);
    2916             : #ifdef ENABLE_UFFD
    2917         658 :             NETCDF_UFFD_UNMAP(pCtx);
    2918             : #endif
    2919         658 :             NCDF_ERR(status);
    2920         658 :             if (status != NC_NOERR)
    2921           0 :                 eErr = CE_Failure;
    2922             :         }
    2923             : 
    2924        1004 :         if (fpVSIMEM)
    2925          15 :             VSIFCloseL(fpVSIMEM);
    2926             : 
    2927             : #ifdef ENABLE_NCDUMP
    2928        1004 :         if (bFileToDestroyAtClosing)
    2929           0 :             VSIUnlink(osFilename);
    2930             : #endif
    2931             : 
    2932        1004 :         if (GDALPamDataset::Close() != CE_None)
    2933           0 :             eErr = CE_Failure;
    2934             :     }
    2935        1750 :     return eErr;
    2936             : }
    2937             : 
    2938             : /************************************************************************/
    2939             : /*                            SetDefineMode()                           */
    2940             : /************************************************************************/
    2941       14226 : bool netCDFDataset::SetDefineMode(bool bNewDefineMode)
    2942             : {
    2943             :     // Do nothing if already in new define mode
    2944             :     // or if dataset is in read-only mode or if dataset is true NC4 dataset.
    2945       14785 :     if (bDefineMode == bNewDefineMode || GetAccess() == GA_ReadOnly ||
    2946         559 :         eFormat == NCDF_FORMAT_NC4)
    2947       13814 :         return true;
    2948             : 
    2949         412 :     CPLDebug("GDAL_netCDF", "SetDefineMode(%d) old=%d",
    2950         412 :              static_cast<int>(bNewDefineMode), static_cast<int>(bDefineMode));
    2951             : 
    2952         412 :     bDefineMode = bNewDefineMode;
    2953             : 
    2954             :     int status;
    2955         412 :     if (bDefineMode)
    2956         143 :         status = nc_redef(cdfid);
    2957             :     else
    2958         269 :         status = nc_enddef(cdfid);
    2959             : 
    2960         412 :     NCDF_ERR(status);
    2961         412 :     return status == NC_NOERR;
    2962             : }
    2963             : 
    2964             : /************************************************************************/
    2965             : /*                      GetMetadataDomainList()                         */
    2966             : /************************************************************************/
    2967             : 
    2968          27 : char **netCDFDataset::GetMetadataDomainList()
    2969             : {
    2970          27 :     char **papszDomains = BuildMetadataDomainList(
    2971             :         GDALDataset::GetMetadataDomainList(), TRUE, "SUBDATASETS", nullptr);
    2972          28 :     for (const auto &kv : m_oMapDomainToJSon)
    2973           1 :         papszDomains = CSLAddString(papszDomains, ("json:" + kv.first).c_str());
    2974          27 :     return papszDomains;
    2975             : }
    2976             : 
    2977             : /************************************************************************/
    2978             : /*                            GetMetadata()                             */
    2979             : /************************************************************************/
    2980         368 : char **netCDFDataset::GetMetadata(const char *pszDomain)
    2981             : {
    2982         368 :     if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "SUBDATASETS"))
    2983          38 :         return papszSubDatasets;
    2984             : 
    2985         330 :     if (pszDomain != nullptr && STARTS_WITH(pszDomain, "json:"))
    2986             :     {
    2987           1 :         auto iter = m_oMapDomainToJSon.find(pszDomain + strlen("json:"));
    2988           1 :         if (iter != m_oMapDomainToJSon.end())
    2989           1 :             return iter->second.List();
    2990             :     }
    2991             : 
    2992         329 :     return GDALDataset::GetMetadata(pszDomain);
    2993             : }
    2994             : 
    2995             : /************************************************************************/
    2996             : /*                        SetMetadataItem()                             */
    2997             : /************************************************************************/
    2998             : 
    2999          43 : CPLErr netCDFDataset::SetMetadataItem(const char *pszName, const char *pszValue,
    3000             :                                       const char *pszDomain)
    3001             : {
    3002          85 :     if (GetAccess() == GA_Update &&
    3003          85 :         (pszDomain == nullptr || pszDomain[0] == '\0') && pszValue != nullptr)
    3004             :     {
    3005          42 :         std::string osName(pszName);
    3006             : 
    3007             :         // Same logic as in CopyMetadata()
    3008          42 :         if (STARTS_WITH(osName.c_str(), "NC_GLOBAL#"))
    3009           8 :             osName = osName.substr(strlen("NC_GLOBAL#"));
    3010          34 :         else if (strchr(osName.c_str(), '#') == nullptr)
    3011           5 :             osName = "GDAL_" + osName;
    3012             : 
    3013          84 :         if (STARTS_WITH(osName.c_str(), "NETCDF_DIM_") ||
    3014          42 :             strchr(osName.c_str(), '#') != nullptr)
    3015             :         {
    3016             :             // do nothing
    3017          29 :             return CE_None;
    3018             :         }
    3019             :         else
    3020             :         {
    3021          13 :             SetDefineMode(true);
    3022             : 
    3023          13 :             if (!NCDFPutAttr(cdfid, NC_GLOBAL, osName.c_str(), pszValue))
    3024          13 :                 return CE_Failure;
    3025             :         }
    3026             :     }
    3027             : 
    3028           1 :     return GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
    3029             : }
    3030             : 
    3031             : /************************************************************************/
    3032             : /*                          SetMetadata()                               */
    3033             : /************************************************************************/
    3034             : 
    3035           8 : CPLErr netCDFDataset::SetMetadata(char **papszMD, const char *pszDomain)
    3036             : {
    3037          13 :     if (GetAccess() == GA_Update &&
    3038           5 :         (pszDomain == nullptr || pszDomain[0] == '\0'))
    3039             :     {
    3040             :         // We don't handle metadata item removal for now
    3041          50 :         for (const char *const *papszIter = papszMD; papszIter && *papszIter;
    3042             :              ++papszIter)
    3043             :         {
    3044          42 :             char *pszName = nullptr;
    3045          42 :             const char *pszValue = CPLParseNameValue(*papszIter, &pszName);
    3046          42 :             if (pszName && pszValue)
    3047          42 :                 SetMetadataItem(pszName, pszValue);
    3048          42 :             CPLFree(pszName);
    3049             :         }
    3050           8 :         return CE_None;
    3051             :     }
    3052           0 :     return GDALPamDataset::SetMetadata(papszMD, pszDomain);
    3053             : }
    3054             : 
    3055             : /************************************************************************/
    3056             : /*                          GetSpatialRef()                             */
    3057             : /************************************************************************/
    3058             : 
    3059         184 : const OGRSpatialReference *netCDFDataset::GetSpatialRef() const
    3060             : {
    3061         184 :     if (m_bHasProjection)
    3062          74 :         return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
    3063             : 
    3064         110 :     return GDALPamDataset::GetSpatialRef();
    3065             : }
    3066             : 
    3067             : /************************************************************************/
    3068             : /*                           FetchCopyParam()                            */
    3069             : /************************************************************************/
    3070             : 
    3071         436 : double netCDFDataset::FetchCopyParam(const char *pszGridMappingValue,
    3072             :                                      const char *pszParam, double dfDefault,
    3073             :                                      bool *pbFound)
    3074             : 
    3075             : {
    3076             :     char *pszTemp =
    3077         436 :         CPLStrdup(CPLSPrintf("%s#%s", pszGridMappingValue, pszParam));
    3078         436 :     const char *pszValue = CSLFetchNameValue(papszMetadata, pszTemp);
    3079         436 :     CPLFree(pszTemp);
    3080             : 
    3081         436 :     if (pbFound)
    3082             :     {
    3083         436 :         *pbFound = (pszValue != nullptr);
    3084             :     }
    3085             : 
    3086         436 :     if (pszValue)
    3087             :     {
    3088           0 :         return CPLAtofM(pszValue);
    3089             :     }
    3090             : 
    3091         436 :     return dfDefault;
    3092             : }
    3093             : 
    3094             : /************************************************************************/
    3095             : /*                           FetchStandardParallels()                   */
    3096             : /************************************************************************/
    3097             : 
    3098             : std::vector<std::string>
    3099           0 : netCDFDataset::FetchStandardParallels(const char *pszGridMappingValue)
    3100             : {
    3101             :     // cf-1.0 tags
    3102           0 :     const char *pszValue = FetchAttr(pszGridMappingValue, CF_PP_STD_PARALLEL);
    3103             : 
    3104           0 :     std::vector<std::string> ret;
    3105           0 :     if (pszValue != nullptr)
    3106             :     {
    3107           0 :         CPLStringList aosValues;
    3108           0 :         if (pszValue[0] != '{' &&
    3109           0 :             CPLString(pszValue).Trim().find(' ') != std::string::npos)
    3110             :         {
    3111             :             // Some files like
    3112             :             // ftp://data.knmi.nl/download/KNW-NetCDF-3D/1.0/noversion/2013/11/14/KNW-1.0_H37-ERA_NL_20131114.nc
    3113             :             // do not use standard formatting for arrays, but just space
    3114             :             // separated syntax
    3115           0 :             aosValues = CSLTokenizeString2(pszValue, " ", 0);
    3116             :         }
    3117             :         else
    3118             :         {
    3119           0 :             aosValues = NCDFTokenizeArray(pszValue);
    3120             :         }
    3121           0 :         for (int i = 0; i < aosValues.size(); i++)
    3122             :         {
    3123           0 :             ret.push_back(aosValues[i]);
    3124             :         }
    3125             :     }
    3126             :     // Try gdal tags.
    3127             :     else
    3128             :     {
    3129           0 :         pszValue = FetchAttr(pszGridMappingValue, CF_PP_STD_PARALLEL_1);
    3130             : 
    3131           0 :         if (pszValue != nullptr)
    3132           0 :             ret.push_back(pszValue);
    3133             : 
    3134           0 :         pszValue = FetchAttr(pszGridMappingValue, CF_PP_STD_PARALLEL_2);
    3135             : 
    3136           0 :         if (pszValue != nullptr)
    3137           0 :             ret.push_back(pszValue);
    3138             :     }
    3139             : 
    3140           0 :     return ret;
    3141             : }
    3142             : 
    3143             : /************************************************************************/
    3144             : /*                           FetchAttr()                                */
    3145             : /************************************************************************/
    3146             : 
    3147        3703 : const char *netCDFDataset::FetchAttr(const char *pszVarFullName,
    3148             :                                      const char *pszAttr)
    3149             : 
    3150             : {
    3151        3703 :     char *pszKey = CPLStrdup(CPLSPrintf("%s#%s", pszVarFullName, pszAttr));
    3152        3703 :     const char *pszValue = CSLFetchNameValue(papszMetadata, pszKey);
    3153        3703 :     CPLFree(pszKey);
    3154        3703 :     return pszValue;
    3155             : }
    3156             : 
    3157        2454 : const char *netCDFDataset::FetchAttr(int nGroupId, int nVarId,
    3158             :                                      const char *pszAttr)
    3159             : 
    3160             : {
    3161        2454 :     char *pszVarFullName = nullptr;
    3162        2454 :     NCDFGetVarFullName(nGroupId, nVarId, &pszVarFullName);
    3163        2454 :     const char *pszValue = FetchAttr(pszVarFullName, pszAttr);
    3164        2454 :     CPLFree(pszVarFullName);
    3165        2454 :     return pszValue;
    3166             : }
    3167             : 
    3168             : /************************************************************************/
    3169             : /*                       IsDifferenceBelow()                            */
    3170             : /************************************************************************/
    3171             : 
    3172        1085 : static bool IsDifferenceBelow(double dfA, double dfB, double dfError)
    3173             : {
    3174        1085 :     const double dfAbsDiff = fabs(dfA - dfB);
    3175        1085 :     return dfAbsDiff <= dfError;
    3176             : }
    3177             : 
    3178             : /************************************************************************/
    3179             : /*                      SetProjectionFromVar()                          */
    3180             : /************************************************************************/
    3181         514 : void netCDFDataset::SetProjectionFromVar(
    3182             :     int nGroupId, int nVarId, bool bReadSRSOnly, const char *pszGivenGM,
    3183             :     std::string *returnProjStr, nccfdriver::SGeometry_Reader *sg,
    3184             :     std::vector<std::string> *paosRemovedMDItems)
    3185             : {
    3186         514 :     bool bGotGeogCS = false;
    3187         514 :     bool bGotCfSRS = false;
    3188         514 :     bool bGotCfWktSRS = false;
    3189         514 :     bool bGotGdalSRS = false;
    3190         514 :     bool bGotCfGT = false;
    3191         514 :     bool bGotGdalGT = false;
    3192             : 
    3193             :     // These values from CF metadata.
    3194         514 :     OGRSpatialReference oSRS;
    3195         514 :     oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3196         514 :     size_t xdim = nRasterXSize;
    3197         514 :     size_t ydim = nRasterYSize;
    3198             : 
    3199             :     // These values from GDAL metadata.
    3200         514 :     const char *pszWKT = nullptr;
    3201         514 :     const char *pszGeoTransform = nullptr;
    3202             : 
    3203         514 :     netCDFDataset *poDS = this;  // Perhaps this should be removed for clarity.
    3204             : 
    3205         514 :     CPLDebug("GDAL_netCDF", "\n=====\nSetProjectionFromVar( %d, %d)", nGroupId,
    3206             :              nVarId);
    3207             : 
    3208             :     // Get x/y range information.
    3209             : 
    3210             :     // Temp variables to use in SetGeoTransform() and SetProjection().
    3211         514 :     GDALGeoTransform tmpGT;
    3212             : 
    3213             :     // Look for grid_mapping metadata.
    3214         514 :     const char *pszValue = pszGivenGM;
    3215         514 :     CPLString osTmpGridMapping;  // let is in this outer scope as pszValue may
    3216             :         // point to it
    3217         514 :     if (pszValue == nullptr)
    3218             :     {
    3219         471 :         pszValue = FetchAttr(nGroupId, nVarId, CF_GRD_MAPPING);
    3220         471 :         if (pszValue && strchr(pszValue, ':') && strchr(pszValue, ' '))
    3221             :         {
    3222             :             // Expanded form of grid_mapping
    3223             :             // e.g. "crsOSGB: x y crsWGS84: lat lon"
    3224             :             // Pickup the grid_mapping whose coordinates are dimensions of the
    3225             :             // variable
    3226           6 :             CPLStringList aosTokens(CSLTokenizeString2(pszValue, " ", 0));
    3227           3 :             if ((aosTokens.size() % 3) == 0)
    3228             :             {
    3229           3 :                 for (int i = 0; i < aosTokens.size() / 3; i++)
    3230             :                 {
    3231           3 :                     if (CSLFindString(poDS->papszDimName,
    3232           9 :                                       aosTokens[3 * i + 1]) >= 0 &&
    3233           3 :                         CSLFindString(poDS->papszDimName,
    3234           3 :                                       aosTokens[3 * i + 2]) >= 0)
    3235             :                     {
    3236           3 :                         osTmpGridMapping = aosTokens[3 * i];
    3237           6 :                         if (!osTmpGridMapping.empty() &&
    3238           3 :                             osTmpGridMapping.back() == ':')
    3239             :                         {
    3240           3 :                             osTmpGridMapping.resize(osTmpGridMapping.size() -
    3241             :                                                     1);
    3242             :                         }
    3243           3 :                         pszValue = osTmpGridMapping.c_str();
    3244           3 :                         break;
    3245             :                     }
    3246             :                 }
    3247             :             }
    3248             :         }
    3249             :     }
    3250         514 :     char *pszGridMappingValue = CPLStrdup(pszValue ? pszValue : "");
    3251             : 
    3252         514 :     if (!EQUAL(pszGridMappingValue, ""))
    3253             :     {
    3254             :         // Read grid_mapping metadata.
    3255         219 :         int nProjGroupID = -1;
    3256         219 :         int nProjVarID = -1;
    3257         219 :         if (NCDFResolveVar(nGroupId, pszGridMappingValue, &nProjGroupID,
    3258         219 :                            &nProjVarID) == CE_None)
    3259             :         {
    3260         218 :             poDS->ReadAttributes(nProjGroupID, nProjVarID);
    3261             : 
    3262             :             // Look for GDAL spatial_ref and GeoTransform within grid_mapping.
    3263         218 :             CPLFree(pszGridMappingValue);
    3264         218 :             pszGridMappingValue = nullptr;
    3265         218 :             NCDFGetVarFullName(nProjGroupID, nProjVarID, &pszGridMappingValue);
    3266         218 :             if (pszGridMappingValue)
    3267             :             {
    3268         218 :                 CPLDebug("GDAL_netCDF", "got grid_mapping %s",
    3269             :                          pszGridMappingValue);
    3270         218 :                 pszWKT = FetchAttr(pszGridMappingValue, NCDF_SPATIAL_REF);
    3271         218 :                 if (!pszWKT)
    3272             :                 {
    3273          34 :                     pszWKT = FetchAttr(pszGridMappingValue, NCDF_CRS_WKT);
    3274             :                 }
    3275             :                 else
    3276             :                 {
    3277         184 :                     bGotGdalSRS = true;
    3278         184 :                     CPLDebug("GDAL_netCDF", "setting WKT from GDAL");
    3279             :                 }
    3280         218 :                 if (pszWKT)
    3281             :                 {
    3282         188 :                     if (!bGotGdalSRS)
    3283             :                     {
    3284           4 :                         bGotCfWktSRS = true;
    3285           4 :                         CPLDebug("GDAL_netCDF", "setting WKT from CF");
    3286             :                     }
    3287         188 :                     if (returnProjStr != nullptr)
    3288             :                     {
    3289          41 :                         (*returnProjStr) = std::string(pszWKT);
    3290             :                     }
    3291             :                     else
    3292             :                     {
    3293         147 :                         m_bAddedProjectionVarsDefs = true;
    3294         147 :                         m_bAddedProjectionVarsData = true;
    3295         294 :                         OGRSpatialReference oSRSTmp;
    3296         147 :                         oSRSTmp.SetAxisMappingStrategy(
    3297             :                             OAMS_TRADITIONAL_GIS_ORDER);
    3298         147 :                         oSRSTmp.importFromWkt(pszWKT);
    3299         147 :                         SetSpatialRefNoUpdate(&oSRSTmp);
    3300             :                     }
    3301             :                     pszGeoTransform =
    3302         188 :                         FetchAttr(pszGridMappingValue, NCDF_GEOTRANSFORM);
    3303             :                 }
    3304             :             }
    3305             :             else
    3306             :             {
    3307           0 :                 pszGridMappingValue = CPLStrdup("");
    3308             :             }
    3309             :         }
    3310             :     }
    3311             : 
    3312             :     // Get information about the file.
    3313             :     //
    3314             :     // Was this file created by the GDAL netcdf driver?
    3315             :     // Was this file created by the newer (CF-conformant) driver?
    3316             :     //
    3317             :     // 1) If GDAL netcdf metadata is set, and version >= 1.9,
    3318             :     //    it was created with the new driver
    3319             :     // 2) Else, if spatial_ref and GeoTransform are present in the
    3320             :     //    grid_mapping variable, it was created by the old driver
    3321         514 :     pszValue = FetchAttr("NC_GLOBAL", "GDAL");
    3322             : 
    3323         514 :     if (pszValue && NCDFIsGDALVersionGTE(pszValue, 1900))
    3324             :     {
    3325         241 :         bIsGdalFile = true;
    3326         241 :         bIsGdalCfFile = true;
    3327             :     }
    3328         273 :     else if (pszWKT != nullptr && pszGeoTransform != nullptr)
    3329             :     {
    3330          17 :         bIsGdalFile = true;
    3331          17 :         bIsGdalCfFile = false;
    3332             :     }
    3333             : 
    3334             :     // Set default bottom-up default value.
    3335             :     // Y axis dimension and absence of GT can modify this value.
    3336             :     // Override with Config option GDAL_NETCDF_BOTTOMUP.
    3337             : 
    3338             :     // New driver is bottom-up by default.
    3339         514 :     if ((bIsGdalFile && !bIsGdalCfFile) || bSwitchedXY)
    3340          19 :         poDS->bBottomUp = false;
    3341             :     else
    3342         495 :         poDS->bBottomUp = true;
    3343             : 
    3344         514 :     CPLDebug("GDAL_netCDF",
    3345             :              "bIsGdalFile=%d bIsGdalCfFile=%d bSwitchedXY=%d bBottomUp=%d",
    3346         514 :              static_cast<int>(bIsGdalFile), static_cast<int>(bIsGdalCfFile),
    3347         514 :              static_cast<int>(bSwitchedXY), static_cast<int>(bBottomUp));
    3348             : 
    3349             :     // Read projection coordinates.
    3350             : 
    3351         514 :     int nGroupDimXID = -1;
    3352         514 :     int nVarDimXID = -1;
    3353         514 :     int nGroupDimYID = -1;
    3354         514 :     int nVarDimYID = -1;
    3355         514 :     if (sg != nullptr)
    3356             :     {
    3357          43 :         nGroupDimXID = sg->get_ncID();
    3358          43 :         nGroupDimYID = sg->get_ncID();
    3359          43 :         nVarDimXID = sg->getNodeCoordVars()[0];
    3360          43 :         nVarDimYID = sg->getNodeCoordVars()[1];
    3361             :     }
    3362             : 
    3363         514 :     if (!bReadSRSOnly)
    3364             :     {
    3365         348 :         NCDFResolveVar(nGroupId, poDS->papszDimName[nXDimID], &nGroupDimXID,
    3366             :                        &nVarDimXID);
    3367         348 :         NCDFResolveVar(nGroupId, poDS->papszDimName[nYDimID], &nGroupDimYID,
    3368             :                        &nVarDimYID);
    3369             :         // TODO: if above resolving fails we should also search for coordinate
    3370             :         // variables without same name than dimension using the same resolving
    3371             :         // logic. This should handle for example NASA Ocean Color L2 products.
    3372             : 
    3373             :         const bool bIgnoreXYAxisNameChecks =
    3374         696 :             CPLTestBool(CSLFetchNameValueDef(
    3375         348 :                 papszOpenOptions, "IGNORE_XY_AXIS_NAME_CHECKS",
    3376             :                 CPLGetConfigOption("GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS",
    3377         348 :                                    "NO"))) ||
    3378             :             // Dataset from https://github.com/OSGeo/gdal/issues/4075 has a res
    3379             :             // and transform attributes
    3380         348 :             (FetchAttr(nGroupId, nVarId, "res") != nullptr &&
    3381         696 :              FetchAttr(nGroupId, nVarId, "transform") != nullptr) ||
    3382         347 :             FetchAttr(nGroupId, NC_GLOBAL, "GMT_version") != nullptr;
    3383             : 
    3384             :         // Check that they are 1D or 2D variables
    3385         348 :         if (nVarDimXID >= 0)
    3386             :         {
    3387         254 :             int ndims = -1;
    3388         254 :             nc_inq_varndims(nGroupId, nVarDimXID, &ndims);
    3389         254 :             if (ndims == 0 || ndims > 2)
    3390           0 :                 nVarDimXID = -1;
    3391         254 :             else if (!bIgnoreXYAxisNameChecks)
    3392             :             {
    3393         252 :                 if (!NCDFIsVarLongitude(nGroupId, nVarDimXID, nullptr) &&
    3394         162 :                     !NCDFIsVarProjectionX(nGroupId, nVarDimXID, nullptr) &&
    3395             :                     // In case of inversion of X/Y
    3396         446 :                     !NCDFIsVarLatitude(nGroupId, nVarDimXID, nullptr) &&
    3397          32 :                     !NCDFIsVarProjectionY(nGroupId, nVarDimXID, nullptr))
    3398             :                 {
    3399             :                     char szVarNameX[NC_MAX_NAME + 1];
    3400          32 :                     CPL_IGNORE_RET_VAL(
    3401          32 :                         nc_inq_varname(nGroupId, nVarDimXID, szVarNameX));
    3402          32 :                     if (!(ndims == 1 &&
    3403          31 :                           (EQUAL(szVarNameX, CF_LONGITUDE_STD_NAME) ||
    3404          30 :                            EQUAL(szVarNameX, CF_LONGITUDE_VAR_NAME))))
    3405             :                     {
    3406          31 :                         CPLDebug(
    3407             :                             "netCDF",
    3408             :                             "Georeferencing ignored due to non-specific "
    3409             :                             "enough X axis name. "
    3410             :                             "Set GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS=YES "
    3411             :                             "as configuration option to bypass this check");
    3412          31 :                         nVarDimXID = -1;
    3413             :                     }
    3414             :                 }
    3415             :             }
    3416             :         }
    3417             : 
    3418         348 :         if (nVarDimYID >= 0)
    3419             :         {
    3420         256 :             int ndims = -1;
    3421         256 :             nc_inq_varndims(nGroupId, nVarDimYID, &ndims);
    3422         256 :             if (ndims == 0 || ndims > 2)
    3423           1 :                 nVarDimYID = -1;
    3424         255 :             else if (!bIgnoreXYAxisNameChecks)
    3425             :             {
    3426         253 :                 if (!NCDFIsVarLatitude(nGroupId, nVarDimYID, nullptr) &&
    3427         163 :                     !NCDFIsVarProjectionY(nGroupId, nVarDimYID, nullptr) &&
    3428             :                     // In case of inversion of X/Y
    3429         449 :                     !NCDFIsVarLongitude(nGroupId, nVarDimYID, nullptr) &&
    3430          33 :                     !NCDFIsVarProjectionX(nGroupId, nVarDimYID, nullptr))
    3431             :                 {
    3432             :                     char szVarNameY[NC_MAX_NAME + 1];
    3433          33 :                     CPL_IGNORE_RET_VAL(
    3434          33 :                         nc_inq_varname(nGroupId, nVarDimYID, szVarNameY));
    3435          33 :                     if (!(ndims == 1 &&
    3436          33 :                           (EQUAL(szVarNameY, CF_LATITUDE_STD_NAME) ||
    3437          32 :                            EQUAL(szVarNameY, CF_LATITUDE_VAR_NAME))))
    3438             :                     {
    3439          32 :                         CPLDebug(
    3440             :                             "netCDF",
    3441             :                             "Georeferencing ignored due to non-specific "
    3442             :                             "enough Y axis name. "
    3443             :                             "Set GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS=YES "
    3444             :                             "as configuration option to bypass this check");
    3445          32 :                         nVarDimYID = -1;
    3446             :                     }
    3447             :                 }
    3448             :             }
    3449             :         }
    3450             : 
    3451         348 :         if ((nVarDimXID >= 0 && xdim == 1) || (nVarDimXID >= 0 && ydim == 1))
    3452             :         {
    3453           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    3454             :                      "1-pixel width/height files not supported, "
    3455             :                      "xdim: %ld ydim: %ld",
    3456             :                      static_cast<long>(xdim), static_cast<long>(ydim));
    3457           0 :             nVarDimXID = -1;
    3458           0 :             nVarDimYID = -1;
    3459             :         }
    3460             :     }
    3461             : 
    3462         514 :     const char *pszUnits = nullptr;
    3463         514 :     if ((nVarDimXID != -1) && (nVarDimYID != -1) && xdim > 0 && ydim > 0)
    3464             :     {
    3465         266 :         const char *pszUnitsX = FetchAttr(nGroupDimXID, nVarDimXID, "units");
    3466         266 :         const char *pszUnitsY = FetchAttr(nGroupDimYID, nVarDimYID, "units");
    3467             :         // Normalize degrees_east/degrees_north to degrees
    3468             :         // Cf https://github.com/OSGeo/gdal/issues/11009
    3469         266 :         if (pszUnitsX && EQUAL(pszUnitsX, "degrees_east"))
    3470          79 :             pszUnitsX = "degrees";
    3471         266 :         if (pszUnitsY && EQUAL(pszUnitsY, "degrees_north"))
    3472          79 :             pszUnitsY = "degrees";
    3473             : 
    3474         266 :         if (pszUnitsX && pszUnitsY)
    3475             :         {
    3476         219 :             if (EQUAL(pszUnitsX, pszUnitsY))
    3477         216 :                 pszUnits = pszUnitsX;
    3478           3 :             else if (!pszWKT && !EQUAL(pszGridMappingValue, ""))
    3479             :             {
    3480           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    3481             :                          "X axis unit (%s) is different from Y axis "
    3482             :                          "unit (%s). SRS will ignore axis unit and be "
    3483             :                          "likely wrong.",
    3484             :                          pszUnitsX, pszUnitsY);
    3485             :             }
    3486             :         }
    3487          47 :         else if (pszUnitsX && !pszWKT && !EQUAL(pszGridMappingValue, ""))
    3488             :         {
    3489           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3490             :                      "X axis unit is defined, but not Y one ."
    3491             :                      "SRS will ignore axis unit and be likely wrong.");
    3492             :         }
    3493          47 :         else if (pszUnitsY && !pszWKT && !EQUAL(pszGridMappingValue, ""))
    3494             :         {
    3495           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3496             :                      "Y axis unit is defined, but not X one ."
    3497             :                      "SRS will ignore axis unit and be likely wrong.");
    3498             :         }
    3499             :     }
    3500             : 
    3501         514 :     if (!pszWKT && !EQUAL(pszGridMappingValue, ""))
    3502             :     {
    3503          31 :         CPLStringList aosGridMappingKeyValues;
    3504          31 :         const size_t nLenGridMappingValue = strlen(pszGridMappingValue);
    3505         789 :         for (const char *const *papszIter = papszMetadata;
    3506         789 :              papszIter && *papszIter; ++papszIter)
    3507             :         {
    3508         758 :             if (STARTS_WITH(*papszIter, pszGridMappingValue) &&
    3509         236 :                 (*papszIter)[nLenGridMappingValue] == '#')
    3510             :             {
    3511         236 :                 char *pszKey = nullptr;
    3512         472 :                 pszValue = CPLParseNameValue(
    3513         236 :                     *papszIter + nLenGridMappingValue + 1, &pszKey);
    3514         236 :                 if (pszKey && pszValue)
    3515         236 :                     aosGridMappingKeyValues.SetNameValue(pszKey, pszValue);
    3516         236 :                 CPLFree(pszKey);
    3517             :             }
    3518             :         }
    3519             : 
    3520          31 :         bGotGeogCS = aosGridMappingKeyValues.FetchNameValue(
    3521             :                          CF_PP_SEMI_MAJOR_AXIS) != nullptr;
    3522             : 
    3523          31 :         oSRS.importFromCF1(aosGridMappingKeyValues.List(), pszUnits);
    3524          31 :         bGotCfSRS = oSRS.IsGeographic() || oSRS.IsProjected();
    3525             :     }
    3526             :     else
    3527             :     {
    3528             :         // Dataset from https://github.com/OSGeo/gdal/issues/4075 has a "crs"
    3529             :         // attribute hold on the variable of interest that contains a PROJ.4
    3530             :         // string
    3531         483 :         pszValue = FetchAttr(nGroupId, nVarId, "crs");
    3532         484 :         if (pszValue &&
    3533           1 :             (strstr(pszValue, "+proj=") != nullptr ||
    3534           0 :              strstr(pszValue, "GEOGCS") != nullptr ||
    3535           0 :              strstr(pszValue, "PROJCS") != nullptr ||
    3536         484 :              strstr(pszValue, "EPSG:") != nullptr) &&
    3537           1 :             oSRS.SetFromUserInput(pszValue) == OGRERR_NONE)
    3538             :         {
    3539           1 :             bGotCfSRS = true;
    3540             :         }
    3541             :     }
    3542             : 
    3543             :     // Set Projection from CF.
    3544         514 :     double dfLinearUnitsConvFactor = 1.0;
    3545         514 :     if ((bGotGeogCS || bGotCfSRS))
    3546             :     {
    3547          31 :         if ((nVarDimXID != -1) && (nVarDimYID != -1) && xdim > 0 && ydim > 0)
    3548             :         {
    3549             :             // Set SRS Units.
    3550             : 
    3551             :             // Check units for x and y.
    3552          28 :             if (oSRS.IsProjected())
    3553             :             {
    3554          25 :                 dfLinearUnitsConvFactor = oSRS.GetLinearUnits(nullptr);
    3555             : 
    3556             :                 // If the user doesn't ask to preserve the axis unit,
    3557             :                 // then normalize to metre
    3558          31 :                 if (dfLinearUnitsConvFactor != 1.0 &&
    3559           6 :                     !CPLFetchBool(GetOpenOptions(), "PRESERVE_AXIS_UNIT_IN_CRS",
    3560             :                                   false))
    3561             :                 {
    3562           5 :                     oSRS.SetLinearUnits("metre", 1.0);
    3563           5 :                     oSRS.SetAuthority("PROJCS|UNIT", "EPSG", 9001);
    3564             :                 }
    3565             :                 else
    3566             :                 {
    3567          20 :                     dfLinearUnitsConvFactor = 1.0;
    3568             :                 }
    3569             :             }
    3570             :         }
    3571             : 
    3572             :         // Set projection.
    3573          31 :         char *pszTempProjection = nullptr;
    3574          31 :         oSRS.exportToWkt(&pszTempProjection);
    3575          31 :         if (pszTempProjection)
    3576             :         {
    3577          31 :             CPLDebug("GDAL_netCDF", "setting WKT from CF");
    3578          31 :             if (returnProjStr != nullptr)
    3579             :             {
    3580           2 :                 (*returnProjStr) = std::string(pszTempProjection);
    3581             :             }
    3582             :             else
    3583             :             {
    3584          29 :                 m_bAddedProjectionVarsDefs = true;
    3585          29 :                 m_bAddedProjectionVarsData = true;
    3586          29 :                 SetSpatialRefNoUpdate(&oSRS);
    3587             :             }
    3588             :         }
    3589          31 :         CPLFree(pszTempProjection);
    3590             :     }
    3591             : 
    3592         514 :     if (!bReadSRSOnly && (nVarDimXID != -1) && (nVarDimYID != -1) && xdim > 0 &&
    3593             :         ydim > 0)
    3594             :     {
    3595             :         double *pdfXCoord =
    3596         223 :             static_cast<double *>(CPLCalloc(xdim, sizeof(double)));
    3597             :         double *pdfYCoord =
    3598         223 :             static_cast<double *>(CPLCalloc(ydim, sizeof(double)));
    3599             : 
    3600         223 :         size_t start[2] = {0, 0};
    3601         223 :         size_t edge[2] = {xdim, 0};
    3602         223 :         int status = nc_get_vara_double(nGroupDimXID, nVarDimXID, start, edge,
    3603             :                                         pdfXCoord);
    3604         223 :         NCDF_ERR(status);
    3605             : 
    3606         223 :         edge[0] = ydim;
    3607         223 :         status = nc_get_vara_double(nGroupDimYID, nVarDimYID, start, edge,
    3608             :                                     pdfYCoord);
    3609         223 :         NCDF_ERR(status);
    3610             : 
    3611         223 :         nc_type nc_var_dimx_datatype = NC_NAT;
    3612             :         status =
    3613         223 :             nc_inq_vartype(nGroupDimXID, nVarDimXID, &nc_var_dimx_datatype);
    3614         223 :         NCDF_ERR(status);
    3615             : 
    3616         223 :         nc_type nc_var_dimy_datatype = NC_NAT;
    3617             :         status =
    3618         223 :             nc_inq_vartype(nGroupDimYID, nVarDimYID, &nc_var_dimy_datatype);
    3619         223 :         NCDF_ERR(status);
    3620             : 
    3621         223 :         if (!poDS->bSwitchedXY)
    3622             :         {
    3623             :             // Convert ]180,540] longitude values to ]-180,0].
    3624         311 :             if (NCDFIsVarLongitude(nGroupDimXID, nVarDimXID, nullptr) &&
    3625          90 :                 CPLTestBool(
    3626             :                     CPLGetConfigOption("GDAL_NETCDF_CENTERLONG_180", "YES")))
    3627             :             {
    3628             :                 // If minimum longitude is > 180, subtract 360 from all.
    3629             :                 // Add a check on the maximum X value too, since
    3630             :                 // NCDFIsVarLongitude() is not very specific by default (see
    3631             :                 // https://github.com/OSGeo/gdal/issues/1440)
    3632          97 :                 if (std::min(pdfXCoord[0], pdfXCoord[xdim - 1]) > 180.0 &&
    3633           7 :                     std::max(pdfXCoord[0], pdfXCoord[xdim - 1]) <= 540)
    3634             :                 {
    3635           0 :                     CPLDebug(
    3636             :                         "GDAL_netCDF",
    3637             :                         "Offsetting longitudes from ]180,540] to ]-180,180]. "
    3638             :                         "Can be disabled with GDAL_NETCDF_CENTERLONG_180=NO");
    3639           0 :                     for (size_t i = 0; i < xdim; i++)
    3640           0 :                         pdfXCoord[i] -= 360;
    3641             :                 }
    3642             :             }
    3643             :         }
    3644             : 
    3645             :         // Is pixel spacing uniform across the map?
    3646             : 
    3647             :         // Check Longitude.
    3648             : 
    3649         223 :         bool bLonSpacingOK = false;
    3650         223 :         if (xdim == 2)
    3651             :         {
    3652          28 :             bLonSpacingOK = true;
    3653             :         }
    3654             :         else
    3655             :         {
    3656         195 :             bool bWestIsLeft = (pdfXCoord[0] < pdfXCoord[xdim - 1]);
    3657             : 
    3658             :             // fix longitudes if longitudes should increase from
    3659             :             // west to east, but west > east
    3660         275 :             if (NCDFIsVarLongitude(nGroupDimXID, nVarDimXID, nullptr) &&
    3661          80 :                 !bWestIsLeft)
    3662             :             {
    3663           2 :                 size_t ndecreases = 0;
    3664             : 
    3665             :                 // there is lon wrap if longitudes increase
    3666             :                 // with one single decrease
    3667         107 :                 for (size_t i = 1; i < xdim; i++)
    3668             :                 {
    3669         105 :                     if (pdfXCoord[i] < pdfXCoord[i - 1])
    3670           1 :                         ndecreases++;
    3671             :                 }
    3672             : 
    3673           2 :                 if (ndecreases == 1)
    3674             :                 {
    3675           1 :                     CPLDebug("GDAL_netCDF", "longitude wrap detected");
    3676           4 :                     for (size_t i = 0; i < xdim; i++)
    3677             :                     {
    3678           3 :                         if (pdfXCoord[i] > pdfXCoord[xdim - 1])
    3679           1 :                             pdfXCoord[i] -= 360;
    3680             :                     }
    3681             :                 }
    3682             :             }
    3683             : 
    3684         195 :             const double dfSpacingBegin = pdfXCoord[1] - pdfXCoord[0];
    3685         195 :             const double dfSpacingMiddle =
    3686         195 :                 pdfXCoord[xdim / 2 + 1] - pdfXCoord[xdim / 2];
    3687         195 :             const double dfSpacingLast =
    3688         195 :                 pdfXCoord[xdim - 1] - pdfXCoord[xdim - 2];
    3689             : 
    3690         195 :             CPLDebug("GDAL_netCDF",
    3691             :                      "xdim: %ld dfSpacingBegin: %f dfSpacingMiddle: %f "
    3692             :                      "dfSpacingLast: %f",
    3693             :                      static_cast<long>(xdim), dfSpacingBegin, dfSpacingMiddle,
    3694             :                      dfSpacingLast);
    3695             : #ifdef NCDF_DEBUG
    3696             :             CPLDebug("GDAL_netCDF", "xcoords: %f %f %f %f %f %f", pdfXCoord[0],
    3697             :                      pdfXCoord[1], pdfXCoord[xdim / 2],
    3698             :                      pdfXCoord[(xdim / 2) + 1], pdfXCoord[xdim - 2],
    3699             :                      pdfXCoord[xdim - 1]);
    3700             : #endif
    3701             : 
    3702             :             // ftp://ftp.cdc.noaa.gov/Datasets/NARR/Dailies/monolevel/vwnd.10m.2015.nc
    3703             :             // requires a 0.02% tolerance, so let's settle for 0.05%
    3704             : 
    3705             :             // For float variables, increase to 0.2% (as seen in
    3706             :             // https://github.com/OSGeo/gdal/issues/3663)
    3707         195 :             const double dfEpsRel =
    3708         195 :                 nc_var_dimx_datatype == NC_FLOAT ? 0.002 : 0.0005;
    3709             : 
    3710             :             const double dfEps =
    3711             :                 dfEpsRel *
    3712         390 :                 std::max(fabs(dfSpacingBegin),
    3713         195 :                          std::max(fabs(dfSpacingMiddle), fabs(dfSpacingLast)));
    3714         384 :             if (IsDifferenceBelow(dfSpacingBegin, dfSpacingLast, dfEps) &&
    3715         384 :                 IsDifferenceBelow(dfSpacingBegin, dfSpacingMiddle, dfEps) &&
    3716         189 :                 IsDifferenceBelow(dfSpacingMiddle, dfSpacingLast, dfEps))
    3717             :             {
    3718         189 :                 bLonSpacingOK = true;
    3719             :             }
    3720           6 :             else if (CPLTestBool(CPLGetConfigOption(
    3721             :                          "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK", "NO")))
    3722             :             {
    3723           0 :                 bLonSpacingOK = true;
    3724           0 :                 CPLDebug(
    3725             :                     "GDAL_netCDF",
    3726             :                     "Longitude/X is not equally spaced, but will be considered "
    3727             :                     "as such because of "
    3728             :                     "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK");
    3729             :             }
    3730             :         }
    3731             : 
    3732         223 :         if (bLonSpacingOK == false)
    3733             :         {
    3734           6 :             CPLDebug(
    3735             :                 "GDAL_netCDF", "%s",
    3736             :                 "Longitude/X is not equally spaced (with a 0.05% tolerance). "
    3737             :                 "You may set the "
    3738             :                 "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK configuration "
    3739             :                 "option to YES to ignore this check");
    3740             :         }
    3741             : 
    3742             :         // Check Latitude.
    3743         223 :         bool bLatSpacingOK = false;
    3744             : 
    3745         223 :         if (ydim == 2)
    3746             :         {
    3747          48 :             bLatSpacingOK = true;
    3748             :         }
    3749             :         else
    3750             :         {
    3751         175 :             const double dfSpacingBegin = pdfYCoord[1] - pdfYCoord[0];
    3752         175 :             const double dfSpacingMiddle =
    3753         175 :                 pdfYCoord[ydim / 2 + 1] - pdfYCoord[ydim / 2];
    3754             : 
    3755         175 :             const double dfSpacingLast =
    3756         175 :                 pdfYCoord[ydim - 1] - pdfYCoord[ydim - 2];
    3757             : 
    3758         175 :             CPLDebug("GDAL_netCDF",
    3759             :                      "ydim: %ld dfSpacingBegin: %f dfSpacingMiddle: %f "
    3760             :                      "dfSpacingLast: %f",
    3761             :                      (long)ydim, dfSpacingBegin, dfSpacingMiddle,
    3762             :                      dfSpacingLast);
    3763             : #ifdef NCDF_DEBUG
    3764             :             CPLDebug("GDAL_netCDF", "ycoords: %f %f %f %f %f %f", pdfYCoord[0],
    3765             :                      pdfYCoord[1], pdfYCoord[ydim / 2],
    3766             :                      pdfYCoord[(ydim / 2) + 1], pdfYCoord[ydim - 2],
    3767             :                      pdfYCoord[ydim - 1]);
    3768             : #endif
    3769             : 
    3770         175 :             const double dfEpsRel =
    3771         175 :                 nc_var_dimy_datatype == NC_FLOAT ? 0.002 : 0.0005;
    3772             : 
    3773             :             const double dfEps =
    3774             :                 dfEpsRel *
    3775         350 :                 std::max(fabs(dfSpacingBegin),
    3776         175 :                          std::max(fabs(dfSpacingMiddle), fabs(dfSpacingLast)));
    3777         348 :             if (IsDifferenceBelow(dfSpacingBegin, dfSpacingLast, dfEps) &&
    3778         348 :                 IsDifferenceBelow(dfSpacingBegin, dfSpacingMiddle, dfEps) &&
    3779         164 :                 IsDifferenceBelow(dfSpacingMiddle, dfSpacingLast, dfEps))
    3780             :             {
    3781         164 :                 bLatSpacingOK = true;
    3782             :             }
    3783          11 :             else if (CPLTestBool(CPLGetConfigOption(
    3784             :                          "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK", "NO")))
    3785             :             {
    3786           0 :                 bLatSpacingOK = true;
    3787           0 :                 CPLDebug(
    3788             :                     "GDAL_netCDF",
    3789             :                     "Latitude/Y is not equally spaced, but will be considered "
    3790             :                     "as such because of "
    3791             :                     "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK");
    3792             :             }
    3793          11 :             else if (!oSRS.IsProjected() &&
    3794          11 :                      fabs(dfSpacingBegin - dfSpacingLast) <= 0.1 &&
    3795          30 :                      fabs(dfSpacingBegin - dfSpacingMiddle) <= 0.1 &&
    3796           8 :                      fabs(dfSpacingMiddle - dfSpacingLast) <= 0.1)
    3797             :             {
    3798           8 :                 bLatSpacingOK = true;
    3799           8 :                 CPLError(CE_Warning, CPLE_AppDefined,
    3800             :                          "Latitude grid not spaced evenly.  "
    3801             :                          "Setting projection for grid spacing is "
    3802             :                          "within 0.1 degrees threshold.");
    3803             : 
    3804           8 :                 CPLDebug("GDAL_netCDF",
    3805             :                          "Latitude grid not spaced evenly, but within 0.1 "
    3806             :                          "degree threshold (probably a Gaussian grid).  "
    3807             :                          "Saving original latitude values in Y_VALUES "
    3808             :                          "geolocation metadata");
    3809           8 :                 Set1DGeolocation(nGroupDimYID, nVarDimYID, "Y");
    3810             :             }
    3811             : 
    3812         175 :             if (bLatSpacingOK == false)
    3813             :             {
    3814           3 :                 CPLDebug(
    3815             :                     "GDAL_netCDF", "%s",
    3816             :                     "Latitude/Y is not equally spaced (with a 0.05% "
    3817             :                     "tolerance). "
    3818             :                     "You may set the "
    3819             :                     "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK configuration "
    3820             :                     "option to YES to ignore this check");
    3821             :             }
    3822             :         }
    3823             : 
    3824         223 :         if (bLonSpacingOK && bLatSpacingOK)
    3825             :         {
    3826             :             // We have gridded data so we can set the Georeferencing info.
    3827             : 
    3828             :             // Enable GeoTransform.
    3829             : 
    3830             :             // In the following "actual_range" and "node_offset"
    3831             :             // are attributes used by netCDF files created by GMT.
    3832             :             // If we find them we know how to proceed. Else, use
    3833             :             // the original algorithm.
    3834         216 :             bGotCfGT = true;
    3835             : 
    3836         216 :             int node_offset = 0;
    3837         216 :             NCDFResolveAttInt(nGroupId, NC_GLOBAL, "node_offset", &node_offset);
    3838             : 
    3839         216 :             double adfActualRange[2] = {0.0, 0.0};
    3840         216 :             double xMinMax[2] = {0.0, 0.0};
    3841         216 :             double yMinMax[2] = {0.0, 0.0};
    3842             : 
    3843             :             const auto RoundMinMaxForFloatVals =
    3844          58 :                 [](double &dfMin, double &dfMax, int nIntervals)
    3845             :             {
    3846             :                 // Helps for a case where longitudes range from
    3847             :                 // -179.99 to 180.0 with a 0.01 degree spacing.
    3848             :                 // However as this is encoded in a float array,
    3849             :                 // -179.99 is actually read as -179.99000549316406 as
    3850             :                 // a double. Try to detect that and correct the rounding
    3851             : 
    3852          87 :                 const auto IsAlmostInteger = [](double dfVal)
    3853             :                 {
    3854          87 :                     constexpr double THRESHOLD_INTEGER = 1e-3;
    3855          87 :                     return std::fabs(dfVal - std::round(dfVal)) <=
    3856          87 :                            THRESHOLD_INTEGER;
    3857             :                 };
    3858             : 
    3859          58 :                 const double dfSpacing = (dfMax - dfMin) / nIntervals;
    3860          58 :                 if (dfSpacing > 0)
    3861             :                 {
    3862          47 :                     const double dfInvSpacing = 1.0 / dfSpacing;
    3863          47 :                     if (IsAlmostInteger(dfInvSpacing))
    3864             :                     {
    3865          20 :                         const double dfRoundedSpacing =
    3866          20 :                             1.0 / std::round(dfInvSpacing);
    3867          20 :                         const double dfMinDivRoundedSpacing =
    3868          20 :                             dfMin / dfRoundedSpacing;
    3869          20 :                         const double dfMaxDivRoundedSpacing =
    3870          20 :                             dfMax / dfRoundedSpacing;
    3871          40 :                         if (IsAlmostInteger(dfMinDivRoundedSpacing) &&
    3872          20 :                             IsAlmostInteger(dfMaxDivRoundedSpacing))
    3873             :                         {
    3874          20 :                             const double dfRoundedMin =
    3875          20 :                                 std::round(dfMinDivRoundedSpacing) *
    3876             :                                 dfRoundedSpacing;
    3877          20 :                             const double dfRoundedMax =
    3878          20 :                                 std::round(dfMaxDivRoundedSpacing) *
    3879             :                                 dfRoundedSpacing;
    3880          20 :                             if (static_cast<float>(dfMin) ==
    3881          20 :                                     static_cast<float>(dfRoundedMin) &&
    3882           8 :                                 static_cast<float>(dfMax) ==
    3883           8 :                                     static_cast<float>(dfRoundedMax))
    3884             :                             {
    3885           7 :                                 dfMin = dfRoundedMin;
    3886           7 :                                 dfMax = dfRoundedMax;
    3887             :                             }
    3888             :                         }
    3889             :                     }
    3890             :                 }
    3891          58 :             };
    3892             : 
    3893         216 :             if (!nc_get_att_double(nGroupDimXID, nVarDimXID, "actual_range",
    3894             :                                    adfActualRange))
    3895             :             {
    3896           3 :                 xMinMax[0] = adfActualRange[0];
    3897           3 :                 xMinMax[1] = adfActualRange[1];
    3898             : 
    3899             :                 // Present xMinMax[] in the same order as padfXCoord
    3900           3 :                 if ((xMinMax[0] - xMinMax[1]) *
    3901           3 :                         (pdfXCoord[0] - pdfXCoord[xdim - 1]) <
    3902             :                     0)
    3903             :                 {
    3904           0 :                     std::swap(xMinMax[0], xMinMax[1]);
    3905             :                 }
    3906             :             }
    3907             :             else
    3908             :             {
    3909         213 :                 xMinMax[0] = pdfXCoord[0];
    3910         213 :                 xMinMax[1] = pdfXCoord[xdim - 1];
    3911         213 :                 node_offset = 0;
    3912             : 
    3913         213 :                 if (nc_var_dimx_datatype == NC_FLOAT)
    3914             :                 {
    3915          29 :                     RoundMinMaxForFloatVals(xMinMax[0], xMinMax[1],
    3916          29 :                                             poDS->nRasterXSize - 1);
    3917             :                 }
    3918             :             }
    3919             : 
    3920         216 :             if (!nc_get_att_double(nGroupDimYID, nVarDimYID, "actual_range",
    3921             :                                    adfActualRange))
    3922             :             {
    3923           3 :                 yMinMax[0] = adfActualRange[0];
    3924           3 :                 yMinMax[1] = adfActualRange[1];
    3925             : 
    3926             :                 // Present yMinMax[] in the same order as pdfYCoord
    3927           3 :                 if ((yMinMax[0] - yMinMax[1]) *
    3928           3 :                         (pdfYCoord[0] - pdfYCoord[ydim - 1]) <
    3929             :                     0)
    3930             :                 {
    3931           1 :                     std::swap(yMinMax[0], yMinMax[1]);
    3932             :                 }
    3933             :             }
    3934             :             else
    3935             :             {
    3936         213 :                 yMinMax[0] = pdfYCoord[0];
    3937         213 :                 yMinMax[1] = pdfYCoord[ydim - 1];
    3938         213 :                 node_offset = 0;
    3939             : 
    3940         213 :                 if (nc_var_dimy_datatype == NC_FLOAT)
    3941             :                 {
    3942          29 :                     RoundMinMaxForFloatVals(yMinMax[0], yMinMax[1],
    3943          29 :                                             poDS->nRasterYSize - 1);
    3944             :                 }
    3945             :             }
    3946             : 
    3947         216 :             double dfCoordOffset = 0.0;
    3948         216 :             double dfCoordScale = 1.0;
    3949         216 :             if (!nc_get_att_double(nGroupId, nVarDimXID, CF_ADD_OFFSET,
    3950         220 :                                    &dfCoordOffset) &&
    3951           4 :                 !nc_get_att_double(nGroupId, nVarDimXID, CF_SCALE_FACTOR,
    3952             :                                    &dfCoordScale))
    3953             :             {
    3954           4 :                 xMinMax[0] = dfCoordOffset + xMinMax[0] * dfCoordScale;
    3955           4 :                 xMinMax[1] = dfCoordOffset + xMinMax[1] * dfCoordScale;
    3956             :             }
    3957             : 
    3958         216 :             if (!nc_get_att_double(nGroupId, nVarDimYID, CF_ADD_OFFSET,
    3959         220 :                                    &dfCoordOffset) &&
    3960           4 :                 !nc_get_att_double(nGroupId, nVarDimYID, CF_SCALE_FACTOR,
    3961             :                                    &dfCoordScale))
    3962             :             {
    3963           4 :                 yMinMax[0] = dfCoordOffset + yMinMax[0] * dfCoordScale;
    3964           4 :                 yMinMax[1] = dfCoordOffset + yMinMax[1] * dfCoordScale;
    3965             :             }
    3966             : 
    3967             :             // Check for reverse order of y-coordinate.
    3968         216 :             if (!bSwitchedXY)
    3969             :             {
    3970         214 :                 poDS->bBottomUp = (yMinMax[0] <= yMinMax[1]);
    3971         214 :                 CPLDebug("GDAL_netCDF", "set bBottomUp = %d from Y axis",
    3972         214 :                          static_cast<int>(poDS->bBottomUp));
    3973         214 :                 if (!poDS->bBottomUp)
    3974             :                 {
    3975          32 :                     std::swap(yMinMax[0], yMinMax[1]);
    3976             :                 }
    3977             :             }
    3978             : 
    3979             :             // Geostationary satellites can specify units in (micro)radians
    3980             :             // So we check if they do, and if so convert to linear units
    3981             :             // (meters)
    3982         216 :             const char *pszProjName = oSRS.GetAttrValue("PROJECTION");
    3983         216 :             if (pszProjName != nullptr)
    3984             :             {
    3985          24 :                 if (EQUAL(pszProjName, SRS_PT_GEOSTATIONARY_SATELLITE))
    3986             :                 {
    3987             :                     double satelliteHeight =
    3988           3 :                         oSRS.GetProjParm(SRS_PP_SATELLITE_HEIGHT, 1.0);
    3989           3 :                     size_t nAttlen = 0;
    3990             :                     char szUnits[NC_MAX_NAME + 1];
    3991           3 :                     szUnits[0] = '\0';
    3992           3 :                     nc_type nAttype = NC_NAT;
    3993           3 :                     nc_inq_att(nGroupId, nVarDimXID, "units", &nAttype,
    3994             :                                &nAttlen);
    3995           6 :                     if (nAttlen < sizeof(szUnits) &&
    3996           3 :                         nc_get_att_text(nGroupId, nVarDimXID, "units",
    3997             :                                         szUnits) == NC_NOERR)
    3998             :                     {
    3999           3 :                         szUnits[nAttlen] = '\0';
    4000           3 :                         if (EQUAL(szUnits, "microradian"))
    4001             :                         {
    4002           1 :                             xMinMax[0] =
    4003           1 :                                 xMinMax[0] * satelliteHeight * 0.000001;
    4004           1 :                             xMinMax[1] =
    4005           1 :                                 xMinMax[1] * satelliteHeight * 0.000001;
    4006             :                         }
    4007           2 :                         else if (EQUAL(szUnits, "rad") ||
    4008           1 :                                  EQUAL(szUnits, "radian"))
    4009             :                         {
    4010           2 :                             xMinMax[0] = xMinMax[0] * satelliteHeight;
    4011           2 :                             xMinMax[1] = xMinMax[1] * satelliteHeight;
    4012             :                         }
    4013             :                     }
    4014           3 :                     szUnits[0] = '\0';
    4015           3 :                     nc_inq_att(nGroupId, nVarDimYID, "units", &nAttype,
    4016             :                                &nAttlen);
    4017           6 :                     if (nAttlen < sizeof(szUnits) &&
    4018           3 :                         nc_get_att_text(nGroupId, nVarDimYID, "units",
    4019             :                                         szUnits) == NC_NOERR)
    4020             :                     {
    4021           3 :                         szUnits[nAttlen] = '\0';
    4022           3 :                         if (EQUAL(szUnits, "microradian"))
    4023             :                         {
    4024           1 :                             yMinMax[0] =
    4025           1 :                                 yMinMax[0] * satelliteHeight * 0.000001;
    4026           1 :                             yMinMax[1] =
    4027           1 :                                 yMinMax[1] * satelliteHeight * 0.000001;
    4028             :                         }
    4029           2 :                         else if (EQUAL(szUnits, "rad") ||
    4030           1 :                                  EQUAL(szUnits, "radian"))
    4031             :                         {
    4032           2 :                             yMinMax[0] = yMinMax[0] * satelliteHeight;
    4033           2 :                             yMinMax[1] = yMinMax[1] * satelliteHeight;
    4034             :                         }
    4035             :                     }
    4036             :                 }
    4037             :             }
    4038             : 
    4039         216 :             tmpGT[0] = xMinMax[0];
    4040         432 :             tmpGT[1] = (xMinMax[1] - xMinMax[0]) /
    4041         216 :                        (poDS->nRasterXSize + (node_offset - 1));
    4042         216 :             tmpGT[2] = 0;
    4043         216 :             if (bSwitchedXY)
    4044             :             {
    4045           2 :                 tmpGT[3] = yMinMax[0];
    4046           2 :                 tmpGT[4] = 0;
    4047           2 :                 tmpGT[5] = (yMinMax[1] - yMinMax[0]) /
    4048           2 :                            (poDS->nRasterYSize + (node_offset - 1));
    4049             :             }
    4050             :             else
    4051             :             {
    4052         214 :                 tmpGT[3] = yMinMax[1];
    4053         214 :                 tmpGT[4] = 0;
    4054         214 :                 tmpGT[5] = (yMinMax[0] - yMinMax[1]) /
    4055         214 :                            (poDS->nRasterYSize + (node_offset - 1));
    4056             :             }
    4057             : 
    4058             :             // Compute the center of the pixel.
    4059         216 :             if (!node_offset)
    4060             :             {
    4061             :                 // Otherwise its already the pixel center.
    4062         216 :                 tmpGT[0] -= (tmpGT[1] / 2);
    4063         216 :                 tmpGT[3] -= (tmpGT[5] / 2);
    4064             :             }
    4065             :         }
    4066             : 
    4067             :         const auto AreSRSEqualThroughProj4String =
    4068           2 :             [](const OGRSpatialReference &oSRS1,
    4069             :                const OGRSpatialReference &oSRS2)
    4070             :         {
    4071           2 :             char *pszProj4Str1 = nullptr;
    4072           2 :             oSRS1.exportToProj4(&pszProj4Str1);
    4073             : 
    4074           2 :             char *pszProj4Str2 = nullptr;
    4075           2 :             oSRS2.exportToProj4(&pszProj4Str2);
    4076             : 
    4077             :             {
    4078           2 :                 char *pszTmp = strstr(pszProj4Str1, "+datum=");
    4079           2 :                 if (pszTmp)
    4080           0 :                     memcpy(pszTmp, "+ellps=", strlen("+ellps="));
    4081             :             }
    4082             : 
    4083             :             {
    4084           2 :                 char *pszTmp = strstr(pszProj4Str2, "+datum=");
    4085           2 :                 if (pszTmp)
    4086           2 :                     memcpy(pszTmp, "+ellps=", strlen("+ellps="));
    4087             :             }
    4088             : 
    4089           2 :             bool bRet = false;
    4090           2 :             if (pszProj4Str1 && pszProj4Str2 &&
    4091           2 :                 EQUAL(pszProj4Str1, pszProj4Str2))
    4092             :             {
    4093           1 :                 bRet = true;
    4094             :             }
    4095             : 
    4096           2 :             CPLFree(pszProj4Str1);
    4097           2 :             CPLFree(pszProj4Str2);
    4098           2 :             return bRet;
    4099             :         };
    4100             : 
    4101         223 :         if (dfLinearUnitsConvFactor != 1.0)
    4102             :         {
    4103          35 :             for (int i = 0; i < 6; ++i)
    4104          30 :                 tmpGT[i] *= dfLinearUnitsConvFactor;
    4105             : 
    4106           5 :             if (paosRemovedMDItems)
    4107             :             {
    4108             :                 char szVarNameX[NC_MAX_NAME + 1];
    4109           5 :                 CPL_IGNORE_RET_VAL(
    4110           5 :                     nc_inq_varname(nGroupId, nVarDimXID, szVarNameX));
    4111             : 
    4112             :                 char szVarNameY[NC_MAX_NAME + 1];
    4113           5 :                 CPL_IGNORE_RET_VAL(
    4114           5 :                     nc_inq_varname(nGroupId, nVarDimYID, szVarNameY));
    4115             : 
    4116           5 :                 paosRemovedMDItems->push_back(
    4117             :                     CPLSPrintf("%s#units", szVarNameX));
    4118           5 :                 paosRemovedMDItems->push_back(
    4119             :                     CPLSPrintf("%s#units", szVarNameY));
    4120             :             }
    4121             :         }
    4122             : 
    4123             :         // If there is a global "geospatial_bounds_crs" attribute, check that it
    4124             :         // is consistent with the SRS, and if so, use it as the SRS
    4125             :         const char *pszGBCRS =
    4126         223 :             FetchAttr(nGroupId, NC_GLOBAL, "geospatial_bounds_crs");
    4127         223 :         if (pszGBCRS && STARTS_WITH(pszGBCRS, "EPSG:"))
    4128             :         {
    4129           4 :             OGRSpatialReference oSRSFromGBCRS;
    4130           2 :             oSRSFromGBCRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    4131           2 :             if (oSRSFromGBCRS.SetFromUserInput(
    4132             :                     pszGBCRS,
    4133             :                     OGRSpatialReference::
    4134           4 :                         SET_FROM_USER_INPUT_LIMITATIONS_get()) == OGRERR_NONE &&
    4135           2 :                 AreSRSEqualThroughProj4String(oSRS, oSRSFromGBCRS))
    4136             :             {
    4137           1 :                 oSRS = std::move(oSRSFromGBCRS);
    4138           1 :                 SetSpatialRefNoUpdate(&oSRS);
    4139             :             }
    4140             :         }
    4141             : 
    4142         223 :         CPLFree(pdfXCoord);
    4143         223 :         CPLFree(pdfYCoord);
    4144             :     }  // end if(has dims)
    4145             : 
    4146             :     // Process custom GeoTransform GDAL value.
    4147         514 :     if (!EQUAL(pszGridMappingValue, ""))
    4148             :     {
    4149         219 :         if (pszGeoTransform != nullptr)
    4150             :         {
    4151             :             CPLStringList aosGeoTransform(
    4152         220 :                 CSLTokenizeString2(pszGeoTransform, " ", CSLT_HONOURSTRINGS));
    4153         110 :             if (aosGeoTransform.size() == 6)
    4154             :             {
    4155         110 :                 GDALGeoTransform gtFromAttribute;
    4156         770 :                 for (int i = 0; i < 6; i++)
    4157             :                 {
    4158         660 :                     gtFromAttribute[i] = CPLAtof(aosGeoTransform[i]);
    4159             :                 }
    4160             : 
    4161         110 :                 if (bGotCfGT)
    4162             :                 {
    4163          94 :                     constexpr double GT_RELERROR_WARN_THRESHOLD = 1e-6;
    4164          94 :                     double dfMaxAbsoluteError = 0.0;
    4165         658 :                     for (int i = 0; i < 6; i++)
    4166             :                     {
    4167             :                         double dfAbsoluteError =
    4168         564 :                             std::abs(tmpGT[i] - gtFromAttribute[i]);
    4169         564 :                         if (dfAbsoluteError >
    4170         564 :                             std::abs(gtFromAttribute[i] *
    4171             :                                      GT_RELERROR_WARN_THRESHOLD))
    4172             :                         {
    4173           2 :                             dfMaxAbsoluteError =
    4174           2 :                                 std::max(dfMaxAbsoluteError, dfAbsoluteError);
    4175             :                         }
    4176             :                     }
    4177             : 
    4178          94 :                     if (dfMaxAbsoluteError > 0)
    4179             :                     {
    4180           2 :                         CPLError(CE_Warning, CPLE_AppDefined,
    4181             :                                  "GeoTransform read from attribute of %s "
    4182             :                                  "variable differs from value calculated from "
    4183             :                                  "dimension variables (max diff = %g). Using "
    4184             :                                  "value from attribute.",
    4185             :                                  pszGridMappingValue, dfMaxAbsoluteError);
    4186             :                     }
    4187             :                 }
    4188             : 
    4189         110 :                 tmpGT = std::move(gtFromAttribute);
    4190         110 :                 bGotGdalGT = true;
    4191             :             }
    4192             :         }
    4193             :         else
    4194             :         {
    4195             :             // Look for corner array values.
    4196             :             // CPLDebug("GDAL_netCDF",
    4197             :             //           "looking for geotransform corners");
    4198         109 :             bool bGotNN = false;
    4199         109 :             double dfNN = FetchCopyParam(pszGridMappingValue,
    4200             :                                          "Northernmost_Northing", 0, &bGotNN);
    4201             : 
    4202         109 :             bool bGotSN = false;
    4203         109 :             double dfSN = FetchCopyParam(pszGridMappingValue,
    4204             :                                          "Southernmost_Northing", 0, &bGotSN);
    4205             : 
    4206         109 :             bool bGotEE = false;
    4207         109 :             double dfEE = FetchCopyParam(pszGridMappingValue,
    4208             :                                          "Easternmost_Easting", 0, &bGotEE);
    4209             : 
    4210         109 :             bool bGotWE = false;
    4211         109 :             double dfWE = FetchCopyParam(pszGridMappingValue,
    4212             :                                          "Westernmost_Easting", 0, &bGotWE);
    4213             : 
    4214             :             // Only set the GeoTransform if we got all the values.
    4215         109 :             if (bGotNN && bGotSN && bGotEE && bGotWE)
    4216             :             {
    4217           0 :                 bGotGdalGT = true;
    4218             : 
    4219           0 :                 tmpGT[0] = dfWE;
    4220           0 :                 tmpGT[1] = (dfEE - dfWE) / (poDS->GetRasterXSize() - 1);
    4221           0 :                 tmpGT[2] = 0.0;
    4222           0 :                 tmpGT[3] = dfNN;
    4223           0 :                 tmpGT[4] = 0.0;
    4224           0 :                 tmpGT[5] = (dfSN - dfNN) / (poDS->GetRasterYSize() - 1);
    4225             :                 // Compute the center of the pixel.
    4226           0 :                 tmpGT[0] = dfWE - (tmpGT[1] / 2);
    4227           0 :                 tmpGT[3] = dfNN - (tmpGT[5] / 2);
    4228             :             }
    4229             :         }  // (pszGeoTransform != NULL)
    4230             : 
    4231         219 :         if (bGotGdalSRS && !bGotGdalGT)
    4232          74 :             CPLDebug("GDAL_netCDF", "Got SRS but no geotransform from GDAL!");
    4233             :     }
    4234             : 
    4235         514 :     if (!pszWKT && !bGotCfSRS)
    4236             :     {
    4237             :         // Some netCDF files have a srid attribute (#6613) like
    4238             :         // urn:ogc:def:crs:EPSG::6931
    4239         295 :         const char *pszSRID = FetchAttr(pszGridMappingValue, "srid");
    4240         295 :         if (pszSRID != nullptr)
    4241             :         {
    4242           0 :             oSRS.Clear();
    4243           0 :             if (oSRS.SetFromUserInput(
    4244             :                     pszSRID,
    4245             :                     OGRSpatialReference::
    4246           0 :                         SET_FROM_USER_INPUT_LIMITATIONS_get()) == OGRERR_NONE)
    4247             :             {
    4248           0 :                 char *pszWKTExport = nullptr;
    4249           0 :                 CPLDebug("GDAL_netCDF", "Got SRS from %s", pszSRID);
    4250           0 :                 oSRS.exportToWkt(&pszWKTExport);
    4251           0 :                 if (returnProjStr != nullptr)
    4252             :                 {
    4253           0 :                     (*returnProjStr) = std::string(pszWKTExport);
    4254             :                 }
    4255             :                 else
    4256             :                 {
    4257           0 :                     m_bAddedProjectionVarsDefs = true;
    4258           0 :                     m_bAddedProjectionVarsData = true;
    4259           0 :                     SetSpatialRefNoUpdate(&oSRS);
    4260             :                 }
    4261           0 :                 CPLFree(pszWKTExport);
    4262             :             }
    4263             :         }
    4264             :     }
    4265             : 
    4266         514 :     CPLFree(pszGridMappingValue);
    4267             : 
    4268         514 :     if (bReadSRSOnly)
    4269         166 :         return;
    4270             : 
    4271             :     // Determines the SRS to be used by the geolocation array, if any
    4272         696 :     std::string osGeolocWKT = SRS_WKT_WGS84_LAT_LONG;
    4273         348 :     if (!m_oSRS.IsEmpty())
    4274             :     {
    4275         262 :         OGRSpatialReference oGeogCRS;
    4276         131 :         oGeogCRS.CopyGeogCSFrom(&m_oSRS);
    4277         131 :         char *pszWKTTmp = nullptr;
    4278         131 :         const char *const apszOptions[] = {"FORMAT=WKT2_2019", nullptr};
    4279         131 :         if (oGeogCRS.exportToWkt(&pszWKTTmp, apszOptions) == OGRERR_NONE)
    4280             :         {
    4281         131 :             osGeolocWKT = pszWKTTmp;
    4282             :         }
    4283         131 :         CPLFree(pszWKTTmp);
    4284             :     }
    4285             : 
    4286             :     // Process geolocation arrays from CF "coordinates" attribute.
    4287         696 :     std::string osGeolocXName, osGeolocYName;
    4288         348 :     if (ProcessCFGeolocation(nGroupId, nVarId, osGeolocWKT, osGeolocXName,
    4289         348 :                              osGeolocYName))
    4290             :     {
    4291          52 :         bool bCanCancelGT = true;
    4292          52 :         if ((nVarDimXID != -1) && (nVarDimYID != -1))
    4293             :         {
    4294             :             char szVarNameX[NC_MAX_NAME + 1];
    4295          44 :             CPL_IGNORE_RET_VAL(
    4296          44 :                 nc_inq_varname(nGroupId, nVarDimXID, szVarNameX));
    4297             :             char szVarNameY[NC_MAX_NAME + 1];
    4298          44 :             CPL_IGNORE_RET_VAL(
    4299          44 :                 nc_inq_varname(nGroupId, nVarDimYID, szVarNameY));
    4300          44 :             bCanCancelGT =
    4301          44 :                 !(osGeolocXName == szVarNameX && osGeolocYName == szVarNameY);
    4302             :         }
    4303          86 :         if (bCanCancelGT && !m_oSRS.IsGeographic() && !m_oSRS.IsProjected() &&
    4304          34 :             !bSwitchedXY)
    4305             :         {
    4306          32 :             bGotCfGT = false;
    4307             :         }
    4308             :     }
    4309         120 :     else if (!bGotCfGT && !bReadSRSOnly && (nVarDimXID != -1) &&
    4310         419 :              (nVarDimYID != -1) && xdim > 0 && ydim > 0 &&
    4311           3 :              ((!bSwitchedXY &&
    4312           3 :                NCDFIsVarLongitude(nGroupId, nVarDimXID, nullptr) &&
    4313           1 :                NCDFIsVarLatitude(nGroupId, nVarDimYID, nullptr)) ||
    4314           2 :               (bSwitchedXY &&
    4315           0 :                NCDFIsVarLongitude(nGroupId, nVarDimYID, nullptr) &&
    4316           0 :                NCDFIsVarLatitude(nGroupId, nVarDimXID, nullptr))))
    4317             :     {
    4318             :         // Case of autotest/gdrivers/data/netcdf/GLMELT_4X5.OCN.nc
    4319             :         // which is indexed by lat, lon variables, but lat has irregular
    4320             :         // spacing.
    4321           1 :         const char *pszGeolocXFullName = poDS->papszDimName[poDS->nXDimID];
    4322           1 :         const char *pszGeolocYFullName = poDS->papszDimName[poDS->nYDimID];
    4323           1 :         if (bSwitchedXY)
    4324             :         {
    4325           0 :             std::swap(pszGeolocXFullName, pszGeolocYFullName);
    4326           0 :             GDALPamDataset::SetMetadataItem("SWAP_XY", "YES", "GEOLOCATION");
    4327             :         }
    4328             : 
    4329           1 :         CPLDebug("GDAL_netCDF", "using variables %s and %s for GEOLOCATION",
    4330             :                  pszGeolocXFullName, pszGeolocYFullName);
    4331             : 
    4332           1 :         GDALPamDataset::SetMetadataItem("SRS", osGeolocWKT.c_str(),
    4333             :                                         "GEOLOCATION");
    4334             : 
    4335           2 :         CPLString osTMP;
    4336           1 :         osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4337           1 :                      pszGeolocXFullName);
    4338             : 
    4339           1 :         GDALPamDataset::SetMetadataItem("X_DATASET", osTMP, "GEOLOCATION");
    4340           1 :         GDALPamDataset::SetMetadataItem("X_BAND", "1", "GEOLOCATION");
    4341           1 :         osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4342           1 :                      pszGeolocYFullName);
    4343             : 
    4344           1 :         GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP, "GEOLOCATION");
    4345           1 :         GDALPamDataset::SetMetadataItem("Y_BAND", "1", "GEOLOCATION");
    4346             : 
    4347           1 :         GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0", "GEOLOCATION");
    4348           1 :         GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1", "GEOLOCATION");
    4349             : 
    4350           1 :         GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0", "GEOLOCATION");
    4351           1 :         GDALPamDataset::SetMetadataItem("LINE_STEP", "1", "GEOLOCATION");
    4352             : 
    4353           1 :         GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION",
    4354             :                                         "PIXEL_CENTER", "GEOLOCATION");
    4355             :     }
    4356             : 
    4357             :     // Set GeoTransform if we got a complete one - after projection has been set
    4358         348 :     if (bGotCfGT || bGotGdalGT)
    4359             :     {
    4360         198 :         m_bAddedProjectionVarsDefs = true;
    4361         198 :         m_bAddedProjectionVarsData = true;
    4362         198 :         SetGeoTransformNoUpdate(tmpGT);
    4363             :     }
    4364             : 
    4365             :     // Debugging reports.
    4366         348 :     CPLDebug("GDAL_netCDF",
    4367             :              "bGotGeogCS=%d bGotCfSRS=%d bGotCfGT=%d bGotCfWktSRS=%d "
    4368             :              "bGotGdalSRS=%d bGotGdalGT=%d",
    4369             :              static_cast<int>(bGotGeogCS), static_cast<int>(bGotCfSRS),
    4370             :              static_cast<int>(bGotCfGT), static_cast<int>(bGotCfWktSRS),
    4371             :              static_cast<int>(bGotGdalSRS), static_cast<int>(bGotGdalGT));
    4372             : 
    4373         348 :     if (!bGotCfGT && !bGotGdalGT)
    4374         150 :         CPLDebug("GDAL_netCDF", "did not get geotransform from CF nor GDAL!");
    4375             : 
    4376         348 :     if (!bGotGeogCS && !bGotCfSRS && !bGotGdalSRS && !bGotCfGT && !bGotCfWktSRS)
    4377         150 :         CPLDebug("GDAL_netCDF", "did not get projection from CF nor GDAL!");
    4378             : 
    4379             :     // wish of 6195
    4380             :     // we don't have CS/SRS, but we do have GT, and we live in -180,360 -90,90
    4381         348 :     if (!bGotGeogCS && !bGotCfSRS && !bGotGdalSRS && !bGotCfWktSRS)
    4382             :     {
    4383         217 :         if (bGotCfGT || bGotGdalGT)
    4384             :         {
    4385         134 :             bool bAssumedLongLat = CPLTestBool(CSLFetchNameValueDef(
    4386          67 :                 papszOpenOptions, "ASSUME_LONGLAT",
    4387             :                 CPLGetConfigOption("GDAL_NETCDF_ASSUME_LONGLAT", "NO")));
    4388             : 
    4389           2 :             if (bAssumedLongLat && tmpGT[0] >= -180 && tmpGT[0] < 360 &&
    4390           2 :                 (tmpGT[0] + tmpGT[1] * poDS->GetRasterXSize()) <= 360 &&
    4391          71 :                 tmpGT[3] <= 90 && tmpGT[3] > -90 &&
    4392           2 :                 (tmpGT[3] + tmpGT[5] * poDS->GetRasterYSize()) >= -90)
    4393             :             {
    4394             : 
    4395           2 :                 poDS->bIsGeographic = true;
    4396           2 :                 char *pszTempProjection = nullptr;
    4397             :                 // seems odd to use 4326 so OGC:CRS84
    4398           2 :                 oSRS.SetFromUserInput("OGC:CRS84");
    4399           2 :                 oSRS.exportToWkt(&pszTempProjection);
    4400           2 :                 if (returnProjStr != nullptr)
    4401             :                 {
    4402           0 :                     (*returnProjStr) = std::string(pszTempProjection);
    4403             :                 }
    4404             :                 else
    4405             :                 {
    4406           2 :                     m_bAddedProjectionVarsDefs = true;
    4407           2 :                     m_bAddedProjectionVarsData = true;
    4408           2 :                     SetSpatialRefNoUpdate(&oSRS);
    4409             :                 }
    4410           2 :                 CPLFree(pszTempProjection);
    4411             : 
    4412           2 :                 CPLDebug("netCDF",
    4413             :                          "Assumed Longitude Latitude CRS 'OGC:CRS84' because "
    4414             :                          "none otherwise available and geotransform within "
    4415             :                          "suitable bounds. "
    4416             :                          "Set GDAL_NETCDF_ASSUME_LONGLAT=NO as configuration "
    4417             :                          "option or "
    4418             :                          "    ASSUME_LONGLAT=NO as open option to bypass this "
    4419             :                          "assumption.");
    4420             :             }
    4421             :         }
    4422             :     }
    4423             : 
    4424             : // Search for Well-known GeogCS if got only CF WKT
    4425             : // Disabled for now, as a named datum also include control points
    4426             : // (see mailing list and bug#4281
    4427             : // For example, WGS84 vs. GDA94 (EPSG:3577) - AEA in netcdf_cf.py
    4428             : 
    4429             : // Disabled for now, but could be set in a config option.
    4430             : #if 0
    4431             :     bool bLookForWellKnownGCS = false;  // This could be a Config Option.
    4432             : 
    4433             :     if( bLookForWellKnownGCS && bGotCfSRS && !bGotGdalSRS )
    4434             :     {
    4435             :         // ET - Could use a more exhaustive method by scanning all EPSG codes in
    4436             :         // data/gcs.csv as proposed by Even in the gdal-dev mailing list "help
    4437             :         // for comparing two WKT".
    4438             :         // This code could be contributed to a new function.
    4439             :         // OGRSpatialReference * OGRSpatialReference::FindMatchingGeogCS(
    4440             :         //     const OGRSpatialReference *poOther) */
    4441             :         CPLDebug("GDAL_netCDF", "Searching for Well-known GeogCS");
    4442             :         const char *pszWKGCSList[] = { "WGS84", "WGS72", "NAD27", "NAD83" };
    4443             :         char *pszWKGCS = NULL;
    4444             :         oSRS.exportToPrettyWkt(&pszWKGCS);
    4445             :         for( size_t i = 0; i < sizeof(pszWKGCSList) / 8; i++ )
    4446             :         {
    4447             :             pszWKGCS = CPLStrdup(pszWKGCSList[i]);
    4448             :             OGRSpatialReference oSRSTmp;
    4449             :             oSRSTmp.SetWellKnownGeogCS(pszWKGCSList[i]);
    4450             :             // Set datum to unknown, bug #4281.
    4451             :             if( oSRSTmp.GetAttrNode("DATUM" ) )
    4452             :                 oSRSTmp.GetAttrNode("DATUM")->GetChild(0)->SetValue("unknown");
    4453             :             // Could use OGRSpatialReference::StripCTParms(), but let's keep
    4454             :             // TOWGS84.
    4455             :             oSRSTmp.GetRoot()->StripNodes("AXIS");
    4456             :             oSRSTmp.GetRoot()->StripNodes("AUTHORITY");
    4457             :             oSRSTmp.GetRoot()->StripNodes("EXTENSION");
    4458             : 
    4459             :             oSRSTmp.exportToPrettyWkt(&pszWKGCS);
    4460             :             if( oSRS.IsSameGeogCS(&oSRSTmp) )
    4461             :             {
    4462             :                 oSRS.SetWellKnownGeogCS(pszWKGCSList[i]);
    4463             :                 oSRS.exportToWkt(&(pszTempProjection));
    4464             :                 SetProjection(pszTempProjection);
    4465             :                 CPLFree(pszTempProjection);
    4466             :             }
    4467             :         }
    4468             :     }
    4469             : #endif
    4470             : }
    4471             : 
    4472         123 : void netCDFDataset::SetProjectionFromVar(int nGroupId, int nVarId,
    4473             :                                          bool bReadSRSOnly)
    4474             : {
    4475         123 :     SetProjectionFromVar(nGroupId, nVarId, bReadSRSOnly, nullptr, nullptr,
    4476             :                          nullptr, nullptr);
    4477         123 : }
    4478             : 
    4479         285 : bool netCDFDataset::ProcessNASAL2OceanGeoLocation(int nGroupId, int nVarId)
    4480             : {
    4481             :     // Cf https://oceancolor.gsfc.nasa.gov/docs/format/l2nc/
    4482             :     // and https://github.com/OSGeo/gdal/issues/7605
    4483             : 
    4484             :     // Check for a structure like:
    4485             :     /* netcdf SNPP_VIIRS.20230406T024200.L2.OC.NRT {
    4486             :         dimensions:
    4487             :             number_of_lines = 3248 ;
    4488             :             pixels_per_line = 3200 ;
    4489             :             [...]
    4490             :             pixel_control_points = 3200 ;
    4491             :         [...]
    4492             :         group: geophysical_data {
    4493             :           variables:
    4494             :             short aot_862(number_of_lines, pixels_per_line) ;  <-- nVarId
    4495             :                 [...]
    4496             :         }
    4497             :         group: navigation_data {
    4498             :           variables:
    4499             :             float longitude(number_of_lines, pixel_control_points) ;
    4500             :                 [...]
    4501             :             float latitude(number_of_lines, pixel_control_points) ;
    4502             :                 [...]
    4503             :         }
    4504             :     }
    4505             :     */
    4506             :     // Note that the longitude and latitude arrays are not indexed by the
    4507             :     // same dimensions. Handle only the case where
    4508             :     // pixel_control_points == pixels_per_line
    4509             :     // If there was a subsampling of the geolocation arrays, we'd need to
    4510             :     // add more logic.
    4511             : 
    4512         570 :     std::string osGroupName;
    4513         285 :     osGroupName.resize(NC_MAX_NAME);
    4514         285 :     NCDF_ERR(nc_inq_grpname(nGroupId, &osGroupName[0]));
    4515         285 :     osGroupName.resize(strlen(osGroupName.data()));
    4516         285 :     if (osGroupName != "geophysical_data")
    4517         284 :         return false;
    4518             : 
    4519           1 :     int nVarDims = 0;
    4520           1 :     NCDF_ERR(nc_inq_varndims(nGroupId, nVarId, &nVarDims));
    4521           1 :     if (nVarDims != 2)
    4522           0 :         return false;
    4523             : 
    4524           1 :     int nNavigationDataGrpId = 0;
    4525           1 :     if (nc_inq_grp_ncid(cdfid, "navigation_data", &nNavigationDataGrpId) !=
    4526             :         NC_NOERR)
    4527           0 :         return false;
    4528             : 
    4529             :     std::array<int, 2> anVarDimIds;
    4530           1 :     NCDF_ERR(nc_inq_vardimid(nGroupId, nVarId, anVarDimIds.data()));
    4531             : 
    4532           1 :     int nLongitudeId = 0;
    4533           1 :     int nLatitudeId = 0;
    4534           1 :     if (nc_inq_varid(nNavigationDataGrpId, "longitude", &nLongitudeId) !=
    4535           2 :             NC_NOERR ||
    4536           1 :         nc_inq_varid(nNavigationDataGrpId, "latitude", &nLatitudeId) !=
    4537             :             NC_NOERR)
    4538             :     {
    4539           0 :         return false;
    4540             :     }
    4541             : 
    4542           1 :     int nDimsLongitude = 0;
    4543           1 :     NCDF_ERR(
    4544             :         nc_inq_varndims(nNavigationDataGrpId, nLongitudeId, &nDimsLongitude));
    4545           1 :     int nDimsLatitude = 0;
    4546           1 :     NCDF_ERR(
    4547             :         nc_inq_varndims(nNavigationDataGrpId, nLatitudeId, &nDimsLatitude));
    4548           1 :     if (!(nDimsLongitude == 2 && nDimsLatitude == 2))
    4549             :     {
    4550           0 :         return false;
    4551             :     }
    4552             : 
    4553             :     std::array<int, 2> anDimLongitudeIds;
    4554           1 :     NCDF_ERR(nc_inq_vardimid(nNavigationDataGrpId, nLongitudeId,
    4555             :                              anDimLongitudeIds.data()));
    4556             :     std::array<int, 2> anDimLatitudeIds;
    4557           1 :     NCDF_ERR(nc_inq_vardimid(nNavigationDataGrpId, nLatitudeId,
    4558             :                              anDimLatitudeIds.data()));
    4559           1 :     if (anDimLongitudeIds != anDimLatitudeIds)
    4560             :     {
    4561           0 :         return false;
    4562             :     }
    4563             : 
    4564             :     std::array<size_t, 2> anSizeVarDimIds;
    4565             :     std::array<size_t, 2> anSizeLongLatIds;
    4566           2 :     if (!(nc_inq_dimlen(cdfid, anVarDimIds[0], &anSizeVarDimIds[0]) ==
    4567           1 :               NC_NOERR &&
    4568           1 :           nc_inq_dimlen(cdfid, anVarDimIds[1], &anSizeVarDimIds[1]) ==
    4569           1 :               NC_NOERR &&
    4570           1 :           nc_inq_dimlen(cdfid, anDimLongitudeIds[0], &anSizeLongLatIds[0]) ==
    4571           1 :               NC_NOERR &&
    4572           1 :           nc_inq_dimlen(cdfid, anDimLongitudeIds[1], &anSizeLongLatIds[1]) ==
    4573             :               NC_NOERR &&
    4574           1 :           anSizeVarDimIds == anSizeLongLatIds))
    4575             :     {
    4576           0 :         return false;
    4577             :     }
    4578             : 
    4579           1 :     const char *pszGeolocXFullName = "/navigation_data/longitude";
    4580           1 :     const char *pszGeolocYFullName = "/navigation_data/latitude";
    4581             : 
    4582           1 :     if (bSwitchedXY)
    4583             :     {
    4584           0 :         std::swap(pszGeolocXFullName, pszGeolocYFullName);
    4585           0 :         GDALPamDataset::SetMetadataItem("SWAP_XY", "YES", "GEOLOCATION");
    4586             :     }
    4587             : 
    4588           1 :     CPLDebug("GDAL_netCDF", "using variables %s and %s for GEOLOCATION",
    4589             :              pszGeolocXFullName, pszGeolocYFullName);
    4590             : 
    4591           1 :     GDALPamDataset::SetMetadataItem("SRS", SRS_WKT_WGS84_LAT_LONG,
    4592             :                                     "GEOLOCATION");
    4593             : 
    4594           1 :     CPLString osTMP;
    4595           1 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocXFullName);
    4596             : 
    4597           1 :     GDALPamDataset::SetMetadataItem("X_DATASET", osTMP, "GEOLOCATION");
    4598           1 :     GDALPamDataset::SetMetadataItem("X_BAND", "1", "GEOLOCATION");
    4599           1 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocYFullName);
    4600             : 
    4601           1 :     GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP, "GEOLOCATION");
    4602           1 :     GDALPamDataset::SetMetadataItem("Y_BAND", "1", "GEOLOCATION");
    4603             : 
    4604           1 :     GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0", "GEOLOCATION");
    4605           1 :     GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1", "GEOLOCATION");
    4606             : 
    4607           1 :     GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0", "GEOLOCATION");
    4608           1 :     GDALPamDataset::SetMetadataItem("LINE_STEP", "1", "GEOLOCATION");
    4609             : 
    4610           1 :     GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION", "PIXEL_CENTER",
    4611             :                                     "GEOLOCATION");
    4612           1 :     return true;
    4613             : }
    4614             : 
    4615         284 : bool netCDFDataset::ProcessNASAEMITGeoLocation(int nGroupId, int nVarId)
    4616             : {
    4617             :     // Cf https://earth.jpl.nasa.gov/emit/data/data-portal/coverage-and-forecasts/
    4618             : 
    4619             :     // Check for a structure like:
    4620             :     /* netcdf EMIT_L2A_RFL_001_20220903T163129_2224611_012 {
    4621             :         dimensions:
    4622             :             downtrack = 1280 ;
    4623             :             crosstrack = 1242 ;
    4624             :             bands = 285 ;
    4625             :             [...]
    4626             : 
    4627             :         variables:
    4628             :             float reflectance(downtrack, crosstrack, bands) ;
    4629             : 
    4630             :         group: location {
    4631             :           variables:
    4632             :                 double lon(downtrack, crosstrack) ;
    4633             :                         lon:_FillValue = -9999. ;
    4634             :                         lon:long_name = "Longitude (WGS-84)" ;
    4635             :                         lon:units = "degrees east" ;
    4636             :                 double lat(downtrack, crosstrack) ;
    4637             :                         lat:_FillValue = -9999. ;
    4638             :                         lat:long_name = "Latitude (WGS-84)" ;
    4639             :                         lat:units = "degrees north" ;
    4640             :           } // group location
    4641             : 
    4642             :     }
    4643             :     or
    4644             :     netcdf EMIT_L2B_MIN_001_20231024T055538_2329704_040 {
    4645             :         dimensions:
    4646             :                 downtrack = 1664 ;
    4647             :                 crosstrack = 1242 ;
    4648             :                 [...]
    4649             :         variables:
    4650             :                 float group_1_band_depth(downtrack, crosstrack) ;
    4651             :                         group_1_band_depth:_FillValue = -9999.f ;
    4652             :                         group_1_band_depth:long_name = "Group 1 Band Depth" ;
    4653             :                         group_1_band_depth:units = "unitless" ;
    4654             :                 [...]
    4655             :         group: location {
    4656             :           variables:
    4657             :                 double lon(downtrack, crosstrack) ;
    4658             :                         lon:_FillValue = -9999. ;
    4659             :                         lon:long_name = "Longitude (WGS-84)" ;
    4660             :                         lon:units = "degrees east" ;
    4661             :                 double lat(downtrack, crosstrack) ;
    4662             :                         lat:_FillValue = -9999. ;
    4663             :                         lat:long_name = "Latitude (WGS-84)" ;
    4664             :                         lat:units = "degrees north" ;
    4665             :         }
    4666             :     */
    4667             : 
    4668         284 :     int nVarDims = 0;
    4669         284 :     NCDF_ERR(nc_inq_varndims(nGroupId, nVarId, &nVarDims));
    4670         284 :     if (nVarDims != 2 && nVarDims != 3)
    4671          14 :         return false;
    4672             : 
    4673         270 :     int nLocationGrpId = 0;
    4674         270 :     if (nc_inq_grp_ncid(cdfid, "location", &nLocationGrpId) != NC_NOERR)
    4675          58 :         return false;
    4676             : 
    4677             :     std::array<int, 3> anVarDimIds;
    4678         212 :     NCDF_ERR(nc_inq_vardimid(nGroupId, nVarId, anVarDimIds.data()));
    4679         212 :     if (nYDimID != anVarDimIds[0] || nXDimID != anVarDimIds[1])
    4680          21 :         return false;
    4681             : 
    4682         191 :     int nLongitudeId = 0;
    4683         191 :     int nLatitudeId = 0;
    4684         229 :     if (nc_inq_varid(nLocationGrpId, "lon", &nLongitudeId) != NC_NOERR ||
    4685          38 :         nc_inq_varid(nLocationGrpId, "lat", &nLatitudeId) != NC_NOERR)
    4686             :     {
    4687         153 :         return false;
    4688             :     }
    4689             : 
    4690          38 :     int nDimsLongitude = 0;
    4691          38 :     NCDF_ERR(nc_inq_varndims(nLocationGrpId, nLongitudeId, &nDimsLongitude));
    4692          38 :     int nDimsLatitude = 0;
    4693          38 :     NCDF_ERR(nc_inq_varndims(nLocationGrpId, nLatitudeId, &nDimsLatitude));
    4694          38 :     if (!(nDimsLongitude == 2 && nDimsLatitude == 2))
    4695             :     {
    4696          34 :         return false;
    4697             :     }
    4698             : 
    4699             :     std::array<int, 2> anDimLongitudeIds;
    4700           4 :     NCDF_ERR(nc_inq_vardimid(nLocationGrpId, nLongitudeId,
    4701             :                              anDimLongitudeIds.data()));
    4702             :     std::array<int, 2> anDimLatitudeIds;
    4703           4 :     NCDF_ERR(
    4704             :         nc_inq_vardimid(nLocationGrpId, nLatitudeId, anDimLatitudeIds.data()));
    4705           4 :     if (anDimLongitudeIds != anDimLatitudeIds)
    4706             :     {
    4707           0 :         return false;
    4708             :     }
    4709             : 
    4710           8 :     if (anDimLongitudeIds[0] != anVarDimIds[0] ||
    4711           4 :         anDimLongitudeIds[1] != anVarDimIds[1])
    4712             :     {
    4713           0 :         return false;
    4714             :     }
    4715             : 
    4716           4 :     const char *pszGeolocXFullName = "/location/lon";
    4717           4 :     const char *pszGeolocYFullName = "/location/lat";
    4718             : 
    4719           4 :     CPLDebug("GDAL_netCDF", "using variables %s and %s for GEOLOCATION",
    4720             :              pszGeolocXFullName, pszGeolocYFullName);
    4721             : 
    4722           4 :     GDALPamDataset::SetMetadataItem("SRS", SRS_WKT_WGS84_LAT_LONG,
    4723             :                                     "GEOLOCATION");
    4724             : 
    4725           4 :     CPLString osTMP;
    4726           4 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocXFullName);
    4727             : 
    4728           4 :     GDALPamDataset::SetMetadataItem("X_DATASET", osTMP, "GEOLOCATION");
    4729           4 :     GDALPamDataset::SetMetadataItem("X_BAND", "1", "GEOLOCATION");
    4730           4 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocYFullName);
    4731             : 
    4732           4 :     GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP, "GEOLOCATION");
    4733           4 :     GDALPamDataset::SetMetadataItem("Y_BAND", "1", "GEOLOCATION");
    4734             : 
    4735           4 :     GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0", "GEOLOCATION");
    4736           4 :     GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1", "GEOLOCATION");
    4737             : 
    4738           4 :     GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0", "GEOLOCATION");
    4739           4 :     GDALPamDataset::SetMetadataItem("LINE_STEP", "1", "GEOLOCATION");
    4740             : 
    4741           4 :     GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION", "PIXEL_CENTER",
    4742             :                                     "GEOLOCATION");
    4743           4 :     return true;
    4744             : }
    4745             : 
    4746         348 : int netCDFDataset::ProcessCFGeolocation(int nGroupId, int nVarId,
    4747             :                                         const std::string &osGeolocWKT,
    4748             :                                         std::string &osGeolocXNameOut,
    4749             :                                         std::string &osGeolocYNameOut)
    4750             : {
    4751         348 :     bool bAddGeoloc = false;
    4752         348 :     char *pszCoordinates = nullptr;
    4753             : 
    4754             :     // If there is no explicit "coordinates" attribute, check if there are
    4755             :     // "lon" and "lat" 2D variables whose dimensions are the last
    4756             :     // 2 ones of the variable of interest.
    4757         348 :     if (NCDFGetAttr(nGroupId, nVarId, "coordinates", &pszCoordinates) !=
    4758             :         CE_None)
    4759             :     {
    4760         302 :         CPLFree(pszCoordinates);
    4761         302 :         pszCoordinates = nullptr;
    4762             : 
    4763         302 :         int nVarDims = 0;
    4764         302 :         NCDF_ERR(nc_inq_varndims(nGroupId, nVarId, &nVarDims));
    4765         302 :         if (nVarDims >= 2)
    4766             :         {
    4767         604 :             std::vector<int> anVarDimIds(nVarDims);
    4768         302 :             NCDF_ERR(nc_inq_vardimid(nGroupId, nVarId, anVarDimIds.data()));
    4769             : 
    4770         302 :             int nLongitudeId = 0;
    4771         302 :             int nLatitudeId = 0;
    4772         370 :             if (nc_inq_varid(nGroupId, "lon", &nLongitudeId) == NC_NOERR &&
    4773          68 :                 nc_inq_varid(nGroupId, "lat", &nLatitudeId) == NC_NOERR)
    4774             :             {
    4775          68 :                 int nDimsLongitude = 0;
    4776          68 :                 NCDF_ERR(
    4777             :                     nc_inq_varndims(nGroupId, nLongitudeId, &nDimsLongitude));
    4778          68 :                 int nDimsLatitude = 0;
    4779          68 :                 NCDF_ERR(
    4780             :                     nc_inq_varndims(nGroupId, nLatitudeId, &nDimsLatitude));
    4781          68 :                 if (nDimsLongitude == 2 && nDimsLatitude == 2)
    4782             :                 {
    4783          34 :                     std::vector<int> anDimLongitudeIds(2);
    4784          17 :                     NCDF_ERR(nc_inq_vardimid(nGroupId, nLongitudeId,
    4785             :                                              anDimLongitudeIds.data()));
    4786          34 :                     std::vector<int> anDimLatitudeIds(2);
    4787          17 :                     NCDF_ERR(nc_inq_vardimid(nGroupId, nLatitudeId,
    4788             :                                              anDimLatitudeIds.data()));
    4789          17 :                     if (anDimLongitudeIds == anDimLatitudeIds &&
    4790          34 :                         anVarDimIds[anVarDimIds.size() - 2] ==
    4791          51 :                             anDimLongitudeIds[0] &&
    4792          34 :                         anVarDimIds[anVarDimIds.size() - 1] ==
    4793          17 :                             anDimLongitudeIds[1])
    4794             :                     {
    4795          17 :                         pszCoordinates = CPLStrdup("lon lat");
    4796             :                     }
    4797             :                 }
    4798             :             }
    4799             :         }
    4800             :     }
    4801             : 
    4802         348 :     if (pszCoordinates)
    4803             :     {
    4804             :         // Get X and Y geolocation names from coordinates attribute.
    4805             :         const CPLStringList aosCoordinates(
    4806         126 :             NCDFTokenizeCoordinatesAttribute(pszCoordinates));
    4807          63 :         if (aosCoordinates.size() >= 2)
    4808             :         {
    4809             :             char szGeolocXName[NC_MAX_NAME + 1];
    4810             :             char szGeolocYName[NC_MAX_NAME + 1];
    4811          60 :             szGeolocXName[0] = '\0';
    4812          60 :             szGeolocYName[0] = '\0';
    4813             : 
    4814             :             // Test that each variable is longitude/latitude.
    4815         193 :             for (int i = 0; i < aosCoordinates.size(); i++)
    4816             :             {
    4817         133 :                 if (NCDFIsVarLongitude(nGroupId, -1, aosCoordinates[i]))
    4818             :                 {
    4819          49 :                     int nOtherGroupId = -1;
    4820          49 :                     int nOtherVarId = -1;
    4821             :                     // Check that the variable actually exists
    4822             :                     // Needed on Sentinel-3 products
    4823          49 :                     if (NCDFResolveVar(nGroupId, aosCoordinates[i],
    4824          49 :                                        &nOtherGroupId, &nOtherVarId) == CE_None)
    4825             :                     {
    4826          47 :                         snprintf(szGeolocXName, sizeof(szGeolocXName), "%s",
    4827             :                                  aosCoordinates[i]);
    4828             :                     }
    4829             :                 }
    4830          84 :                 else if (NCDFIsVarLatitude(nGroupId, -1, aosCoordinates[i]))
    4831             :                 {
    4832          49 :                     int nOtherGroupId = -1;
    4833          49 :                     int nOtherVarId = -1;
    4834             :                     // Check that the variable actually exists
    4835             :                     // Needed on Sentinel-3 products
    4836          49 :                     if (NCDFResolveVar(nGroupId, aosCoordinates[i],
    4837          49 :                                        &nOtherGroupId, &nOtherVarId) == CE_None)
    4838             :                     {
    4839          47 :                         snprintf(szGeolocYName, sizeof(szGeolocYName), "%s",
    4840             :                                  aosCoordinates[i]);
    4841             :                     }
    4842             :                 }
    4843             :             }
    4844             :             // Add GEOLOCATION metadata.
    4845          60 :             if (!EQUAL(szGeolocXName, "") && !EQUAL(szGeolocYName, ""))
    4846             :             {
    4847          47 :                 osGeolocXNameOut = szGeolocXName;
    4848          47 :                 osGeolocYNameOut = szGeolocYName;
    4849             : 
    4850          47 :                 char *pszGeolocXFullName = nullptr;
    4851          47 :                 char *pszGeolocYFullName = nullptr;
    4852          47 :                 if (NCDFResolveVarFullName(nGroupId, szGeolocXName,
    4853          94 :                                            &pszGeolocXFullName) == CE_None &&
    4854          47 :                     NCDFResolveVarFullName(nGroupId, szGeolocYName,
    4855             :                                            &pszGeolocYFullName) == CE_None)
    4856             :                 {
    4857          47 :                     if (bSwitchedXY)
    4858             :                     {
    4859           2 :                         std::swap(pszGeolocXFullName, pszGeolocYFullName);
    4860           2 :                         GDALPamDataset::SetMetadataItem("SWAP_XY", "YES",
    4861             :                                                         "GEOLOCATION");
    4862             :                     }
    4863             : 
    4864          47 :                     bAddGeoloc = true;
    4865          47 :                     CPLDebug("GDAL_netCDF",
    4866             :                              "using variables %s and %s for GEOLOCATION",
    4867             :                              pszGeolocXFullName, pszGeolocYFullName);
    4868             : 
    4869          47 :                     GDALPamDataset::SetMetadataItem("SRS", osGeolocWKT.c_str(),
    4870             :                                                     "GEOLOCATION");
    4871             : 
    4872          94 :                     CPLString osTMP;
    4873          47 :                     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4874          47 :                                  pszGeolocXFullName);
    4875             : 
    4876          47 :                     GDALPamDataset::SetMetadataItem("X_DATASET", osTMP,
    4877             :                                                     "GEOLOCATION");
    4878          47 :                     GDALPamDataset::SetMetadataItem("X_BAND", "1",
    4879             :                                                     "GEOLOCATION");
    4880          47 :                     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4881          47 :                                  pszGeolocYFullName);
    4882             : 
    4883          47 :                     GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP,
    4884             :                                                     "GEOLOCATION");
    4885          47 :                     GDALPamDataset::SetMetadataItem("Y_BAND", "1",
    4886             :                                                     "GEOLOCATION");
    4887             : 
    4888          47 :                     GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0",
    4889             :                                                     "GEOLOCATION");
    4890          47 :                     GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1",
    4891             :                                                     "GEOLOCATION");
    4892             : 
    4893          47 :                     GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0",
    4894             :                                                     "GEOLOCATION");
    4895          47 :                     GDALPamDataset::SetMetadataItem("LINE_STEP", "1",
    4896             :                                                     "GEOLOCATION");
    4897             : 
    4898          47 :                     GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION",
    4899             :                                                     "PIXEL_CENTER",
    4900             :                                                     "GEOLOCATION");
    4901             :                 }
    4902             :                 else
    4903             :                 {
    4904           0 :                     CPLDebug("GDAL_netCDF",
    4905             :                              "cannot resolve location of "
    4906             :                              "lat/lon variables specified by the coordinates "
    4907             :                              "attribute [%s]",
    4908             :                              pszCoordinates);
    4909             :                 }
    4910          47 :                 CPLFree(pszGeolocXFullName);
    4911          47 :                 CPLFree(pszGeolocYFullName);
    4912             :             }
    4913             :             else
    4914             :             {
    4915          13 :                 CPLDebug("GDAL_netCDF",
    4916             :                          "coordinates attribute [%s] is unsupported",
    4917             :                          pszCoordinates);
    4918             :             }
    4919             :         }
    4920             :         else
    4921             :         {
    4922           3 :             CPLDebug("GDAL_netCDF",
    4923             :                      "coordinates attribute [%s] with %d element(s) is "
    4924             :                      "unsupported",
    4925             :                      pszCoordinates, aosCoordinates.size());
    4926             :         }
    4927             :     }
    4928             : 
    4929             :     else
    4930             :     {
    4931         285 :         bAddGeoloc = ProcessNASAL2OceanGeoLocation(nGroupId, nVarId);
    4932             : 
    4933         285 :         if (!bAddGeoloc)
    4934         284 :             bAddGeoloc = ProcessNASAEMITGeoLocation(nGroupId, nVarId);
    4935             :     }
    4936             : 
    4937         348 :     CPLFree(pszCoordinates);
    4938             : 
    4939         348 :     return bAddGeoloc;
    4940             : }
    4941             : 
    4942           8 : CPLErr netCDFDataset::Set1DGeolocation(int nGroupId, int nVarId,
    4943             :                                        const char *szDimName)
    4944             : {
    4945             :     // Get values.
    4946           8 :     char *pszVarValues = nullptr;
    4947           8 :     CPLErr eErr = NCDFGet1DVar(nGroupId, nVarId, &pszVarValues);
    4948           8 :     if (eErr != CE_None)
    4949           0 :         return eErr;
    4950             : 
    4951             :     // Write metadata.
    4952           8 :     char szTemp[NC_MAX_NAME + 1 + 32] = {};
    4953           8 :     snprintf(szTemp, sizeof(szTemp), "%s_VALUES", szDimName);
    4954           8 :     GDALPamDataset::SetMetadataItem(szTemp, pszVarValues, "GEOLOCATION2");
    4955             : 
    4956           8 :     CPLFree(pszVarValues);
    4957             : 
    4958           8 :     return CE_None;
    4959             : }
    4960             : 
    4961           0 : double *netCDFDataset::Get1DGeolocation(CPL_UNUSED const char *szDimName,
    4962             :                                         int &nVarLen)
    4963             : {
    4964           0 :     nVarLen = 0;
    4965             : 
    4966             :     // Get Y_VALUES as tokens.
    4967             :     char **papszValues =
    4968           0 :         NCDFTokenizeArray(GetMetadataItem("Y_VALUES", "GEOLOCATION2"));
    4969           0 :     if (papszValues == nullptr)
    4970           0 :         return nullptr;
    4971             : 
    4972             :     // Initialize and fill array.
    4973           0 :     nVarLen = CSLCount(papszValues);
    4974             :     double *pdfVarValues =
    4975           0 :         static_cast<double *>(CPLCalloc(nVarLen, sizeof(double)));
    4976             : 
    4977           0 :     for (int i = 0, j = 0; i < nVarLen; i++)
    4978             :     {
    4979           0 :         if (!bBottomUp)
    4980           0 :             j = nVarLen - 1 - i;
    4981             :         else
    4982           0 :             j = i;  // Invert latitude values.
    4983           0 :         char *pszTemp = nullptr;
    4984           0 :         pdfVarValues[j] = CPLStrtod(papszValues[i], &pszTemp);
    4985             :     }
    4986           0 :     CSLDestroy(papszValues);
    4987             : 
    4988           0 :     return pdfVarValues;
    4989             : }
    4990             : 
    4991             : /************************************************************************/
    4992             : /*                        SetSpatialRefNoUpdate()                       */
    4993             : /************************************************************************/
    4994             : 
    4995         255 : void netCDFDataset::SetSpatialRefNoUpdate(const OGRSpatialReference *poSRS)
    4996             : {
    4997         255 :     m_oSRS.Clear();
    4998         255 :     if (poSRS)
    4999         248 :         m_oSRS = *poSRS;
    5000         255 :     m_bHasProjection = true;
    5001         255 : }
    5002             : 
    5003             : /************************************************************************/
    5004             : /*                          SetSpatialRef()                             */
    5005             : /************************************************************************/
    5006             : 
    5007          76 : CPLErr netCDFDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
    5008             : {
    5009         152 :     CPLMutexHolderD(&hNCMutex);
    5010             : 
    5011          76 :     if (GetAccess() != GA_Update || m_bHasProjection)
    5012             :     {
    5013           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    5014             :                  "netCDFDataset::_SetProjection() should only be called once "
    5015             :                  "in update mode!");
    5016           0 :         return CE_Failure;
    5017             :     }
    5018             : 
    5019          76 :     if (m_bHasGeoTransform)
    5020             :     {
    5021          32 :         SetSpatialRefNoUpdate(poSRS);
    5022             : 
    5023             :         // For NC4/NC4C, writing both projection variables and data,
    5024             :         // followed by redefining nodata value, cancels the projection
    5025             :         // info from the Band variable, so for now only write the
    5026             :         // variable definitions, and write data at the end.
    5027             :         // See https://trac.osgeo.org/gdal/ticket/7245
    5028          32 :         return AddProjectionVars(true, nullptr, nullptr);
    5029             :     }
    5030             : 
    5031          44 :     SetSpatialRefNoUpdate(poSRS);
    5032             : 
    5033          44 :     return CE_None;
    5034             : }
    5035             : 
    5036             : /************************************************************************/
    5037             : /*                     SetGeoTransformNoUpdate()                        */
    5038             : /************************************************************************/
    5039             : 
    5040         275 : void netCDFDataset::SetGeoTransformNoUpdate(const GDALGeoTransform &gt)
    5041             : {
    5042         275 :     m_gt = gt;
    5043         275 :     m_bHasGeoTransform = true;
    5044         275 : }
    5045             : 
    5046             : /************************************************************************/
    5047             : /*                          SetGeoTransform()                           */
    5048             : /************************************************************************/
    5049             : 
    5050          77 : CPLErr netCDFDataset::SetGeoTransform(const GDALGeoTransform &gt)
    5051             : {
    5052         154 :     CPLMutexHolderD(&hNCMutex);
    5053             : 
    5054          77 :     if (GetAccess() != GA_Update || m_bHasGeoTransform)
    5055             :     {
    5056           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    5057             :                  "netCDFDataset::SetGeoTransform() should only be called once "
    5058             :                  "in update mode!");
    5059           0 :         return CE_Failure;
    5060             :     }
    5061             : 
    5062          77 :     CPLDebug("GDAL_netCDF", "SetGeoTransform(%f,%f,%f,%f,%f,%f)", gt[0], gt[1],
    5063             :              gt[2], gt[3], gt[4], gt[5]);
    5064             : 
    5065          77 :     SetGeoTransformNoUpdate(gt);
    5066             : 
    5067          77 :     if (m_bHasProjection)
    5068             :     {
    5069             : 
    5070             :         // For NC4/NC4C, writing both projection variables and data,
    5071             :         // followed by redefining nodata value, cancels the projection
    5072             :         // info from the Band variable, so for now only write the
    5073             :         // variable definitions, and write data at the end.
    5074             :         // See https://trac.osgeo.org/gdal/ticket/7245
    5075           3 :         return AddProjectionVars(true, nullptr, nullptr);
    5076             :     }
    5077             : 
    5078          74 :     return CE_None;
    5079             : }
    5080             : 
    5081             : /************************************************************************/
    5082             : /*                         NCDFWriteSRSVariable()                       */
    5083             : /************************************************************************/
    5084             : 
    5085         128 : int NCDFWriteSRSVariable(int cdfid, const OGRSpatialReference *poSRS,
    5086             :                          char **ppszCFProjection, bool bWriteGDALTags,
    5087             :                          const std::string &srsVarName)
    5088             : {
    5089         128 :     char *pszCFProjection = nullptr;
    5090         128 :     char **papszKeyValues = nullptr;
    5091         128 :     poSRS->exportToCF1(&pszCFProjection, &papszKeyValues, nullptr, nullptr);
    5092             : 
    5093         128 :     if (bWriteGDALTags)
    5094             :     {
    5095         127 :         const char *pszWKT = CSLFetchNameValue(papszKeyValues, NCDF_CRS_WKT);
    5096         127 :         if (pszWKT)
    5097             :         {
    5098             :             // SPATIAL_REF is deprecated. Will be removed in GDAL 4.
    5099         127 :             papszKeyValues =
    5100         127 :                 CSLSetNameValue(papszKeyValues, NCDF_SPATIAL_REF, pszWKT);
    5101             :         }
    5102             :     }
    5103             : 
    5104         128 :     const int nValues = CSLCount(papszKeyValues);
    5105             : 
    5106             :     int NCDFVarID;
    5107         256 :     std::string varNameRadix(pszCFProjection);
    5108         128 :     int nCounter = 2;
    5109             :     while (true)
    5110             :     {
    5111         130 :         NCDFVarID = -1;
    5112         130 :         nc_inq_varid(cdfid, pszCFProjection, &NCDFVarID);
    5113         130 :         if (NCDFVarID < 0)
    5114         125 :             break;
    5115             : 
    5116           5 :         int nbAttr = 0;
    5117           5 :         NCDF_ERR(nc_inq_varnatts(cdfid, NCDFVarID, &nbAttr));
    5118           5 :         bool bSame = nbAttr == nValues;
    5119          41 :         for (int i = 0; bSame && (i < nbAttr); i++)
    5120             :         {
    5121             :             char szAttrName[NC_MAX_NAME + 1];
    5122          38 :             szAttrName[0] = 0;
    5123          38 :             NCDF_ERR(nc_inq_attname(cdfid, NCDFVarID, i, szAttrName));
    5124             : 
    5125             :             const char *pszValue =
    5126          38 :                 CSLFetchNameValue(papszKeyValues, szAttrName);
    5127          38 :             if (!pszValue)
    5128             :             {
    5129           0 :                 bSame = false;
    5130           2 :                 break;
    5131             :             }
    5132             : 
    5133          38 :             nc_type atttype = NC_NAT;
    5134          38 :             size_t attlen = 0;
    5135          38 :             NCDF_ERR(
    5136             :                 nc_inq_att(cdfid, NCDFVarID, szAttrName, &atttype, &attlen));
    5137          38 :             if (atttype != NC_CHAR && atttype != NC_DOUBLE)
    5138             :             {
    5139           0 :                 bSame = false;
    5140           0 :                 break;
    5141             :             }
    5142          38 :             if (atttype == NC_CHAR)
    5143             :             {
    5144          15 :                 if (CPLGetValueType(pszValue) != CPL_VALUE_STRING)
    5145             :                 {
    5146           0 :                     bSame = false;
    5147           0 :                     break;
    5148             :                 }
    5149          15 :                 std::string val;
    5150          15 :                 val.resize(attlen);
    5151          15 :                 nc_get_att_text(cdfid, NCDFVarID, szAttrName, &val[0]);
    5152          15 :                 if (val != pszValue)
    5153             :                 {
    5154           0 :                     bSame = false;
    5155           0 :                     break;
    5156             :                 }
    5157             :             }
    5158             :             else
    5159             :             {
    5160             :                 const CPLStringList aosTokens(
    5161          23 :                     CSLTokenizeString2(pszValue, ",", 0));
    5162          23 :                 if (static_cast<size_t>(aosTokens.size()) != attlen)
    5163             :                 {
    5164           0 :                     bSame = false;
    5165           0 :                     break;
    5166             :                 }
    5167             :                 double vals[2];
    5168          23 :                 nc_get_att_double(cdfid, NCDFVarID, szAttrName, vals);
    5169          44 :                 if (vals[0] != CPLAtof(aosTokens[0]) ||
    5170          21 :                     (attlen == 2 && vals[1] != CPLAtof(aosTokens[1])))
    5171             :                 {
    5172           2 :                     bSame = false;
    5173           2 :                     break;
    5174             :                 }
    5175             :             }
    5176             :         }
    5177           5 :         if (bSame)
    5178             :         {
    5179           3 :             *ppszCFProjection = pszCFProjection;
    5180           3 :             CSLDestroy(papszKeyValues);
    5181           3 :             return NCDFVarID;
    5182             :         }
    5183           2 :         CPLFree(pszCFProjection);
    5184           2 :         pszCFProjection =
    5185           2 :             CPLStrdup(CPLSPrintf("%s_%d", varNameRadix.c_str(), nCounter));
    5186           2 :         nCounter++;
    5187           2 :     }
    5188             : 
    5189         125 :     *ppszCFProjection = pszCFProjection;
    5190             : 
    5191             :     const char *pszVarName;
    5192             : 
    5193         125 :     if (srsVarName != "")
    5194             :     {
    5195          38 :         pszVarName = srsVarName.c_str();
    5196             :     }
    5197             :     else
    5198             :     {
    5199          87 :         pszVarName = pszCFProjection;
    5200             :     }
    5201             : 
    5202         125 :     int status = nc_def_var(cdfid, pszVarName, NC_CHAR, 0, nullptr, &NCDFVarID);
    5203         125 :     NCDF_ERR(status);
    5204        1199 :     for (int i = 0; i < nValues; ++i)
    5205             :     {
    5206        1074 :         char *pszKey = nullptr;
    5207        1074 :         const char *pszValue = CPLParseNameValue(papszKeyValues[i], &pszKey);
    5208        1074 :         if (pszKey && pszValue)
    5209             :         {
    5210        2148 :             const CPLStringList aosTokens(CSLTokenizeString2(pszValue, ",", 0));
    5211        1074 :             double adfValues[2] = {0, 0};
    5212        1074 :             const int nDoubleCount = std::min(2, aosTokens.size());
    5213        1074 :             if (!(aosTokens.size() == 2 &&
    5214        2147 :                   CPLGetValueType(aosTokens[0]) != CPL_VALUE_STRING) &&
    5215        1073 :                 CPLGetValueType(pszValue) == CPL_VALUE_STRING)
    5216             :             {
    5217         499 :                 status = nc_put_att_text(cdfid, NCDFVarID, pszKey,
    5218             :                                          strlen(pszValue), pszValue);
    5219             :             }
    5220             :             else
    5221             :             {
    5222        1151 :                 for (int j = 0; j < nDoubleCount; ++j)
    5223         576 :                     adfValues[j] = CPLAtof(aosTokens[j]);
    5224         575 :                 status = nc_put_att_double(cdfid, NCDFVarID, pszKey, NC_DOUBLE,
    5225             :                                            nDoubleCount, adfValues);
    5226             :             }
    5227        1074 :             NCDF_ERR(status);
    5228             :         }
    5229        1074 :         CPLFree(pszKey);
    5230             :     }
    5231             : 
    5232         125 :     CSLDestroy(papszKeyValues);
    5233         125 :     return NCDFVarID;
    5234             : }
    5235             : 
    5236             : /************************************************************************/
    5237             : /*                   NCDFWriteLonLatVarsAttributes()                    */
    5238             : /************************************************************************/
    5239             : 
    5240         101 : void NCDFWriteLonLatVarsAttributes(nccfdriver::netCDFVID &vcdf, int nVarLonID,
    5241             :                                    int nVarLatID)
    5242             : {
    5243             : 
    5244             :     try
    5245             :     {
    5246         101 :         vcdf.nc_put_vatt_text(nVarLatID, CF_STD_NAME, CF_LATITUDE_STD_NAME);
    5247         101 :         vcdf.nc_put_vatt_text(nVarLatID, CF_LNG_NAME, CF_LATITUDE_LNG_NAME);
    5248         101 :         vcdf.nc_put_vatt_text(nVarLatID, CF_UNITS, CF_DEGREES_NORTH);
    5249         101 :         vcdf.nc_put_vatt_text(nVarLonID, CF_STD_NAME, CF_LONGITUDE_STD_NAME);
    5250         101 :         vcdf.nc_put_vatt_text(nVarLonID, CF_LNG_NAME, CF_LONGITUDE_LNG_NAME);
    5251         101 :         vcdf.nc_put_vatt_text(nVarLonID, CF_UNITS, CF_DEGREES_EAST);
    5252             :     }
    5253           0 :     catch (nccfdriver::SG_Exception &e)
    5254             :     {
    5255           0 :         CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5256             :     }
    5257         101 : }
    5258             : 
    5259             : /************************************************************************/
    5260             : /*                   NCDFWriteRLonRLatVarsAttributes()                    */
    5261             : /************************************************************************/
    5262             : 
    5263           0 : void NCDFWriteRLonRLatVarsAttributes(nccfdriver::netCDFVID &vcdf,
    5264             :                                      int nVarRLonID, int nVarRLatID)
    5265             : {
    5266             :     try
    5267             :     {
    5268           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_STD_NAME, "grid_latitude");
    5269           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_LNG_NAME,
    5270             :                               "latitude in rotated pole grid");
    5271           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_UNITS, "degrees");
    5272           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_AXIS, "Y");
    5273             : 
    5274           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_STD_NAME, "grid_longitude");
    5275           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_LNG_NAME,
    5276             :                               "longitude in rotated pole grid");
    5277           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_UNITS, "degrees");
    5278           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_AXIS, "X");
    5279             :     }
    5280           0 :     catch (nccfdriver::SG_Exception &e)
    5281             :     {
    5282           0 :         CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5283             :     }
    5284           0 : }
    5285             : 
    5286             : /************************************************************************/
    5287             : /*                        NCDFGetProjectedCFUnit()                      */
    5288             : /************************************************************************/
    5289             : 
    5290          40 : std::string NCDFGetProjectedCFUnit(const OGRSpatialReference *poSRS)
    5291             : {
    5292          40 :     char *pszUnitsToWrite = nullptr;
    5293          40 :     poSRS->exportToCF1(nullptr, nullptr, &pszUnitsToWrite, nullptr);
    5294          40 :     std::string osRet = pszUnitsToWrite ? pszUnitsToWrite : std::string();
    5295          40 :     CPLFree(pszUnitsToWrite);
    5296          80 :     return osRet;
    5297             : }
    5298             : 
    5299             : /************************************************************************/
    5300             : /*                     NCDFWriteXYVarsAttributes()                      */
    5301             : /************************************************************************/
    5302             : 
    5303          29 : void NCDFWriteXYVarsAttributes(nccfdriver::netCDFVID &vcdf, int nVarXID,
    5304             :                                int nVarYID, const OGRSpatialReference *poSRS)
    5305             : {
    5306          58 :     const std::string osUnitsToWrite = NCDFGetProjectedCFUnit(poSRS);
    5307             : 
    5308             :     try
    5309             :     {
    5310          29 :         vcdf.nc_put_vatt_text(nVarXID, CF_STD_NAME, CF_PROJ_X_COORD);
    5311          29 :         vcdf.nc_put_vatt_text(nVarXID, CF_LNG_NAME, CF_PROJ_X_COORD_LONG_NAME);
    5312          29 :         if (!osUnitsToWrite.empty())
    5313          29 :             vcdf.nc_put_vatt_text(nVarXID, CF_UNITS, osUnitsToWrite.c_str());
    5314          29 :         vcdf.nc_put_vatt_text(nVarYID, CF_STD_NAME, CF_PROJ_Y_COORD);
    5315          29 :         vcdf.nc_put_vatt_text(nVarYID, CF_LNG_NAME, CF_PROJ_Y_COORD_LONG_NAME);
    5316          29 :         if (!osUnitsToWrite.empty())
    5317          29 :             vcdf.nc_put_vatt_text(nVarYID, CF_UNITS, osUnitsToWrite.c_str());
    5318             :     }
    5319           0 :     catch (nccfdriver::SG_Exception &e)
    5320             :     {
    5321           0 :         CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5322             :     }
    5323          29 : }
    5324             : 
    5325             : /************************************************************************/
    5326             : /*                          AddProjectionVars()                         */
    5327             : /************************************************************************/
    5328             : 
    5329         164 : CPLErr netCDFDataset::AddProjectionVars(bool bDefsOnly,
    5330             :                                         GDALProgressFunc pfnProgress,
    5331             :                                         void *pProgressData)
    5332             : {
    5333         164 :     if (nCFVersion >= 1.8)
    5334           0 :         return CE_None;  // do nothing
    5335             : 
    5336         164 :     bool bWriteGridMapping = false;
    5337         164 :     bool bWriteLonLat = false;
    5338         164 :     bool bHasGeoloc = false;
    5339         164 :     bool bWriteGDALTags = false;
    5340         164 :     bool bWriteGeoTransform = false;
    5341             : 
    5342             :     // For GEOLOCATION information.
    5343         164 :     GDALDatasetH hDS_X = nullptr;
    5344         164 :     GDALRasterBandH hBand_X = nullptr;
    5345         164 :     GDALDatasetH hDS_Y = nullptr;
    5346         164 :     GDALRasterBandH hBand_Y = nullptr;
    5347             : 
    5348         328 :     OGRSpatialReference oSRS(m_oSRS);
    5349         164 :     if (!m_oSRS.IsEmpty())
    5350             :     {
    5351         138 :         if (oSRS.IsProjected())
    5352          50 :             bIsProjected = true;
    5353          88 :         else if (oSRS.IsGeographic())
    5354          88 :             bIsGeographic = true;
    5355             :     }
    5356             : 
    5357         164 :     if (bDefsOnly)
    5358             :     {
    5359          82 :         char *pszProjection = nullptr;
    5360          82 :         m_oSRS.exportToWkt(&pszProjection);
    5361          82 :         CPLDebug("GDAL_netCDF",
    5362             :                  "SetProjection, WKT now = [%s]\nprojected: %d geographic: %d",
    5363          82 :                  pszProjection ? pszProjection : "(null)",
    5364          82 :                  static_cast<int>(bIsProjected),
    5365          82 :                  static_cast<int>(bIsGeographic));
    5366          82 :         CPLFree(pszProjection);
    5367             : 
    5368          82 :         if (!m_bHasGeoTransform)
    5369           5 :             CPLDebug("GDAL_netCDF",
    5370             :                      "netCDFDataset::AddProjectionVars() called, "
    5371             :                      "but GeoTransform has not yet been defined!");
    5372             : 
    5373          82 :         if (!m_bHasProjection)
    5374           6 :             CPLDebug("GDAL_netCDF",
    5375             :                      "netCDFDataset::AddProjectionVars() called, "
    5376             :                      "but Projection has not yet been defined!");
    5377             :     }
    5378             : 
    5379             :     // Check GEOLOCATION information.
    5380         164 :     char **papszGeolocationInfo = netCDFDataset::GetMetadata("GEOLOCATION");
    5381         164 :     if (papszGeolocationInfo != nullptr)
    5382             :     {
    5383             :         // Look for geolocation datasets.
    5384             :         const char *pszDSName =
    5385          10 :             CSLFetchNameValue(papszGeolocationInfo, "X_DATASET");
    5386          10 :         if (pszDSName != nullptr)
    5387          10 :             hDS_X = GDALOpenShared(pszDSName, GA_ReadOnly);
    5388          10 :         pszDSName = CSLFetchNameValue(papszGeolocationInfo, "Y_DATASET");
    5389          10 :         if (pszDSName != nullptr)
    5390          10 :             hDS_Y = GDALOpenShared(pszDSName, GA_ReadOnly);
    5391             : 
    5392          10 :         if (hDS_X != nullptr && hDS_Y != nullptr)
    5393             :         {
    5394          10 :             int nBand = std::max(1, atoi(CSLFetchNameValueDef(
    5395          10 :                                         papszGeolocationInfo, "X_BAND", "0")));
    5396          10 :             hBand_X = GDALGetRasterBand(hDS_X, nBand);
    5397          10 :             nBand = std::max(1, atoi(CSLFetchNameValueDef(papszGeolocationInfo,
    5398          10 :                                                           "Y_BAND", "0")));
    5399          10 :             hBand_Y = GDALGetRasterBand(hDS_Y, nBand);
    5400             : 
    5401             :             // If geoloc bands are found, do basic validation based on their
    5402             :             // dimensions.
    5403          10 :             if (hBand_X != nullptr && hBand_Y != nullptr)
    5404             :             {
    5405          10 :                 int nXSize_XBand = GDALGetRasterXSize(hDS_X);
    5406          10 :                 int nYSize_XBand = GDALGetRasterYSize(hDS_X);
    5407          10 :                 int nXSize_YBand = GDALGetRasterXSize(hDS_Y);
    5408          10 :                 int nYSize_YBand = GDALGetRasterYSize(hDS_Y);
    5409             : 
    5410             :                 // TODO 1D geolocation arrays not implemented.
    5411          10 :                 if (nYSize_XBand == 1 && nYSize_YBand == 1)
    5412             :                 {
    5413           0 :                     bHasGeoloc = false;
    5414           0 :                     CPLDebug("GDAL_netCDF",
    5415             :                              "1D GEOLOCATION arrays not supported yet");
    5416             :                 }
    5417             :                 // 2D bands must have same sizes as the raster bands.
    5418          10 :                 else if (nXSize_XBand != nRasterXSize ||
    5419          10 :                          nYSize_XBand != nRasterYSize ||
    5420          10 :                          nXSize_YBand != nRasterXSize ||
    5421          10 :                          nYSize_YBand != nRasterYSize)
    5422             :                 {
    5423           0 :                     bHasGeoloc = false;
    5424           0 :                     CPLDebug("GDAL_netCDF",
    5425             :                              "GEOLOCATION array sizes (%dx%d %dx%d) differ "
    5426             :                              "from raster (%dx%d), not supported",
    5427             :                              nXSize_XBand, nYSize_XBand, nXSize_YBand,
    5428             :                              nYSize_YBand, nRasterXSize, nRasterYSize);
    5429             :                 }
    5430             :                 else
    5431             :                 {
    5432          10 :                     bHasGeoloc = true;
    5433          10 :                     CPLDebug("GDAL_netCDF",
    5434             :                              "dataset has GEOLOCATION information, will try to "
    5435             :                              "write it");
    5436             :                 }
    5437             :             }
    5438             :         }
    5439             :     }
    5440             : 
    5441             :     // Process projection options.
    5442         164 :     if (bIsProjected)
    5443             :     {
    5444             :         bool bIsCfProjection =
    5445          50 :             oSRS.exportToCF1(nullptr, nullptr, nullptr, nullptr) == OGRERR_NONE;
    5446          50 :         bWriteGridMapping = true;
    5447          50 :         bWriteGDALTags = CPL_TO_BOOL(
    5448          50 :             CSLFetchBoolean(papszCreationOptions, "WRITE_GDAL_TAGS", TRUE));
    5449             :         // Force WRITE_GDAL_TAGS if is not a CF projection.
    5450          50 :         if (!bWriteGDALTags && !bIsCfProjection)
    5451           0 :             bWriteGDALTags = true;
    5452          50 :         if (bWriteGDALTags)
    5453          50 :             bWriteGeoTransform = true;
    5454             : 
    5455             :         // Write lon/lat: default is NO, except if has geolocation.
    5456             :         // With IF_NEEDED: write if has geoloc or is not CF projection.
    5457             :         const char *pszValue =
    5458          50 :             CSLFetchNameValue(papszCreationOptions, "WRITE_LONLAT");
    5459          50 :         if (pszValue)
    5460             :         {
    5461           2 :             if (EQUAL(pszValue, "IF_NEEDED"))
    5462             :             {
    5463           0 :                 bWriteLonLat = bHasGeoloc || !bIsCfProjection;
    5464             :             }
    5465             :             else
    5466             :             {
    5467           2 :                 bWriteLonLat = CPLTestBool(pszValue);
    5468             :             }
    5469             :         }
    5470             :         else
    5471             :         {
    5472          48 :             bWriteLonLat = bHasGeoloc;
    5473             :         }
    5474             : 
    5475             :         // Save value of pszCFCoordinates for later.
    5476          50 :         if (bWriteLonLat)
    5477             :         {
    5478           4 :             pszCFCoordinates = NCDF_LONLAT;
    5479             :         }
    5480             :     }
    5481             :     else
    5482             :     {
    5483             :         // Files without a Datum will not have a grid_mapping variable and
    5484             :         // geographic information.
    5485         114 :         bWriteGridMapping = bIsGeographic;
    5486             : 
    5487         114 :         if (bHasGeoloc)
    5488             :         {
    5489           8 :             bWriteLonLat = true;
    5490             :         }
    5491             :         else
    5492             :         {
    5493         106 :             bWriteGDALTags = CPL_TO_BOOL(CSLFetchBoolean(
    5494         106 :                 papszCreationOptions, "WRITE_GDAL_TAGS", bWriteGridMapping));
    5495         106 :             if (bWriteGDALTags)
    5496          88 :                 bWriteGeoTransform = true;
    5497             : 
    5498         106 :             const char *pszValue = CSLFetchNameValueDef(papszCreationOptions,
    5499             :                                                         "WRITE_LONLAT", "YES");
    5500         106 :             if (EQUAL(pszValue, "IF_NEEDED"))
    5501           0 :                 bWriteLonLat = true;
    5502             :             else
    5503         106 :                 bWriteLonLat = CPLTestBool(pszValue);
    5504             :             //  Don't write lon/lat if no source geotransform.
    5505         106 :             if (!m_bHasGeoTransform)
    5506           0 :                 bWriteLonLat = false;
    5507             :             // If we don't write lon/lat, set dimnames to X/Y and write gdal
    5508             :             // tags.
    5509         106 :             if (!bWriteLonLat)
    5510             :             {
    5511           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    5512             :                          "creating geographic file without lon/lat values!");
    5513           0 :                 if (m_bHasGeoTransform)
    5514             :                 {
    5515           0 :                     bWriteGDALTags = true;  // Not desirable if no geotransform.
    5516           0 :                     bWriteGeoTransform = true;
    5517             :                 }
    5518             :             }
    5519             :         }
    5520             :     }
    5521             : 
    5522             :     // Make sure we write grid_mapping if we need to write GDAL tags.
    5523         164 :     if (bWriteGDALTags)
    5524         138 :         bWriteGridMapping = true;
    5525             : 
    5526             :     // bottom-up value: new driver is bottom-up by default.
    5527             :     // Override with WRITE_BOTTOMUP.
    5528         164 :     bBottomUp = CPL_TO_BOOL(
    5529         164 :         CSLFetchBoolean(papszCreationOptions, "WRITE_BOTTOMUP", TRUE));
    5530             : 
    5531         164 :     if (bDefsOnly)
    5532             :     {
    5533          82 :         CPLDebug(
    5534             :             "GDAL_netCDF",
    5535             :             "bIsProjected=%d bIsGeographic=%d bWriteGridMapping=%d "
    5536             :             "bWriteGDALTags=%d bWriteLonLat=%d bBottomUp=%d bHasGeoloc=%d",
    5537          82 :             static_cast<int>(bIsProjected), static_cast<int>(bIsGeographic),
    5538             :             static_cast<int>(bWriteGridMapping),
    5539             :             static_cast<int>(bWriteGDALTags), static_cast<int>(bWriteLonLat),
    5540          82 :             static_cast<int>(bBottomUp), static_cast<int>(bHasGeoloc));
    5541             :     }
    5542             : 
    5543             :     // Exit if nothing to do.
    5544         164 :     if (!bIsProjected && !bWriteLonLat)
    5545           0 :         return CE_None;
    5546             : 
    5547             :     // Define dimension names.
    5548             : 
    5549         164 :     constexpr const char *ROTATED_POLE_VAR_NAME = "rotated_pole";
    5550             : 
    5551         164 :     if (bDefsOnly)
    5552             :     {
    5553          82 :         int nVarLonID = -1;
    5554          82 :         int nVarLatID = -1;
    5555          82 :         int nVarXID = -1;
    5556          82 :         int nVarYID = -1;
    5557             : 
    5558          82 :         m_bAddedProjectionVarsDefs = true;
    5559             : 
    5560             :         // Make sure we are in define mode.
    5561          82 :         SetDefineMode(true);
    5562             : 
    5563             :         // Write projection attributes.
    5564          82 :         if (bWriteGridMapping)
    5565             :         {
    5566          69 :             const int NCDFVarID = NCDFWriteSRSVariable(
    5567             :                 cdfid, &oSRS, &pszCFProjection, bWriteGDALTags);
    5568          69 :             if (NCDFVarID < 0)
    5569           0 :                 return CE_Failure;
    5570             : 
    5571             :             // Optional GDAL custom projection tags.
    5572          69 :             if (bWriteGDALTags)
    5573             :             {
    5574         138 :                 CPLString osGeoTransform;
    5575         483 :                 for (int i = 0; i < 6; i++)
    5576             :                 {
    5577         414 :                     osGeoTransform += CPLSPrintf("%.17g ", m_gt[i]);
    5578             :                 }
    5579          69 :                 CPLDebug("GDAL_netCDF", "szGeoTransform = %s",
    5580             :                          osGeoTransform.c_str());
    5581             : 
    5582             :                 // if( strlen(pszProj4Defn) > 0 ) {
    5583             :                 //     nc_put_att_text(cdfid, NCDFVarID, "proj4",
    5584             :                 //                      strlen(pszProj4Defn), pszProj4Defn);
    5585             :                 // }
    5586             : 
    5587             :                 // For now, write the geotransform for back-compat or else
    5588             :                 // the old (1.8.1) driver overrides the CF geotransform with
    5589             :                 // empty values from dfNN, dfSN, dfEE, dfWE;
    5590             : 
    5591             :                 // TODO: fix this in 1.8 branch, and then remove this here.
    5592          69 :                 if (bWriteGeoTransform && m_bHasGeoTransform)
    5593             :                 {
    5594             :                     {
    5595          68 :                         const int status = nc_put_att_text(
    5596             :                             cdfid, NCDFVarID, NCDF_GEOTRANSFORM,
    5597             :                             osGeoTransform.size(), osGeoTransform.c_str());
    5598          68 :                         NCDF_ERR(status);
    5599             :                     }
    5600             :                 }
    5601             :             }
    5602             : 
    5603             :             // Write projection variable to band variable.
    5604             :             // Need to call later if there are no bands.
    5605          69 :             AddGridMappingRef();
    5606             :         }  // end if( bWriteGridMapping )
    5607             : 
    5608             :         // Write CF Projection vars.
    5609             : 
    5610          82 :         const bool bIsRotatedPole =
    5611         151 :             pszCFProjection != nullptr &&
    5612          69 :             EQUAL(pszCFProjection, ROTATED_POLE_VAR_NAME);
    5613          82 :         if (bIsRotatedPole)
    5614             :         {
    5615             :             // Rename dims to rlat/rlon.
    5616             :             papszDimName
    5617           0 :                 .Clear();  // If we add other dims one day, this has to change
    5618           0 :             papszDimName.AddString(NCDF_DIMNAME_RLAT);
    5619           0 :             papszDimName.AddString(NCDF_DIMNAME_RLON);
    5620             : 
    5621           0 :             int status = nc_rename_dim(cdfid, nYDimID, NCDF_DIMNAME_RLAT);
    5622           0 :             NCDF_ERR(status);
    5623           0 :             status = nc_rename_dim(cdfid, nXDimID, NCDF_DIMNAME_RLON);
    5624           0 :             NCDF_ERR(status);
    5625             :         }
    5626             :         // Rename dimensions if lon/lat.
    5627          82 :         else if (!bIsProjected && !bHasGeoloc)
    5628             :         {
    5629             :             // Rename dims to lat/lon.
    5630             :             papszDimName
    5631          53 :                 .Clear();  // If we add other dims one day, this has to change
    5632          53 :             papszDimName.AddString(NCDF_DIMNAME_LAT);
    5633          53 :             papszDimName.AddString(NCDF_DIMNAME_LON);
    5634             : 
    5635          53 :             int status = nc_rename_dim(cdfid, nYDimID, NCDF_DIMNAME_LAT);
    5636          53 :             NCDF_ERR(status);
    5637          53 :             status = nc_rename_dim(cdfid, nXDimID, NCDF_DIMNAME_LON);
    5638          53 :             NCDF_ERR(status);
    5639             :         }
    5640             : 
    5641             :         // Write X/Y attributes.
    5642             :         else /* if( bIsProjected || bHasGeoloc ) */
    5643             :         {
    5644             :             // X
    5645             :             int anXDims[1];
    5646          29 :             anXDims[0] = nXDimID;
    5647          29 :             CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d)", cdfid,
    5648             :                      CF_PROJ_X_VAR_NAME, NC_DOUBLE);
    5649          29 :             int status = nc_def_var(cdfid, CF_PROJ_X_VAR_NAME, NC_DOUBLE, 1,
    5650             :                                     anXDims, &nVarXID);
    5651          29 :             NCDF_ERR(status);
    5652             : 
    5653             :             // Y
    5654             :             int anYDims[1];
    5655          29 :             anYDims[0] = nYDimID;
    5656          29 :             CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d)", cdfid,
    5657             :                      CF_PROJ_Y_VAR_NAME, NC_DOUBLE);
    5658          29 :             status = nc_def_var(cdfid, CF_PROJ_Y_VAR_NAME, NC_DOUBLE, 1,
    5659             :                                 anYDims, &nVarYID);
    5660          29 :             NCDF_ERR(status);
    5661             : 
    5662          29 :             if (bIsProjected)
    5663             :             {
    5664          25 :                 NCDFWriteXYVarsAttributes(this->vcdf, nVarXID, nVarYID, &oSRS);
    5665             :             }
    5666             :             else
    5667             :             {
    5668           4 :                 CPLAssert(bHasGeoloc);
    5669             :                 try
    5670             :                 {
    5671           4 :                     vcdf.nc_put_vatt_text(nVarXID, CF_AXIS, CF_SG_X_AXIS);
    5672           4 :                     vcdf.nc_put_vatt_text(nVarXID, CF_LNG_NAME,
    5673             :                                           "x-coordinate in Cartesian system");
    5674           4 :                     vcdf.nc_put_vatt_text(nVarXID, CF_UNITS, "m");
    5675           4 :                     vcdf.nc_put_vatt_text(nVarYID, CF_AXIS, CF_SG_Y_AXIS);
    5676           4 :                     vcdf.nc_put_vatt_text(nVarYID, CF_LNG_NAME,
    5677             :                                           "y-coordinate in Cartesian system");
    5678           4 :                     vcdf.nc_put_vatt_text(nVarYID, CF_UNITS, "m");
    5679             : 
    5680           4 :                     pszCFCoordinates = NCDF_LONLAT;
    5681             :                 }
    5682           0 :                 catch (nccfdriver::SG_Exception &e)
    5683             :                 {
    5684           0 :                     CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5685           0 :                     return CE_Failure;
    5686             :                 }
    5687             :             }
    5688             :         }
    5689             : 
    5690             :         // Write lat/lon attributes if needed.
    5691          82 :         if (bWriteLonLat)
    5692             :         {
    5693          59 :             int *panLatDims = nullptr;
    5694          59 :             int *panLonDims = nullptr;
    5695          59 :             int nLatDims = -1;
    5696          59 :             int nLonDims = -1;
    5697             : 
    5698             :             // Get information.
    5699          59 :             if (bHasGeoloc)
    5700             :             {
    5701             :                 // Geoloc
    5702           5 :                 nLatDims = 2;
    5703             :                 panLatDims =
    5704           5 :                     static_cast<int *>(CPLCalloc(nLatDims, sizeof(int)));
    5705           5 :                 panLatDims[0] = nYDimID;
    5706           5 :                 panLatDims[1] = nXDimID;
    5707           5 :                 nLonDims = 2;
    5708             :                 panLonDims =
    5709           5 :                     static_cast<int *>(CPLCalloc(nLonDims, sizeof(int)));
    5710           5 :                 panLonDims[0] = nYDimID;
    5711           5 :                 panLonDims[1] = nXDimID;
    5712             :             }
    5713          54 :             else if (bIsProjected)
    5714             :             {
    5715             :                 // Projected
    5716           1 :                 nLatDims = 2;
    5717             :                 panLatDims =
    5718           1 :                     static_cast<int *>(CPLCalloc(nLatDims, sizeof(int)));
    5719           1 :                 panLatDims[0] = nYDimID;
    5720           1 :                 panLatDims[1] = nXDimID;
    5721           1 :                 nLonDims = 2;
    5722             :                 panLonDims =
    5723           1 :                     static_cast<int *>(CPLCalloc(nLonDims, sizeof(int)));
    5724           1 :                 panLonDims[0] = nYDimID;
    5725           1 :                 panLonDims[1] = nXDimID;
    5726             :             }
    5727             :             else
    5728             :             {
    5729             :                 // Geographic
    5730          53 :                 nLatDims = 1;
    5731             :                 panLatDims =
    5732          53 :                     static_cast<int *>(CPLCalloc(nLatDims, sizeof(int)));
    5733          53 :                 panLatDims[0] = nYDimID;
    5734          53 :                 nLonDims = 1;
    5735             :                 panLonDims =
    5736          53 :                     static_cast<int *>(CPLCalloc(nLonDims, sizeof(int)));
    5737          53 :                 panLonDims[0] = nXDimID;
    5738             :             }
    5739             : 
    5740          59 :             nc_type eLonLatType = NC_NAT;
    5741          59 :             if (bIsProjected)
    5742             :             {
    5743           2 :                 eLonLatType = NC_FLOAT;
    5744           4 :                 const char *pszValue = CSLFetchNameValueDef(
    5745           2 :                     papszCreationOptions, "TYPE_LONLAT", "FLOAT");
    5746           2 :                 if (EQUAL(pszValue, "DOUBLE"))
    5747           0 :                     eLonLatType = NC_DOUBLE;
    5748             :             }
    5749             :             else
    5750             :             {
    5751          57 :                 eLonLatType = NC_DOUBLE;
    5752         114 :                 const char *pszValue = CSLFetchNameValueDef(
    5753          57 :                     papszCreationOptions, "TYPE_LONLAT", "DOUBLE");
    5754          57 :                 if (EQUAL(pszValue, "FLOAT"))
    5755           0 :                     eLonLatType = NC_FLOAT;
    5756             :             }
    5757             : 
    5758             :             // Def vars and attributes.
    5759             :             {
    5760          59 :                 const char *pszVarName =
    5761          59 :                     bIsRotatedPole ? NCDF_DIMNAME_RLAT : CF_LATITUDE_VAR_NAME;
    5762          59 :                 int status = nc_def_var(cdfid, pszVarName, eLonLatType,
    5763             :                                         nLatDims, panLatDims, &nVarLatID);
    5764          59 :                 CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d,%d,-,-) got id %d",
    5765             :                          cdfid, pszVarName, eLonLatType, nLatDims, nVarLatID);
    5766          59 :                 NCDF_ERR(status);
    5767          59 :                 DefVarDeflate(nVarLatID, false);  // Don't set chunking.
    5768             :             }
    5769             : 
    5770             :             {
    5771          59 :                 const char *pszVarName =
    5772          59 :                     bIsRotatedPole ? NCDF_DIMNAME_RLON : CF_LONGITUDE_VAR_NAME;
    5773          59 :                 int status = nc_def_var(cdfid, pszVarName, eLonLatType,
    5774             :                                         nLonDims, panLonDims, &nVarLonID);
    5775          59 :                 CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d,%d,-,-) got id %d",
    5776             :                          cdfid, pszVarName, eLonLatType, nLatDims, nVarLonID);
    5777          59 :                 NCDF_ERR(status);
    5778          59 :                 DefVarDeflate(nVarLonID, false);  // Don't set chunking.
    5779             :             }
    5780             : 
    5781          59 :             if (bIsRotatedPole)
    5782           0 :                 NCDFWriteRLonRLatVarsAttributes(this->vcdf, nVarLonID,
    5783             :                                                 nVarLatID);
    5784             :             else
    5785          59 :                 NCDFWriteLonLatVarsAttributes(this->vcdf, nVarLonID, nVarLatID);
    5786             : 
    5787          59 :             CPLFree(panLatDims);
    5788          59 :             CPLFree(panLonDims);
    5789             :         }
    5790             :     }
    5791             : 
    5792         164 :     if (!bDefsOnly)
    5793             :     {
    5794          82 :         m_bAddedProjectionVarsData = true;
    5795             : 
    5796          82 :         int nVarXID = -1;
    5797          82 :         int nVarYID = -1;
    5798             : 
    5799          82 :         nc_inq_varid(cdfid, CF_PROJ_X_VAR_NAME, &nVarXID);
    5800          82 :         nc_inq_varid(cdfid, CF_PROJ_Y_VAR_NAME, &nVarYID);
    5801             : 
    5802          82 :         int nVarLonID = -1;
    5803          82 :         int nVarLatID = -1;
    5804             : 
    5805          82 :         const bool bIsRotatedPole =
    5806         151 :             pszCFProjection != nullptr &&
    5807          69 :             EQUAL(pszCFProjection, ROTATED_POLE_VAR_NAME);
    5808          82 :         nc_inq_varid(cdfid,
    5809             :                      bIsRotatedPole ? NCDF_DIMNAME_RLON : CF_LONGITUDE_VAR_NAME,
    5810             :                      &nVarLonID);
    5811          82 :         nc_inq_varid(cdfid,
    5812             :                      bIsRotatedPole ? NCDF_DIMNAME_RLAT : CF_LATITUDE_VAR_NAME,
    5813             :                      &nVarLatID);
    5814             : 
    5815             :         // Get projection values.
    5816             : 
    5817          82 :         double *padLonVal = nullptr;
    5818          82 :         double *padLatVal = nullptr;
    5819             : 
    5820          82 :         if (bIsProjected)
    5821             :         {
    5822          25 :             OGRSpatialReference *poLatLonSRS = nullptr;
    5823          25 :             OGRCoordinateTransformation *poTransform = nullptr;
    5824             : 
    5825             :             size_t startX[1];
    5826             :             size_t countX[1];
    5827             :             size_t startY[1];
    5828             :             size_t countY[1];
    5829             : 
    5830          25 :             CPLDebug("GDAL_netCDF", "Getting (X,Y) values");
    5831             : 
    5832             :             double *padXVal =
    5833          25 :                 static_cast<double *>(CPLMalloc(nRasterXSize * sizeof(double)));
    5834             :             double *padYVal =
    5835          25 :                 static_cast<double *>(CPLMalloc(nRasterYSize * sizeof(double)));
    5836             : 
    5837             :             // Get Y values.
    5838          25 :             const double dfY0 = (!bBottomUp) ? m_gt[3] :
    5839             :                                              // Invert latitude values.
    5840          25 :                                     m_gt[3] + (m_gt[5] * nRasterYSize);
    5841          25 :             const double dfDY = m_gt[5];
    5842             : 
    5843        1456 :             for (int j = 0; j < nRasterYSize; j++)
    5844             :             {
    5845             :                 // The data point is centered inside the pixel.
    5846        1431 :                 if (!bBottomUp)
    5847           0 :                     padYVal[j] = dfY0 + (j + 0.5) * dfDY;
    5848             :                 else  // Invert latitude values.
    5849        1431 :                     padYVal[j] = dfY0 - (j + 0.5) * dfDY;
    5850             :             }
    5851          25 :             startX[0] = 0;
    5852          25 :             countX[0] = nRasterXSize;
    5853             : 
    5854             :             // Get X values.
    5855          25 :             const double dfX0 = m_gt[0];
    5856          25 :             const double dfDX = m_gt[1];
    5857             : 
    5858        1477 :             for (int i = 0; i < nRasterXSize; i++)
    5859             :             {
    5860             :                 // The data point is centered inside the pixel.
    5861        1452 :                 padXVal[i] = dfX0 + (i + 0.5) * dfDX;
    5862             :             }
    5863          25 :             startY[0] = 0;
    5864          25 :             countY[0] = nRasterYSize;
    5865             : 
    5866             :             // Write X/Y values.
    5867             : 
    5868             :             // Make sure we are in data mode.
    5869          25 :             SetDefineMode(false);
    5870             : 
    5871          25 :             CPLDebug("GDAL_netCDF", "Writing X values");
    5872             :             int status =
    5873          25 :                 nc_put_vara_double(cdfid, nVarXID, startX, countX, padXVal);
    5874          25 :             NCDF_ERR(status);
    5875             : 
    5876          25 :             CPLDebug("GDAL_netCDF", "Writing Y values");
    5877             :             status =
    5878          25 :                 nc_put_vara_double(cdfid, nVarYID, startY, countY, padYVal);
    5879          25 :             NCDF_ERR(status);
    5880             : 
    5881          25 :             if (pfnProgress)
    5882          21 :                 pfnProgress(0.20, nullptr, pProgressData);
    5883             : 
    5884             :             // Write lon/lat arrays (CF coordinates) if requested.
    5885             : 
    5886             :             // Get OGR transform if GEOLOCATION is not available.
    5887          25 :             if (bWriteLonLat && !bHasGeoloc)
    5888             :             {
    5889           1 :                 poLatLonSRS = m_oSRS.CloneGeogCS();
    5890           1 :                 if (poLatLonSRS != nullptr)
    5891             :                 {
    5892           1 :                     poLatLonSRS->SetAxisMappingStrategy(
    5893             :                         OAMS_TRADITIONAL_GIS_ORDER);
    5894             :                     poTransform =
    5895           1 :                         OGRCreateCoordinateTransformation(&m_oSRS, poLatLonSRS);
    5896             :                 }
    5897             :                 // If no OGR transform, then don't write CF lon/lat.
    5898           1 :                 if (poTransform == nullptr)
    5899             :                 {
    5900           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    5901             :                              "Unable to get Coordinate Transform");
    5902           0 :                     bWriteLonLat = false;
    5903             :                 }
    5904             :             }
    5905             : 
    5906          25 :             if (bWriteLonLat)
    5907             :             {
    5908           2 :                 if (!bHasGeoloc)
    5909           1 :                     CPLDebug("GDAL_netCDF", "Transforming (X,Y)->(lon,lat)");
    5910             :                 else
    5911           1 :                     CPLDebug("GDAL_netCDF",
    5912             :                              "Writing (lon,lat) from GEOLOCATION arrays");
    5913             : 
    5914           2 :                 bool bOK = true;
    5915           2 :                 double dfProgress = 0.2;
    5916             : 
    5917           2 :                 size_t start[] = {0, 0};
    5918           2 :                 size_t count[] = {1, (size_t)nRasterXSize};
    5919             :                 padLatVal = static_cast<double *>(
    5920           2 :                     CPLMalloc(nRasterXSize * sizeof(double)));
    5921             :                 padLonVal = static_cast<double *>(
    5922           2 :                     CPLMalloc(nRasterXSize * sizeof(double)));
    5923             : 
    5924          61 :                 for (int j = 0; j < nRasterYSize && bOK && status == NC_NOERR;
    5925             :                      j++)
    5926             :                 {
    5927          59 :                     start[0] = j;
    5928             : 
    5929             :                     // Get values from geotransform.
    5930          59 :                     if (!bHasGeoloc)
    5931             :                     {
    5932             :                         // Fill values to transform.
    5933         420 :                         for (int i = 0; i < nRasterXSize; i++)
    5934             :                         {
    5935         400 :                             padLatVal[i] = padYVal[j];
    5936         400 :                             padLonVal[i] = padXVal[i];
    5937             :                         }
    5938             : 
    5939             :                         // Do the transform.
    5940          20 :                         bOK = CPL_TO_BOOL(poTransform->Transform(
    5941          20 :                             nRasterXSize, padLonVal, padLatVal, nullptr));
    5942          20 :                         if (!bOK)
    5943             :                         {
    5944           0 :                             CPLError(CE_Failure, CPLE_AppDefined,
    5945             :                                      "Unable to Transform (X,Y) to (lon,lat).");
    5946             :                         }
    5947             :                     }
    5948             :                     // Get values from geoloc arrays.
    5949             :                     else
    5950             :                     {
    5951          39 :                         CPLErr eErr = GDALRasterIO(
    5952             :                             hBand_Y, GF_Read, 0, j, nRasterXSize, 1, padLatVal,
    5953             :                             nRasterXSize, 1, GDT_Float64, 0, 0);
    5954          39 :                         if (eErr == CE_None)
    5955             :                         {
    5956          39 :                             eErr = GDALRasterIO(
    5957             :                                 hBand_X, GF_Read, 0, j, nRasterXSize, 1,
    5958             :                                 padLonVal, nRasterXSize, 1, GDT_Float64, 0, 0);
    5959             :                         }
    5960             : 
    5961          39 :                         if (eErr == CE_None)
    5962             :                         {
    5963          39 :                             bOK = true;
    5964             :                         }
    5965             :                         else
    5966             :                         {
    5967           0 :                             bOK = false;
    5968           0 :                             CPLError(CE_Failure, CPLE_AppDefined,
    5969             :                                      "Unable to get scanline %d", j);
    5970             :                         }
    5971             :                     }
    5972             : 
    5973             :                     // Write data.
    5974          59 :                     if (bOK)
    5975             :                     {
    5976          59 :                         status = nc_put_vara_double(cdfid, nVarLatID, start,
    5977             :                                                     count, padLatVal);
    5978          59 :                         NCDF_ERR(status);
    5979          59 :                         status = nc_put_vara_double(cdfid, nVarLonID, start,
    5980             :                                                     count, padLonVal);
    5981          59 :                         NCDF_ERR(status);
    5982             :                     }
    5983             : 
    5984          59 :                     if (pfnProgress && (nRasterYSize / 10) > 0 &&
    5985          59 :                         (j % (nRasterYSize / 10) == 0))
    5986             :                     {
    5987          23 :                         dfProgress += 0.08;
    5988          23 :                         pfnProgress(dfProgress, nullptr, pProgressData);
    5989             :                     }
    5990             :                 }
    5991             :             }
    5992             : 
    5993          25 :             if (poLatLonSRS != nullptr)
    5994           1 :                 delete poLatLonSRS;
    5995          25 :             if (poTransform != nullptr)
    5996           1 :                 delete poTransform;
    5997             : 
    5998          25 :             CPLFree(padXVal);
    5999          25 :             CPLFree(padYVal);
    6000             :         }  // Projected
    6001             : 
    6002             :         // If not projected/geographic and has geoloc
    6003          57 :         else if (!bIsGeographic && bHasGeoloc)
    6004             :         {
    6005             :             // Use
    6006             :             // https://cfconventions.org/Data/cf-conventions/cf-conventions-1.9/cf-conventions.html#_two_dimensional_latitude_longitude_coordinate_variables
    6007             : 
    6008           4 :             bool bOK = true;
    6009           4 :             double dfProgress = 0.2;
    6010             : 
    6011             :             // Make sure we are in data mode.
    6012           4 :             SetDefineMode(false);
    6013             : 
    6014             :             size_t startX[1];
    6015             :             size_t countX[1];
    6016             :             size_t startY[1];
    6017             :             size_t countY[1];
    6018           4 :             startX[0] = 0;
    6019           4 :             countX[0] = nRasterXSize;
    6020             : 
    6021           4 :             startY[0] = 0;
    6022           4 :             countY[0] = nRasterYSize;
    6023             : 
    6024           8 :             std::vector<double> adfXVal(nRasterXSize);
    6025          16 :             for (int i = 0; i < nRasterXSize; i++)
    6026          12 :                 adfXVal[i] = i;
    6027             : 
    6028           8 :             std::vector<double> adfYVal(nRasterYSize);
    6029          12 :             for (int i = 0; i < nRasterYSize; i++)
    6030           8 :                 adfYVal[i] = bBottomUp ? nRasterYSize - 1 - i : i;
    6031             : 
    6032           4 :             CPLDebug("GDAL_netCDF", "Writing X values");
    6033           4 :             int status = nc_put_vara_double(cdfid, nVarXID, startX, countX,
    6034           4 :                                             adfXVal.data());
    6035           4 :             NCDF_ERR(status);
    6036             : 
    6037           4 :             CPLDebug("GDAL_netCDF", "Writing Y values");
    6038           4 :             status = nc_put_vara_double(cdfid, nVarYID, startY, countY,
    6039           4 :                                         adfYVal.data());
    6040           4 :             NCDF_ERR(status);
    6041             : 
    6042           4 :             if (pfnProgress)
    6043           0 :                 pfnProgress(0.20, nullptr, pProgressData);
    6044             : 
    6045           4 :             size_t start[] = {0, 0};
    6046           4 :             size_t count[] = {1, (size_t)nRasterXSize};
    6047             :             padLatVal =
    6048           4 :                 static_cast<double *>(CPLMalloc(nRasterXSize * sizeof(double)));
    6049             :             padLonVal =
    6050           4 :                 static_cast<double *>(CPLMalloc(nRasterXSize * sizeof(double)));
    6051             : 
    6052          12 :             for (int j = 0; j < nRasterYSize && bOK && status == NC_NOERR; j++)
    6053             :             {
    6054           8 :                 start[0] = j;
    6055             : 
    6056           8 :                 CPLErr eErr = GDALRasterIO(hBand_Y, GF_Read, 0,
    6057           8 :                                            bBottomUp ? nRasterYSize - 1 - j : j,
    6058             :                                            nRasterXSize, 1, padLatVal,
    6059             :                                            nRasterXSize, 1, GDT_Float64, 0, 0);
    6060           8 :                 if (eErr == CE_None)
    6061             :                 {
    6062           8 :                     eErr = GDALRasterIO(hBand_X, GF_Read, 0,
    6063           8 :                                         bBottomUp ? nRasterYSize - 1 - j : j,
    6064             :                                         nRasterXSize, 1, padLonVal,
    6065             :                                         nRasterXSize, 1, GDT_Float64, 0, 0);
    6066             :                 }
    6067             : 
    6068           8 :                 if (eErr == CE_None)
    6069             :                 {
    6070           8 :                     bOK = true;
    6071             :                 }
    6072             :                 else
    6073             :                 {
    6074           0 :                     bOK = false;
    6075           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    6076             :                              "Unable to get scanline %d", j);
    6077             :                 }
    6078             : 
    6079             :                 // Write data.
    6080           8 :                 if (bOK)
    6081             :                 {
    6082           8 :                     status = nc_put_vara_double(cdfid, nVarLatID, start, count,
    6083             :                                                 padLatVal);
    6084           8 :                     NCDF_ERR(status);
    6085           8 :                     status = nc_put_vara_double(cdfid, nVarLonID, start, count,
    6086             :                                                 padLonVal);
    6087           8 :                     NCDF_ERR(status);
    6088             :                 }
    6089             : 
    6090           8 :                 if (pfnProgress && (nRasterYSize / 10) > 0 &&
    6091           0 :                     (j % (nRasterYSize / 10) == 0))
    6092             :                 {
    6093           0 :                     dfProgress += 0.08;
    6094           0 :                     pfnProgress(dfProgress, nullptr, pProgressData);
    6095             :                 }
    6096           4 :             }
    6097             :         }
    6098             : 
    6099             :         // If not projected, assume geographic to catch grids without Datum.
    6100          53 :         else if (bWriteLonLat)
    6101             :         {
    6102             :             // Get latitude values.
    6103          53 :             const double dfY0 = (!bBottomUp) ? m_gt[3] :
    6104             :                                              // Invert latitude values.
    6105          53 :                                     m_gt[3] + (m_gt[5] * nRasterYSize);
    6106          53 :             const double dfDY = m_gt[5];
    6107             : 
    6108             :             // Override lat values with the ones in GEOLOCATION/Y_VALUES.
    6109          53 :             if (netCDFDataset::GetMetadataItem("Y_VALUES", "GEOLOCATION") !=
    6110             :                 nullptr)
    6111             :             {
    6112           0 :                 int nTemp = 0;
    6113           0 :                 padLatVal = Get1DGeolocation("Y_VALUES", nTemp);
    6114             :                 // Make sure we got the correct amount, if not fallback to GT */
    6115             :                 // could add test fabs(fabs(padLatVal[0]) - fabs(dfY0)) <= 0.1))
    6116           0 :                 if (nTemp == nRasterYSize)
    6117             :                 {
    6118           0 :                     CPLDebug(
    6119             :                         "GDAL_netCDF",
    6120             :                         "Using Y_VALUES geolocation metadata for lat values");
    6121             :                 }
    6122             :                 else
    6123             :                 {
    6124           0 :                     CPLDebug("GDAL_netCDF",
    6125             :                              "Got %d elements from Y_VALUES geolocation "
    6126             :                              "metadata, need %d",
    6127             :                              nTemp, nRasterYSize);
    6128           0 :                     if (padLatVal)
    6129             :                     {
    6130           0 :                         CPLFree(padLatVal);
    6131           0 :                         padLatVal = nullptr;
    6132             :                     }
    6133             :                 }
    6134             :             }
    6135             : 
    6136          53 :             if (padLatVal == nullptr)
    6137             :             {
    6138             :                 padLatVal = static_cast<double *>(
    6139          53 :                     CPLMalloc(nRasterYSize * sizeof(double)));
    6140        7105 :                 for (int i = 0; i < nRasterYSize; i++)
    6141             :                 {
    6142             :                     // The data point is centered inside the pixel.
    6143        7052 :                     if (!bBottomUp)
    6144           0 :                         padLatVal[i] = dfY0 + (i + 0.5) * dfDY;
    6145             :                     else  // Invert latitude values.
    6146        7052 :                         padLatVal[i] = dfY0 - (i + 0.5) * dfDY;
    6147             :                 }
    6148             :             }
    6149             : 
    6150          53 :             size_t startLat[1] = {0};
    6151          53 :             size_t countLat[1] = {static_cast<size_t>(nRasterYSize)};
    6152             : 
    6153             :             // Get longitude values.
    6154          53 :             const double dfX0 = m_gt[0];
    6155          53 :             const double dfDX = m_gt[1];
    6156             : 
    6157             :             padLonVal =
    6158          53 :                 static_cast<double *>(CPLMalloc(nRasterXSize * sizeof(double)));
    6159        7157 :             for (int i = 0; i < nRasterXSize; i++)
    6160             :             {
    6161             :                 // The data point is centered inside the pixel.
    6162        7104 :                 padLonVal[i] = dfX0 + (i + 0.5) * dfDX;
    6163             :             }
    6164             : 
    6165          53 :             size_t startLon[1] = {0};
    6166          53 :             size_t countLon[1] = {static_cast<size_t>(nRasterXSize)};
    6167             : 
    6168             :             // Write latitude and longitude values.
    6169             : 
    6170             :             // Make sure we are in data mode.
    6171          53 :             SetDefineMode(false);
    6172             : 
    6173             :             // Write values.
    6174          53 :             CPLDebug("GDAL_netCDF", "Writing lat values");
    6175             : 
    6176          53 :             int status = nc_put_vara_double(cdfid, nVarLatID, startLat,
    6177             :                                             countLat, padLatVal);
    6178          53 :             NCDF_ERR(status);
    6179             : 
    6180          53 :             CPLDebug("GDAL_netCDF", "Writing lon values");
    6181          53 :             status = nc_put_vara_double(cdfid, nVarLonID, startLon, countLon,
    6182             :                                         padLonVal);
    6183          53 :             NCDF_ERR(status);
    6184             : 
    6185             :         }  // Not projected.
    6186             : 
    6187          82 :         CPLFree(padLatVal);
    6188          82 :         CPLFree(padLonVal);
    6189             : 
    6190          82 :         if (pfnProgress)
    6191          41 :             pfnProgress(1.00, nullptr, pProgressData);
    6192             :     }
    6193             : 
    6194         164 :     if (hDS_X != nullptr)
    6195             :     {
    6196          10 :         GDALClose(hDS_X);
    6197             :     }
    6198         164 :     if (hDS_Y != nullptr)
    6199             :     {
    6200          10 :         GDALClose(hDS_Y);
    6201             :     }
    6202             : 
    6203         164 :     return CE_None;
    6204             : }
    6205             : 
    6206             : // Write Projection variable to band variable.
    6207             : // Moved from AddProjectionVars() for cases when bands are added after
    6208             : // projection.
    6209         375 : bool netCDFDataset::AddGridMappingRef()
    6210             : {
    6211         375 :     bool bRet = true;
    6212         375 :     bool bOldDefineMode = bDefineMode;
    6213             : 
    6214         566 :     if ((GetAccess() == GA_Update) && (nBands >= 1) && (GetRasterBand(1)) &&
    6215         191 :         ((pszCFCoordinates != nullptr && !EQUAL(pszCFCoordinates, "")) ||
    6216         185 :          (pszCFProjection != nullptr && !EQUAL(pszCFProjection, ""))))
    6217             :     {
    6218          73 :         bAddedGridMappingRef = true;
    6219             : 
    6220             :         // Make sure we are in define mode.
    6221          73 :         SetDefineMode(true);
    6222             : 
    6223         192 :         for (int i = 1; i <= nBands; i++)
    6224             :         {
    6225             :             const int nVarId =
    6226         119 :                 static_cast<netCDFRasterBand *>(GetRasterBand(i))->nZId;
    6227             : 
    6228         119 :             if (pszCFProjection != nullptr && !EQUAL(pszCFProjection, ""))
    6229             :             {
    6230             :                 int status =
    6231         230 :                     nc_put_att_text(cdfid, nVarId, CF_GRD_MAPPING,
    6232         115 :                                     strlen(pszCFProjection), pszCFProjection);
    6233         115 :                 NCDF_ERR(status);
    6234         115 :                 if (status != NC_NOERR)
    6235           0 :                     bRet = false;
    6236             :             }
    6237         119 :             if (pszCFCoordinates != nullptr && !EQUAL(pszCFCoordinates, ""))
    6238             :             {
    6239             :                 int status =
    6240           6 :                     nc_put_att_text(cdfid, nVarId, CF_COORDINATES,
    6241             :                                     strlen(pszCFCoordinates), pszCFCoordinates);
    6242           6 :                 NCDF_ERR(status);
    6243           6 :                 if (status != NC_NOERR)
    6244           0 :                     bRet = false;
    6245             :             }
    6246             :         }
    6247             : 
    6248             :         // Go back to previous define mode.
    6249          73 :         SetDefineMode(bOldDefineMode);
    6250             :     }
    6251         375 :     return bRet;
    6252             : }
    6253             : 
    6254             : /************************************************************************/
    6255             : /*                          GetGeoTransform()                           */
    6256             : /************************************************************************/
    6257             : 
    6258         117 : CPLErr netCDFDataset::GetGeoTransform(GDALGeoTransform &gt) const
    6259             : 
    6260             : {
    6261         117 :     gt = m_gt;
    6262         117 :     if (m_bHasGeoTransform)
    6263          86 :         return CE_None;
    6264             : 
    6265          31 :     return GDALPamDataset::GetGeoTransform(gt);
    6266             : }
    6267             : 
    6268             : /************************************************************************/
    6269             : /*                                rint()                                */
    6270             : /************************************************************************/
    6271             : 
    6272           0 : double netCDFDataset::rint(double dfX)
    6273             : {
    6274           0 :     return std::round(dfX);
    6275             : }
    6276             : 
    6277             : /************************************************************************/
    6278             : /*                          NCDFReadIsoMetadata()                       */
    6279             : /************************************************************************/
    6280             : 
    6281          16 : static void NCDFReadMetadataAsJson(int cdfid, CPLJSONObject &obj)
    6282             : {
    6283          16 :     int nbAttr = 0;
    6284          16 :     NCDF_ERR(nc_inq_varnatts(cdfid, NC_GLOBAL, &nbAttr));
    6285             : 
    6286          32 :     std::map<std::string, CPLJSONArray> oMapNameToArray;
    6287          40 :     for (int l = 0; l < nbAttr; l++)
    6288             :     {
    6289             :         char szAttrName[NC_MAX_NAME + 1];
    6290          24 :         szAttrName[0] = 0;
    6291          24 :         NCDF_ERR(nc_inq_attname(cdfid, NC_GLOBAL, l, szAttrName));
    6292             : 
    6293          24 :         char *pszMetaValue = nullptr;
    6294          24 :         if (NCDFGetAttr(cdfid, NC_GLOBAL, szAttrName, &pszMetaValue) == CE_None)
    6295             :         {
    6296          24 :             nc_type nAttrType = NC_NAT;
    6297          24 :             size_t nAttrLen = 0;
    6298             : 
    6299          24 :             NCDF_ERR(nc_inq_att(cdfid, NC_GLOBAL, szAttrName, &nAttrType,
    6300             :                                 &nAttrLen));
    6301             : 
    6302          24 :             std::string osAttrName(szAttrName);
    6303          24 :             const auto sharpPos = osAttrName.find('#');
    6304          24 :             if (sharpPos == std::string::npos)
    6305             :             {
    6306          16 :                 if (nAttrType == NC_DOUBLE || nAttrType == NC_FLOAT)
    6307           4 :                     obj.Add(osAttrName, CPLAtof(pszMetaValue));
    6308             :                 else
    6309          12 :                     obj.Add(osAttrName, pszMetaValue);
    6310             :             }
    6311             :             else
    6312             :             {
    6313           8 :                 osAttrName.resize(sharpPos);
    6314           8 :                 auto iter = oMapNameToArray.find(osAttrName);
    6315           8 :                 if (iter == oMapNameToArray.end())
    6316             :                 {
    6317           8 :                     CPLJSONArray array;
    6318           4 :                     obj.Add(osAttrName, array);
    6319           4 :                     oMapNameToArray[osAttrName] = array;
    6320           4 :                     array.Add(pszMetaValue);
    6321             :                 }
    6322             :                 else
    6323             :                 {
    6324           4 :                     iter->second.Add(pszMetaValue);
    6325             :                 }
    6326             :             }
    6327          24 :             CPLFree(pszMetaValue);
    6328          24 :             pszMetaValue = nullptr;
    6329             :         }
    6330             :     }
    6331             : 
    6332          16 :     int nSubGroups = 0;
    6333          16 :     int *panSubGroupIds = nullptr;
    6334          16 :     NCDFGetSubGroups(cdfid, &nSubGroups, &panSubGroupIds);
    6335          16 :     oMapNameToArray.clear();
    6336          28 :     for (int i = 0; i < nSubGroups; i++)
    6337             :     {
    6338          24 :         CPLJSONObject subObj;
    6339          12 :         NCDFReadMetadataAsJson(panSubGroupIds[i], subObj);
    6340             : 
    6341          24 :         std::string osGroupName;
    6342          12 :         osGroupName.resize(NC_MAX_NAME);
    6343          12 :         NCDF_ERR(nc_inq_grpname(panSubGroupIds[i], &osGroupName[0]));
    6344          12 :         osGroupName.resize(strlen(osGroupName.data()));
    6345          12 :         const auto sharpPos = osGroupName.find('#');
    6346          12 :         if (sharpPos == std::string::npos)
    6347             :         {
    6348           4 :             obj.Add(osGroupName, subObj);
    6349             :         }
    6350             :         else
    6351             :         {
    6352           8 :             osGroupName.resize(sharpPos);
    6353           8 :             auto iter = oMapNameToArray.find(osGroupName);
    6354           8 :             if (iter == oMapNameToArray.end())
    6355             :             {
    6356           8 :                 CPLJSONArray array;
    6357           4 :                 obj.Add(osGroupName, array);
    6358           4 :                 oMapNameToArray[osGroupName] = array;
    6359           4 :                 array.Add(subObj);
    6360             :             }
    6361             :             else
    6362             :             {
    6363           4 :                 iter->second.Add(subObj);
    6364             :             }
    6365             :         }
    6366             :     }
    6367          16 :     CPLFree(panSubGroupIds);
    6368          16 : }
    6369             : 
    6370           4 : std::string NCDFReadMetadataAsJson(int cdfid)
    6371             : {
    6372           8 :     CPLJSONDocument oDoc;
    6373           8 :     CPLJSONObject oRoot = oDoc.GetRoot();
    6374           4 :     NCDFReadMetadataAsJson(cdfid, oRoot);
    6375           8 :     return oDoc.SaveAsString();
    6376             : }
    6377             : 
    6378             : /************************************************************************/
    6379             : /*                        ReadAttributes()                              */
    6380             : /************************************************************************/
    6381        1795 : CPLErr netCDFDataset::ReadAttributes(int cdfidIn, int var)
    6382             : 
    6383             : {
    6384        1795 :     char *pszVarFullName = nullptr;
    6385        1795 :     ERR_RET(NCDFGetVarFullName(cdfidIn, var, &pszVarFullName));
    6386             : 
    6387             :     // For metadata in Sentinel 5
    6388        1795 :     if (STARTS_WITH(pszVarFullName, "/METADATA/"))
    6389             :     {
    6390           6 :         for (const char *key :
    6391             :              {"ISO_METADATA", "ESA_METADATA", "EOP_METADATA", "QA_STATISTICS",
    6392           8 :               "GRANULE_DESCRIPTION", "ALGORITHM_SETTINGS"})
    6393             :         {
    6394          14 :             if (var == NC_GLOBAL &&
    6395           7 :                 strcmp(pszVarFullName,
    6396             :                        CPLSPrintf("/METADATA/%s/NC_GLOBAL", key)) == 0)
    6397             :             {
    6398           1 :                 CPLFree(pszVarFullName);
    6399           1 :                 CPLStringList aosList;
    6400           2 :                 aosList.AddString(CPLString(NCDFReadMetadataAsJson(cdfidIn))
    6401           1 :                                       .replaceAll("\\/", '/'));
    6402           1 :                 m_oMapDomainToJSon[key] = std::move(aosList);
    6403           1 :                 return CE_None;
    6404             :             }
    6405             :         }
    6406             :     }
    6407        1794 :     if (STARTS_WITH(pszVarFullName, "/PRODUCT/SUPPORT_DATA/"))
    6408             :     {
    6409           0 :         CPLFree(pszVarFullName);
    6410           0 :         CPLStringList aosList;
    6411             :         aosList.AddString(
    6412           0 :             CPLString(NCDFReadMetadataAsJson(cdfidIn)).replaceAll("\\/", '/'));
    6413           0 :         m_oMapDomainToJSon["SUPPORT_DATA"] = std::move(aosList);
    6414           0 :         return CE_None;
    6415             :     }
    6416             : 
    6417        1794 :     size_t nMetaNameSize =
    6418        1794 :         sizeof(char) * (strlen(pszVarFullName) + 1 + NC_MAX_NAME + 1);
    6419        1794 :     char *pszMetaName = static_cast<char *>(CPLMalloc(nMetaNameSize));
    6420             : 
    6421        1794 :     int nbAttr = 0;
    6422        1794 :     NCDF_ERR(nc_inq_varnatts(cdfidIn, var, &nbAttr));
    6423             : 
    6424        9029 :     for (int l = 0; l < nbAttr; l++)
    6425             :     {
    6426             :         char szAttrName[NC_MAX_NAME + 1];
    6427        7235 :         szAttrName[0] = 0;
    6428        7235 :         NCDF_ERR(nc_inq_attname(cdfidIn, var, l, szAttrName));
    6429        7235 :         snprintf(pszMetaName, nMetaNameSize, "%s#%s", pszVarFullName,
    6430             :                  szAttrName);
    6431             : 
    6432        7235 :         char *pszMetaTemp = nullptr;
    6433        7235 :         if (NCDFGetAttr(cdfidIn, var, szAttrName, &pszMetaTemp) == CE_None)
    6434             :         {
    6435        7234 :             papszMetadata =
    6436        7234 :                 CSLSetNameValue(papszMetadata, pszMetaName, pszMetaTemp);
    6437        7234 :             CPLFree(pszMetaTemp);
    6438        7234 :             pszMetaTemp = nullptr;
    6439             :         }
    6440             :         else
    6441             :         {
    6442           1 :             CPLDebug("GDAL_netCDF", "invalid metadata %s", pszMetaName);
    6443             :         }
    6444             :     }
    6445             : 
    6446        1794 :     CPLFree(pszVarFullName);
    6447        1794 :     CPLFree(pszMetaName);
    6448             : 
    6449        1794 :     if (var == NC_GLOBAL)
    6450             :     {
    6451             :         // Recurse on sub-groups.
    6452         522 :         int nSubGroups = 0;
    6453         522 :         int *panSubGroupIds = nullptr;
    6454         522 :         NCDFGetSubGroups(cdfidIn, &nSubGroups, &panSubGroupIds);
    6455         552 :         for (int i = 0; i < nSubGroups; i++)
    6456             :         {
    6457          30 :             ReadAttributes(panSubGroupIds[i], var);
    6458             :         }
    6459         522 :         CPLFree(panSubGroupIds);
    6460             :     }
    6461             : 
    6462        1794 :     return CE_None;
    6463             : }
    6464             : 
    6465             : /************************************************************************/
    6466             : /*                netCDFDataset::CreateSubDatasetList()                 */
    6467             : /************************************************************************/
    6468          51 : void netCDFDataset::CreateSubDatasetList(int nGroupId)
    6469             : {
    6470             :     char szVarStdName[NC_MAX_NAME + 1];
    6471          51 :     int *ponDimIds = nullptr;
    6472             :     nc_type nAttype;
    6473             :     size_t nAttlen;
    6474             : 
    6475          51 :     netCDFDataset *poDS = this;
    6476             : 
    6477             :     int nVarCount;
    6478          51 :     nc_inq_nvars(nGroupId, &nVarCount);
    6479             : 
    6480          51 :     const bool bListAllArrays = CPLTestBool(
    6481          51 :         CSLFetchNameValueDef(papszOpenOptions, "LIST_ALL_ARRAYS", "NO"));
    6482             : 
    6483         329 :     for (int nVar = 0; nVar < nVarCount; nVar++)
    6484             :     {
    6485             : 
    6486             :         int nDims;
    6487         278 :         nc_inq_varndims(nGroupId, nVar, &nDims);
    6488             : 
    6489         278 :         if ((bListAllArrays && nDims > 0) || nDims >= 2)
    6490             :         {
    6491         158 :             ponDimIds = static_cast<int *>(CPLCalloc(nDims, sizeof(int)));
    6492         158 :             nc_inq_vardimid(nGroupId, nVar, ponDimIds);
    6493             : 
    6494             :             // Create Sub dataset list.
    6495         158 :             CPLString osDim;
    6496         487 :             for (int i = 0; i < nDims; i++)
    6497             :             {
    6498             :                 size_t nDimLen;
    6499         329 :                 nc_inq_dimlen(nGroupId, ponDimIds[i], &nDimLen);
    6500         329 :                 if (!osDim.empty())
    6501         171 :                     osDim += 'x';
    6502         329 :                 osDim += CPLSPrintf("%d", (int)nDimLen);
    6503             :             }
    6504         158 :             CPLFree(ponDimIds);
    6505             : 
    6506             :             nc_type nVarType;
    6507         158 :             nc_inq_vartype(nGroupId, nVar, &nVarType);
    6508         158 :             const char *pszType = "";
    6509         158 :             switch (nVarType)
    6510             :             {
    6511          38 :                 case NC_BYTE:
    6512          38 :                     pszType = "8-bit integer";
    6513          38 :                     break;
    6514           2 :                 case NC_CHAR:
    6515           2 :                     pszType = "8-bit character";
    6516           2 :                     break;
    6517           6 :                 case NC_SHORT:
    6518           6 :                     pszType = "16-bit integer";
    6519           6 :                     break;
    6520          10 :                 case NC_INT:
    6521          10 :                     pszType = "32-bit integer";
    6522          10 :                     break;
    6523          51 :                 case NC_FLOAT:
    6524          51 :                     pszType = "32-bit floating-point";
    6525          51 :                     break;
    6526          33 :                 case NC_DOUBLE:
    6527          33 :                     pszType = "64-bit floating-point";
    6528          33 :                     break;
    6529           4 :                 case NC_UBYTE:
    6530           4 :                     pszType = "8-bit unsigned integer";
    6531           4 :                     break;
    6532           1 :                 case NC_USHORT:
    6533           1 :                     pszType = "16-bit unsigned integer";
    6534           1 :                     break;
    6535           1 :                 case NC_UINT:
    6536           1 :                     pszType = "32-bit unsigned integer";
    6537           1 :                     break;
    6538           1 :                 case NC_INT64:
    6539           1 :                     pszType = "64-bit integer";
    6540           1 :                     break;
    6541           1 :                 case NC_UINT64:
    6542           1 :                     pszType = "64-bit unsigned integer";
    6543           1 :                     break;
    6544          10 :                 default:
    6545          10 :                     break;
    6546             :             }
    6547             : 
    6548         158 :             char *pszName = nullptr;
    6549         158 :             if (NCDFGetVarFullName(nGroupId, nVar, &pszName) != CE_None)
    6550           0 :                 continue;
    6551             : 
    6552         158 :             nSubDatasets++;
    6553             : 
    6554         158 :             nAttlen = 0;
    6555         158 :             nc_inq_att(nGroupId, nVar, CF_STD_NAME, &nAttype, &nAttlen);
    6556         316 :             if (nAttlen < sizeof(szVarStdName) &&
    6557         158 :                 nc_get_att_text(nGroupId, nVar, CF_STD_NAME, szVarStdName) ==
    6558             :                     NC_NOERR)
    6559             :             {
    6560          53 :                 szVarStdName[nAttlen] = '\0';
    6561             :             }
    6562             :             else
    6563             :             {
    6564         105 :                 snprintf(szVarStdName, sizeof(szVarStdName), "%s", pszName);
    6565             :             }
    6566             : 
    6567             :             char szTemp[NC_MAX_NAME + 1];
    6568         158 :             snprintf(szTemp, sizeof(szTemp), "SUBDATASET_%d_NAME",
    6569             :                      nSubDatasets);
    6570             : 
    6571         158 :             if (strchr(pszName, ' ') || strchr(pszName, ':'))
    6572             :             {
    6573           1 :                 poDS->papszSubDatasets = CSLSetNameValue(
    6574             :                     poDS->papszSubDatasets, szTemp,
    6575             :                     CPLSPrintf("NETCDF:\"%s\":\"%s\"", poDS->osFilename.c_str(),
    6576             :                                pszName));
    6577             :             }
    6578             :             else
    6579             :             {
    6580         157 :                 poDS->papszSubDatasets = CSLSetNameValue(
    6581             :                     poDS->papszSubDatasets, szTemp,
    6582             :                     CPLSPrintf("NETCDF:\"%s\":%s", poDS->osFilename.c_str(),
    6583             :                                pszName));
    6584             :             }
    6585             : 
    6586         158 :             CPLFree(pszName);
    6587             : 
    6588         158 :             snprintf(szTemp, sizeof(szTemp), "SUBDATASET_%d_DESC",
    6589             :                      nSubDatasets);
    6590             : 
    6591         158 :             poDS->papszSubDatasets =
    6592         158 :                 CSLSetNameValue(poDS->papszSubDatasets, szTemp,
    6593             :                                 CPLSPrintf("[%s] %s (%s)", osDim.c_str(),
    6594             :                                            szVarStdName, pszType));
    6595             :         }
    6596             :     }
    6597             : 
    6598             :     // Recurse on sub groups.
    6599          51 :     int nSubGroups = 0;
    6600          51 :     int *panSubGroupIds = nullptr;
    6601          51 :     NCDFGetSubGroups(nGroupId, &nSubGroups, &panSubGroupIds);
    6602          56 :     for (int i = 0; i < nSubGroups; i++)
    6603             :     {
    6604           5 :         CreateSubDatasetList(panSubGroupIds[i]);
    6605             :     }
    6606          51 :     CPLFree(panSubGroupIds);
    6607          51 : }
    6608             : 
    6609             : /************************************************************************/
    6610             : /*                            TestCapability()                          */
    6611             : /************************************************************************/
    6612             : 
    6613         248 : int netCDFDataset::TestCapability(const char *pszCap)
    6614             : {
    6615         248 :     if (EQUAL(pszCap, ODsCCreateLayer))
    6616             :     {
    6617         223 :         return eAccess == GA_Update && nBands == 0 &&
    6618         218 :                (eMultipleLayerBehavior != SINGLE_LAYER ||
    6619         229 :                 this->GetLayerCount() == 0 || bSGSupport);
    6620             :     }
    6621         136 :     else if (EQUAL(pszCap, ODsCZGeometries))
    6622           2 :         return true;
    6623             : 
    6624         134 :     return false;
    6625             : }
    6626             : 
    6627             : /************************************************************************/
    6628             : /*                            GetLayer()                                */
    6629             : /************************************************************************/
    6630             : 
    6631         384 : OGRLayer *netCDFDataset::GetLayer(int nIdx)
    6632             : {
    6633         384 :     if (nIdx < 0 || nIdx >= this->GetLayerCount())
    6634           2 :         return nullptr;
    6635         382 :     return papoLayers[nIdx].get();
    6636             : }
    6637             : 
    6638             : /************************************************************************/
    6639             : /*                            ICreateLayer()                            */
    6640             : /************************************************************************/
    6641             : 
    6642          59 : OGRLayer *netCDFDataset::ICreateLayer(const char *pszName,
    6643             :                                       const OGRGeomFieldDefn *poGeomFieldDefn,
    6644             :                                       CSLConstList papszOptions)
    6645             : {
    6646          59 :     int nLayerCDFId = cdfid;
    6647          59 :     if (!TestCapability(ODsCCreateLayer))
    6648           0 :         return nullptr;
    6649             : 
    6650          59 :     const auto eGType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
    6651             :     const auto poSpatialRef =
    6652          59 :         poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
    6653             : 
    6654         118 :     CPLString osNetCDFLayerName(pszName);
    6655          59 :     const netCDFWriterConfigLayer *poLayerConfig = nullptr;
    6656          59 :     if (oWriterConfig.m_bIsValid)
    6657             :     {
    6658             :         std::map<CPLString, netCDFWriterConfigLayer>::const_iterator
    6659           2 :             oLayerIter = oWriterConfig.m_oLayers.find(pszName);
    6660           2 :         if (oLayerIter != oWriterConfig.m_oLayers.end())
    6661             :         {
    6662           1 :             poLayerConfig = &(oLayerIter->second);
    6663           1 :             osNetCDFLayerName = poLayerConfig->m_osNetCDFName;
    6664             :         }
    6665             :     }
    6666             : 
    6667          59 :     netCDFDataset *poLayerDataset = nullptr;
    6668          59 :     if (eMultipleLayerBehavior == SEPARATE_FILES)
    6669             :     {
    6670           2 :         char **papszDatasetOptions = nullptr;
    6671           2 :         papszDatasetOptions = CSLSetNameValue(
    6672             :             papszDatasetOptions, "CONFIG_FILE",
    6673           2 :             CSLFetchNameValue(papszCreationOptions, "CONFIG_FILE"));
    6674             :         papszDatasetOptions =
    6675           2 :             CSLSetNameValue(papszDatasetOptions, "FORMAT",
    6676           2 :                             CSLFetchNameValue(papszCreationOptions, "FORMAT"));
    6677           2 :         papszDatasetOptions = CSLSetNameValue(
    6678             :             papszDatasetOptions, "WRITE_GDAL_TAGS",
    6679           2 :             CSLFetchNameValue(papszCreationOptions, "WRITE_GDAL_TAGS"));
    6680             :         const CPLString osLayerFilename(
    6681           2 :             CPLFormFilenameSafe(osFilename, osNetCDFLayerName, "nc"));
    6682           2 :         CPLAcquireMutex(hNCMutex, 1000.0);
    6683           2 :         poLayerDataset =
    6684           2 :             CreateLL(osLayerFilename, 0, 0, 0, papszDatasetOptions);
    6685           2 :         CPLReleaseMutex(hNCMutex);
    6686           2 :         CSLDestroy(papszDatasetOptions);
    6687           2 :         if (poLayerDataset == nullptr)
    6688           0 :             return nullptr;
    6689             : 
    6690           2 :         nLayerCDFId = poLayerDataset->cdfid;
    6691           2 :         NCDFAddGDALHistory(nLayerCDFId, osLayerFilename, bWriteGDALVersion,
    6692           2 :                            bWriteGDALHistory, "", "Create",
    6693             :                            NCDF_CONVENTIONS_CF_V1_6);
    6694             :     }
    6695          57 :     else if (eMultipleLayerBehavior == SEPARATE_GROUPS)
    6696             :     {
    6697           2 :         SetDefineMode(true);
    6698             : 
    6699           2 :         nLayerCDFId = -1;
    6700           2 :         int status = nc_def_grp(cdfid, osNetCDFLayerName, &nLayerCDFId);
    6701           2 :         NCDF_ERR(status);
    6702           2 :         if (status != NC_NOERR)
    6703           0 :             return nullptr;
    6704             : 
    6705           2 :         NCDFAddGDALHistory(nLayerCDFId, osFilename, bWriteGDALVersion,
    6706           2 :                            bWriteGDALHistory, "", "Create",
    6707             :                            NCDF_CONVENTIONS_CF_V1_6);
    6708             :     }
    6709             : 
    6710             :     // Make a clone to workaround a bug in released MapServer versions
    6711             :     // that destroys the passed SRS instead of releasing it .
    6712          59 :     OGRSpatialReference *poSRS = nullptr;
    6713          59 :     if (poSpatialRef)
    6714             :     {
    6715          43 :         poSRS = poSpatialRef->Clone();
    6716          43 :         poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    6717             :     }
    6718             :     std::shared_ptr<netCDFLayer> poLayer(
    6719          59 :         new netCDFLayer(poLayerDataset ? poLayerDataset : this, nLayerCDFId,
    6720         118 :                         osNetCDFLayerName, eGType, poSRS));
    6721          59 :     if (poSRS != nullptr)
    6722          43 :         poSRS->Release();
    6723             : 
    6724             :     // Fetch layer creation options coming from config file
    6725          59 :     char **papszNewOptions = CSLDuplicate(papszOptions);
    6726          59 :     if (oWriterConfig.m_bIsValid)
    6727             :     {
    6728           2 :         std::map<CPLString, CPLString>::const_iterator oIter;
    6729           3 :         for (oIter = oWriterConfig.m_oLayerCreationOptions.begin();
    6730           3 :              oIter != oWriterConfig.m_oLayerCreationOptions.end(); ++oIter)
    6731             :         {
    6732             :             papszNewOptions =
    6733           1 :                 CSLSetNameValue(papszNewOptions, oIter->first, oIter->second);
    6734             :         }
    6735           2 :         if (poLayerConfig != nullptr)
    6736             :         {
    6737           3 :             for (oIter = poLayerConfig->m_oLayerCreationOptions.begin();
    6738           3 :                  oIter != poLayerConfig->m_oLayerCreationOptions.end(); ++oIter)
    6739             :             {
    6740           2 :                 papszNewOptions = CSLSetNameValue(papszNewOptions, oIter->first,
    6741           2 :                                                   oIter->second);
    6742             :             }
    6743             :         }
    6744             :     }
    6745             : 
    6746          59 :     const bool bRet = poLayer->Create(papszNewOptions, poLayerConfig);
    6747          59 :     CSLDestroy(papszNewOptions);
    6748             : 
    6749          59 :     if (!bRet)
    6750             :     {
    6751           0 :         return nullptr;
    6752             :     }
    6753             : 
    6754          59 :     if (poLayerDataset != nullptr)
    6755           2 :         apoVectorDatasets.push_back(poLayerDataset);
    6756             : 
    6757          59 :     papoLayers.push_back(poLayer);
    6758          59 :     return poLayer.get();
    6759             : }
    6760             : 
    6761             : /************************************************************************/
    6762             : /*                           CloneAttributes()                          */
    6763             : /************************************************************************/
    6764             : 
    6765         137 : bool netCDFDataset::CloneAttributes(int old_cdfid, int new_cdfid, int nSrcVarId,
    6766             :                                     int nDstVarId)
    6767             : {
    6768         137 :     int nAttCount = -1;
    6769         137 :     int status = nc_inq_varnatts(old_cdfid, nSrcVarId, &nAttCount);
    6770         137 :     NCDF_ERR(status);
    6771             : 
    6772         693 :     for (int i = 0; i < nAttCount; i++)
    6773             :     {
    6774             :         char szName[NC_MAX_NAME + 1];
    6775         556 :         szName[0] = 0;
    6776         556 :         status = nc_inq_attname(old_cdfid, nSrcVarId, i, szName);
    6777         556 :         NCDF_ERR(status);
    6778             : 
    6779             :         status =
    6780         556 :             nc_copy_att(old_cdfid, nSrcVarId, szName, new_cdfid, nDstVarId);
    6781         556 :         NCDF_ERR(status);
    6782         556 :         if (status != NC_NOERR)
    6783           0 :             return false;
    6784             :     }
    6785             : 
    6786         137 :     return true;
    6787             : }
    6788             : 
    6789             : /************************************************************************/
    6790             : /*                          CloneVariableContent()                      */
    6791             : /************************************************************************/
    6792             : 
    6793         121 : bool netCDFDataset::CloneVariableContent(int old_cdfid, int new_cdfid,
    6794             :                                          int nSrcVarId, int nDstVarId)
    6795             : {
    6796         121 :     int nVarDimCount = -1;
    6797         121 :     int status = nc_inq_varndims(old_cdfid, nSrcVarId, &nVarDimCount);
    6798         121 :     NCDF_ERR(status);
    6799         121 :     int anDimIds[] = {-1, 1};
    6800         121 :     status = nc_inq_vardimid(old_cdfid, nSrcVarId, anDimIds);
    6801         121 :     NCDF_ERR(status);
    6802         121 :     nc_type nc_datatype = NC_NAT;
    6803         121 :     status = nc_inq_vartype(old_cdfid, nSrcVarId, &nc_datatype);
    6804         121 :     NCDF_ERR(status);
    6805         121 :     size_t nTypeSize = 0;
    6806         121 :     switch (nc_datatype)
    6807             :     {
    6808          35 :         case NC_BYTE:
    6809             :         case NC_CHAR:
    6810          35 :             nTypeSize = 1;
    6811          35 :             break;
    6812           4 :         case NC_SHORT:
    6813           4 :             nTypeSize = 2;
    6814           4 :             break;
    6815          24 :         case NC_INT:
    6816          24 :             nTypeSize = 4;
    6817          24 :             break;
    6818           4 :         case NC_FLOAT:
    6819           4 :             nTypeSize = 4;
    6820           4 :             break;
    6821          43 :         case NC_DOUBLE:
    6822          43 :             nTypeSize = 8;
    6823          43 :             break;
    6824           2 :         case NC_UBYTE:
    6825           2 :             nTypeSize = 1;
    6826           2 :             break;
    6827           2 :         case NC_USHORT:
    6828           2 :             nTypeSize = 2;
    6829           2 :             break;
    6830           2 :         case NC_UINT:
    6831           2 :             nTypeSize = 4;
    6832           2 :             break;
    6833           4 :         case NC_INT64:
    6834             :         case NC_UINT64:
    6835           4 :             nTypeSize = 8;
    6836           4 :             break;
    6837           1 :         case NC_STRING:
    6838           1 :             nTypeSize = sizeof(char *);
    6839           1 :             break;
    6840           0 :         default:
    6841             :         {
    6842           0 :             CPLError(CE_Failure, CPLE_NotSupported, "Unsupported data type: %d",
    6843             :                      nc_datatype);
    6844           0 :             return false;
    6845             :         }
    6846             :     }
    6847             : 
    6848         121 :     size_t nElems = 1;
    6849             :     size_t anStart[NC_MAX_DIMS];
    6850             :     size_t anCount[NC_MAX_DIMS];
    6851         121 :     size_t nRecords = 1;
    6852         261 :     for (int i = 0; i < nVarDimCount; i++)
    6853             :     {
    6854         140 :         anStart[i] = 0;
    6855         140 :         if (i == 0)
    6856             :         {
    6857         116 :             anCount[i] = 1;
    6858         116 :             status = nc_inq_dimlen(old_cdfid, anDimIds[i], &nRecords);
    6859         116 :             NCDF_ERR(status);
    6860             :         }
    6861             :         else
    6862             :         {
    6863          24 :             anCount[i] = 0;
    6864          24 :             status = nc_inq_dimlen(old_cdfid, anDimIds[i], &anCount[i]);
    6865          24 :             NCDF_ERR(status);
    6866          24 :             nElems *= anCount[i];
    6867             :         }
    6868             :     }
    6869             : 
    6870             :     /* Workaround in some cases a netCDF bug:
    6871             :      * https://github.com/Unidata/netcdf-c/pull/1442 */
    6872         121 :     if (nRecords > 0 && nRecords < 10 * 1000 * 1000 / (nElems * nTypeSize))
    6873             :     {
    6874         119 :         nElems *= nRecords;
    6875         119 :         anCount[0] = nRecords;
    6876         119 :         nRecords = 1;
    6877             :     }
    6878             : 
    6879         121 :     void *pBuffer = VSI_MALLOC2_VERBOSE(nElems, nTypeSize);
    6880         121 :     if (pBuffer == nullptr)
    6881           0 :         return false;
    6882             : 
    6883         240 :     for (size_t iRecord = 0; iRecord < nRecords; iRecord++)
    6884             :     {
    6885         119 :         anStart[0] = iRecord;
    6886             : 
    6887         119 :         switch (nc_datatype)
    6888             :         {
    6889           5 :             case NC_BYTE:
    6890             :                 status =
    6891           5 :                     nc_get_vara_schar(old_cdfid, nSrcVarId, anStart, anCount,
    6892             :                                       static_cast<signed char *>(pBuffer));
    6893           5 :                 if (!status)
    6894           5 :                     status = nc_put_vara_schar(
    6895             :                         new_cdfid, nDstVarId, anStart, anCount,
    6896             :                         static_cast<signed char *>(pBuffer));
    6897           5 :                 break;
    6898          28 :             case NC_CHAR:
    6899             :                 status =
    6900          28 :                     nc_get_vara_text(old_cdfid, nSrcVarId, anStart, anCount,
    6901             :                                      static_cast<char *>(pBuffer));
    6902          28 :                 if (!status)
    6903             :                     status =
    6904          28 :                         nc_put_vara_text(new_cdfid, nDstVarId, anStart, anCount,
    6905             :                                          static_cast<char *>(pBuffer));
    6906          28 :                 break;
    6907           4 :             case NC_SHORT:
    6908             :                 status =
    6909           4 :                     nc_get_vara_short(old_cdfid, nSrcVarId, anStart, anCount,
    6910             :                                       static_cast<short *>(pBuffer));
    6911           4 :                 if (!status)
    6912           4 :                     status = nc_put_vara_short(new_cdfid, nDstVarId, anStart,
    6913             :                                                anCount,
    6914             :                                                static_cast<short *>(pBuffer));
    6915           4 :                 break;
    6916          24 :             case NC_INT:
    6917          24 :                 status = nc_get_vara_int(old_cdfid, nSrcVarId, anStart, anCount,
    6918             :                                          static_cast<int *>(pBuffer));
    6919          24 :                 if (!status)
    6920             :                     status =
    6921          24 :                         nc_put_vara_int(new_cdfid, nDstVarId, anStart, anCount,
    6922             :                                         static_cast<int *>(pBuffer));
    6923          24 :                 break;
    6924           4 :             case NC_FLOAT:
    6925             :                 status =
    6926           4 :                     nc_get_vara_float(old_cdfid, nSrcVarId, anStart, anCount,
    6927             :                                       static_cast<float *>(pBuffer));
    6928           4 :                 if (!status)
    6929           4 :                     status = nc_put_vara_float(new_cdfid, nDstVarId, anStart,
    6930             :                                                anCount,
    6931             :                                                static_cast<float *>(pBuffer));
    6932           4 :                 break;
    6933          43 :             case NC_DOUBLE:
    6934             :                 status =
    6935          43 :                     nc_get_vara_double(old_cdfid, nSrcVarId, anStart, anCount,
    6936             :                                        static_cast<double *>(pBuffer));
    6937          43 :                 if (!status)
    6938          43 :                     status = nc_put_vara_double(new_cdfid, nDstVarId, anStart,
    6939             :                                                 anCount,
    6940             :                                                 static_cast<double *>(pBuffer));
    6941          43 :                 break;
    6942           1 :             case NC_STRING:
    6943             :                 status =
    6944           1 :                     nc_get_vara_string(old_cdfid, nSrcVarId, anStart, anCount,
    6945             :                                        static_cast<char **>(pBuffer));
    6946           1 :                 if (!status)
    6947             :                 {
    6948           1 :                     status = nc_put_vara_string(
    6949             :                         new_cdfid, nDstVarId, anStart, anCount,
    6950             :                         static_cast<const char **>(pBuffer));
    6951           1 :                     nc_free_string(nElems, static_cast<char **>(pBuffer));
    6952             :                 }
    6953           1 :                 break;
    6954             : 
    6955           2 :             case NC_UBYTE:
    6956             :                 status =
    6957           2 :                     nc_get_vara_uchar(old_cdfid, nSrcVarId, anStart, anCount,
    6958             :                                       static_cast<unsigned char *>(pBuffer));
    6959           2 :                 if (!status)
    6960           2 :                     status = nc_put_vara_uchar(
    6961             :                         new_cdfid, nDstVarId, anStart, anCount,
    6962             :                         static_cast<unsigned char *>(pBuffer));
    6963           2 :                 break;
    6964           2 :             case NC_USHORT:
    6965             :                 status =
    6966           2 :                     nc_get_vara_ushort(old_cdfid, nSrcVarId, anStart, anCount,
    6967             :                                        static_cast<unsigned short *>(pBuffer));
    6968           2 :                 if (!status)
    6969           2 :                     status = nc_put_vara_ushort(
    6970             :                         new_cdfid, nDstVarId, anStart, anCount,
    6971             :                         static_cast<unsigned short *>(pBuffer));
    6972           2 :                 break;
    6973           2 :             case NC_UINT:
    6974             :                 status =
    6975           2 :                     nc_get_vara_uint(old_cdfid, nSrcVarId, anStart, anCount,
    6976             :                                      static_cast<unsigned int *>(pBuffer));
    6977           2 :                 if (!status)
    6978             :                     status =
    6979           2 :                         nc_put_vara_uint(new_cdfid, nDstVarId, anStart, anCount,
    6980             :                                          static_cast<unsigned int *>(pBuffer));
    6981           2 :                 break;
    6982           2 :             case NC_INT64:
    6983             :                 status =
    6984           2 :                     nc_get_vara_longlong(old_cdfid, nSrcVarId, anStart, anCount,
    6985             :                                          static_cast<long long *>(pBuffer));
    6986           2 :                 if (!status)
    6987           2 :                     status = nc_put_vara_longlong(
    6988             :                         new_cdfid, nDstVarId, anStart, anCount,
    6989             :                         static_cast<long long *>(pBuffer));
    6990           2 :                 break;
    6991           2 :             case NC_UINT64:
    6992           2 :                 status = nc_get_vara_ulonglong(
    6993             :                     old_cdfid, nSrcVarId, anStart, anCount,
    6994             :                     static_cast<unsigned long long *>(pBuffer));
    6995           2 :                 if (!status)
    6996           2 :                     status = nc_put_vara_ulonglong(
    6997             :                         new_cdfid, nDstVarId, anStart, anCount,
    6998             :                         static_cast<unsigned long long *>(pBuffer));
    6999           2 :                 break;
    7000           0 :             default:
    7001           0 :                 status = NC_EBADTYPE;
    7002             :         }
    7003             : 
    7004         119 :         NCDF_ERR(status);
    7005         119 :         if (status != NC_NOERR)
    7006             :         {
    7007           0 :             VSIFree(pBuffer);
    7008           0 :             return false;
    7009             :         }
    7010             :     }
    7011             : 
    7012         121 :     VSIFree(pBuffer);
    7013         121 :     return true;
    7014             : }
    7015             : 
    7016             : /************************************************************************/
    7017             : /*                         NCDFIsUnlimitedDim()                         */
    7018             : /************************************************************************/
    7019             : 
    7020          58 : bool NCDFIsUnlimitedDim(bool bIsNC4, int cdfid, int nDimId)
    7021             : {
    7022          58 :     if (bIsNC4)
    7023             :     {
    7024          16 :         int nUnlimitedDims = 0;
    7025          16 :         nc_inq_unlimdims(cdfid, &nUnlimitedDims, nullptr);
    7026          16 :         bool bFound = false;
    7027          16 :         if (nUnlimitedDims)
    7028             :         {
    7029             :             int *panUnlimitedDimIds =
    7030          16 :                 static_cast<int *>(CPLMalloc(sizeof(int) * nUnlimitedDims));
    7031          16 :             nc_inq_unlimdims(cdfid, nullptr, panUnlimitedDimIds);
    7032          30 :             for (int i = 0; i < nUnlimitedDims; i++)
    7033             :             {
    7034          22 :                 if (panUnlimitedDimIds[i] == nDimId)
    7035             :                 {
    7036           8 :                     bFound = true;
    7037           8 :                     break;
    7038             :                 }
    7039             :             }
    7040          16 :             CPLFree(panUnlimitedDimIds);
    7041             :         }
    7042          16 :         return bFound;
    7043             :     }
    7044             :     else
    7045             :     {
    7046          42 :         int nUnlimitedDimId = -1;
    7047          42 :         nc_inq(cdfid, nullptr, nullptr, nullptr, &nUnlimitedDimId);
    7048          42 :         return nDimId == nUnlimitedDimId;
    7049             :     }
    7050             : }
    7051             : 
    7052             : /************************************************************************/
    7053             : /*                              CloneGrp()                              */
    7054             : /************************************************************************/
    7055             : 
    7056          16 : bool netCDFDataset::CloneGrp(int nOldGrpId, int nNewGrpId, bool bIsNC4,
    7057             :                              int nLayerId, int nDimIdToGrow, size_t nNewSize)
    7058             : {
    7059             :     // Clone dimensions
    7060          16 :     int nDimCount = -1;
    7061          16 :     int status = nc_inq_ndims(nOldGrpId, &nDimCount);
    7062          16 :     NCDF_ERR(status);
    7063          16 :     if (nDimCount < 0 || nDimCount > NC_MAX_DIMS)
    7064           0 :         return false;
    7065             :     int anDimIds[NC_MAX_DIMS];
    7066          16 :     int nUnlimiDimID = -1;
    7067          16 :     status = nc_inq_unlimdim(nOldGrpId, &nUnlimiDimID);
    7068          16 :     NCDF_ERR(status);
    7069          16 :     if (bIsNC4)
    7070             :     {
    7071             :         // In NC4, the dimension ids of a group are not necessarily in
    7072             :         // [0,nDimCount-1] range
    7073           8 :         int nDimCount2 = -1;
    7074           8 :         status = nc_inq_dimids(nOldGrpId, &nDimCount2, anDimIds, FALSE);
    7075           8 :         NCDF_ERR(status);
    7076           8 :         CPLAssert(nDimCount == nDimCount2);
    7077             :     }
    7078             :     else
    7079             :     {
    7080          36 :         for (int i = 0; i < nDimCount; i++)
    7081          28 :             anDimIds[i] = i;
    7082             :     }
    7083          60 :     for (int i = 0; i < nDimCount; i++)
    7084             :     {
    7085             :         char szDimName[NC_MAX_NAME + 1];
    7086          44 :         szDimName[0] = 0;
    7087          44 :         size_t nLen = 0;
    7088          44 :         const int nDimId = anDimIds[i];
    7089          44 :         status = nc_inq_dim(nOldGrpId, nDimId, szDimName, &nLen);
    7090          44 :         NCDF_ERR(status);
    7091          44 :         if (NCDFIsUnlimitedDim(bIsNC4, nOldGrpId, nDimId))
    7092          16 :             nLen = NC_UNLIMITED;
    7093          28 :         else if (nDimId == nDimIdToGrow && nOldGrpId == nLayerId)
    7094          13 :             nLen = nNewSize;
    7095          44 :         int nNewDimId = -1;
    7096          44 :         status = nc_def_dim(nNewGrpId, szDimName, nLen, &nNewDimId);
    7097          44 :         NCDF_ERR(status);
    7098          44 :         CPLAssert(nDimId == nNewDimId);
    7099          44 :         if (status != NC_NOERR)
    7100             :         {
    7101           0 :             return false;
    7102             :         }
    7103             :     }
    7104             : 
    7105             :     // Clone main attributes
    7106          16 :     if (!CloneAttributes(nOldGrpId, nNewGrpId, NC_GLOBAL, NC_GLOBAL))
    7107             :     {
    7108           0 :         return false;
    7109             :     }
    7110             : 
    7111             :     // Clone variable definitions
    7112          16 :     int nVarCount = -1;
    7113          16 :     status = nc_inq_nvars(nOldGrpId, &nVarCount);
    7114          16 :     NCDF_ERR(status);
    7115             : 
    7116         137 :     for (int i = 0; i < nVarCount; i++)
    7117             :     {
    7118             :         char szVarName[NC_MAX_NAME + 1];
    7119         121 :         szVarName[0] = 0;
    7120         121 :         status = nc_inq_varname(nOldGrpId, i, szVarName);
    7121         121 :         NCDF_ERR(status);
    7122         121 :         nc_type nc_datatype = NC_NAT;
    7123         121 :         status = nc_inq_vartype(nOldGrpId, i, &nc_datatype);
    7124         121 :         NCDF_ERR(status);
    7125         121 :         int nVarDimCount = -1;
    7126         121 :         status = nc_inq_varndims(nOldGrpId, i, &nVarDimCount);
    7127         121 :         NCDF_ERR(status);
    7128         121 :         status = nc_inq_vardimid(nOldGrpId, i, anDimIds);
    7129         121 :         NCDF_ERR(status);
    7130         121 :         int nNewVarId = -1;
    7131         121 :         status = nc_def_var(nNewGrpId, szVarName, nc_datatype, nVarDimCount,
    7132             :                             anDimIds, &nNewVarId);
    7133         121 :         NCDF_ERR(status);
    7134         121 :         CPLAssert(i == nNewVarId);
    7135         121 :         if (status != NC_NOERR)
    7136             :         {
    7137           0 :             return false;
    7138             :         }
    7139             : 
    7140         121 :         if (!CloneAttributes(nOldGrpId, nNewGrpId, i, i))
    7141             :         {
    7142           0 :             return false;
    7143             :         }
    7144             :     }
    7145             : 
    7146          16 :     status = nc_enddef(nNewGrpId);
    7147          16 :     NCDF_ERR(status);
    7148          16 :     if (status != NC_NOERR)
    7149             :     {
    7150           0 :         return false;
    7151             :     }
    7152             : 
    7153             :     // Clone variable content
    7154         137 :     for (int i = 0; i < nVarCount; i++)
    7155             :     {
    7156         121 :         if (!CloneVariableContent(nOldGrpId, nNewGrpId, i, i))
    7157             :         {
    7158           0 :             return false;
    7159             :         }
    7160             :     }
    7161             : 
    7162          16 :     return true;
    7163             : }
    7164             : 
    7165             : /************************************************************************/
    7166             : /*                              GrowDim()                               */
    7167             : /************************************************************************/
    7168             : 
    7169          13 : bool netCDFDataset::GrowDim(int nLayerId, int nDimIdToGrow, size_t nNewSize)
    7170             : {
    7171             :     int nCreationMode;
    7172             :     // Set nCreationMode based on eFormat.
    7173          13 :     switch (eFormat)
    7174             :     {
    7175             : #ifdef NETCDF_HAS_NC2
    7176           0 :         case NCDF_FORMAT_NC2:
    7177           0 :             nCreationMode = NC_CLOBBER | NC_64BIT_OFFSET;
    7178           0 :             break;
    7179             : #endif
    7180           5 :         case NCDF_FORMAT_NC4:
    7181           5 :             nCreationMode = NC_CLOBBER | NC_NETCDF4;
    7182           5 :             break;
    7183           0 :         case NCDF_FORMAT_NC4C:
    7184           0 :             nCreationMode = NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL;
    7185           0 :             break;
    7186           8 :         case NCDF_FORMAT_NC:
    7187             :         default:
    7188           8 :             nCreationMode = NC_CLOBBER;
    7189           8 :             break;
    7190             :     }
    7191             : 
    7192          13 :     int new_cdfid = -1;
    7193          26 :     CPLString osTmpFilename(osFilename + ".tmp");
    7194          26 :     CPLString osFilenameForNCCreate(osTmpFilename);
    7195             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    7196             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    7197             :     {
    7198             :         char *pszTemp =
    7199             :             CPLRecode(osFilenameForNCCreate, CPL_ENC_UTF8, "CP_ACP");
    7200             :         osFilenameForNCCreate = pszTemp;
    7201             :         CPLFree(pszTemp);
    7202             :     }
    7203             : #endif
    7204          13 :     int status = nc_create(osFilenameForNCCreate, nCreationMode, &new_cdfid);
    7205          13 :     NCDF_ERR(status);
    7206          13 :     if (status != NC_NOERR)
    7207           0 :         return false;
    7208             : 
    7209          13 :     if (!CloneGrp(cdfid, new_cdfid, eFormat == NCDF_FORMAT_NC4, nLayerId,
    7210             :                   nDimIdToGrow, nNewSize))
    7211             :     {
    7212           0 :         GDAL_nc_close(new_cdfid);
    7213           0 :         return false;
    7214             :     }
    7215             : 
    7216          13 :     int nGroupCount = 0;
    7217          26 :     std::vector<CPLString> oListGrpName;
    7218          31 :     if (eFormat == NCDF_FORMAT_NC4 &&
    7219          18 :         nc_inq_grps(cdfid, &nGroupCount, nullptr) == NC_NOERR &&
    7220           5 :         nGroupCount > 0)
    7221             :     {
    7222             :         int *panGroupIds =
    7223           2 :             static_cast<int *>(CPLMalloc(sizeof(int) * nGroupCount));
    7224           2 :         status = nc_inq_grps(cdfid, nullptr, panGroupIds);
    7225           2 :         NCDF_ERR(status);
    7226           5 :         for (int i = 0; i < nGroupCount; i++)
    7227             :         {
    7228             :             char szGroupName[NC_MAX_NAME + 1];
    7229           3 :             szGroupName[0] = 0;
    7230           3 :             NCDF_ERR(nc_inq_grpname(panGroupIds[i], szGroupName));
    7231           3 :             int nNewGrpId = -1;
    7232           3 :             status = nc_def_grp(new_cdfid, szGroupName, &nNewGrpId);
    7233           3 :             NCDF_ERR(status);
    7234           3 :             if (status != NC_NOERR)
    7235             :             {
    7236           0 :                 CPLFree(panGroupIds);
    7237           0 :                 GDAL_nc_close(new_cdfid);
    7238           0 :                 return false;
    7239             :             }
    7240           3 :             if (!CloneGrp(panGroupIds[i], nNewGrpId, /*bIsNC4=*/true, nLayerId,
    7241             :                           nDimIdToGrow, nNewSize))
    7242             :             {
    7243           0 :                 CPLFree(panGroupIds);
    7244           0 :                 GDAL_nc_close(new_cdfid);
    7245           0 :                 return false;
    7246             :             }
    7247             :         }
    7248           2 :         CPLFree(panGroupIds);
    7249             : 
    7250           5 :         for (int i = 0; i < this->GetLayerCount(); i++)
    7251             :         {
    7252           3 :             auto poLayer = dynamic_cast<netCDFLayer *>(papoLayers[i].get());
    7253           3 :             if (poLayer)
    7254             :             {
    7255             :                 char szGroupName[NC_MAX_NAME + 1];
    7256           3 :                 szGroupName[0] = 0;
    7257           3 :                 status = nc_inq_grpname(poLayer->GetCDFID(), szGroupName);
    7258           3 :                 NCDF_ERR(status);
    7259           3 :                 oListGrpName.push_back(szGroupName);
    7260             :             }
    7261             :         }
    7262             :     }
    7263             : 
    7264          13 :     GDAL_nc_close(cdfid);
    7265          13 :     cdfid = -1;
    7266          13 :     GDAL_nc_close(new_cdfid);
    7267             : 
    7268          26 :     CPLString osOriFilename(osFilename + ".ori");
    7269          26 :     if (VSIRename(osFilename, osOriFilename) != 0 ||
    7270          13 :         VSIRename(osTmpFilename, osFilename) != 0)
    7271             :     {
    7272           0 :         CPLError(CE_Failure, CPLE_FileIO, "Renaming of files failed");
    7273           0 :         return false;
    7274             :     }
    7275          13 :     VSIUnlink(osOriFilename);
    7276             : 
    7277          26 :     CPLString osFilenameForNCOpen(osFilename);
    7278             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    7279             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    7280             :     {
    7281             :         char *pszTemp = CPLRecode(osFilenameForNCOpen, CPL_ENC_UTF8, "CP_ACP");
    7282             :         osFilenameForNCOpen = pszTemp;
    7283             :         CPLFree(pszTemp);
    7284             :     }
    7285             : #endif
    7286          13 :     status = GDAL_nc_open(osFilenameForNCOpen, NC_WRITE, &cdfid);
    7287          13 :     NCDF_ERR(status);
    7288          13 :     if (status != NC_NOERR)
    7289           0 :         return false;
    7290          13 :     bDefineMode = false;
    7291             : 
    7292          13 :     if (!oListGrpName.empty())
    7293             :     {
    7294           5 :         for (int i = 0; i < this->GetLayerCount(); i++)
    7295             :         {
    7296           3 :             auto poLayer = dynamic_cast<netCDFLayer *>(papoLayers[i].get());
    7297           3 :             if (poLayer)
    7298             :             {
    7299           3 :                 int nNewLayerCDFID = -1;
    7300           3 :                 status = nc_inq_ncid(cdfid, oListGrpName[i].c_str(),
    7301             :                                      &nNewLayerCDFID);
    7302           3 :                 NCDF_ERR(status);
    7303           3 :                 poLayer->SetCDFID(nNewLayerCDFID);
    7304             :             }
    7305             :         }
    7306             :     }
    7307             :     else
    7308             :     {
    7309          22 :         for (int i = 0; i < this->GetLayerCount(); i++)
    7310             :         {
    7311          11 :             auto poLayer = dynamic_cast<netCDFLayer *>(papoLayers[i].get());
    7312          11 :             if (poLayer)
    7313          11 :                 poLayer->SetCDFID(cdfid);
    7314             :         }
    7315             :     }
    7316             : 
    7317          13 :     return true;
    7318             : }
    7319             : 
    7320             : #ifdef ENABLE_NCDUMP
    7321             : 
    7322             : /************************************************************************/
    7323             : /*                      netCDFDatasetCreateTempFile()                   */
    7324             : /************************************************************************/
    7325             : 
    7326             : /* Create a netCDF file from a text dump (format of ncdump) */
    7327             : /* Mostly to easy fuzzing of the driver, while still generating valid */
    7328             : /* netCDF files. */
    7329             : /* Note: not all data types are supported ! */
    7330           4 : bool netCDFDatasetCreateTempFile(NetCDFFormatEnum eFormat,
    7331             :                                  const char *pszTmpFilename, VSILFILE *fpSrc)
    7332             : {
    7333           4 :     CPL_IGNORE_RET_VAL(eFormat);
    7334           4 :     int nCreateMode = NC_CLOBBER;
    7335           4 :     if (eFormat == NCDF_FORMAT_NC4)
    7336           1 :         nCreateMode |= NC_NETCDF4;
    7337           3 :     else if (eFormat == NCDF_FORMAT_NC4C)
    7338           0 :         nCreateMode |= NC_NETCDF4 | NC_CLASSIC_MODEL;
    7339           4 :     int nCdfId = -1;
    7340           4 :     int status = nc_create(pszTmpFilename, nCreateMode, &nCdfId);
    7341           4 :     if (status != NC_NOERR)
    7342             :     {
    7343           0 :         return false;
    7344             :     }
    7345           4 :     VSIFSeekL(fpSrc, 0, SEEK_SET);
    7346             :     const char *pszLine;
    7347           4 :     constexpr int SECTION_NONE = 0;
    7348           4 :     constexpr int SECTION_DIMENSIONS = 1;
    7349           4 :     constexpr int SECTION_VARIABLES = 2;
    7350           4 :     constexpr int SECTION_DATA = 3;
    7351           4 :     int nActiveSection = SECTION_NONE;
    7352           8 :     std::map<CPLString, int> oMapDimToId;
    7353           8 :     std::map<int, int> oMapDimIdToDimLen;
    7354           8 :     std::map<CPLString, int> oMapVarToId;
    7355           8 :     std::map<int, std::vector<int>> oMapVarIdToVectorOfDimId;
    7356           8 :     std::map<int, int> oMapVarIdToType;
    7357           4 :     std::set<CPLString> oSetAttrDefined;
    7358           4 :     oMapVarToId[""] = -1;
    7359           4 :     size_t nTotalVarSize = 0;
    7360         208 :     while ((pszLine = CPLReadLineL(fpSrc)) != nullptr)
    7361             :     {
    7362         204 :         if (STARTS_WITH(pszLine, "dimensions:") &&
    7363             :             nActiveSection == SECTION_NONE)
    7364             :         {
    7365           4 :             nActiveSection = SECTION_DIMENSIONS;
    7366             :         }
    7367         200 :         else if (STARTS_WITH(pszLine, "variables:") &&
    7368             :                  nActiveSection == SECTION_DIMENSIONS)
    7369             :         {
    7370           4 :             nActiveSection = SECTION_VARIABLES;
    7371             :         }
    7372         196 :         else if (STARTS_WITH(pszLine, "data:") &&
    7373             :                  nActiveSection == SECTION_VARIABLES)
    7374             :         {
    7375           4 :             nActiveSection = SECTION_DATA;
    7376           4 :             status = nc_enddef(nCdfId);
    7377           4 :             if (status != NC_NOERR)
    7378             :             {
    7379           0 :                 CPLDebug("netCDF", "nc_enddef() failed: %s",
    7380             :                          nc_strerror(status));
    7381             :             }
    7382             :         }
    7383         192 :         else if (nActiveSection == SECTION_DIMENSIONS)
    7384             :         {
    7385           9 :             char **papszTokens = CSLTokenizeString2(pszLine, " \t=;", 0);
    7386           9 :             if (CSLCount(papszTokens) == 2)
    7387             :             {
    7388           9 :                 const char *pszDimName = papszTokens[0];
    7389           9 :                 bool bValidName = true;
    7390           9 :                 if (STARTS_WITH(pszDimName, "_nc4_non_coord_"))
    7391             :                 {
    7392             :                     // This is an internal netcdf prefix. Using it may
    7393             :                     // cause memory leaks.
    7394           0 :                     bValidName = false;
    7395             :                 }
    7396           9 :                 if (!bValidName)
    7397             :                 {
    7398           0 :                     CPLDebug("netCDF",
    7399             :                              "nc_def_dim(%s) failed: invalid name found",
    7400             :                              pszDimName);
    7401           0 :                     CSLDestroy(papszTokens);
    7402           0 :                     continue;
    7403             :                 }
    7404             : 
    7405             :                 const bool bIsASCII =
    7406           9 :                     CPLIsASCII(pszDimName, static_cast<size_t>(-1));
    7407           9 :                 if (!bIsASCII)
    7408             :                 {
    7409             :                     // Workaround https://github.com/Unidata/netcdf-c/pull/450
    7410           0 :                     CPLDebug("netCDF",
    7411             :                              "nc_def_dim(%s) failed: rejected because "
    7412             :                              "of non-ASCII characters",
    7413             :                              pszDimName);
    7414           0 :                     CSLDestroy(papszTokens);
    7415           0 :                     continue;
    7416             :                 }
    7417           9 :                 int nDimSize = EQUAL(papszTokens[1], "UNLIMITED")
    7418             :                                    ? NC_UNLIMITED
    7419           9 :                                    : atoi(papszTokens[1]);
    7420           9 :                 if (nDimSize >= 1000)
    7421           1 :                     nDimSize = 1000;  // to avoid very long processing
    7422           9 :                 if (nDimSize >= 0)
    7423             :                 {
    7424           9 :                     int nDimId = -1;
    7425           9 :                     status = nc_def_dim(nCdfId, pszDimName, nDimSize, &nDimId);
    7426           9 :                     if (status != NC_NOERR)
    7427             :                     {
    7428           0 :                         CPLDebug("netCDF", "nc_def_dim(%s, %d) failed: %s",
    7429             :                                  pszDimName, nDimSize, nc_strerror(status));
    7430             :                     }
    7431             :                     else
    7432             :                     {
    7433             : #ifdef DEBUG_VERBOSE
    7434             :                         CPLDebug("netCDF", "nc_def_dim(%s, %d) (%s) succeeded",
    7435             :                                  pszDimName, nDimSize, pszLine);
    7436             : #endif
    7437           9 :                         oMapDimToId[pszDimName] = nDimId;
    7438           9 :                         oMapDimIdToDimLen[nDimId] = nDimSize;
    7439             :                     }
    7440             :                 }
    7441             :             }
    7442           9 :             CSLDestroy(papszTokens);
    7443             :         }
    7444         183 :         else if (nActiveSection == SECTION_VARIABLES)
    7445             :         {
    7446         390 :             while (*pszLine == ' ' || *pszLine == '\t')
    7447         249 :                 pszLine++;
    7448         141 :             const char *pszColumn = strchr(pszLine, ':');
    7449         141 :             const char *pszEqual = strchr(pszLine, '=');
    7450         141 :             if (pszColumn == nullptr)
    7451             :             {
    7452          21 :                 char **papszTokens = CSLTokenizeString2(pszLine, " \t=(),;", 0);
    7453          21 :                 if (CSLCount(papszTokens) >= 2)
    7454             :                 {
    7455          17 :                     const char *pszVarName = papszTokens[1];
    7456          17 :                     bool bValidName = true;
    7457          17 :                     if (STARTS_WITH(pszVarName, "_nc4_non_coord_"))
    7458             :                     {
    7459             :                         // This is an internal netcdf prefix. Using it may
    7460             :                         // cause memory leaks.
    7461           0 :                         bValidName = false;
    7462             :                     }
    7463         138 :                     for (int i = 0; pszVarName[i]; i++)
    7464             :                     {
    7465         121 :                         if (!((pszVarName[i] >= 'a' && pszVarName[i] <= 'z') ||
    7466          28 :                               (pszVarName[i] >= 'A' && pszVarName[i] <= 'Z') ||
    7467           9 :                               (pszVarName[i] >= '0' && pszVarName[i] <= '9') ||
    7468           6 :                               pszVarName[i] == '_'))
    7469             :                         {
    7470           0 :                             bValidName = false;
    7471             :                         }
    7472             :                     }
    7473          17 :                     if (!bValidName)
    7474             :                     {
    7475           0 :                         CPLDebug(
    7476             :                             "netCDF",
    7477             :                             "nc_def_var(%s) failed: illegal character found",
    7478             :                             pszVarName);
    7479           0 :                         CSLDestroy(papszTokens);
    7480           0 :                         continue;
    7481             :                     }
    7482          17 :                     if (oMapVarToId.find(pszVarName) != oMapVarToId.end())
    7483             :                     {
    7484           0 :                         CPLDebug("netCDF",
    7485             :                                  "nc_def_var(%s) failed: already defined",
    7486             :                                  pszVarName);
    7487           0 :                         CSLDestroy(papszTokens);
    7488           0 :                         continue;
    7489             :                     }
    7490          17 :                     const char *pszVarType = papszTokens[0];
    7491          17 :                     int nc_datatype = NC_BYTE;
    7492          17 :                     size_t nDataTypeSize = 1;
    7493          17 :                     if (EQUAL(pszVarType, "char"))
    7494             :                     {
    7495           6 :                         nc_datatype = NC_CHAR;
    7496           6 :                         nDataTypeSize = 1;
    7497             :                     }
    7498          11 :                     else if (EQUAL(pszVarType, "byte"))
    7499             :                     {
    7500           3 :                         nc_datatype = NC_BYTE;
    7501           3 :                         nDataTypeSize = 1;
    7502             :                     }
    7503           8 :                     else if (EQUAL(pszVarType, "short"))
    7504             :                     {
    7505           0 :                         nc_datatype = NC_SHORT;
    7506           0 :                         nDataTypeSize = 2;
    7507             :                     }
    7508           8 :                     else if (EQUAL(pszVarType, "int"))
    7509             :                     {
    7510           0 :                         nc_datatype = NC_INT;
    7511           0 :                         nDataTypeSize = 4;
    7512             :                     }
    7513           8 :                     else if (EQUAL(pszVarType, "float"))
    7514             :                     {
    7515           0 :                         nc_datatype = NC_FLOAT;
    7516           0 :                         nDataTypeSize = 4;
    7517             :                     }
    7518           8 :                     else if (EQUAL(pszVarType, "double"))
    7519             :                     {
    7520           8 :                         nc_datatype = NC_DOUBLE;
    7521           8 :                         nDataTypeSize = 8;
    7522             :                     }
    7523           0 :                     else if (EQUAL(pszVarType, "ubyte"))
    7524             :                     {
    7525           0 :                         nc_datatype = NC_UBYTE;
    7526           0 :                         nDataTypeSize = 1;
    7527             :                     }
    7528           0 :                     else if (EQUAL(pszVarType, "ushort"))
    7529             :                     {
    7530           0 :                         nc_datatype = NC_USHORT;
    7531           0 :                         nDataTypeSize = 2;
    7532             :                     }
    7533           0 :                     else if (EQUAL(pszVarType, "uint"))
    7534             :                     {
    7535           0 :                         nc_datatype = NC_UINT;
    7536           0 :                         nDataTypeSize = 4;
    7537             :                     }
    7538           0 :                     else if (EQUAL(pszVarType, "int64"))
    7539             :                     {
    7540           0 :                         nc_datatype = NC_INT64;
    7541           0 :                         nDataTypeSize = 8;
    7542             :                     }
    7543           0 :                     else if (EQUAL(pszVarType, "uint64"))
    7544             :                     {
    7545           0 :                         nc_datatype = NC_UINT64;
    7546           0 :                         nDataTypeSize = 8;
    7547             :                     }
    7548             : 
    7549          17 :                     int nDims = CSLCount(papszTokens) - 2;
    7550          17 :                     if (nDims >= 32)
    7551             :                     {
    7552             :                         // The number of dimensions in a netCDFv4 file is
    7553             :                         // limited by #define H5S_MAX_RANK    32
    7554             :                         // but libnetcdf doesn't check that...
    7555           0 :                         CPLDebug("netCDF",
    7556             :                                  "nc_def_var(%s) failed: too many dimensions",
    7557             :                                  pszVarName);
    7558           0 :                         CSLDestroy(papszTokens);
    7559           0 :                         continue;
    7560             :                     }
    7561          17 :                     std::vector<int> aoDimIds;
    7562          17 :                     bool bFailed = false;
    7563          17 :                     size_t nSize = 1;
    7564          35 :                     for (int i = 0; i < nDims; i++)
    7565             :                     {
    7566          18 :                         const char *pszDimName = papszTokens[2 + i];
    7567          18 :                         if (oMapDimToId.find(pszDimName) == oMapDimToId.end())
    7568             :                         {
    7569           0 :                             bFailed = true;
    7570           0 :                             break;
    7571             :                         }
    7572          18 :                         const int nDimId = oMapDimToId[pszDimName];
    7573          18 :                         aoDimIds.push_back(nDimId);
    7574             : 
    7575          18 :                         const size_t nDimSize = oMapDimIdToDimLen[nDimId];
    7576          18 :                         if (nDimSize != 0)
    7577             :                         {
    7578          18 :                             if (nSize >
    7579          18 :                                 std::numeric_limits<size_t>::max() / nDimSize)
    7580             :                             {
    7581           0 :                                 bFailed = true;
    7582           0 :                                 break;
    7583             :                             }
    7584             :                             else
    7585             :                             {
    7586          18 :                                 nSize *= nDimSize;
    7587             :                             }
    7588             :                         }
    7589             :                     }
    7590          17 :                     if (bFailed)
    7591             :                     {
    7592           0 :                         CPLDebug("netCDF",
    7593             :                                  "nc_def_var(%s) failed: unknown dimension(s)",
    7594             :                                  pszVarName);
    7595           0 :                         CSLDestroy(papszTokens);
    7596           0 :                         continue;
    7597             :                     }
    7598          17 :                     if (nSize > 100U * 1024 * 1024 / nDataTypeSize)
    7599             :                     {
    7600           0 :                         CPLDebug("netCDF",
    7601             :                                  "nc_def_var(%s) failed: too large data",
    7602             :                                  pszVarName);
    7603           0 :                         CSLDestroy(papszTokens);
    7604           0 :                         continue;
    7605             :                     }
    7606          17 :                     if (nTotalVarSize >
    7607          34 :                             std::numeric_limits<size_t>::max() - nSize ||
    7608          17 :                         nTotalVarSize + nSize > 100 * 1024 * 1024)
    7609             :                     {
    7610           0 :                         CPLDebug("netCDF",
    7611             :                                  "nc_def_var(%s) failed: too large data",
    7612             :                                  pszVarName);
    7613           0 :                         CSLDestroy(papszTokens);
    7614           0 :                         continue;
    7615             :                     }
    7616          17 :                     nTotalVarSize += nSize;
    7617             : 
    7618          17 :                     int nVarId = -1;
    7619             :                     status =
    7620          30 :                         nc_def_var(nCdfId, pszVarName, nc_datatype, nDims,
    7621          13 :                                    (nDims) ? &aoDimIds[0] : nullptr, &nVarId);
    7622          17 :                     if (status != NC_NOERR)
    7623             :                     {
    7624           0 :                         CPLDebug("netCDF", "nc_def_var(%s) failed: %s",
    7625             :                                  pszVarName, nc_strerror(status));
    7626             :                     }
    7627             :                     else
    7628             :                     {
    7629             : #ifdef DEBUG_VERBOSE
    7630             :                         CPLDebug("netCDF", "nc_def_var(%s) (%s) succeeded",
    7631             :                                  pszVarName, pszLine);
    7632             : #endif
    7633          17 :                         oMapVarToId[pszVarName] = nVarId;
    7634          17 :                         oMapVarIdToType[nVarId] = nc_datatype;
    7635          17 :                         oMapVarIdToVectorOfDimId[nVarId] = std::move(aoDimIds);
    7636             :                     }
    7637             :                 }
    7638          21 :                 CSLDestroy(papszTokens);
    7639             :             }
    7640         120 :             else if (pszEqual != nullptr && pszEqual - pszColumn > 0)
    7641             :             {
    7642         116 :                 CPLString osVarName(pszLine, pszColumn - pszLine);
    7643         116 :                 CPLString osAttrName(pszColumn + 1, pszEqual - pszColumn - 1);
    7644         116 :                 osAttrName.Trim();
    7645         116 :                 if (oMapVarToId.find(osVarName) == oMapVarToId.end())
    7646             :                 {
    7647           0 :                     CPLDebug("netCDF",
    7648             :                              "nc_put_att(%s:%s) failed: "
    7649             :                              "no corresponding variable",
    7650             :                              osVarName.c_str(), osAttrName.c_str());
    7651           0 :                     continue;
    7652             :                 }
    7653         116 :                 bool bValidName = true;
    7654        1743 :                 for (size_t i = 0; i < osAttrName.size(); i++)
    7655             :                 {
    7656        1865 :                     if (!((osAttrName[i] >= 'a' && osAttrName[i] <= 'z') ||
    7657         238 :                           (osAttrName[i] >= 'A' && osAttrName[i] <= 'Z') ||
    7658         158 :                           (osAttrName[i] >= '0' && osAttrName[i] <= '9') ||
    7659         158 :                           osAttrName[i] == '_'))
    7660             :                     {
    7661           0 :                         bValidName = false;
    7662             :                     }
    7663             :                 }
    7664         116 :                 if (!bValidName)
    7665             :                 {
    7666           0 :                     CPLDebug(
    7667             :                         "netCDF",
    7668             :                         "nc_put_att(%s:%s) failed: illegal character found",
    7669             :                         osVarName.c_str(), osAttrName.c_str());
    7670           0 :                     continue;
    7671             :                 }
    7672         116 :                 if (oSetAttrDefined.find(osVarName + ":" + osAttrName) !=
    7673         232 :                     oSetAttrDefined.end())
    7674             :                 {
    7675           0 :                     CPLDebug("netCDF",
    7676             :                              "nc_put_att(%s:%s) failed: already defined",
    7677             :                              osVarName.c_str(), osAttrName.c_str());
    7678           0 :                     continue;
    7679             :                 }
    7680             : 
    7681         116 :                 const int nVarId = oMapVarToId[osVarName];
    7682         116 :                 const char *pszValue = pszEqual + 1;
    7683         232 :                 while (*pszValue == ' ')
    7684         116 :                     pszValue++;
    7685             : 
    7686         116 :                 status = NC_EBADTYPE;
    7687         116 :                 if (*pszValue == '"')
    7688             :                 {
    7689             :                     // For _FillValue, the attribute type should match
    7690             :                     // the variable type. Leaks memory with NC4 otherwise
    7691          74 :                     if (osAttrName == "_FillValue")
    7692             :                     {
    7693           0 :                         CPLDebug("netCDF", "nc_put_att_(%s:%s) failed: %s",
    7694             :                                  osVarName.c_str(), osAttrName.c_str(),
    7695             :                                  nc_strerror(status));
    7696           0 :                         continue;
    7697             :                     }
    7698             : 
    7699             :                     // Unquote and unescape string value
    7700          74 :                     CPLString osVal(pszValue + 1);
    7701         222 :                     while (!osVal.empty())
    7702             :                     {
    7703         222 :                         if (osVal.back() == ';' || osVal.back() == ' ')
    7704             :                         {
    7705         148 :                             osVal.pop_back();
    7706             :                         }
    7707          74 :                         else if (osVal.back() == '"')
    7708             :                         {
    7709          74 :                             osVal.pop_back();
    7710          74 :                             break;
    7711             :                         }
    7712             :                         else
    7713             :                         {
    7714           0 :                             break;
    7715             :                         }
    7716             :                     }
    7717          74 :                     osVal.replaceAll("\\\"", '"');
    7718          74 :                     status = nc_put_att_text(nCdfId, nVarId, osAttrName,
    7719             :                                              osVal.size(), osVal.c_str());
    7720             :                 }
    7721             :                 else
    7722             :                 {
    7723          84 :                     CPLString osVal(pszValue);
    7724         126 :                     while (!osVal.empty())
    7725             :                     {
    7726         126 :                         if (osVal.back() == ';' || osVal.back() == ' ')
    7727             :                         {
    7728          84 :                             osVal.pop_back();
    7729             :                         }
    7730             :                         else
    7731             :                         {
    7732          42 :                             break;
    7733             :                         }
    7734             :                     }
    7735          42 :                     int nc_datatype = -1;
    7736          42 :                     if (!osVal.empty() && osVal.back() == 'b')
    7737             :                     {
    7738           3 :                         nc_datatype = NC_BYTE;
    7739           3 :                         osVal.pop_back();
    7740             :                     }
    7741          39 :                     else if (!osVal.empty() && osVal.back() == 's')
    7742             :                     {
    7743           3 :                         nc_datatype = NC_SHORT;
    7744           3 :                         osVal.pop_back();
    7745             :                     }
    7746          42 :                     if (CPLGetValueType(osVal) == CPL_VALUE_INTEGER)
    7747             :                     {
    7748           7 :                         if (nc_datatype < 0)
    7749           4 :                             nc_datatype = NC_INT;
    7750             :                     }
    7751          35 :                     else if (CPLGetValueType(osVal) == CPL_VALUE_REAL)
    7752             :                     {
    7753          32 :                         nc_datatype = NC_DOUBLE;
    7754             :                     }
    7755             :                     else
    7756             :                     {
    7757           3 :                         nc_datatype = -1;
    7758             :                     }
    7759             : 
    7760             :                     // For _FillValue, check that the attribute type matches
    7761             :                     // the variable type. Leaks memory with NC4 otherwise
    7762          42 :                     if (osAttrName == "_FillValue")
    7763             :                     {
    7764           6 :                         if (nVarId < 0 ||
    7765           3 :                             nc_datatype != oMapVarIdToType[nVarId])
    7766             :                         {
    7767           0 :                             nc_datatype = -1;
    7768             :                         }
    7769             :                     }
    7770             : 
    7771          42 :                     if (nc_datatype == NC_BYTE)
    7772             :                     {
    7773             :                         signed char chVal =
    7774           3 :                             static_cast<signed char>(atoi(osVal));
    7775           3 :                         status = nc_put_att_schar(nCdfId, nVarId, osAttrName,
    7776             :                                                   NC_BYTE, 1, &chVal);
    7777             :                     }
    7778          39 :                     else if (nc_datatype == NC_SHORT)
    7779             :                     {
    7780           0 :                         short nVal = static_cast<short>(atoi(osVal));
    7781           0 :                         status = nc_put_att_short(nCdfId, nVarId, osAttrName,
    7782             :                                                   NC_SHORT, 1, &nVal);
    7783             :                     }
    7784          39 :                     else if (nc_datatype == NC_INT)
    7785             :                     {
    7786           4 :                         int nVal = static_cast<int>(atoi(osVal));
    7787           4 :                         status = nc_put_att_int(nCdfId, nVarId, osAttrName,
    7788             :                                                 NC_INT, 1, &nVal);
    7789             :                     }
    7790          35 :                     else if (nc_datatype == NC_DOUBLE)
    7791             :                     {
    7792          32 :                         double dfVal = CPLAtof(osVal);
    7793          32 :                         status = nc_put_att_double(nCdfId, nVarId, osAttrName,
    7794             :                                                    NC_DOUBLE, 1, &dfVal);
    7795             :                     }
    7796             :                 }
    7797         116 :                 if (status != NC_NOERR)
    7798             :                 {
    7799           3 :                     CPLDebug("netCDF", "nc_put_att_(%s:%s) failed: %s",
    7800             :                              osVarName.c_str(), osAttrName.c_str(),
    7801             :                              nc_strerror(status));
    7802             :                 }
    7803             :                 else
    7804             :                 {
    7805         113 :                     oSetAttrDefined.insert(osVarName + ":" + osAttrName);
    7806             : #ifdef DEBUG_VERBOSE
    7807             :                     CPLDebug("netCDF", "nc_put_att_(%s:%s) (%s) succeeded",
    7808             :                              osVarName.c_str(), osAttrName.c_str(), pszLine);
    7809             : #endif
    7810             :                 }
    7811             :             }
    7812             :         }
    7813          42 :         else if (nActiveSection == SECTION_DATA)
    7814             :         {
    7815          55 :             while (*pszLine == ' ' || *pszLine == '\t')
    7816          17 :                 pszLine++;
    7817          38 :             const char *pszEqual = strchr(pszLine, '=');
    7818          38 :             if (pszEqual)
    7819             :             {
    7820          17 :                 CPLString osVarName(pszLine, pszEqual - pszLine);
    7821          17 :                 osVarName.Trim();
    7822          17 :                 if (oMapVarToId.find(osVarName) == oMapVarToId.end())
    7823           0 :                     continue;
    7824          17 :                 const int nVarId = oMapVarToId[osVarName];
    7825          17 :                 CPLString osAccVal(pszEqual + 1);
    7826          17 :                 osAccVal.Trim();
    7827         153 :                 while (osAccVal.empty() || osAccVal.back() != ';')
    7828             :                 {
    7829         136 :                     pszLine = CPLReadLineL(fpSrc);
    7830         136 :                     if (pszLine == nullptr)
    7831           0 :                         break;
    7832         272 :                     CPLString osVal(pszLine);
    7833         136 :                     osVal.Trim();
    7834         136 :                     osAccVal += osVal;
    7835             :                 }
    7836          17 :                 if (pszLine == nullptr)
    7837           0 :                     break;
    7838          17 :                 osAccVal.pop_back();
    7839             : 
    7840             :                 const std::vector<int> aoDimIds =
    7841          34 :                     oMapVarIdToVectorOfDimId[nVarId];
    7842          17 :                 size_t nSize = 1;
    7843          34 :                 std::vector<size_t> aoStart, aoEdge;
    7844          17 :                 aoStart.resize(aoDimIds.size());
    7845          17 :                 aoEdge.resize(aoDimIds.size());
    7846          35 :                 for (size_t i = 0; i < aoDimIds.size(); ++i)
    7847             :                 {
    7848          18 :                     const size_t nDimSize = oMapDimIdToDimLen[aoDimIds[i]];
    7849          36 :                     if (nDimSize != 0 &&
    7850          18 :                         nSize > std::numeric_limits<size_t>::max() / nDimSize)
    7851             :                     {
    7852           0 :                         nSize = 0;
    7853             :                     }
    7854             :                     else
    7855             :                     {
    7856          18 :                         nSize *= nDimSize;
    7857             :                     }
    7858          18 :                     aoStart[i] = 0;
    7859          18 :                     aoEdge[i] = nDimSize;
    7860             :                 }
    7861             : 
    7862          17 :                 status = NC_EBADTYPE;
    7863          17 :                 if (nSize == 0)
    7864             :                 {
    7865             :                     // Might happen with a unlimited dimension
    7866             :                 }
    7867          17 :                 else if (oMapVarIdToType[nVarId] == NC_DOUBLE)
    7868             :                 {
    7869           8 :                     if (!aoStart.empty())
    7870             :                     {
    7871             :                         char **papszTokens =
    7872           8 :                             CSLTokenizeString2(osAccVal, " ,;", 0);
    7873           8 :                         size_t nTokens = CSLCount(papszTokens);
    7874           8 :                         if (nTokens >= nSize)
    7875             :                         {
    7876             :                             double *padfVals = static_cast<double *>(
    7877           8 :                                 VSI_CALLOC_VERBOSE(nSize, sizeof(double)));
    7878           8 :                             if (padfVals)
    7879             :                             {
    7880         132 :                                 for (size_t i = 0; i < nSize; i++)
    7881             :                                 {
    7882         124 :                                     padfVals[i] = CPLAtof(papszTokens[i]);
    7883             :                                 }
    7884           8 :                                 status = nc_put_vara_double(
    7885           8 :                                     nCdfId, nVarId, &aoStart[0], &aoEdge[0],
    7886             :                                     padfVals);
    7887           8 :                                 VSIFree(padfVals);
    7888             :                             }
    7889             :                         }
    7890           8 :                         CSLDestroy(papszTokens);
    7891             :                     }
    7892             :                 }
    7893           9 :                 else if (oMapVarIdToType[nVarId] == NC_BYTE)
    7894             :                 {
    7895           3 :                     if (!aoStart.empty())
    7896             :                     {
    7897             :                         char **papszTokens =
    7898           3 :                             CSLTokenizeString2(osAccVal, " ,;", 0);
    7899           3 :                         size_t nTokens = CSLCount(papszTokens);
    7900           3 :                         if (nTokens >= nSize)
    7901             :                         {
    7902             :                             signed char *panVals = static_cast<signed char *>(
    7903           3 :                                 VSI_CALLOC_VERBOSE(nSize, sizeof(signed char)));
    7904           3 :                             if (panVals)
    7905             :                             {
    7906        1203 :                                 for (size_t i = 0; i < nSize; i++)
    7907             :                                 {
    7908        1200 :                                     panVals[i] = static_cast<signed char>(
    7909        1200 :                                         atoi(papszTokens[i]));
    7910             :                                 }
    7911           3 :                                 status = nc_put_vara_schar(nCdfId, nVarId,
    7912           3 :                                                            &aoStart[0],
    7913           3 :                                                            &aoEdge[0], panVals);
    7914           3 :                                 VSIFree(panVals);
    7915             :                             }
    7916             :                         }
    7917           3 :                         CSLDestroy(papszTokens);
    7918             :                     }
    7919             :                 }
    7920           6 :                 else if (oMapVarIdToType[nVarId] == NC_CHAR)
    7921             :                 {
    7922           6 :                     if (aoStart.size() == 2)
    7923             :                     {
    7924           4 :                         std::vector<CPLString> aoStrings;
    7925           2 :                         bool bInString = false;
    7926           4 :                         CPLString osCurString;
    7927         935 :                         for (size_t i = 0; i < osAccVal.size();)
    7928             :                         {
    7929         933 :                             if (!bInString)
    7930             :                             {
    7931           8 :                                 if (osAccVal[i] == '"')
    7932             :                                 {
    7933           4 :                                     bInString = true;
    7934           4 :                                     osCurString.clear();
    7935             :                                 }
    7936           8 :                                 i++;
    7937             :                             }
    7938         926 :                             else if (osAccVal[i] == '\\' &&
    7939         926 :                                      i + 1 < osAccVal.size() &&
    7940           1 :                                      osAccVal[i + 1] == '"')
    7941             :                             {
    7942           1 :                                 osCurString += '"';
    7943           1 :                                 i += 2;
    7944             :                             }
    7945         924 :                             else if (osAccVal[i] == '"')
    7946             :                             {
    7947           4 :                                 aoStrings.push_back(osCurString);
    7948           4 :                                 osCurString.clear();
    7949           4 :                                 bInString = false;
    7950           4 :                                 i++;
    7951             :                             }
    7952             :                             else
    7953             :                             {
    7954         920 :                                 osCurString += osAccVal[i];
    7955         920 :                                 i++;
    7956             :                             }
    7957             :                         }
    7958           2 :                         const size_t nRecords = oMapDimIdToDimLen[aoDimIds[0]];
    7959           2 :                         const size_t nWidth = oMapDimIdToDimLen[aoDimIds[1]];
    7960           2 :                         size_t nIters = aoStrings.size();
    7961           2 :                         if (nIters > nRecords)
    7962           0 :                             nIters = nRecords;
    7963           6 :                         for (size_t i = 0; i < nIters; i++)
    7964             :                         {
    7965             :                             size_t anIndex[2];
    7966           4 :                             anIndex[0] = i;
    7967           4 :                             anIndex[1] = 0;
    7968             :                             size_t anCount[2];
    7969           4 :                             anCount[0] = 1;
    7970           4 :                             anCount[1] = aoStrings[i].size();
    7971           4 :                             if (anCount[1] > nWidth)
    7972           0 :                                 anCount[1] = nWidth;
    7973             :                             status =
    7974           4 :                                 nc_put_vara_text(nCdfId, nVarId, anIndex,
    7975           4 :                                                  anCount, aoStrings[i].c_str());
    7976           4 :                             if (status != NC_NOERR)
    7977           0 :                                 break;
    7978             :                         }
    7979             :                     }
    7980             :                 }
    7981          17 :                 if (status != NC_NOERR)
    7982             :                 {
    7983           4 :                     CPLDebug("netCDF", "nc_put_var_(%s) failed: %s",
    7984             :                              osVarName.c_str(), nc_strerror(status));
    7985             :                 }
    7986             :             }
    7987             :         }
    7988             :     }
    7989             : 
    7990           4 :     GDAL_nc_close(nCdfId);
    7991           4 :     return true;
    7992             : }
    7993             : 
    7994             : #endif  // ENABLE_NCDUMP
    7995             : 
    7996             : /************************************************************************/
    7997             : /*                                Open()                                */
    7998             : /************************************************************************/
    7999             : 
    8000         678 : GDALDataset *netCDFDataset::Open(GDALOpenInfo *poOpenInfo)
    8001             : 
    8002             : {
    8003             : #ifdef NCDF_DEBUG
    8004             :     CPLDebug("GDAL_netCDF", "\n=====\nOpen(), filename=[%s]",
    8005             :              poOpenInfo->pszFilename);
    8006             : #endif
    8007             : 
    8008             :     // Does this appear to be a netcdf file?
    8009         678 :     NetCDFFormatEnum eTmpFormat = NCDF_FORMAT_NONE;
    8010         678 :     if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "NETCDF:"))
    8011             :     {
    8012         618 :         eTmpFormat = netCDFIdentifyFormat(poOpenInfo, /* bCheckExt = */ true);
    8013             : #ifdef NCDF_DEBUG
    8014             :         CPLDebug("GDAL_netCDF", "identified format %d", eTmpFormat);
    8015             : #endif
    8016             :         // Note: not calling Identify() directly, because we want the file type.
    8017             :         // Only support NCDF_FORMAT* formats.
    8018         618 :         if (NCDF_FORMAT_NC == eTmpFormat || NCDF_FORMAT_NC2 == eTmpFormat ||
    8019           2 :             NCDF_FORMAT_NC4 == eTmpFormat || NCDF_FORMAT_NC4C == eTmpFormat)
    8020             :         {
    8021             :             // ok
    8022             :         }
    8023           2 :         else if (eTmpFormat == NCDF_FORMAT_HDF4 &&
    8024           0 :                  poOpenInfo->IsSingleAllowedDriver("netCDF"))
    8025             :         {
    8026             :             // ok
    8027             :         }
    8028             :         else
    8029             :         {
    8030           2 :             return nullptr;
    8031             :         }
    8032             :     }
    8033             :     else
    8034             :     {
    8035             : #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    8036             :         // We don't necessarily want to catch bugs in libnetcdf ...
    8037             :         if (CPLGetConfigOption("DISABLE_OPEN_REAL_NETCDF_FILES", nullptr))
    8038             :         {
    8039             :             return nullptr;
    8040             :         }
    8041             : #endif
    8042             :     }
    8043             : 
    8044         676 :     if (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER)
    8045             :     {
    8046         183 :         return OpenMultiDim(poOpenInfo);
    8047             :     }
    8048             : 
    8049         986 :     CPLMutexHolderD(&hNCMutex);
    8050             : 
    8051         493 :     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock with
    8052             :         // GDALDataset own mutex.
    8053         493 :     netCDFDataset *poDS = new netCDFDataset();
    8054         493 :     poDS->papszOpenOptions = CSLDuplicate(poOpenInfo->papszOpenOptions);
    8055         493 :     CPLAcquireMutex(hNCMutex, 1000.0);
    8056             : 
    8057         493 :     poDS->SetDescription(poOpenInfo->pszFilename);
    8058             : 
    8059             :     // Check if filename start with NETCDF: tag.
    8060         493 :     bool bTreatAsSubdataset = false;
    8061         986 :     CPLString osSubdatasetName;
    8062             : 
    8063             : #ifdef ENABLE_NCDUMP
    8064         493 :     const char *pszHeader =
    8065             :         reinterpret_cast<const char *>(poOpenInfo->pabyHeader);
    8066         493 :     if (poOpenInfo->fpL != nullptr && STARTS_WITH(pszHeader, "netcdf ") &&
    8067           3 :         strstr(pszHeader, "dimensions:") && strstr(pszHeader, "variables:"))
    8068             :     {
    8069             :         // By default create a temporary file that will be destroyed,
    8070             :         // unless NETCDF_TMP_FILE is defined. Can be useful to see which
    8071             :         // netCDF file has been generated from a potential fuzzed input.
    8072           3 :         poDS->osFilename = CPLGetConfigOption("NETCDF_TMP_FILE", "");
    8073           3 :         if (poDS->osFilename.empty())
    8074             :         {
    8075           3 :             poDS->bFileToDestroyAtClosing = true;
    8076           3 :             poDS->osFilename = CPLGenerateTempFilenameSafe("netcdf_tmp");
    8077             :         }
    8078           3 :         if (!netCDFDatasetCreateTempFile(eTmpFormat, poDS->osFilename,
    8079             :                                          poOpenInfo->fpL))
    8080             :         {
    8081           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8082             :                 // deadlock with GDALDataset own mutex.
    8083           0 :             delete poDS;
    8084           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8085           0 :             return nullptr;
    8086             :         }
    8087           3 :         bTreatAsSubdataset = false;
    8088           3 :         poDS->eFormat = eTmpFormat;
    8089             :     }
    8090             :     else
    8091             : #endif
    8092             : 
    8093         490 :         if (STARTS_WITH_CI(poOpenInfo->pszFilename, "NETCDF:"))
    8094             :     {
    8095             :         char **papszName =
    8096          60 :             CSLTokenizeString2(poOpenInfo->pszFilename, ":",
    8097             :                                CSLT_HONOURSTRINGS | CSLT_PRESERVEESCAPES);
    8098             : 
    8099         120 :         if (CSLCount(papszName) >= 3 &&
    8100          60 :             ((strlen(papszName[1]) == 1 && /* D:\\bla */
    8101           0 :               (papszName[2][0] == '/' || papszName[2][0] == '\\')) ||
    8102          60 :              EQUAL(papszName[1], "http") || EQUAL(papszName[1], "https") ||
    8103          60 :              EQUAL(papszName[1], "/vsicurl/http") ||
    8104          60 :              EQUAL(papszName[1], "/vsicurl/https") ||
    8105          60 :              EQUAL(papszName[1], "/vsicurl_streaming/http") ||
    8106          60 :              EQUAL(papszName[1], "/vsicurl_streaming/https")))
    8107             :         {
    8108           0 :             const int nCountBefore = CSLCount(papszName);
    8109           0 :             CPLString osTmp = papszName[1];
    8110           0 :             osTmp += ':';
    8111           0 :             osTmp += papszName[2];
    8112           0 :             CPLFree(papszName[1]);
    8113           0 :             CPLFree(papszName[2]);
    8114           0 :             papszName[1] = CPLStrdup(osTmp);
    8115           0 :             memmove(papszName + 2, papszName + 3,
    8116           0 :                     (nCountBefore - 2) * sizeof(char *));
    8117             :         }
    8118             : 
    8119          60 :         if (CSLCount(papszName) == 3)
    8120             :         {
    8121          60 :             poDS->osFilename = papszName[1];
    8122          60 :             osSubdatasetName = papszName[2];
    8123          60 :             bTreatAsSubdataset = true;
    8124          60 :             CSLDestroy(papszName);
    8125             :         }
    8126           0 :         else if (CSLCount(papszName) == 2)
    8127             :         {
    8128           0 :             poDS->osFilename = papszName[1];
    8129           0 :             osSubdatasetName = "";
    8130           0 :             bTreatAsSubdataset = false;
    8131           0 :             CSLDestroy(papszName);
    8132             :         }
    8133             :         else
    8134             :         {
    8135           0 :             CSLDestroy(papszName);
    8136           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8137             :                 // deadlock with GDALDataset own mutex.
    8138           0 :             delete poDS;
    8139           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8140           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    8141             :                      "Failed to parse NETCDF: prefix string into expected 2, 3 "
    8142             :                      "or 4 fields.");
    8143           0 :             return nullptr;
    8144             :         }
    8145             : 
    8146         120 :         if (!STARTS_WITH(poDS->osFilename, "http://") &&
    8147          60 :             !STARTS_WITH(poDS->osFilename, "https://"))
    8148             :         {
    8149             :             // Identify Format from real file, with bCheckExt=FALSE.
    8150             :             GDALOpenInfo *poOpenInfo2 =
    8151          60 :                 new GDALOpenInfo(poDS->osFilename.c_str(), GA_ReadOnly);
    8152          60 :             poDS->eFormat =
    8153          60 :                 netCDFIdentifyFormat(poOpenInfo2, /* bCheckExt = */ false);
    8154          60 :             delete poOpenInfo2;
    8155          60 :             if (NCDF_FORMAT_NONE == poDS->eFormat ||
    8156          60 :                 NCDF_FORMAT_UNKNOWN == poDS->eFormat)
    8157             :             {
    8158           0 :                 CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8159             :                     // deadlock with GDALDataset own mutex.
    8160           0 :                 delete poDS;
    8161           0 :                 CPLAcquireMutex(hNCMutex, 1000.0);
    8162           0 :                 return nullptr;
    8163             :             }
    8164             :         }
    8165             :     }
    8166             :     else
    8167             :     {
    8168         430 :         poDS->osFilename = poOpenInfo->pszFilename;
    8169         430 :         bTreatAsSubdataset = false;
    8170         430 :         poDS->eFormat = eTmpFormat;
    8171             :     }
    8172             : 
    8173             : // Try opening the dataset.
    8174             : #if defined(NCDF_DEBUG) && defined(ENABLE_UFFD)
    8175             :     CPLDebug("GDAL_netCDF", "calling nc_open_mem(%s)",
    8176             :              poDS->osFilename.c_str());
    8177             : #elif defined(NCDF_DEBUG) && !defined(ENABLE_UFFD)
    8178             :     CPLDebug("GDAL_netCDF", "calling nc_open(%s)", poDS->osFilename.c_str());
    8179             : #endif
    8180         493 :     int cdfid = -1;
    8181         493 :     const int nMode = ((poOpenInfo->nOpenFlags & GDAL_OF_UPDATE) != 0)
    8182             :                           ? NC_WRITE
    8183             :                           : NC_NOWRITE;
    8184         986 :     CPLString osFilenameForNCOpen(poDS->osFilename);
    8185             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    8186             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    8187             :     {
    8188             :         char *pszTemp = CPLRecode(osFilenameForNCOpen, CPL_ENC_UTF8, "CP_ACP");
    8189             :         osFilenameForNCOpen = pszTemp;
    8190             :         CPLFree(pszTemp);
    8191             :     }
    8192             : #endif
    8193         493 :     int status2 = -1;
    8194             : 
    8195             : #ifdef ENABLE_UFFD
    8196         493 :     cpl_uffd_context *pCtx = nullptr;
    8197             : #endif
    8198             : 
    8199         508 :     if (STARTS_WITH(osFilenameForNCOpen, "/vsimem/") &&
    8200          15 :         poOpenInfo->eAccess == GA_ReadOnly)
    8201             :     {
    8202          15 :         vsi_l_offset nLength = 0;
    8203          15 :         poDS->fpVSIMEM = VSIFOpenL(osFilenameForNCOpen, "rb");
    8204          15 :         if (poDS->fpVSIMEM)
    8205             :         {
    8206             :             // We assume that the file will not be modified. If it is, then
    8207             :             // pabyBuffer might become invalid.
    8208             :             GByte *pabyBuffer =
    8209          15 :                 VSIGetMemFileBuffer(osFilenameForNCOpen, &nLength, false);
    8210          15 :             if (pabyBuffer)
    8211             :             {
    8212          15 :                 status2 = nc_open_mem(CPLGetFilename(osFilenameForNCOpen),
    8213             :                                       nMode, static_cast<size_t>(nLength),
    8214             :                                       pabyBuffer, &cdfid);
    8215             :             }
    8216             :         }
    8217             :     }
    8218             :     else
    8219             :     {
    8220             :         const bool bVsiFile =
    8221         478 :             !strncmp(osFilenameForNCOpen, "/vsi", strlen("/vsi"));
    8222             : #ifdef ENABLE_UFFD
    8223         478 :         bool bReadOnly = (poOpenInfo->eAccess == GA_ReadOnly);
    8224         478 :         void *pVma = nullptr;
    8225         478 :         uint64_t nVmaSize = 0;
    8226             : 
    8227         478 :         if (bVsiFile)
    8228             :         {
    8229           2 :             if (bReadOnly)
    8230             :             {
    8231           2 :                 if (CPLIsUserFaultMappingSupported())
    8232             :                 {
    8233           2 :                     pCtx = CPLCreateUserFaultMapping(osFilenameForNCOpen, &pVma,
    8234             :                                                      &nVmaSize);
    8235             :                 }
    8236             :                 else
    8237             :                 {
    8238           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    8239             :                              "Opening a /vsi file with the netCDF driver "
    8240             :                              "requires Linux userfaultfd to be available. "
    8241             :                              "If running from Docker, "
    8242             :                              "--security-opt seccomp=unconfined might be "
    8243             :                              "needed.%s",
    8244           0 :                              ((poDS->eFormat == NCDF_FORMAT_NC4 ||
    8245           0 :                                poDS->eFormat == NCDF_FORMAT_HDF5) &&
    8246           0 :                               GDALGetDriverByName("HDF5"))
    8247             :                                  ? " Or you may set the GDAL_SKIP=netCDF "
    8248             :                                    "configuration option to force the use of "
    8249             :                                    "the HDF5 driver."
    8250             :                                  : "");
    8251             :                 }
    8252             :             }
    8253             :             else
    8254             :             {
    8255           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    8256             :                          "Opening a /vsi file with the netCDF driver is only "
    8257             :                          "supported in read-only mode");
    8258             :             }
    8259             :         }
    8260         478 :         if (pCtx != nullptr && pVma != nullptr && nVmaSize > 0)
    8261             :         {
    8262             :             // netCDF code, at least for netCDF 4.7.0, is confused by filenames
    8263             :             // like /vsicurl/http[s]://example.com/foo.nc, so just pass the
    8264             :             // final part
    8265           2 :             status2 = nc_open_mem(CPLGetFilename(osFilenameForNCOpen), nMode,
    8266             :                                   static_cast<size_t>(nVmaSize), pVma, &cdfid);
    8267             :         }
    8268             :         else
    8269         476 :             status2 = GDAL_nc_open(osFilenameForNCOpen, nMode, &cdfid);
    8270             : #else
    8271             :         if (bVsiFile)
    8272             :         {
    8273             :             CPLError(
    8274             :                 CE_Failure, CPLE_AppDefined,
    8275             :                 "Opening a /vsi file with the netCDF driver requires Linux "
    8276             :                 "userfaultfd to be available.%s",
    8277             :                 ((poDS->eFormat == NCDF_FORMAT_NC4 ||
    8278             :                   poDS->eFormat == NCDF_FORMAT_HDF5) &&
    8279             :                  GDALGetDriverByName("HDF5"))
    8280             :                     ? " Or you may set the GDAL_SKIP=netCDF "
    8281             :                       "configuration option to force the use of the HDF5 "
    8282             :                       "driver."
    8283             :                     : "");
    8284             :             status2 = NC_EIO;
    8285             :         }
    8286             :         else
    8287             :         {
    8288             :             status2 = GDAL_nc_open(osFilenameForNCOpen, nMode, &cdfid);
    8289             :         }
    8290             : #endif
    8291             :     }
    8292         493 :     if (status2 != NC_NOERR)
    8293             :     {
    8294             : #ifdef NCDF_DEBUG
    8295             :         CPLDebug("GDAL_netCDF", "error opening");
    8296             : #endif
    8297           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8298             :             // with GDALDataset own mutex.
    8299           0 :         delete poDS;
    8300           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8301           0 :         return nullptr;
    8302             :     }
    8303             : #ifdef NCDF_DEBUG
    8304             :     CPLDebug("GDAL_netCDF", "got cdfid=%d", cdfid);
    8305             : #endif
    8306             : 
    8307             : #if defined(ENABLE_NCDUMP) && !defined(_WIN32)
    8308             :     // Try to destroy the temporary file right now on Unix
    8309         493 :     if (poDS->bFileToDestroyAtClosing)
    8310             :     {
    8311           3 :         if (VSIUnlink(poDS->osFilename) == 0)
    8312             :         {
    8313           3 :             poDS->bFileToDestroyAtClosing = false;
    8314             :         }
    8315             :     }
    8316             : #endif
    8317             : 
    8318             :     // Is this a real netCDF file?
    8319             :     int ndims;
    8320             :     int ngatts;
    8321             :     int nvars;
    8322             :     int unlimdimid;
    8323         493 :     int status = nc_inq(cdfid, &ndims, &nvars, &ngatts, &unlimdimid);
    8324         493 :     if (status != NC_NOERR)
    8325             :     {
    8326           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8327             :             // with GDALDataset own mutex.
    8328           0 :         delete poDS;
    8329           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8330           0 :         return nullptr;
    8331             :     }
    8332             : 
    8333             :     // Get file type from netcdf.
    8334         493 :     int nTmpFormat = NCDF_FORMAT_NONE;
    8335         493 :     status = nc_inq_format(cdfid, &nTmpFormat);
    8336         493 :     if (status != NC_NOERR)
    8337             :     {
    8338           0 :         NCDF_ERR(status);
    8339             :     }
    8340             :     else
    8341             :     {
    8342         493 :         CPLDebug("GDAL_netCDF",
    8343             :                  "driver detected file type=%d, libnetcdf detected type=%d",
    8344         493 :                  poDS->eFormat, nTmpFormat);
    8345         493 :         if (static_cast<NetCDFFormatEnum>(nTmpFormat) != poDS->eFormat)
    8346             :         {
    8347             :             // Warn if file detection conflicts with that from libnetcdf
    8348             :             // except for NC4C, which we have no way of detecting initially.
    8349          26 :             if (nTmpFormat != NCDF_FORMAT_NC4C &&
    8350          13 :                 !STARTS_WITH(poDS->osFilename, "http://") &&
    8351           0 :                 !STARTS_WITH(poDS->osFilename, "https://"))
    8352             :             {
    8353           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    8354             :                          "NetCDF driver detected file type=%d, but libnetcdf "
    8355             :                          "detected type=%d",
    8356           0 :                          poDS->eFormat, nTmpFormat);
    8357             :             }
    8358          13 :             CPLDebug("GDAL_netCDF", "setting file type to %d, was %d",
    8359          13 :                      nTmpFormat, poDS->eFormat);
    8360          13 :             poDS->eFormat = static_cast<NetCDFFormatEnum>(nTmpFormat);
    8361             :         }
    8362             :     }
    8363             : 
    8364             :     // Does the request variable exist?
    8365         493 :     if (bTreatAsSubdataset)
    8366             :     {
    8367             :         int dummy;
    8368          60 :         if (NCDFOpenSubDataset(cdfid, osSubdatasetName.c_str(), &dummy,
    8369          60 :                                &dummy) != CE_None)
    8370             :         {
    8371           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    8372             :                      "%s is a netCDF file, but %s is not a variable.",
    8373             :                      poOpenInfo->pszFilename, osSubdatasetName.c_str());
    8374             : 
    8375           0 :             GDAL_nc_close(cdfid);
    8376             : #ifdef ENABLE_UFFD
    8377           0 :             NETCDF_UFFD_UNMAP(pCtx);
    8378             : #endif
    8379           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8380             :                 // deadlock with GDALDataset own mutex.
    8381           0 :             delete poDS;
    8382           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8383           0 :             return nullptr;
    8384             :         }
    8385             :     }
    8386             : 
    8387             :     // Figure out whether or not the listed dataset has support for simple
    8388             :     // geometries (CF-1.8)
    8389         493 :     poDS->nCFVersion = nccfdriver::getCFVersion(cdfid);
    8390         493 :     bool bHasSimpleGeometries = false;  // but not necessarily valid
    8391         493 :     if (poDS->nCFVersion >= 1.8)
    8392             :     {
    8393          75 :         bHasSimpleGeometries = poDS->DetectAndFillSGLayers(cdfid);
    8394          75 :         if (bHasSimpleGeometries)
    8395             :         {
    8396          67 :             poDS->bSGSupport = true;
    8397          67 :             poDS->vcdf.enableFullVirtualMode();
    8398             :         }
    8399             :     }
    8400             : 
    8401             :     char szConventions[NC_MAX_NAME + 1];
    8402         493 :     szConventions[0] = '\0';
    8403         493 :     nc_type nAttype = NC_NAT;
    8404         493 :     size_t nAttlen = 0;
    8405         493 :     nc_inq_att(cdfid, NC_GLOBAL, "Conventions", &nAttype, &nAttlen);
    8406         986 :     if (nAttlen >= sizeof(szConventions) ||
    8407         493 :         nc_get_att_text(cdfid, NC_GLOBAL, "Conventions", szConventions) !=
    8408             :             NC_NOERR)
    8409             :     {
    8410          57 :         CPLDebug("GDAL_netCDF", "No UNIDATA NC_GLOBAL:Conventions attribute");
    8411             :         // Note that 'Conventions' is always capital 'C' in CF spec.
    8412             :     }
    8413             :     else
    8414             :     {
    8415         436 :         szConventions[nAttlen] = '\0';
    8416             :     }
    8417             : 
    8418             :     // Create band information objects.
    8419         493 :     CPLDebug("GDAL_netCDF", "var_count = %d", nvars);
    8420             : 
    8421             :     // Create a corresponding GDALDataset.
    8422             :     // Create Netcdf Subdataset if filename as NETCDF tag.
    8423         493 :     poDS->cdfid = cdfid;
    8424             : #ifdef ENABLE_UFFD
    8425         493 :     poDS->pCtx = pCtx;
    8426             : #endif
    8427         493 :     poDS->eAccess = poOpenInfo->eAccess;
    8428         493 :     poDS->bDefineMode = false;
    8429             : 
    8430         493 :     poDS->ReadAttributes(cdfid, NC_GLOBAL);
    8431             : 
    8432             :     // Identify coordinate and boundary variables that we should
    8433             :     // ignore as Raster Bands.
    8434         493 :     char **papszIgnoreVars = nullptr;
    8435         493 :     NCDFGetCoordAndBoundVarFullNames(cdfid, &papszIgnoreVars);
    8436             :     // Filter variables to keep only valid 2+D raster bands and vector fields.
    8437         493 :     int nRasterVars = 0;
    8438         493 :     int nIgnoredVars = 0;
    8439         493 :     int nGroupID = -1;
    8440         493 :     int nVarID = -1;
    8441             : 
    8442             :     std::map<std::array<int, 3>, std::vector<std::pair<int, int>>>
    8443         986 :         oMap2DDimsToGroupAndVar;
    8444        1138 :     if ((poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
    8445         152 :         STARTS_WITH(CSLFetchNameValueDef(poDS->papszMetadata,
    8446             :                                          "NC_GLOBAL#mission_name", ""),
    8447           1 :                     "Sentinel 3") &&
    8448           1 :         EQUAL(CSLFetchNameValueDef(poDS->papszMetadata,
    8449             :                                    "NC_GLOBAL#altimeter_sensor_name", ""),
    8450         645 :               "SRAL") &&
    8451           1 :         EQUAL(CSLFetchNameValueDef(poDS->papszMetadata,
    8452             :                                    "NC_GLOBAL#radiometer_sensor_name", ""),
    8453             :               "MWR"))
    8454             :     {
    8455           1 :         if (poDS->eAccess == GA_Update)
    8456             :         {
    8457           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8458             :                 // deadlock with GDALDataset own mutex.
    8459           0 :             delete poDS;
    8460           0 :             return nullptr;
    8461             :         }
    8462           1 :         poDS->ProcessSentinel3_SRAL_MWR();
    8463             :     }
    8464             :     else
    8465             :     {
    8466         492 :         poDS->FilterVars(cdfid, (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0,
    8467         643 :                          (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
    8468         151 :                              !bHasSimpleGeometries,
    8469             :                          papszIgnoreVars, &nRasterVars, &nGroupID, &nVarID,
    8470             :                          &nIgnoredVars, oMap2DDimsToGroupAndVar);
    8471             :     }
    8472         493 :     CSLDestroy(papszIgnoreVars);
    8473             : 
    8474             :     // Case where there is no raster variable
    8475         493 :     if (nRasterVars == 0 && !bTreatAsSubdataset)
    8476             :     {
    8477         119 :         poDS->GDALPamDataset::SetMetadata(poDS->papszMetadata);
    8478         119 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8479             :             // with GDALDataset own mutex.
    8480         119 :         poDS->TryLoadXML();
    8481             :         // If the dataset has been opened in raster mode only, exit
    8482         119 :         if ((poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
    8483           9 :             (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0)
    8484             :         {
    8485           4 :             delete poDS;
    8486           4 :             poDS = nullptr;
    8487             :         }
    8488             :         // Otherwise if the dataset is opened in vector mode, that there is
    8489             :         // no vector layer and we are in read-only, exit too.
    8490         115 :         else if (poDS->GetLayerCount() == 0 &&
    8491         123 :                  (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
    8492           8 :                  poOpenInfo->eAccess == GA_ReadOnly)
    8493             :         {
    8494           8 :             delete poDS;
    8495           8 :             poDS = nullptr;
    8496             :         }
    8497         119 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8498         119 :         return poDS;
    8499             :     }
    8500             : 
    8501             :     // We have more than one variable with 2 dimensions in the
    8502             :     // file, then treat this as a subdataset container dataset.
    8503         374 :     bool bSeveralVariablesAsBands = false;
    8504         374 :     const bool bListAllArrays = CPLTestBool(
    8505         374 :         CSLFetchNameValueDef(poDS->papszOpenOptions, "LIST_ALL_ARRAYS", "NO"));
    8506         374 :     if (bListAllArrays || ((nRasterVars > 1) && !bTreatAsSubdataset))
    8507             :     {
    8508          28 :         if (CPLFetchBool(poOpenInfo->papszOpenOptions, "VARIABLES_AS_BANDS",
    8509          34 :                          false) &&
    8510           6 :             oMap2DDimsToGroupAndVar.size() == 1)
    8511             :         {
    8512           6 :             std::tie(nGroupID, nVarID) =
    8513          12 :                 oMap2DDimsToGroupAndVar.begin()->second.front();
    8514           6 :             bSeveralVariablesAsBands = true;
    8515             :         }
    8516             :         else
    8517             :         {
    8518          22 :             poDS->CreateSubDatasetList(cdfid);
    8519          22 :             poDS->GDALPamDataset::SetMetadata(poDS->papszMetadata);
    8520          22 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8521             :                 // deadlock with GDALDataset own mutex.
    8522          22 :             poDS->TryLoadXML();
    8523          22 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8524          22 :             return poDS;
    8525             :         }
    8526             :     }
    8527             : 
    8528             :     // If we are not treating things as a subdataset, then capture
    8529             :     // the name of the single available variable as the subdataset.
    8530         352 :     if (!bTreatAsSubdataset)
    8531             :     {
    8532         292 :         char *pszVarName = nullptr;
    8533         292 :         NCDF_ERR(NCDFGetVarFullName(nGroupID, nVarID, &pszVarName));
    8534         292 :         osSubdatasetName = (pszVarName != nullptr ? pszVarName : "");
    8535         292 :         CPLFree(pszVarName);
    8536             :     }
    8537             : 
    8538             :     // We have ignored at least one variable, so we should report them
    8539             :     // as subdatasets for reference.
    8540         352 :     if (nIgnoredVars > 0 && !bTreatAsSubdataset)
    8541             :     {
    8542          24 :         CPLDebug("GDAL_netCDF",
    8543             :                  "As %d variables were ignored, creating subdataset list "
    8544             :                  "for reference. Variable #%d [%s] is the main variable",
    8545             :                  nIgnoredVars, nVarID, osSubdatasetName.c_str());
    8546          24 :         poDS->CreateSubDatasetList(cdfid);
    8547             :     }
    8548             : 
    8549             :     // Open the NETCDF subdataset NETCDF:"filename":subdataset.
    8550         352 :     int var = -1;
    8551         352 :     NCDFOpenSubDataset(cdfid, osSubdatasetName.c_str(), &nGroupID, &var);
    8552             :     // Now we can forget the root cdfid and only use the selected group.
    8553         352 :     cdfid = nGroupID;
    8554         352 :     int nd = 0;
    8555         352 :     nc_inq_varndims(cdfid, var, &nd);
    8556             : 
    8557         352 :     poDS->m_anDimIds.resize(nd);
    8558             : 
    8559             :     // X, Y, Z position in array
    8560         704 :     std::vector<int> anBandDimPos(nd);
    8561             : 
    8562         352 :     nc_inq_vardimid(cdfid, var, poDS->m_anDimIds.data());
    8563             : 
    8564             :     // Check if somebody tried to pass a variable with less than 1D.
    8565         352 :     if (nd < 1)
    8566             :     {
    8567           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8568             :                  "Variable has %d dimension(s) - not supported.", nd);
    8569           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8570             :             // with GDALDataset own mutex.
    8571           0 :         delete poDS;
    8572           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8573           0 :         return nullptr;
    8574             :     }
    8575             : 
    8576             :     // CF-1 Convention
    8577             :     //
    8578             :     // Dimensions to appear in the relative order T, then Z, then Y,
    8579             :     // then X  to the file. All other dimensions should, whenever
    8580             :     // possible, be placed to the left of the spatiotemporal
    8581             :     // dimensions.
    8582             : 
    8583             :     // Verify that dimensions are in the {T,Z,Y,X} or {T,Z,Y,X} order
    8584             :     // Ideally we should detect for other ordering and act accordingly
    8585             :     // Only done if file has Conventions=CF-* and only prints warning
    8586             :     // To disable set GDAL_NETCDF_VERIFY_DIMS=NO and to use only
    8587             :     // attributes (not varnames) set GDAL_NETCDF_VERIFY_DIMS=STRICT
    8588             :     const bool bCheckDims =
    8589         704 :         CPLTestBool(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES")) &&
    8590         352 :         STARTS_WITH_CI(szConventions, "CF");
    8591             : 
    8592         352 :     if (nd >= 2 && bCheckDims)
    8593             :     {
    8594         267 :         char szDimName1[NC_MAX_NAME + 1] = {};
    8595         267 :         char szDimName2[NC_MAX_NAME + 1] = {};
    8596         267 :         status = nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 1], szDimName1);
    8597         267 :         NCDF_ERR(status);
    8598         267 :         status = nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 2], szDimName2);
    8599         267 :         NCDF_ERR(status);
    8600         425 :         if (NCDFIsVarLongitude(cdfid, -1, szDimName1) == false &&
    8601         158 :             NCDFIsVarProjectionX(cdfid, -1, szDimName1) == false)
    8602             :         {
    8603           4 :             CPLError(CE_Warning, CPLE_AppDefined,
    8604             :                      "dimension #%d (%s) is not a Longitude/X dimension.",
    8605             :                      nd - 1, szDimName1);
    8606             :         }
    8607         425 :         if (NCDFIsVarLatitude(cdfid, -1, szDimName2) == false &&
    8608         158 :             NCDFIsVarProjectionY(cdfid, -1, szDimName2) == false)
    8609             :         {
    8610           4 :             CPLError(CE_Warning, CPLE_AppDefined,
    8611             :                      "dimension #%d (%s) is not a Latitude/Y dimension.",
    8612             :                      nd - 2, szDimName2);
    8613             :         }
    8614         267 :         if ((NCDFIsVarLongitude(cdfid, -1, szDimName2) ||
    8615         269 :              NCDFIsVarProjectionX(cdfid, -1, szDimName2)) &&
    8616           2 :             (NCDFIsVarLatitude(cdfid, -1, szDimName1) ||
    8617           0 :              NCDFIsVarProjectionY(cdfid, -1, szDimName1)))
    8618             :         {
    8619           2 :             poDS->bSwitchedXY = true;
    8620             :         }
    8621         267 :         if (nd >= 3)
    8622             :         {
    8623          52 :             char szDimName3[NC_MAX_NAME + 1] = {};
    8624             :             status =
    8625          52 :                 nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 3], szDimName3);
    8626          52 :             NCDF_ERR(status);
    8627          52 :             if (nd >= 4)
    8628             :             {
    8629          13 :                 char szDimName4[NC_MAX_NAME + 1] = {};
    8630             :                 status =
    8631          13 :                     nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 4], szDimName4);
    8632          13 :                 NCDF_ERR(status);
    8633          13 :                 if (NCDFIsVarVerticalCoord(cdfid, -1, szDimName3) == false)
    8634             :                 {
    8635           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    8636             :                              "dimension #%d (%s) is not a Vertical dimension.",
    8637             :                              nd - 3, szDimName3);
    8638             :                 }
    8639          13 :                 if (NCDFIsVarTimeCoord(cdfid, -1, szDimName4) == false)
    8640             :                 {
    8641           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    8642             :                              "dimension #%d (%s) is not a Time dimension.",
    8643             :                              nd - 4, szDimName4);
    8644             :                 }
    8645             :             }
    8646             :             else
    8647             :             {
    8648          75 :                 if (NCDFIsVarVerticalCoord(cdfid, -1, szDimName3) == false &&
    8649          36 :                     NCDFIsVarTimeCoord(cdfid, -1, szDimName3) == false)
    8650             :                 {
    8651           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    8652             :                              "dimension #%d (%s) is not a "
    8653             :                              "Time or Vertical dimension.",
    8654             :                              nd - 3, szDimName3);
    8655             :                 }
    8656             :             }
    8657             :         }
    8658             :     }
    8659             : 
    8660             :     // For example for EMIT data (https://earth.jpl.nasa.gov/emit/data/data-portal/coverage-and-forecasts/),
    8661             :     // dimension order is downtrack, crosstrack, bands
    8662         352 :     bool bYXBandOrder = false;
    8663         352 :     if (nd == 3)
    8664             :     {
    8665          44 :         char szDimName[NC_MAX_NAME + 1] = {};
    8666          44 :         status = nc_inq_dimname(cdfid, poDS->m_anDimIds[2], szDimName);
    8667          44 :         NCDF_ERR(status);
    8668          44 :         bYXBandOrder =
    8669          44 :             strcmp(szDimName, "bands") == 0 || strcmp(szDimName, "band") == 0;
    8670             :     }
    8671             : 
    8672             :     // Get X dimensions information.
    8673             :     size_t xdim;
    8674         352 :     poDS->nXDimID = poDS->m_anDimIds[bYXBandOrder ? 1 : nd - 1];
    8675         352 :     nc_inq_dimlen(cdfid, poDS->nXDimID, &xdim);
    8676             : 
    8677             :     // Get Y dimension information.
    8678             :     size_t ydim;
    8679         352 :     if (nd >= 2)
    8680             :     {
    8681         348 :         poDS->nYDimID = poDS->m_anDimIds[bYXBandOrder ? 0 : nd - 2];
    8682         348 :         nc_inq_dimlen(cdfid, poDS->nYDimID, &ydim);
    8683             :     }
    8684             :     else
    8685             :     {
    8686           4 :         poDS->nYDimID = -1;
    8687           4 :         ydim = 1;
    8688             :     }
    8689             : 
    8690         352 :     if (xdim > INT_MAX || ydim > INT_MAX)
    8691             :     {
    8692           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    8693             :                  "Invalid raster dimensions: " CPL_FRMT_GUIB "x" CPL_FRMT_GUIB,
    8694             :                  static_cast<GUIntBig>(xdim), static_cast<GUIntBig>(ydim));
    8695           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8696             :             // with GDALDataset own mutex.
    8697           0 :         delete poDS;
    8698           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8699           0 :         return nullptr;
    8700             :     }
    8701             : 
    8702         352 :     poDS->nRasterXSize = static_cast<int>(xdim);
    8703         352 :     poDS->nRasterYSize = static_cast<int>(ydim);
    8704             : 
    8705         352 :     unsigned int k = 0;
    8706        1131 :     for (int j = 0; j < nd; j++)
    8707             :     {
    8708         779 :         if (poDS->m_anDimIds[j] == poDS->nXDimID)
    8709             :         {
    8710         352 :             anBandDimPos[0] = j;  // Save Position of XDim
    8711         352 :             k++;
    8712             :         }
    8713         779 :         if (poDS->m_anDimIds[j] == poDS->nYDimID)
    8714             :         {
    8715         348 :             anBandDimPos[1] = j;  // Save Position of YDim
    8716         348 :             k++;
    8717             :         }
    8718             :     }
    8719             :     // X and Y Dimension Ids were not found!
    8720         352 :     if ((nd >= 2 && k != 2) || (nd == 1 && k != 1))
    8721             :     {
    8722           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8723             :             // with GDALDataset own mutex.
    8724           0 :         delete poDS;
    8725           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8726           0 :         return nullptr;
    8727             :     }
    8728             : 
    8729             :     // Read Metadata for this variable.
    8730             : 
    8731             :     // Should disable as is also done at band level, except driver needs the
    8732             :     // variables as metadata (e.g. projection).
    8733         352 :     poDS->ReadAttributes(cdfid, var);
    8734             : 
    8735             :     // Read Metadata for each dimension.
    8736         352 :     int *panDimIds = nullptr;
    8737         352 :     NCDFGetVisibleDims(cdfid, &ndims, &panDimIds);
    8738             :     // With NetCDF-4 groups panDimIds is not always [0..dim_count-1] like
    8739             :     // in NetCDF-3 because we see only the dimensions of the selected group
    8740             :     // and its parents.
    8741             :     // poDS->papszDimName is indexed by dim IDs, so it must contains all IDs
    8742             :     // [0..max(panDimIds)], but they are not all useful so we fill names
    8743             :     // of useless dims with empty string.
    8744         352 :     if (panDimIds)
    8745             :     {
    8746         352 :         const int nMaxDimId = *std::max_element(panDimIds, panDimIds + ndims);
    8747         352 :         std::set<int> oSetExistingDimIds;
    8748        1171 :         for (int i = 0; i < ndims; i++)
    8749             :         {
    8750         819 :             oSetExistingDimIds.insert(panDimIds[i]);
    8751             :         }
    8752         352 :         std::set<int> oSetDimIdsUsedByVar;
    8753        1131 :         for (int i = 0; i < nd; i++)
    8754             :         {
    8755         779 :             oSetDimIdsUsedByVar.insert(poDS->m_anDimIds[i]);
    8756             :         }
    8757        1173 :         for (int j = 0; j <= nMaxDimId; j++)
    8758             :         {
    8759             :             // Is j dim used?
    8760         821 :             if (oSetExistingDimIds.find(j) != oSetExistingDimIds.end())
    8761             :             {
    8762             :                 // Useful dim.
    8763         819 :                 char szTemp[NC_MAX_NAME + 1] = {};
    8764         819 :                 status = nc_inq_dimname(cdfid, j, szTemp);
    8765         819 :                 if (status != NC_NOERR)
    8766             :                 {
    8767           0 :                     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8768             :                         // deadlock with GDALDataset own
    8769             :                         // mutex.
    8770           0 :                     delete poDS;
    8771           0 :                     CPLAcquireMutex(hNCMutex, 1000.0);
    8772           0 :                     return nullptr;
    8773             :                 }
    8774         819 :                 poDS->papszDimName.AddString(szTemp);
    8775             : 
    8776         819 :                 if (oSetDimIdsUsedByVar.find(j) != oSetDimIdsUsedByVar.end())
    8777             :                 {
    8778         779 :                     int nDimGroupId = -1;
    8779         779 :                     int nDimVarId = -1;
    8780         779 :                     if (NCDFResolveVar(cdfid, poDS->papszDimName[j],
    8781         779 :                                        &nDimGroupId, &nDimVarId) == CE_None)
    8782             :                     {
    8783         579 :                         poDS->ReadAttributes(nDimGroupId, nDimVarId);
    8784             :                     }
    8785             :                 }
    8786             :             }
    8787             :             else
    8788             :             {
    8789             :                 // Useless dim.
    8790           2 :                 poDS->papszDimName.AddString("");
    8791             :             }
    8792             :         }
    8793         352 :         CPLFree(panDimIds);
    8794             :     }
    8795             : 
    8796             :     // Set projection info.
    8797         704 :     std::vector<std::string> aosRemovedMDItems;
    8798         352 :     if (nd > 1)
    8799             :     {
    8800         348 :         poDS->SetProjectionFromVar(cdfid, var,
    8801             :                                    /*bReadSRSOnly=*/false,
    8802             :                                    /* pszGivenGM = */ nullptr,
    8803             :                                    /* returnProjStr = */ nullptr,
    8804             :                                    /* sg = */ nullptr, &aosRemovedMDItems);
    8805             :     }
    8806             : 
    8807             :     // Override bottom-up with GDAL_NETCDF_BOTTOMUP config option.
    8808         352 :     const char *pszValue = CPLGetConfigOption("GDAL_NETCDF_BOTTOMUP", nullptr);
    8809         352 :     if (pszValue)
    8810             :     {
    8811          24 :         poDS->bBottomUp = CPLTestBool(pszValue);
    8812          24 :         CPLDebug("GDAL_netCDF",
    8813             :                  "set bBottomUp=%d because GDAL_NETCDF_BOTTOMUP=%s",
    8814          24 :                  static_cast<int>(poDS->bBottomUp), pszValue);
    8815             :     }
    8816             : 
    8817             :     // Save non-spatial dimension info.
    8818             : 
    8819         352 :     int *panBandZLev = nullptr;
    8820         352 :     int nDim = (nd >= 2) ? 2 : 1;
    8821             :     size_t lev_count;
    8822         352 :     size_t nTotLevCount = 1;
    8823         352 :     nc_type nType = NC_NAT;
    8824             : 
    8825         704 :     CPLString osExtraDimNames;
    8826             : 
    8827         352 :     if (nd > 2)
    8828             :     {
    8829          60 :         nDim = 2;
    8830          60 :         panBandZLev = static_cast<int *>(CPLCalloc(nd - 2, sizeof(int)));
    8831             : 
    8832          60 :         osExtraDimNames = "{";
    8833             : 
    8834          60 :         char szDimName[NC_MAX_NAME + 1] = {};
    8835             : 
    8836          60 :         bool bREPORT_EXTRA_DIM_VALUESWarningEmitted = false;
    8837         259 :         for (int j = 0; j < nd; j++)
    8838             :         {
    8839         338 :             if ((poDS->m_anDimIds[j] != poDS->nXDimID) &&
    8840         139 :                 (poDS->m_anDimIds[j] != poDS->nYDimID))
    8841             :             {
    8842          79 :                 nc_inq_dimlen(cdfid, poDS->m_anDimIds[j], &lev_count);
    8843          79 :                 nTotLevCount *= lev_count;
    8844          79 :                 panBandZLev[nDim - 2] = static_cast<int>(lev_count);
    8845          79 :                 anBandDimPos[nDim] = j;  // Save Position of ZDim
    8846             :                 // Save non-spatial dimension names.
    8847          79 :                 if (nc_inq_dimname(cdfid, poDS->m_anDimIds[j], szDimName) ==
    8848             :                     NC_NOERR)
    8849             :                 {
    8850          79 :                     osExtraDimNames += szDimName;
    8851          79 :                     if (j < nd - 3)
    8852             :                     {
    8853          19 :                         osExtraDimNames += ",";
    8854             :                     }
    8855             : 
    8856          79 :                     int nIdxGroupID = -1;
    8857          79 :                     int nIdxVarID = Get1DVariableIndexedByDimension(
    8858          79 :                         cdfid, poDS->m_anDimIds[j], szDimName, true,
    8859          79 :                         &nIdxGroupID);
    8860          79 :                     poDS->m_anExtraDimGroupIds.push_back(nIdxGroupID);
    8861          79 :                     poDS->m_anExtraDimVarIds.push_back(nIdxVarID);
    8862             : 
    8863          79 :                     if (nIdxVarID >= 0)
    8864             :                     {
    8865          70 :                         nc_inq_vartype(nIdxGroupID, nIdxVarID, &nType);
    8866             :                         char szExtraDimDef[NC_MAX_NAME + 1];
    8867          70 :                         snprintf(szExtraDimDef, sizeof(szExtraDimDef),
    8868             :                                  "{%ld,%d}", (long)lev_count, nType);
    8869             :                         char szTemp[NC_MAX_NAME + 32 + 1];
    8870          70 :                         snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_DEF",
    8871             :                                  szDimName);
    8872          70 :                         poDS->papszMetadata = CSLSetNameValue(
    8873             :                             poDS->papszMetadata, szTemp, szExtraDimDef);
    8874             : 
    8875             :                         // Retrieving data for unlimited dimensions might be
    8876             :                         // costly on network storage, so don't do it.
    8877             :                         // Each band will capture the value along the extra
    8878             :                         // dimension in its NETCDF_DIM_xxxx band metadata item
    8879             :                         // Addresses use case of
    8880             :                         // https://lists.osgeo.org/pipermail/gdal-dev/2023-May/057209.html
    8881             :                         const bool bIsLocal =
    8882          70 :                             VSIIsLocal(osFilenameForNCOpen.c_str());
    8883             :                         bool bListDimValues =
    8884          71 :                             bIsLocal || lev_count == 1 ||
    8885           1 :                             !NCDFIsUnlimitedDim(poDS->eFormat ==
    8886             :                                                     NCDF_FORMAT_NC4,
    8887           1 :                                                 cdfid, poDS->m_anDimIds[j]);
    8888             :                         const char *pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES =
    8889          70 :                             CPLGetConfigOption(
    8890             :                                 "GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES", nullptr);
    8891          70 :                         if (pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES)
    8892             :                         {
    8893           2 :                             bListDimValues = CPLTestBool(
    8894             :                                 pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES);
    8895             :                         }
    8896          68 :                         else if (!bListDimValues && !bIsLocal &&
    8897           1 :                                  !bREPORT_EXTRA_DIM_VALUESWarningEmitted)
    8898             :                         {
    8899           1 :                             bREPORT_EXTRA_DIM_VALUESWarningEmitted = true;
    8900           1 :                             CPLDebug(
    8901             :                                 "GDAL_netCDF",
    8902             :                                 "Listing extra dimension values is skipped "
    8903             :                                 "because this dataset is hosted on a network "
    8904             :                                 "file system, and such an operation could be "
    8905             :                                 "slow. If you still want to proceed, set the "
    8906             :                                 "GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES "
    8907             :                                 "configuration option to YES");
    8908             :                         }
    8909          70 :                         if (bListDimValues)
    8910             :                         {
    8911          68 :                             char *pszTemp = nullptr;
    8912          68 :                             if (NCDFGet1DVar(nIdxGroupID, nIdxVarID,
    8913          68 :                                              &pszTemp) == CE_None)
    8914             :                             {
    8915          68 :                                 snprintf(szTemp, sizeof(szTemp),
    8916             :                                          "NETCDF_DIM_%s_VALUES", szDimName);
    8917          68 :                                 poDS->papszMetadata = CSLSetNameValue(
    8918             :                                     poDS->papszMetadata, szTemp, pszTemp);
    8919          68 :                                 CPLFree(pszTemp);
    8920             :                             }
    8921             :                         }
    8922             :                     }
    8923             :                 }
    8924             :                 else
    8925             :                 {
    8926           0 :                     poDS->m_anExtraDimGroupIds.push_back(-1);
    8927           0 :                     poDS->m_anExtraDimVarIds.push_back(-1);
    8928             :                 }
    8929             : 
    8930          79 :                 nDim++;
    8931             :             }
    8932             :         }
    8933          60 :         osExtraDimNames += "}";
    8934          60 :         poDS->papszMetadata = CSLSetNameValue(
    8935             :             poDS->papszMetadata, "NETCDF_DIM_EXTRA", osExtraDimNames);
    8936             :     }
    8937             : 
    8938             :     // Store Metadata.
    8939         362 :     for (const auto &osStr : aosRemovedMDItems)
    8940          10 :         poDS->papszMetadata =
    8941          10 :             CSLSetNameValue(poDS->papszMetadata, osStr.c_str(), nullptr);
    8942             : 
    8943         352 :     poDS->GDALPamDataset::SetMetadata(poDS->papszMetadata);
    8944             : 
    8945             :     // Create bands.
    8946             : 
    8947             :     // Arbitrary threshold.
    8948             :     int nMaxBandCount =
    8949         352 :         atoi(CPLGetConfigOption("GDAL_MAX_BAND_COUNT", "32768"));
    8950         352 :     if (nMaxBandCount <= 0)
    8951           0 :         nMaxBandCount = 32768;
    8952         352 :     if (nTotLevCount > static_cast<unsigned int>(nMaxBandCount))
    8953             :     {
    8954           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8955             :                  "Limiting number of bands to %d instead of %u", nMaxBandCount,
    8956             :                  static_cast<unsigned int>(nTotLevCount));
    8957           0 :         nTotLevCount = static_cast<unsigned int>(nMaxBandCount);
    8958             :     }
    8959         352 :     if (poDS->nRasterXSize == 0 || poDS->nRasterYSize == 0)
    8960             :     {
    8961           0 :         poDS->nRasterXSize = 0;
    8962           0 :         poDS->nRasterYSize = 0;
    8963           0 :         nTotLevCount = 0;
    8964           0 :         if (poDS->GetLayerCount() == 0)
    8965             :         {
    8966           0 :             CPLFree(panBandZLev);
    8967           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8968             :                 // deadlock with GDALDataset own mutex.
    8969           0 :             delete poDS;
    8970           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8971           0 :             return nullptr;
    8972             :         }
    8973             :     }
    8974         352 :     if (bSeveralVariablesAsBands)
    8975             :     {
    8976           6 :         const auto &listVariables = oMap2DDimsToGroupAndVar.begin()->second;
    8977          24 :         for (int iBand = 0; iBand < static_cast<int>(listVariables.size());
    8978             :              ++iBand)
    8979             :         {
    8980          18 :             int bandVarGroupId = listVariables[iBand].first;
    8981          18 :             int bandVarId = listVariables[iBand].second;
    8982             :             netCDFRasterBand *poBand = new netCDFRasterBand(
    8983           0 :                 netCDFRasterBand::CONSTRUCTOR_OPEN(), poDS, bandVarGroupId,
    8984          18 :                 bandVarId, nDim, 0, nullptr, anBandDimPos.data(), iBand + 1);
    8985          18 :             poDS->SetBand(iBand + 1, poBand);
    8986             :         }
    8987             :     }
    8988             :     else
    8989             :     {
    8990         800 :         for (unsigned int lev = 0; lev < nTotLevCount; lev++)
    8991             :         {
    8992             :             netCDFRasterBand *poBand = new netCDFRasterBand(
    8993           0 :                 netCDFRasterBand::CONSTRUCTOR_OPEN(), poDS, cdfid, var, nDim,
    8994         454 :                 lev, panBandZLev, anBandDimPos.data(), lev + 1);
    8995         454 :             poDS->SetBand(lev + 1, poBand);
    8996             :         }
    8997             :     }
    8998             : 
    8999         352 :     if (panBandZLev)
    9000          60 :         CPLFree(panBandZLev);
    9001             :     // Handle angular geographic coordinates here
    9002             : 
    9003             :     // Initialize any PAM information.
    9004         352 :     if (bTreatAsSubdataset)
    9005             :     {
    9006          60 :         poDS->SetPhysicalFilename(poDS->osFilename);
    9007          60 :         poDS->SetSubdatasetName(osSubdatasetName);
    9008             :     }
    9009             : 
    9010         352 :     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock with
    9011             :         // GDALDataset own mutex.
    9012         352 :     poDS->TryLoadXML();
    9013             : 
    9014         352 :     if (bTreatAsSubdataset)
    9015          60 :         poDS->oOvManager.Initialize(poDS, ":::VIRTUAL:::");
    9016             :     else
    9017         292 :         poDS->oOvManager.Initialize(poDS, poDS->osFilename);
    9018             : 
    9019         352 :     CPLAcquireMutex(hNCMutex, 1000.0);
    9020             : 
    9021         352 :     return poDS;
    9022             : }
    9023             : 
    9024             : /************************************************************************/
    9025             : /*                            CopyMetadata()                            */
    9026             : /*                                                                      */
    9027             : /*      Create a copy of metadata for NC_GLOBAL or a variable           */
    9028             : /************************************************************************/
    9029             : 
    9030         157 : static void CopyMetadata(GDALDataset *poSrcDS, GDALRasterBand *poSrcBand,
    9031             :                          GDALRasterBand *poDstBand, int nCdfId, int CDFVarID,
    9032             :                          const char *pszPrefix)
    9033             : {
    9034             :     // Remove the following band meta but set them later from band data.
    9035         157 :     const char *const papszIgnoreBand[] = {
    9036             :         CF_ADD_OFFSET,  CF_SCALE_FACTOR, "valid_range", "_Unsigned",
    9037             :         NCDF_FillValue, "coordinates",   nullptr};
    9038         157 :     const char *const papszIgnoreGlobal[] = {"NETCDF_DIM_EXTRA", nullptr};
    9039             : 
    9040         157 :     CSLConstList papszMetadata = nullptr;
    9041         157 :     if (poSrcDS)
    9042             :     {
    9043          66 :         papszMetadata = poSrcDS->GetMetadata();
    9044             :     }
    9045          91 :     else if (poSrcBand)
    9046             :     {
    9047          91 :         papszMetadata = poSrcBand->GetMetadata();
    9048             :     }
    9049             : 
    9050         637 :     for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(papszMetadata))
    9051             :     {
    9052             : #ifdef NCDF_DEBUG
    9053             :         CPLDebug("GDAL_netCDF", "copy metadata [%s]=[%s]", pszKey, pszValue);
    9054             : #endif
    9055             : 
    9056         480 :         CPLString osMetaName(pszKey);
    9057             : 
    9058             :         // Check for items that match pszPrefix if applicable.
    9059         480 :         if (pszPrefix && !EQUAL(pszPrefix, ""))
    9060             :         {
    9061             :             // Remove prefix.
    9062         115 :             if (STARTS_WITH(osMetaName.c_str(), pszPrefix))
    9063             :             {
    9064          17 :                 osMetaName = osMetaName.substr(strlen(pszPrefix));
    9065             :             }
    9066             :             // Only copy items that match prefix.
    9067             :             else
    9068             :             {
    9069          98 :                 continue;
    9070             :             }
    9071             :         }
    9072             : 
    9073             :         // Fix various issues with metadata translation.
    9074         382 :         if (CDFVarID == NC_GLOBAL)
    9075             :         {
    9076             :             // Do not copy items in papszIgnoreGlobal and NETCDF_DIM_*.
    9077         481 :             if ((CSLFindString(papszIgnoreGlobal, osMetaName) != -1) ||
    9078         238 :                 (STARTS_WITH(osMetaName, "NETCDF_DIM_")))
    9079          21 :                 continue;
    9080             :             // Remove NC_GLOBAL prefix for netcdf global Metadata.
    9081         222 :             else if (STARTS_WITH(osMetaName, "NC_GLOBAL#"))
    9082             :             {
    9083          33 :                 osMetaName = osMetaName.substr(strlen("NC_GLOBAL#"));
    9084             :             }
    9085             :             // GDAL Metadata renamed as GDAL-[meta].
    9086         189 :             else if (strstr(osMetaName, "#") == nullptr)
    9087             :             {
    9088          16 :                 osMetaName = "GDAL_" + osMetaName;
    9089             :             }
    9090             :             // Keep time, lev and depth information for safe-keeping.
    9091             :             // Time and vertical coordinate handling need improvements.
    9092             :             /*
    9093             :             else if( STARTS_WITH(szMetaName, "time#") )
    9094             :             {
    9095             :                 szMetaName[4] = '-';
    9096             :             }
    9097             :             else if( STARTS_WITH(szMetaName, "lev#") )
    9098             :             {
    9099             :                 szMetaName[3] = '-';
    9100             :             }
    9101             :             else if( STARTS_WITH(szMetaName, "depth#") )
    9102             :             {
    9103             :                 szMetaName[5] = '-';
    9104             :             }
    9105             :             */
    9106             :             // Only copy data without # (previously all data was copied).
    9107         222 :             if (strstr(osMetaName, "#") != nullptr)
    9108         173 :                 continue;
    9109             :             // netCDF attributes do not like the '#' character.
    9110             :             // for( unsigned int h=0; h < strlen(szMetaName) -1 ; h++ ) {
    9111             :             //     if( szMetaName[h] == '#') szMetaName[h] = '-';
    9112             :             // }
    9113             :         }
    9114             :         else
    9115             :         {
    9116             :             // Do not copy varname, stats, NETCDF_DIM_*, nodata
    9117             :             // and items in papszIgnoreBand.
    9118         139 :             if (STARTS_WITH(osMetaName, "NETCDF_VARNAME") ||
    9119         107 :                 STARTS_WITH(osMetaName, "STATISTICS_") ||
    9120         107 :                 STARTS_WITH(osMetaName, "NETCDF_DIM_") ||
    9121          74 :                 STARTS_WITH(osMetaName, "missing_value") ||
    9122         293 :                 STARTS_WITH(osMetaName, "_FillValue") ||
    9123          47 :                 CSLFindString(papszIgnoreBand, osMetaName) != -1)
    9124          97 :                 continue;
    9125             :         }
    9126             : 
    9127             : #ifdef NCDF_DEBUG
    9128             :         CPLDebug("GDAL_netCDF", "copy name=[%s] value=[%s]", osMetaName.c_str(),
    9129             :                  pszValue);
    9130             : #endif
    9131          91 :         if (NCDFPutAttr(nCdfId, CDFVarID, osMetaName, pszValue) != CE_None)
    9132             :         {
    9133           0 :             CPLDebug("GDAL_netCDF", "NCDFPutAttr(%d, %d, %s, %s) failed",
    9134             :                      nCdfId, CDFVarID, osMetaName.c_str(), pszValue);
    9135             :         }
    9136             :     }
    9137             : 
    9138             :     // Set add_offset and scale_factor here if present.
    9139         157 :     if (poSrcBand && poDstBand)
    9140             :     {
    9141             : 
    9142          91 :         int bGotAddOffset = FALSE;
    9143          91 :         const double dfAddOffset = poSrcBand->GetOffset(&bGotAddOffset);
    9144          91 :         int bGotScale = FALSE;
    9145          91 :         const double dfScale = poSrcBand->GetScale(&bGotScale);
    9146             : 
    9147          91 :         if (bGotAddOffset && dfAddOffset != 0.0)
    9148           1 :             poDstBand->SetOffset(dfAddOffset);
    9149          91 :         if (bGotScale && dfScale != 1.0)
    9150           1 :             poDstBand->SetScale(dfScale);
    9151             :     }
    9152         157 : }
    9153             : 
    9154             : /************************************************************************/
    9155             : /*                            CreateLL()                                */
    9156             : /*                                                                      */
    9157             : /*      Shared functionality between netCDFDataset::Create() and        */
    9158             : /*      netCDF::CreateCopy() for creating netcdf file based on a set of */
    9159             : /*      options and a configuration.                                    */
    9160             : /************************************************************************/
    9161             : 
    9162         198 : netCDFDataset *netCDFDataset::CreateLL(const char *pszFilename, int nXSize,
    9163             :                                        int nYSize, int nBandsIn,
    9164             :                                        char **papszOptions)
    9165             : {
    9166         198 :     if (!((nXSize == 0 && nYSize == 0 && nBandsIn == 0) ||
    9167         126 :           (nXSize > 0 && nYSize > 0 && nBandsIn > 0)))
    9168             :     {
    9169           1 :         return nullptr;
    9170             :     }
    9171             : 
    9172         197 :     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock with
    9173             :         // GDALDataset own mutex.
    9174         197 :     netCDFDataset *poDS = new netCDFDataset();
    9175         197 :     CPLAcquireMutex(hNCMutex, 1000.0);
    9176             : 
    9177         197 :     poDS->nRasterXSize = nXSize;
    9178         197 :     poDS->nRasterYSize = nYSize;
    9179         197 :     poDS->eAccess = GA_Update;
    9180         197 :     poDS->osFilename = pszFilename;
    9181             : 
    9182             :     // From gtiff driver, is this ok?
    9183             :     /*
    9184             :     poDS->nBlockXSize = nXSize;
    9185             :     poDS->nBlockYSize = 1;
    9186             :     poDS->nBlocksPerBand =
    9187             :         DIV_ROUND_UP((nYSize, poDS->nBlockYSize))
    9188             :         * DIV_ROUND_UP((nXSize, poDS->nBlockXSize));
    9189             :         */
    9190             : 
    9191             :     // process options.
    9192         197 :     poDS->papszCreationOptions = CSLDuplicate(papszOptions);
    9193         197 :     poDS->ProcessCreationOptions();
    9194             : 
    9195         197 :     if (poDS->eMultipleLayerBehavior == SEPARATE_FILES)
    9196             :     {
    9197             :         VSIStatBuf sStat;
    9198           2 :         if (VSIStat(pszFilename, &sStat) == 0)
    9199             :         {
    9200           0 :             if (!VSI_ISDIR(sStat.st_mode))
    9201             :             {
    9202           0 :                 CPLError(CE_Failure, CPLE_FileIO,
    9203             :                          "%s is an existing file, but not a directory",
    9204             :                          pszFilename);
    9205           0 :                 CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9206             :                     // deadlock with GDALDataset own
    9207             :                     // mutex.
    9208           0 :                 delete poDS;
    9209           0 :                 CPLAcquireMutex(hNCMutex, 1000.0);
    9210           0 :                 return nullptr;
    9211             :             }
    9212             :         }
    9213           2 :         else if (VSIMkdir(pszFilename, 0755) != 0)
    9214             :         {
    9215           1 :             CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s directory",
    9216             :                      pszFilename);
    9217           1 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9218             :                 // deadlock with GDALDataset own mutex.
    9219           1 :             delete poDS;
    9220           1 :             CPLAcquireMutex(hNCMutex, 1000.0);
    9221           1 :             return nullptr;
    9222             :         }
    9223             : 
    9224           1 :         return poDS;
    9225             :     }
    9226             :     // Create the dataset.
    9227         390 :     CPLString osFilenameForNCCreate(pszFilename);
    9228             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    9229             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    9230             :     {
    9231             :         char *pszTemp =
    9232             :             CPLRecode(osFilenameForNCCreate, CPL_ENC_UTF8, "CP_ACP");
    9233             :         osFilenameForNCCreate = pszTemp;
    9234             :         CPLFree(pszTemp);
    9235             :     }
    9236             : #endif
    9237             : 
    9238             : #if defined(_WIN32)
    9239             :     {
    9240             :         // Works around bug of msys2 netCDF 4.9.0 package where nc_create()
    9241             :         // crashes
    9242             :         VSIStatBuf sStat;
    9243             :         const std::string osDirname =
    9244             :             CPLGetDirnameSafe(osFilenameForNCCreate.c_str());
    9245             :         if (VSIStat(osDirname.c_str(), &sStat) != 0)
    9246             :         {
    9247             :             CPLError(CE_Failure, CPLE_OpenFailed,
    9248             :                      "Unable to create netCDF file %s: non existing output "
    9249             :                      "directory",
    9250             :                      pszFilename);
    9251             :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9252             :                 // deadlock with GDALDataset own mutex.
    9253             :             delete poDS;
    9254             :             CPLAcquireMutex(hNCMutex, 1000.0);
    9255             :             return nullptr;
    9256             :         }
    9257             :     }
    9258             : #endif
    9259             : 
    9260             :     int status =
    9261         195 :         nc_create(osFilenameForNCCreate, poDS->nCreateMode, &(poDS->cdfid));
    9262             : 
    9263             :     // Put into define mode.
    9264         195 :     poDS->SetDefineMode(true);
    9265             : 
    9266         195 :     if (status != NC_NOERR)
    9267             :     {
    9268          30 :         CPLError(CE_Failure, CPLE_OpenFailed,
    9269             :                  "Unable to create netCDF file %s (Error code %d): %s .",
    9270             :                  pszFilename, status, nc_strerror(status));
    9271          30 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    9272             :             // with GDALDataset own mutex.
    9273          30 :         delete poDS;
    9274          30 :         CPLAcquireMutex(hNCMutex, 1000.0);
    9275          30 :         return nullptr;
    9276             :     }
    9277             : 
    9278             :     // Define dimensions.
    9279         165 :     if (nXSize > 0 && nYSize > 0)
    9280             :     {
    9281         112 :         poDS->papszDimName.AddString(NCDF_DIMNAME_X);
    9282             :         status =
    9283         112 :             nc_def_dim(poDS->cdfid, NCDF_DIMNAME_X, nXSize, &(poDS->nXDimID));
    9284         112 :         NCDF_ERR(status);
    9285         112 :         CPLDebug("GDAL_netCDF", "status nc_def_dim(%d, %s, %d, -) got id %d",
    9286             :                  poDS->cdfid, NCDF_DIMNAME_X, nXSize, poDS->nXDimID);
    9287             : 
    9288         112 :         poDS->papszDimName.AddString(NCDF_DIMNAME_Y);
    9289             :         status =
    9290         112 :             nc_def_dim(poDS->cdfid, NCDF_DIMNAME_Y, nYSize, &(poDS->nYDimID));
    9291         112 :         NCDF_ERR(status);
    9292         112 :         CPLDebug("GDAL_netCDF", "status nc_def_dim(%d, %s, %d, -) got id %d",
    9293             :                  poDS->cdfid, NCDF_DIMNAME_Y, nYSize, poDS->nYDimID);
    9294             :     }
    9295             : 
    9296         165 :     return poDS;
    9297             : }
    9298             : 
    9299             : /************************************************************************/
    9300             : /*                            Create()                                  */
    9301             : /************************************************************************/
    9302             : 
    9303         126 : GDALDataset *netCDFDataset::Create(const char *pszFilename, int nXSize,
    9304             :                                    int nYSize, int nBandsIn, GDALDataType eType,
    9305             :                                    char **papszOptions)
    9306             : {
    9307         126 :     CPLDebug("GDAL_netCDF", "\n=====\nnetCDFDataset::Create(%s, ...)",
    9308             :              pszFilename);
    9309             : 
    9310             :     const char *legacyCreationOp =
    9311         126 :         CSLFetchNameValueDef(papszOptions, "GEOMETRY_ENCODING", "CF_1.8");
    9312         252 :     std::string legacyCreationOp_s = std::string(legacyCreationOp);
    9313             : 
    9314             :     // Check legacy creation op FIRST
    9315             : 
    9316         126 :     bool legacyCreateMode = false;
    9317             : 
    9318         126 :     if (nXSize != 0 || nYSize != 0 || nBandsIn != 0)
    9319             :     {
    9320          56 :         legacyCreateMode = true;
    9321             :     }
    9322          70 :     else if (legacyCreationOp_s == "CF_1.8")
    9323             :     {
    9324          54 :         legacyCreateMode = false;
    9325             :     }
    9326             : 
    9327          16 :     else if (legacyCreationOp_s == "WKT")
    9328             :     {
    9329          16 :         legacyCreateMode = true;
    9330             :     }
    9331             : 
    9332             :     else
    9333             :     {
    9334           0 :         CPLError(
    9335             :             CE_Failure, CPLE_NotSupported,
    9336             :             "Dataset creation option GEOMETRY_ENCODING=%s is not supported.",
    9337             :             legacyCreationOp_s.c_str());
    9338           0 :         return nullptr;
    9339             :     }
    9340             : 
    9341         252 :     CPLStringList aosOptions(CSLDuplicate(papszOptions));
    9342         238 :     if (aosOptions.FetchNameValue("FORMAT") == nullptr &&
    9343         112 :         (eType == GDT_UInt16 || eType == GDT_UInt32 || eType == GDT_UInt64 ||
    9344             :          eType == GDT_Int64))
    9345             :     {
    9346          10 :         CPLDebug("netCDF", "Selecting FORMAT=NC4 due to data type");
    9347          10 :         aosOptions.SetNameValue("FORMAT", "NC4");
    9348             :     }
    9349             : 
    9350         252 :     CPLStringList aosBandNames;
    9351         126 :     if (const char *pszBandNames = aosOptions.FetchNameValue("BAND_NAMES"))
    9352             :     {
    9353             :         aosBandNames =
    9354           2 :             CSLTokenizeString2(pszBandNames, ",", CSLT_HONOURSTRINGS);
    9355             : 
    9356           2 :         if (aosBandNames.Count() != nBandsIn)
    9357             :         {
    9358           1 :             CPLError(CE_Failure, CPLE_OpenFailed,
    9359             :                      "Attempted to create netCDF with %d bands but %d names "
    9360             :                      "provided in BAND_NAMES.",
    9361             :                      nBandsIn, aosBandNames.Count());
    9362             : 
    9363           1 :             return nullptr;
    9364             :         }
    9365             :     }
    9366             : 
    9367         250 :     CPLMutexHolderD(&hNCMutex);
    9368             : 
    9369         125 :     auto poDS = netCDFDataset::CreateLL(pszFilename, nXSize, nYSize, nBandsIn,
    9370             :                                         aosOptions.List());
    9371             : 
    9372         125 :     if (!poDS)
    9373          19 :         return nullptr;
    9374             : 
    9375         106 :     if (!legacyCreateMode)
    9376             :     {
    9377          37 :         poDS->bSGSupport = true;
    9378          37 :         poDS->vcdf.enableFullVirtualMode();
    9379             :     }
    9380             : 
    9381             :     else
    9382             :     {
    9383          69 :         poDS->bSGSupport = false;
    9384             :     }
    9385             : 
    9386             :     // Should we write signed or unsigned byte?
    9387             :     // TODO should this only be done in Create()
    9388         106 :     poDS->bSignedData = true;
    9389         106 :     const char *pszValue = CSLFetchNameValueDef(papszOptions, "PIXELTYPE", "");
    9390         106 :     if (eType == GDT_Byte && !EQUAL(pszValue, "SIGNEDBYTE"))
    9391          15 :         poDS->bSignedData = false;
    9392             : 
    9393             :     // Add Conventions, GDAL info and history.
    9394         106 :     if (poDS->cdfid >= 0)
    9395             :     {
    9396             :         const char *CF_Vector_Conv =
    9397         173 :             poDS->bSGSupport ||
    9398             :                     // Use of variable length strings require CF-1.8
    9399          68 :                     EQUAL(aosOptions.FetchNameValueDef("FORMAT", ""), "NC4")
    9400             :                 ? NCDF_CONVENTIONS_CF_V1_8
    9401         173 :                 : NCDF_CONVENTIONS_CF_V1_6;
    9402         105 :         poDS->bWriteGDALVersion = CPLTestBool(
    9403             :             CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_VERSION", "YES"));
    9404         105 :         poDS->bWriteGDALHistory = CPLTestBool(
    9405             :             CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_HISTORY", "YES"));
    9406         105 :         NCDFAddGDALHistory(poDS->cdfid, pszFilename, poDS->bWriteGDALVersion,
    9407         105 :                            poDS->bWriteGDALHistory, "", "Create",
    9408             :                            (nBandsIn == 0) ? CF_Vector_Conv
    9409             :                                            : GDAL_DEFAULT_NCDF_CONVENTIONS);
    9410             :     }
    9411             : 
    9412             :     // Define bands.
    9413         197 :     for (int iBand = 1; iBand <= nBandsIn; iBand++)
    9414             :     {
    9415             :         const char *pszBandName =
    9416          91 :             aosBandNames.empty() ? nullptr : aosBandNames[iBand - 1];
    9417             : 
    9418          91 :         poDS->SetBand(iBand, new netCDFRasterBand(
    9419          91 :                                  netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS,
    9420          91 :                                  eType, iBand, poDS->bSignedData, pszBandName));
    9421             :     }
    9422             : 
    9423         106 :     CPLDebug("GDAL_netCDF", "netCDFDataset::Create(%s, ...) done", pszFilename);
    9424             :     // Return same dataset.
    9425         106 :     return poDS;
    9426             : }
    9427             : 
    9428             : template <class T>
    9429          91 : static CPLErr NCDFCopyBand(GDALRasterBand *poSrcBand, GDALRasterBand *poDstBand,
    9430             :                            int nXSize, int nYSize, GDALProgressFunc pfnProgress,
    9431             :                            void *pProgressData)
    9432             : {
    9433          91 :     GDALDataType eDT = poSrcBand->GetRasterDataType();
    9434          91 :     CPLErr eErr = CE_None;
    9435          91 :     T *patScanline = static_cast<T *>(CPLMalloc(nXSize * sizeof(T)));
    9436             : 
    9437        6308 :     for (int iLine = 0; iLine < nYSize && eErr == CE_None; iLine++)
    9438             :     {
    9439        6217 :         eErr = poSrcBand->RasterIO(GF_Read, 0, iLine, nXSize, 1, patScanline,
    9440             :                                    nXSize, 1, eDT, 0, 0, nullptr);
    9441        6217 :         if (eErr != CE_None)
    9442             :         {
    9443           0 :             CPLDebug(
    9444             :                 "GDAL_netCDF",
    9445             :                 "NCDFCopyBand(), poSrcBand->RasterIO() returned error code %d",
    9446             :                 eErr);
    9447             :         }
    9448             :         else
    9449             :         {
    9450        6217 :             eErr =
    9451             :                 poDstBand->RasterIO(GF_Write, 0, iLine, nXSize, 1, patScanline,
    9452             :                                     nXSize, 1, eDT, 0, 0, nullptr);
    9453        6217 :             if (eErr != CE_None)
    9454           0 :                 CPLDebug("GDAL_netCDF",
    9455             :                          "NCDFCopyBand(), poDstBand->RasterIO() returned error "
    9456             :                          "code %d",
    9457             :                          eErr);
    9458             :         }
    9459             : 
    9460        6217 :         if (nYSize > 10 && (iLine % (nYSize / 10) == 1))
    9461             :         {
    9462         277 :             if (!pfnProgress(1.0 * iLine / nYSize, nullptr, pProgressData))
    9463             :             {
    9464           0 :                 eErr = CE_Failure;
    9465           0 :                 CPLError(CE_Failure, CPLE_UserInterrupt,
    9466             :                          "User terminated CreateCopy()");
    9467             :             }
    9468             :         }
    9469             :     }
    9470             : 
    9471          91 :     CPLFree(patScanline);
    9472             : 
    9473          91 :     pfnProgress(1.0, nullptr, pProgressData);
    9474             : 
    9475          91 :     return eErr;
    9476             : }
    9477             : 
    9478             : /************************************************************************/
    9479             : /*                            CreateCopy()                              */
    9480             : /************************************************************************/
    9481             : 
    9482             : GDALDataset *
    9483          82 : netCDFDataset::CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
    9484             :                           CPL_UNUSED int bStrict, char **papszOptions,
    9485             :                           GDALProgressFunc pfnProgress, void *pProgressData)
    9486             : {
    9487         164 :     CPLMutexHolderD(&hNCMutex);
    9488             : 
    9489          82 :     CPLDebug("GDAL_netCDF", "\n=====\nnetCDFDataset::CreateCopy(%s, ...)",
    9490             :              pszFilename);
    9491             : 
    9492          82 :     if (poSrcDS->GetRootGroup())
    9493             :     {
    9494           5 :         auto poDrv = GDALDriver::FromHandle(GDALGetDriverByName("netCDF"));
    9495           5 :         if (poDrv)
    9496             :         {
    9497           5 :             return poDrv->DefaultCreateCopy(pszFilename, poSrcDS, bStrict,
    9498             :                                             papszOptions, pfnProgress,
    9499           5 :                                             pProgressData);
    9500             :         }
    9501             :     }
    9502             : 
    9503          77 :     const int nBands = poSrcDS->GetRasterCount();
    9504          77 :     const int nXSize = poSrcDS->GetRasterXSize();
    9505          77 :     const int nYSize = poSrcDS->GetRasterYSize();
    9506          77 :     const char *pszWKT = poSrcDS->GetProjectionRef();
    9507             : 
    9508             :     // Check input bands for errors.
    9509          77 :     if (nBands == 0)
    9510             :     {
    9511           1 :         CPLError(CE_Failure, CPLE_NotSupported,
    9512             :                  "NetCDF driver does not support "
    9513             :                  "source dataset with zero band.");
    9514           1 :         return nullptr;
    9515             :     }
    9516             : 
    9517          76 :     GDALDataType eDT = GDT_Unknown;
    9518          76 :     GDALRasterBand *poSrcBand = nullptr;
    9519         181 :     for (int iBand = 1; iBand <= nBands; iBand++)
    9520             :     {
    9521         109 :         poSrcBand = poSrcDS->GetRasterBand(iBand);
    9522         109 :         eDT = poSrcBand->GetRasterDataType();
    9523         109 :         if (eDT == GDT_Unknown || GDALDataTypeIsComplex(eDT))
    9524             :         {
    9525           4 :             CPLError(CE_Failure, CPLE_NotSupported,
    9526             :                      "NetCDF driver does not support source dataset with band "
    9527             :                      "of complex type.");
    9528           4 :             return nullptr;
    9529             :         }
    9530             :     }
    9531             : 
    9532         144 :     CPLStringList aosBandNames;
    9533          72 :     if (const char *pszBandNames =
    9534          72 :             CSLFetchNameValue(papszOptions, "BAND_NAMES"))
    9535             :     {
    9536             :         aosBandNames =
    9537           2 :             CSLTokenizeString2(pszBandNames, ",", CSLT_HONOURSTRINGS);
    9538             : 
    9539           2 :         if (aosBandNames.Count() != nBands)
    9540             :         {
    9541           1 :             CPLError(CE_Failure, CPLE_OpenFailed,
    9542             :                      "Attempted to create netCDF with %d bands but %d names "
    9543             :                      "provided in BAND_NAMES.",
    9544             :                      nBands, aosBandNames.Count());
    9545             : 
    9546           1 :             return nullptr;
    9547             :         }
    9548             :     }
    9549             : 
    9550          71 :     if (!pfnProgress(0.0, nullptr, pProgressData))
    9551           0 :         return nullptr;
    9552             : 
    9553             :     // Same as in Create().
    9554         142 :     CPLStringList aosOptions(CSLDuplicate(papszOptions));
    9555         133 :     if (aosOptions.FetchNameValue("FORMAT") == nullptr &&
    9556          62 :         (eDT == GDT_UInt16 || eDT == GDT_UInt32 || eDT == GDT_UInt64 ||
    9557             :          eDT == GDT_Int64))
    9558             :     {
    9559           6 :         CPLDebug("netCDF", "Selecting FORMAT=NC4 due to data type");
    9560           6 :         aosOptions.SetNameValue("FORMAT", "NC4");
    9561             :     }
    9562          71 :     netCDFDataset *poDS = netCDFDataset::CreateLL(pszFilename, nXSize, nYSize,
    9563             :                                                   nBands, aosOptions.List());
    9564          71 :     if (!poDS)
    9565          13 :         return nullptr;
    9566             : 
    9567             :     // Copy global metadata.
    9568             :     // Add Conventions, GDAL info and history.
    9569          58 :     CopyMetadata(poSrcDS, nullptr, nullptr, poDS->cdfid, NC_GLOBAL, nullptr);
    9570          58 :     const bool bWriteGDALVersion = CPLTestBool(
    9571             :         CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_VERSION", "YES"));
    9572          58 :     const bool bWriteGDALHistory = CPLTestBool(
    9573             :         CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_HISTORY", "YES"));
    9574          58 :     NCDFAddGDALHistory(
    9575             :         poDS->cdfid, pszFilename, bWriteGDALVersion, bWriteGDALHistory,
    9576          58 :         poSrcDS->GetMetadataItem("NC_GLOBAL#history"), "CreateCopy",
    9577          58 :         poSrcDS->GetMetadataItem("NC_GLOBAL#Conventions"));
    9578             : 
    9579          58 :     pfnProgress(0.1, nullptr, pProgressData);
    9580             : 
    9581             :     // Check for extra dimensions.
    9582          58 :     int nDim = 2;
    9583             :     char **papszExtraDimNames =
    9584          58 :         NCDFTokenizeArray(poSrcDS->GetMetadataItem("NETCDF_DIM_EXTRA", ""));
    9585          58 :     char **papszExtraDimValues = nullptr;
    9586             : 
    9587          58 :     if (papszExtraDimNames != nullptr && CSLCount(papszExtraDimNames) > 0)
    9588             :     {
    9589           5 :         size_t nDimSizeTot = 1;
    9590             :         // first make sure dimensions lengths compatible with band count
    9591             :         // for( int i=0; i<CSLCount(papszExtraDimNames ); i++ ) {
    9592          13 :         for (int i = CSLCount(papszExtraDimNames) - 1; i >= 0; i--)
    9593             :         {
    9594             :             char szTemp[NC_MAX_NAME + 32 + 1];
    9595           8 :             snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_DEF",
    9596           8 :                      papszExtraDimNames[i]);
    9597             :             papszExtraDimValues =
    9598           8 :                 NCDFTokenizeArray(poSrcDS->GetMetadataItem(szTemp, ""));
    9599           8 :             const size_t nDimSize = atol(papszExtraDimValues[0]);
    9600           8 :             CSLDestroy(papszExtraDimValues);
    9601           8 :             nDimSizeTot *= nDimSize;
    9602             :         }
    9603           5 :         if (nDimSizeTot == (size_t)nBands)
    9604             :         {
    9605           5 :             nDim = 2 + CSLCount(papszExtraDimNames);
    9606             :         }
    9607             :         else
    9608             :         {
    9609             :             // if nBands != #bands computed raise a warning
    9610             :             // just issue a debug message, because it was probably intentional
    9611           0 :             CPLDebug("GDAL_netCDF",
    9612             :                      "Warning: Number of bands (%d) is not compatible with "
    9613             :                      "dimensions "
    9614             :                      "(total=%ld names=%s)",
    9615             :                      nBands, (long)nDimSizeTot,
    9616           0 :                      poSrcDS->GetMetadataItem("NETCDF_DIM_EXTRA", ""));
    9617           0 :             CSLDestroy(papszExtraDimNames);
    9618           0 :             papszExtraDimNames = nullptr;
    9619             :         }
    9620             :     }
    9621             : 
    9622          58 :     int *panDimIds = static_cast<int *>(CPLCalloc(nDim, sizeof(int)));
    9623          58 :     int *panBandDimPos = static_cast<int *>(CPLCalloc(nDim, sizeof(int)));
    9624             : 
    9625             :     nc_type nVarType;
    9626          58 :     int *panBandZLev = nullptr;
    9627          58 :     int *panDimVarIds = nullptr;
    9628             : 
    9629          58 :     if (nDim > 2)
    9630             :     {
    9631           5 :         panBandZLev = static_cast<int *>(CPLCalloc(nDim - 2, sizeof(int)));
    9632           5 :         panDimVarIds = static_cast<int *>(CPLCalloc(nDim - 2, sizeof(int)));
    9633             : 
    9634             :         // Define all dims.
    9635          13 :         for (int i = CSLCount(papszExtraDimNames) - 1; i >= 0; i--)
    9636             :         {
    9637           8 :             poDS->papszDimName.AddString(papszExtraDimNames[i]);
    9638             :             char szTemp[NC_MAX_NAME + 32 + 1];
    9639           8 :             snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_DEF",
    9640           8 :                      papszExtraDimNames[i]);
    9641             :             papszExtraDimValues =
    9642           8 :                 NCDFTokenizeArray(poSrcDS->GetMetadataItem(szTemp, ""));
    9643           8 :             const int nDimSize = papszExtraDimValues && papszExtraDimValues[0]
    9644          16 :                                      ? atoi(papszExtraDimValues[0])
    9645             :                                      : 0;
    9646             :             // nc_type is an enum in netcdf-3, needs casting.
    9647           8 :             nVarType = static_cast<nc_type>(papszExtraDimValues &&
    9648           8 :                                                     papszExtraDimValues[0] &&
    9649           8 :                                                     papszExtraDimValues[1]
    9650           8 :                                                 ? atol(papszExtraDimValues[1])
    9651             :                                                 : 0);
    9652           8 :             CSLDestroy(papszExtraDimValues);
    9653           8 :             panBandZLev[i] = nDimSize;
    9654           8 :             panBandDimPos[i + 2] = i;  // Save Position of ZDim.
    9655             : 
    9656             :             // Define dim.
    9657          16 :             int status = nc_def_dim(poDS->cdfid, papszExtraDimNames[i],
    9658           8 :                                     nDimSize, &(panDimIds[i]));
    9659           8 :             NCDF_ERR(status);
    9660             : 
    9661             :             // Define dim var.
    9662           8 :             int anDim[1] = {panDimIds[i]};
    9663          16 :             status = nc_def_var(poDS->cdfid, papszExtraDimNames[i], nVarType, 1,
    9664           8 :                                 anDim, &(panDimVarIds[i]));
    9665           8 :             NCDF_ERR(status);
    9666             : 
    9667             :             // Add dim metadata, using global var# items.
    9668           8 :             snprintf(szTemp, sizeof(szTemp), "%s#", papszExtraDimNames[i]);
    9669           8 :             CopyMetadata(poSrcDS, nullptr, nullptr, poDS->cdfid,
    9670           8 :                          panDimVarIds[i], szTemp);
    9671             :         }
    9672             :     }
    9673             : 
    9674             :     // Copy GeoTransform and Projection.
    9675             : 
    9676             :     // Copy geolocation info.
    9677          58 :     char **papszGeolocationInfo = poSrcDS->GetMetadata("GEOLOCATION");
    9678          58 :     if (papszGeolocationInfo != nullptr)
    9679           5 :         poDS->GDALPamDataset::SetMetadata(papszGeolocationInfo, "GEOLOCATION");
    9680             : 
    9681             :     // Copy geotransform.
    9682          58 :     bool bGotGeoTransform = false;
    9683          58 :     GDALGeoTransform gt;
    9684          58 :     CPLErr eErr = poSrcDS->GetGeoTransform(gt);
    9685          58 :     if (eErr == CE_None)
    9686             :     {
    9687          40 :         poDS->SetGeoTransform(gt);
    9688             :         // Disable AddProjectionVars() from being called.
    9689          40 :         bGotGeoTransform = true;
    9690          40 :         poDS->m_bHasGeoTransform = false;
    9691             :     }
    9692             : 
    9693             :     // Copy projection.
    9694          58 :     void *pScaledProgress = nullptr;
    9695          58 :     if (bGotGeoTransform || (pszWKT && pszWKT[0] != 0))
    9696             :     {
    9697          41 :         poDS->SetProjection(pszWKT ? pszWKT : "");
    9698             : 
    9699             :         // Now we can call AddProjectionVars() directly.
    9700          41 :         poDS->m_bHasGeoTransform = bGotGeoTransform;
    9701          41 :         poDS->AddProjectionVars(true, nullptr, nullptr);
    9702             :         pScaledProgress =
    9703          41 :             GDALCreateScaledProgress(0.1, 0.25, pfnProgress, pProgressData);
    9704          41 :         poDS->AddProjectionVars(false, GDALScaledProgress, pScaledProgress);
    9705          41 :         GDALDestroyScaledProgress(pScaledProgress);
    9706             :     }
    9707             :     else
    9708             :     {
    9709          17 :         poDS->bBottomUp =
    9710          17 :             CPL_TO_BOOL(CSLFetchBoolean(papszOptions, "WRITE_BOTTOMUP", TRUE));
    9711          17 :         if (papszGeolocationInfo)
    9712             :         {
    9713           4 :             poDS->AddProjectionVars(true, nullptr, nullptr);
    9714           4 :             poDS->AddProjectionVars(false, nullptr, nullptr);
    9715             :         }
    9716             :     }
    9717             : 
    9718             :     // Save X,Y dim positions.
    9719          58 :     panDimIds[nDim - 1] = poDS->nXDimID;
    9720          58 :     panBandDimPos[0] = nDim - 1;
    9721          58 :     panDimIds[nDim - 2] = poDS->nYDimID;
    9722          58 :     panBandDimPos[1] = nDim - 2;
    9723             : 
    9724             :     // Write extra dim values - after projection for optimization.
    9725          58 :     if (nDim > 2)
    9726             :     {
    9727             :         // Make sure we are in data mode.
    9728           5 :         static_cast<netCDFDataset *>(poDS)->SetDefineMode(false);
    9729          13 :         for (int i = CSLCount(papszExtraDimNames) - 1; i >= 0; i--)
    9730             :         {
    9731             :             char szTemp[NC_MAX_NAME + 32 + 1];
    9732           8 :             snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_VALUES",
    9733           8 :                      papszExtraDimNames[i]);
    9734           8 :             if (poSrcDS->GetMetadataItem(szTemp) != nullptr)
    9735             :             {
    9736           8 :                 NCDFPut1DVar(poDS->cdfid, panDimVarIds[i],
    9737           8 :                              poSrcDS->GetMetadataItem(szTemp));
    9738             :             }
    9739             :         }
    9740             :     }
    9741             : 
    9742          58 :     pfnProgress(0.25, nullptr, pProgressData);
    9743             : 
    9744             :     // Define Bands.
    9745          58 :     netCDFRasterBand *poBand = nullptr;
    9746          58 :     int nBandID = -1;
    9747             : 
    9748         149 :     for (int iBand = 1; iBand <= nBands; iBand++)
    9749             :     {
    9750          91 :         CPLDebug("GDAL_netCDF", "creating band # %d/%d nDim = %d", iBand,
    9751             :                  nBands, nDim);
    9752             : 
    9753          91 :         poSrcBand = poSrcDS->GetRasterBand(iBand);
    9754          91 :         eDT = poSrcBand->GetRasterDataType();
    9755             : 
    9756             :         // Get var name from NETCDF_VARNAME.
    9757             :         const char *pszNETCDF_VARNAME =
    9758          91 :             poSrcBand->GetMetadataItem("NETCDF_VARNAME");
    9759             :         char szBandName[NC_MAX_NAME + 1];
    9760          91 :         if (!aosBandNames.empty())
    9761             :         {
    9762           2 :             snprintf(szBandName, sizeof(szBandName), "%s",
    9763             :                      aosBandNames[iBand - 1]);
    9764             :         }
    9765          89 :         else if (pszNETCDF_VARNAME)
    9766             :         {
    9767          32 :             if (nBands > 1 && papszExtraDimNames == nullptr)
    9768           0 :                 snprintf(szBandName, sizeof(szBandName), "%s%d",
    9769             :                          pszNETCDF_VARNAME, iBand);
    9770             :             else
    9771          32 :                 snprintf(szBandName, sizeof(szBandName), "%s",
    9772             :                          pszNETCDF_VARNAME);
    9773             :         }
    9774             :         else
    9775             :         {
    9776          57 :             szBandName[0] = '\0';
    9777             :         }
    9778             : 
    9779             :         // Get long_name from <var>#long_name.
    9780          91 :         const char *pszLongName = "";
    9781          91 :         if (pszNETCDF_VARNAME)
    9782             :         {
    9783             :             pszLongName =
    9784          64 :                 poSrcDS->GetMetadataItem(std::string(pszNETCDF_VARNAME)
    9785          32 :                                              .append("#")
    9786          32 :                                              .append(CF_LNG_NAME)
    9787          32 :                                              .c_str());
    9788          32 :             if (!pszLongName)
    9789          25 :                 pszLongName = "";
    9790             :         }
    9791             : 
    9792          91 :         constexpr bool bSignedData = false;
    9793             : 
    9794          91 :         if (nDim > 2)
    9795          27 :             poBand = new netCDFRasterBand(
    9796          27 :                 netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS, eDT, iBand,
    9797             :                 bSignedData, szBandName, pszLongName, nBandID, nDim, iBand - 1,
    9798          27 :                 panBandZLev, panBandDimPos, panDimIds);
    9799             :         else
    9800          64 :             poBand = new netCDFRasterBand(
    9801          64 :                 netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS, eDT, iBand,
    9802          64 :                 bSignedData, szBandName, pszLongName);
    9803             : 
    9804          91 :         poDS->SetBand(iBand, poBand);
    9805             : 
    9806             :         // Set nodata value, if any.
    9807          91 :         GDALCopyNoDataValue(poBand, poSrcBand);
    9808             : 
    9809             :         // Copy Metadata for band.
    9810          91 :         CopyMetadata(nullptr, poSrcDS->GetRasterBand(iBand), poBand,
    9811             :                      poDS->cdfid, poBand->nZId);
    9812             : 
    9813             :         // If more than 2D pass the first band's netcdf var ID to subsequent
    9814             :         // bands.
    9815          91 :         if (nDim > 2)
    9816          27 :             nBandID = poBand->nZId;
    9817             :     }
    9818             : 
    9819             :     // Write projection variable to band variable.
    9820          58 :     poDS->AddGridMappingRef();
    9821             : 
    9822          58 :     pfnProgress(0.5, nullptr, pProgressData);
    9823             : 
    9824             :     // Write bands.
    9825             : 
    9826             :     // Make sure we are in data mode.
    9827          58 :     poDS->SetDefineMode(false);
    9828             : 
    9829          58 :     double dfTemp = 0.5;
    9830             : 
    9831          58 :     eErr = CE_None;
    9832             : 
    9833         149 :     for (int iBand = 1; iBand <= nBands && eErr == CE_None; iBand++)
    9834             :     {
    9835          91 :         const double dfTemp2 = dfTemp + 0.4 / nBands;
    9836          91 :         pScaledProgress = GDALCreateScaledProgress(dfTemp, dfTemp2, pfnProgress,
    9837             :                                                    pProgressData);
    9838          91 :         dfTemp = dfTemp2;
    9839             : 
    9840          91 :         CPLDebug("GDAL_netCDF", "copying band data # %d/%d ", iBand, nBands);
    9841             : 
    9842          91 :         poSrcBand = poSrcDS->GetRasterBand(iBand);
    9843          91 :         eDT = poSrcBand->GetRasterDataType();
    9844             : 
    9845          91 :         GDALRasterBand *poDstBand = poDS->GetRasterBand(iBand);
    9846             : 
    9847             :         // Copy band data.
    9848          91 :         if (eDT == GDT_Byte)
    9849             :         {
    9850          51 :             CPLDebug("GDAL_netCDF", "GByte Band#%d", iBand);
    9851          51 :             eErr = NCDFCopyBand<GByte>(poSrcBand, poDstBand, nXSize, nYSize,
    9852             :                                        GDALScaledProgress, pScaledProgress);
    9853             :         }
    9854          40 :         else if (eDT == GDT_Int8)
    9855             :         {
    9856           1 :             CPLDebug("GDAL_netCDF", "GInt8 Band#%d", iBand);
    9857           1 :             eErr = NCDFCopyBand<GInt8>(poSrcBand, poDstBand, nXSize, nYSize,
    9858             :                                        GDALScaledProgress, pScaledProgress);
    9859             :         }
    9860          39 :         else if (eDT == GDT_UInt16)
    9861             :         {
    9862           2 :             CPLDebug("GDAL_netCDF", "GUInt16 Band#%d", iBand);
    9863           2 :             eErr = NCDFCopyBand<GInt16>(poSrcBand, poDstBand, nXSize, nYSize,
    9864             :                                         GDALScaledProgress, pScaledProgress);
    9865             :         }
    9866          37 :         else if (eDT == GDT_Int16)
    9867             :         {
    9868           5 :             CPLDebug("GDAL_netCDF", "GInt16 Band#%d", iBand);
    9869           5 :             eErr = NCDFCopyBand<GUInt16>(poSrcBand, poDstBand, nXSize, nYSize,
    9870             :                                          GDALScaledProgress, pScaledProgress);
    9871             :         }
    9872          32 :         else if (eDT == GDT_UInt32)
    9873             :         {
    9874           2 :             CPLDebug("GDAL_netCDF", "GUInt32 Band#%d", iBand);
    9875           2 :             eErr = NCDFCopyBand<GUInt32>(poSrcBand, poDstBand, nXSize, nYSize,
    9876             :                                          GDALScaledProgress, pScaledProgress);
    9877             :         }
    9878          30 :         else if (eDT == GDT_Int32)
    9879             :         {
    9880          18 :             CPLDebug("GDAL_netCDF", "GInt32 Band#%d", iBand);
    9881          18 :             eErr = NCDFCopyBand<GInt32>(poSrcBand, poDstBand, nXSize, nYSize,
    9882             :                                         GDALScaledProgress, pScaledProgress);
    9883             :         }
    9884          12 :         else if (eDT == GDT_UInt64)
    9885             :         {
    9886           2 :             CPLDebug("GDAL_netCDF", "GUInt64 Band#%d", iBand);
    9887           2 :             eErr = NCDFCopyBand<std::uint64_t>(poSrcBand, poDstBand, nXSize,
    9888             :                                                nYSize, GDALScaledProgress,
    9889             :                                                pScaledProgress);
    9890             :         }
    9891          10 :         else if (eDT == GDT_Int64)
    9892             :         {
    9893           2 :             CPLDebug("GDAL_netCDF", "GInt64 Band#%d", iBand);
    9894             :             eErr =
    9895           2 :                 NCDFCopyBand<std::int64_t>(poSrcBand, poDstBand, nXSize, nYSize,
    9896             :                                            GDALScaledProgress, pScaledProgress);
    9897             :         }
    9898           8 :         else if (eDT == GDT_Float32)
    9899             :         {
    9900           6 :             CPLDebug("GDAL_netCDF", "float Band#%d", iBand);
    9901           6 :             eErr = NCDFCopyBand<float>(poSrcBand, poDstBand, nXSize, nYSize,
    9902             :                                        GDALScaledProgress, pScaledProgress);
    9903             :         }
    9904           2 :         else if (eDT == GDT_Float64)
    9905             :         {
    9906           2 :             CPLDebug("GDAL_netCDF", "double Band#%d", iBand);
    9907           2 :             eErr = NCDFCopyBand<double>(poSrcBand, poDstBand, nXSize, nYSize,
    9908             :                                         GDALScaledProgress, pScaledProgress);
    9909             :         }
    9910             :         else
    9911             :         {
    9912           0 :             CPLError(CE_Failure, CPLE_NotSupported,
    9913             :                      "The NetCDF driver does not support GDAL data type %d",
    9914             :                      eDT);
    9915             :         }
    9916             : 
    9917          91 :         GDALDestroyScaledProgress(pScaledProgress);
    9918             :     }
    9919             : 
    9920          58 :     delete (poDS);
    9921             : 
    9922          58 :     CPLFree(panDimIds);
    9923          58 :     CPLFree(panBandDimPos);
    9924          58 :     CPLFree(panBandZLev);
    9925          58 :     CPLFree(panDimVarIds);
    9926          58 :     if (papszExtraDimNames)
    9927           5 :         CSLDestroy(papszExtraDimNames);
    9928             : 
    9929          58 :     if (eErr != CE_None)
    9930           0 :         return nullptr;
    9931             : 
    9932          58 :     pfnProgress(0.95, nullptr, pProgressData);
    9933             : 
    9934             :     // Re-open dataset so we can return it.
    9935         116 :     CPLStringList aosOpenOptions;
    9936          58 :     aosOpenOptions.AddString("VARIABLES_AS_BANDS=YES");
    9937          58 :     GDALOpenInfo oOpenInfo(pszFilename, GA_Update);
    9938          58 :     oOpenInfo.nOpenFlags = GDAL_OF_RASTER | GDAL_OF_UPDATE;
    9939          58 :     oOpenInfo.papszOpenOptions = aosOpenOptions.List();
    9940          58 :     auto poRetDS = Open(&oOpenInfo);
    9941             : 
    9942             :     // PAM cloning is disabled. See bug #4244.
    9943             :     // if( poDS )
    9944             :     //     poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
    9945             : 
    9946          58 :     pfnProgress(1.0, nullptr, pProgressData);
    9947             : 
    9948          58 :     return poRetDS;
    9949             : }
    9950             : 
    9951             : // Note: some logic depends on bIsProjected and bIsGeoGraphic.
    9952             : // May not be known when Create() is called, see AddProjectionVars().
    9953         254 : void netCDFDataset::ProcessCreationOptions()
    9954             : {
    9955             :     const char *pszConfig =
    9956         254 :         CSLFetchNameValue(papszCreationOptions, "CONFIG_FILE");
    9957         254 :     if (pszConfig != nullptr)
    9958             :     {
    9959           4 :         if (oWriterConfig.Parse(pszConfig))
    9960             :         {
    9961             :             // Override dataset creation options from the config file
    9962           2 :             std::map<CPLString, CPLString>::iterator oIter;
    9963           3 :             for (oIter = oWriterConfig.m_oDatasetCreationOptions.begin();
    9964           3 :                  oIter != oWriterConfig.m_oDatasetCreationOptions.end();
    9965           1 :                  ++oIter)
    9966             :             {
    9967           2 :                 papszCreationOptions = CSLSetNameValue(
    9968           2 :                     papszCreationOptions, oIter->first, oIter->second);
    9969             :             }
    9970             :         }
    9971             :     }
    9972             : 
    9973             :     // File format.
    9974         254 :     eFormat = NCDF_FORMAT_NC;
    9975         254 :     const char *pszValue = CSLFetchNameValue(papszCreationOptions, "FORMAT");
    9976         254 :     if (pszValue != nullptr)
    9977             :     {
    9978          93 :         if (EQUAL(pszValue, "NC"))
    9979             :         {
    9980           3 :             eFormat = NCDF_FORMAT_NC;
    9981             :         }
    9982             : #ifdef NETCDF_HAS_NC2
    9983          90 :         else if (EQUAL(pszValue, "NC2"))
    9984             :         {
    9985           1 :             eFormat = NCDF_FORMAT_NC2;
    9986             :         }
    9987             : #endif
    9988          89 :         else if (EQUAL(pszValue, "NC4"))
    9989             :         {
    9990          85 :             eFormat = NCDF_FORMAT_NC4;
    9991             :         }
    9992           4 :         else if (EQUAL(pszValue, "NC4C"))
    9993             :         {
    9994           4 :             eFormat = NCDF_FORMAT_NC4C;
    9995             :         }
    9996             :         else
    9997             :         {
    9998           0 :             CPLError(CE_Failure, CPLE_NotSupported,
    9999             :                      "FORMAT=%s in not supported, using the default NC format.",
   10000             :                      pszValue);
   10001             :         }
   10002             :     }
   10003             : 
   10004             :     // COMPRESS option.
   10005         254 :     pszValue = CSLFetchNameValue(papszCreationOptions, "COMPRESS");
   10006         254 :     if (pszValue != nullptr)
   10007             :     {
   10008           3 :         if (EQUAL(pszValue, "NONE"))
   10009             :         {
   10010           1 :             eCompress = NCDF_COMPRESS_NONE;
   10011             :         }
   10012           2 :         else if (EQUAL(pszValue, "DEFLATE"))
   10013             :         {
   10014           2 :             eCompress = NCDF_COMPRESS_DEFLATE;
   10015           2 :             if (!((eFormat == NCDF_FORMAT_NC4) ||
   10016           2 :                   (eFormat == NCDF_FORMAT_NC4C)))
   10017             :             {
   10018           1 :                 CPLError(CE_Warning, CPLE_IllegalArg,
   10019             :                          "NOTICE: Format set to NC4C because compression is "
   10020             :                          "set to DEFLATE.");
   10021           1 :                 eFormat = NCDF_FORMAT_NC4C;
   10022             :             }
   10023             :         }
   10024             :         else
   10025             :         {
   10026           0 :             CPLError(CE_Failure, CPLE_NotSupported,
   10027             :                      "COMPRESS=%s is not supported.", pszValue);
   10028             :         }
   10029             :     }
   10030             : 
   10031             :     // ZLEVEL option.
   10032         254 :     pszValue = CSLFetchNameValue(papszCreationOptions, "ZLEVEL");
   10033         254 :     if (pszValue != nullptr)
   10034             :     {
   10035           1 :         nZLevel = atoi(pszValue);
   10036           1 :         if (!(nZLevel >= 1 && nZLevel <= 9))
   10037             :         {
   10038           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
   10039             :                      "ZLEVEL=%s value not recognised, ignoring.", pszValue);
   10040           0 :             nZLevel = NCDF_DEFLATE_LEVEL;
   10041             :         }
   10042             :     }
   10043             : 
   10044             :     // CHUNKING option.
   10045         254 :     bChunking =
   10046         254 :         CPL_TO_BOOL(CSLFetchBoolean(papszCreationOptions, "CHUNKING", TRUE));
   10047             : 
   10048             :     // MULTIPLE_LAYERS option.
   10049             :     const char *pszMultipleLayerBehavior =
   10050         254 :         CSLFetchNameValueDef(papszCreationOptions, "MULTIPLE_LAYERS", "NO");
   10051         508 :     const char *pszGeometryEnc = CSLFetchNameValueDef(
   10052         254 :         papszCreationOptions, "GEOMETRY_ENCODING", "CF_1.8");
   10053         254 :     if (EQUAL(pszMultipleLayerBehavior, "NO") ||
   10054           3 :         EQUAL(pszGeometryEnc, "CF_1.8"))
   10055             :     {
   10056         251 :         eMultipleLayerBehavior = SINGLE_LAYER;
   10057             :     }
   10058           3 :     else if (EQUAL(pszMultipleLayerBehavior, "SEPARATE_FILES"))
   10059             :     {
   10060           2 :         eMultipleLayerBehavior = SEPARATE_FILES;
   10061             :     }
   10062           1 :     else if (EQUAL(pszMultipleLayerBehavior, "SEPARATE_GROUPS"))
   10063             :     {
   10064           1 :         if (eFormat == NCDF_FORMAT_NC4)
   10065             :         {
   10066           1 :             eMultipleLayerBehavior = SEPARATE_GROUPS;
   10067             :         }
   10068             :         else
   10069             :         {
   10070           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
   10071             :                      "MULTIPLE_LAYERS=%s is recognised only with FORMAT=NC4",
   10072             :                      pszMultipleLayerBehavior);
   10073             :         }
   10074             :     }
   10075             :     else
   10076             :     {
   10077           0 :         CPLError(CE_Warning, CPLE_IllegalArg,
   10078             :                  "MULTIPLE_LAYERS=%s not recognised", pszMultipleLayerBehavior);
   10079             :     }
   10080             : 
   10081             :     // Set nCreateMode based on eFormat.
   10082         254 :     switch (eFormat)
   10083             :     {
   10084             : #ifdef NETCDF_HAS_NC2
   10085           1 :         case NCDF_FORMAT_NC2:
   10086           1 :             nCreateMode = NC_CLOBBER | NC_64BIT_OFFSET;
   10087           1 :             break;
   10088             : #endif
   10089          85 :         case NCDF_FORMAT_NC4:
   10090          85 :             nCreateMode = NC_CLOBBER | NC_NETCDF4;
   10091          85 :             break;
   10092           5 :         case NCDF_FORMAT_NC4C:
   10093           5 :             nCreateMode = NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL;
   10094           5 :             break;
   10095         163 :         case NCDF_FORMAT_NC:
   10096             :         default:
   10097         163 :             nCreateMode = NC_CLOBBER;
   10098         163 :             break;
   10099             :     }
   10100             : 
   10101         254 :     CPLDebug("GDAL_netCDF", "file options: format=%d compress=%d zlevel=%d",
   10102         254 :              eFormat, eCompress, nZLevel);
   10103         254 : }
   10104             : 
   10105         278 : int netCDFDataset::DefVarDeflate(int nVarId, bool bChunkingArg)
   10106             : {
   10107         278 :     if (eCompress == NCDF_COMPRESS_DEFLATE)
   10108             :     {
   10109             :         // Must set chunk size to avoid huge performance hit (set
   10110             :         // bChunkingArg=TRUE)
   10111             :         // perhaps another solution it to change the chunk cache?
   10112             :         // http://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html#Chunk-Cache
   10113             :         // TODO: make sure this is okay.
   10114           2 :         CPLDebug("GDAL_netCDF", "DefVarDeflate(%d, %d) nZlevel=%d", nVarId,
   10115             :                  static_cast<int>(bChunkingArg), nZLevel);
   10116             : 
   10117           2 :         int status = nc_def_var_deflate(cdfid, nVarId, 1, 1, nZLevel);
   10118           2 :         NCDF_ERR(status);
   10119             : 
   10120           2 :         if (status == NC_NOERR && bChunkingArg && bChunking)
   10121             :         {
   10122             :             // set chunking to be 1 for all dims, except X dim
   10123             :             // size_t chunksize[] = { 1, (size_t)nRasterXSize };
   10124             :             size_t chunksize[MAX_NC_DIMS];
   10125             :             int nd;
   10126           2 :             nc_inq_varndims(cdfid, nVarId, &nd);
   10127           2 :             chunksize[0] = (size_t)1;
   10128           2 :             chunksize[1] = (size_t)1;
   10129           2 :             for (int i = 2; i < nd; i++)
   10130           0 :                 chunksize[i] = (size_t)1;
   10131           2 :             chunksize[nd - 1] = (size_t)nRasterXSize;
   10132             : 
   10133             :             // Config options just for testing purposes
   10134             :             const char *pszBlockXSize =
   10135           2 :                 CPLGetConfigOption("BLOCKXSIZE", nullptr);
   10136           2 :             if (pszBlockXSize)
   10137           0 :                 chunksize[nd - 1] = (size_t)atoi(pszBlockXSize);
   10138             : 
   10139             :             const char *pszBlockYSize =
   10140           2 :                 CPLGetConfigOption("BLOCKYSIZE", nullptr);
   10141           2 :             if (nd >= 2 && pszBlockYSize)
   10142           0 :                 chunksize[nd - 2] = (size_t)atoi(pszBlockYSize);
   10143             : 
   10144           2 :             CPLDebug("GDAL_netCDF",
   10145             :                      "DefVarDeflate() chunksize={%ld, %ld} chunkX=%ld nd=%d",
   10146           2 :                      (long)chunksize[0], (long)chunksize[1],
   10147           2 :                      (long)chunksize[nd - 1], nd);
   10148             : #ifdef NCDF_DEBUG
   10149             :             for (int i = 0; i < nd; i++)
   10150             :                 CPLDebug("GDAL_netCDF", "DefVarDeflate() chunk[%d]=%ld", i,
   10151             :                          chunksize[i]);
   10152             : #endif
   10153             : 
   10154           2 :             status = nc_def_var_chunking(cdfid, nVarId, NC_CHUNKED, chunksize);
   10155           2 :             NCDF_ERR(status);
   10156             :         }
   10157             :         else
   10158             :         {
   10159           0 :             CPLDebug("GDAL_netCDF", "chunksize not set");
   10160             :         }
   10161           2 :         return status;
   10162             :     }
   10163         276 :     return NC_NOERR;
   10164             : }
   10165             : 
   10166             : /************************************************************************/
   10167             : /*                           NCDFUnloadDriver()                         */
   10168             : /************************************************************************/
   10169             : 
   10170           8 : static void NCDFUnloadDriver(CPL_UNUSED GDALDriver *poDriver)
   10171             : {
   10172           8 :     if (hNCMutex != nullptr)
   10173           4 :         CPLDestroyMutex(hNCMutex);
   10174           8 :     hNCMutex = nullptr;
   10175           8 : }
   10176             : 
   10177             : /************************************************************************/
   10178             : /*                          GDALRegister_netCDF()                       */
   10179             : /************************************************************************/
   10180             : 
   10181             : class GDALnetCDFDriver final : public GDALDriver
   10182             : {
   10183             :   public:
   10184          18 :     GDALnetCDFDriver() = default;
   10185             : 
   10186             :     const char *GetMetadataItem(const char *pszName,
   10187             :                                 const char *pszDomain) override;
   10188             : 
   10189          89 :     char **GetMetadata(const char *pszDomain) override
   10190             :     {
   10191         178 :         std::lock_guard oLock(m_oMutex);
   10192          89 :         InitializeDCAPVirtualIO();
   10193         178 :         return GDALDriver::GetMetadata(pszDomain);
   10194             :     }
   10195             : 
   10196             :   private:
   10197             :     std::mutex m_oMutex{};
   10198             :     bool m_bInitialized = false;
   10199             : 
   10200         102 :     void InitializeDCAPVirtualIO()
   10201             :     {
   10202         102 :         if (!m_bInitialized)
   10203             :         {
   10204          12 :             m_bInitialized = true;
   10205             : 
   10206             : #ifdef ENABLE_UFFD
   10207          12 :             if (CPLIsUserFaultMappingSupported())
   10208             :             {
   10209          12 :                 SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
   10210             :             }
   10211             : #endif
   10212             :         }
   10213         102 :     }
   10214             : };
   10215             : 
   10216        1339 : const char *GDALnetCDFDriver::GetMetadataItem(const char *pszName,
   10217             :                                               const char *pszDomain)
   10218             : {
   10219        2678 :     std::lock_guard oLock(m_oMutex);
   10220        1339 :     if (EQUAL(pszName, GDAL_DCAP_VIRTUALIO))
   10221             :     {
   10222          13 :         InitializeDCAPVirtualIO();
   10223             :     }
   10224        2678 :     return GDALDriver::GetMetadataItem(pszName, pszDomain);
   10225             : }
   10226             : 
   10227          18 : void GDALRegister_netCDF()
   10228             : 
   10229             : {
   10230          18 :     if (!GDAL_CHECK_VERSION("netCDF driver"))
   10231           0 :         return;
   10232             : 
   10233          18 :     if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
   10234           0 :         return;
   10235             : 
   10236          18 :     GDALDriver *poDriver = new GDALnetCDFDriver();
   10237          18 :     netCDFDriverSetCommonMetadata(poDriver);
   10238             : 
   10239          18 :     poDriver->SetMetadataItem("NETCDF_CONVENTIONS",
   10240          18 :                               GDAL_DEFAULT_NCDF_CONVENTIONS);
   10241          18 :     poDriver->SetMetadataItem("NETCDF_VERSION", nc_inq_libvers());
   10242             : 
   10243             :     // Set pfns and register driver.
   10244          18 :     poDriver->pfnOpen = netCDFDataset::Open;
   10245          18 :     poDriver->pfnCreateCopy = netCDFDataset::CreateCopy;
   10246          18 :     poDriver->pfnCreate = netCDFDataset::Create;
   10247          18 :     poDriver->pfnCreateMultiDimensional = netCDFDataset::CreateMultiDimensional;
   10248          18 :     poDriver->pfnUnloadDriver = NCDFUnloadDriver;
   10249             : 
   10250          18 :     GetGDALDriverManager()->RegisterDriver(poDriver);
   10251             : }
   10252             : 
   10253             : /************************************************************************/
   10254             : /*                          New functions                               */
   10255             : /************************************************************************/
   10256             : 
   10257             : /* Test for GDAL version string >= target */
   10258         241 : static bool NCDFIsGDALVersionGTE(const char *pszVersion, int nTarget)
   10259             : {
   10260             : 
   10261             :     // Valid strings are "GDAL 1.9dev, released 2011/01/18" and "GDAL 1.8.1 ".
   10262         241 :     if (pszVersion == nullptr || EQUAL(pszVersion, ""))
   10263           0 :         return false;
   10264         241 :     else if (!STARTS_WITH_CI(pszVersion, "GDAL "))
   10265           0 :         return false;
   10266             :     // 2.0dev of 2011/12/29 has been later renamed as 1.10dev.
   10267         241 :     else if (EQUAL("GDAL 2.0dev, released 2011/12/29", pszVersion))
   10268           0 :         return nTarget <= GDAL_COMPUTE_VERSION(1, 10, 0);
   10269         241 :     else if (STARTS_WITH_CI(pszVersion, "GDAL 1.9dev"))
   10270           2 :         return nTarget <= 1900;
   10271         239 :     else if (STARTS_WITH_CI(pszVersion, "GDAL 1.8dev"))
   10272           0 :         return nTarget <= 1800;
   10273             : 
   10274         239 :     char **papszTokens = CSLTokenizeString2(pszVersion + 5, ".", 0);
   10275             : 
   10276         239 :     int nVersions[] = {0, 0, 0, 0};
   10277         956 :     for (int iToken = 0; papszTokens && iToken < 4 && papszTokens[iToken];
   10278             :          iToken++)
   10279             :     {
   10280         717 :         nVersions[iToken] = atoi(papszTokens[iToken]);
   10281         717 :         if (nVersions[iToken] < 0)
   10282           0 :             nVersions[iToken] = 0;
   10283         717 :         else if (nVersions[iToken] > 99)
   10284           0 :             nVersions[iToken] = 99;
   10285             :     }
   10286             : 
   10287         239 :     int nVersion = 0;
   10288         239 :     if (nVersions[0] > 1 || nVersions[1] >= 10)
   10289         239 :         nVersion =
   10290         239 :             GDAL_COMPUTE_VERSION(nVersions[0], nVersions[1], nVersions[2]);
   10291             :     else
   10292           0 :         nVersion = nVersions[0] * 1000 + nVersions[1] * 100 +
   10293           0 :                    nVersions[2] * 10 + nVersions[3];
   10294             : 
   10295         239 :     CSLDestroy(papszTokens);
   10296         239 :     return nTarget <= nVersion;
   10297             : }
   10298             : 
   10299             : // Add Conventions, GDAL version and history.
   10300         167 : static void NCDFAddGDALHistory(int fpImage, const char *pszFilename,
   10301             :                                bool bWriteGDALVersion, bool bWriteGDALHistory,
   10302             :                                const char *pszOldHist,
   10303             :                                const char *pszFunctionName,
   10304             :                                const char *pszCFVersion)
   10305             : {
   10306         167 :     if (pszCFVersion == nullptr)
   10307             :     {
   10308          42 :         pszCFVersion = GDAL_DEFAULT_NCDF_CONVENTIONS;
   10309             :     }
   10310         167 :     int status = nc_put_att_text(fpImage, NC_GLOBAL, "Conventions",
   10311             :                                  strlen(pszCFVersion), pszCFVersion);
   10312         167 :     NCDF_ERR(status);
   10313             : 
   10314         167 :     if (bWriteGDALVersion)
   10315             :     {
   10316         165 :         const char *pszNCDF_GDAL = GDALVersionInfo("--version");
   10317         165 :         status = nc_put_att_text(fpImage, NC_GLOBAL, "GDAL",
   10318             :                                  strlen(pszNCDF_GDAL), pszNCDF_GDAL);
   10319         165 :         NCDF_ERR(status);
   10320             :     }
   10321             : 
   10322         167 :     if (bWriteGDALHistory)
   10323             :     {
   10324             :         // Add history.
   10325         330 :         CPLString osTmp;
   10326             : #ifdef GDAL_SET_CMD_LINE_DEFINED_TMP
   10327             :         if (!EQUAL(GDALGetCmdLine(), ""))
   10328             :             osTmp = GDALGetCmdLine();
   10329             :         else
   10330             :             osTmp =
   10331             :                 CPLSPrintf("GDAL %s( %s, ... )", pszFunctionName, pszFilename);
   10332             : #else
   10333         165 :         osTmp = CPLSPrintf("GDAL %s( %s, ... )", pszFunctionName, pszFilename);
   10334             : #endif
   10335             : 
   10336         165 :         NCDFAddHistory(fpImage, osTmp.c_str(), pszOldHist);
   10337             :     }
   10338           2 :     else if (pszOldHist != nullptr)
   10339             :     {
   10340           0 :         status = nc_put_att_text(fpImage, NC_GLOBAL, "history",
   10341             :                                  strlen(pszOldHist), pszOldHist);
   10342           0 :         NCDF_ERR(status);
   10343             :     }
   10344         167 : }
   10345             : 
   10346             : // Code taken from cdo and libcdi, used for writing the history attribute.
   10347             : 
   10348             : // void cdoDefHistory(int fileID, char *histstring)
   10349         165 : static void NCDFAddHistory(int fpImage, const char *pszAddHist,
   10350             :                            const char *pszOldHist)
   10351             : {
   10352             :     // Check pszOldHist - as if there was no previous history, it will be
   10353             :     // a null pointer - if so set as empty.
   10354         165 :     if (nullptr == pszOldHist)
   10355             :     {
   10356          53 :         pszOldHist = "";
   10357             :     }
   10358             : 
   10359             :     char strtime[32];
   10360         165 :     strtime[0] = '\0';
   10361             : 
   10362         165 :     time_t tp = time(nullptr);
   10363         165 :     if (tp != -1)
   10364             :     {
   10365             :         struct tm ltime;
   10366         165 :         VSILocalTime(&tp, &ltime);
   10367         165 :         (void)strftime(strtime, sizeof(strtime),
   10368             :                        "%a %b %d %H:%M:%S %Y: ", &ltime);
   10369             :     }
   10370             : 
   10371             :     // status = nc_get_att_text(fpImage, NC_GLOBAL,
   10372             :     //                           "history", pszOldHist);
   10373             :     // printf("status: %d pszOldHist: [%s]\n",status,pszOldHist);
   10374             : 
   10375         165 :     size_t nNewHistSize =
   10376         165 :         strlen(pszOldHist) + strlen(strtime) + strlen(pszAddHist) + 1 + 1;
   10377             :     char *pszNewHist =
   10378         165 :         static_cast<char *>(CPLMalloc(nNewHistSize * sizeof(char)));
   10379             : 
   10380         165 :     strcpy(pszNewHist, strtime);
   10381         165 :     strcat(pszNewHist, pszAddHist);
   10382             : 
   10383             :     // int disableHistory = FALSE;
   10384             :     // if( !disableHistory )
   10385             :     {
   10386         165 :         if (!EQUAL(pszOldHist, ""))
   10387           3 :             strcat(pszNewHist, "\n");
   10388         165 :         strcat(pszNewHist, pszOldHist);
   10389             :     }
   10390             : 
   10391         165 :     const int status = nc_put_att_text(fpImage, NC_GLOBAL, "history",
   10392             :                                        strlen(pszNewHist), pszNewHist);
   10393         165 :     NCDF_ERR(status);
   10394             : 
   10395         165 :     CPLFree(pszNewHist);
   10396         165 : }
   10397             : 
   10398        6133 : static CPLErr NCDFSafeStrcat(char **ppszDest, const char *pszSrc,
   10399             :                              size_t *nDestSize)
   10400             : {
   10401             :     /* Reallocate the data string until the content fits */
   10402        6133 :     while (*nDestSize < (strlen(*ppszDest) + strlen(pszSrc) + 1))
   10403             :     {
   10404         408 :         (*nDestSize) *= 2;
   10405         408 :         *ppszDest = static_cast<char *>(
   10406         408 :             CPLRealloc(reinterpret_cast<void *>(*ppszDest), *nDestSize));
   10407             : #ifdef NCDF_DEBUG
   10408             :         CPLDebug("GDAL_netCDF", "NCDFSafeStrcat() resized str from %ld to %ld",
   10409             :                  (*nDestSize) / 2, *nDestSize);
   10410             : #endif
   10411             :     }
   10412        5725 :     strcat(*ppszDest, pszSrc);
   10413             : 
   10414        5725 :     return CE_None;
   10415             : }
   10416             : 
   10417             : /* helper function for NCDFGetAttr() */
   10418             : /* if pdfValue != nullptr, sets *pdfValue to first value returned */
   10419             : /* if ppszValue != nullptr, sets *ppszValue with all attribute values */
   10420             : /* *ppszValue is the responsibility of the caller and must be freed */
   10421       63223 : static CPLErr NCDFGetAttr1(int nCdfId, int nVarId, const char *pszAttrName,
   10422             :                            double *pdfValue, char **ppszValue)
   10423             : {
   10424       63223 :     nc_type nAttrType = NC_NAT;
   10425       63223 :     size_t nAttrLen = 0;
   10426             : 
   10427       63223 :     if (ppszValue)
   10428       62076 :         *ppszValue = nullptr;
   10429             : 
   10430       63223 :     int status = nc_inq_att(nCdfId, nVarId, pszAttrName, &nAttrType, &nAttrLen);
   10431       63223 :     if (status != NC_NOERR)
   10432       33970 :         return CE_Failure;
   10433             : 
   10434             : #ifdef NCDF_DEBUG
   10435             :     CPLDebug("GDAL_netCDF", "NCDFGetAttr1(%s) len=%ld type=%d", pszAttrName,
   10436             :              nAttrLen, nAttrType);
   10437             : #endif
   10438       29253 :     if (nAttrLen == 0 && nAttrType != NC_CHAR)
   10439           1 :         return CE_Failure;
   10440             : 
   10441             :     /* Allocate guaranteed minimum size (use 10 or 20 if not a string) */
   10442       29252 :     size_t nAttrValueSize = nAttrLen + 1;
   10443       29252 :     if (nAttrType != NC_CHAR && nAttrValueSize < 10)
   10444        3210 :         nAttrValueSize = 10;
   10445       29252 :     if (nAttrType == NC_DOUBLE && nAttrValueSize < 20)
   10446        1562 :         nAttrValueSize = 20;
   10447       29252 :     if (nAttrType == NC_INT64 && nAttrValueSize < 20)
   10448          22 :         nAttrValueSize = 22;
   10449             :     char *pszAttrValue =
   10450       29252 :         static_cast<char *>(CPLCalloc(nAttrValueSize, sizeof(char)));
   10451       29252 :     *pszAttrValue = '\0';
   10452             : 
   10453       29252 :     if (nAttrLen > 1 && nAttrType != NC_CHAR)
   10454         596 :         NCDFSafeStrcat(&pszAttrValue, "{", &nAttrValueSize);
   10455             : 
   10456       29252 :     double dfValue = 0.0;
   10457       29252 :     size_t m = 0;
   10458             :     char szTemp[256];
   10459       29252 :     bool bSetDoubleFromStr = false;
   10460             : 
   10461       29252 :     switch (nAttrType)
   10462             :     {
   10463       26040 :         case NC_CHAR:
   10464       26040 :             CPL_IGNORE_RET_VAL(
   10465       26040 :                 nc_get_att_text(nCdfId, nVarId, pszAttrName, pszAttrValue));
   10466       26040 :             pszAttrValue[nAttrLen] = '\0';
   10467       26040 :             bSetDoubleFromStr = true;
   10468       26040 :             dfValue = 0.0;
   10469       26040 :             break;
   10470          94 :         case NC_BYTE:
   10471             :         {
   10472             :             signed char *pscTemp = static_cast<signed char *>(
   10473          94 :                 CPLCalloc(nAttrLen, sizeof(signed char)));
   10474          94 :             nc_get_att_schar(nCdfId, nVarId, pszAttrName, pscTemp);
   10475          94 :             dfValue = static_cast<double>(pscTemp[0]);
   10476          94 :             if (nAttrLen > 1)
   10477             :             {
   10478          24 :                 for (m = 0; m < nAttrLen - 1; m++)
   10479             :                 {
   10480          13 :                     snprintf(szTemp, sizeof(szTemp), "%d,", pscTemp[m]);
   10481          13 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10482             :                 }
   10483             :             }
   10484          94 :             snprintf(szTemp, sizeof(szTemp), "%d", pscTemp[m]);
   10485          94 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10486          94 :             CPLFree(pscTemp);
   10487          94 :             break;
   10488             :         }
   10489         483 :         case NC_SHORT:
   10490             :         {
   10491             :             short *psTemp =
   10492         483 :                 static_cast<short *>(CPLCalloc(nAttrLen, sizeof(short)));
   10493         483 :             nc_get_att_short(nCdfId, nVarId, pszAttrName, psTemp);
   10494         483 :             dfValue = static_cast<double>(psTemp[0]);
   10495         483 :             if (nAttrLen > 1)
   10496             :             {
   10497         760 :                 for (m = 0; m < nAttrLen - 1; m++)
   10498             :                 {
   10499         380 :                     snprintf(szTemp, sizeof(szTemp), "%d,", psTemp[m]);
   10500         380 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10501             :                 }
   10502             :             }
   10503         483 :             snprintf(szTemp, sizeof(szTemp), "%d", psTemp[m]);
   10504         483 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10505         483 :             CPLFree(psTemp);
   10506         483 :             break;
   10507             :         }
   10508         528 :         case NC_INT:
   10509             :         {
   10510         528 :             int *pnTemp = static_cast<int *>(CPLCalloc(nAttrLen, sizeof(int)));
   10511         528 :             nc_get_att_int(nCdfId, nVarId, pszAttrName, pnTemp);
   10512         528 :             dfValue = static_cast<double>(pnTemp[0]);
   10513         528 :             if (nAttrLen > 1)
   10514             :             {
   10515         218 :                 for (m = 0; m < nAttrLen - 1; m++)
   10516             :                 {
   10517         139 :                     snprintf(szTemp, sizeof(szTemp), "%d,", pnTemp[m]);
   10518         139 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10519             :                 }
   10520             :             }
   10521         528 :             snprintf(szTemp, sizeof(szTemp), "%d", pnTemp[m]);
   10522         528 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10523         528 :             CPLFree(pnTemp);
   10524         528 :             break;
   10525             :         }
   10526         393 :         case NC_FLOAT:
   10527             :         {
   10528             :             float *pfTemp =
   10529         393 :                 static_cast<float *>(CPLCalloc(nAttrLen, sizeof(float)));
   10530         393 :             nc_get_att_float(nCdfId, nVarId, pszAttrName, pfTemp);
   10531         393 :             dfValue = static_cast<double>(pfTemp[0]);
   10532         393 :             if (nAttrLen > 1)
   10533             :             {
   10534          56 :                 for (m = 0; m < nAttrLen - 1; m++)
   10535             :                 {
   10536          28 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%.8g,", pfTemp[m]);
   10537          28 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10538             :                 }
   10539             :             }
   10540         393 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.8g", pfTemp[m]);
   10541         393 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10542         393 :             CPLFree(pfTemp);
   10543         393 :             break;
   10544             :         }
   10545        1562 :         case NC_DOUBLE:
   10546             :         {
   10547             :             double *pdfTemp =
   10548        1562 :                 static_cast<double *>(CPLCalloc(nAttrLen, sizeof(double)));
   10549        1562 :             nc_get_att_double(nCdfId, nVarId, pszAttrName, pdfTemp);
   10550        1562 :             dfValue = pdfTemp[0];
   10551        1562 :             if (nAttrLen > 1)
   10552             :             {
   10553         166 :                 for (m = 0; m < nAttrLen - 1; m++)
   10554             :                 {
   10555          90 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%.16g,", pdfTemp[m]);
   10556          90 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10557             :                 }
   10558             :             }
   10559        1562 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.16g", pdfTemp[m]);
   10560        1562 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10561        1562 :             CPLFree(pdfTemp);
   10562        1562 :             break;
   10563             :         }
   10564          10 :         case NC_STRING:
   10565             :         {
   10566             :             char **ppszTemp =
   10567          10 :                 static_cast<char **>(CPLCalloc(nAttrLen, sizeof(char *)));
   10568          10 :             nc_get_att_string(nCdfId, nVarId, pszAttrName, ppszTemp);
   10569          10 :             bSetDoubleFromStr = true;
   10570          10 :             dfValue = 0.0;
   10571          10 :             if (nAttrLen > 1)
   10572             :             {
   10573          19 :                 for (m = 0; m < nAttrLen - 1; m++)
   10574             :                 {
   10575          12 :                     NCDFSafeStrcat(&pszAttrValue,
   10576          12 :                                    ppszTemp[m] ? ppszTemp[m] : "{NULL}",
   10577             :                                    &nAttrValueSize);
   10578          12 :                     NCDFSafeStrcat(&pszAttrValue, ",", &nAttrValueSize);
   10579             :                 }
   10580             :             }
   10581          10 :             NCDFSafeStrcat(&pszAttrValue, ppszTemp[m] ? ppszTemp[m] : "{NULL}",
   10582             :                            &nAttrValueSize);
   10583          10 :             nc_free_string(nAttrLen, ppszTemp);
   10584          10 :             CPLFree(ppszTemp);
   10585          10 :             break;
   10586             :         }
   10587          28 :         case NC_UBYTE:
   10588             :         {
   10589             :             unsigned char *pucTemp = static_cast<unsigned char *>(
   10590          28 :                 CPLCalloc(nAttrLen, sizeof(unsigned char)));
   10591          28 :             nc_get_att_uchar(nCdfId, nVarId, pszAttrName, pucTemp);
   10592          28 :             dfValue = static_cast<double>(pucTemp[0]);
   10593          28 :             if (nAttrLen > 1)
   10594             :             {
   10595           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10596             :                 {
   10597           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pucTemp[m]);
   10598           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10599             :                 }
   10600             :             }
   10601          28 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pucTemp[m]);
   10602          28 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10603          28 :             CPLFree(pucTemp);
   10604          28 :             break;
   10605             :         }
   10606          26 :         case NC_USHORT:
   10607             :         {
   10608             :             unsigned short *pusTemp;
   10609             :             pusTemp = static_cast<unsigned short *>(
   10610          26 :                 CPLCalloc(nAttrLen, sizeof(unsigned short)));
   10611          26 :             nc_get_att_ushort(nCdfId, nVarId, pszAttrName, pusTemp);
   10612          26 :             dfValue = static_cast<double>(pusTemp[0]);
   10613          26 :             if (nAttrLen > 1)
   10614             :             {
   10615          10 :                 for (m = 0; m < nAttrLen - 1; m++)
   10616             :                 {
   10617           5 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pusTemp[m]);
   10618           5 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10619             :                 }
   10620             :             }
   10621          26 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pusTemp[m]);
   10622          26 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10623          26 :             CPLFree(pusTemp);
   10624          26 :             break;
   10625             :         }
   10626          18 :         case NC_UINT:
   10627             :         {
   10628             :             unsigned int *punTemp =
   10629          18 :                 static_cast<unsigned int *>(CPLCalloc(nAttrLen, sizeof(int)));
   10630          18 :             nc_get_att_uint(nCdfId, nVarId, pszAttrName, punTemp);
   10631          18 :             dfValue = static_cast<double>(punTemp[0]);
   10632          18 :             if (nAttrLen > 1)
   10633             :             {
   10634           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10635             :                 {
   10636           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%u,", punTemp[m]);
   10637           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10638             :                 }
   10639             :             }
   10640          18 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", punTemp[m]);
   10641          18 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10642          18 :             CPLFree(punTemp);
   10643          18 :             break;
   10644             :         }
   10645          22 :         case NC_INT64:
   10646             :         {
   10647             :             GIntBig *panTemp =
   10648          22 :                 static_cast<GIntBig *>(CPLCalloc(nAttrLen, sizeof(GIntBig)));
   10649          22 :             nc_get_att_longlong(nCdfId, nVarId, pszAttrName, panTemp);
   10650          22 :             dfValue = static_cast<double>(panTemp[0]);
   10651          22 :             if (nAttrLen > 1)
   10652             :             {
   10653           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10654             :                 {
   10655           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB ",",
   10656           0 :                                 panTemp[m]);
   10657           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10658             :                 }
   10659             :             }
   10660          22 :             CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB, panTemp[m]);
   10661          22 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10662          22 :             CPLFree(panTemp);
   10663          22 :             break;
   10664             :         }
   10665          22 :         case NC_UINT64:
   10666             :         {
   10667             :             GUIntBig *panTemp =
   10668          22 :                 static_cast<GUIntBig *>(CPLCalloc(nAttrLen, sizeof(GUIntBig)));
   10669          22 :             nc_get_att_ulonglong(nCdfId, nVarId, pszAttrName, panTemp);
   10670          22 :             dfValue = static_cast<double>(panTemp[0]);
   10671          22 :             if (nAttrLen > 1)
   10672             :             {
   10673           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10674             :                 {
   10675           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB ",",
   10676           0 :                                 panTemp[m]);
   10677           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10678             :                 }
   10679             :             }
   10680          22 :             CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB, panTemp[m]);
   10681          22 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10682          22 :             CPLFree(panTemp);
   10683          22 :             break;
   10684             :         }
   10685          26 :         default:
   10686          26 :             CPLDebug("GDAL_netCDF",
   10687             :                      "NCDFGetAttr unsupported type %d for attribute %s",
   10688             :                      nAttrType, pszAttrName);
   10689          26 :             break;
   10690             :     }
   10691             : 
   10692       29252 :     if (nAttrLen > 1 && nAttrType != NC_CHAR)
   10693         596 :         NCDFSafeStrcat(&pszAttrValue, "}", &nAttrValueSize);
   10694             : 
   10695       29252 :     if (bSetDoubleFromStr)
   10696             :     {
   10697       26050 :         if (CPLGetValueType(pszAttrValue) == CPL_VALUE_STRING)
   10698             :         {
   10699       25868 :             if (ppszValue == nullptr && pdfValue != nullptr)
   10700             :             {
   10701           1 :                 CPLFree(pszAttrValue);
   10702           1 :                 return CE_Failure;
   10703             :             }
   10704             :         }
   10705       26049 :         dfValue = CPLAtof(pszAttrValue);
   10706             :     }
   10707             : 
   10708             :     /* set return values */
   10709       29251 :     if (ppszValue)
   10710       28939 :         *ppszValue = pszAttrValue;
   10711             :     else
   10712         312 :         CPLFree(pszAttrValue);
   10713             : 
   10714       29251 :     if (pdfValue)
   10715         312 :         *pdfValue = dfValue;
   10716             : 
   10717       29251 :     return CE_None;
   10718             : }
   10719             : 
   10720             : /* sets pdfValue to first value found */
   10721        1147 : CPLErr NCDFGetAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10722             :                    double *pdfValue)
   10723             : {
   10724        1147 :     return NCDFGetAttr1(nCdfId, nVarId, pszAttrName, pdfValue, nullptr);
   10725             : }
   10726             : 
   10727             : /* pszValue is the responsibility of the caller and must be freed */
   10728       62076 : CPLErr NCDFGetAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10729             :                    char **pszValue)
   10730             : {
   10731       62076 :     return NCDFGetAttr1(nCdfId, nVarId, pszAttrName, nullptr, pszValue);
   10732             : }
   10733             : 
   10734             : /* By default write NC_CHAR, but detect for int/float/double and */
   10735             : /* NC4 string arrays */
   10736         106 : static CPLErr NCDFPutAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10737             :                           const char *pszValue)
   10738             : {
   10739         106 :     int status = 0;
   10740         106 :     char *pszTemp = nullptr;
   10741             : 
   10742             :     /* get the attribute values as tokens */
   10743         106 :     char **papszValues = NCDFTokenizeArray(pszValue);
   10744         106 :     if (papszValues == nullptr)
   10745           0 :         return CE_Failure;
   10746             : 
   10747         106 :     size_t nAttrLen = CSLCount(papszValues);
   10748             : 
   10749             :     /* first detect type */
   10750         106 :     nc_type nAttrType = NC_CHAR;
   10751         106 :     nc_type nTmpAttrType = NC_CHAR;
   10752         225 :     for (size_t i = 0; i < nAttrLen; i++)
   10753             :     {
   10754         119 :         nTmpAttrType = NC_CHAR;
   10755         119 :         bool bFoundType = false;
   10756         119 :         errno = 0;
   10757         119 :         int nValue = static_cast<int>(strtol(papszValues[i], &pszTemp, 10));
   10758             :         /* test for int */
   10759             :         /* TODO test for Byte and short - can this be done safely? */
   10760         119 :         if (errno == 0 && papszValues[i] != pszTemp && *pszTemp == 0)
   10761             :         {
   10762             :             char szTemp[256];
   10763          19 :             CPLsnprintf(szTemp, sizeof(szTemp), "%d", nValue);
   10764          19 :             if (EQUAL(szTemp, papszValues[i]))
   10765             :             {
   10766          19 :                 bFoundType = true;
   10767          19 :                 nTmpAttrType = NC_INT;
   10768             :             }
   10769             :             else
   10770             :             {
   10771             :                 unsigned int unValue = static_cast<unsigned int>(
   10772           0 :                     strtoul(papszValues[i], &pszTemp, 10));
   10773           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u", unValue);
   10774           0 :                 if (EQUAL(szTemp, papszValues[i]))
   10775             :                 {
   10776           0 :                     bFoundType = true;
   10777           0 :                     nTmpAttrType = NC_UINT;
   10778             :                 }
   10779             :             }
   10780             :         }
   10781         119 :         if (!bFoundType)
   10782             :         {
   10783             :             /* test for double */
   10784         100 :             errno = 0;
   10785         100 :             double dfValue = CPLStrtod(papszValues[i], &pszTemp);
   10786         100 :             if ((errno == 0) && (papszValues[i] != pszTemp) && (*pszTemp == 0))
   10787             :             {
   10788             :                 // Test for float instead of double.
   10789             :                 // strtof() is C89, which is not available in MSVC.
   10790             :                 // See if we loose precision if we cast to float and write to
   10791             :                 // char*.
   10792          14 :                 float fValue = float(dfValue);
   10793             :                 char szTemp[256];
   10794          14 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%.8g", fValue);
   10795          14 :                 if (EQUAL(szTemp, papszValues[i]))
   10796           8 :                     nTmpAttrType = NC_FLOAT;
   10797             :                 else
   10798           6 :                     nTmpAttrType = NC_DOUBLE;
   10799             :             }
   10800             :         }
   10801         119 :         if ((nTmpAttrType <= NC_DOUBLE && nAttrType <= NC_DOUBLE &&
   10802          99 :              nTmpAttrType > nAttrType) ||
   10803          99 :             (nTmpAttrType == NC_UINT && nAttrType < NC_FLOAT) ||
   10804           5 :             (nTmpAttrType >= NC_FLOAT && nAttrType == NC_UINT))
   10805          20 :             nAttrType = nTmpAttrType;
   10806             :     }
   10807             : 
   10808             : #ifdef DEBUG
   10809         106 :     if (EQUAL(pszAttrName, "DEBUG_EMPTY_DOUBLE_ATTR"))
   10810             :     {
   10811           0 :         nAttrType = NC_DOUBLE;
   10812           0 :         nAttrLen = 0;
   10813             :     }
   10814             : #endif
   10815             : 
   10816             :     /* now write the data */
   10817         106 :     if (nAttrType == NC_CHAR)
   10818             :     {
   10819          86 :         int nTmpFormat = 0;
   10820          86 :         if (nAttrLen > 1)
   10821             :         {
   10822           0 :             status = nc_inq_format(nCdfId, &nTmpFormat);
   10823           0 :             NCDF_ERR(status);
   10824             :         }
   10825          86 :         if (nAttrLen > 1 && nTmpFormat == NCDF_FORMAT_NC4)
   10826           0 :             status = nc_put_att_string(nCdfId, nVarId, pszAttrName, nAttrLen,
   10827             :                                        const_cast<const char **>(papszValues));
   10828             :         else
   10829          86 :             status = nc_put_att_text(nCdfId, nVarId, pszAttrName,
   10830             :                                      strlen(pszValue), pszValue);
   10831          86 :         NCDF_ERR(status);
   10832             :     }
   10833             :     else
   10834             :     {
   10835          20 :         switch (nAttrType)
   10836             :         {
   10837          11 :             case NC_INT:
   10838             :             {
   10839             :                 int *pnTemp =
   10840          11 :                     static_cast<int *>(CPLCalloc(nAttrLen, sizeof(int)));
   10841          30 :                 for (size_t i = 0; i < nAttrLen; i++)
   10842             :                 {
   10843          19 :                     pnTemp[i] =
   10844          19 :                         static_cast<int>(strtol(papszValues[i], &pszTemp, 10));
   10845             :                 }
   10846          11 :                 status = nc_put_att_int(nCdfId, nVarId, pszAttrName, NC_INT,
   10847             :                                         nAttrLen, pnTemp);
   10848          11 :                 NCDF_ERR(status);
   10849          11 :                 CPLFree(pnTemp);
   10850          11 :                 break;
   10851             :             }
   10852           0 :             case NC_UINT:
   10853             :             {
   10854             :                 unsigned int *punTemp = static_cast<unsigned int *>(
   10855           0 :                     CPLCalloc(nAttrLen, sizeof(unsigned int)));
   10856           0 :                 for (size_t i = 0; i < nAttrLen; i++)
   10857             :                 {
   10858           0 :                     punTemp[i] = static_cast<unsigned int>(
   10859           0 :                         strtol(papszValues[i], &pszTemp, 10));
   10860             :                 }
   10861           0 :                 status = nc_put_att_uint(nCdfId, nVarId, pszAttrName, NC_UINT,
   10862             :                                          nAttrLen, punTemp);
   10863           0 :                 NCDF_ERR(status);
   10864           0 :                 CPLFree(punTemp);
   10865           0 :                 break;
   10866             :             }
   10867           6 :             case NC_FLOAT:
   10868             :             {
   10869             :                 float *pfTemp =
   10870           6 :                     static_cast<float *>(CPLCalloc(nAttrLen, sizeof(float)));
   10871          14 :                 for (size_t i = 0; i < nAttrLen; i++)
   10872             :                 {
   10873           8 :                     pfTemp[i] =
   10874           8 :                         static_cast<float>(CPLStrtod(papszValues[i], &pszTemp));
   10875             :                 }
   10876           6 :                 status = nc_put_att_float(nCdfId, nVarId, pszAttrName, NC_FLOAT,
   10877             :                                           nAttrLen, pfTemp);
   10878           6 :                 NCDF_ERR(status);
   10879           6 :                 CPLFree(pfTemp);
   10880           6 :                 break;
   10881             :             }
   10882           3 :             case NC_DOUBLE:
   10883             :             {
   10884             :                 double *pdfTemp =
   10885           3 :                     static_cast<double *>(CPLCalloc(nAttrLen, sizeof(double)));
   10886           9 :                 for (size_t i = 0; i < nAttrLen; i++)
   10887             :                 {
   10888           6 :                     pdfTemp[i] = CPLStrtod(papszValues[i], &pszTemp);
   10889             :                 }
   10890           3 :                 status = nc_put_att_double(nCdfId, nVarId, pszAttrName,
   10891             :                                            NC_DOUBLE, nAttrLen, pdfTemp);
   10892           3 :                 NCDF_ERR(status);
   10893           3 :                 CPLFree(pdfTemp);
   10894           3 :                 break;
   10895             :             }
   10896           0 :             default:
   10897           0 :                 if (papszValues)
   10898           0 :                     CSLDestroy(papszValues);
   10899           0 :                 return CE_Failure;
   10900             :                 break;
   10901             :         }
   10902             :     }
   10903             : 
   10904         106 :     if (papszValues)
   10905         106 :         CSLDestroy(papszValues);
   10906             : 
   10907         106 :     return CE_None;
   10908             : }
   10909             : 
   10910          76 : static CPLErr NCDFGet1DVar(int nCdfId, int nVarId, char **pszValue)
   10911             : {
   10912             :     /* get var information */
   10913          76 :     int nVarDimId = -1;
   10914          76 :     int status = nc_inq_varndims(nCdfId, nVarId, &nVarDimId);
   10915          76 :     if (status != NC_NOERR || nVarDimId != 1)
   10916           0 :         return CE_Failure;
   10917             : 
   10918          76 :     status = nc_inq_vardimid(nCdfId, nVarId, &nVarDimId);
   10919          76 :     if (status != NC_NOERR)
   10920           0 :         return CE_Failure;
   10921             : 
   10922          76 :     nc_type nVarType = NC_NAT;
   10923          76 :     status = nc_inq_vartype(nCdfId, nVarId, &nVarType);
   10924          76 :     if (status != NC_NOERR)
   10925           0 :         return CE_Failure;
   10926             : 
   10927          76 :     size_t nVarLen = 0;
   10928          76 :     status = nc_inq_dimlen(nCdfId, nVarDimId, &nVarLen);
   10929          76 :     if (status != NC_NOERR)
   10930           0 :         return CE_Failure;
   10931             : 
   10932          76 :     size_t start[1] = {0};
   10933          76 :     size_t count[1] = {nVarLen};
   10934             : 
   10935             :     /* Allocate guaranteed minimum size */
   10936          76 :     size_t nVarValueSize = NCDF_MAX_STR_LEN;
   10937             :     char *pszVarValue =
   10938          76 :         static_cast<char *>(CPLCalloc(nVarValueSize, sizeof(char)));
   10939          76 :     *pszVarValue = '\0';
   10940             : 
   10941          76 :     if (nVarLen == 0)
   10942             :     {
   10943             :         /* set return values */
   10944           1 :         *pszValue = pszVarValue;
   10945             : 
   10946           1 :         return CE_None;
   10947             :     }
   10948             : 
   10949          75 :     if (nVarLen > 1 && nVarType != NC_CHAR)
   10950          40 :         NCDFSafeStrcat(&pszVarValue, "{", &nVarValueSize);
   10951             : 
   10952          75 :     switch (nVarType)
   10953             :     {
   10954           0 :         case NC_CHAR:
   10955           0 :             nc_get_vara_text(nCdfId, nVarId, start, count, pszVarValue);
   10956           0 :             pszVarValue[nVarLen] = '\0';
   10957           0 :             break;
   10958           0 :         case NC_BYTE:
   10959             :         {
   10960             :             signed char *pscTemp = static_cast<signed char *>(
   10961           0 :                 CPLCalloc(nVarLen, sizeof(signed char)));
   10962           0 :             nc_get_vara_schar(nCdfId, nVarId, start, count, pscTemp);
   10963             :             char szTemp[256];
   10964           0 :             size_t m = 0;
   10965           0 :             for (; m < nVarLen - 1; m++)
   10966             :             {
   10967           0 :                 snprintf(szTemp, sizeof(szTemp), "%d,", pscTemp[m]);
   10968           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   10969             :             }
   10970           0 :             snprintf(szTemp, sizeof(szTemp), "%d", pscTemp[m]);
   10971           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   10972           0 :             CPLFree(pscTemp);
   10973           0 :             break;
   10974             :         }
   10975           0 :         case NC_SHORT:
   10976             :         {
   10977             :             short *psTemp =
   10978           0 :                 static_cast<short *>(CPLCalloc(nVarLen, sizeof(short)));
   10979           0 :             nc_get_vara_short(nCdfId, nVarId, start, count, psTemp);
   10980             :             char szTemp[256];
   10981           0 :             size_t m = 0;
   10982           0 :             for (; m < nVarLen - 1; m++)
   10983             :             {
   10984           0 :                 snprintf(szTemp, sizeof(szTemp), "%d,", psTemp[m]);
   10985           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   10986             :             }
   10987           0 :             snprintf(szTemp, sizeof(szTemp), "%d", psTemp[m]);
   10988           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   10989           0 :             CPLFree(psTemp);
   10990           0 :             break;
   10991             :         }
   10992          19 :         case NC_INT:
   10993             :         {
   10994          19 :             int *pnTemp = static_cast<int *>(CPLCalloc(nVarLen, sizeof(int)));
   10995          19 :             nc_get_vara_int(nCdfId, nVarId, start, count, pnTemp);
   10996             :             char szTemp[256];
   10997          19 :             size_t m = 0;
   10998          36 :             for (; m < nVarLen - 1; m++)
   10999             :             {
   11000          17 :                 snprintf(szTemp, sizeof(szTemp), "%d,", pnTemp[m]);
   11001          17 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11002             :             }
   11003          19 :             snprintf(szTemp, sizeof(szTemp), "%d", pnTemp[m]);
   11004          19 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11005          19 :             CPLFree(pnTemp);
   11006          19 :             break;
   11007             :         }
   11008           8 :         case NC_FLOAT:
   11009             :         {
   11010             :             float *pfTemp =
   11011           8 :                 static_cast<float *>(CPLCalloc(nVarLen, sizeof(float)));
   11012           8 :             nc_get_vara_float(nCdfId, nVarId, start, count, pfTemp);
   11013             :             char szTemp[256];
   11014           8 :             size_t m = 0;
   11015         325 :             for (; m < nVarLen - 1; m++)
   11016             :             {
   11017         317 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%.8g,", pfTemp[m]);
   11018         317 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11019             :             }
   11020           8 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.8g", pfTemp[m]);
   11021           8 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11022           8 :             CPLFree(pfTemp);
   11023           8 :             break;
   11024             :         }
   11025          47 :         case NC_DOUBLE:
   11026             :         {
   11027             :             double *pdfTemp =
   11028          47 :                 static_cast<double *>(CPLCalloc(nVarLen, sizeof(double)));
   11029          47 :             nc_get_vara_double(nCdfId, nVarId, start, count, pdfTemp);
   11030             :             char szTemp[256];
   11031          47 :             size_t m = 0;
   11032         225 :             for (; m < nVarLen - 1; m++)
   11033             :             {
   11034         178 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%.16g,", pdfTemp[m]);
   11035         178 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11036             :             }
   11037          47 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.16g", pdfTemp[m]);
   11038          47 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11039          47 :             CPLFree(pdfTemp);
   11040          47 :             break;
   11041             :         }
   11042           0 :         case NC_STRING:
   11043             :         {
   11044             :             char **ppszTemp =
   11045           0 :                 static_cast<char **>(CPLCalloc(nVarLen, sizeof(char *)));
   11046           0 :             nc_get_vara_string(nCdfId, nVarId, start, count, ppszTemp);
   11047           0 :             size_t m = 0;
   11048           0 :             for (; m < nVarLen - 1; m++)
   11049             :             {
   11050           0 :                 NCDFSafeStrcat(&pszVarValue, ppszTemp[m], &nVarValueSize);
   11051           0 :                 NCDFSafeStrcat(&pszVarValue, ",", &nVarValueSize);
   11052             :             }
   11053           0 :             NCDFSafeStrcat(&pszVarValue, ppszTemp[m], &nVarValueSize);
   11054           0 :             nc_free_string(nVarLen, ppszTemp);
   11055           0 :             CPLFree(ppszTemp);
   11056           0 :             break;
   11057             :         }
   11058           0 :         case NC_UBYTE:
   11059             :         {
   11060             :             unsigned char *pucTemp;
   11061             :             pucTemp = static_cast<unsigned char *>(
   11062           0 :                 CPLCalloc(nVarLen, sizeof(unsigned char)));
   11063           0 :             nc_get_vara_uchar(nCdfId, nVarId, start, count, pucTemp);
   11064             :             char szTemp[256];
   11065           0 :             size_t m = 0;
   11066           0 :             for (; m < nVarLen - 1; m++)
   11067             :             {
   11068           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pucTemp[m]);
   11069           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11070             :             }
   11071           0 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pucTemp[m]);
   11072           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11073           0 :             CPLFree(pucTemp);
   11074           0 :             break;
   11075             :         }
   11076           0 :         case NC_USHORT:
   11077             :         {
   11078             :             unsigned short *pusTemp;
   11079             :             pusTemp = static_cast<unsigned short *>(
   11080           0 :                 CPLCalloc(nVarLen, sizeof(unsigned short)));
   11081           0 :             nc_get_vara_ushort(nCdfId, nVarId, start, count, pusTemp);
   11082             :             char szTemp[256];
   11083           0 :             size_t m = 0;
   11084           0 :             for (; m < nVarLen - 1; m++)
   11085             :             {
   11086           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pusTemp[m]);
   11087           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11088             :             }
   11089           0 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pusTemp[m]);
   11090           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11091           0 :             CPLFree(pusTemp);
   11092           0 :             break;
   11093             :         }
   11094           0 :         case NC_UINT:
   11095             :         {
   11096             :             unsigned int *punTemp;
   11097             :             punTemp = static_cast<unsigned int *>(
   11098           0 :                 CPLCalloc(nVarLen, sizeof(unsigned int)));
   11099           0 :             nc_get_vara_uint(nCdfId, nVarId, start, count, punTemp);
   11100             :             char szTemp[256];
   11101           0 :             size_t m = 0;
   11102           0 :             for (; m < nVarLen - 1; m++)
   11103             :             {
   11104           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u,", punTemp[m]);
   11105           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11106             :             }
   11107           0 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", punTemp[m]);
   11108           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11109           0 :             CPLFree(punTemp);
   11110           0 :             break;
   11111             :         }
   11112           1 :         case NC_INT64:
   11113             :         {
   11114             :             long long *pnTemp =
   11115           1 :                 static_cast<long long *>(CPLCalloc(nVarLen, sizeof(long long)));
   11116           1 :             nc_get_vara_longlong(nCdfId, nVarId, start, count, pnTemp);
   11117             :             char szTemp[256];
   11118           1 :             size_t m = 0;
   11119           2 :             for (; m < nVarLen - 1; m++)
   11120             :             {
   11121           1 :                 snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB ",", pnTemp[m]);
   11122           1 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11123             :             }
   11124           1 :             snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB, pnTemp[m]);
   11125           1 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11126           1 :             CPLFree(pnTemp);
   11127           1 :             break;
   11128             :         }
   11129           0 :         case NC_UINT64:
   11130             :         {
   11131             :             unsigned long long *pnTemp = static_cast<unsigned long long *>(
   11132           0 :                 CPLCalloc(nVarLen, sizeof(unsigned long long)));
   11133           0 :             nc_get_vara_ulonglong(nCdfId, nVarId, start, count, pnTemp);
   11134             :             char szTemp[256];
   11135           0 :             size_t m = 0;
   11136           0 :             for (; m < nVarLen - 1; m++)
   11137             :             {
   11138           0 :                 snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB ",", pnTemp[m]);
   11139           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11140             :             }
   11141           0 :             snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB, pnTemp[m]);
   11142           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11143           0 :             CPLFree(pnTemp);
   11144           0 :             break;
   11145             :         }
   11146           0 :         default:
   11147           0 :             CPLDebug("GDAL_netCDF", "NCDFGetVar1D unsupported type %d",
   11148             :                      nVarType);
   11149           0 :             CPLFree(pszVarValue);
   11150           0 :             pszVarValue = nullptr;
   11151           0 :             break;
   11152             :     }
   11153             : 
   11154          75 :     if (pszVarValue != nullptr && nVarLen > 1 && nVarType != NC_CHAR)
   11155          40 :         NCDFSafeStrcat(&pszVarValue, "}", &nVarValueSize);
   11156             : 
   11157             :     /* set return values */
   11158          75 :     *pszValue = pszVarValue;
   11159             : 
   11160          75 :     return CE_None;
   11161             : }
   11162             : 
   11163           8 : static CPLErr NCDFPut1DVar(int nCdfId, int nVarId, const char *pszValue)
   11164             : {
   11165           8 :     if (EQUAL(pszValue, ""))
   11166           0 :         return CE_Failure;
   11167             : 
   11168             :     /* get var information */
   11169           8 :     int nVarDimId = -1;
   11170           8 :     int status = nc_inq_varndims(nCdfId, nVarId, &nVarDimId);
   11171           8 :     if (status != NC_NOERR || nVarDimId != 1)
   11172           0 :         return CE_Failure;
   11173             : 
   11174           8 :     status = nc_inq_vardimid(nCdfId, nVarId, &nVarDimId);
   11175           8 :     if (status != NC_NOERR)
   11176           0 :         return CE_Failure;
   11177             : 
   11178           8 :     nc_type nVarType = NC_CHAR;
   11179           8 :     status = nc_inq_vartype(nCdfId, nVarId, &nVarType);
   11180           8 :     if (status != NC_NOERR)
   11181           0 :         return CE_Failure;
   11182             : 
   11183           8 :     size_t nVarLen = 0;
   11184           8 :     status = nc_inq_dimlen(nCdfId, nVarDimId, &nVarLen);
   11185           8 :     if (status != NC_NOERR)
   11186           0 :         return CE_Failure;
   11187             : 
   11188           8 :     size_t start[1] = {0};
   11189           8 :     size_t count[1] = {nVarLen};
   11190             : 
   11191             :     /* get the values as tokens */
   11192           8 :     char **papszValues = NCDFTokenizeArray(pszValue);
   11193           8 :     if (papszValues == nullptr)
   11194           0 :         return CE_Failure;
   11195             : 
   11196           8 :     nVarLen = CSLCount(papszValues);
   11197             : 
   11198             :     /* now write the data */
   11199           8 :     if (nVarType == NC_CHAR)
   11200             :     {
   11201           0 :         status = nc_put_vara_text(nCdfId, nVarId, start, count, pszValue);
   11202           0 :         NCDF_ERR(status);
   11203             :     }
   11204             :     else
   11205             :     {
   11206           8 :         switch (nVarType)
   11207             :         {
   11208           0 :             case NC_BYTE:
   11209             :             {
   11210             :                 signed char *pscTemp = static_cast<signed char *>(
   11211           0 :                     CPLCalloc(nVarLen, sizeof(signed char)));
   11212           0 :                 for (size_t i = 0; i < nVarLen; i++)
   11213             :                 {
   11214           0 :                     char *pszTemp = nullptr;
   11215           0 :                     pscTemp[i] = static_cast<signed char>(
   11216           0 :                         strtol(papszValues[i], &pszTemp, 10));
   11217             :                 }
   11218             :                 status =
   11219           0 :                     nc_put_vara_schar(nCdfId, nVarId, start, count, pscTemp);
   11220           0 :                 NCDF_ERR(status);
   11221           0 :                 CPLFree(pscTemp);
   11222           0 :                 break;
   11223             :             }
   11224           0 :             case NC_SHORT:
   11225             :             {
   11226             :                 short *psTemp =
   11227           0 :                     static_cast<short *>(CPLCalloc(nVarLen, sizeof(short)));
   11228           0 :                 for (size_t i = 0; i < nVarLen; i++)
   11229             :                 {
   11230           0 :                     char *pszTemp = nullptr;
   11231           0 :                     psTemp[i] = static_cast<short>(
   11232           0 :                         strtol(papszValues[i], &pszTemp, 10));
   11233             :                 }
   11234             :                 status =
   11235           0 :                     nc_put_vara_short(nCdfId, nVarId, start, count, psTemp);
   11236           0 :                 NCDF_ERR(status);
   11237           0 :                 CPLFree(psTemp);
   11238           0 :                 break;
   11239             :             }
   11240           3 :             case NC_INT:
   11241             :             {
   11242             :                 int *pnTemp =
   11243           3 :                     static_cast<int *>(CPLCalloc(nVarLen, sizeof(int)));
   11244          11 :                 for (size_t i = 0; i < nVarLen; i++)
   11245             :                 {
   11246           8 :                     char *pszTemp = nullptr;
   11247           8 :                     pnTemp[i] =
   11248           8 :                         static_cast<int>(strtol(papszValues[i], &pszTemp, 10));
   11249             :                 }
   11250           3 :                 status = nc_put_vara_int(nCdfId, nVarId, start, count, pnTemp);
   11251           3 :                 NCDF_ERR(status);
   11252           3 :                 CPLFree(pnTemp);
   11253           3 :                 break;
   11254             :             }
   11255           0 :             case NC_FLOAT:
   11256             :             {
   11257             :                 float *pfTemp =
   11258           0 :                     static_cast<float *>(CPLCalloc(nVarLen, sizeof(float)));
   11259           0 :                 for (size_t i = 0; i < nVarLen; i++)
   11260             :                 {
   11261           0 :                     char *pszTemp = nullptr;
   11262           0 :                     pfTemp[i] =
   11263           0 :                         static_cast<float>(CPLStrtod(papszValues[i], &pszTemp));
   11264             :                 }
   11265             :                 status =
   11266           0 :                     nc_put_vara_float(nCdfId, nVarId, start, count, pfTemp);
   11267           0 :                 NCDF_ERR(status);
   11268           0 :                 CPLFree(pfTemp);
   11269           0 :                 break;
   11270             :             }
   11271           5 :             case NC_DOUBLE:
   11272             :             {
   11273             :                 double *pdfTemp =
   11274           5 :                     static_cast<double *>(CPLCalloc(nVarLen, sizeof(double)));
   11275          19 :                 for (size_t i = 0; i < nVarLen; i++)
   11276             :                 {
   11277          14 :                     char *pszTemp = nullptr;
   11278          14 :                     pdfTemp[i] = CPLStrtod(papszValues[i], &pszTemp);
   11279             :                 }
   11280             :                 status =
   11281           5 :                     nc_put_vara_double(nCdfId, nVarId, start, count, pdfTemp);
   11282           5 :                 NCDF_ERR(status);
   11283           5 :                 CPLFree(pdfTemp);
   11284           5 :                 break;
   11285             :             }
   11286           0 :             default:
   11287             :             {
   11288           0 :                 int nTmpFormat = 0;
   11289           0 :                 status = nc_inq_format(nCdfId, &nTmpFormat);
   11290           0 :                 NCDF_ERR(status);
   11291           0 :                 if (nTmpFormat == NCDF_FORMAT_NC4)
   11292             :                 {
   11293           0 :                     switch (nVarType)
   11294             :                     {
   11295           0 :                         case NC_STRING:
   11296             :                         {
   11297             :                             status =
   11298           0 :                                 nc_put_vara_string(nCdfId, nVarId, start, count,
   11299             :                                                    (const char **)papszValues);
   11300           0 :                             NCDF_ERR(status);
   11301           0 :                             break;
   11302             :                         }
   11303           0 :                         case NC_UBYTE:
   11304             :                         {
   11305             :                             unsigned char *pucTemp =
   11306             :                                 static_cast<unsigned char *>(
   11307           0 :                                     CPLCalloc(nVarLen, sizeof(unsigned char)));
   11308           0 :                             for (size_t i = 0; i < nVarLen; i++)
   11309             :                             {
   11310           0 :                                 char *pszTemp = nullptr;
   11311           0 :                                 pucTemp[i] = static_cast<unsigned char>(
   11312           0 :                                     strtoul(papszValues[i], &pszTemp, 10));
   11313             :                             }
   11314           0 :                             status = nc_put_vara_uchar(nCdfId, nVarId, start,
   11315             :                                                        count, pucTemp);
   11316           0 :                             NCDF_ERR(status);
   11317           0 :                             CPLFree(pucTemp);
   11318           0 :                             break;
   11319             :                         }
   11320           0 :                         case NC_USHORT:
   11321             :                         {
   11322             :                             unsigned short *pusTemp =
   11323             :                                 static_cast<unsigned short *>(
   11324           0 :                                     CPLCalloc(nVarLen, sizeof(unsigned short)));
   11325           0 :                             for (size_t i = 0; i < nVarLen; i++)
   11326             :                             {
   11327           0 :                                 char *pszTemp = nullptr;
   11328           0 :                                 pusTemp[i] = static_cast<unsigned short>(
   11329           0 :                                     strtoul(papszValues[i], &pszTemp, 10));
   11330             :                             }
   11331           0 :                             status = nc_put_vara_ushort(nCdfId, nVarId, start,
   11332             :                                                         count, pusTemp);
   11333           0 :                             NCDF_ERR(status);
   11334           0 :                             CPLFree(pusTemp);
   11335           0 :                             break;
   11336             :                         }
   11337           0 :                         case NC_UINT:
   11338             :                         {
   11339             :                             unsigned int *punTemp = static_cast<unsigned int *>(
   11340           0 :                                 CPLCalloc(nVarLen, sizeof(unsigned int)));
   11341           0 :                             for (size_t i = 0; i < nVarLen; i++)
   11342             :                             {
   11343           0 :                                 char *pszTemp = nullptr;
   11344           0 :                                 punTemp[i] = static_cast<unsigned int>(
   11345           0 :                                     strtoul(papszValues[i], &pszTemp, 10));
   11346             :                             }
   11347           0 :                             status = nc_put_vara_uint(nCdfId, nVarId, start,
   11348             :                                                       count, punTemp);
   11349           0 :                             NCDF_ERR(status);
   11350           0 :                             CPLFree(punTemp);
   11351           0 :                             break;
   11352             :                         }
   11353           0 :                         default:
   11354           0 :                             if (papszValues)
   11355           0 :                                 CSLDestroy(papszValues);
   11356           0 :                             return CE_Failure;
   11357             :                             break;
   11358             :                     }
   11359             :                 }
   11360           0 :                 break;
   11361             :             }
   11362             :         }
   11363             :     }
   11364             : 
   11365           8 :     if (papszValues)
   11366           8 :         CSLDestroy(papszValues);
   11367             : 
   11368           8 :     return CE_None;
   11369             : }
   11370             : 
   11371             : /************************************************************************/
   11372             : /*                           GetDefaultNoDataValue()                    */
   11373             : /************************************************************************/
   11374             : 
   11375         188 : double NCDFGetDefaultNoDataValue(int nCdfId, int nVarId, int nVarType,
   11376             :                                  bool &bGotNoData)
   11377             : 
   11378             : {
   11379         188 :     int nNoFill = 0;
   11380         188 :     double dfNoData = 0.0;
   11381             : 
   11382         188 :     switch (nVarType)
   11383             :     {
   11384           0 :         case NC_CHAR:
   11385             :         case NC_BYTE:
   11386             :         case NC_UBYTE:
   11387             :             // Don't do default fill-values for bytes, too risky.
   11388             :             // This function should not be called in those cases.
   11389           0 :             CPLAssert(false);
   11390             :             break;
   11391          24 :         case NC_SHORT:
   11392             :         {
   11393          24 :             short nFillVal = 0;
   11394          24 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11395             :                 NC_NOERR)
   11396             :             {
   11397          24 :                 if (!nNoFill)
   11398             :                 {
   11399          23 :                     bGotNoData = true;
   11400          23 :                     dfNoData = nFillVal;
   11401             :                 }
   11402             :             }
   11403             :             else
   11404           0 :                 dfNoData = NC_FILL_SHORT;
   11405          24 :             break;
   11406             :         }
   11407          26 :         case NC_INT:
   11408             :         {
   11409          26 :             int nFillVal = 0;
   11410          26 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11411             :                 NC_NOERR)
   11412             :             {
   11413          26 :                 if (!nNoFill)
   11414             :                 {
   11415          25 :                     bGotNoData = true;
   11416          25 :                     dfNoData = nFillVal;
   11417             :                 }
   11418             :             }
   11419             :             else
   11420           0 :                 dfNoData = NC_FILL_INT;
   11421          26 :             break;
   11422             :         }
   11423          71 :         case NC_FLOAT:
   11424             :         {
   11425          71 :             float fFillVal = 0;
   11426          71 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &fFillVal) ==
   11427             :                 NC_NOERR)
   11428             :             {
   11429          71 :                 if (!nNoFill)
   11430             :                 {
   11431          67 :                     bGotNoData = true;
   11432          67 :                     dfNoData = fFillVal;
   11433             :                 }
   11434             :             }
   11435             :             else
   11436           0 :                 dfNoData = NC_FILL_FLOAT;
   11437          71 :             break;
   11438             :         }
   11439          34 :         case NC_DOUBLE:
   11440             :         {
   11441          34 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &dfNoData) ==
   11442             :                 NC_NOERR)
   11443             :             {
   11444          34 :                 if (!nNoFill)
   11445             :                 {
   11446          34 :                     bGotNoData = true;
   11447             :                 }
   11448             :             }
   11449             :             else
   11450           0 :                 dfNoData = NC_FILL_DOUBLE;
   11451          34 :             break;
   11452             :         }
   11453           7 :         case NC_USHORT:
   11454             :         {
   11455           7 :             unsigned short nFillVal = 0;
   11456           7 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11457             :                 NC_NOERR)
   11458             :             {
   11459           7 :                 if (!nNoFill)
   11460             :                 {
   11461           7 :                     bGotNoData = true;
   11462           7 :                     dfNoData = nFillVal;
   11463             :                 }
   11464             :             }
   11465             :             else
   11466           0 :                 dfNoData = NC_FILL_USHORT;
   11467           7 :             break;
   11468             :         }
   11469           7 :         case NC_UINT:
   11470             :         {
   11471           7 :             unsigned int nFillVal = 0;
   11472           7 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11473             :                 NC_NOERR)
   11474             :             {
   11475           7 :                 if (!nNoFill)
   11476             :                 {
   11477           7 :                     bGotNoData = true;
   11478           7 :                     dfNoData = nFillVal;
   11479             :                 }
   11480             :             }
   11481             :             else
   11482           0 :                 dfNoData = NC_FILL_UINT;
   11483           7 :             break;
   11484             :         }
   11485          19 :         default:
   11486          19 :             dfNoData = 0.0;
   11487          19 :             break;
   11488             :     }
   11489             : 
   11490         188 :     return dfNoData;
   11491             : }
   11492             : 
   11493             : /************************************************************************/
   11494             : /*                      NCDFGetDefaultNoDataValueAsInt64()              */
   11495             : /************************************************************************/
   11496             : 
   11497           2 : int64_t NCDFGetDefaultNoDataValueAsInt64(int nCdfId, int nVarId,
   11498             :                                          bool &bGotNoData)
   11499             : 
   11500             : {
   11501           2 :     int nNoFill = 0;
   11502           2 :     long long nFillVal = 0;
   11503           2 :     if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) == NC_NOERR)
   11504             :     {
   11505           2 :         if (!nNoFill)
   11506             :         {
   11507           2 :             bGotNoData = true;
   11508           2 :             return static_cast<int64_t>(nFillVal);
   11509             :         }
   11510             :     }
   11511             :     else
   11512           0 :         return static_cast<int64_t>(NC_FILL_INT64);
   11513           0 :     return 0;
   11514             : }
   11515             : 
   11516             : /************************************************************************/
   11517             : /*                     NCDFGetDefaultNoDataValueAsUInt64()              */
   11518             : /************************************************************************/
   11519             : 
   11520           1 : uint64_t NCDFGetDefaultNoDataValueAsUInt64(int nCdfId, int nVarId,
   11521             :                                            bool &bGotNoData)
   11522             : 
   11523             : {
   11524           1 :     int nNoFill = 0;
   11525           1 :     unsigned long long nFillVal = 0;
   11526           1 :     if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) == NC_NOERR)
   11527             :     {
   11528           1 :         if (!nNoFill)
   11529             :         {
   11530           1 :             bGotNoData = true;
   11531           1 :             return static_cast<uint64_t>(nFillVal);
   11532             :         }
   11533             :     }
   11534             :     else
   11535           0 :         return static_cast<uint64_t>(NC_FILL_UINT64);
   11536           0 :     return 0;
   11537             : }
   11538             : 
   11539       11087 : static int NCDFDoesVarContainAttribVal(int nCdfId,
   11540             :                                        const char *const *papszAttribNames,
   11541             :                                        const char *const *papszAttribValues,
   11542             :                                        int nVarId, const char *pszVarName,
   11543             :                                        bool bStrict = true)
   11544             : {
   11545       11087 :     if (nVarId == -1 && pszVarName != nullptr)
   11546        8116 :         NCDFResolveVar(nCdfId, pszVarName, &nCdfId, &nVarId);
   11547             : 
   11548       11087 :     if (nVarId == -1)
   11549         878 :         return -1;
   11550             : 
   11551       10209 :     bool bFound = false;
   11552       47648 :     for (int i = 0; !bFound && papszAttribNames != nullptr &&
   11553       45390 :                     papszAttribNames[i] != nullptr;
   11554             :          i++)
   11555             :     {
   11556       37439 :         char *pszTemp = nullptr;
   11557       37439 :         if (NCDFGetAttr(nCdfId, nVarId, papszAttribNames[i], &pszTemp) ==
   11558       53756 :                 CE_None &&
   11559       16317 :             pszTemp != nullptr)
   11560             :         {
   11561       16317 :             if (bStrict)
   11562             :             {
   11563       16317 :                 if (EQUAL(pszTemp, papszAttribValues[i]))
   11564        2258 :                     bFound = true;
   11565             :             }
   11566             :             else
   11567             :             {
   11568           0 :                 if (EQUALN(pszTemp, papszAttribValues[i],
   11569             :                            strlen(papszAttribValues[i])))
   11570           0 :                     bFound = true;
   11571             :             }
   11572       16317 :             CPLFree(pszTemp);
   11573             :         }
   11574             :     }
   11575       10209 :     return bFound;
   11576             : }
   11577             : 
   11578        1958 : static int NCDFDoesVarContainAttribVal2(int nCdfId, const char *papszAttribName,
   11579             :                                         const char *const *papszAttribValues,
   11580             :                                         int nVarId, const char *pszVarName,
   11581             :                                         int bStrict = true)
   11582             : {
   11583        1958 :     if (nVarId == -1 && pszVarName != nullptr)
   11584        1575 :         NCDFResolveVar(nCdfId, pszVarName, &nCdfId, &nVarId);
   11585             : 
   11586        1958 :     if (nVarId == -1)
   11587           0 :         return -1;
   11588             : 
   11589        1958 :     bool bFound = false;
   11590        1958 :     char *pszTemp = nullptr;
   11591        2337 :     if (NCDFGetAttr(nCdfId, nVarId, papszAttribName, &pszTemp) != CE_None ||
   11592         379 :         pszTemp == nullptr)
   11593        1579 :         return FALSE;
   11594             : 
   11595        7703 :     for (int i = 0; !bFound && i < CSLCount(papszAttribValues); i++)
   11596             :     {
   11597        7324 :         if (bStrict)
   11598             :         {
   11599        7296 :             if (EQUAL(pszTemp, papszAttribValues[i]))
   11600          31 :                 bFound = true;
   11601             :         }
   11602             :         else
   11603             :         {
   11604          28 :             if (EQUALN(pszTemp, papszAttribValues[i],
   11605             :                        strlen(papszAttribValues[i])))
   11606           0 :                 bFound = true;
   11607             :         }
   11608             :     }
   11609             : 
   11610         379 :     CPLFree(pszTemp);
   11611             : 
   11612         379 :     return bFound;
   11613             : }
   11614             : 
   11615         876 : static bool NCDFEqual(const char *papszName, const char *const *papszValues)
   11616             : {
   11617         876 :     if (papszName == nullptr || EQUAL(papszName, ""))
   11618           0 :         return false;
   11619             : 
   11620        2392 :     for (int i = 0; papszValues && papszValues[i]; ++i)
   11621             :     {
   11622        1636 :         if (EQUAL(papszName, papszValues[i]))
   11623         120 :             return true;
   11624             :     }
   11625             : 
   11626         756 :     return false;
   11627             : }
   11628             : 
   11629             : // Test that a variable is longitude/latitude coordinate,
   11630             : // following CF 4.1 and 4.2.
   11631        3782 : bool NCDFIsVarLongitude(int nCdfId, int nVarId, const char *pszVarName)
   11632             : {
   11633             :     // Check for matching attributes.
   11634        3782 :     int bVal = NCDFDoesVarContainAttribVal(nCdfId, papszCFLongitudeAttribNames,
   11635             :                                            papszCFLongitudeAttribValues, nVarId,
   11636             :                                            pszVarName);
   11637             :     // If not found using attributes then check using var name
   11638             :     // unless GDAL_NETCDF_VERIFY_DIMS=STRICT.
   11639        3782 :     if (bVal == -1)
   11640             :     {
   11641         280 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11642             :                    "STRICT"))
   11643         280 :             bVal = NCDFEqual(pszVarName, papszCFLongitudeVarNames);
   11644             :         else
   11645           0 :             bVal = FALSE;
   11646             :     }
   11647        3502 :     else if (bVal)
   11648             :     {
   11649             :         // Check that the units is not 'm' or '1'. See #6759
   11650         789 :         char *pszTemp = nullptr;
   11651        1169 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11652         380 :             pszTemp != nullptr)
   11653             :         {
   11654         380 :             if (EQUAL(pszTemp, "m") || EQUAL(pszTemp, "1"))
   11655          97 :                 bVal = false;
   11656         380 :             CPLFree(pszTemp);
   11657             :         }
   11658             :     }
   11659             : 
   11660        3782 :     return CPL_TO_BOOL(bVal);
   11661             : }
   11662             : 
   11663        2167 : bool NCDFIsVarLatitude(int nCdfId, int nVarId, const char *pszVarName)
   11664             : {
   11665        2167 :     int bVal = NCDFDoesVarContainAttribVal(nCdfId, papszCFLatitudeAttribNames,
   11666             :                                            papszCFLatitudeAttribValues, nVarId,
   11667             :                                            pszVarName);
   11668        2167 :     if (bVal == -1)
   11669             :     {
   11670         163 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11671             :                    "STRICT"))
   11672         163 :             bVal = NCDFEqual(pszVarName, papszCFLatitudeVarNames);
   11673             :         else
   11674           0 :             bVal = FALSE;
   11675             :     }
   11676        2004 :     else if (bVal)
   11677             :     {
   11678             :         // Check that the units is not 'm' or '1'. See #6759
   11679         543 :         char *pszTemp = nullptr;
   11680         685 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11681         142 :             pszTemp != nullptr)
   11682             :         {
   11683         142 :             if (EQUAL(pszTemp, "m") || EQUAL(pszTemp, "1"))
   11684          36 :                 bVal = false;
   11685         142 :             CPLFree(pszTemp);
   11686             :         }
   11687             :     }
   11688             : 
   11689        2167 :     return CPL_TO_BOOL(bVal);
   11690             : }
   11691             : 
   11692        2293 : bool NCDFIsVarProjectionX(int nCdfId, int nVarId, const char *pszVarName)
   11693             : {
   11694        2293 :     int bVal = NCDFDoesVarContainAttribVal(
   11695             :         nCdfId, papszCFProjectionXAttribNames, papszCFProjectionXAttribValues,
   11696             :         nVarId, pszVarName);
   11697        2293 :     if (bVal == -1)
   11698             :     {
   11699         274 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11700             :                    "STRICT"))
   11701         274 :             bVal = NCDFEqual(pszVarName, papszCFProjectionXVarNames);
   11702             :         else
   11703           0 :             bVal = FALSE;
   11704             :     }
   11705        2019 :     else if (bVal)
   11706             :     {
   11707             :         // Check that the units is not '1'
   11708         374 :         char *pszTemp = nullptr;
   11709         533 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11710         159 :             pszTemp != nullptr)
   11711             :         {
   11712         159 :             if (EQUAL(pszTemp, "1"))
   11713           5 :                 bVal = false;
   11714         159 :             CPLFree(pszTemp);
   11715             :         }
   11716             :     }
   11717             : 
   11718        2293 :     return CPL_TO_BOOL(bVal);
   11719             : }
   11720             : 
   11721        1623 : bool NCDFIsVarProjectionY(int nCdfId, int nVarId, const char *pszVarName)
   11722             : {
   11723        1623 :     int bVal = NCDFDoesVarContainAttribVal(
   11724             :         nCdfId, papszCFProjectionYAttribNames, papszCFProjectionYAttribValues,
   11725             :         nVarId, pszVarName);
   11726        1623 :     if (bVal == -1)
   11727             :     {
   11728         159 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11729             :                    "STRICT"))
   11730         159 :             bVal = NCDFEqual(pszVarName, papszCFProjectionYVarNames);
   11731             :         else
   11732           0 :             bVal = FALSE;
   11733             :     }
   11734        1464 :     else if (bVal)
   11735             :     {
   11736             :         // Check that the units is not '1'
   11737         373 :         char *pszTemp = nullptr;
   11738         531 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11739         158 :             pszTemp != nullptr)
   11740             :         {
   11741         158 :             if (EQUAL(pszTemp, "1"))
   11742           5 :                 bVal = false;
   11743         158 :             CPLFree(pszTemp);
   11744             :         }
   11745             :     }
   11746             : 
   11747        1623 :     return CPL_TO_BOOL(bVal);
   11748             : }
   11749             : 
   11750             : /* test that a variable is a vertical coordinate, following CF 4.3 */
   11751        1020 : bool NCDFIsVarVerticalCoord(int nCdfId, int nVarId, const char *pszVarName)
   11752             : {
   11753             :     /* check for matching attributes */
   11754        1020 :     if (NCDFDoesVarContainAttribVal(nCdfId, papszCFVerticalAttribNames,
   11755             :                                     papszCFVerticalAttribValues, nVarId,
   11756        1020 :                                     pszVarName))
   11757          72 :         return true;
   11758             :     /* check for matching units */
   11759         948 :     else if (NCDFDoesVarContainAttribVal2(nCdfId, CF_UNITS,
   11760             :                                           papszCFVerticalUnitsValues, nVarId,
   11761         948 :                                           pszVarName))
   11762          31 :         return true;
   11763             :     /* check for matching standard name */
   11764         917 :     else if (NCDFDoesVarContainAttribVal2(nCdfId, CF_STD_NAME,
   11765             :                                           papszCFVerticalStandardNameValues,
   11766         917 :                                           nVarId, pszVarName))
   11767           0 :         return true;
   11768             :     else
   11769         917 :         return false;
   11770             : }
   11771             : 
   11772             : /* test that a variable is a time coordinate, following CF 4.4 */
   11773         202 : bool NCDFIsVarTimeCoord(int nCdfId, int nVarId, const char *pszVarName)
   11774             : {
   11775             :     /* check for matching attributes */
   11776         202 :     if (NCDFDoesVarContainAttribVal(nCdfId, papszCFTimeAttribNames,
   11777             :                                     papszCFTimeAttribValues, nVarId,
   11778         202 :                                     pszVarName))
   11779         109 :         return true;
   11780             :     /* check for matching units */
   11781          93 :     else if (NCDFDoesVarContainAttribVal2(nCdfId, CF_UNITS,
   11782             :                                           papszCFTimeUnitsValues, nVarId,
   11783          93 :                                           pszVarName, false))
   11784           0 :         return true;
   11785             :     else
   11786          93 :         return false;
   11787             : }
   11788             : 
   11789             : // Parse a string, and return as a string list.
   11790             : // If it an array of the form {a,b}, then tokenize it.
   11791             : // Otherwise, return a copy.
   11792         188 : static char **NCDFTokenizeArray(const char *pszValue)
   11793             : {
   11794         188 :     if (pszValue == nullptr || EQUAL(pszValue, ""))
   11795          53 :         return nullptr;
   11796             : 
   11797         135 :     char **papszValues = nullptr;
   11798         135 :     const int nLen = static_cast<int>(strlen(pszValue));
   11799             : 
   11800         135 :     if (pszValue[0] == '{' && nLen > 2 && pszValue[nLen - 1] == '}')
   11801             :     {
   11802          41 :         char *pszTemp = static_cast<char *>(CPLMalloc((nLen - 2) + 1));
   11803          41 :         strncpy(pszTemp, pszValue + 1, nLen - 2);
   11804          41 :         pszTemp[nLen - 2] = '\0';
   11805          41 :         papszValues = CSLTokenizeString2(pszTemp, ",", CSLT_ALLOWEMPTYTOKENS);
   11806          41 :         CPLFree(pszTemp);
   11807             :     }
   11808             :     else
   11809             :     {
   11810          94 :         papszValues = reinterpret_cast<char **>(CPLCalloc(2, sizeof(char *)));
   11811          94 :         papszValues[0] = CPLStrdup(pszValue);
   11812          94 :         papszValues[1] = nullptr;
   11813             :     }
   11814             : 
   11815         135 :     return papszValues;
   11816             : }
   11817             : 
   11818             : // Open a NetCDF subdataset from full path /group1/group2/.../groupn/var.
   11819             : // Leading slash is optional.
   11820         412 : static CPLErr NCDFOpenSubDataset(int nCdfId, const char *pszSubdatasetName,
   11821             :                                  int *pnGroupId, int *pnVarId)
   11822             : {
   11823         412 :     *pnGroupId = -1;
   11824         412 :     *pnVarId = -1;
   11825             : 
   11826             :     // Open group.
   11827             :     char *pszGroupFullName =
   11828         412 :         CPLStrdup(CPLGetPathSafe(pszSubdatasetName).c_str());
   11829             :     // Add a leading slash if needed.
   11830         412 :     if (pszGroupFullName[0] != '/')
   11831             :     {
   11832         396 :         char *old = pszGroupFullName;
   11833         396 :         pszGroupFullName = CPLStrdup(CPLSPrintf("/%s", pszGroupFullName));
   11834         396 :         CPLFree(old);
   11835             :     }
   11836             :     // Detect root group.
   11837         412 :     if (EQUAL(pszGroupFullName, "/"))
   11838             :     {
   11839         396 :         *pnGroupId = nCdfId;
   11840         396 :         CPLFree(pszGroupFullName);
   11841             :     }
   11842             :     else
   11843             :     {
   11844          16 :         int status = nc_inq_grp_full_ncid(nCdfId, pszGroupFullName, pnGroupId);
   11845          16 :         CPLFree(pszGroupFullName);
   11846          16 :         NCDF_ERR_RET(status);
   11847             :     }
   11848             : 
   11849             :     // Open var.
   11850         412 :     const char *pszVarName = CPLGetFilename(pszSubdatasetName);
   11851         412 :     NCDF_ERR_RET(nc_inq_varid(*pnGroupId, pszVarName, pnVarId));
   11852             : 
   11853         412 :     return CE_None;
   11854             : }
   11855             : 
   11856             : // Get all dimensions visible from a given NetCDF (or group) ID and any of
   11857             : // its parents.
   11858         352 : static CPLErr NCDFGetVisibleDims(int nGroupId, int *pnDims, int **ppanDimIds)
   11859             : {
   11860         352 :     int nDims = 0;
   11861         352 :     int *panDimIds = nullptr;
   11862         352 :     NCDF_ERR_RET(nc_inq_dimids(nGroupId, &nDims, nullptr, true));
   11863             : 
   11864         352 :     panDimIds = static_cast<int *>(CPLMalloc(nDims * sizeof(int)));
   11865             : 
   11866         352 :     int status = nc_inq_dimids(nGroupId, nullptr, panDimIds, true);
   11867         352 :     if (status != NC_NOERR)
   11868           0 :         CPLFree(panDimIds);
   11869         352 :     NCDF_ERR_RET(status);
   11870             : 
   11871         352 :     *pnDims = nDims;
   11872         352 :     *ppanDimIds = panDimIds;
   11873             : 
   11874         352 :     return CE_None;
   11875             : }
   11876             : 
   11877             : // Get direct sub-groups IDs of a given NetCDF (or group) ID.
   11878             : // Consider only direct children, does not get children of children.
   11879        3051 : static CPLErr NCDFGetSubGroups(int nGroupId, int *pnSubGroups,
   11880             :                                int **ppanSubGroupIds)
   11881             : {
   11882        3051 :     *pnSubGroups = 0;
   11883        3051 :     *ppanSubGroupIds = nullptr;
   11884             : 
   11885             :     int nSubGroups;
   11886        3051 :     NCDF_ERR_RET(nc_inq_grps(nGroupId, &nSubGroups, nullptr));
   11887             :     int *panSubGroupIds =
   11888        3051 :         static_cast<int *>(CPLMalloc(nSubGroups * sizeof(int)));
   11889        3051 :     NCDF_ERR_RET(nc_inq_grps(nGroupId, nullptr, panSubGroupIds));
   11890        3051 :     *pnSubGroups = nSubGroups;
   11891        3051 :     *ppanSubGroupIds = panSubGroupIds;
   11892             : 
   11893        3051 :     return CE_None;
   11894             : }
   11895             : 
   11896             : // Get the full name of a given NetCDF (or group) ID
   11897             : // (e.g. /group1/group2/.../groupn).
   11898             : // bNC3Compat remove the leading slash for top-level variables for
   11899             : // backward compatibility (top-level variables are the ones in the root group).
   11900       15474 : static CPLErr NCDFGetGroupFullName(int nGroupId, char **ppszFullName,
   11901             :                                    bool bNC3Compat)
   11902             : {
   11903       15474 :     *ppszFullName = nullptr;
   11904             : 
   11905             :     size_t nFullNameLen;
   11906       15474 :     NCDF_ERR_RET(nc_inq_grpname_len(nGroupId, &nFullNameLen));
   11907       15474 :     *ppszFullName =
   11908       15474 :         static_cast<char *>(CPLMalloc((nFullNameLen + 1) * sizeof(char)));
   11909       15474 :     int status = nc_inq_grpname_full(nGroupId, &nFullNameLen, *ppszFullName);
   11910       15474 :     if (status != NC_NOERR)
   11911             :     {
   11912           0 :         CPLFree(*ppszFullName);
   11913           0 :         *ppszFullName = nullptr;
   11914           0 :         NCDF_ERR_RET(status);
   11915             :     }
   11916             : 
   11917       15474 :     if (bNC3Compat && EQUAL(*ppszFullName, "/"))
   11918        7959 :         (*ppszFullName)[0] = '\0';
   11919             : 
   11920       15474 :     return CE_None;
   11921             : }
   11922             : 
   11923        7325 : CPLString NCDFGetGroupFullName(int nGroupId)
   11924             : {
   11925        7325 :     char *pszFullname = nullptr;
   11926        7325 :     NCDFGetGroupFullName(nGroupId, &pszFullname, false);
   11927        7325 :     CPLString osRet(pszFullname ? pszFullname : "");
   11928        7325 :     CPLFree(pszFullname);
   11929       14650 :     return osRet;
   11930             : }
   11931             : 
   11932             : // Get the full name of a given NetCDF variable ID
   11933             : // (e.g. /group1/group2/.../groupn/var).
   11934             : // Handle also NC_GLOBAL as nVarId.
   11935             : // bNC3Compat remove the leading slash for top-level variables for
   11936             : // backward compatibility (top-level variables are the ones in the root group).
   11937        8100 : static CPLErr NCDFGetVarFullName(int nGroupId, int nVarId, char **ppszFullName,
   11938             :                                  bool bNC3Compat)
   11939             : {
   11940        8100 :     *ppszFullName = nullptr;
   11941        8100 :     char *pszGroupFullName = nullptr;
   11942        8100 :     ERR_RET(NCDFGetGroupFullName(nGroupId, &pszGroupFullName, bNC3Compat));
   11943             :     char szVarName[NC_MAX_NAME + 1];
   11944        8100 :     if (nVarId == NC_GLOBAL)
   11945             :     {
   11946        1093 :         strcpy(szVarName, "NC_GLOBAL");
   11947             :     }
   11948             :     else
   11949             :     {
   11950        7007 :         int status = nc_inq_varname(nGroupId, nVarId, szVarName);
   11951        7007 :         if (status != NC_NOERR)
   11952             :         {
   11953           0 :             CPLFree(pszGroupFullName);
   11954           0 :             NCDF_ERR_RET(status);
   11955             :         }
   11956             :     }
   11957        8100 :     const char *pszSep = "/";
   11958        8100 :     if (EQUAL(pszGroupFullName, "/") || EQUAL(pszGroupFullName, ""))
   11959        7912 :         pszSep = "";
   11960        8100 :     *ppszFullName =
   11961        8100 :         CPLStrdup(CPLSPrintf("%s%s%s", pszGroupFullName, pszSep, szVarName));
   11962        8100 :     CPLFree(pszGroupFullName);
   11963        8100 :     return CE_None;
   11964             : }
   11965             : 
   11966             : // Get the NetCDF root group ID of a given group ID.
   11967           0 : static CPLErr NCDFGetRootGroup(int nStartGroupId, int *pnRootGroupId)
   11968             : {
   11969           0 :     *pnRootGroupId = -1;
   11970             :     // Recurse on parent group.
   11971             :     int nParentGroupId;
   11972           0 :     int status = nc_inq_grp_parent(nStartGroupId, &nParentGroupId);
   11973           0 :     if (status == NC_NOERR)
   11974           0 :         return NCDFGetRootGroup(nParentGroupId, pnRootGroupId);
   11975           0 :     else if (status != NC_ENOGRP)
   11976           0 :         NCDF_ERR_RET(status);
   11977             :     else  // No more parent group.
   11978             :     {
   11979           0 :         *pnRootGroupId = nStartGroupId;
   11980             :     }
   11981             : 
   11982           0 :     return CE_None;
   11983             : }
   11984             : 
   11985             : // Implementation of NCDFResolveVar/Att.
   11986       13182 : static CPLErr NCDFResolveElem(int nStartGroupId, const char *pszVar,
   11987             :                               const char *pszAtt, int *pnGroupId, int *pnId,
   11988             :                               bool bMandatory)
   11989             : {
   11990       13182 :     if (!pszVar && !pszAtt)
   11991             :     {
   11992           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
   11993             :                  "pszVar and pszAtt NCDFResolveElem() args are both null.");
   11994           0 :         return CE_Failure;
   11995             :     }
   11996             : 
   11997             :     enum
   11998             :     {
   11999             :         NCRM_PARENT,
   12000             :         NCRM_WIDTH_WISE
   12001       13182 :     } eNCResolveMode = NCRM_PARENT;
   12002             : 
   12003       26364 :     std::queue<int> aoQueueGroupIdsToVisit;
   12004       13182 :     aoQueueGroupIdsToVisit.push(nStartGroupId);
   12005             : 
   12006       14851 :     while (!aoQueueGroupIdsToVisit.empty())
   12007             :     {
   12008             :         // Get the first group of the FIFO queue.
   12009       13329 :         *pnGroupId = aoQueueGroupIdsToVisit.front();
   12010       13329 :         aoQueueGroupIdsToVisit.pop();
   12011             : 
   12012             :         // Look if this group contains the searched element.
   12013             :         int status;
   12014       13329 :         if (pszVar)
   12015       13110 :             status = nc_inq_varid(*pnGroupId, pszVar, pnId);
   12016             :         else  // pszAtt != nullptr.
   12017         219 :             status = nc_inq_attid(*pnGroupId, NC_GLOBAL, pszAtt, pnId);
   12018             : 
   12019       13329 :         if (status == NC_NOERR)
   12020             :         {
   12021       11660 :             return CE_None;
   12022             :         }
   12023        1669 :         else if ((pszVar && status != NC_ENOTVAR) ||
   12024         216 :                  (pszAtt && status != NC_ENOTATT))
   12025             :         {
   12026           0 :             NCDF_ERR(status);
   12027             :         }
   12028             :         // Element not found, in NC4 case we must search in other groups
   12029             :         // following the CF logic.
   12030             : 
   12031             :         // The first resolve mode consists to search on parent groups.
   12032        1669 :         if (eNCResolveMode == NCRM_PARENT)
   12033             :         {
   12034        1569 :             int nParentGroupId = -1;
   12035        1569 :             int status2 = nc_inq_grp_parent(*pnGroupId, &nParentGroupId);
   12036        1569 :             if (status2 == NC_NOERR)
   12037          45 :                 aoQueueGroupIdsToVisit.push(nParentGroupId);
   12038        1524 :             else if (status2 != NC_ENOGRP)
   12039           0 :                 NCDF_ERR(status2);
   12040        1524 :             else if (pszVar)
   12041             :                 // When resolving a variable, if there is no more
   12042             :                 // parent group then we switch to width-wise search mode
   12043             :                 // starting from the latest found parent group.
   12044        1311 :                 eNCResolveMode = NCRM_WIDTH_WISE;
   12045             :         }
   12046             : 
   12047             :         // The second resolve mode is a width-wise search.
   12048        1669 :         if (eNCResolveMode == NCRM_WIDTH_WISE)
   12049             :         {
   12050             :             // Enqueue all direct sub-groups.
   12051        1411 :             int nSubGroups = 0;
   12052        1411 :             int *panSubGroupIds = nullptr;
   12053        1411 :             NCDFGetSubGroups(*pnGroupId, &nSubGroups, &panSubGroupIds);
   12054        1513 :             for (int i = 0; i < nSubGroups; i++)
   12055         102 :                 aoQueueGroupIdsToVisit.push(panSubGroupIds[i]);
   12056        1411 :             CPLFree(panSubGroupIds);
   12057             :         }
   12058             :     }
   12059             : 
   12060        1522 :     if (bMandatory)
   12061             :     {
   12062           0 :         char *pszStartGroupFullName = nullptr;
   12063           0 :         NCDFGetGroupFullName(nStartGroupId, &pszStartGroupFullName);
   12064           0 :         CPLError(CE_Failure, CPLE_AppDefined,
   12065             :                  "Cannot resolve mandatory %s %s from group %s",
   12066             :                  (pszVar ? pszVar : pszAtt),
   12067             :                  (pszVar ? "variable" : "attribute"),
   12068           0 :                  (pszStartGroupFullName ? pszStartGroupFullName : ""));
   12069           0 :         CPLFree(pszStartGroupFullName);
   12070             :     }
   12071             : 
   12072        1522 :     *pnGroupId = -1;
   12073        1522 :     *pnId = -1;
   12074        1522 :     return CE_Failure;
   12075             : }
   12076             : 
   12077             : // Resolve a variable name from a given starting group following the CF logic:
   12078             : // - if var name is an absolute path then directly open it
   12079             : // - first search in the starting group and its parent groups
   12080             : // - then if there is no more parent group we switch to a width-wise search
   12081             : //   mode starting from the latest found parent group.
   12082             : // The full CF logic is described here:
   12083             : // https://github.com/diwg/cf2/blob/master/group/cf2-group.adoc#scope
   12084             : // If bMandatory then print an error if resolving fails.
   12085             : // TODO: implement support of relative paths.
   12086             : // TODO: to follow strictly the CF logic, when searching for a coordinate
   12087             : //       variable, we must stop the parent search mode once the corresponding
   12088             : //       dimension is found and start the width-wise search from this group.
   12089             : // TODO: to follow strictly the CF logic, when searching in width-wise mode
   12090             : //       we should skip every groups already visited during the parent
   12091             : //       search mode (but revisiting them should have no impact so we could
   12092             : //       let as it is if it is simpler...)
   12093             : // TODO: CF specifies that the width-wise search order is "left-to-right" so
   12094             : //       maybe we must sort sibling groups alphabetically? but maybe not
   12095             : //       necessary if nc_inq_grps() already sort them?
   12096       12966 : CPLErr NCDFResolveVar(int nStartGroupId, const char *pszVar, int *pnGroupId,
   12097             :                       int *pnVarId, bool bMandatory)
   12098             : {
   12099       12966 :     *pnGroupId = -1;
   12100       12966 :     *pnVarId = -1;
   12101       12966 :     int nGroupId = nStartGroupId, nVarId;
   12102       12966 :     if (pszVar[0] == '/')
   12103             :     {
   12104             :         // This is an absolute path: we can open the var directly.
   12105             :         int nRootGroupId;
   12106           0 :         ERR_RET(NCDFGetRootGroup(nStartGroupId, &nRootGroupId));
   12107           0 :         ERR_RET(NCDFOpenSubDataset(nRootGroupId, pszVar, &nGroupId, &nVarId));
   12108             :     }
   12109             :     else
   12110             :     {
   12111             :         // We have to search the variable following the CF logic.
   12112       12966 :         ERR_RET(NCDFResolveElem(nStartGroupId, pszVar, nullptr, &nGroupId,
   12113             :                                 &nVarId, bMandatory));
   12114             :     }
   12115       11657 :     *pnGroupId = nGroupId;
   12116       11657 :     *pnVarId = nVarId;
   12117       11657 :     return CE_None;
   12118             : }
   12119             : 
   12120             : // Like NCDFResolveVar but returns directly the var full name.
   12121        1380 : static CPLErr NCDFResolveVarFullName(int nStartGroupId, const char *pszVar,
   12122             :                                      char **ppszFullName, bool bMandatory)
   12123             : {
   12124        1380 :     *ppszFullName = nullptr;
   12125             :     int nGroupId, nVarId;
   12126        1380 :     ERR_RET(
   12127             :         NCDFResolveVar(nStartGroupId, pszVar, &nGroupId, &nVarId, bMandatory));
   12128        1354 :     return NCDFGetVarFullName(nGroupId, nVarId, ppszFullName);
   12129             : }
   12130             : 
   12131             : // Like NCDFResolveVar but resolves an attribute instead a variable and
   12132             : // returns its integer value.
   12133             : // Only GLOBAL attributes are supported for the moment.
   12134         216 : static CPLErr NCDFResolveAttInt(int nStartGroupId, int nStartVarId,
   12135             :                                 const char *pszAtt, int *pnAtt, bool bMandatory)
   12136             : {
   12137         216 :     int nGroupId = nStartGroupId, nAttId = nStartVarId;
   12138         216 :     ERR_RET(NCDFResolveElem(nStartGroupId, nullptr, pszAtt, &nGroupId, &nAttId,
   12139             :                             bMandatory));
   12140           3 :     NCDF_ERR_RET(nc_get_att_int(nGroupId, NC_GLOBAL, pszAtt, pnAtt));
   12141           3 :     return CE_None;
   12142             : }
   12143             : 
   12144             : // Filter variables to keep only valid 2+D raster bands and vector fields in
   12145             : // a given a NetCDF (or group) ID and its sub-groups.
   12146             : // Coordinate or boundary variables are ignored.
   12147             : // It also creates corresponding vector layers.
   12148         525 : CPLErr netCDFDataset::FilterVars(
   12149             :     int nCdfId, bool bKeepRasters, bool bKeepVectors, char **papszIgnoreVars,
   12150             :     int *pnRasterVars, int *pnGroupId, int *pnVarId, int *pnIgnoredVars,
   12151             :     std::map<std::array<int, 3>, std::vector<std::pair<int, int>>>
   12152             :         &oMap2DDimsToGroupAndVar)
   12153             : {
   12154         525 :     int nVars = 0;
   12155         525 :     int nRasterVars = 0;
   12156         525 :     NCDF_ERR(nc_inq(nCdfId, nullptr, &nVars, nullptr, nullptr));
   12157             : 
   12158        1050 :     std::vector<int> anPotentialVectorVarID;
   12159             :     // oMapDimIdToCount[x] = number of times dim x is the first dimension of
   12160             :     // potential vector variables
   12161        1050 :     std::map<int, int> oMapDimIdToCount;
   12162         525 :     int nVarXId = -1;
   12163         525 :     int nVarYId = -1;
   12164         525 :     int nVarZId = -1;
   12165         525 :     int nVarTimeId = -1;
   12166         525 :     int nVarTimeDimId = -1;
   12167         525 :     bool bIsVectorOnly = true;
   12168         525 :     int nProfileDimId = -1;
   12169         525 :     int nParentIndexVarID = -1;
   12170             : 
   12171        3217 :     for (int v = 0; v < nVars; v++)
   12172             :     {
   12173             :         int nVarDims;
   12174        2692 :         NCDF_ERR_RET(nc_inq_varndims(nCdfId, v, &nVarDims));
   12175             :         // Should we ignore this variable?
   12176             :         char szTemp[NC_MAX_NAME + 1];
   12177        2692 :         szTemp[0] = '\0';
   12178        2692 :         NCDF_ERR_RET(nc_inq_varname(nCdfId, v, szTemp));
   12179             : 
   12180        2692 :         if (strstr(szTemp, "_node_coordinates") ||
   12181        2692 :             strstr(szTemp, "_node_count"))
   12182             :         {
   12183             :             // Ignore CF-1.8 Simple Geometries helper variables
   12184          69 :             continue;
   12185             :         }
   12186             : 
   12187        3917 :         if (nVarDims == 1 && (NCDFIsVarLongitude(nCdfId, -1, szTemp) ||
   12188        1294 :                               NCDFIsVarProjectionX(nCdfId, -1, szTemp)))
   12189             :         {
   12190         358 :             nVarXId = v;
   12191             :         }
   12192        3201 :         else if (nVarDims == 1 && (NCDFIsVarLatitude(nCdfId, -1, szTemp) ||
   12193         936 :                                    NCDFIsVarProjectionY(nCdfId, -1, szTemp)))
   12194             :         {
   12195         358 :             nVarYId = v;
   12196             :         }
   12197        1907 :         else if (nVarDims == 1 && NCDFIsVarVerticalCoord(nCdfId, -1, szTemp))
   12198             :         {
   12199          78 :             nVarZId = v;
   12200             :         }
   12201             :         else
   12202             :         {
   12203        1829 :             char *pszVarFullName = nullptr;
   12204        1829 :             CPLErr eErr = NCDFGetVarFullName(nCdfId, v, &pszVarFullName);
   12205        1829 :             if (eErr != CE_None)
   12206             :             {
   12207           0 :                 CPLFree(pszVarFullName);
   12208           0 :                 continue;
   12209             :             }
   12210             :             bool bIgnoreVar =
   12211        1829 :                 (CSLFindString(papszIgnoreVars, pszVarFullName) != -1);
   12212        1829 :             CPLFree(pszVarFullName);
   12213        1829 :             if (bIgnoreVar)
   12214             :             {
   12215         102 :                 if (nVarDims == 1 && NCDFIsVarTimeCoord(nCdfId, -1, szTemp))
   12216             :                 {
   12217          11 :                     nVarTimeId = v;
   12218          11 :                     nc_inq_vardimid(nCdfId, v, &nVarTimeDimId);
   12219             :                 }
   12220          91 :                 else if (nVarDims > 1)
   12221             :                 {
   12222          87 :                     (*pnIgnoredVars)++;
   12223          87 :                     CPLDebug("GDAL_netCDF", "variable #%d [%s] was ignored", v,
   12224             :                              szTemp);
   12225             :                 }
   12226             :             }
   12227             :             // Only accept 2+D vars.
   12228        1727 :             else if (nVarDims >= 2)
   12229             :             {
   12230         704 :                 bool bRasterCandidate = true;
   12231             :                 // Identify variables that might be vector variables
   12232         704 :                 if (nVarDims == 2)
   12233             :                 {
   12234         630 :                     int anDimIds[2] = {-1, -1};
   12235         630 :                     nc_inq_vardimid(nCdfId, v, anDimIds);
   12236             : 
   12237         630 :                     nc_type vartype = NC_NAT;
   12238         630 :                     nc_inq_vartype(nCdfId, v, &vartype);
   12239             : 
   12240             :                     char szDimNameFirst[NC_MAX_NAME + 1];
   12241             :                     char szDimNameSecond[NC_MAX_NAME + 1];
   12242         630 :                     szDimNameFirst[0] = '\0';
   12243         630 :                     szDimNameSecond[0] = '\0';
   12244        1417 :                     if (vartype == NC_CHAR &&
   12245         157 :                         nc_inq_dimname(nCdfId, anDimIds[0], szDimNameFirst) ==
   12246         157 :                             NC_NOERR &&
   12247         157 :                         nc_inq_dimname(nCdfId, anDimIds[1], szDimNameSecond) ==
   12248         157 :                             NC_NOERR &&
   12249         157 :                         !NCDFIsVarLongitude(nCdfId, -1, szDimNameSecond) &&
   12250         157 :                         !NCDFIsVarProjectionX(nCdfId, -1, szDimNameSecond) &&
   12251         944 :                         !NCDFIsVarLatitude(nCdfId, -1, szDimNameFirst) &&
   12252         157 :                         !NCDFIsVarProjectionY(nCdfId, -1, szDimNameFirst))
   12253             :                     {
   12254         157 :                         anPotentialVectorVarID.push_back(v);
   12255         157 :                         oMapDimIdToCount[anDimIds[0]]++;
   12256         157 :                         if (strstr(szDimNameSecond, "_max_width"))
   12257             :                         {
   12258         127 :                             bRasterCandidate = false;
   12259             :                         }
   12260             :                         else
   12261             :                         {
   12262          30 :                             std::array<int, 3> oKey{anDimIds[0], anDimIds[1],
   12263          30 :                                                     vartype};
   12264          30 :                             oMap2DDimsToGroupAndVar[oKey].emplace_back(
   12265          30 :                                 std::pair(nCdfId, v));
   12266             :                         }
   12267             :                     }
   12268             :                     else
   12269             :                     {
   12270         473 :                         std::array<int, 3> oKey{anDimIds[0], anDimIds[1],
   12271         473 :                                                 vartype};
   12272         473 :                         oMap2DDimsToGroupAndVar[oKey].emplace_back(
   12273         473 :                             std::pair(nCdfId, v));
   12274         473 :                         bIsVectorOnly = false;
   12275             :                     }
   12276             :                 }
   12277             :                 else
   12278             :                 {
   12279          74 :                     bIsVectorOnly = false;
   12280             :                 }
   12281         704 :                 if (bKeepRasters && bRasterCandidate)
   12282             :                 {
   12283         548 :                     *pnGroupId = nCdfId;
   12284         548 :                     *pnVarId = v;
   12285         548 :                     nRasterVars++;
   12286             :                 }
   12287             :             }
   12288        1023 :             else if (nVarDims == 1)
   12289             :             {
   12290         728 :                 nc_type atttype = NC_NAT;
   12291         728 :                 size_t attlen = 0;
   12292         728 :                 if (nc_inq_att(nCdfId, v, "instance_dimension", &atttype,
   12293          14 :                                &attlen) == NC_NOERR &&
   12294         728 :                     atttype == NC_CHAR && attlen < NC_MAX_NAME)
   12295             :                 {
   12296             :                     char szInstanceDimension[NC_MAX_NAME + 1];
   12297          14 :                     if (nc_get_att_text(nCdfId, v, "instance_dimension",
   12298          14 :                                         szInstanceDimension) == NC_NOERR)
   12299             :                     {
   12300          14 :                         szInstanceDimension[attlen] = 0;
   12301          14 :                         int status = nc_inq_dimid(nCdfId, szInstanceDimension,
   12302             :                                                   &nProfileDimId);
   12303          14 :                         if (status == NC_NOERR)
   12304          14 :                             nParentIndexVarID = v;
   12305             :                         else
   12306           0 :                             nProfileDimId = -1;
   12307          14 :                         if (status == NC_EBADDIM)
   12308           0 :                             CPLError(CE_Warning, CPLE_AppDefined,
   12309             :                                      "Attribute instance_dimension='%s' refers "
   12310             :                                      "to a non existing dimension",
   12311             :                                      szInstanceDimension);
   12312             :                         else
   12313          14 :                             NCDF_ERR(status);
   12314             :                     }
   12315             :                 }
   12316         728 :                 if (v != nParentIndexVarID)
   12317             :                 {
   12318         714 :                     anPotentialVectorVarID.push_back(v);
   12319         714 :                     int nDimId = -1;
   12320         714 :                     nc_inq_vardimid(nCdfId, v, &nDimId);
   12321         714 :                     oMapDimIdToCount[nDimId]++;
   12322             :                 }
   12323             :             }
   12324             :         }
   12325             :     }
   12326             : 
   12327             :     // If we are opened in raster-only mode and that there are only 1D or 2D
   12328             :     // variables and that the 2D variables have no X/Y dim, and all
   12329             :     // variables refer to the same main dimension (or 2 dimensions for
   12330             :     // featureType=profile), then it is a pure vector dataset
   12331             :     CPLString osFeatureType(
   12332         525 :         CSLFetchNameValueDef(papszMetadata, "NC_GLOBAL#featureType", ""));
   12333         414 :     if (bKeepRasters && !bKeepVectors && bIsVectorOnly && nRasterVars > 0 &&
   12334         939 :         !anPotentialVectorVarID.empty() &&
   12335           0 :         (oMapDimIdToCount.size() == 1 ||
   12336           0 :          (EQUAL(osFeatureType, "profile") && oMapDimIdToCount.size() == 2 &&
   12337           0 :           nProfileDimId >= 0)))
   12338             :     {
   12339           0 :         anPotentialVectorVarID.resize(0);
   12340             :     }
   12341             :     else
   12342             :     {
   12343         525 :         *pnRasterVars += nRasterVars;
   12344             :     }
   12345             : 
   12346         525 :     if (!anPotentialVectorVarID.empty() && bKeepVectors)
   12347             :     {
   12348             :         // Take the dimension that is referenced the most times.
   12349          64 :         if (!(oMapDimIdToCount.size() == 1 ||
   12350          27 :               (EQUAL(osFeatureType, "profile") &&
   12351          26 :                oMapDimIdToCount.size() == 2 && nProfileDimId >= 0)))
   12352             :         {
   12353           1 :             CPLError(CE_Warning, CPLE_AppDefined,
   12354             :                      "The dataset has several variables that could be "
   12355             :                      "identified as vector fields, but not all share the same "
   12356             :                      "primary dimension. Consequently they will be ignored.");
   12357             :         }
   12358             :         else
   12359             :         {
   12360          50 :             if (nVarTimeId >= 0 &&
   12361          50 :                 oMapDimIdToCount.find(nVarTimeDimId) != oMapDimIdToCount.end())
   12362             :             {
   12363           1 :                 anPotentialVectorVarID.push_back(nVarTimeId);
   12364             :             }
   12365          49 :             CreateGrpVectorLayers(nCdfId, osFeatureType, anPotentialVectorVarID,
   12366             :                                   oMapDimIdToCount, nVarXId, nVarYId, nVarZId,
   12367             :                                   nProfileDimId, nParentIndexVarID,
   12368             :                                   bKeepRasters);
   12369             :         }
   12370             :     }
   12371             : 
   12372             :     // Recurse on sub-groups.
   12373         525 :     int nSubGroups = 0;
   12374         525 :     int *panSubGroupIds = nullptr;
   12375         525 :     NCDFGetSubGroups(nCdfId, &nSubGroups, &panSubGroupIds);
   12376         558 :     for (int i = 0; i < nSubGroups; i++)
   12377             :     {
   12378          33 :         FilterVars(panSubGroupIds[i], bKeepRasters, bKeepVectors,
   12379             :                    papszIgnoreVars, pnRasterVars, pnGroupId, pnVarId,
   12380             :                    pnIgnoredVars, oMap2DDimsToGroupAndVar);
   12381             :     }
   12382         525 :     CPLFree(panSubGroupIds);
   12383             : 
   12384         525 :     return CE_None;
   12385             : }
   12386             : 
   12387             : // Create vector layers from given potentially identified vector variables
   12388             : // resulting from the scanning of a NetCDF (or group) ID.
   12389          49 : CPLErr netCDFDataset::CreateGrpVectorLayers(
   12390             :     int nCdfId, const CPLString &osFeatureType,
   12391             :     const std::vector<int> &anPotentialVectorVarID,
   12392             :     const std::map<int, int> &oMapDimIdToCount, int nVarXId, int nVarYId,
   12393             :     int nVarZId, int nProfileDimId, int nParentIndexVarID, bool bKeepRasters)
   12394             : {
   12395          49 :     char *pszGroupName = nullptr;
   12396          49 :     NCDFGetGroupFullName(nCdfId, &pszGroupName);
   12397          49 :     if (pszGroupName == nullptr || pszGroupName[0] == '\0')
   12398             :     {
   12399          47 :         CPLFree(pszGroupName);
   12400          47 :         pszGroupName = CPLStrdup(CPLGetBasenameSafe(osFilename).c_str());
   12401             :     }
   12402          49 :     OGRwkbGeometryType eGType = wkbUnknown;
   12403             :     CPLString osLayerName = CSLFetchNameValueDef(
   12404          98 :         papszMetadata, "NC_GLOBAL#ogr_layer_name", pszGroupName);
   12405          49 :     CPLFree(pszGroupName);
   12406          49 :     papszMetadata =
   12407          49 :         CSLSetNameValue(papszMetadata, "NC_GLOBAL#ogr_layer_name", nullptr);
   12408             : 
   12409          49 :     if (EQUAL(osFeatureType, "point") || EQUAL(osFeatureType, "profile"))
   12410             :     {
   12411          33 :         papszMetadata =
   12412          33 :             CSLSetNameValue(papszMetadata, "NC_GLOBAL#featureType", nullptr);
   12413          33 :         eGType = wkbPoint;
   12414             :     }
   12415             : 
   12416             :     const char *pszLayerType =
   12417          49 :         CSLFetchNameValue(papszMetadata, "NC_GLOBAL#ogr_layer_type");
   12418          49 :     if (pszLayerType != nullptr)
   12419             :     {
   12420           9 :         eGType = OGRFromOGCGeomType(pszLayerType);
   12421           9 :         papszMetadata =
   12422           9 :             CSLSetNameValue(papszMetadata, "NC_GLOBAL#ogr_layer_type", nullptr);
   12423             :     }
   12424             : 
   12425             :     CPLString osGeometryField =
   12426          98 :         CSLFetchNameValueDef(papszMetadata, "NC_GLOBAL#ogr_geometry_field", "");
   12427          49 :     papszMetadata =
   12428          49 :         CSLSetNameValue(papszMetadata, "NC_GLOBAL#ogr_geometry_field", nullptr);
   12429             : 
   12430          49 :     int nFirstVarId = -1;
   12431          49 :     int nVectorDim = oMapDimIdToCount.rbegin()->first;
   12432          49 :     if (EQUAL(osFeatureType, "profile") && oMapDimIdToCount.size() == 2)
   12433             :     {
   12434          13 :         if (nVectorDim == nProfileDimId)
   12435           0 :             nVectorDim = oMapDimIdToCount.begin()->first;
   12436             :     }
   12437             :     else
   12438             :     {
   12439          36 :         nProfileDimId = -1;
   12440             :     }
   12441          62 :     for (size_t j = 0; j < anPotentialVectorVarID.size(); j++)
   12442             :     {
   12443          62 :         int anDimIds[2] = {-1, -1};
   12444          62 :         nc_inq_vardimid(nCdfId, anPotentialVectorVarID[j], anDimIds);
   12445          62 :         if (nVectorDim == anDimIds[0])
   12446             :         {
   12447          49 :             nFirstVarId = anPotentialVectorVarID[j];
   12448          49 :             break;
   12449             :         }
   12450             :     }
   12451             : 
   12452             :     // In case where coordinates are explicitly specified for one of the
   12453             :     // field/variable, use them in priority over the ones that might have been
   12454             :     // identified above.
   12455          49 :     char *pszCoordinates = nullptr;
   12456          49 :     if (NCDFGetAttr(nCdfId, nFirstVarId, "coordinates", &pszCoordinates) ==
   12457             :         CE_None)
   12458             :     {
   12459          34 :         char **papszTokens = NCDFTokenizeCoordinatesAttribute(pszCFCoordinates);
   12460          34 :         for (int i = 0; papszTokens != nullptr && papszTokens[i] != nullptr;
   12461             :              i++)
   12462             :         {
   12463           0 :             if (NCDFIsVarLongitude(nCdfId, -1, papszTokens[i]) ||
   12464           0 :                 NCDFIsVarProjectionX(nCdfId, -1, papszTokens[i]))
   12465             :             {
   12466           0 :                 nVarXId = -1;
   12467           0 :                 CPL_IGNORE_RET_VAL(
   12468           0 :                     nc_inq_varid(nCdfId, papszTokens[i], &nVarXId));
   12469             :             }
   12470           0 :             else if (NCDFIsVarLatitude(nCdfId, -1, papszTokens[i]) ||
   12471           0 :                      NCDFIsVarProjectionY(nCdfId, -1, papszTokens[i]))
   12472             :             {
   12473           0 :                 nVarYId = -1;
   12474           0 :                 CPL_IGNORE_RET_VAL(
   12475           0 :                     nc_inq_varid(nCdfId, papszTokens[i], &nVarYId));
   12476             :             }
   12477           0 :             else if (NCDFIsVarVerticalCoord(nCdfId, -1, papszTokens[i]))
   12478             :             {
   12479           0 :                 nVarZId = -1;
   12480           0 :                 CPL_IGNORE_RET_VAL(
   12481           0 :                     nc_inq_varid(nCdfId, papszTokens[i], &nVarZId));
   12482             :             }
   12483             :         }
   12484          34 :         CSLDestroy(papszTokens);
   12485             :     }
   12486          49 :     CPLFree(pszCoordinates);
   12487             : 
   12488             :     // Check that the X,Y,Z vars share 1D and share the same dimension as
   12489             :     // attribute variables.
   12490          49 :     if (nVarXId >= 0 && nVarYId >= 0)
   12491             :     {
   12492          38 :         int nVarDimCount = -1;
   12493          38 :         int nVarDimId = -1;
   12494          38 :         if (nc_inq_varndims(nCdfId, nVarXId, &nVarDimCount) != NC_NOERR ||
   12495          38 :             nVarDimCount != 1 ||
   12496          38 :             nc_inq_vardimid(nCdfId, nVarXId, &nVarDimId) != NC_NOERR ||
   12497          38 :             nVarDimId != ((nProfileDimId >= 0) ? nProfileDimId : nVectorDim) ||
   12498          35 :             nc_inq_varndims(nCdfId, nVarYId, &nVarDimCount) != NC_NOERR ||
   12499          35 :             nVarDimCount != 1 ||
   12500         111 :             nc_inq_vardimid(nCdfId, nVarYId, &nVarDimId) != NC_NOERR ||
   12501          35 :             nVarDimId != ((nProfileDimId >= 0) ? nProfileDimId : nVectorDim))
   12502             :         {
   12503           3 :             nVarXId = nVarYId = -1;
   12504             :         }
   12505          69 :         else if (nVarZId >= 0 &&
   12506          34 :                  (nc_inq_varndims(nCdfId, nVarZId, &nVarDimCount) != NC_NOERR ||
   12507          34 :                   nVarDimCount != 1 ||
   12508          34 :                   nc_inq_vardimid(nCdfId, nVarZId, &nVarDimId) != NC_NOERR ||
   12509          34 :                   nVarDimId != nVectorDim))
   12510             :         {
   12511           0 :             nVarZId = -1;
   12512             :         }
   12513             :     }
   12514             : 
   12515          49 :     if (eGType == wkbUnknown && nVarXId >= 0 && nVarYId >= 0)
   12516             :     {
   12517           2 :         eGType = wkbPoint;
   12518             :     }
   12519          49 :     if (eGType == wkbPoint && nVarXId >= 0 && nVarYId >= 0 && nVarZId >= 0)
   12520             :     {
   12521          34 :         eGType = wkbPoint25D;
   12522             :     }
   12523          49 :     if (eGType == wkbUnknown && osGeometryField.empty())
   12524             :     {
   12525           5 :         eGType = wkbNone;
   12526             :     }
   12527             : 
   12528             :     // Read projection info
   12529          49 :     char **papszMetadataBackup = CSLDuplicate(papszMetadata);
   12530          49 :     ReadAttributes(nCdfId, nFirstVarId);
   12531          49 :     if (!this->bSGSupport)
   12532          49 :         SetProjectionFromVar(nCdfId, nFirstVarId, true);
   12533          49 :     const char *pszValue = FetchAttr(nCdfId, nFirstVarId, CF_GRD_MAPPING);
   12534          49 :     char *pszGridMapping = (pszValue ? CPLStrdup(pszValue) : nullptr);
   12535          49 :     CSLDestroy(papszMetadata);
   12536          49 :     papszMetadata = papszMetadataBackup;
   12537             : 
   12538          49 :     OGRSpatialReference *poSRS = nullptr;
   12539          49 :     if (!m_oSRS.IsEmpty())
   12540             :     {
   12541          21 :         poSRS = m_oSRS.Clone();
   12542             :     }
   12543             :     // Reset if there's a 2D raster
   12544          49 :     m_bHasProjection = false;
   12545          49 :     m_bHasGeoTransform = false;
   12546             : 
   12547          49 :     if (!bKeepRasters)
   12548             :     {
   12549             :         // Strip out uninteresting metadata.
   12550          45 :         papszMetadata =
   12551          45 :             CSLSetNameValue(papszMetadata, "NC_GLOBAL#Conventions", nullptr);
   12552          45 :         papszMetadata =
   12553          45 :             CSLSetNameValue(papszMetadata, "NC_GLOBAL#GDAL", nullptr);
   12554          45 :         papszMetadata =
   12555          45 :             CSLSetNameValue(papszMetadata, "NC_GLOBAL#history", nullptr);
   12556             :     }
   12557             : 
   12558             :     std::shared_ptr<netCDFLayer> poLayer(
   12559          49 :         new netCDFLayer(this, nCdfId, osLayerName, eGType, poSRS));
   12560          49 :     if (poSRS != nullptr)
   12561          21 :         poSRS->Release();
   12562          49 :     poLayer->SetRecordDimID(nVectorDim);
   12563          49 :     if (wkbFlatten(eGType) == wkbPoint && nVarXId >= 0 && nVarYId >= 0)
   12564             :     {
   12565          35 :         poLayer->SetXYZVars(nVarXId, nVarYId, nVarZId);
   12566             :     }
   12567          14 :     else if (!osGeometryField.empty())
   12568             :     {
   12569           9 :         poLayer->SetWKTGeometryField(osGeometryField);
   12570             :     }
   12571          49 :     if (pszGridMapping != nullptr)
   12572             :     {
   12573          21 :         poLayer->SetGridMapping(pszGridMapping);
   12574          21 :         CPLFree(pszGridMapping);
   12575             :     }
   12576          49 :     poLayer->SetProfile(nProfileDimId, nParentIndexVarID);
   12577             : 
   12578         574 :     for (size_t j = 0; j < anPotentialVectorVarID.size(); j++)
   12579             :     {
   12580         525 :         int anDimIds[2] = {-1, -1};
   12581         525 :         nc_inq_vardimid(nCdfId, anPotentialVectorVarID[j], anDimIds);
   12582         525 :         if (anDimIds[0] == nVectorDim ||
   12583          24 :             (nProfileDimId >= 0 && anDimIds[0] == nProfileDimId))
   12584             :         {
   12585             : #ifdef NCDF_DEBUG
   12586             :             char szTemp2[NC_MAX_NAME + 1] = {};
   12587             :             CPL_IGNORE_RET_VAL(
   12588             :                 nc_inq_varname(nCdfId, anPotentialVectorVarID[j], szTemp2));
   12589             :             CPLDebug("GDAL_netCDF", "Variable %s is a vector field", szTemp2);
   12590             : #endif
   12591         525 :             poLayer->AddField(anPotentialVectorVarID[j]);
   12592             :         }
   12593             :     }
   12594             : 
   12595          49 :     if (poLayer->GetLayerDefn()->GetFieldCount() != 0 ||
   12596           0 :         poLayer->GetGeomType() != wkbNone)
   12597             :     {
   12598          49 :         papoLayers.push_back(poLayer);
   12599             :     }
   12600             : 
   12601          98 :     return CE_None;
   12602             : }
   12603             : 
   12604             : // Get all coordinate and boundary variables full names referenced in
   12605             : // a given a NetCDF (or group) ID and its sub-groups.
   12606             : // These variables are identified in other variable's
   12607             : // "coordinates" and "bounds" attribute.
   12608             : // Searching coordinate and boundary variables may need to explore
   12609             : // parents groups (or other groups in case of reference given in form of an
   12610             : // absolute path).
   12611             : // See CF sections 5.2, 5.6 and 7.1
   12612         526 : static CPLErr NCDFGetCoordAndBoundVarFullNames(int nCdfId, char ***ppapszVars)
   12613             : {
   12614         526 :     int nVars = 0;
   12615         526 :     NCDF_ERR(nc_inq(nCdfId, nullptr, &nVars, nullptr, nullptr));
   12616             : 
   12617        3232 :     for (int v = 0; v < nVars; v++)
   12618             :     {
   12619        2706 :         char *pszTemp = nullptr;
   12620        2706 :         char **papszTokens = nullptr;
   12621        2706 :         if (NCDFGetAttr(nCdfId, v, "coordinates", &pszTemp) == CE_None)
   12622         445 :             papszTokens = NCDFTokenizeCoordinatesAttribute(pszTemp);
   12623        2706 :         CPLFree(pszTemp);
   12624        2706 :         pszTemp = nullptr;
   12625        2706 :         if (NCDFGetAttr(nCdfId, v, "bounds", &pszTemp) == CE_None &&
   12626        2706 :             pszTemp != nullptr && !EQUAL(pszTemp, ""))
   12627          17 :             papszTokens = CSLAddString(papszTokens, pszTemp);
   12628        2706 :         CPLFree(pszTemp);
   12629        3992 :         for (int i = 0; papszTokens != nullptr && papszTokens[i] != nullptr;
   12630             :              i++)
   12631             :         {
   12632        1286 :             char *pszVarFullName = nullptr;
   12633        1286 :             if (NCDFResolveVarFullName(nCdfId, papszTokens[i],
   12634        1286 :                                        &pszVarFullName) == CE_None)
   12635        1260 :                 *ppapszVars = CSLAddString(*ppapszVars, pszVarFullName);
   12636        1286 :             CPLFree(pszVarFullName);
   12637             :         }
   12638        2706 :         CSLDestroy(papszTokens);
   12639             :     }
   12640             : 
   12641             :     // Recurse on sub-groups.
   12642             :     int nSubGroups;
   12643         526 :     int *panSubGroupIds = nullptr;
   12644         526 :     NCDFGetSubGroups(nCdfId, &nSubGroups, &panSubGroupIds);
   12645         559 :     for (int i = 0; i < nSubGroups; i++)
   12646             :     {
   12647          33 :         NCDFGetCoordAndBoundVarFullNames(panSubGroupIds[i], ppapszVars);
   12648             :     }
   12649         526 :     CPLFree(panSubGroupIds);
   12650             : 
   12651         526 :     return CE_None;
   12652             : }
   12653             : 
   12654             : // Check if give type is user defined
   12655        1520 : bool NCDFIsUserDefinedType(int /*ncid*/, int type)
   12656             : {
   12657        1520 :     return type >= NC_FIRSTUSERTYPEID;
   12658             : }
   12659             : 
   12660         558 : char **NCDFTokenizeCoordinatesAttribute(const char *pszCoordinates)
   12661             : {
   12662             :     // CF conventions use space as the separator for variable names in the
   12663             :     // coordinates attribute, but some products such as
   12664             :     // https://sentinel.esa.int/web/sentinel/technical-guides/sentinel-3-synergy/products-algorithms/level-2-aod-algorithms-and-products/level-2-aod-products-description
   12665             :     // use comma.
   12666         558 :     return CSLTokenizeString2(pszCoordinates, ", ", 0);
   12667             : }

Generated by: LCOV version 1.14