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

The following examples show how to use ucar.nc2.NetcdfFiles#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: TestOpenInMemory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean scanBufrFile(String filename)
    throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
  int count = 0;
  try (RandomAccessFile raf = new RandomAccessFile(filename, "r")) {

    MessageScanner scan = new MessageScanner(raf);
    while (scan.hasNext()) {
      Message m = scan.next();
      if (m == null)
        continue;
      byte[] mbytes = scan.getMessageBytes(m);
      try (NetcdfFile ncfile = NetcdfFiles.openInMemory("test", mbytes, "ucar.nc2.iosp.bufr.BufrIosp2")) {
        NetcdfDataset ncd = new NetcdfDataset(ncfile);
      }
    }
  }
  return true;
}
 
Example 2
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 3
Source File: TestBufr2Xml.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testStuff() throws Exception {
  String unitDir = TestDir.cdmUnitTestDir + "datasets/bufr/exclude/";
  String filename = unitDir + "uniqueExamples.bufr";

  int size = 0;
  int count = 0;

  try (RandomAccessFile raf = new RandomAccessFile(filename, "r"); OutputStream out = new ByteArrayOutputStream()) {
    MessageScanner scan = new MessageScanner(raf);
    while (scan.hasNext()) {
      Message message = scan.next();
      if (message == null || !message.isTablesComplete() || !message.isBitCountOk())
        continue;
      byte[] mbytes = scan.getMessageBytesFromLast(message);
      NetcdfFile ncfile = NetcdfFiles.openInMemory("test", mbytes, "ucar.nc2.iosp.bufr.BufrIosp");
      NetcdfDataset ncd = new NetcdfDataset(ncfile);
      new Bufr2Xml(message, ncd, out, true);
      out.close();
      count++;
      size += message.getMessageSize();
    }

  } catch (Throwable e) {
    e.printStackTrace();
  }

  System.out.printf("total size= %f Kb %n", .001 * size);
}
 
Example 4
Source File: TestWcsServer.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testVert() throws IOException {
  String endpoint = TestOnLocalServer.withHttpPath(
      dataset1 + "&request=GetCoverage&COVERAGE=Temperature&TIME=2012-04-19T00:00:00Z&FORMAT=NetCDF3&vertical=800");
  byte[] content = TestOnLocalServer.getContent(endpoint, 200, null);

  // Open the binary response in memory
  try (NetcdfFile nf = NetcdfFiles.openInMemory("test_data.nc", content)) {
    DtCoverageDataset dt = getDtCoverageDataset(nf);
    Formatter errlog = new Formatter();
    FeatureDatasetCoverage cdc = DtCoverageAdapter.factory(dt, errlog);
    Assert.assertNotNull(errlog.toString(), cdc);
    Assert.assertEquals(1, cdc.getCoverageCollections().size());
    CoverageCollection cd = cdc.getCoverageCollections().get(0);
    assertNotNull(cd);

    Coverage cov = cd.findCoverage("Temperature");
    assertNotNull("Temperature", cov);

    CoverageCoordSys csys = cov.getCoordSys();
    assertNotNull("csys", csys);

    CoverageCoordAxis1D time = (CoverageCoordAxis1D) csys.getAxis(AxisType.Time);
    assertNotNull("time", time);
    Assert.assertEquals(1, time.getNcoords());
    CalendarDate date = time.makeDate(time.getCoordMidpoint(0));
    logger.debug("date = {}", date);
    Assert.assertEquals(date, CalendarDate.parseISOformat(null, "2012-04-19T00:00:00Z"));

    CoverageCoordAxis1D vert = (CoverageCoordAxis1D) csys.getZAxis();
    assertNotNull("vert", vert);
    Assert.assertEquals(1, vert.getNcoords());
    double vertCoord = vert.getCoordMidpoint(0);
    logger.debug("date = {}", date);
    Assert2.assertNearlyEquals(800.0, vertCoord);
  }
}
 
Example 5
Source File: NcssGridIntegrationTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void openBinaryNew(byte[] content, String gridName) throws IOException {
  try (NetcdfFile nf = NetcdfFiles.openInMemory("test_data.nc", content)) {
    GridDataset gdsDataset = new GridDataset(NetcdfDatasets.enhance(nf, NetcdfDataset.getDefaultEnhanceMode(), null));
    assertNotNull(gdsDataset.findGridByName(gridName));
    logger.debug("{}", nf);
  }
}
 
Example 6
Source File: TestGridAsPointP.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void checkGridAsPointNetcdfNew(byte[] content, String varName) throws JDOMException, IOException {
  // Open the binary response in memory
  Formatter errlog = new Formatter();
  try (NetcdfFile nf = NetcdfFiles.openInMemory("checkGridAsPointNetcdf.nc", content)) {
    FeatureDataset fd = FeatureDatasetFactoryManager.wrap(FeatureType.STATION,
        NetcdfDatasets.enhance(nf, NetcdfDataset.getDefaultEnhanceMode(), null), null, errlog);
    assertNotNull(errlog.toString(), fd);
    VariableSimpleIF v = fd.getDataVariable(varName);
    assertNotNull(varName, v);
  }
}
 
Example 7
Source File: TestWcsServer.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testGetCoverageNetcdf() throws IOException {
  String endpoint = TestOnLocalServer.withHttpPath(
      dataset2 + "&request=GetCoverage&COVERAGE=sst&BBOX=10,0,300,80&TIME=2002-12-01T00:00:00Z&FORMAT=NetCDF3");
  byte[] content = TestOnLocalServer.getContent(endpoint, 200, null);

  // Open the binary response in memory
  try (NetcdfFile nf = NetcdfFiles.openInMemory("test_data.nc", content)) {
    DtCoverageDataset dt = getDtCoverageDataset(nf);
    Formatter errlog = new Formatter();
    FeatureDatasetCoverage cdc = DtCoverageAdapter.factory(dt, errlog);
    Assert.assertNotNull(errlog.toString(), cdc);
    Assert.assertEquals(1, cdc.getCoverageCollections().size());
    CoverageCollection cd = cdc.getCoverageCollections().get(0);
    assertNotNull(cd);

    Coverage cov = cd.findCoverage("sst");
    assertNotNull("sst", cov);

    CoverageCoordSys csys = cov.getCoordSys();
    assertNotNull("csys", csys);

    CoverageCoordAxis1D time = (CoverageCoordAxis1D) csys.getAxis(AxisType.Time);
    assertNotNull("time", time);
    Assert.assertEquals(1, time.getNcoords());
    CalendarDate date = time.makeDate(time.getCoordMidpoint(0));
    logger.debug("date = {}", date);
    CalendarDate expected = CalendarDate.parseISOformat(Calendar.gregorian.toString(), "2002-12-01T00:00:00Z"); // CF
                                                                                                                // i
                                                                                                                // guess
    Assert.assertEquals(expected.getMillis(), date.getMillis());
    Assert.assertEquals(expected.getCalendar(), date.getCalendar());
    Assert.assertEquals(expected, date);

    HorizCoordSys hcs = csys.getHorizCoordSys();
    CoverageCoordAxis1D xaxis = hcs.getXAxis();
    Assert.assertEquals(291, xaxis.getNcoords());
    Assert2.assertNearlyEquals(10.5, xaxis.getCoordMidpoint(0));
    Assert2.assertNearlyEquals(300.5, xaxis.getEndValue()); // LOOK is that ok? BB = 10-300: its just catching the
                                                            // edge
    Assert2.assertNearlyEquals(1.0, xaxis.getResolution());

    CoverageCoordAxis1D yaxis = hcs.getYAxis();
    Assert.assertEquals(81, yaxis.getNcoords());
    Assert2.assertNearlyEquals(79.5, yaxis.getCoordMidpoint(0));
    Assert2.assertNearlyEquals(-.5, yaxis.getEndValue()); // LOOK is that ok? BB = 0-80: its just catching the edge
    Assert2.assertNearlyEquals(-1.0, yaxis.getResolution());
  }
}