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

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#openFile() . 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: NCdumpW.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Main program.
 * <p>
 * <strong>ucar.nc2.NCdumpW filename [-cdl | -ncml] [-c | -vall] [-v varName1;varName2;..] [-v varName(0:1,:,12)]
 * </strong>
 * <p>
 * where:
 * <ul>
 * <li>filename : path of any CDM readable file
 * <li>cdl or ncml: output format is CDL or NcML
 * <li>-vall : dump all variable data
 * <li>-c : dump coordinate variable data
 * <li>-v varName1;varName2; : dump specified variable(s)
 * <li>-v varName(0:1,:,12) : dump specified variable section
 * </ul>
 * Default is to dump the header info only.
 *
 * @param args arguments
 */
public static void main(String[] args) {
  if (args.length == 0) {
    System.out.println(usage);
    return;
  }

  // pull out the filename from the command
  String filename = args[0];
  try (Writer writer = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
      NetcdfFile nc = NetcdfDataset.openFile(filename, null)) {
    // the rest of the command
    StringBuilder command = new StringBuilder();
    for (int i = 1; i < args.length; i++) {
      command.append(args[i]);
      command.append(" ");
    }
    print(nc, command.toString(), writer, null);
  } catch (IOException ioe) {
    ioe.printStackTrace();
  }
}
 
Example 2
Source File: TimeCdmRemote.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static void testRead(String filename, Stat stat, boolean readData) throws IOException, InvalidRangeException {
  long bytes = 0;
  long start = System.nanoTime();
  if (show)
    System.out.printf("%n------Reading filename %s (%s)%n", filename, stat.getName());

  try (NetcdfFile ncfile = NetcdfDataset.openFile(filename, null)) {
    if (readData)
      bytes = readAllData(ncfile);
    long end = System.nanoTime();
    double took = ((double) (end - start)) / 1000 / 1000 / 1000; // secs
    double rate = 0;
    ncfile.close();
    if (stat != null && bytes > 0) {
      rate = bytes / took / 1000 / 1000;
      stat.sample(rate); // Mb/sec
    }
    if (show)
      System.out.printf(" bytes = %d took =%f secs rate=%f MB/sec%n", bytes, took, rate);
  }
}
 
Example 3
Source File: TimeFileWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void timeFileWriter(String location, String fileOut) throws IOException {
  long startTime = System.currentTimeMillis();

  try (NetcdfFile ds = NetcdfDataset.openFile(location, null)) {
    FileWriter2 writer = new FileWriter2(ds, fileOut, NetcdfFileWriter.Version.netcdf3, null);
    NetcdfFile ncfileOut = writer.write();
    ncfileOut.close();
  }

  // NetcdfFile ncfileOut = ucar.nc2.FileWriter.writeToFile(ds, fileOut, false, 0);
  long diff = System.currentTimeMillis() - startTime; // msecs

  File result = new File(fileOut);
  double size = .001 * result.length(); // km
  double rate = size / diff; // Mb/sec
  System.out.println("FileWriter.writeToFile " + location + "  took " + diff + " msecs rate= " + rate + " Mb/sec");
}
 
Example 4
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 5
Source File: NCdumpW.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * ncdump that parses a command string.
 *
 * @param command command string
 * @param out send output here
 * @param ct allow task to be cancelled; may be null.
 * @return true if successful
 * @throws IOException on write error
 */
public static boolean print(String command, Writer out, ucar.nc2.util.CancelTask ct) throws IOException {
  // pull out the filename from the command
  String filename;
  StringTokenizer stoke = new StringTokenizer(command);
  if (stoke.hasMoreTokens())
    filename = stoke.nextToken();
  else {
    out.write(usage);
    return false;
  }

  try (NetcdfFile nc = NetcdfDataset.openFile(filename, ct)) {
    // the rest of the command
    int pos = command.indexOf(filename);
    command = command.substring(pos + filename.length());
    return print(nc, command, out, ct);

  } catch (java.io.FileNotFoundException e) {
    out.write("file not found= ");
    out.write(filename);
    return false;

  } finally {
    out.close();
  }
}
 
Example 6
Source File: TimeFileWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void timeChannelWriter(String location, String fileOut) throws IOException, InvalidRangeException {
  long startTime = System.currentTimeMillis();

  NetcdfFile ncfile = NetcdfDataset.openFile(location, null);

  FileOutputStream fout = new FileOutputStream(fileOut);
  N3channelWriter.writeToChannel(ncfile, fout.getChannel());
  long diff = System.currentTimeMillis() - startTime; // msecs

  File result = new File(fileOut);
  double size = .001 * result.length(); // km
  double rate = size / diff; // Mb/sec
  System.out
      .println("N3channelWriter.writeToFile " + location + "  took " + diff + " msecs rate= " + rate + " Mb/sec");
}
 
Example 7
Source File: TestNexrad2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int doAct(String filename) throws IOException {
  if (deleteUncompress && filename.endsWith(".uncompress")) {
    File uf = new File(filename);
    if (!uf.delete())
      System.out.printf("Failed to delete %s%n", filename);
    return 0;
  }

  try (NetcdfFile ncfile = NetcdfDataset.openFile(filename, null)) {
    return testRead(ncfile);
  }
}
 
Example 8
Source File: TestNexrad2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCoordSys() throws IOException {
  // NetcdfDataset ncd = NetcdfDataset.openDataset(
  // "dods://localhost:8080/thredds/dodsC/testAll/Level2_KSOX_20051010_2322.ar2v", false, null);
  String filename = TestDir.cdmUnitTestDir + "formats/nexrad/level2/Level2_KYUX_20060527_2335.ar2v";
  try (NetcdfFile ncfile = NetcdfDataset.openFile(filename, null)) {
    testCoordSystem(ncfile);
  }
}
 
Example 9
Source File: TestNexrad2HiResolution.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int doAct(String filename) throws IOException {
  if (deleteUncompress && !filename.endsWith(".bz2")) {
    File uf = new File(filename);
    if (!uf.delete())
      System.out.printf("Failed to delete %s%n", filename);
    return 0;
  }

  try (NetcdfFile ncfile = NetcdfDataset.openFile(filename, null)) {
    testRead(ncfile);
    testCoordSystem(ncfile);
    return 1;
  }
}
 
Example 10
Source File: MemoryCounterAgentTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static void testN3() throws IOException {
  NetcdfFile ncfile = NetcdfDataset.openFile("C:/data/test2.nc", null);
  measureSize("beforeRead", ncfile, null, true);

  for (Variable v : ncfile.getVariables()) {
    v.read();
  }
  measureSize("afterRead", ncfile, null, true);

  ncfile.close();
}
 
Example 11
Source File: TestCdmRemoteCompareHeadersP.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static int compareDatasetsOld(String local, String remote, boolean readData) throws IOException {
  try (NetcdfFile ncfile = NetcdfDataset.openFile(local, null); NetcdfFile ncremote = new CdmRemote(remote)) {

    Formatter f = new Formatter();
    CompareNetcdf2 mind = new CompareNetcdf2(f, false, false, readData);
    boolean ok = mind.compare(ncfile, ncremote, new NcstreamObjFilter());
    if (!ok) {
      System.out.printf("--Compare %s to %s%n", local, remote);
      System.out.printf("  %s%n", f);
    }
    Assert.assertTrue(local + " != " + remote, ok);
  }
  return 1;
}
 
Example 12
Source File: NCdumpW.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * ncdump-like print of netcdf file.
 *
 * @param filename NetcdfFile to open
 * @param out print to this stream
 * @param showAll dump all variable data
 * @param showCoords only print header and coordinate variables
 * @param ncml print NcML representation (other arguments are ignored)
 * @param strict print strict CDL representation
 * @param varNames semicolon delimited list of variables whose data should be printed
 * @param ct allow task to be cancelled; may be null.
 * @return true if successful
 * @throws IOException on write error
 */
public static boolean print(String filename, Writer out, boolean showAll, boolean showCoords, boolean ncml,
    boolean strict, String varNames, ucar.nc2.util.CancelTask ct) throws IOException {
  try (NetcdfFile nc = NetcdfDataset.openFile(filename, ct)) {
    return print(nc, out, showAll, showCoords, ncml, strict, varNames, ct);
  } catch (java.io.FileNotFoundException e) {
    out.write("file not found= ");
    out.write(filename);
    out.flush();
    return false;
  }
}