Skip to content

Commit bbbebd4

Browse files
authored
fixed netCDF variable lookup for 1D raster variables (#536)
1 parent 15c1848 commit bbbebd4

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

libs/gdal/frmts/gsky_netcdf/netcdfdataset.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5222,15 +5222,19 @@ void netCDFDataset::CreateSubDatasetList( int nGroupId )
52225222
int nVarCount;
52235223
nc_inq_nvars(nGroupId, &nVarCount);
52245224

5225+
char **papszIgnoreVars = nullptr;
5226+
NCDFGetCoordAndBoundVarFullNames(cdfid, &papszIgnoreVars);
5227+
52255228
for( int nVar = 0; nVar < nVarCount; nVar++ )
52265229
{
52275230
int nDims;
52285231
nc_inq_varndims(nGroupId, nVar, &nDims);
52295232

52305233
bool isSubdataset = false;
5234+
char szTemp[NC_MAX_NAME + 1];
5235+
nc_inq_varname(nGroupId, nVar, szTemp);
5236+
52315237
if( nDims == 1) {
5232-
char szTemp[NC_MAX_NAME + 1];
5233-
nc_inq_varname(nGroupId, nVar, szTemp);
52345238
if( mainVariableId != nullptr && !strcmp(szTemp, mainVariableId) ) {
52355239
isSubdataset = true;
52365240
} else if( !NCDFIsVarLongitude(nGroupId, -1, szTemp) &&
@@ -5242,7 +5246,23 @@ void netCDFDataset::CreateSubDatasetList( int nGroupId )
52425246
isSubdataset = true;
52435247
}
52445248
} else if( nDims >= 2) {
5245-
isSubdataset = true;
5249+
char *pszVarFullName = nullptr;
5250+
CPLErr eErr = NCDFGetVarFullName(nGroupId, nVar, &pszVarFullName);
5251+
if( eErr != CE_None ) {
5252+
CPLFree(pszVarFullName);
5253+
continue;
5254+
}
5255+
bool bIgnoreVar = (CSLFindString(papszIgnoreVars, pszVarFullName)
5256+
!= -1);
5257+
CPLFree(pszVarFullName);
5258+
if( bIgnoreVar )
5259+
{
5260+
CPLDebug("GDAL_netCDF", "variable #%d [%s] was ignored", nVar,
5261+
szTemp);
5262+
continue;
5263+
}
5264+
5265+
isSubdataset = true;
52465266
}
52475267

52485268
if( isSubdataset )
@@ -5342,6 +5362,7 @@ void netCDFDataset::CreateSubDatasetList( int nGroupId )
53425362
szVarStdName, pszType));
53435363
}
53445364
}
5365+
CSLDestroy(papszIgnoreVars);
53455366

53465367
// Recurse on sub groups.
53475368
int nSubGroups = 0;
@@ -6904,6 +6925,8 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
69046925
poOpenInfo->pszFilename);
69056926
#endif
69066927

6928+
char *mainVariableId = nullptr;
6929+
69076930
// Does this appear to be a netcdf file?
69086931
NetCDFFormatEnum eTmpFormat = NCDF_FORMAT_NONE;
69096932
if( !STARTS_WITH_CI(poOpenInfo->pszFilename, "NETCDF:") )
@@ -6922,6 +6945,16 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
69226945
}
69236946
else
69246947
{
6948+
char **tokens =
6949+
CSLTokenizeString2(poOpenInfo->pszFilename,
6950+
"#", CSLT_HONOURSTRINGS|CSLT_PRESERVEESCAPES);
6951+
6952+
if( CSLCount(tokens) >= 2 )
6953+
{
6954+
poOpenInfo->pszFilename = tokens[0];
6955+
mainVariableId = tokens[1];
6956+
}
6957+
69256958
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
69266959
// We don't necessarily want to catch bugs in libnetcdf ...
69276960
if( CPLGetConfigOption("DISABLE_OPEN_REAL_NETCDF_FILES", nullptr) )
@@ -6937,6 +6970,7 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
69376970
// GDALDataset own mutex.
69386971
netCDFDataset *poDS = new netCDFDataset();
69396972
poDS->papszOpenOptions = CSLDuplicate(poOpenInfo->papszOpenOptions);
6973+
poDS->mainVariableId = mainVariableId;
69406974
CPLAcquireMutex(hNCMutex, 1000.0);
69416975

69426976
poDS->SetDescription(poOpenInfo->pszFilename);
@@ -7308,7 +7342,7 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
73087342

73097343
if( !bTreatAsSubdataset ) {
73107344
const char *subDatasetQuery = CSLFetchNameValue(poOpenInfo->papszOpenOptions, "sd_query");
7311-
if( (subDatasetQuery != nullptr && CPLTestBool(subDatasetQuery)) || nRasterVars > 1 )
7345+
if( (subDatasetQuery != nullptr && CPLTestBool(subDatasetQuery)) || nRasterVars > 0 )
73127346
{
73137347
poDS->CreateSubDatasetList(cdfid);
73147348
poDS->SetMetadata(poDS->papszMetadata);

0 commit comments

Comments
 (0)