@@ -5222,15 +5222,19 @@ void netCDFDataset::CreateSubDatasetList( int nGroupId )
5222
5222
int nVarCount;
5223
5223
nc_inq_nvars (nGroupId, &nVarCount);
5224
5224
5225
+ char **papszIgnoreVars = nullptr ;
5226
+ NCDFGetCoordAndBoundVarFullNames (cdfid, &papszIgnoreVars);
5227
+
5225
5228
for ( int nVar = 0 ; nVar < nVarCount; nVar++ )
5226
5229
{
5227
5230
int nDims;
5228
5231
nc_inq_varndims (nGroupId, nVar, &nDims);
5229
5232
5230
5233
bool isSubdataset = false ;
5234
+ char szTemp[NC_MAX_NAME + 1 ];
5235
+ nc_inq_varname (nGroupId, nVar, szTemp);
5236
+
5231
5237
if ( nDims == 1 ) {
5232
- char szTemp[NC_MAX_NAME + 1 ];
5233
- nc_inq_varname (nGroupId, nVar, szTemp);
5234
5238
if ( mainVariableId != nullptr && !strcmp (szTemp, mainVariableId) ) {
5235
5239
isSubdataset = true ;
5236
5240
} else if ( !NCDFIsVarLongitude (nGroupId, -1 , szTemp) &&
@@ -5242,7 +5246,23 @@ void netCDFDataset::CreateSubDatasetList( int nGroupId )
5242
5246
isSubdataset = true ;
5243
5247
}
5244
5248
} 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 ;
5246
5266
}
5247
5267
5248
5268
if ( isSubdataset )
@@ -5342,6 +5362,7 @@ void netCDFDataset::CreateSubDatasetList( int nGroupId )
5342
5362
szVarStdName, pszType));
5343
5363
}
5344
5364
}
5365
+ CSLDestroy (papszIgnoreVars);
5345
5366
5346
5367
// Recurse on sub groups.
5347
5368
int nSubGroups = 0 ;
@@ -6904,6 +6925,8 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
6904
6925
poOpenInfo->pszFilename );
6905
6926
#endif
6906
6927
6928
+ char *mainVariableId = nullptr ;
6929
+
6907
6930
// Does this appear to be a netcdf file?
6908
6931
NetCDFFormatEnum eTmpFormat = NCDF_FORMAT_NONE;
6909
6932
if ( !STARTS_WITH_CI (poOpenInfo->pszFilename , " NETCDF:" ) )
@@ -6922,6 +6945,16 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
6922
6945
}
6923
6946
else
6924
6947
{
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
+
6925
6958
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
6926
6959
// We don't necessarily want to catch bugs in libnetcdf ...
6927
6960
if ( CPLGetConfigOption (" DISABLE_OPEN_REAL_NETCDF_FILES" , nullptr ) )
@@ -6937,6 +6970,7 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
6937
6970
// GDALDataset own mutex.
6938
6971
netCDFDataset *poDS = new netCDFDataset ();
6939
6972
poDS->papszOpenOptions = CSLDuplicate (poOpenInfo->papszOpenOptions );
6973
+ poDS->mainVariableId = mainVariableId;
6940
6974
CPLAcquireMutex (hNCMutex, 1000.0 );
6941
6975
6942
6976
poDS->SetDescription (poOpenInfo->pszFilename );
@@ -7308,7 +7342,7 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
7308
7342
7309
7343
if ( !bTreatAsSubdataset ) {
7310
7344
const char *subDatasetQuery = CSLFetchNameValue (poOpenInfo->papszOpenOptions , " sd_query" );
7311
- if ( (subDatasetQuery != nullptr && CPLTestBool (subDatasetQuery)) || nRasterVars > 1 )
7345
+ if ( (subDatasetQuery != nullptr && CPLTestBool (subDatasetQuery)) || nRasterVars > 0 )
7312
7346
{
7313
7347
poDS->CreateSubDatasetList (cdfid);
7314
7348
poDS->SetMetadata (poDS->papszMetadata );
0 commit comments