12
12
import org .springframework .beans .factory .annotation .Autowired ;
13
13
import org .springframework .beans .factory .annotation .Qualifier ;
14
14
import org .springframework .stereotype .Component ;
15
+ import thredds .core .DataRootManager .DataRootMatch ;
15
16
import thredds .featurecollection .FeatureCollectionCache ;
16
17
import thredds .featurecollection .InvDatasetFeatureCollection ;
17
18
import thredds .server .admin .DebugCommands ;
@@ -174,7 +175,7 @@ public NetcdfFile openNetcdfFile(HttpServletRequest req, HttpServletResponse res
174
175
}
175
176
176
177
// look for a match
177
- DataRootManager . DataRootMatch match = dataRootManager .findDataRootMatch (reqPath );
178
+ DataRootMatch match = dataRootManager .findDataRootMatch (reqPath );
178
179
179
180
// look for a feature collection dataset
180
181
if ((match != null ) && (match .dataRoot .getFeatureCollection () != null )) {
@@ -200,49 +201,12 @@ public NetcdfFile openNetcdfFile(HttpServletRequest req, HttpServletResponse res
200
201
201
202
// common case - its a file
202
203
if (match != null ) {
203
- org .jdom2 .Element netcdfElem = null ; // find ncml if it exists
204
- if (match .dataRoot != null ) {
205
- DatasetScan dscan = match .dataRoot .getDatasetScan ();
206
- // if (dscan == null) dscan = match.dataRoot.getDatasetRootProxy(); // no ncml possible in getDatasetRootProxy
207
- if (dscan != null )
208
- netcdfElem = dscan .getNcmlElement ();
209
- }
210
-
211
204
String location = dataRootManager .getLocationFromRequestPath (reqPath );
212
205
if (location == null )
213
206
throw new FileNotFoundException (reqPath );
214
207
215
- // if there's an ncml element, open it through NcMLReader, supplying the underlying file
216
- // from NetcdfFiles.open(), therefore not being cached.
217
- // This is safer given all the trouble we have with ncml and caching.
218
- if (netcdfElem != null ) {
219
- String ncmlLocation = "DatasetScan#" + location ; // LOOK some descriptive name
220
- // open with openFile(), not acquireFile, so we skip the caches
221
- NetcdfDataset ncd = null ;
222
-
223
- // look for addRecords attribute on the netcdf element. The new API in netCDF-Java does not handle this,
224
- // so we will handle it special here.
225
- Attribute addRecordsAttr = netcdfElem .getAttribute ("addRecords" );
226
- boolean addRecords = false ;
227
- if (addRecordsAttr != null ) {
228
- addRecords = Boolean .valueOf (addRecordsAttr .getValue ());
229
- }
230
-
231
- NetcdfFile ncf ;
232
- if (addRecords ) {
233
- DatasetUrl datasetUrl = DatasetUrl .findDatasetUrl (location );
234
- // work around for presence of addRecords="true" on a netcdf element
235
- ncf = NetcdfDatasets .openFile (datasetUrl , -1 , null , NetcdfFile .IOSP_MESSAGE_ADD_RECORD_STRUCTURE );
236
- } else {
237
- ncf = NetcdfDatasets .openFile (location , null );
238
- }
239
-
240
- NetcdfDataset .Builder modifiedDsBuilder = NcmlReader .mergeNcml (ncf , netcdfElem );
241
-
242
- // set new location to indicate this is a dataset from a dataset scan wrapped with NcML.
243
- modifiedDsBuilder .setLocation (ncmlLocation );
244
- ncd = modifiedDsBuilder .build ();
245
- return ncd ;
208
+ if (hasDatasetScanNcml (match )) {
209
+ return openNcmlDatasetScan (location , match );
246
210
}
247
211
248
212
DatasetUrl durl = DatasetUrl .findDatasetUrl (location );
@@ -258,6 +222,44 @@ public NetcdfFile openNetcdfFile(HttpServletRequest req, HttpServletResponse res
258
222
return ncfile ;
259
223
}
260
224
225
+ private boolean hasDatasetScanNcml (DataRootMatch match ) {
226
+ return match != null && match .dataRoot != null && match .dataRoot .getDatasetScan () != null
227
+ && match .dataRoot .getDatasetScan ().getNcmlElement () != null ;
228
+ }
229
+
230
+ private NetcdfFile openNcmlDatasetScan (String location , DataRootMatch match ) throws IOException {
231
+ org .jdom2 .Element netcdfElem = match .dataRoot .getDatasetScan ().getNcmlElement ();
232
+ // if there's an ncml element, open it through NcMLReader, supplying the underlying file
233
+ // from NetcdfFiles.open(), therefore not being cached.
234
+ // This is safer given all the trouble we have with ncml and caching.
235
+
236
+ String ncmlLocation = "DatasetScan#" + location ; // LOOK some descriptive name
237
+ // open with openFile(), not acquireFile, so we skip the caches
238
+
239
+ // look for addRecords attribute on the netcdf element. The new API in netCDF-Java does not handle this,
240
+ // so we will handle it special here.
241
+ Attribute addRecordsAttr = netcdfElem .getAttribute ("addRecords" );
242
+ boolean addRecords = false ;
243
+ if (addRecordsAttr != null ) {
244
+ addRecords = Boolean .valueOf (addRecordsAttr .getValue ());
245
+ }
246
+
247
+ NetcdfFile ncf ;
248
+ if (addRecords ) {
249
+ DatasetUrl datasetUrl = DatasetUrl .findDatasetUrl (location );
250
+ // work around for presence of addRecords="true" on a netcdf element
251
+ ncf = NetcdfDatasets .openFile (datasetUrl , -1 , null , NetcdfFile .IOSP_MESSAGE_ADD_RECORD_STRUCTURE );
252
+ } else {
253
+ ncf = NetcdfDatasets .openFile (location , null );
254
+ }
255
+
256
+ NetcdfDataset .Builder <?> modifiedDsBuilder = NcmlReader .mergeNcml (ncf , netcdfElem );
257
+
258
+ // set new location to indicate this is a dataset from a dataset scan wrapped with NcML.
259
+ modifiedDsBuilder .setLocation (ncmlLocation );
260
+ return modifiedDsBuilder .build ();
261
+ }
262
+
261
263
/**
262
264
* Open a file as a GridDataset, using getNetcdfFile(), so that it gets wrapped in NcML if needed.
263
265
*/
@@ -355,7 +357,8 @@ public FeatureDatasetPoint openPointDataset(HttpServletRequest req, HttpServletR
355
357
if (featureDatasetPoint != null ) {
356
358
return featureDatasetPoint ;
357
359
} else {
358
- throw new UnsupportedOperationException ("Could not open as a PointDataset: " + errlog );
360
+ log .error ("Could not open as a PointDataset: " + errlog );
361
+ throw new UnsupportedOperationException ("Could not open as a point dataset" );
359
362
}
360
363
} catch (Throwable t ) {
361
364
if (ncd == null )
@@ -420,8 +423,14 @@ public CoverageCollection openCoverageDataset(HttpServletRequest req, HttpServle
420
423
}
421
424
422
425
// otherwise, assume it's a local file with a datasetRoot in the urlPath.
423
- // try to open as a FeatureDatasetCoverage. This allows GRIB to be handled specially
424
426
String location = getLocationFromRequestPath (reqPath );
427
+
428
+ // Ncml in datasetScan
429
+ if (location != null && hasDatasetScanNcml (match )) {
430
+ return openCoverageFromDatasetScanNcml (location , match , reqPath );
431
+ }
432
+
433
+ // try to open as a FeatureDatasetCoverage. This allows GRIB to be handled specially
425
434
if (location != null ) {
426
435
Optional <FeatureDatasetCoverage > opt = CoverageDatasetFactory .openCoverageDataset (location );
427
436
// hack - CoverageDatasetFactory bombs out on an object store location string during the grib check,
@@ -431,12 +440,8 @@ public CoverageCollection openCoverageDataset(HttpServletRequest req, HttpServle
431
440
// and pass that to CoverageDataset
432
441
DtCoverageDataset gds = new DtCoverageDataset (NetcdfDatasets .openDataset (location ));
433
442
if (!gds .getGrids ().isEmpty ()) {
434
- Formatter errlog = new Formatter ();
435
- FeatureDatasetCoverage result = DtCoverageAdapter .factory (gds , errlog );
436
- if (result != null )
437
- opt = Optional .of (result );
438
- else
439
- opt = Optional .empty (errlog .toString ());
443
+ FeatureDatasetCoverage result = DtCoverageAdapter .factory (gds , new Formatter ());
444
+ opt = Optional .of (result );
440
445
}
441
446
}
442
447
@@ -451,6 +456,20 @@ public CoverageCollection openCoverageDataset(HttpServletRequest req, HttpServle
451
456
return null ;
452
457
}
453
458
459
+ private CoverageCollection openCoverageFromDatasetScanNcml (String location , DataRootMatch match , String reqPath )
460
+ throws IOException {
461
+ final NetcdfFile ncf = openNcmlDatasetScan (location , match );
462
+ final NetcdfDataset ncd = NetcdfDatasets .enhance (ncf , NetcdfDataset .getDefaultEnhanceMode (), null );
463
+ final DtCoverageDataset gds = new DtCoverageDataset (ncd );
464
+
465
+ if (gds .getGrids ().isEmpty ()) {
466
+ throw new FileNotFoundException ("Error opening grid dataset " + reqPath + ". err= no grids found." );
467
+ }
468
+
469
+ final FeatureDatasetCoverage coverage = DtCoverageAdapter .factory (gds , new Formatter ());
470
+ return coverage .getSingleCoverageCollection ();
471
+ }
472
+
454
473
public SimpleGeometryFeatureDataset openSimpleGeometryDataset (HttpServletRequest req , HttpServletResponse res ,
455
474
String reqPath ) throws IOException {
456
475
// first look for a feature collection
0 commit comments