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

The following examples show how to use ucar.nc2.NetcdfFiles#open() . 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 compareBuilder() {
  if (ds == null)
    return;
  String fileLocation = ds.getLocation();
  try (NetcdfFile org = NetcdfFile.open(fileLocation)) {
    try (NetcdfFile withBuilder = NetcdfFiles.open(fileLocation)) {
      Formatter f = new Formatter();
      CompareNetcdf2 compare = new CompareNetcdf2(f, false, false, true);
      boolean ok = 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: TestNc4IospReading.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean doCompare(String location, boolean showCompare, boolean showEach, boolean compareData)
    throws IOException {
  try (NetcdfFile ncfile = NetcdfFiles.open(location); NetcdfFile jni = openJni(location)) {
    jni.setLocation(location + " (jni)");
    // System.out.printf("Compare %s to %s%n", ncfile.getIosp().getClass().getName(),
    // jni.getIosp().getClass().getName());

    Formatter f = new Formatter();
    CompareNetcdf2 tc = new CompareNetcdf2(f, showCompare, showEach, compareData);
    boolean ok = tc.compare(ncfile, jni, new CompareNetcdf2.Netcdf4ObjectFilter());
    System.out.printf(" %s compare %s ok = %s%n", ok ? "" : "***", location, ok);
    if (!ok || (showCompare && showCompareResults))
      System.out.printf("%s%n=====================================%n", f);
    return ok;
  }
}
 
Example 3
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code1_spherical_specified_bad() throws IOException {
  String filename = dir + "sfc_d01_20080430_1200_f00000.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("LambertConformal_Projection");
    Attribute axis = v.findAttribute("earth_radius");
    Assert.assertEquals(6371200., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 4
Source File: TestDataTemplate.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDrs2() throws IOException {
  final String testfile = "../grib/src/test/data/ds.snow.grib2";
  try (NetcdfFile nc = NetcdfFiles.open(testfile)) {
    Variable var = nc.findVariable("Total_snowfall_surface_6_Hour_Accumulation");
    float[] data = (float[]) var.read().get1DJavaArray(DataType.FLOAT);

    Assert.assertTrue(Float.isNaN(data[0]));
    Assert.assertTrue(Float.isNaN(data[1234]));
  }
}
 
Example 5
Source File: TestH4iospNewProblem.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void compareWithBuilder(String filename) throws IOException {
  logger.info("TestBuilders on {}%n", filename);
  try (NetcdfFile org = NetcdfFile.open(filename)) {
    try (NetcdfFile withBuilder = NetcdfFiles.open(filename)) {
      Formatter f = new Formatter();
      CompareNetcdf2 compare = new CompareNetcdf2(f, false, false, true);
      if (!compare.compare(org, withBuilder, new DimensionsFilter())) {
        System.out.printf("Compare %s%n%s%n", filename, f);
        fail();
      }
    }
  }
}
 
Example 6
Source File: TestH4iospNewProblem.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void showNew(String filename) throws IOException {

    try (NetcdfFile withBuilder = NetcdfFiles.open(filename)) {
      // Variable v = withBuilder.findVariable("catchments_x");
      // Array data = v.read();
      System.out.printf("withBuilder = %s%n", withBuilder);
    }
  }
 
Example 7
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@Category(NeedsUcarNetwork.class)
public void testActiveScaleS3() throws IOException {
  // do not need credentials to run this one, just access to the internal ucar network.
  String host = "stratus.ucar.edu";
  String bucket = "rda-data";
  String key = "ds262.0/CERFACS/uo_Omon_NEMO3-2_FRCCORE2_f_r1i1p1_199801-200712.nc";
  String s3Uri = "cdms3://" + host + "/" + bucket + "?" + key;
  try (NetcdfFile ncfile = NetcdfFiles.open(s3Uri)) {
    Attribute conv = ncfile.findGlobalAttributeIgnoreCase("Conventions");
    assertThat(conv).isNotNull();
    assertThat(conv.getStringValue()).ignoringCase().isEqualTo("CF-1.4");
  }
}
 
Example 8
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@Category(NeedsUcarNetwork.class)
public void testActiveScaleWithCredsS3() throws IOException {
  String host = "stratus.ucar.edu";
  String bucket = "unidata-netcdf-zarr-testing";
  String key = "netcdf-java/test/GFS_Global_0p25deg_20200326_1200_apparent_temperature.nc4";
  String s3Uri = "cdms3://" + NCAR_PROFILE_NAME + "@" + host + "/" + bucket + "?" + key;
  try (NetcdfFile ncfile = NetcdfFiles.open(s3Uri)) {
    Attribute conv = ncfile.findGlobalAttributeIgnoreCase("Conventions");
    assertThat(conv).isNotNull();
    assertThat(conv.getStringValue()).ignoringCase().isEqualTo("CF-1.6");
  }
}
 
Example 9
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@Category(NeedsUcarNetwork.class)
public void ncarPartialReadFile() throws IOException, InvalidRangeException {
  try (NetcdfFile ncfile = NetcdfFiles.open(NCAR_G16_S3_URI)) {
    testPartialReadGoes16S3(ncfile);
  }
}
 
Example 10
Source File: TestGrib1Unpack.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEcmwfExtendedComplexData2() throws IOException {
  final String testfile = "../grib/src/test/data/complex_packing2.grib1";
  try (NetcdfFile nc = NetcdfFiles.open(testfile)) {

    Variable var = nc.findVariable("Snowfall_surface");
    Array data = var.read();

    float first = data.getFloat(0);

    Assert.assertEquals(.326607, first, 1e-6);
  }
}
 
Example 11
Source File: TestBufrRead.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void openNetcdf(String filename) throws IOException {
  System.out.printf("%n***openNetcdf bufr %s%n", filename);
  try (NetcdfFile ncfile = NetcdfFiles.open(filename)) {
    if (show)
      System.out.printf("%s%n", ncfile);
  }
}
 
Example 12
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void awsSharedCredsPrecedence() throws IOException {
  // Test that the region set in the custom shared credentials file takes precedence
  System.setProperty(AWS_REGION_PROP_NAME, Region.AP_SOUTH_1.id());
  System.setProperty(AWS_SHARED_CREDENTIALS_FILE_PROP, credentialsFileBadDefault);
  String cdmS3Uri = String.format("cdms3://%s@aws/noaa-goes16?", GOOD_PROFILE_NAME) + COMMON_G16_KEY;
  try (NetcdfFile ncfile = NetcdfFiles.open(cdmS3Uri)) {
    assertThat(ncfile).isNotNull();
  } finally {
    System.clearProperty(AWS_SHARED_CREDENTIALS_FILE_PROP);
    System.clearProperty(AWS_REGION_PROP_NAME);
  }
}
 
Example 13
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code5_assume_WGS84() throws IOException {
  String filename = dir + "Albers_viirs_s.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("AlbersEqualArea_Projection");
    Attribute axis = v.findAttribute("semi_major_axis");
    Assert.assertEquals(6378137., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 14
Source File: NetCDFDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void readDataInfo(String fileName) {
    this.setFileName(fileName);
    try {
        //ncfile = NetcdfFile.open(fileName);
        //ncfile = NetcdfDatasets.openDataset(fileName);
        ncfile = NetcdfFiles.open(fileName);
        readDataInfo(false);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: TestSubsettingUtils.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void subsetVariables(String filename, String varName, Section s)
    throws InvalidRangeException, IOException {
  System.out.println("testVariableSubset=" + filename + "," + varName);

  try (NetcdfFile ncfile = NetcdfFiles.open(filename)) {
    Variable v = ncfile.findVariable(varName);
    assert (null != v);
    subsetVariable(v, s, v.read());
  }
}
 
Example 16
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void awsPartialReadFileSimple() throws IOException, InvalidRangeException {
  System.setProperty(AWS_REGION_PROP_NAME, AWS_G16_REGION);
  try (NetcdfFile ncfile = NetcdfFiles.open(AWS_G16_S3_URI_SIMPLE)) {
    testPartialReadGoes16S3(ncfile);
  } finally {
    System.clearProperty(AWS_REGION_PROP_NAME);
  }
}
 
Example 17
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void osdcPartialReadFile() throws IOException, InvalidRangeException {
  try (NetcdfFile ncfile = NetcdfFiles.open(OSDC_G16_S3_URI)) {
    testPartialReadGoes16S3(ncfile);
  }
}
 
Example 18
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void gcsPartialReadFile() throws IOException, InvalidRangeException {
  try (NetcdfFile ncfile = NetcdfFiles.open(GCS_G16_S3_URI)) {
    testPartialReadGoes16S3(ncfile);
  }
}
 
Example 19
Source File: TestDir.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public int doAct(String filename) throws IOException {
  try (NetcdfFile ncfile = NetcdfFiles.open(filename)) {
    return readAllData(ncfile);
  }
}
 
Example 20
Source File: TestCoverageFileWritingCompare.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void writeTestFile() throws IOException, InvalidRangeException {
  // skip test requiring netcdf4 if not present.
  NetcdfFileWriter.Version version = NetcdfFormatWriter.convertToNetcdfFileWriterVersion(format);
  if (version.useJniIosp() && !Nc4Iosp.isClibraryPresent()) {
    return;
  }
  System.out.printf("Test Dataset %s type %s%n", endpoint, type);
  File tempFile = tempFolder.newFile();
  File tempFile2 = tempFolder.newFile();

  try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) {
    Assert.assertNotNull(endpoint, cc);
    CoverageCollection gcs = cc.findCoverageDataset(type);

    for (String covName : covList) {
      Assert.assertNotNull(covName, gcs.findCoverage(covName));
    }

    // write the file using CFGridCoverageWriter2
    System.out.printf(" CFGridCoverageWriter2 write to %s%n", tempFile2.getAbsolutePath());

    try (NetcdfFileWriter writer = NetcdfFileWriter
        .createNew(NetcdfFormatWriter.convertToNetcdfFileWriterVersion(format), tempFile2.getPath(), null)) {
      Optional<Long> estimatedSizeo = CFGridCoverageWriter2.write(gcs, covList, params, false, writer);
      if (!estimatedSizeo.isPresent()) {
        throw new InvalidRangeException("Request contains no data: " + estimatedSizeo.getErrorMessage());
      }
    }

    // write the file using CFGridCoverageWriter
    System.out.printf(" CFGridCoverageWriter write to %s%n", tempFile.getAbsolutePath());
    NetcdfFormatWriter.Builder writerb =
        NetcdfFormatWriter.builder().setNewFile(true).setFormat(format).setLocation(tempFile.getPath());
    NetcdfFormatWriter.Result result = CFGridCoverageWriter.write(gcs, covList, params, false, writerb, 0);
    if (!result.wasWritten()) {
      throw new InvalidRangeException("Error writing: " + result.getErrorMessage());
    }
  }

  // Compare that the files are identical
  try (NetcdfFile org = NetcdfFile.open(tempFile2.getPath())) {
    try (NetcdfFile copy = NetcdfFiles.open(tempFile.getPath())) {
      Formatter f = new Formatter();
      CompareNetcdf2 compare = new CompareNetcdf2(f, false, false, true);
      boolean ok = compare.compare(org, copy, new FileWritingObjFilter());
      System.out.printf("%s %s%n", ok ? "OK" : "NOT OK", f);
      assertThat(ok).isTrue();
    }
  }
}