Skip to content

Commit 1a8c996

Browse files
committed
implemented time locator
1 parent 36c3302 commit 1a8c996

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed

core/src/main/java/com/bc/fiduceo/reader/smos/SmosL1CDailyGriddedReader.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SmosL1CDailyGriddedReader extends NetCDFReader {
3636

3737
private File productDir;
3838
private PixelLocator pixelLocator;
39+
private TimeLocator timeLocator;
3940

4041
SmosL1CDailyGriddedReader(ReaderContext readerContext) {
4142
this.readerContext = readerContext;
@@ -65,6 +66,7 @@ public void close() throws IOException {
6566
super.close();
6667

6768
pixelLocator = null;
69+
timeLocator = null;
6870
if (productDir != null) {
6971
readerContext.deleteTempFile(productDir);
7072
productDir = null;
@@ -120,7 +122,27 @@ public PixelLocator getSubScenePixelLocator(Polygon sceneGeometry) throws IOExce
120122

121123
@Override
122124
public TimeLocator getTimeLocator() throws IOException {
123-
throw new IllegalStateException("not implemented");
125+
if (timeLocator == null) {
126+
final Array days = arrayCache.get("Days");
127+
final Array seconds = arrayCache.get("UTC_Seconds");
128+
final Array micros = arrayCache.get("UTC_Microseconds");
129+
130+
// get layer at index 8 which is the 40deg observation angle bin
131+
int layerIndex = 8;
132+
int[] offset = {layerIndex, 0, 0};
133+
int[] shape = days.getShape();
134+
shape[0] = 1; // just one z-layer
135+
try {
136+
final Array daysLayer = days.section(offset, shape);
137+
final Array secondsLayer = seconds.section(offset, shape);
138+
final Array microsLayer = micros.section(offset, shape);
139+
140+
timeLocator = new SmosL1CTimeLocator(daysLayer, secondsLayer, microsLayer);
141+
} catch (InvalidRangeException e) {
142+
throw new IOException(e);
143+
}
144+
}
145+
return timeLocator;
124146
}
125147

126148
@Override
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
11
package com.bc.fiduceo.reader.smos;
22

33
import com.bc.fiduceo.reader.time.TimeLocator;
4+
import com.bc.fiduceo.util.NetCDFUtils;
5+
import ucar.ma2.Array;
6+
import ucar.ma2.DataType;
7+
import ucar.ma2.Index;
8+
9+
import java.util.Date;
410

511
class SmosL1CTimeLocator implements TimeLocator {
12+
private final Array days;
13+
private final Array seconds;
14+
private final Array micros;
15+
private final int maxX;
16+
private final int maxY;
17+
private final long outFillValue;
18+
private final int inFillValue;
19+
20+
SmosL1CTimeLocator(Array days, Array seconds, Array micros) {
21+
this.days = days;
22+
this.seconds = seconds;
23+
this.micros = micros;
24+
25+
int[] shape = days.getShape();
26+
maxX = shape[1];
27+
maxY = shape[0];
28+
29+
outFillValue = -1L;
30+
inFillValue = (int) NetCDFUtils.getDefaultFillValue(DataType.INT, false);
31+
}
632

733
@Override
834
public long getTimeFor(int x, int y) {
9-
return 0;
35+
if (x < 0 | x >= maxX | y < 0 | y >= maxY) {
36+
return outFillValue;
37+
}
38+
39+
final Index index = days.getIndex();
40+
index.set(y, x);
41+
final int day = days.getInt(index);
42+
final int second = seconds.getInt(index);
43+
final int micro = micros.getInt(index);
44+
if (day == inFillValue | second == inFillValue | micro == inFillValue) {
45+
return outFillValue;
46+
}
47+
48+
final Date date = SmosL1CDailyGriddedReader.cfiDateToUtc(day, second, micro);
49+
return date.getTime();
1050
}
1151
}

core/src/test/java/com/bc/fiduceo/reader/smos/SmosL1CDailyGriddedReader_IO_Test.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.bc.fiduceo.location.PixelLocator;
99
import com.bc.fiduceo.reader.AcquisitionInfo;
1010
import com.bc.fiduceo.reader.ReaderContext;
11+
import com.bc.fiduceo.reader.time.TimeLocator;
1112
import com.bc.fiduceo.util.TempFileUtils;
1213
import org.junit.After;
1314
import org.junit.Before;
@@ -178,6 +179,28 @@ public void testGetSubScenePixelLocator_CDF3TD() throws IOException {
178179
}
179180
}
180181

182+
@Test
183+
public void testGetTimeLocator_CDF3TA() throws IOException {
184+
final File file = getCDF3TAFile();
185+
186+
try {
187+
reader.open(file);
188+
189+
final TimeLocator timeLocator = reader.getTimeLocator();
190+
assertEquals(1465599546734L, timeLocator.getTimeFor(0, 0));
191+
assertEquals(1465576328309L, timeLocator.getTimeFor(100, 100));
192+
assertEquals(1465570699654L, timeLocator.getTimeFor(200, 200));
193+
assertEquals(-1L, timeLocator.getTimeFor(300, 300));
194+
assertEquals(-1L, timeLocator.getTimeFor(340, 300));
195+
assertEquals(-1L, timeLocator.getTimeFor(341, 300));
196+
assertEquals(1465559087142L, timeLocator.getTimeFor(342, 300));
197+
assertEquals(1465559079942L, timeLocator.getTimeFor(343, 300));
198+
assertEquals(1465559037942L, timeLocator.getTimeFor(350, 300));
199+
} finally {
200+
reader.close();
201+
}
202+
}
203+
181204
private File getCDF3TAFile() throws IOException {
182205
final String testFilePath = TestUtil.assembleFileSystemPath(new String[]{"miras-smos-CDF3TA", "re07", "2016", "162", "SM_RE07_MIR_CDF3TA_20160610T000000_20160610T235959_330_001_7.tgz"}, false);
183206
return TestUtil.getTestDataFileAsserted(testFilePath);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.bc.fiduceo.reader.smos;
2+
3+
import org.junit.Test;
4+
import ucar.ma2.Array;
5+
import ucar.ma2.DataType;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class SmosL1CTimeLocatorTest {
10+
11+
@Test
12+
public void testGetTimeFor() {
13+
final Array days = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{6533, 6533, 6533, 6533, 6533, 6533, 6533, 6533, 6533});
14+
final Array seconds = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{85318, 85317, 85317, 85307, 85306, 85306, 85297, 85296, 85295});
15+
final Array micros = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{154074, 954065, 354059, 153977, 553973, 353961, 353903, 153887, 953875});
16+
17+
final SmosL1CTimeLocator timeLocator = new SmosL1CTimeLocator(days, seconds, micros);
18+
19+
long time = timeLocator.getTimeFor(0, 0);
20+
assertEquals(1511221318154L, time);
21+
22+
time = timeLocator.getTimeFor(1, 0);
23+
assertEquals(1511221317954L, time);
24+
25+
time = timeLocator.getTimeFor(2, 2);
26+
assertEquals(1511221295953L, time);
27+
}
28+
29+
@Test
30+
public void testGetTimeFor_outside() {
31+
final Array days = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{6533, 6533, 6533, 6533, 6533, 6533, 6533, 6533, 6533});
32+
final Array seconds = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{85318, 85317, 85317, 85307, 85306, 85306, 85297, 85296, 85295});
33+
final Array micros = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{154074, 954065, 354059, 153977, 553973, 353961, 353903, 153887, 953875});
34+
35+
final SmosL1CTimeLocator timeLocator = new SmosL1CTimeLocator(days, seconds, micros);
36+
37+
long time = timeLocator.getTimeFor(-1, 0);
38+
assertEquals(-1L, time);
39+
40+
time = timeLocator.getTimeFor(3, 0);
41+
assertEquals(-1L, time);
42+
43+
time = timeLocator.getTimeFor(1, -1);
44+
assertEquals(-1L, time);
45+
46+
time = timeLocator.getTimeFor(1, 3);
47+
assertEquals(-1L, time);
48+
}
49+
50+
@Test
51+
public void testGetTimeFor_fillValue() {
52+
final Array days = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{-2147483647, 6533, 6533, 6533, 6533, 6533, 6533, 6533, 6533});
53+
final Array seconds = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{85318, -2147483647, 85317, 85307, 85306, 85306, 85297, 85296, 85295});
54+
final Array micros = Array.factory(DataType.INT, new int[]{3, 3}, new int[]{154074, 954065, -2147483647, 153977, 553973, 353961, 353903, 153887, 953875});
55+
56+
final SmosL1CTimeLocator timeLocator = new SmosL1CTimeLocator(days, seconds, micros);
57+
long time = timeLocator.getTimeFor(0, 0); // day has fill value
58+
assertEquals(-1L, time);
59+
60+
time = timeLocator.getTimeFor(1, 0); // second has fill value
61+
assertEquals(-1L, time);
62+
63+
time = timeLocator.getTimeFor(2, 0); // micro has fill value
64+
assertEquals(-1L, time);
65+
66+
time = timeLocator.getTimeFor(0, 1);
67+
assertEquals(1511221307153L, time);
68+
}
69+
}

0 commit comments

Comments
 (0)