Skip to content

Commit d76d133

Browse files
committed
intermediate sirds reader
1 parent 58d65fd commit d76d133

File tree

7 files changed

+373
-12
lines changed

7 files changed

+373
-12
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.bc.fiduceo.reader.insitu.sirds_sst;
2+
3+
import com.bc.fiduceo.core.Dimension;
4+
import com.bc.fiduceo.core.Interval;
5+
import com.bc.fiduceo.core.NodeType;
6+
import com.bc.fiduceo.geometry.Polygon;
7+
import com.bc.fiduceo.location.PixelLocator;
8+
import com.bc.fiduceo.reader.AcquisitionInfo;
9+
import com.bc.fiduceo.reader.netcdf.NetCDFReader;
10+
import com.bc.fiduceo.reader.time.TimeLocator;
11+
import com.bc.fiduceo.util.TimeUtils;
12+
import ucar.ma2.Array;
13+
import ucar.ma2.ArrayInt;
14+
import ucar.ma2.InvalidRangeException;
15+
import ucar.nc2.Variable;
16+
17+
import java.io.IOException;
18+
import java.util.Calendar;
19+
import java.util.List;
20+
21+
public class SirdsInsituReader extends NetCDFReader {
22+
23+
private static final String REGEX = "SSTCCI2_refdata_[a-z]+(_[a-z]+)?_\\d{6}.nc";
24+
private int[] timeMinMax;
25+
26+
@Override
27+
public AcquisitionInfo read() throws IOException {
28+
final AcquisitionInfo info = new AcquisitionInfo();
29+
30+
ensureSensingTimesAvailable();
31+
32+
info.setSensingStart(TimeUtils.create(timeMinMax[0] * 1000L));
33+
info.setSensingStop(TimeUtils.create(timeMinMax[1] * 1000L));
34+
35+
info.setNodeType(NodeType.UNDEFINED);
36+
return info;
37+
}
38+
39+
private void ensureSensingTimesAvailable() throws IOException {
40+
if (timeMinMax == null) {
41+
final Calendar calendar = TimeUtils.getUTCCalendar();
42+
final Array year = arrayCache.get("YEAR");
43+
final Array month = arrayCache.get("MONTH");
44+
final Array day = arrayCache.get("DAY");
45+
final Array hour = arrayCache.get("HOUR");
46+
final Array minute = arrayCache.get("MINUTE");
47+
final Array second = arrayCache.get("SECOND");
48+
final int numMeasures = (int) year.getSize();
49+
final int[] timeStamps = new int[numMeasures];
50+
for (int i = 0; i < numMeasures; i++) {
51+
final short yearVal = year.getShort(i);
52+
final byte monthVal = (byte) (month.getByte(i) - 1);
53+
final byte dayVal = day.getByte(i);
54+
final byte hourVal = hour.getByte(i);
55+
final byte minuteVal = minute.getByte(i);
56+
final byte secondVal = second.getByte(i);
57+
58+
//noinspection MagicConstant
59+
calendar.set(yearVal, monthVal, dayVal, hourVal, minuteVal, secondVal);
60+
timeStamps[i] = (int) (calendar.getTime().getTime() / 1000);
61+
}
62+
63+
timeMinMax = extractMinMax(timeStamps);
64+
}
65+
}
66+
67+
@Override
68+
public String getRegEx() {
69+
return REGEX;
70+
}
71+
72+
@Override
73+
public PixelLocator getPixelLocator() throws IOException {
74+
throw new RuntimeException("not implemented");
75+
}
76+
77+
@Override
78+
public PixelLocator getSubScenePixelLocator(Polygon sceneGeometry) throws IOException {
79+
throw new RuntimeException("not implemented");
80+
}
81+
82+
@Override
83+
public TimeLocator getTimeLocator() throws IOException {
84+
throw new RuntimeException("not implemented");
85+
}
86+
87+
@Override
88+
public int[] extractYearMonthDayFromFilename(String fileName) {
89+
throw new RuntimeException("not implemented");
90+
}
91+
92+
@Override
93+
public Array readRaw(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
94+
throw new RuntimeException("not implemented");
95+
}
96+
97+
@Override
98+
public Array readScaled(int centerX, int centerY, Interval interval, String variableName) throws IOException, InvalidRangeException {
99+
throw new RuntimeException("not implemented");
100+
}
101+
102+
@Override
103+
public ArrayInt.D2 readAcquisitionTime(int x, int y, Interval interval) throws IOException, InvalidRangeException {
104+
throw new RuntimeException("not implemented");
105+
}
106+
107+
@Override
108+
public List<Variable> getVariables() throws InvalidRangeException, IOException {
109+
throw new RuntimeException("not implemented");
110+
}
111+
112+
@Override
113+
public Dimension getProductSize() throws IOException {
114+
throw new RuntimeException("not implemented");
115+
}
116+
117+
@Override
118+
public String getLongitudeVariableName() {
119+
return "LONGITUDE";
120+
}
121+
122+
@Override
123+
public String getLatitudeVariableName() {
124+
return "LATITUDE";
125+
}
126+
127+
// package access for testing only tb 2021-07-21
128+
static int[] extractMinMax(int[] timeStamps) {
129+
int min = Integer.MAX_VALUE;
130+
int max = Integer.MIN_VALUE;
131+
for (int value : timeStamps) {
132+
if (value < min) {
133+
min = value;
134+
}
135+
if (value > max) {
136+
max = value;
137+
}
138+
}
139+
140+
return new int[]{min, max};
141+
}
142+
143+
@Override
144+
public void close() throws IOException {
145+
timeMinMax = null;
146+
super.close();
147+
}
148+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bc.fiduceo.reader.insitu.sirds_sst;
2+
3+
import com.bc.fiduceo.reader.DataType;
4+
import com.bc.fiduceo.reader.Reader;
5+
import com.bc.fiduceo.reader.ReaderContext;
6+
import com.bc.fiduceo.reader.ReaderPlugin;
7+
8+
public class SirdsInsituReaderPlugin implements ReaderPlugin {
9+
10+
private final String[] SUPPORTED_KEYS = {"animal-sirds", "argo-sirds", "argo_surf-sirds", "bottle-sirds", "ctd-sirds", "drifter-sirds", "drifter_cmems-sirds", "gtmba-sirds", "mbt-sirds", "mooring-sirds", "ship-sirds", "xbt-sirds"};
11+
12+
@Override
13+
public Reader createReader(ReaderContext readerContext) {
14+
return new SirdsInsituReader();
15+
}
16+
17+
@Override
18+
public String[] getSupportedSensorKeys() {
19+
return SUPPORTED_KEYS;
20+
}
21+
22+
@Override
23+
public DataType getDataType() {
24+
return DataType.INSITU;
25+
}
26+
}
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.sirds_sst;
2+
3+
import com.bc.fiduceo.reader.DataType;
4+
import com.bc.fiduceo.reader.Reader;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class SirdsInsituReaderPluginTest {
11+
12+
private SirdsInsituReaderPlugin plugin;
13+
14+
@Before
15+
public void setUp() throws Exception {
16+
plugin = new SirdsInsituReaderPlugin();
17+
}
18+
19+
@Test
20+
public void testGetSupportedSensorKeys() throws Exception {
21+
final String[] expected = {"animal-sirds", "argo-sirds", "argo_surf-sirds", "bottle-sirds", "ctd-sirds", "drifter-sirds", "drifter_cmems-sirds", "gtmba-sirds", "mbt-sirds", "mooring-sirds", "ship-sirds", "xbt-sirds"};
22+
23+
final String[] sensorKeys = plugin.getSupportedSensorKeys();
24+
assertArrayEquals(expected, sensorKeys);
25+
}
26+
27+
@Test
28+
public void testCreateReader() {
29+
final Reader reader = plugin.createReader(null);
30+
assertNotNull(reader);
31+
assertTrue(reader instanceof SirdsInsituReader);
32+
}
33+
34+
@Test
35+
public void testGetDataType() {
36+
assertEquals(DataType.INSITU, plugin.getDataType());
37+
}
38+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.bc.fiduceo.reader.insitu.sirds_sst;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class SirdsInsituReaderTest {
12+
13+
private SirdsInsituReader insituReader;
14+
15+
@Before
16+
public void setUp() {
17+
insituReader = new SirdsInsituReader();
18+
}
19+
20+
@Test
21+
public void testGetRegEx() {
22+
final String expected = "SSTCCI2_refdata_[a-z]+(_[a-z]+)?_\\d{6}.nc";
23+
24+
assertEquals(expected, insituReader.getRegEx());
25+
final Pattern pattern = java.util.regex.Pattern.compile(expected);
26+
27+
Matcher matcher = pattern.matcher("SSTCCI2_refdata_argosurf_202005.nc");
28+
assertTrue(matcher.matches());
29+
30+
matcher = pattern.matcher("SSTCCI2_refdata_bottle_198904.nc");
31+
assertTrue(matcher.matches());
32+
33+
matcher = pattern.matcher("SSTCCI2_refdata_drifter_198802.nc");
34+
assertTrue(matcher.matches());
35+
36+
matcher = pattern.matcher("SSTCCI2_refdata_drifter_cmems_201801.nc");
37+
assertTrue(matcher.matches());
38+
39+
matcher = pattern.matcher("SSTCCI2_refdata_argosurf_201712.nc");
40+
assertTrue(matcher.matches());
41+
42+
matcher = pattern.matcher("insitu_5_WMOID_5901880_20100514_20100627.nc");
43+
assertFalse(matcher.matches());
44+
45+
matcher = pattern.matcher("insitu_9_WMOID_14456569_19980913_19981123.nc");
46+
assertFalse(matcher.matches());
47+
48+
matcher = pattern.matcher("insitu_10_WMOID_9733500_19840123_19840404.nc");
49+
assertFalse(matcher.matches());
50+
51+
matcher = pattern.matcher("AMSR_E_L2A_BrightnessTemperatures_V12_200502170536_D.hdf");
52+
assertFalse(matcher.matches());
53+
54+
matcher = pattern.matcher("NSS.HIRX.TN.D79287.S1623.E1807.B0516566.GC.nc");
55+
assertFalse(matcher.matches());
56+
}
57+
58+
@Test
59+
public void testGetLongitudeVariableName() {
60+
assertEquals("LONGITUDE", insituReader.getLongitudeVariableName());
61+
}
62+
63+
@Test
64+
public void testGetLatitudeVariableName() {
65+
assertEquals("LATITUDE", insituReader.getLatitudeVariableName());
66+
}
67+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.bc.fiduceo.reader.insitu.sirds_sst;
2+
3+
import com.bc.fiduceo.IOTestRunner;
4+
import com.bc.fiduceo.TestUtil;
5+
import com.bc.fiduceo.core.NodeType;
6+
import com.bc.fiduceo.reader.AcquisitionInfo;
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
15+
import static org.junit.Assert.*;
16+
17+
@RunWith(IOTestRunner.class)
18+
public class SirdsInsituReader_IO_Test {
19+
20+
private SirdsInsituReader insituReader;
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
insituReader = new SirdsInsituReader();
25+
}
26+
27+
@After
28+
public void tearDown() throws Exception {
29+
insituReader.close();
30+
}
31+
32+
@Test
33+
public void testReadAcquisitionInfo_drifter() throws Exception {
34+
openFile("SSTCCI2_refdata_drifter_201304.nc", "v1.0");
35+
36+
final AcquisitionInfo info = insituReader.read();
37+
assertNotNull(info);
38+
39+
TestUtil.assertCorrectUTCDate(2013, 4, 1, 0, 0, 0, 0, info.getSensingStart());
40+
TestUtil.assertCorrectUTCDate(2013, 4, 30, 23, 58, 47, 0, info.getSensingStop());
41+
42+
assertEquals(NodeType.UNDEFINED, info.getNodeType());
43+
44+
assertNull(info.getBoundingGeometry());
45+
}
46+
47+
@Test
48+
public void testReadAcquisitionInfo_mooring() throws Exception {
49+
openFile("SSTCCI2_refdata_mooring_201602.nc", "v1.0");
50+
51+
final AcquisitionInfo info = insituReader.read();
52+
assertNotNull(info);
53+
54+
TestUtil.assertCorrectUTCDate(2016, 2, 1, 0, 0, 0, 0, info.getSensingStart());
55+
TestUtil.assertCorrectUTCDate(2016, 2, 29, 23, 58, 11, 0, info.getSensingStop());
56+
57+
assertEquals(NodeType.UNDEFINED, info.getNodeType());
58+
59+
assertNull(info.getBoundingGeometry());
60+
}
61+
62+
@Test
63+
public void testReadAcquisitionInfo_xbt() throws Exception {
64+
openFile("SSTCCI2_refdata_xbt_200204.nc", "v1.0");
65+
66+
final AcquisitionInfo info = insituReader.read();
67+
assertNotNull(info);
68+
69+
TestUtil.assertCorrectUTCDate(2002, 4, 1, 0, 1, 12, 0, info.getSensingStart());
70+
TestUtil.assertCorrectUTCDate(2002, 4, 30, 23, 43, 48, 0, info.getSensingStop());
71+
72+
assertEquals(NodeType.UNDEFINED, info.getNodeType());
73+
74+
assertNull(info.getBoundingGeometry());
75+
}
76+
77+
private void openFile(String fileName, String version) throws IOException {
78+
final String testFilePath = TestUtil.assembleFileSystemPath(new String[]{"insitu", "sirds", version, fileName}, false);
79+
final File insituDataFile = TestUtil.getTestDataFileAsserted(testFilePath);
80+
81+
insituReader.open(insituDataFile);
82+
}
83+
}

0 commit comments

Comments
 (0)