2121import org .esa .snap .core .util .StringUtils ;
2222import ucar .ma2 .DataType ;
2323import ucar .ma2 .*;
24- import ucar .nc2 .Attribute ;
2524import ucar .nc2 .NetcdfFile ;
2625import ucar .nc2 .Variable ;
2726
3332import java .util .regex .Pattern ;
3433import java .util .zip .ZipFile ;
3534
36- import static com .bc .fiduceo .util .NetCDFUtils .* ;
35+ import static com .bc .fiduceo .util .NetCDFUtils .scaleIfNecessary ;
3736import static ucar .ma2 .DataType .INT ;
3837import static ucar .nc2 .NetcdfFiles .openInMemory ;
3938
@@ -48,7 +47,6 @@ public class SlstrRegriddedSubsetReader implements Reader {
4847 private HashMap <String , Variable > _variablesLUT ;
4948 private String _manifest ;
5049 private PixelLocator pixelLocator ;
51- private BoundingPolygonCreator boundingPolygonCreator ;
5250 private TimeLocator_MicrosSince2000 _timeLocator ;
5351 private long [] _timeStamps2000 ;
5452
@@ -101,7 +99,7 @@ public AcquisitionInfo read() throws IOException {
10199
102100 private void extractGeometries (AcquisitionInfo acquisitionInfo ) throws IOException {
103101 final GeometryFactory geometryFactory = _readerContext .getGeometryFactory ();
104- boundingPolygonCreator = new BoundingPolygonCreator (new Interval (250 , 250 ), geometryFactory );
102+ final BoundingPolygonCreator boundingPolygonCreator = new BoundingPolygonCreator (new Interval (250 , 250 ), geometryFactory );
105103 final Array longitude = _variablesLUT .get (LONGITUDE_VAR_NAME ).read ();
106104 final Array latitude = _variablesLUT .get (LATITUDE_VAR_NAME ).read ();
107105 final Geometry boundingGeometry = boundingPolygonCreator .createBoundingGeometry (longitude , latitude );
@@ -159,13 +157,7 @@ public PixelLocator getPixelLocator() throws IOException {
159157
160158 @ Override
161159 public PixelLocator getSubScenePixelLocator (Polygon sceneGeometry ) throws IOException {
162- final Dimension productSize = getProductSize ();
163- final int height = productSize .getNy ();
164- final int width = productSize .getNx ();
165- final int subsetHeight = boundingPolygonCreator .getSubsetHeight (height , 250 );
166- final PixelLocator pixelLocator = getPixelLocator ();
167-
168- return PixelLocatorFactory .getSubScenePixelLocator (sceneGeometry , width , height , subsetHeight , pixelLocator );
160+ return getPixelLocator ();
169161 }
170162
171163 @ Override
@@ -207,27 +199,18 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
207199 @ Override
208200 public Array readRaw (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
209201 final Variable variable = _variablesLUT .get (variableName );
210- final Number fillValue = findAttributeSafe (CF_FILL_VALUE_NAME , variable ).getNumericValue ();
211- if (fillValue == null ) {
212- throw new IOException ("The attribute '" + CF_FILL_VALUE_NAME + "' of variable '" + variableName + "' does not contain a numeric value." );
213- }
202+ final Number fillValue = NetCDFUtils .getFillValue (variable );
203+ // @todo 1 tb/** this needs to be cached! Else we read the full array for every matchup. tb 2022-07-21
214204 final Array fullArray = variable .read ();
215- final Array windowArray = RawDataReader .read (centerX , centerY , interval , fillValue , fullArray , getProductSize ());
216- return windowArray .reshape (windowArray .getShape ());
205+ return RawDataReader .read (centerX , centerY , interval , fillValue , fullArray , getProductSize ());
217206 }
218207
219208 @ Override
220209 public Array readScaled (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
221210 final Array rawData = readRaw (centerX , centerY , interval , variableName );
222211 final Variable variable = _variablesLUT .get (variableName );
223- final double scaleFactor = variable .attributes ().findAttributeDouble (CF_SCALE_FACTOR_NAME , 1.0 );
224- final double offset = variable .attributes ().findAttributeDouble (CF_ADD_OFFSET_NAME , 0.0 );
225- if (ReaderUtils .mustScale (scaleFactor , offset )) {
226- final MAMath .ScaleOffset scaleOffset = new MAMath .ScaleOffset (scaleFactor , offset );
227- return MAMath .convert2Unpacked (rawData , scaleOffset );
228- }
229212
230- return rawData ;
213+ return scaleIfNecessary ( variable , rawData ) ;
231214 }
232215
233216 @ Override
@@ -305,24 +288,20 @@ private void initVariables() throws IOException {
305288 final Variable fullSizeVar = _variables .get (0 );
306289 final NetcdfFile fullSizeNcFile = fullSizeVar .getNetcdfFile ();
307290 assert fullSizeNcFile != null ;
308- final Number fullTrackOffset = findGlobalAttributeSafe ("track_offset" , fullSizeNcFile ).getNumericValue ();
309- assert fullTrackOffset != null ;
310- final String fullAcrossResStr = findGlobalAttributeSafe ("resolution" , fullSizeNcFile ).getStringValue ();
311- assert fullAcrossResStr != null ;
291+ final double fullTrackOffset = NetCDFUtils .getGlobalAttributeDouble ("track_offset" , fullSizeNcFile );
292+ final String fullAcrossResStr = NetCDFUtils .getGlobalAttributeString ("resolution" , fullSizeNcFile );
312293 final Number fullAcrossRes = Double .parseDouble (StringUtils .split (fullAcrossResStr , " " .toCharArray (), true )[1 ]);
313294
314295 for (Map .Entry <int [], ArrayList <Variable >> entry : shapeGroupedVariablesMap .entrySet ()) {
315296 final ArrayList <Variable > vars = entry .getValue ();
316297 for (Variable var : vars ) {
317298 final NetcdfFile varNcFile = var .getNetcdfFile ();
318299 assert varNcFile != null ;
319- final Number varTrackOffset = findGlobalAttributeSafe ("track_offset" , varNcFile ).getNumericValue ();
320- assert varTrackOffset != null ;
321- final String varAcrossResStr = findGlobalAttributeSafe ("resolution" , varNcFile ).getStringValue ();
322- assert varAcrossResStr != null ;
300+ final double varTrackOffset = NetCDFUtils .getGlobalAttributeDouble ("track_offset" , varNcFile );
301+ final String varAcrossResStr = NetCDFUtils .getGlobalAttributeString ("resolution" , varNcFile );
323302 final Number varAcrossRes = Double .parseDouble (StringUtils .split (varAcrossResStr , " " .toCharArray (), true )[1 ]);
324303 final double subsamplingX = varAcrossRes .doubleValue () / fullAcrossRes .doubleValue ();
325- final double offsetX = fullTrackOffset . doubleValue () - varTrackOffset . doubleValue () * subsamplingX ;
304+ final double offsetX = fullTrackOffset - varTrackOffset * subsamplingX ;
326305 _variables .add (
327306 new SlstrSubsetTiePointVariable (var , fullSizeShape [1 ], fullSizeShape [0 ], offsetX , subsamplingX )
328307 );
@@ -413,21 +392,4 @@ static String extractName(String key) {
413392 boolean isNadirView () {
414393 return _nadirView ;
415394 }
416-
417- @ SuppressWarnings ("SameParameterValue" )
418- private Attribute findAttributeSafe (String attributeName , Variable variable ) throws IOException {
419- final Attribute attribute = variable .findAttribute (attributeName );
420- if (attribute == null ) {
421- throw new IOException ("Attribute '" + attributeName + "' is expected in variable '" + variable .getShortName () + "'" );
422- }
423- return attribute ;
424- }
425-
426- private Attribute findGlobalAttributeSafe (String attributeName , NetcdfFile netcdfFile ) throws IOException {
427- final Attribute attribute = netcdfFile .findGlobalAttribute (attributeName );
428- if (attribute == null ) {
429- throw new IOException ("Global attribute '" + attributeName + "' is expected in netcdf file '" + netcdfFile .getLocation () + "'" );
430- }
431- return attribute ;
432- }
433395}
0 commit comments