Java Code Examples for ucar.nc2.NetcdfFile#openInMemory()

The following examples show how to use ucar.nc2.NetcdfFile#openInMemory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CFPointObWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Open a ucar.nc2.dt.PointObsDataset, write out in CF point format.
 *
 * @param fileIn open through TypedDatasetFactory.open(FeatureType.POINT, ..)
 * @param fileOut write to this netcdf-3 file
 * @param inMemory if true, read file into memory for efficiency
 * @return true on success
 * @throws IOException on read/write error
 */
public static boolean rewritePointObsDataset(String fileIn, String fileOut, boolean inMemory) throws IOException {
  System.out.println("Rewrite2 .nc files from " + fileIn + " to " + fileOut + " inMemory= " + inMemory);

  long start = System.currentTimeMillis();

  // do it in memory for speed
  NetcdfFile ncfile = inMemory ? NetcdfFile.openInMemory(fileIn) : NetcdfFile.open(fileIn);
  NetcdfDataset ncd = new NetcdfDataset(ncfile);

  StringBuilder errlog = new StringBuilder();
  PointObsDataset pobsDataset = (PointObsDataset) TypedDatasetFactory.open(FeatureType.POINT, ncd, null, errlog);
  if (pobsDataset == null)
    return false;

  writePointObsDataset(pobsDataset, fileOut);
  pobsDataset.close();

  long took = System.currentTimeMillis() - start;
  System.out.println(" that took " + (took - start) + " msecs");
  return true;
}
 
Example 2
Source File: CFPointObWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Open a ucar.nc2.ft.PointFeatureCollection, write out in CF point format.
 *
 * @param fileIn open through TypedDatasetFactory.open(FeatureType.POINT, ..)
 * @param fileOut write to this netcdf-3 file
 * @param inMemory if true, read file into memory for efficiency
 * @return true on success
 * @throws IOException on read/write error
 */
public static boolean rewritePointFeatureDataset(String fileIn, String fileOut, boolean inMemory) throws IOException {
  System.out.println("Rewrite2 .nc files from " + fileIn + " to " + fileOut + " inMemory= " + inMemory);

  long start = System.currentTimeMillis();

  // do it in memory for speed
  NetcdfFile ncfile = inMemory ? NetcdfFile.openInMemory(fileIn) : NetcdfFile.open(fileIn);
  NetcdfDataset ncd = new NetcdfDataset(ncfile);

  Formatter errlog = new Formatter();
  FeatureDataset fd = FeatureDatasetFactoryManager.wrap(FeatureType.ANY_POINT, ncd, null, errlog);
  if (fd == null)
    return false;

  if (fd instanceof FeatureDatasetPoint) {
    writePointFeatureCollection((FeatureDatasetPoint) fd, fileOut);
    fd.close();
    long took = System.currentTimeMillis() - start;
    System.out.println(" that took " + (took - start) + " msecs");
    return true;
  }

  return false;

}
 
Example 3
Source File: AllVariablesSubsettingTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void shouldGetAllVariables() throws Exception {
  MvcResult mvc = this.mockMvc.perform(requestBuilder).andReturn();
  assertEquals(200, mvc.getResponse().getStatus());

  // Open the binary response in memory
  // NetcdfFile nf = NetcdfFile.openInMemory("test_data.ncs", response.getContentAsByteArray() );
  NetcdfFile nf;
  GridDataset gdsDataset;
  if (TestDir.cdmUseBuilders) {
    nf = NetcdfFiles.openInMemory("test_data.ncs", mvc.getResponse().getContentAsByteArray());
    gdsDataset =
        new ucar.nc2.dt.grid.GridDataset(NetcdfDatasets.enhance(nf, NetcdfDataset.getDefaultEnhanceMode(), null));
  } else {
    nf = NetcdfFile.openInMemory("test_data.ncs", mvc.getResponse().getContentAsByteArray());
    gdsDataset = new ucar.nc2.dt.grid.GridDataset(new NetcdfDataset(nf));
  }

  assertTrue(gdsDataset.getCalendarDateRange().isPoint());
  assertEquals(7, gdsDataset.getDataVariables().size());

}
 
Example 4
Source File: NcssGridIntegrationTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void openBinaryOld(byte[] content, String gridName) throws IOException {
  try (NetcdfFile nf = NetcdfFile.openInMemory("test_data.nc", content)) {
    GridDataset gdsDataset = new GridDataset(new NetcdfDataset(nf));
    assertNotNull(gdsDataset.findGridByName(gridName));
    logger.debug("{}", nf);
  }
}
 
Example 5
Source File: TestGridAsPointP.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void checkGridAsPointNetcdfOld(byte[] content, String varName) throws JDOMException, IOException {
  // Open the binary response in memory
  Formatter errlog = new Formatter();
  try (NetcdfFile nf = NetcdfFile.openInMemory("checkGridAsPointNetcdf.nc", content)) {
    FeatureDataset fd = FeatureDatasetFactoryManager.wrap(FeatureType.STATION, new NetcdfDataset(nf), null, errlog);
    assertNotNull(errlog.toString(), fd);
    VariableSimpleIF v = fd.getDataVariable(varName);
    assertNotNull(varName, v);
  }
}
 
Example 6
Source File: TestCase.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a netCDF reader from the UCAR library for the specified data set.
 * We use the UCAR library as a reference implementation for the tests.
 *
 * @param  file  the dataset as one of the {@code NETCDF_*} constants.
 * @return the decoder for the specified dataset.
 * @throws IOException if an I/O error occurred while opening the file.
 */
protected static NetcdfFile createUCAR(final TestData file) throws IOException {
    /*
     * Binary netCDF files need to be read either from a file, or from a byte array in memory.
     * Reading from a file is not possible if the test file is in geoapi-conformance JAR file.
     * But since those test files are less than 15 kilobytes, loading them in memory is okay.
     */
    String location = file.location().toString();
    location = location.substring(location.lastIndexOf('/') + 1);
    return NetcdfFile.openInMemory(location, file.content());
}