99import com .bc .fiduceo .geometry .Polygon ;
1010import com .bc .fiduceo .location .PixelLocator ;
1111import com .bc .fiduceo .reader .*;
12+ import com .bc .fiduceo .reader .slstr .VariableType ;
13+ import com .bc .fiduceo .reader .slstr .utility .Transform ;
1214import com .bc .fiduceo .reader .slstr .utility .TransformFactory ;
1315import com .bc .fiduceo .reader .snap .SNAP_PixelLocator ;
1416import com .bc .fiduceo .reader .time .TimeLocator ;
3739import java .io .File ;
3840import java .io .IOException ;
3941import java .io .StringReader ;
40- import java .util .*;
42+ import java .util .ArrayList ;
43+ import java .util .Arrays ;
44+ import java .util .List ;
45+ import java .util .TreeSet ;
4146import java .util .regex .Matcher ;
4247import java .util .regex .Pattern ;
4348import java .util .zip .ZipFile ;
4449
4550import static com .bc .fiduceo .reader .slstr .utility .ManifestUtil .getObliqueGridOffset ;
46- import static com .bc .fiduceo .util .NetCDFUtils .scaleIfNecessary ;
4751import static ucar .ma2 .DataType .INT ;
4852
4953public class SlstrRegriddedSubsetReader implements Reader {
5054
51- private final ReaderContext _readerContext ;
52- private final boolean _nadirView ;
55+ private final ReaderContext readerContext ;
5356 private final String LONGITUDE_VAR_NAME = "longitude_in" ;
5457 private final String LATITUDE_VAR_NAME = "latitude_in" ;
55- private String _manifest ;
58+ private String manifestXml ;
5659 private PixelLocator pixelLocator ;
57- private TimeLocator_MicrosSince2000 _timeLocator ;
58- private long [] _timeStamps2000 ;
60+ private TimeLocator_MicrosSince2000 timeLocator ;
61+ private long [] timeStamps2000 ;
5962 private TransformFactory transformFactory ;
6063 private final NcCache ncCache ;
6164 private RasterInfo rasterInfo ;
6265 private Manifest manifest ;
6366
64- public SlstrRegriddedSubsetReader (ReaderContext readerContext , boolean nadirView ) {
65- _readerContext = readerContext ;
66- _nadirView = nadirView ;
67+ public SlstrRegriddedSubsetReader (ReaderContext readerContext ) {
68+ this .readerContext = readerContext ;
6769 ncCache = new NcCache ();
6870 }
6971
@@ -82,9 +84,9 @@ public void open(File file) throws IOException {
8284
8385 try {
8486 final TreeSet <String > keyManifest = store .getKeysEndingWith ("xfdumanifest.xml" );
85- _manifest = new String (store .getBytes (keyManifest .first ()));
87+ manifestXml = new String (store .getBytes (keyManifest .first ()));
8688
87- final InputSource is = new InputSource (new StringReader (_manifest ));
89+ final InputSource is = new InputSource (new StringReader (manifestXml ));
8890 final Document document = DocumentBuilderFactory .newInstance ().newDocumentBuilder ().parse (is );
8991 manifest = XfduManifest .createManifest (document );
9092
@@ -108,6 +110,7 @@ public void open(File file) throws IOException {
108110 public void close () throws IOException {
109111 ncCache .close ();
110112 transformFactory = null ;
113+ rasterInfo = null ;
111114 }
112115
113116 @ Override
@@ -121,7 +124,7 @@ public AcquisitionInfo read() throws IOException {
121124 }
122125
123126 private void extractGeometries (AcquisitionInfo acquisitionInfo ) throws IOException {
124- final GeometryFactory geometryFactory = _readerContext .getGeometryFactory ();
127+ final GeometryFactory geometryFactory = readerContext .getGeometryFactory ();
125128 final BoundingPolygonCreator boundingPolygonCreator = new BoundingPolygonCreator (new Interval (250 , 250 ), geometryFactory );
126129
127130 final Variable lonVariable = ncCache .getVariable (LONGITUDE_VAR_NAME );
@@ -144,9 +147,9 @@ private void extractGeometries(AcquisitionInfo acquisitionInfo) throws IOExcepti
144147
145148 private NodeType findNodeType () {
146149 final NodeType nodeType ;
147- if (_manifest .contains ("groundTrackDirection" )) {
150+ if (manifestXml .contains ("groundTrackDirection" )) {
148151 final Pattern pattern = Pattern .compile ("groundTrackDirection *= *['\" ]ascending['\" ]" );
149- final Matcher matcher = pattern .matcher (_manifest );
152+ final Matcher matcher = pattern .matcher (manifestXml );
150153 if (matcher .find ()) {
151154 nodeType = NodeType .ASCENDING ;
152155 } else {
@@ -195,21 +198,21 @@ public PixelLocator getSubScenePixelLocator(Polygon sceneGeometry) throws IOExce
195198
196199 @ Override
197200 public TimeLocator getTimeLocator () throws IOException {
198- if (_timeLocator == null ) {
201+ if (timeLocator == null ) {
199202 ensureTimeStamps ();
200- _timeLocator = new TimeLocator_MicrosSince2000 (_timeStamps2000 );
203+ timeLocator = new TimeLocator_MicrosSince2000 (timeStamps2000 );
201204 }
202- return _timeLocator ;
205+ return timeLocator ;
203206 }
204207
205208 private void ensureTimeStamps () throws IOException {
206- if (_timeStamps2000 == null ) {
209+ if (timeStamps2000 == null ) {
207210 final Variable timeStampVariable = ncCache .getVariable ("time_stamp_i" );
208211
209212 final Array array = timeStampVariable .read ();
210- _timeStamps2000 = (long []) array .get1DJavaArray (DataType .LONG );
213+ timeStamps2000 = (long []) array .get1DJavaArray (DataType .LONG );
211214
212- if (_timeStamps2000 == null ) {
215+ if (timeStamps2000 == null ) {
213216 throw new IOException ("Unable to read time stamp data" );
214217 }
215218 }
@@ -228,18 +231,27 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
228231 @ Override
229232 public Array readRaw (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
230233 final Variable variable = ncCache .getVariable (variableName );
234+ final Array rawArray = ncCache .getRawArray (variableName );
235+ final Transform transform = getTransform (variableName );
231236 final Number fillValue = NetCDFUtils .getFillValue (variable );
232- // @todo 1 tb/** this needs to be cached! Else we read the full array for every matchup. tb 2022-07-21
233- final Array fullArray = variable .read ();
234- return RawDataReader .read (centerX , centerY , interval , fillValue , fullArray , getProductSize ());
237+
238+ final int mappedX = (int ) transform .mapCoordinate_X (centerX );
239+ final int mappedY = (int ) transform .mapCoordinate_Y (centerY );
240+
241+ return RawDataReader .read (mappedX , mappedY , interval , fillValue , rawArray , getProductSize ());
235242 }
236243
237244 @ Override
238245 public Array readScaled (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
239- final Array rawData = readRaw (centerX , centerY , interval , variableName );
240246 final Variable variable = ncCache .getVariable (variableName );
247+ final Array scaledArray = ncCache .getScaledArray (variableName );
248+ final Transform transform = getTransform (variableName );
249+ final Number fillValue = NetCDFUtils .getFillValue (variable );
241250
242- return scaleIfNecessary (variable , rawData );
251+ final int mappedX = (int ) transform .mapCoordinate_X (centerX );
252+ final int mappedY = (int ) transform .mapCoordinate_Y (centerY );
253+
254+ return RawDataReader .read (mappedX , mappedY , interval , fillValue , scaledArray , getProductSize ());
243255 }
244256
245257 @ Override
@@ -262,10 +274,10 @@ public ArrayInt.D2 readAcquisitionTime(int x, int y, Interval interval) throws I
262274 final Index index = target .getIndex ();
263275 for (int yIdx = 0 ; yIdx < targetHeight ; yIdx ++) {
264276 final int srcY = startY + yIdx ;
265- if (srcY < 0 || srcY >= _timeStamps2000 .length ) {
277+ if (srcY < 0 || srcY >= timeStamps2000 .length ) {
266278 continue ;
267279 }
268- final long lineTimeMillis = TimeUtils .millisSince2000ToUnixEpoch (_timeStamps2000 [srcY ]);
280+ final long lineTimeMillis = TimeUtils .millisSince2000ToUnixEpoch (timeStamps2000 [srcY ]);
269281 int lineTimeSeconds = (int ) Math .round (lineTimeMillis * 0.001 );
270282 index .set0 (yIdx );
271283 for (int xIdx = 0 ; xIdx < targetWidth ; xIdx ++) {
@@ -327,11 +339,6 @@ static String extractName(String key) {
327339 }
328340 }
329341
330- // just for testing tb 2022-07-18
331- boolean isNadirView () {
332- return _nadirView ;
333- }
334-
335342 static RasterInfo getRasterInfo (Manifest manifest ) {
336343 final RasterInfo rasterInfo = new RasterInfo ();
337344
@@ -364,6 +371,16 @@ static RasterInfo getRasterInfo(Manifest manifest) {
364371 return rasterInfo ;
365372 }
366373
374+ private Transform getTransform (String variableName ) {
375+ Transform transform ;
376+ if (variableName .endsWith ("_io" )) {
377+ transform = transformFactory .get (VariableType .OBLIQUE_1km );
378+ } else {
379+ transform = transformFactory .get (VariableType .NADIR_1km );
380+ }
381+ return transform ;
382+ }
383+
367384 private static int getRasterAttributeInt (MetadataElement element , String attributeName ) {
368385 final MetadataAttribute spatialResolution = element .getAttribute (attributeName );
369386 final String resolutionString = spatialResolution .getData ().getElemString ();
0 commit comments