Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netcdf.py: use tmp_path #10645

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
844 changes: 417 additions & 427 deletions autotest/gdrivers/netcdf.py

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions autotest/gdrivers/netcdf_multidim.py
Original file line number Diff line number Diff line change
Expand Up @@ -4005,9 +4005,17 @@ def test2():
rg_subset = rg.SubsetDimensionFromSelection("/x=440750")
rg_subset.OpenMDArray("Band1").GetStatistics(False, force=True)

def test3():
ds = gdal.Open(filename)
ds.SetMetadataItem("foo", "bar")

def reopen():

ds = gdal.Open(filename)
assert ds.GetMetadataItem("foo") == "bar"

aux_xml = open(filename + ".aux.xml", "rb").read().decode("UTF-8")
assert '<MDI key="foo">bar</MDI>' in aux_xml
assert (
'<DerivedDataset name="AsClassicDataset(1,0) view of Sliced view of /Band1 ([0:10,...])">'
in aux_xml
Expand Down Expand Up @@ -4053,6 +4061,7 @@ def reopen():

test()
test2()
test3()
reopen()


Expand Down
35 changes: 35 additions & 0 deletions autotest/gdrivers/s102.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
###############################################################################

import os
import shutil
import struct

import gdaltest
Expand Down Expand Up @@ -330,3 +331,37 @@ def test_s102_force_opening_no_match():

drv = gdal.IdentifyDriverEx("data/byte.tif", allowed_drivers=["S102"])
assert drv is None


###############################################################################


def test_s102_metadata_compute_stats_first(tmp_path):

out_filename = str(tmp_path / "out.h5")
shutil.copy("data/s102/test_s102_v2.1.h5", out_filename)
with gdal.Open(out_filename) as ds:
ds.GetRasterBand(1).ComputeStatistics(False)
with gdal.Open(out_filename) as ds:
assert ds.GetRasterBand(1).GetMetadataItem("STATISTICS_MINIMUM") is not None
ds.SetMetadataItem("foo", "bar")
with gdal.Open(out_filename) as ds:
assert ds.GetRasterBand(1).GetMetadataItem("STATISTICS_MINIMUM") is not None
assert ds.GetMetadataItem("foo") == "bar"


###############################################################################


def test_s102_metadata_compute_stats_after(tmp_path):

out_filename = str(tmp_path / "out.h5")
shutil.copy("data/s102/test_s102_v2.1.h5", out_filename)
with gdal.Open(out_filename) as ds:
ds.SetMetadataItem("foo", "bar")
with gdal.Open(out_filename) as ds:
assert ds.GetMetadataItem("foo") == "bar"
ds.GetRasterBand(1).ComputeStatistics(False)
with gdal.Open(out_filename) as ds:
assert ds.GetRasterBand(1).GetMetadataItem("STATISTICS_MINIMUM") is not None
assert ds.GetMetadataItem("foo") == "bar"
47 changes: 4 additions & 43 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,7 +2833,7 @@ netCDFDataset::netCDFDataset()
papszMetadata(nullptr), bBottomUp(true), eFormat(NCDF_FORMAT_NONE),
bIsGdalFile(false), bIsGdalCfFile(false), pszCFProjection(nullptr),
pszCFCoordinates(nullptr), nCFVersion(1.6), bSGSupport(false),
eMultipleLayerBehavior(SINGLE_LAYER), logCount(0), vcdf(cdfid),
eMultipleLayerBehavior(SINGLE_LAYER), logCount(0), vcdf(this, cdfid),
GeometryScribe(vcdf, this->generateLogName()),
FieldScribe(vcdf, this->generateLogName()),
bufManager(CPLGetUsablePhysicalRAM() / 5),
Expand Down Expand Up @@ -2907,7 +2907,7 @@ CPLErr netCDFDataset::Close()
if (netCDFDataset::FlushCache(true) != CE_None)
eErr = CE_Failure;

if (!SGCommitPendingTransaction())
if (GetAccess() == GA_Update && !SGCommitPendingTransaction())
eErr = CE_Failure;

for (size_t i = 0; i < apoVectorDatasets.size(); i++)
Expand Down Expand Up @@ -3033,6 +3033,7 @@ CPLErr netCDFDataset::SetMetadataItem(const char *pszName, const char *pszValue,
strchr(osName.c_str(), '#') != nullptr)
{
// do nothing
return CE_None;
}
else
{
Expand Down Expand Up @@ -3065,6 +3066,7 @@ CPLErr netCDFDataset::SetMetadata(char **papszMD, const char *pszDomain)
SetMetadataItem(pszName, pszValue);
CPLFree(pszName);
}
return CE_None;
}
return GDALPamDataset::SetMetadata(papszMD, pszDomain);
}
Expand All @@ -3081,47 +3083,6 @@ const OGRSpatialReference *netCDFDataset::GetSpatialRef() const
return GDALPamDataset::GetSpatialRef();
}

/************************************************************************/
/* SerializeToXML() */
/************************************************************************/

CPLXMLNode *netCDFDataset::SerializeToXML(const char *pszUnused)

{
// Overridden from GDALPamDataset to add only band histogram
// and statistics. See bug #4244.

if (psPam == nullptr)
return nullptr;

// Setup root node and attributes.
CPLXMLNode *psDSTree = CPLCreateXMLNode(nullptr, CXT_Element, "PAMDataset");

// Process bands.
for (int iBand = 0; iBand < GetRasterCount(); iBand++)
{
netCDFRasterBand *poBand =
static_cast<netCDFRasterBand *>(GetRasterBand(iBand + 1));

if (poBand == nullptr || !(poBand->GetMOFlags() & GMO_PAM_CLASS))
continue;

CPLXMLNode *psBandTree = poBand->SerializeToXML(pszUnused);

if (psBandTree != nullptr)
CPLAddXMLChild(psDSTree, psBandTree);
}

// We don't want to return anything if we had no metadata to attach.
if (psDSTree->psChild == nullptr)
{
CPLDestroyXMLNode(psDSTree);
psDSTree = nullptr;
}

return psDSTree;
}

/************************************************************************/
/* FetchCopyParam() */
/************************************************************************/
Expand Down
3 changes: 1 addition & 2 deletions frmts/netcdf/netcdfdataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class netCDFDataset final : public GDALPamDataset
friend class netCDFRasterBand; // TMP
friend class netCDFLayer;
friend class netCDFVariable;
friend class nccfdriver::netCDFVID;

typedef enum
{
Expand Down Expand Up @@ -543,8 +544,6 @@ class netCDFDataset final : public GDALPamDataset
void SetSpatialRefNoUpdate(const OGRSpatialReference *);

protected:
CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;

OGRLayer *ICreateLayer(const char *pszName,
const OGRGeomFieldDefn *poGeomFieldDefn,
CSLConstList papszOptions) override;
Expand Down
2 changes: 1 addition & 1 deletion frmts/netcdf/netcdflayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ netCDFLayer::netCDFLayer(netCDFDataset *poDS, int nLayerCDFId,
m_bProfileVarUnlimited(false), m_nParentIndexVarID(-1),
layerVID_alloc(poDS->cdfid == m_nLayerCDFId
? nullptr
: new nccfdriver::netCDFVID(m_nLayerCDFId)),
: new nccfdriver::netCDFVID(poDS, m_nLayerCDFId)),
layerVID(layerVID_alloc.get() == nullptr ? poDS->vcdf : *layerVID_alloc),
m_SGeometryFeatInd(0), m_poLayerConfig(nullptr),
m_layerSGDefn(poDS->cdfid, nccfdriver::OGRtoRaw(eGeomType), poDS->vcdf,
Expand Down
4 changes: 2 additions & 2 deletions frmts/netcdf/netcdfvirtual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ void netCDFVID::nc_resize_vdim(int dimid, size_t dimlen)

void netCDFVID::nc_set_define_mode()
{
NCDF_ERR(nc_redef(ncid));
m_poDS->SetDefineMode(true);
}

void netCDFVID::nc_set_data_mode()
{
NCDF_ERR(nc_enddef(ncid));
m_poDS->SetDefineMode(false);
}

void netCDFVID::nc_vmap()
Expand Down
6 changes: 5 additions & 1 deletion frmts/netcdf/netcdfvirtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "netcdfsg.h"
#include "netcdf.h"

class netCDFDataset;

// netCDF Virtual
// Provides a layer of "virtual ncID"
// that can be mapped to a real netCDF ID
Expand Down Expand Up @@ -320,6 +322,7 @@ class netCDFVVariable
*/
class netCDFVID
{
netCDFDataset *m_poDS = nullptr;
int &ncid; // ncid REF. which tracks ncID changes that may be made upstream
int dimTicket = 0;
int varTicket = 0;
Expand Down Expand Up @@ -476,7 +479,8 @@ class netCDFVID
}

// Constructor
explicit netCDFVID(int &ncid_in) : ncid(ncid_in)
explicit netCDFVID(netCDFDataset *poDS, int &ncid_in)
: m_poDS(poDS), ncid(ncid_in)
{
}
};
Expand Down
4 changes: 3 additions & 1 deletion gcore/gdalpamdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,9 @@ CPLErr GDALPamDataset::XMLInit(const CPLXMLNode *psTree, const char *pszUnused)
psIter = psIter->psNext)
{
if (psIter->eType == CXT_Element &&
strcmp(psIter->pszValue, "Array") == 0)
(strcmp(psIter->pszValue, "Array") == 0 ||
(psPam->osDerivedDatasetName.empty() &&
strcmp(psIter->pszValue, "DerivedDataset") == 0)))
{
CPLXMLNode sArrayTmp = *psIter;
sArrayTmp.psNext = nullptr;
Expand Down
Loading