Skip to content

Commit ffe122f

Browse files
committed
intermediate
1 parent 88aaedf commit ffe122f

File tree

6 files changed

+165
-2
lines changed

6 files changed

+165
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.bc.fiduceo.reader.insitu.ndbc;
2+
3+
class Station {
4+
5+
private final String id;
6+
private float lon;
7+
private float lat;
8+
private float anHeight;
9+
private StationType type;
10+
11+
Station(String id) {
12+
this.id = id;
13+
14+
lon = Float.NaN;
15+
lat = Float.NaN;
16+
anHeight = Float.NaN;
17+
}
18+
19+
String getId() {
20+
return id;
21+
}
22+
23+
float getLon() {
24+
return lon;
25+
}
26+
27+
float getLat() {
28+
return lat;
29+
}
30+
31+
public StationType getType() {
32+
return type;
33+
}
34+
35+
public float getAnemometerHeight() {
36+
return anHeight;
37+
}
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.bc.fiduceo.reader.insitu.ndbc;
2+
3+
import org.esa.snap.core.util.StringUtils;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
12+
class StationDatabase {
13+
14+
private static final int CW_STATION_TOKENS = 5;
15+
private static final int SM_STATION_TOKENS = 8;
16+
17+
private final HashMap<String, Station> stationMap;
18+
19+
StationDatabase() {
20+
stationMap = new HashMap<>();
21+
}
22+
23+
void load(InputStream inputStream) throws IOException {
24+
final ArrayList<String> lineList = readInputLines(inputStream);
25+
26+
final String firstLine = lineList.get(0);
27+
final String[] tokens = StringUtils.split(firstLine, new char[]{'|'}, false);
28+
if (tokens.length == CW_STATION_TOKENS) {
29+
parseCwStation(lineList);
30+
} else if (tokens.length == SM_STATION_TOKENS) {
31+
parseSmStations(lineList);
32+
} else {
33+
throw new IllegalArgumentException("unknown input data format");
34+
}
35+
36+
}
37+
38+
private void parseSmStations(ArrayList<String> lineList) {
39+
40+
}
41+
42+
private void parseCwStation(ArrayList<String> lineList) {
43+
44+
}
45+
46+
private static ArrayList<String> readInputLines(InputStream inputStream) throws IOException {
47+
final ArrayList<String> lineList = new ArrayList<>();
48+
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
49+
String line;
50+
while ((line = reader.readLine()) != null) {
51+
if (line.startsWith("#")) {
52+
// skip comment lines tb 2022-11-03
53+
continue;
54+
}
55+
lineList.add(line);
56+
}
57+
return lineList;
58+
}
59+
60+
Station get(String id) {
61+
return new Station(id);
62+
}
63+
}

core/src/main/java/com/bc/fiduceo/reader/insitu/ndbc/InstrumentType.java renamed to core/src/main/java/com/bc/fiduceo/reader/insitu/ndbc/StationType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bc.fiduceo.reader.insitu.ndbc;
22

3-
enum InstrumentType {
3+
enum StationType {
44
OCEAN_BUOY, // > 5km distance to land
55
COAST_BUOY, // <= 5km distance to land
66
LAKE_BUOY, // inland water

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,13 @@ OLCN6 | 43.341 | -78.719 | LAKE_STATION | 10.0 | 8.0
370370
OLSA2 | 52.94 | -168.87 | COAST_STATION | 8.4 | 7.8 | 7.3 | NaN
371371
ORIN7 | 35.796 | -75.548 | COAST_STATION | 6.7 | 5.2 | 4.1 | 0.8
372372
OSGN6 | 43.464 | -76.511 | LAKE_STATION | 14.9 | 10.5 | 78.2 | NaN
373-
PCBF1 | 30.213 | -85.88 | COAST_STATION | 8.6 | 3.3 | 9.1 | NaN
373+
PCBF1 | 30.213 | -85.88 | COAST_STATION | 8.6 | 3.3 | 9.1 | NaN
374+
PCLM4 | 47.276 | -88.528 | LAKE_STATION | 10.0 | 2.7 | 198.4 | NaN
375+
PCLM4 | 33.735 | -118.241 | COAST_STATION | 10.7 | NaN | NaN | NaN
376+
PFXC1 | 33.747 | -118.215 | COAST_STATION | 22.9 | 22.9 | NaN | NaN
377+
PGBP7 | 13.428 | 144.796 | COAST_STATION | 14.3 | 10.5 | NaN | NaN
378+
PILA2 | 59.742 | -149.47 | COAST_STATION | 6.6 | 6.4 | 25.0 | NaN
379+
PILM4 | 48.223 | -88.366 | LAKE_STATION | 15.5 | 9.8 | 230.4 | NaN
380+
PLSF1 | 24.693 | -82.773 | OCEAN_STATION | 17.7 | 17.4 | 15.8 | 1.5
381+
PLXA2 | 56.247 | -134.647 | COAST_STATION | 8.5 | 7.9 | 5.6 | 4.0
382+
PMOA2 | 55.99 | -160.562 | COAST_STATION | 13.2 | 7.3 | 5.4 | 1.9
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bc.fiduceo.reader.insitu.ndbc;
2+
3+
import org.junit.Test;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class StationDatabaseTest {
11+
12+
@Test
13+
public void testLoadAndGet_CW() throws IOException {
14+
final String resourceFile = "# buoy ID | latitude | longitude | type | anemometer height\n" +
15+
"41002 | 31.759 | -74.936 | OCEAN_BUOY | 4.1\n" +
16+
"41004 | 32.502 | -79.099 | OCEAN_BUOY | 4.1\n" +
17+
"41008 | 31.4 | -80.866 | OCEAN_BUOY | 4.9\n" +
18+
"41010 | 28.878 | -78.485 | OCEAN_BUOY | 4.1";
19+
20+
final ByteArrayInputStream inputStream = new ByteArrayInputStream(resourceFile.getBytes());
21+
22+
final StationDatabase stationDatabase = new StationDatabase();
23+
stationDatabase.load(inputStream);
24+
25+
Station station = stationDatabase.get("41002");
26+
assertEquals("41002", station.getId());
27+
// @todo 1 tb/tb continue here 2023-02-22
28+
//assertEquals(31.759f, station.getLat(), 1e-8);
29+
30+
station = stationDatabase.get("41004");
31+
assertEquals("41004", station.getId());
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bc.fiduceo.reader.insitu.ndbc;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNull;
7+
8+
public class StationTest {
9+
10+
@Test
11+
public void testConstruction() {
12+
final Station station = new Station("abgdzt");
13+
14+
assertEquals("abgdzt", station.getId());
15+
assertEquals(Float.NaN, station.getLon(), 1e-8);
16+
assertEquals(Float.NaN, station.getLat(), 1e-8);
17+
assertNull(station.getType());
18+
assertEquals(Float.NaN, station.getAnemometerHeight(), 1e-8);
19+
}
20+
}

0 commit comments

Comments
 (0)