88import com .bc .fiduceo .geometry .LineString ;
99import com .bc .fiduceo .geometry .Polygon ;
1010import com .bc .fiduceo .location .PixelLocator ;
11- import com .bc .fiduceo .location .PixelLocatorFactory ;
1211import com .bc .fiduceo .reader .*;
12+ import com .bc .fiduceo .reader .slstr .utility .TransformFactory ;
13+ import com .bc .fiduceo .reader .snap .SNAP_PixelLocator ;
1314import com .bc .fiduceo .reader .time .TimeLocator ;
1415import com .bc .fiduceo .reader .time .TimeLocator_MicrosSince2000 ;
1516import com .bc .fiduceo .store .FileSystemStore ;
1617import com .bc .fiduceo .store .Store ;
1718import com .bc .fiduceo .store .ZipStore ;
1819import com .bc .fiduceo .util .NetCDFUtils ;
1920import com .bc .fiduceo .util .TimeUtils ;
21+ import org .esa .s3tbx .dataio .s3 .Manifest ;
22+ import org .esa .s3tbx .dataio .s3 .XfduManifest ;
23+ import org .esa .snap .core .dataio .geocoding .*;
24+ import org .esa .snap .core .dataio .geocoding .forward .PixelForward ;
25+ import org .esa .snap .core .dataio .geocoding .inverse .PixelQuadTreeInverse ;
26+ import org .esa .snap .core .datamodel .MetadataElement ;
2027import org .esa .snap .core .datamodel .ProductData ;
2128import org .esa .snap .core .util .StringUtils ;
29+ import org .w3c .dom .Document ;
30+ import org .xml .sax .InputSource ;
31+ import org .xml .sax .SAXException ;
2232import ucar .ma2 .DataType ;
2333import ucar .ma2 .*;
2434import ucar .nc2 .NetcdfFile ;
2535import ucar .nc2 .Variable ;
2636
37+ import javax .xml .parsers .DocumentBuilderFactory ;
38+ import javax .xml .parsers .ParserConfigurationException ;
2739import java .io .File ;
2840import java .io .IOException ;
41+ import java .io .StringReader ;
2942import java .text .ParseException ;
3043import java .util .*;
3144import java .util .regex .Matcher ;
3245import java .util .regex .Pattern ;
3346import java .util .zip .ZipFile ;
3447
48+ import static com .bc .fiduceo .reader .slstr .utility .ManifestUtil .getObliqueGridOffset ;
3549import static com .bc .fiduceo .util .NetCDFUtils .scaleIfNecessary ;
3650import static ucar .ma2 .DataType .INT ;
3751import static ucar .nc2 .NetcdfFiles .openInMemory ;
@@ -40,15 +54,16 @@ public class SlstrRegriddedSubsetReader implements Reader {
4054
4155 private final ReaderContext _readerContext ;
4256 private final boolean _nadirView ;
43- private final String LONGITUDE_VAR_NAME = "longitude_tx " ;
44- private final String LATITUDE_VAR_NAME = "latitude_tx " ;
57+ private final String LONGITUDE_VAR_NAME = "longitude_in " ;
58+ private final String LATITUDE_VAR_NAME = "latitude_in " ;
4559 private TreeMap <String , NetcdfFile > _ncFiles ;
4660 private ArrayList <Variable > _variables ;
4761 private HashMap <String , Variable > _variablesLUT ;
4862 private String _manifest ;
4963 private PixelLocator pixelLocator ;
5064 private TimeLocator_MicrosSince2000 _timeLocator ;
5165 private long [] _timeStamps2000 ;
66+ private TransformFactory transformFactory ;
5267
5368 public SlstrRegriddedSubsetReader (ReaderContext readerContext , boolean nadirView ) {
5469 _readerContext = readerContext ;
@@ -71,6 +86,21 @@ public void open(File file) throws IOException {
7186 _manifest = new String (store .getBytes (keyManifest .first ()));
7287 openNcFiles (store );
7388 initVariables ();
89+
90+ final InputSource is = new InputSource (new StringReader (_manifest ));
91+ final Document document = DocumentBuilderFactory .newInstance ().newDocumentBuilder ().parse (is );
92+ final Manifest manifest = XfduManifest .createManifest (document );
93+
94+ final MetadataElement metadataRoot = new MetadataElement ("root" );
95+ metadataRoot .addElement (manifest .getMetadata ());
96+ final int obliqueGridOffset = getObliqueGridOffset (metadataRoot );
97+ final Dimension productSize = getProductSize ();
98+
99+ transformFactory = new TransformFactory (productSize .getNx () * 2 ,
100+ productSize .getNy () * 2 ,
101+ obliqueGridOffset );
102+ } catch (ParserConfigurationException | SAXException e ) {
103+ throw new IOException (e .getMessage ());
74104 } finally {
75105 store .close ();
76106 }
@@ -79,6 +109,7 @@ public void open(File file) throws IOException {
79109 @ Override
80110 public void close () throws IOException {
81111 _ncFiles .clear ();
112+ transformFactory = null ;
82113 }
83114
84115 @ Override
@@ -100,8 +131,12 @@ public AcquisitionInfo read() throws IOException {
100131 private void extractGeometries (AcquisitionInfo acquisitionInfo ) throws IOException {
101132 final GeometryFactory geometryFactory = _readerContext .getGeometryFactory ();
102133 final BoundingPolygonCreator boundingPolygonCreator = new BoundingPolygonCreator (new Interval (250 , 250 ), geometryFactory );
103- final Array longitude = _variablesLUT .get (LONGITUDE_VAR_NAME ).read ();
104- final Array latitude = _variablesLUT .get (LATITUDE_VAR_NAME ).read ();
134+
135+ final Variable lonVariable = _variablesLUT .get (LONGITUDE_VAR_NAME );
136+ final Array longitude = NetCDFUtils .readAndScaleIfNecessary (lonVariable );
137+ final Variable latVariable = _variablesLUT .get (LATITUDE_VAR_NAME );
138+ final Array latitude = NetCDFUtils .readAndScaleIfNecessary (latVariable );
139+
105140 final Geometry boundingGeometry = boundingPolygonCreator .createBoundingGeometry (longitude , latitude );
106141 if (!boundingGeometry .isValid ()) {
107142 throw new RuntimeException ("Detected invalid bounding geometry" );
@@ -147,10 +182,24 @@ public String getRegEx() {
147182 @ Override
148183 public PixelLocator getPixelLocator () throws IOException {
149184 if (pixelLocator == null ) {
150- final Array longitude = _variablesLUT .get (LONGITUDE_VAR_NAME ).read ();
151- final Array latitude = _variablesLUT .get (LATITUDE_VAR_NAME ).read ();
185+ final Variable lonVariable = _variablesLUT .get (LONGITUDE_VAR_NAME );
186+ final Array longitude = NetCDFUtils .readAndScaleIfNecessary (lonVariable );
187+ final Variable latVariable = _variablesLUT .get (LATITUDE_VAR_NAME );
188+ final Array latitude = NetCDFUtils .readAndScaleIfNecessary (latVariable );
152189 final int [] shape = latitude .getShape ();
153- pixelLocator = PixelLocatorFactory .getSwathPixelLocator (longitude , latitude , shape [1 ], shape [0 ]);
190+
191+ final GeoRaster geoRaster = new GeoRaster ((double []) longitude .get1DJavaArray (DataType .DOUBLE ),
192+ (double []) latitude .get1DJavaArray (DataType .DOUBLE ),
193+ LONGITUDE_VAR_NAME , LATITUDE_VAR_NAME ,
194+ shape [1 ], shape [0 ], shape [1 ], shape [0 ],
195+ 1.0 , 0.5 , 0.5 , 1.0 , 1.0 );
196+
197+ final ForwardCoding forward = ComponentFactory .getForward (PixelForward .KEY );
198+ final InverseCoding inverse = ComponentFactory .getInverse (PixelQuadTreeInverse .KEY );
199+ final ComponentGeoCoding componentGeoCoding = new ComponentGeoCoding (geoRaster , forward , inverse , GeoChecks .ANTIMERIDIAN );
200+ componentGeoCoding .initialize ();
201+
202+ pixelLocator = new SNAP_PixelLocator (componentGeoCoding );
154203 }
155204 return pixelLocator ;
156205 }
0 commit comments