LCOV - code coverage report
Current view: top level - frmts/netcdf - netcdfdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 4963 5825 85.2 %
Date: 2026-11-17 04:47:18 Functions: 159 165 96.4 %

          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 CPLStringList 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, std::string &osFullName,
     111             :                                    bool bNC3Compat = true);
     112             : static CPLErr NCDFGetVarFullName(int nGroupId, int nVarId,
     113             :                                  std::string &osFullName,
     114             :                                  bool bNC3Compat = true);
     115             : static CPLErr NCDFGetRootGroup(int nStartGroupId, int *pnRootGroupId);
     116             : 
     117             : static std::pair<int, int> ReadExtraDimDef(GDALDataset *poSrcDS,
     118             :                                            const char *pszDimName);
     119             : 
     120             : static CPLErr NCDFResolveVarFullName(int nStartGroupId, const char *pszVar,
     121             :                                      std::string &osFullName,
     122             :                                      bool bMandatory = false);
     123             : static CPLErr NCDFResolveAttInt(int nStartGroupId, int nStartVarId,
     124             :                                 const char *pszAtt, int *pnAtt,
     125             :                                 bool bMandatory = false);
     126             : static CPLErr NCDFGetCoordAndBoundVarFullNames(int nCdfId,
     127             :                                                CPLStringList &aosVars);
     128             : 
     129             : // Uncomment this for more debug output.
     130             : // #define NCDF_DEBUG 1
     131             : 
     132             : CPLMutex *hNCMutex = nullptr;
     133             : 
     134             : // Workaround https://github.com/OSGeo/gdal/issues/6253
     135             : // Having 2 netCDF handles on the same file doesn't work in a multi-threaded
     136             : // way. Apparently having the same handle works better (this is OK since
     137             : // we have a global mutex on the netCDF library)
     138             : static std::map<std::string, int> goMapNameToNetCDFId;
     139             : static std::map<int, std::pair<std::string, int>> goMapNetCDFIdToKeyAndCount;
     140             : 
     141         839 : int GDAL_nc_open(const char *pszFilename, int nMode, int *pID)
     142             : {
     143        1678 :     std::string osKey(pszFilename);
     144         839 :     osKey += "#####";
     145         839 :     osKey += std::to_string(nMode);
     146         839 :     auto oIter = goMapNameToNetCDFId.find(osKey);
     147         839 :     if (oIter == goMapNameToNetCDFId.end())
     148             :     {
     149         773 :         int ret = nc_open(pszFilename, nMode, pID);
     150         773 :         if (ret != NC_NOERR)
     151           3 :             return ret;
     152         770 :         goMapNameToNetCDFId[osKey] = *pID;
     153         770 :         goMapNetCDFIdToKeyAndCount[*pID] =
     154        1540 :             std::pair<std::string, int>(osKey, 1);
     155         770 :         return ret;
     156             :     }
     157             :     else
     158             :     {
     159          66 :         *pID = oIter->second;
     160          66 :         goMapNetCDFIdToKeyAndCount[oIter->second].second++;
     161          66 :         return NC_NOERR;
     162             :     }
     163             : }
     164             : 
     165        1132 : int GDAL_nc_close(int cdfid)
     166             : {
     167        1132 :     int ret = NC_NOERR;
     168        1132 :     auto oIter = goMapNetCDFIdToKeyAndCount.find(cdfid);
     169        1132 :     if (oIter != goMapNetCDFIdToKeyAndCount.end())
     170             :     {
     171         836 :         if (--oIter->second.second == 0)
     172             :         {
     173         770 :             ret = nc_close(cdfid);
     174         770 :             goMapNameToNetCDFId.erase(oIter->second.first);
     175         770 :             goMapNetCDFIdToKeyAndCount.erase(oIter);
     176             :         }
     177             :     }
     178             :     else
     179             :     {
     180             :         // we can go here if file opened with nc_open_mem() or nc_create()
     181         296 :         ret = nc_close(cdfid);
     182             :     }
     183        1132 :     return ret;
     184             : }
     185             : 
     186             : /************************************************************************/
     187             : /* ==================================================================== */
     188             : /*                         netCDFRasterBand                             */
     189             : /* ==================================================================== */
     190             : /************************************************************************/
     191             : 
     192             : class netCDFRasterBand final : public GDALPamRasterBand
     193             : {
     194             :     friend class netCDFDataset;
     195             : 
     196             :     nc_type nc_datatype;
     197             :     int cdfid;
     198             :     int nZId;
     199             :     int nZDim;
     200             :     int nLevel;
     201             :     int nBandXPos;
     202             :     int nBandYPos;
     203             :     int *panBandZPos;
     204             :     int *panBandZLev;
     205             :     bool m_bNoDataSet = false;
     206             :     double m_dfNoDataValue = 0;
     207             :     bool m_bNoDataSetAsInt64 = false;
     208             :     int64_t m_nNodataValueInt64 = 0;
     209             :     bool m_bNoDataSetAsUInt64 = false;
     210             :     uint64_t m_nNodataValueUInt64 = 0;
     211             :     bool bValidRangeValid = false;
     212             :     double adfValidRange[2]{0, 0};
     213             :     bool m_bHaveScale = false;
     214             :     bool m_bHaveOffset = false;
     215             :     double m_dfScale = 1;
     216             :     double m_dfOffset = 0;
     217             :     CPLString m_osUnitType{};
     218             :     bool bSignedData;
     219             :     bool bCheckLongitude;
     220             :     bool m_bCreateMetadataFromOtherVarsDone = false;
     221             : 
     222             :     void CreateMetadataFromAttributes();
     223             :     void CreateMetadataFromOtherVars();
     224             : 
     225             :     template <class T>
     226             :     void CheckData(void *pImage, void *pImageNC, size_t nTmpBlockXSize,
     227             :                    size_t nTmpBlockYSize, bool bCheckIsNan = false);
     228             :     template <class T>
     229             :     void CheckDataCpx(void *pImage, void *pImageNC, size_t nTmpBlockXSize,
     230             :                       size_t nTmpBlockYSize, bool bCheckIsNan = false);
     231             :     void SetBlockSize();
     232             : 
     233             :     bool FetchNetcdfChunk(size_t xstart, size_t ystart, void *pImage);
     234             : 
     235             :     void SetNoDataValueNoUpdate(double dfNoData);
     236             :     void SetNoDataValueNoUpdate(int64_t nNoData);
     237             :     void SetNoDataValueNoUpdate(uint64_t nNoData);
     238             : 
     239             :     void SetOffsetNoUpdate(double dfVal);
     240             :     void SetScaleNoUpdate(double dfVal);
     241             :     void SetUnitTypeNoUpdate(const char *pszNewValue);
     242             : 
     243             :   protected:
     244             :     CPLXMLNode *SerializeToXML(const char *pszUnused) override;
     245             : 
     246             :   public:
     247             :     struct CONSTRUCTOR_OPEN
     248             :     {
     249             :     };
     250             : 
     251             :     struct CONSTRUCTOR_CREATE
     252             :     {
     253             :     };
     254             : 
     255             :     netCDFRasterBand(const CONSTRUCTOR_OPEN &, netCDFDataset *poDS,
     256             :                      int nGroupId, int nZId, int nZDim, int nLevel,
     257             :                      const int *panBandZLen, const int *panBandPos, int nBand);
     258             :     netCDFRasterBand(const CONSTRUCTOR_CREATE &, netCDFDataset *poDS,
     259             :                      GDALDataType eType, int nBand, bool bSigned = true,
     260             :                      const char *pszBandName = nullptr,
     261             :                      const char *pszLongName = nullptr, int nZId = -1,
     262             :                      int nZDim = 2, int nLevel = 0,
     263             :                      const int *panBandZLev = nullptr,
     264             :                      const int *panBandZPos = nullptr,
     265             :                      const int *paDimIds = nullptr);
     266             :     ~netCDFRasterBand() override;
     267             : 
     268             :     double GetNoDataValue(int *) override;
     269             :     int64_t GetNoDataValueAsInt64(int *pbSuccess = nullptr) override;
     270             :     uint64_t GetNoDataValueAsUInt64(int *pbSuccess = nullptr) override;
     271             :     CPLErr SetNoDataValue(double) override;
     272             :     CPLErr SetNoDataValueAsInt64(int64_t nNoData) override;
     273             :     CPLErr SetNoDataValueAsUInt64(uint64_t nNoData) override;
     274             :     // virtual CPLErr DeleteNoDataValue();
     275             :     double GetOffset(int *) override;
     276             :     CPLErr SetOffset(double) override;
     277             :     double GetScale(int *) override;
     278             :     CPLErr SetScale(double) override;
     279             :     const char *GetUnitType() override;
     280             :     CPLErr SetUnitType(const char *) override;
     281             :     CPLErr IReadBlock(int, int, void *) override;
     282             :     CPLErr IWriteBlock(int, int, void *) override;
     283             : 
     284             :     CSLConstList GetMetadata(const char *pszDomain = "") override;
     285             :     const char *GetMetadataItem(const char *pszName,
     286             :                                 const char *pszDomain = "") override;
     287             : 
     288             :     CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
     289             :                            const char *pszDomain = "") override;
     290             :     CPLErr SetMetadata(CSLConstList papszMD,
     291             :                        const char *pszDomain = "") override;
     292             : };
     293             : 
     294             : /************************************************************************/
     295             : /*                          netCDFRasterBand()                          */
     296             : /************************************************************************/
     297             : 
     298         507 : netCDFRasterBand::netCDFRasterBand(const netCDFRasterBand::CONSTRUCTOR_OPEN &,
     299             :                                    netCDFDataset *poNCDFDS, int nGroupId,
     300             :                                    int nZIdIn, int nZDimIn, int nLevelIn,
     301             :                                    const int *panBandZLevIn,
     302         507 :                                    const int *panBandZPosIn, int nBandIn)
     303             :     : nc_datatype(NC_NAT), cdfid(nGroupId), nZId(nZIdIn), nZDim(nZDimIn),
     304         507 :       nLevel(nLevelIn), nBandXPos(panBandZPosIn[0]),
     305         507 :       nBandYPos(nZDim == 1 ? -1 : panBandZPosIn[1]), panBandZPos(nullptr),
     306             :       panBandZLev(nullptr),
     307             :       bSignedData(true),  // Default signed, except for Byte.
     308        1014 :       bCheckLongitude(false)
     309             : {
     310         507 :     poDS = poNCDFDS;
     311         507 :     nBand = nBandIn;
     312             : 
     313             :     // Take care of all other dimensions.
     314         507 :     if (nZDim > 2)
     315             :     {
     316         182 :         panBandZPos = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     317         182 :         panBandZLev = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     318             : 
     319         490 :         for (int i = 0; i < nZDim - 2; i++)
     320             :         {
     321         308 :             panBandZPos[i] = panBandZPosIn[i + 2];
     322         308 :             panBandZLev[i] = panBandZLevIn[i];
     323             :         }
     324             :     }
     325             : 
     326         507 :     nRasterXSize = poDS->GetRasterXSize();
     327         507 :     nRasterYSize = poDS->GetRasterYSize();
     328         507 :     nBlockXSize = poDS->GetRasterXSize();
     329         507 :     nBlockYSize = 1;
     330             : 
     331             :     // Get the type of the "z" variable, our target raster array.
     332         507 :     if (nc_inq_var(cdfid, nZId, nullptr, &nc_datatype, nullptr, nullptr,
     333         507 :                    nullptr) != NC_NOERR)
     334             :     {
     335           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Error in nc_var_inq() on 'z'.");
     336           0 :         return;
     337             :     }
     338             : 
     339         507 :     if (NCDFIsUserDefinedType(cdfid, nc_datatype))
     340             :     {
     341             :         // First enquire and check that the number of fields is 2
     342             :         size_t nfields, compoundsize;
     343           5 :         if (nc_inq_compound(cdfid, nc_datatype, nullptr, &compoundsize,
     344           5 :                             &nfields) != NC_NOERR)
     345             :         {
     346           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     347             :                      "Error in nc_inq_compound() on 'z'.");
     348           0 :             return;
     349             :         }
     350             : 
     351           5 :         if (nfields != 2)
     352             :         {
     353           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     354             :                      "Unsupported data type encountered in nc_inq_compound() "
     355             :                      "on 'z'.");
     356           0 :             return;
     357             :         }
     358             : 
     359             :         // Now check that that two types are the same in the struct.
     360             :         nc_type field_type1, field_type2;
     361             :         int field_dims1, field_dims2;
     362           5 :         if (nc_inq_compound_field(cdfid, nc_datatype, 0, nullptr, nullptr,
     363             :                                   &field_type1, &field_dims1,
     364           5 :                                   nullptr) != NC_NOERR)
     365             :         {
     366           0 :             CPLError(
     367             :                 CE_Failure, CPLE_AppDefined,
     368             :                 "Error in querying Field 1 in nc_inq_compound_field() on 'z'.");
     369           0 :             return;
     370             :         }
     371             : 
     372           5 :         if (nc_inq_compound_field(cdfid, nc_datatype, 0, nullptr, nullptr,
     373             :                                   &field_type2, &field_dims2,
     374           5 :                                   nullptr) != NC_NOERR)
     375             :         {
     376           0 :             CPLError(
     377             :                 CE_Failure, CPLE_AppDefined,
     378             :                 "Error in querying Field 2 in nc_inq_compound_field() on 'z'.");
     379           0 :             return;
     380             :         }
     381             : 
     382           5 :         if ((field_type1 != field_type2) || (field_dims1 != field_dims2) ||
     383           5 :             (field_dims1 != 0))
     384             :         {
     385           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     386             :                      "Error in interpreting compound data type on 'z'.");
     387           0 :             return;
     388             :         }
     389             : 
     390           5 :         if (field_type1 == NC_SHORT)
     391           0 :             eDataType = GDT_CInt16;
     392           5 :         else if (field_type1 == NC_INT)
     393           0 :             eDataType = GDT_CInt32;
     394           5 :         else if (field_type1 == NC_FLOAT)
     395           4 :             eDataType = GDT_CFloat32;
     396           1 :         else if (field_type1 == NC_DOUBLE)
     397           1 :             eDataType = GDT_CFloat64;
     398             :         else
     399             :         {
     400           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     401             :                      "Unsupported netCDF compound data type encountered.");
     402           0 :             return;
     403             :         }
     404             :     }
     405             :     else
     406             :     {
     407         502 :         if (nc_datatype == NC_BYTE)
     408         164 :             eDataType = GDT_UInt8;
     409         338 :         else if (nc_datatype == NC_CHAR)
     410           0 :             eDataType = GDT_UInt8;
     411         338 :         else if (nc_datatype == NC_SHORT)
     412          43 :             eDataType = GDT_Int16;
     413         295 :         else if (nc_datatype == NC_INT)
     414          88 :             eDataType = GDT_Int32;
     415         207 :         else if (nc_datatype == NC_FLOAT)
     416         126 :             eDataType = GDT_Float32;
     417          81 :         else if (nc_datatype == NC_DOUBLE)
     418          43 :             eDataType = GDT_Float64;
     419          38 :         else if (nc_datatype == NC_UBYTE)
     420          15 :             eDataType = GDT_UInt8;
     421          23 :         else if (nc_datatype == NC_USHORT)
     422           4 :             eDataType = GDT_UInt16;
     423          19 :         else if (nc_datatype == NC_UINT)
     424           4 :             eDataType = GDT_UInt32;
     425          15 :         else if (nc_datatype == NC_INT64)
     426           8 :             eDataType = GDT_Int64;
     427           7 :         else if (nc_datatype == NC_UINT64)
     428           7 :             eDataType = GDT_UInt64;
     429             :         else
     430             :         {
     431           0 :             if (nBand == 1)
     432           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
     433             :                          "Unsupported netCDF datatype (%d), treat as Float32.",
     434           0 :                          static_cast<int>(nc_datatype));
     435           0 :             eDataType = GDT_Float32;
     436           0 :             nc_datatype = NC_FLOAT;
     437             :         }
     438             :     }
     439             : 
     440             :     // Find and set No Data for this variable.
     441         507 :     nc_type atttype = NC_NAT;
     442         507 :     size_t attlen = 0;
     443         507 :     const char *pszNoValueName = nullptr;
     444             : 
     445             :     // Find attribute name, either _FillValue or missing_value.
     446         507 :     int status = nc_inq_att(cdfid, nZId, NCDF_FillValue, &atttype, &attlen);
     447         507 :     if (status == NC_NOERR)
     448             :     {
     449         250 :         pszNoValueName = NCDF_FillValue;
     450             :     }
     451             :     else
     452             :     {
     453         257 :         status = nc_inq_att(cdfid, nZId, "missing_value", &atttype, &attlen);
     454         257 :         if (status == NC_NOERR)
     455             :         {
     456          12 :             pszNoValueName = "missing_value";
     457             :         }
     458             :     }
     459             : 
     460             :     // Fetch missing value.
     461         507 :     double dfNoData = 0.0;
     462         507 :     bool bGotNoData = false;
     463         507 :     int64_t nNoDataAsInt64 = 0;
     464         507 :     bool bGotNoDataAsInt64 = false;
     465         507 :     uint64_t nNoDataAsUInt64 = 0;
     466         507 :     bool bGotNoDataAsUInt64 = false;
     467         507 :     if (status == NC_NOERR)
     468             :     {
     469         262 :         nc_type nAttrType = NC_NAT;
     470         262 :         size_t nAttrLen = 0;
     471         262 :         status = nc_inq_att(cdfid, nZId, pszNoValueName, &nAttrType, &nAttrLen);
     472         262 :         if (status == NC_NOERR && nAttrLen == 1 && nAttrType == NC_INT64)
     473             :         {
     474             :             long long v;
     475           7 :             nc_get_att_longlong(cdfid, nZId, pszNoValueName, &v);
     476           7 :             bGotNoData = true;
     477           7 :             bGotNoDataAsInt64 = true;
     478           7 :             nNoDataAsInt64 = static_cast<int64_t>(v);
     479             :         }
     480         255 :         else if (status == NC_NOERR && nAttrLen == 1 && nAttrType == NC_UINT64)
     481             :         {
     482             :             unsigned long long v;
     483           7 :             nc_get_att_ulonglong(cdfid, nZId, pszNoValueName, &v);
     484           7 :             bGotNoData = true;
     485           7 :             bGotNoDataAsUInt64 = true;
     486           7 :             nNoDataAsUInt64 = static_cast<uint64_t>(v);
     487             :         }
     488         248 :         else if (NCDFGetAttr(cdfid, nZId, pszNoValueName, &dfNoData) == CE_None)
     489             :         {
     490         247 :             bGotNoData = true;
     491             :         }
     492             :     }
     493             : 
     494             :     // If NoData was not found, use the default value, but for non-Byte types
     495             :     // as it is not recommended:
     496             :     // https://www.unidata.ucar.edu/software/netcdf/docs/attribute_conventions.html
     497         507 :     nc_type vartype = NC_NAT;
     498         507 :     if (!bGotNoData)
     499             :     {
     500         246 :         nc_inq_vartype(cdfid, nZId, &vartype);
     501         246 :         if (vartype == NC_INT64)
     502             :         {
     503             :             nNoDataAsInt64 =
     504           1 :                 NCDFGetDefaultNoDataValueAsInt64(cdfid, nZId, bGotNoData);
     505           1 :             bGotNoDataAsInt64 = bGotNoData;
     506             :         }
     507         245 :         else if (vartype == NC_UINT64)
     508             :         {
     509             :             nNoDataAsUInt64 =
     510           0 :                 NCDFGetDefaultNoDataValueAsUInt64(cdfid, nZId, bGotNoData);
     511           0 :             bGotNoDataAsUInt64 = bGotNoData;
     512             :         }
     513         245 :         else if (vartype != NC_CHAR && vartype != NC_BYTE &&
     514         104 :                  vartype != NC_UBYTE)
     515             :         {
     516          94 :             dfNoData =
     517          94 :                 NCDFGetDefaultNoDataValue(cdfid, nZId, vartype, bGotNoData);
     518          94 :             if (bGotNoData)
     519             :             {
     520          83 :                 CPLDebug("GDAL_netCDF",
     521             :                          "did not get nodata value for variable #%d, using "
     522             :                          "default %f",
     523             :                          nZId, dfNoData);
     524             :             }
     525             :         }
     526             :     }
     527             : 
     528         507 :     bool bHasUnderscoreUnsignedAttr = false;
     529         507 :     bool bUnderscoreUnsignedAttrVal = false;
     530             :     {
     531         507 :         char *pszTemp = nullptr;
     532         507 :         if (NCDFGetAttr(cdfid, nZId, "_Unsigned", &pszTemp) == CE_None)
     533             :         {
     534         156 :             if (EQUAL(pszTemp, "true"))
     535             :             {
     536         149 :                 bHasUnderscoreUnsignedAttr = true;
     537         149 :                 bUnderscoreUnsignedAttrVal = true;
     538             :             }
     539           7 :             else if (EQUAL(pszTemp, "false"))
     540             :             {
     541           7 :                 bHasUnderscoreUnsignedAttr = true;
     542           7 :                 bUnderscoreUnsignedAttrVal = false;
     543             :             }
     544         156 :             CPLFree(pszTemp);
     545             :         }
     546             :     }
     547             : 
     548             :     // Look for valid_range or valid_min/valid_max.
     549             : 
     550             :     // First look for valid_range.
     551         507 :     if (CPLFetchBool(poNCDFDS->GetOpenOptions(), "HONOUR_VALID_RANGE", true))
     552             :     {
     553         505 :         char *pszValidRange = nullptr;
     554         505 :         if (NCDFGetAttr(cdfid, nZId, "valid_range", &pszValidRange) ==
     555         149 :                 CE_None &&
     556         654 :             pszValidRange[0] == '{' &&
     557         149 :             pszValidRange[strlen(pszValidRange) - 1] == '}')
     558             :         {
     559             :             const std::string osValidRange =
     560         447 :                 std::string(pszValidRange).substr(1, strlen(pszValidRange) - 2);
     561             :             const CPLStringList aosValidRange(
     562         298 :                 CSLTokenizeString2(osValidRange.c_str(), ",", 0));
     563         149 :             if (aosValidRange.size() == 2 &&
     564         298 :                 CPLGetValueType(aosValidRange[0]) != CPL_VALUE_STRING &&
     565         149 :                 CPLGetValueType(aosValidRange[1]) != CPL_VALUE_STRING)
     566             :             {
     567         149 :                 bValidRangeValid = true;
     568         149 :                 adfValidRange[0] = CPLAtof(aosValidRange[0]);
     569         149 :                 adfValidRange[1] = CPLAtof(aosValidRange[1]);
     570             :             }
     571             :         }
     572         505 :         CPLFree(pszValidRange);
     573             : 
     574             :         // If not found look for valid_min and valid_max.
     575         505 :         if (!bValidRangeValid)
     576             :         {
     577         356 :             double dfMin = 0;
     578         356 :             double dfMax = 0;
     579         371 :             if (NCDFGetAttr(cdfid, nZId, "valid_min", &dfMin) == CE_None &&
     580          15 :                 NCDFGetAttr(cdfid, nZId, "valid_max", &dfMax) == CE_None)
     581             :             {
     582           8 :                 adfValidRange[0] = dfMin;
     583           8 :                 adfValidRange[1] = dfMax;
     584           8 :                 bValidRangeValid = true;
     585             :             }
     586             :         }
     587             : 
     588         505 :         if (bValidRangeValid &&
     589         157 :             (adfValidRange[0] < 0 || adfValidRange[1] < 0) &&
     590          16 :             nc_datatype == NC_SHORT && bHasUnderscoreUnsignedAttr &&
     591             :             bUnderscoreUnsignedAttrVal)
     592             :         {
     593           2 :             if (adfValidRange[0] < 0)
     594           0 :                 adfValidRange[0] += 65536;
     595           2 :             if (adfValidRange[1] < 0)
     596           2 :                 adfValidRange[1] += 65536;
     597           2 :             if (adfValidRange[0] <= adfValidRange[1])
     598             :             {
     599             :                 // Updating metadata item
     600           2 :                 GDALPamRasterBand::SetMetadataItem(
     601             :                     "valid_range",
     602           2 :                     CPLSPrintf("{%d,%d}", static_cast<int>(adfValidRange[0]),
     603           2 :                                static_cast<int>(adfValidRange[1])));
     604             :             }
     605             :         }
     606             : 
     607         505 :         if (bValidRangeValid && adfValidRange[0] > adfValidRange[1])
     608             :         {
     609           0 :             CPLError(CE_Warning, CPLE_AppDefined,
     610             :                      "netCDFDataset::valid_range: min > max:\n"
     611             :                      "  min: %lf\n  max: %lf\n",
     612             :                      adfValidRange[0], adfValidRange[1]);
     613           0 :             bValidRangeValid = false;
     614           0 :             adfValidRange[0] = 0.0;
     615           0 :             adfValidRange[1] = 0.0;
     616             :         }
     617             :     }
     618             : 
     619             :     // Special For Byte Bands: check for signed/unsigned byte.
     620         507 :     if (nc_datatype == NC_BYTE)
     621             :     {
     622             :         // netcdf uses signed byte by default, but GDAL uses unsigned by default
     623             :         // This may cause unexpected results, but is needed for back-compat.
     624         164 :         if (poNCDFDS->bIsGdalFile)
     625         142 :             bSignedData = false;
     626             :         else
     627          22 :             bSignedData = true;
     628             : 
     629             :         // For NC4 format NC_BYTE is (normally) signed, NC_UBYTE is unsigned.
     630             :         // But in case a NC3 file was converted automatically and has hints
     631             :         // that it is unsigned, take them into account
     632         164 :         if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     633             :         {
     634           3 :             bSignedData = true;
     635             :         }
     636             : 
     637             :         // If we got valid_range, test for signed/unsigned range.
     638             :         // https://docs.unidata.ucar.edu/netcdf-c/current/attribute_conventions.html
     639         164 :         if (bValidRangeValid)
     640             :         {
     641             :             // If we got valid_range={0,255}, treat as unsigned.
     642         145 :             if (adfValidRange[0] == 0 && adfValidRange[1] == 255)
     643             :             {
     644         138 :                 bSignedData = false;
     645             :                 // Reset valid_range.
     646         138 :                 bValidRangeValid = false;
     647             :             }
     648             :             // If we got valid_range={-128,127}, treat as signed.
     649           7 :             else if (adfValidRange[0] == -128 && adfValidRange[1] == 127)
     650             :             {
     651           7 :                 bSignedData = true;
     652             :                 // Reset valid_range.
     653           7 :                 bValidRangeValid = false;
     654             :             }
     655             :         }
     656             :         // Else test for _Unsigned.
     657             :         // https://docs.unidata.ucar.edu/nug/current/best_practices.html
     658             :         else
     659             :         {
     660          19 :             if (bHasUnderscoreUnsignedAttr)
     661           7 :                 bSignedData = !bUnderscoreUnsignedAttrVal;
     662             :         }
     663             : 
     664         164 :         if (bSignedData)
     665             :         {
     666          19 :             eDataType = GDT_Int8;
     667             :         }
     668         145 :         else if (dfNoData < 0)
     669             :         {
     670             :             // Fix nodata value as it was stored signed.
     671           6 :             dfNoData += 256;
     672           6 :             if (pszNoValueName)
     673             :             {
     674             :                 // Updating metadata item
     675           6 :                 GDALPamRasterBand::SetMetadataItem(
     676             :                     pszNoValueName,
     677             :                     CPLSPrintf("%d", static_cast<int>(dfNoData)));
     678             :             }
     679             :         }
     680             :     }
     681         343 :     else if (nc_datatype == NC_SHORT)
     682             :     {
     683          43 :         if (bHasUnderscoreUnsignedAttr)
     684             :         {
     685           4 :             bSignedData = !bUnderscoreUnsignedAttrVal;
     686           4 :             if (!bSignedData)
     687           4 :                 eDataType = GDT_UInt16;
     688             :         }
     689             : 
     690             :         // Fix nodata value as it was stored signed.
     691          43 :         if (!bSignedData && dfNoData < 0)
     692             :         {
     693           4 :             dfNoData += 65536;
     694           4 :             if (pszNoValueName)
     695             :             {
     696             :                 // Updating metadata item
     697           4 :                 GDALPamRasterBand::SetMetadataItem(
     698             :                     pszNoValueName,
     699             :                     CPLSPrintf("%d", static_cast<int>(dfNoData)));
     700             :             }
     701             :         }
     702             :     }
     703             : 
     704         300 :     else if (nc_datatype == NC_UBYTE || nc_datatype == NC_USHORT ||
     705         281 :              nc_datatype == NC_UINT || nc_datatype == NC_UINT64)
     706             :     {
     707          30 :         bSignedData = false;
     708             :     }
     709             : 
     710         507 :     CPLDebug("GDAL_netCDF", "netcdf type=%d gdal type=%d signedByte=%d",
     711         507 :              nc_datatype, eDataType, static_cast<int>(bSignedData));
     712             : 
     713         507 :     if (bGotNoData)
     714             :     {
     715             :         // Set nodata value.
     716         345 :         if (bGotNoDataAsInt64)
     717             :         {
     718           8 :             if (eDataType == GDT_Int64)
     719             :             {
     720           8 :                 SetNoDataValueNoUpdate(nNoDataAsInt64);
     721             :             }
     722           0 :             else if (eDataType == GDT_UInt64 && nNoDataAsInt64 >= 0)
     723             :             {
     724           0 :                 SetNoDataValueNoUpdate(static_cast<uint64_t>(nNoDataAsInt64));
     725             :             }
     726             :             else
     727             :             {
     728           0 :                 SetNoDataValueNoUpdate(static_cast<double>(nNoDataAsInt64));
     729             :             }
     730             :         }
     731         337 :         else if (bGotNoDataAsUInt64)
     732             :         {
     733           7 :             if (eDataType == GDT_UInt64)
     734             :             {
     735           7 :                 SetNoDataValueNoUpdate(nNoDataAsUInt64);
     736             :             }
     737           0 :             else if (eDataType == GDT_Int64 &&
     738             :                      nNoDataAsUInt64 <=
     739           0 :                          static_cast<uint64_t>(
     740           0 :                              std::numeric_limits<int64_t>::max()))
     741             :             {
     742           0 :                 SetNoDataValueNoUpdate(static_cast<int64_t>(nNoDataAsUInt64));
     743             :             }
     744             :             else
     745             :             {
     746           0 :                 SetNoDataValueNoUpdate(static_cast<double>(nNoDataAsUInt64));
     747             :             }
     748             :         }
     749             :         else
     750             :         {
     751             : #ifdef NCDF_DEBUG
     752             :             CPLDebug("GDAL_netCDF", "SetNoDataValue(%f) read", dfNoData);
     753             : #endif
     754         330 :             if (eDataType == GDT_Int64 && GDALIsValueExactAs<int64_t>(dfNoData))
     755             :             {
     756           0 :                 SetNoDataValueNoUpdate(static_cast<int64_t>(dfNoData));
     757             :             }
     758         330 :             else if (eDataType == GDT_UInt64 &&
     759           0 :                      GDALIsValueExactAs<uint64_t>(dfNoData))
     760             :             {
     761           0 :                 SetNoDataValueNoUpdate(static_cast<uint64_t>(dfNoData));
     762             :             }
     763             :             else
     764             :             {
     765         330 :                 SetNoDataValueNoUpdate(dfNoData);
     766             :             }
     767             :         }
     768             :     }
     769             : 
     770         507 :     CreateMetadataFromAttributes();
     771             : 
     772             :     // Attempt to fetch the scale_factor and add_offset attributes for the
     773             :     // variable and set them.  If these values are not available, set
     774             :     // offset to 0 and scale to 1.
     775         507 :     if (nc_inq_attid(cdfid, nZId, CF_ADD_OFFSET, nullptr) == NC_NOERR)
     776             :     {
     777          21 :         double dfOffset = 0;
     778          21 :         status = nc_get_att_double(cdfid, nZId, CF_ADD_OFFSET, &dfOffset);
     779          21 :         CPLDebug("GDAL_netCDF", "got add_offset=%.16g, status=%d", dfOffset,
     780             :                  status);
     781          21 :         SetOffsetNoUpdate(dfOffset);
     782             :     }
     783             : 
     784         507 :     bool bHasScale = false;
     785         507 :     if (nc_inq_attid(cdfid, nZId, CF_SCALE_FACTOR, nullptr) == NC_NOERR)
     786             :     {
     787          23 :         bHasScale = true;
     788          23 :         double dfScale = 1;
     789          23 :         status = nc_get_att_double(cdfid, nZId, CF_SCALE_FACTOR, &dfScale);
     790          23 :         CPLDebug("GDAL_netCDF", "got scale_factor=%.16g, status=%d", dfScale,
     791             :                  status);
     792          23 :         SetScaleNoUpdate(dfScale);
     793             :     }
     794             : 
     795          12 :     if (bValidRangeValid && GDALDataTypeIsInteger(eDataType) &&
     796           4 :         eDataType != GDT_Int64 && eDataType != GDT_UInt64 &&
     797           4 :         (std::fabs(std::round(adfValidRange[0]) - adfValidRange[0]) > 1e-5 ||
     798         519 :          std::fabs(std::round(adfValidRange[1]) - adfValidRange[1]) > 1e-5) &&
     799           1 :         CSLFetchNameValue(poNCDFDS->GetOpenOptions(), "HONOUR_VALID_RANGE") ==
     800             :             nullptr)
     801             :     {
     802           1 :         CPLError(CE_Warning, CPLE_AppDefined,
     803             :                  "validity range = %f, %f contains floating-point values, "
     804             :                  "whereas data type is integer. valid_range is thus likely "
     805             :                  "wrong%s. Ignoring it.",
     806             :                  adfValidRange[0], adfValidRange[1],
     807             :                  bHasScale ? " (likely scaled using scale_factor/add_factor "
     808             :                              "whereas it should be using the packed data type)"
     809             :                            : "");
     810           1 :         bValidRangeValid = false;
     811           1 :         adfValidRange[0] = 0.0;
     812           1 :         adfValidRange[1] = 0.0;
     813             :     }
     814             : 
     815             :     // Should we check for longitude values > 360?
     816         507 :     bCheckLongitude =
     817        1014 :         CPLTestBool(CPLGetConfigOption("GDAL_NETCDF_CENTERLONG_180", "YES")) &&
     818         507 :         NCDFIsVarLongitude(cdfid, nZId, nullptr);
     819             : 
     820             :     // Attempt to fetch the units attribute for the variable and set it.
     821         507 :     SetUnitTypeNoUpdate(netCDFRasterBand::GetMetadataItem(CF_UNITS));
     822             : 
     823         507 :     SetBlockSize();
     824             : }
     825             : 
     826         634 : void netCDFRasterBand::SetBlockSize()
     827             : {
     828             :     // Check for variable chunking (netcdf-4 only).
     829             :     // GDAL block size should be set to hdf5 chunk size.
     830         634 :     int nTmpFormat = 0;
     831         634 :     int status = nc_inq_format(cdfid, &nTmpFormat);
     832         634 :     NetCDFFormatEnum eTmpFormat = static_cast<NetCDFFormatEnum>(nTmpFormat);
     833         634 :     if ((status == NC_NOERR) &&
     834         542 :         (eTmpFormat == NCDF_FORMAT_NC4 || eTmpFormat == NCDF_FORMAT_NC4C))
     835             :     {
     836         108 :         size_t chunksize[MAX_NC_DIMS] = {};
     837             :         // Check for chunksize and set it as the blocksize (optimizes read).
     838         108 :         status = nc_inq_var_chunking(cdfid, nZId, &nTmpFormat, chunksize);
     839         108 :         if ((status == NC_NOERR) && (nTmpFormat == NC_CHUNKED))
     840             :         {
     841          14 :             nBlockXSize = (int)chunksize[nZDim - 1];
     842          14 :             if (nZDim >= 2)
     843          14 :                 nBlockYSize = (int)chunksize[nZDim - 2];
     844             :             else
     845           0 :                 nBlockYSize = 1;
     846             :         }
     847             :     }
     848             : 
     849             :     // Deal with bottom-up datasets and nBlockYSize != 1.
     850         634 :     auto poGDS = cpl::down_cast<netCDFDataset *>(poDS);
     851         634 :     if (poGDS->bBottomUp && nBlockYSize != 1 && poGDS->poChunkCache == nullptr)
     852             :     {
     853           6 :         if (poGDS->eAccess == GA_ReadOnly)
     854             :         {
     855             :             // Try to cache 1 or 2 'rows' of netCDF chunks along the whole
     856             :             // width of the raster
     857           6 :             size_t nChunks =
     858           6 :                 static_cast<size_t>(DIV_ROUND_UP(nRasterXSize, nBlockXSize));
     859           6 :             if ((nRasterYSize % nBlockYSize) != 0)
     860           2 :                 nChunks *= 2;
     861             :             const size_t nChunkSize =
     862           6 :                 static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
     863           6 :                 nBlockXSize * nBlockYSize;
     864           6 :             constexpr size_t MAX_CACHE_SIZE = 100 * 1024 * 1024;
     865           6 :             nChunks = std::min(nChunks, MAX_CACHE_SIZE / nChunkSize);
     866           6 :             if (nChunks)
     867             :             {
     868             :                 poGDS->poChunkCache =
     869           6 :                     std::make_unique<netCDFDataset::ChunkCacheType>(nChunks);
     870             :             }
     871             :         }
     872             :         else
     873             :         {
     874           0 :             nBlockYSize = 1;
     875             :         }
     876             :     }
     877         634 : }
     878             : 
     879             : // Constructor in create mode.
     880             : // If nZId and following variables are not passed, the band will have 2
     881             : // dimensions.
     882             : // TODO: Get metadata, missing val from band #1 if nZDim > 2.
     883         127 : netCDFRasterBand::netCDFRasterBand(
     884             :     const netCDFRasterBand::CONSTRUCTOR_CREATE &, netCDFDataset *poNCDFDS,
     885             :     const GDALDataType eTypeIn, int nBandIn, bool bSigned,
     886             :     const char *pszBandName, const char *pszLongName, int nZIdIn, int nZDimIn,
     887             :     int nLevelIn, const int *panBandZLevIn, const int *panBandZPosIn,
     888         127 :     const int *paDimIds)
     889         127 :     : nc_datatype(NC_NAT), cdfid(poNCDFDS->GetCDFID()), nZId(nZIdIn),
     890             :       nZDim(nZDimIn), nLevel(nLevelIn), nBandXPos(1), nBandYPos(0),
     891             :       panBandZPos(nullptr), panBandZLev(nullptr), bSignedData(bSigned),
     892         127 :       bCheckLongitude(false), m_bCreateMetadataFromOtherVarsDone(true)
     893             : {
     894         127 :     poDS = poNCDFDS;
     895         127 :     nBand = nBandIn;
     896             : 
     897         127 :     nRasterXSize = poDS->GetRasterXSize();
     898         127 :     nRasterYSize = poDS->GetRasterYSize();
     899         127 :     nBlockXSize = poDS->GetRasterXSize();
     900         127 :     nBlockYSize = 1;
     901             : 
     902         127 :     if (poDS->GetAccess() != GA_Update)
     903             :     {
     904           0 :         CPLError(CE_Failure, CPLE_NotSupported,
     905             :                  "Dataset is not in update mode, "
     906             :                  "wrong netCDFRasterBand constructor");
     907           0 :         return;
     908             :     }
     909             : 
     910             :     // Take care of all other dimensions.
     911         127 :     if (nZDim > 2 && paDimIds != nullptr)
     912             :     {
     913          28 :         nBandXPos = panBandZPosIn[0];
     914          28 :         nBandYPos = panBandZPosIn[1];
     915             : 
     916          28 :         panBandZPos = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     917          28 :         panBandZLev = static_cast<int *>(CPLCalloc(nZDim - 1, sizeof(int)));
     918             : 
     919          78 :         for (int i = 0; i < nZDim - 2; i++)
     920             :         {
     921          50 :             panBandZPos[i] = panBandZPosIn[i + 2];
     922          50 :             panBandZLev[i] = panBandZLevIn[i];
     923             :         }
     924             :     }
     925             : 
     926             :     // Get the type of the "z" variable, our target raster array.
     927         127 :     eDataType = eTypeIn;
     928             : 
     929         127 :     switch (eDataType)
     930             :     {
     931          72 :         case GDT_UInt8:
     932          72 :             nc_datatype = NC_BYTE;
     933             :             // NC_UBYTE (unsigned byte) is only available for NC4.
     934          72 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     935           3 :                 nc_datatype = NC_UBYTE;
     936          72 :             break;
     937           3 :         case GDT_Int8:
     938           3 :             nc_datatype = NC_BYTE;
     939           3 :             break;
     940           7 :         case GDT_Int16:
     941           7 :             nc_datatype = NC_SHORT;
     942           7 :             break;
     943          20 :         case GDT_Int32:
     944          20 :             nc_datatype = NC_INT;
     945          20 :             break;
     946          11 :         case GDT_Float32:
     947          11 :             nc_datatype = NC_FLOAT;
     948          11 :             break;
     949           4 :         case GDT_Float64:
     950           4 :             nc_datatype = NC_DOUBLE;
     951           4 :             break;
     952           3 :         case GDT_Int64:
     953           3 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     954             :             {
     955           3 :                 nc_datatype = NC_INT64;
     956             :             }
     957             :             else
     958             :             {
     959           0 :                 if (nBand == 1)
     960           0 :                     CPLError(
     961             :                         CE_Warning, CPLE_AppDefined,
     962             :                         "Unsupported GDAL datatype %s, treat as NC_DOUBLE.",
     963             :                         "Int64");
     964           0 :                 nc_datatype = NC_DOUBLE;
     965           0 :                 eDataType = GDT_Float64;
     966             :             }
     967           3 :             break;
     968           3 :         case GDT_UInt64:
     969           3 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     970             :             {
     971           3 :                 nc_datatype = NC_UINT64;
     972             :             }
     973             :             else
     974             :             {
     975           0 :                 if (nBand == 1)
     976           0 :                     CPLError(
     977             :                         CE_Warning, CPLE_AppDefined,
     978             :                         "Unsupported GDAL datatype %s, treat as NC_DOUBLE.",
     979             :                         "UInt64");
     980           0 :                 nc_datatype = NC_DOUBLE;
     981           0 :                 eDataType = GDT_Float64;
     982             :             }
     983           3 :             break;
     984           2 :         case GDT_UInt16:
     985           2 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     986             :             {
     987           2 :                 nc_datatype = NC_USHORT;
     988           2 :                 break;
     989             :             }
     990             :             [[fallthrough]];
     991             :         case GDT_UInt32:
     992           2 :             if (poNCDFDS->eFormat == NCDF_FORMAT_NC4)
     993             :             {
     994           2 :                 nc_datatype = NC_UINT;
     995           2 :                 break;
     996             :             }
     997             :             [[fallthrough]];
     998             :         default:
     999           0 :             if (nBand == 1)
    1000           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    1001             :                          "Unsupported GDAL datatype (%d), treat as NC_FLOAT.",
    1002           0 :                          static_cast<int>(eDataType));
    1003           0 :             nc_datatype = NC_FLOAT;
    1004           0 :             eDataType = GDT_Float32;
    1005           0 :             break;
    1006             :     }
    1007             : 
    1008             :     // Define the variable if necessary (if nZId == -1).
    1009         127 :     bool bDefineVar = false;
    1010             : 
    1011         127 :     if (nZId == -1)
    1012             :     {
    1013         105 :         bDefineVar = true;
    1014             : 
    1015             :         // Make sure we are in define mode.
    1016         105 :         cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1017             : 
    1018             :         char szTempPrivate[256 + 1];
    1019         105 :         const char *pszTemp = nullptr;
    1020         105 :         if (!pszBandName || EQUAL(pszBandName, ""))
    1021             :         {
    1022          77 :             snprintf(szTempPrivate, sizeof(szTempPrivate), "Band%d", nBand);
    1023          77 :             pszTemp = szTempPrivate;
    1024             :         }
    1025             :         else
    1026             :         {
    1027          28 :             pszTemp = pszBandName;
    1028             :         }
    1029             : 
    1030             :         int status;
    1031         105 :         if (nZDim > 2 && paDimIds != nullptr)
    1032             :         {
    1033           6 :             status =
    1034           6 :                 nc_def_var(cdfid, pszTemp, nc_datatype, nZDim, paDimIds, &nZId);
    1035             :         }
    1036             :         else
    1037             :         {
    1038          99 :             int anBandDims[2] = {poNCDFDS->nYDimID, poNCDFDS->nXDimID};
    1039             :             status =
    1040          99 :                 nc_def_var(cdfid, pszTemp, nc_datatype, 2, anBandDims, &nZId);
    1041             :         }
    1042         105 :         NCDF_ERR(status);
    1043         105 :         CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d) id=%d", cdfid, pszTemp,
    1044             :                  nc_datatype, nZId);
    1045             : 
    1046         105 :         if (!pszLongName || EQUAL(pszLongName, ""))
    1047             :         {
    1048          92 :             snprintf(szTempPrivate, sizeof(szTempPrivate),
    1049             :                      "GDAL Band Number %d", nBand);
    1050          92 :             pszTemp = szTempPrivate;
    1051             :         }
    1052             :         else
    1053             :         {
    1054          13 :             pszTemp = pszLongName;
    1055             :         }
    1056             :         status =
    1057         105 :             nc_put_att_text(cdfid, nZId, CF_LNG_NAME, strlen(pszTemp), pszTemp);
    1058         105 :         NCDF_ERR(status);
    1059             : 
    1060         105 :         poNCDFDS->DefVarDeflate(nZId, true);
    1061             :     }
    1062             : 
    1063             :     // For Byte data add signed/unsigned info.
    1064         127 :     if (eDataType == GDT_UInt8 || eDataType == GDT_Int8)
    1065             :     {
    1066          75 :         if (bDefineVar)
    1067             :         {
    1068             :             // Only add attributes if creating variable.
    1069             :             // For unsigned NC_BYTE (except NC4 format),
    1070             :             // add valid_range and _Unsigned ( defined in CF-1 and NUG ).
    1071          67 :             if (nc_datatype == NC_BYTE && poNCDFDS->eFormat != NCDF_FORMAT_NC4)
    1072             :             {
    1073          64 :                 CPLDebug("GDAL_netCDF",
    1074             :                          "adding valid_range attributes for Byte Band");
    1075          64 :                 short l_adfValidRange[2] = {0, 0};
    1076             :                 int status;
    1077          64 :                 if (bSignedData || eDataType == GDT_Int8)
    1078             :                 {
    1079           3 :                     l_adfValidRange[0] = -128;
    1080           3 :                     l_adfValidRange[1] = 127;
    1081           3 :                     status =
    1082           3 :                         nc_put_att_text(cdfid, nZId, "_Unsigned", 5, "false");
    1083             :                 }
    1084             :                 else
    1085             :                 {
    1086          61 :                     l_adfValidRange[0] = 0;
    1087          61 :                     l_adfValidRange[1] = 255;
    1088             :                     status =
    1089          61 :                         nc_put_att_text(cdfid, nZId, "_Unsigned", 4, "true");
    1090             :                 }
    1091          64 :                 NCDF_ERR(status);
    1092          64 :                 status = nc_put_att_short(cdfid, nZId, "valid_range", NC_SHORT,
    1093             :                                           2, l_adfValidRange);
    1094          64 :                 NCDF_ERR(status);
    1095             :             }
    1096             :         }
    1097             :     }
    1098             : 
    1099         127 :     if (nc_datatype != NC_BYTE && nc_datatype != NC_CHAR &&
    1100          55 :         nc_datatype != NC_UBYTE)
    1101             :     {
    1102             :         // Set default nodata.
    1103          52 :         bool bIgnored = false;
    1104             :         double dfNoData =
    1105          52 :             NCDFGetDefaultNoDataValue(cdfid, nZId, nc_datatype, bIgnored);
    1106             : #ifdef NCDF_DEBUG
    1107             :         CPLDebug("GDAL_netCDF", "SetNoDataValue(%f) default", dfNoData);
    1108             : #endif
    1109          52 :         netCDFRasterBand::SetNoDataValue(dfNoData);
    1110             :     }
    1111             : 
    1112         127 :     SetBlockSize();
    1113             : }
    1114             : 
    1115             : /************************************************************************/
    1116             : /*                         ~netCDFRasterBand()                          */
    1117             : /************************************************************************/
    1118             : 
    1119        1268 : netCDFRasterBand::~netCDFRasterBand()
    1120             : {
    1121         634 :     netCDFRasterBand::FlushCache(true);
    1122         634 :     CPLFree(panBandZPos);
    1123         634 :     CPLFree(panBandZLev);
    1124        1268 : }
    1125             : 
    1126             : /************************************************************************/
    1127             : /*                            GetMetadata()                             */
    1128             : /************************************************************************/
    1129             : 
    1130          60 : CSLConstList netCDFRasterBand::GetMetadata(const char *pszDomain)
    1131             : {
    1132          60 :     if (!m_bCreateMetadataFromOtherVarsDone)
    1133          58 :         CreateMetadataFromOtherVars();
    1134          60 :     return GDALPamRasterBand::GetMetadata(pszDomain);
    1135             : }
    1136             : 
    1137             : /************************************************************************/
    1138             : /*                          GetMetadataItem()                           */
    1139             : /************************************************************************/
    1140             : 
    1141         594 : const char *netCDFRasterBand::GetMetadataItem(const char *pszName,
    1142             :                                               const char *pszDomain)
    1143             : {
    1144         594 :     if (!m_bCreateMetadataFromOtherVarsDone &&
    1145         578 :         STARTS_WITH(pszName, "NETCDF_DIM_") &&
    1146           1 :         (!pszDomain || pszDomain[0] == 0))
    1147           1 :         CreateMetadataFromOtherVars();
    1148         594 :     return GDALPamRasterBand::GetMetadataItem(pszName, pszDomain);
    1149             : }
    1150             : 
    1151             : /************************************************************************/
    1152             : /*                          SetMetadataItem()                           */
    1153             : /************************************************************************/
    1154             : 
    1155           7 : CPLErr netCDFRasterBand::SetMetadataItem(const char *pszName,
    1156             :                                          const char *pszValue,
    1157             :                                          const char *pszDomain)
    1158             : {
    1159           9 :     if (GetAccess() == GA_Update &&
    1160           9 :         (pszDomain == nullptr || pszDomain[0] == '\0') && pszValue != nullptr)
    1161             :     {
    1162             :         // Same logic as in CopyMetadata()
    1163             : 
    1164           2 :         const char *const papszIgnoreBand[] = {
    1165             :             CF_ADD_OFFSET,  CF_SCALE_FACTOR, "valid_range", "_Unsigned",
    1166             :             NCDF_FillValue, "coordinates",   nullptr};
    1167             :         // Do not copy varname, stats, NETCDF_DIM_*, nodata
    1168             :         // and items in papszIgnoreBand.
    1169           6 :         if (STARTS_WITH(pszName, "NETCDF_VARNAME") ||
    1170           2 :             STARTS_WITH(pszName, "STATISTICS_") ||
    1171           2 :             STARTS_WITH(pszName, "NETCDF_DIM_") ||
    1172           2 :             STARTS_WITH(pszName, "missing_value") ||
    1173           6 :             STARTS_WITH(pszName, "_FillValue") ||
    1174           2 :             CSLFindString(papszIgnoreBand, pszName) != -1)
    1175             :         {
    1176             :             // do nothing
    1177             :         }
    1178             :         else
    1179             :         {
    1180           2 :             cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1181             : 
    1182           2 :             if (!NCDFPutAttr(cdfid, nZId, pszName, pszValue))
    1183           2 :                 return CE_Failure;
    1184             :         }
    1185             :     }
    1186             : 
    1187           5 :     return GDALPamRasterBand::SetMetadataItem(pszName, pszValue, pszDomain);
    1188             : }
    1189             : 
    1190             : /************************************************************************/
    1191             : /*                            SetMetadata()                             */
    1192             : /************************************************************************/
    1193             : 
    1194           2 : CPLErr netCDFRasterBand::SetMetadata(CSLConstList papszMD,
    1195             :                                      const char *pszDomain)
    1196             : {
    1197           4 :     if (GetAccess() == GA_Update &&
    1198           2 :         (pszDomain == nullptr || pszDomain[0] == '\0'))
    1199             :     {
    1200             :         // We don't handle metadata item removal for now
    1201           4 :         for (const char *const *papszIter = papszMD; papszIter && *papszIter;
    1202             :              ++papszIter)
    1203             :         {
    1204           2 :             char *pszName = nullptr;
    1205           2 :             const char *pszValue = CPLParseNameValue(*papszIter, &pszName);
    1206           2 :             if (pszName && pszValue)
    1207           2 :                 SetMetadataItem(pszName, pszValue);
    1208           2 :             CPLFree(pszName);
    1209             :         }
    1210             :     }
    1211           2 :     return GDALPamRasterBand::SetMetadata(papszMD, pszDomain);
    1212             : }
    1213             : 
    1214             : /************************************************************************/
    1215             : /*                             GetOffset()                              */
    1216             : /************************************************************************/
    1217          56 : double netCDFRasterBand::GetOffset(int *pbSuccess)
    1218             : {
    1219          56 :     if (pbSuccess != nullptr)
    1220          50 :         *pbSuccess = static_cast<int>(m_bHaveOffset);
    1221             : 
    1222          56 :     return m_dfOffset;
    1223             : }
    1224             : 
    1225             : /************************************************************************/
    1226             : /*                             SetOffset()                              */
    1227             : /************************************************************************/
    1228           6 : CPLErr netCDFRasterBand::SetOffset(double dfNewOffset)
    1229             : {
    1230          12 :     CPLMutexHolderD(&hNCMutex);
    1231             : 
    1232             :     // Write value if in update mode.
    1233           6 :     if (poDS->GetAccess() == GA_Update)
    1234             :     {
    1235             :         // Make sure we are in define mode.
    1236           6 :         cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1237             : 
    1238           6 :         const int status = nc_put_att_double(cdfid, nZId, CF_ADD_OFFSET,
    1239             :                                              NC_DOUBLE, 1, &dfNewOffset);
    1240             : 
    1241           6 :         NCDF_ERR(status);
    1242           6 :         if (status == NC_NOERR)
    1243             :         {
    1244           6 :             SetOffsetNoUpdate(dfNewOffset);
    1245           6 :             return CE_None;
    1246             :         }
    1247             : 
    1248           0 :         return CE_Failure;
    1249             :     }
    1250             : 
    1251           0 :     SetOffsetNoUpdate(dfNewOffset);
    1252           0 :     return CE_None;
    1253             : }
    1254             : 
    1255             : /************************************************************************/
    1256             : /*                         SetOffsetNoUpdate()                          */
    1257             : /************************************************************************/
    1258          27 : void netCDFRasterBand::SetOffsetNoUpdate(double dfVal)
    1259             : {
    1260          27 :     m_dfOffset = dfVal;
    1261          27 :     m_bHaveOffset = true;
    1262          27 : }
    1263             : 
    1264             : /************************************************************************/
    1265             : /*                              GetScale()                              */
    1266             : /************************************************************************/
    1267          56 : double netCDFRasterBand::GetScale(int *pbSuccess)
    1268             : {
    1269          56 :     if (pbSuccess != nullptr)
    1270          50 :         *pbSuccess = static_cast<int>(m_bHaveScale);
    1271             : 
    1272          56 :     return m_dfScale;
    1273             : }
    1274             : 
    1275             : /************************************************************************/
    1276             : /*                              SetScale()                              */
    1277             : /************************************************************************/
    1278           1 : CPLErr netCDFRasterBand::SetScale(double dfNewScale)
    1279             : {
    1280           2 :     CPLMutexHolderD(&hNCMutex);
    1281             : 
    1282             :     // Write value if in update mode.
    1283           1 :     if (poDS->GetAccess() == GA_Update)
    1284             :     {
    1285             :         // Make sure we are in define mode.
    1286           1 :         cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1287             : 
    1288           1 :         const int status = nc_put_att_double(cdfid, nZId, CF_SCALE_FACTOR,
    1289             :                                              NC_DOUBLE, 1, &dfNewScale);
    1290             : 
    1291           1 :         NCDF_ERR(status);
    1292           1 :         if (status == NC_NOERR)
    1293             :         {
    1294           1 :             SetScaleNoUpdate(dfNewScale);
    1295           1 :             return CE_None;
    1296             :         }
    1297             : 
    1298           0 :         return CE_Failure;
    1299             :     }
    1300             : 
    1301           0 :     SetScaleNoUpdate(dfNewScale);
    1302           0 :     return CE_None;
    1303             : }
    1304             : 
    1305             : /************************************************************************/
    1306             : /*                          SetScaleNoUpdate()                          */
    1307             : /************************************************************************/
    1308          24 : void netCDFRasterBand::SetScaleNoUpdate(double dfVal)
    1309             : {
    1310          24 :     m_dfScale = dfVal;
    1311          24 :     m_bHaveScale = true;
    1312          24 : }
    1313             : 
    1314             : /************************************************************************/
    1315             : /*                            GetUnitType()                             */
    1316             : /************************************************************************/
    1317             : 
    1318          27 : const char *netCDFRasterBand::GetUnitType()
    1319             : 
    1320             : {
    1321          27 :     if (!m_osUnitType.empty())
    1322           6 :         return m_osUnitType;
    1323             : 
    1324          21 :     return GDALRasterBand::GetUnitType();
    1325             : }
    1326             : 
    1327             : /************************************************************************/
    1328             : /*                            SetUnitType()                             */
    1329             : /************************************************************************/
    1330             : 
    1331           1 : CPLErr netCDFRasterBand::SetUnitType(const char *pszNewValue)
    1332             : 
    1333             : {
    1334           2 :     CPLMutexHolderD(&hNCMutex);
    1335             : 
    1336           2 :     const std::string osUnitType = (pszNewValue != nullptr ? pszNewValue : "");
    1337             : 
    1338           1 :     if (!osUnitType.empty())
    1339             :     {
    1340             :         // Write value if in update mode.
    1341           1 :         if (poDS->GetAccess() == GA_Update)
    1342             :         {
    1343             :             // Make sure we are in define mode.
    1344           1 :             cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(TRUE);
    1345             : 
    1346           1 :             const int status = nc_put_att_text(
    1347             :                 cdfid, nZId, CF_UNITS, osUnitType.size(), osUnitType.c_str());
    1348             : 
    1349           1 :             NCDF_ERR(status);
    1350           1 :             if (status == NC_NOERR)
    1351             :             {
    1352           1 :                 SetUnitTypeNoUpdate(pszNewValue);
    1353           1 :                 return CE_None;
    1354             :             }
    1355             : 
    1356           0 :             return CE_Failure;
    1357             :         }
    1358             :     }
    1359             : 
    1360           0 :     SetUnitTypeNoUpdate(pszNewValue);
    1361             : 
    1362           0 :     return CE_None;
    1363             : }
    1364             : 
    1365             : /************************************************************************/
    1366             : /*                        SetUnitTypeNoUpdate()                         */
    1367             : /************************************************************************/
    1368             : 
    1369         508 : void netCDFRasterBand::SetUnitTypeNoUpdate(const char *pszNewValue)
    1370             : {
    1371         508 :     m_osUnitType = (pszNewValue != nullptr ? pszNewValue : "");
    1372         508 : }
    1373             : 
    1374             : /************************************************************************/
    1375             : /*                           GetNoDataValue()                           */
    1376             : /************************************************************************/
    1377             : 
    1378         208 : double netCDFRasterBand::GetNoDataValue(int *pbSuccess)
    1379             : 
    1380             : {
    1381         208 :     if (m_bNoDataSetAsInt64)
    1382             :     {
    1383           0 :         if (pbSuccess)
    1384           0 :             *pbSuccess = TRUE;
    1385           0 :         return GDALGetNoDataValueCastToDouble(m_nNodataValueInt64);
    1386             :     }
    1387             : 
    1388         208 :     if (m_bNoDataSetAsUInt64)
    1389             :     {
    1390           0 :         if (pbSuccess)
    1391           0 :             *pbSuccess = TRUE;
    1392           0 :         return GDALGetNoDataValueCastToDouble(m_nNodataValueUInt64);
    1393             :     }
    1394             : 
    1395         208 :     if (m_bNoDataSet)
    1396             :     {
    1397         145 :         if (pbSuccess)
    1398         128 :             *pbSuccess = TRUE;
    1399         145 :         return m_dfNoDataValue;
    1400             :     }
    1401             : 
    1402          63 :     return GDALPamRasterBand::GetNoDataValue(pbSuccess);
    1403             : }
    1404             : 
    1405             : /************************************************************************/
    1406             : /*                       GetNoDataValueAsInt64()                        */
    1407             : /************************************************************************/
    1408             : 
    1409           4 : int64_t netCDFRasterBand::GetNoDataValueAsInt64(int *pbSuccess)
    1410             : 
    1411             : {
    1412           4 :     if (m_bNoDataSetAsInt64)
    1413             :     {
    1414           4 :         if (pbSuccess)
    1415           4 :             *pbSuccess = TRUE;
    1416             : 
    1417           4 :         return m_nNodataValueInt64;
    1418             :     }
    1419             : 
    1420           0 :     return GDALPamRasterBand::GetNoDataValueAsInt64(pbSuccess);
    1421             : }
    1422             : 
    1423             : /************************************************************************/
    1424             : /*                       GetNoDataValueAsUInt64()                       */
    1425             : /************************************************************************/
    1426             : 
    1427           4 : uint64_t netCDFRasterBand::GetNoDataValueAsUInt64(int *pbSuccess)
    1428             : 
    1429             : {
    1430           4 :     if (m_bNoDataSetAsUInt64)
    1431             :     {
    1432           4 :         if (pbSuccess)
    1433           4 :             *pbSuccess = TRUE;
    1434             : 
    1435           4 :         return m_nNodataValueUInt64;
    1436             :     }
    1437             : 
    1438           0 :     return GDALPamRasterBand::GetNoDataValueAsUInt64(pbSuccess);
    1439             : }
    1440             : 
    1441             : /************************************************************************/
    1442             : /*                           SetNoDataValue()                           */
    1443             : /************************************************************************/
    1444             : 
    1445          89 : CPLErr netCDFRasterBand::SetNoDataValue(double dfNoData)
    1446             : 
    1447             : {
    1448         178 :     CPLMutexHolderD(&hNCMutex);
    1449             : 
    1450             :     // If already set to new value, don't do anything.
    1451          89 :     if (m_bNoDataSet && CPLIsEqual(dfNoData, m_dfNoDataValue))
    1452          19 :         return CE_None;
    1453             : 
    1454             :     // Write value if in update mode.
    1455          70 :     if (poDS->GetAccess() == GA_Update)
    1456             :     {
    1457             :         // netcdf-4 does not allow to set _FillValue after leaving define mode,
    1458             :         // but it is ok if variable has not been written to, so only print
    1459             :         // debug. See bug #4484.
    1460          81 :         if (m_bNoDataSet &&
    1461          11 :             !cpl::down_cast<netCDFDataset *>(poDS)->GetDefineMode())
    1462             :         {
    1463           0 :             CPLDebug("GDAL_netCDF",
    1464             :                      "Setting NoDataValue to %.17g (previously set to %.17g) "
    1465             :                      "but file is no longer in define mode (id #%d, band #%d)",
    1466             :                      dfNoData, m_dfNoDataValue, cdfid, nBand);
    1467             :         }
    1468             : #ifdef NCDF_DEBUG
    1469             :         else
    1470             :         {
    1471             :             CPLDebug("GDAL_netCDF",
    1472             :                      "Setting NoDataValue to %.17g (id #%d, band #%d)",
    1473             :                      dfNoData, cdfid, nBand);
    1474             :         }
    1475             : #endif
    1476             :         // Make sure we are in define mode.
    1477          70 :         cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1478             : 
    1479             :         int status;
    1480          70 :         if (eDataType == GDT_UInt8)
    1481             :         {
    1482           6 :             if (bSignedData)
    1483             :             {
    1484           0 :                 signed char cNoDataValue = static_cast<signed char>(dfNoData);
    1485           0 :                 status = nc_put_att_schar(cdfid, nZId, NCDF_FillValue,
    1486             :                                           nc_datatype, 1, &cNoDataValue);
    1487             :             }
    1488             :             else
    1489             :             {
    1490           6 :                 const unsigned char ucNoDataValue =
    1491           6 :                     static_cast<unsigned char>(dfNoData);
    1492           6 :                 status = nc_put_att_uchar(cdfid, nZId, NCDF_FillValue,
    1493             :                                           nc_datatype, 1, &ucNoDataValue);
    1494             :             }
    1495             :         }
    1496          64 :         else if (eDataType == GDT_Int16)
    1497             :         {
    1498          10 :             short nsNoDataValue = static_cast<short>(dfNoData);
    1499          10 :             status = nc_put_att_short(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1500             :                                       1, &nsNoDataValue);
    1501             :         }
    1502          54 :         else if (eDataType == GDT_Int32)
    1503             :         {
    1504          23 :             int nNoDataValue = static_cast<int>(dfNoData);
    1505          23 :             status = nc_put_att_int(cdfid, nZId, NCDF_FillValue, nc_datatype, 1,
    1506             :                                     &nNoDataValue);
    1507             :         }
    1508          31 :         else if (eDataType == GDT_Float32)
    1509             :         {
    1510          14 :             float fNoDataValue = static_cast<float>(dfNoData);
    1511          14 :             status = nc_put_att_float(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1512             :                                       1, &fNoDataValue);
    1513             :         }
    1514          19 :         else if (eDataType == GDT_UInt16 &&
    1515           2 :                  cpl::down_cast<netCDFDataset *>(poDS)->eFormat ==
    1516             :                      NCDF_FORMAT_NC4)
    1517             :         {
    1518           2 :             unsigned short usNoDataValue =
    1519           2 :                 static_cast<unsigned short>(dfNoData);
    1520           2 :             status = nc_put_att_ushort(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1521             :                                        1, &usNoDataValue);
    1522             :         }
    1523          18 :         else if (eDataType == GDT_UInt32 &&
    1524           3 :                  cpl::down_cast<netCDFDataset *>(poDS)->eFormat ==
    1525             :                      NCDF_FORMAT_NC4)
    1526             :         {
    1527           3 :             unsigned int unNoDataValue = static_cast<unsigned int>(dfNoData);
    1528           3 :             status = nc_put_att_uint(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1529             :                                      1, &unNoDataValue);
    1530             :         }
    1531             :         else
    1532             :         {
    1533          12 :             status = nc_put_att_double(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1534             :                                        1, &dfNoData);
    1535             :         }
    1536             : 
    1537          70 :         NCDF_ERR(status);
    1538             : 
    1539             :         // Update status if write worked.
    1540          70 :         if (status == NC_NOERR)
    1541             :         {
    1542          70 :             SetNoDataValueNoUpdate(dfNoData);
    1543          70 :             return CE_None;
    1544             :         }
    1545             : 
    1546           0 :         return CE_Failure;
    1547             :     }
    1548             : 
    1549           0 :     SetNoDataValueNoUpdate(dfNoData);
    1550           0 :     return CE_None;
    1551             : }
    1552             : 
    1553             : /************************************************************************/
    1554             : /*                       SetNoDataValueNoUpdate()                       */
    1555             : /************************************************************************/
    1556             : 
    1557         400 : void netCDFRasterBand::SetNoDataValueNoUpdate(double dfNoData)
    1558             : {
    1559         400 :     m_dfNoDataValue = dfNoData;
    1560         400 :     m_bNoDataSet = true;
    1561         400 :     m_bNoDataSetAsInt64 = false;
    1562         400 :     m_bNoDataSetAsUInt64 = false;
    1563         400 : }
    1564             : 
    1565             : /************************************************************************/
    1566             : /*                       SetNoDataValueAsInt64()                        */
    1567             : /************************************************************************/
    1568             : 
    1569           3 : CPLErr netCDFRasterBand::SetNoDataValueAsInt64(int64_t nNoData)
    1570             : 
    1571             : {
    1572           6 :     CPLMutexHolderD(&hNCMutex);
    1573             : 
    1574             :     // If already set to new value, don't do anything.
    1575           3 :     if (m_bNoDataSetAsInt64 && nNoData == m_nNodataValueInt64)
    1576           0 :         return CE_None;
    1577             : 
    1578             :     // Write value if in update mode.
    1579           3 :     if (poDS->GetAccess() == GA_Update)
    1580             :     {
    1581             :         // netcdf-4 does not allow to set NCDF_FillValue after leaving define mode,
    1582             :         // but it is ok if variable has not been written to, so only print
    1583             :         // debug. See bug #4484.
    1584           3 :         if (m_bNoDataSetAsInt64 &&
    1585           0 :             !cpl::down_cast<netCDFDataset *>(poDS)->GetDefineMode())
    1586             :         {
    1587           0 :             CPLDebug("GDAL_netCDF",
    1588             :                      "Setting NoDataValue to " CPL_FRMT_GIB
    1589             :                      " (previously set to " CPL_FRMT_GIB ") "
    1590             :                      "but file is no longer in define mode (id #%d, band #%d)",
    1591             :                      static_cast<GIntBig>(nNoData),
    1592           0 :                      static_cast<GIntBig>(m_nNodataValueInt64), cdfid, nBand);
    1593             :         }
    1594             : #ifdef NCDF_DEBUG
    1595             :         else
    1596             :         {
    1597             :             CPLDebug("GDAL_netCDF",
    1598             :                      "Setting NoDataValue to " CPL_FRMT_GIB
    1599             :                      " (id #%d, band #%d)",
    1600             :                      static_cast<GIntBig>(nNoData), cdfid, nBand);
    1601             :         }
    1602             : #endif
    1603             :         // Make sure we are in define mode.
    1604           3 :         cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1605             : 
    1606             :         int status;
    1607           6 :         if (eDataType == GDT_Int64 &&
    1608           3 :             cpl::down_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    1609             :         {
    1610           3 :             long long tmp = static_cast<long long>(nNoData);
    1611           3 :             status = nc_put_att_longlong(cdfid, nZId, NCDF_FillValue,
    1612             :                                          nc_datatype, 1, &tmp);
    1613             :         }
    1614             :         else
    1615             :         {
    1616           0 :             double dfNoData = static_cast<double>(nNoData);
    1617           0 :             status = nc_put_att_double(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1618             :                                        1, &dfNoData);
    1619             :         }
    1620             : 
    1621           3 :         NCDF_ERR(status);
    1622             : 
    1623             :         // Update status if write worked.
    1624           3 :         if (status == NC_NOERR)
    1625             :         {
    1626           3 :             SetNoDataValueNoUpdate(nNoData);
    1627           3 :             return CE_None;
    1628             :         }
    1629             : 
    1630           0 :         return CE_Failure;
    1631             :     }
    1632             : 
    1633           0 :     SetNoDataValueNoUpdate(nNoData);
    1634           0 :     return CE_None;
    1635             : }
    1636             : 
    1637             : /************************************************************************/
    1638             : /*                       SetNoDataValueNoUpdate()                       */
    1639             : /************************************************************************/
    1640             : 
    1641          11 : void netCDFRasterBand::SetNoDataValueNoUpdate(int64_t nNoData)
    1642             : {
    1643          11 :     m_nNodataValueInt64 = nNoData;
    1644          11 :     m_bNoDataSet = false;
    1645          11 :     m_bNoDataSetAsInt64 = true;
    1646          11 :     m_bNoDataSetAsUInt64 = false;
    1647          11 : }
    1648             : 
    1649             : /************************************************************************/
    1650             : /*                       SetNoDataValueAsUInt64()                       */
    1651             : /************************************************************************/
    1652             : 
    1653           3 : CPLErr netCDFRasterBand::SetNoDataValueAsUInt64(uint64_t nNoData)
    1654             : 
    1655             : {
    1656           6 :     CPLMutexHolderD(&hNCMutex);
    1657             : 
    1658             :     // If already set to new value, don't do anything.
    1659           3 :     if (m_bNoDataSetAsUInt64 && nNoData == m_nNodataValueUInt64)
    1660           0 :         return CE_None;
    1661             : 
    1662             :     // Write value if in update mode.
    1663           3 :     if (poDS->GetAccess() == GA_Update)
    1664             :     {
    1665             :         // netcdf-4 does not allow to set _FillValue after leaving define mode,
    1666             :         // but it is ok if variable has not been written to, so only print
    1667             :         // debug. See bug #4484.
    1668           3 :         if (m_bNoDataSetAsUInt64 &&
    1669           0 :             !cpl::down_cast<netCDFDataset *>(poDS)->GetDefineMode())
    1670             :         {
    1671           0 :             CPLDebug("GDAL_netCDF",
    1672             :                      "Setting NoDataValue to " CPL_FRMT_GUIB
    1673             :                      " (previously set to " CPL_FRMT_GUIB ") "
    1674             :                      "but file is no longer in define mode (id #%d, band #%d)",
    1675             :                      static_cast<GUIntBig>(nNoData),
    1676           0 :                      static_cast<GUIntBig>(m_nNodataValueUInt64), cdfid, nBand);
    1677             :         }
    1678             : #ifdef NCDF_DEBUG
    1679             :         else
    1680             :         {
    1681             :             CPLDebug("GDAL_netCDF",
    1682             :                      "Setting NoDataValue to " CPL_FRMT_GUIB
    1683             :                      " (id #%d, band #%d)",
    1684             :                      static_cast<GUIntBig>(nNoData), cdfid, nBand);
    1685             :         }
    1686             : #endif
    1687             :         // Make sure we are in define mode.
    1688           3 :         cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1689             : 
    1690             :         int status;
    1691           6 :         if (eDataType == GDT_UInt64 &&
    1692           3 :             cpl::down_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    1693             :         {
    1694           3 :             unsigned long long tmp = static_cast<long long>(nNoData);
    1695           3 :             status = nc_put_att_ulonglong(cdfid, nZId, NCDF_FillValue,
    1696             :                                           nc_datatype, 1, &tmp);
    1697             :         }
    1698             :         else
    1699             :         {
    1700           0 :             double dfNoData = static_cast<double>(nNoData);
    1701           0 :             status = nc_put_att_double(cdfid, nZId, NCDF_FillValue, nc_datatype,
    1702             :                                        1, &dfNoData);
    1703             :         }
    1704             : 
    1705           3 :         NCDF_ERR(status);
    1706             : 
    1707             :         // Update status if write worked.
    1708           3 :         if (status == NC_NOERR)
    1709             :         {
    1710           3 :             SetNoDataValueNoUpdate(nNoData);
    1711           3 :             return CE_None;
    1712             :         }
    1713             : 
    1714           0 :         return CE_Failure;
    1715             :     }
    1716             : 
    1717           0 :     SetNoDataValueNoUpdate(nNoData);
    1718           0 :     return CE_None;
    1719             : }
    1720             : 
    1721             : /************************************************************************/
    1722             : /*                       SetNoDataValueNoUpdate()                       */
    1723             : /************************************************************************/
    1724             : 
    1725          10 : void netCDFRasterBand::SetNoDataValueNoUpdate(uint64_t nNoData)
    1726             : {
    1727          10 :     m_nNodataValueUInt64 = nNoData;
    1728          10 :     m_bNoDataSet = false;
    1729          10 :     m_bNoDataSetAsInt64 = false;
    1730          10 :     m_bNoDataSetAsUInt64 = true;
    1731          10 : }
    1732             : 
    1733             : /************************************************************************/
    1734             : /*                         DeleteNoDataValue()                          */
    1735             : /************************************************************************/
    1736             : 
    1737             : #ifdef notdef
    1738             : CPLErr netCDFRasterBand::DeleteNoDataValue()
    1739             : 
    1740             : {
    1741             :     CPLMutexHolderD(&hNCMutex);
    1742             : 
    1743             :     if (!bNoDataSet)
    1744             :         return CE_None;
    1745             : 
    1746             :     // Write value if in update mode.
    1747             :     if (poDS->GetAccess() == GA_Update)
    1748             :     {
    1749             :         // Make sure we are in define mode.
    1750             :         static_cast<netCDFDataset *>(poDS)->SetDefineMode(true);
    1751             : 
    1752             :         status = nc_del_att(cdfid, nZId, NCDF_FillValue);
    1753             : 
    1754             :         NCDF_ERR(status);
    1755             : 
    1756             :         // Update status if write worked.
    1757             :         if (status == NC_NOERR)
    1758             :         {
    1759             :             dfNoDataValue = 0.0;
    1760             :             bNoDataSet = false;
    1761             :             return CE_None;
    1762             :         }
    1763             : 
    1764             :         return CE_Failure;
    1765             :     }
    1766             : 
    1767             :     dfNoDataValue = 0.0;
    1768             :     bNoDataSet = false;
    1769             :     return CE_None;
    1770             : }
    1771             : #endif
    1772             : 
    1773             : /************************************************************************/
    1774             : /*                           SerializeToXML()                           */
    1775             : /************************************************************************/
    1776             : 
    1777           5 : CPLXMLNode *netCDFRasterBand::SerializeToXML(const char * /* pszUnused */)
    1778             : {
    1779             :     // Overridden from GDALPamDataset to add only band histogram
    1780             :     // and statistics. See bug #4244.
    1781           5 :     if (psPam == nullptr)
    1782           0 :         return nullptr;
    1783             : 
    1784             :     // Setup root node and attributes.
    1785             :     CPLXMLNode *psTree =
    1786           5 :         CPLCreateXMLNode(nullptr, CXT_Element, "PAMRasterBand");
    1787             : 
    1788           5 :     if (GetBand() > 0)
    1789             :     {
    1790          10 :         CPLString oFmt;
    1791           5 :         CPLSetXMLValue(psTree, "#band", oFmt.Printf("%d", GetBand()));
    1792             :     }
    1793             : 
    1794             :     // Histograms.
    1795           5 :     if (psPam->psSavedHistograms != nullptr)
    1796           1 :         CPLAddXMLChild(psTree, CPLCloneXMLTree(psPam->psSavedHistograms));
    1797             : 
    1798             :     // Metadata (statistics only).
    1799           5 :     GDALMultiDomainMetadata oMDMDStats;
    1800           5 :     const char *papszMDStats[] = {"STATISTICS_MINIMUM", "STATISTICS_MAXIMUM",
    1801             :                                   "STATISTICS_MEAN", "STATISTICS_STDDEV",
    1802             :                                   nullptr};
    1803          25 :     for (int i = 0; i < CSLCount(papszMDStats); i++)
    1804             :     {
    1805          20 :         const char *pszMDI = GetMetadataItem(papszMDStats[i]);
    1806          20 :         if (pszMDI)
    1807           4 :             oMDMDStats.SetMetadataItem(papszMDStats[i], pszMDI);
    1808             :     }
    1809           5 :     CPLXMLNode *psMD = oMDMDStats.Serialize();
    1810             : 
    1811           5 :     if (psMD != nullptr)
    1812             :     {
    1813           1 :         if (psMD->psChild == nullptr)
    1814           0 :             CPLDestroyXMLNode(psMD);
    1815             :         else
    1816           1 :             CPLAddXMLChild(psTree, psMD);
    1817             :     }
    1818             : 
    1819             :     // We don't want to return anything if we had no metadata to attach.
    1820           5 :     if (psTree->psChild == nullptr || psTree->psChild->psNext == nullptr)
    1821             :     {
    1822           3 :         CPLDestroyXMLNode(psTree);
    1823           3 :         psTree = nullptr;
    1824             :     }
    1825             : 
    1826           5 :     return psTree;
    1827             : }
    1828             : 
    1829             : /************************************************************************/
    1830             : /*                  Get1DVariableIndexedByDimension()                   */
    1831             : /************************************************************************/
    1832             : 
    1833          85 : static int Get1DVariableIndexedByDimension(int cdfid, int nDimId,
    1834             :                                            const char *pszDimName,
    1835             :                                            bool bVerboseError, int *pnGroupID)
    1836             : {
    1837          85 :     *pnGroupID = -1;
    1838          85 :     int nVarID = -1;
    1839             :     // First try to find a variable whose name is identical to the dimension
    1840             :     // name, and check that it is indeed indexed by this dimension
    1841          85 :     if (NCDFResolveVar(cdfid, pszDimName, pnGroupID, &nVarID) == CE_None)
    1842             :     {
    1843          71 :         int nDimCountOfVariable = 0;
    1844          71 :         nc_inq_varndims(*pnGroupID, nVarID, &nDimCountOfVariable);
    1845          71 :         if (nDimCountOfVariable == 1)
    1846             :         {
    1847          71 :             int nDimIdOfVariable = -1;
    1848          71 :             nc_inq_vardimid(*pnGroupID, nVarID, &nDimIdOfVariable);
    1849          71 :             if (nDimIdOfVariable == nDimId)
    1850             :             {
    1851          71 :                 return nVarID;
    1852             :             }
    1853             :         }
    1854             :     }
    1855             : 
    1856             :     // Otherwise iterate over the variables to find potential candidates
    1857             :     // TODO: should be modified to search also in other groups using the same
    1858             :     //       logic than in NCDFResolveVar(), but maybe not needed if it's a
    1859             :     //       very rare case? and I think this is not CF compliant.
    1860          14 :     int nvars = 0;
    1861          14 :     CPL_IGNORE_RET_VAL(nc_inq(cdfid, nullptr, &nvars, nullptr, nullptr));
    1862             : 
    1863          14 :     int nCountCandidateVars = 0;
    1864          14 :     int nCandidateVarID = -1;
    1865          65 :     for (int k = 0; k < nvars; k++)
    1866             :     {
    1867          51 :         int nDimCountOfVariable = 0;
    1868          51 :         nc_inq_varndims(cdfid, k, &nDimCountOfVariable);
    1869          51 :         if (nDimCountOfVariable == 1)
    1870             :         {
    1871          27 :             int nDimIdOfVariable = -1;
    1872          27 :             nc_inq_vardimid(cdfid, k, &nDimIdOfVariable);
    1873          27 :             if (nDimIdOfVariable == nDimId)
    1874             :             {
    1875           7 :                 nCountCandidateVars++;
    1876           7 :                 nCandidateVarID = k;
    1877             :             }
    1878             :         }
    1879             :     }
    1880          14 :     if (nCountCandidateVars > 1)
    1881             :     {
    1882           1 :         if (bVerboseError)
    1883             :         {
    1884           1 :             CPLError(CE_Warning, CPLE_AppDefined,
    1885             :                      "Several 1D variables are indexed by dimension %s",
    1886             :                      pszDimName);
    1887             :         }
    1888           1 :         *pnGroupID = -1;
    1889           1 :         return -1;
    1890             :     }
    1891          13 :     else if (nCandidateVarID < 0)
    1892             :     {
    1893           8 :         if (bVerboseError)
    1894             :         {
    1895           8 :             CPLError(CE_Warning, CPLE_AppDefined,
    1896             :                      "No 1D variable is indexed by dimension %s", pszDimName);
    1897             :         }
    1898             :     }
    1899          13 :     *pnGroupID = cdfid;
    1900          13 :     return nCandidateVarID;
    1901             : }
    1902             : 
    1903             : /************************************************************************/
    1904             : /*                    CreateMetadataFromAttributes()                    */
    1905             : /************************************************************************/
    1906             : 
    1907         507 : void netCDFRasterBand::CreateMetadataFromAttributes()
    1908             : {
    1909         507 :     char szVarName[NC_MAX_NAME + 1] = {};
    1910         507 :     int status = nc_inq_varname(cdfid, nZId, szVarName);
    1911         507 :     NCDF_ERR(status);
    1912             : 
    1913         507 :     GDALPamRasterBand::SetMetadataItem("NETCDF_VARNAME", szVarName);
    1914             : 
    1915             :     // Get attribute metadata.
    1916         507 :     int nAtt = 0;
    1917         507 :     NCDF_ERR(nc_inq_varnatts(cdfid, nZId, &nAtt));
    1918             : 
    1919        2244 :     for (int i = 0; i < nAtt; i++)
    1920             :     {
    1921        1737 :         char szMetaName[NC_MAX_NAME + 1] = {};
    1922        1737 :         status = nc_inq_attname(cdfid, nZId, i, szMetaName);
    1923        1737 :         if (status != NC_NOERR)
    1924          12 :             continue;
    1925             : 
    1926        1737 :         if (GDALPamRasterBand::GetMetadataItem(szMetaName) != nullptr)
    1927             :         {
    1928          12 :             continue;
    1929             :         }
    1930             : 
    1931        1725 :         char *pszMetaValue = nullptr;
    1932        1725 :         if (NCDFGetAttr(cdfid, nZId, szMetaName, &pszMetaValue) == CE_None)
    1933             :         {
    1934        1725 :             GDALPamRasterBand::SetMetadataItem(szMetaName, pszMetaValue);
    1935             :         }
    1936             :         else
    1937             :         {
    1938           0 :             CPLDebug("GDAL_netCDF", "invalid Band metadata %s", szMetaName);
    1939             :         }
    1940             : 
    1941        1725 :         if (pszMetaValue)
    1942             :         {
    1943        1725 :             CPLFree(pszMetaValue);
    1944        1725 :             pszMetaValue = nullptr;
    1945             :         }
    1946             :     }
    1947         507 : }
    1948             : 
    1949             : /************************************************************************/
    1950             : /*                    CreateMetadataFromOtherVars()                     */
    1951             : /************************************************************************/
    1952             : 
    1953          59 : void netCDFRasterBand::CreateMetadataFromOtherVars()
    1954             : 
    1955             : {
    1956          59 :     CPLAssert(!m_bCreateMetadataFromOtherVarsDone);
    1957          59 :     m_bCreateMetadataFromOtherVarsDone = true;
    1958             : 
    1959          59 :     netCDFDataset *l_poDS = cpl::down_cast<netCDFDataset *>(poDS);
    1960          59 :     const int nPamFlagsBackup = l_poDS->nPamFlags;
    1961             : 
    1962             :     // Compute all dimensions from Band number and save in Metadata.
    1963          59 :     int nd = 0;
    1964          59 :     nc_inq_varndims(cdfid, nZId, &nd);
    1965             :     // Compute multidimention band position.
    1966             :     //
    1967             :     // BandPosition = (Total - sum(PastBandLevels) - 1)/sum(remainingLevels)
    1968             :     // if Data[2,3,4,x,y]
    1969             :     //
    1970             :     //  BandPos0 = (nBand) / (3*4)
    1971             :     //  BandPos1 = (nBand - BandPos0*(3*4)) / (4)
    1972             :     //  BandPos2 = (nBand - BandPos0*(3*4)) % (4)
    1973             : 
    1974          59 :     int Sum = 1;
    1975          59 :     if (nd == 3)
    1976             :     {
    1977           6 :         Sum *= panBandZLev[0];
    1978             :     }
    1979             : 
    1980             :     // Loop over non-spatial dimensions.
    1981          59 :     int Taken = 0;
    1982             : 
    1983         100 :     for (int i = 0; i < nd - 2; i++)
    1984             :     {
    1985             :         int result;
    1986          41 :         if (i != nd - 2 - 1)
    1987             :         {
    1988          18 :             Sum = 1;
    1989          37 :             for (int j = i + 1; j < nd - 2; j++)
    1990             :             {
    1991          19 :                 Sum *= panBandZLev[j];
    1992             :             }
    1993          18 :             result = static_cast<int>((nLevel - Taken) / Sum);
    1994             :         }
    1995             :         else
    1996             :         {
    1997          23 :             result = static_cast<int>((nLevel - Taken) % Sum);
    1998             :         }
    1999             : 
    2000          41 :         char szName[NC_MAX_NAME + 1] = {};
    2001          41 :         snprintf(szName, sizeof(szName), "%s",
    2002          41 :                  l_poDS->papszDimName[l_poDS->m_anDimIds[panBandZPos[i]]]);
    2003             : 
    2004             :         char szMetaName[NC_MAX_NAME + 1 + 32];
    2005          41 :         snprintf(szMetaName, sizeof(szMetaName), "NETCDF_DIM_%s", szName);
    2006             : 
    2007          41 :         const int nGroupID = l_poDS->m_anExtraDimGroupIds[i];
    2008          41 :         const int nVarID = l_poDS->m_anExtraDimVarIds[i];
    2009          41 :         if (nVarID < 0)
    2010             :         {
    2011           2 :             GDALPamRasterBand::SetMetadataItem(szMetaName,
    2012             :                                                CPLSPrintf("%d", result + 1));
    2013             :         }
    2014             :         else
    2015             :         {
    2016             :             // TODO: Make sure all the status checks make sense.
    2017             : 
    2018          39 :             nc_type nVarType = NC_NAT;
    2019          39 :             /* status = */ nc_inq_vartype(nGroupID, nVarID, &nVarType);
    2020             : 
    2021          39 :             int nDims = 0;
    2022          39 :             /* status = */ nc_inq_varndims(nGroupID, nVarID, &nDims);
    2023             : 
    2024          39 :             char szMetaTemp[256] = {};
    2025          39 :             if (nDims == 1)
    2026             :             {
    2027          39 :                 size_t count[1] = {1};
    2028          39 :                 size_t start[1] = {static_cast<size_t>(result)};
    2029             : 
    2030          39 :                 switch (nVarType)
    2031             :                 {
    2032           0 :                     case NC_BYTE:
    2033             :                         // TODO: Check for signed/unsigned byte.
    2034             :                         signed char cData;
    2035           0 :                         /* status = */ nc_get_vara_schar(nGroupID, nVarID,
    2036             :                                                          start, count, &cData);
    2037           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", cData);
    2038           0 :                         break;
    2039           0 :                     case NC_SHORT:
    2040             :                         short sData;
    2041           0 :                         /* status = */ nc_get_vara_short(nGroupID, nVarID,
    2042             :                                                          start, count, &sData);
    2043           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", sData);
    2044           0 :                         break;
    2045          19 :                     case NC_INT:
    2046             :                     {
    2047             :                         int nData;
    2048          19 :                         /* status = */ nc_get_vara_int(nGroupID, nVarID, start,
    2049             :                                                        count, &nData);
    2050          19 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", nData);
    2051          19 :                         break;
    2052             :                     }
    2053           0 :                     case NC_FLOAT:
    2054             :                         float fData;
    2055           0 :                         /* status = */ nc_get_vara_float(nGroupID, nVarID,
    2056             :                                                          start, count, &fData);
    2057           0 :                         CPLsnprintf(szMetaTemp, sizeof(szMetaTemp), "%.8g",
    2058             :                                     fData);
    2059           0 :                         break;
    2060          18 :                     case NC_DOUBLE:
    2061             :                         double dfData;
    2062          18 :                         /* status = */ nc_get_vara_double(
    2063             :                             nGroupID, nVarID, start, count, &dfData);
    2064          18 :                         CPLsnprintf(szMetaTemp, sizeof(szMetaTemp), "%.16g",
    2065             :                                     dfData);
    2066          18 :                         break;
    2067           0 :                     case NC_UBYTE:
    2068             :                         unsigned char ucData;
    2069           0 :                         /* status = */ nc_get_vara_uchar(nGroupID, nVarID,
    2070             :                                                          start, count, &ucData);
    2071           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%u", ucData);
    2072           0 :                         break;
    2073           0 :                     case NC_USHORT:
    2074             :                         unsigned short usData;
    2075           0 :                         /* status = */ nc_get_vara_ushort(
    2076             :                             nGroupID, nVarID, start, count, &usData);
    2077           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%u", usData);
    2078           0 :                         break;
    2079           0 :                     case NC_UINT:
    2080             :                     {
    2081             :                         unsigned int unData;
    2082           0 :                         /* status = */ nc_get_vara_uint(nGroupID, nVarID, start,
    2083             :                                                         count, &unData);
    2084           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), "%u", unData);
    2085           0 :                         break;
    2086             :                     }
    2087           2 :                     case NC_INT64:
    2088             :                     {
    2089             :                         long long nData;
    2090           2 :                         /* status = */ nc_get_vara_longlong(
    2091             :                             nGroupID, nVarID, start, count, &nData);
    2092           2 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), CPL_FRMT_GIB,
    2093             :                                  nData);
    2094           2 :                         break;
    2095             :                     }
    2096           0 :                     case NC_UINT64:
    2097             :                     {
    2098             :                         unsigned long long unData;
    2099           0 :                         /* status = */ nc_get_vara_ulonglong(
    2100             :                             nGroupID, nVarID, start, count, &unData);
    2101           0 :                         snprintf(szMetaTemp, sizeof(szMetaTemp), CPL_FRMT_GUIB,
    2102             :                                  unData);
    2103           0 :                         break;
    2104             :                     }
    2105           0 :                     default:
    2106           0 :                         CPLDebug("GDAL_netCDF", "invalid dim %s, type=%d",
    2107             :                                  szMetaTemp, nVarType);
    2108           0 :                         break;
    2109             :                 }
    2110             :             }
    2111             :             else
    2112             :             {
    2113           0 :                 snprintf(szMetaTemp, sizeof(szMetaTemp), "%d", result + 1);
    2114             :             }
    2115             : 
    2116             :             // Save dimension value.
    2117             :             // NOTE: removed #original_units as not part of CF-1.
    2118             : 
    2119          39 :             GDALPamRasterBand::SetMetadataItem(szMetaName, szMetaTemp);
    2120             :         }
    2121             : 
    2122             :         // Avoid int32 overflow. Perhaps something more sensible to do here ?
    2123          41 :         if (result > 0 && Sum > INT_MAX / result)
    2124           0 :             break;
    2125          41 :         if (Taken > INT_MAX - result * Sum)
    2126           0 :             break;
    2127             : 
    2128          41 :         Taken += result * Sum;
    2129             :     }  // End loop non-spatial dimensions.
    2130             : 
    2131          59 :     l_poDS->nPamFlags = nPamFlagsBackup;
    2132          59 : }
    2133             : 
    2134             : /************************************************************************/
    2135             : /*                             CheckData()                              */
    2136             : /************************************************************************/
    2137             : template <class T>
    2138        6054 : void netCDFRasterBand::CheckData(void *pImage, void *pImageNC,
    2139             :                                  size_t nTmpBlockXSize, size_t nTmpBlockYSize,
    2140             :                                  bool bCheckIsNan)
    2141             : {
    2142        6054 :     CPLAssert(pImage != nullptr && pImageNC != nullptr);
    2143             : 
    2144             :     // If this block is not a full block (in the x axis), we need to re-arrange
    2145             :     // the data this is because partial blocks are not arranged the same way in
    2146             :     // netcdf and gdal.
    2147        6054 :     if (nTmpBlockXSize != static_cast<size_t>(nBlockXSize))
    2148             :     {
    2149           6 :         T *ptrWrite = static_cast<T *>(pImage);
    2150           6 :         T *ptrRead = static_cast<T *>(pImageNC);
    2151          29 :         for (size_t j = 0; j < nTmpBlockYSize;
    2152          23 :              j++, ptrWrite += nBlockXSize, ptrRead += nTmpBlockXSize)
    2153             :         {
    2154          23 :             memmove(ptrWrite, ptrRead, nTmpBlockXSize * sizeof(T));
    2155             :         }
    2156             :     }
    2157             : 
    2158             :     // Is valid data checking needed or requested?
    2159        6054 :     if (bValidRangeValid || bCheckIsNan)
    2160             :     {
    2161        1365 :         T *ptrImage = static_cast<T *>(pImage);
    2162        2784 :         for (size_t j = 0; j < nTmpBlockYSize; j++)
    2163             :         {
    2164             :             // k moves along the gdal block, skipping the out-of-range pixels.
    2165        1419 :             size_t k = j * nBlockXSize;
    2166       99038 :             for (size_t i = 0; i < nTmpBlockXSize; i++, k++)
    2167             :             {
    2168             :                 // Check for nodata and nan.
    2169       97619 :                 if (CPLIsEqual((double)ptrImage[k], m_dfNoDataValue))
    2170        6301 :                     continue;
    2171       91318 :                 if (bCheckIsNan && std::isnan((double)ptrImage[k]))
    2172             :                 {
    2173        5737 :                     ptrImage[k] = (T)m_dfNoDataValue;
    2174        5737 :                     continue;
    2175             :                 }
    2176             :                 // Check for valid_range.
    2177       85581 :                 if (bValidRangeValid)
    2178             :                 {
    2179       40986 :                     if (((adfValidRange[0] != m_dfNoDataValue) &&
    2180       40986 :                          (ptrImage[k] < (T)adfValidRange[0])) ||
    2181       40983 :                         ((adfValidRange[1] != m_dfNoDataValue) &&
    2182       40983 :                          (ptrImage[k] > (T)adfValidRange[1])))
    2183             :                     {
    2184           4 :                         ptrImage[k] = (T)m_dfNoDataValue;
    2185             :                     }
    2186             :                 }
    2187             :             }
    2188             :         }
    2189             :     }
    2190             : 
    2191             :     // If minimum longitude is > 180, subtract 360 from all.
    2192             :     // If not, disable checking for further calls (check just once).
    2193             :     // Only check first and last block elements since lon must be monotonic.
    2194        6054 :     const bool bIsSigned = std::numeric_limits<T>::is_signed;
    2195        5665 :     if (bCheckLongitude && bIsSigned &&
    2196          11 :         !CPLIsEqual((double)((T *)pImage)[0], m_dfNoDataValue) &&
    2197          10 :         !CPLIsEqual((double)((T *)pImage)[nTmpBlockXSize - 1],
    2198        2838 :                     m_dfNoDataValue) &&
    2199          10 :         std::min(((T *)pImage)[0], ((T *)pImage)[nTmpBlockXSize - 1]) > 180.0)
    2200             :     {
    2201           0 :         T *ptrImage = static_cast<T *>(pImage);
    2202           0 :         for (size_t j = 0; j < nTmpBlockYSize; j++)
    2203             :         {
    2204           0 :             size_t k = j * nBlockXSize;
    2205           0 :             for (size_t i = 0; i < nTmpBlockXSize; i++, k++)
    2206             :             {
    2207           0 :                 if (!CPLIsEqual((double)ptrImage[k], m_dfNoDataValue))
    2208           0 :                     ptrImage[k] = static_cast<T>(ptrImage[k] - 360);
    2209             :             }
    2210             :         }
    2211             :     }
    2212             :     else
    2213             :     {
    2214        6054 :         bCheckLongitude = false;
    2215             :     }
    2216        6054 : }
    2217             : 
    2218             : /************************************************************************/
    2219             : /*                            CheckDataCpx()                            */
    2220             : /************************************************************************/
    2221             : template <class T>
    2222          25 : void netCDFRasterBand::CheckDataCpx(void *pImage, void *pImageNC,
    2223             :                                     size_t nTmpBlockXSize,
    2224             :                                     size_t nTmpBlockYSize, bool bCheckIsNan)
    2225             : {
    2226          25 :     CPLAssert(pImage != nullptr && pImageNC != nullptr);
    2227             : 
    2228             :     // If this block is not a full block (in the x axis), we need to re-arrange
    2229             :     // the data this is because partial blocks are not arranged the same way in
    2230             :     // netcdf and gdal.
    2231          25 :     if (nTmpBlockXSize != static_cast<size_t>(nBlockXSize))
    2232             :     {
    2233           0 :         T *ptrWrite = static_cast<T *>(pImage);
    2234           0 :         T *ptrRead = static_cast<T *>(pImageNC);
    2235           0 :         for (size_t j = 0; j < nTmpBlockYSize; j++,
    2236           0 :                     ptrWrite += (2 * nBlockXSize),
    2237           0 :                     ptrRead += (2 * nTmpBlockXSize))
    2238             :         {
    2239           0 :             memmove(ptrWrite, ptrRead, nTmpBlockXSize * sizeof(T) * 2);
    2240             :         }
    2241             :     }
    2242             : 
    2243             :     // Is valid data checking needed or requested?
    2244          25 :     if (bValidRangeValid || bCheckIsNan)
    2245             :     {
    2246           0 :         T *ptrImage = static_cast<T *>(pImage);
    2247           0 :         for (size_t j = 0; j < nTmpBlockYSize; j++)
    2248             :         {
    2249             :             // k moves along the gdal block, skipping the out-of-range pixels.
    2250           0 :             size_t k = 2 * j * nBlockXSize;
    2251           0 :             for (size_t i = 0; i < (2 * nTmpBlockXSize); i++, k++)
    2252             :             {
    2253             :                 // Check for nodata and nan.
    2254           0 :                 if (CPLIsEqual((double)ptrImage[k], m_dfNoDataValue))
    2255           0 :                     continue;
    2256           0 :                 if (bCheckIsNan && std::isnan((double)ptrImage[k]))
    2257             :                 {
    2258           0 :                     ptrImage[k] = (T)m_dfNoDataValue;
    2259           0 :                     continue;
    2260             :                 }
    2261             :                 // Check for valid_range.
    2262           0 :                 if (bValidRangeValid)
    2263             :                 {
    2264           0 :                     if (((adfValidRange[0] != m_dfNoDataValue) &&
    2265           0 :                          (ptrImage[k] < (T)adfValidRange[0])) ||
    2266           0 :                         ((adfValidRange[1] != m_dfNoDataValue) &&
    2267           0 :                          (ptrImage[k] > (T)adfValidRange[1])))
    2268             :                     {
    2269           0 :                         ptrImage[k] = (T)m_dfNoDataValue;
    2270             :                     }
    2271             :                 }
    2272             :             }
    2273             :         }
    2274             :     }
    2275          25 : }
    2276             : 
    2277             : /************************************************************************/
    2278             : /*                          FetchNetcdfChunk()                          */
    2279             : /************************************************************************/
    2280             : 
    2281        6079 : bool netCDFRasterBand::FetchNetcdfChunk(size_t xstart, size_t ystart,
    2282             :                                         void *pImage)
    2283             : {
    2284        6079 :     size_t start[MAX_NC_DIMS] = {};
    2285        6079 :     size_t edge[MAX_NC_DIMS] = {};
    2286             : 
    2287        6079 :     start[nBandXPos] = xstart;
    2288        6079 :     edge[nBandXPos] = nBlockXSize;
    2289        6079 :     if ((start[nBandXPos] + edge[nBandXPos]) > (size_t)nRasterXSize)
    2290           6 :         edge[nBandXPos] = nRasterXSize - start[nBandXPos];
    2291        6079 :     if (nBandYPos >= 0)
    2292             :     {
    2293        6075 :         start[nBandYPos] = ystart;
    2294        6075 :         edge[nBandYPos] = nBlockYSize;
    2295        6075 :         if ((start[nBandYPos] + edge[nBandYPos]) > (size_t)nRasterYSize)
    2296           4 :             edge[nBandYPos] = nRasterYSize - start[nBandYPos];
    2297             :     }
    2298        6079 :     const size_t nYChunkSize = nBandYPos < 0 ? 1 : edge[nBandYPos];
    2299             : 
    2300             : #ifdef NCDF_DEBUG
    2301             :     CPLDebug("GDAL_netCDF", "start={%ld,%ld} edge={%ld,%ld} bBottomUp=%d",
    2302             :              start[nBandXPos], nBandYPos < 0 ? 0 : start[nBandYPos],
    2303             :              edge[nBandXPos], nYChunkSize, ((netCDFDataset *)poDS)->bBottomUp);
    2304             : #endif
    2305             : 
    2306        6079 :     int nd = 0;
    2307        6079 :     nc_inq_varndims(cdfid, nZId, &nd);
    2308        6079 :     if (nd == 3)
    2309             :     {
    2310        1120 :         start[panBandZPos[0]] = nLevel;  // z
    2311        1120 :         edge[panBandZPos[0]] = 1;
    2312             :     }
    2313             : 
    2314             :     // Compute multidimention band position.
    2315             :     //
    2316             :     // BandPosition = (Total - sum(PastBandLevels) - 1)/sum(remainingLevels)
    2317             :     // if Data[2,3,4,x,y]
    2318             :     //
    2319             :     //  BandPos0 = (nBand) / (3*4)
    2320             :     //  BandPos1 = (nBand - (3*4)) / (4)
    2321             :     //  BandPos2 = (nBand - (3*4)) % (4)
    2322        6079 :     if (nd > 3)
    2323             :     {
    2324         160 :         int Sum = -1;
    2325         160 :         int Taken = 0;
    2326         480 :         for (int i = 0; i < nd - 2; i++)
    2327             :         {
    2328         320 :             if (i != nd - 2 - 1)
    2329             :             {
    2330         160 :                 Sum = 1;
    2331         320 :                 for (int j = i + 1; j < nd - 2; j++)
    2332             :                 {
    2333         160 :                     Sum *= panBandZLev[j];
    2334             :                 }
    2335         160 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) / Sum);
    2336         160 :                 edge[panBandZPos[i]] = 1;
    2337             :             }
    2338             :             else
    2339             :             {
    2340         160 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) % Sum);
    2341         160 :                 edge[panBandZPos[i]] = 1;
    2342             :             }
    2343         320 :             Taken += static_cast<int>(start[panBandZPos[i]]) * Sum;
    2344             :         }
    2345             :     }
    2346             : 
    2347             :     // Make sure we are in data mode.
    2348        6079 :     cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(false);
    2349             : 
    2350             :     // If this block is not a full block in the x axis, we need to
    2351             :     // re-arrange the data because partial blocks are not arranged the
    2352             :     // same way in netcdf and gdal, so we first we read the netcdf data at
    2353             :     // the end of the gdal block buffer then re-arrange rows in CheckData().
    2354        6079 :     void *pImageNC = pImage;
    2355        6079 :     if (edge[nBandXPos] != static_cast<size_t>(nBlockXSize))
    2356             :     {
    2357           6 :         pImageNC = static_cast<GByte *>(pImage) +
    2358           6 :                    ((static_cast<size_t>(nBlockXSize) * nBlockYSize -
    2359          12 :                      edge[nBandXPos] * nYChunkSize) *
    2360           6 :                     GDALGetDataTypeSizeBytes(eDataType));
    2361             :     }
    2362             : 
    2363             :     // Read data according to type.
    2364             :     int status;
    2365        6079 :     if (eDataType == GDT_UInt8)
    2366             :     {
    2367        3205 :         if (bSignedData)
    2368             :         {
    2369           0 :             status = nc_get_vara_schar(cdfid, nZId, start, edge,
    2370             :                                        static_cast<signed char *>(pImageNC));
    2371           0 :             if (status == NC_NOERR)
    2372           0 :                 CheckData<signed char>(pImage, pImageNC, edge[nBandXPos],
    2373             :                                        nYChunkSize, false);
    2374             :         }
    2375             :         else
    2376             :         {
    2377        3205 :             status = nc_get_vara_uchar(cdfid, nZId, start, edge,
    2378             :                                        static_cast<unsigned char *>(pImageNC));
    2379        3205 :             if (status == NC_NOERR)
    2380        3205 :                 CheckData<unsigned char>(pImage, pImageNC, edge[nBandXPos],
    2381             :                                          nYChunkSize, false);
    2382             :         }
    2383             :     }
    2384        2874 :     else if (eDataType == GDT_Int8)
    2385             :     {
    2386          60 :         status = nc_get_vara_schar(cdfid, nZId, start, edge,
    2387             :                                    static_cast<signed char *>(pImageNC));
    2388          60 :         if (status == NC_NOERR)
    2389          60 :             CheckData<signed char>(pImage, pImageNC, edge[nBandXPos],
    2390             :                                    nYChunkSize, false);
    2391             :     }
    2392        2814 :     else if (nc_datatype == NC_SHORT)
    2393             :     {
    2394         487 :         status = nc_get_vara_short(cdfid, nZId, start, edge,
    2395             :                                    static_cast<short *>(pImageNC));
    2396         487 :         if (status == NC_NOERR)
    2397             :         {
    2398         487 :             if (eDataType == GDT_Int16)
    2399             :             {
    2400         484 :                 CheckData<GInt16>(pImage, pImageNC, edge[nBandXPos],
    2401             :                                   nYChunkSize, false);
    2402             :             }
    2403             :             else
    2404             :             {
    2405           3 :                 CheckData<GUInt16>(pImage, pImageNC, edge[nBandXPos],
    2406             :                                    nYChunkSize, false);
    2407             :             }
    2408             :         }
    2409             :     }
    2410        2327 :     else if (eDataType == GDT_Int32)
    2411             :     {
    2412             : #if SIZEOF_UNSIGNED_LONG == 4
    2413             :         status = nc_get_vara_long(cdfid, nZId, start, edge,
    2414             :                                   static_cast<long *>(pImageNC));
    2415             :         if (status == NC_NOERR)
    2416             :             CheckData<long>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2417             :                             false);
    2418             : #else
    2419         912 :         status = nc_get_vara_int(cdfid, nZId, start, edge,
    2420             :                                  static_cast<int *>(pImageNC));
    2421         912 :         if (status == NC_NOERR)
    2422         912 :             CheckData<int>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2423             :                            false);
    2424             : #endif
    2425             :     }
    2426        1415 :     else if (eDataType == GDT_Float32)
    2427             :     {
    2428        1278 :         status = nc_get_vara_float(cdfid, nZId, start, edge,
    2429             :                                    static_cast<float *>(pImageNC));
    2430        1278 :         if (status == NC_NOERR)
    2431        1278 :             CheckData<float>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2432             :                              true);
    2433             :     }
    2434         137 :     else if (eDataType == GDT_Float64)
    2435             :     {
    2436          86 :         status = nc_get_vara_double(cdfid, nZId, start, edge,
    2437             :                                     static_cast<double *>(pImageNC));
    2438          86 :         if (status == NC_NOERR)
    2439          86 :             CheckData<double>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2440             :                               true);
    2441             :     }
    2442          51 :     else if (eDataType == GDT_UInt16)
    2443             :     {
    2444           6 :         status = nc_get_vara_ushort(cdfid, nZId, start, edge,
    2445             :                                     static_cast<unsigned short *>(pImageNC));
    2446           6 :         if (status == NC_NOERR)
    2447           6 :             CheckData<unsigned short>(pImage, pImageNC, edge[nBandXPos],
    2448             :                                       nYChunkSize, false);
    2449             :     }
    2450          45 :     else if (eDataType == GDT_UInt32)
    2451             :     {
    2452           6 :         status = nc_get_vara_uint(cdfid, nZId, start, edge,
    2453             :                                   static_cast<unsigned int *>(pImageNC));
    2454           6 :         if (status == NC_NOERR)
    2455           6 :             CheckData<unsigned int>(pImage, pImageNC, edge[nBandXPos],
    2456             :                                     nYChunkSize, false);
    2457             :     }
    2458          39 :     else if (eDataType == GDT_Int64)
    2459             :     {
    2460           7 :         status = nc_get_vara_longlong(cdfid, nZId, start, edge,
    2461             :                                       static_cast<long long *>(pImageNC));
    2462           7 :         if (status == NC_NOERR)
    2463           7 :             CheckData<std::int64_t>(pImage, pImageNC, edge[nBandXPos],
    2464             :                                     nYChunkSize, false);
    2465             :     }
    2466          32 :     else if (eDataType == GDT_UInt64)
    2467             :     {
    2468             :         status =
    2469           7 :             nc_get_vara_ulonglong(cdfid, nZId, start, edge,
    2470             :                                   static_cast<unsigned long long *>(pImageNC));
    2471           7 :         if (status == NC_NOERR)
    2472           7 :             CheckData<std::uint64_t>(pImage, pImageNC, edge[nBandXPos],
    2473             :                                      nYChunkSize, false);
    2474             :     }
    2475          25 :     else if (eDataType == GDT_CInt16)
    2476             :     {
    2477           0 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2478           0 :         if (status == NC_NOERR)
    2479           0 :             CheckDataCpx<short>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2480             :                                 false);
    2481             :     }
    2482          25 :     else if (eDataType == GDT_CInt32)
    2483             :     {
    2484           0 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2485           0 :         if (status == NC_NOERR)
    2486           0 :             CheckDataCpx<int>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2487             :                               false);
    2488             :     }
    2489          25 :     else if (eDataType == GDT_CFloat32)
    2490             :     {
    2491          20 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2492          20 :         if (status == NC_NOERR)
    2493          20 :             CheckDataCpx<float>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2494             :                                 false);
    2495             :     }
    2496           5 :     else if (eDataType == GDT_CFloat64)
    2497             :     {
    2498           5 :         status = nc_get_vara(cdfid, nZId, start, edge, pImageNC);
    2499           5 :         if (status == NC_NOERR)
    2500           5 :             CheckDataCpx<double>(pImage, pImageNC, edge[nBandXPos], nYChunkSize,
    2501             :                                  false);
    2502             :     }
    2503             : 
    2504             :     else
    2505           0 :         status = NC_EBADTYPE;
    2506             : 
    2507        6079 :     if (status != NC_NOERR)
    2508             :     {
    2509           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    2510             :                  "netCDF chunk fetch failed: #%d (%s)", status,
    2511             :                  nc_strerror(status));
    2512           0 :         return false;
    2513             :     }
    2514        6079 :     return true;
    2515             : }
    2516             : 
    2517             : /************************************************************************/
    2518             : /*                             IReadBlock()                             */
    2519             : /************************************************************************/
    2520             : 
    2521        6079 : CPLErr netCDFRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
    2522             :                                     void *pImage)
    2523             : 
    2524             : {
    2525       12158 :     CPLMutexHolderD(&hNCMutex);
    2526             : 
    2527             :     // Locate X, Y and Z position in the array.
    2528             : 
    2529        6079 :     size_t xstart = static_cast<size_t>(nBlockXOff) * nBlockXSize;
    2530        6079 :     size_t ystart = 0;
    2531             : 
    2532             :     // Check y order.
    2533        6079 :     if (nBandYPos >= 0)
    2534             :     {
    2535        6075 :         auto poGDS = cpl::down_cast<netCDFDataset *>(poDS);
    2536        6075 :         if (poGDS->bBottomUp)
    2537             :         {
    2538        5118 :             if (nBlockYSize == 1)
    2539             :             {
    2540        5105 :                 ystart = nRasterYSize - 1 - nBlockYOff;
    2541             :             }
    2542             :             else
    2543             :             {
    2544             :                 // in GDAL space
    2545          13 :                 ystart = static_cast<size_t>(nBlockYOff) * nBlockYSize;
    2546             :                 const size_t yend =
    2547          26 :                     std::min(ystart + nBlockYSize - 1,
    2548          13 :                              static_cast<size_t>(nRasterYSize - 1));
    2549             :                 // in netCDF space
    2550          13 :                 const size_t nFirstChunkLine = nRasterYSize - 1 - yend;
    2551          13 :                 const size_t nLastChunkLine = nRasterYSize - 1 - ystart;
    2552          13 :                 const size_t nFirstChunkBlock = nFirstChunkLine / nBlockYSize;
    2553          13 :                 const size_t nLastChunkBlock = nLastChunkLine / nBlockYSize;
    2554             : 
    2555             :                 const auto firstKey = netCDFDataset::ChunkKey(
    2556          13 :                     nBlockXOff, nFirstChunkBlock, nBand);
    2557             :                 const auto secondKey =
    2558          13 :                     netCDFDataset::ChunkKey(nBlockXOff, nLastChunkBlock, nBand);
    2559             : 
    2560             :                 // Retrieve data from the one or 2 needed netCDF chunks
    2561          13 :                 std::shared_ptr<std::vector<GByte>> firstChunk;
    2562          13 :                 std::shared_ptr<std::vector<GByte>> secondChunk;
    2563          13 :                 if (poGDS->poChunkCache)
    2564             :                 {
    2565          13 :                     poGDS->poChunkCache->tryGet(firstKey, firstChunk);
    2566          13 :                     if (firstKey != secondKey)
    2567           6 :                         poGDS->poChunkCache->tryGet(secondKey, secondChunk);
    2568             :                 }
    2569             :                 const size_t nChunkLineSize =
    2570          13 :                     static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
    2571          13 :                     nBlockXSize;
    2572          13 :                 const size_t nChunkSize = nChunkLineSize * nBlockYSize;
    2573          13 :                 if (!firstChunk)
    2574             :                 {
    2575             :                     firstChunk =
    2576          11 :                         std::make_shared<std::vector<GByte>>(nChunkSize);
    2577          11 :                     if (!FetchNetcdfChunk(xstart,
    2578          11 :                                           nFirstChunkBlock * nBlockYSize,
    2579          11 :                                           firstChunk.get()->data()))
    2580           0 :                         return CE_Failure;
    2581          11 :                     if (poGDS->poChunkCache)
    2582          11 :                         poGDS->poChunkCache->insert(firstKey, firstChunk);
    2583             :                 }
    2584          13 :                 if (!secondChunk && firstKey != secondKey)
    2585             :                 {
    2586             :                     secondChunk =
    2587           2 :                         std::make_shared<std::vector<GByte>>(nChunkSize);
    2588           2 :                     if (!FetchNetcdfChunk(xstart, nLastChunkBlock * nBlockYSize,
    2589           2 :                                           secondChunk.get()->data()))
    2590           0 :                         return CE_Failure;
    2591           2 :                     if (poGDS->poChunkCache)
    2592           2 :                         poGDS->poChunkCache->insert(secondKey, secondChunk);
    2593             :                 }
    2594             : 
    2595             :                 // Assemble netCDF chunks into GDAL block
    2596          13 :                 GByte *pabyImage = static_cast<GByte *>(pImage);
    2597          13 :                 const size_t nFirstChunkBlockLine =
    2598          13 :                     nFirstChunkBlock * nBlockYSize;
    2599          13 :                 const size_t nLastChunkBlockLine =
    2600          13 :                     nLastChunkBlock * nBlockYSize;
    2601         146 :                 for (size_t iLine = ystart; iLine <= yend; iLine++)
    2602             :                 {
    2603         133 :                     const size_t nLineFromBottom = nRasterYSize - 1 - iLine;
    2604         133 :                     const size_t nChunkY = nLineFromBottom / nBlockYSize;
    2605         133 :                     if (nChunkY == nFirstChunkBlock)
    2606             :                     {
    2607         121 :                         memcpy(pabyImage + nChunkLineSize * (iLine - ystart),
    2608         121 :                                firstChunk.get()->data() +
    2609         121 :                                    (nLineFromBottom - nFirstChunkBlockLine) *
    2610             :                                        nChunkLineSize,
    2611             :                                nChunkLineSize);
    2612             :                     }
    2613             :                     else
    2614             :                     {
    2615          12 :                         CPLAssert(nChunkY == nLastChunkBlock);
    2616          12 :                         assert(secondChunk);
    2617          12 :                         memcpy(pabyImage + nChunkLineSize * (iLine - ystart),
    2618          12 :                                secondChunk.get()->data() +
    2619          12 :                                    (nLineFromBottom - nLastChunkBlockLine) *
    2620             :                                        nChunkLineSize,
    2621             :                                nChunkLineSize);
    2622             :                     }
    2623             :                 }
    2624          13 :                 return CE_None;
    2625             :             }
    2626             :         }
    2627             :         else
    2628             :         {
    2629         957 :             ystart = static_cast<size_t>(nBlockYOff) * nBlockYSize;
    2630             :         }
    2631             :     }
    2632             : 
    2633        6066 :     return FetchNetcdfChunk(xstart, ystart, pImage) ? CE_None : CE_Failure;
    2634             : }
    2635             : 
    2636             : /************************************************************************/
    2637             : /*                            IWriteBlock()                             */
    2638             : /************************************************************************/
    2639             : 
    2640        6621 : CPLErr netCDFRasterBand::IWriteBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
    2641             :                                      void *pImage)
    2642             : {
    2643       13242 :     CPLMutexHolderD(&hNCMutex);
    2644             : 
    2645             : #ifdef NCDF_DEBUG
    2646             :     if (nBlockYOff == 0 || (nBlockYOff == nRasterYSize - 1))
    2647             :         CPLDebug("GDAL_netCDF",
    2648             :                  "netCDFRasterBand::IWriteBlock( %d, %d, ...) nBand=%d",
    2649             :                  nBlockXOff, nBlockYOff, nBand);
    2650             : #endif
    2651             : 
    2652        6621 :     int nd = 0;
    2653        6621 :     nc_inq_varndims(cdfid, nZId, &nd);
    2654             : 
    2655             :     // Locate X, Y and Z position in the array.
    2656             : 
    2657             :     size_t start[MAX_NC_DIMS];
    2658        6621 :     memset(start, 0, sizeof(start));
    2659        6621 :     start[nBandXPos] = static_cast<size_t>(nBlockXOff) * nBlockXSize;
    2660             : 
    2661             :     // check y order.
    2662        6621 :     if (cpl::down_cast<netCDFDataset *>(poDS)->bBottomUp)
    2663             :     {
    2664        6557 :         if (nBlockYSize == 1)
    2665             :         {
    2666        6557 :             start[nBandYPos] = nRasterYSize - 1 - nBlockYOff;
    2667             :         }
    2668             :         else
    2669             :         {
    2670           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2671             :                      "nBlockYSize = %d, only 1 supported when "
    2672             :                      "writing bottom-up dataset",
    2673             :                      nBlockYSize);
    2674           0 :             return CE_Failure;
    2675             :         }
    2676             :     }
    2677             :     else
    2678             :     {
    2679          64 :         start[nBandYPos] = static_cast<size_t>(nBlockYOff) * nBlockYSize;  // y
    2680             :     }
    2681             : 
    2682        6621 :     size_t edge[MAX_NC_DIMS] = {};
    2683             : 
    2684        6621 :     edge[nBandXPos] = nBlockXSize;
    2685        6621 :     if ((start[nBandXPos] + edge[nBandXPos]) > (size_t)nRasterXSize)
    2686           0 :         edge[nBandXPos] = nRasterXSize - start[nBandXPos];
    2687        6621 :     edge[nBandYPos] = nBlockYSize;
    2688        6621 :     if ((start[nBandYPos] + edge[nBandYPos]) > (size_t)nRasterYSize)
    2689           0 :         edge[nBandYPos] = nRasterYSize - start[nBandYPos];
    2690             : 
    2691        6621 :     if (nd == 3)
    2692             :     {
    2693         630 :         start[panBandZPos[0]] = nLevel;  // z
    2694         630 :         edge[panBandZPos[0]] = 1;
    2695             :     }
    2696             : 
    2697             :     // Compute multidimention band position.
    2698             :     //
    2699             :     // BandPosition = (Total - sum(PastBandLevels) - 1)/sum(remainingLevels)
    2700             :     // if Data[2,3,4,x,y]
    2701             :     //
    2702             :     //  BandPos0 = (nBand) / (3*4)
    2703             :     //  BandPos1 = (nBand - (3*4)) / (4)
    2704             :     //  BandPos2 = (nBand - (3*4)) % (4)
    2705        6621 :     if (nd > 3)
    2706             :     {
    2707         178 :         int Sum = -1;
    2708         178 :         int Taken = 0;
    2709         534 :         for (int i = 0; i < nd - 2; i++)
    2710             :         {
    2711         356 :             if (i != nd - 2 - 1)
    2712             :             {
    2713         178 :                 Sum = 1;
    2714         356 :                 for (int j = i + 1; j < nd - 2; j++)
    2715             :                 {
    2716         178 :                     Sum *= panBandZLev[j];
    2717             :                 }
    2718         178 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) / Sum);
    2719         178 :                 edge[panBandZPos[i]] = 1;
    2720             :             }
    2721             :             else
    2722             :             {
    2723         178 :                 start[panBandZPos[i]] = (int)((nLevel - Taken) % Sum);
    2724         178 :                 edge[panBandZPos[i]] = 1;
    2725             :             }
    2726         356 :             Taken += static_cast<int>(start[panBandZPos[i]]) * Sum;
    2727             :         }
    2728             :     }
    2729             : 
    2730             :     // Make sure we are in data mode.
    2731        6621 :     cpl::down_cast<netCDFDataset *>(poDS)->SetDefineMode(false);
    2732             : 
    2733             :     // Copy data according to type.
    2734        6621 :     int status = 0;
    2735        6621 :     if (eDataType == GDT_UInt8)
    2736             :     {
    2737        6022 :         if (bSignedData)
    2738           0 :             status = nc_put_vara_schar(cdfid, nZId, start, edge,
    2739             :                                        static_cast<signed char *>(pImage));
    2740             :         else
    2741        6022 :             status = nc_put_vara_uchar(cdfid, nZId, start, edge,
    2742             :                                        static_cast<unsigned char *>(pImage));
    2743             :     }
    2744         599 :     else if (eDataType == GDT_Int8)
    2745             :     {
    2746          40 :         status = nc_put_vara_schar(cdfid, nZId, start, edge,
    2747             :                                    static_cast<signed char *>(pImage));
    2748             :     }
    2749         559 :     else if (nc_datatype == NC_SHORT)
    2750             :     {
    2751         101 :         status = nc_put_vara_short(cdfid, nZId, start, edge,
    2752             :                                    static_cast<short *>(pImage));
    2753             :     }
    2754         458 :     else if (eDataType == GDT_Int32)
    2755             :     {
    2756         210 :         status = nc_put_vara_int(cdfid, nZId, start, edge,
    2757             :                                  static_cast<int *>(pImage));
    2758             :     }
    2759         248 :     else if (eDataType == GDT_Float32)
    2760             :     {
    2761         168 :         status = nc_put_vara_float(cdfid, nZId, start, edge,
    2762             :                                    static_cast<float *>(pImage));
    2763             :     }
    2764          80 :     else if (eDataType == GDT_Float64)
    2765             :     {
    2766          50 :         status = nc_put_vara_double(cdfid, nZId, start, edge,
    2767             :                                     static_cast<double *>(pImage));
    2768             :     }
    2769          42 :     else if (eDataType == GDT_UInt16 &&
    2770          12 :              cpl::down_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2771             :     {
    2772          12 :         status = nc_put_vara_ushort(cdfid, nZId, start, edge,
    2773             :                                     static_cast<unsigned short *>(pImage));
    2774             :     }
    2775          30 :     else if (eDataType == GDT_UInt32 &&
    2776          12 :              cpl::down_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2777             :     {
    2778          12 :         status = nc_put_vara_uint(cdfid, nZId, start, edge,
    2779             :                                   static_cast<unsigned int *>(pImage));
    2780             :     }
    2781           9 :     else if (eDataType == GDT_UInt64 &&
    2782           3 :              cpl::down_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2783             :     {
    2784             :         status =
    2785           3 :             nc_put_vara_ulonglong(cdfid, nZId, start, edge,
    2786             :                                   static_cast<unsigned long long *>(pImage));
    2787             :     }
    2788           6 :     else if (eDataType == GDT_Int64 &&
    2789           3 :              cpl::down_cast<netCDFDataset *>(poDS)->eFormat == NCDF_FORMAT_NC4)
    2790             :     {
    2791           3 :         status = nc_put_vara_longlong(cdfid, nZId, start, edge,
    2792             :                                       static_cast<long long *>(pImage));
    2793             :     }
    2794             :     else
    2795             :     {
    2796           0 :         CPLError(CE_Failure, CPLE_NotSupported,
    2797             :                  "The NetCDF driver does not support GDAL data type %d",
    2798           0 :                  eDataType);
    2799           0 :         status = NC_EBADTYPE;
    2800             :     }
    2801        6621 :     NCDF_ERR(status);
    2802             : 
    2803        6621 :     if (status != NC_NOERR)
    2804             :     {
    2805           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    2806             :                  "netCDF scanline write failed: %s", nc_strerror(status));
    2807           0 :         return CE_Failure;
    2808             :     }
    2809             : 
    2810        6621 :     return CE_None;
    2811             : }
    2812             : 
    2813             : /************************************************************************/
    2814             : /* ==================================================================== */
    2815             : /*                              netCDFDataset                           */
    2816             : /* ==================================================================== */
    2817             : /************************************************************************/
    2818             : 
    2819             : /************************************************************************/
    2820             : /*                           netCDFDataset()                            */
    2821             : /************************************************************************/
    2822             : 
    2823        1306 : netCDFDataset::netCDFDataset()
    2824             :     :
    2825             : // Basic dataset vars.
    2826             : #ifdef ENABLE_NCDUMP
    2827             :       bFileToDestroyAtClosing(false),
    2828             : #endif
    2829             :       cdfid(-1), nSubDatasets(0), bBottomUp(true), eFormat(NCDF_FORMAT_NONE),
    2830             :       bIsGdalFile(false), bIsGdalCfFile(false), pszCFProjection(nullptr),
    2831             :       pszCFCoordinates(nullptr), bSGSupport(false),
    2832        1306 :       eMultipleLayerBehavior(SINGLE_LAYER), logCount(0), vcdf(this, cdfid),
    2833        1306 :       GeometryScribe(vcdf, generateLogName()),
    2834        1306 :       FieldScribe(vcdf, generateLogName()),
    2835        2612 :       bufManager(CPLGetUsablePhysicalRAM() / 5),
    2836             : 
    2837             :       // projection/GT.
    2838             :       nXDimID(-1), nYDimID(-1), bIsProjected(false),
    2839             :       bIsGeographic(false),  // Can be not projected, and also not geographic
    2840             :       // State vars.
    2841             :       bDefineMode(true), bAddedGridMappingRef(false),
    2842             : 
    2843             :       // Create vars.
    2844             :       eCompress(NCDF_COMPRESS_NONE), nZLevel(NCDF_DEFLATE_LEVEL),
    2845        3918 :       bChunking(false), nCreateMode(NC_CLOBBER), bSignedData(true)
    2846             : {
    2847        1306 :     m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2848             : 
    2849             :     // Set buffers
    2850        1306 :     bufManager.addBuffer(&(GeometryScribe.getMemBuffer()));
    2851        1306 :     bufManager.addBuffer(&(FieldScribe.getMemBuffer()));
    2852        1306 : }
    2853             : 
    2854             : /************************************************************************/
    2855             : /*                           ~netCDFDataset()                           */
    2856             : /************************************************************************/
    2857             : 
    2858        2499 : netCDFDataset::~netCDFDataset()
    2859             : 
    2860             : {
    2861        1306 :     netCDFDataset::Close();
    2862        2499 : }
    2863             : 
    2864             : /************************************************************************/
    2865             : /*                               Close()                                */
    2866             : /************************************************************************/
    2867             : 
    2868        2153 : CPLErr netCDFDataset::Close(GDALProgressFunc, void *)
    2869             : {
    2870        2153 :     CPLErr eErr = CE_None;
    2871        2153 :     if (nOpenFlags != OPEN_FLAGS_CLOSED)
    2872             :     {
    2873        2612 :         CPLMutexHolderD(&hNCMutex);
    2874             : 
    2875             : #ifdef NCDF_DEBUG
    2876             :         CPLDebug("GDAL_netCDF",
    2877             :                  "netCDFDataset::~netCDFDataset(), cdfid=%d filename=%s", cdfid,
    2878             :                  osFilename.c_str());
    2879             : #endif
    2880             : 
    2881             :         // Write data related to geotransform
    2882        1616 :         if (GetAccess() == GA_Update && !m_bAddedProjectionVarsData &&
    2883         310 :             (m_bHasProjection || m_bHasGeoTransform))
    2884             :         {
    2885             :             // Ensure projection is written if GeoTransform OR Projection are
    2886             :             // missing.
    2887           6 :             if (!m_bAddedProjectionVarsDefs)
    2888             :             {
    2889           2 :                 AddProjectionVars(true, nullptr, nullptr);
    2890             :             }
    2891           6 :             AddProjectionVars(false, nullptr, nullptr);
    2892             :         }
    2893             : 
    2894        1306 :         if (netCDFDataset::FlushCache(true) != CE_None)
    2895           0 :             eErr = CE_Failure;
    2896             : 
    2897        1306 :         if (GetAccess() == GA_Update && !SGCommitPendingTransaction())
    2898           0 :             eErr = CE_Failure;
    2899             : 
    2900        1308 :         for (auto *poVectorDS : apoVectorDatasets)
    2901           2 :             delete poVectorDS;
    2902             : 
    2903             :         // Make sure projection variable is written to band variable.
    2904        1306 :         if (GetAccess() == GA_Update && !bAddedGridMappingRef)
    2905             :         {
    2906         371 :             if (!AddGridMappingRef())
    2907           0 :                 eErr = CE_Failure;
    2908             :         }
    2909             : 
    2910        1306 :         CPLFree(pszCFProjection);
    2911             : 
    2912        1306 :         if (cdfid > 0)
    2913             :         {
    2914             : #ifdef NCDF_DEBUG
    2915             :             CPLDebug("GDAL_netCDF", "calling nc_close( %d)", cdfid);
    2916             : #endif
    2917         673 :             int status = GDAL_nc_close(cdfid);
    2918             : #ifdef ENABLE_UFFD
    2919         673 :             NETCDF_UFFD_UNMAP(pCtx);
    2920             : #endif
    2921         673 :             NCDF_ERR(status);
    2922         673 :             if (status != NC_NOERR)
    2923           0 :                 eErr = CE_Failure;
    2924             :         }
    2925             : 
    2926        1306 :         if (fpVSIMEM)
    2927          15 :             VSIFCloseL(fpVSIMEM);
    2928             : 
    2929             : #ifdef ENABLE_NCDUMP
    2930        1306 :         if (bFileToDestroyAtClosing)
    2931           0 :             VSIUnlink(osFilename);
    2932             : #endif
    2933             : 
    2934        1306 :         if (GDALPamDataset::Close() != CE_None)
    2935           0 :             eErr = CE_Failure;
    2936             :     }
    2937        2153 :     return eErr;
    2938             : }
    2939             : 
    2940             : /************************************************************************/
    2941             : /*                           SetDefineMode()                            */
    2942             : /************************************************************************/
    2943       14598 : bool netCDFDataset::SetDefineMode(bool bNewDefineMode)
    2944             : {
    2945             :     // Do nothing if already in new define mode
    2946             :     // or if dataset is in read-only mode or if dataset is true NC4 dataset.
    2947       15176 :     if (bDefineMode == bNewDefineMode || GetAccess() == GA_ReadOnly ||
    2948         578 :         eFormat == NCDF_FORMAT_NC4)
    2949       14182 :         return true;
    2950             : 
    2951         416 :     CPLDebug("GDAL_netCDF", "SetDefineMode(%d) old=%d",
    2952         416 :              static_cast<int>(bNewDefineMode), static_cast<int>(bDefineMode));
    2953             : 
    2954         416 :     bDefineMode = bNewDefineMode;
    2955             : 
    2956             :     int status;
    2957         416 :     if (bDefineMode)
    2958         152 :         status = nc_redef(cdfid);
    2959             :     else
    2960         264 :         status = nc_enddef(cdfid);
    2961             : 
    2962         416 :     NCDF_ERR(status);
    2963         416 :     return status == NC_NOERR;
    2964             : }
    2965             : 
    2966             : /************************************************************************/
    2967             : /*                       GetMetadataDomainList()                        */
    2968             : /************************************************************************/
    2969             : 
    2970          26 : char **netCDFDataset::GetMetadataDomainList()
    2971             : {
    2972             :     char **papszDomains =
    2973          26 :         BuildMetadataDomainList(GDALDataset::GetMetadataDomainList(), TRUE,
    2974             :                                 GDAL_MDD_SUBDATASETS, nullptr);
    2975          27 :     for (const auto &kv : m_oMapDomainToJSon)
    2976           1 :         papszDomains = CSLAddString(papszDomains, ("json:" + kv.first).c_str());
    2977          26 :     return papszDomains;
    2978             : }
    2979             : 
    2980             : /************************************************************************/
    2981             : /*                            GetMetadata()                             */
    2982             : /************************************************************************/
    2983         388 : CSLConstList netCDFDataset::GetMetadata(const char *pszDomain)
    2984             : {
    2985         388 :     if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, GDAL_MDD_SUBDATASETS))
    2986          47 :         return aosSubDatasets.List();
    2987             : 
    2988         341 :     if (pszDomain != nullptr && STARTS_WITH(pszDomain, "json:"))
    2989             :     {
    2990           6 :         auto iter = m_oMapDomainToJSon.find(pszDomain + strlen("json:"));
    2991           6 :         if (iter != m_oMapDomainToJSon.end())
    2992           1 :             return iter->second.List();
    2993             :     }
    2994             : 
    2995         340 :     return GDALDataset::GetMetadata(pszDomain);
    2996             : }
    2997             : 
    2998             : /************************************************************************/
    2999             : /*                          SetMetadataItem()                           */
    3000             : /************************************************************************/
    3001             : 
    3002          43 : CPLErr netCDFDataset::SetMetadataItem(const char *pszName, const char *pszValue,
    3003             :                                       const char *pszDomain)
    3004             : {
    3005          85 :     if (GetAccess() == GA_Update &&
    3006          85 :         (pszDomain == nullptr || pszDomain[0] == '\0') && pszValue != nullptr)
    3007             :     {
    3008          42 :         std::string osName(pszName);
    3009             : 
    3010             :         // Same logic as in CopyMetadata()
    3011          42 :         if (cpl::starts_with(osName, "NC_GLOBAL#"))
    3012           8 :             osName = osName.substr(strlen("NC_GLOBAL#"));
    3013          34 :         else if (strchr(osName.c_str(), '#') == nullptr)
    3014           5 :             osName = "GDAL_" + osName;
    3015             : 
    3016          84 :         if (cpl::starts_with(osName, "NETCDF_DIM_") ||
    3017          42 :             strchr(osName.c_str(), '#') != nullptr)
    3018             :         {
    3019             :             // do nothing
    3020          29 :             return CE_None;
    3021             :         }
    3022             :         else
    3023             :         {
    3024          13 :             SetDefineMode(true);
    3025             : 
    3026          13 :             if (!NCDFPutAttr(cdfid, NC_GLOBAL, osName.c_str(), pszValue))
    3027          13 :                 return CE_Failure;
    3028             :         }
    3029             :     }
    3030             : 
    3031           1 :     return GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
    3032             : }
    3033             : 
    3034             : /************************************************************************/
    3035             : /*                            SetMetadata()                             */
    3036             : /************************************************************************/
    3037             : 
    3038           8 : CPLErr netCDFDataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
    3039             : {
    3040          13 :     if (GetAccess() == GA_Update &&
    3041           5 :         (pszDomain == nullptr || pszDomain[0] == '\0'))
    3042             :     {
    3043             :         // We don't handle metadata item removal for now
    3044          50 :         for (const char *const *papszIter = papszMD; papszIter && *papszIter;
    3045             :              ++papszIter)
    3046             :         {
    3047          42 :             char *pszName = nullptr;
    3048          42 :             const char *pszValue = CPLParseNameValue(*papszIter, &pszName);
    3049          42 :             if (pszName && pszValue)
    3050          42 :                 SetMetadataItem(pszName, pszValue);
    3051          42 :             CPLFree(pszName);
    3052             :         }
    3053           8 :         return CE_None;
    3054             :     }
    3055           0 :     return GDALPamDataset::SetMetadata(papszMD, pszDomain);
    3056             : }
    3057             : 
    3058             : /************************************************************************/
    3059             : /*                           GetSpatialRef()                            */
    3060             : /************************************************************************/
    3061             : 
    3062         252 : const OGRSpatialReference *netCDFDataset::GetSpatialRef() const
    3063             : {
    3064         252 :     if (m_bHasProjection)
    3065         121 :         return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
    3066             : 
    3067         131 :     return GDALPamDataset::GetSpatialRef();
    3068             : }
    3069             : 
    3070             : /************************************************************************/
    3071             : /*                           FetchCopyParam()                           */
    3072             : /************************************************************************/
    3073             : 
    3074         452 : double netCDFDataset::FetchCopyParam(const char *pszGridMappingValue,
    3075             :                                      const char *pszParam, double dfDefault,
    3076             :                                      bool *pbFound) const
    3077             : 
    3078             : {
    3079         904 :     std::string osTemp = CPLOPrintf("%s#%s", pszGridMappingValue, pszParam);
    3080         452 :     const char *pszValue = aosMetadata.FetchNameValue(osTemp.c_str());
    3081             : 
    3082         452 :     if (pbFound)
    3083             :     {
    3084         452 :         *pbFound = (pszValue != nullptr);
    3085             :     }
    3086             : 
    3087         452 :     if (pszValue)
    3088             :     {
    3089           0 :         return CPLAtofM(pszValue);
    3090             :     }
    3091             : 
    3092         452 :     return dfDefault;
    3093             : }
    3094             : 
    3095             : /************************************************************************/
    3096             : /*                       FetchStandardParallels()                       */
    3097             : /************************************************************************/
    3098             : 
    3099             : std::vector<std::string>
    3100           0 : netCDFDataset::FetchStandardParallels(const char *pszGridMappingValue) const
    3101             : {
    3102             :     // cf-1.0 tags
    3103           0 :     const char *pszValue = FetchAttr(pszGridMappingValue, CF_PP_STD_PARALLEL);
    3104             : 
    3105           0 :     std::vector<std::string> ret;
    3106           0 :     if (pszValue != nullptr)
    3107             :     {
    3108           0 :         CPLStringList aosValues;
    3109           0 :         if (pszValue[0] != '{' &&
    3110           0 :             CPLString(pszValue).Trim().find(' ') != std::string::npos)
    3111             :         {
    3112             :             // Some files like
    3113             :             // ftp://data.knmi.nl/download/KNW-NetCDF-3D/1.0/noversion/2013/11/14/KNW-1.0_H37-ERA_NL_20131114.nc
    3114             :             // do not use standard formatting for arrays, but just space
    3115             :             // separated syntax
    3116           0 :             aosValues = CSLTokenizeString2(pszValue, " ", 0);
    3117             :         }
    3118             :         else
    3119             :         {
    3120           0 :             aosValues = NCDFTokenizeArray(pszValue);
    3121             :         }
    3122           0 :         for (const auto &pszToken : aosValues)
    3123             :         {
    3124           0 :             ret.push_back(pszToken);
    3125             :         }
    3126             :     }
    3127             :     // Try gdal tags.
    3128             :     else
    3129             :     {
    3130           0 :         pszValue = FetchAttr(pszGridMappingValue, CF_PP_STD_PARALLEL_1);
    3131             : 
    3132           0 :         if (pszValue != nullptr)
    3133           0 :             ret.push_back(pszValue);
    3134             : 
    3135           0 :         pszValue = FetchAttr(pszGridMappingValue, CF_PP_STD_PARALLEL_2);
    3136             : 
    3137           0 :         if (pszValue != nullptr)
    3138           0 :             ret.push_back(pszValue);
    3139             :     }
    3140             : 
    3141           0 :     return ret;
    3142             : }
    3143             : 
    3144             : /************************************************************************/
    3145             : /*                             FetchAttr()                              */
    3146             : /************************************************************************/
    3147             : 
    3148        4318 : const char *netCDFDataset::FetchAttr(const char *pszVarFullName,
    3149             :                                      const char *pszAttr) const
    3150             : 
    3151             : {
    3152        4318 :     auto oKey = CPLOPrintf("%s#%s", pszVarFullName, pszAttr);
    3153        4318 :     const char *pszValue = aosMetadata.FetchNameValue(oKey.c_str());
    3154        8636 :     return pszValue;
    3155             : }
    3156             : 
    3157        2805 : const char *netCDFDataset::FetchAttr(int nGroupId, int nVarId,
    3158             :                                      const char *pszAttr) const
    3159             : 
    3160             : {
    3161        2805 :     std::string osFullName;
    3162        2805 :     NCDFGetVarFullName(nGroupId, nVarId, osFullName);
    3163        2805 :     const char *pszValue = FetchAttr(osFullName.c_str(), pszAttr);
    3164        5610 :     return pszValue;
    3165             : }
    3166             : 
    3167             : /************************************************************************/
    3168             : /*                         IsDifferenceBelow()                          */
    3169             : /************************************************************************/
    3170             : 
    3171        1139 : static bool IsDifferenceBelow(double dfA, double dfB, double dfError)
    3172             : {
    3173        1139 :     const double dfAbsDiff = fabs(dfA - dfB);
    3174        1139 :     return dfAbsDiff <= dfError;
    3175             : }
    3176             : 
    3177             : /************************************************************************/
    3178             : /*                        SetProjectionFromVar()                        */
    3179             : /************************************************************************/
    3180         627 : void netCDFDataset::SetProjectionFromVar(
    3181             :     int nGroupId, int nVarId, bool bReadSRSOnly, const char *pszGivenGM,
    3182             :     std::string *returnProjStr, nccfdriver::SGeometry_Reader *sg,
    3183             :     std::vector<std::string> *paosRemovedMDItems)
    3184             : {
    3185         627 :     bool bGotGeogCS = false;
    3186         627 :     bool bGotCfSRS = false;
    3187         627 :     bool bGotCfWktSRS = false;
    3188         627 :     bool bGotGdalSRS = false;
    3189         627 :     bool bGotCfGT = false;
    3190         627 :     bool bGotGdalGT = false;
    3191             : 
    3192             :     // These values from CF metadata.
    3193         627 :     OGRSpatialReference oSRS;
    3194         627 :     oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3195         627 :     size_t xdim = nRasterXSize;
    3196         627 :     size_t ydim = nRasterYSize;
    3197             : 
    3198             :     // These values from GDAL metadata.
    3199         627 :     const char *pszWKT = nullptr;
    3200         627 :     const char *pszGeoTransform = nullptr;
    3201             : 
    3202         627 :     netCDFDataset *poDS = this;  // Perhaps this should be removed for clarity.
    3203             : 
    3204         627 :     CPLDebug("GDAL_netCDF", "\n=====\nSetProjectionFromVar( %d, %d)", nGroupId,
    3205             :              nVarId);
    3206             : 
    3207             :     // Get x/y range information.
    3208             : 
    3209             :     // Temp variables to use in SetGeoTransform() and SetProjection().
    3210         627 :     GDALGeoTransform tmpGT;
    3211             : 
    3212             :     // Look for grid_mapping metadata.
    3213         627 :     const char *pszValue = pszGivenGM;
    3214         627 :     CPLString osTmpGridMapping;  // let is in this outer scope as pszValue may
    3215             :     // point to it
    3216         627 :     if (pszValue == nullptr)
    3217             :     {
    3218         583 :         pszValue = FetchAttr(nGroupId, nVarId, CF_GRD_MAPPING);
    3219         583 :         if (pszValue && strchr(pszValue, ':') && strchr(pszValue, ' '))
    3220             :         {
    3221             :             // Expanded form of grid_mapping
    3222             :             // e.g. "crsOSGB: x y crsWGS84: lat lon"
    3223             :             // Pickup the grid_mapping whose coordinates are dimensions of the
    3224             :             // variable
    3225           6 :             const CPLStringList aosTokens(CSLTokenizeString2(pszValue, " ", 0));
    3226           3 :             if ((aosTokens.size() % 3) == 0)
    3227             :             {
    3228           3 :                 for (int i = 0; i < aosTokens.size() / 3; i++)
    3229             :                 {
    3230           3 :                     if (CSLFindString(poDS->papszDimName,
    3231           9 :                                       aosTokens[3 * i + 1]) >= 0 &&
    3232           3 :                         CSLFindString(poDS->papszDimName,
    3233           3 :                                       aosTokens[3 * i + 2]) >= 0)
    3234             :                     {
    3235           3 :                         osTmpGridMapping = aosTokens[3 * i];
    3236           6 :                         if (!osTmpGridMapping.empty() &&
    3237           3 :                             osTmpGridMapping.back() == ':')
    3238             :                         {
    3239           3 :                             osTmpGridMapping.resize(osTmpGridMapping.size() -
    3240             :                                                     1);
    3241             :                         }
    3242           3 :                         pszValue = osTmpGridMapping.c_str();
    3243           3 :                         break;
    3244             :                     }
    3245             :                 }
    3246             :             }
    3247             :         }
    3248             :     }
    3249         627 :     std::string osGridMappingValue = pszValue ? pszValue : "";
    3250             : 
    3251         627 :     if (!osGridMappingValue.empty())
    3252             :     {
    3253             :         // Read grid_mapping metadata.
    3254         256 :         int nProjGroupID = -1;
    3255         256 :         int nProjVarID = -1;
    3256         256 :         if (NCDFResolveVar(nGroupId, osGridMappingValue.c_str(), &nProjGroupID,
    3257         256 :                            &nProjVarID) == CE_None)
    3258             :         {
    3259         254 :             poDS->ReadAttributes(nProjGroupID, nProjVarID);
    3260             : 
    3261             :             // Look for GDAL spatial_ref and GeoTransform within grid_mapping.
    3262         254 :             if (NCDFGetVarFullName(nProjGroupID, nProjVarID,
    3263         254 :                                    osGridMappingValue) == CE_None)
    3264             :             {
    3265         254 :                 CPLDebug("GDAL_netCDF", "got grid_mapping %s",
    3266             :                          osGridMappingValue.c_str());
    3267             :                 pszWKT =
    3268         254 :                     FetchAttr(osGridMappingValue.c_str(), NCDF_SPATIAL_REF);
    3269         254 :                 if (!pszWKT)
    3270             :                 {
    3271             :                     pszWKT =
    3272          36 :                         FetchAttr(osGridMappingValue.c_str(), NCDF_CRS_WKT);
    3273             :                 }
    3274             :                 else
    3275             :                 {
    3276         218 :                     bGotGdalSRS = true;
    3277         218 :                     CPLDebug("GDAL_netCDF", "setting WKT from GDAL");
    3278             :                 }
    3279         254 :                 if (pszWKT)
    3280             :                 {
    3281         224 :                     if (!bGotGdalSRS)
    3282             :                     {
    3283           6 :                         bGotCfWktSRS = true;
    3284           6 :                         CPLDebug("GDAL_netCDF", "setting WKT from CF");
    3285             :                     }
    3286         224 :                     if (returnProjStr != nullptr)
    3287             :                     {
    3288          42 :                         (*returnProjStr) = std::string(pszWKT);
    3289             :                     }
    3290             :                     else
    3291             :                     {
    3292         182 :                         m_bAddedProjectionVarsDefs = true;
    3293         182 :                         m_bAddedProjectionVarsData = true;
    3294         364 :                         OGRSpatialReference oSRSTmp;
    3295         182 :                         oSRSTmp.SetAxisMappingStrategy(
    3296             :                             OAMS_TRADITIONAL_GIS_ORDER);
    3297         182 :                         oSRSTmp.importFromWkt(pszWKT);
    3298         182 :                         SetSpatialRefNoUpdate(&oSRSTmp);
    3299             :                     }
    3300         224 :                     pszGeoTransform = FetchAttr(osGridMappingValue.c_str(),
    3301             :                                                 NCDF_GEOTRANSFORM);
    3302             :                 }
    3303             :             }
    3304             :         }
    3305             :         else
    3306             :         {
    3307           4 :             std::string osVarName = "unknown";
    3308           2 :             NCDFGetVarFullName(nGroupId, nVarId, osVarName);
    3309             : 
    3310           2 :             CPLError(CE_Warning, CPLE_AppDefined,
    3311             :                      "'%s' attribute of variable '%s' references grid mapping "
    3312             :                      "variable '%s', but no such variable exists. The spatial "
    3313             :                      "referencing of this dataset may be incorrect.",
    3314             :                      CF_GRD_MAPPING, osVarName.c_str(),
    3315             :                      osGridMappingValue.c_str());
    3316             :         }
    3317             :     }
    3318             : 
    3319             :     // Get information about the file.
    3320             :     //
    3321             :     // Was this file created by the GDAL netcdf driver?
    3322             :     // Was this file created by the newer (CF-conformant) driver?
    3323             :     //
    3324             :     // 1) If GDAL netcdf metadata is set, and version >= 1.9,
    3325             :     //    it was created with the new driver
    3326             :     // 2) Else, if spatial_ref and GeoTransform are present in the
    3327             :     //    grid_mapping variable, it was created by the old driver
    3328         627 :     pszValue = FetchAttr("NC_GLOBAL", "GDAL");
    3329             : 
    3330         627 :     if (pszValue && NCDFIsGDALVersionGTE(pszValue, 1900))
    3331             :     {
    3332         284 :         bIsGdalFile = true;
    3333         284 :         bIsGdalCfFile = true;
    3334             :     }
    3335         343 :     else if (pszWKT != nullptr && pszGeoTransform != nullptr)
    3336             :     {
    3337          39 :         bIsGdalFile = true;
    3338          39 :         bIsGdalCfFile = false;
    3339             :     }
    3340             : 
    3341             :     // Set default bottom-up default value.
    3342             :     // Y axis dimension and absence of GT can modify this value.
    3343             :     // Override with Config option GDAL_NETCDF_BOTTOMUP.
    3344             : 
    3345             :     // New driver is bottom-up by default.
    3346         627 :     if ((bIsGdalFile && !bIsGdalCfFile) || bSwitchedXY)
    3347          41 :         poDS->bBottomUp = false;
    3348             :     else
    3349         586 :         poDS->bBottomUp = true;
    3350             : 
    3351         627 :     CPLDebug("GDAL_netCDF",
    3352             :              "bIsGdalFile=%d bIsGdalCfFile=%d bSwitchedXY=%d bBottomUp=%d",
    3353         627 :              static_cast<int>(bIsGdalFile), static_cast<int>(bIsGdalCfFile),
    3354         627 :              static_cast<int>(bSwitchedXY), static_cast<int>(bBottomUp));
    3355             : 
    3356             :     // Read projection coordinates.
    3357             : 
    3358         627 :     int nGroupDimXID = -1;
    3359         627 :     int nVarDimXID = -1;
    3360         627 :     int nGroupDimYID = -1;
    3361         627 :     int nVarDimYID = -1;
    3362         627 :     if (sg != nullptr)
    3363             :     {
    3364          44 :         nGroupDimXID = sg->get_ncID();
    3365          44 :         nGroupDimYID = sg->get_ncID();
    3366          44 :         nVarDimXID = sg->getNodeCoordVars()[0];
    3367          44 :         nVarDimYID = sg->getNodeCoordVars()[1];
    3368             :     }
    3369             : 
    3370         627 :     if (!bReadSRSOnly)
    3371             :     {
    3372         370 :         NCDFResolveVar(nGroupId, poDS->papszDimName[nXDimID], &nGroupDimXID,
    3373             :                        &nVarDimXID);
    3374         370 :         NCDFResolveVar(nGroupId, poDS->papszDimName[nYDimID], &nGroupDimYID,
    3375             :                        &nVarDimYID);
    3376             :         // TODO: if above resolving fails we should also search for coordinate
    3377             :         // variables without same name than dimension using the same resolving
    3378             :         // logic. This should handle for example NASA Ocean Color L2 products.
    3379             : 
    3380             :         const bool bIgnoreXYAxisNameChecks =
    3381         740 :             CPLTestBool(CSLFetchNameValueDef(
    3382         370 :                 papszOpenOptions, "IGNORE_XY_AXIS_NAME_CHECKS",
    3383             :                 CPLGetConfigOption("GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS",
    3384         370 :                                    "NO"))) ||
    3385             :             // Dataset from https://github.com/OSGeo/gdal/issues/4075 has a res
    3386             :             // and transform attributes
    3387         370 :             (FetchAttr(nGroupId, nVarId, "res") != nullptr &&
    3388         740 :              FetchAttr(nGroupId, nVarId, "transform") != nullptr) ||
    3389         369 :             FetchAttr(nGroupId, NC_GLOBAL, "GMT_version") != nullptr;
    3390             : 
    3391             :         // Check that they are 1D or 2D variables
    3392         370 :         if (nVarDimXID >= 0)
    3393             :         {
    3394         264 :             int ndims = -1;
    3395         264 :             nc_inq_varndims(nGroupId, nVarDimXID, &ndims);
    3396         264 :             if (ndims == 0 || ndims > 2)
    3397           0 :                 nVarDimXID = -1;
    3398         264 :             else if (!bIgnoreXYAxisNameChecks)
    3399             :             {
    3400         262 :                 if (!NCDFIsVarLongitude(nGroupId, nVarDimXID, nullptr) &&
    3401         179 :                     !NCDFIsVarProjectionX(nGroupId, nVarDimXID, nullptr) &&
    3402             :                     // In case of inversion of X/Y
    3403         473 :                     !NCDFIsVarLatitude(nGroupId, nVarDimXID, nullptr) &&
    3404          32 :                     !NCDFIsVarProjectionY(nGroupId, nVarDimXID, nullptr))
    3405             :                 {
    3406             :                     char szVarNameX[NC_MAX_NAME + 1];
    3407          32 :                     CPL_IGNORE_RET_VAL(
    3408          32 :                         nc_inq_varname(nGroupId, nVarDimXID, szVarNameX));
    3409          32 :                     if (!(ndims == 1 &&
    3410          31 :                           (EQUAL(szVarNameX, CF_LONGITUDE_STD_NAME) ||
    3411          30 :                            EQUAL(szVarNameX, CF_LONGITUDE_VAR_NAME))))
    3412             :                     {
    3413          31 :                         CPLDebug(
    3414             :                             "netCDF",
    3415             :                             "Georeferencing ignored due to non-specific "
    3416             :                             "enough X axis name. "
    3417             :                             "Set GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS=YES "
    3418             :                             "as configuration option to bypass this check");
    3419          31 :                         nVarDimXID = -1;
    3420             :                     }
    3421             :                 }
    3422             :             }
    3423             :         }
    3424             : 
    3425         370 :         if (nVarDimYID >= 0)
    3426             :         {
    3427         266 :             int ndims = -1;
    3428         266 :             nc_inq_varndims(nGroupId, nVarDimYID, &ndims);
    3429         266 :             if (ndims == 0 || ndims > 2)
    3430           1 :                 nVarDimYID = -1;
    3431         265 :             else if (!bIgnoreXYAxisNameChecks)
    3432             :             {
    3433         263 :                 if (!NCDFIsVarLatitude(nGroupId, nVarDimYID, nullptr) &&
    3434         180 :                     !NCDFIsVarProjectionY(nGroupId, nVarDimYID, nullptr) &&
    3435             :                     // In case of inversion of X/Y
    3436         476 :                     !NCDFIsVarLongitude(nGroupId, nVarDimYID, nullptr) &&
    3437          33 :                     !NCDFIsVarProjectionX(nGroupId, nVarDimYID, nullptr))
    3438             :                 {
    3439             :                     char szVarNameY[NC_MAX_NAME + 1];
    3440          33 :                     CPL_IGNORE_RET_VAL(
    3441          33 :                         nc_inq_varname(nGroupId, nVarDimYID, szVarNameY));
    3442          33 :                     if (!(ndims == 1 &&
    3443          33 :                           (EQUAL(szVarNameY, CF_LATITUDE_STD_NAME) ||
    3444          32 :                            EQUAL(szVarNameY, CF_LATITUDE_VAR_NAME))))
    3445             :                     {
    3446          32 :                         CPLDebug(
    3447             :                             "netCDF",
    3448             :                             "Georeferencing ignored due to non-specific "
    3449             :                             "enough Y axis name. "
    3450             :                             "Set GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS=YES "
    3451             :                             "as configuration option to bypass this check");
    3452          32 :                         nVarDimYID = -1;
    3453             :                     }
    3454             :                 }
    3455             :             }
    3456             :         }
    3457             : 
    3458         370 :         if ((nVarDimXID >= 0 && xdim == 1) || (nVarDimXID >= 0 && ydim == 1))
    3459             :         {
    3460           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    3461             :                      "1-pixel width/height files not supported, "
    3462             :                      "xdim: %ld ydim: %ld",
    3463             :                      static_cast<long>(xdim), static_cast<long>(ydim));
    3464           0 :             nVarDimXID = -1;
    3465           0 :             nVarDimYID = -1;
    3466             :         }
    3467             :     }
    3468             : 
    3469         627 :     const char *pszUnits = nullptr;
    3470         627 :     if ((nVarDimXID != -1) && (nVarDimYID != -1) && xdim > 0 && ydim > 0)
    3471             :     {
    3472         277 :         const char *pszUnitsX = FetchAttr(nGroupDimXID, nVarDimXID, "units");
    3473         277 :         const char *pszUnitsY = FetchAttr(nGroupDimYID, nVarDimYID, "units");
    3474             :         // Normalize degrees_east/degrees_north to degrees
    3475             :         // Cf https://github.com/OSGeo/gdal/issues/11009
    3476         277 :         if (pszUnitsX && EQUAL(pszUnitsX, "degrees_east"))
    3477          71 :             pszUnitsX = "degrees";
    3478         277 :         if (pszUnitsY && EQUAL(pszUnitsY, "degrees_north"))
    3479          71 :             pszUnitsY = "degrees";
    3480             : 
    3481         277 :         if (pszUnitsX && pszUnitsY)
    3482             :         {
    3483         229 :             if (EQUAL(pszUnitsX, pszUnitsY))
    3484         226 :                 pszUnits = pszUnitsX;
    3485           3 :             else if (!pszWKT && !osGridMappingValue.empty())
    3486             :             {
    3487           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    3488             :                          "X axis unit (%s) is different from Y axis "
    3489             :                          "unit (%s). SRS will ignore axis unit and be "
    3490             :                          "likely wrong.",
    3491             :                          pszUnitsX, pszUnitsY);
    3492             :             }
    3493             :         }
    3494          48 :         else if (pszUnitsX && !pszWKT && !osGridMappingValue.empty())
    3495             :         {
    3496           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3497             :                      "X axis unit is defined, but not Y one ."
    3498             :                      "SRS will ignore axis unit and be likely wrong.");
    3499             :         }
    3500          48 :         else if (pszUnitsY && !pszWKT && !osGridMappingValue.empty())
    3501             :         {
    3502           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3503             :                      "Y axis unit is defined, but not X one ."
    3504             :                      "SRS will ignore axis unit and be likely wrong.");
    3505             :         }
    3506             :     }
    3507             : 
    3508         627 :     if (!pszWKT && !osGridMappingValue.empty())
    3509             :     {
    3510          32 :         CPLStringList aosGridMappingKeyValues;
    3511          32 :         const size_t nLenGridMappingValue = osGridMappingValue.size();
    3512         817 :         for (const char *pszIter : aosMetadata)
    3513             :         {
    3514        1021 :             if (STARTS_WITH(pszIter, osGridMappingValue.c_str()) &&
    3515         236 :                 pszIter[nLenGridMappingValue] == '#')
    3516             :             {
    3517         236 :                 char *pszKey = nullptr;
    3518         236 :                 pszValue = CPLParseNameValue(pszIter + nLenGridMappingValue + 1,
    3519             :                                              &pszKey);
    3520         236 :                 if (pszKey && pszValue)
    3521         236 :                     aosGridMappingKeyValues.SetNameValue(pszKey, pszValue);
    3522         236 :                 CPLFree(pszKey);
    3523             :             }
    3524             :         }
    3525             : 
    3526          32 :         bGotGeogCS = aosGridMappingKeyValues.FetchNameValue(
    3527             :                          CF_PP_SEMI_MAJOR_AXIS) != nullptr;
    3528             : 
    3529          32 :         oSRS.importFromCF1(aosGridMappingKeyValues.List(), pszUnits);
    3530          32 :         bGotCfSRS = oSRS.IsGeographic() || oSRS.IsProjected();
    3531             :     }
    3532             :     else
    3533             :     {
    3534             :         // Dataset from https://github.com/OSGeo/gdal/issues/4075 has a "crs"
    3535             :         // attribute hold on the variable of interest that contains a PROJ.4
    3536             :         // string
    3537         595 :         pszValue = FetchAttr(nGroupId, nVarId, "crs");
    3538         596 :         if (pszValue &&
    3539           1 :             (strstr(pszValue, "+proj=") != nullptr ||
    3540           0 :              strstr(pszValue, "GEOGCS") != nullptr ||
    3541           0 :              strstr(pszValue, "PROJCS") != nullptr ||
    3542         596 :              strstr(pszValue, "EPSG:") != nullptr) &&
    3543           1 :             oSRS.SetFromUserInput(pszValue) == OGRERR_NONE)
    3544             :         {
    3545           1 :             bGotCfSRS = true;
    3546             :         }
    3547             :     }
    3548             : 
    3549             :     // Set Projection from CF.
    3550         627 :     double dfLinearUnitsConvFactor = 1.0;
    3551         627 :     if ((bGotGeogCS || bGotCfSRS))
    3552             :     {
    3553          31 :         if ((nVarDimXID != -1) && (nVarDimYID != -1) && xdim > 0 && ydim > 0)
    3554             :         {
    3555             :             // Set SRS Units.
    3556             : 
    3557             :             // Check units for x and y.
    3558          28 :             if (oSRS.IsProjected())
    3559             :             {
    3560          25 :                 dfLinearUnitsConvFactor = oSRS.GetLinearUnits(nullptr);
    3561             : 
    3562             :                 // If the user doesn't ask to preserve the axis unit,
    3563             :                 // then normalize to metre
    3564          31 :                 if (dfLinearUnitsConvFactor != 1.0 &&
    3565           6 :                     !CPLFetchBool(GetOpenOptions(), "PRESERVE_AXIS_UNIT_IN_CRS",
    3566             :                                   false))
    3567             :                 {
    3568           5 :                     oSRS.SetLinearUnits("metre", 1.0);
    3569           5 :                     oSRS.SetAuthority("PROJCS|UNIT", "EPSG", 9001);
    3570             :                 }
    3571             :                 else
    3572             :                 {
    3573          20 :                     dfLinearUnitsConvFactor = 1.0;
    3574             :                 }
    3575             :             }
    3576             :         }
    3577             : 
    3578             :         // Set projection.
    3579          31 :         char *pszTempProjection = nullptr;
    3580          31 :         oSRS.exportToWkt(&pszTempProjection);
    3581          31 :         if (pszTempProjection)
    3582             :         {
    3583          31 :             CPLDebug("GDAL_netCDF", "setting WKT from CF");
    3584          31 :             if (returnProjStr != nullptr)
    3585             :             {
    3586           2 :                 (*returnProjStr) = std::string(pszTempProjection);
    3587             :             }
    3588             :             else
    3589             :             {
    3590          29 :                 m_bAddedProjectionVarsDefs = true;
    3591          29 :                 m_bAddedProjectionVarsData = true;
    3592          29 :                 SetSpatialRefNoUpdate(&oSRS);
    3593             :             }
    3594             :         }
    3595          31 :         CPLFree(pszTempProjection);
    3596             :     }
    3597             : 
    3598         627 :     if (!bReadSRSOnly && (nVarDimXID != -1) && (nVarDimYID != -1) && xdim > 0 &&
    3599             :         ydim > 0)
    3600             :     {
    3601             :         double *pdfXCoord =
    3602         233 :             static_cast<double *>(CPLCalloc(xdim, sizeof(double)));
    3603             :         double *pdfYCoord =
    3604         233 :             static_cast<double *>(CPLCalloc(ydim, sizeof(double)));
    3605             : 
    3606         233 :         size_t start[2] = {0, 0};
    3607         233 :         size_t edge[2] = {xdim, 0};
    3608         233 :         int status = nc_get_vara_double(nGroupDimXID, nVarDimXID, start, edge,
    3609             :                                         pdfXCoord);
    3610         233 :         NCDF_ERR(status);
    3611             : 
    3612         233 :         edge[0] = ydim;
    3613         233 :         status = nc_get_vara_double(nGroupDimYID, nVarDimYID, start, edge,
    3614             :                                     pdfYCoord);
    3615         233 :         NCDF_ERR(status);
    3616             : 
    3617         233 :         nc_type nc_var_dimx_datatype = NC_NAT;
    3618             :         status =
    3619         233 :             nc_inq_vartype(nGroupDimXID, nVarDimXID, &nc_var_dimx_datatype);
    3620         233 :         NCDF_ERR(status);
    3621             : 
    3622         233 :         nc_type nc_var_dimy_datatype = NC_NAT;
    3623             :         status =
    3624         233 :             nc_inq_vartype(nGroupDimYID, nVarDimYID, &nc_var_dimy_datatype);
    3625         233 :         NCDF_ERR(status);
    3626             : 
    3627         233 :         if (!poDS->bSwitchedXY)
    3628             :         {
    3629             :             // Convert ]180,540] longitude values to ]-180,0].
    3630         314 :             if (NCDFIsVarLongitude(nGroupDimXID, nVarDimXID, nullptr) &&
    3631          83 :                 CPLTestBool(
    3632             :                     CPLGetConfigOption("GDAL_NETCDF_CENTERLONG_180", "YES")))
    3633             :             {
    3634             :                 // If minimum longitude is > 180, subtract 360 from all.
    3635             :                 // Add a check on the maximum X value too, since
    3636             :                 // NCDFIsVarLongitude() is not very specific by default (see
    3637             :                 // https://github.com/OSGeo/gdal/issues/1440)
    3638          90 :                 if (std::min(pdfXCoord[0], pdfXCoord[xdim - 1]) > 180.0 &&
    3639           7 :                     std::max(pdfXCoord[0], pdfXCoord[xdim - 1]) <= 540)
    3640             :                 {
    3641           0 :                     CPLDebug(
    3642             :                         "GDAL_netCDF",
    3643             :                         "Offsetting longitudes from ]180,540] to ]-180,180]. "
    3644             :                         "Can be disabled with GDAL_NETCDF_CENTERLONG_180=NO");
    3645           0 :                     for (size_t i = 0; i < xdim; i++)
    3646           0 :                         pdfXCoord[i] -= 360;
    3647             :                 }
    3648             :             }
    3649             :         }
    3650             : 
    3651             :         // Is pixel spacing uniform across the map?
    3652             : 
    3653             :         // Check Longitude.
    3654             : 
    3655         233 :         bool bLonSpacingOK = false;
    3656         233 :         if (xdim == 2)
    3657             :         {
    3658          29 :             bLonSpacingOK = true;
    3659             :         }
    3660             :         else
    3661             :         {
    3662         204 :             bool bWestIsLeft = (pdfXCoord[0] < pdfXCoord[xdim - 1]);
    3663             : 
    3664             :             // fix longitudes if longitudes should increase from
    3665             :             // west to east, but west > east
    3666         277 :             if (NCDFIsVarLongitude(nGroupDimXID, nVarDimXID, nullptr) &&
    3667          73 :                 !bWestIsLeft)
    3668             :             {
    3669           2 :                 size_t ndecreases = 0;
    3670             : 
    3671             :                 // there is lon wrap if longitudes increase
    3672             :                 // with one single decrease
    3673         107 :                 for (size_t i = 1; i < xdim; i++)
    3674             :                 {
    3675         105 :                     if (pdfXCoord[i] < pdfXCoord[i - 1])
    3676           1 :                         ndecreases++;
    3677             :                 }
    3678             : 
    3679           2 :                 if (ndecreases == 1)
    3680             :                 {
    3681           1 :                     CPLDebug("GDAL_netCDF", "longitude wrap detected");
    3682           4 :                     for (size_t i = 0; i < xdim; i++)
    3683             :                     {
    3684           3 :                         if (pdfXCoord[i] > pdfXCoord[xdim - 1])
    3685           1 :                             pdfXCoord[i] -= 360;
    3686             :                     }
    3687             :                 }
    3688             :             }
    3689             : 
    3690         204 :             const double dfSpacingBegin = pdfXCoord[1] - pdfXCoord[0];
    3691         204 :             const double dfSpacingMiddle =
    3692         204 :                 pdfXCoord[xdim / 2 + 1] - pdfXCoord[xdim / 2];
    3693         204 :             const double dfSpacingLast =
    3694         204 :                 pdfXCoord[xdim - 1] - pdfXCoord[xdim - 2];
    3695             : 
    3696         204 :             CPLDebug("GDAL_netCDF",
    3697             :                      "xdim: %ld dfSpacingBegin: %f dfSpacingMiddle: %f "
    3698             :                      "dfSpacingLast: %f",
    3699             :                      static_cast<long>(xdim), dfSpacingBegin, dfSpacingMiddle,
    3700             :                      dfSpacingLast);
    3701             : #ifdef NCDF_DEBUG
    3702             :             CPLDebug("GDAL_netCDF", "xcoords: %f %f %f %f %f %f", pdfXCoord[0],
    3703             :                      pdfXCoord[1], pdfXCoord[xdim / 2],
    3704             :                      pdfXCoord[(xdim / 2) + 1], pdfXCoord[xdim - 2],
    3705             :                      pdfXCoord[xdim - 1]);
    3706             : #endif
    3707             : 
    3708             :             // ftp://ftp.cdc.noaa.gov/Datasets/NARR/Dailies/monolevel/vwnd.10m.2015.nc
    3709             :             // requires a 0.02% tolerance, so let's settle for 0.05%
    3710             : 
    3711             :             // For float variables, increase to 0.2% (as seen in
    3712             :             // https://github.com/OSGeo/gdal/issues/3663)
    3713         204 :             const double dfEpsRel =
    3714         204 :                 nc_var_dimx_datatype == NC_FLOAT ? 0.002 : 0.0005;
    3715             : 
    3716             :             const double dfEps =
    3717             :                 dfEpsRel *
    3718         408 :                 std::max(fabs(dfSpacingBegin),
    3719         204 :                          std::max(fabs(dfSpacingMiddle), fabs(dfSpacingLast)));
    3720         402 :             if (IsDifferenceBelow(dfSpacingBegin, dfSpacingLast, dfEps) &&
    3721         402 :                 IsDifferenceBelow(dfSpacingBegin, dfSpacingMiddle, dfEps) &&
    3722         198 :                 IsDifferenceBelow(dfSpacingMiddle, dfSpacingLast, dfEps))
    3723             :             {
    3724         198 :                 bLonSpacingOK = true;
    3725             :             }
    3726           6 :             else if (CPLTestBool(CPLGetConfigOption(
    3727             :                          "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK", "NO")))
    3728             :             {
    3729           0 :                 bLonSpacingOK = true;
    3730           0 :                 CPLDebug(
    3731             :                     "GDAL_netCDF",
    3732             :                     "Longitude/X is not equally spaced, but will be considered "
    3733             :                     "as such because of "
    3734             :                     "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK");
    3735             :             }
    3736             :         }
    3737             : 
    3738         233 :         if (bLonSpacingOK == false)
    3739             :         {
    3740           6 :             CPLDebug(
    3741             :                 "GDAL_netCDF", "%s",
    3742             :                 "Longitude/X is not equally spaced (with a 0.05% tolerance). "
    3743             :                 "You may set the "
    3744             :                 "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK configuration "
    3745             :                 "option to YES to ignore this check");
    3746             :         }
    3747             : 
    3748             :         // Check Latitude.
    3749         233 :         bool bLatSpacingOK = false;
    3750             : 
    3751         233 :         if (ydim == 2)
    3752             :         {
    3753          49 :             bLatSpacingOK = true;
    3754             :         }
    3755             :         else
    3756             :         {
    3757         184 :             const double dfSpacingBegin = pdfYCoord[1] - pdfYCoord[0];
    3758         184 :             const double dfSpacingMiddle =
    3759         184 :                 pdfYCoord[ydim / 2 + 1] - pdfYCoord[ydim / 2];
    3760             : 
    3761         184 :             const double dfSpacingLast =
    3762         184 :                 pdfYCoord[ydim - 1] - pdfYCoord[ydim - 2];
    3763             : 
    3764         184 :             CPLDebug("GDAL_netCDF",
    3765             :                      "ydim: %ld dfSpacingBegin: %f dfSpacingMiddle: %f "
    3766             :                      "dfSpacingLast: %f",
    3767             :                      (long)ydim, dfSpacingBegin, dfSpacingMiddle,
    3768             :                      dfSpacingLast);
    3769             : #ifdef NCDF_DEBUG
    3770             :             CPLDebug("GDAL_netCDF", "ycoords: %f %f %f %f %f %f", pdfYCoord[0],
    3771             :                      pdfYCoord[1], pdfYCoord[ydim / 2],
    3772             :                      pdfYCoord[(ydim / 2) + 1], pdfYCoord[ydim - 2],
    3773             :                      pdfYCoord[ydim - 1]);
    3774             : #endif
    3775             : 
    3776         184 :             const double dfEpsRel =
    3777         184 :                 nc_var_dimy_datatype == NC_FLOAT ? 0.002 : 0.0005;
    3778             : 
    3779             :             const double dfEps =
    3780             :                 dfEpsRel *
    3781         368 :                 std::max(fabs(dfSpacingBegin),
    3782         184 :                          std::max(fabs(dfSpacingMiddle), fabs(dfSpacingLast)));
    3783         366 :             if (IsDifferenceBelow(dfSpacingBegin, dfSpacingLast, dfEps) &&
    3784         366 :                 IsDifferenceBelow(dfSpacingBegin, dfSpacingMiddle, dfEps) &&
    3785         173 :                 IsDifferenceBelow(dfSpacingMiddle, dfSpacingLast, dfEps))
    3786             :             {
    3787         173 :                 bLatSpacingOK = true;
    3788             :             }
    3789          11 :             else if (CPLTestBool(CPLGetConfigOption(
    3790             :                          "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK", "NO")))
    3791             :             {
    3792           0 :                 bLatSpacingOK = true;
    3793           0 :                 CPLDebug(
    3794             :                     "GDAL_netCDF",
    3795             :                     "Latitude/Y is not equally spaced, but will be considered "
    3796             :                     "as such because of "
    3797             :                     "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK");
    3798             :             }
    3799          11 :             else if (!oSRS.IsProjected() &&
    3800          11 :                      fabs(dfSpacingBegin - dfSpacingLast) <= 0.1 &&
    3801          30 :                      fabs(dfSpacingBegin - dfSpacingMiddle) <= 0.1 &&
    3802           8 :                      fabs(dfSpacingMiddle - dfSpacingLast) <= 0.1)
    3803             :             {
    3804           8 :                 bLatSpacingOK = true;
    3805           8 :                 CPLError(CE_Warning, CPLE_AppDefined,
    3806             :                          "Latitude grid not spaced evenly.  "
    3807             :                          "Setting projection for grid spacing is "
    3808             :                          "within 0.1 degrees threshold.");
    3809             : 
    3810           8 :                 CPLDebug("GDAL_netCDF",
    3811             :                          "Latitude grid not spaced evenly, but within 0.1 "
    3812             :                          "degree threshold (probably a Gaussian grid).  "
    3813             :                          "Saving original latitude values in Y_VALUES "
    3814             :                          "geolocation metadata");
    3815           8 :                 Set1DGeolocation(nGroupDimYID, nVarDimYID, "Y");
    3816             :             }
    3817             : 
    3818         184 :             if (bLatSpacingOK == false)
    3819             :             {
    3820           3 :                 CPLDebug(
    3821             :                     "GDAL_netCDF", "%s",
    3822             :                     "Latitude/Y is not equally spaced (with a 0.05% "
    3823             :                     "tolerance). "
    3824             :                     "You may set the "
    3825             :                     "GDAL_NETCDF_IGNORE_EQUALLY_SPACED_XY_CHECK configuration "
    3826             :                     "option to YES to ignore this check");
    3827             :             }
    3828             :         }
    3829             : 
    3830         233 :         if (bLonSpacingOK && bLatSpacingOK)
    3831             :         {
    3832             :             // We have gridded data so we can set the Georeferencing info.
    3833             : 
    3834             :             // Enable GeoTransform.
    3835             : 
    3836             :             // In the following "actual_range" and "node_offset"
    3837             :             // are attributes used by netCDF files created by GMT.
    3838             :             // If we find them we know how to proceed. Else, use
    3839             :             // the original algorithm.
    3840         226 :             bGotCfGT = true;
    3841             : 
    3842         226 :             int node_offset = 0;
    3843             :             const bool bUseActualRange =
    3844         226 :                 NCDFResolveAttInt(nGroupId, NC_GLOBAL, "node_offset",
    3845         226 :                                   &node_offset) == CE_None;
    3846             : 
    3847         226 :             double adfActualRange[2] = {0.0, 0.0};
    3848         226 :             double xMinMax[2] = {0.0, 0.0};
    3849         226 :             double yMinMax[2] = {0.0, 0.0};
    3850             : 
    3851             :             const auto RoundMinMaxForFloatVals =
    3852          60 :                 [](double &dfMin, double &dfMax, int nIntervals)
    3853             :             {
    3854             :                 // Helps for a case where longitudes range from
    3855             :                 // -179.99 to 180.0 with a 0.01 degree spacing.
    3856             :                 // However as this is encoded in a float array,
    3857             :                 // -179.99 is actually read as -179.99000549316406 as
    3858             :                 // a double. Try to detect that and correct the rounding
    3859             : 
    3860          88 :                 const auto IsAlmostInteger = [](double dfVal)
    3861             :                 {
    3862          88 :                     constexpr double THRESHOLD_INTEGER = 1e-3;
    3863          88 :                     return std::fabs(dfVal - std::round(dfVal)) <=
    3864          88 :                            THRESHOLD_INTEGER;
    3865             :                 };
    3866             : 
    3867          60 :                 const double dfSpacing = (dfMax - dfMin) / nIntervals;
    3868          60 :                 if (dfSpacing > 0)
    3869             :                 {
    3870          48 :                     const double dfInvSpacing = 1.0 / dfSpacing;
    3871          48 :                     if (IsAlmostInteger(dfInvSpacing))
    3872             :                     {
    3873          20 :                         const double dfRoundedSpacing =
    3874          20 :                             1.0 / std::round(dfInvSpacing);
    3875          20 :                         const double dfMinDivRoundedSpacing =
    3876          20 :                             dfMin / dfRoundedSpacing;
    3877          20 :                         const double dfMaxDivRoundedSpacing =
    3878          20 :                             dfMax / dfRoundedSpacing;
    3879          40 :                         if (IsAlmostInteger(dfMinDivRoundedSpacing) &&
    3880          20 :                             IsAlmostInteger(dfMaxDivRoundedSpacing))
    3881             :                         {
    3882          20 :                             const double dfRoundedMin =
    3883          20 :                                 std::round(dfMinDivRoundedSpacing) *
    3884             :                                 dfRoundedSpacing;
    3885          20 :                             const double dfRoundedMax =
    3886          20 :                                 std::round(dfMaxDivRoundedSpacing) *
    3887             :                                 dfRoundedSpacing;
    3888          20 :                             if (static_cast<float>(dfMin) ==
    3889          20 :                                     static_cast<float>(dfRoundedMin) &&
    3890           8 :                                 static_cast<float>(dfMax) ==
    3891           8 :                                     static_cast<float>(dfRoundedMax))
    3892             :                             {
    3893           7 :                                 dfMin = dfRoundedMin;
    3894           7 :                                 dfMax = dfRoundedMax;
    3895             :                             }
    3896             :                         }
    3897             :                     }
    3898             :                 }
    3899          60 :             };
    3900             : 
    3901         229 :             if (bUseActualRange &&
    3902           3 :                 !nc_get_att_double(nGroupDimXID, nVarDimXID, "actual_range",
    3903             :                                    adfActualRange))
    3904             :             {
    3905           1 :                 xMinMax[0] = adfActualRange[0];
    3906           1 :                 xMinMax[1] = adfActualRange[1];
    3907             : 
    3908             :                 // Present xMinMax[] in the same order as padfXCoord
    3909           1 :                 if ((xMinMax[0] - xMinMax[1]) *
    3910           1 :                         (pdfXCoord[0] - pdfXCoord[xdim - 1]) <
    3911             :                     0)
    3912             :                 {
    3913           0 :                     std::swap(xMinMax[0], xMinMax[1]);
    3914             :                 }
    3915             :             }
    3916             :             else
    3917             :             {
    3918         225 :                 xMinMax[0] = pdfXCoord[0];
    3919         225 :                 xMinMax[1] = pdfXCoord[xdim - 1];
    3920         225 :                 node_offset = 0;
    3921             : 
    3922         225 :                 if (nc_var_dimx_datatype == NC_FLOAT)
    3923             :                 {
    3924          30 :                     RoundMinMaxForFloatVals(xMinMax[0], xMinMax[1],
    3925          30 :                                             poDS->nRasterXSize - 1);
    3926             :                 }
    3927             :             }
    3928             : 
    3929         229 :             if (bUseActualRange &&
    3930           3 :                 !nc_get_att_double(nGroupDimYID, nVarDimYID, "actual_range",
    3931             :                                    adfActualRange))
    3932             :             {
    3933           1 :                 yMinMax[0] = adfActualRange[0];
    3934           1 :                 yMinMax[1] = adfActualRange[1];
    3935             : 
    3936             :                 // Present yMinMax[] in the same order as pdfYCoord
    3937           1 :                 if ((yMinMax[0] - yMinMax[1]) *
    3938           1 :                         (pdfYCoord[0] - pdfYCoord[ydim - 1]) <
    3939             :                     0)
    3940             :                 {
    3941           0 :                     std::swap(yMinMax[0], yMinMax[1]);
    3942             :                 }
    3943             :             }
    3944             :             else
    3945             :             {
    3946         225 :                 yMinMax[0] = pdfYCoord[0];
    3947         225 :                 yMinMax[1] = pdfYCoord[ydim - 1];
    3948         225 :                 node_offset = 0;
    3949             : 
    3950         225 :                 if (nc_var_dimy_datatype == NC_FLOAT)
    3951             :                 {
    3952          30 :                     RoundMinMaxForFloatVals(yMinMax[0], yMinMax[1],
    3953          30 :                                             poDS->nRasterYSize - 1);
    3954             :                 }
    3955             :             }
    3956             : 
    3957         226 :             double dfCoordOffset = 0.0;
    3958         226 :             double dfCoordScale = 1.0;
    3959         226 :             if (!nc_get_att_double(nGroupId, nVarDimXID, CF_ADD_OFFSET,
    3960         230 :                                    &dfCoordOffset) &&
    3961           4 :                 !nc_get_att_double(nGroupId, nVarDimXID, CF_SCALE_FACTOR,
    3962             :                                    &dfCoordScale))
    3963             :             {
    3964           4 :                 xMinMax[0] = dfCoordOffset + xMinMax[0] * dfCoordScale;
    3965           4 :                 xMinMax[1] = dfCoordOffset + xMinMax[1] * dfCoordScale;
    3966             :             }
    3967             : 
    3968         226 :             if (!nc_get_att_double(nGroupId, nVarDimYID, CF_ADD_OFFSET,
    3969         230 :                                    &dfCoordOffset) &&
    3970           4 :                 !nc_get_att_double(nGroupId, nVarDimYID, CF_SCALE_FACTOR,
    3971             :                                    &dfCoordScale))
    3972             :             {
    3973           4 :                 yMinMax[0] = dfCoordOffset + yMinMax[0] * dfCoordScale;
    3974           4 :                 yMinMax[1] = dfCoordOffset + yMinMax[1] * dfCoordScale;
    3975             :             }
    3976             : 
    3977             :             // Check for reverse order of y-coordinate.
    3978         226 :             if (!bSwitchedXY)
    3979             :             {
    3980         224 :                 poDS->bBottomUp = (yMinMax[0] <= yMinMax[1]);
    3981         224 :                 if (!poDS->bBottomUp)
    3982             :                 {
    3983          34 :                     std::swap(yMinMax[0], yMinMax[1]);
    3984             :                 }
    3985             :             }
    3986             : 
    3987             :             // Geostationary satellites can specify units in (micro)radians
    3988             :             // So we check if they do, and if so convert to linear units
    3989             :             // (meters)
    3990         226 :             const char *pszProjName = oSRS.GetAttrValue("PROJECTION");
    3991         226 :             if (pszProjName != nullptr)
    3992             :             {
    3993          24 :                 if (EQUAL(pszProjName, SRS_PT_GEOSTATIONARY_SATELLITE))
    3994             :                 {
    3995             :                     const double satelliteHeight =
    3996           3 :                         oSRS.GetProjParm(SRS_PP_SATELLITE_HEIGHT, 1.0);
    3997           6 :                     std::string osUnits;
    3998           3 :                     if (NCDFGetAttr(nGroupId, nVarDimXID, "units", osUnits) ==
    3999             :                         CE_None)
    4000             :                     {
    4001           3 :                         if (EQUAL(osUnits.c_str(), "microradian"))
    4002             :                         {
    4003           1 :                             xMinMax[0] =
    4004           1 :                                 xMinMax[0] * satelliteHeight * 0.000001;
    4005           1 :                             xMinMax[1] =
    4006           1 :                                 xMinMax[1] * satelliteHeight * 0.000001;
    4007             :                         }
    4008           3 :                         else if (EQUAL(osUnits.c_str(), "rad") ||
    4009           1 :                                  EQUAL(osUnits.c_str(), "radian"))
    4010             :                         {
    4011           2 :                             xMinMax[0] = xMinMax[0] * satelliteHeight;
    4012           2 :                             xMinMax[1] = xMinMax[1] * satelliteHeight;
    4013             :                         }
    4014             :                     }
    4015           3 :                     if (NCDFGetAttr(nGroupId, nVarDimYID, "units", osUnits) ==
    4016             :                         CE_None)
    4017             :                     {
    4018           3 :                         if (EQUAL(osUnits.c_str(), "microradian"))
    4019             :                         {
    4020           1 :                             yMinMax[0] =
    4021           1 :                                 yMinMax[0] * satelliteHeight * 0.000001;
    4022           1 :                             yMinMax[1] =
    4023           1 :                                 yMinMax[1] * satelliteHeight * 0.000001;
    4024             :                         }
    4025           3 :                         else if (EQUAL(osUnits.c_str(), "rad") ||
    4026           1 :                                  EQUAL(osUnits.c_str(), "radian"))
    4027             :                         {
    4028           2 :                             yMinMax[0] = yMinMax[0] * satelliteHeight;
    4029           2 :                             yMinMax[1] = yMinMax[1] * satelliteHeight;
    4030             :                         }
    4031             :                     }
    4032             :                 }
    4033             :             }
    4034             : 
    4035         226 :             tmpGT[0] = xMinMax[0];
    4036         452 :             tmpGT[1] = (xMinMax[1] - xMinMax[0]) /
    4037         226 :                        (poDS->nRasterXSize + (node_offset - 1));
    4038         226 :             tmpGT[2] = 0;
    4039         226 :             if (bSwitchedXY)
    4040             :             {
    4041           2 :                 tmpGT[3] = yMinMax[0];
    4042           2 :                 tmpGT[4] = 0;
    4043           2 :                 tmpGT[5] = (yMinMax[1] - yMinMax[0]) /
    4044           2 :                            (poDS->nRasterYSize + (node_offset - 1));
    4045             :             }
    4046             :             else
    4047             :             {
    4048         224 :                 tmpGT[3] = yMinMax[1];
    4049         224 :                 tmpGT[4] = 0;
    4050         224 :                 tmpGT[5] = (yMinMax[0] - yMinMax[1]) /
    4051         224 :                            (poDS->nRasterYSize + (node_offset - 1));
    4052             :             }
    4053             : 
    4054             :             // Compute the center of the pixel.
    4055         226 :             if (!node_offset)
    4056             :             {
    4057             :                 // Otherwise its already the pixel center.
    4058         226 :                 tmpGT[0] -= (tmpGT[1] / 2);
    4059         226 :                 tmpGT[3] -= (tmpGT[5] / 2);
    4060             :             }
    4061             :         }
    4062             : 
    4063             :         const auto AreSRSEqualThroughProj4String =
    4064           2 :             [](const OGRSpatialReference &oSRS1,
    4065             :                const OGRSpatialReference &oSRS2)
    4066             :         {
    4067           2 :             char *pszProj4Str1 = nullptr;
    4068           2 :             oSRS1.exportToProj4(&pszProj4Str1);
    4069             : 
    4070           2 :             char *pszProj4Str2 = nullptr;
    4071           2 :             oSRS2.exportToProj4(&pszProj4Str2);
    4072             : 
    4073             :             {
    4074           2 :                 char *pszTmp = strstr(pszProj4Str1, "+datum=");
    4075           2 :                 if (pszTmp)
    4076           0 :                     memcpy(pszTmp, "+ellps=", strlen("+ellps="));
    4077             :             }
    4078             : 
    4079             :             {
    4080           2 :                 char *pszTmp = strstr(pszProj4Str2, "+datum=");
    4081           2 :                 if (pszTmp)
    4082           2 :                     memcpy(pszTmp, "+ellps=", strlen("+ellps="));
    4083             :             }
    4084             : 
    4085           2 :             bool bRet = false;
    4086           2 :             if (pszProj4Str1 && pszProj4Str2 &&
    4087           2 :                 EQUAL(pszProj4Str1, pszProj4Str2))
    4088             :             {
    4089           1 :                 bRet = true;
    4090             :             }
    4091             : 
    4092           2 :             CPLFree(pszProj4Str1);
    4093           2 :             CPLFree(pszProj4Str2);
    4094           2 :             return bRet;
    4095             :         };
    4096             : 
    4097         233 :         if (dfLinearUnitsConvFactor != 1.0)
    4098             :         {
    4099          35 :             for (int i = 0; i < 6; ++i)
    4100          30 :                 tmpGT[i] *= dfLinearUnitsConvFactor;
    4101             : 
    4102           5 :             if (paosRemovedMDItems)
    4103             :             {
    4104             :                 char szVarNameX[NC_MAX_NAME + 1];
    4105           5 :                 CPL_IGNORE_RET_VAL(
    4106           5 :                     nc_inq_varname(nGroupId, nVarDimXID, szVarNameX));
    4107             : 
    4108             :                 char szVarNameY[NC_MAX_NAME + 1];
    4109           5 :                 CPL_IGNORE_RET_VAL(
    4110           5 :                     nc_inq_varname(nGroupId, nVarDimYID, szVarNameY));
    4111             : 
    4112           5 :                 paosRemovedMDItems->push_back(
    4113             :                     CPLSPrintf("%s#units", szVarNameX));
    4114           5 :                 paosRemovedMDItems->push_back(
    4115             :                     CPLSPrintf("%s#units", szVarNameY));
    4116             :             }
    4117             :         }
    4118             : 
    4119             :         // If there is a global "geospatial_bounds_crs" attribute, check that it
    4120             :         // is consistent with the SRS, and if so, use it as the SRS
    4121             :         const char *pszGBCRS =
    4122         233 :             FetchAttr(nGroupId, NC_GLOBAL, "geospatial_bounds_crs");
    4123         233 :         if (pszGBCRS && STARTS_WITH(pszGBCRS, "EPSG:"))
    4124             :         {
    4125           4 :             OGRSpatialReference oSRSFromGBCRS;
    4126           2 :             oSRSFromGBCRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    4127           2 :             if (oSRSFromGBCRS.SetFromUserInput(
    4128             :                     pszGBCRS,
    4129             :                     OGRSpatialReference::
    4130           4 :                         SET_FROM_USER_INPUT_LIMITATIONS_get()) == OGRERR_NONE &&
    4131           2 :                 AreSRSEqualThroughProj4String(oSRS, oSRSFromGBCRS))
    4132             :             {
    4133           1 :                 oSRS = std::move(oSRSFromGBCRS);
    4134           1 :                 SetSpatialRefNoUpdate(&oSRS);
    4135             :             }
    4136             :         }
    4137             : 
    4138         233 :         CPLFree(pdfXCoord);
    4139         233 :         CPLFree(pdfYCoord);
    4140             :     }  // end if(has dims)
    4141             : 
    4142             :     // Process custom GeoTransform GDAL value.
    4143         627 :     if (!osGridMappingValue.empty())
    4144             :     {
    4145         256 :         if (pszGeoTransform != nullptr)
    4146             :         {
    4147             :             const CPLStringList aosGeoTransform(
    4148         286 :                 CSLTokenizeString2(pszGeoTransform, " ", CSLT_HONOURSTRINGS));
    4149         143 :             if (aosGeoTransform.size() == 6)
    4150             :             {
    4151         143 :                 bool bUseGeoTransformFromAttribute = true;
    4152             : 
    4153         143 :                 GDALGeoTransform gtFromAttribute;
    4154        1001 :                 for (int i = 0; i < 6; i++)
    4155             :                 {
    4156         858 :                     gtFromAttribute[i] = CPLAtof(aosGeoTransform[i]);
    4157             :                 }
    4158             : 
    4159             :                 // When GDAL writes a raster that is north-up oriented, it
    4160             :                 // writes the "GeoTransform" attribute unmodified, that is with
    4161             :                 // gt.yscale < 0, but the first line is actually the southern-most
    4162             :                 // one, consistently with the values of the "y" coordinate
    4163             :                 // variable. This is wrong... but we have always done that, so
    4164             :                 // this is hard to fix now.
    4165             :                 // However there are datasets like
    4166             :                 // https://public.hub.geosphere.at/datahub/resources/spartacus-v2-1d-1km/filelisting/TN/SPARTACUS2-DAILY_TN_2026.nc
    4167             :                 // that correctly use a positive gt.yscale value. So make sure to not emit
    4168             :                 // a warning when comparing against the geotransform derived from
    4169             :                 // the x/y coordinates.
    4170         143 :                 GDALGeoTransform gtFromAttributeNorthUp = gtFromAttribute;
    4171         148 :                 if (gtFromAttributeNorthUp.yscale > 0 &&
    4172           5 :                     gtFromAttributeNorthUp.IsAxisAligned())
    4173             :                 {
    4174           1 :                     gtFromAttributeNorthUp.yorig +=
    4175           1 :                         poDS->nRasterYSize * gtFromAttributeNorthUp.yscale;
    4176           1 :                     gtFromAttributeNorthUp.yscale =
    4177           1 :                         -gtFromAttributeNorthUp.yscale;
    4178             :                 }
    4179             : 
    4180         143 :                 if (bGotCfGT)
    4181             :                 {
    4182          99 :                     constexpr double GT_RELERROR_WARN_THRESHOLD = 1e-6;
    4183          99 :                     double dfMaxAbsoluteError = 0.0;
    4184         693 :                     for (int i = 0; i < 6; i++)
    4185             :                     {
    4186             :                         double dfAbsoluteError =
    4187         594 :                             std::abs(tmpGT[i] - gtFromAttributeNorthUp[i]);
    4188         594 :                         if (dfAbsoluteError >
    4189         594 :                             std::abs(gtFromAttributeNorthUp[i] *
    4190             :                                      GT_RELERROR_WARN_THRESHOLD))
    4191             :                         {
    4192           3 :                             dfMaxAbsoluteError =
    4193           3 :                                 std::max(dfMaxAbsoluteError, dfAbsoluteError);
    4194             :                         }
    4195             :                     }
    4196             : 
    4197          99 :                     if (dfMaxAbsoluteError > 0)
    4198             :                     {
    4199           3 :                         bUseGeoTransformFromAttribute = false;
    4200           3 :                         CPLError(CE_Warning, CPLE_AppDefined,
    4201             :                                  "GeoTransform read from attribute of %s "
    4202             :                                  "variable differs from value calculated from "
    4203             :                                  "dimension variables (max diff = %g). Using "
    4204             :                                  "value calculated from dimension variables.",
    4205             :                                  osGridMappingValue.c_str(),
    4206             :                                  dfMaxAbsoluteError);
    4207             :                     }
    4208             :                 }
    4209             : 
    4210         143 :                 if (bUseGeoTransformFromAttribute)
    4211             :                 {
    4212         140 :                     if (bGotCfGT)
    4213             :                     {
    4214          96 :                         tmpGT = gtFromAttributeNorthUp;
    4215          96 :                         if (gtFromAttributeNorthUp.IsAxisAligned())
    4216             :                         {
    4217             :                             // Axis direction depends on whether it is GDAL-style CF file
    4218          96 :                             poDS->bBottomUp = bIsGdalCfFile;
    4219             :                         }
    4220             :                     }
    4221             :                     else
    4222             :                     {
    4223          44 :                         tmpGT = gtFromAttribute;
    4224             :                     }
    4225         140 :                     bGotGdalGT = true;
    4226             :                 }
    4227             :             }
    4228             :         }
    4229             :         else
    4230             :         {
    4231             :             // Look for corner array values.
    4232             :             // CPLDebug("GDAL_netCDF",
    4233             :             //           "looking for geotransform corners");
    4234         113 :             bool bGotNN = false;
    4235         113 :             double dfNN = FetchCopyParam(osGridMappingValue.c_str(),
    4236             :                                          "Northernmost_Northing", 0, &bGotNN);
    4237             : 
    4238         113 :             bool bGotSN = false;
    4239         113 :             double dfSN = FetchCopyParam(osGridMappingValue.c_str(),
    4240             :                                          "Southernmost_Northing", 0, &bGotSN);
    4241             : 
    4242         113 :             bool bGotEE = false;
    4243         113 :             double dfEE = FetchCopyParam(osGridMappingValue.c_str(),
    4244             :                                          "Easternmost_Easting", 0, &bGotEE);
    4245             : 
    4246         113 :             bool bGotWE = false;
    4247         113 :             double dfWE = FetchCopyParam(osGridMappingValue.c_str(),
    4248             :                                          "Westernmost_Easting", 0, &bGotWE);
    4249             : 
    4250             :             // Only set the GeoTransform if we got all the values.
    4251         113 :             if (bGotNN && bGotSN && bGotEE && bGotWE)
    4252             :             {
    4253           0 :                 bGotGdalGT = true;
    4254             : 
    4255           0 :                 tmpGT[0] = dfWE;
    4256           0 :                 tmpGT[1] = (dfEE - dfWE) / (poDS->GetRasterXSize() - 1);
    4257           0 :                 tmpGT[2] = 0.0;
    4258           0 :                 tmpGT[3] = dfNN;
    4259           0 :                 tmpGT[4] = 0.0;
    4260           0 :                 tmpGT[5] = (dfSN - dfNN) / (poDS->GetRasterYSize() - 1);
    4261             :                 // Compute the center of the pixel.
    4262           0 :                 tmpGT[0] = dfWE - (tmpGT[1] / 2);
    4263           0 :                 tmpGT[3] = dfNN - (tmpGT[5] / 2);
    4264             :             }
    4265             :         }  // (pszGeoTransform != NULL)
    4266             : 
    4267         256 :         if (bGotGdalSRS && !bGotGdalGT)
    4268          78 :             CPLDebug("GDAL_netCDF", "Got SRS but no geotransform from GDAL!");
    4269             :     }
    4270             : 
    4271         627 :     if (bGotCfGT || bGotGdalGT)
    4272             :     {
    4273         270 :         CPLDebug("GDAL_netCDF", "set bBottomUp = %d from Y axis",
    4274         270 :                  static_cast<int>(poDS->bBottomUp));
    4275             :     }
    4276             : 
    4277         627 :     if (!pszWKT && !bGotCfSRS)
    4278             :     {
    4279             :         // Some netCDF files have a srid attribute (#6613) like
    4280             :         // urn:ogc:def:crs:EPSG::6931
    4281         372 :         const char *pszSRID = FetchAttr(osGridMappingValue.c_str(), "srid");
    4282         372 :         if (pszSRID != nullptr)
    4283             :         {
    4284           0 :             oSRS.Clear();
    4285           0 :             if (oSRS.SetFromUserInput(
    4286             :                     pszSRID,
    4287             :                     OGRSpatialReference::
    4288           0 :                         SET_FROM_USER_INPUT_LIMITATIONS_get()) == OGRERR_NONE)
    4289             :             {
    4290           0 :                 CPLDebug("GDAL_netCDF", "Got SRS from %s", pszSRID);
    4291           0 :                 std::string osWKTExport = oSRS.exportToWkt();
    4292           0 :                 if (!osWKTExport.empty())
    4293             :                 {
    4294           0 :                     (*returnProjStr) = std::move(osWKTExport);
    4295             :                 }
    4296             :                 else
    4297             :                 {
    4298           0 :                     m_bAddedProjectionVarsDefs = true;
    4299           0 :                     m_bAddedProjectionVarsData = true;
    4300           0 :                     SetSpatialRefNoUpdate(&oSRS);
    4301             :                 }
    4302             :             }
    4303             :         }
    4304             :     }
    4305             : 
    4306         627 :     if (bReadSRSOnly)
    4307         257 :         return;
    4308             : 
    4309             :     // Determines the SRS to be used by the geolocation array, if any
    4310         740 :     std::string osGeolocWKT = SRS_WKT_WGS84_LAT_LONG;
    4311         370 :     if (!m_oSRS.IsEmpty())
    4312             :     {
    4313         292 :         OGRSpatialReference oGeogCRS;
    4314         146 :         oGeogCRS.CopyGeogCSFrom(&m_oSRS, true);
    4315         146 :         const char *const apszOptions[] = {"FORMAT=WKT2_2019", nullptr};
    4316             : 
    4317         292 :         std::string osWKTTmp = oGeogCRS.exportToWkt(apszOptions);
    4318         146 :         if (!osWKTTmp.empty())
    4319             :         {
    4320         146 :             osGeolocWKT = std::move(osWKTTmp);
    4321             :         }
    4322             :     }
    4323             : 
    4324             :     // Process geolocation arrays from CF "coordinates" attribute.
    4325         740 :     std::string osGeolocXName, osGeolocYName;
    4326         370 :     if (ProcessCFGeolocation(nGroupId, nVarId, osGeolocWKT, osGeolocXName,
    4327         370 :                              osGeolocYName))
    4328             :     {
    4329          62 :         bool bCanCancelGT = true;
    4330          62 :         if ((nVarDimXID != -1) && (nVarDimYID != -1))
    4331             :         {
    4332             :             char szVarNameX[NC_MAX_NAME + 1];
    4333          45 :             CPL_IGNORE_RET_VAL(
    4334          45 :                 nc_inq_varname(nGroupId, nVarDimXID, szVarNameX));
    4335             :             char szVarNameY[NC_MAX_NAME + 1];
    4336          45 :             CPL_IGNORE_RET_VAL(
    4337          45 :                 nc_inq_varname(nGroupId, nVarDimYID, szVarNameY));
    4338          45 :             bCanCancelGT =
    4339          45 :                 !(osGeolocXName == szVarNameX && osGeolocYName == szVarNameY);
    4340             :         }
    4341         101 :         if (bCanCancelGT && !m_oSRS.IsGeographic() && !m_oSRS.IsProjected() &&
    4342          39 :             !bSwitchedXY)
    4343             :         {
    4344          37 :             bGotCfGT = false;
    4345             :         }
    4346             :     }
    4347         123 :     else if (!bGotCfGT && !bReadSRSOnly && (nVarDimXID != -1) &&
    4348         434 :              (nVarDimYID != -1) && xdim > 0 && ydim > 0 &&
    4349           3 :              ((!bSwitchedXY &&
    4350           3 :                NCDFIsVarLongitude(nGroupId, nVarDimXID, nullptr) &&
    4351           1 :                NCDFIsVarLatitude(nGroupId, nVarDimYID, nullptr)) ||
    4352           2 :               (bSwitchedXY &&
    4353           0 :                NCDFIsVarLongitude(nGroupId, nVarDimYID, nullptr) &&
    4354           0 :                NCDFIsVarLatitude(nGroupId, nVarDimXID, nullptr))))
    4355             :     {
    4356             :         // Case of autotest/gdrivers/data/netcdf/GLMELT_4X5.OCN.nc
    4357             :         // which is indexed by lat, lon variables, but lat has irregular
    4358             :         // spacing.
    4359           1 :         const char *pszGeolocXFullName = poDS->papszDimName[poDS->nXDimID];
    4360           1 :         const char *pszGeolocYFullName = poDS->papszDimName[poDS->nYDimID];
    4361           1 :         if (bSwitchedXY)
    4362             :         {
    4363           0 :             std::swap(pszGeolocXFullName, pszGeolocYFullName);
    4364           0 :             GDALPamDataset::SetMetadataItem("SWAP_XY", "YES",
    4365             :                                             GDAL_MDD_GEOLOCATION);
    4366             :         }
    4367             : 
    4368           1 :         CPLDebug("GDAL_netCDF", "using variables %s and %s for GEOLOCATION",
    4369             :                  pszGeolocXFullName, pszGeolocYFullName);
    4370             : 
    4371           1 :         GDALPamDataset::SetMetadataItem("SRS", osGeolocWKT.c_str(),
    4372             :                                         GDAL_MDD_GEOLOCATION);
    4373             : 
    4374           2 :         CPLString osTMP;
    4375           1 :         osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4376           1 :                      pszGeolocXFullName);
    4377             : 
    4378           1 :         GDALPamDataset::SetMetadataItem("X_DATASET", osTMP,
    4379             :                                         GDAL_MDD_GEOLOCATION);
    4380           1 :         GDALPamDataset::SetMetadataItem("X_BAND", "1", GDAL_MDD_GEOLOCATION);
    4381           1 :         osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4382           1 :                      pszGeolocYFullName);
    4383             : 
    4384           1 :         GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP,
    4385             :                                         GDAL_MDD_GEOLOCATION);
    4386           1 :         GDALPamDataset::SetMetadataItem("Y_BAND", "1", GDAL_MDD_GEOLOCATION);
    4387             : 
    4388           1 :         GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0",
    4389             :                                         GDAL_MDD_GEOLOCATION);
    4390           1 :         GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1",
    4391             :                                         GDAL_MDD_GEOLOCATION);
    4392             : 
    4393           1 :         GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0",
    4394             :                                         GDAL_MDD_GEOLOCATION);
    4395           1 :         GDALPamDataset::SetMetadataItem("LINE_STEP", "1", GDAL_MDD_GEOLOCATION);
    4396             : 
    4397           1 :         GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION",
    4398             :                                         "PIXEL_CENTER", GDAL_MDD_GEOLOCATION);
    4399             :     }
    4400             : 
    4401             :     // Set GeoTransform if we got a complete one - after projection has been set
    4402         370 :     if (bGotCfGT || bGotGdalGT)
    4403             :     {
    4404         216 :         m_bAddedProjectionVarsDefs = true;
    4405         216 :         m_bAddedProjectionVarsData = true;
    4406         216 :         SetGeoTransformNoUpdate(tmpGT);
    4407             :     }
    4408             : 
    4409             :     // Debugging reports.
    4410         370 :     CPLDebug("GDAL_netCDF",
    4411             :              "bGotGeogCS=%d bGotCfSRS=%d bGotCfGT=%d bGotCfWktSRS=%d "
    4412             :              "bGotGdalSRS=%d bGotGdalGT=%d",
    4413             :              static_cast<int>(bGotGeogCS), static_cast<int>(bGotCfSRS),
    4414             :              static_cast<int>(bGotCfGT), static_cast<int>(bGotCfWktSRS),
    4415             :              static_cast<int>(bGotGdalSRS), static_cast<int>(bGotGdalGT));
    4416             : 
    4417         370 :     if (!bGotCfGT && !bGotGdalGT)
    4418         154 :         CPLDebug("GDAL_netCDF", "did not get geotransform from CF nor GDAL!");
    4419             : 
    4420         370 :     if (!bGotGeogCS && !bGotCfSRS && !bGotGdalSRS && !bGotCfGT && !bGotCfWktSRS)
    4421         154 :         CPLDebug("GDAL_netCDF", "did not get projection from CF nor GDAL!");
    4422             : 
    4423             :     // wish of 6195
    4424             :     // we don't have CS/SRS, but we do have GT, and we live in -180,360 -90,90
    4425         370 :     if (!bGotGeogCS && !bGotCfSRS && !bGotGdalSRS && !bGotCfWktSRS)
    4426             :     {
    4427         224 :         if (bGotCfGT || bGotGdalGT)
    4428             :         {
    4429         140 :             bool bAssumedLongLat = CPLTestBool(CSLFetchNameValueDef(
    4430          70 :                 papszOpenOptions, "ASSUME_LONGLAT",
    4431             :                 CPLGetConfigOption("GDAL_NETCDF_ASSUME_LONGLAT", "NO")));
    4432             : 
    4433           2 :             if (bAssumedLongLat && tmpGT[0] >= -180 && tmpGT[0] < 360 &&
    4434           2 :                 (tmpGT[0] + tmpGT[1] * poDS->GetRasterXSize()) <= 360 &&
    4435          74 :                 tmpGT[3] <= 90 && tmpGT[3] > -90 &&
    4436           2 :                 (tmpGT[3] + tmpGT[5] * poDS->GetRasterYSize()) >= -90)
    4437             :             {
    4438             : 
    4439           2 :                 poDS->bIsGeographic = true;
    4440             :                 // seems odd to use 4326 so OGC:CRS84
    4441           2 :                 oSRS.SetFromUserInput("OGC:CRS84");
    4442           2 :                 if (returnProjStr != nullptr)
    4443             :                 {
    4444           0 :                     *returnProjStr = oSRS.exportToWkt();
    4445             :                 }
    4446             :                 else
    4447             :                 {
    4448           2 :                     m_bAddedProjectionVarsDefs = true;
    4449           2 :                     m_bAddedProjectionVarsData = true;
    4450           2 :                     SetSpatialRefNoUpdate(&oSRS);
    4451             :                 }
    4452             : 
    4453           2 :                 CPLDebug("netCDF",
    4454             :                          "Assumed Longitude Latitude CRS 'OGC:CRS84' because "
    4455             :                          "none otherwise available and geotransform within "
    4456             :                          "suitable bounds. "
    4457             :                          "Set GDAL_NETCDF_ASSUME_LONGLAT=NO as configuration "
    4458             :                          "option or "
    4459             :                          "    ASSUME_LONGLAT=NO as open option to bypass this "
    4460             :                          "assumption.");
    4461             :             }
    4462             :         }
    4463             :     }
    4464             : 
    4465             : // Search for Well-known GeogCS if got only CF WKT
    4466             : // Disabled for now, as a named datum also include control points
    4467             : // (see mailing list and bug#4281
    4468             : // For example, WGS84 vs. GDA94 (EPSG:3577) - AEA in netcdf_cf.py
    4469             : 
    4470             : // Disabled for now, but could be set in a config option.
    4471             : #if 0
    4472             :     bool bLookForWellKnownGCS = false;  // This could be a Config Option.
    4473             : 
    4474             :     if( bLookForWellKnownGCS && bGotCfSRS && !bGotGdalSRS )
    4475             :     {
    4476             :         // ET - Could use a more exhaustive method by scanning all EPSG codes in
    4477             :         // data/gcs.csv as proposed by Even in the gdal-dev mailing list "help
    4478             :         // for comparing two WKT".
    4479             :         // This code could be contributed to a new function.
    4480             :         // OGRSpatialReference * OGRSpatialReference::FindMatchingGeogCS(
    4481             :         //     const OGRSpatialReference *poOther) */
    4482             :         CPLDebug("GDAL_netCDF", "Searching for Well-known GeogCS");
    4483             :         const char *pszWKGCSList[] = { "WGS84", "WGS72", "NAD27", "NAD83" };
    4484             :         char *pszWKGCS = NULL;
    4485             :         oSRS.exportToPrettyWkt(&pszWKGCS);
    4486             :         for( size_t i = 0; i < sizeof(pszWKGCSList) / 8; i++ )
    4487             :         {
    4488             :             pszWKGCS = CPLStrdup(pszWKGCSList[i]);
    4489             :             OGRSpatialReference oSRSTmp;
    4490             :             oSRSTmp.SetWellKnownGeogCS(pszWKGCSList[i]);
    4491             :             // Set datum to unknown, bug #4281.
    4492             :             if( oSRSTmp.GetAttrNode("DATUM" ) )
    4493             :                 oSRSTmp.GetAttrNode("DATUM")->GetChild(0)->SetValue("unknown");
    4494             :             // Could use OGRSpatialReference::StripCTParms(), but let's keep
    4495             :             // TOWGS84.
    4496             :             oSRSTmp.GetRoot()->StripNodes("AXIS");
    4497             :             oSRSTmp.GetRoot()->StripNodes("AUTHORITY");
    4498             :             oSRSTmp.GetRoot()->StripNodes("EXTENSION");
    4499             : 
    4500             :             oSRSTmp.exportToPrettyWkt(&pszWKGCS);
    4501             :             if( oSRS.IsSameGeogCS(&oSRSTmp) )
    4502             :             {
    4503             :                 oSRS.SetWellKnownGeogCS(pszWKGCSList[i]);
    4504             :                 oSRS.exportToWkt(&(pszTempProjection));
    4505             :                 SetProjection(pszTempProjection);
    4506             :                 CPLFree(pszTempProjection);
    4507             :             }
    4508             :         }
    4509             :     }
    4510             : #endif
    4511             : }
    4512             : 
    4513         213 : void netCDFDataset::SetProjectionFromVar(int nGroupId, int nVarId,
    4514             :                                          bool bReadSRSOnly)
    4515             : {
    4516         213 :     SetProjectionFromVar(nGroupId, nVarId, bReadSRSOnly, nullptr, nullptr,
    4517             :                          nullptr, nullptr);
    4518         213 : }
    4519             : 
    4520         296 : bool netCDFDataset::ProcessNASAL2OceanGeoLocation(int nGroupId, int nVarId)
    4521             : {
    4522             :     // Cf https://oceancolor.gsfc.nasa.gov/docs/format/l2nc/
    4523             :     // and https://github.com/OSGeo/gdal/issues/7605
    4524             : 
    4525             :     // Check for a structure like:
    4526             :     /* netcdf SNPP_VIIRS.20230406T024200.L2.OC.NRT {
    4527             :         dimensions:
    4528             :             number_of_lines = 3248 ;
    4529             :             pixels_per_line = 3200 ;
    4530             :             [...]
    4531             :             pixel_control_points = 3200 ;
    4532             :         [...]
    4533             :         group: geophysical_data {
    4534             :           variables:
    4535             :             short aot_862(number_of_lines, pixels_per_line) ;  <-- nVarId
    4536             :                 [...]
    4537             :         }
    4538             :         group: navigation_data {
    4539             :           variables:
    4540             :             float longitude(number_of_lines, pixel_control_points) ;
    4541             :                 [...]
    4542             :             float latitude(number_of_lines, pixel_control_points) ;
    4543             :                 [...]
    4544             :         }
    4545             :     }
    4546             :     */
    4547             :     // Note that the longitude and latitude arrays are not indexed by the
    4548             :     // same dimensions. Handle only the case where
    4549             :     // pixel_control_points == pixels_per_line
    4550             :     // If there was a subsampling of the geolocation arrays, we'd need to
    4551             :     // add more logic.
    4552             : 
    4553         592 :     std::string osGroupName;
    4554         296 :     osGroupName.resize(NC_MAX_NAME);
    4555         296 :     NCDF_ERR(nc_inq_grpname(nGroupId, &osGroupName[0]));
    4556         296 :     osGroupName.resize(strlen(osGroupName.data()));
    4557         296 :     if (osGroupName != "geophysical_data")
    4558         295 :         return false;
    4559             : 
    4560           1 :     int nVarDims = 0;
    4561           1 :     NCDF_ERR(nc_inq_varndims(nGroupId, nVarId, &nVarDims));
    4562           1 :     if (nVarDims != 2)
    4563           0 :         return false;
    4564             : 
    4565           1 :     int nNavigationDataGrpId = 0;
    4566           1 :     if (nc_inq_grp_ncid(cdfid, "navigation_data", &nNavigationDataGrpId) !=
    4567             :         NC_NOERR)
    4568           0 :         return false;
    4569             : 
    4570             :     std::array<int, 2> anVarDimIds;
    4571           1 :     NCDF_ERR(nc_inq_vardimid(nGroupId, nVarId, anVarDimIds.data()));
    4572             : 
    4573           1 :     int nLongitudeId = 0;
    4574           1 :     int nLatitudeId = 0;
    4575           1 :     if (nc_inq_varid(nNavigationDataGrpId, "longitude", &nLongitudeId) !=
    4576           2 :             NC_NOERR ||
    4577           1 :         nc_inq_varid(nNavigationDataGrpId, "latitude", &nLatitudeId) !=
    4578             :             NC_NOERR)
    4579             :     {
    4580           0 :         return false;
    4581             :     }
    4582             : 
    4583           1 :     int nDimsLongitude = 0;
    4584           1 :     NCDF_ERR(
    4585             :         nc_inq_varndims(nNavigationDataGrpId, nLongitudeId, &nDimsLongitude));
    4586           1 :     int nDimsLatitude = 0;
    4587           1 :     NCDF_ERR(
    4588             :         nc_inq_varndims(nNavigationDataGrpId, nLatitudeId, &nDimsLatitude));
    4589           1 :     if (!(nDimsLongitude == 2 && nDimsLatitude == 2))
    4590             :     {
    4591           0 :         return false;
    4592             :     }
    4593             : 
    4594             :     std::array<int, 2> anDimLongitudeIds;
    4595           1 :     NCDF_ERR(nc_inq_vardimid(nNavigationDataGrpId, nLongitudeId,
    4596             :                              anDimLongitudeIds.data()));
    4597             :     std::array<int, 2> anDimLatitudeIds;
    4598           1 :     NCDF_ERR(nc_inq_vardimid(nNavigationDataGrpId, nLatitudeId,
    4599             :                              anDimLatitudeIds.data()));
    4600           1 :     if (anDimLongitudeIds != anDimLatitudeIds)
    4601             :     {
    4602           0 :         return false;
    4603             :     }
    4604             : 
    4605             :     std::array<size_t, 2> anSizeVarDimIds;
    4606             :     std::array<size_t, 2> anSizeLongLatIds;
    4607           2 :     if (!(nc_inq_dimlen(cdfid, anVarDimIds[0], &anSizeVarDimIds[0]) ==
    4608           1 :               NC_NOERR &&
    4609           1 :           nc_inq_dimlen(cdfid, anVarDimIds[1], &anSizeVarDimIds[1]) ==
    4610           1 :               NC_NOERR &&
    4611           1 :           nc_inq_dimlen(cdfid, anDimLongitudeIds[0], &anSizeLongLatIds[0]) ==
    4612           1 :               NC_NOERR &&
    4613           1 :           nc_inq_dimlen(cdfid, anDimLongitudeIds[1], &anSizeLongLatIds[1]) ==
    4614             :               NC_NOERR &&
    4615           1 :           anSizeVarDimIds == anSizeLongLatIds))
    4616             :     {
    4617           0 :         return false;
    4618             :     }
    4619             : 
    4620           1 :     const char *pszGeolocXFullName = "/navigation_data/longitude";
    4621           1 :     const char *pszGeolocYFullName = "/navigation_data/latitude";
    4622             : 
    4623           1 :     if (bSwitchedXY)
    4624             :     {
    4625           0 :         std::swap(pszGeolocXFullName, pszGeolocYFullName);
    4626           0 :         GDALPamDataset::SetMetadataItem("SWAP_XY", "YES", GDAL_MDD_GEOLOCATION);
    4627             :     }
    4628             : 
    4629           1 :     CPLDebug("GDAL_netCDF", "using variables %s and %s for GEOLOCATION",
    4630             :              pszGeolocXFullName, pszGeolocYFullName);
    4631             : 
    4632           1 :     GDALPamDataset::SetMetadataItem("SRS", SRS_WKT_WGS84_LAT_LONG,
    4633             :                                     GDAL_MDD_GEOLOCATION);
    4634             : 
    4635           1 :     CPLString osTMP;
    4636           1 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocXFullName);
    4637             : 
    4638           1 :     GDALPamDataset::SetMetadataItem("X_DATASET", osTMP, GDAL_MDD_GEOLOCATION);
    4639           1 :     GDALPamDataset::SetMetadataItem("X_BAND", "1", GDAL_MDD_GEOLOCATION);
    4640           1 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocYFullName);
    4641             : 
    4642           1 :     GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP, GDAL_MDD_GEOLOCATION);
    4643           1 :     GDALPamDataset::SetMetadataItem("Y_BAND", "1", GDAL_MDD_GEOLOCATION);
    4644             : 
    4645           1 :     GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0", GDAL_MDD_GEOLOCATION);
    4646           1 :     GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1", GDAL_MDD_GEOLOCATION);
    4647             : 
    4648           1 :     GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0", GDAL_MDD_GEOLOCATION);
    4649           1 :     GDALPamDataset::SetMetadataItem("LINE_STEP", "1", GDAL_MDD_GEOLOCATION);
    4650             : 
    4651           1 :     GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION", "PIXEL_CENTER",
    4652             :                                     GDAL_MDD_GEOLOCATION);
    4653           1 :     return true;
    4654             : }
    4655             : 
    4656         295 : bool netCDFDataset::ProcessNASAEMITGeoLocation(int nGroupId, int nVarId)
    4657             : {
    4658             :     // Cf https://earth.jpl.nasa.gov/emit/data/data-portal/coverage-and-forecasts/
    4659             : 
    4660             :     // Check for a structure like:
    4661             :     /* netcdf EMIT_L2A_RFL_001_20220903T163129_2224611_012 {
    4662             :         dimensions:
    4663             :             downtrack = 1280 ;
    4664             :             crosstrack = 1242 ;
    4665             :             bands = 285 ;
    4666             :             [...]
    4667             : 
    4668             :         variables:
    4669             :             float reflectance(downtrack, crosstrack, bands) ;
    4670             : 
    4671             :         group: location {
    4672             :           variables:
    4673             :                 double lon(downtrack, crosstrack) ;
    4674             :                         lon:_FillValue = -9999. ;
    4675             :                         lon:long_name = "Longitude (WGS-84)" ;
    4676             :                         lon:units = "degrees east" ;
    4677             :                 double lat(downtrack, crosstrack) ;
    4678             :                         lat:_FillValue = -9999. ;
    4679             :                         lat:long_name = "Latitude (WGS-84)" ;
    4680             :                         lat:units = "degrees north" ;
    4681             :           } // group location
    4682             : 
    4683             :     }
    4684             :     or
    4685             :     netcdf EMIT_L2B_MIN_001_20231024T055538_2329704_040 {
    4686             :         dimensions:
    4687             :                 downtrack = 1664 ;
    4688             :                 crosstrack = 1242 ;
    4689             :                 [...]
    4690             :         variables:
    4691             :                 float group_1_band_depth(downtrack, crosstrack) ;
    4692             :                         group_1_band_depth:_FillValue = -9999.f ;
    4693             :                         group_1_band_depth:long_name = "Group 1 Band Depth" ;
    4694             :                         group_1_band_depth:units = "unitless" ;
    4695             :                 [...]
    4696             :         group: location {
    4697             :           variables:
    4698             :                 double lon(downtrack, crosstrack) ;
    4699             :                         lon:_FillValue = -9999. ;
    4700             :                         lon:long_name = "Longitude (WGS-84)" ;
    4701             :                         lon:units = "degrees east" ;
    4702             :                 double lat(downtrack, crosstrack) ;
    4703             :                         lat:_FillValue = -9999. ;
    4704             :                         lat:long_name = "Latitude (WGS-84)" ;
    4705             :                         lat:units = "degrees north" ;
    4706             :         }
    4707             :     */
    4708             : 
    4709         295 :     int nVarDims = 0;
    4710         295 :     NCDF_ERR(nc_inq_varndims(nGroupId, nVarId, &nVarDims));
    4711         295 :     if (nVarDims != 2 && nVarDims != 3)
    4712          14 :         return false;
    4713             : 
    4714         281 :     int nLocationGrpId = 0;
    4715         281 :     if (nc_inq_grp_ncid(cdfid, "location", &nLocationGrpId) != NC_NOERR)
    4716          60 :         return false;
    4717             : 
    4718             :     std::array<int, 3> anVarDimIds;
    4719         221 :     NCDF_ERR(nc_inq_vardimid(nGroupId, nVarId, anVarDimIds.data()));
    4720         221 :     if (nYDimID != anVarDimIds[0] || nXDimID != anVarDimIds[1])
    4721          21 :         return false;
    4722             : 
    4723         200 :     int nLongitudeId = 0;
    4724         200 :     int nLatitudeId = 0;
    4725         228 :     if (nc_inq_varid(nLocationGrpId, "lon", &nLongitudeId) != NC_NOERR ||
    4726          28 :         nc_inq_varid(nLocationGrpId, "lat", &nLatitudeId) != NC_NOERR)
    4727             :     {
    4728         172 :         return false;
    4729             :     }
    4730             : 
    4731          28 :     int nDimsLongitude = 0;
    4732          28 :     NCDF_ERR(nc_inq_varndims(nLocationGrpId, nLongitudeId, &nDimsLongitude));
    4733          28 :     int nDimsLatitude = 0;
    4734          28 :     NCDF_ERR(nc_inq_varndims(nLocationGrpId, nLatitudeId, &nDimsLatitude));
    4735          28 :     if (!(nDimsLongitude == 2 && nDimsLatitude == 2))
    4736             :     {
    4737          24 :         return false;
    4738             :     }
    4739             : 
    4740             :     std::array<int, 2> anDimLongitudeIds;
    4741           4 :     NCDF_ERR(nc_inq_vardimid(nLocationGrpId, nLongitudeId,
    4742             :                              anDimLongitudeIds.data()));
    4743             :     std::array<int, 2> anDimLatitudeIds;
    4744           4 :     NCDF_ERR(
    4745             :         nc_inq_vardimid(nLocationGrpId, nLatitudeId, anDimLatitudeIds.data()));
    4746           4 :     if (anDimLongitudeIds != anDimLatitudeIds)
    4747             :     {
    4748           0 :         return false;
    4749             :     }
    4750             : 
    4751           8 :     if (anDimLongitudeIds[0] != anVarDimIds[0] ||
    4752           4 :         anDimLongitudeIds[1] != anVarDimIds[1])
    4753             :     {
    4754           0 :         return false;
    4755             :     }
    4756             : 
    4757           4 :     const char *pszGeolocXFullName = "/location/lon";
    4758           4 :     const char *pszGeolocYFullName = "/location/lat";
    4759             : 
    4760           4 :     CPLDebug("GDAL_netCDF", "using variables %s and %s for GEOLOCATION",
    4761             :              pszGeolocXFullName, pszGeolocYFullName);
    4762             : 
    4763           4 :     GDALPamDataset::SetMetadataItem("SRS", SRS_WKT_WGS84_LAT_LONG,
    4764             :                                     GDAL_MDD_GEOLOCATION);
    4765             : 
    4766           4 :     CPLString osTMP;
    4767           4 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocXFullName);
    4768             : 
    4769           4 :     GDALPamDataset::SetMetadataItem("X_DATASET", osTMP, GDAL_MDD_GEOLOCATION);
    4770           4 :     GDALPamDataset::SetMetadataItem("X_BAND", "1", GDAL_MDD_GEOLOCATION);
    4771           4 :     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(), pszGeolocYFullName);
    4772             : 
    4773           4 :     GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP, GDAL_MDD_GEOLOCATION);
    4774           4 :     GDALPamDataset::SetMetadataItem("Y_BAND", "1", GDAL_MDD_GEOLOCATION);
    4775             : 
    4776           4 :     GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0", GDAL_MDD_GEOLOCATION);
    4777           4 :     GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1", GDAL_MDD_GEOLOCATION);
    4778             : 
    4779           4 :     GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0", GDAL_MDD_GEOLOCATION);
    4780           4 :     GDALPamDataset::SetMetadataItem("LINE_STEP", "1", GDAL_MDD_GEOLOCATION);
    4781             : 
    4782           4 :     GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION", "PIXEL_CENTER",
    4783             :                                     GDAL_MDD_GEOLOCATION);
    4784           4 :     return true;
    4785             : }
    4786             : 
    4787         370 : int netCDFDataset::ProcessCFGeolocation(int nGroupId, int nVarId,
    4788             :                                         const std::string &osGeolocWKT,
    4789             :                                         std::string &osGeolocXNameOut,
    4790             :                                         std::string &osGeolocYNameOut)
    4791             : {
    4792         370 :     bool bAddGeoloc = false;
    4793         370 :     char *pszCoordinates = nullptr;
    4794             : 
    4795             :     // If there is no explicit "coordinates" attribute, check if there are
    4796             :     // "lon" and "lat" 2D variables whose dimensions are the last
    4797             :     // 2 ones of the variable of interest.
    4798         370 :     if (NCDFGetAttr(nGroupId, nVarId, "coordinates", &pszCoordinates) !=
    4799             :         CE_None)
    4800             :     {
    4801         317 :         CPLFree(pszCoordinates);
    4802         317 :         pszCoordinates = nullptr;
    4803             : 
    4804         317 :         int nVarDims = 0;
    4805         317 :         NCDF_ERR(nc_inq_varndims(nGroupId, nVarId, &nVarDims));
    4806         317 :         if (nVarDims >= 2)
    4807             :         {
    4808         634 :             std::vector<int> anVarDimIds(nVarDims);
    4809         317 :             NCDF_ERR(nc_inq_vardimid(nGroupId, nVarId, anVarDimIds.data()));
    4810             : 
    4811         317 :             int nLongitudeId = 0;
    4812         317 :             int nLatitudeId = 0;
    4813         380 :             if (nc_inq_varid(nGroupId, "lon", &nLongitudeId) == NC_NOERR &&
    4814          63 :                 nc_inq_varid(nGroupId, "lat", &nLatitudeId) == NC_NOERR)
    4815             :             {
    4816          63 :                 int nDimsLongitude = 0;
    4817          63 :                 NCDF_ERR(
    4818             :                     nc_inq_varndims(nGroupId, nLongitudeId, &nDimsLongitude));
    4819          63 :                 int nDimsLatitude = 0;
    4820          63 :                 NCDF_ERR(
    4821             :                     nc_inq_varndims(nGroupId, nLatitudeId, &nDimsLatitude));
    4822          63 :                 if (nDimsLongitude == 2 && nDimsLatitude == 2)
    4823             :                 {
    4824          42 :                     std::vector<int> anDimLongitudeIds(2);
    4825          21 :                     NCDF_ERR(nc_inq_vardimid(nGroupId, nLongitudeId,
    4826             :                                              anDimLongitudeIds.data()));
    4827          42 :                     std::vector<int> anDimLatitudeIds(2);
    4828          21 :                     NCDF_ERR(nc_inq_vardimid(nGroupId, nLatitudeId,
    4829             :                                              anDimLatitudeIds.data()));
    4830          21 :                     if (anDimLongitudeIds == anDimLatitudeIds &&
    4831          42 :                         anVarDimIds[anVarDimIds.size() - 2] ==
    4832          63 :                             anDimLongitudeIds[0] &&
    4833          42 :                         anVarDimIds[anVarDimIds.size() - 1] ==
    4834          21 :                             anDimLongitudeIds[1])
    4835             :                     {
    4836          21 :                         pszCoordinates = CPLStrdup("lon lat");
    4837             :                     }
    4838             :                 }
    4839             :             }
    4840             :         }
    4841             :     }
    4842             : 
    4843         370 :     if (pszCoordinates)
    4844             :     {
    4845             :         // Get X and Y geolocation names from coordinates attribute.
    4846             :         const CPLStringList aosCoordinates(
    4847         148 :             NCDFTokenizeCoordinatesAttribute(pszCoordinates));
    4848          74 :         if (aosCoordinates.size() >= 2)
    4849             :         {
    4850             :             char szGeolocXName[NC_MAX_NAME + 1];
    4851             :             char szGeolocYName[NC_MAX_NAME + 1];
    4852          70 :             szGeolocXName[0] = '\0';
    4853          70 :             szGeolocYName[0] = '\0';
    4854             : 
    4855             :             // Test that each variable is longitude/latitude.
    4856         226 :             for (const auto &pszVar : aosCoordinates)
    4857             :             {
    4858         156 :                 if (NCDFIsVarLongitude(nGroupId, -1, pszVar))
    4859             :                 {
    4860          59 :                     int nOtherGroupId = -1;
    4861          59 :                     int nOtherVarId = -1;
    4862             :                     // Check that the variable actually exists
    4863             :                     // Needed on Sentinel-3 products
    4864          59 :                     if (NCDFResolveVar(nGroupId, pszVar, &nOtherGroupId,
    4865          59 :                                        &nOtherVarId) == CE_None)
    4866             :                     {
    4867          57 :                         snprintf(szGeolocXName, sizeof(szGeolocXName), "%s",
    4868             :                                  pszVar);
    4869             :                     }
    4870             :                 }
    4871          97 :                 else if (NCDFIsVarLatitude(nGroupId, -1, pszVar))
    4872             :                 {
    4873          59 :                     int nOtherGroupId = -1;
    4874          59 :                     int nOtherVarId = -1;
    4875             :                     // Check that the variable actually exists
    4876             :                     // Needed on Sentinel-3 products
    4877          59 :                     if (NCDFResolveVar(nGroupId, pszVar, &nOtherGroupId,
    4878          59 :                                        &nOtherVarId) == CE_None)
    4879             :                     {
    4880          57 :                         snprintf(szGeolocYName, sizeof(szGeolocYName), "%s",
    4881             :                                  pszVar);
    4882             :                     }
    4883             :                 }
    4884             :             }
    4885             :             // Add GEOLOCATION metadata.
    4886          70 :             if (!EQUAL(szGeolocXName, "") && !EQUAL(szGeolocYName, ""))
    4887             :             {
    4888          57 :                 osGeolocXNameOut = szGeolocXName;
    4889          57 :                 osGeolocYNameOut = szGeolocYName;
    4890             : 
    4891         114 :                 std::string osGeolocXFullName;
    4892         114 :                 std::string osGeolocYFullName;
    4893          57 :                 if (NCDFResolveVarFullName(nGroupId, szGeolocXName,
    4894         114 :                                            osGeolocXFullName) == CE_None &&
    4895          57 :                     NCDFResolveVarFullName(nGroupId, szGeolocYName,
    4896             :                                            osGeolocYFullName) == CE_None)
    4897             :                 {
    4898          57 :                     if (bSwitchedXY)
    4899             :                     {
    4900           2 :                         std::swap(osGeolocXFullName, osGeolocYFullName);
    4901           2 :                         GDALPamDataset::SetMetadataItem("SWAP_XY", "YES",
    4902             :                                                         GDAL_MDD_GEOLOCATION);
    4903             :                     }
    4904             : 
    4905          57 :                     bAddGeoloc = true;
    4906          57 :                     CPLDebug("GDAL_netCDF",
    4907             :                              "using variables %s and %s for GEOLOCATION",
    4908             :                              osGeolocXFullName.c_str(),
    4909             :                              osGeolocYFullName.c_str());
    4910             : 
    4911          57 :                     GDALPamDataset::SetMetadataItem("SRS", osGeolocWKT.c_str(),
    4912             :                                                     GDAL_MDD_GEOLOCATION);
    4913             : 
    4914         114 :                     CPLString osTMP;
    4915             :                     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4916          57 :                                  osGeolocXFullName.c_str());
    4917             : 
    4918          57 :                     GDALPamDataset::SetMetadataItem("X_DATASET", osTMP,
    4919             :                                                     GDAL_MDD_GEOLOCATION);
    4920          57 :                     GDALPamDataset::SetMetadataItem("X_BAND", "1",
    4921             :                                                     GDAL_MDD_GEOLOCATION);
    4922             :                     osTMP.Printf("NETCDF:\"%s\":%s", osFilename.c_str(),
    4923          57 :                                  osGeolocYFullName.c_str());
    4924             : 
    4925          57 :                     GDALPamDataset::SetMetadataItem("Y_DATASET", osTMP,
    4926             :                                                     GDAL_MDD_GEOLOCATION);
    4927          57 :                     GDALPamDataset::SetMetadataItem("Y_BAND", "1",
    4928             :                                                     GDAL_MDD_GEOLOCATION);
    4929             : 
    4930          57 :                     GDALPamDataset::SetMetadataItem("PIXEL_OFFSET", "0",
    4931             :                                                     GDAL_MDD_GEOLOCATION);
    4932          57 :                     GDALPamDataset::SetMetadataItem("PIXEL_STEP", "1",
    4933             :                                                     GDAL_MDD_GEOLOCATION);
    4934             : 
    4935          57 :                     GDALPamDataset::SetMetadataItem("LINE_OFFSET", "0",
    4936             :                                                     GDAL_MDD_GEOLOCATION);
    4937          57 :                     GDALPamDataset::SetMetadataItem("LINE_STEP", "1",
    4938             :                                                     GDAL_MDD_GEOLOCATION);
    4939             : 
    4940          57 :                     GDALPamDataset::SetMetadataItem("GEOREFERENCING_CONVENTION",
    4941             :                                                     "PIXEL_CENTER",
    4942             :                                                     GDAL_MDD_GEOLOCATION);
    4943             :                 }
    4944             :                 else
    4945             :                 {
    4946           0 :                     CPLDebug("GDAL_netCDF",
    4947             :                              "cannot resolve location of "
    4948             :                              "lat/lon variables specified by the coordinates "
    4949             :                              "attribute [%s]",
    4950             :                              pszCoordinates);
    4951          57 :                 }
    4952             :             }
    4953             :             else
    4954             :             {
    4955          13 :                 CPLDebug("GDAL_netCDF",
    4956             :                          "coordinates attribute [%s] is unsupported",
    4957             :                          pszCoordinates);
    4958             :             }
    4959             :         }
    4960             :         else
    4961             :         {
    4962           4 :             CPLDebug("GDAL_netCDF",
    4963             :                      "coordinates attribute [%s] with %d element(s) is "
    4964             :                      "unsupported",
    4965             :                      pszCoordinates, aosCoordinates.size());
    4966             :         }
    4967             :     }
    4968             : 
    4969             :     else
    4970             :     {
    4971         296 :         bAddGeoloc = ProcessNASAL2OceanGeoLocation(nGroupId, nVarId);
    4972             : 
    4973         296 :         if (!bAddGeoloc)
    4974         295 :             bAddGeoloc = ProcessNASAEMITGeoLocation(nGroupId, nVarId);
    4975             :     }
    4976             : 
    4977         370 :     CPLFree(pszCoordinates);
    4978             : 
    4979         370 :     return bAddGeoloc;
    4980             : }
    4981             : 
    4982           8 : CPLErr netCDFDataset::Set1DGeolocation(int nGroupId, int nVarId,
    4983             :                                        const char *szDimName)
    4984             : {
    4985             :     // Get values.
    4986           8 :     char *pszVarValues = nullptr;
    4987           8 :     CPLErr eErr = NCDFGet1DVar(nGroupId, nVarId, &pszVarValues);
    4988           8 :     if (eErr != CE_None)
    4989           0 :         return eErr;
    4990             : 
    4991             :     // Write metadata.
    4992           8 :     char szTemp[NC_MAX_NAME + 1 + 32] = {};
    4993           8 :     snprintf(szTemp, sizeof(szTemp), "%s_VALUES", szDimName);
    4994           8 :     GDALPamDataset::SetMetadataItem(szTemp, pszVarValues, "GEOLOCATION2");
    4995             : 
    4996           8 :     CPLFree(pszVarValues);
    4997             : 
    4998           8 :     return CE_None;
    4999             : }
    5000             : 
    5001           0 : double *netCDFDataset::Get1DGeolocation(CPL_UNUSED const char *szDimName,
    5002             :                                         int &nVarLen)
    5003             : {
    5004           0 :     nVarLen = 0;
    5005             : 
    5006             :     // Get Y_VALUES as tokens.
    5007             :     const CPLStringList aosValues(
    5008           0 :         NCDFTokenizeArray(GetMetadataItem("Y_VALUES", "GEOLOCATION2")));
    5009           0 :     if (aosValues.empty())
    5010           0 :         return nullptr;
    5011             : 
    5012             :     // Initialize and fill array.
    5013           0 :     nVarLen = aosValues.size();
    5014             :     double *pdfVarValues =
    5015           0 :         static_cast<double *>(CPLCalloc(nVarLen, sizeof(double)));
    5016             : 
    5017           0 :     for (int i = 0, j = 0; i < nVarLen; i++)
    5018             :     {
    5019           0 :         if (!bBottomUp)
    5020           0 :             j = nVarLen - 1 - i;
    5021             :         else
    5022           0 :             j = i;  // Invert latitude values.
    5023           0 :         char *pszTemp = nullptr;
    5024           0 :         pdfVarValues[j] = CPLStrtod(aosValues[i], &pszTemp);
    5025             :     }
    5026             : 
    5027           0 :     return pdfVarValues;
    5028             : }
    5029             : 
    5030             : /************************************************************************/
    5031             : /*                       SetSpatialRefNoUpdate()                        */
    5032             : /************************************************************************/
    5033             : 
    5034         271 : void netCDFDataset::SetSpatialRefNoUpdate(const OGRSpatialReference *poSRS)
    5035             : {
    5036         271 :     m_oSRS.Clear();
    5037         271 :     if (poSRS)
    5038         263 :         m_oSRS = *poSRS;
    5039         271 :     m_bHasProjection = true;
    5040         271 : }
    5041             : 
    5042             : /************************************************************************/
    5043             : /*                           SetSpatialRef()                            */
    5044             : /************************************************************************/
    5045             : 
    5046          57 : CPLErr netCDFDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
    5047             : {
    5048         114 :     CPLMutexHolderD(&hNCMutex);
    5049             : 
    5050          57 :     if (GetAccess() != GA_Update || m_bHasProjection)
    5051             :     {
    5052           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    5053             :                  "netCDFDataset::_SetProjection() should only be called once "
    5054             :                  "in update mode!");
    5055           0 :         return CE_Failure;
    5056             :     }
    5057             : 
    5058          57 :     if (m_bHasGeoTransform)
    5059             :     {
    5060           1 :         SetSpatialRefNoUpdate(poSRS);
    5061             : 
    5062             :         // For NC4/NC4C, writing both projection variables and data,
    5063             :         // followed by redefining nodata value, cancels the projection
    5064             :         // info from the Band variable, so for now only write the
    5065             :         // variable definitions, and write data at the end.
    5066             :         // See https://trac.osgeo.org/gdal/ticket/7245
    5067           1 :         return AddProjectionVars(true, nullptr, nullptr);
    5068             :     }
    5069             : 
    5070          56 :     SetSpatialRefNoUpdate(poSRS);
    5071             : 
    5072          56 :     return CE_None;
    5073             : }
    5074             : 
    5075             : /************************************************************************/
    5076             : /*                      SetGeoTransformNoUpdate()                       */
    5077             : /************************************************************************/
    5078             : 
    5079         274 : void netCDFDataset::SetGeoTransformNoUpdate(const GDALGeoTransform &gt)
    5080             : {
    5081         274 :     m_gt = gt;
    5082         274 :     m_bHasGeoTransform = true;
    5083         274 : }
    5084             : 
    5085             : /************************************************************************/
    5086             : /*                          SetGeoTransform()                           */
    5087             : /************************************************************************/
    5088             : 
    5089          58 : CPLErr netCDFDataset::SetGeoTransform(const GDALGeoTransform &gt)
    5090             : {
    5091         116 :     CPLMutexHolderD(&hNCMutex);
    5092             : 
    5093          58 :     if (GetAccess() != GA_Update || m_bHasGeoTransform)
    5094             :     {
    5095           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    5096             :                  "netCDFDataset::SetGeoTransform() should only be called once "
    5097             :                  "in update mode!");
    5098           0 :         return CE_Failure;
    5099             :     }
    5100             : 
    5101          58 :     CPLDebug("GDAL_netCDF", "SetGeoTransform(%f,%f,%f,%f,%f,%f)", gt.xorig,
    5102          58 :              gt.xscale, gt.xrot, gt.yorig, gt.yrot, gt.yscale);
    5103             : 
    5104          58 :     SetGeoTransformNoUpdate(gt);
    5105             : 
    5106          58 :     if (m_bHasProjection)
    5107             :     {
    5108             : 
    5109             :         // For NC4/NC4C, writing both projection variables and data,
    5110             :         // followed by redefining nodata value, cancels the projection
    5111             :         // info from the Band variable, so for now only write the
    5112             :         // variable definitions, and write data at the end.
    5113             :         // See https://trac.osgeo.org/gdal/ticket/7245
    5114           3 :         return AddProjectionVars(true, nullptr, nullptr);
    5115             :     }
    5116             : 
    5117          55 :     return CE_None;
    5118             : }
    5119             : 
    5120             : /************************************************************************/
    5121             : /*                        NCDFWriteSRSVariable()                        */
    5122             : /************************************************************************/
    5123             : 
    5124         112 : int NCDFWriteSRSVariable(int cdfid, const OGRSpatialReference *poSRS,
    5125             :                          char **ppszCFProjection, bool bWriteGDALTags,
    5126             :                          const std::string &srsVarName)
    5127             : {
    5128         112 :     char *pszCFProjection = nullptr;
    5129         112 :     char **papszKeyValues = nullptr;
    5130         112 :     poSRS->exportToCF1(&pszCFProjection, &papszKeyValues, nullptr, nullptr);
    5131             : 
    5132         112 :     if (bWriteGDALTags)
    5133             :     {
    5134         111 :         const char *pszWKT = CSLFetchNameValue(papszKeyValues, NCDF_CRS_WKT);
    5135         111 :         if (pszWKT)
    5136             :         {
    5137             :             // SPATIAL_REF is deprecated. Will be removed in GDAL 4.
    5138         111 :             papszKeyValues =
    5139         111 :                 CSLSetNameValue(papszKeyValues, NCDF_SPATIAL_REF, pszWKT);
    5140             :         }
    5141             :     }
    5142             : 
    5143         112 :     const int nValues = CSLCount(papszKeyValues);
    5144             : 
    5145             :     int NCDFVarID;
    5146         224 :     std::string varNameRadix(pszCFProjection);
    5147         112 :     int nCounter = 2;
    5148             :     while (true)
    5149             :     {
    5150         114 :         NCDFVarID = -1;
    5151         114 :         nc_inq_varid(cdfid, pszCFProjection, &NCDFVarID);
    5152         114 :         if (NCDFVarID < 0)
    5153         109 :             break;
    5154             : 
    5155           5 :         int nbAttr = 0;
    5156           5 :         NCDF_ERR(nc_inq_varnatts(cdfid, NCDFVarID, &nbAttr));
    5157           5 :         bool bSame = nbAttr == nValues;
    5158          41 :         for (int i = 0; bSame && (i < nbAttr); i++)
    5159             :         {
    5160             :             char szAttrName[NC_MAX_NAME + 1];
    5161          38 :             szAttrName[0] = 0;
    5162          38 :             NCDF_ERR(nc_inq_attname(cdfid, NCDFVarID, i, szAttrName));
    5163             : 
    5164             :             const char *pszValue =
    5165          38 :                 CSLFetchNameValue(papszKeyValues, szAttrName);
    5166          38 :             if (!pszValue)
    5167             :             {
    5168           0 :                 bSame = false;
    5169           2 :                 break;
    5170             :             }
    5171             : 
    5172          38 :             nc_type atttype = NC_NAT;
    5173          38 :             size_t attlen = 0;
    5174          38 :             NCDF_ERR(
    5175             :                 nc_inq_att(cdfid, NCDFVarID, szAttrName, &atttype, &attlen));
    5176          38 :             if (atttype != NC_CHAR && atttype != NC_DOUBLE)
    5177             :             {
    5178           0 :                 bSame = false;
    5179           0 :                 break;
    5180             :             }
    5181          38 :             if (atttype == NC_CHAR)
    5182             :             {
    5183          15 :                 if (CPLGetValueType(pszValue) != CPL_VALUE_STRING)
    5184             :                 {
    5185           0 :                     bSame = false;
    5186           0 :                     break;
    5187             :                 }
    5188          15 :                 std::string val;
    5189          15 :                 NCDFGetAttr(cdfid, NCDFVarID, szAttrName, val);
    5190          15 :                 if (val != pszValue)
    5191             :                 {
    5192           0 :                     bSame = false;
    5193           0 :                     break;
    5194             :                 }
    5195             :             }
    5196             :             else
    5197             :             {
    5198             :                 const CPLStringList aosTokens(
    5199          23 :                     CSLTokenizeString2(pszValue, ",", 0));
    5200          23 :                 if (static_cast<size_t>(aosTokens.size()) != attlen)
    5201             :                 {
    5202           0 :                     bSame = false;
    5203           0 :                     break;
    5204             :                 }
    5205             :                 double vals[2];
    5206          23 :                 nc_get_att_double(cdfid, NCDFVarID, szAttrName, vals);
    5207          44 :                 if (vals[0] != CPLAtof(aosTokens[0]) ||
    5208          21 :                     (attlen == 2 && vals[1] != CPLAtof(aosTokens[1])))
    5209             :                 {
    5210           2 :                     bSame = false;
    5211           2 :                     break;
    5212             :                 }
    5213             :             }
    5214             :         }
    5215           5 :         if (bSame)
    5216             :         {
    5217           3 :             *ppszCFProjection = pszCFProjection;
    5218           3 :             CSLDestroy(papszKeyValues);
    5219           3 :             return NCDFVarID;
    5220             :         }
    5221           2 :         CPLFree(pszCFProjection);
    5222           2 :         pszCFProjection =
    5223           2 :             CPLStrdup(CPLSPrintf("%s_%d", varNameRadix.c_str(), nCounter));
    5224           2 :         nCounter++;
    5225           2 :     }
    5226             : 
    5227         109 :     *ppszCFProjection = pszCFProjection;
    5228             : 
    5229             :     const char *pszVarName;
    5230             : 
    5231         109 :     if (srsVarName != "")
    5232             :     {
    5233          38 :         pszVarName = srsVarName.c_str();
    5234             :     }
    5235             :     else
    5236             :     {
    5237          71 :         pszVarName = pszCFProjection;
    5238             :     }
    5239             : 
    5240         109 :     int status = nc_def_var(cdfid, pszVarName, NC_CHAR, 0, nullptr, &NCDFVarID);
    5241         109 :     NCDF_ERR(status);
    5242        1146 :     for (int i = 0; i < nValues; ++i)
    5243             :     {
    5244        1037 :         char *pszKey = nullptr;
    5245        1037 :         const char *pszValue = CPLParseNameValue(papszKeyValues[i], &pszKey);
    5246        1037 :         if (pszKey && pszValue)
    5247             :         {
    5248        2074 :             const CPLStringList aosTokens(CSLTokenizeString2(pszValue, ",", 0));
    5249        1037 :             double adfValues[2] = {0, 0};
    5250        1037 :             const int nDoubleCount = std::min(2, aosTokens.size());
    5251        1037 :             if (!(aosTokens.size() == 2 &&
    5252        2073 :                   CPLGetValueType(aosTokens[0]) != CPL_VALUE_STRING) &&
    5253        1036 :                 CPLGetValueType(pszValue) == CPL_VALUE_STRING)
    5254             :             {
    5255         435 :                 status = nc_put_att_text(cdfid, NCDFVarID, pszKey,
    5256             :                                          strlen(pszValue), pszValue);
    5257             :             }
    5258             :             else
    5259             :             {
    5260        1205 :                 for (int j = 0; j < nDoubleCount; ++j)
    5261         603 :                     adfValues[j] = CPLAtof(aosTokens[j]);
    5262         602 :                 status = nc_put_att_double(cdfid, NCDFVarID, pszKey, NC_DOUBLE,
    5263             :                                            nDoubleCount, adfValues);
    5264             :             }
    5265        1037 :             NCDF_ERR(status);
    5266             :         }
    5267        1037 :         CPLFree(pszKey);
    5268             :     }
    5269             : 
    5270         109 :     CSLDestroy(papszKeyValues);
    5271         109 :     return NCDFVarID;
    5272             : }
    5273             : 
    5274             : /************************************************************************/
    5275             : /*                   NCDFWriteLonLatVarsAttributes()                    */
    5276             : /************************************************************************/
    5277             : 
    5278          73 : void NCDFWriteLonLatVarsAttributes(nccfdriver::netCDFVID &vcdf, int nVarLonID,
    5279             :                                    int nVarLatID)
    5280             : {
    5281             : 
    5282             :     try
    5283             :     {
    5284          73 :         vcdf.nc_put_vatt_text(nVarLatID, CF_STD_NAME, CF_LATITUDE_STD_NAME);
    5285          73 :         vcdf.nc_put_vatt_text(nVarLatID, CF_LNG_NAME, CF_LATITUDE_LNG_NAME);
    5286          73 :         vcdf.nc_put_vatt_text(nVarLatID, CF_UNITS, CF_DEGREES_NORTH);
    5287          73 :         vcdf.nc_put_vatt_text(nVarLonID, CF_STD_NAME, CF_LONGITUDE_STD_NAME);
    5288          73 :         vcdf.nc_put_vatt_text(nVarLonID, CF_LNG_NAME, CF_LONGITUDE_LNG_NAME);
    5289          73 :         vcdf.nc_put_vatt_text(nVarLonID, CF_UNITS, CF_DEGREES_EAST);
    5290             :     }
    5291           0 :     catch (nccfdriver::SG_Exception &e)
    5292             :     {
    5293           0 :         CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5294             :     }
    5295          73 : }
    5296             : 
    5297             : /************************************************************************/
    5298             : /*                  NCDFWriteRLonRLatVarsAttributes()                   */
    5299             : /************************************************************************/
    5300             : 
    5301           0 : void NCDFWriteRLonRLatVarsAttributes(nccfdriver::netCDFVID &vcdf,
    5302             :                                      int nVarRLonID, int nVarRLatID)
    5303             : {
    5304             :     try
    5305             :     {
    5306           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_STD_NAME, "grid_latitude");
    5307           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_LNG_NAME,
    5308             :                               "latitude in rotated pole grid");
    5309           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_UNITS, "degrees");
    5310           0 :         vcdf.nc_put_vatt_text(nVarRLatID, CF_AXIS, "Y");
    5311             : 
    5312           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_STD_NAME, "grid_longitude");
    5313           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_LNG_NAME,
    5314             :                               "longitude in rotated pole grid");
    5315           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_UNITS, "degrees");
    5316           0 :         vcdf.nc_put_vatt_text(nVarRLonID, CF_AXIS, "X");
    5317             :     }
    5318           0 :     catch (nccfdriver::SG_Exception &e)
    5319             :     {
    5320           0 :         CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5321             :     }
    5322           0 : }
    5323             : 
    5324             : /************************************************************************/
    5325             : /*                       NCDFGetProjectedCFUnit()                       */
    5326             : /************************************************************************/
    5327             : 
    5328          51 : std::string NCDFGetProjectedCFUnit(const OGRSpatialReference *poSRS)
    5329             : {
    5330          51 :     char *pszUnitsToWrite = nullptr;
    5331          51 :     poSRS->exportToCF1(nullptr, nullptr, &pszUnitsToWrite, nullptr);
    5332          51 :     std::string osRet = pszUnitsToWrite ? pszUnitsToWrite : std::string();
    5333          51 :     CPLFree(pszUnitsToWrite);
    5334         102 :     return osRet;
    5335             : }
    5336             : 
    5337             : /************************************************************************/
    5338             : /*                     NCDFWriteXYVarsAttributes()                      */
    5339             : /************************************************************************/
    5340             : 
    5341          36 : void NCDFWriteXYVarsAttributes(nccfdriver::netCDFVID &vcdf, int nVarXID,
    5342             :                                int nVarYID, const OGRSpatialReference *poSRS)
    5343             : {
    5344          72 :     const std::string osUnitsToWrite = NCDFGetProjectedCFUnit(poSRS);
    5345             : 
    5346             :     try
    5347             :     {
    5348          36 :         vcdf.nc_put_vatt_text(nVarXID, CF_STD_NAME, CF_PROJ_X_COORD);
    5349          36 :         vcdf.nc_put_vatt_text(nVarXID, CF_LNG_NAME, CF_PROJ_X_COORD_LONG_NAME);
    5350          36 :         if (!osUnitsToWrite.empty())
    5351          36 :             vcdf.nc_put_vatt_text(nVarXID, CF_UNITS, osUnitsToWrite.c_str());
    5352          36 :         vcdf.nc_put_vatt_text(nVarYID, CF_STD_NAME, CF_PROJ_Y_COORD);
    5353          36 :         vcdf.nc_put_vatt_text(nVarYID, CF_LNG_NAME, CF_PROJ_Y_COORD_LONG_NAME);
    5354          36 :         if (!osUnitsToWrite.empty())
    5355          36 :             vcdf.nc_put_vatt_text(nVarYID, CF_UNITS, osUnitsToWrite.c_str());
    5356             :     }
    5357           0 :     catch (nccfdriver::SG_Exception &e)
    5358             :     {
    5359           0 :         CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5360             :     }
    5361          36 : }
    5362             : 
    5363             : /************************************************************************/
    5364             : /*                         AddProjectionVars()                          */
    5365             : /************************************************************************/
    5366             : 
    5367         126 : CPLErr netCDFDataset::AddProjectionVars(bool bDefsOnly,
    5368             :                                         GDALProgressFunc pfnProgress,
    5369             :                                         void *pProgressData)
    5370             : {
    5371         126 :     if (nCFVersionMajor > 1 || (nCFVersionMajor == 1 && nCFVersionMinor >= 8))
    5372           0 :         return CE_None;  // do nothing
    5373             : 
    5374         126 :     bool bWriteGridMapping = false;
    5375         126 :     bool bWriteLonLat = false;
    5376         126 :     bool bHasGeoloc = false;
    5377         126 :     bool bWriteGDALTags = false;
    5378         126 :     bool bWriteGeoTransform = false;
    5379             : 
    5380             :     // For GEOLOCATION information.
    5381         126 :     GDALDatasetUniquePtr poDS_X;
    5382         126 :     GDALDatasetUniquePtr poDS_Y;
    5383         126 :     GDALRasterBand *poBand_X = nullptr;
    5384         126 :     GDALRasterBand *poBand_Y = nullptr;
    5385             : 
    5386         252 :     OGRSpatialReference oSRS(m_oSRS);
    5387         126 :     if (!m_oSRS.IsEmpty())
    5388             :     {
    5389          98 :         if (oSRS.IsProjected())
    5390          72 :             bIsProjected = true;
    5391          26 :         else if (oSRS.IsGeographic())
    5392          26 :             bIsGeographic = true;
    5393             :     }
    5394             : 
    5395         126 :     if (bDefsOnly)
    5396             :     {
    5397         126 :         const std::string osProjection = m_oSRS.exportToWkt();
    5398         112 :         CPLDebug("GDAL_netCDF",
    5399             :                  "SetProjection, WKT now = [%s]\nprojected: %d geographic: %d",
    5400          49 :                  osProjection.empty() ? "(null)" : osProjection.c_str(),
    5401          63 :                  static_cast<int>(bIsProjected),
    5402          63 :                  static_cast<int>(bIsGeographic));
    5403             : 
    5404          63 :         if (!m_bHasGeoTransform)
    5405           5 :             CPLDebug("GDAL_netCDF",
    5406             :                      "netCDFDataset::AddProjectionVars() called, "
    5407             :                      "but GeoTransform has not yet been defined!");
    5408             : 
    5409          63 :         if (!m_bHasProjection)
    5410           6 :             CPLDebug("GDAL_netCDF",
    5411             :                      "netCDFDataset::AddProjectionVars() called, "
    5412             :                      "but Projection has not yet been defined!");
    5413             :     }
    5414             : 
    5415             :     // Check GEOLOCATION information.
    5416             :     CSLConstList papszGeolocationInfo =
    5417         126 :         netCDFDataset::GetMetadata(GDAL_MDD_GEOLOCATION);
    5418         126 :     if (papszGeolocationInfo != nullptr)
    5419             :     {
    5420             :         // Look for geolocation datasets.
    5421             :         const char *pszDSName =
    5422          12 :             CSLFetchNameValue(papszGeolocationInfo, "X_DATASET");
    5423          12 :         if (pszDSName != nullptr)
    5424          12 :             poDS_X.reset(GDALDataset::Open(
    5425             :                 pszDSName,
    5426             :                 GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_SHARED));
    5427          12 :         pszDSName = CSLFetchNameValue(papszGeolocationInfo, "Y_DATASET");
    5428          12 :         if (pszDSName != nullptr)
    5429          12 :             poDS_Y.reset(GDALDataset::Open(
    5430             :                 pszDSName,
    5431             :                 GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_SHARED));
    5432             : 
    5433          12 :         if (poDS_X != nullptr && poDS_Y != nullptr)
    5434             :         {
    5435          12 :             int nBand = std::max(1, atoi(CSLFetchNameValueDef(
    5436          12 :                                         papszGeolocationInfo, "X_BAND", "0")));
    5437          12 :             poBand_X = poDS_X->GetRasterBand(nBand);
    5438          12 :             nBand = std::max(1, atoi(CSLFetchNameValueDef(papszGeolocationInfo,
    5439          12 :                                                           "Y_BAND", "0")));
    5440          12 :             poBand_Y = poDS_Y->GetRasterBand(nBand);
    5441             : 
    5442             :             // If geoloc bands are found, do basic validation based on their
    5443             :             // dimensions.
    5444          12 :             if (poBand_X != nullptr && poBand_Y != nullptr)
    5445             :             {
    5446          12 :                 const int nXSize_XBand = poBand_X->GetXSize();
    5447          12 :                 const int nYSize_XBand = poBand_X->GetYSize();
    5448          12 :                 const int nXSize_YBand = poBand_Y->GetXSize();
    5449          12 :                 const int nYSize_YBand = poBand_Y->GetYSize();
    5450             : 
    5451             :                 // TODO 1D geolocation arrays not implemented.
    5452          12 :                 if (nYSize_XBand == 1 && nYSize_YBand == 1)
    5453             :                 {
    5454           2 :                     bHasGeoloc = false;
    5455           2 :                     CPLDebug("GDAL_netCDF",
    5456             :                              "1D GEOLOCATION arrays not supported yet");
    5457             :                 }
    5458             :                 // 2D bands must have same sizes as the raster bands.
    5459          10 :                 else if (nXSize_XBand != nRasterXSize ||
    5460          10 :                          nYSize_XBand != nRasterYSize ||
    5461          10 :                          nXSize_YBand != nRasterXSize ||
    5462          10 :                          nYSize_YBand != nRasterYSize)
    5463             :                 {
    5464           0 :                     bHasGeoloc = false;
    5465           0 :                     CPLDebug("GDAL_netCDF",
    5466             :                              "GEOLOCATION array sizes (%dx%d %dx%d) differ "
    5467             :                              "from raster (%dx%d), not supported",
    5468             :                              nXSize_XBand, nYSize_XBand, nXSize_YBand,
    5469             :                              nYSize_YBand, nRasterXSize, nRasterYSize);
    5470             :                 }
    5471             :                 else
    5472             :                 {
    5473          10 :                     bHasGeoloc = true;
    5474          10 :                     CPLDebug("GDAL_netCDF",
    5475             :                              "dataset has GEOLOCATION information, will try to "
    5476             :                              "write it");
    5477             :                 }
    5478             :             }
    5479             :         }
    5480             :     }
    5481             : 
    5482             :     // Process projection options.
    5483         126 :     if (bIsProjected)
    5484             :     {
    5485             :         bool bIsCfProjection =
    5486          72 :             oSRS.exportToCF1(nullptr, nullptr, nullptr, nullptr) == OGRERR_NONE;
    5487          72 :         bWriteGridMapping = true;
    5488          72 :         bWriteGDALTags = aosCreationOptions.FetchBool("WRITE_GDAL_TAGS", true);
    5489             :         // Force WRITE_GDAL_TAGS if is not a CF projection.
    5490          72 :         if (!bWriteGDALTags && !bIsCfProjection)
    5491           0 :             bWriteGDALTags = true;
    5492          72 :         if (bWriteGDALTags)
    5493          72 :             bWriteGeoTransform = true;
    5494             : 
    5495             :         // Write lon/lat: default is NO, except if has geolocation.
    5496             :         // With IF_NEEDED: write if has geoloc or is not CF projection.
    5497             :         const char *pszValue =
    5498          72 :             aosCreationOptions.FetchNameValue("WRITE_LONLAT");
    5499          72 :         if (pszValue)
    5500             :         {
    5501           6 :             if (EQUAL(pszValue, "IF_NEEDED"))
    5502             :             {
    5503           0 :                 bWriteLonLat = bHasGeoloc || !bIsCfProjection;
    5504             :             }
    5505             :             else
    5506             :             {
    5507           6 :                 bWriteLonLat = CPLTestBool(pszValue);
    5508             :             }
    5509             :         }
    5510             :         else
    5511             :         {
    5512          66 :             bWriteLonLat = bHasGeoloc;
    5513             :         }
    5514             : 
    5515             :         // Save value of pszCFCoordinates for later.
    5516          72 :         if (bWriteLonLat)
    5517             :         {
    5518           8 :             pszCFCoordinates = NCDF_LONLAT;
    5519             :         }
    5520             :     }
    5521             :     else
    5522             :     {
    5523             :         // Files without a Datum will not have a grid_mapping variable and
    5524             :         // geographic information.
    5525          54 :         bWriteGridMapping = bIsGeographic;
    5526             : 
    5527          54 :         if (bHasGeoloc)
    5528             :         {
    5529           8 :             bWriteLonLat = true;
    5530             :         }
    5531             :         else
    5532             :         {
    5533          46 :             bWriteGDALTags = aosCreationOptions.FetchBool("WRITE_GDAL_TAGS",
    5534             :                                                           bWriteGridMapping);
    5535          46 :             if (bWriteGDALTags)
    5536          26 :                 bWriteGeoTransform = true;
    5537             : 
    5538             :             const char *pszValue =
    5539          46 :                 aosCreationOptions.FetchNameValueDef("WRITE_LONLAT", "YES");
    5540          46 :             if (EQUAL(pszValue, "IF_NEEDED"))
    5541           0 :                 bWriteLonLat = true;
    5542             :             else
    5543          46 :                 bWriteLonLat = CPLTestBool(pszValue);
    5544             :             //  Don't write lon/lat if no source geotransform.
    5545          46 :             if (!m_bHasGeoTransform)
    5546           0 :                 bWriteLonLat = false;
    5547             :             // If we don't write lon/lat, set dimnames to X/Y and write gdal
    5548             :             // tags.
    5549          46 :             if (!bWriteLonLat)
    5550             :             {
    5551           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    5552             :                          "creating geographic file without lon/lat values!");
    5553           0 :                 if (m_bHasGeoTransform)
    5554             :                 {
    5555           0 :                     bWriteGDALTags = true;  // Not desirable if no geotransform.
    5556           0 :                     bWriteGeoTransform = true;
    5557             :                 }
    5558             :             }
    5559             :         }
    5560             :     }
    5561             : 
    5562             :     // Make sure we write grid_mapping if we need to write GDAL tags.
    5563         126 :     if (bWriteGDALTags)
    5564          98 :         bWriteGridMapping = true;
    5565             : 
    5566             :     // bottom-up value: new driver is bottom-up by default.
    5567             :     // Override with WRITE_BOTTOMUP.
    5568         126 :     bBottomUp = aosCreationOptions.FetchBool("WRITE_BOTTOMUP", true);
    5569             : 
    5570         126 :     if (bDefsOnly)
    5571             :     {
    5572          63 :         CPLDebug(
    5573             :             "GDAL_netCDF",
    5574             :             "bIsProjected=%d bIsGeographic=%d bWriteGridMapping=%d "
    5575             :             "bWriteGDALTags=%d bWriteLonLat=%d bBottomUp=%d bHasGeoloc=%d",
    5576          63 :             static_cast<int>(bIsProjected), static_cast<int>(bIsGeographic),
    5577             :             static_cast<int>(bWriteGridMapping),
    5578             :             static_cast<int>(bWriteGDALTags), static_cast<int>(bWriteLonLat),
    5579          63 :             static_cast<int>(bBottomUp), static_cast<int>(bHasGeoloc));
    5580             :     }
    5581             : 
    5582             :     // Exit if nothing to do.
    5583         126 :     if (!bIsProjected && !bWriteLonLat)
    5584           0 :         return CE_None;
    5585             : 
    5586             :     // Define dimension names.
    5587             : 
    5588         126 :     constexpr const char *ROTATED_POLE_VAR_NAME = "rotated_pole";
    5589             : 
    5590         126 :     if (bDefsOnly)
    5591             :     {
    5592          63 :         int nVarLonID = -1;
    5593          63 :         int nVarLatID = -1;
    5594          63 :         int nVarXID = -1;
    5595          63 :         int nVarYID = -1;
    5596             : 
    5597          63 :         m_bAddedProjectionVarsDefs = true;
    5598             : 
    5599             :         // Make sure we are in define mode.
    5600          63 :         SetDefineMode(true);
    5601             : 
    5602             :         // Write projection attributes.
    5603          63 :         if (bWriteGridMapping)
    5604             :         {
    5605          49 :             const int NCDFVarID = NCDFWriteSRSVariable(
    5606             :                 cdfid, &oSRS, &pszCFProjection, bWriteGDALTags);
    5607          49 :             if (NCDFVarID < 0)
    5608           0 :                 return CE_Failure;
    5609             : 
    5610             :             // Optional GDAL custom projection tags.
    5611          49 :             if (bWriteGDALTags && bWriteGeoTransform && m_bHasGeoTransform)
    5612             :             {
    5613          48 :                 GDALGeoTransform gt(m_gt);
    5614          48 :                 if (!bBottomUp)
    5615             :                 {
    5616             :                     // Change origin from top to bottom and sign of coefficients
    5617             :                     // indexed by row
    5618           2 :                     gt.yorig += nRasterYSize * gt.yscale;
    5619           2 :                     gt.xorig += nRasterYSize * gt.xrot;
    5620           2 :                     gt.xrot = -gt.xrot;
    5621           2 :                     gt.yscale = -gt.yscale;
    5622             :                 }
    5623          96 :                 std::string osGeoTransform = gt.ToString(" ");
    5624          48 :                 CPLDebug("GDAL_netCDF", "szGeoTransform = %s",
    5625             :                          osGeoTransform.c_str());
    5626             : 
    5627          48 :                 const int status = nc_put_att_text(
    5628             :                     cdfid, NCDFVarID, NCDF_GEOTRANSFORM, osGeoTransform.size(),
    5629             :                     osGeoTransform.c_str());
    5630          48 :                 NCDF_ERR(status);
    5631             :             }
    5632             : 
    5633             :             // Write projection variable to band variable.
    5634             :             // Need to call later if there are no bands.
    5635          49 :             AddGridMappingRef();
    5636             :         }  // end if( bWriteGridMapping )
    5637             : 
    5638             :         // Write CF Projection vars.
    5639             : 
    5640          63 :         const bool bIsRotatedPole =
    5641         112 :             pszCFProjection != nullptr &&
    5642          49 :             EQUAL(pszCFProjection, ROTATED_POLE_VAR_NAME);
    5643             : 
    5644          63 :         if (m_bHasGeoTransform && !m_gt.IsAxisAligned())
    5645             :         {
    5646             :             // Do not write X/Y coordinate arrays
    5647             :         }
    5648             : 
    5649          59 :         else if (bIsRotatedPole)
    5650             :         {
    5651             :             // Rename dims to rlat/rlon.
    5652             :             papszDimName
    5653           0 :                 .Clear();  // If we add other dims one day, this has to change
    5654           0 :             papszDimName.AddString(NCDF_DIMNAME_RLAT);
    5655           0 :             papszDimName.AddString(NCDF_DIMNAME_RLON);
    5656             : 
    5657           0 :             int status = nc_rename_dim(cdfid, nYDimID, NCDF_DIMNAME_RLAT);
    5658           0 :             NCDF_ERR(status);
    5659           0 :             status = nc_rename_dim(cdfid, nXDimID, NCDF_DIMNAME_RLON);
    5660           0 :             NCDF_ERR(status);
    5661             :         }
    5662             :         // Rename dimensions if lon/lat.
    5663          59 :         else if (!bIsProjected && !bHasGeoloc)
    5664             :         {
    5665             :             // Rename dims to lat/lon.
    5666             :             papszDimName
    5667          23 :                 .Clear();  // If we add other dims one day, this has to change
    5668          23 :             papszDimName.AddString(NCDF_DIMNAME_LAT);
    5669          23 :             papszDimName.AddString(NCDF_DIMNAME_LON);
    5670             : 
    5671          23 :             int status = nc_rename_dim(cdfid, nYDimID, NCDF_DIMNAME_LAT);
    5672          23 :             NCDF_ERR(status);
    5673          23 :             status = nc_rename_dim(cdfid, nXDimID, NCDF_DIMNAME_LON);
    5674          23 :             NCDF_ERR(status);
    5675             :         }
    5676             : 
    5677             :         // Write X/Y attributes.
    5678             :         else /* if( bIsProjected || bHasGeoloc ) */
    5679             :         {
    5680             :             // X
    5681             :             int anXDims[1];
    5682          36 :             anXDims[0] = nXDimID;
    5683          36 :             CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d)", cdfid,
    5684             :                      CF_PROJ_X_VAR_NAME, NC_DOUBLE);
    5685          36 :             int status = nc_def_var(cdfid, CF_PROJ_X_VAR_NAME, NC_DOUBLE, 1,
    5686             :                                     anXDims, &nVarXID);
    5687          36 :             NCDF_ERR(status);
    5688             : 
    5689             :             // Y
    5690             :             int anYDims[1];
    5691          36 :             anYDims[0] = nYDimID;
    5692          36 :             CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d)", cdfid,
    5693             :                      CF_PROJ_Y_VAR_NAME, NC_DOUBLE);
    5694          36 :             status = nc_def_var(cdfid, CF_PROJ_Y_VAR_NAME, NC_DOUBLE, 1,
    5695             :                                 anYDims, &nVarYID);
    5696          36 :             NCDF_ERR(status);
    5697             : 
    5698          36 :             if (bIsProjected)
    5699             :             {
    5700          32 :                 NCDFWriteXYVarsAttributes(this->vcdf, nVarXID, nVarYID, &oSRS);
    5701             :             }
    5702             :             else
    5703             :             {
    5704           4 :                 CPLAssert(bHasGeoloc);
    5705             :                 try
    5706             :                 {
    5707           4 :                     vcdf.nc_put_vatt_text(nVarXID, CF_AXIS, CF_SG_X_AXIS);
    5708           4 :                     vcdf.nc_put_vatt_text(nVarXID, CF_LNG_NAME,
    5709             :                                           "x-coordinate in Cartesian system");
    5710           4 :                     vcdf.nc_put_vatt_text(nVarXID, CF_UNITS, "m");
    5711           4 :                     vcdf.nc_put_vatt_text(nVarYID, CF_AXIS, CF_SG_Y_AXIS);
    5712           4 :                     vcdf.nc_put_vatt_text(nVarYID, CF_LNG_NAME,
    5713             :                                           "y-coordinate in Cartesian system");
    5714           4 :                     vcdf.nc_put_vatt_text(nVarYID, CF_UNITS, "m");
    5715             : 
    5716           4 :                     pszCFCoordinates = NCDF_LONLAT;
    5717             :                 }
    5718           0 :                 catch (nccfdriver::SG_Exception &e)
    5719             :                 {
    5720           0 :                     CPLError(CE_Failure, CPLE_FileIO, "%s", e.get_err_msg());
    5721           0 :                     return CE_Failure;
    5722             :                 }
    5723             :             }
    5724             :         }
    5725             : 
    5726             :         // Write lat/lon attributes if needed.
    5727          63 :         if (bWriteLonLat)
    5728             :         {
    5729          31 :             int anLatDims[2] = {0, 0};
    5730          31 :             int anLonDims[2] = {0, 0};
    5731          31 :             int nLatDims = -1;
    5732          31 :             int nLonDims = -1;
    5733             : 
    5734             :             // Get information.
    5735          31 :             if (bHasGeoloc)
    5736             :             {
    5737             :                 // Geoloc
    5738           5 :                 nLatDims = 2;
    5739           5 :                 anLatDims[0] = nYDimID;
    5740           5 :                 anLatDims[1] = nXDimID;
    5741           5 :                 nLonDims = 2;
    5742           5 :                 anLonDims[0] = nYDimID;
    5743           5 :                 anLonDims[1] = nXDimID;
    5744             :             }
    5745          26 :             else if (bIsProjected)
    5746             :             {
    5747             :                 // Projected
    5748           3 :                 nLatDims = 2;
    5749           3 :                 anLatDims[0] = nYDimID;
    5750           3 :                 anLatDims[1] = nXDimID;
    5751           3 :                 nLonDims = 2;
    5752           3 :                 anLonDims[0] = nYDimID;
    5753           3 :                 anLonDims[1] = nXDimID;
    5754             :             }
    5755             :             else
    5756             :             {
    5757             :                 // Geographic
    5758          23 :                 nLatDims = 1;
    5759          23 :                 anLatDims[0] = nYDimID;
    5760          23 :                 nLonDims = 1;
    5761          23 :                 anLonDims[0] = nXDimID;
    5762             :             }
    5763             : 
    5764          31 :             nc_type eLonLatType = NC_NAT;
    5765          31 :             if (bIsProjected)
    5766             :             {
    5767           4 :                 eLonLatType = NC_FLOAT;
    5768           4 :                 const char *pszValue = aosCreationOptions.FetchNameValueDef(
    5769             :                     "TYPE_LONLAT", "FLOAT");
    5770           4 :                 if (EQUAL(pszValue, "DOUBLE"))
    5771           0 :                     eLonLatType = NC_DOUBLE;
    5772             :             }
    5773             :             else
    5774             :             {
    5775          27 :                 eLonLatType = NC_DOUBLE;
    5776          27 :                 const char *pszValue = aosCreationOptions.FetchNameValueDef(
    5777             :                     "TYPE_LONLAT", "DOUBLE");
    5778          27 :                 if (EQUAL(pszValue, "FLOAT"))
    5779           0 :                     eLonLatType = NC_FLOAT;
    5780             :             }
    5781             : 
    5782             :             // Def vars and attributes.
    5783             :             {
    5784          31 :                 const char *pszVarName =
    5785          31 :                     bIsRotatedPole ? NCDF_DIMNAME_RLAT : CF_LATITUDE_VAR_NAME;
    5786          31 :                 int status = nc_def_var(cdfid, pszVarName, eLonLatType,
    5787             :                                         nLatDims, anLatDims, &nVarLatID);
    5788          31 :                 CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d,%d,-,-) got id %d",
    5789             :                          cdfid, pszVarName, eLonLatType, nLatDims, nVarLatID);
    5790          31 :                 NCDF_ERR(status);
    5791          31 :                 DefVarDeflate(nVarLatID, false);  // Don't set chunking.
    5792             :             }
    5793             : 
    5794             :             {
    5795          31 :                 const char *pszVarName =
    5796          31 :                     bIsRotatedPole ? NCDF_DIMNAME_RLON : CF_LONGITUDE_VAR_NAME;
    5797          31 :                 int status = nc_def_var(cdfid, pszVarName, eLonLatType,
    5798             :                                         nLonDims, anLonDims, &nVarLonID);
    5799          31 :                 CPLDebug("GDAL_netCDF", "nc_def_var(%d,%s,%d,%d,-,-) got id %d",
    5800             :                          cdfid, pszVarName, eLonLatType, nLatDims, nVarLonID);
    5801          31 :                 NCDF_ERR(status);
    5802          31 :                 DefVarDeflate(nVarLonID, false);  // Don't set chunking.
    5803             :             }
    5804             : 
    5805          31 :             if (bIsRotatedPole)
    5806           0 :                 NCDFWriteRLonRLatVarsAttributes(this->vcdf, nVarLonID,
    5807             :                                                 nVarLatID);
    5808             :             else
    5809          31 :                 NCDFWriteLonLatVarsAttributes(this->vcdf, nVarLonID, nVarLatID);
    5810             :         }
    5811             :     }
    5812             : 
    5813         126 :     if (!bDefsOnly)
    5814             :     {
    5815          63 :         m_bAddedProjectionVarsData = true;
    5816             : 
    5817          63 :         int nVarXID = -1;
    5818          63 :         int nVarYID = -1;
    5819             : 
    5820          63 :         nc_inq_varid(cdfid, CF_PROJ_X_VAR_NAME, &nVarXID);
    5821          63 :         nc_inq_varid(cdfid, CF_PROJ_Y_VAR_NAME, &nVarYID);
    5822             : 
    5823          63 :         int nVarLonID = -1;
    5824          63 :         int nVarLatID = -1;
    5825             : 
    5826          63 :         const bool bIsRotatedPole =
    5827         112 :             pszCFProjection != nullptr &&
    5828          49 :             EQUAL(pszCFProjection, ROTATED_POLE_VAR_NAME);
    5829          63 :         nc_inq_varid(cdfid,
    5830             :                      bIsRotatedPole ? NCDF_DIMNAME_RLON : CF_LONGITUDE_VAR_NAME,
    5831             :                      &nVarLonID);
    5832          63 :         nc_inq_varid(cdfid,
    5833             :                      bIsRotatedPole ? NCDF_DIMNAME_RLAT : CF_LATITUDE_VAR_NAME,
    5834             :                      &nVarLatID);
    5835             : 
    5836             :         // Get projection values.
    5837             : 
    5838          63 :         if (bIsProjected)
    5839             :         {
    5840           0 :             std::unique_ptr<OGRSpatialReference> poLatLonSRS;
    5841           0 :             std::unique_ptr<OGRCoordinateTransformation> poTransform;
    5842             : 
    5843             :             size_t startX[1];
    5844             :             size_t countX[1];
    5845             :             size_t startY[1];
    5846             :             size_t countY[1];
    5847             : 
    5848          36 :             CPLDebug("GDAL_netCDF", "Getting (X,Y) values");
    5849             : 
    5850             :             std::unique_ptr<double, decltype(&VSIFree)> adXValKeeper(
    5851             :                 static_cast<double *>(
    5852          72 :                     VSI_MALLOC2_VERBOSE(nRasterXSize, sizeof(double))),
    5853          36 :                 VSIFree);
    5854             :             std::unique_ptr<double, decltype(&VSIFree)> adYValKeeper(
    5855             :                 static_cast<double *>(
    5856          72 :                     VSI_MALLOC2_VERBOSE(nRasterYSize, sizeof(double))),
    5857          36 :                 VSIFree);
    5858          36 :             double *padXVal = adXValKeeper.get();
    5859          36 :             double *padYVal = adYValKeeper.get();
    5860          36 :             if (!padXVal || !padYVal)
    5861             :             {
    5862           0 :                 return CE_Failure;
    5863             :             }
    5864             : 
    5865             :             // Make sure we are in data mode.
    5866          36 :             SetDefineMode(false);
    5867             : 
    5868          36 :             int status = NC_NOERR;
    5869             : 
    5870          36 :             if (m_gt.IsAxisAligned())
    5871             :             {
    5872             :                 // Get Y values.
    5873          32 :                 const double dfY0 =
    5874          32 :                     (!bBottomUp) ? m_gt.yorig :
    5875             :                                  // Invert latitude values.
    5876          32 :                         m_gt.yorig + (m_gt.yscale * nRasterYSize);
    5877          32 :                 const double dfDY = m_gt.yscale;
    5878             : 
    5879        1583 :                 for (int j = 0; j < nRasterYSize; j++)
    5880             :                 {
    5881             :                     // The data point is centered inside the pixel.
    5882        1551 :                     if (!bBottomUp)
    5883           0 :                         padYVal[j] = dfY0 + (j + 0.5) * dfDY;
    5884             :                     else  // Invert latitude values.
    5885        1551 :                         padYVal[j] = dfY0 - (j + 0.5) * dfDY;
    5886             :                 }
    5887          32 :                 startX[0] = 0;
    5888          32 :                 countX[0] = nRasterXSize;
    5889             : 
    5890             :                 // Get X values.
    5891          32 :                 const double dfX0 = m_gt.xorig;
    5892          32 :                 const double dfDX = m_gt.xscale;
    5893             : 
    5894        1624 :                 for (int i = 0; i < nRasterXSize; i++)
    5895             :                 {
    5896             :                     // The data point is centered inside the pixel.
    5897        1592 :                     padXVal[i] = dfX0 + (i + 0.5) * dfDX;
    5898             :                 }
    5899          32 :                 startY[0] = 0;
    5900          32 :                 countY[0] = nRasterYSize;
    5901             : 
    5902             :                 // Write X/Y values.
    5903             : 
    5904          32 :                 CPLDebug("GDAL_netCDF", "Writing X values");
    5905             :                 status =
    5906          32 :                     nc_put_vara_double(cdfid, nVarXID, startX, countX, padXVal);
    5907          32 :                 NCDF_ERR(status);
    5908             : 
    5909          32 :                 CPLDebug("GDAL_netCDF", "Writing Y values");
    5910             :                 status =
    5911          32 :                     nc_put_vara_double(cdfid, nVarYID, startY, countY, padYVal);
    5912          32 :                 NCDF_ERR(status);
    5913             :             }
    5914             : 
    5915          36 :             if (pfnProgress)
    5916          32 :                 pfnProgress(0.20, nullptr, pProgressData);
    5917             : 
    5918             :             // Write lon/lat arrays (CF coordinates) if requested.
    5919             : 
    5920             :             // Get OGR transform if GEOLOCATION is not available.
    5921          36 :             if (bWriteLonLat && !bHasGeoloc)
    5922             :             {
    5923           3 :                 poLatLonSRS.reset(m_oSRS.CloneGeogCS());
    5924           3 :                 if (poLatLonSRS != nullptr)
    5925             :                 {
    5926           3 :                     poLatLonSRS->SetAxisMappingStrategy(
    5927             :                         OAMS_TRADITIONAL_GIS_ORDER);
    5928           3 :                     poTransform.reset(OGRCreateCoordinateTransformation(
    5929           3 :                         &m_oSRS, poLatLonSRS.get()));
    5930             :                 }
    5931             :                 // If no OGR transform, then don't write CF lon/lat.
    5932           3 :                 if (poTransform == nullptr)
    5933             :                 {
    5934           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    5935             :                              "Unable to get Coordinate Transform");
    5936           0 :                     bWriteLonLat = false;
    5937             :                 }
    5938             :             }
    5939             : 
    5940          36 :             if (bWriteLonLat)
    5941             :             {
    5942           4 :                 if (!bHasGeoloc)
    5943           3 :                     CPLDebug("GDAL_netCDF", "Transforming (X,Y)->(lon,lat)");
    5944             :                 else
    5945           1 :                     CPLDebug("GDAL_netCDF",
    5946             :                              "Writing (lon,lat) from GEOLOCATION arrays");
    5947             : 
    5948           4 :                 bool bOK = true;
    5949           4 :                 double dfProgress = 0.2;
    5950             : 
    5951           4 :                 size_t start[] = {0, 0};
    5952           4 :                 size_t count[] = {1, (size_t)nRasterXSize};
    5953             :                 std::unique_ptr<double, decltype(&VSIFree)> adLatValKeeper(
    5954             :                     static_cast<double *>(
    5955           8 :                         VSI_MALLOC2_VERBOSE(nRasterXSize, sizeof(double))),
    5956           4 :                     VSIFree);
    5957             :                 std::unique_ptr<double, decltype(&VSIFree)> adLonValKeeper(
    5958             :                     static_cast<double *>(
    5959           8 :                         VSI_MALLOC2_VERBOSE(nRasterXSize, sizeof(double))),
    5960           4 :                     VSIFree);
    5961           4 :                 double *padLonVal = adLonValKeeper.get();
    5962           4 :                 double *padLatVal = adLatValKeeper.get();
    5963           4 :                 if (!padLonVal || !padLatVal)
    5964             :                 {
    5965           0 :                     return CE_Failure;
    5966             :                 }
    5967             : 
    5968         103 :                 for (int j = 0; j < nRasterYSize && bOK && status == NC_NOERR;
    5969             :                      j++)
    5970             :                 {
    5971          99 :                     start[0] = j;
    5972             : 
    5973             :                     // Get values from geotransform.
    5974          99 :                     if (!bHasGeoloc)
    5975             :                     {
    5976             :                         // Fill values to transform.
    5977          60 :                         if (m_gt.IsAxisAligned())
    5978             :                         {
    5979         420 :                             for (int i = 0; i < nRasterXSize; i++)
    5980             :                             {
    5981         400 :                                 padLatVal[i] = padYVal[j];
    5982         400 :                                 padLonVal[i] = padXVal[i];
    5983             :                             }
    5984             :                         }
    5985             :                         else
    5986             :                         {
    5987         840 :                             for (int i = 0; i < nRasterXSize; i++)
    5988             :                             {
    5989         800 :                                 if (!bBottomUp)
    5990             :                                 {
    5991         400 :                                     padLatVal[i] = m_gt.yorig +
    5992         400 :                                                    (i + 0.5) * m_gt.yrot +
    5993         400 :                                                    (j + 0.5) * m_gt.yscale;
    5994         400 :                                     padLonVal[i] = m_gt.xorig +
    5995         400 :                                                    (i + 0.5) * m_gt.xscale +
    5996         400 :                                                    (j + 0.5) * m_gt.xrot;
    5997             :                                 }
    5998             :                                 else
    5999             :                                 {
    6000         400 :                                     padLatVal[i] =
    6001         400 :                                         m_gt.yorig + (i + 0.5) * m_gt.yrot +
    6002         400 :                                         (nRasterYSize - j - 0.5) * m_gt.yscale;
    6003         400 :                                     padLonVal[i] =
    6004         400 :                                         m_gt.xorig + (i + 0.5) * m_gt.xscale +
    6005         400 :                                         (nRasterYSize - j - 0.5) * m_gt.xrot;
    6006             :                                 }
    6007             :                             }
    6008             :                         }
    6009             : 
    6010             :                         // Do the transform.
    6011         120 :                         bOK = CPL_TO_BOOL(poTransform->Transform(
    6012          60 :                             nRasterXSize, padLonVal, padLatVal, nullptr));
    6013          60 :                         if (!bOK)
    6014             :                         {
    6015           0 :                             CPLError(CE_Failure, CPLE_AppDefined,
    6016             :                                      "Unable to Transform (X,Y) to (lon,lat).");
    6017             :                         }
    6018             :                     }
    6019             :                     // Get values from geoloc arrays.
    6020             :                     else
    6021             :                     {
    6022          39 :                         CPLErr eErr = poBand_Y->RasterIO(
    6023             :                             GF_Read, 0, j, nRasterXSize, 1, padLatVal,
    6024             :                             nRasterXSize, 1, GDT_Float64, 0, 0, nullptr);
    6025          39 :                         if (eErr == CE_None)
    6026             :                         {
    6027          39 :                             eErr = poBand_X->RasterIO(
    6028             :                                 GF_Read, 0, j, nRasterXSize, 1, padLonVal,
    6029             :                                 nRasterXSize, 1, GDT_Float64, 0, 0, nullptr);
    6030             :                         }
    6031             : 
    6032          39 :                         if (eErr == CE_None)
    6033             :                         {
    6034          39 :                             bOK = true;
    6035             :                         }
    6036             :                         else
    6037             :                         {
    6038           0 :                             bOK = false;
    6039           0 :                             CPLError(CE_Failure, CPLE_AppDefined,
    6040             :                                      "Unable to get scanline %d", j);
    6041             :                         }
    6042             :                     }
    6043             : 
    6044             :                     // Write data.
    6045          99 :                     if (bOK)
    6046             :                     {
    6047          99 :                         status = nc_put_vara_double(cdfid, nVarLatID, start,
    6048             :                                                     count, padLatVal);
    6049          99 :                         NCDF_ERR(status);
    6050          99 :                         status = nc_put_vara_double(cdfid, nVarLonID, start,
    6051             :                                                     count, padLonVal);
    6052          99 :                         NCDF_ERR(status);
    6053             :                     }
    6054             : 
    6055          99 :                     if (pfnProgress && (nRasterYSize / 10) > 0 &&
    6056          99 :                         (j % (nRasterYSize / 10) == 0))
    6057             :                     {
    6058          43 :                         dfProgress += 0.08;
    6059          43 :                         pfnProgress(dfProgress, nullptr, pProgressData);
    6060             :                     }
    6061             :                 }
    6062             :             }
    6063             :         }  // Projected
    6064             : 
    6065             :         // If not projected/geographic and has geoloc
    6066          27 :         else if (!bIsGeographic && bHasGeoloc && m_gt.IsAxisAligned())
    6067             :         {
    6068             :             // Use
    6069             :             // https://cfconventions.org/Data/cf-conventions/cf-conventions-1.9/cf-conventions.html#_two_dimensional_latitude_longitude_coordinate_variables
    6070             : 
    6071           4 :             bool bOK = true;
    6072           4 :             double dfProgress = 0.2;
    6073             : 
    6074             :             // Make sure we are in data mode.
    6075           4 :             SetDefineMode(false);
    6076             : 
    6077             :             size_t startX[1];
    6078             :             size_t countX[1];
    6079             :             size_t startY[1];
    6080             :             size_t countY[1];
    6081           4 :             startX[0] = 0;
    6082           4 :             countX[0] = nRasterXSize;
    6083             : 
    6084           4 :             startY[0] = 0;
    6085           4 :             countY[0] = nRasterYSize;
    6086             : 
    6087           4 :             std::vector<double> adfXVal;
    6088           4 :             std::vector<double> adfYVal;
    6089             :             try
    6090             :             {
    6091           4 :                 adfXVal.resize(nRasterXSize);
    6092           4 :                 adfYVal.resize(nRasterYSize);
    6093             :             }
    6094           0 :             catch (const std::exception &)
    6095             :             {
    6096           0 :                 CPLError(CE_Failure, CPLE_OutOfMemory,
    6097             :                          "Out of memory allocating temporary array");
    6098           0 :                 return CE_Failure;
    6099             :             }
    6100          16 :             for (int i = 0; i < nRasterXSize; i++)
    6101          12 :                 adfXVal[i] = i;
    6102          12 :             for (int i = 0; i < nRasterYSize; i++)
    6103           8 :                 adfYVal[i] = bBottomUp ? nRasterYSize - 1 - i : i;
    6104             : 
    6105           4 :             CPLDebug("GDAL_netCDF", "Writing X values");
    6106           4 :             int status = nc_put_vara_double(cdfid, nVarXID, startX, countX,
    6107           4 :                                             adfXVal.data());
    6108           4 :             NCDF_ERR(status);
    6109             : 
    6110           4 :             CPLDebug("GDAL_netCDF", "Writing Y values");
    6111           4 :             status = nc_put_vara_double(cdfid, nVarYID, startY, countY,
    6112           4 :                                         adfYVal.data());
    6113           4 :             NCDF_ERR(status);
    6114             : 
    6115           4 :             if (pfnProgress)
    6116           0 :                 pfnProgress(0.20, nullptr, pProgressData);
    6117             : 
    6118           4 :             size_t start[] = {0, 0};
    6119           4 :             size_t count[] = {1, (size_t)nRasterXSize};
    6120             : 
    6121             :             std::unique_ptr<double, decltype(&VSIFree)> adLatValKeeper(
    6122             :                 static_cast<double *>(
    6123           8 :                     VSI_MALLOC2_VERBOSE(nRasterXSize, sizeof(double))),
    6124           4 :                 VSIFree);
    6125             :             std::unique_ptr<double, decltype(&VSIFree)> adLonValKeeper(
    6126             :                 static_cast<double *>(
    6127           8 :                     VSI_MALLOC2_VERBOSE(nRasterXSize, sizeof(double))),
    6128           4 :                 VSIFree);
    6129           4 :             double *padLonVal = adLonValKeeper.get();
    6130           4 :             double *padLatVal = adLatValKeeper.get();
    6131           4 :             if (!padLonVal || !padLatVal)
    6132             :             {
    6133           0 :                 return CE_Failure;
    6134             :             }
    6135             : 
    6136          12 :             for (int j = 0; j < nRasterYSize && bOK && status == NC_NOERR; j++)
    6137             :             {
    6138           8 :                 start[0] = j;
    6139             : 
    6140           8 :                 CPLErr eErr = poBand_Y->RasterIO(
    6141           8 :                     GF_Read, 0, bBottomUp ? nRasterYSize - 1 - j : j,
    6142             :                     nRasterXSize, 1, padLatVal, nRasterXSize, 1, GDT_Float64, 0,
    6143             :                     0, nullptr);
    6144           8 :                 if (eErr == CE_None)
    6145             :                 {
    6146           8 :                     eErr = poBand_X->RasterIO(
    6147           8 :                         GF_Read, 0, bBottomUp ? nRasterYSize - 1 - j : j,
    6148             :                         nRasterXSize, 1, padLonVal, nRasterXSize, 1,
    6149             :                         GDT_Float64, 0, 0, nullptr);
    6150             :                 }
    6151             : 
    6152           8 :                 if (eErr == CE_None)
    6153             :                 {
    6154           8 :                     bOK = true;
    6155             :                 }
    6156             :                 else
    6157             :                 {
    6158           0 :                     bOK = false;
    6159           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    6160             :                              "Unable to get scanline %d", j);
    6161             :                 }
    6162             : 
    6163             :                 // Write data.
    6164           8 :                 if (bOK)
    6165             :                 {
    6166           8 :                     status = nc_put_vara_double(cdfid, nVarLatID, start, count,
    6167             :                                                 padLatVal);
    6168           8 :                     NCDF_ERR(status);
    6169           8 :                     status = nc_put_vara_double(cdfid, nVarLonID, start, count,
    6170             :                                                 padLonVal);
    6171           8 :                     NCDF_ERR(status);
    6172             :                 }
    6173             : 
    6174           8 :                 if (pfnProgress && (nRasterYSize / 10) > 0 &&
    6175           0 :                     (j % (nRasterYSize / 10) == 0))
    6176             :                 {
    6177           0 :                     dfProgress += 0.08;
    6178           0 :                     pfnProgress(dfProgress, nullptr, pProgressData);
    6179             :                 }
    6180             :             }
    6181             :         }
    6182             : 
    6183             :         // If not projected, assume geographic to catch grids without Datum.
    6184          23 :         else if (bWriteLonLat)
    6185             :         {
    6186             :             // Get latitude values.
    6187          23 :             const double dfY0 = (!bBottomUp) ? m_gt.yorig :
    6188             :                                              // Invert latitude values.
    6189          23 :                                     m_gt.yorig + (m_gt.yscale * nRasterYSize);
    6190          23 :             const double dfDY = m_gt.yscale;
    6191             : 
    6192             :             std::unique_ptr<double, decltype(&VSIFree)> adLatValKeeper(nullptr,
    6193          23 :                                                                        VSIFree);
    6194          23 :             double *padLatVal = nullptr;
    6195             :             // Override lat values with the ones in GEOLOCATION/Y_VALUES.
    6196          23 :             if (netCDFDataset::GetMetadataItem("Y_VALUES",
    6197          23 :                                                GDAL_MDD_GEOLOCATION) != nullptr)
    6198             :             {
    6199           0 :                 int nTemp = 0;
    6200           0 :                 adLatValKeeper.reset(Get1DGeolocation("Y_VALUES", nTemp));
    6201           0 :                 padLatVal = adLatValKeeper.get();
    6202             :                 // Make sure we got the correct amount, if not fallback to GT */
    6203             :                 // could add test fabs(fabs(padLatVal[0]) - fabs(dfY0)) <= 0.1))
    6204           0 :                 if (nTemp == nRasterYSize)
    6205             :                 {
    6206           0 :                     CPLDebug(
    6207             :                         "GDAL_netCDF",
    6208             :                         "Using Y_VALUES geolocation metadata for lat values");
    6209             :                 }
    6210             :                 else
    6211             :                 {
    6212           0 :                     CPLDebug("GDAL_netCDF",
    6213             :                              "Got %d elements from Y_VALUES geolocation "
    6214             :                              "metadata, need %d",
    6215             :                              nTemp, nRasterYSize);
    6216           0 :                     padLatVal = nullptr;
    6217             :                 }
    6218             :             }
    6219             : 
    6220          23 :             if (padLatVal == nullptr)
    6221             :             {
    6222          23 :                 adLatValKeeper.reset(static_cast<double *>(
    6223          23 :                     VSI_MALLOC2_VERBOSE(nRasterYSize, sizeof(double))));
    6224          23 :                 padLatVal = adLatValKeeper.get();
    6225          23 :                 if (!padLatVal)
    6226             :                 {
    6227           0 :                     return CE_Failure;
    6228             :                 }
    6229        3995 :                 for (int i = 0; i < nRasterYSize; i++)
    6230             :                 {
    6231             :                     // The data point is centered inside the pixel.
    6232        3972 :                     if (!bBottomUp)
    6233           0 :                         padLatVal[i] = dfY0 + (i + 0.5) * dfDY;
    6234             :                     else  // Invert latitude values.
    6235        3972 :                         padLatVal[i] = dfY0 - (i + 0.5) * dfDY;
    6236             :                 }
    6237             :             }
    6238             : 
    6239          23 :             size_t startLat[1] = {0};
    6240          23 :             size_t countLat[1] = {static_cast<size_t>(nRasterYSize)};
    6241             : 
    6242             :             // Get longitude values.
    6243          23 :             const double dfX0 = m_gt.xorig;
    6244          23 :             const double dfDX = m_gt.xscale;
    6245             : 
    6246             :             std::unique_ptr<double, decltype(&VSIFree)> adLonValKeeper(
    6247             :                 static_cast<double *>(
    6248          46 :                     VSI_MALLOC2_VERBOSE(nRasterXSize, sizeof(double))),
    6249          23 :                 VSIFree);
    6250          23 :             double *padLonVal = adLonValKeeper.get();
    6251          23 :             if (!padLonVal)
    6252             :             {
    6253           0 :                 return CE_Failure;
    6254             :             }
    6255        4047 :             for (int i = 0; i < nRasterXSize; i++)
    6256             :             {
    6257             :                 // The data point is centered inside the pixel.
    6258        4024 :                 padLonVal[i] = dfX0 + (i + 0.5) * dfDX;
    6259             :             }
    6260             : 
    6261          23 :             size_t startLon[1] = {0};
    6262          23 :             size_t countLon[1] = {static_cast<size_t>(nRasterXSize)};
    6263             : 
    6264             :             // Write latitude and longitude values.
    6265             : 
    6266             :             // Make sure we are in data mode.
    6267          23 :             SetDefineMode(false);
    6268             : 
    6269             :             // Write values.
    6270          23 :             CPLDebug("GDAL_netCDF", "Writing lat values");
    6271             : 
    6272          23 :             int status = nc_put_vara_double(cdfid, nVarLatID, startLat,
    6273             :                                             countLat, padLatVal);
    6274          23 :             NCDF_ERR(status);
    6275             : 
    6276          23 :             CPLDebug("GDAL_netCDF", "Writing lon values");
    6277          23 :             status = nc_put_vara_double(cdfid, nVarLonID, startLon, countLon,
    6278             :                                         padLonVal);
    6279          23 :             NCDF_ERR(status);
    6280             : 
    6281             :         }  // Not projected.
    6282             : 
    6283          63 :         if (pfnProgress)
    6284          53 :             pfnProgress(1.00, nullptr, pProgressData);
    6285             :     }
    6286             : 
    6287         126 :     return CE_None;
    6288             : }
    6289             : 
    6290             : // Write Projection variable to band variable.
    6291             : // Moved from AddProjectionVars() for cases when bands are added after
    6292             : // projection.
    6293         490 : bool netCDFDataset::AddGridMappingRef()
    6294             : {
    6295         490 :     bool bRet = true;
    6296         490 :     bool bOldDefineMode = bDefineMode;
    6297             : 
    6298         675 :     if ((GetAccess() == GA_Update) && (nBands >= 1) && (GetRasterBand(1)) &&
    6299         185 :         ((pszCFCoordinates != nullptr && !EQUAL(pszCFCoordinates, "")) ||
    6300         177 :          (pszCFProjection != nullptr && !EQUAL(pszCFProjection, ""))))
    6301             :     {
    6302          53 :         bAddedGridMappingRef = true;
    6303             : 
    6304             :         // Make sure we are in define mode.
    6305          53 :         SetDefineMode(true);
    6306             : 
    6307         116 :         for (int i = 1; i <= nBands; i++)
    6308             :         {
    6309             :             const int nVarId =
    6310          63 :                 cpl::down_cast<netCDFRasterBand *>(GetRasterBand(i))->nZId;
    6311             : 
    6312          63 :             if (pszCFProjection != nullptr && !EQUAL(pszCFProjection, ""))
    6313             :             {
    6314             :                 int status =
    6315         118 :                     nc_put_att_text(cdfid, nVarId, CF_GRD_MAPPING,
    6316          59 :                                     strlen(pszCFProjection), pszCFProjection);
    6317          59 :                 NCDF_ERR(status);
    6318          59 :                 if (status != NC_NOERR)
    6319           0 :                     bRet = false;
    6320             :             }
    6321          63 :             if (pszCFCoordinates != nullptr && !EQUAL(pszCFCoordinates, ""))
    6322             :             {
    6323             :                 int status =
    6324           8 :                     nc_put_att_text(cdfid, nVarId, CF_COORDINATES,
    6325             :                                     strlen(pszCFCoordinates), pszCFCoordinates);
    6326           8 :                 NCDF_ERR(status);
    6327           8 :                 if (status != NC_NOERR)
    6328           0 :                     bRet = false;
    6329             :             }
    6330             :         }
    6331             : 
    6332             :         // Go back to previous define mode.
    6333          53 :         SetDefineMode(bOldDefineMode);
    6334             :     }
    6335         490 :     return bRet;
    6336             : }
    6337             : 
    6338             : /************************************************************************/
    6339             : /*                          GetGeoTransform()                           */
    6340             : /************************************************************************/
    6341             : 
    6342         105 : CPLErr netCDFDataset::GetGeoTransform(GDALGeoTransform &gt) const
    6343             : 
    6344             : {
    6345         105 :     gt = m_gt;
    6346         105 :     if (m_bHasGeoTransform)
    6347          73 :         return CE_None;
    6348             : 
    6349          32 :     return GDALPamDataset::GetGeoTransform(gt);
    6350             : }
    6351             : 
    6352             : /************************************************************************/
    6353             : /*                                rint()                                */
    6354             : /************************************************************************/
    6355             : 
    6356           0 : double netCDFDataset::rint(double dfX)
    6357             : {
    6358           0 :     return std::round(dfX);
    6359             : }
    6360             : 
    6361             : /************************************************************************/
    6362             : /*                        NCDFReadIsoMetadata()                         */
    6363             : /************************************************************************/
    6364             : 
    6365          16 : static void NCDFReadMetadataAsJson(int cdfid, CPLJSONObject &obj)
    6366             : {
    6367          16 :     int nbAttr = 0;
    6368          16 :     NCDF_ERR(nc_inq_varnatts(cdfid, NC_GLOBAL, &nbAttr));
    6369             : 
    6370          32 :     std::map<std::string, CPLJSONArray> oMapNameToArray;
    6371          40 :     for (int l = 0; l < nbAttr; l++)
    6372             :     {
    6373             :         char szAttrName[NC_MAX_NAME + 1];
    6374          24 :         szAttrName[0] = 0;
    6375          24 :         NCDF_ERR(nc_inq_attname(cdfid, NC_GLOBAL, l, szAttrName));
    6376             : 
    6377          24 :         char *pszMetaValue = nullptr;
    6378          24 :         if (NCDFGetAttr(cdfid, NC_GLOBAL, szAttrName, &pszMetaValue) == CE_None)
    6379             :         {
    6380          24 :             nc_type nAttrType = NC_NAT;
    6381          24 :             size_t nAttrLen = 0;
    6382             : 
    6383          24 :             NCDF_ERR(nc_inq_att(cdfid, NC_GLOBAL, szAttrName, &nAttrType,
    6384             :                                 &nAttrLen));
    6385             : 
    6386          24 :             std::string osAttrName(szAttrName);
    6387          24 :             const auto sharpPos = osAttrName.find('#');
    6388          24 :             if (sharpPos == std::string::npos)
    6389             :             {
    6390          16 :                 if (nAttrType == NC_DOUBLE || nAttrType == NC_FLOAT)
    6391           4 :                     obj.Add(osAttrName, CPLAtof(pszMetaValue));
    6392             :                 else
    6393          12 :                     obj.Add(osAttrName, pszMetaValue);
    6394             :             }
    6395             :             else
    6396             :             {
    6397           8 :                 osAttrName.resize(sharpPos);
    6398           8 :                 auto iter = oMapNameToArray.find(osAttrName);
    6399           8 :                 if (iter == oMapNameToArray.end())
    6400             :                 {
    6401           8 :                     CPLJSONArray array;
    6402           4 :                     obj.Add(osAttrName, array);
    6403           4 :                     oMapNameToArray[osAttrName] = array;
    6404           4 :                     array.Add(pszMetaValue);
    6405             :                 }
    6406             :                 else
    6407             :                 {
    6408           4 :                     iter->second.Add(pszMetaValue);
    6409             :                 }
    6410             :             }
    6411          24 :             CPLFree(pszMetaValue);
    6412          24 :             pszMetaValue = nullptr;
    6413             :         }
    6414             :     }
    6415             : 
    6416          16 :     int nSubGroups = 0;
    6417          16 :     int *panSubGroupIds = nullptr;
    6418          16 :     NCDFGetSubGroups(cdfid, &nSubGroups, &panSubGroupIds);
    6419          16 :     oMapNameToArray.clear();
    6420          28 :     for (int i = 0; i < nSubGroups; i++)
    6421             :     {
    6422          24 :         CPLJSONObject subObj;
    6423          12 :         NCDFReadMetadataAsJson(panSubGroupIds[i], subObj);
    6424             : 
    6425          24 :         std::string osGroupName;
    6426          12 :         osGroupName.resize(NC_MAX_NAME);
    6427          12 :         NCDF_ERR(nc_inq_grpname(panSubGroupIds[i], &osGroupName[0]));
    6428          12 :         osGroupName.resize(strlen(osGroupName.data()));
    6429          12 :         const auto sharpPos = osGroupName.find('#');
    6430          12 :         if (sharpPos == std::string::npos)
    6431             :         {
    6432           4 :             obj.Add(osGroupName, subObj);
    6433             :         }
    6434             :         else
    6435             :         {
    6436           8 :             osGroupName.resize(sharpPos);
    6437           8 :             auto iter = oMapNameToArray.find(osGroupName);
    6438           8 :             if (iter == oMapNameToArray.end())
    6439             :             {
    6440           8 :                 CPLJSONArray array;
    6441           4 :                 obj.Add(osGroupName, array);
    6442           4 :                 oMapNameToArray[osGroupName] = array;
    6443           4 :                 array.Add(subObj);
    6444             :             }
    6445             :             else
    6446             :             {
    6447           4 :                 iter->second.Add(subObj);
    6448             :             }
    6449             :         }
    6450             :     }
    6451          16 :     CPLFree(panSubGroupIds);
    6452          16 : }
    6453             : 
    6454           4 : std::string NCDFReadMetadataAsJson(int cdfid)
    6455             : {
    6456           8 :     CPLJSONDocument oDoc;
    6457           8 :     CPLJSONObject oRoot = oDoc.GetRoot();
    6458           4 :     NCDFReadMetadataAsJson(cdfid, oRoot);
    6459           8 :     return oDoc.SaveAsString();
    6460             : }
    6461             : 
    6462             : /************************************************************************/
    6463             : /*                           ReadAttributes()                           */
    6464             : /************************************************************************/
    6465        2017 : CPLErr netCDFDataset::ReadAttributes(int cdfidIn, int var)
    6466             : 
    6467             : {
    6468        4034 :     std::string osVarFullName;
    6469        2017 :     ERR_RET(NCDFGetVarFullName(cdfidIn, var, osVarFullName));
    6470             : 
    6471             :     // For metadata in Sentinel 5
    6472        2017 :     if (cpl::starts_with(osVarFullName, "/METADATA/"))
    6473             :     {
    6474           6 :         for (const char *key :
    6475             :              {"ISO_METADATA", "ESA_METADATA", "EOP_METADATA", "QA_STATISTICS",
    6476           8 :               "GRANULE_DESCRIPTION", "ALGORITHM_SETTINGS"})
    6477             :         {
    6478          14 :             if (var == NC_GLOBAL &&
    6479          14 :                 osVarFullName == CPLOPrintf("/METADATA/%s/NC_GLOBAL", key))
    6480             :             {
    6481           1 :                 CPLStringList aosList;
    6482           2 :                 aosList.AddString(CPLString(NCDFReadMetadataAsJson(cdfidIn))
    6483           1 :                                       .replaceAll("\\/", '/'));
    6484           1 :                 m_oMapDomainToJSon[key] = std::move(aosList);
    6485           1 :                 return CE_None;
    6486             :             }
    6487             :         }
    6488             :     }
    6489        2016 :     if (cpl::starts_with(osVarFullName, "/PRODUCT/SUPPORT_DATA/"))
    6490             :     {
    6491           0 :         CPLStringList aosList;
    6492             :         aosList.AddString(
    6493           0 :             CPLString(NCDFReadMetadataAsJson(cdfidIn)).replaceAll("\\/", '/'));
    6494           0 :         m_oMapDomainToJSon["SUPPORT_DATA"] = std::move(aosList);
    6495           0 :         return CE_None;
    6496             :     }
    6497             : 
    6498             :     size_t nMetaNameSize =
    6499        2016 :         sizeof(char) * (osVarFullName.size() + 1 + NC_MAX_NAME + 1);
    6500        2016 :     char *pszMetaName = static_cast<char *>(CPLMalloc(nMetaNameSize));
    6501             : 
    6502        2016 :     int nbAttr = 0;
    6503        2016 :     NCDF_ERR(nc_inq_varnatts(cdfidIn, var, &nbAttr));
    6504             : 
    6505       10549 :     for (int l = 0; l < nbAttr; l++)
    6506             :     {
    6507             :         char szAttrName[NC_MAX_NAME + 1];
    6508        8533 :         szAttrName[0] = 0;
    6509        8533 :         NCDF_ERR(nc_inq_attname(cdfidIn, var, l, szAttrName));
    6510        8533 :         snprintf(pszMetaName, nMetaNameSize, "%s#%s", osVarFullName.c_str(),
    6511             :                  szAttrName);
    6512             : 
    6513        8533 :         char *pszMetaTemp = nullptr;
    6514        8533 :         if (NCDFGetAttr(cdfidIn, var, szAttrName, &pszMetaTemp) == CE_None)
    6515             :         {
    6516        8532 :             aosMetadata.SetNameValue(pszMetaName, pszMetaTemp);
    6517        8532 :             CPLFree(pszMetaTemp);
    6518        8532 :             pszMetaTemp = nullptr;
    6519             :         }
    6520             :         else
    6521             :         {
    6522           1 :             CPLDebug("GDAL_netCDF", "invalid metadata %s", pszMetaName);
    6523             :         }
    6524             :     }
    6525             : 
    6526        2016 :     CPLFree(pszMetaName);
    6527             : 
    6528        2016 :     if (var == NC_GLOBAL)
    6529             :     {
    6530             :         // Recurse on sub-groups.
    6531         561 :         int nSubGroups = 0;
    6532         561 :         int *panSubGroupIds = nullptr;
    6533         561 :         NCDFGetSubGroups(cdfidIn, &nSubGroups, &panSubGroupIds);
    6534         595 :         for (int i = 0; i < nSubGroups; i++)
    6535             :         {
    6536          34 :             ReadAttributes(panSubGroupIds[i], var);
    6537             :         }
    6538         561 :         CPLFree(panSubGroupIds);
    6539             :     }
    6540             : 
    6541        2016 :     return CE_None;
    6542             : }
    6543             : 
    6544             : /************************************************************************/
    6545             : /*                netCDFDataset::CreateSubDatasetList()                 */
    6546             : /************************************************************************/
    6547          48 : void netCDFDataset::CreateSubDatasetList(int nGroupId)
    6548             : {
    6549          96 :     std::string osVarStdName;
    6550          48 :     int *ponDimIds = nullptr;
    6551             : 
    6552          48 :     netCDFDataset *poDS = this;
    6553             : 
    6554             :     int nVarCount;
    6555          48 :     nc_inq_nvars(nGroupId, &nVarCount);
    6556             : 
    6557          48 :     const bool bListAllArrays = CPLTestBool(
    6558          48 :         CSLFetchNameValueDef(papszOpenOptions, "LIST_ALL_ARRAYS", "NO"));
    6559             : 
    6560         273 :     for (int nVar = 0; nVar < nVarCount; nVar++)
    6561             :     {
    6562             : 
    6563             :         int nDims;
    6564         225 :         nc_inq_varndims(nGroupId, nVar, &nDims);
    6565             : 
    6566         225 :         if ((bListAllArrays && nDims > 0) || nDims >= 2)
    6567             :         {
    6568         136 :             ponDimIds = static_cast<int *>(CPLCalloc(nDims, sizeof(int)));
    6569         136 :             nc_inq_vardimid(nGroupId, nVar, ponDimIds);
    6570             : 
    6571             :             // Create Sub dataset list.
    6572         136 :             CPLString osDim;
    6573         422 :             for (int i = 0; i < nDims; i++)
    6574             :             {
    6575             :                 size_t nDimLen;
    6576         286 :                 nc_inq_dimlen(nGroupId, ponDimIds[i], &nDimLen);
    6577         286 :                 if (!osDim.empty())
    6578         150 :                     osDim += 'x';
    6579         286 :                 osDim += CPLSPrintf("%d", (int)nDimLen);
    6580             :             }
    6581         136 :             CPLFree(ponDimIds);
    6582             : 
    6583             :             nc_type nVarType;
    6584         136 :             nc_inq_vartype(nGroupId, nVar, &nVarType);
    6585         136 :             const char *pszType = "";
    6586         136 :             switch (nVarType)
    6587             :             {
    6588          25 :                 case NC_BYTE:
    6589          25 :                     pszType = "8-bit integer";
    6590          25 :                     break;
    6591           2 :                 case NC_CHAR:
    6592           2 :                     pszType = "8-bit character";
    6593           2 :                     break;
    6594           3 :                 case NC_SHORT:
    6595           3 :                     pszType = "16-bit integer";
    6596           3 :                     break;
    6597           7 :                 case NC_INT:
    6598           7 :                     pszType = "32-bit integer";
    6599           7 :                     break;
    6600          47 :                 case NC_FLOAT:
    6601          47 :                     pszType = "32-bit floating-point";
    6602          47 :                     break;
    6603          31 :                 case NC_DOUBLE:
    6604          31 :                     pszType = "64-bit floating-point";
    6605          31 :                     break;
    6606           4 :                 case NC_UBYTE:
    6607           4 :                     pszType = "8-bit unsigned integer";
    6608           4 :                     break;
    6609           1 :                 case NC_USHORT:
    6610           1 :                     pszType = "16-bit unsigned integer";
    6611           1 :                     break;
    6612           1 :                 case NC_UINT:
    6613           1 :                     pszType = "32-bit unsigned integer";
    6614           1 :                     break;
    6615           1 :                 case NC_INT64:
    6616           1 :                     pszType = "64-bit integer";
    6617           1 :                     break;
    6618           1 :                 case NC_UINT64:
    6619           1 :                     pszType = "64-bit unsigned integer";
    6620           1 :                     break;
    6621          13 :                 default:
    6622          13 :                     break;
    6623             :             }
    6624             : 
    6625         136 :             std::string osVarName;
    6626         136 :             if (NCDFGetVarFullName(nGroupId, nVar, osVarName) != CE_None)
    6627           0 :                 continue;
    6628             : 
    6629         136 :             nSubDatasets++;
    6630             : 
    6631         136 :             if (NCDFGetAttr(nGroupId, nVar, CF_STD_NAME, osVarStdName) !=
    6632             :                 CE_None)
    6633             :             {
    6634          72 :                 osVarStdName = osVarName;
    6635             :             }
    6636             : 
    6637             :             const std::string osSubDatasetName =
    6638         272 :                 CPLOPrintf("SUBDATASET_%d_NAME", nSubDatasets);
    6639             : 
    6640         272 :             if (osVarName.find(' ') != std::string::npos ||
    6641         136 :                 osVarName.find(':') != std::string::npos)
    6642             :             {
    6643             :                 poDS->aosSubDatasets.SetNameValue(
    6644             :                     osSubDatasetName.c_str(),
    6645             :                     CPLSPrintf(R"(NETCDF:"%s":"%s")", poDS->osFilename.c_str(),
    6646           1 :                                osVarName.c_str()));
    6647             :             }
    6648             :             else
    6649             :             {
    6650             :                 poDS->aosSubDatasets.SetNameValue(
    6651             :                     osSubDatasetName.c_str(),
    6652             :                     CPLSPrintf("NETCDF:\"%s\":%s", poDS->osFilename.c_str(),
    6653         135 :                                osVarName.c_str()));
    6654             :             }
    6655             : 
    6656             :             const std::string osSubDatasetDesc =
    6657         272 :                 CPLOPrintf("SUBDATASET_%d_DESC", nSubDatasets);
    6658             : 
    6659             :             poDS->aosSubDatasets.SetNameValue(
    6660             :                 osSubDatasetDesc.c_str(),
    6661             :                 CPLSPrintf("[%s] %s (%s)", osDim.c_str(), osVarStdName.c_str(),
    6662         136 :                            pszType));
    6663             :         }
    6664             :     }
    6665             : 
    6666             :     // Recurse on sub groups.
    6667          48 :     int nSubGroups = 0;
    6668          48 :     int *panSubGroupIds = nullptr;
    6669          48 :     NCDFGetSubGroups(nGroupId, &nSubGroups, &panSubGroupIds);
    6670          56 :     for (int i = 0; i < nSubGroups; i++)
    6671             :     {
    6672           8 :         CreateSubDatasetList(panSubGroupIds[i]);
    6673             :     }
    6674          48 :     CPLFree(panSubGroupIds);
    6675          48 : }
    6676             : 
    6677             : /************************************************************************/
    6678             : /*                           TestCapability()                           */
    6679             : /************************************************************************/
    6680             : 
    6681         244 : bool netCDFDataset::TestCapability(const char *pszCap) const
    6682             : {
    6683         244 :     if (EQUAL(pszCap, ODsCCreateLayer))
    6684             :     {
    6685         221 :         return eAccess == GA_Update && nBands == 0 &&
    6686         215 :                (eMultipleLayerBehavior != SINGLE_LAYER ||
    6687         226 :                 this->GetLayerCount() == 0 || bSGSupport);
    6688             :     }
    6689         133 :     else if (EQUAL(pszCap, ODsCZGeometries))
    6690           2 :         return true;
    6691             : 
    6692         131 :     return false;
    6693             : }
    6694             : 
    6695             : /************************************************************************/
    6696             : /*                              GetLayer()                              */
    6697             : /************************************************************************/
    6698             : 
    6699         443 : const OGRLayer *netCDFDataset::GetLayer(int nIdx) const
    6700             : {
    6701         443 :     if (nIdx < 0 || nIdx >= this->GetLayerCount())
    6702           2 :         return nullptr;
    6703         441 :     return papoLayers[nIdx].get();
    6704             : }
    6705             : 
    6706             : /************************************************************************/
    6707             : /*                            ICreateLayer()                            */
    6708             : /************************************************************************/
    6709             : 
    6710          59 : OGRLayer *netCDFDataset::ICreateLayer(const char *pszName,
    6711             :                                       const OGRGeomFieldDefn *poGeomFieldDefn,
    6712             :                                       CSLConstList papszOptions)
    6713             : {
    6714          59 :     int nLayerCDFId = cdfid;
    6715          59 :     if (!TestCapability(ODsCCreateLayer))
    6716           0 :         return nullptr;
    6717             : 
    6718          59 :     const auto eGType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
    6719             :     const auto poSpatialRef =
    6720          59 :         poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
    6721             : 
    6722         118 :     CPLString osNetCDFLayerName(pszName);
    6723          59 :     const netCDFWriterConfigLayer *poLayerConfig = nullptr;
    6724          59 :     if (oWriterConfig.m_bIsValid)
    6725             :     {
    6726             :         std::map<CPLString, netCDFWriterConfigLayer>::const_iterator
    6727           3 :             oLayerIter = oWriterConfig.m_oLayers.find(pszName);
    6728           3 :         if (oLayerIter != oWriterConfig.m_oLayers.end())
    6729             :         {
    6730           1 :             poLayerConfig = &(oLayerIter->second);
    6731           1 :             osNetCDFLayerName = poLayerConfig->m_osNetCDFName;
    6732             :         }
    6733             :     }
    6734             : 
    6735          59 :     netCDFDataset *poLayerDataset = nullptr;
    6736          59 :     if (eMultipleLayerBehavior == SEPARATE_FILES)
    6737             :     {
    6738           3 :         if (CPLLaunderForFilenameSafe(osNetCDFLayerName.c_str(), nullptr) !=
    6739             :             osNetCDFLayerName)
    6740             :         {
    6741           1 :             CPLError(CE_Failure, CPLE_AppDefined,
    6742             :                      "Illegal characters in '%s' to form a valid filename",
    6743             :                      osNetCDFLayerName.c_str());
    6744           1 :             return nullptr;
    6745             :         }
    6746           2 :         CPLStringList aosDatasetOptions;
    6747             :         aosDatasetOptions.SetNameValue(
    6748           2 :             "CONFIG_FILE", aosCreationOptions.FetchNameValue("CONFIG_FILE"));
    6749             :         aosDatasetOptions.SetNameValue(
    6750           2 :             "FORMAT", aosCreationOptions.FetchNameValue("FORMAT"));
    6751             :         aosDatasetOptions.SetNameValue(
    6752             :             "WRITE_GDAL_TAGS",
    6753           2 :             aosCreationOptions.FetchNameValue("WRITE_GDAL_TAGS"));
    6754             :         const CPLString osLayerFilename(
    6755           2 :             CPLFormFilenameSafe(osFilename, osNetCDFLayerName, "nc"));
    6756           2 :         CPLAcquireMutex(hNCMutex, 1000.0);
    6757           2 :         poLayerDataset =
    6758           2 :             CreateLL(osLayerFilename, 0, 0, 0, aosDatasetOptions.List());
    6759           2 :         CPLReleaseMutex(hNCMutex);
    6760           2 :         if (poLayerDataset == nullptr)
    6761           0 :             return nullptr;
    6762             : 
    6763           2 :         nLayerCDFId = poLayerDataset->cdfid;
    6764           2 :         NCDFAddGDALHistory(nLayerCDFId, osLayerFilename, bWriteGDALVersion,
    6765           2 :                            bWriteGDALHistory, "", "Create",
    6766             :                            NCDF_CONVENTIONS_CF_V1_6);
    6767             :     }
    6768          56 :     else if (eMultipleLayerBehavior == SEPARATE_GROUPS)
    6769             :     {
    6770           2 :         SetDefineMode(true);
    6771             : 
    6772           2 :         nLayerCDFId = -1;
    6773           2 :         int status = nc_def_grp(cdfid, osNetCDFLayerName, &nLayerCDFId);
    6774           2 :         NCDF_ERR(status);
    6775           2 :         if (status != NC_NOERR)
    6776           0 :             return nullptr;
    6777             : 
    6778           2 :         NCDFAddGDALHistory(nLayerCDFId, osFilename, bWriteGDALVersion,
    6779           2 :                            bWriteGDALHistory, "", "Create",
    6780             :                            NCDF_CONVENTIONS_CF_V1_6);
    6781             :     }
    6782             : 
    6783             :     // Make a clone to workaround a bug in released MapServer versions
    6784             :     // that destroys the passed SRS instead of releasing it .
    6785          58 :     OGRSpatialReference *poSRS = nullptr;
    6786          58 :     if (poSpatialRef)
    6787             :     {
    6788          43 :         poSRS = poSpatialRef->Clone();
    6789          43 :         poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    6790             :     }
    6791             :     auto poLayer = std::make_shared<netCDFLayer>(
    6792          58 :         poLayerDataset ? poLayerDataset : this, nLayerCDFId, osNetCDFLayerName,
    6793         116 :         eGType, poSRS);
    6794          58 :     if (poSRS != nullptr)
    6795          43 :         poSRS->Release();
    6796             : 
    6797             :     // Fetch layer creation options coming from config file
    6798         116 :     CPLStringList aosNewOptions(CSLDuplicate(papszOptions));
    6799          58 :     if (oWriterConfig.m_bIsValid)
    6800             :     {
    6801           2 :         for (const auto &[osName, osValue] :
    6802           5 :              oWriterConfig.m_oLayerCreationOptions)
    6803             :         {
    6804           1 :             aosNewOptions.SetNameValue(osName, osValue);
    6805             :         }
    6806           3 :         if (poLayerConfig != nullptr)
    6807             :         {
    6808           4 :             for (const auto &[osName, osValue] :
    6809           5 :                  poLayerConfig->m_oLayerCreationOptions)
    6810             :             {
    6811           2 :                 aosNewOptions.SetNameValue(osName, osValue);
    6812             :             }
    6813             :         }
    6814             :     }
    6815             : 
    6816          58 :     const bool bRet = poLayer->Create(aosNewOptions.List(), poLayerConfig);
    6817             : 
    6818          58 :     if (!bRet)
    6819             :     {
    6820           0 :         return nullptr;
    6821             :     }
    6822             : 
    6823          58 :     if (poLayerDataset != nullptr)
    6824           2 :         apoVectorDatasets.push_back(poLayerDataset);
    6825             : 
    6826          58 :     papoLayers.push_back(poLayer);
    6827          58 :     return poLayer.get();
    6828             : }
    6829             : 
    6830             : /************************************************************************/
    6831             : /*                          CloneAttributes()                           */
    6832             : /************************************************************************/
    6833             : 
    6834         137 : bool netCDFDataset::CloneAttributes(int old_cdfid, int new_cdfid, int nSrcVarId,
    6835             :                                     int nDstVarId)
    6836             : {
    6837         137 :     int nAttCount = -1;
    6838         137 :     int status = nc_inq_varnatts(old_cdfid, nSrcVarId, &nAttCount);
    6839         137 :     NCDF_ERR(status);
    6840             : 
    6841         693 :     for (int i = 0; i < nAttCount; i++)
    6842             :     {
    6843             :         char szName[NC_MAX_NAME + 1];
    6844         556 :         szName[0] = 0;
    6845         556 :         status = nc_inq_attname(old_cdfid, nSrcVarId, i, szName);
    6846         556 :         NCDF_ERR(status);
    6847             : 
    6848             :         status =
    6849         556 :             nc_copy_att(old_cdfid, nSrcVarId, szName, new_cdfid, nDstVarId);
    6850         556 :         NCDF_ERR(status);
    6851         556 :         if (status != NC_NOERR)
    6852           0 :             return false;
    6853             :     }
    6854             : 
    6855         137 :     return true;
    6856             : }
    6857             : 
    6858             : /************************************************************************/
    6859             : /*                        CloneVariableContent()                        */
    6860             : /************************************************************************/
    6861             : 
    6862         121 : bool netCDFDataset::CloneVariableContent(int old_cdfid, int new_cdfid,
    6863             :                                          int nSrcVarId, int nDstVarId)
    6864             : {
    6865         121 :     int nVarDimCount = -1;
    6866         121 :     int status = nc_inq_varndims(old_cdfid, nSrcVarId, &nVarDimCount);
    6867         121 :     NCDF_ERR(status);
    6868         121 :     int anDimIds[] = {-1, 1};
    6869         121 :     status = nc_inq_vardimid(old_cdfid, nSrcVarId, anDimIds);
    6870         121 :     NCDF_ERR(status);
    6871         121 :     nc_type nc_datatype = NC_NAT;
    6872         121 :     status = nc_inq_vartype(old_cdfid, nSrcVarId, &nc_datatype);
    6873         121 :     NCDF_ERR(status);
    6874         121 :     size_t nTypeSize = 0;
    6875         121 :     switch (nc_datatype)
    6876             :     {
    6877          35 :         case NC_BYTE:
    6878             :         case NC_CHAR:
    6879          35 :             nTypeSize = 1;
    6880          35 :             break;
    6881           4 :         case NC_SHORT:
    6882           4 :             nTypeSize = 2;
    6883           4 :             break;
    6884          24 :         case NC_INT:
    6885          24 :             nTypeSize = 4;
    6886          24 :             break;
    6887           4 :         case NC_FLOAT:
    6888           4 :             nTypeSize = 4;
    6889           4 :             break;
    6890          43 :         case NC_DOUBLE:
    6891          43 :             nTypeSize = 8;
    6892          43 :             break;
    6893           2 :         case NC_UBYTE:
    6894           2 :             nTypeSize = 1;
    6895           2 :             break;
    6896           2 :         case NC_USHORT:
    6897           2 :             nTypeSize = 2;
    6898           2 :             break;
    6899           2 :         case NC_UINT:
    6900           2 :             nTypeSize = 4;
    6901           2 :             break;
    6902           4 :         case NC_INT64:
    6903             :         case NC_UINT64:
    6904           4 :             nTypeSize = 8;
    6905           4 :             break;
    6906           1 :         case NC_STRING:
    6907           1 :             nTypeSize = sizeof(char *);
    6908           1 :             break;
    6909           0 :         default:
    6910             :         {
    6911           0 :             CPLError(CE_Failure, CPLE_NotSupported, "Unsupported data type: %d",
    6912             :                      nc_datatype);
    6913           0 :             return false;
    6914             :         }
    6915             :     }
    6916             : 
    6917         121 :     size_t nElems = 1;
    6918             :     size_t anStart[NC_MAX_DIMS];
    6919             :     size_t anCount[NC_MAX_DIMS];
    6920         121 :     size_t nRecords = 1;
    6921         261 :     for (int i = 0; i < nVarDimCount; i++)
    6922             :     {
    6923         140 :         anStart[i] = 0;
    6924         140 :         if (i == 0)
    6925             :         {
    6926         116 :             anCount[i] = 1;
    6927         116 :             status = nc_inq_dimlen(old_cdfid, anDimIds[i], &nRecords);
    6928         116 :             NCDF_ERR(status);
    6929             :         }
    6930             :         else
    6931             :         {
    6932          24 :             anCount[i] = 0;
    6933          24 :             status = nc_inq_dimlen(old_cdfid, anDimIds[i], &anCount[i]);
    6934          24 :             NCDF_ERR(status);
    6935          24 :             nElems *= anCount[i];
    6936             :         }
    6937             :     }
    6938             : 
    6939             :     /* Workaround in some cases a netCDF bug:
    6940             :      * https://github.com/Unidata/netcdf-c/pull/1442 */
    6941         121 :     if (nRecords > 0 && nRecords < 10 * 1000 * 1000 / (nElems * nTypeSize))
    6942             :     {
    6943         119 :         nElems *= nRecords;
    6944         119 :         anCount[0] = nRecords;
    6945         119 :         nRecords = 1;
    6946             :     }
    6947             : 
    6948         121 :     void *pBuffer = VSI_MALLOC2_VERBOSE(nElems, nTypeSize);
    6949         121 :     if (pBuffer == nullptr)
    6950           0 :         return false;
    6951             : 
    6952         240 :     for (size_t iRecord = 0; iRecord < nRecords; iRecord++)
    6953             :     {
    6954         119 :         anStart[0] = iRecord;
    6955             : 
    6956         119 :         switch (nc_datatype)
    6957             :         {
    6958           5 :             case NC_BYTE:
    6959             :                 status =
    6960           5 :                     nc_get_vara_schar(old_cdfid, nSrcVarId, anStart, anCount,
    6961             :                                       static_cast<signed char *>(pBuffer));
    6962           5 :                 if (!status)
    6963           5 :                     status = nc_put_vara_schar(
    6964             :                         new_cdfid, nDstVarId, anStart, anCount,
    6965             :                         static_cast<signed char *>(pBuffer));
    6966           5 :                 break;
    6967          28 :             case NC_CHAR:
    6968             :                 status =
    6969          28 :                     nc_get_vara_text(old_cdfid, nSrcVarId, anStart, anCount,
    6970             :                                      static_cast<char *>(pBuffer));
    6971          28 :                 if (!status)
    6972             :                     status =
    6973          28 :                         nc_put_vara_text(new_cdfid, nDstVarId, anStart, anCount,
    6974             :                                          static_cast<char *>(pBuffer));
    6975          28 :                 break;
    6976           4 :             case NC_SHORT:
    6977             :                 status =
    6978           4 :                     nc_get_vara_short(old_cdfid, nSrcVarId, anStart, anCount,
    6979             :                                       static_cast<short *>(pBuffer));
    6980           4 :                 if (!status)
    6981           4 :                     status = nc_put_vara_short(new_cdfid, nDstVarId, anStart,
    6982             :                                                anCount,
    6983             :                                                static_cast<short *>(pBuffer));
    6984           4 :                 break;
    6985          24 :             case NC_INT:
    6986          24 :                 status = nc_get_vara_int(old_cdfid, nSrcVarId, anStart, anCount,
    6987             :                                          static_cast<int *>(pBuffer));
    6988          24 :                 if (!status)
    6989             :                     status =
    6990          24 :                         nc_put_vara_int(new_cdfid, nDstVarId, anStart, anCount,
    6991             :                                         static_cast<int *>(pBuffer));
    6992          24 :                 break;
    6993           4 :             case NC_FLOAT:
    6994             :                 status =
    6995           4 :                     nc_get_vara_float(old_cdfid, nSrcVarId, anStart, anCount,
    6996             :                                       static_cast<float *>(pBuffer));
    6997           4 :                 if (!status)
    6998           4 :                     status = nc_put_vara_float(new_cdfid, nDstVarId, anStart,
    6999             :                                                anCount,
    7000             :                                                static_cast<float *>(pBuffer));
    7001           4 :                 break;
    7002          43 :             case NC_DOUBLE:
    7003             :                 status =
    7004          43 :                     nc_get_vara_double(old_cdfid, nSrcVarId, anStart, anCount,
    7005             :                                        static_cast<double *>(pBuffer));
    7006          43 :                 if (!status)
    7007          43 :                     status = nc_put_vara_double(new_cdfid, nDstVarId, anStart,
    7008             :                                                 anCount,
    7009             :                                                 static_cast<double *>(pBuffer));
    7010          43 :                 break;
    7011           1 :             case NC_STRING:
    7012             :                 status =
    7013           1 :                     nc_get_vara_string(old_cdfid, nSrcVarId, anStart, anCount,
    7014             :                                        static_cast<char **>(pBuffer));
    7015           1 :                 if (!status)
    7016             :                 {
    7017           1 :                     status = nc_put_vara_string(
    7018             :                         new_cdfid, nDstVarId, anStart, anCount,
    7019             :                         static_cast<const char **>(pBuffer));
    7020           1 :                     nc_free_string(nElems, static_cast<char **>(pBuffer));
    7021             :                 }
    7022           1 :                 break;
    7023             : 
    7024           2 :             case NC_UBYTE:
    7025             :                 status =
    7026           2 :                     nc_get_vara_uchar(old_cdfid, nSrcVarId, anStart, anCount,
    7027             :                                       static_cast<unsigned char *>(pBuffer));
    7028           2 :                 if (!status)
    7029           2 :                     status = nc_put_vara_uchar(
    7030             :                         new_cdfid, nDstVarId, anStart, anCount,
    7031             :                         static_cast<unsigned char *>(pBuffer));
    7032           2 :                 break;
    7033           2 :             case NC_USHORT:
    7034             :                 status =
    7035           2 :                     nc_get_vara_ushort(old_cdfid, nSrcVarId, anStart, anCount,
    7036             :                                        static_cast<unsigned short *>(pBuffer));
    7037           2 :                 if (!status)
    7038           2 :                     status = nc_put_vara_ushort(
    7039             :                         new_cdfid, nDstVarId, anStart, anCount,
    7040             :                         static_cast<unsigned short *>(pBuffer));
    7041           2 :                 break;
    7042           2 :             case NC_UINT:
    7043             :                 status =
    7044           2 :                     nc_get_vara_uint(old_cdfid, nSrcVarId, anStart, anCount,
    7045             :                                      static_cast<unsigned int *>(pBuffer));
    7046           2 :                 if (!status)
    7047             :                     status =
    7048           2 :                         nc_put_vara_uint(new_cdfid, nDstVarId, anStart, anCount,
    7049             :                                          static_cast<unsigned int *>(pBuffer));
    7050           2 :                 break;
    7051           2 :             case NC_INT64:
    7052             :                 status =
    7053           2 :                     nc_get_vara_longlong(old_cdfid, nSrcVarId, anStart, anCount,
    7054             :                                          static_cast<long long *>(pBuffer));
    7055           2 :                 if (!status)
    7056           2 :                     status = nc_put_vara_longlong(
    7057             :                         new_cdfid, nDstVarId, anStart, anCount,
    7058             :                         static_cast<long long *>(pBuffer));
    7059           2 :                 break;
    7060           2 :             case NC_UINT64:
    7061           2 :                 status = nc_get_vara_ulonglong(
    7062             :                     old_cdfid, nSrcVarId, anStart, anCount,
    7063             :                     static_cast<unsigned long long *>(pBuffer));
    7064           2 :                 if (!status)
    7065           2 :                     status = nc_put_vara_ulonglong(
    7066             :                         new_cdfid, nDstVarId, anStart, anCount,
    7067             :                         static_cast<unsigned long long *>(pBuffer));
    7068           2 :                 break;
    7069           0 :             default:
    7070           0 :                 status = NC_EBADTYPE;
    7071             :         }
    7072             : 
    7073         119 :         NCDF_ERR(status);
    7074         119 :         if (status != NC_NOERR)
    7075             :         {
    7076           0 :             VSIFree(pBuffer);
    7077           0 :             return false;
    7078             :         }
    7079             :     }
    7080             : 
    7081         121 :     VSIFree(pBuffer);
    7082         121 :     return true;
    7083             : }
    7084             : 
    7085             : /************************************************************************/
    7086             : /*                         NCDFIsUnlimitedDim()                         */
    7087             : /************************************************************************/
    7088             : 
    7089          80 : bool NCDFIsUnlimitedDim(bool bIsNC4, int cdfid, int nDimId)
    7090             : {
    7091          80 :     if (bIsNC4)
    7092             :     {
    7093          16 :         int nUnlimitedDims = 0;
    7094          16 :         nc_inq_unlimdims(cdfid, &nUnlimitedDims, nullptr);
    7095          16 :         bool bFound = false;
    7096          16 :         if (nUnlimitedDims)
    7097             :         {
    7098             :             int *panUnlimitedDimIds =
    7099          16 :                 static_cast<int *>(CPLMalloc(sizeof(int) * nUnlimitedDims));
    7100          16 :             nc_inq_unlimdims(cdfid, nullptr, panUnlimitedDimIds);
    7101          30 :             for (int i = 0; i < nUnlimitedDims; i++)
    7102             :             {
    7103          22 :                 if (panUnlimitedDimIds[i] == nDimId)
    7104             :                 {
    7105           8 :                     bFound = true;
    7106           8 :                     break;
    7107             :                 }
    7108             :             }
    7109          16 :             CPLFree(panUnlimitedDimIds);
    7110             :         }
    7111          16 :         return bFound;
    7112             :     }
    7113             :     else
    7114             :     {
    7115          64 :         int nUnlimitedDimId = -1;
    7116          64 :         nc_inq(cdfid, nullptr, nullptr, nullptr, &nUnlimitedDimId);
    7117          64 :         return nDimId == nUnlimitedDimId;
    7118             :     }
    7119             : }
    7120             : 
    7121             : /************************************************************************/
    7122             : /*                              CloneGrp()                              */
    7123             : /************************************************************************/
    7124             : 
    7125          16 : bool netCDFDataset::CloneGrp(int nOldGrpId, int nNewGrpId, bool bIsNC4,
    7126             :                              int nLayerId, int nDimIdToGrow, size_t nNewSize)
    7127             : {
    7128             :     // Clone dimensions
    7129          16 :     int nDimCount = -1;
    7130          16 :     int status = nc_inq_ndims(nOldGrpId, &nDimCount);
    7131          16 :     NCDF_ERR(status);
    7132          16 :     if (nDimCount < 0 || nDimCount > NC_MAX_DIMS)
    7133           0 :         return false;
    7134             :     int anDimIds[NC_MAX_DIMS];
    7135          16 :     int nUnlimiDimID = -1;
    7136          16 :     status = nc_inq_unlimdim(nOldGrpId, &nUnlimiDimID);
    7137          16 :     NCDF_ERR(status);
    7138          16 :     if (bIsNC4)
    7139             :     {
    7140             :         // In NC4, the dimension ids of a group are not necessarily in
    7141             :         // [0,nDimCount-1] range
    7142           8 :         int nDimCount2 = -1;
    7143           8 :         status = nc_inq_dimids(nOldGrpId, &nDimCount2, anDimIds, FALSE);
    7144           8 :         NCDF_ERR(status);
    7145           8 :         CPLAssert(nDimCount == nDimCount2);
    7146             :     }
    7147             :     else
    7148             :     {
    7149          36 :         for (int i = 0; i < nDimCount; i++)
    7150          28 :             anDimIds[i] = i;
    7151             :     }
    7152          60 :     for (int i = 0; i < nDimCount; i++)
    7153             :     {
    7154             :         char szDimName[NC_MAX_NAME + 1];
    7155          44 :         szDimName[0] = 0;
    7156          44 :         size_t nLen = 0;
    7157          44 :         const int nDimId = anDimIds[i];
    7158          44 :         status = nc_inq_dim(nOldGrpId, nDimId, szDimName, &nLen);
    7159          44 :         NCDF_ERR(status);
    7160          44 :         if (NCDFIsUnlimitedDim(bIsNC4, nOldGrpId, nDimId))
    7161          16 :             nLen = NC_UNLIMITED;
    7162          28 :         else if (nDimId == nDimIdToGrow && nOldGrpId == nLayerId)
    7163          13 :             nLen = nNewSize;
    7164          44 :         int nNewDimId = -1;
    7165          44 :         status = nc_def_dim(nNewGrpId, szDimName, nLen, &nNewDimId);
    7166          44 :         NCDF_ERR(status);
    7167          44 :         CPLAssert(nDimId == nNewDimId);
    7168          44 :         if (status != NC_NOERR)
    7169             :         {
    7170           0 :             return false;
    7171             :         }
    7172             :     }
    7173             : 
    7174             :     // Clone main attributes
    7175          16 :     if (!CloneAttributes(nOldGrpId, nNewGrpId, NC_GLOBAL, NC_GLOBAL))
    7176             :     {
    7177           0 :         return false;
    7178             :     }
    7179             : 
    7180             :     // Clone variable definitions
    7181          16 :     int nVarCount = -1;
    7182          16 :     status = nc_inq_nvars(nOldGrpId, &nVarCount);
    7183          16 :     NCDF_ERR(status);
    7184             : 
    7185         137 :     for (int i = 0; i < nVarCount; i++)
    7186             :     {
    7187             :         char szVarName[NC_MAX_NAME + 1];
    7188         121 :         szVarName[0] = 0;
    7189         121 :         status = nc_inq_varname(nOldGrpId, i, szVarName);
    7190         121 :         NCDF_ERR(status);
    7191         121 :         nc_type nc_datatype = NC_NAT;
    7192         121 :         status = nc_inq_vartype(nOldGrpId, i, &nc_datatype);
    7193         121 :         NCDF_ERR(status);
    7194         121 :         int nVarDimCount = -1;
    7195         121 :         status = nc_inq_varndims(nOldGrpId, i, &nVarDimCount);
    7196         121 :         NCDF_ERR(status);
    7197         121 :         status = nc_inq_vardimid(nOldGrpId, i, anDimIds);
    7198         121 :         NCDF_ERR(status);
    7199         121 :         int nNewVarId = -1;
    7200         121 :         status = nc_def_var(nNewGrpId, szVarName, nc_datatype, nVarDimCount,
    7201             :                             anDimIds, &nNewVarId);
    7202         121 :         NCDF_ERR(status);
    7203         121 :         CPLAssert(i == nNewVarId);
    7204         121 :         if (status != NC_NOERR)
    7205             :         {
    7206           0 :             return false;
    7207             :         }
    7208             : 
    7209         121 :         if (!CloneAttributes(nOldGrpId, nNewGrpId, i, i))
    7210             :         {
    7211           0 :             return false;
    7212             :         }
    7213             :     }
    7214             : 
    7215          16 :     status = nc_enddef(nNewGrpId);
    7216          16 :     NCDF_ERR(status);
    7217          16 :     if (status != NC_NOERR)
    7218             :     {
    7219           0 :         return false;
    7220             :     }
    7221             : 
    7222             :     // Clone variable content
    7223         137 :     for (int i = 0; i < nVarCount; i++)
    7224             :     {
    7225         121 :         if (!CloneVariableContent(nOldGrpId, nNewGrpId, i, i))
    7226             :         {
    7227           0 :             return false;
    7228             :         }
    7229             :     }
    7230             : 
    7231          16 :     return true;
    7232             : }
    7233             : 
    7234             : /************************************************************************/
    7235             : /*                              GrowDim()                               */
    7236             : /************************************************************************/
    7237             : 
    7238          13 : bool netCDFDataset::GrowDim(int nLayerId, int nDimIdToGrow, size_t nNewSize)
    7239             : {
    7240             :     int nCreationMode;
    7241             :     // Set nCreationMode based on eFormat.
    7242          13 :     switch (eFormat)
    7243             :     {
    7244             : #ifdef NETCDF_HAS_NC2
    7245           0 :         case NCDF_FORMAT_NC2:
    7246           0 :             nCreationMode = NC_CLOBBER | NC_64BIT_OFFSET;
    7247           0 :             break;
    7248             : #endif
    7249           5 :         case NCDF_FORMAT_NC4:
    7250           5 :             nCreationMode = NC_CLOBBER | NC_NETCDF4;
    7251           5 :             break;
    7252           0 :         case NCDF_FORMAT_NC4C:
    7253           0 :             nCreationMode = NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL;
    7254           0 :             break;
    7255           8 :         case NCDF_FORMAT_NC:
    7256             :         default:
    7257           8 :             nCreationMode = NC_CLOBBER;
    7258           8 :             break;
    7259             :     }
    7260             : 
    7261          13 :     int new_cdfid = -1;
    7262          26 :     CPLString osTmpFilename(osFilename + ".tmp");
    7263          26 :     CPLString osFilenameForNCCreate(osTmpFilename);
    7264             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    7265             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    7266             :     {
    7267             :         char *pszTemp =
    7268             :             CPLRecode(osFilenameForNCCreate, CPL_ENC_UTF8, "CP_ACP");
    7269             :         osFilenameForNCCreate = pszTemp;
    7270             :         CPLFree(pszTemp);
    7271             :     }
    7272             : #endif
    7273          13 :     int status = nc_create(osFilenameForNCCreate, nCreationMode, &new_cdfid);
    7274          13 :     NCDF_ERR(status);
    7275          13 :     if (status != NC_NOERR)
    7276           0 :         return false;
    7277             : 
    7278          13 :     if (!CloneGrp(cdfid, new_cdfid, eFormat == NCDF_FORMAT_NC4, nLayerId,
    7279             :                   nDimIdToGrow, nNewSize))
    7280             :     {
    7281           0 :         GDAL_nc_close(new_cdfid);
    7282           0 :         return false;
    7283             :     }
    7284             : 
    7285          13 :     int nGroupCount = 0;
    7286          26 :     std::vector<CPLString> oListGrpName;
    7287          31 :     if (eFormat == NCDF_FORMAT_NC4 &&
    7288          18 :         nc_inq_grps(cdfid, &nGroupCount, nullptr) == NC_NOERR &&
    7289           5 :         nGroupCount > 0)
    7290             :     {
    7291             :         int *panGroupIds =
    7292           2 :             static_cast<int *>(CPLMalloc(sizeof(int) * nGroupCount));
    7293           2 :         status = nc_inq_grps(cdfid, nullptr, panGroupIds);
    7294           2 :         NCDF_ERR(status);
    7295           5 :         for (int i = 0; i < nGroupCount; i++)
    7296             :         {
    7297             :             char szGroupName[NC_MAX_NAME + 1];
    7298           3 :             szGroupName[0] = 0;
    7299           3 :             NCDF_ERR(nc_inq_grpname(panGroupIds[i], szGroupName));
    7300           3 :             int nNewGrpId = -1;
    7301           3 :             status = nc_def_grp(new_cdfid, szGroupName, &nNewGrpId);
    7302           3 :             NCDF_ERR(status);
    7303           3 :             if (status != NC_NOERR)
    7304             :             {
    7305           0 :                 CPLFree(panGroupIds);
    7306           0 :                 GDAL_nc_close(new_cdfid);
    7307           0 :                 return false;
    7308             :             }
    7309           3 :             if (!CloneGrp(panGroupIds[i], nNewGrpId, /*bIsNC4=*/true, nLayerId,
    7310             :                           nDimIdToGrow, nNewSize))
    7311             :             {
    7312           0 :                 CPLFree(panGroupIds);
    7313           0 :                 GDAL_nc_close(new_cdfid);
    7314           0 :                 return false;
    7315             :             }
    7316             :         }
    7317           2 :         CPLFree(panGroupIds);
    7318             : 
    7319           5 :         for (int i = 0; i < this->GetLayerCount(); i++)
    7320             :         {
    7321           3 :             auto poLayer = dynamic_cast<netCDFLayer *>(papoLayers[i].get());
    7322           3 :             if (poLayer)
    7323             :             {
    7324             :                 char szGroupName[NC_MAX_NAME + 1];
    7325           3 :                 szGroupName[0] = 0;
    7326           3 :                 status = nc_inq_grpname(poLayer->GetCDFID(), szGroupName);
    7327           3 :                 NCDF_ERR(status);
    7328           3 :                 oListGrpName.push_back(szGroupName);
    7329             :             }
    7330             :         }
    7331             :     }
    7332             : 
    7333          13 :     GDAL_nc_close(cdfid);
    7334          13 :     cdfid = -1;
    7335          13 :     GDAL_nc_close(new_cdfid);
    7336             : 
    7337          26 :     CPLString osOriFilename(osFilename + ".ori");
    7338          26 :     if (VSIRename(osFilename, osOriFilename) != 0 ||
    7339          13 :         VSIRename(osTmpFilename, osFilename) != 0)
    7340             :     {
    7341           0 :         CPLError(CE_Failure, CPLE_FileIO, "Renaming of files failed");
    7342           0 :         return false;
    7343             :     }
    7344          13 :     VSIUnlink(osOriFilename);
    7345             : 
    7346          26 :     CPLString osFilenameForNCOpen(osFilename);
    7347             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    7348             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    7349             :     {
    7350             :         char *pszTemp = CPLRecode(osFilenameForNCOpen, CPL_ENC_UTF8, "CP_ACP");
    7351             :         osFilenameForNCOpen = pszTemp;
    7352             :         CPLFree(pszTemp);
    7353             :     }
    7354             : #endif
    7355          13 :     status = GDAL_nc_open(osFilenameForNCOpen, NC_WRITE, &cdfid);
    7356          13 :     NCDF_ERR(status);
    7357          13 :     if (status != NC_NOERR)
    7358           0 :         return false;
    7359          13 :     bDefineMode = false;
    7360             : 
    7361          13 :     if (!oListGrpName.empty())
    7362             :     {
    7363           5 :         for (int i = 0; i < this->GetLayerCount(); i++)
    7364             :         {
    7365           3 :             auto poLayer = dynamic_cast<netCDFLayer *>(papoLayers[i].get());
    7366           3 :             if (poLayer)
    7367             :             {
    7368           3 :                 int nNewLayerCDFID = -1;
    7369           3 :                 status = nc_inq_ncid(cdfid, oListGrpName[i].c_str(),
    7370             :                                      &nNewLayerCDFID);
    7371           3 :                 NCDF_ERR(status);
    7372           3 :                 poLayer->SetCDFID(nNewLayerCDFID);
    7373             :             }
    7374             :         }
    7375             :     }
    7376             :     else
    7377             :     {
    7378          22 :         for (int i = 0; i < this->GetLayerCount(); i++)
    7379             :         {
    7380          11 :             auto poLayer = dynamic_cast<netCDFLayer *>(papoLayers[i].get());
    7381          11 :             if (poLayer)
    7382          11 :                 poLayer->SetCDFID(cdfid);
    7383             :         }
    7384             :     }
    7385             : 
    7386          13 :     return true;
    7387             : }
    7388             : 
    7389             : #ifdef ENABLE_NCDUMP
    7390             : 
    7391             : /************************************************************************/
    7392             : /*                    netCDFDatasetCreateTempFile()                     */
    7393             : /************************************************************************/
    7394             : 
    7395             : /* Create a netCDF file from a text dump (format of ncdump) */
    7396             : /* Mostly to easy fuzzing of the driver, while still generating valid */
    7397             : /* netCDF files. */
    7398             : /* Note: not all data types are supported ! */
    7399           4 : bool netCDFDatasetCreateTempFile(NetCDFFormatEnum eFormat,
    7400             :                                  const char *pszTmpFilename, VSILFILE *fpSrc)
    7401             : {
    7402           4 :     CPL_IGNORE_RET_VAL(eFormat);
    7403           4 :     int nCreateMode = NC_CLOBBER;
    7404           4 :     if (eFormat == NCDF_FORMAT_NC4)
    7405           1 :         nCreateMode |= NC_NETCDF4;
    7406           3 :     else if (eFormat == NCDF_FORMAT_NC4C)
    7407           0 :         nCreateMode |= NC_NETCDF4 | NC_CLASSIC_MODEL;
    7408           4 :     int nCdfId = -1;
    7409           4 :     int status = nc_create(pszTmpFilename, nCreateMode, &nCdfId);
    7410           4 :     if (status != NC_NOERR)
    7411             :     {
    7412           0 :         return false;
    7413             :     }
    7414           4 :     VSIFSeekL(fpSrc, 0, SEEK_SET);
    7415             :     const char *pszLine;
    7416           4 :     constexpr int SECTION_NONE = 0;
    7417           4 :     constexpr int SECTION_DIMENSIONS = 1;
    7418           4 :     constexpr int SECTION_VARIABLES = 2;
    7419           4 :     constexpr int SECTION_DATA = 3;
    7420           4 :     int nActiveSection = SECTION_NONE;
    7421           8 :     std::map<CPLString, int> oMapDimToId;
    7422           8 :     std::map<int, int> oMapDimIdToDimLen;
    7423           8 :     std::map<CPLString, int> oMapVarToId;
    7424           8 :     std::map<int, std::vector<int>> oMapVarIdToVectorOfDimId;
    7425           8 :     std::map<int, int> oMapVarIdToType;
    7426           4 :     std::set<CPLString> oSetAttrDefined;
    7427           4 :     oMapVarToId[""] = -1;
    7428           4 :     size_t nTotalVarSize = 0;
    7429         208 :     while ((pszLine = CPLReadLineL(fpSrc)) != nullptr)
    7430             :     {
    7431         204 :         if (STARTS_WITH(pszLine, "dimensions:") &&
    7432             :             nActiveSection == SECTION_NONE)
    7433             :         {
    7434           4 :             nActiveSection = SECTION_DIMENSIONS;
    7435             :         }
    7436         200 :         else if (STARTS_WITH(pszLine, "variables:") &&
    7437             :                  nActiveSection == SECTION_DIMENSIONS)
    7438             :         {
    7439           4 :             nActiveSection = SECTION_VARIABLES;
    7440             :         }
    7441         196 :         else if (STARTS_WITH(pszLine, "data:") &&
    7442             :                  nActiveSection == SECTION_VARIABLES)
    7443             :         {
    7444           4 :             nActiveSection = SECTION_DATA;
    7445           4 :             status = nc_enddef(nCdfId);
    7446           4 :             if (status != NC_NOERR)
    7447             :             {
    7448           0 :                 CPLDebug("netCDF", "nc_enddef() failed: %s",
    7449             :                          nc_strerror(status));
    7450             :             }
    7451             :         }
    7452         192 :         else if (nActiveSection == SECTION_DIMENSIONS)
    7453             :         {
    7454             :             const CPLStringList aosTokens(
    7455           9 :                 CSLTokenizeString2(pszLine, " \t=;", 0));
    7456           9 :             if (aosTokens.size() == 2)
    7457             :             {
    7458           9 :                 const char *pszDimName = aosTokens[0];
    7459           9 :                 bool bValidName = true;
    7460           9 :                 if (STARTS_WITH(pszDimName, "_nc4_non_coord_"))
    7461             :                 {
    7462             :                     // This is an internal netcdf prefix. Using it may
    7463             :                     // cause memory leaks.
    7464           0 :                     bValidName = false;
    7465             :                 }
    7466           9 :                 if (!bValidName)
    7467             :                 {
    7468           0 :                     CPLDebug("netCDF",
    7469             :                              "nc_def_dim(%s) failed: invalid name found",
    7470             :                              pszDimName);
    7471           0 :                     continue;
    7472             :                 }
    7473             : 
    7474             :                 const bool bIsASCII =
    7475           9 :                     CPLIsASCII(pszDimName, static_cast<size_t>(-1));
    7476           9 :                 if (!bIsASCII)
    7477             :                 {
    7478             :                     // Workaround https://github.com/Unidata/netcdf-c/pull/450
    7479           0 :                     CPLDebug("netCDF",
    7480             :                              "nc_def_dim(%s) failed: rejected because "
    7481             :                              "of non-ASCII characters",
    7482             :                              pszDimName);
    7483           0 :                     continue;
    7484             :                 }
    7485           9 :                 int nDimSize = EQUAL(aosTokens[1], "UNLIMITED")
    7486             :                                    ? NC_UNLIMITED
    7487           9 :                                    : atoi(aosTokens[1]);
    7488           9 :                 if (nDimSize >= 1000)
    7489           1 :                     nDimSize = 1000;  // to avoid very long processing
    7490           9 :                 if (nDimSize >= 0)
    7491             :                 {
    7492           9 :                     int nDimId = -1;
    7493           9 :                     status = nc_def_dim(nCdfId, pszDimName, nDimSize, &nDimId);
    7494           9 :                     if (status != NC_NOERR)
    7495             :                     {
    7496           0 :                         CPLDebug("netCDF", "nc_def_dim(%s, %d) failed: %s",
    7497             :                                  pszDimName, nDimSize, nc_strerror(status));
    7498             :                     }
    7499             :                     else
    7500             :                     {
    7501             : #ifdef DEBUG_VERBOSE
    7502             :                         CPLDebug("netCDF", "nc_def_dim(%s, %d) (%s) succeeded",
    7503             :                                  pszDimName, nDimSize, pszLine);
    7504             : #endif
    7505           9 :                         oMapDimToId[pszDimName] = nDimId;
    7506           9 :                         oMapDimIdToDimLen[nDimId] = nDimSize;
    7507             :                     }
    7508             :                 }
    7509             :             }
    7510             :         }
    7511         183 :         else if (nActiveSection == SECTION_VARIABLES)
    7512             :         {
    7513         390 :             while (*pszLine == ' ' || *pszLine == '\t')
    7514         249 :                 pszLine++;
    7515         141 :             const char *pszColumn = strchr(pszLine, ':');
    7516         141 :             const char *pszEqual = strchr(pszLine, '=');
    7517         141 :             if (pszColumn == nullptr)
    7518             :             {
    7519             :                 const CPLStringList aosTokens(
    7520          21 :                     CSLTokenizeString2(pszLine, " \t=(),;", 0));
    7521          21 :                 if (aosTokens.size() >= 2)
    7522             :                 {
    7523          17 :                     const char *pszVarName = aosTokens[1];
    7524          17 :                     bool bValidName = true;
    7525          17 :                     if (STARTS_WITH(pszVarName, "_nc4_non_coord_"))
    7526             :                     {
    7527             :                         // This is an internal netcdf prefix. Using it may
    7528             :                         // cause memory leaks.
    7529           0 :                         bValidName = false;
    7530             :                     }
    7531         138 :                     for (int i = 0; pszVarName[i]; i++)
    7532             :                     {
    7533         121 :                         if (!((pszVarName[i] >= 'a' && pszVarName[i] <= 'z') ||
    7534          28 :                               (pszVarName[i] >= 'A' && pszVarName[i] <= 'Z') ||
    7535           9 :                               (pszVarName[i] >= '0' && pszVarName[i] <= '9') ||
    7536           6 :                               pszVarName[i] == '_'))
    7537             :                         {
    7538           0 :                             bValidName = false;
    7539             :                         }
    7540             :                     }
    7541          17 :                     if (!bValidName)
    7542             :                     {
    7543           0 :                         CPLDebug(
    7544             :                             "netCDF",
    7545             :                             "nc_def_var(%s) failed: illegal character found",
    7546             :                             pszVarName);
    7547           0 :                         continue;
    7548             :                     }
    7549          17 :                     if (oMapVarToId.find(pszVarName) != oMapVarToId.end())
    7550             :                     {
    7551           0 :                         CPLDebug("netCDF",
    7552             :                                  "nc_def_var(%s) failed: already defined",
    7553             :                                  pszVarName);
    7554           0 :                         continue;
    7555             :                     }
    7556          17 :                     const char *pszVarType = aosTokens[0];
    7557          17 :                     int nc_datatype = NC_BYTE;
    7558          17 :                     size_t nDataTypeSize = 1;
    7559          17 :                     if (EQUAL(pszVarType, "char"))
    7560             :                     {
    7561           6 :                         nc_datatype = NC_CHAR;
    7562           6 :                         nDataTypeSize = 1;
    7563             :                     }
    7564          11 :                     else if (EQUAL(pszVarType, "byte"))
    7565             :                     {
    7566           3 :                         nc_datatype = NC_BYTE;
    7567           3 :                         nDataTypeSize = 1;
    7568             :                     }
    7569           8 :                     else if (EQUAL(pszVarType, "short"))
    7570             :                     {
    7571           0 :                         nc_datatype = NC_SHORT;
    7572           0 :                         nDataTypeSize = 2;
    7573             :                     }
    7574           8 :                     else if (EQUAL(pszVarType, "int"))
    7575             :                     {
    7576           0 :                         nc_datatype = NC_INT;
    7577           0 :                         nDataTypeSize = 4;
    7578             :                     }
    7579           8 :                     else if (EQUAL(pszVarType, "float"))
    7580             :                     {
    7581           0 :                         nc_datatype = NC_FLOAT;
    7582           0 :                         nDataTypeSize = 4;
    7583             :                     }
    7584           8 :                     else if (EQUAL(pszVarType, "double"))
    7585             :                     {
    7586           8 :                         nc_datatype = NC_DOUBLE;
    7587           8 :                         nDataTypeSize = 8;
    7588             :                     }
    7589           0 :                     else if (EQUAL(pszVarType, "ubyte"))
    7590             :                     {
    7591           0 :                         nc_datatype = NC_UBYTE;
    7592           0 :                         nDataTypeSize = 1;
    7593             :                     }
    7594           0 :                     else if (EQUAL(pszVarType, "ushort"))
    7595             :                     {
    7596           0 :                         nc_datatype = NC_USHORT;
    7597           0 :                         nDataTypeSize = 2;
    7598             :                     }
    7599           0 :                     else if (EQUAL(pszVarType, "uint"))
    7600             :                     {
    7601           0 :                         nc_datatype = NC_UINT;
    7602           0 :                         nDataTypeSize = 4;
    7603             :                     }
    7604           0 :                     else if (EQUAL(pszVarType, "int64"))
    7605             :                     {
    7606           0 :                         nc_datatype = NC_INT64;
    7607           0 :                         nDataTypeSize = 8;
    7608             :                     }
    7609           0 :                     else if (EQUAL(pszVarType, "uint64"))
    7610             :                     {
    7611           0 :                         nc_datatype = NC_UINT64;
    7612           0 :                         nDataTypeSize = 8;
    7613             :                     }
    7614             : 
    7615          17 :                     int nDims = aosTokens.size() - 2;
    7616          17 :                     if (nDims >= 32)
    7617             :                     {
    7618             :                         // The number of dimensions in a netCDFv4 file is
    7619             :                         // limited by #define H5S_MAX_RANK    32
    7620             :                         // but libnetcdf doesn't check that...
    7621           0 :                         CPLDebug("netCDF",
    7622             :                                  "nc_def_var(%s) failed: too many dimensions",
    7623             :                                  pszVarName);
    7624           0 :                         continue;
    7625             :                     }
    7626          17 :                     std::vector<int> aoDimIds;
    7627          17 :                     bool bFailed = false;
    7628          17 :                     size_t nSize = 1;
    7629          35 :                     for (int i = 0; i < nDims; i++)
    7630             :                     {
    7631          18 :                         const char *pszDimName = aosTokens[2 + i];
    7632          18 :                         if (oMapDimToId.find(pszDimName) == oMapDimToId.end())
    7633             :                         {
    7634           0 :                             bFailed = true;
    7635           0 :                             break;
    7636             :                         }
    7637          18 :                         const int nDimId = oMapDimToId[pszDimName];
    7638          18 :                         aoDimIds.push_back(nDimId);
    7639             : 
    7640          18 :                         const size_t nDimSize = oMapDimIdToDimLen[nDimId];
    7641          18 :                         if (nDimSize != 0)
    7642             :                         {
    7643          18 :                             if (nSize >
    7644          18 :                                 std::numeric_limits<size_t>::max() / nDimSize)
    7645             :                             {
    7646           0 :                                 bFailed = true;
    7647           0 :                                 break;
    7648             :                             }
    7649             :                             else
    7650             :                             {
    7651          18 :                                 nSize *= nDimSize;
    7652             :                             }
    7653             :                         }
    7654             :                     }
    7655          17 :                     if (bFailed)
    7656             :                     {
    7657           0 :                         CPLDebug("netCDF",
    7658             :                                  "nc_def_var(%s) failed: unknown dimension(s)",
    7659             :                                  pszVarName);
    7660           0 :                         continue;
    7661             :                     }
    7662          17 :                     if (nSize > 100U * 1024 * 1024 / nDataTypeSize)
    7663             :                     {
    7664           0 :                         CPLDebug("netCDF",
    7665             :                                  "nc_def_var(%s) failed: too large data",
    7666             :                                  pszVarName);
    7667           0 :                         continue;
    7668             :                     }
    7669          17 :                     if (nTotalVarSize >
    7670          34 :                             std::numeric_limits<size_t>::max() - nSize ||
    7671          17 :                         nTotalVarSize + nSize > 100 * 1024 * 1024)
    7672             :                     {
    7673           0 :                         CPLDebug("netCDF",
    7674             :                                  "nc_def_var(%s) failed: too large data",
    7675             :                                  pszVarName);
    7676           0 :                         continue;
    7677             :                     }
    7678          17 :                     nTotalVarSize += nSize;
    7679             : 
    7680          17 :                     int nVarId = -1;
    7681             :                     status =
    7682          30 :                         nc_def_var(nCdfId, pszVarName, nc_datatype, nDims,
    7683          13 :                                    (nDims) ? &aoDimIds[0] : nullptr, &nVarId);
    7684          17 :                     if (status != NC_NOERR)
    7685             :                     {
    7686           0 :                         CPLDebug("netCDF", "nc_def_var(%s) failed: %s",
    7687             :                                  pszVarName, nc_strerror(status));
    7688             :                     }
    7689             :                     else
    7690             :                     {
    7691             : #ifdef DEBUG_VERBOSE
    7692             :                         CPLDebug("netCDF", "nc_def_var(%s) (%s) succeeded",
    7693             :                                  pszVarName, pszLine);
    7694             : #endif
    7695          17 :                         oMapVarToId[pszVarName] = nVarId;
    7696          17 :                         oMapVarIdToType[nVarId] = nc_datatype;
    7697          17 :                         oMapVarIdToVectorOfDimId[nVarId] = std::move(aoDimIds);
    7698             :                     }
    7699             :                 }
    7700             :             }
    7701         120 :             else if (pszEqual != nullptr && pszEqual - pszColumn > 0)
    7702             :             {
    7703         116 :                 CPLString osVarName(pszLine, pszColumn - pszLine);
    7704         116 :                 CPLString osAttrName(pszColumn + 1, pszEqual - pszColumn - 1);
    7705         116 :                 osAttrName.Trim();
    7706         116 :                 if (oMapVarToId.find(osVarName) == oMapVarToId.end())
    7707             :                 {
    7708           0 :                     CPLDebug("netCDF",
    7709             :                              "nc_put_att(%s:%s) failed: "
    7710             :                              "no corresponding variable",
    7711             :                              osVarName.c_str(), osAttrName.c_str());
    7712           0 :                     continue;
    7713             :                 }
    7714         116 :                 bool bValidName = true;
    7715        1743 :                 for (const auto &c : osAttrName)
    7716             :                 {
    7717        1627 :                     if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
    7718         158 :                           (c >= '0' && c <= '9') || c == '_'))
    7719             :                     {
    7720           0 :                         bValidName = false;
    7721             :                     }
    7722             :                 }
    7723         116 :                 if (!bValidName)
    7724             :                 {
    7725           0 :                     CPLDebug(
    7726             :                         "netCDF",
    7727             :                         "nc_put_att(%s:%s) failed: illegal character found",
    7728             :                         osVarName.c_str(), osAttrName.c_str());
    7729           0 :                     continue;
    7730             :                 }
    7731         116 :                 if (oSetAttrDefined.find(osVarName + ":" + osAttrName) !=
    7732         232 :                     oSetAttrDefined.end())
    7733             :                 {
    7734           0 :                     CPLDebug("netCDF",
    7735             :                              "nc_put_att(%s:%s) failed: already defined",
    7736             :                              osVarName.c_str(), osAttrName.c_str());
    7737           0 :                     continue;
    7738             :                 }
    7739             : 
    7740         116 :                 const int nVarId = oMapVarToId[osVarName];
    7741         116 :                 const char *pszValue = pszEqual + 1;
    7742         232 :                 while (*pszValue == ' ')
    7743         116 :                     pszValue++;
    7744             : 
    7745         116 :                 status = NC_EBADTYPE;
    7746         116 :                 if (*pszValue == '"')
    7747             :                 {
    7748             :                     // For _FillValue, the attribute type should match
    7749             :                     // the variable type. Leaks memory with NC4 otherwise
    7750          74 :                     if (osAttrName == "_FillValue")
    7751             :                     {
    7752           0 :                         CPLDebug("netCDF", "nc_put_att_(%s:%s) failed: %s",
    7753             :                                  osVarName.c_str(), osAttrName.c_str(),
    7754             :                                  nc_strerror(status));
    7755           0 :                         continue;
    7756             :                     }
    7757             : 
    7758             :                     // Unquote and unescape string value
    7759          74 :                     CPLString osVal(pszValue + 1);
    7760         222 :                     while (!osVal.empty())
    7761             :                     {
    7762         222 :                         if (osVal.back() == ';' || osVal.back() == ' ')
    7763             :                         {
    7764         148 :                             osVal.pop_back();
    7765             :                         }
    7766          74 :                         else if (osVal.back() == '"')
    7767             :                         {
    7768          74 :                             osVal.pop_back();
    7769          74 :                             break;
    7770             :                         }
    7771             :                         else
    7772             :                         {
    7773           0 :                             break;
    7774             :                         }
    7775             :                     }
    7776          74 :                     osVal.replaceAll("\\\"", '"');
    7777          74 :                     status = nc_put_att_text(nCdfId, nVarId, osAttrName,
    7778             :                                              osVal.size(), osVal.c_str());
    7779             :                 }
    7780             :                 else
    7781             :                 {
    7782          84 :                     CPLString osVal(pszValue);
    7783         126 :                     while (!osVal.empty())
    7784             :                     {
    7785         126 :                         if (osVal.back() == ';' || osVal.back() == ' ')
    7786             :                         {
    7787          84 :                             osVal.pop_back();
    7788             :                         }
    7789             :                         else
    7790             :                         {
    7791          42 :                             break;
    7792             :                         }
    7793             :                     }
    7794          42 :                     int nc_datatype = -1;
    7795          42 :                     if (!osVal.empty() && osVal.back() == 'b')
    7796             :                     {
    7797           3 :                         nc_datatype = NC_BYTE;
    7798           3 :                         osVal.pop_back();
    7799             :                     }
    7800          39 :                     else if (!osVal.empty() && osVal.back() == 's')
    7801             :                     {
    7802           3 :                         nc_datatype = NC_SHORT;
    7803           3 :                         osVal.pop_back();
    7804             :                     }
    7805          42 :                     if (CPLGetValueType(osVal) == CPL_VALUE_INTEGER)
    7806             :                     {
    7807           7 :                         if (nc_datatype < 0)
    7808           4 :                             nc_datatype = NC_INT;
    7809             :                     }
    7810          35 :                     else if (CPLGetValueType(osVal) == CPL_VALUE_REAL)
    7811             :                     {
    7812          32 :                         nc_datatype = NC_DOUBLE;
    7813             :                     }
    7814             :                     else
    7815             :                     {
    7816           3 :                         nc_datatype = -1;
    7817             :                     }
    7818             : 
    7819             :                     // For _FillValue, check that the attribute type matches
    7820             :                     // the variable type. Leaks memory with NC4 otherwise
    7821          42 :                     if (osAttrName == "_FillValue")
    7822             :                     {
    7823           6 :                         if (nVarId < 0 ||
    7824           3 :                             nc_datatype != oMapVarIdToType[nVarId])
    7825             :                         {
    7826           0 :                             nc_datatype = -1;
    7827             :                         }
    7828             :                     }
    7829             : 
    7830          42 :                     if (nc_datatype == NC_BYTE)
    7831             :                     {
    7832             :                         signed char chVal =
    7833           3 :                             static_cast<signed char>(atoi(osVal));
    7834           3 :                         status = nc_put_att_schar(nCdfId, nVarId, osAttrName,
    7835             :                                                   NC_BYTE, 1, &chVal);
    7836             :                     }
    7837          39 :                     else if (nc_datatype == NC_SHORT)
    7838             :                     {
    7839           0 :                         short nVal = static_cast<short>(atoi(osVal));
    7840           0 :                         status = nc_put_att_short(nCdfId, nVarId, osAttrName,
    7841             :                                                   NC_SHORT, 1, &nVal);
    7842             :                     }
    7843          39 :                     else if (nc_datatype == NC_INT)
    7844             :                     {
    7845           4 :                         int nVal = static_cast<int>(atoi(osVal));
    7846           4 :                         status = nc_put_att_int(nCdfId, nVarId, osAttrName,
    7847             :                                                 NC_INT, 1, &nVal);
    7848             :                     }
    7849          35 :                     else if (nc_datatype == NC_DOUBLE)
    7850             :                     {
    7851          32 :                         double dfVal = CPLAtof(osVal);
    7852          32 :                         status = nc_put_att_double(nCdfId, nVarId, osAttrName,
    7853             :                                                    NC_DOUBLE, 1, &dfVal);
    7854             :                     }
    7855             :                 }
    7856         116 :                 if (status != NC_NOERR)
    7857             :                 {
    7858           3 :                     CPLDebug("netCDF", "nc_put_att_(%s:%s) failed: %s",
    7859             :                              osVarName.c_str(), osAttrName.c_str(),
    7860             :                              nc_strerror(status));
    7861             :                 }
    7862             :                 else
    7863             :                 {
    7864         113 :                     oSetAttrDefined.insert(osVarName + ":" + osAttrName);
    7865             : #ifdef DEBUG_VERBOSE
    7866             :                     CPLDebug("netCDF", "nc_put_att_(%s:%s) (%s) succeeded",
    7867             :                              osVarName.c_str(), osAttrName.c_str(), pszLine);
    7868             : #endif
    7869             :                 }
    7870             :             }
    7871             :         }
    7872          42 :         else if (nActiveSection == SECTION_DATA)
    7873             :         {
    7874          55 :             while (*pszLine == ' ' || *pszLine == '\t')
    7875          17 :                 pszLine++;
    7876          38 :             const char *pszEqual = strchr(pszLine, '=');
    7877          38 :             if (pszEqual)
    7878             :             {
    7879          17 :                 CPLString osVarName(pszLine, pszEqual - pszLine);
    7880          17 :                 osVarName.Trim();
    7881          17 :                 if (oMapVarToId.find(osVarName) == oMapVarToId.end())
    7882           0 :                     continue;
    7883          17 :                 const int nVarId = oMapVarToId[osVarName];
    7884          17 :                 CPLString osAccVal(pszEqual + 1);
    7885          17 :                 osAccVal.Trim();
    7886         153 :                 while (osAccVal.empty() || osAccVal.back() != ';')
    7887             :                 {
    7888         136 :                     pszLine = CPLReadLineL(fpSrc);
    7889         136 :                     if (pszLine == nullptr)
    7890           0 :                         break;
    7891         272 :                     CPLString osVal(pszLine);
    7892         136 :                     osVal.Trim();
    7893         136 :                     osAccVal += osVal;
    7894             :                 }
    7895          17 :                 if (pszLine == nullptr)
    7896           0 :                     break;
    7897          17 :                 osAccVal.pop_back();
    7898             : 
    7899             :                 const std::vector<int> aoDimIds =
    7900          34 :                     oMapVarIdToVectorOfDimId[nVarId];
    7901          17 :                 size_t nSize = 1;
    7902          34 :                 std::vector<size_t> aoStart, aoEdge;
    7903          17 :                 aoStart.resize(aoDimIds.size());
    7904          17 :                 aoEdge.resize(aoDimIds.size());
    7905          35 :                 for (size_t i = 0; i < aoDimIds.size(); ++i)
    7906             :                 {
    7907          18 :                     const size_t nDimSize = oMapDimIdToDimLen[aoDimIds[i]];
    7908          36 :                     if (nDimSize != 0 &&
    7909          18 :                         nSize > std::numeric_limits<size_t>::max() / nDimSize)
    7910             :                     {
    7911           0 :                         nSize = 0;
    7912             :                     }
    7913             :                     else
    7914             :                     {
    7915          18 :                         nSize *= nDimSize;
    7916             :                     }
    7917          18 :                     aoStart[i] = 0;
    7918          18 :                     aoEdge[i] = nDimSize;
    7919             :                 }
    7920             : 
    7921          17 :                 status = NC_EBADTYPE;
    7922          17 :                 if (nSize == 0)
    7923             :                 {
    7924             :                     // Might happen with an unlimited dimension
    7925             :                 }
    7926          17 :                 else if (oMapVarIdToType[nVarId] == NC_DOUBLE)
    7927             :                 {
    7928           8 :                     if (!aoStart.empty())
    7929             :                     {
    7930             :                         const CPLStringList aosTokens(
    7931          16 :                             CSLTokenizeString2(osAccVal, " ,;", 0));
    7932           8 :                         size_t nTokens = aosTokens.size();
    7933           8 :                         if (nTokens >= nSize)
    7934             :                         {
    7935             :                             double *padfVals = static_cast<double *>(
    7936           8 :                                 VSI_CALLOC_VERBOSE(nSize, sizeof(double)));
    7937           8 :                             if (padfVals)
    7938             :                             {
    7939         132 :                                 for (size_t i = 0; i < nSize; i++)
    7940             :                                 {
    7941         124 :                                     padfVals[i] = CPLAtof(aosTokens[i]);
    7942             :                                 }
    7943           8 :                                 status = nc_put_vara_double(
    7944           8 :                                     nCdfId, nVarId, &aoStart[0], &aoEdge[0],
    7945             :                                     padfVals);
    7946           8 :                                 VSIFree(padfVals);
    7947             :                             }
    7948             :                         }
    7949             :                     }
    7950             :                 }
    7951           9 :                 else if (oMapVarIdToType[nVarId] == NC_BYTE)
    7952             :                 {
    7953           3 :                     if (!aoStart.empty())
    7954             :                     {
    7955             :                         const CPLStringList aosTokens(
    7956           6 :                             CSLTokenizeString2(osAccVal, " ,;", 0));
    7957           3 :                         size_t nTokens = aosTokens.size();
    7958           3 :                         if (nTokens >= nSize)
    7959             :                         {
    7960             :                             signed char *panVals = static_cast<signed char *>(
    7961           3 :                                 VSI_CALLOC_VERBOSE(nSize, sizeof(signed char)));
    7962           3 :                             if (panVals)
    7963             :                             {
    7964        1203 :                                 for (size_t i = 0; i < nSize; i++)
    7965             :                                 {
    7966        1200 :                                     panVals[i] = static_cast<signed char>(
    7967        1200 :                                         atoi(aosTokens[i]));
    7968             :                                 }
    7969           3 :                                 status = nc_put_vara_schar(nCdfId, nVarId,
    7970           3 :                                                            &aoStart[0],
    7971           3 :                                                            &aoEdge[0], panVals);
    7972           3 :                                 VSIFree(panVals);
    7973             :                             }
    7974             :                         }
    7975             :                     }
    7976             :                 }
    7977           6 :                 else if (oMapVarIdToType[nVarId] == NC_CHAR)
    7978             :                 {
    7979           6 :                     if (aoStart.size() == 2)
    7980             :                     {
    7981           4 :                         std::vector<CPLString> aoStrings;
    7982           2 :                         bool bInString = false;
    7983           4 :                         CPLString osCurString;
    7984         935 :                         for (size_t i = 0; i < osAccVal.size();)
    7985             :                         {
    7986         933 :                             if (!bInString)
    7987             :                             {
    7988           8 :                                 if (osAccVal[i] == '"')
    7989             :                                 {
    7990           4 :                                     bInString = true;
    7991           4 :                                     osCurString.clear();
    7992             :                                 }
    7993           8 :                                 i++;
    7994             :                             }
    7995         926 :                             else if (osAccVal[i] == '\\' &&
    7996         926 :                                      i + 1 < osAccVal.size() &&
    7997           1 :                                      osAccVal[i + 1] == '"')
    7998             :                             {
    7999           1 :                                 osCurString += '"';
    8000           1 :                                 i += 2;
    8001             :                             }
    8002         924 :                             else if (osAccVal[i] == '"')
    8003             :                             {
    8004           4 :                                 aoStrings.push_back(osCurString);
    8005           4 :                                 osCurString.clear();
    8006           4 :                                 bInString = false;
    8007           4 :                                 i++;
    8008             :                             }
    8009             :                             else
    8010             :                             {
    8011         920 :                                 osCurString += osAccVal[i];
    8012         920 :                                 i++;
    8013             :                             }
    8014             :                         }
    8015           2 :                         const size_t nRecords = oMapDimIdToDimLen[aoDimIds[0]];
    8016           2 :                         const size_t nWidth = oMapDimIdToDimLen[aoDimIds[1]];
    8017           2 :                         size_t nIters = aoStrings.size();
    8018           2 :                         if (nIters > nRecords)
    8019           0 :                             nIters = nRecords;
    8020           6 :                         for (size_t i = 0; i < nIters; i++)
    8021             :                         {
    8022             :                             size_t anIndex[2];
    8023           4 :                             anIndex[0] = i;
    8024           4 :                             anIndex[1] = 0;
    8025             :                             size_t anCount[2];
    8026           4 :                             anCount[0] = 1;
    8027           4 :                             anCount[1] = aoStrings[i].size();
    8028           4 :                             if (anCount[1] > nWidth)
    8029           0 :                                 anCount[1] = nWidth;
    8030             :                             status =
    8031           4 :                                 nc_put_vara_text(nCdfId, nVarId, anIndex,
    8032           4 :                                                  anCount, aoStrings[i].c_str());
    8033           4 :                             if (status != NC_NOERR)
    8034           0 :                                 break;
    8035             :                         }
    8036             :                     }
    8037             :                 }
    8038          17 :                 if (status != NC_NOERR)
    8039             :                 {
    8040           4 :                     CPLDebug("netCDF", "nc_put_var_(%s) failed: %s",
    8041             :                              osVarName.c_str(), nc_strerror(status));
    8042             :                 }
    8043             :             }
    8044             :         }
    8045             :     }
    8046             : 
    8047           4 :     GDAL_nc_close(nCdfId);
    8048           4 :     return true;
    8049             : }
    8050             : 
    8051             : #endif  // ENABLE_NCDUMP
    8052             : 
    8053             : /************************************************************************/
    8054             : /*                                Open()                                */
    8055             : /************************************************************************/
    8056             : 
    8057         850 : GDALDataset *netCDFDataset::Open(GDALOpenInfo *poOpenInfo)
    8058             : 
    8059             : {
    8060             : #ifdef NCDF_DEBUG
    8061             :     CPLDebug("GDAL_netCDF", "\n=====\nOpen(), filename=[%s]",
    8062             :              poOpenInfo->pszFilename);
    8063             : #endif
    8064             : 
    8065             :     // Does this appear to be a netcdf file?
    8066         850 :     NetCDFFormatEnum eTmpFormat = NCDF_FORMAT_NONE;
    8067         850 :     if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "NETCDF:"))
    8068             :     {
    8069         781 :         eTmpFormat = netCDFIdentifyFormat(poOpenInfo, /* bCheckExt = */ true);
    8070             : #ifdef NCDF_DEBUG
    8071             :         CPLDebug("GDAL_netCDF", "identified format %d", eTmpFormat);
    8072             : #endif
    8073             :         // Note: not calling Identify() directly, because we want the file type.
    8074             :         // Only support NCDF_FORMAT* formats.
    8075         781 :         if (NCDF_FORMAT_NC == eTmpFormat || NCDF_FORMAT_NC2 == eTmpFormat ||
    8076           2 :             NCDF_FORMAT_NC4 == eTmpFormat || NCDF_FORMAT_NC4C == eTmpFormat)
    8077             :         {
    8078             :             // ok
    8079             :         }
    8080           2 :         else if (eTmpFormat == NCDF_FORMAT_HDF4 &&
    8081           0 :                  poOpenInfo->IsSingleAllowedDriver("netCDF"))
    8082             :         {
    8083             :             // ok
    8084             :         }
    8085             :         else
    8086             :         {
    8087           2 :             return nullptr;
    8088             :         }
    8089             :     }
    8090             :     else
    8091             :     {
    8092             : #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    8093             :         // We don't necessarily want to catch bugs in libnetcdf ...
    8094             :         if (CPLGetConfigOption("DISABLE_OPEN_REAL_NETCDF_FILES", nullptr))
    8095             :         {
    8096             :             return nullptr;
    8097             :         }
    8098             : #endif
    8099             :     }
    8100             : 
    8101         848 :     if (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER)
    8102             :     {
    8103         320 :         return OpenMultiDim(poOpenInfo);
    8104             :     }
    8105             : 
    8106        1056 :     CPLMutexHolderD(&hNCMutex);
    8107             : 
    8108         528 :     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock with
    8109             :     // GDALDataset own mutex.
    8110         528 :     netCDFDataset *poDS = new netCDFDataset();
    8111         528 :     poDS->papszOpenOptions = CSLDuplicate(poOpenInfo->papszOpenOptions);
    8112         528 :     CPLAcquireMutex(hNCMutex, 1000.0);
    8113             : 
    8114         528 :     poDS->SetDescription(poOpenInfo->pszFilename);
    8115             : 
    8116             :     // Check if filename start with NETCDF: tag.
    8117         528 :     bool bTreatAsSubdataset = false;
    8118        1056 :     CPLString osSubdatasetName;
    8119             : 
    8120             : #ifdef ENABLE_NCDUMP
    8121         528 :     const char *pszHeader =
    8122             :         reinterpret_cast<const char *>(poOpenInfo->pabyHeader);
    8123         528 :     if (poOpenInfo->fpL != nullptr && STARTS_WITH(pszHeader, "netcdf ") &&
    8124           3 :         strstr(pszHeader, "dimensions:") && strstr(pszHeader, "variables:"))
    8125             :     {
    8126             :         // By default create a temporary file that will be destroyed,
    8127             :         // unless NETCDF_TMP_FILE is defined. Can be useful to see which
    8128             :         // netCDF file has been generated from a potential fuzzed input.
    8129           3 :         poDS->osFilename = CPLGetConfigOption("NETCDF_TMP_FILE", "");
    8130           3 :         if (poDS->osFilename.empty())
    8131             :         {
    8132           3 :             poDS->bFileToDestroyAtClosing = true;
    8133           3 :             poDS->osFilename = CPLGenerateTempFilenameSafe("netcdf_tmp");
    8134             :         }
    8135           3 :         if (!netCDFDatasetCreateTempFile(eTmpFormat, poDS->osFilename,
    8136             :                                          poOpenInfo->fpL))
    8137             :         {
    8138           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8139             :             // deadlock with GDALDataset own mutex.
    8140           0 :             delete poDS;
    8141           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8142           0 :             return nullptr;
    8143             :         }
    8144           3 :         bTreatAsSubdataset = false;
    8145           3 :         poDS->eFormat = eTmpFormat;
    8146             :     }
    8147             :     else
    8148             : #endif
    8149             : 
    8150         525 :         if (STARTS_WITH_CI(poOpenInfo->pszFilename, "NETCDF:"))
    8151             :     {
    8152             :         GDALSubdatasetInfoH hInfo =
    8153          69 :             GDALGetSubdatasetInfo(poOpenInfo->pszFilename);
    8154          69 :         if (hInfo)
    8155             :         {
    8156          69 :             char *pszPath = GDALSubdatasetInfoGetPathComponent(hInfo);
    8157          69 :             poDS->osFilename = pszPath;
    8158          69 :             CPLFree(pszPath);
    8159             : 
    8160             :             char *pszSubdataset =
    8161          69 :                 GDALSubdatasetInfoGetSubdatasetComponent(hInfo);
    8162          69 :             if (pszSubdataset && pszSubdataset[0] != '\0')
    8163             :             {
    8164          69 :                 osSubdatasetName = pszSubdataset;
    8165          69 :                 bTreatAsSubdataset = true;
    8166             :             }
    8167             :             else
    8168             :             {
    8169           0 :                 osSubdatasetName = "";
    8170           0 :                 bTreatAsSubdataset = false;
    8171             :             }
    8172          69 :             CPLFree(pszSubdataset);
    8173             : 
    8174          69 :             GDALDestroySubdatasetInfo(hInfo);
    8175             :         }
    8176             : 
    8177         138 :         if (!STARTS_WITH(poDS->osFilename, "http://") &&
    8178          69 :             !STARTS_WITH(poDS->osFilename, "https://"))
    8179             :         {
    8180             :             // Identify Format from real file, with bCheckExt=FALSE.
    8181             :             auto poOpenInfo2 = std::make_unique<GDALOpenInfo>(
    8182          69 :                 poDS->osFilename.c_str(), GA_ReadOnly);
    8183          69 :             poDS->eFormat = netCDFIdentifyFormat(poOpenInfo2.get(),
    8184             :                                                  /* bCheckExt = */ false);
    8185          69 :             if (NCDF_FORMAT_NONE == poDS->eFormat ||
    8186          69 :                 NCDF_FORMAT_UNKNOWN == poDS->eFormat)
    8187             :             {
    8188           0 :                 CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8189             :                 // deadlock with GDALDataset own mutex.
    8190           0 :                 delete poDS;
    8191           0 :                 CPLAcquireMutex(hNCMutex, 1000.0);
    8192           0 :                 return nullptr;
    8193             :             }
    8194             :         }
    8195             :     }
    8196             :     else
    8197             :     {
    8198         456 :         poDS->osFilename = poOpenInfo->pszFilename;
    8199         456 :         bTreatAsSubdataset = false;
    8200         456 :         poDS->eFormat = eTmpFormat;
    8201             :     }
    8202             : 
    8203             : // Try opening the dataset.
    8204             : #if defined(NCDF_DEBUG) && defined(ENABLE_UFFD)
    8205             :     CPLDebug("GDAL_netCDF", "calling nc_open_mem(%s)",
    8206             :              poDS->osFilename.c_str());
    8207             : #elif defined(NCDF_DEBUG) && !defined(ENABLE_UFFD)
    8208             :     CPLDebug("GDAL_netCDF", "calling nc_open(%s)", poDS->osFilename.c_str());
    8209             : #endif
    8210         528 :     int cdfid = -1;
    8211         528 :     const int nMode = ((poOpenInfo->nOpenFlags & GDAL_OF_UPDATE) != 0)
    8212             :                           ? NC_WRITE
    8213             :                           : NC_NOWRITE;
    8214        1056 :     CPLString osFilenameForNCOpen(poDS->osFilename);
    8215             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    8216             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    8217             :     {
    8218             :         char *pszTemp = CPLRecode(osFilenameForNCOpen, CPL_ENC_UTF8, "CP_ACP");
    8219             :         osFilenameForNCOpen = pszTemp;
    8220             :         CPLFree(pszTemp);
    8221             :     }
    8222             : #endif
    8223         528 :     int status2 = -1;
    8224             : 
    8225             : #ifdef ENABLE_UFFD
    8226         528 :     cpl_uffd_context *pCtx = nullptr;
    8227             : #endif
    8228             : 
    8229         543 :     if (STARTS_WITH(osFilenameForNCOpen, "/vsimem/") &&
    8230          15 :         poOpenInfo->eAccess == GA_ReadOnly)
    8231             :     {
    8232          15 :         vsi_l_offset nLength = 0;
    8233          15 :         poDS->fpVSIMEM = VSIFOpenL(osFilenameForNCOpen, "rb");
    8234          15 :         if (poDS->fpVSIMEM)
    8235             :         {
    8236             :             // We assume that the file will not be modified. If it is, then
    8237             :             // pabyBuffer might become invalid.
    8238             :             GByte *pabyBuffer =
    8239          15 :                 VSIGetMemFileBuffer(osFilenameForNCOpen, &nLength, false);
    8240          15 :             if (pabyBuffer)
    8241             :             {
    8242          15 :                 status2 = nc_open_mem(CPLGetFilename(osFilenameForNCOpen),
    8243             :                                       nMode, static_cast<size_t>(nLength),
    8244             :                                       pabyBuffer, &cdfid);
    8245             :             }
    8246             :         }
    8247             :     }
    8248             :     else
    8249             :     {
    8250             :         const bool bVsiFile =
    8251         513 :             !strncmp(osFilenameForNCOpen, "/vsi", strlen("/vsi"));
    8252             : #ifdef ENABLE_UFFD
    8253         513 :         bool bReadOnly = (poOpenInfo->eAccess == GA_ReadOnly);
    8254         513 :         void *pVma = nullptr;
    8255         513 :         uint64_t nVmaSize = 0;
    8256             : 
    8257         513 :         if (bVsiFile)
    8258             :         {
    8259           2 :             if (bReadOnly)
    8260             :             {
    8261           2 :                 if (CPLIsUserFaultMappingSupported())
    8262             :                 {
    8263           2 :                     pCtx = CPLCreateUserFaultMapping(osFilenameForNCOpen, &pVma,
    8264             :                                                      &nVmaSize);
    8265             :                 }
    8266             :                 else
    8267             :                 {
    8268           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    8269             :                              "Opening a /vsi file with the netCDF driver "
    8270             :                              "requires Linux userfaultfd to be available. "
    8271             :                              "If running from Docker, "
    8272             :                              "--security-opt seccomp=unconfined might be "
    8273             :                              "needed.%s",
    8274           0 :                              ((poDS->eFormat == NCDF_FORMAT_NC4 ||
    8275           0 :                                poDS->eFormat == NCDF_FORMAT_HDF5) &&
    8276           0 :                               GDALGetDriverByName("HDF5"))
    8277             :                                  ? " Or you may set the GDAL_SKIP=netCDF "
    8278             :                                    "configuration option to force the use of "
    8279             :                                    "the HDF5 driver."
    8280             :                                  : "");
    8281             :                 }
    8282             :             }
    8283             :             else
    8284             :             {
    8285           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    8286             :                          "Opening a /vsi file with the netCDF driver is only "
    8287             :                          "supported in read-only mode");
    8288             :             }
    8289             :         }
    8290         513 :         if (pCtx != nullptr && pVma != nullptr && nVmaSize > 0)
    8291             :         {
    8292             :             // netCDF code, at least for netCDF 4.7.0, is confused by filenames
    8293             :             // like /vsicurl/http[s]://example.com/foo.nc, so just pass the
    8294             :             // final part
    8295           2 :             status2 = nc_open_mem(CPLGetFilename(osFilenameForNCOpen), nMode,
    8296             :                                   static_cast<size_t>(nVmaSize), pVma, &cdfid);
    8297             :         }
    8298             :         else
    8299         511 :             status2 = GDAL_nc_open(osFilenameForNCOpen, nMode, &cdfid);
    8300             : #else
    8301             :         if (bVsiFile)
    8302             :         {
    8303             :             CPLError(
    8304             :                 CE_Failure, CPLE_AppDefined,
    8305             :                 "Opening a /vsi file with the netCDF driver requires Linux "
    8306             :                 "userfaultfd to be available.%s",
    8307             :                 ((poDS->eFormat == NCDF_FORMAT_NC4 ||
    8308             :                   poDS->eFormat == NCDF_FORMAT_HDF5) &&
    8309             :                  GDALGetDriverByName("HDF5"))
    8310             :                     ? " Or you may set the GDAL_SKIP=netCDF "
    8311             :                       "configuration option to force the use of the HDF5 "
    8312             :                       "driver."
    8313             :                     : "");
    8314             :             status2 = NC_EIO;
    8315             :         }
    8316             :         else
    8317             :         {
    8318             :             status2 = GDAL_nc_open(osFilenameForNCOpen, nMode, &cdfid);
    8319             :         }
    8320             : #endif
    8321             :     }
    8322         528 :     if (status2 != NC_NOERR)
    8323             :     {
    8324             : #ifdef NCDF_DEBUG
    8325             :         CPLDebug("GDAL_netCDF", "error opening");
    8326             : #endif
    8327           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8328             :         // with GDALDataset own mutex.
    8329           0 :         delete poDS;
    8330           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8331           0 :         return nullptr;
    8332             :     }
    8333             : #ifdef NCDF_DEBUG
    8334             :     CPLDebug("GDAL_netCDF", "got cdfid=%d", cdfid);
    8335             : #endif
    8336             : 
    8337             : #if defined(ENABLE_NCDUMP) && !defined(_WIN32)
    8338             :     // Try to destroy the temporary file right now on Unix
    8339         528 :     if (poDS->bFileToDestroyAtClosing)
    8340             :     {
    8341           3 :         if (VSIUnlink(poDS->osFilename) == 0)
    8342             :         {
    8343           3 :             poDS->bFileToDestroyAtClosing = false;
    8344             :         }
    8345             :     }
    8346             : #endif
    8347             : 
    8348             :     // Is this a real netCDF file?
    8349             :     int ndims;
    8350             :     int ngatts;
    8351             :     int nvars;
    8352             :     int unlimdimid;
    8353         528 :     int status = nc_inq(cdfid, &ndims, &nvars, &ngatts, &unlimdimid);
    8354         528 :     if (status != NC_NOERR)
    8355             :     {
    8356           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8357             :         // with GDALDataset own mutex.
    8358           0 :         delete poDS;
    8359           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8360           0 :         return nullptr;
    8361             :     }
    8362             : 
    8363             :     // Get file type from netcdf.
    8364         528 :     int nTmpFormat = NCDF_FORMAT_NONE;
    8365         528 :     status = nc_inq_format(cdfid, &nTmpFormat);
    8366         528 :     if (status != NC_NOERR)
    8367             :     {
    8368           0 :         NCDF_ERR(status);
    8369             :     }
    8370             :     else
    8371             :     {
    8372         528 :         CPLDebug("GDAL_netCDF",
    8373             :                  "driver detected file type=%d, libnetcdf detected type=%d",
    8374         528 :                  poDS->eFormat, nTmpFormat);
    8375         528 :         if (static_cast<NetCDFFormatEnum>(nTmpFormat) != poDS->eFormat)
    8376             :         {
    8377             :             // Warn if file detection conflicts with that from libnetcdf
    8378             :             // except for NC4C, which we have no way of detecting initially.
    8379          26 :             if (nTmpFormat != NCDF_FORMAT_NC4C &&
    8380          13 :                 !STARTS_WITH(poDS->osFilename, "http://") &&
    8381           0 :                 !STARTS_WITH(poDS->osFilename, "https://"))
    8382             :             {
    8383           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    8384             :                          "NetCDF driver detected file type=%d, but libnetcdf "
    8385             :                          "detected type=%d",
    8386           0 :                          poDS->eFormat, nTmpFormat);
    8387             :             }
    8388          13 :             CPLDebug("GDAL_netCDF", "setting file type to %d, was %d",
    8389          13 :                      nTmpFormat, poDS->eFormat);
    8390          13 :             poDS->eFormat = static_cast<NetCDFFormatEnum>(nTmpFormat);
    8391             :         }
    8392             :     }
    8393             : 
    8394             :     // Does the request variable exist?
    8395         528 :     if (bTreatAsSubdataset)
    8396             :     {
    8397             :         int dummy;
    8398          69 :         if (NCDFOpenSubDataset(cdfid, osSubdatasetName.c_str(), &dummy,
    8399          69 :                                &dummy) != CE_None)
    8400             :         {
    8401           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    8402             :                      "%s is a netCDF file, but %s is not a variable.",
    8403             :                      poOpenInfo->pszFilename, osSubdatasetName.c_str());
    8404             : 
    8405           0 :             GDAL_nc_close(cdfid);
    8406             : #ifdef ENABLE_UFFD
    8407           0 :             NETCDF_UFFD_UNMAP(pCtx);
    8408             : #endif
    8409           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8410             :             // deadlock with GDALDataset own mutex.
    8411           0 :             delete poDS;
    8412           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8413           0 :             return nullptr;
    8414             :         }
    8415             :     }
    8416             : 
    8417             :     // Figure out whether or not the listed dataset has support for simple
    8418             :     // geometries (CF-1.8)
    8419         528 :     nccfdriver::getCFVersion(cdfid, poDS->nCFVersionMajor,
    8420         528 :                              poDS->nCFVersionMinor);
    8421         528 :     bool bHasSimpleGeometries = false;  // but not necessarily valid
    8422         528 :     if (poDS->nCFVersionMajor > 1 ||
    8423         528 :         (poDS->nCFVersionMajor == 1 && poDS->nCFVersionMinor >= 8))
    8424             :     {
    8425          74 :         bHasSimpleGeometries = poDS->DetectAndFillSGLayers(cdfid);
    8426          74 :         if (bHasSimpleGeometries)
    8427             :         {
    8428          66 :             poDS->bSGSupport = true;
    8429          66 :             poDS->vcdf.enableFullVirtualMode();
    8430             :         }
    8431             :     }
    8432             : 
    8433        1056 :     std::string osConventions;
    8434         528 :     if (NCDFGetAttr(cdfid, NC_GLOBAL, "Conventions", osConventions) != CE_None)
    8435             :     {
    8436          60 :         CPLDebug("GDAL_netCDF", "No UNIDATA NC_GLOBAL:Conventions attribute");
    8437             :         // Note that 'Conventions' is always capital 'C' in CF spec.
    8438             :     }
    8439             : 
    8440             :     // Create band information objects.
    8441         528 :     CPLDebug("GDAL_netCDF", "var_count = %d", nvars);
    8442             : 
    8443             :     // Create a corresponding GDALDataset.
    8444             :     // Create Netcdf Subdataset if filename as NETCDF tag.
    8445         528 :     poDS->cdfid = cdfid;
    8446             : #ifdef ENABLE_UFFD
    8447         528 :     poDS->pCtx = pCtx;
    8448             : #endif
    8449         528 :     poDS->eAccess = poOpenInfo->eAccess;
    8450         528 :     poDS->bDefineMode = false;
    8451             : 
    8452         528 :     poDS->ReadAttributes(cdfid, NC_GLOBAL);
    8453             : 
    8454             :     // Identify coordinate and boundary variables that we should
    8455             :     // ignore as Raster Bands.
    8456        1056 :     CPLStringList aosIgnoreVars;
    8457         528 :     NCDFGetCoordAndBoundVarFullNames(cdfid, aosIgnoreVars);
    8458             :     // Filter variables to keep only valid 2+D raster bands and vector fields.
    8459         528 :     int nRasterVars = 0;
    8460         528 :     int nIgnoredVars = 0;
    8461         528 :     int nGroupID = -1;
    8462         528 :     int nVarID = -1;
    8463             : 
    8464             :     std::map<std::array<int, 3>, std::vector<std::pair<int, int>>>
    8465        1056 :         oMap2DDimsToGroupAndVar;
    8466        1387 :     if ((poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
    8467         331 :         STARTS_WITH(
    8468             :             poDS->aosMetadata.FetchNameValueDef("NC_GLOBAL#mission_name", ""),
    8469           1 :             "Sentinel 3") &&
    8470           1 :         EQUAL(poDS->aosMetadata.FetchNameValueDef(
    8471             :                   "NC_GLOBAL#altimeter_sensor_name", ""),
    8472         859 :               "SRAL") &&
    8473           1 :         EQUAL(poDS->aosMetadata.FetchNameValueDef(
    8474             :                   "NC_GLOBAL#radiometer_sensor_name", ""),
    8475             :               "MWR"))
    8476             :     {
    8477           1 :         if (poDS->eAccess == GA_Update)
    8478             :         {
    8479           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8480             :             // deadlock with GDALDataset own mutex.
    8481           0 :             delete poDS;
    8482           0 :             return nullptr;
    8483             :         }
    8484           1 :         poDS->ProcessSentinel3_SRAL_MWR();
    8485             :     }
    8486             :     else
    8487             :     {
    8488         527 :         poDS->FilterVars(cdfid, (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0,
    8489         857 :                          (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
    8490         330 :                              !bHasSimpleGeometries,
    8491             :                          aosIgnoreVars, &nRasterVars, &nGroupID, &nVarID,
    8492             :                          &nIgnoredVars, oMap2DDimsToGroupAndVar);
    8493             :     }
    8494             : 
    8495         528 :     const bool bListAllArrays = CPLTestBool(
    8496         528 :         CSLFetchNameValueDef(poDS->papszOpenOptions, "LIST_ALL_ARRAYS", "NO"));
    8497             : 
    8498             :     // Case where there is no raster variable
    8499         528 :     if (!bListAllArrays && nRasterVars == 0 && !bTreatAsSubdataset)
    8500             :     {
    8501         138 :         poDS->GDALPamDataset::SetMetadata(poDS->aosMetadata);
    8502         138 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8503             :         // with GDALDataset own mutex.
    8504         138 :         poDS->TryLoadXML();
    8505             :         // If the dataset has been opened in raster mode only, exit
    8506         138 :         if ((poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
    8507           6 :             (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0)
    8508             :         {
    8509           4 :             delete poDS;
    8510           4 :             poDS = nullptr;
    8511             :         }
    8512             :         // Otherwise if the dataset is opened in vector mode, that there is
    8513             :         // no vector layer and we are in read-only, exit too.
    8514         134 :         else if (poDS->GetLayerCount() == 0 &&
    8515         142 :                  (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
    8516           8 :                  poOpenInfo->eAccess == GA_ReadOnly)
    8517             :         {
    8518           8 :             delete poDS;
    8519           8 :             poDS = nullptr;
    8520             :         }
    8521         138 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8522         138 :         return poDS;
    8523             :     }
    8524             : 
    8525             :     // We have more than one variable with 2 dimensions in the
    8526             :     // file, then treat this as a subdataset container dataset.
    8527         390 :     bool bSeveralVariablesAsBands = false;
    8528         390 :     if (bListAllArrays || ((nRasterVars > 1) && !bTreatAsSubdataset))
    8529             :     {
    8530          17 :         if (CPLFetchBool(poOpenInfo->papszOpenOptions, "VARIABLES_AS_BANDS",
    8531          23 :                          false) &&
    8532           6 :             oMap2DDimsToGroupAndVar.size() == 1)
    8533             :         {
    8534           6 :             std::tie(nGroupID, nVarID) =
    8535          12 :                 oMap2DDimsToGroupAndVar.begin()->second.front();
    8536           6 :             bSeveralVariablesAsBands = true;
    8537             :         }
    8538             :         else
    8539             :         {
    8540          11 :             poDS->CreateSubDatasetList(cdfid);
    8541          11 :             poDS->GDALPamDataset::SetMetadata(poDS->aosMetadata);
    8542          11 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8543             :             // deadlock with GDALDataset own mutex.
    8544          11 :             poDS->TryLoadXML();
    8545          11 :             CPLAcquireMutex(hNCMutex, 1000.0);
    8546          11 :             return poDS;
    8547             :         }
    8548             :     }
    8549             : 
    8550             :     // If we are not treating things as a subdataset, then capture
    8551             :     // the name of the single available variable as the subdataset.
    8552         379 :     if (!bTreatAsSubdataset)
    8553             :     {
    8554         310 :         NCDF_ERR(NCDFGetVarFullName(nGroupID, nVarID, osSubdatasetName));
    8555             :     }
    8556             : 
    8557             :     // We have ignored at least one variable, so we should report them
    8558             :     // as subdatasets for reference.
    8559         379 :     if (nIgnoredVars > 0 && !bTreatAsSubdataset)
    8560             :     {
    8561          29 :         CPLDebug("GDAL_netCDF",
    8562             :                  "As %d variables were ignored, creating subdataset list "
    8563             :                  "for reference. Variable #%d [%s] is the main variable",
    8564             :                  nIgnoredVars, nVarID, osSubdatasetName.c_str());
    8565          29 :         poDS->CreateSubDatasetList(cdfid);
    8566             :     }
    8567             : 
    8568             :     // Open the NETCDF subdataset NETCDF:"filename":subdataset.
    8569         379 :     int var = -1;
    8570         379 :     NCDFOpenSubDataset(cdfid, osSubdatasetName.c_str(), &nGroupID, &var);
    8571             :     // Now we can forget the root cdfid and only use the selected group.
    8572         379 :     cdfid = nGroupID;
    8573         379 :     int nd = 0;
    8574         379 :     nc_inq_varndims(cdfid, var, &nd);
    8575             : 
    8576         379 :     poDS->m_anDimIds.resize(nd);
    8577             : 
    8578             :     // X, Y, Z position in array
    8579         758 :     std::vector<int> anBandDimPos(nd);
    8580             : 
    8581         379 :     nc_inq_vardimid(cdfid, var, poDS->m_anDimIds.data());
    8582             : 
    8583             :     // Check if somebody tried to pass a variable with less than 1D.
    8584         379 :     if (nd < 1)
    8585             :     {
    8586           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    8587             :                  "Variable has %d dimension(s) - not supported.", nd);
    8588           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8589             :         // with GDALDataset own mutex.
    8590           0 :         delete poDS;
    8591           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8592           0 :         return nullptr;
    8593             :     }
    8594             : 
    8595             :     // CF-1 Convention
    8596             :     //
    8597             :     // Dimensions to appear in the relative order T, then Z, then Y,
    8598             :     // then X  to the file. All other dimensions should, whenever
    8599             :     // possible, be placed to the left of the spatiotemporal
    8600             :     // dimensions.
    8601             : 
    8602             :     // Verify that dimensions are in the {T,Z,Y,X} or {T,Z,Y,X} order
    8603             :     // Ideally we should detect for other ordering and act accordingly
    8604             :     // Only done if file has Conventions=CF-* and only prints warning
    8605             :     // To disable set GDAL_NETCDF_VERIFY_DIMS=NO and to use only
    8606             :     // attributes (not varnames) set GDAL_NETCDF_VERIFY_DIMS=STRICT
    8607             :     const bool bCheckDims =
    8608         758 :         CPLTestBool(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES")) &&
    8609         379 :         STARTS_WITH_CI(osConventions.c_str(), "CF");
    8610             : 
    8611         379 :     bool bYXBandOrder = false;
    8612         379 :     if (nd == 3)
    8613             :     {
    8614             :         // If there's a coordinates attributes, and the variable it points to
    8615             :         // are 2D variables indexed by the same first and second dimension than
    8616             :         // our variable of interest, then it is Y,X,Band order.
    8617          50 :         char *pszCoordinates = nullptr;
    8618          50 :         if (NCDFGetAttr(cdfid, var, "coordinates", &pszCoordinates) ==
    8619          69 :                 CE_None &&
    8620          19 :             pszCoordinates)
    8621             :         {
    8622             :             const CPLStringList aosCoordinates(
    8623          38 :                 NCDFTokenizeCoordinatesAttribute(pszCoordinates));
    8624          19 :             if (aosCoordinates.size() == 2)
    8625             :             {
    8626             :                 // Test that each variable is longitude/latitude.
    8627          13 :                 for (const auto &pszVar : aosCoordinates)
    8628             :                 {
    8629          13 :                     if (NCDFIsVarLongitude(cdfid, -1, pszVar) ||
    8630           4 :                         NCDFIsVarLatitude(cdfid, -1, pszVar))
    8631             :                     {
    8632           9 :                         int nOtherGroupId = -1;
    8633           9 :                         int nOtherVarId = -1;
    8634           9 :                         if (NCDFResolveVar(cdfid, pszVar, &nOtherGroupId,
    8635           9 :                                            &nOtherVarId) == CE_None)
    8636             :                         {
    8637           9 :                             int coordDimCount = 0;
    8638           9 :                             nc_inq_varndims(nOtherGroupId, nOtherVarId,
    8639             :                                             &coordDimCount);
    8640           9 :                             if (coordDimCount == 2)
    8641             :                             {
    8642           3 :                                 int coordDimIds[2] = {0, 0};
    8643           3 :                                 nc_inq_vardimid(nOtherGroupId, nOtherVarId,
    8644             :                                                 coordDimIds);
    8645           4 :                                 if (coordDimIds[0] == poDS->m_anDimIds[0] &&
    8646           1 :                                     coordDimIds[1] == poDS->m_anDimIds[1])
    8647             :                                 {
    8648           1 :                                     bYXBandOrder = true;
    8649           1 :                                     break;
    8650             :                                 }
    8651             :                             }
    8652             :                         }
    8653             :                     }
    8654             :                 }
    8655             :             }
    8656             :         }
    8657          50 :         CPLFree(pszCoordinates);
    8658             : 
    8659          50 :         if (!bYXBandOrder)
    8660             :         {
    8661          49 :             char szDim0Name[NC_MAX_NAME + 1] = {};
    8662          49 :             char szDim1Name[NC_MAX_NAME + 1] = {};
    8663          49 :             status = nc_inq_dimname(cdfid, poDS->m_anDimIds[0], szDim0Name);
    8664          49 :             NCDF_ERR(status);
    8665          49 :             status = nc_inq_dimname(cdfid, poDS->m_anDimIds[1], szDim1Name);
    8666          49 :             NCDF_ERR(status);
    8667             : 
    8668          49 :             if (strcmp(szDim0Name, "number_of_lines") == 0 &&
    8669           1 :                 strcmp(szDim1Name, "pixels_per_line") == 0)
    8670             :             {
    8671             :                 // Like in PACE OCI products
    8672           1 :                 bYXBandOrder = true;
    8673             :             }
    8674             :             else
    8675             :             {
    8676             :                 // For example for EMIT data (https://earth.jpl.nasa.gov/emit/data/data-portal/coverage-and-forecasts/),
    8677             :                 // dimension order is downtrack, crosstrack, bands
    8678          48 :                 char szDim2Name[NC_MAX_NAME + 1] = {};
    8679          48 :                 status = nc_inq_dimname(cdfid, poDS->m_anDimIds[2], szDim2Name);
    8680          48 :                 NCDF_ERR(status);
    8681          94 :                 bYXBandOrder = strcmp(szDim2Name, "bands") == 0 ||
    8682          46 :                                strcmp(szDim2Name, "band") == 0;
    8683             :             }
    8684             :         }
    8685             :     }
    8686             : 
    8687         379 :     if (nd >= 2 && bCheckDims && !bYXBandOrder)
    8688             :     {
    8689         286 :         char szDimName1[NC_MAX_NAME + 1] = {};
    8690         286 :         char szDimName2[NC_MAX_NAME + 1] = {};
    8691         286 :         status = nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 1], szDimName1);
    8692         286 :         NCDF_ERR(status);
    8693         286 :         status = nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 2], szDimName2);
    8694         286 :         NCDF_ERR(status);
    8695         470 :         if (NCDFIsVarLongitude(cdfid, -1, szDimName1) == false &&
    8696         184 :             NCDFIsVarProjectionX(cdfid, -1, szDimName1) == false)
    8697             :         {
    8698           2 :             CPLError(CE_Warning, CPLE_AppDefined,
    8699             :                      "dimension #%d (%s) is not a Longitude/X dimension.",
    8700             :                      nd - 1, szDimName1);
    8701             :         }
    8702         470 :         if (NCDFIsVarLatitude(cdfid, -1, szDimName2) == false &&
    8703         184 :             NCDFIsVarProjectionY(cdfid, -1, szDimName2) == false)
    8704             :         {
    8705           2 :             CPLError(CE_Warning, CPLE_AppDefined,
    8706             :                      "dimension #%d (%s) is not a Latitude/Y dimension.",
    8707             :                      nd - 2, szDimName2);
    8708             :         }
    8709         286 :         if ((NCDFIsVarLongitude(cdfid, -1, szDimName2) ||
    8710         288 :              NCDFIsVarProjectionX(cdfid, -1, szDimName2)) &&
    8711           2 :             (NCDFIsVarLatitude(cdfid, -1, szDimName1) ||
    8712           0 :              NCDFIsVarProjectionY(cdfid, -1, szDimName1)))
    8713             :         {
    8714           2 :             poDS->bSwitchedXY = true;
    8715             :         }
    8716         286 :         if (nd >= 3)
    8717             :         {
    8718          55 :             char szDimName3[NC_MAX_NAME + 1] = {};
    8719             :             status =
    8720          55 :                 nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 3], szDimName3);
    8721          55 :             NCDF_ERR(status);
    8722          55 :             if (nd >= 4)
    8723             :             {
    8724          13 :                 char szDimName4[NC_MAX_NAME + 1] = {};
    8725             :                 status =
    8726          13 :                     nc_inq_dimname(cdfid, poDS->m_anDimIds[nd - 4], szDimName4);
    8727          13 :                 NCDF_ERR(status);
    8728          13 :                 if (NCDFIsVarVerticalCoord(cdfid, -1, szDimName3) == false)
    8729             :                 {
    8730           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    8731             :                              "dimension #%d (%s) is not a Vertical dimension.",
    8732             :                              nd - 3, szDimName3);
    8733             :                 }
    8734          13 :                 if (NCDFIsVarTimeCoord(cdfid, -1, szDimName4) == false)
    8735             :                 {
    8736           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    8737             :                              "dimension #%d (%s) is not a Time dimension.",
    8738             :                              nd - 4, szDimName4);
    8739             :                 }
    8740             :             }
    8741             :             else
    8742             :             {
    8743          81 :                 if (NCDFIsVarVerticalCoord(cdfid, -1, szDimName3) == false &&
    8744          39 :                     NCDFIsVarTimeCoord(cdfid, -1, szDimName3) == false)
    8745             :                 {
    8746           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    8747             :                              "dimension #%d (%s) is not a "
    8748             :                              "Time or Vertical dimension.",
    8749             :                              nd - 3, szDimName3);
    8750             :                 }
    8751             :             }
    8752             :         }
    8753             :     }
    8754             : 
    8755             :     // Get X dimensions information.
    8756             :     size_t xdim;
    8757         379 :     poDS->nXDimID = poDS->m_anDimIds[bYXBandOrder ? 1 : nd - 1];
    8758         379 :     nc_inq_dimlen(cdfid, poDS->nXDimID, &xdim);
    8759             : 
    8760             :     // Get Y dimension information.
    8761             :     size_t ydim;
    8762         379 :     if (nd >= 2)
    8763             :     {
    8764         370 :         poDS->nYDimID = poDS->m_anDimIds[bYXBandOrder ? 0 : nd - 2];
    8765         370 :         nc_inq_dimlen(cdfid, poDS->nYDimID, &ydim);
    8766             :     }
    8767             :     else
    8768             :     {
    8769           9 :         poDS->nYDimID = -1;
    8770           9 :         ydim = 1;
    8771             :     }
    8772             : 
    8773         379 :     if (xdim > INT_MAX || ydim > INT_MAX)
    8774             :     {
    8775           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    8776             :                  "Invalid raster dimensions: " CPL_FRMT_GUIB "x" CPL_FRMT_GUIB,
    8777             :                  static_cast<GUIntBig>(xdim), static_cast<GUIntBig>(ydim));
    8778           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8779             :         // with GDALDataset own mutex.
    8780           0 :         delete poDS;
    8781           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8782           0 :         return nullptr;
    8783             :     }
    8784             : 
    8785         379 :     poDS->nRasterXSize = static_cast<int>(xdim);
    8786         379 :     poDS->nRasterYSize = static_cast<int>(ydim);
    8787             : 
    8788         379 :     unsigned int k = 0;
    8789        1213 :     for (int j = 0; j < nd; j++)
    8790             :     {
    8791         834 :         if (poDS->m_anDimIds[j] == poDS->nXDimID)
    8792             :         {
    8793         379 :             anBandDimPos[0] = j;  // Save Position of XDim
    8794         379 :             k++;
    8795             :         }
    8796         834 :         if (poDS->m_anDimIds[j] == poDS->nYDimID)
    8797             :         {
    8798         370 :             anBandDimPos[1] = j;  // Save Position of YDim
    8799         370 :             k++;
    8800             :         }
    8801             :     }
    8802             :     // X and Y Dimension Ids were not found!
    8803         379 :     if ((nd >= 2 && k != 2) || (nd == 1 && k != 1))
    8804             :     {
    8805           0 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    8806             :         // with GDALDataset own mutex.
    8807           0 :         delete poDS;
    8808           0 :         CPLAcquireMutex(hNCMutex, 1000.0);
    8809           0 :         return nullptr;
    8810             :     }
    8811             : 
    8812             :     // Read Metadata for this variable.
    8813             : 
    8814             :     // Should disable as is also done at band level, except driver needs the
    8815             :     // variables as metadata (e.g. projection).
    8816         379 :     poDS->ReadAttributes(cdfid, var);
    8817             : 
    8818             :     // Read Metadata for each dimension.
    8819         379 :     int *panDimIds = nullptr;
    8820         379 :     NCDFGetVisibleDims(cdfid, &ndims, &panDimIds);
    8821             :     // With NetCDF-4 groups panDimIds is not always [0..dim_count-1] like
    8822             :     // in NetCDF-3 because we see only the dimensions of the selected group
    8823             :     // and its parents.
    8824             :     // poDS->papszDimName is indexed by dim IDs, so it must contains all IDs
    8825             :     // [0..max(panDimIds)], but they are not all useful so we fill names
    8826             :     // of useless dims with empty string.
    8827         379 :     if (panDimIds)
    8828             :     {
    8829         379 :         const int nMaxDimId = *std::max_element(panDimIds, panDimIds + ndims);
    8830         379 :         std::set<int> oSetExistingDimIds;
    8831        1261 :         for (int i = 0; i < ndims; i++)
    8832             :         {
    8833         882 :             oSetExistingDimIds.insert(panDimIds[i]);
    8834             :         }
    8835         379 :         std::set<int> oSetDimIdsUsedByVar;
    8836        1213 :         for (int i = 0; i < nd; i++)
    8837             :         {
    8838         834 :             oSetDimIdsUsedByVar.insert(poDS->m_anDimIds[i]);
    8839             :         }
    8840        1263 :         for (int j = 0; j <= nMaxDimId; j++)
    8841             :         {
    8842             :             // Is j dim used?
    8843         884 :             if (oSetExistingDimIds.find(j) != oSetExistingDimIds.end())
    8844             :             {
    8845             :                 // Useful dim.
    8846         882 :                 char szTemp[NC_MAX_NAME + 1] = {};
    8847         882 :                 status = nc_inq_dimname(cdfid, j, szTemp);
    8848         882 :                 if (status != NC_NOERR)
    8849             :                 {
    8850           0 :                     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    8851             :                     // deadlock with GDALDataset own
    8852             :                     // mutex.
    8853           0 :                     delete poDS;
    8854           0 :                     CPLAcquireMutex(hNCMutex, 1000.0);
    8855           0 :                     return nullptr;
    8856             :                 }
    8857         882 :                 poDS->papszDimName.AddString(szTemp);
    8858             : 
    8859         882 :                 if (oSetDimIdsUsedByVar.find(j) != oSetDimIdsUsedByVar.end())
    8860             :                 {
    8861         834 :                     int nDimGroupId = -1;
    8862         834 :                     int nDimVarId = -1;
    8863         834 :                     if (NCDFResolveVar(cdfid, poDS->papszDimName[j],
    8864         834 :                                        &nDimGroupId, &nDimVarId) == CE_None)
    8865             :                     {
    8866         609 :                         poDS->ReadAttributes(nDimGroupId, nDimVarId);
    8867             :                     }
    8868             :                 }
    8869             :             }
    8870             :             else
    8871             :             {
    8872             :                 // Useless dim.
    8873           2 :                 poDS->papszDimName.AddString("");
    8874             :             }
    8875             :         }
    8876         379 :         CPLFree(panDimIds);
    8877             :     }
    8878             : 
    8879             :     // Set projection info.
    8880         758 :     std::vector<std::string> aosRemovedMDItems;
    8881         379 :     if (nd > 1)
    8882             :     {
    8883         370 :         poDS->SetProjectionFromVar(cdfid, var,
    8884             :                                    /*bReadSRSOnly=*/false,
    8885             :                                    /* pszGivenGM = */ nullptr,
    8886             :                                    /* returnProjStr = */ nullptr,
    8887             :                                    /* sg = */ nullptr, &aosRemovedMDItems);
    8888             :     }
    8889             : 
    8890             :     // Override bottom-up with GDAL_NETCDF_BOTTOMUP config option.
    8891         379 :     const char *pszValue = CPLGetConfigOption("GDAL_NETCDF_BOTTOMUP", nullptr);
    8892         379 :     if (pszValue)
    8893             :     {
    8894          24 :         poDS->bBottomUp = CPLTestBool(pszValue);
    8895          24 :         CPLDebug("GDAL_netCDF",
    8896             :                  "set bBottomUp=%d because GDAL_NETCDF_BOTTOMUP=%s",
    8897          24 :                  static_cast<int>(poDS->bBottomUp), pszValue);
    8898             :     }
    8899             : 
    8900             :     // Save non-spatial dimension info.
    8901             : 
    8902         379 :     int *panBandZLev = nullptr;
    8903         379 :     int nDim = (nd >= 2) ? 2 : 1;
    8904             :     size_t lev_count;
    8905         379 :     size_t nTotLevCount = 1;
    8906         379 :     nc_type nType = NC_NAT;
    8907             : 
    8908         379 :     if (nd > 2)
    8909             :     {
    8910          66 :         nDim = 2;
    8911          66 :         panBandZLev = static_cast<int *>(CPLCalloc(nd - 2, sizeof(int)));
    8912             : 
    8913         132 :         CPLString osExtraDimNames = "{";
    8914             : 
    8915          66 :         char szDimName[NC_MAX_NAME + 1] = {};
    8916             : 
    8917          66 :         bool bREPORT_EXTRA_DIM_VALUESWarningEmitted = false;
    8918         283 :         for (int j = 0; j < nd; j++)
    8919             :         {
    8920         368 :             if ((poDS->m_anDimIds[j] != poDS->nXDimID) &&
    8921         151 :                 (poDS->m_anDimIds[j] != poDS->nYDimID))
    8922             :             {
    8923          85 :                 nc_inq_dimlen(cdfid, poDS->m_anDimIds[j], &lev_count);
    8924          85 :                 nTotLevCount *= lev_count;
    8925          85 :                 panBandZLev[nDim - 2] = static_cast<int>(lev_count);
    8926          85 :                 anBandDimPos[nDim] = j;  // Save Position of ZDim
    8927             :                 // Save non-spatial dimension names.
    8928          85 :                 if (nc_inq_dimname(cdfid, poDS->m_anDimIds[j], szDimName) ==
    8929             :                     NC_NOERR)
    8930             :                 {
    8931          85 :                     osExtraDimNames += szDimName;
    8932          85 :                     if (j < nd - 3)
    8933             :                     {
    8934          19 :                         osExtraDimNames += ",";
    8935             :                     }
    8936             : 
    8937          85 :                     int nIdxGroupID = -1;
    8938          85 :                     int nIdxVarID = Get1DVariableIndexedByDimension(
    8939          85 :                         cdfid, poDS->m_anDimIds[j], szDimName, true,
    8940          85 :                         &nIdxGroupID);
    8941          85 :                     poDS->m_anExtraDimGroupIds.push_back(nIdxGroupID);
    8942          85 :                     poDS->m_anExtraDimVarIds.push_back(nIdxVarID);
    8943             : 
    8944          85 :                     if (nIdxVarID >= 0)
    8945             :                     {
    8946          76 :                         nc_inq_vartype(nIdxGroupID, nIdxVarID, &nType);
    8947             :                         char szExtraDimDef[NC_MAX_NAME + 1];
    8948          76 :                         snprintf(szExtraDimDef, sizeof(szExtraDimDef),
    8949             :                                  "{%ld,%d}", (long)lev_count, nType);
    8950             :                         char szTemp[NC_MAX_NAME + 32 + 1];
    8951          76 :                         snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_DEF",
    8952             :                                  szDimName);
    8953          76 :                         poDS->aosMetadata.SetNameValue(szTemp, szExtraDimDef);
    8954             : 
    8955             :                         // Retrieving data for unlimited dimensions might be
    8956             :                         // costly on network storage, so don't do it.
    8957             :                         // Each band will capture the value along the extra
    8958             :                         // dimension in its NETCDF_DIM_xxxx band metadata item
    8959             :                         // Addresses use case of
    8960             :                         // https://lists.osgeo.org/pipermail/gdal-dev/2023-May/057209.html
    8961             :                         const bool bIsLocal =
    8962          76 :                             VSIIsLocal(osFilenameForNCOpen.c_str());
    8963             :                         bool bListDimValues =
    8964          77 :                             bIsLocal || lev_count == 1 ||
    8965           1 :                             !NCDFIsUnlimitedDim(poDS->eFormat ==
    8966             :                                                     NCDF_FORMAT_NC4,
    8967           1 :                                                 cdfid, poDS->m_anDimIds[j]);
    8968             :                         const char *pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES =
    8969          76 :                             CPLGetConfigOption(
    8970             :                                 "GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES", nullptr);
    8971          76 :                         if (pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES)
    8972             :                         {
    8973           2 :                             bListDimValues = CPLTestBool(
    8974             :                                 pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES);
    8975             :                         }
    8976          74 :                         else if (!bListDimValues && !bIsLocal &&
    8977           1 :                                  !bREPORT_EXTRA_DIM_VALUESWarningEmitted)
    8978             :                         {
    8979           1 :                             bREPORT_EXTRA_DIM_VALUESWarningEmitted = true;
    8980           1 :                             CPLDebug(
    8981             :                                 "GDAL_netCDF",
    8982             :                                 "Listing extra dimension values is skipped "
    8983             :                                 "because this dataset is hosted on a network "
    8984             :                                 "file system, and such an operation could be "
    8985             :                                 "slow. If you still want to proceed, set the "
    8986             :                                 "GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES "
    8987             :                                 "configuration option to YES");
    8988             :                         }
    8989          76 :                         if (bListDimValues)
    8990             :                         {
    8991          74 :                             char *pszTemp = nullptr;
    8992          74 :                             if (NCDFGet1DVar(nIdxGroupID, nIdxVarID,
    8993          74 :                                              &pszTemp) == CE_None)
    8994             :                             {
    8995          74 :                                 snprintf(szTemp, sizeof(szTemp),
    8996             :                                          "NETCDF_DIM_%s_VALUES", szDimName);
    8997          74 :                                 poDS->aosMetadata.SetNameValue(szTemp, pszTemp);
    8998          74 :                                 CPLFree(pszTemp);
    8999             :                             }
    9000             :                         }
    9001             :                     }
    9002             :                 }
    9003             :                 else
    9004             :                 {
    9005           0 :                     poDS->m_anExtraDimGroupIds.push_back(-1);
    9006           0 :                     poDS->m_anExtraDimVarIds.push_back(-1);
    9007             :                 }
    9008             : 
    9009          85 :                 nDim++;
    9010             :             }
    9011             :         }
    9012          66 :         osExtraDimNames += "}";
    9013          66 :         poDS->aosMetadata.SetNameValue("NETCDF_DIM_EXTRA", osExtraDimNames);
    9014             :     }
    9015             : 
    9016             :     // Store Metadata.
    9017         389 :     for (const auto &osStr : aosRemovedMDItems)
    9018          10 :         poDS->aosMetadata.SetNameValue(osStr.c_str(), nullptr);
    9019             : 
    9020         379 :     poDS->GDALPamDataset::SetMetadata(poDS->aosMetadata);
    9021             : 
    9022             :     // Create bands.
    9023             : 
    9024             :     // Arbitrary threshold.
    9025             :     int nMaxBandCount =
    9026         379 :         atoi(CPLGetConfigOption("GDAL_MAX_BAND_COUNT", "32768"));
    9027         379 :     if (nMaxBandCount <= 0)
    9028           0 :         nMaxBandCount = 32768;
    9029         379 :     if (nTotLevCount > static_cast<unsigned int>(nMaxBandCount))
    9030             :     {
    9031           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    9032             :                  "Limiting number of bands to %d instead of %u", nMaxBandCount,
    9033             :                  static_cast<unsigned int>(nTotLevCount));
    9034           0 :         nTotLevCount = static_cast<unsigned int>(nMaxBandCount);
    9035             :     }
    9036         379 :     if (poDS->nRasterXSize == 0 || poDS->nRasterYSize == 0)
    9037             :     {
    9038           0 :         poDS->nRasterXSize = 0;
    9039           0 :         poDS->nRasterYSize = 0;
    9040           0 :         nTotLevCount = 0;
    9041           0 :         if (poDS->GetLayerCount() == 0)
    9042             :         {
    9043           0 :             CPLFree(panBandZLev);
    9044           0 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9045             :             // deadlock with GDALDataset own mutex.
    9046           0 :             delete poDS;
    9047           0 :             CPLAcquireMutex(hNCMutex, 1000.0);
    9048           0 :             return nullptr;
    9049             :         }
    9050             :     }
    9051         379 :     if (bSeveralVariablesAsBands)
    9052             :     {
    9053           6 :         const auto &listVariables = oMap2DDimsToGroupAndVar.begin()->second;
    9054          24 :         for (int iBand = 0; iBand < static_cast<int>(listVariables.size());
    9055             :              ++iBand)
    9056             :         {
    9057          18 :             int bandVarGroupId = listVariables[iBand].first;
    9058          18 :             int bandVarId = listVariables[iBand].second;
    9059             :             netCDFRasterBand *poBand = new netCDFRasterBand(
    9060           0 :                 netCDFRasterBand::CONSTRUCTOR_OPEN(), poDS, bandVarGroupId,
    9061          18 :                 bandVarId, nDim, 0, nullptr, anBandDimPos.data(), iBand + 1);
    9062          18 :             poDS->SetBand(iBand + 1, poBand);
    9063             :         }
    9064             :     }
    9065             :     else
    9066             :     {
    9067         862 :         for (unsigned int lev = 0; lev < nTotLevCount; lev++)
    9068             :         {
    9069             :             netCDFRasterBand *poBand = new netCDFRasterBand(
    9070           0 :                 netCDFRasterBand::CONSTRUCTOR_OPEN(), poDS, cdfid, var, nDim,
    9071         489 :                 lev, panBandZLev, anBandDimPos.data(), lev + 1);
    9072         489 :             poDS->SetBand(lev + 1, poBand);
    9073             :         }
    9074             :     }
    9075             : 
    9076         379 :     if (panBandZLev)
    9077          66 :         CPLFree(panBandZLev);
    9078             :     // Handle angular geographic coordinates here
    9079             : 
    9080             :     // Initialize any PAM information.
    9081         379 :     if (bTreatAsSubdataset)
    9082             :     {
    9083          69 :         poDS->SetPhysicalFilename(poDS->osFilename);
    9084          69 :         poDS->SetSubdatasetName(osSubdatasetName);
    9085             :     }
    9086             : 
    9087         379 :     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock with
    9088             :     // GDALDataset own mutex.
    9089         379 :     poDS->TryLoadXML();
    9090             : 
    9091         379 :     if (bTreatAsSubdataset)
    9092          69 :         poDS->oOvManager.Initialize(poDS, ":::VIRTUAL:::");
    9093             :     else
    9094         310 :         poDS->oOvManager.Initialize(poDS, poDS->osFilename);
    9095             : 
    9096         379 :     CPLAcquireMutex(hNCMutex, 1000.0);
    9097             : 
    9098         379 :     return poDS;
    9099             : }
    9100             : 
    9101             : /************************************************************************/
    9102             : /*                            CopyMetadata()                            */
    9103             : /*                                                                      */
    9104             : /*      Create a copy of metadata for NC_GLOBAL or a variable           */
    9105             : /************************************************************************/
    9106             : 
    9107         182 : static void CopyMetadata(GDALDataset *poSrcDS, GDALRasterBand *poSrcBand,
    9108             :                          GDALRasterBand *poDstBand, int nCdfId, int CDFVarID,
    9109             :                          const char *pszPrefix)
    9110             : {
    9111             :     // Remove the following band meta but set them later from band data.
    9112         182 :     const char *const papszIgnoreBand[] = {
    9113             :         CF_ADD_OFFSET,  CF_SCALE_FACTOR, "valid_range", "_Unsigned",
    9114             :         NCDF_FillValue, "coordinates",   nullptr};
    9115         182 :     const char *const papszIgnoreGlobal[] = {"NETCDF_DIM_EXTRA", nullptr};
    9116             : 
    9117         182 :     CSLConstList papszMetadata = nullptr;
    9118         182 :     if (poSrcDS)
    9119             :     {
    9120          79 :         papszMetadata = poSrcDS->GetMetadata();
    9121             :     }
    9122         103 :     else if (poSrcBand)
    9123             :     {
    9124         103 :         papszMetadata = poSrcBand->GetMetadata();
    9125             :     }
    9126             : 
    9127         976 :     for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(papszMetadata))
    9128             :     {
    9129             : #ifdef NCDF_DEBUG
    9130             :         CPLDebug("GDAL_netCDF", "copy metadata [%s]=[%s]", pszKey, pszValue);
    9131             : #endif
    9132             : 
    9133         794 :         CPLString osMetaName(pszKey);
    9134             : 
    9135             :         // Check for items that match pszPrefix if applicable.
    9136         794 :         if (pszPrefix && !EQUAL(pszPrefix, ""))
    9137             :         {
    9138             :             // Remove prefix.
    9139         171 :             if (STARTS_WITH(osMetaName.c_str(), pszPrefix))
    9140             :             {
    9141          21 :                 osMetaName = osMetaName.substr(strlen(pszPrefix));
    9142             :             }
    9143             :             // Only copy items that match prefix.
    9144             :             else
    9145             :             {
    9146         150 :                 continue;
    9147             :             }
    9148             :         }
    9149             : 
    9150             :         // Fix various issues with metadata translation.
    9151         644 :         if (CDFVarID == NC_GLOBAL)
    9152             :         {
    9153             :             // Do not copy items in papszIgnoreGlobal and NETCDF_DIM_*.
    9154         874 :             if ((CSLFindString(papszIgnoreGlobal, osMetaName) != -1) ||
    9155         434 :                 (STARTS_WITH(osMetaName, "NETCDF_DIM_")))
    9156          24 :                 continue;
    9157             :             // Remove NC_GLOBAL prefix for netcdf global Metadata.
    9158         416 :             else if (STARTS_WITH(osMetaName, "NC_GLOBAL#"))
    9159             :             {
    9160          59 :                 osMetaName = osMetaName.substr(strlen("NC_GLOBAL#"));
    9161             :             }
    9162             :             // GDAL Metadata renamed as GDAL-[meta].
    9163         357 :             else if (strstr(osMetaName, "#") == nullptr)
    9164             :             {
    9165          22 :                 osMetaName = "GDAL_" + osMetaName;
    9166             :             }
    9167             :             // Keep time, lev and depth information for safe-keeping.
    9168             :             // Time and vertical coordinate handling need improvements.
    9169             :             /*
    9170             :             else if( STARTS_WITH(szMetaName, "time#") )
    9171             :             {
    9172             :                 szMetaName[4] = '-';
    9173             :             }
    9174             :             else if( STARTS_WITH(szMetaName, "lev#") )
    9175             :             {
    9176             :                 szMetaName[3] = '-';
    9177             :             }
    9178             :             else if( STARTS_WITH(szMetaName, "depth#") )
    9179             :             {
    9180             :                 szMetaName[5] = '-';
    9181             :             }
    9182             :             */
    9183             :             // Only copy data without # (previously all data was copied).
    9184         416 :             if (strstr(osMetaName, "#") != nullptr)
    9185         335 :                 continue;
    9186             :             // netCDF attributes do not like the '#' character.
    9187             :             // for( unsigned int h=0; h < strlen(szMetaName) -1 ; h++ ) {
    9188             :             //     if( szMetaName[h] == '#') szMetaName[h] = '-';
    9189             :             // }
    9190             :         }
    9191             :         else
    9192             :         {
    9193             :             // Do not copy varname, stats, NETCDF_DIM_*, nodata
    9194             :             // and items in papszIgnoreBand.
    9195         204 :             if (STARTS_WITH(osMetaName, "NETCDF_VARNAME") ||
    9196         166 :                 STARTS_WITH(osMetaName, "STATISTICS_") ||
    9197         166 :                 STARTS_WITH(osMetaName, "NETCDF_DIM_") ||
    9198         132 :                 STARTS_WITH(osMetaName, "missing_value") ||
    9199         474 :                 STARTS_WITH(osMetaName, "_FillValue") ||
    9200         104 :                 CSLFindString(papszIgnoreBand, osMetaName) != -1)
    9201         116 :                 continue;
    9202             :         }
    9203             : 
    9204             : #ifdef NCDF_DEBUG
    9205             :         CPLDebug("GDAL_netCDF", "copy name=[%s] value=[%s]", osMetaName.c_str(),
    9206             :                  pszValue);
    9207             : #endif
    9208         169 :         if (NCDFPutAttr(nCdfId, CDFVarID, osMetaName, pszValue) != CE_None)
    9209             :         {
    9210           0 :             CPLDebug("GDAL_netCDF", "NCDFPutAttr(%d, %d, %s, %s) failed",
    9211             :                      nCdfId, CDFVarID, osMetaName.c_str(), pszValue);
    9212             :         }
    9213             :     }
    9214             : 
    9215             :     // Set add_offset and scale_factor here if present.
    9216         182 :     if (poSrcBand && poDstBand)
    9217             :     {
    9218             : 
    9219         103 :         int bGotAddOffset = FALSE;
    9220         103 :         const double dfAddOffset = poSrcBand->GetOffset(&bGotAddOffset);
    9221         103 :         int bGotScale = FALSE;
    9222         103 :         const double dfScale = poSrcBand->GetScale(&bGotScale);
    9223             : 
    9224         103 :         if (bGotAddOffset && dfAddOffset != 0.0)
    9225           6 :             poDstBand->SetOffset(dfAddOffset);
    9226         103 :         if (bGotScale && dfScale != 1.0)
    9227           1 :             poDstBand->SetScale(dfScale);
    9228             :     }
    9229         182 : }
    9230             : 
    9231             : /************************************************************************/
    9232             : /*                            CreateLL()                                */
    9233             : /*                                                                      */
    9234             : /*      Shared functionality between netCDFDataset::Create() and        */
    9235             : /*      netCDF::CreateCopy() for creating netcdf file based on a set of */
    9236             : /*      options and a configuration.                                    */
    9237             : /************************************************************************/
    9238             : 
    9239         232 : netCDFDataset *netCDFDataset::CreateLL(const char *pszFilename, int nXSize,
    9240             :                                        int nYSize, int nBandsIn,
    9241             :                                        CSLConstList papszOptions)
    9242             : {
    9243         232 :     if (!((nXSize == 0 && nYSize == 0 && nBandsIn == 0) ||
    9244         137 :           (nXSize > 0 && nYSize > 0 && nBandsIn > 0)))
    9245             :     {
    9246           1 :         return nullptr;
    9247             :     }
    9248             : 
    9249         231 :     CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock with
    9250             :     // GDALDataset own mutex.
    9251         231 :     netCDFDataset *poDS = new netCDFDataset();
    9252         231 :     CPLAcquireMutex(hNCMutex, 1000.0);
    9253             : 
    9254         231 :     poDS->nRasterXSize = nXSize;
    9255         231 :     poDS->nRasterYSize = nYSize;
    9256         231 :     poDS->eAccess = GA_Update;
    9257         231 :     poDS->osFilename = pszFilename;
    9258             : 
    9259             :     // From gtiff driver, is this ok?
    9260             :     /*
    9261             :     poDS->nBlockXSize = nXSize;
    9262             :     poDS->nBlockYSize = 1;
    9263             :     poDS->nBlocksPerBand =
    9264             :         DIV_ROUND_UP((nYSize, poDS->nBlockYSize))
    9265             :         * DIV_ROUND_UP((nXSize, poDS->nBlockXSize));
    9266             :         */
    9267             : 
    9268             :     // process options.
    9269         231 :     poDS->aosCreationOptions = CSLDuplicate(papszOptions);
    9270         231 :     if (!poDS->ProcessCreationOptions())
    9271             :     {
    9272          23 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9273             :         // deadlock with GDALDataset own
    9274             :         // mutex.
    9275          23 :         delete poDS;
    9276          23 :         CPLAcquireMutex(hNCMutex, 1000.0);
    9277          23 :         return nullptr;
    9278             :     }
    9279             : 
    9280         208 :     if (poDS->eMultipleLayerBehavior == SEPARATE_FILES)
    9281             :     {
    9282             :         VSIStatBuf sStat;
    9283           3 :         if (VSIStat(pszFilename, &sStat) == 0)
    9284             :         {
    9285           0 :             if (!VSI_ISDIR(sStat.st_mode))
    9286             :             {
    9287           0 :                 CPLError(CE_Failure, CPLE_FileIO,
    9288             :                          "%s is an existing file, but not a directory",
    9289             :                          pszFilename);
    9290           0 :                 CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9291             :                 // deadlock with GDALDataset own
    9292             :                 // mutex.
    9293           0 :                 delete poDS;
    9294           0 :                 CPLAcquireMutex(hNCMutex, 1000.0);
    9295           0 :                 return nullptr;
    9296             :             }
    9297             :         }
    9298           3 :         else if (VSIMkdir(pszFilename, 0755) != 0)
    9299             :         {
    9300           1 :             CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s directory",
    9301             :                      pszFilename);
    9302           1 :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9303             :             // deadlock with GDALDataset own mutex.
    9304           1 :             delete poDS;
    9305           1 :             CPLAcquireMutex(hNCMutex, 1000.0);
    9306           1 :             return nullptr;
    9307             :         }
    9308             : 
    9309           2 :         return poDS;
    9310             :     }
    9311             :     // Create the dataset.
    9312         410 :     CPLString osFilenameForNCCreate(pszFilename);
    9313             : #if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
    9314             :     if (CPLTestBool(CPLGetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")))
    9315             :     {
    9316             :         char *pszTemp =
    9317             :             CPLRecode(osFilenameForNCCreate, CPL_ENC_UTF8, "CP_ACP");
    9318             :         osFilenameForNCCreate = pszTemp;
    9319             :         CPLFree(pszTemp);
    9320             :     }
    9321             : #endif
    9322             : 
    9323             : #if defined(_WIN32)
    9324             :     {
    9325             :         // Works around bug of msys2 netCDF 4.9.0 package where nc_create()
    9326             :         // crashes
    9327             :         VSIStatBuf sStat;
    9328             :         const std::string osDirname =
    9329             :             CPLGetDirnameSafe(osFilenameForNCCreate.c_str());
    9330             :         if (VSIStat(osDirname.c_str(), &sStat) != 0)
    9331             :         {
    9332             :             CPLError(CE_Failure, CPLE_OpenFailed,
    9333             :                      "Unable to create netCDF file %s: non existing output "
    9334             :                      "directory",
    9335             :                      pszFilename);
    9336             :             CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll
    9337             :             // deadlock with GDALDataset own mutex.
    9338             :             delete poDS;
    9339             :             CPLAcquireMutex(hNCMutex, 1000.0);
    9340             :             return nullptr;
    9341             :         }
    9342             :     }
    9343             : #endif
    9344             : 
    9345             :     int status =
    9346         205 :         nc_create(osFilenameForNCCreate, poDS->nCreateMode, &(poDS->cdfid));
    9347             : 
    9348             :     // Put into define mode.
    9349         205 :     poDS->SetDefineMode(true);
    9350             : 
    9351         205 :     if (status != NC_NOERR)
    9352             :     {
    9353          60 :         CPLError(CE_Failure, CPLE_OpenFailed,
    9354             :                  "Unable to create netCDF file %s (Error code %d): %s .",
    9355             :                  pszFilename, status, nc_strerror(status));
    9356          60 :         CPLReleaseMutex(hNCMutex);  // Release mutex otherwise we'll deadlock
    9357             :         // with GDALDataset own mutex.
    9358          60 :         delete poDS;
    9359          60 :         CPLAcquireMutex(hNCMutex, 1000.0);
    9360          60 :         return nullptr;
    9361             :     }
    9362             : 
    9363             :     // Define dimensions.
    9364         145 :     if (nXSize > 0 && nYSize > 0)
    9365             :     {
    9366          93 :         poDS->papszDimName.AddString(NCDF_DIMNAME_X);
    9367             :         status =
    9368          93 :             nc_def_dim(poDS->cdfid, NCDF_DIMNAME_X, nXSize, &(poDS->nXDimID));
    9369          93 :         NCDF_ERR(status);
    9370          93 :         CPLDebug("GDAL_netCDF", "status nc_def_dim(%d, %s, %d, -) got id %d",
    9371             :                  poDS->cdfid, NCDF_DIMNAME_X, nXSize, poDS->nXDimID);
    9372             : 
    9373          93 :         poDS->papszDimName.AddString(NCDF_DIMNAME_Y);
    9374             :         status =
    9375          93 :             nc_def_dim(poDS->cdfid, NCDF_DIMNAME_Y, nYSize, &(poDS->nYDimID));
    9376          93 :         NCDF_ERR(status);
    9377          93 :         CPLDebug("GDAL_netCDF", "status nc_def_dim(%d, %s, %d, -) got id %d",
    9378             :                  poDS->cdfid, NCDF_DIMNAME_Y, nYSize, poDS->nYDimID);
    9379             :     }
    9380             : 
    9381         145 :     return poDS;
    9382             : }
    9383             : 
    9384             : /************************************************************************/
    9385             : /*                               Create()                               */
    9386             : /************************************************************************/
    9387             : 
    9388         149 : GDALDataset *netCDFDataset::Create(const char *pszFilename, int nXSize,
    9389             :                                    int nYSize, int nBandsIn, GDALDataType eType,
    9390             :                                    CSLConstList papszOptions)
    9391             : {
    9392         149 :     CPLDebug("GDAL_netCDF", "\n=====\nnetCDFDataset::Create(%s, ...)",
    9393             :              pszFilename);
    9394             : 
    9395             :     const char *legacyCreationOp =
    9396         149 :         CSLFetchNameValueDef(papszOptions, "GEOMETRY_ENCODING", "CF_1.8");
    9397         298 :     std::string legacyCreationOp_s = std::string(legacyCreationOp);
    9398             : 
    9399             :     // Check legacy creation op FIRST
    9400             : 
    9401         149 :     bool legacyCreateMode = false;
    9402             : 
    9403         149 :     if (nXSize != 0 || nYSize != 0 || nBandsIn != 0)
    9404             :     {
    9405          56 :         legacyCreateMode = true;
    9406             :     }
    9407          93 :     else if (legacyCreationOp_s == "CF_1.8")
    9408             :     {
    9409          76 :         legacyCreateMode = false;
    9410             :     }
    9411             : 
    9412          17 :     else if (legacyCreationOp_s == "WKT")
    9413             :     {
    9414          17 :         legacyCreateMode = true;
    9415             :     }
    9416             : 
    9417             :     else
    9418             :     {
    9419           0 :         CPLError(
    9420             :             CE_Failure, CPLE_NotSupported,
    9421             :             "Dataset creation option GEOMETRY_ENCODING=%s is not supported.",
    9422             :             legacyCreationOp_s.c_str());
    9423           0 :         return nullptr;
    9424             :     }
    9425             : 
    9426         298 :     CPLStringList aosOptions(CSLDuplicate(papszOptions));
    9427         284 :     if (aosOptions.FetchNameValue("FORMAT") == nullptr &&
    9428         135 :         (eType == GDT_UInt16 || eType == GDT_UInt32 || eType == GDT_UInt64 ||
    9429             :          eType == GDT_Int64))
    9430             :     {
    9431          10 :         CPLDebug("netCDF", "Selecting FORMAT=NC4 due to data type");
    9432          10 :         aosOptions.SetNameValue("FORMAT", "NC4");
    9433             :     }
    9434             : 
    9435         298 :     CPLStringList aosBandNames;
    9436         149 :     if (const char *pszBandNames = aosOptions.FetchNameValue("BAND_NAMES"))
    9437             :     {
    9438             :         aosBandNames =
    9439           2 :             CSLTokenizeString2(pszBandNames, ",", CSLT_HONOURSTRINGS);
    9440             : 
    9441           2 :         if (aosBandNames.Count() != nBandsIn)
    9442             :         {
    9443           1 :             CPLError(CE_Failure, CPLE_OpenFailed,
    9444             :                      "Attempted to create netCDF with %d bands but %d names "
    9445             :                      "provided in BAND_NAMES.",
    9446             :                      nBandsIn, aosBandNames.Count());
    9447             : 
    9448           1 :             return nullptr;
    9449             :         }
    9450             :     }
    9451             : 
    9452         296 :     CPLMutexHolderD(&hNCMutex);
    9453             : 
    9454         148 :     auto poDS = netCDFDataset::CreateLL(pszFilename, nXSize, nYSize, nBandsIn,
    9455         148 :                                         aosOptions.List());
    9456             : 
    9457         148 :     if (!poDS)
    9458          73 :         return nullptr;
    9459             : 
    9460          75 :     if (!legacyCreateMode)
    9461             :     {
    9462          36 :         poDS->bSGSupport = true;
    9463          36 :         poDS->vcdf.enableFullVirtualMode();
    9464             :     }
    9465             : 
    9466             :     else
    9467             :     {
    9468          39 :         poDS->bSGSupport = false;
    9469             :     }
    9470             : 
    9471             :     // Should we write signed or unsigned byte?
    9472             :     // TODO should this only be done in Create()
    9473          75 :     poDS->bSignedData = true;
    9474          75 :     const char *pszValue = CSLFetchNameValueDef(papszOptions, "PIXELTYPE", "");
    9475          75 :     if (eType == GDT_UInt8 && !EQUAL(pszValue, "SIGNEDBYTE"))
    9476          10 :         poDS->bSignedData = false;
    9477             : 
    9478             :     // Add Conventions, GDAL info and history.
    9479          75 :     if (poDS->cdfid >= 0)
    9480             :     {
    9481             :         const char *CF_Vector_Conv =
    9482         110 :             poDS->bSGSupport ||
    9483             :                     // Use of variable length strings require CF-1.8
    9484          37 :                     EQUAL(aosOptions.FetchNameValueDef("FORMAT", ""), "NC4")
    9485             :                 ? NCDF_CONVENTIONS_CF_V1_8
    9486         110 :                 : NCDF_CONVENTIONS_CF_V1_6;
    9487          73 :         poDS->bWriteGDALVersion = CPLTestBool(
    9488             :             CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_VERSION", "YES"));
    9489          73 :         poDS->bWriteGDALHistory = CPLTestBool(
    9490             :             CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_HISTORY", "YES"));
    9491          73 :         NCDFAddGDALHistory(poDS->cdfid, pszFilename, poDS->bWriteGDALVersion,
    9492          73 :                            poDS->bWriteGDALHistory, "", "Create",
    9493             :                            (nBandsIn == 0) ? CF_Vector_Conv
    9494             :                                            : GDAL_DEFAULT_NCDF_CONVENTIONS);
    9495             :     }
    9496             : 
    9497             :     // Define bands.
    9498          99 :     for (int iBand = 1; iBand <= nBandsIn; iBand++)
    9499             :     {
    9500             :         const char *pszBandName =
    9501          24 :             aosBandNames.empty() ? nullptr : aosBandNames[iBand - 1];
    9502             : 
    9503          24 :         poDS->SetBand(iBand, new netCDFRasterBand(
    9504          24 :                                  netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS,
    9505          24 :                                  eType, iBand, poDS->bSignedData, pszBandName));
    9506             :     }
    9507             : 
    9508          75 :     CPLDebug("GDAL_netCDF", "netCDFDataset::Create(%s, ...) done", pszFilename);
    9509             :     // Return same dataset.
    9510          75 :     return poDS;
    9511             : }
    9512             : 
    9513             : template <class T>
    9514         103 : static CPLErr NCDFCopyBand(GDALRasterBand *poSrcBand, GDALRasterBand *poDstBand,
    9515             :                            int nXSize, int nYSize, GDALProgressFunc pfnProgress,
    9516             :                            void *pProgressData)
    9517             : {
    9518         103 :     const GDALDataType eDT = poSrcBand->GetRasterDataType();
    9519         103 :     T *patScanline = static_cast<T *>(VSI_MALLOC2_VERBOSE(nXSize, sizeof(T)));
    9520         103 :     CPLErr eErr = patScanline ? CE_None : CE_Failure;
    9521             : 
    9522        6540 :     for (int iLine = 0; iLine < nYSize && eErr == CE_None; iLine++)
    9523             :     {
    9524        6437 :         eErr = poSrcBand->RasterIO(GF_Read, 0, iLine, nXSize, 1, patScanline,
    9525             :                                    nXSize, 1, eDT, 0, 0, nullptr);
    9526        6437 :         if (eErr != CE_None)
    9527             :         {
    9528           0 :             CPLDebug(
    9529             :                 "GDAL_netCDF",
    9530             :                 "NCDFCopyBand(), poSrcBand->RasterIO() returned error code %d",
    9531             :                 eErr);
    9532             :         }
    9533             :         else
    9534             :         {
    9535        6437 :             eErr =
    9536             :                 poDstBand->RasterIO(GF_Write, 0, iLine, nXSize, 1, patScanline,
    9537             :                                     nXSize, 1, eDT, 0, 0, nullptr);
    9538        6437 :             if (eErr != CE_None)
    9539           0 :                 CPLDebug("GDAL_netCDF",
    9540             :                          "NCDFCopyBand(), poDstBand->RasterIO() returned error "
    9541             :                          "code %d",
    9542             :                          eErr);
    9543             :         }
    9544             : 
    9545        6437 :         if (nYSize > 10 && (iLine % (nYSize / 10) == 1))
    9546             :         {
    9547         377 :             if (!pfnProgress(1.0 * iLine / nYSize, nullptr, pProgressData))
    9548             :             {
    9549           0 :                 eErr = CE_Failure;
    9550           0 :                 CPLError(CE_Failure, CPLE_UserInterrupt,
    9551             :                          "User terminated CreateCopy()");
    9552             :             }
    9553             :         }
    9554             :     }
    9555             : 
    9556         103 :     CPLFree(patScanline);
    9557             : 
    9558         103 :     pfnProgress(1.0, nullptr, pProgressData);
    9559             : 
    9560         103 :     return eErr;
    9561             : }
    9562             : 
    9563             : /************************************************************************/
    9564             : /*                             CreateCopy()                             */
    9565             : /************************************************************************/
    9566             : 
    9567             : GDALDataset *
    9568         100 : netCDFDataset::CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
    9569             :                           CPL_UNUSED int bStrict, CSLConstList papszOptions,
    9570             :                           GDALProgressFunc pfnProgress, void *pProgressData)
    9571             : {
    9572         200 :     CPLMutexHolderD(&hNCMutex);
    9573             : 
    9574         100 :     CPLDebug("GDAL_netCDF", "\n=====\nnetCDFDataset::CreateCopy(%s, ...)",
    9575             :              pszFilename);
    9576             : 
    9577         100 :     if (poSrcDS->GetRootGroup())
    9578             :     {
    9579          12 :         auto poDrv = GDALDriver::FromHandle(GDALGetDriverByName("netCDF"));
    9580          12 :         if (poDrv)
    9581             :         {
    9582          12 :             return poDrv->DefaultCreateCopy(pszFilename, poSrcDS, bStrict,
    9583             :                                             papszOptions, pfnProgress,
    9584          12 :                                             pProgressData);
    9585             :         }
    9586             :     }
    9587             : 
    9588          88 :     const int nBands = poSrcDS->GetRasterCount();
    9589          88 :     const int nXSize = poSrcDS->GetRasterXSize();
    9590          88 :     const int nYSize = poSrcDS->GetRasterYSize();
    9591          88 :     const char *pszWKT = poSrcDS->GetProjectionRef();
    9592             : 
    9593             :     // Check input bands for errors.
    9594          88 :     if (nBands == 0)
    9595             :     {
    9596           1 :         CPLError(CE_Failure, CPLE_NotSupported,
    9597             :                  "NetCDF driver does not support "
    9598             :                  "source datasets with zero bands.");
    9599           1 :         return nullptr;
    9600             :     }
    9601             : 
    9602          87 :     GDALDataType eDT = GDT_Unknown;
    9603          87 :     GDALRasterBand *poSrcBand = nullptr;
    9604         203 :     for (int iBand = 1; iBand <= nBands; iBand++)
    9605             :     {
    9606         120 :         poSrcBand = poSrcDS->GetRasterBand(iBand);
    9607         120 :         eDT = poSrcBand->GetRasterDataType();
    9608         120 :         if (eDT == GDT_Unknown || GDALDataTypeIsComplex(eDT))
    9609             :         {
    9610           4 :             CPLError(CE_Failure, CPLE_NotSupported,
    9611             :                      "NetCDF driver does not support source dataset with band "
    9612             :                      "of complex type.");
    9613           4 :             return nullptr;
    9614             :         }
    9615             :     }
    9616             : 
    9617         166 :     CPLStringList aosBandNames;
    9618          83 :     if (const char *pszBandNames =
    9619          83 :             CSLFetchNameValue(papszOptions, "BAND_NAMES"))
    9620             :     {
    9621             :         aosBandNames =
    9622           2 :             CSLTokenizeString2(pszBandNames, ",", CSLT_HONOURSTRINGS);
    9623             : 
    9624           2 :         if (aosBandNames.Count() != nBands)
    9625             :         {
    9626           1 :             CPLError(CE_Failure, CPLE_OpenFailed,
    9627             :                      "Attempted to create netCDF with %d bands but %d names "
    9628             :                      "provided in BAND_NAMES.",
    9629             :                      nBands, aosBandNames.Count());
    9630             : 
    9631           1 :             return nullptr;
    9632             :         }
    9633             :     }
    9634             : 
    9635          82 :     if (!pfnProgress(0.0, nullptr, pProgressData))
    9636           0 :         return nullptr;
    9637             : 
    9638             :     // Check for extra dimensions.
    9639          82 :     int nDim = 2;
    9640             :     CPLStringList aosExtraDimNames =
    9641         164 :         NCDFTokenizeArray(poSrcDS->GetMetadataItem("NETCDF_DIM_EXTRA", ""));
    9642             : 
    9643             :     // Same as in Create().
    9644         164 :     CPLStringList aosOptions(CSLDuplicate(papszOptions));
    9645          82 :     if (aosOptions.FetchNameValue("FORMAT") == nullptr)
    9646             :     {
    9647          74 :         if (eDT == GDT_UInt16 || eDT == GDT_UInt32 || eDT == GDT_UInt64 ||
    9648             :             eDT == GDT_Int64)
    9649             :         {
    9650           6 :             CPLDebug("netCDF", "Selecting FORMAT=NC4 due to data type");
    9651           6 :             aosOptions.SetNameValue("FORMAT", "NC4");
    9652             :         }
    9653          68 :         else if (!aosExtraDimNames.empty())
    9654             :         {
    9655          14 :             for (const auto &pszDimName : aosExtraDimNames)
    9656             :             {
    9657           9 :                 const auto [nDimSize, nDimType] =
    9658           9 :                     ReadExtraDimDef(poSrcDS, pszDimName);
    9659             :                 {
    9660           9 :                     if (nDimType > NC_DOUBLE)
    9661             :                     {
    9662           1 :                         CPLDebug("netCDF",
    9663             :                                  "Selecting FORMAT=NC4 due to data type of "
    9664             :                                  "extra dimension '%s'",
    9665             :                                  pszDimName);
    9666           1 :                         aosOptions.SetNameValue("FORMAT", "NC4");
    9667           1 :                         break;
    9668             :                     }
    9669             :                 }
    9670             :             }
    9671             :         }
    9672             :     }
    9673             : 
    9674          82 :     netCDFDataset *poDS = netCDFDataset::CreateLL(pszFilename, nXSize, nYSize,
    9675          82 :                                                   nBands, aosOptions.List());
    9676          82 :     if (!poDS)
    9677          12 :         return nullptr;
    9678             : 
    9679             :     // Copy global metadata.
    9680             :     // Add Conventions, GDAL info and history.
    9681          70 :     CopyMetadata(poSrcDS, nullptr, nullptr, poDS->cdfid, NC_GLOBAL, nullptr);
    9682          70 :     const bool bWriteGDALVersion = CPLTestBool(
    9683             :         CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_VERSION", "YES"));
    9684          70 :     const bool bWriteGDALHistory = CPLTestBool(
    9685             :         CSLFetchNameValueDef(papszOptions, "WRITE_GDAL_HISTORY", "YES"));
    9686          70 :     NCDFAddGDALHistory(
    9687             :         poDS->cdfid, pszFilename, bWriteGDALVersion, bWriteGDALHistory,
    9688          70 :         poSrcDS->GetMetadataItem("NC_GLOBAL#history"), "CreateCopy",
    9689          70 :         poSrcDS->GetMetadataItem("NC_GLOBAL#Conventions"));
    9690             : 
    9691          70 :     pfnProgress(0.1, nullptr, pProgressData);
    9692             : 
    9693          70 :     if (!aosExtraDimNames.empty())
    9694             :     {
    9695           6 :         size_t nDimSizeTot = 1;
    9696             :         // first make sure dimensions lengths compatible with band count
    9697             :         // for( int i=0; i<CSLCount(papszExtraDimNames ); i++ ) {
    9698          15 :         for (int i = aosExtraDimNames.size() - 1; i >= 0; i--)
    9699             :         {
    9700           9 :             const auto [nDimSize, _] =
    9701           9 :                 ReadExtraDimDef(poSrcDS, aosExtraDimNames[i]);
    9702           9 :             nDimSizeTot *= nDimSize;
    9703             :         }
    9704           6 :         if (nDimSizeTot == (size_t)nBands)
    9705             :         {
    9706           6 :             nDim = 2 + aosExtraDimNames.size();
    9707             :         }
    9708             :         else
    9709             :         {
    9710             :             // if nBands != #bands computed raise a warning
    9711             :             // just issue a debug message, because it was probably intentional
    9712           0 :             CPLDebug("GDAL_netCDF",
    9713             :                      "Warning: Number of bands (%d) is not compatible with "
    9714             :                      "dimensions "
    9715             :                      "(total=%ld names=%s)",
    9716             :                      nBands, (long)nDimSizeTot,
    9717           0 :                      poSrcDS->GetMetadataItem("NETCDF_DIM_EXTRA", ""));
    9718           0 :             aosExtraDimNames.clear();
    9719             :         }
    9720             :     }
    9721             : 
    9722          70 :     int *panDimIds = static_cast<int *>(CPLCalloc(nDim, sizeof(int)));
    9723          70 :     int *panBandDimPos = static_cast<int *>(CPLCalloc(nDim, sizeof(int)));
    9724             : 
    9725             :     nc_type nVarType;
    9726          70 :     int *panBandZLev = nullptr;
    9727          70 :     int *panDimVarIds = nullptr;
    9728             : 
    9729          70 :     if (nDim > 2)
    9730             :     {
    9731           6 :         panBandZLev = static_cast<int *>(CPLCalloc(nDim - 2, sizeof(int)));
    9732           6 :         panDimVarIds = static_cast<int *>(CPLCalloc(nDim - 2, sizeof(int)));
    9733             : 
    9734             :         // Define all dims.
    9735          15 :         for (int i = aosExtraDimNames.size() - 1; i >= 0; i--)
    9736             :         {
    9737           9 :             poDS->papszDimName.AddString(aosExtraDimNames[i]);
    9738             :             char szTemp[NC_MAX_NAME + 32 + 1];
    9739           9 :             snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_DEF",
    9740             :                      aosExtraDimNames[i]);
    9741             :             const CPLStringList aosExtraDimValues =
    9742          18 :                 NCDFTokenizeArray(poSrcDS->GetMetadataItem(szTemp, ""));
    9743             :             const int nDimSize =
    9744           9 :                 aosExtraDimValues.empty() ? 0 : atoi(aosExtraDimValues[0]);
    9745             :             // nc_type is an enum in netcdf-3, needs casting.
    9746           0 :             nVarType = static_cast<nc_type>(
    9747           9 :                 aosExtraDimValues.size() >= 2 ? atol(aosExtraDimValues[1]) : 0);
    9748           9 :             panBandZLev[i] = nDimSize;
    9749           9 :             panBandDimPos[i + 2] = i;  // Save Position of ZDim.
    9750             : 
    9751             :             // Define dim.
    9752           9 :             int status = nc_def_dim(poDS->cdfid, aosExtraDimNames[i], nDimSize,
    9753           9 :                                     &(panDimIds[i]));
    9754           9 :             NCDF_ERR(status);
    9755             : 
    9756             :             // Define dim var.
    9757           9 :             int anDim[1] = {panDimIds[i]};
    9758           9 :             status = nc_def_var(poDS->cdfid, aosExtraDimNames[i], nVarType, 1,
    9759           9 :                                 anDim, &(panDimVarIds[i]));
    9760           9 :             NCDF_ERR(status);
    9761             : 
    9762             :             // Add dim metadata, using global var# items.
    9763           9 :             snprintf(szTemp, sizeof(szTemp), "%s#", aosExtraDimNames[i]);
    9764           9 :             CopyMetadata(poSrcDS, nullptr, nullptr, poDS->cdfid,
    9765           9 :                          panDimVarIds[i], szTemp);
    9766             :         }
    9767             :     }
    9768             : 
    9769             :     // Copy GeoTransform and Projection.
    9770             : 
    9771             :     // Copy geolocation info.
    9772             :     CSLConstList papszGeolocationInfo =
    9773          70 :         poSrcDS->GetMetadata(GDAL_MDD_GEOLOCATION);
    9774          70 :     if (papszGeolocationInfo != nullptr)
    9775           6 :         poDS->GDALPamDataset::SetMetadata(papszGeolocationInfo,
    9776             :                                           GDAL_MDD_GEOLOCATION);
    9777             : 
    9778             :     // Copy geotransform.
    9779          70 :     bool bGotGeoTransform = false;
    9780          70 :     GDALGeoTransform gt;
    9781          70 :     CPLErr eErr = poSrcDS->GetGeoTransform(gt);
    9782          70 :     if (eErr == CE_None)
    9783             :     {
    9784          52 :         poDS->SetGeoTransform(gt);
    9785             :         // Disable AddProjectionVars() from being called.
    9786          52 :         bGotGeoTransform = true;
    9787          52 :         poDS->m_bHasGeoTransform = false;
    9788             :     }
    9789             : 
    9790             :     // Copy projection.
    9791          70 :     void *pScaledProgress = nullptr;
    9792          70 :     if (bGotGeoTransform || (pszWKT && pszWKT[0] != 0))
    9793             :     {
    9794          53 :         poDS->SetProjection(pszWKT ? pszWKT : "");
    9795             : 
    9796             :         // Now we can call AddProjectionVars() directly.
    9797          53 :         poDS->m_bHasGeoTransform = bGotGeoTransform;
    9798          53 :         poDS->AddProjectionVars(true, nullptr, nullptr);
    9799             :         pScaledProgress =
    9800          53 :             GDALCreateScaledProgress(0.1, 0.25, pfnProgress, pProgressData);
    9801          53 :         poDS->AddProjectionVars(false, GDALScaledProgress, pScaledProgress);
    9802          53 :         GDALDestroyScaledProgress(pScaledProgress);
    9803             :     }
    9804             :     else
    9805             :     {
    9806          17 :         poDS->bBottomUp =
    9807          17 :             CPL_TO_BOOL(CSLFetchBoolean(papszOptions, "WRITE_BOTTOMUP", TRUE));
    9808          17 :         if (papszGeolocationInfo)
    9809             :         {
    9810           4 :             poDS->AddProjectionVars(true, nullptr, nullptr);
    9811           4 :             poDS->AddProjectionVars(false, nullptr, nullptr);
    9812             :         }
    9813             :     }
    9814             : 
    9815             :     // Save X,Y dim positions.
    9816          70 :     panDimIds[nDim - 1] = poDS->nXDimID;
    9817          70 :     panBandDimPos[0] = nDim - 1;
    9818          70 :     panDimIds[nDim - 2] = poDS->nYDimID;
    9819          70 :     panBandDimPos[1] = nDim - 2;
    9820             : 
    9821             :     // Write extra dim values - after projection for optimization.
    9822          70 :     if (nDim > 2)
    9823             :     {
    9824             :         // Make sure we are in data mode.
    9825           6 :         poDS->SetDefineMode(false);
    9826          15 :         for (int i = aosExtraDimNames.size() - 1; i >= 0; i--)
    9827             :         {
    9828             :             char szTemp[NC_MAX_NAME + 32 + 1];
    9829           9 :             snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_VALUES",
    9830             :                      aosExtraDimNames[i]);
    9831           9 :             if (poSrcDS->GetMetadataItem(szTemp) != nullptr)
    9832             :             {
    9833           9 :                 NCDFPut1DVar(poDS->cdfid, panDimVarIds[i],
    9834           9 :                              poSrcDS->GetMetadataItem(szTemp));
    9835             :             }
    9836             :         }
    9837             :     }
    9838             : 
    9839          70 :     pfnProgress(0.25, nullptr, pProgressData);
    9840             : 
    9841             :     // Define Bands.
    9842          70 :     netCDFRasterBand *poBand = nullptr;
    9843          70 :     int nBandID = -1;
    9844             : 
    9845         173 :     for (int iBand = 1; iBand <= nBands; iBand++)
    9846             :     {
    9847         103 :         CPLDebug("GDAL_netCDF", "creating band # %d/%d nDim = %d", iBand,
    9848             :                  nBands, nDim);
    9849             : 
    9850         103 :         poSrcBand = poSrcDS->GetRasterBand(iBand);
    9851         103 :         eDT = poSrcBand->GetRasterDataType();
    9852             : 
    9853             :         // Get var name from NETCDF_VARNAME.
    9854             :         const char *pszNETCDF_VARNAME =
    9855         103 :             poSrcBand->GetMetadataItem("NETCDF_VARNAME");
    9856             :         char szBandName[NC_MAX_NAME + 1];
    9857         103 :         if (!aosBandNames.empty())
    9858             :         {
    9859           2 :             snprintf(szBandName, sizeof(szBandName), "%s",
    9860             :                      aosBandNames[iBand - 1]);
    9861             :         }
    9862         101 :         else if (pszNETCDF_VARNAME)
    9863             :         {
    9864          38 :             if (nBands > 1 && aosExtraDimNames.empty())
    9865           0 :                 snprintf(szBandName, sizeof(szBandName), "%s%d",
    9866             :                          pszNETCDF_VARNAME, iBand);
    9867             :             else
    9868          38 :                 snprintf(szBandName, sizeof(szBandName), "%s",
    9869             :                          pszNETCDF_VARNAME);
    9870             :         }
    9871             :         else
    9872             :         {
    9873          63 :             szBandName[0] = '\0';
    9874             :         }
    9875             : 
    9876             :         // Get long_name from <var>#long_name.
    9877         103 :         const char *pszLongName = "";
    9878         103 :         if (pszNETCDF_VARNAME)
    9879             :         {
    9880             :             pszLongName =
    9881          76 :                 poSrcDS->GetMetadataItem(std::string(pszNETCDF_VARNAME)
    9882          38 :                                              .append("#")
    9883          38 :                                              .append(CF_LNG_NAME)
    9884          38 :                                              .c_str());
    9885          38 :             if (!pszLongName)
    9886          25 :                 pszLongName = "";
    9887             :         }
    9888             : 
    9889         103 :         constexpr bool bSignedData = false;
    9890             : 
    9891         103 :         if (nDim > 2)
    9892          28 :             poBand = new netCDFRasterBand(
    9893          28 :                 netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS, eDT, iBand,
    9894             :                 bSignedData, szBandName, pszLongName, nBandID, nDim, iBand - 1,
    9895          28 :                 panBandZLev, panBandDimPos, panDimIds);
    9896             :         else
    9897          75 :             poBand = new netCDFRasterBand(
    9898          75 :                 netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS, eDT, iBand,
    9899          75 :                 bSignedData, szBandName, pszLongName);
    9900             : 
    9901         103 :         poDS->SetBand(iBand, poBand);
    9902             : 
    9903             :         // Set nodata value, if any.
    9904         103 :         GDALCopyNoDataValue(poBand, poSrcBand);
    9905             : 
    9906             :         // Copy Metadata for band.
    9907         103 :         CopyMetadata(nullptr, poSrcDS->GetRasterBand(iBand), poBand,
    9908             :                      poDS->cdfid, poBand->nZId);
    9909             : 
    9910             :         // If more than 2D pass the first band's netcdf var ID to subsequent
    9911             :         // bands.
    9912         103 :         if (nDim > 2)
    9913          28 :             nBandID = poBand->nZId;
    9914             :     }
    9915             : 
    9916             :     // Write projection variable to band variable.
    9917          70 :     poDS->AddGridMappingRef();
    9918             : 
    9919          70 :     pfnProgress(0.5, nullptr, pProgressData);
    9920             : 
    9921             :     // Write bands.
    9922             : 
    9923             :     // Make sure we are in data mode.
    9924          70 :     poDS->SetDefineMode(false);
    9925             : 
    9926          70 :     double dfTemp = 0.5;
    9927             : 
    9928          70 :     eErr = CE_None;
    9929             : 
    9930         173 :     for (int iBand = 1; iBand <= nBands && eErr == CE_None; iBand++)
    9931             :     {
    9932         103 :         const double dfTemp2 = dfTemp + 0.4 / nBands;
    9933         103 :         pScaledProgress = GDALCreateScaledProgress(dfTemp, dfTemp2, pfnProgress,
    9934             :                                                    pProgressData);
    9935         103 :         dfTemp = dfTemp2;
    9936             : 
    9937         103 :         CPLDebug("GDAL_netCDF", "copying band data # %d/%d ", iBand, nBands);
    9938             : 
    9939         103 :         poSrcBand = poSrcDS->GetRasterBand(iBand);
    9940         103 :         eDT = poSrcBand->GetRasterDataType();
    9941             : 
    9942         103 :         GDALRasterBand *poDstBand = poDS->GetRasterBand(iBand);
    9943             : 
    9944             :         // Copy band data.
    9945         103 :         if (eDT == GDT_UInt8)
    9946             :         {
    9947          61 :             CPLDebug("GDAL_netCDF", "GByte Band#%d", iBand);
    9948          61 :             eErr = NCDFCopyBand<GByte>(poSrcBand, poDstBand, nXSize, nYSize,
    9949             :                                        GDALScaledProgress, pScaledProgress);
    9950             :         }
    9951          42 :         else if (eDT == GDT_Int8)
    9952             :         {
    9953           1 :             CPLDebug("GDAL_netCDF", "GInt8 Band#%d", iBand);
    9954           1 :             eErr = NCDFCopyBand<GInt8>(poSrcBand, poDstBand, nXSize, nYSize,
    9955             :                                        GDALScaledProgress, pScaledProgress);
    9956             :         }
    9957          41 :         else if (eDT == GDT_UInt16)
    9958             :         {
    9959           2 :             CPLDebug("GDAL_netCDF", "GUInt16 Band#%d", iBand);
    9960           2 :             eErr = NCDFCopyBand<GInt16>(poSrcBand, poDstBand, nXSize, nYSize,
    9961             :                                         GDALScaledProgress, pScaledProgress);
    9962             :         }
    9963          39 :         else if (eDT == GDT_Int16)
    9964             :         {
    9965           5 :             CPLDebug("GDAL_netCDF", "GInt16 Band#%d", iBand);
    9966           5 :             eErr = NCDFCopyBand<GUInt16>(poSrcBand, poDstBand, nXSize, nYSize,
    9967             :                                          GDALScaledProgress, pScaledProgress);
    9968             :         }
    9969          34 :         else if (eDT == GDT_UInt32)
    9970             :         {
    9971           2 :             CPLDebug("GDAL_netCDF", "GUInt32 Band#%d", iBand);
    9972           2 :             eErr = NCDFCopyBand<GUInt32>(poSrcBand, poDstBand, nXSize, nYSize,
    9973             :                                          GDALScaledProgress, pScaledProgress);
    9974             :         }
    9975          32 :         else if (eDT == GDT_Int32)
    9976             :         {
    9977          18 :             CPLDebug("GDAL_netCDF", "GInt32 Band#%d", iBand);
    9978          18 :             eErr = NCDFCopyBand<GInt32>(poSrcBand, poDstBand, nXSize, nYSize,
    9979             :                                         GDALScaledProgress, pScaledProgress);
    9980             :         }
    9981          14 :         else if (eDT == GDT_UInt64)
    9982             :         {
    9983           2 :             CPLDebug("GDAL_netCDF", "GUInt64 Band#%d", iBand);
    9984           2 :             eErr = NCDFCopyBand<std::uint64_t>(poSrcBand, poDstBand, nXSize,
    9985             :                                                nYSize, GDALScaledProgress,
    9986             :                                                pScaledProgress);
    9987             :         }
    9988          12 :         else if (eDT == GDT_Int64)
    9989             :         {
    9990           2 :             CPLDebug("GDAL_netCDF", "GInt64 Band#%d", iBand);
    9991             :             eErr =
    9992           2 :                 NCDFCopyBand<std::int64_t>(poSrcBand, poDstBand, nXSize, nYSize,
    9993             :                                            GDALScaledProgress, pScaledProgress);
    9994             :         }
    9995          10 :         else if (eDT == GDT_Float32)
    9996             :         {
    9997           8 :             CPLDebug("GDAL_netCDF", "float Band#%d", iBand);
    9998           8 :             eErr = NCDFCopyBand<float>(poSrcBand, poDstBand, nXSize, nYSize,
    9999             :                                        GDALScaledProgress, pScaledProgress);
   10000             :         }
   10001           2 :         else if (eDT == GDT_Float64)
   10002             :         {
   10003           2 :             CPLDebug("GDAL_netCDF", "double Band#%d", iBand);
   10004           2 :             eErr = NCDFCopyBand<double>(poSrcBand, poDstBand, nXSize, nYSize,
   10005             :                                         GDALScaledProgress, pScaledProgress);
   10006             :         }
   10007             :         else
   10008             :         {
   10009           0 :             CPLError(CE_Failure, CPLE_NotSupported,
   10010             :                      "The NetCDF driver does not support GDAL data type %d",
   10011             :                      eDT);
   10012             :         }
   10013             : 
   10014         103 :         GDALDestroyScaledProgress(pScaledProgress);
   10015             :     }
   10016             : 
   10017          70 :     delete (poDS);
   10018             : 
   10019          70 :     CPLFree(panDimIds);
   10020          70 :     CPLFree(panBandDimPos);
   10021          70 :     CPLFree(panBandZLev);
   10022          70 :     CPLFree(panDimVarIds);
   10023             : 
   10024          70 :     if (eErr != CE_None)
   10025           0 :         return nullptr;
   10026             : 
   10027          70 :     pfnProgress(0.95, nullptr, pProgressData);
   10028             : 
   10029             :     // Re-open dataset so we can return it.
   10030         140 :     CPLStringList aosOpenOptions;
   10031          70 :     aosOpenOptions.AddString("VARIABLES_AS_BANDS=YES");
   10032          70 :     GDALOpenInfo oOpenInfo(pszFilename, GA_Update);
   10033          70 :     oOpenInfo.nOpenFlags = GDAL_OF_RASTER | GDAL_OF_UPDATE;
   10034          70 :     oOpenInfo.papszOpenOptions = aosOpenOptions.List();
   10035          70 :     auto poRetDS = Open(&oOpenInfo);
   10036             : 
   10037             :     // PAM cloning is disabled. See bug #4244.
   10038             :     // if( poDS )
   10039             :     //     poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
   10040             : 
   10041          70 :     pfnProgress(1.0, nullptr, pProgressData);
   10042             : 
   10043          70 :     return poRetDS;
   10044             : }
   10045             : 
   10046             : // Note: some logic depends on bIsProjected and bIsGeoGraphic.
   10047             : // May not be known when Create() is called, see AddProjectionVars().
   10048         345 : bool netCDFDataset::ProcessCreationOptions()
   10049             : {
   10050         345 :     const char *pszConfig = aosCreationOptions.FetchNameValue("CONFIG_FILE");
   10051         345 :     if (pszConfig != nullptr)
   10052             :     {
   10053          26 :         if (oWriterConfig.Parse(pszConfig))
   10054             :         {
   10055             :             // Override dataset creation options from the config file
   10056           2 :             for (const auto &[osName, osValue] :
   10057           5 :                  oWriterConfig.m_oDatasetCreationOptions)
   10058             :             {
   10059           1 :                 aosCreationOptions.SetNameValue(osName, osValue);
   10060             :             }
   10061             :         }
   10062             :         else
   10063             :         {
   10064          23 :             return false;
   10065             :         }
   10066             :     }
   10067             : 
   10068             :     // File format.
   10069         322 :     eFormat = NCDF_FORMAT_NC;
   10070         322 :     const char *pszValue = aosCreationOptions.FetchNameValue("FORMAT");
   10071         322 :     if (pszValue != nullptr)
   10072             :     {
   10073         151 :         if (EQUAL(pszValue, "NC"))
   10074             :         {
   10075           3 :             eFormat = NCDF_FORMAT_NC;
   10076             :         }
   10077             : #ifdef NETCDF_HAS_NC2
   10078         148 :         else if (EQUAL(pszValue, "NC2"))
   10079             :         {
   10080           1 :             eFormat = NCDF_FORMAT_NC2;
   10081             :         }
   10082             : #endif
   10083         147 :         else if (EQUAL(pszValue, "NC4"))
   10084             :         {
   10085         143 :             eFormat = NCDF_FORMAT_NC4;
   10086             :         }
   10087           4 :         else if (EQUAL(pszValue, "NC4C"))
   10088             :         {
   10089           4 :             eFormat = NCDF_FORMAT_NC4C;
   10090             :         }
   10091             :         else
   10092             :         {
   10093           0 :             CPLError(CE_Warning, CPLE_NotSupported,
   10094             :                      "FORMAT=%s in not supported, using the default NC format.",
   10095             :                      pszValue);
   10096             :         }
   10097             :     }
   10098             : 
   10099             :     // COMPRESS option.
   10100         322 :     pszValue = aosCreationOptions.FetchNameValue("COMPRESS");
   10101         322 :     if (pszValue != nullptr)
   10102             :     {
   10103           3 :         if (EQUAL(pszValue, "NONE"))
   10104             :         {
   10105           1 :             eCompress = NCDF_COMPRESS_NONE;
   10106             :         }
   10107           2 :         else if (EQUAL(pszValue, "DEFLATE"))
   10108             :         {
   10109           2 :             eCompress = NCDF_COMPRESS_DEFLATE;
   10110           2 :             if (!((eFormat == NCDF_FORMAT_NC4) ||
   10111           2 :                   (eFormat == NCDF_FORMAT_NC4C)))
   10112             :             {
   10113           1 :                 CPLError(CE_Warning, CPLE_IllegalArg,
   10114             :                          "NOTICE: Format set to NC4C because compression is "
   10115             :                          "set to DEFLATE.");
   10116           1 :                 eFormat = NCDF_FORMAT_NC4C;
   10117             :             }
   10118             :         }
   10119             :         else
   10120             :         {
   10121           0 :             CPLError(CE_Warning, CPLE_NotSupported,
   10122             :                      "COMPRESS=%s is not supported.", pszValue);
   10123             :         }
   10124             :     }
   10125             : 
   10126             :     // ZLEVEL option.
   10127         322 :     pszValue = aosCreationOptions.FetchNameValue("ZLEVEL");
   10128         322 :     if (pszValue != nullptr)
   10129             :     {
   10130           1 :         nZLevel = atoi(pszValue);
   10131           1 :         if (!(nZLevel >= 1 && nZLevel <= 9))
   10132             :         {
   10133           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
   10134             :                      "ZLEVEL=%s value not recognised, ignoring.", pszValue);
   10135           0 :             nZLevel = NCDF_DEFLATE_LEVEL;
   10136             :         }
   10137             :     }
   10138             : 
   10139             :     // CHUNKING option.
   10140         322 :     bChunking = aosCreationOptions.FetchBool("CHUNKING", true);
   10141             : 
   10142             :     // MULTIPLE_LAYERS option.
   10143             :     const char *pszMultipleLayerBehavior =
   10144         322 :         aosCreationOptions.FetchNameValueDef("MULTIPLE_LAYERS", "NO");
   10145             :     const char *pszGeometryEnc =
   10146         322 :         aosCreationOptions.FetchNameValueDef("GEOMETRY_ENCODING", "CF_1.8");
   10147         322 :     if (EQUAL(pszMultipleLayerBehavior, "NO") ||
   10148           4 :         EQUAL(pszGeometryEnc, "CF_1.8"))
   10149             :     {
   10150         318 :         eMultipleLayerBehavior = SINGLE_LAYER;
   10151             :     }
   10152           4 :     else if (EQUAL(pszMultipleLayerBehavior, "SEPARATE_FILES"))
   10153             :     {
   10154           3 :         eMultipleLayerBehavior = SEPARATE_FILES;
   10155             :     }
   10156           1 :     else if (EQUAL(pszMultipleLayerBehavior, "SEPARATE_GROUPS"))
   10157             :     {
   10158           1 :         if (eFormat == NCDF_FORMAT_NC4)
   10159             :         {
   10160           1 :             eMultipleLayerBehavior = SEPARATE_GROUPS;
   10161             :         }
   10162             :         else
   10163             :         {
   10164           0 :             CPLError(CE_Warning, CPLE_IllegalArg,
   10165             :                      "MULTIPLE_LAYERS=%s is recognised only with FORMAT=NC4",
   10166             :                      pszMultipleLayerBehavior);
   10167             :         }
   10168             :     }
   10169             :     else
   10170             :     {
   10171           0 :         CPLError(CE_Warning, CPLE_IllegalArg,
   10172             :                  "MULTIPLE_LAYERS=%s not recognised", pszMultipleLayerBehavior);
   10173             :     }
   10174             : 
   10175             :     // Set nCreateMode based on eFormat.
   10176         322 :     switch (eFormat)
   10177             :     {
   10178             : #ifdef NETCDF_HAS_NC2
   10179           1 :         case NCDF_FORMAT_NC2:
   10180           1 :             nCreateMode = NC_CLOBBER | NC_64BIT_OFFSET;
   10181           1 :             break;
   10182             : #endif
   10183         143 :         case NCDF_FORMAT_NC4:
   10184         143 :             nCreateMode = NC_CLOBBER | NC_NETCDF4;
   10185         143 :             break;
   10186           5 :         case NCDF_FORMAT_NC4C:
   10187           5 :             nCreateMode = NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL;
   10188           5 :             break;
   10189         173 :         case NCDF_FORMAT_NC:
   10190             :         default:
   10191         173 :             nCreateMode = NC_CLOBBER;
   10192         173 :             break;
   10193             :     }
   10194             : 
   10195         322 :     CPLDebug("GDAL_netCDF", "file options: format=%d compress=%d zlevel=%d",
   10196         322 :              eFormat, eCompress, nZLevel);
   10197             : 
   10198         322 :     return true;
   10199             : }
   10200             : 
   10201         167 : int netCDFDataset::DefVarDeflate(int nVarId, bool bChunkingArg) const
   10202             : {
   10203         167 :     if (eCompress == NCDF_COMPRESS_DEFLATE)
   10204             :     {
   10205             :         // Must set chunk size to avoid huge performance hit (set
   10206             :         // bChunkingArg=TRUE)
   10207             :         // perhaps another solution it to change the chunk cache?
   10208             :         // http://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html#Chunk-Cache
   10209             :         // TODO: make sure this is okay.
   10210           2 :         CPLDebug("GDAL_netCDF", "DefVarDeflate(%d, %d) nZlevel=%d", nVarId,
   10211           2 :                  static_cast<int>(bChunkingArg), nZLevel);
   10212             : 
   10213           2 :         int status = nc_def_var_deflate(cdfid, nVarId, 1, 1, nZLevel);
   10214           2 :         NCDF_ERR(status);
   10215             : 
   10216           2 :         if (status == NC_NOERR && bChunkingArg && bChunking)
   10217             :         {
   10218             :             // set chunking to be 1 for all dims, except X dim
   10219             :             // size_t chunksize[] = { 1, (size_t)nRasterXSize };
   10220             :             size_t chunksize[MAX_NC_DIMS];
   10221             :             int nd;
   10222           2 :             nc_inq_varndims(cdfid, nVarId, &nd);
   10223           2 :             chunksize[0] = (size_t)1;
   10224           2 :             chunksize[1] = (size_t)1;
   10225           2 :             for (int i = 2; i < nd; i++)
   10226           0 :                 chunksize[i] = (size_t)1;
   10227           2 :             chunksize[nd - 1] = (size_t)nRasterXSize;
   10228             : 
   10229             :             // Config options just for testing purposes
   10230             :             const char *pszBlockXSize =
   10231           2 :                 CPLGetConfigOption("BLOCKXSIZE", nullptr);
   10232           2 :             if (pszBlockXSize)
   10233           0 :                 chunksize[nd - 1] = (size_t)atoi(pszBlockXSize);
   10234             : 
   10235             :             const char *pszBlockYSize =
   10236           2 :                 CPLGetConfigOption("BLOCKYSIZE", nullptr);
   10237           2 :             if (nd >= 2 && pszBlockYSize)
   10238           0 :                 chunksize[nd - 2] = (size_t)atoi(pszBlockYSize);
   10239             : 
   10240           2 :             CPLDebug("GDAL_netCDF",
   10241             :                      "DefVarDeflate() chunksize={%ld, %ld} chunkX=%ld nd=%d",
   10242           2 :                      (long)chunksize[0], (long)chunksize[1],
   10243           2 :                      (long)chunksize[nd - 1], nd);
   10244             : #ifdef NCDF_DEBUG
   10245             :             for (int i = 0; i < nd; i++)
   10246             :                 CPLDebug("GDAL_netCDF", "DefVarDeflate() chunk[%d]=%ld", i,
   10247             :                          chunksize[i]);
   10248             : #endif
   10249             : 
   10250           2 :             status = nc_def_var_chunking(cdfid, nVarId, NC_CHUNKED, chunksize);
   10251           2 :             NCDF_ERR(status);
   10252             :         }
   10253             :         else
   10254             :         {
   10255           0 :             CPLDebug("GDAL_netCDF", "chunksize not set");
   10256             :         }
   10257           2 :         return status;
   10258             :     }
   10259         165 :     return NC_NOERR;
   10260             : }
   10261             : 
   10262             : /************************************************************************/
   10263             : /*                          NCDFUnloadDriver()                          */
   10264             : /************************************************************************/
   10265             : 
   10266          10 : static void NCDFUnloadDriver(CPL_UNUSED GDALDriver *poDriver)
   10267             : {
   10268          10 :     if (hNCMutex != nullptr)
   10269           6 :         CPLDestroyMutex(hNCMutex);
   10270          10 :     hNCMutex = nullptr;
   10271          10 : }
   10272             : 
   10273             : /************************************************************************/
   10274             : /*                        GDALRegister_netCDF()                         */
   10275             : /************************************************************************/
   10276             : 
   10277             : class GDALnetCDFDriver final : public GDALDriver
   10278             : {
   10279             :   public:
   10280          20 :     GDALnetCDFDriver() = default;
   10281             : 
   10282             :     const char *GetMetadataItem(const char *pszName,
   10283             :                                 const char *pszDomain) override;
   10284             : 
   10285         122 :     CSLConstList GetMetadata(const char *pszDomain) override
   10286             :     {
   10287         244 :         std::lock_guard oLock(m_oMutex);
   10288         122 :         InitializeDCAPVirtualIO();
   10289         244 :         return GDALDriver::GetMetadata(pszDomain);
   10290             :     }
   10291             : 
   10292             :   private:
   10293             :     std::recursive_mutex m_oMutex{};
   10294             :     bool m_bInitialized = false;
   10295             : 
   10296         135 :     void InitializeDCAPVirtualIO()
   10297             :     {
   10298         135 :         if (!m_bInitialized)
   10299             :         {
   10300          11 :             m_bInitialized = true;
   10301             : 
   10302             : #ifdef ENABLE_UFFD
   10303          11 :             if (CPLIsUserFaultMappingSupported())
   10304             :             {
   10305          11 :                 SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
   10306             :             }
   10307             : #endif
   10308             :         }
   10309         135 :     }
   10310             : };
   10311             : 
   10312        1454 : const char *GDALnetCDFDriver::GetMetadataItem(const char *pszName,
   10313             :                                               const char *pszDomain)
   10314             : {
   10315        2908 :     std::lock_guard oLock(m_oMutex);
   10316        1454 :     if (EQUAL(pszName, GDAL_DCAP_VIRTUALIO))
   10317             :     {
   10318          13 :         InitializeDCAPVirtualIO();
   10319             :     }
   10320        2908 :     return GDALDriver::GetMetadataItem(pszName, pszDomain);
   10321             : }
   10322             : 
   10323          20 : void GDALRegister_netCDF()
   10324             : 
   10325             : {
   10326          20 :     if (!GDAL_CHECK_VERSION("netCDF driver"))
   10327           0 :         return;
   10328             : 
   10329          20 :     if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
   10330           0 :         return;
   10331             : 
   10332          20 :     GDALDriver *poDriver = new GDALnetCDFDriver();
   10333          20 :     netCDFDriverSetCommonMetadata(poDriver);
   10334             : 
   10335          20 :     poDriver->SetMetadataItem("NETCDF_CONVENTIONS",
   10336          20 :                               GDAL_DEFAULT_NCDF_CONVENTIONS);
   10337          20 :     poDriver->SetMetadataItem("NETCDF_VERSION", nc_inq_libvers());
   10338             : 
   10339             :     // Set pfns and register driver.
   10340          20 :     poDriver->pfnOpen = netCDFDataset::Open;
   10341          20 :     poDriver->pfnCreateCopy = netCDFDataset::CreateCopy;
   10342          20 :     poDriver->pfnCreate = netCDFDataset::Create;
   10343          20 :     poDriver->pfnCreateMultiDimensional = netCDFDataset::CreateMultiDimensional;
   10344          20 :     poDriver->pfnUnloadDriver = NCDFUnloadDriver;
   10345             : 
   10346          20 :     GetGDALDriverManager()->RegisterDriver(poDriver);
   10347             : }
   10348             : 
   10349             : /************************************************************************/
   10350             : /*                            New functions                             */
   10351             : /************************************************************************/
   10352             : 
   10353             : /* Test for GDAL version string >= target */
   10354         284 : static bool NCDFIsGDALVersionGTE(const char *pszVersion, int nTarget)
   10355             : {
   10356             : 
   10357             :     // Valid strings are "GDAL 1.9dev, released 2011/01/18" and "GDAL 1.8.1 ".
   10358         284 :     if (pszVersion == nullptr || EQUAL(pszVersion, ""))
   10359           0 :         return false;
   10360         284 :     else if (!STARTS_WITH_CI(pszVersion, "GDAL "))
   10361           0 :         return false;
   10362             :     // 2.0dev of 2011/12/29 has been later renamed as 1.10dev.
   10363         284 :     else if (EQUAL("GDAL 2.0dev, released 2011/12/29", pszVersion))
   10364           0 :         return nTarget <= GDAL_COMPUTE_VERSION(1, 10, 0);
   10365         284 :     else if (STARTS_WITH_CI(pszVersion, "GDAL 1.9dev"))
   10366           2 :         return nTarget <= 1900;
   10367         282 :     else if (STARTS_WITH_CI(pszVersion, "GDAL 1.8dev"))
   10368           0 :         return nTarget <= 1800;
   10369             : 
   10370         282 :     const CPLStringList aosTokens(CSLTokenizeString2(pszVersion + 5, ".", 0));
   10371             : 
   10372         282 :     int nVersions[] = {0, 0, 0, 0};
   10373        1128 :     for (int iToken = 0; iToken < std::min(4, aosTokens.size()); iToken++)
   10374             :     {
   10375         846 :         nVersions[iToken] = atoi(aosTokens[iToken]);
   10376         846 :         if (nVersions[iToken] < 0)
   10377           0 :             nVersions[iToken] = 0;
   10378         846 :         else if (nVersions[iToken] > 99)
   10379           0 :             nVersions[iToken] = 99;
   10380             :     }
   10381             : 
   10382         282 :     int nVersion = 0;
   10383         282 :     if (nVersions[0] > 1 || nVersions[1] >= 10)
   10384         282 :         nVersion =
   10385         282 :             GDAL_COMPUTE_VERSION(nVersions[0], nVersions[1], nVersions[2]);
   10386             :     else
   10387           0 :         nVersion = nVersions[0] * 1000 + nVersions[1] * 100 +
   10388           0 :                    nVersions[2] * 10 + nVersions[3];
   10389             : 
   10390         282 :     return nTarget <= nVersion;
   10391             : }
   10392             : 
   10393             : // Add Conventions, GDAL version and history.
   10394         147 : static void NCDFAddGDALHistory(int fpImage, const char *pszFilename,
   10395             :                                bool bWriteGDALVersion, bool bWriteGDALHistory,
   10396             :                                const char *pszOldHist,
   10397             :                                const char *pszFunctionName,
   10398             :                                const char *pszCFVersion)
   10399             : {
   10400         147 :     if (pszCFVersion == nullptr)
   10401             :     {
   10402          48 :         pszCFVersion = GDAL_DEFAULT_NCDF_CONVENTIONS;
   10403             :     }
   10404         147 :     int status = nc_put_att_text(fpImage, NC_GLOBAL, "Conventions",
   10405             :                                  strlen(pszCFVersion), pszCFVersion);
   10406         147 :     NCDF_ERR(status);
   10407             : 
   10408         147 :     if (bWriteGDALVersion)
   10409             :     {
   10410         145 :         const char *pszNCDF_GDAL = GDALVersionInfo("--version");
   10411         145 :         status = nc_put_att_text(fpImage, NC_GLOBAL, "GDAL",
   10412             :                                  strlen(pszNCDF_GDAL), pszNCDF_GDAL);
   10413         145 :         NCDF_ERR(status);
   10414             :     }
   10415             : 
   10416         147 :     if (bWriteGDALHistory)
   10417             :     {
   10418             :         // Add history.
   10419         290 :         CPLString osTmp;
   10420             : #ifdef GDAL_SET_CMD_LINE_DEFINED_TMP
   10421             :         if (!EQUAL(GDALGetCmdLine(), ""))
   10422             :             osTmp = GDALGetCmdLine();
   10423             :         else
   10424             :             osTmp =
   10425             :                 CPLSPrintf("GDAL %s( %s, ... )", pszFunctionName, pszFilename);
   10426             : #else
   10427         145 :         osTmp = CPLSPrintf("GDAL %s( %s, ... )", pszFunctionName, pszFilename);
   10428             : #endif
   10429             : 
   10430         145 :         NCDFAddHistory(fpImage, osTmp.c_str(), pszOldHist);
   10431             :     }
   10432           2 :     else if (pszOldHist != nullptr)
   10433             :     {
   10434           0 :         status = nc_put_att_text(fpImage, NC_GLOBAL, "history",
   10435             :                                  strlen(pszOldHist), pszOldHist);
   10436           0 :         NCDF_ERR(status);
   10437             :     }
   10438         147 : }
   10439             : 
   10440             : // Code taken from cdo and libcdi, used for writing the history attribute.
   10441             : 
   10442             : // void cdoDefHistory(int fileID, char *histstring)
   10443         145 : static void NCDFAddHistory(int fpImage, const char *pszAddHist,
   10444             :                            const char *pszOldHist)
   10445             : {
   10446             :     // Check pszOldHist - as if there was no previous history, it will be
   10447             :     // a null pointer - if so set as empty.
   10448         145 :     if (nullptr == pszOldHist)
   10449             :     {
   10450          59 :         pszOldHist = "";
   10451             :     }
   10452             : 
   10453             :     char strtime[32];
   10454         145 :     strtime[0] = '\0';
   10455             : 
   10456         145 :     time_t tp = time(nullptr);
   10457         145 :     if (tp != -1)
   10458             :     {
   10459             :         struct tm ltime;
   10460         145 :         VSILocalTime(&tp, &ltime);
   10461         145 :         (void)strftime(strtime, sizeof(strtime),
   10462             :                        "%a %b %d %H:%M:%S %Y: ", &ltime);
   10463             :     }
   10464             : 
   10465             :     // status = nc_get_att_text(fpImage, NC_GLOBAL,
   10466             :     //                           "history", pszOldHist);
   10467             :     // printf("status: %d pszOldHist: [%s]\n",status,pszOldHist);
   10468             : 
   10469         145 :     size_t nNewHistSize =
   10470         145 :         strlen(pszOldHist) + strlen(strtime) + strlen(pszAddHist) + 1 + 1;
   10471             :     char *pszNewHist =
   10472         145 :         static_cast<char *>(CPLMalloc(nNewHistSize * sizeof(char)));
   10473             : 
   10474         145 :     strcpy(pszNewHist, strtime);
   10475         145 :     strcat(pszNewHist, pszAddHist);
   10476             : 
   10477             :     // int disableHistory = FALSE;
   10478             :     // if( !disableHistory )
   10479             :     {
   10480         145 :         if (!EQUAL(pszOldHist, ""))
   10481           9 :             strcat(pszNewHist, "\n");
   10482         145 :         strcat(pszNewHist, pszOldHist);
   10483             :     }
   10484             : 
   10485         145 :     const int status = nc_put_att_text(fpImage, NC_GLOBAL, "history",
   10486             :                                        strlen(pszNewHist), pszNewHist);
   10487         145 :     NCDF_ERR(status);
   10488             : 
   10489         145 :     CPLFree(pszNewHist);
   10490         145 : }
   10491             : 
   10492        7291 : static CPLErr NCDFSafeStrcat(char **ppszDest, const char *pszSrc,
   10493             :                              size_t *nDestSize)
   10494             : {
   10495             :     /* Reallocate the data string until the content fits */
   10496        7291 :     while (*nDestSize < (strlen(*ppszDest) + strlen(pszSrc) + 1))
   10497             :     {
   10498         471 :         (*nDestSize) *= 2;
   10499         471 :         *ppszDest = static_cast<char *>(
   10500         471 :             CPLRealloc(reinterpret_cast<void *>(*ppszDest), *nDestSize));
   10501             : #ifdef NCDF_DEBUG
   10502             :         CPLDebug("GDAL_netCDF", "NCDFSafeStrcat() resized str from %ld to %ld",
   10503             :                  (*nDestSize) / 2, *nDestSize);
   10504             : #endif
   10505             :     }
   10506        6820 :     strcat(*ppszDest, pszSrc);
   10507             : 
   10508        6820 :     return CE_None;
   10509             : }
   10510             : 
   10511             : /* helper function for NCDFGetAttr() */
   10512             : /* if pdfValue != nullptr, sets *pdfValue to first value returned */
   10513             : /* if ppszValue != nullptr, sets *ppszValue with all attribute values */
   10514             : /* *ppszValue is the responsibility of the caller and must be freed */
   10515       78006 : static CPLErr NCDFGetAttr1(int nCdfId, int nVarId, const char *pszAttrName,
   10516             :                            double *pdfValue, char **ppszValue)
   10517             : {
   10518       78006 :     nc_type nAttrType = NC_NAT;
   10519       78006 :     size_t nAttrLen = 0;
   10520             : 
   10521       78006 :     if (ppszValue)
   10522       76669 :         *ppszValue = nullptr;
   10523             : 
   10524       78006 :     int status = nc_inq_att(nCdfId, nVarId, pszAttrName, &nAttrType, &nAttrLen);
   10525       78006 :     if (status != NC_NOERR)
   10526       41667 :         return CE_Failure;
   10527             : 
   10528             : #ifdef NCDF_DEBUG
   10529             :     CPLDebug("GDAL_netCDF", "NCDFGetAttr1(%s) len=%ld type=%d", pszAttrName,
   10530             :              nAttrLen, nAttrType);
   10531             : #endif
   10532       36339 :     if (nAttrLen == 0 && nAttrType != NC_CHAR)
   10533           1 :         return CE_Failure;
   10534             : 
   10535             :     /* Allocate guaranteed minimum size (use 10 or 20 if not a string) */
   10536       36338 :     size_t nAttrValueSize = nAttrLen + 1;
   10537       36338 :     if (nAttrType != NC_CHAR && nAttrValueSize < 10)
   10538        4051 :         nAttrValueSize = 10;
   10539       36338 :     if (nAttrType == NC_DOUBLE && nAttrValueSize < 20)
   10540        1945 :         nAttrValueSize = 20;
   10541       36338 :     if (nAttrType == NC_INT64 && nAttrValueSize < 20)
   10542          49 :         nAttrValueSize = 22;
   10543             :     char *pszAttrValue =
   10544       36338 :         static_cast<char *>(CPLCalloc(nAttrValueSize, sizeof(char)));
   10545       36338 :     *pszAttrValue = '\0';
   10546             : 
   10547       36338 :     if (nAttrLen > 1 && nAttrType != NC_CHAR)
   10548         674 :         NCDFSafeStrcat(&pszAttrValue, "{", &nAttrValueSize);
   10549             : 
   10550       36338 :     double dfValue = 0.0;
   10551       36338 :     size_t m = 0;
   10552             :     char szTemp[256];
   10553       36338 :     bool bSetDoubleFromStr = false;
   10554             : 
   10555       36338 :     switch (nAttrType)
   10556             :     {
   10557       32285 :         case NC_CHAR:
   10558       32285 :             CPL_IGNORE_RET_VAL(
   10559       32285 :                 nc_get_att_text(nCdfId, nVarId, pszAttrName, pszAttrValue));
   10560       32285 :             pszAttrValue[nAttrLen] = '\0';
   10561       32285 :             bSetDoubleFromStr = true;
   10562       32285 :             dfValue = 0.0;
   10563       32285 :             break;
   10564          94 :         case NC_BYTE:
   10565             :         {
   10566             :             signed char *pscTemp = static_cast<signed char *>(
   10567          94 :                 CPLCalloc(nAttrLen, sizeof(signed char)));
   10568          94 :             nc_get_att_schar(nCdfId, nVarId, pszAttrName, pscTemp);
   10569          94 :             dfValue = static_cast<double>(pscTemp[0]);
   10570          94 :             if (nAttrLen > 1)
   10571             :             {
   10572          24 :                 for (m = 0; m < nAttrLen - 1; m++)
   10573             :                 {
   10574          13 :                     snprintf(szTemp, sizeof(szTemp), "%d,", pscTemp[m]);
   10575          13 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10576             :                 }
   10577             :             }
   10578          94 :             snprintf(szTemp, sizeof(szTemp), "%d", pscTemp[m]);
   10579          94 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10580          94 :             CPLFree(pscTemp);
   10581          94 :             break;
   10582             :         }
   10583         562 :         case NC_SHORT:
   10584             :         {
   10585             :             short *psTemp =
   10586         562 :                 static_cast<short *>(CPLCalloc(nAttrLen, sizeof(short)));
   10587         562 :             nc_get_att_short(nCdfId, nVarId, pszAttrName, psTemp);
   10588         562 :             dfValue = static_cast<double>(psTemp[0]);
   10589         562 :             if (nAttrLen > 1)
   10590             :             {
   10591         910 :                 for (m = 0; m < nAttrLen - 1; m++)
   10592             :                 {
   10593         455 :                     snprintf(szTemp, sizeof(szTemp), "%d,", psTemp[m]);
   10594         455 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10595             :                 }
   10596             :             }
   10597         562 :             snprintf(szTemp, sizeof(szTemp), "%d", psTemp[m]);
   10598         562 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10599         562 :             CPLFree(psTemp);
   10600         562 :             break;
   10601             :         }
   10602         617 :         case NC_INT:
   10603             :         {
   10604         617 :             int *pnTemp = static_cast<int *>(CPLCalloc(nAttrLen, sizeof(int)));
   10605         617 :             nc_get_att_int(nCdfId, nVarId, pszAttrName, pnTemp);
   10606         617 :             dfValue = static_cast<double>(pnTemp[0]);
   10607         617 :             if (nAttrLen > 1)
   10608             :             {
   10609         218 :                 for (m = 0; m < nAttrLen - 1; m++)
   10610             :                 {
   10611         139 :                     snprintf(szTemp, sizeof(szTemp), "%d,", pnTemp[m]);
   10612         139 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10613             :                 }
   10614             :             }
   10615         617 :             snprintf(szTemp, sizeof(szTemp), "%d", pnTemp[m]);
   10616         617 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10617         617 :             CPLFree(pnTemp);
   10618         617 :             break;
   10619             :         }
   10620         404 :         case NC_FLOAT:
   10621             :         {
   10622             :             float *pfTemp =
   10623         404 :                 static_cast<float *>(CPLCalloc(nAttrLen, sizeof(float)));
   10624         404 :             nc_get_att_float(nCdfId, nVarId, pszAttrName, pfTemp);
   10625         404 :             dfValue = static_cast<double>(pfTemp[0]);
   10626         404 :             if (nAttrLen > 1)
   10627             :             {
   10628          60 :                 for (m = 0; m < nAttrLen - 1; m++)
   10629             :                 {
   10630          30 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%.8g,", pfTemp[m]);
   10631          30 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10632             :                 }
   10633             :             }
   10634         404 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.8g", pfTemp[m]);
   10635         404 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10636         404 :             CPLFree(pfTemp);
   10637         404 :             break;
   10638             :         }
   10639        1945 :         case NC_DOUBLE:
   10640             :         {
   10641             :             double *pdfTemp =
   10642        1945 :                 static_cast<double *>(CPLCalloc(nAttrLen, sizeof(double)));
   10643        1945 :             nc_get_att_double(nCdfId, nVarId, pszAttrName, pdfTemp);
   10644        1945 :             dfValue = pdfTemp[0];
   10645        1945 :             if (nAttrLen > 1)
   10646             :             {
   10647         168 :                 for (m = 0; m < nAttrLen - 1; m++)
   10648             :                 {
   10649          91 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%.16g,", pdfTemp[m]);
   10650          91 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10651             :                 }
   10652             :             }
   10653        1945 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.16g", pdfTemp[m]);
   10654        1945 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10655        1945 :             CPLFree(pdfTemp);
   10656        1945 :             break;
   10657             :         }
   10658         264 :         case NC_STRING:
   10659             :         {
   10660             :             char **ppszTemp =
   10661         264 :                 static_cast<char **>(CPLCalloc(nAttrLen, sizeof(char *)));
   10662         264 :             nc_get_att_string(nCdfId, nVarId, pszAttrName, ppszTemp);
   10663         264 :             bSetDoubleFromStr = true;
   10664         264 :             dfValue = 0.0;
   10665         264 :             if (nAttrLen > 1)
   10666             :             {
   10667          19 :                 for (m = 0; m < nAttrLen - 1; m++)
   10668             :                 {
   10669          12 :                     NCDFSafeStrcat(&pszAttrValue,
   10670          12 :                                    ppszTemp[m] ? ppszTemp[m] : "{NULL}",
   10671             :                                    &nAttrValueSize);
   10672          12 :                     NCDFSafeStrcat(&pszAttrValue, ",", &nAttrValueSize);
   10673             :                 }
   10674             :             }
   10675         264 :             NCDFSafeStrcat(&pszAttrValue, ppszTemp[m] ? ppszTemp[m] : "{NULL}",
   10676             :                            &nAttrValueSize);
   10677         264 :             nc_free_string(nAttrLen, ppszTemp);
   10678         264 :             CPLFree(ppszTemp);
   10679         264 :             break;
   10680             :         }
   10681          23 :         case NC_UBYTE:
   10682             :         {
   10683             :             unsigned char *pucTemp = static_cast<unsigned char *>(
   10684          23 :                 CPLCalloc(nAttrLen, sizeof(unsigned char)));
   10685          23 :             nc_get_att_uchar(nCdfId, nVarId, pszAttrName, pucTemp);
   10686          23 :             dfValue = static_cast<double>(pucTemp[0]);
   10687          23 :             if (nAttrLen > 1)
   10688             :             {
   10689           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10690             :                 {
   10691           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pucTemp[m]);
   10692           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10693             :                 }
   10694             :             }
   10695          23 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pucTemp[m]);
   10696          23 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10697          23 :             CPLFree(pucTemp);
   10698          23 :             break;
   10699             :         }
   10700          26 :         case NC_USHORT:
   10701             :         {
   10702             :             unsigned short *pusTemp = static_cast<unsigned short *>(
   10703          26 :                 CPLCalloc(nAttrLen, sizeof(unsigned short)));
   10704          26 :             nc_get_att_ushort(nCdfId, nVarId, pszAttrName, pusTemp);
   10705          26 :             dfValue = static_cast<double>(pusTemp[0]);
   10706          26 :             if (nAttrLen > 1)
   10707             :             {
   10708          10 :                 for (m = 0; m < nAttrLen - 1; m++)
   10709             :                 {
   10710           5 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pusTemp[m]);
   10711           5 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10712             :                 }
   10713             :             }
   10714          26 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pusTemp[m]);
   10715          26 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10716          26 :             CPLFree(pusTemp);
   10717          26 :             break;
   10718             :         }
   10719          21 :         case NC_UINT:
   10720             :         {
   10721             :             unsigned int *punTemp =
   10722          21 :                 static_cast<unsigned int *>(CPLCalloc(nAttrLen, sizeof(int)));
   10723          21 :             nc_get_att_uint(nCdfId, nVarId, pszAttrName, punTemp);
   10724          21 :             dfValue = static_cast<double>(punTemp[0]);
   10725          21 :             if (nAttrLen > 1)
   10726             :             {
   10727           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10728             :                 {
   10729           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), "%u,", punTemp[m]);
   10730           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10731             :                 }
   10732             :             }
   10733          21 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", punTemp[m]);
   10734          21 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10735          21 :             CPLFree(punTemp);
   10736          21 :             break;
   10737             :         }
   10738          49 :         case NC_INT64:
   10739             :         {
   10740             :             GIntBig *panTemp =
   10741          49 :                 static_cast<GIntBig *>(CPLCalloc(nAttrLen, sizeof(GIntBig)));
   10742          49 :             nc_get_att_longlong(nCdfId, nVarId, pszAttrName, panTemp);
   10743          49 :             dfValue = static_cast<double>(panTemp[0]);
   10744          49 :             if (nAttrLen > 1)
   10745             :             {
   10746           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10747             :                 {
   10748           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB ",",
   10749           0 :                                 panTemp[m]);
   10750           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10751             :                 }
   10752             :             }
   10753          49 :             CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB, panTemp[m]);
   10754          49 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10755          49 :             CPLFree(panTemp);
   10756          49 :             break;
   10757             :         }
   10758          22 :         case NC_UINT64:
   10759             :         {
   10760             :             GUIntBig *panTemp =
   10761          22 :                 static_cast<GUIntBig *>(CPLCalloc(nAttrLen, sizeof(GUIntBig)));
   10762          22 :             nc_get_att_ulonglong(nCdfId, nVarId, pszAttrName, panTemp);
   10763          22 :             dfValue = static_cast<double>(panTemp[0]);
   10764          22 :             if (nAttrLen > 1)
   10765             :             {
   10766           0 :                 for (m = 0; m < nAttrLen - 1; m++)
   10767             :                 {
   10768           0 :                     CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB ",",
   10769           0 :                                 panTemp[m]);
   10770           0 :                     NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10771             :                 }
   10772             :             }
   10773          22 :             CPLsnprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB, panTemp[m]);
   10774          22 :             NCDFSafeStrcat(&pszAttrValue, szTemp, &nAttrValueSize);
   10775          22 :             CPLFree(panTemp);
   10776          22 :             break;
   10777             :         }
   10778          26 :         default:
   10779          26 :             CPLDebug("GDAL_netCDF",
   10780             :                      "NCDFGetAttr unsupported type %d for attribute %s",
   10781             :                      nAttrType, pszAttrName);
   10782          26 :             break;
   10783             :     }
   10784             : 
   10785       36338 :     if (nAttrLen > 1 && nAttrType != NC_CHAR)
   10786         674 :         NCDFSafeStrcat(&pszAttrValue, "}", &nAttrValueSize);
   10787             : 
   10788       36338 :     if (bSetDoubleFromStr)
   10789             :     {
   10790       32549 :         if (CPLGetValueType(pszAttrValue) == CPL_VALUE_STRING)
   10791             :         {
   10792       32361 :             if (ppszValue == nullptr && pdfValue != nullptr)
   10793             :             {
   10794           1 :                 CPLFree(pszAttrValue);
   10795           1 :                 return CE_Failure;
   10796             :             }
   10797             :         }
   10798       32548 :         dfValue = CPLAtof(pszAttrValue);
   10799             :     }
   10800             : 
   10801             :     /* set return values */
   10802       36337 :     if (ppszValue)
   10803       36023 :         *ppszValue = pszAttrValue;
   10804             :     else
   10805         314 :         CPLFree(pszAttrValue);
   10806             : 
   10807       36337 :     if (pdfValue)
   10808         314 :         *pdfValue = dfValue;
   10809             : 
   10810       36337 :     return CE_None;
   10811             : }
   10812             : 
   10813             : /* sets pdfValue to first value found */
   10814        1337 : CPLErr NCDFGetAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10815             :                    double *pdfValue)
   10816             : {
   10817        1337 :     return NCDFGetAttr1(nCdfId, nVarId, pszAttrName, pdfValue, nullptr);
   10818             : }
   10819             : 
   10820             : /* pszValue is the responsibility of the caller and must be freed */
   10821       76669 : CPLErr NCDFGetAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10822             :                    char **pszValue)
   10823             : {
   10824       76669 :     return NCDFGetAttr1(nCdfId, nVarId, pszAttrName, nullptr, pszValue);
   10825             : }
   10826             : 
   10827        3081 : CPLErr NCDFGetAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10828             :                    std::string &osValue)
   10829             : {
   10830        3081 :     nc_type nAttrType = NC_NAT;
   10831        3081 :     size_t nAttrLen = 0;
   10832             : 
   10833        3081 :     int status = nc_inq_att(nCdfId, nVarId, pszAttrName, &nAttrType, &nAttrLen);
   10834        3081 :     if (status != NC_NOERR)
   10835        1350 :         return CE_Failure;
   10836             : 
   10837        1731 :     if (nAttrType != NC_CHAR)
   10838           2 :         return CE_Failure;
   10839             : 
   10840             :     try
   10841             :     {
   10842        1729 :         osValue.resize(nAttrLen, 0);
   10843             :     }
   10844           0 :     catch (const std::exception &)
   10845             :     {
   10846           0 :         return CE_Failure;
   10847             :     }
   10848             : 
   10849        1729 :     const auto nErr = nc_get_att_text(nCdfId, nVarId, pszAttrName,
   10850        1729 :                                       osValue.data()) != NC_NOERR;
   10851        1729 :     NCDF_ERR_RET(nErr);
   10852             : 
   10853        1729 :     return CE_None;
   10854             : }
   10855             : 
   10856             : /* By default write NC_CHAR, but detect for int/float/double and */
   10857             : /* NC4 string arrays */
   10858         184 : static CPLErr NCDFPutAttr(int nCdfId, int nVarId, const char *pszAttrName,
   10859             :                           const char *pszValue)
   10860             : {
   10861         184 :     int status = 0;
   10862         184 :     char *pszTemp = nullptr;
   10863             : 
   10864             :     /* get the attribute values as tokens */
   10865         368 :     CPLStringList aosValues = NCDFTokenizeArray(pszValue);
   10866         184 :     if (aosValues.empty())
   10867           0 :         return CE_Failure;
   10868             : 
   10869         184 :     size_t nAttrLen = aosValues.size();
   10870             : 
   10871             :     /* first detect type */
   10872         184 :     nc_type nAttrType = NC_CHAR;
   10873         184 :     nc_type nTmpAttrType = NC_CHAR;
   10874         381 :     for (size_t i = 0; i < nAttrLen; i++)
   10875             :     {
   10876         197 :         nTmpAttrType = NC_CHAR;
   10877         197 :         bool bFoundType = false;
   10878         197 :         errno = 0;
   10879         197 :         int nValue = static_cast<int>(strtol(aosValues[i], &pszTemp, 10));
   10880             :         /* test for int */
   10881             :         /* TODO test for Byte and short - can this be done safely? */
   10882         197 :         if (errno == 0 && aosValues[i] != pszTemp && *pszTemp == 0)
   10883             :         {
   10884             :             char szTemp[256];
   10885          35 :             CPLsnprintf(szTemp, sizeof(szTemp), "%d", nValue);
   10886          35 :             if (EQUAL(szTemp, aosValues[i]))
   10887             :             {
   10888          35 :                 bFoundType = true;
   10889          35 :                 nTmpAttrType = NC_INT;
   10890             :             }
   10891             :             else
   10892             :             {
   10893             :                 unsigned int unValue = static_cast<unsigned int>(
   10894           0 :                     strtoul(aosValues[i], &pszTemp, 10));
   10895           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u", unValue);
   10896           0 :                 if (EQUAL(szTemp, aosValues[i]))
   10897             :                 {
   10898           0 :                     bFoundType = true;
   10899           0 :                     nTmpAttrType = NC_UINT;
   10900             :                 }
   10901             :             }
   10902             :         }
   10903         197 :         if (!bFoundType)
   10904             :         {
   10905             :             /* test for double */
   10906         162 :             errno = 0;
   10907         162 :             double dfValue = CPLStrtod(aosValues[i], &pszTemp);
   10908         162 :             if ((errno == 0) && (aosValues[i] != pszTemp) && (*pszTemp == 0))
   10909             :             {
   10910             :                 // Test for float instead of double.
   10911             :                 // strtof() is C89, which is not available in MSVC.
   10912             :                 // See if we lose precision if we cast to float and write to
   10913             :                 // char*.
   10914          18 :                 float fValue = float(dfValue);
   10915             :                 char szTemp[256];
   10916          18 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%.8g", fValue);
   10917          18 :                 if (EQUAL(szTemp, aosValues[i]))
   10918          11 :                     nTmpAttrType = NC_FLOAT;
   10919             :                 else
   10920           7 :                     nTmpAttrType = NC_DOUBLE;
   10921             :             }
   10922             :         }
   10923         197 :         if ((nTmpAttrType <= NC_DOUBLE && nAttrType <= NC_DOUBLE &&
   10924         157 :              nTmpAttrType > nAttrType) ||
   10925         157 :             (nTmpAttrType == NC_UINT && nAttrType < NC_FLOAT) ||
   10926           5 :             (nTmpAttrType >= NC_FLOAT && nAttrType == NC_UINT))
   10927          40 :             nAttrType = nTmpAttrType;
   10928             :     }
   10929             : 
   10930             : #ifdef DEBUG
   10931         184 :     if (EQUAL(pszAttrName, "DEBUG_EMPTY_DOUBLE_ATTR"))
   10932             :     {
   10933           0 :         nAttrType = NC_DOUBLE;
   10934           0 :         nAttrLen = 0;
   10935             :     }
   10936             : #endif
   10937             : 
   10938             :     /* now write the data */
   10939         184 :     if (nAttrType == NC_CHAR)
   10940             :     {
   10941         144 :         int nTmpFormat = 0;
   10942         144 :         if (nAttrLen > 1)
   10943             :         {
   10944           0 :             status = nc_inq_format(nCdfId, &nTmpFormat);
   10945           0 :             NCDF_ERR(status);
   10946             :         }
   10947         144 :         if (nAttrLen > 1 && nTmpFormat == NCDF_FORMAT_NC4)
   10948           0 :             status =
   10949           0 :                 nc_put_att_string(nCdfId, nVarId, pszAttrName, nAttrLen,
   10950           0 :                                   const_cast<const char **>(aosValues.List()));
   10951             :         else
   10952         144 :             status = nc_put_att_text(nCdfId, nVarId, pszAttrName,
   10953             :                                      strlen(pszValue), pszValue);
   10954         144 :         NCDF_ERR(status);
   10955             :     }
   10956             :     else
   10957             :     {
   10958          40 :         switch (nAttrType)
   10959             :         {
   10960          27 :             case NC_INT:
   10961             :             {
   10962             :                 int *pnTemp =
   10963          27 :                     static_cast<int *>(CPLCalloc(nAttrLen, sizeof(int)));
   10964          62 :                 for (size_t i = 0; i < nAttrLen; i++)
   10965             :                 {
   10966          35 :                     pnTemp[i] =
   10967          35 :                         static_cast<int>(strtol(aosValues[i], &pszTemp, 10));
   10968             :                 }
   10969          27 :                 status = nc_put_att_int(nCdfId, nVarId, pszAttrName, NC_INT,
   10970             :                                         nAttrLen, pnTemp);
   10971          27 :                 NCDF_ERR(status);
   10972          27 :                 CPLFree(pnTemp);
   10973          27 :                 break;
   10974             :             }
   10975           0 :             case NC_UINT:
   10976             :             {
   10977             :                 unsigned int *punTemp = static_cast<unsigned int *>(
   10978           0 :                     CPLCalloc(nAttrLen, sizeof(unsigned int)));
   10979           0 :                 for (size_t i = 0; i < nAttrLen; i++)
   10980             :                 {
   10981           0 :                     punTemp[i] = static_cast<unsigned int>(
   10982           0 :                         strtol(aosValues[i], &pszTemp, 10));
   10983             :                 }
   10984           0 :                 status = nc_put_att_uint(nCdfId, nVarId, pszAttrName, NC_UINT,
   10985             :                                          nAttrLen, punTemp);
   10986           0 :                 NCDF_ERR(status);
   10987           0 :                 CPLFree(punTemp);
   10988           0 :                 break;
   10989             :             }
   10990           9 :             case NC_FLOAT:
   10991             :             {
   10992             :                 float *pfTemp =
   10993           9 :                     static_cast<float *>(CPLCalloc(nAttrLen, sizeof(float)));
   10994          20 :                 for (size_t i = 0; i < nAttrLen; i++)
   10995             :                 {
   10996          11 :                     pfTemp[i] =
   10997          11 :                         static_cast<float>(CPLStrtod(aosValues[i], &pszTemp));
   10998             :                 }
   10999           9 :                 status = nc_put_att_float(nCdfId, nVarId, pszAttrName, NC_FLOAT,
   11000             :                                           nAttrLen, pfTemp);
   11001           9 :                 NCDF_ERR(status);
   11002           9 :                 CPLFree(pfTemp);
   11003           9 :                 break;
   11004             :             }
   11005           4 :             case NC_DOUBLE:
   11006             :             {
   11007             :                 double *pdfTemp =
   11008           4 :                     static_cast<double *>(CPLCalloc(nAttrLen, sizeof(double)));
   11009          11 :                 for (size_t i = 0; i < nAttrLen; i++)
   11010             :                 {
   11011           7 :                     pdfTemp[i] = CPLStrtod(aosValues[i], &pszTemp);
   11012             :                 }
   11013           4 :                 status = nc_put_att_double(nCdfId, nVarId, pszAttrName,
   11014             :                                            NC_DOUBLE, nAttrLen, pdfTemp);
   11015           4 :                 NCDF_ERR(status);
   11016           4 :                 CPLFree(pdfTemp);
   11017           4 :                 break;
   11018             :             }
   11019           0 :             default:
   11020           0 :                 return CE_Failure;
   11021             :         }
   11022             :     }
   11023             : 
   11024         184 :     return CE_None;
   11025             : }
   11026             : 
   11027          82 : static CPLErr NCDFGet1DVar(int nCdfId, int nVarId, char **pszValue)
   11028             : {
   11029             :     /* get var information */
   11030          82 :     int nVarDimId = -1;
   11031          82 :     int status = nc_inq_varndims(nCdfId, nVarId, &nVarDimId);
   11032          82 :     if (status != NC_NOERR || nVarDimId != 1)
   11033           0 :         return CE_Failure;
   11034             : 
   11035          82 :     status = nc_inq_vardimid(nCdfId, nVarId, &nVarDimId);
   11036          82 :     if (status != NC_NOERR)
   11037           0 :         return CE_Failure;
   11038             : 
   11039          82 :     nc_type nVarType = NC_NAT;
   11040          82 :     status = nc_inq_vartype(nCdfId, nVarId, &nVarType);
   11041          82 :     if (status != NC_NOERR)
   11042           0 :         return CE_Failure;
   11043             : 
   11044          82 :     size_t nVarLen = 0;
   11045          82 :     status = nc_inq_dimlen(nCdfId, nVarDimId, &nVarLen);
   11046          82 :     if (status != NC_NOERR)
   11047           0 :         return CE_Failure;
   11048             : 
   11049          82 :     size_t start[1] = {0};
   11050          82 :     size_t count[1] = {nVarLen};
   11051             : 
   11052             :     /* Allocate guaranteed minimum size */
   11053          82 :     size_t nVarValueSize = NCDF_MAX_STR_LEN;
   11054             :     char *pszVarValue =
   11055          82 :         static_cast<char *>(CPLCalloc(nVarValueSize, sizeof(char)));
   11056          82 :     *pszVarValue = '\0';
   11057             : 
   11058          82 :     if (nVarLen == 0)
   11059             :     {
   11060             :         /* set return values */
   11061           1 :         *pszValue = pszVarValue;
   11062             : 
   11063           1 :         return CE_None;
   11064             :     }
   11065             : 
   11066          81 :     if (nVarLen > 1 && nVarType != NC_CHAR)
   11067          43 :         NCDFSafeStrcat(&pszVarValue, "{", &nVarValueSize);
   11068             : 
   11069          81 :     switch (nVarType)
   11070             :     {
   11071           0 :         case NC_CHAR:
   11072           0 :             nc_get_vara_text(nCdfId, nVarId, start, count, pszVarValue);
   11073           0 :             pszVarValue[nVarLen] = '\0';
   11074           0 :             break;
   11075           0 :         case NC_BYTE:
   11076             :         {
   11077             :             signed char *pscTemp = static_cast<signed char *>(
   11078           0 :                 CPLCalloc(nVarLen, sizeof(signed char)));
   11079           0 :             nc_get_vara_schar(nCdfId, nVarId, start, count, pscTemp);
   11080             :             char szTemp[256];
   11081           0 :             size_t m = 0;
   11082           0 :             for (; m < nVarLen - 1; m++)
   11083             :             {
   11084           0 :                 snprintf(szTemp, sizeof(szTemp), "%d,", pscTemp[m]);
   11085           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11086             :             }
   11087           0 :             snprintf(szTemp, sizeof(szTemp), "%d", pscTemp[m]);
   11088           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11089           0 :             CPLFree(pscTemp);
   11090           0 :             break;
   11091             :         }
   11092           0 :         case NC_SHORT:
   11093             :         {
   11094             :             short *psTemp =
   11095           0 :                 static_cast<short *>(CPLCalloc(nVarLen, sizeof(short)));
   11096           0 :             nc_get_vara_short(nCdfId, nVarId, start, count, psTemp);
   11097             :             char szTemp[256];
   11098           0 :             size_t m = 0;
   11099           0 :             for (; m < nVarLen - 1; m++)
   11100             :             {
   11101           0 :                 snprintf(szTemp, sizeof(szTemp), "%d,", psTemp[m]);
   11102           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11103             :             }
   11104           0 :             snprintf(szTemp, sizeof(szTemp), "%d", psTemp[m]);
   11105           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11106           0 :             CPLFree(psTemp);
   11107           0 :             break;
   11108             :         }
   11109          22 :         case NC_INT:
   11110             :         {
   11111          22 :             int *pnTemp = static_cast<int *>(CPLCalloc(nVarLen, sizeof(int)));
   11112          22 :             nc_get_vara_int(nCdfId, nVarId, start, count, pnTemp);
   11113             :             char szTemp[256];
   11114          22 :             size_t m = 0;
   11115          47 :             for (; m < nVarLen - 1; m++)
   11116             :             {
   11117          25 :                 snprintf(szTemp, sizeof(szTemp), "%d,", pnTemp[m]);
   11118          25 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11119             :             }
   11120          22 :             snprintf(szTemp, sizeof(szTemp), "%d", pnTemp[m]);
   11121          22 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11122          22 :             CPLFree(pnTemp);
   11123          22 :             break;
   11124             :         }
   11125           8 :         case NC_FLOAT:
   11126             :         {
   11127             :             float *pfTemp =
   11128           8 :                 static_cast<float *>(CPLCalloc(nVarLen, sizeof(float)));
   11129           8 :             nc_get_vara_float(nCdfId, nVarId, start, count, pfTemp);
   11130             :             char szTemp[256];
   11131           8 :             size_t m = 0;
   11132         325 :             for (; m < nVarLen - 1; m++)
   11133             :             {
   11134         317 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%.8g,", pfTemp[m]);
   11135         317 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11136             :             }
   11137           8 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.8g", pfTemp[m]);
   11138           8 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11139           8 :             CPLFree(pfTemp);
   11140           8 :             break;
   11141             :         }
   11142          48 :         case NC_DOUBLE:
   11143             :         {
   11144             :             double *pdfTemp =
   11145          48 :                 static_cast<double *>(CPLCalloc(nVarLen, sizeof(double)));
   11146          48 :             nc_get_vara_double(nCdfId, nVarId, start, count, pdfTemp);
   11147             :             char szTemp[256];
   11148          48 :             size_t m = 0;
   11149         226 :             for (; m < nVarLen - 1; m++)
   11150             :             {
   11151         178 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%.16g,", pdfTemp[m]);
   11152         178 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11153             :             }
   11154          48 :             CPLsnprintf(szTemp, sizeof(szTemp), "%.16g", pdfTemp[m]);
   11155          48 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11156          48 :             CPLFree(pdfTemp);
   11157          48 :             break;
   11158             :         }
   11159           0 :         case NC_STRING:
   11160             :         {
   11161             :             char **ppszTemp =
   11162           0 :                 static_cast<char **>(CPLCalloc(nVarLen, sizeof(char *)));
   11163           0 :             nc_get_vara_string(nCdfId, nVarId, start, count, ppszTemp);
   11164           0 :             size_t m = 0;
   11165           0 :             for (; m < nVarLen - 1; m++)
   11166             :             {
   11167           0 :                 NCDFSafeStrcat(&pszVarValue, ppszTemp[m], &nVarValueSize);
   11168           0 :                 NCDFSafeStrcat(&pszVarValue, ",", &nVarValueSize);
   11169             :             }
   11170           0 :             NCDFSafeStrcat(&pszVarValue, ppszTemp[m], &nVarValueSize);
   11171           0 :             nc_free_string(nVarLen, ppszTemp);
   11172           0 :             CPLFree(ppszTemp);
   11173           0 :             break;
   11174             :         }
   11175           0 :         case NC_UBYTE:
   11176             :         {
   11177             :             unsigned char *pucTemp = static_cast<unsigned char *>(
   11178           0 :                 CPLCalloc(nVarLen, sizeof(unsigned char)));
   11179           0 :             nc_get_vara_uchar(nCdfId, nVarId, start, count, pucTemp);
   11180             :             char szTemp[256];
   11181           0 :             size_t m = 0;
   11182           0 :             for (; m < nVarLen - 1; m++)
   11183             :             {
   11184           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pucTemp[m]);
   11185           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11186             :             }
   11187           0 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pucTemp[m]);
   11188           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11189           0 :             CPLFree(pucTemp);
   11190           0 :             break;
   11191             :         }
   11192           0 :         case NC_USHORT:
   11193             :         {
   11194             :             unsigned short *pusTemp = static_cast<unsigned short *>(
   11195           0 :                 CPLCalloc(nVarLen, sizeof(unsigned short)));
   11196           0 :             nc_get_vara_ushort(nCdfId, nVarId, start, count, pusTemp);
   11197             :             char szTemp[256];
   11198           0 :             size_t m = 0;
   11199           0 :             for (; m < nVarLen - 1; m++)
   11200             :             {
   11201           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u,", pusTemp[m]);
   11202           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11203             :             }
   11204           0 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", pusTemp[m]);
   11205           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11206           0 :             CPLFree(pusTemp);
   11207           0 :             break;
   11208             :         }
   11209           0 :         case NC_UINT:
   11210             :         {
   11211             :             unsigned int *punTemp = static_cast<unsigned int *>(
   11212           0 :                 CPLCalloc(nVarLen, sizeof(unsigned int)));
   11213           0 :             nc_get_vara_uint(nCdfId, nVarId, start, count, punTemp);
   11214             :             char szTemp[256];
   11215           0 :             size_t m = 0;
   11216           0 :             for (; m < nVarLen - 1; m++)
   11217             :             {
   11218           0 :                 CPLsnprintf(szTemp, sizeof(szTemp), "%u,", punTemp[m]);
   11219           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11220             :             }
   11221           0 :             CPLsnprintf(szTemp, sizeof(szTemp), "%u", punTemp[m]);
   11222           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11223           0 :             CPLFree(punTemp);
   11224           0 :             break;
   11225             :         }
   11226           3 :         case NC_INT64:
   11227             :         {
   11228             :             long long *pnTemp =
   11229           3 :                 static_cast<long long *>(CPLCalloc(nVarLen, sizeof(long long)));
   11230           3 :             nc_get_vara_longlong(nCdfId, nVarId, start, count, pnTemp);
   11231             :             char szTemp[256];
   11232           3 :             size_t m = 0;
   11233           4 :             for (; m < nVarLen - 1; m++)
   11234             :             {
   11235           1 :                 snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB ",", pnTemp[m]);
   11236           1 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11237             :             }
   11238           3 :             snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GIB, pnTemp[m]);
   11239           3 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11240           3 :             CPLFree(pnTemp);
   11241           3 :             break;
   11242             :         }
   11243           0 :         case NC_UINT64:
   11244             :         {
   11245             :             unsigned long long *pnTemp = static_cast<unsigned long long *>(
   11246           0 :                 CPLCalloc(nVarLen, sizeof(unsigned long long)));
   11247           0 :             nc_get_vara_ulonglong(nCdfId, nVarId, start, count, pnTemp);
   11248             :             char szTemp[256];
   11249           0 :             size_t m = 0;
   11250           0 :             for (; m < nVarLen - 1; m++)
   11251             :             {
   11252           0 :                 snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB ",", pnTemp[m]);
   11253           0 :                 NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11254             :             }
   11255           0 :             snprintf(szTemp, sizeof(szTemp), CPL_FRMT_GUIB, pnTemp[m]);
   11256           0 :             NCDFSafeStrcat(&pszVarValue, szTemp, &nVarValueSize);
   11257           0 :             CPLFree(pnTemp);
   11258           0 :             break;
   11259             :         }
   11260           0 :         default:
   11261           0 :             CPLDebug("GDAL_netCDF", "NCDFGetVar1D unsupported type %d",
   11262             :                      nVarType);
   11263           0 :             CPLFree(pszVarValue);
   11264           0 :             pszVarValue = nullptr;
   11265           0 :             break;
   11266             :     }
   11267             : 
   11268          81 :     if (pszVarValue != nullptr && nVarLen > 1 && nVarType != NC_CHAR)
   11269          43 :         NCDFSafeStrcat(&pszVarValue, "}", &nVarValueSize);
   11270             : 
   11271             :     /* set return values */
   11272          81 :     *pszValue = pszVarValue;
   11273             : 
   11274          81 :     return CE_None;
   11275             : }
   11276             : 
   11277           9 : static CPLErr NCDFPut1DVar(int nCdfId, int nVarId, const char *pszValue)
   11278             : {
   11279           9 :     if (EQUAL(pszValue, ""))
   11280           0 :         return CE_Failure;
   11281             : 
   11282             :     /* get var information */
   11283           9 :     int nVarDimId = -1;
   11284           9 :     int status = nc_inq_varndims(nCdfId, nVarId, &nVarDimId);
   11285           9 :     if (status != NC_NOERR || nVarDimId != 1)
   11286           0 :         return CE_Failure;
   11287             : 
   11288           9 :     status = nc_inq_vardimid(nCdfId, nVarId, &nVarDimId);
   11289           9 :     if (status != NC_NOERR)
   11290           0 :         return CE_Failure;
   11291             : 
   11292           9 :     nc_type nVarType = NC_CHAR;
   11293           9 :     status = nc_inq_vartype(nCdfId, nVarId, &nVarType);
   11294           9 :     if (status != NC_NOERR)
   11295           0 :         return CE_Failure;
   11296             : 
   11297           9 :     size_t nVarLen = 0;
   11298           9 :     status = nc_inq_dimlen(nCdfId, nVarDimId, &nVarLen);
   11299           9 :     if (status != NC_NOERR)
   11300           0 :         return CE_Failure;
   11301             : 
   11302           9 :     size_t start[1] = {0};
   11303           9 :     size_t count[1] = {nVarLen};
   11304             : 
   11305             :     /* get the values as tokens */
   11306          18 :     CPLStringList aosValues = NCDFTokenizeArray(pszValue);
   11307           9 :     if (aosValues.empty())
   11308           0 :         return CE_Failure;
   11309             : 
   11310           9 :     nVarLen = aosValues.size();
   11311             : 
   11312             :     /* now write the data */
   11313           9 :     if (nVarType == NC_CHAR)
   11314             :     {
   11315           0 :         status = nc_put_vara_text(nCdfId, nVarId, start, count, pszValue);
   11316           0 :         NCDF_ERR(status);
   11317             :     }
   11318             :     else
   11319             :     {
   11320           9 :         switch (nVarType)
   11321             :         {
   11322           0 :             case NC_BYTE:
   11323             :             {
   11324             :                 signed char *pscTemp = static_cast<signed char *>(
   11325           0 :                     CPLCalloc(nVarLen, sizeof(signed char)));
   11326           0 :                 for (size_t i = 0; i < nVarLen; i++)
   11327             :                 {
   11328           0 :                     char *pszTemp = nullptr;
   11329           0 :                     pscTemp[i] = static_cast<signed char>(
   11330           0 :                         strtol(aosValues[i], &pszTemp, 10));
   11331             :                 }
   11332             :                 status =
   11333           0 :                     nc_put_vara_schar(nCdfId, nVarId, start, count, pscTemp);
   11334           0 :                 NCDF_ERR(status);
   11335           0 :                 CPLFree(pscTemp);
   11336           0 :                 break;
   11337             :             }
   11338           0 :             case NC_SHORT:
   11339             :             {
   11340             :                 short *psTemp =
   11341           0 :                     static_cast<short *>(CPLCalloc(nVarLen, sizeof(short)));
   11342           0 :                 for (size_t i = 0; i < nVarLen; i++)
   11343             :                 {
   11344           0 :                     char *pszTemp = nullptr;
   11345           0 :                     psTemp[i] =
   11346           0 :                         static_cast<short>(strtol(aosValues[i], &pszTemp, 10));
   11347             :                 }
   11348             :                 status =
   11349           0 :                     nc_put_vara_short(nCdfId, nVarId, start, count, psTemp);
   11350           0 :                 NCDF_ERR(status);
   11351           0 :                 CPLFree(psTemp);
   11352           0 :                 break;
   11353             :             }
   11354           3 :             case NC_INT:
   11355             :             {
   11356             :                 int *pnTemp =
   11357           3 :                     static_cast<int *>(CPLCalloc(nVarLen, sizeof(int)));
   11358          11 :                 for (size_t i = 0; i < nVarLen; i++)
   11359             :                 {
   11360           8 :                     char *pszTemp = nullptr;
   11361           8 :                     pnTemp[i] =
   11362           8 :                         static_cast<int>(strtol(aosValues[i], &pszTemp, 10));
   11363             :                 }
   11364           3 :                 status = nc_put_vara_int(nCdfId, nVarId, start, count, pnTemp);
   11365           3 :                 NCDF_ERR(status);
   11366           3 :                 CPLFree(pnTemp);
   11367           3 :                 break;
   11368             :             }
   11369           0 :             case NC_FLOAT:
   11370             :             {
   11371             :                 float *pfTemp =
   11372           0 :                     static_cast<float *>(CPLCalloc(nVarLen, sizeof(float)));
   11373           0 :                 for (size_t i = 0; i < nVarLen; i++)
   11374             :                 {
   11375           0 :                     char *pszTemp = nullptr;
   11376           0 :                     pfTemp[i] =
   11377           0 :                         static_cast<float>(CPLStrtod(aosValues[i], &pszTemp));
   11378             :                 }
   11379             :                 status =
   11380           0 :                     nc_put_vara_float(nCdfId, nVarId, start, count, pfTemp);
   11381           0 :                 NCDF_ERR(status);
   11382           0 :                 CPLFree(pfTemp);
   11383           0 :                 break;
   11384             :             }
   11385           5 :             case NC_DOUBLE:
   11386             :             {
   11387             :                 double *pdfTemp =
   11388           5 :                     static_cast<double *>(CPLCalloc(nVarLen, sizeof(double)));
   11389          19 :                 for (size_t i = 0; i < nVarLen; i++)
   11390             :                 {
   11391          14 :                     char *pszTemp = nullptr;
   11392          14 :                     pdfTemp[i] = CPLStrtod(aosValues[i], &pszTemp);
   11393             :                 }
   11394             :                 status =
   11395           5 :                     nc_put_vara_double(nCdfId, nVarId, start, count, pdfTemp);
   11396           5 :                 NCDF_ERR(status);
   11397           5 :                 CPLFree(pdfTemp);
   11398           5 :                 break;
   11399             :             }
   11400           1 :             default:
   11401             :             {
   11402           1 :                 int nTmpFormat = 0;
   11403           1 :                 status = nc_inq_format(nCdfId, &nTmpFormat);
   11404           1 :                 NCDF_ERR(status);
   11405           1 :                 if (nTmpFormat == NCDF_FORMAT_NC4)
   11406             :                 {
   11407           1 :                     switch (nVarType)
   11408             :                     {
   11409           0 :                         case NC_STRING:
   11410             :                         {
   11411           0 :                             status = nc_put_vara_string(
   11412             :                                 nCdfId, nVarId, start, count,
   11413           0 :                                 const_cast<const char **>(aosValues.List()));
   11414           0 :                             NCDF_ERR(status);
   11415           0 :                             break;
   11416             :                         }
   11417           0 :                         case NC_UBYTE:
   11418             :                         {
   11419             :                             unsigned char *pucTemp =
   11420             :                                 static_cast<unsigned char *>(
   11421           0 :                                     CPLCalloc(nVarLen, sizeof(unsigned char)));
   11422           0 :                             for (size_t i = 0; i < nVarLen; i++)
   11423             :                             {
   11424           0 :                                 char *pszTemp = nullptr;
   11425           0 :                                 pucTemp[i] = static_cast<unsigned char>(
   11426           0 :                                     strtoul(aosValues[i], &pszTemp, 10));
   11427             :                             }
   11428           0 :                             status = nc_put_vara_uchar(nCdfId, nVarId, start,
   11429             :                                                        count, pucTemp);
   11430           0 :                             NCDF_ERR(status);
   11431           0 :                             CPLFree(pucTemp);
   11432           0 :                             break;
   11433             :                         }
   11434           0 :                         case NC_USHORT:
   11435             :                         {
   11436             :                             unsigned short *pusTemp =
   11437             :                                 static_cast<unsigned short *>(
   11438           0 :                                     CPLCalloc(nVarLen, sizeof(unsigned short)));
   11439           0 :                             for (size_t i = 0; i < nVarLen; i++)
   11440             :                             {
   11441           0 :                                 char *pszTemp = nullptr;
   11442           0 :                                 pusTemp[i] = static_cast<unsigned short>(
   11443           0 :                                     strtoul(aosValues[i], &pszTemp, 10));
   11444             :                             }
   11445           0 :                             status = nc_put_vara_ushort(nCdfId, nVarId, start,
   11446             :                                                         count, pusTemp);
   11447           0 :                             NCDF_ERR(status);
   11448           0 :                             CPLFree(pusTemp);
   11449           0 :                             break;
   11450             :                         }
   11451           0 :                         case NC_UINT:
   11452             :                         {
   11453             :                             unsigned int *punTemp = static_cast<unsigned int *>(
   11454           0 :                                 CPLCalloc(nVarLen, sizeof(unsigned int)));
   11455           0 :                             for (size_t i = 0; i < nVarLen; i++)
   11456             :                             {
   11457           0 :                                 char *pszTemp = nullptr;
   11458           0 :                                 punTemp[i] = static_cast<unsigned int>(
   11459           0 :                                     strtoul(aosValues[i], &pszTemp, 10));
   11460             :                             }
   11461           0 :                             status = nc_put_vara_uint(nCdfId, nVarId, start,
   11462             :                                                       count, punTemp);
   11463           0 :                             NCDF_ERR(status);
   11464           0 :                             CPLFree(punTemp);
   11465           0 :                             break;
   11466             :                         }
   11467           1 :                         default:
   11468           1 :                             return CE_Failure;
   11469             :                     }
   11470             :                 }
   11471           0 :                 break;
   11472             :             }
   11473             :         }
   11474             :     }
   11475             : 
   11476           8 :     return CE_None;
   11477             : }
   11478             : 
   11479             : /************************************************************************/
   11480             : /*                       GetDefaultNoDataValue()                        */
   11481             : /************************************************************************/
   11482             : 
   11483         155 : double NCDFGetDefaultNoDataValue(int nCdfId, int nVarId, int nVarType,
   11484             :                                  bool &bGotNoData)
   11485             : 
   11486             : {
   11487         155 :     int nNoFill = 0;
   11488         155 :     double dfNoData = 0.0;
   11489             : 
   11490         155 :     switch (nVarType)
   11491             :     {
   11492           0 :         case NC_CHAR:
   11493             :         case NC_BYTE:
   11494             :         case NC_UBYTE:
   11495             :             // Don't do default fill-values for bytes, too risky.
   11496             :             // This function should not be called in those cases.
   11497           0 :             CPLAssert(false);
   11498             :             break;
   11499          20 :         case NC_SHORT:
   11500             :         {
   11501          20 :             short nFillVal = 0;
   11502          20 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11503             :                 NC_NOERR)
   11504             :             {
   11505          20 :                 if (!nNoFill)
   11506             :                 {
   11507          19 :                     bGotNoData = true;
   11508          19 :                     dfNoData = nFillVal;
   11509             :                 }
   11510             :             }
   11511             :             else
   11512           0 :                 dfNoData = NC_FILL_SHORT;
   11513          20 :             break;
   11514             :         }
   11515          22 :         case NC_INT:
   11516             :         {
   11517          22 :             int nFillVal = 0;
   11518          22 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11519             :                 NC_NOERR)
   11520             :             {
   11521          22 :                 if (!nNoFill)
   11522             :                 {
   11523          21 :                     bGotNoData = true;
   11524          21 :                     dfNoData = nFillVal;
   11525             :                 }
   11526             :             }
   11527             :             else
   11528           0 :                 dfNoData = NC_FILL_INT;
   11529          22 :             break;
   11530             :         }
   11531          65 :         case NC_FLOAT:
   11532             :         {
   11533          65 :             float fFillVal = 0;
   11534          65 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &fFillVal) ==
   11535             :                 NC_NOERR)
   11536             :             {
   11537          65 :                 if (!nNoFill)
   11538             :                 {
   11539          61 :                     bGotNoData = true;
   11540          61 :                     dfNoData = fFillVal;
   11541             :                 }
   11542             :             }
   11543             :             else
   11544           0 :                 dfNoData = NC_FILL_FLOAT;
   11545          65 :             break;
   11546             :         }
   11547          31 :         case NC_DOUBLE:
   11548             :         {
   11549          31 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &dfNoData) ==
   11550             :                 NC_NOERR)
   11551             :             {
   11552          31 :                 if (!nNoFill)
   11553             :                 {
   11554          31 :                     bGotNoData = true;
   11555             :                 }
   11556             :             }
   11557             :             else
   11558           0 :                 dfNoData = NC_FILL_DOUBLE;
   11559          31 :             break;
   11560             :         }
   11561           3 :         case NC_USHORT:
   11562             :         {
   11563           3 :             unsigned short nFillVal = 0;
   11564           3 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11565             :                 NC_NOERR)
   11566             :             {
   11567           3 :                 if (!nNoFill)
   11568             :                 {
   11569           3 :                     bGotNoData = true;
   11570           3 :                     dfNoData = nFillVal;
   11571             :                 }
   11572             :             }
   11573             :             else
   11574           0 :                 dfNoData = NC_FILL_USHORT;
   11575           3 :             break;
   11576             :         }
   11577           3 :         case NC_UINT:
   11578             :         {
   11579           3 :             unsigned int nFillVal = 0;
   11580           3 :             if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) ==
   11581             :                 NC_NOERR)
   11582             :             {
   11583           3 :                 if (!nNoFill)
   11584             :                 {
   11585           3 :                     bGotNoData = true;
   11586           3 :                     dfNoData = nFillVal;
   11587             :                 }
   11588             :             }
   11589             :             else
   11590           0 :                 dfNoData = NC_FILL_UINT;
   11591           3 :             break;
   11592             :         }
   11593          11 :         default:
   11594          11 :             dfNoData = 0.0;
   11595          11 :             break;
   11596             :     }
   11597             : 
   11598         155 :     return dfNoData;
   11599             : }
   11600             : 
   11601             : /************************************************************************/
   11602             : /*                  NCDFGetDefaultNoDataValueAsInt64()                  */
   11603             : /************************************************************************/
   11604             : 
   11605           2 : int64_t NCDFGetDefaultNoDataValueAsInt64(int nCdfId, int nVarId,
   11606             :                                          bool &bGotNoData)
   11607             : 
   11608             : {
   11609           2 :     int nNoFill = 0;
   11610           2 :     long long nFillVal = 0;
   11611           2 :     if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) == NC_NOERR)
   11612             :     {
   11613           2 :         if (!nNoFill)
   11614             :         {
   11615           2 :             bGotNoData = true;
   11616           2 :             return static_cast<int64_t>(nFillVal);
   11617             :         }
   11618             :     }
   11619             :     else
   11620           0 :         return static_cast<int64_t>(NC_FILL_INT64);
   11621           0 :     return 0;
   11622             : }
   11623             : 
   11624             : /************************************************************************/
   11625             : /*                 NCDFGetDefaultNoDataValueAsUInt64()                  */
   11626             : /************************************************************************/
   11627             : 
   11628           1 : uint64_t NCDFGetDefaultNoDataValueAsUInt64(int nCdfId, int nVarId,
   11629             :                                            bool &bGotNoData)
   11630             : 
   11631             : {
   11632           1 :     int nNoFill = 0;
   11633           1 :     unsigned long long nFillVal = 0;
   11634           1 :     if (nc_inq_var_fill(nCdfId, nVarId, &nNoFill, &nFillVal) == NC_NOERR)
   11635             :     {
   11636           1 :         if (!nNoFill)
   11637             :         {
   11638           1 :             bGotNoData = true;
   11639           1 :             return static_cast<uint64_t>(nFillVal);
   11640             :         }
   11641             :     }
   11642             :     else
   11643           0 :         return static_cast<uint64_t>(NC_FILL_UINT64);
   11644           0 :     return 0;
   11645             : }
   11646             : 
   11647       13307 : static int NCDFDoesVarContainAttribVal(int nCdfId,
   11648             :                                        const char *const *papszAttribNames,
   11649             :                                        const char *const *papszAttribValues,
   11650             :                                        int nVarId, const char *pszVarName,
   11651             :                                        bool bStrict = true)
   11652             : {
   11653       13307 :     if (nVarId == -1 && pszVarName != nullptr)
   11654        9039 :         NCDFResolveVar(nCdfId, pszVarName, &nCdfId, &nVarId);
   11655             : 
   11656       13307 :     if (nVarId == -1)
   11657        1046 :         return -1;
   11658             : 
   11659       12261 :     bool bFound = false;
   11660       60614 :     for (int i = 0; !bFound && papszAttribNames != nullptr &&
   11661       57971 :                     papszAttribNames[i] != nullptr;
   11662             :          i++)
   11663             :     {
   11664       48353 :         char *pszTemp = nullptr;
   11665       48353 :         if (NCDFGetAttr(nCdfId, nVarId, papszAttribNames[i], &pszTemp) ==
   11666       69497 :                 CE_None &&
   11667       21144 :             pszTemp != nullptr)
   11668             :         {
   11669       21144 :             if (bStrict)
   11670             :             {
   11671       21144 :                 if (EQUAL(pszTemp, papszAttribValues[i]))
   11672        2643 :                     bFound = true;
   11673             :             }
   11674             :             else
   11675             :             {
   11676           0 :                 if (EQUALN(pszTemp, papszAttribValues[i],
   11677             :                            strlen(papszAttribValues[i])))
   11678           0 :                     bFound = true;
   11679             :             }
   11680       21144 :             CPLFree(pszTemp);
   11681             :         }
   11682             :     }
   11683       12261 :     return bFound;
   11684             : }
   11685             : 
   11686        2291 : static int NCDFDoesVarContainAttribVal2(int nCdfId, const char *papszAttribName,
   11687             :                                         const char *const *papszAttribValues,
   11688             :                                         int nVarId, const char *pszVarName,
   11689             :                                         int bStrict = true)
   11690             : {
   11691        2291 :     if (nVarId == -1 && pszVarName != nullptr)
   11692        1729 :         NCDFResolveVar(nCdfId, pszVarName, &nCdfId, &nVarId);
   11693             : 
   11694        2291 :     if (nVarId == -1)
   11695           0 :         return -1;
   11696             : 
   11697        2291 :     bool bFound = false;
   11698        2291 :     char *pszTemp = nullptr;
   11699        2698 :     if (NCDFGetAttr(nCdfId, nVarId, papszAttribName, &pszTemp) != CE_None ||
   11700         407 :         pszTemp == nullptr)
   11701        1884 :         return FALSE;
   11702             : 
   11703        8257 :     for (int i = 0; !bFound && i < CSLCount(papszAttribValues); i++)
   11704             :     {
   11705        7850 :         if (bStrict)
   11706             :         {
   11707        7820 :             if (EQUAL(pszTemp, papszAttribValues[i]))
   11708          31 :                 bFound = true;
   11709             :         }
   11710             :         else
   11711             :         {
   11712          30 :             if (EQUALN(pszTemp, papszAttribValues[i],
   11713             :                        strlen(papszAttribValues[i])))
   11714           2 :                 bFound = true;
   11715             :         }
   11716             :     }
   11717             : 
   11718         407 :     CPLFree(pszTemp);
   11719             : 
   11720         407 :     return bFound;
   11721             : }
   11722             : 
   11723        1044 : static bool NCDFEqual(const char *papszName, const char *const *papszValues)
   11724             : {
   11725        1044 :     if (papszName == nullptr || EQUAL(papszName, ""))
   11726           0 :         return false;
   11727             : 
   11728        3225 :     for (int i = 0; papszValues && papszValues[i]; ++i)
   11729             :     {
   11730        2325 :         if (EQUAL(papszName, papszValues[i]))
   11731         144 :             return true;
   11732             :     }
   11733             : 
   11734         900 :     return false;
   11735             : }
   11736             : 
   11737             : // Test that a variable is longitude/latitude coordinate,
   11738             : // following CF 4.1 and 4.2.
   11739        4403 : bool NCDFIsVarLongitude(int nCdfId, int nVarId, const char *pszVarName)
   11740             : {
   11741             :     // Check for matching attributes.
   11742        4403 :     int bVal = NCDFDoesVarContainAttribVal(nCdfId, papszCFLongitudeAttribNames,
   11743             :                                            papszCFLongitudeAttribValues, nVarId,
   11744             :                                            pszVarName);
   11745             :     // If not found using attributes then check using var name
   11746             :     // unless GDAL_NETCDF_VERIFY_DIMS=STRICT.
   11747        4403 :     if (bVal == -1)
   11748             :     {
   11749         338 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11750             :                    "STRICT"))
   11751         338 :             bVal = NCDFEqual(pszVarName, papszCFLongitudeVarNames);
   11752             :         else
   11753           0 :             bVal = FALSE;
   11754             :     }
   11755        4065 :     else if (bVal)
   11756             :     {
   11757             :         // Check that the units is not 'm' or '1'. See #6759
   11758         789 :         char *pszTemp = nullptr;
   11759        1156 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11760         367 :             pszTemp != nullptr)
   11761             :         {
   11762         367 :             if (EQUAL(pszTemp, "m") || EQUAL(pszTemp, "1"))
   11763         100 :                 bVal = false;
   11764         367 :             CPLFree(pszTemp);
   11765             :         }
   11766             :     }
   11767             : 
   11768        4403 :     return CPL_TO_BOOL(bVal);
   11769             : }
   11770             : 
   11771        2568 : bool NCDFIsVarLatitude(int nCdfId, int nVarId, const char *pszVarName)
   11772             : {
   11773        2568 :     int bVal = NCDFDoesVarContainAttribVal(nCdfId, papszCFLatitudeAttribNames,
   11774             :                                            papszCFLatitudeAttribValues, nVarId,
   11775             :                                            pszVarName);
   11776        2568 :     if (bVal == -1)
   11777             :     {
   11778         189 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11779             :                    "STRICT"))
   11780         189 :             bVal = NCDFEqual(pszVarName, papszCFLatitudeVarNames);
   11781             :         else
   11782           0 :             bVal = FALSE;
   11783             :     }
   11784        2379 :     else if (bVal)
   11785             :     {
   11786             :         // Check that the units is not 'm' or '1'. See #6759
   11787         550 :         char *pszTemp = nullptr;
   11788         687 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11789         137 :             pszTemp != nullptr)
   11790             :         {
   11791         137 :             if (EQUAL(pszTemp, "m") || EQUAL(pszTemp, "1"))
   11792          37 :                 bVal = false;
   11793         137 :             CPLFree(pszTemp);
   11794             :         }
   11795             :     }
   11796             : 
   11797        2568 :     return CPL_TO_BOOL(bVal);
   11798             : }
   11799             : 
   11800        2836 : bool NCDFIsVarProjectionX(int nCdfId, int nVarId, const char *pszVarName)
   11801             : {
   11802        2836 :     int bVal = NCDFDoesVarContainAttribVal(
   11803             :         nCdfId, papszCFProjectionXAttribNames, papszCFProjectionXAttribValues,
   11804             :         nVarId, pszVarName);
   11805        2836 :     if (bVal == -1)
   11806             :     {
   11807         332 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11808             :                    "STRICT"))
   11809         332 :             bVal = NCDFEqual(pszVarName, papszCFProjectionXVarNames);
   11810             :         else
   11811           0 :             bVal = FALSE;
   11812             :     }
   11813        2504 :     else if (bVal)
   11814             :     {
   11815             :         // Check that the units is not '1'
   11816         533 :         char *pszTemp = nullptr;
   11817         818 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11818         285 :             pszTemp != nullptr)
   11819             :         {
   11820         285 :             if (EQUAL(pszTemp, "1"))
   11821           5 :                 bVal = false;
   11822         285 :             CPLFree(pszTemp);
   11823             :         }
   11824             :     }
   11825             : 
   11826        2836 :     return CPL_TO_BOOL(bVal);
   11827             : }
   11828             : 
   11829        2015 : bool NCDFIsVarProjectionY(int nCdfId, int nVarId, const char *pszVarName)
   11830             : {
   11831        2015 :     int bVal = NCDFDoesVarContainAttribVal(
   11832             :         nCdfId, papszCFProjectionYAttribNames, papszCFProjectionYAttribValues,
   11833             :         nVarId, pszVarName);
   11834        2015 :     if (bVal == -1)
   11835             :     {
   11836         185 :         if (!EQUAL(CPLGetConfigOption("GDAL_NETCDF_VERIFY_DIMS", "YES"),
   11837             :                    "STRICT"))
   11838         185 :             bVal = NCDFEqual(pszVarName, papszCFProjectionYVarNames);
   11839             :         else
   11840           0 :             bVal = FALSE;
   11841             :     }
   11842        1830 :     else if (bVal)
   11843             :     {
   11844             :         // Check that the units is not '1'
   11845         525 :         char *pszTemp = nullptr;
   11846         803 :         if (NCDFGetAttr(nCdfId, nVarId, "units", &pszTemp) == CE_None &&
   11847         278 :             pszTemp != nullptr)
   11848             :         {
   11849         278 :             if (EQUAL(pszTemp, "1"))
   11850           5 :                 bVal = false;
   11851         278 :             CPLFree(pszTemp);
   11852             :         }
   11853             :     }
   11854             : 
   11855        2015 :     return CPL_TO_BOOL(bVal);
   11856             : }
   11857             : 
   11858             : /* test that a variable is a vertical coordinate, following CF 4.3 */
   11859        1215 : bool NCDFIsVarVerticalCoord(int nCdfId, int nVarId, const char *pszVarName)
   11860             : {
   11861             :     /* check for matching attributes */
   11862        1215 :     if (NCDFDoesVarContainAttribVal(nCdfId, papszCFVerticalAttribNames,
   11863             :                                     papszCFVerticalAttribValues, nVarId,
   11864        1215 :                                     pszVarName))
   11865         130 :         return true;
   11866             :     /* check for matching units */
   11867        1085 :     else if (NCDFDoesVarContainAttribVal2(nCdfId, CF_UNITS,
   11868             :                                           papszCFVerticalUnitsValues, nVarId,
   11869        1085 :                                           pszVarName))
   11870          31 :         return true;
   11871             :     /* check for matching standard name */
   11872        1054 :     else if (NCDFDoesVarContainAttribVal2(nCdfId, CF_STD_NAME,
   11873             :                                           papszCFVerticalStandardNameValues,
   11874        1054 :                                           nVarId, pszVarName))
   11875           0 :         return true;
   11876             :     else
   11877        1054 :         return false;
   11878             : }
   11879             : 
   11880             : /* test that a variable is a time coordinate, following CF 4.4 */
   11881         270 : bool NCDFIsVarTimeCoord(int nCdfId, int nVarId, const char *pszVarName)
   11882             : {
   11883             :     /* check for matching attributes */
   11884         270 :     if (NCDFDoesVarContainAttribVal(nCdfId, papszCFTimeAttribNames,
   11885             :                                     papszCFTimeAttribValues, nVarId,
   11886         270 :                                     pszVarName))
   11887         118 :         return true;
   11888             :     /* check for matching units */
   11889         152 :     else if (NCDFDoesVarContainAttribVal2(nCdfId, CF_UNITS,
   11890             :                                           papszCFTimeUnitsValues, nVarId,
   11891         152 :                                           pszVarName, false))
   11892           2 :         return true;
   11893             :     else
   11894         150 :         return false;
   11895             : }
   11896             : 
   11897             : // Parse a string, and return as a string list.
   11898             : // If it is an array of the form {a,b}, then tokenize it.
   11899             : // Otherwise, return a copy.
   11900         302 : static CPLStringList NCDFTokenizeArray(const char *pszValue)
   11901             : {
   11902         302 :     if (pszValue == nullptr || EQUAL(pszValue, ""))
   11903          76 :         return CPLStringList();
   11904             : 
   11905         452 :     CPLStringList aosValues;
   11906         226 :     const int nLen = static_cast<int>(strlen(pszValue));
   11907             : 
   11908         226 :     if (pszValue[0] == '{' && nLen > 2 && pszValue[nLen - 1] == '}')
   11909             :     {
   11910          53 :         char *pszTemp = static_cast<char *>(CPLMalloc((nLen - 2) + 1));
   11911          53 :         strncpy(pszTemp, pszValue + 1, nLen - 2);
   11912          53 :         pszTemp[nLen - 2] = '\0';
   11913             :         aosValues.Assign(
   11914          53 :             CSLTokenizeString2(pszTemp, ",", CSLT_ALLOWEMPTYTOKENS));
   11915          53 :         CPLFree(pszTemp);
   11916             :     }
   11917             :     else
   11918             :     {
   11919         173 :         aosValues.AddString(pszValue);
   11920             :     }
   11921             : 
   11922         226 :     return aosValues;
   11923             : }
   11924             : 
   11925             : // Open a NetCDF subdataset from full path /group1/group2/.../groupn/var.
   11926             : // Leading slash is optional.
   11927         450 : static CPLErr NCDFOpenSubDataset(int nCdfId, const char *pszSubdatasetName,
   11928             :                                  int *pnGroupId, int *pnVarId)
   11929             : {
   11930         450 :     *pnGroupId = -1;
   11931         450 :     *pnVarId = -1;
   11932             : 
   11933             :     // Open group.
   11934         900 :     std::string osGroupFullName = CPLGetPathSafe(pszSubdatasetName);
   11935             :     // Add a leading slash if needed.
   11936         450 :     if (osGroupFullName.empty() || osGroupFullName[0] != '/')
   11937             :     {
   11938         429 :         osGroupFullName = "/" + osGroupFullName;
   11939             :     }
   11940             :     // Detect root group.
   11941         450 :     if (osGroupFullName == "/")
   11942             :     {
   11943         431 :         *pnGroupId = nCdfId;
   11944             :     }
   11945             :     else
   11946             :     {
   11947             :         int status =
   11948          19 :             nc_inq_grp_full_ncid(nCdfId, osGroupFullName.c_str(), pnGroupId);
   11949          19 :         NCDF_ERR_RET(status);
   11950             :     }
   11951             : 
   11952             :     // Open var.
   11953         450 :     const char *pszVarName = CPLGetFilename(pszSubdatasetName);
   11954         450 :     NCDF_ERR_RET(nc_inq_varid(*pnGroupId, pszVarName, pnVarId));
   11955             : 
   11956         450 :     return CE_None;
   11957             : }
   11958             : 
   11959             : // Get all dimensions visible from a given NetCDF (or group) ID and any of
   11960             : // its parents.
   11961         379 : static CPLErr NCDFGetVisibleDims(int nGroupId, int *pnDims, int **ppanDimIds)
   11962             : {
   11963         379 :     int nDims = 0;
   11964         379 :     int *panDimIds = nullptr;
   11965         379 :     NCDF_ERR_RET(nc_inq_dimids(nGroupId, &nDims, nullptr, true));
   11966             : 
   11967         379 :     panDimIds = static_cast<int *>(CPLMalloc(nDims * sizeof(int)));
   11968             : 
   11969         379 :     int status = nc_inq_dimids(nGroupId, nullptr, panDimIds, true);
   11970         379 :     if (status != NC_NOERR)
   11971           0 :         CPLFree(panDimIds);
   11972         379 :     NCDF_ERR_RET(status);
   11973             : 
   11974         379 :     *pnDims = nDims;
   11975         379 :     *ppanDimIds = panDimIds;
   11976             : 
   11977         379 :     return CE_None;
   11978             : }
   11979             : 
   11980             : // Get direct sub-groups IDs of a given NetCDF (or group) ID.
   11981             : // Consider only direct children, does not get children of children.
   11982        3418 : static CPLErr NCDFGetSubGroups(int nGroupId, int *pnSubGroups,
   11983             :                                int **ppanSubGroupIds)
   11984             : {
   11985        3418 :     *pnSubGroups = 0;
   11986        3418 :     *ppanSubGroupIds = nullptr;
   11987             : 
   11988             :     int nSubGroups;
   11989        3418 :     NCDF_ERR_RET(nc_inq_grps(nGroupId, &nSubGroups, nullptr));
   11990             :     int *panSubGroupIds =
   11991        3418 :         static_cast<int *>(CPLMalloc(nSubGroups * sizeof(int)));
   11992        3418 :     NCDF_ERR_RET(nc_inq_grps(nGroupId, nullptr, panSubGroupIds));
   11993        3418 :     *pnSubGroups = nSubGroups;
   11994        3418 :     *ppanSubGroupIds = panSubGroupIds;
   11995             : 
   11996        3418 :     return CE_None;
   11997             : }
   11998             : 
   11999             : // Get the full name of a given NetCDF (or group) ID
   12000             : // (e.g. /group1/group2/.../groupn).
   12001             : // bNC3Compat remove the leading slash for top-level variables for
   12002             : // backward compatibility (top-level variables are the ones in the root group).
   12003       20833 : static CPLErr NCDFGetGroupFullName(int nGroupId, std::string &osFullName,
   12004             :                                    bool bNC3Compat)
   12005             : {
   12006       20833 :     osFullName = "";
   12007             : 
   12008             :     size_t nFullNameLen;
   12009       20833 :     NCDF_ERR_RET(nc_inq_grpname_len(nGroupId, &nFullNameLen));
   12010       20833 :     osFullName.resize(nFullNameLen);
   12011             : 
   12012             :     const int status =
   12013       20833 :         nc_inq_grpname_full(nGroupId, &nFullNameLen, osFullName.data());
   12014       20833 :     if (status != NC_NOERR)
   12015             :     {
   12016           0 :         osFullName = "";
   12017           0 :         NCDF_ERR_RET(status);
   12018             :     }
   12019             : 
   12020       20833 :     if (bNC3Compat && osFullName == "/")
   12021        8910 :         osFullName = "";
   12022             : 
   12023       20833 :     return CE_None;
   12024             : }
   12025             : 
   12026       11708 : CPLString NCDFGetGroupFullName(int nGroupId)
   12027             : {
   12028       11708 :     CPLString osFullName;
   12029       11708 :     NCDFGetGroupFullName(nGroupId, osFullName, false);
   12030             : 
   12031       11708 :     return osFullName;
   12032             : }
   12033             : 
   12034             : // Get the full name of a given NetCDF variable ID
   12035             : // (e.g. /group1/group2/.../groupn/var).
   12036             : // Handle also NC_GLOBAL as nVarId.
   12037             : // bNC3Compat remove the leading slash for top-level variables for
   12038             : // backward compatibility (top-level variables are the ones in the root group).
   12039        9025 : static CPLErr NCDFGetVarFullName(int nGroupId, int nVarId,
   12040             :                                  std::string &osFullName, bool bNC3Compat)
   12041             : {
   12042        9025 :     osFullName = "";
   12043       18050 :     std::string osGroupFullName;
   12044        9025 :     ERR_RET(NCDFGetGroupFullName(nGroupId, osGroupFullName, bNC3Compat));
   12045             :     char szVarName[NC_MAX_NAME + 1];
   12046        9025 :     if (nVarId == NC_GLOBAL)
   12047             :     {
   12048        1164 :         strcpy(szVarName, "NC_GLOBAL");
   12049             :     }
   12050             :     else
   12051             :     {
   12052        7861 :         int status = nc_inq_varname(nGroupId, nVarId, szVarName);
   12053        7861 :         if (status != NC_NOERR)
   12054             :         {
   12055           0 :             NCDF_ERR_RET(status);
   12056             :         }
   12057             :     }
   12058        9025 :     const char *pszSep = "/";
   12059        9025 :     if (osGroupFullName.empty() || osGroupFullName == "/")
   12060        8812 :         pszSep = "";
   12061             :     osFullName =
   12062        9025 :         CPLOPrintf("%s%s%s", osGroupFullName.c_str(), pszSep, szVarName);
   12063             : 
   12064        9025 :     return CE_None;
   12065             : }
   12066             : 
   12067             : // Get the NetCDF root group ID of a given group ID.
   12068           2 : static CPLErr NCDFGetRootGroup(int nStartGroupId, int *pnRootGroupId)
   12069             : {
   12070           2 :     *pnRootGroupId = -1;
   12071             :     // Recurse on parent group.
   12072             :     int nParentGroupId;
   12073           2 :     int status = nc_inq_grp_parent(nStartGroupId, &nParentGroupId);
   12074           2 :     if (status == NC_NOERR)
   12075           0 :         return NCDFGetRootGroup(nParentGroupId, pnRootGroupId);
   12076           2 :     else if (status != NC_ENOGRP)
   12077           0 :         NCDF_ERR_RET(status);
   12078             :     else  // No more parent group.
   12079             :     {
   12080           2 :         *pnRootGroupId = nStartGroupId;
   12081             :     }
   12082             : 
   12083           2 :     return CE_None;
   12084             : }
   12085             : 
   12086             : // Read the size and type of an extra dimension
   12087          18 : static std::pair<int, int> ReadExtraDimDef(GDALDataset *poSrcDS,
   12088             :                                            const char *pszDimName)
   12089             : {
   12090             :     static char szTemp[NC_MAX_NAME + 32 + 1];
   12091          18 :     snprintf(szTemp, sizeof(szTemp), "NETCDF_DIM_%s_DEF", pszDimName);
   12092             :     const CPLStringList aosExtraDimValues =
   12093          18 :         NCDFTokenizeArray(poSrcDS->GetMetadataItem(szTemp, ""));
   12094             :     const int nDimSize =
   12095          18 :         aosExtraDimValues.empty() ? 0 : atoi(aosExtraDimValues[0]);
   12096             : 
   12097             :     // nc_type is an enum in netcdf-3, needs casting.
   12098             :     const int nVarType = static_cast<nc_type>(
   12099          18 :         aosExtraDimValues.size() >= 2 ? atol(aosExtraDimValues[1]) : 0);
   12100             : 
   12101          36 :     return {nDimSize, nVarType};
   12102             : }
   12103             : 
   12104             : // Implementation of NCDFResolveVar/Att.
   12105       14672 : static CPLErr NCDFResolveElem(int nStartGroupId, const char *pszVar,
   12106             :                               const char *pszAtt, int *pnGroupId, int *pnId,
   12107             :                               bool bMandatory)
   12108             : {
   12109       14672 :     if (!pszVar && !pszAtt)
   12110             :     {
   12111           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
   12112             :                  "pszVar and pszAtt NCDFResolveElem() args are both null.");
   12113           0 :         return CE_Failure;
   12114             :     }
   12115             : 
   12116             :     enum
   12117             :     {
   12118             :         NCRM_PARENT,
   12119             :         NCRM_WIDTH_WISE
   12120       14672 :     } eNCResolveMode = NCRM_PARENT;
   12121             : 
   12122       29344 :     std::queue<int> aoQueueGroupIdsToVisit;
   12123       14672 :     aoQueueGroupIdsToVisit.push(nStartGroupId);
   12124             : 
   12125       16621 :     while (!aoQueueGroupIdsToVisit.empty())
   12126             :     {
   12127             :         // Get the first group of the FIFO queue.
   12128       14867 :         *pnGroupId = aoQueueGroupIdsToVisit.front();
   12129       14867 :         aoQueueGroupIdsToVisit.pop();
   12130             : 
   12131             :         // Look if this group contains the searched element.
   12132             :         int status;
   12133       14867 :         if (pszVar)
   12134       14638 :             status = nc_inq_varid(*pnGroupId, pszVar, pnId);
   12135             :         else  // pszAtt != nullptr.
   12136         229 :             status = nc_inq_attid(*pnGroupId, NC_GLOBAL, pszAtt, pnId);
   12137             : 
   12138       14867 :         if (status == NC_NOERR)
   12139             :         {
   12140       12918 :             return CE_None;
   12141             :         }
   12142        1949 :         else if ((pszVar && status != NC_ENOTVAR) ||
   12143         226 :                  (pszAtt && status != NC_ENOTATT))
   12144             :         {
   12145           0 :             NCDF_ERR(status);
   12146             :         }
   12147             :         // Element not found, in NC4 case we must search in other groups
   12148             :         // following the CF logic.
   12149             : 
   12150             :         // The first resolve mode consists to search on parent groups.
   12151        1949 :         if (eNCResolveMode == NCRM_PARENT)
   12152             :         {
   12153        1829 :             int nParentGroupId = -1;
   12154        1829 :             int status2 = nc_inq_grp_parent(*pnGroupId, &nParentGroupId);
   12155        1829 :             if (status2 == NC_NOERR)
   12156          62 :                 aoQueueGroupIdsToVisit.push(nParentGroupId);
   12157        1767 :             else if (status2 != NC_ENOGRP)
   12158           0 :                 NCDF_ERR(status2);
   12159        1767 :             else if (pszVar)
   12160             :                 // When resolving a variable, if there is no more
   12161             :                 // parent group then we switch to width-wise search mode
   12162             :                 // starting from the latest found parent group.
   12163        1544 :                 eNCResolveMode = NCRM_WIDTH_WISE;
   12164             :         }
   12165             : 
   12166             :         // The second resolve mode is a width-wise search.
   12167        1949 :         if (eNCResolveMode == NCRM_WIDTH_WISE)
   12168             :         {
   12169             :             // Enqueue all direct sub-groups.
   12170        1664 :             int nSubGroups = 0;
   12171        1664 :             int *panSubGroupIds = nullptr;
   12172        1664 :             NCDFGetSubGroups(*pnGroupId, &nSubGroups, &panSubGroupIds);
   12173        1797 :             for (int i = 0; i < nSubGroups; i++)
   12174         133 :                 aoQueueGroupIdsToVisit.push(panSubGroupIds[i]);
   12175        1664 :             CPLFree(panSubGroupIds);
   12176             :         }
   12177             :     }
   12178             : 
   12179        1754 :     if (bMandatory)
   12180             :     {
   12181           0 :         std::string osStartGroupFullName;
   12182           0 :         NCDFGetGroupFullName(nStartGroupId, osStartGroupFullName);
   12183           0 :         CPLError(CE_Failure, CPLE_AppDefined,
   12184             :                  "Cannot resolve mandatory %s %s from group %s",
   12185             :                  (pszVar ? pszVar : pszAtt),
   12186             :                  (pszVar ? "variable" : "attribute"),
   12187             :                  osStartGroupFullName.c_str());
   12188             :     }
   12189             : 
   12190        1754 :     *pnGroupId = -1;
   12191        1754 :     *pnId = -1;
   12192        1754 :     return CE_Failure;
   12193             : }
   12194             : 
   12195             : // Resolve a variable name from a given starting group following the CF logic:
   12196             : // - if var name is an absolute path then directly open it
   12197             : // - first search in the starting group and its parent groups
   12198             : // - then if there is no more parent group we switch to a width-wise search
   12199             : //   mode starting from the latest found parent group.
   12200             : // The full CF logic is described here:
   12201             : // https://github.com/diwg/cf2/blob/master/group/cf2-group.adoc#scope
   12202             : // If bMandatory then print an error if resolving fails.
   12203             : // TODO: implement support of relative paths.
   12204             : // TODO: to follow strictly the CF logic, when searching for a coordinate
   12205             : //       variable, we must stop the parent search mode once the corresponding
   12206             : //       dimension is found and start the width-wise search from this group.
   12207             : // TODO: to follow strictly the CF logic, when searching in width-wise mode
   12208             : //       we should skip every groups already visited during the parent
   12209             : //       search mode (but revisiting them should have no impact so we could
   12210             : //       let as it is if it is simpler...)
   12211             : // TODO: CF specifies that the width-wise search order is "left-to-right" so
   12212             : //       maybe we must sort sibling groups alphabetically? but maybe not
   12213             : //       necessary if nc_inq_grps() already sort them?
   12214       14448 : CPLErr NCDFResolveVar(int nStartGroupId, const char *pszVar, int *pnGroupId,
   12215             :                       int *pnVarId, bool bMandatory)
   12216             : {
   12217       14448 :     *pnGroupId = -1;
   12218       14448 :     *pnVarId = -1;
   12219       14448 :     int nGroupId = nStartGroupId, nVarId;
   12220       14448 :     if (pszVar[0] == '/')
   12221             :     {
   12222             :         // This is an absolute path: we can open the var directly.
   12223             :         int nRootGroupId;
   12224           2 :         ERR_RET(NCDFGetRootGroup(nStartGroupId, &nRootGroupId));
   12225           2 :         ERR_RET(NCDFOpenSubDataset(nRootGroupId, pszVar, &nGroupId, &nVarId));
   12226             :     }
   12227             :     else
   12228             :     {
   12229             :         // We have to search the variable following the CF logic.
   12230       14446 :         ERR_RET(NCDFResolveElem(nStartGroupId, pszVar, nullptr, &nGroupId,
   12231             :                                 &nVarId, bMandatory));
   12232             :     }
   12233       12917 :     *pnGroupId = nGroupId;
   12234       12917 :     *pnVarId = nVarId;
   12235       12917 :     return CE_None;
   12236             : }
   12237             : 
   12238             : // Like NCDFResolveVar but returns directly the var full name.
   12239        1567 : static CPLErr NCDFResolveVarFullName(int nStartGroupId, const char *pszVar,
   12240             :                                      std::string &osFullName, bool bMandatory)
   12241             : {
   12242             :     int nGroupId, nVarId;
   12243        1567 :     osFullName = "";
   12244        1567 :     ERR_RET(
   12245             :         NCDFResolveVar(nStartGroupId, pszVar, &nGroupId, &nVarId, bMandatory));
   12246        1537 :     return NCDFGetVarFullName(nGroupId, nVarId, osFullName);
   12247             : }
   12248             : 
   12249             : // Like NCDFResolveVar but resolves an attribute instead a variable and
   12250             : // returns its integer value.
   12251             : // Only GLOBAL attributes are supported for the moment.
   12252         226 : static CPLErr NCDFResolveAttInt(int nStartGroupId, int nStartVarId,
   12253             :                                 const char *pszAtt, int *pnAtt, bool bMandatory)
   12254             : {
   12255         226 :     int nGroupId = nStartGroupId, nAttId = nStartVarId;
   12256         226 :     ERR_RET(NCDFResolveElem(nStartGroupId, nullptr, pszAtt, &nGroupId, &nAttId,
   12257             :                             bMandatory));
   12258           3 :     NCDF_ERR_RET(nc_get_att_int(nGroupId, NC_GLOBAL, pszAtt, pnAtt));
   12259           3 :     return CE_None;
   12260             : }
   12261             : 
   12262             : // Filter variables to keep only valid 2+D raster bands and vector fields in
   12263             : // a given a NetCDF (or group) ID and its sub-groups.
   12264             : // Coordinate or boundary variables are ignored.
   12265             : // It also creates corresponding vector layers.
   12266         564 : CPLErr netCDFDataset::FilterVars(
   12267             :     int nCdfId, bool bKeepRasters, bool bKeepVectors,
   12268             :     const CPLStringList &aosIgnoreVars, int *pnRasterVars, int *pnGroupId,
   12269             :     int *pnVarId, int *pnIgnoredVars,
   12270             :     std::map<std::array<int, 3>, std::vector<std::pair<int, int>>>
   12271             :         &oMap2DDimsToGroupAndVar)
   12272             : {
   12273         564 :     int nVars = 0;
   12274         564 :     int nRasterVars = 0;
   12275         564 :     NCDF_ERR(nc_inq(nCdfId, nullptr, &nVars, nullptr, nullptr));
   12276             : 
   12277        1128 :     std::vector<int> anPotentialVectorVarID;
   12278             :     // oMapDimIdToCount[x] = number of times dim x is the first dimension of
   12279             :     // potential vector variables
   12280        1128 :     std::map<int, int> oMapDimIdToCount;
   12281         564 :     int nVarXId = -1;
   12282         564 :     int nVarYId = -1;
   12283         564 :     int nVarZId = -1;
   12284         564 :     int nVarTimeId = -1;
   12285         564 :     int nVarTimeDimId = -1;
   12286         564 :     bool bIsVectorOnly = true;
   12287         564 :     int nProfileDimId = -1;
   12288         564 :     int nParentIndexVarID = -1;
   12289             : 
   12290        3454 :     for (int v = 0; v < nVars; v++)
   12291             :     {
   12292             :         int nVarDims;
   12293        2890 :         NCDF_ERR_RET(nc_inq_varndims(nCdfId, v, &nVarDims));
   12294             :         // Should we ignore this variable?
   12295             :         char szTemp[NC_MAX_NAME + 1];
   12296        2890 :         szTemp[0] = '\0';
   12297        2890 :         NCDF_ERR_RET(nc_inq_varname(nCdfId, v, szTemp));
   12298             : 
   12299        2890 :         if (strstr(szTemp, "_node_coordinates") ||
   12300        2890 :             strstr(szTemp, "_node_count"))
   12301             :         {
   12302             :             // Ignore CF-1.8 Simple Geometries helper variables
   12303          70 :             continue;
   12304             :         }
   12305             : 
   12306        4245 :         if (nVarDims == 1 && (NCDFIsVarLongitude(nCdfId, -1, szTemp) ||
   12307        1425 :                               NCDFIsVarProjectionX(nCdfId, -1, szTemp)))
   12308             :         {
   12309         380 :             nVarXId = v;
   12310             :         }
   12311        3485 :         else if (nVarDims == 1 && (NCDFIsVarLatitude(nCdfId, -1, szTemp) ||
   12312        1045 :                                    NCDFIsVarProjectionY(nCdfId, -1, szTemp)))
   12313             :         {
   12314         379 :             nVarYId = v;
   12315             :         }
   12316        2061 :         else if (nVarDims == 1 && NCDFIsVarVerticalCoord(nCdfId, -1, szTemp))
   12317             :         {
   12318          97 :             nVarZId = v;
   12319             :         }
   12320             :         else
   12321             :         {
   12322        1964 :             std::string osFullName;
   12323        1964 :             CPLErr eErr = NCDFGetVarFullName(nCdfId, v, osFullName);
   12324        1964 :             if (eErr != CE_None)
   12325             :             {
   12326           0 :                 continue;
   12327             :             }
   12328             :             const bool bIgnoreVar =
   12329        1964 :                 aosIgnoreVars.FindString(osFullName.c_str()) != -1;
   12330        1964 :             if (bIgnoreVar)
   12331             :             {
   12332         135 :                 if (nVarDims == 1 && NCDFIsVarTimeCoord(nCdfId, -1, szTemp))
   12333             :                 {
   12334          16 :                     nVarTimeId = v;
   12335          16 :                     nc_inq_vardimid(nCdfId, v, &nVarTimeDimId);
   12336             :                 }
   12337         119 :                 else if (nVarDims > 1)
   12338             :                 {
   12339         105 :                     (*pnIgnoredVars)++;
   12340         105 :                     CPLDebug("GDAL_netCDF", "variable #%d [%s] was ignored", v,
   12341             :                              szTemp);
   12342             :                 }
   12343             :             }
   12344             :             // Only accept 2+D vars.
   12345        1829 :             else if (nVarDims >= 2)
   12346             :             {
   12347         730 :                 bool bRasterCandidate = true;
   12348             :                 // Identify variables that might be vector variables
   12349         730 :                 if (nVarDims == 2)
   12350             :                 {
   12351         645 :                     int anDimIds[2] = {-1, -1};
   12352         645 :                     nc_inq_vardimid(nCdfId, v, anDimIds);
   12353             : 
   12354         645 :                     nc_type vartype = NC_NAT;
   12355         645 :                     nc_inq_vartype(nCdfId, v, &vartype);
   12356             : 
   12357             :                     char szDimNameFirst[NC_MAX_NAME + 1];
   12358             :                     char szDimNameSecond[NC_MAX_NAME + 1];
   12359         645 :                     szDimNameFirst[0] = '\0';
   12360         645 :                     szDimNameSecond[0] = '\0';
   12361        1485 :                     if (vartype == NC_CHAR &&
   12362         195 :                         nc_inq_dimname(nCdfId, anDimIds[0], szDimNameFirst) ==
   12363         195 :                             NC_NOERR &&
   12364         195 :                         nc_inq_dimname(nCdfId, anDimIds[1], szDimNameSecond) ==
   12365         195 :                             NC_NOERR &&
   12366         195 :                         !NCDFIsVarLongitude(nCdfId, -1, szDimNameSecond) &&
   12367         195 :                         !NCDFIsVarProjectionX(nCdfId, -1, szDimNameSecond) &&
   12368        1035 :                         !NCDFIsVarLatitude(nCdfId, -1, szDimNameFirst) &&
   12369         195 :                         !NCDFIsVarProjectionY(nCdfId, -1, szDimNameFirst))
   12370             :                     {
   12371         195 :                         anPotentialVectorVarID.push_back(v);
   12372         195 :                         oMapDimIdToCount[anDimIds[0]]++;
   12373         195 :                         if (strstr(szDimNameSecond, "_max_width"))
   12374             :                         {
   12375         165 :                             bRasterCandidate = false;
   12376             :                         }
   12377             :                         else
   12378             :                         {
   12379          30 :                             std::array<int, 3> oKey{anDimIds[0], anDimIds[1],
   12380          30 :                                                     vartype};
   12381          30 :                             oMap2DDimsToGroupAndVar[oKey].emplace_back(
   12382          30 :                                 std::pair(nCdfId, v));
   12383             :                         }
   12384             :                     }
   12385             :                     else
   12386             :                     {
   12387         450 :                         std::array<int, 3> oKey{anDimIds[0], anDimIds[1],
   12388         450 :                                                 vartype};
   12389         450 :                         oMap2DDimsToGroupAndVar[oKey].emplace_back(
   12390         450 :                             std::pair(nCdfId, v));
   12391         450 :                         bIsVectorOnly = false;
   12392             :                     }
   12393             :                 }
   12394             :                 else
   12395             :                 {
   12396          85 :                     bIsVectorOnly = false;
   12397             :                 }
   12398         730 :                 if (bKeepRasters && bRasterCandidate)
   12399             :                 {
   12400         536 :                     *pnGroupId = nCdfId;
   12401         536 :                     *pnVarId = v;
   12402         536 :                     nRasterVars++;
   12403             :                 }
   12404             :             }
   12405        1099 :             else if (nVarDims == 1)
   12406             :             {
   12407         797 :                 nc_type atttype = NC_NAT;
   12408         797 :                 size_t attlen = 0;
   12409         797 :                 if (nc_inq_att(nCdfId, v, "instance_dimension", &atttype,
   12410          36 :                                &attlen) == NC_NOERR &&
   12411         797 :                     atttype == NC_CHAR && attlen < NC_MAX_NAME)
   12412             :                 {
   12413          72 :                     std::string osInstanceDimension;
   12414          36 :                     if (NCDFGetAttr(nCdfId, v, "instance_dimension",
   12415          36 :                                     osInstanceDimension) == CE_None)
   12416             :                     {
   12417             :                         const int status =
   12418          36 :                             nc_inq_dimid(nCdfId, osInstanceDimension.c_str(),
   12419             :                                          &nProfileDimId);
   12420          36 :                         if (status == NC_NOERR)
   12421          36 :                             nParentIndexVarID = v;
   12422             :                         else
   12423           0 :                             nProfileDimId = -1;
   12424          36 :                         if (status == NC_EBADDIM)
   12425           0 :                             CPLError(CE_Warning, CPLE_AppDefined,
   12426             :                                      "Attribute instance_dimension='%s' refers "
   12427             :                                      "to a non existing dimension",
   12428             :                                      osInstanceDimension.c_str());
   12429             :                         else
   12430          36 :                             NCDF_ERR(status);
   12431             :                     }
   12432             :                 }
   12433         797 :                 if (v != nParentIndexVarID)
   12434             :                 {
   12435         761 :                     anPotentialVectorVarID.push_back(v);
   12436         761 :                     int nDimId = -1;
   12437         761 :                     nc_inq_vardimid(nCdfId, v, &nDimId);
   12438         761 :                     oMapDimIdToCount[nDimId]++;
   12439             :                 }
   12440             :             }
   12441             :         }
   12442             :     }
   12443             : 
   12444             :     // If we are opened in raster-only mode and that there are only 1D or 2D
   12445             :     // variables and that the 2D variables have no X/Y dim, and all
   12446             :     // variables refer to the same main dimension (or 2 dimensions for
   12447             :     // featureType=profile), then it is a pure vector dataset
   12448             :     CPLString osFeatureType(
   12449         564 :         aosMetadata.FetchNameValueDef("NC_GLOBAL#featureType", ""));
   12450         431 :     if (bKeepRasters && !bKeepVectors && bIsVectorOnly && nRasterVars > 0 &&
   12451         995 :         !anPotentialVectorVarID.empty() &&
   12452           0 :         (oMapDimIdToCount.size() == 1 ||
   12453           0 :          (EQUAL(osFeatureType, "profile") && oMapDimIdToCount.size() == 2 &&
   12454           0 :           nProfileDimId >= 0)))
   12455             :     {
   12456           0 :         anPotentialVectorVarID.resize(0);
   12457             :     }
   12458             :     else
   12459             :     {
   12460         564 :         *pnRasterVars += nRasterVars;
   12461             :     }
   12462             : 
   12463         564 :     if (!anPotentialVectorVarID.empty() && bKeepVectors)
   12464             :     {
   12465             :         // Take the dimension that is referenced the most times.
   12466         135 :         if (!(oMapDimIdToCount.size() == 1 ||
   12467          70 :               (EQUAL(osFeatureType, "profile") &&
   12468          70 :                oMapDimIdToCount.size() == 2 && nProfileDimId >= 0)))
   12469             :         {
   12470           0 :             CPLError(CE_Warning, CPLE_AppDefined,
   12471             :                      "The dataset has several variables that could be "
   12472             :                      "identified as vector fields, but not all share the same "
   12473             :                      "primary dimension. Consequently they will be ignored.");
   12474             :         }
   12475             :         else
   12476             :         {
   12477         101 :             if (nVarTimeId >= 0 &&
   12478         101 :                 oMapDimIdToCount.find(nVarTimeDimId) != oMapDimIdToCount.end())
   12479             :             {
   12480           1 :                 anPotentialVectorVarID.push_back(nVarTimeId);
   12481             :             }
   12482         100 :             CreateGrpVectorLayers(nCdfId, osFeatureType, anPotentialVectorVarID,
   12483             :                                   oMapDimIdToCount, nVarXId, nVarYId, nVarZId,
   12484             :                                   nProfileDimId, nParentIndexVarID,
   12485             :                                   bKeepRasters);
   12486             :         }
   12487             :     }
   12488             : 
   12489             :     // Recurse on sub-groups.
   12490         564 :     int nSubGroups = 0;
   12491         564 :     int *panSubGroupIds = nullptr;
   12492         564 :     NCDFGetSubGroups(nCdfId, &nSubGroups, &panSubGroupIds);
   12493         601 :     for (int i = 0; i < nSubGroups; i++)
   12494             :     {
   12495          37 :         FilterVars(panSubGroupIds[i], bKeepRasters, bKeepVectors, aosIgnoreVars,
   12496             :                    pnRasterVars, pnGroupId, pnVarId, pnIgnoredVars,
   12497             :                    oMap2DDimsToGroupAndVar);
   12498             :     }
   12499         564 :     CPLFree(panSubGroupIds);
   12500             : 
   12501         564 :     return CE_None;
   12502             : }
   12503             : 
   12504             : // Create vector layers from given potentially identified vector variables
   12505             : // resulting from the scanning of a NetCDF (or group) ID.
   12506         100 : CPLErr netCDFDataset::CreateGrpVectorLayers(
   12507             :     int nCdfId, const CPLString &osFeatureType,
   12508             :     const std::vector<int> &anPotentialVectorVarID,
   12509             :     const std::map<int, int> &oMapDimIdToCount, int nVarXId, int nVarYId,
   12510             :     int nVarZId, int nProfileDimId, int nParentIndexVarID, bool bKeepRasters)
   12511             : {
   12512         200 :     std::string osGroupName;
   12513         100 :     NCDFGetGroupFullName(nCdfId, osGroupName);
   12514         100 :     if (osGroupName.empty())
   12515             :     {
   12516          98 :         osGroupName = CPLGetBasenameSafe(osFilename);
   12517             :     }
   12518         100 :     OGRwkbGeometryType eGType = wkbUnknown;
   12519             :     CPLString osLayerName = aosMetadata.FetchNameValueDef(
   12520         200 :         "NC_GLOBAL#ogr_layer_name", osGroupName.c_str());
   12521         100 :     aosMetadata.SetNameValue("NC_GLOBAL#ogr_layer_name", nullptr);
   12522             : 
   12523         100 :     if (EQUAL(osFeatureType, "point") || EQUAL(osFeatureType, "profile"))
   12524             :     {
   12525          54 :         aosMetadata.SetNameValue("NC_GLOBAL#featureType", nullptr);
   12526          54 :         eGType = wkbPoint;
   12527             :     }
   12528             : 
   12529             :     const char *pszLayerType =
   12530         100 :         aosMetadata.FetchNameValue("NC_GLOBAL#ogr_layer_type");
   12531         100 :     if (pszLayerType != nullptr)
   12532             :     {
   12533          10 :         eGType = OGRFromOGCGeomType(pszLayerType);
   12534          10 :         aosMetadata.SetNameValue("NC_GLOBAL#ogr_layer_type", nullptr);
   12535             :     }
   12536             : 
   12537             :     CPLString osGeometryField =
   12538         200 :         aosMetadata.FetchNameValueDef("NC_GLOBAL#ogr_geometry_field", "");
   12539         100 :     aosMetadata.SetNameValue("NC_GLOBAL#ogr_geometry_field", nullptr);
   12540             : 
   12541         100 :     int nFirstVarId = -1;
   12542         100 :     int nVectorDim = oMapDimIdToCount.rbegin()->first;
   12543         100 :     if (EQUAL(osFeatureType, "profile") && oMapDimIdToCount.size() == 2)
   12544             :     {
   12545          35 :         if (nVectorDim == nProfileDimId)
   12546           0 :             nVectorDim = oMapDimIdToCount.begin()->first;
   12547             :     }
   12548             :     else
   12549             :     {
   12550          65 :         nProfileDimId = -1;
   12551             :     }
   12552         135 :     for (auto nVarID : anPotentialVectorVarID)
   12553             :     {
   12554         135 :         int anDimIds[2] = {-1, -1};
   12555         135 :         nc_inq_vardimid(nCdfId, nVarID, anDimIds);
   12556         135 :         if (nVectorDim == anDimIds[0])
   12557             :         {
   12558         100 :             nFirstVarId = nVarID;
   12559         100 :             break;
   12560             :         }
   12561             :     }
   12562             : 
   12563             :     // In case where coordinates are explicitly specified for one of the
   12564             :     // field/variable, use them in priority over the ones that might have been
   12565             :     // identified above.
   12566         100 :     char *pszCoordinates = nullptr;
   12567         100 :     if (NCDFGetAttr(nCdfId, nFirstVarId, "coordinates", &pszCoordinates) ==
   12568             :         CE_None)
   12569             :     {
   12570             :         const CPLStringList aosTokens(
   12571         110 :             NCDFTokenizeCoordinatesAttribute(pszCFCoordinates));
   12572          55 :         for (const auto &pszToken : aosTokens)
   12573             :         {
   12574           0 :             if (NCDFIsVarLongitude(nCdfId, -1, pszToken) ||
   12575           0 :                 NCDFIsVarProjectionX(nCdfId, -1, pszToken))
   12576             :             {
   12577           0 :                 nVarXId = -1;
   12578           0 :                 CPL_IGNORE_RET_VAL(nc_inq_varid(nCdfId, pszToken, &nVarXId));
   12579             :             }
   12580           0 :             else if (NCDFIsVarLatitude(nCdfId, -1, pszToken) ||
   12581           0 :                      NCDFIsVarProjectionY(nCdfId, -1, pszToken))
   12582             :             {
   12583           0 :                 nVarYId = -1;
   12584           0 :                 CPL_IGNORE_RET_VAL(nc_inq_varid(nCdfId, pszToken, &nVarYId));
   12585             :             }
   12586           0 :             else if (NCDFIsVarVerticalCoord(nCdfId, -1, pszToken))
   12587             :             {
   12588           0 :                 nVarZId = -1;
   12589           0 :                 CPL_IGNORE_RET_VAL(nc_inq_varid(nCdfId, pszToken, &nVarZId));
   12590             :             }
   12591             :         }
   12592             :     }
   12593         100 :     CPLFree(pszCoordinates);
   12594             : 
   12595             :     // Check that the X,Y,Z vars share 1D and share the same dimension as
   12596             :     // attribute variables.
   12597         100 :     if (nVarXId >= 0 && nVarYId >= 0)
   12598             :     {
   12599          84 :         int nVarDimCount = -1;
   12600          84 :         int nVarDimId = -1;
   12601          84 :         if (nc_inq_varndims(nCdfId, nVarXId, &nVarDimCount) != NC_NOERR ||
   12602          84 :             nVarDimCount != 1 ||
   12603          84 :             nc_inq_vardimid(nCdfId, nVarXId, &nVarDimId) != NC_NOERR ||
   12604          84 :             nVarDimId != ((nProfileDimId >= 0) ? nProfileDimId : nVectorDim) ||
   12605          56 :             nc_inq_varndims(nCdfId, nVarYId, &nVarDimCount) != NC_NOERR ||
   12606          56 :             nVarDimCount != 1 ||
   12607         224 :             nc_inq_vardimid(nCdfId, nVarYId, &nVarDimId) != NC_NOERR ||
   12608          56 :             nVarDimId != ((nProfileDimId >= 0) ? nProfileDimId : nVectorDim))
   12609             :         {
   12610          28 :             nVarXId = nVarYId = -1;
   12611             :         }
   12612         111 :         else if (nVarZId >= 0 &&
   12613          55 :                  (nc_inq_varndims(nCdfId, nVarZId, &nVarDimCount) != NC_NOERR ||
   12614          55 :                   nVarDimCount != 1 ||
   12615          55 :                   nc_inq_vardimid(nCdfId, nVarZId, &nVarDimId) != NC_NOERR ||
   12616          55 :                   nVarDimId != nVectorDim))
   12617             :         {
   12618           0 :             nVarZId = -1;
   12619             :         }
   12620             :     }
   12621             : 
   12622         100 :     if (eGType == wkbUnknown && nVarXId >= 0 && nVarYId >= 0)
   12623             :     {
   12624           2 :         eGType = wkbPoint;
   12625             :     }
   12626         100 :     if (eGType == wkbPoint && nVarXId >= 0 && nVarYId >= 0 && nVarZId >= 0)
   12627             :     {
   12628          55 :         eGType = wkbPoint25D;
   12629             :     }
   12630         100 :     if (eGType == wkbUnknown && osGeometryField.empty())
   12631             :     {
   12632          34 :         eGType = wkbNone;
   12633             :     }
   12634             : 
   12635             :     // Read projection info
   12636         200 :     CPLStringList aosMetadataBackup = aosMetadata;
   12637         100 :     ReadAttributes(nCdfId, nFirstVarId);
   12638         100 :     if (!this->bSGSupport)
   12639         100 :         SetProjectionFromVar(nCdfId, nFirstVarId, true);
   12640         100 :     const char *pszValue = FetchAttr(nCdfId, nFirstVarId, CF_GRD_MAPPING);
   12641         200 :     std::string osGridMapping = pszValue ? pszValue : "";
   12642         100 :     aosMetadata = std::move(aosMetadataBackup);
   12643             : 
   12644         100 :     OGRSpatialReference *poSRS = nullptr;
   12645         100 :     if (!m_oSRS.IsEmpty())
   12646             :     {
   12647          21 :         poSRS = m_oSRS.Clone();
   12648             :     }
   12649             :     // Reset if there's a 2D raster
   12650         100 :     m_bHasProjection = false;
   12651         100 :     m_bHasGeoTransform = false;
   12652             : 
   12653         100 :     if (!bKeepRasters)
   12654             :     {
   12655             :         // Strip out uninteresting metadata.
   12656          67 :         aosMetadata.SetNameValue("NC_GLOBAL#Conventions", nullptr);
   12657          67 :         aosMetadata.SetNameValue("NC_GLOBAL#GDAL", nullptr);
   12658          67 :         aosMetadata.SetNameValue("NC_GLOBAL#history", nullptr);
   12659             :     }
   12660             : 
   12661             :     auto poLayer =
   12662         100 :         std::make_shared<netCDFLayer>(this, nCdfId, osLayerName, eGType, poSRS);
   12663         100 :     if (poSRS != nullptr)
   12664          21 :         poSRS->Release();
   12665         100 :     poLayer->SetRecordDimID(nVectorDim);
   12666         100 :     if (wkbFlatten(eGType) == wkbPoint && nVarXId >= 0 && nVarYId >= 0)
   12667             :     {
   12668          56 :         poLayer->SetXYZVars(nVarXId, nVarYId, nVarZId);
   12669             :     }
   12670          44 :     else if (!osGeometryField.empty())
   12671             :     {
   12672          10 :         poLayer->SetWKTGeometryField(osGeometryField);
   12673             :     }
   12674         100 :     if (!osGridMapping.empty())
   12675             :     {
   12676          21 :         poLayer->SetGridMapping(osGridMapping.c_str());
   12677             :     }
   12678         100 :     poLayer->SetProfile(nProfileDimId, nParentIndexVarID);
   12679             : 
   12680         742 :     for (int nVarID : anPotentialVectorVarID)
   12681             :     {
   12682         642 :         int anDimIds[2] = {-1, -1};
   12683         642 :         nc_inq_vardimid(nCdfId, nVarID, anDimIds);
   12684         642 :         if (anDimIds[0] == nVectorDim ||
   12685          68 :             (nProfileDimId >= 0 && anDimIds[0] == nProfileDimId))
   12686             :         {
   12687             : #ifdef NCDF_DEBUG
   12688             :             char szTemp2[NC_MAX_NAME + 1] = {};
   12689             :             CPL_IGNORE_RET_VAL(nc_inq_varname(nCdfId, nVarID, szTemp2));
   12690             :             CPLDebug("GDAL_netCDF", "Variable %s is a vector field", szTemp2);
   12691             : #endif
   12692         642 :             poLayer->AddField(nVarID);
   12693             :         }
   12694             :     }
   12695             : 
   12696         100 :     if (poLayer->GetLayerDefn()->GetFieldCount() != 0 ||
   12697           0 :         poLayer->GetGeomType() != wkbNone)
   12698             :     {
   12699         100 :         papoLayers.push_back(poLayer);
   12700             :     }
   12701             : 
   12702         200 :     return CE_None;
   12703             : }
   12704             : 
   12705             : // Get all coordinate and boundary variables full names referenced in
   12706             : // a given a NetCDF (or group) ID and its sub-groups.
   12707             : // These variables are identified in other variable's
   12708             : // "coordinates" and "bounds" attribute.
   12709             : // Searching coordinate and boundary variables may need to explore
   12710             : // parents groups (or other groups in case of reference given in form of an
   12711             : // absolute path).
   12712             : // See CF sections 5.2, 5.6 and 7.1
   12713         565 : static CPLErr NCDFGetCoordAndBoundVarFullNames(int nCdfId,
   12714             :                                                CPLStringList &aosVars)
   12715             : {
   12716         565 :     int nVars = 0;
   12717         565 :     NCDF_ERR(nc_inq(nCdfId, nullptr, &nVars, nullptr, nullptr));
   12718             : 
   12719        3469 :     for (int v = 0; v < nVars; v++)
   12720             :     {
   12721        2904 :         char *pszTemp = nullptr;
   12722        5808 :         CPLStringList aosTokens;
   12723        2904 :         if (NCDFGetAttr(nCdfId, v, "coordinates", &pszTemp) == CE_None)
   12724         500 :             aosTokens.Assign(NCDFTokenizeCoordinatesAttribute(pszTemp));
   12725        2904 :         CPLFree(pszTemp);
   12726        2904 :         pszTemp = nullptr;
   12727        2904 :         if (NCDFGetAttr(nCdfId, v, "bounds", &pszTemp) == CE_None &&
   12728        2904 :             pszTemp != nullptr && !EQUAL(pszTemp, ""))
   12729          20 :             aosTokens.AddString(pszTemp);
   12730        2904 :         CPLFree(pszTemp);
   12731        4357 :         for (const auto &pszToken : aosTokens)
   12732             :         {
   12733        2906 :             std::string osVarFullName;
   12734        1453 :             if (NCDFResolveVarFullName(nCdfId, pszToken, osVarFullName) ==
   12735             :                 CE_None)
   12736        1423 :                 aosVars.AddString(osVarFullName);
   12737             :         }
   12738             :     }
   12739             : 
   12740             :     // Recurse on sub-groups.
   12741             :     int nSubGroups;
   12742         565 :     int *panSubGroupIds = nullptr;
   12743         565 :     NCDFGetSubGroups(nCdfId, &nSubGroups, &panSubGroupIds);
   12744         602 :     for (int i = 0; i < nSubGroups; i++)
   12745             :     {
   12746          37 :         NCDFGetCoordAndBoundVarFullNames(panSubGroupIds[i], aosVars);
   12747             :     }
   12748         565 :     CPLFree(panSubGroupIds);
   12749             : 
   12750         565 :     return CE_None;
   12751             : }
   12752             : 
   12753             : // Check if give type is user defined
   12754        2061 : bool NCDFIsUserDefinedType(int /*ncid*/, int type)
   12755             : {
   12756        2061 :     return type >= NC_FIRSTUSERTYPEID;
   12757             : }
   12758             : 
   12759         664 : char **NCDFTokenizeCoordinatesAttribute(const char *pszCoordinates)
   12760             : {
   12761             :     // CF conventions use space as the separator for variable names in the
   12762             :     // coordinates attribute, but some products such as
   12763             :     // https://sentinel.esa.int/web/sentinel/technical-guides/sentinel-3-synergy/products-algorithms/level-2-aod-algorithms-and-products/level-2-aod-products-description
   12764             :     // use comma.
   12765         664 :     return CSLTokenizeString2(pszCoordinates, ", ", 0);
   12766             : }

Generated by: LCOV version 1.14