File dsp =TestData.file(this,"datastore.properties");
FileUtils.copyFileToDirectory(dsp, mosaic);
// have the reader harvest it
ImageMosaicFormat format = new ImageMosaicFormat();
ImageMosaicReader reader = format.getReader(mosaic);
SimpleFeatureIterator it = null;
assertNotNull(reader);
try {
String[] names = reader.getGridCoverageNames();
assertEquals(1, names.length);
assertEquals("O3", names[0]);
// check we have the two granules we expect
GranuleSource source = reader.getGranules("O3", true);
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
Query q = new Query(Query.ALL);
q.setSortBy(new SortBy[] {ff.sort("time", SortOrder.ASCENDING)});
SimpleFeatureCollection granules = source.getGranules(q);
assertEquals(2, granules.size());
it = granules.features();
assertTrue(it.hasNext());
SimpleFeature f = it.next();
assertEquals("polyphemus_20130301_test.nc", f.getAttribute("location"));
assertEquals(0, f.getAttribute("imageindex"));
assertEquals("2013-03-01T00:00:00.000Z", ConvertersHack.convert(f.getAttribute("time"), String.class));
assertTrue(it.hasNext());
f = it.next();
assertEquals("polyphemus_20130301_test.nc", f.getAttribute("location"));
assertEquals(1, f.getAttribute("imageindex"));
assertEquals("2013-03-01T01:00:00.000Z", ConvertersHack.convert(f.getAttribute("time"), String.class));
it.close();
// close the reader and re-open it
reader.dispose();
reader = format.getReader(mosaic);
source = reader.getGranules("O3", true);
// wait a bit, we have to make sure the old indexes are recognized as old
Thread.sleep(1000);
// now replace the netcdf file with a more up to date version of the same
File nc2 = TestData.file(this,"polyphemus_20130301_test_more_times.nc");
File target = new File(mosaic, "polyphemus_20130301_test.nc");
FileUtils.copyFile(nc2, target, false);
File fileToHarvest = new File(mosaic, "polyphemus_20130301_test.nc");
List<HarvestedSource> harvestSummary = reader.harvest(null, fileToHarvest, null);
assertEquals(1, harvestSummary.size());
HarvestedSource hf = harvestSummary.get(0);
assertEquals("polyphemus_20130301_test.nc", ((File) hf.getSource()).getName());
assertTrue(hf.success());
assertEquals(1, reader.getGridCoverageNames().length);
// check that we have four times now
source = reader.getGranules("O3", true);
granules = source.getGranules(q);
assertEquals(4, granules.size());
it = granules.features();
f = it.next();
assertEquals("polyphemus_20130301_test.nc", f.getAttribute("location"));
assertEquals(0, f.getAttribute("imageindex"));
assertEquals("2013-03-01T00:00:00.000Z", ConvertersHack.convert(f.getAttribute("time"), String.class));
assertTrue(it.hasNext());
f = it.next();
assertEquals("polyphemus_20130301_test.nc", f.getAttribute("location"));
assertEquals(1, f.getAttribute("imageindex"));
assertEquals("2013-03-01T01:00:00.000Z", ConvertersHack.convert(f.getAttribute("time"), String.class));
f = it.next();
assertEquals("polyphemus_20130301_test.nc", f.getAttribute("location"));
assertEquals(2, f.getAttribute("imageindex"));
assertEquals("2013-03-01T02:00:00.000Z", ConvertersHack.convert(f.getAttribute("time"), String.class));
assertTrue(it.hasNext());
f = it.next();
assertEquals("polyphemus_20130301_test.nc", f.getAttribute("location"));
assertEquals(3, f.getAttribute("imageindex"));
assertEquals("2013-03-01T03:00:00.000Z", ConvertersHack.convert(f.getAttribute("time"), String.class));
it.close();
} finally {
if(it != null) {
it.close();
}
reader.dispose();
}
}