Skip to content

Commit

Permalink
add test for applying enhancements in the TDS
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyajohnson committed Nov 30, 2023
1 parent dce8bd2 commit 7293ca4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package thredds.server.services;

import org.junit.Ignore;
import org.junit.Test;
import thredds.test.util.TestOnLocalServer;
import thredds.util.ContentType;
import ucar.ma2.Array;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFiles;
import ucar.nc2.Variable;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import static com.google.common.truth.Truth.assertThat;

public class TestEnhancements {

final static String ENHANCED_FILE = "localContent/testOffset.nc";
final static String NCML_ENHANCED_FILE = "testOffsetWithNcml.nc";
final static String ENHANCED_VAR_NAME = "variableWithOffset";
final static String NOT_ENHANCED_VAR_NAME = "variableWithoutOffset";

@Test
public void testNCSSWithEnhancements() throws IOException {
// scale-offset set as variable attribute
final String endpoint = TestOnLocalServer
.withHttpPath("/ncss/grid/" + ENHANCED_FILE + "?var=" + ENHANCED_VAR_NAME + "&var=" + NOT_ENHANCED_VAR_NAME);
final byte[] content = TestOnLocalServer.getContent(endpoint, HttpServletResponse.SC_OK, ContentType.netcdf);

try (NetcdfFile netcdfFile = NetcdfFiles.openInMemory("test_data.nc", content)) {
final Variable enhancedVar = netcdfFile.findVariable(ENHANCED_VAR_NAME);
final Variable orgVar = netcdfFile.findVariable(NOT_ENHANCED_VAR_NAME);
assertThat(enhancedVar != null).isTrue();
assertThat(orgVar != null).isTrue();
assertThat(enhancedVar.findAttribute("add_offset")).isNull();
final Array values1 = enhancedVar.read();
final Array values2 = orgVar.read();
for (int i = 0; i < values1.getSize(); i++) {
assertThat(values1.getDouble(i)).isEqualTo(values2.getDouble(i));
}
}
}

@Ignore("TODO: fix services to be consistent in how they apply ncml and enhancements")
@Test
public void testNCSSWithEnhancementsNcML() throws IOException {
// scale-offset set as variable attribute
final String endpoint = TestOnLocalServer.withHttpPath(
"/ncss/grid/" + NCML_ENHANCED_FILE + "?var=" + ENHANCED_VAR_NAME + "&var=" + NOT_ENHANCED_VAR_NAME);
final byte[] content = TestOnLocalServer.getContent(endpoint, HttpServletResponse.SC_OK, ContentType.netcdf);

try (NetcdfFile netcdfFile = NetcdfFiles.openInMemory("test_data.nc", content)) {
final Variable enhancedVar = netcdfFile.findVariable(ENHANCED_VAR_NAME);
final Variable orgVar = netcdfFile.findVariable(NOT_ENHANCED_VAR_NAME);
assertThat(enhancedVar != null).isTrue();
assertThat(orgVar != null).isTrue();
assertThat(orgVar.findAttribute("add_offset")).isNull();
final Array values1 = enhancedVar.read();
final Array values2 = orgVar.read();
for (int i = 0; i < values1.getSize(); i++) {
assertThat(values1.getDouble(i)).isEqualTo(values2.getDouble(i) + 100);
}
}
}
}
12 changes: 12 additions & 0 deletions tds/src/test/content/thredds/catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@

<dataset name="Test Single Dataset" ID="testDataset" serviceName="odap" urlPath="localContent/testData.nc" dataType="Grid"/>
<dataset name="Test Grid as Point" ID="testGAP" serviceName="all" urlPath="localContent/testGridAsPoint.nc" dataType="Grid"/>
<dataset name="Test Enhancements">
<dataset name="With Attribute" ID="withAttribute" serviceName="all" urlPath="localContent/testOffset.nc" dataType="Grid"/>
<dataset name="With NcML" ID="withNcMl" serviceName="all" urlPath="testOffsetWithNcml.nc">
<serviceName>all</serviceName>
<ncml:netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" enhance="all"
location="content/testdata/testOffset.nc">
<variable name="variableWithoutOffset">
<attribute name="add_offset" value="-100"/>
</variable>
</ncml:netcdf>
</dataset>
</dataset>

<dataset name="Test Dap4 Dataset" ID="testDap4Dataset" serviceName="dap4"
urlPath="cdmUnitTest/ft/coverage/03061219_ruc.nc" dataType="Grid"/>
Expand Down

0 comments on commit 7293ca4

Please sign in to comment.