Skip to content

Commit

Permalink
Merge pull request OSGeo#10728 from rouault/multidim_asclassicds_meta…
Browse files Browse the repository at this point in the history
…data

Multidim: AsClassicDataset(): make it able to retrieve dataset metadata from PAM
  • Loading branch information
rouault committed Sep 5, 2024
2 parents 0cd4fba + 4bb6d16 commit 1ab5e60
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
53 changes: 53 additions & 0 deletions autotest/gdrivers/netcdf_multidim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,9 @@ def test_netcdf_multidim_getcoordinatevariables():

def test_netcdf_multidim_getresampled_with_geoloc():

if os.path.exists("data/netcdf/sentinel5p_fake.nc.aux.xml"):
os.unlink("data/netcdf/sentinel5p_fake.nc.aux.xml")

ds = gdal.OpenEx("data/netcdf/sentinel5p_fake.nc", gdal.OF_MULTIDIM_RASTER)
rg = ds.GetRootGroup()

Expand All @@ -2118,6 +2121,8 @@ def test_netcdf_multidim_getresampled_with_geoloc():
warped_ds = gdal.Warp("", "data/netcdf/sentinel5p_fake.nc", format="MEM")
assert warped_ds.ReadRaster() == resampled_ar.Read()

assert not os.path.exists("data/netcdf/sentinel5p_fake.nc.aux.xml")


def test_netcdf_multidim_cache():

Expand Down Expand Up @@ -3575,6 +3580,9 @@ def reopen():

def test_netcdf_multidim_getresampled_with_geoloc_EMIT_L2A():

if os.path.exists("data/netcdf/fake_EMIT_L2A.nc.aux.xml"):
os.unlink("data/netcdf/fake_EMIT_L2A.nc.aux.xml")

ds = gdal.OpenEx("data/netcdf/fake_EMIT_L2A.nc", gdal.OF_MULTIDIM_RASTER)
rg = ds.GetRootGroup()

Expand Down Expand Up @@ -3770,6 +3778,8 @@ def test_netcdf_multidim_getresampled_with_geoloc_EMIT_L2A():
20.0,
)

assert not os.path.exists("data/netcdf/fake_EMIT_L2A.nc.aux.xml")


def test_netcdf_multidim_getresampled_with_geoloc_EMIT_L2A_with_good_wavelengths():

Expand Down Expand Up @@ -4116,3 +4126,46 @@ def test_netcdf_multidim_chunk_cache_options():
"CHUNK_SLOTS": "1000",
"PREEMPTION": "0.900000",
}


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


def test_netcdf_multidim_as_classic_dataset_metadata():
def set_metadata():
ds = gdal.OpenEx(
"data/netcdf/fake_EMIT_L2A_with_good_wavelengths.nc",
gdal.OF_MULTIDIM_RASTER,
)
rg = ds.GetRootGroup()
ar = rg.OpenMDArray("reflectance")
resampled_ar = ar.GetResampled(
[None, None, None], gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar is not None
classic_ds = ar.AsClassicDataset(1, 0)
classic_ds.SetMetadataItem("foo", "bar")

def check_metadata():
ds = gdal.OpenEx(
"data/netcdf/fake_EMIT_L2A_with_good_wavelengths.nc",
gdal.OF_MULTIDIM_RASTER,
)
rg = ds.GetRootGroup()
ar = rg.OpenMDArray("reflectance")
resampled_ar = ar.GetResampled(
[None, None, None], gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar is not None
classic_ds = ar.AsClassicDataset(1, 0)
assert classic_ds.GetMetadataItem("foo") == "bar"

pam_filename = "data/netcdf/fake_EMIT_L2A_with_good_wavelengths.nc.aux.xml"
if os.path.exists(pam_filename):
os.unlink(pam_filename)

set_metadata()
check_metadata()

assert os.path.exists(pam_filename)
os.unlink(pam_filename)
3 changes: 3 additions & 0 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,7 @@ void netCDFRasterBand::CreateMetadataFromOtherVars()
m_bCreateMetadataFromOtherVarsDone = true;

netCDFDataset *l_poDS = reinterpret_cast<netCDFDataset *>(poDS);
const int nPamFlagsBackup = l_poDS->nPamFlags;

// Compute all dimensions from Band number and save in Metadata.
int nd = 0;
Expand Down Expand Up @@ -2134,6 +2135,8 @@ void netCDFRasterBand::CreateMetadataFromOtherVars()

Taken += result * Sum;
} // End loop non-spatial dimensions.

l_poDS->nPamFlags = nPamFlagsBackup;
}

/************************************************************************/
Expand Down
6 changes: 6 additions & 0 deletions gcore/gdalmultidim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9500,6 +9500,12 @@ GDALDatasetFromArray *GDALDatasetFromArray::Create(
}
poDS->SetDerivedDatasetName(osDerivedDatasetName.c_str());
poDS->TryLoadXML();

for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(
CSLConstList(poDS->GDALPamDataset::GetMetadata())))
{
poDS->m_oMDD.SetMetadataItem(pszKey, pszValue);
}
}

return poDS.release();
Expand Down

0 comments on commit 1ab5e60

Please sign in to comment.