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

The following examples show how to use ucar.nc2.NetcdfFile#getVariables() . 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: H5diag.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void showCompress(Formatter f) throws IOException {
  NetcdfFile ncfile = iosp.getNetcdfFile();

  Size totalSize = new Size(0, 0);
  for (Variable v : ncfile.getVariables()) {
    H5header.Vinfo vinfo = (H5header.Vinfo) v.getSPobject();
    showCompress(v, vinfo, totalSize, f);
  }
  f.format("%n");
  f.format(" total bytes   = %d%n", totalSize.nominal);
  f.format(" total storage = %d%n", totalSize.storage);
  f.format(" compression = %f%n", totalSize.getRatio());
  f.format(" nchunks     = %d%n", totalSize.count);

  File raf = new File(ncfile.getLocation());
  f.format(" file size    = %d%n", raf.length());
  float overhead = totalSize.storage == 0 ? 0 : ((float) raf.length() / totalSize.storage);
  f.format(" overhead     = %f%n", overhead);
}
 
Example 2
Source File: Hdf5DataTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setHdf5File(RandomAccessFile raf) throws IOException {
  closeOpenFiles();

  this.location = raf.getLocation();
  List<VarBean> beanList = new ArrayList<>();

  iosp = new H5iosp();
  NetcdfFile ncfile = new NetcdfFileSubclass(iosp, location);
  try {
    iosp.open(raf, ncfile, null);
  } catch (Throwable t) {
    StringWriter sw = new StringWriter(20000);
    t.printStackTrace(new PrintWriter(sw));
    infoTA.setText(sw.toString());
  }

  for (Variable v : ncfile.getVariables()) {
    beanList.add(new VarBean(v));
  }

  objectTable.setBeans(beanList);
}
 
Example 3
Source File: TestDir.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static int readAllData(NetcdfFile ncfile) throws IOException {
  logger.debug("------Reading ncfile {}", ncfile.getLocation());
  try {
    for (Variable v : ncfile.getVariables()) {
      if (v.getSize() > max_size) {
        Section s = makeSubset(v);
        logger.debug("  Try to read variable {} size={} section={}", v.getNameAndDimensions(), v.getSize(), s);
        v.read(s);
      } else {
        logger.debug("  Try to read variable {} size={}", v.getNameAndDimensions(), v.getSize());
        v.read();
      }
    }

    return 1;
  } catch (InvalidRangeException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: DataImportExample.java    From tablestore-examples with Apache License 2.0 6 votes vote down vote up
/**
 * read data from netcdf file and write data to table store.
 * @param meta
 * @param ncFileName
 * @throws Exception
 */
public void importFromNcFile(GridDataSetMeta meta, String ncFileName) throws Exception {
    GridDataWriter writer = tableStoreGrid.getDataWriter(meta);
    NetcdfFile ncFile = NetcdfFile.open(ncFileName);
    List<Variable> variables = ncFile.getVariables();
    for (Variable variable : variables) {
        if (meta.getVariables().contains(variable.getShortName())) {
            for (int t = 0; t < meta.gettSize(); t++) {
                for (int z = 0; z < meta.getzSize(); z++) {
                    Array array = variable.read(new int[]{t, z, 0, 0}, new int[]{1, 1, meta.getxSize(), meta.getySize()});
                    Grid2D grid2D = new Grid2D(array.getDataAsByteBuffer(), variable.getDataType(),
                            new int[] {0, 0}, new int[] {meta.getxSize(), meta.getySize()});
                    writer.writeGrid2D(variable.getShortName(), t, z, grid2D);
                }
            }
        }
    }
}
 
Example 5
Source File: TestDir.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static public int readAllData(NetcdfFile ncfile) throws IOException {
  logger.debug("------Reading ncfile {}", ncfile.getLocation());
  try {
    for (Variable v : ncfile.getVariables()) {
      if (v.getSize() > max_size) {
        Section s = makeSubset(v);
        logger.debug("  Try to read variable {} size={} section={}", v.getNameAndDimensions(), v.getSize(), s);
        v.read(s);
      } else {
        logger.debug("  Try to read variable {} size={}", v.getNameAndDimensions(), v.getSize());
        v.read();
      }
    }

    return 1;
  } catch (InvalidRangeException e) {
    throw new RuntimeException(e);
  }
}
 
Example 6
Source File: DatasetWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<VariableBean> makeVariableBeans(NetcdfFile ds) {
  List<VariableBean> vlist = new ArrayList<>();
  for (Variable v : ds.getVariables()) {
    vlist.add(new VariableBean(v));
  }
  return vlist;
}
 
Example 7
Source File: DatasetViewer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<VariableBean> getVariableBeans(NetcdfFile ds) {
  List<VariableBean> vlist = new ArrayList<>();
  for (Variable v : ds.getVariables()) {
    vlist.add(new VariableBean(v));
  }
  return vlist;
}
 
Example 8
Source File: TestHttpOpen.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private long readAllData(NetcdfFile ncfile) throws IOException {
  System.out.println("------Open " + ncfile.getLocation());

  long total = 0;
  for (Variable v : ncfile.getVariables()) {
    long nbytes = v.getSize() * v.getElementSize();
    System.out.println("  Try to read variable " + v.getFullName() + " " + nbytes);
    v.read();
    total += v.getSize() * v.getElementSize();
  }
  ncfile.close();
  return total;
}
 
Example 9
Source File: LCMSNetCDFParser.java    From act with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Print top-level details about a NetCDF file.  Used for debugging.
 * @param netcdfFile The NetCDF file whose details to print.
 */
public static void printNetcdfFileDetails(NetcdfFile netcdfFile) {
  System.out.format("Details: %s\n", netcdfFile.getDetailInfo());
  System.out.format("File type description: %s\n", netcdfFile.getFileTypeDescription());
  System.out.format("Title: %s\n", netcdfFile.getTitle());
  System.out.println("Variables:");
  for (Variable v : netcdfFile.getVariables()) {
    System.out.format("  %s\n", v.getNameAndDimensions());
  }
  System.out.println("Global attributes:");
  for (Attribute a : netcdfFile.getGlobalAttributes()) {
    System.out.format("  %s: %s (%s)\n", a.getFullName(), a.getStringValue(), a.getDataType().toString());
  }
}
 
Example 10
Source File: DAPDownloaderTest.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFilterVariables() throws Exception {
    final URL resource = getClass().getResource("test.nc");
    final NetcdfFile netcdfFile = NetcdfFileOpener.open(resource.toString());
    final List<Variable> variables = netcdfFile.getVariables();
    final List<String> variableNames = new ArrayList<String>();
    for (Variable variable : variables) {
        variableNames.add(variable.getFullName());
    }
    String constraintExpression = null;
    List<String> filteredVariables = DAPDownloader.filterVariables(variableNames, constraintExpression);
    assertEquals(2, filteredVariables.size());
    assertEquals("sst", filteredVariables.get(0));
    assertEquals("wind", filteredVariables.get(1));

    constraintExpression = "sst[0:1:10][0:1:10]";
    filteredVariables = DAPDownloader.filterVariables(variableNames, constraintExpression);
    assertEquals(1, filteredVariables.size());
    assertEquals("sst", filteredVariables.get(0));

    constraintExpression = "bogusVariable[0:1:10][0:1:10]";
    filteredVariables = DAPDownloader.filterVariables(variableNames, constraintExpression);
    assertEquals(2, filteredVariables.size());
    assertEquals("sst", filteredVariables.get(0));
    assertEquals("wind", filteredVariables.get(1));

    constraintExpression = "sst[0:1:10][0:1:10],wind[0:1:10][0:1:10]";
    filteredVariables = DAPDownloader.filterVariables(variableNames, constraintExpression);
    assertEquals(2, filteredVariables.size());
    assertEquals("sst", filteredVariables.get(0));
    assertEquals("wind", filteredVariables.get(1));

    constraintExpression = "sst[0:1:10][0:1:10],wind[0:1:10][0:1:10],sst";
    filteredVariables = DAPDownloader.filterVariables(variableNames, constraintExpression);
    assertEquals(2, filteredVariables.size());
    assertEquals("sst", filteredVariables.get(0));
    assertEquals("wind", filteredVariables.get(1));
}
 
Example 11
Source File: DatasetViewer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private List<String> getVariableNames(NetcdfFile nc) {
  List<String> result = new ArrayList<>();
  for (Variable v : nc.getVariables())
    result.add(v.getFullName());
  return result;
}
 
Example 12
Source File: NetcdfDataObject.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Read data from netcdf file.
 *
 * @throws IOException
 */
//TODO remove, always read data lazily. AK
private void readNetcdfFile() throws IOException  {
	this.exchangeItems.clear();
	NetcdfFile netcdfFile = this.netcdfFileWriter.getNetcdfFile();

	Results.putMessage(this.getClass().getSimpleName() + ": reading data from file " + this.file.getAbsolutePath());

	//in most netcdfFiles the time and spatial coordinate variables are shared between multiple data variables.
	//Therefore cache timeInfo objects so that time coordinate variables
	//are never read more than once.
	Map<Variable, IArrayTimeInfo> timeInfoCache = new HashMap<Variable, IArrayTimeInfo>();
	for (Variable variable : netcdfFile.getVariables()) {
		//do not create exchangeItems for coordinate variables.
		//the coordinate values will be stored in the metadata objects contained in the exchangeItems.
		if (variable.isCoordinateVariable()) {
			continue;
		}

		if (!variable.getDataType().isNumeric()) {
			continue;
		}

		//create exchangeItem.
		IExchangeItem exchangeItem;
		//Note: never use standard name or long name as exchangeItemId, since these are not always unique within a netcdf file.
		String exchangeItemId = variable.getShortName();
		if (variable.getDimensions().isEmpty()) {//if scalar.
			exchangeItem = new DoubleExchangeItem(exchangeItemId, Role.InOut, variable.readScalarDouble());

		} else {//if array.
			ArrayExchangeItem arrayBasedExchangeItem = new ArrayExchangeItem(exchangeItemId, IPrevExchangeItem.Role.InOut);
			arrayBasedExchangeItem.setQuantityInfo(new QuantityInfo(exchangeItemId, variable.getUnitsString()));
			IArrayTimeInfo newTimeInfo = NetcdfUtils.createTimeInfo(variable, netcdfFile, timeInfoCache);
			//skip variables that do not depend on time.
			if (newTimeInfo == null && !allowTimeIndependentItems) continue;

			arrayBasedExchangeItem.setTimeInfo(newTimeInfo);
			//TODO cache variables with spatial coordinates. AK
			arrayBasedExchangeItem.setGeometryInfo(NetcdfUtils.createGeometryInfo(variable, netcdfFile));
			IArray array = (IArray) NetcdfUtils.readData(variable);
			arrayBasedExchangeItem.setArray(array);
			exchangeItem = arrayBasedExchangeItem;
		}

		this.exchangeItems.add(exchangeItem);
	}
}