Skip to content

Commit

Permalink
[GEOS-8767]: NetCDF-Out DataPacking and NaN management fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dromagnoli committed Jun 1, 2018
1 parent 327876d commit d04094e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ protected void updateDimensionValues(GridCoverage2D coverage) {
final String dimensionName = dimension.getName();
final Object value = properties.get(dimensionName);
if (value == null) {
if (LOGGER.isLoggable(Level.WARNING)) {
Set<String> dimensions = crsWriter.getCoordinatesDimensionNames();
// Coordinates dimensions (lon/lat) aren't taken into account
// for values update. Do not warn if they are missing
if (dimensions != null && !dimensions.contains(dimensionName)
&& LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("No Dimensions available with the specified name: " + dimensionName);
}
} else {
Expand Down Expand Up @@ -530,27 +534,8 @@ protected boolean checkCompliant(Variable var) {
+ "Proceeding with standard_name set");
}
}
} catch (NoSuchUnitException e1) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(e1.getLocalizedMessage());
}
} catch (UnitParseException e1) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(e1.getLocalizedMessage());
}
} catch (SpecificationException e1) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(e1.getLocalizedMessage());
}
} catch (UnitDBException e1) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(e1.getLocalizedMessage());
}
} catch (PrefixDBException e1) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(e1.getLocalizedMessage());
}
} catch (UnitSystemException e1) {
} catch (UnitDBException | SpecificationException |
PrefixDBException | UnitSystemException e1) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(e1.getLocalizedMessage());
}
Expand All @@ -561,11 +546,9 @@ protected boolean checkCompliant(Variable var) {
" UCAR unit so it doesn't allow to define a standard name");
}
}

}
}


// Return the result
return validName && validUOM;
}
Expand Down Expand Up @@ -595,19 +578,18 @@ protected void setPixel(int x, int y, DataType imageDataType,
case SHORT:
case INT:
sample = data.getSample(x, y, bandIdx);
validSample = !Double.isNaN(noDataValue) && !isNaN(sample, noDataValue);
validSample = !isNaN(sample, noDataValue);
if (unitConverter != null && validSample) {
sample = (int) unitConverter.convert(sample);
}
if (dataPacker != null) {

sample = dataPacker.pack((double) sample);
}
setIntegerSample(netCDFDataType, matrix, matrixIndex, sample);
break;
case FLOAT:
float sampleFloat = data.getSampleFloat(x, y, bandIdx);
validSample = !Double.isNaN(noDataValue) && !isNaN(sampleFloat, noDataValue);
validSample = !isNaN(sampleFloat, noDataValue);
if (unitConverter != null && validSample) {
sampleFloat = (float) unitConverter.convert(sampleFloat);
}
Expand All @@ -620,8 +602,8 @@ protected void setPixel(int x, int y, DataType imageDataType,
break;
case DOUBLE:
double sampleDouble = data.getSampleDouble(x, y, bandIdx);
validSample = !Double.isNaN(noDataValue) && !isNaN(sampleDouble, noDataValue);
if (unitConverter != null && !Double.isNaN(noDataValue) && !isNaN(sampleDouble, noDataValue)) {
validSample = !isNaN(sampleDouble, noDataValue);
if (unitConverter != null && validSample) {
sampleDouble = unitConverter.convert(sampleDouble);
}
if (dataPacker != null) {
Expand Down Expand Up @@ -651,6 +633,10 @@ protected void setIntegerSample(DataType netCDFDataType, Array matrix, Index mat
}

protected boolean isNaN(Number sample, double noDataValue) {
double sampleValue = sample.doubleValue();
if (Double.isNaN(noDataValue)) {
return Double.isNaN(sampleValue);
}
return (Math.abs(noDataValue - sample.doubleValue()) < EQUALITY_DELTA);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class NetCDFCRSWriter {
*/
private GridCoverage2D sampleGranule;

/** A map to assign a Dimension manager to each coordinate */
/** A map to assign a Dimension Mapping to each coordinate */
private Map<String, NetCDFDimensionMapping> coordinatesDimensions = new LinkedHashMap<String, NetCDFDimensionMapping>();

/** The underlying CoordinateReferenceSystem */
Expand Down Expand Up @@ -423,5 +423,7 @@ private Attribute getGeoTransformAttribute(MathTransform transform) {

}


public Set<String> getCoordinatesDimensionNames() {
return coordinatesDimensions.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -83,6 +84,7 @@ public class WCSNetCDFMosaicTest extends WCSNetCDFBaseTest {
public static QName VISIBILITYPACKED = new QName(CiteTestData.WCS_URI, "visibilityPacked", CiteTestData.WCS_PREFIX);
public static QName VISIBILITYCOMPRESSED = new QName(CiteTestData.WCS_URI, "visibilityCompressed", CiteTestData.WCS_PREFIX);
public static QName VISIBILITYCFPACKED = new QName(CiteTestData.WCS_URI, "visibilityCFPacked", CiteTestData.WCS_PREFIX);
public static QName VISIBILITYNANPACKED = new QName(CiteTestData.WCS_URI, "visibilityNaNPacked", CiteTestData.WCS_PREFIX);
public static QName TEMPERATURE_SURFACE = new QName(CiteTestData.WCS_URI, "Temperature_surface", CiteTestData.WCS_PREFIX);

private final static String STANDARD_NAME = "visibility_in_air";
Expand Down Expand Up @@ -150,6 +152,9 @@ protected void onSetUp(SystemTestData testData) throws Exception {
testData.addRasterLayer(VISIBILITYCFPACKED, "visibility.zip", null, null, this.getClass(), getCatalog());
setupNetCDFoutSettings(VISIBILITYCFPACKED);

testData.addRasterLayer(VISIBILITYNANPACKED, "visibility.zip", null, null, this.getClass(), getCatalog());
setupNetCDFoutSettings(VISIBILITYNANPACKED, false);

testData.addRasterLayer(VISIBILITYCOMPRESSED, "visibility.zip", null, null, this.getClass(), getCatalog());
setupNetCDFoutSettings(VISIBILITYCOMPRESSED);

Expand All @@ -161,6 +166,10 @@ protected void onSetUp(SystemTestData testData) throws Exception {
}

private void setupNetCDFoutSettings(QName name) {
setupNetCDFoutSettings(name, true);
}

private void setupNetCDFoutSettings(QName name, boolean setNoData) {
CoverageInfo info = getCatalog().getCoverageByName(getLayerId(name));

// Set the Declared SRS
Expand All @@ -179,7 +188,7 @@ private void setupNetCDFoutSettings(QName name) {
List<Double> nullValues = dimension.getNullValues();
if (nullValues != null) {
nullValues.clear();
nullValues.add(ORIGINAL_FILL_VALUE);
if (setNoData) nullValues.add(ORIGINAL_FILL_VALUE);
}

NetCDFLayerSettingsContainer container = new NetCDFLayerSettingsContainer();
Expand Down Expand Up @@ -470,6 +479,29 @@ public void testRequestNetCDFCFDataPacking() throws Exception {

dataset.close();
}

@Test
public void testRequestNetCDFNaNDataPacking() throws Exception {

// http response from the request inside the string
MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" +
"&coverageId=wcs__visibilityNaNPacked&format=application/x-netcdf");
assertNotNull(response);
byte[] netcdfOut = getBinary(response);
File file = File.createTempFile("netcdf", "outNaNPK.nc", new File("./target"));
FileUtils.writeByteArrayToFile(file, netcdfOut);

NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath());
Variable var = dataset.findVariable(STANDARD_NAME);
assertNotNull(var);

Array readData = var.read(NETCDF_SECTION);
assertEquals(DataType.SHORT, readData.getDataType());

// Check the fix on dataPacking NaN management
assertNotEquals(readData.getShort(0), -32768, 1E-6);
dataset.close();
}

private void addViewToCatalog() throws Exception {
final Catalog cat = getCatalog();
Expand Down

0 comments on commit d04094e

Please sign in to comment.