Skip to content

Commit 96497bb

Browse files
Implement SmapReader.LOOK enum and use this in SmapReaderPlugins
Prevent NullPointerException in assert#DValue methods of NCTestUtils
1 parent e966b3b commit 96497bb

File tree

7 files changed

+315
-30
lines changed

7 files changed

+315
-30
lines changed

core/src/main/java/com/bc/fiduceo/reader/smap/SmapReader.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,31 @@
4141

4242
class SmapReader extends NetCDFReader {
4343

44+
/**
45+
* For detailed information about the meaning of "look" please see page 56 in the document:
46+
* https://data.remss.com/smap/SSS/V05.0/documents/SMAP_NASA_RSS_Salinity_Release_V5.0.pdf
47+
*/
48+
public enum LOOK {
49+
// Since index positions are zero based, 0 means "look 1". And "look 1" means "for look".
50+
FOR(0),
51+
// Since index positions are zero based, 1 means "look 2". And "look 2" means "aft look".
52+
AFT(1);
53+
54+
private final int index;
55+
56+
LOOK(int val) {
57+
this.index = val;
58+
}
59+
60+
public int getIndex() {
61+
return index;
62+
}
63+
}
64+
4465
private static final String DIM_NAME_X = "xdim_grid";
4566
private static final String DIM_NAME_Y = "ydim_grid";
4667
private static final String CF_FillValue = NetCDFUtils.CF_FILL_VALUE_NAME;
4768

48-
4969
// DATA FORMAT SPECIFICATION
5070
// at: https://data.remss.com/smap/SSS/V05.0/documents/SMAP_NASA_RSS_Salinity_Release_V5.0.pdf
5171
// Page 55 / Chapter: 12
@@ -102,14 +122,10 @@ class SmapReader extends NetCDFReader {
102122
* @param readerContext
103123
* @param look
104124
*/
105-
SmapReader(ReaderContext readerContext, int look) {
125+
SmapReader(ReaderContext readerContext, LOOK look) {
106126
this.geometryFactory = readerContext.getGeometryFactory();
107127

108-
if (look < 0 || look > 1) {
109-
throw new RuntimeException("look must be 0 or 1");
110-
}
111-
112-
this.lookValue = look;
128+
this.lookValue = look.getIndex();
113129
this.lookExtName = LOOKS[lookValue];
114130
}
115131

@@ -206,10 +222,8 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
206222
public Array readRaw(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
207223
final VariableInfo variableInfo = variableInfos.get(variableName);
208224
final int[] offset = variableInfo.offset;
209-
final int[] fullXYShape = new int[offset.length];
210-
Arrays.fill(fullXYShape, 1);
211-
fullXYShape[variableInfo.xDimIndex] = pWidth;
212-
fullXYShape[variableInfo.yDimIndex] = pHeight;
225+
final int[] windowShape = new int[offset.length];
226+
Arrays.fill(windowShape, 1);
213227

214228
final Variable variable = netcdfFile.findVariable(variableInfo.ncVarName);
215229

@@ -221,16 +235,16 @@ public Array readRaw(int centerX, int centerY, Interval interval, String variabl
221235
if (isWindowInside) {
222236
offset[variableInfo.xDimIndex] = offsetX;
223237
offset[variableInfo.yDimIndex] = offsetY;
224-
fullXYShape[variableInfo.xDimIndex] = winWith;
225-
fullXYShape[variableInfo.yDimIndex] = winHeight;
226-
return variable.read(offset, fullXYShape).reduce();
238+
windowShape[variableInfo.xDimIndex] = winWith;
239+
windowShape[variableInfo.yDimIndex] = winHeight;
240+
return variable.read(offset, windowShape).reduce();
227241
} else {
228242
final Rectangle2D insideWindow = RawDataReader.getInsideWindow(offsetX, offsetY, winWith, winHeight, pWidth, pHeight);
229243
offset[variableInfo.xDimIndex] = (int) insideWindow.getX();
230244
offset[variableInfo.yDimIndex] = (int) insideWindow.getY();
231-
fullXYShape[variableInfo.xDimIndex] = (int) insideWindow.getWidth();
232-
fullXYShape[variableInfo.yDimIndex] = (int) insideWindow.getHeight();
233-
final Array insideRead = variable.read(offset, fullXYShape).reduce();
245+
windowShape[variableInfo.xDimIndex] = (int) insideWindow.getWidth();
246+
windowShape[variableInfo.yDimIndex] = (int) insideWindow.getHeight();
247+
final Array insideRead = variable.read(offset, windowShape).reduce();
234248
final Array a = NetCDFUtils.create(insideRead.getDataType(), new int[]{winHeight, winWith}, variableInfo.fillValue);
235249
final int targetOffsY = Math.min(offsetY, 0);
236250
final int targetOffsX = Math.min(offsetX, 0);

core/src/main/java/com/bc/fiduceo/reader/smap/SmapReaderPluginAftLook.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.bc.fiduceo.reader.Reader;
55
import com.bc.fiduceo.reader.ReaderContext;
66
import com.bc.fiduceo.reader.ReaderPlugin;
7+
import com.bc.fiduceo.reader.smap.SmapReader.LOOK;
78

89
/**
910
* Aft look Reader Plugin for SMAP Salinity data products.
@@ -16,9 +17,7 @@ public class SmapReaderPluginAftLook implements ReaderPlugin {
1617

1718
@Override
1819
public Reader createReader(ReaderContext readerContext) {
19-
// Since index positions are zero based, 1 means "look 2". And "look 2" means "aft look".
20-
final int aftLook = 1;
21-
return new SmapReader(readerContext, aftLook);
20+
return new SmapReader(readerContext, LOOK.AFT);
2221
}
2322

2423
@Override

core/src/main/java/com/bc/fiduceo/reader/smap/SmapReaderPluginForLook.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.bc.fiduceo.reader.Reader;
55
import com.bc.fiduceo.reader.ReaderContext;
66
import com.bc.fiduceo.reader.ReaderPlugin;
7+
import com.bc.fiduceo.reader.smap.SmapReader.LOOK;
78

89
/**
910
* For look Reader Plugin for SMAP Salinity data products.
@@ -16,9 +17,7 @@ public class SmapReaderPluginForLook implements ReaderPlugin {
1617

1718
@Override
1819
public Reader createReader(ReaderContext readerContext) {
19-
// Since index positions are zero based, 0 means "look 1". And "look 1" means "for look".
20-
final int forLook = 0;
21-
return new SmapReader(readerContext, forLook);
20+
return new SmapReader(readerContext, LOOK.FOR);
2221
}
2322

2423
@Override

core/src/test/java/com/bc/fiduceo/NCTestUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,33 +208,33 @@ public static Variable getVariable(String variableName, NetcdfFile netcdfFile, b
208208
}
209209

210210
public static void assert2DValueInt(int x, int y, int expected, Variable variable) throws IOException, InvalidRangeException {
211-
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
211+
assertNotNull("NetCDF Variable expected", variable);
212212
final Array data = variable.read(new int[]{y, x}, new int[]{1, 1});
213213
assertEquals(expected, data.getInt(0));
214214
}
215215

216216
public static void assert2DValueFloat(int x, int y, float expected, Variable variable) throws IOException, InvalidRangeException {
217-
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
217+
assertNotNull("NetCDF Variable expected", variable);
218218
final Array data = variable.read(new int[]{y, x}, new int[]{1, 1});
219219
assertEquals(expected, data.getFloat(0), 1e-8);
220220
}
221221

222222
public static void assert3DValueDouble(int x, int y, int z, double expected, Variable variable) throws IOException, InvalidRangeException {
223-
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
223+
assertNotNull("NetCDF Variable expected", variable);
224224
final Array data = variable.read(new int[]{z, y, x}, new int[]{1, 1, 1});
225225
assertEquals(expected, data.getDouble(0), 1e-8);
226226
}
227227

228228
public static void assert1DValueLong(int x, long expected, Variable variable) throws IOException {
229-
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
229+
assertNotNull("NetCDF Variable expected", variable);
230230
final Array array = variable.read();
231231
final Index index = array.getIndex();
232232
index.set(x);
233233
assertEquals(expected, array.getLong(index));
234234
}
235235

236236
public static void assert3DValueLong(int x, int y, int z, long expected, Variable variable) throws IOException, InvalidRangeException {
237-
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
237+
assertNotNull("NetCDF Variable expected", variable);
238238
final Array data = variable.read(new int[]{z, y, x}, new int[]{1, 1, 1});
239239
assertEquals(expected, data.getLong(0));
240240
}

core/src/test/java/com/bc/fiduceo/reader/smap/SmapReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SmapReaderTest {
1818

1919
@Before
2020
public void setUp() {
21-
reader = new SmapReader(new ReaderContext(), 1); // empty context sufficient in this test tb 2022-11-17
21+
reader = new SmapReader(new ReaderContext(), SmapReader.LOOK.AFT); // empty context sufficient in this test tb 2022-11-17
2222
}
2323

2424
@Test

ingestion-tool/src/test/java/com/bc/fiduceo/ingest/IngestionToolIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ public void testIngest_smap_sss__for_look() throws SQLException, ParseException
11731173
IngestionToolMain.main(args);
11741174

11751175
final List<SatelliteObservation> observations = storage.get();
1176-
assertEquals(1, observations.size());
1176+
assertEquals(2, observations.size());
11771177

11781178
final SatelliteObservation observation = getSatelliteObservation("RSS_SMAP_SSS_L2C_r16092_20180204T202311_2018035_FNL_V05.0.nc", observations);
11791179
TestUtil.assertCorrectUTCDate(2018, 2, 4, 20, 23, 11, observation.getStartTime());

0 commit comments

Comments
 (0)