2

I'm experimenting about netcdf and how to convert them in a feature collection. My primary tool is geotools ver. 17.2. Moving my very first step was to bring these two dependencies:

...    
<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-coverage</artifactId>
  <version>${geotools.version}</version>
</dependency>
<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-netcdf</artifactId>
  <version>${geotools.version}</version>
</dependency>
...

and write this:

public void readSupportedDataByGeotools() throws Exception {
  NetCDFReader netCDFReader = null;
  try {
    netCDFReader = new NetCDFReader(resourceUrl, null);
    CoordinateReferenceSystem crs = netCDFReader.getCoordinateReferenceSystem();
    String[] coverageNames = netCDFReader.getGridCoverageNames();
    for (String name : coverageNames) {
    GridCoverage2D grid = netCDFReader.read(name, null);
    log.info(String.format("Name: %s, dimension: %d.", name, grid.getDimension()));
    }
  } finally {
      if (netCDFReader != null) {
        netCDFReader.dispose();
      }
  }
}

Simply iterate over netcdf variables. In the code, resourceUrl points to a netcdf file who contains some time series. File is valid, I can open it with unidata toolsui This is what I get back when I run that code:

org.geotools.data.DataSourceException: Unable to connect
at org.geotools.coverage.io.netcdf.NetCDFReader.<init>(NetCDFReader.java:159)
at edu.pezzati.transformation.NetCDFReaderTest.readSupportedDataByGeotools(NetCDFReaderTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.geotools.data.DataSourceException: java.io.IOException: java.lang.IllegalArgumentException: No valid coverages name to be written
at org.geotools.coverage.io.netcdf.NetCDFAccess.<init>(NetCDFAccess.java:126)
at org.geotools.coverage.io.netcdf.NetCDFDriver.connect(NetCDFDriver.java:92)
at org.geotools.coverage.io.impl.DefaultFileDriver.process(DefaultFileDriver.java:216)
at org.geotools.coverage.io.netcdf.NetCDFReader.<init>(NetCDFReader.java:157)
... 25 more
Caused by: java.lang.RuntimeException: java.io.IOException: java.lang.IllegalArgumentException: No valid coverages name to be written
at org.geotools.imageio.netcdf.NetCDFImageReader.setInput(NetCDFImageReader.java:268)
at javax.imageio.ImageReader.setInput(ImageReader.java:380)
at org.geotools.coverage.io.netcdf.NetCDFAccess.<init>(NetCDFAccess.java:115)
... 28 more
Caused by: java.io.IOException: java.lang.IllegalArgumentException: No valid coverages name to be written
at org.geotools.imageio.netcdf.NetCDFImageReader.initIndex(NetCDFImageReader.java:369)
at org.geotools.imageio.netcdf.NetCDFImageReader.init(NetCDFImageReader.java:476)
at org.geotools.imageio.netcdf.NetCDFImageReader.setInput(NetCDFImageReader.java:266)
... 30 more
Caused by: java.lang.IllegalArgumentException: No valid coverages name to be written
at org.geotools.imageio.netcdf.AncillaryFileManager.storeIndexer(AncillaryFileManager.java:387)
at org.geotools.imageio.netcdf.AncillaryFileManager.writeToDisk(AncillaryFileManager.java:373)
at org.geotools.imageio.netcdf.NetCDFImageReader.initIndex(NetCDFImageReader.java:357)
... 32 more

What am I missing?

UPDATE

Following Ian suggestion, I set the two properties this way:

@BeforeClass
public static void init() {
  JAIExt.initJAIEXT(true, true);
  System.setProperty("netcdf.coordinates.enablePlugins", "true");
}

@Before
public void initTest() {
  resourceUrl = Thread.currentThread().getContextClassLoader().getResource("exampleh2.nc");
}

@Test
public void readSupportedDataByGeotools() throws Exception {
...

and now I get this error:

org.geotools.data.DataSourceException: Unable to connect
at org.geotools.coverage.io.netcdf.NetCDFReader.<init>(NetCDFReader.java:159)
at edu.pezzati.transformation.NetCDFReaderTest.readSupportedDataByGeotools(NetCDFReaderTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.geotools.data.DataSourceException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at org.geotools.coverage.io.netcdf.NetCDFAccess.<init>(NetCDFAccess.java:126)
at org.geotools.coverage.io.netcdf.NetCDFDriver.connect(NetCDFDriver.java:92)
at org.geotools.coverage.io.impl.DefaultFileDriver.process(DefaultFileDriver.java:216)
at org.geotools.coverage.io.netcdf.NetCDFReader.<init>(NetCDFReader.java:157)
... 26 more
Caused by: java.lang.RuntimeException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at org.geotools.imageio.netcdf.NetCDFImageReader.setInput(NetCDFImageReader.java:268)
at javax.imageio.ImageReader.setInput(ImageReader.java:380)
at org.geotools.coverage.io.netcdf.NetCDFAccess.<init>(NetCDFAccess.java:115)
... 29 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
at org.geotools.imageio.netcdf.NetCDFImageReader.settingTypeNames(NetCDFImageReader.java:505)
at org.geotools.imageio.netcdf.NetCDFImageReader.init(NetCDFImageReader.java:465)
at org.geotools.imageio.netcdf.NetCDFImageReader.setInput(NetCDFImageReader.java:266)
... 31 more

UPDATE

Using unidata libs I can iterate over netcdf's variables and dimensions Here is what I did:

@Test
public void readSupportedDataByUnimav() throws Exception {
  NetcdfFile ncdf = NetcdfDataset.openFile(resourceUrl.toURI().toString(), null);
  List<Variable> variables = ncdf.getRootGroup().getVariables();
  if (!variables.isEmpty())
    log.info("Variables:");
  for (Variable var : variables) {
    log.info(String.format("- name: %s, name and dimensions: %s.", var.getFullName(),
        var.getNameAndDimensions()));
  }
  List<Dimension> dimensions = ncdf.getDimensions();
  if (!dimensions.isEmpty()) {
    log.info("Dimensions:");
  }
  for (Dimension dim : dimensions) {
    log.info(String.format("- name: %s, length: %d.", dim.getFullName(), dim.getLength()));
  }
}
  • is resourceUrl a URL or a string? and did you set JAIExt.initJAIEXT(true, true); System.setProperty("netcdf.coordinates.enablePlugins", "true"); – Ian Turton Jan 9 '18 at 14:59
  • Hello @IanTurton. resourceUrl is a URL. I set the properties you mention and now I get a new error, I update my question to quote that. Thankyou. – Francesco Jan 10 '18 at 7:44
  • It seems that the NetCDFImageReader expects its AncillaryFileManager to bring some coverage names.. Maybe I misconfigure something. – Francesco Jan 10 '18 at 7:58
  • Can you run your code against one of the test data files in github.com/geotools/geotools/tree/master/modules/plugin/… to see if it your data or your code that is bad? – Ian Turton Jan 10 '18 at 9:05
  • 1
    so your file is broken, non-standard or doing something unexpected? can you go back to the supplier? – Ian Turton Jan 10 '18 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.