Java Code Examples for ucar.nc2.dataset.NetcdfDataset#openDataset()

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#openDataset() . 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: DatasetViewer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void compareBuilderDS() {
  if (ds == null)
    return;
  String fileLocation = ds.getLocation();
  try (NetcdfDataset org = NetcdfDataset.openDataset(fileLocation)) {
    try (NetcdfDataset withBuilder = NetcdfDatasets.openDataset(fileLocation)) {
      Formatter f = new Formatter();
      CompareNetcdf2 compare = new CompareNetcdf2(f, false, false, true);
      compare.compare(org, withBuilder, new CoordsObjFilter());
      infoTA.setText(f.toString());
      infoTA.gotoTop();
      infoWindow.setTitle("Compare Old (file1) with Builder (file 2)");
      infoWindow.show();
    }
  } catch (Throwable ioe) {
    StringWriter sw = new StringWriter(10000);
    ioe.printStackTrace(new PrintWriter(sw));
    infoTA.setText(sw.toString());
    infoTA.gotoTop();
    infoWindow.show();
  }
}
 
Example 2
Source File: TestGridSubsetCoordinateSystem.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCoordinateSystemDomain() throws Exception {
  System.err.printf("%nOpen %s grid='%s'%n", filename, gridName);
  Grib.setDebugFlags(new DebugFlagsImpl("Grib/indexOnly"));

  try (NetcdfDataset ncd = NetcdfDataset.openDataset(filename)) {
    Assert.assertNotNull(filename, ncd);
    VariableDS vds = (VariableDS) ncd.findVariable(gridName);
    Assert.assertNotNull(gridName, vds);
    for (CoordinateSystem cs : vds.getCoordinateSystems()) {
      System.err.printf("  CoordinateSystem= '%s'%n", cs);
      testDomain("CoordinateSystem ", vds.getDimensions(), cs.getCoordinateAxes());
    }

  } finally {
    Grib.setDebugFlags(new DebugFlagsImpl(""));
  }
}
 
Example 3
Source File: NCdumpPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void run(Object o) throws IOException {
  try {
    boolean useBuilders = ToolsUI.getToolsUI().getUseBuilders();
    if (useCoords) {
      ncfile = useBuilders ? NetcdfDatasets.openDataset(filename, true, null)
          : NetcdfDataset.openDataset(filename, true, null);
    } else {
      DatasetUrl durl = DatasetUrl.findDatasetUrl(filename);
      ncfile = useBuilders ? NetcdfDatasets.openFile(durl, -1, null, null) : NetcdfDataset.openFile(filename, null);
    }

    StringWriter sw = new StringWriter(50000);
    Ncdump.ncdump(ncfile, command, sw, task);
    result = sw.toString();
  } finally {
    try {
      if (ncfile != null) {
        ncfile.close();
      }
      ncfile = null;
    } catch (IOException ioe) {
      System.out.printf("Error closing %n");
    }
  }
}
 
Example 4
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = IOException.class)
public void testNcmlDatasetNoProtocolInNcmlAbsPath() throws IOException, InvalidRangeException {
  // if using an absolute path in the NcML file location attr of the element netcdf, then
  // you must prepend file:
  // this should fail with an IOException
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExisting6.xml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 5
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testPolygon() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/outflow_3seg_5timesteps_vlen.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the cfConvention
  expected = cfConvention;
  found = ncd.getConventionUsed();
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
Example 6
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testLine() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_vlen_3hru_5timestep.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the cfConvention
  expected = cfConvention;
  found = ncd.getConventionUsed();
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
Example 7
Source File: TestAggUnionSimple.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@BeforeClass
public static void setUp() throws IOException {
  if (ncfile != null)
    return;
  String filename = "file:./" + TestNcMLRead.topDir + "aggUnionSimple.xml";
  ncfile = NetcdfDataset.openDataset(filename, false, null);
}
 
Example 8
Source File: TestNcmlReaderProblems.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void compareDS(String ncmlLocation) throws IOException {
  System.out.printf("Compare NetcdfDataset.openDataset %s%n", ncmlLocation);
  try (NetcdfDataset org = NetcdfDataset.openDataset(ncmlLocation)) {
    try (NetcdfDataset withBuilder = NetcdfDatasets.openDataset(ncmlLocation)) {
      Formatter f = new Formatter();
      CompareNetcdf2 compare = new CompareNetcdf2(f, false, false, true);
      boolean ok = compare.compare(org, withBuilder, new CoordsObjFilter());
      System.out.printf("%s %s%n", ok ? "OK" : "NOT OK", f);
      assertThat(ok).isTrue();
    }
  }
}
 
Example 9
Source File: TestMiscPointFeature.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTryWithWrap() throws IOException {
  String location = TestDir.cdmLocalFromTestDataDir + "testWrite.nc";
  NetcdfDataset ncd = NetcdfDataset.openDataset(location);
  Formatter errlog = new Formatter();
  try (FeatureDataset fdataset = FeatureDatasetFactoryManager.wrap(null, ncd, null, errlog)) {
    assert (fdataset == null);
  }
  ncd.close();
}
 
Example 10
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDatasetWcoords() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExistingWcoords.xml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" testNcmlDatasetWcoords.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
  logger.debug(" testNcmlDatasetWcoords.closed ");
}
 
Example 11
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDatasetNoProtocolInNcmlRelPath() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExisting7.xml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 12
Source File: TestH5.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static NetcdfDataset openH5dataset(String filename) {
  try {
    System.out.println("**** Open " + testDir + filename);
    NetcdfDataset ncfile = NetcdfDataset.openDataset(testDir + filename);
    if (TestH5.dumpFile)
      System.out.println("open H5 " + ncfile);
    return ncfile;

  } catch (java.io.IOException e) {
    System.out.println(" fail = " + e);
    e.printStackTrace();
    assert (false);
    return null;
  }
}
 
Example 13
Source File: DsgSubsetWriterTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static boolean compareNetCDFOld(File expectedResultFile, File actualResultFile) throws IOException {
  try (NetcdfFile expectedNcFile = NetcdfDataset.openDataset(expectedResultFile.getAbsolutePath());
      NetcdfFile actualNcFile = NetcdfDataset.openDataset(actualResultFile.getAbsolutePath())) {
    Formatter formatter = new Formatter();
    boolean contentsAreEqual = new CompareNetcdf2(formatter, false, false, true).compare(expectedNcFile, actualNcFile,
        new NcssNetcdfObjFilter());

    if (!contentsAreEqual) {
      System.err.println(formatter.toString());
    }

    return contentsAreEqual;
  }
}
 
Example 14
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNcmlDataset() throws IOException, InvalidRangeException {
  String filename = "file:./" + TestNcMLRead.topDir + "aggExisting.xml";

  NetcdfFile ncfile = NetcdfDataset.openDataset(filename, true, null);
  logger.debug(" TestNcmlAggExisting.open {}", filename);

  testDimensions(ncfile);
  testCoordVar(ncfile);
  testAggCoordVar(ncfile);
  testReadData(ncfile);
  testReadSlice(ncfile);
  ncfile.close();
}
 
Example 15
Source File: TestNcMLequals.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testEnhanceEquals(String ncmlLocation) throws IOException {
  System.out.println("testEnhanceEquals");
  try (NetcdfDataset ncml = NcMLReader.readNcML(ncmlLocation, null);
      NetcdfDataset ncmlEnhanced = new NetcdfDataset(ncml, true)) {

    String locref = ncml.getReferencedFile().getLocation();
    NetcdfDataset ncdrefEnhanced = NetcdfDataset.openDataset(locref, true, null);

    Assert
        .assertTrue(CompareNetcdf2.compareFiles(ncmlEnhanced, ncdrefEnhanced, new Formatter(), false, false, false));
  }
}
 
Example 16
Source File: MemoryCounterAgentTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static void testNcd() throws IOException {
  String filename = "C:/data/test2.nc";
  NetcdfDataset ncfile = NetcdfDataset.openDataset(filename);
  measureSize("C:/data/test2.nc", ncfile, null, true);

  NetcdfDatasetInfo info = new NetcdfDatasetInfo(filename);
  measureSize("info", info, null, true);
  String pifo = info.getParseInfo();
  System.out.println("info= " + pifo);
  ncfile.close();
}
 
Example 17
Source File: NcmlEditor.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read text from textArea through NcMLReader
 * then write it back out via resulting dataset
 */
private void checkNcml(Formatter f) {
  if (ncmlLocation == null) {
    return;
  }
  boolean useBuilders = ToolsUI.getToolsUI().getUseBuilders();
  try (NetcdfDataset ncd =
      useBuilders ? NetcdfDatasets.openDataset(ncmlLocation) : NetcdfDataset.openDataset(ncmlLocation)) {
    ncd.check(f);
  } catch (IOException ioe) {
    JOptionPane.showMessageDialog(this, "ERROR: " + ioe.getMessage());
    ioe.printStackTrace();
  }
}
 
Example 18
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCoordinateVariable() throws IOException {
  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/outflow_3seg_5timesteps_vlen.nc";
  // open the test file
  try (NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile)) {
    for (CoordinateAxis axis : ncd.getCoordinateAxes()) {
      System.out.printf("Try to read %s ", axis.getFullName());
      Array data = axis.read();
      System.out.printf(" OK (%d) %n", data.getSize());
    }
  }
}
 
Example 19
Source File: TestAggFmrc.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read aggregation of unsigned variable for test
 */
@Before
public void prepAggDataset() {
  String filenameScan = "file:./" + TestNcMLRead.topDir + "fmrc/" + FILENAME_SCAN;
  String filenameExplicit = "file:./" + TestNcMLRead.topDir + "fmrc/" + FILENAME_EXPLICIT;
  String filenameA = "file:./" + TestNcMLRead.topDir + "fmrc/" + FILENAME_A;
  String filenameB = "file:./" + TestNcMLRead.topDir + "fmrc/" + FILENAME_B;
  String filenameC = "file:./" + TestNcMLRead.topDir + "fmrc/" + FILENAME_C;
  try {

    fmrcScan = Fmrc.open(filenameScan, null);
    Assert.assertNotNull(fmrcScan);
    GridDataset bestScan = fmrcScan.getDatasetBest();
    Variable bestScanVar = bestScan.findGridByName(AGG_VAR_NAME).getVariable();
    valuesBestScanVar = bestScanVar.read();

    fmrcExplicit = Fmrc.open(filenameExplicit, null);
    Assert.assertNotNull(fmrcExplicit);
    GridDataset bestExplicit = fmrcExplicit.getDatasetBest();
    Variable bestExplicitVar = bestExplicit.findGridByName(AGG_VAR_NAME).getVariable();
    valuesBestExplicitVar = bestExplicitVar.read();

    ncfileScan = NcMLReader.readNcML(filenameScan, null);
    varScan = ncfileScan.findVariable(AGG_VAR_NAME);
    valuesScan = varScan.read();

    ncfileExplicit = NcMLReader.readNcML(filenameExplicit, null);
    varExplicit = ncfileScan.findVariable(AGG_VAR_NAME);
    valuesExplicit = varExplicit.read();

    ncfileA = NetcdfDataset.openDataset(filenameA);
    varA = ncfileA.findVariable(AGG_VAR_NAME);
    valuesA = varA.read();

    ncfileB = NetcdfDataset.openDataset(filenameB);
    varB = ncfileB.findVariable(AGG_VAR_NAME);
    valuesB = varB.read();

    ncfileC = NetcdfDataset.openDataset(filenameC);
    varC = ncfileC.findVariable(AGG_VAR_NAME);
    valuesC = varC.read();

  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
Example 20
Source File: MemoryCounterAgentTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static void testNcml() throws IOException {
  String filename = "C:/dev/tds/thredds/cdm/src/test/data/ncml/aggUnionSimple.xml";
  NetcdfDataset ncfile = NetcdfDataset.openDataset(filename, false, null);
  measureSize("aggUnionSimple", ncfile, null, true);
  ncfile.close();
}