Skip to content

Commit c5b7f5d

Browse files
committed
intermediate adding tests and refactoring
1 parent 35be911 commit c5b7f5d

File tree

6 files changed

+314
-61
lines changed

6 files changed

+314
-61
lines changed

core/src/main/java/com/bc/fiduceo/reader/slstr/SlstrReader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import com.bc.fiduceo.reader.AcquisitionInfo;
99
import com.bc.fiduceo.reader.ReaderContext;
1010
import com.bc.fiduceo.reader.ReaderUtils;
11-
import com.bc.fiduceo.reader.time.TimeLocator;
1211
import com.bc.fiduceo.reader.snap.SNAP_Reader;
1312
import com.bc.fiduceo.reader.snap.VariableProxy;
13+
import com.bc.fiduceo.reader.time.TimeLocator;
1414
import com.bc.fiduceo.reader.time.TimeLocator_MicrosSince2000;
1515
import com.bc.fiduceo.util.NetCDFUtils;
1616
import com.bc.fiduceo.util.TimeUtils;
@@ -20,7 +20,6 @@
2020
import ucar.ma2.Array;
2121
import ucar.ma2.ArrayInt;
2222
import ucar.ma2.DataType;
23-
import ucar.ma2.Index;
2423
import ucar.nc2.Variable;
2524

2625
import java.io.File;
@@ -213,7 +212,6 @@ public Array readRaw(int centerX, int centerY, Interval interval, String variabl
213212

214213
final VariableType variableType = variableNames.getVariableType(variableName);
215214
final Transform transform = transformFactory.get(variableType);
216-
final Dimension rasterSize = transform.getRasterSize();
217215

218216
final RasterDataNode dataNode = getRasterDataNode(variableName);
219217

@@ -225,7 +223,6 @@ public Array readRaw(int centerX, int centerY, Interval interval, String variabl
225223
final int height = mappedInterval.getY();
226224
final int[] shape = getShape(mappedInterval);
227225
final Array readArray = Array.factory(targetDataType, shape);
228-
final Array targetArray = NetCDFUtils.create(targetDataType, shape, noDataValue);
229226

230227
final int mappedX = (int) (transform.mapCoordinate_X(centerX) + 0.5);
231228
final int mappedY = (int) (transform.mapCoordinate_Y(centerY) + 0.5);

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

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.esa.snap.core.util.StringUtils;
2222
import ucar.ma2.DataType;
2323
import ucar.ma2.*;
24-
import ucar.nc2.Attribute;
2524
import ucar.nc2.NetcdfFile;
2625
import ucar.nc2.Variable;
2726

@@ -33,7 +32,7 @@
3332
import java.util.regex.Pattern;
3433
import java.util.zip.ZipFile;
3534

36-
import static com.bc.fiduceo.util.NetCDFUtils.*;
35+
import static com.bc.fiduceo.util.NetCDFUtils.scaleIfNecessary;
3736
import static ucar.ma2.DataType.INT;
3837
import static ucar.nc2.NetcdfFiles.openInMemory;
3938

@@ -48,7 +47,6 @@ public class SlstrRegriddedSubsetReader implements Reader {
4847
private HashMap<String, Variable> _variablesLUT;
4948
private String _manifest;
5049
private PixelLocator pixelLocator;
51-
private BoundingPolygonCreator boundingPolygonCreator;
5250
private TimeLocator_MicrosSince2000 _timeLocator;
5351
private long[] _timeStamps2000;
5452

@@ -101,7 +99,7 @@ public AcquisitionInfo read() throws IOException {
10199

102100
private void extractGeometries(AcquisitionInfo acquisitionInfo) throws IOException {
103101
final GeometryFactory geometryFactory = _readerContext.getGeometryFactory();
104-
boundingPolygonCreator = new BoundingPolygonCreator(new Interval(250, 250), geometryFactory);
102+
final BoundingPolygonCreator boundingPolygonCreator = new BoundingPolygonCreator(new Interval(250, 250), geometryFactory);
105103
final Array longitude = _variablesLUT.get(LONGITUDE_VAR_NAME).read();
106104
final Array latitude = _variablesLUT.get(LATITUDE_VAR_NAME).read();
107105
final Geometry boundingGeometry = boundingPolygonCreator.createBoundingGeometry(longitude, latitude);
@@ -159,13 +157,7 @@ public PixelLocator getPixelLocator() throws IOException {
159157

160158
@Override
161159
public PixelLocator getSubScenePixelLocator(Polygon sceneGeometry) throws IOException {
162-
final Dimension productSize = getProductSize();
163-
final int height = productSize.getNy();
164-
final int width = productSize.getNx();
165-
final int subsetHeight = boundingPolygonCreator.getSubsetHeight(height, 250);
166-
final PixelLocator pixelLocator = getPixelLocator();
167-
168-
return PixelLocatorFactory.getSubScenePixelLocator(sceneGeometry, width, height, subsetHeight, pixelLocator);
160+
return getPixelLocator();
169161
}
170162

171163
@Override
@@ -207,27 +199,18 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
207199
@Override
208200
public Array readRaw(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
209201
final Variable variable = _variablesLUT.get(variableName);
210-
final Number fillValue = findAttributeSafe(CF_FILL_VALUE_NAME, variable).getNumericValue();
211-
if (fillValue == null) {
212-
throw new IOException("The attribute '" + CF_FILL_VALUE_NAME + "' of variable '" + variableName + "' does not contain a numeric value.");
213-
}
202+
final Number fillValue = NetCDFUtils.getFillValue(variable);
203+
// @todo 1 tb/** this needs to be cached! Else we read the full array for every matchup. tb 2022-07-21
214204
final Array fullArray = variable.read();
215-
final Array windowArray = RawDataReader.read(centerX, centerY, interval, fillValue, fullArray, getProductSize());
216-
return windowArray.reshape(windowArray.getShape());
205+
return RawDataReader.read(centerX, centerY, interval, fillValue, fullArray, getProductSize());
217206
}
218207

219208
@Override
220209
public Array readScaled(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
221210
final Array rawData = readRaw(centerX, centerY, interval, variableName);
222211
final Variable variable = _variablesLUT.get(variableName);
223-
final double scaleFactor = variable.attributes().findAttributeDouble(CF_SCALE_FACTOR_NAME, 1.0);
224-
final double offset = variable.attributes().findAttributeDouble(CF_ADD_OFFSET_NAME, 0.0);
225-
if (ReaderUtils.mustScale(scaleFactor, offset)) {
226-
final MAMath.ScaleOffset scaleOffset = new MAMath.ScaleOffset(scaleFactor, offset);
227-
return MAMath.convert2Unpacked(rawData, scaleOffset);
228-
}
229212

230-
return rawData;
213+
return scaleIfNecessary(variable, rawData);
231214
}
232215

233216
@Override
@@ -305,24 +288,20 @@ private void initVariables() throws IOException {
305288
final Variable fullSizeVar = _variables.get(0);
306289
final NetcdfFile fullSizeNcFile = fullSizeVar.getNetcdfFile();
307290
assert fullSizeNcFile != null;
308-
final Number fullTrackOffset = findGlobalAttributeSafe("track_offset", fullSizeNcFile).getNumericValue();
309-
assert fullTrackOffset != null;
310-
final String fullAcrossResStr = findGlobalAttributeSafe("resolution", fullSizeNcFile).getStringValue();
311-
assert fullAcrossResStr != null;
291+
final double fullTrackOffset = NetCDFUtils.getGlobalAttributeDouble("track_offset", fullSizeNcFile);
292+
final String fullAcrossResStr = NetCDFUtils.getGlobalAttributeString("resolution", fullSizeNcFile);
312293
final Number fullAcrossRes = Double.parseDouble(StringUtils.split(fullAcrossResStr, " ".toCharArray(), true)[1]);
313294

314295
for (Map.Entry<int[], ArrayList<Variable>> entry : shapeGroupedVariablesMap.entrySet()) {
315296
final ArrayList<Variable> vars = entry.getValue();
316297
for (Variable var : vars) {
317298
final NetcdfFile varNcFile = var.getNetcdfFile();
318299
assert varNcFile != null;
319-
final Number varTrackOffset = findGlobalAttributeSafe("track_offset", varNcFile).getNumericValue();
320-
assert varTrackOffset != null;
321-
final String varAcrossResStr = findGlobalAttributeSafe("resolution", varNcFile).getStringValue();
322-
assert varAcrossResStr != null;
300+
final double varTrackOffset = NetCDFUtils.getGlobalAttributeDouble("track_offset", varNcFile);
301+
final String varAcrossResStr = NetCDFUtils.getGlobalAttributeString("resolution", varNcFile);
323302
final Number varAcrossRes = Double.parseDouble(StringUtils.split(varAcrossResStr, " ".toCharArray(), true)[1]);
324303
final double subsamplingX = varAcrossRes.doubleValue() / fullAcrossRes.doubleValue();
325-
final double offsetX = fullTrackOffset.doubleValue() - varTrackOffset.doubleValue() * subsamplingX;
304+
final double offsetX = fullTrackOffset - varTrackOffset * subsamplingX;
326305
_variables.add(
327306
new SlstrSubsetTiePointVariable(var, fullSizeShape[1], fullSizeShape[0], offsetX, subsamplingX)
328307
);
@@ -413,21 +392,4 @@ static String extractName(String key) {
413392
boolean isNadirView() {
414393
return _nadirView;
415394
}
416-
417-
@SuppressWarnings("SameParameterValue")
418-
private Attribute findAttributeSafe(String attributeName, Variable variable) throws IOException {
419-
final Attribute attribute = variable.findAttribute(attributeName);
420-
if (attribute == null) {
421-
throw new IOException("Attribute '" + attributeName + "' is expected in variable '" + variable.getShortName() + "'");
422-
}
423-
return attribute;
424-
}
425-
426-
private Attribute findGlobalAttributeSafe(String attributeName, NetcdfFile netcdfFile) throws IOException {
427-
final Attribute attribute = netcdfFile.findGlobalAttribute(attributeName);
428-
if (attribute == null) {
429-
throw new IOException("Global attribute '" + attributeName + "' is expected in netcdf file '" + netcdfFile.getLocation() + "'");
430-
}
431-
return attribute;
432-
}
433395
}

core/src/main/java/com/bc/fiduceo/util/NetCDFUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ public static int getGlobalAttributeInt(String attributeName, NetcdfFile netcdfF
171171
return attribute.getNumericValue().intValue();
172172
}
173173

174+
public static double getGlobalAttributeDouble(String attributeName, NetcdfFile netcdfFile) {
175+
final Attribute attribute = getGlobalAttributeSafe(attributeName, netcdfFile);
176+
return attribute.getNumericValue().doubleValue();
177+
}
178+
174179
public static int getAttributeInt(Variable variable, String name, int defaultValue) {
175180
final Attribute attribute = variable.findAttribute(name);
176181
if (attribute == null) {
@@ -343,9 +348,9 @@ public static Array getCenterPosArrayFromMMDFile(NetcdfFile netcdfFile, String v
343348
return array;
344349
}
345350

346-
public static Array readAndScaleIfNecessary(Variable angleVariable) throws IOException {
347-
final Array longitudes = angleVariable.read();
348-
return scaleIfNecessary(angleVariable, longitudes);
351+
public static Array readAndScaleIfNecessary(Variable variable) throws IOException {
352+
final Array dataArray = variable.read();
353+
return scaleIfNecessary(variable, dataArray);
349354
}
350355

351356
public static Array scaleIfNecessary(Variable variable, Array array) {

core/src/test/java/com/bc/fiduceo/reader/slstr/SlstrReader_IO_Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ public void testReadRaw_S3A_1km_nadir_left() throws IOException {
410410
reader.open(file);
411411

412412
final Interval interval = new Interval(5, 5);
413-
final Array array = reader.readScaled(0, 318, interval, "S7_exception_in");
413+
final Array array = reader.readRaw(0, 318, interval, "S7_exception_in");
414414
NCTestUtils.assertValueAt(-1, 0, 1, array);
415415
NCTestUtils.assertValueAt(-1, 1, 1, array);
416-
NCTestUtils.assertValueAt(128, 2, 1, array);
417-
NCTestUtils.assertValueAt(128, 3, 1, array);
418-
NCTestUtils.assertValueAt(128, 4, 1, array);
416+
NCTestUtils.assertValueAt(-128, 2, 1, array);
417+
NCTestUtils.assertValueAt(-128, 3, 1, array);
418+
NCTestUtils.assertValueAt(-128, 4, 1, array);
419419
} finally {
420420
reader.close();
421421
}

0 commit comments

Comments
 (0)