Skip to content

Commit a5e3ffc

Browse files
committed
added string variable reading
1 parent 93feef6 commit a5e3ffc

File tree

3 files changed

+137
-34
lines changed

3 files changed

+137
-34
lines changed

core/src/main/java/com/bc/fiduceo/reader/insitu/ndbc/NdbcCWReader.java

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class NdbcCWReader extends NdbcReader {
4545
private static final String LATITUDE = "latitude";
4646
private static final String BAROMETER_HEIGHT = "barometer_height";
4747
private static final String WDIR = "WDIR";
48+
public static final String LONGITUDE = "longitude";
49+
public static final String AIR_TEMP_HEIGHT = "air_temp_height";
50+
public static final String TIME = "time";
51+
public static final String GDR = "GDR";
4852
private static StationDatabase stationDatabase;
4953

5054
private ArrayList<CwRecord> records;
@@ -164,42 +168,51 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
164168

165169
@Override
166170
public Array readRaw(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
167-
if (variableName.equals(STATION_ID)) {
168-
169-
} else if (variableName.equals(STATION_TYPE)) {
170-
final StationType type = station.getType();
171-
return createResultArray(toByte(type), -1, DataType.BYTE, interval);
172-
} else if (variableName.equals(MEASUREMENT_TYPE)) {
173-
final MeasurementType measurementType = station.getMeasurementType();
174-
return createResultArray(toByte(measurementType), -1, DataType.BYTE, interval);
175-
} else if (variableName.equals(LATITUDE)) {
176-
return createResultArray(station.getLat(), Float.NaN, DataType.FLOAT, interval);
177-
} else if (variableName.equals(ANEMOMETER_HEIGHT)) {
178-
return createResultArray(station.getAnemometerHeight(), Float.NaN, DataType.FLOAT, interval);
179-
} else if (variableName.equals(BAROMETER_HEIGHT)) {
180-
return createResultArray(station.getBarometerHeight(), Float.NaN, DataType.FLOAT, interval);
181-
} else if (variableName.equals(SST_DEPTH)) {
182-
return createResultArray(station.getSSTDepth(), Float.NaN, DataType.FLOAT, interval);
183-
} else if (variableName.equals(WDIR)) {
184-
final CwRecord record = records.get(centerY);
185-
return createResultArray(record.windDir, 999, DataType.SHORT, interval);
186-
} else if (variableName.equals(WSPD)) {
187-
final CwRecord record = records.get(centerY);
188-
return createResultArray(record.windSpeed, 99.f, DataType.FLOAT, interval);
189-
} else if (variableName.equals(GST)) {
190-
final CwRecord record = records.get(centerY);
191-
return createResultArray(record.gustSpeed, 99.f, DataType.FLOAT, interval);
192-
} else if (variableName.equals(GTIME)) {
193-
final CwRecord record = records.get(centerY);
194-
return createResultArray(record.gustTime, 9999, DataType.SHORT, interval);
171+
final CwRecord record = records.get(centerY);
172+
173+
switch (variableName) {
174+
case STATION_ID:
175+
final Array resultArray = Array.factory(DataType.STRING, new int[]{1, 1});
176+
resultArray.setObject(0, station.getId());
177+
return resultArray;
178+
case STATION_TYPE:
179+
final StationType type = station.getType();
180+
return createResultArray(toByte(type), -1, DataType.BYTE, interval);
181+
case MEASUREMENT_TYPE:
182+
final MeasurementType measurementType = station.getMeasurementType();
183+
return createResultArray(toByte(measurementType), -1, DataType.BYTE, interval);
184+
case LONGITUDE:
185+
return createResultArray(station.getLon(), Float.NaN, DataType.FLOAT, interval);
186+
case LATITUDE:
187+
return createResultArray(station.getLat(), Float.NaN, DataType.FLOAT, interval);
188+
case ANEMOMETER_HEIGHT:
189+
return createResultArray(station.getAnemometerHeight(), Float.NaN, DataType.FLOAT, interval);
190+
case AIR_TEMP_HEIGHT:
191+
return createResultArray(station.getAirTemperatureHeight(), Float.NaN, DataType.FLOAT, interval);
192+
case BAROMETER_HEIGHT:
193+
return createResultArray(station.getBarometerHeight(), Float.NaN, DataType.FLOAT, interval);
194+
case SST_DEPTH:
195+
return createResultArray(station.getSSTDepth(), Float.NaN, DataType.FLOAT, interval);
196+
case TIME:
197+
return createResultArray(record.utc, NetCDFUtils.getDefaultFillValue(int.class), DataType.INT, interval);
198+
case WDIR:
199+
return createResultArray(record.windDir, 999, DataType.SHORT, interval);
200+
case WSPD:
201+
return createResultArray(record.windSpeed, 99.f, DataType.FLOAT, interval);
202+
case GST:
203+
return createResultArray(record.gustSpeed, 99.f, DataType.FLOAT, interval);
204+
case GDR:
205+
return createResultArray(record.gustDir, 999, DataType.SHORT, interval);
206+
case GTIME:
207+
return createResultArray(record.gustTime, 9999, DataType.SHORT, interval);
195208
}
196209

197210
return null;
198211
}
199212

200213
@Override
201214
public Array readScaled(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
202-
throw new RuntimeException("not implemented");
215+
return readRaw(centerX, centerY, interval, variableName); // nothing to scale here tb 2023-02-28
203216
}
204217

205218
@Override
@@ -227,7 +240,7 @@ public List<Variable> getVariables() throws InvalidRangeException, IOException {
227240
attributes.add(new Attribute(CF_UNITS_NAME, "degree_east"));
228241
attributes.add(new Attribute(CF_FILL_VALUE_NAME, NetCDFUtils.getDefaultFillValue(float.class)));
229242
attributes.add(new Attribute(CF_STANDARD_NAME, "longitude"));
230-
variables.add(new VariableProxy("longitude", DataType.FLOAT, attributes));
243+
variables.add(new VariableProxy(LONGITUDE, DataType.FLOAT, attributes));
231244

232245
attributes = new ArrayList<>();
233246
attributes.add(new Attribute(CF_UNITS_NAME, "degree_north"));
@@ -245,7 +258,7 @@ public List<Variable> getVariables() throws InvalidRangeException, IOException {
245258
attributes.add(new Attribute(CF_UNITS_NAME, "m"));
246259
attributes.add(new Attribute(CF_FILL_VALUE_NAME, Float.NaN));
247260
attributes.add(new Attribute(CF_LONG_NAME, "Height of instrument above site elevation"));
248-
variables.add(new VariableProxy("air_temp_height", DataType.FLOAT, attributes));
261+
variables.add(new VariableProxy(AIR_TEMP_HEIGHT, DataType.FLOAT, attributes));
249262

250263
attributes = new ArrayList<>();
251264
attributes.add(new Attribute(CF_UNITS_NAME, "m"));
@@ -263,7 +276,7 @@ public List<Variable> getVariables() throws InvalidRangeException, IOException {
263276
attributes.add(new Attribute(CF_UNITS_NAME, "seconds since 1970-01-01"));
264277
attributes.add(new Attribute(CF_FILL_VALUE_NAME, NetCDFUtils.getDefaultFillValue(int.class)));
265278
attributes.add(new Attribute(CF_STANDARD_NAME, "time"));
266-
variables.add(new VariableProxy("time", DataType.INT, attributes));
279+
variables.add(new VariableProxy(TIME, DataType.INT, attributes));
267280

268281
// @todo 1 tb/tb check CF standard names for the measurement data 2023-02027
269282
attributes = new ArrayList<>();
@@ -282,7 +295,7 @@ public List<Variable> getVariables() throws InvalidRangeException, IOException {
282295
attributes.add(new Attribute(CF_UNITS_NAME, "degT"));
283296
attributes.add(new Attribute(CF_FILL_VALUE_NAME, 999));
284297
attributes.add(new Attribute(CF_LONG_NAME, "Direction, in degrees clockwise from true North, of the GST, reported at the last hourly 10-minute segment."));
285-
variables.add(new VariableProxy("GDR", DataType.SHORT, attributes));
298+
variables.add(new VariableProxy(GDR, DataType.SHORT, attributes));
286299

287300
attributes = new ArrayList<>();
288301
attributes.add(new Attribute(CF_UNITS_NAME, "m/s"));

core/src/main/resources/com/bc/fiduceo/reader/insitu/ndbc/buoy_locations_sm.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,14 @@ SBIO1 | 41.629 | -82.841 | LAKE_STATION | 24.3 | 23.4
407407
SBLM4 | 43.81 | -83.72 | LAKE_STATION | 13.0 | 10.3 | 188.1 | NaN
408408
SDHN4 | 40.467 | -74.009 | COAST_STATION | 5.5 | 5.1 | 2.4 | 0.5
409409
SGNW3 | 43.749 | -87.693 | LAKE_STATION | 19.2 | 15.5 | 189.0 | 1.0
410-
SHBL1 | 29.868 | -89.673 | COAST_STATION | 15.6 | 15.0 | 8.9 | 1.0
410+
SHBL1 | 29.868 | -89.673 | COAST_STATION | 15.6 | 15.0 | 8.9 | 1.0
411+
SHPF1 | 30.058 | -84.291 | COAST_STATION | 5.7 | 11.9 | 5.4 | 0.6
412+
SHXA2 | 57.055 | -135.349 | COAST_STATION | 10.7 | 10.7 | 15.2 | NaN
413+
SIPF1 | 27.862 | -80.445 | COAST_STATION | 1.0 | 0.3 | 1.0 | 8.53
414+
SJNP4 | 18.459 | -66.116 | COAST_STATION | 5.7 | 2.8 | 3.0 | 3.8
415+
SJOM4 | 42.098 | -86.494 | LAKE_STATION | 10.0 | 3.3 | 184.1 | NaN
416+
SJSN4 | 39.305 | -75.377 | COAST_STATION | 14.8 | 3.0 | 7.4 | 0.67
417+
SMKF1 | 24.628 | -81.109 | OCEAN_STATION | 7.2 | 7.0 | 6.0 | NaN
418+
SPGF1 | 26.704 | -78.995 | COAST_STATION | 6.6 | 6.1 | 2.7 | NaN
419+
SPLL1 | 26.704 | -78.995 | OCEAN_STATION | 10.0 | 10.0 | 3.0 | 1.0
420+
SRLM4 | 45.773 | -84.137 | LAKE_STATION | 30.0 | NaN | NaN | NaN

core/src/test/java/com/bc/fiduceo/reader/insitu/ndbc/NdbcCWReader_IO_Test.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,86 @@ public void testReadRaw_1x1_oceanBuoy() throws InvalidRangeException, IOExceptio
223223
}
224224
}
225225

226+
@Test
227+
public void testReadScaled_1x1_oceanBuoy() throws InvalidRangeException, IOException {
228+
final File testFile = getOCEAN_BUOY();
229+
230+
try {
231+
reader.open(testFile);
232+
233+
Array array = reader.readScaled(13, 11, new Interval(1, 1), "air_temp_height");
234+
assertEquals(DataType.FLOAT, array.getDataType());
235+
assertEquals(Float.NaN, array.getFloat(0), 1e-8);
236+
237+
array = reader.readRaw(14, 12, new Interval(1, 1), "time");
238+
assertEquals(DataType.INT, array.getDataType());
239+
assertEquals(1464742800, array.getInt(0));
240+
241+
array = reader.readRaw(15, 13, new Interval(1, 1), "GDR");
242+
assertEquals(DataType.SHORT, array.getDataType());
243+
assertEquals(999, array.getShort(0));
244+
} finally {
245+
reader.close();
246+
}
247+
}
248+
249+
@Test
250+
public void testReadScaled_1x1_lakeBuoy() throws InvalidRangeException, IOException {
251+
final File testFile = getLAKE_BUOY();
252+
253+
try {
254+
reader.open(testFile);
255+
256+
Array array = reader.readScaled(13, 14, new Interval(1, 1), "barometer_height");
257+
assertEquals(DataType.FLOAT, array.getDataType());
258+
assertEquals(Float.NaN, array.getFloat(0), 1e-8);
259+
260+
array = reader.readRaw(14, 15, new Interval(1, 1), "WDIR");
261+
assertEquals(DataType.SHORT, array.getDataType());
262+
assertEquals(24, array.getShort(0));
263+
264+
array = reader.readRaw(15, 16, new Interval(1, 1), "GST");
265+
assertEquals(DataType.FLOAT, array.getDataType());
266+
assertEquals(99.f, array.getFloat(0), 1e-8);
267+
} finally {
268+
reader.close();
269+
}
270+
}
271+
272+
@Test
273+
public void testReadRaw_3x3_lakeBuoy() throws InvalidRangeException, IOException {
274+
final File testFile = getLAKE_BUOY();
275+
276+
try {
277+
reader.open(testFile);
278+
279+
final Array array = reader.readRaw(7, 4, new Interval(3, 3), "longitude");
280+
assertEquals(DataType.FLOAT, array.getDataType());
281+
assertEquals(Float.NaN, array.getFloat(0), 1e-8);
282+
assertEquals(Float.NaN, array.getFloat(2), 1e-8);
283+
assertEquals(-87.793f, array.getFloat(4), 1e-8);
284+
assertEquals(Float.NaN, array.getFloat(6), 1e-8);
285+
assertEquals(Float.NaN, array.getFloat(8), 1e-8);
286+
} finally {
287+
reader.close();
288+
}
289+
}
290+
291+
@Test
292+
public void testReadRaw_String() throws IOException, InvalidRangeException {
293+
final File testFile = getOCEAN_BUOY();
294+
295+
try {
296+
reader.open(testFile);
297+
298+
final Array array = reader.readRaw(7, 4, new Interval(3, 3), "station_id");
299+
assertEquals(DataType.STRING, array.getDataType());
300+
assertEquals("42002", array.getObject(0));
301+
} finally {
302+
reader.close();
303+
}
304+
}
305+
226306
private static File getOCEAN_BUOY() throws IOException {
227307
final String relativePath = TestUtil.assembleFileSystemPath(new String[]{"insitu", "ndbc", "ndbc-cw-ob", "v1", "2016", "42002c2016.txt"}, false);
228308
return TestUtil.getTestDataFileAsserted(relativePath);

0 commit comments

Comments
 (0)