Skip to content

Commit 13306c1

Browse files
committed
implemented array caching, refactored tests, removed separate view plugins
1 parent ec8976f commit 13306c1

16 files changed

+271
-362
lines changed

core/src/main/java/com/bc/fiduceo/reader/slstr_subset/NcCache.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.bc.fiduceo.reader.slstr_subset;
22

33
import com.bc.fiduceo.store.Store;
4+
import com.bc.fiduceo.util.NetCDFUtils;
45
import org.esa.snap.core.util.StringUtils;
56
import org.esa.snap.core.util.io.CsvReader;
7+
import ucar.ma2.Array;
68
import ucar.nc2.NetcdfFile;
79
import ucar.nc2.Variable;
810

9-
import java.awt.image.Raster;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.io.InputStreamReader;
@@ -28,7 +29,9 @@ class NcCache {
2829
private RasterInfo rasterInfo;
2930
private HashMap<String, String> dddBMap;
3031
private HashMap<String, NetcdfFile> ncFilesMap;
31-
private HashMap<String, Variable> variableMap;
32+
private HashMap<String, Variable> variablesMap;
33+
private HashMap<String, Array> rawArraysMap;
34+
private HashMap<String, Array> scaledArraysMap;
3235

3336
void open(Store store, RasterInfo rasterInfo) throws IOException {
3437
this.store = store;
@@ -37,15 +40,22 @@ void open(Store store, RasterInfo rasterInfo) throws IOException {
3740
parseDDDB();
3841

3942
ncFilesMap = new HashMap<>();
40-
variableMap = new HashMap<>();
43+
variablesMap = new HashMap<>();
44+
rawArraysMap = new HashMap<>();
45+
scaledArraysMap = new HashMap<>();
4146
}
4247

4348
void close() throws IOException {
4449
dddBMap.clear();
4550
dddBMap = null;
4651

47-
variableMap.clear();
48-
variableMap = null;
52+
rawArraysMap.clear();
53+
rawArraysMap = null;
54+
scaledArraysMap.clear();
55+
scaledArraysMap = null;
56+
57+
variablesMap.clear();
58+
variablesMap = null;
4959

5060
for (final NetcdfFile ncFile : ncFilesMap.values()) {
5161
ncFile.close();
@@ -78,7 +88,7 @@ List<String> getVariableNames() {
7888
}
7989

8090
Variable getVariable(String variableName) throws IOException {
81-
Variable variable = variableMap.get(variableName);
91+
Variable variable = variablesMap.get(variableName);
8292
if (variable != null) {
8393
return variable;
8494
} else {
@@ -98,11 +108,32 @@ Variable getVariable(String variableName) throws IOException {
98108
final double offset = rasterInfo.rasterTrackOffset - rasterInfo.tiePointTrackOffset * subSampling;
99109
variable = new SlstrSubsetTiePointVariable(variable, rasterInfo.rasterWidth, rasterInfo.rasterHeight, offset, subSampling);
100110
}
101-
variableMap.put(variableName, variable);
111+
variablesMap.put(variableName, variable);
102112
return variable;
103113
}
104114
}
105115

116+
Array getRawArray(String variableName) throws IOException {
117+
Array array = rawArraysMap.get(variableName);
118+
if (array == null) {
119+
final Variable variable = getVariable(variableName);
120+
array = variable.read();
121+
rawArraysMap.put(variableName, array);
122+
}
123+
return array;
124+
}
125+
126+
Array getScaledArray(String variableName) throws IOException {
127+
Array array = scaledArraysMap.get(variableName);
128+
if (array == null) {
129+
final Array rawArray = getRawArray(variableName);
130+
Variable variable = getVariable(variableName);
131+
array = NetCDFUtils.scaleIfNecessary(variable, rawArray);
132+
scaledArraysMap.put(variableName, array);
133+
}
134+
return array;
135+
}
136+
106137
// package access for testing only tb 2022-08-18
107138
static boolean isTiePointVariable(String variableName) {
108139
return variableName.endsWith("_tn") || variableName.endsWith("_to");

core/src/main/java/com/bc/fiduceo/reader/slstr_subset/SlstrRegriddedSubsetReader.java

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.bc.fiduceo.geometry.Polygon;
1010
import com.bc.fiduceo.location.PixelLocator;
1111
import com.bc.fiduceo.reader.*;
12+
import com.bc.fiduceo.reader.slstr.VariableType;
13+
import com.bc.fiduceo.reader.slstr.utility.Transform;
1214
import com.bc.fiduceo.reader.slstr.utility.TransformFactory;
1315
import com.bc.fiduceo.reader.snap.SNAP_PixelLocator;
1416
import com.bc.fiduceo.reader.time.TimeLocator;
@@ -37,33 +39,33 @@
3739
import java.io.File;
3840
import java.io.IOException;
3941
import java.io.StringReader;
40-
import java.util.*;
42+
import java.util.ArrayList;
43+
import java.util.Arrays;
44+
import java.util.List;
45+
import java.util.TreeSet;
4146
import java.util.regex.Matcher;
4247
import java.util.regex.Pattern;
4348
import java.util.zip.ZipFile;
4449

4550
import static com.bc.fiduceo.reader.slstr.utility.ManifestUtil.getObliqueGridOffset;
46-
import static com.bc.fiduceo.util.NetCDFUtils.scaleIfNecessary;
4751
import static ucar.ma2.DataType.INT;
4852

4953
public class SlstrRegriddedSubsetReader implements Reader {
5054

51-
private final ReaderContext _readerContext;
52-
private final boolean _nadirView;
55+
private final ReaderContext readerContext;
5356
private final String LONGITUDE_VAR_NAME = "longitude_in";
5457
private final String LATITUDE_VAR_NAME = "latitude_in";
55-
private String _manifest;
58+
private String manifestXml;
5659
private PixelLocator pixelLocator;
57-
private TimeLocator_MicrosSince2000 _timeLocator;
58-
private long[] _timeStamps2000;
60+
private TimeLocator_MicrosSince2000 timeLocator;
61+
private long[] timeStamps2000;
5962
private TransformFactory transformFactory;
6063
private final NcCache ncCache;
6164
private RasterInfo rasterInfo;
6265
private Manifest manifest;
6366

64-
public SlstrRegriddedSubsetReader(ReaderContext readerContext, boolean nadirView) {
65-
_readerContext = readerContext;
66-
_nadirView = nadirView;
67+
public SlstrRegriddedSubsetReader(ReaderContext readerContext) {
68+
this.readerContext = readerContext;
6769
ncCache = new NcCache();
6870
}
6971

@@ -82,9 +84,9 @@ public void open(File file) throws IOException {
8284

8385
try {
8486
final TreeSet<String> keyManifest = store.getKeysEndingWith("xfdumanifest.xml");
85-
_manifest = new String(store.getBytes(keyManifest.first()));
87+
manifestXml = new String(store.getBytes(keyManifest.first()));
8688

87-
final InputSource is = new InputSource(new StringReader(_manifest));
89+
final InputSource is = new InputSource(new StringReader(manifestXml));
8890
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
8991
manifest = XfduManifest.createManifest(document);
9092

@@ -108,6 +110,7 @@ public void open(File file) throws IOException {
108110
public void close() throws IOException {
109111
ncCache.close();
110112
transformFactory = null;
113+
rasterInfo = null;
111114
}
112115

113116
@Override
@@ -121,7 +124,7 @@ public AcquisitionInfo read() throws IOException {
121124
}
122125

123126
private void extractGeometries(AcquisitionInfo acquisitionInfo) throws IOException {
124-
final GeometryFactory geometryFactory = _readerContext.getGeometryFactory();
127+
final GeometryFactory geometryFactory = readerContext.getGeometryFactory();
125128
final BoundingPolygonCreator boundingPolygonCreator = new BoundingPolygonCreator(new Interval(250, 250), geometryFactory);
126129

127130
final Variable lonVariable = ncCache.getVariable(LONGITUDE_VAR_NAME);
@@ -144,9 +147,9 @@ private void extractGeometries(AcquisitionInfo acquisitionInfo) throws IOExcepti
144147

145148
private NodeType findNodeType() {
146149
final NodeType nodeType;
147-
if (_manifest.contains("groundTrackDirection")) {
150+
if (manifestXml.contains("groundTrackDirection")) {
148151
final Pattern pattern = Pattern.compile("groundTrackDirection *= *['\"]ascending['\"]");
149-
final Matcher matcher = pattern.matcher(_manifest);
152+
final Matcher matcher = pattern.matcher(manifestXml);
150153
if (matcher.find()) {
151154
nodeType = NodeType.ASCENDING;
152155
} else {
@@ -195,21 +198,21 @@ public PixelLocator getSubScenePixelLocator(Polygon sceneGeometry) throws IOExce
195198

196199
@Override
197200
public TimeLocator getTimeLocator() throws IOException {
198-
if (_timeLocator == null) {
201+
if (timeLocator == null) {
199202
ensureTimeStamps();
200-
_timeLocator = new TimeLocator_MicrosSince2000(_timeStamps2000);
203+
timeLocator = new TimeLocator_MicrosSince2000(timeStamps2000);
201204
}
202-
return _timeLocator;
205+
return timeLocator;
203206
}
204207

205208
private void ensureTimeStamps() throws IOException {
206-
if (_timeStamps2000 == null) {
209+
if (timeStamps2000 == null) {
207210
final Variable timeStampVariable = ncCache.getVariable("time_stamp_i");
208211

209212
final Array array = timeStampVariable.read();
210-
_timeStamps2000 = (long[]) array.get1DJavaArray(DataType.LONG);
213+
timeStamps2000 = (long[]) array.get1DJavaArray(DataType.LONG);
211214

212-
if (_timeStamps2000 == null) {
215+
if (timeStamps2000 == null) {
213216
throw new IOException("Unable to read time stamp data");
214217
}
215218
}
@@ -228,18 +231,27 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
228231
@Override
229232
public Array readRaw(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
230233
final Variable variable = ncCache.getVariable(variableName);
234+
final Array rawArray = ncCache.getRawArray(variableName);
235+
final Transform transform = getTransform(variableName);
231236
final Number fillValue = NetCDFUtils.getFillValue(variable);
232-
// @todo 1 tb/** this needs to be cached! Else we read the full array for every matchup. tb 2022-07-21
233-
final Array fullArray = variable.read();
234-
return RawDataReader.read(centerX, centerY, interval, fillValue, fullArray, getProductSize());
237+
238+
final int mappedX = (int) transform.mapCoordinate_X(centerX);
239+
final int mappedY = (int) transform.mapCoordinate_Y(centerY);
240+
241+
return RawDataReader.read(mappedX, mappedY, interval, fillValue, rawArray, getProductSize());
235242
}
236243

237244
@Override
238245
public Array readScaled(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
239-
final Array rawData = readRaw(centerX, centerY, interval, variableName);
240246
final Variable variable = ncCache.getVariable(variableName);
247+
final Array scaledArray = ncCache.getScaledArray(variableName);
248+
final Transform transform = getTransform(variableName);
249+
final Number fillValue = NetCDFUtils.getFillValue(variable);
241250

242-
return scaleIfNecessary(variable, rawData);
251+
final int mappedX = (int) transform.mapCoordinate_X(centerX);
252+
final int mappedY = (int) transform.mapCoordinate_Y(centerY);
253+
254+
return RawDataReader.read(mappedX, mappedY, interval, fillValue, scaledArray, getProductSize());
243255
}
244256

245257
@Override
@@ -262,10 +274,10 @@ public ArrayInt.D2 readAcquisitionTime(int x, int y, Interval interval) throws I
262274
final Index index = target.getIndex();
263275
for (int yIdx = 0; yIdx < targetHeight; yIdx++) {
264276
final int srcY = startY + yIdx;
265-
if (srcY < 0 || srcY >= _timeStamps2000.length) {
277+
if (srcY < 0 || srcY >= timeStamps2000.length) {
266278
continue;
267279
}
268-
final long lineTimeMillis = TimeUtils.millisSince2000ToUnixEpoch(_timeStamps2000[srcY]);
280+
final long lineTimeMillis = TimeUtils.millisSince2000ToUnixEpoch(timeStamps2000[srcY]);
269281
int lineTimeSeconds = (int) Math.round(lineTimeMillis * 0.001);
270282
index.set0(yIdx);
271283
for (int xIdx = 0; xIdx < targetWidth; xIdx++) {
@@ -327,11 +339,6 @@ static String extractName(String key) {
327339
}
328340
}
329341

330-
// just for testing tb 2022-07-18
331-
boolean isNadirView() {
332-
return _nadirView;
333-
}
334-
335342
static RasterInfo getRasterInfo(Manifest manifest) {
336343
final RasterInfo rasterInfo = new RasterInfo();
337344

@@ -364,6 +371,16 @@ static RasterInfo getRasterInfo(Manifest manifest) {
364371
return rasterInfo;
365372
}
366373

374+
private Transform getTransform(String variableName) {
375+
Transform transform;
376+
if (variableName.endsWith("_io")) {
377+
transform = transformFactory.get(VariableType.OBLIQUE_1km);
378+
} else {
379+
transform = transformFactory.get(VariableType.NADIR_1km);
380+
}
381+
return transform;
382+
}
383+
367384
private static int getRasterAttributeInt(MetadataElement element, String attributeName) {
368385
final MetadataAttribute spatialResolution = element.getAttribute(attributeName);
369386
final String resolutionString = spatialResolution.getData().getElemString();

core/src/main/java/com/bc/fiduceo/reader/slstr_subset/SlstrRegriddedSubsetReaderPluginNadir.java renamed to core/src/main/java/com/bc/fiduceo/reader/slstr_subset/SlstrRegriddedSubsetReaderPlugin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
import com.bc.fiduceo.reader.ReaderContext;
66
import com.bc.fiduceo.reader.ReaderPlugin;
77

8-
public class SlstrRegriddedSubsetReaderPluginNadir implements ReaderPlugin {
8+
public class SlstrRegriddedSubsetReaderPlugin implements ReaderPlugin {
99

1010
@Override
1111
public String[] getSupportedSensorKeys() {
12-
return new String[]{"slstr-s3a-uor-n", "slstr-s3b-uor-n"};
12+
return new String[]{"slstr-s3a-uor", "slstr-s3b-uor"};
1313
}
1414

1515
@Override
1616
public Reader createReader(ReaderContext readerContext) {
17-
return new SlstrRegriddedSubsetReader(readerContext, true);
17+
return new SlstrRegriddedSubsetReader(readerContext);
1818
}
1919

2020
@Override

core/src/main/java/com/bc/fiduceo/reader/slstr_subset/SlstrRegriddedSubsetReaderPluginOblique.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

core/src/main/resources/META-INF/services/com.bc.fiduceo.reader.ReaderPlugin

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ com.bc.fiduceo.reader.fiduceo_fcdr.HIRS_FCDR_ReaderPlugin
2121
com.bc.fiduceo.reader.slstr.SlstrReaderPlugin
2222
com.bc.fiduceo.reader.slstr.SlstrNRReaderPlugin
2323
com.bc.fiduceo.reader.slstr.SlstrNTReaderPlugin
24-
com.bc.fiduceo.reader.slstr_subset.SlstrRegriddedSubsetReaderPluginNadir
25-
com.bc.fiduceo.reader.slstr_subset.SlstrRegriddedSubsetReaderPluginOblique
24+
com.bc.fiduceo.reader.slstr_subset.SlstrRegriddedSubsetReaderPlugin
2625
com.bc.fiduceo.reader.insitu.sirds_sst.SirdsAnimalInsituReaderPlugin
2726
com.bc.fiduceo.reader.insitu.sirds_sst.SirdsArgoInsituReaderPlugin
2827
com.bc.fiduceo.reader.insitu.sirds_sst.SirdsArgosurfInsituReaderPlugin

0 commit comments

Comments
 (0)