Refine search
NetcdfFile ncFile = null; try { ncFile = NetcdfFile.open(tis.getFile().getAbsolutePath()); metadata.set("File-Type-Description", ncFile.getFileTypeDescription()); for (Attribute attr : ncFile.getGlobalAttributes()) { Property property = resolveMetadataKey(attr.getFullName()); if (attr.getDataType().isString()) { metadata.add(property, attr.getStringValue()); } else if (attr.getDataType().isNumeric()) { int value = attr.getNumericValue().intValue(); xhtml.newline(); for (Dimension dim : ncFile.getDimensions()) { xhtml.element("li", dim.getFullName() + " = " + dim.getLength()); for (Variable var : ncFile.getVariables()) { xhtml.startElement("li"); xhtml.characters(var.getDataType() + " " + var.getNameAndDimensions()); xhtml.newline(); List<Attribute> attributes = var.getAttributes(); if (!attributes.isEmpty()) { xhtml.startElement("ul");
private static Variable getAuxiliaryCoordinate( NetcdfDataset dataset, Group group, Variable var, String dimName) { Variable coordinateVariable = null; Attribute attribute = var.findAttribute(NetCDFUtilities.COORDINATES); if (attribute != null) { String coordinates = attribute.getStringValue(); String[] coords = coordinates.split(" "); for (String coord : coords) { Variable coordVar = dataset.findVariable(group, coord); List<Dimension> varDimensions = coordVar.getDimensions(); if (varDimensions != null && varDimensions.size() == 1 && varDimensions.get(0).getFullName().equalsIgnoreCase(dimName)) { coordinateVariable = coordVar; break; } } } return coordinateVariable; }
return false; } else if (checkType == CheckType.NOSCALARS) { List<Dimension> dimensions = var.getDimensions(); if (dimensions.size() < 2) { return false; DataType dataType = var.getDataType(); if (dataType == DataType.CHAR) { return false; return isVariableAccepted(var.getFullName(), CheckType.NONE); } else if (checkType == CheckType.ONLYGEOGRIDS) { List<Dimension> dimensions = var.getDimensions(); if (dimensions.size() < 2) { return false; String dimName = dimension.getFullName(); Group group = dimension.getGroup(); DataType dataType = var.getDataType(); if (dataType == DataType.CHAR) { return false; return isVariableAccepted(var.getFullName(), CheckType.NONE); } else { return isVariableAccepted(var.getFullName(), checkType);
/** * Get Z Dimension Lenght for standard CF variables * * @param var * @return */ public static int getZDimensionLength(Variable var) { final int rank = var.getRank(); if (rank > 2) { return var.getDimension(rank - Z_DIMENSION).getLength(); } // TODO: Should I avoid use this method in case of 2D Variables? return 0; }
private String getCoordinatesForVariable(String shortName) { Variable var = dataset.findVariable(null, shortName); if (var != null) { // Getting the coordinates attribute Attribute attribute = var.findAttribute(NetCDFUtilities.COORDINATES); if (attribute != null) { return attribute.getStringValue(); } else { return var.getDimensionsString(); } } return null; }
private void testCoordVar(NetcdfFile ncfile, int n) { Variable lat = ncfile.findVariable("y"); assert null != lat; assert lat.getShortName().equals("y"); assert lat.getRank() == 1; assert lat.getSize() == n; assert lat.getShape()[0] == n; assert lat.getDataType() == DataType.DOUBLE || lat.getDataType() == DataType.FLOAT; assert !lat.isUnlimited(); assert lat.getDimension(0).equals(ncfile.findDimension("y")); Attribute att = lat.findAttribute("units"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("km"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; try { Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == n; assert data.getShape()[0] == n; } catch (IOException io) {} }
public void testGrads2() throws IOException, InvalidRangeException { String fileIn = TestDir.cdmUnitTestDir + "formats/grads/pdef.ctl"; try (ucar.nc2.NetcdfFile ncf = ucar.nc2.NetcdfFile.open(fileIn)) { logger.debug("open {}", ncf.getLocation()); ucar.nc2.Variable v = ncf.findVariable("pdef"); assert v != null; assert v.getDataType() == DataType.FLOAT; Attribute att = v.findAttribute(CDM.MISSING_VALUE); assert att != null; assert att.getDataType() == DataType.FLOAT; Assert2.assertNearlyEquals(att.getNumericValue().floatValue(), -9999.0f); Array data = v.read(); assert Arrays.equals(data.getShape(), new int[]{1, 1, 180, 360}); } }
public void testCoordVar(NetcdfFile ncfile) throws IOException { Variable lat = ncfile.findVariable("latitude"); assert lat.getDataType() == DataType.FLOAT; assert lat.getDimension(0).equals(ncfile.findDimension("latitude")); Attribute att = lat.findAttribute("units"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("degree_N"); Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == 630; assert data.getShape()[0] == 630; assert data.getElementType() == float.class; IndexIterator dataI = data.getIndexIterator(); Assert2.assertNearlyEquals(dataI.getFloatNext(), 43.0f); Assert2.assertNearlyEquals(dataI.getFloatNext(), 43.01045f); Assert2.assertNearlyEquals(dataI.getFloatNext(), 43.020893f); }
NetcdfFile ncFile = NetcdfDataset.openFile(gribFile.getAbsolutePath(), null); for (Attribute attr : ncFile.getGlobalAttributes()) { Property property = resolveMetadataKey(attr.getFullName()); if (attr.getDataType().isString()) { metadata.add(property, attr.getStringValue()); } else if (attr.getDataType().isNumeric()) { int value = attr.getNumericValue().intValue(); xhtml.newline(); for (Dimension dim : ncFile.getDimensions()){ xhtml.element("li", dim.getFullName() + "=" + String.valueOf(dim.getLength()) + ";"); xhtml.newline(); xhtml.newline(); for (Variable var : ncFile.getVariables()){ xhtml.element("p", String.valueOf(var.getDataType()) + var.getNameAndDimensions() + ";"); for(Attribute element : var.getAttributes()){ xhtml.element("li", " :" + element + ";"); xhtml.newline();
public float getRadialDistance(int gate) throws IOException { Variable sp = ds.findVariable("gate"); Array spData = sp.read(); sp.setCachedData(spData, false); Index index = spData.getIndex(); return spData.getFloat(index.set(gate)); }
private boolean makeCoordinateAxis( Variable ncvar, Dimension dim) { if (ncvar.getRank() != 1) return false; Dimension vdim = ncvar.getDimension(0); if (!vdim.equals(dim)) return false; if (!dim.getShortName().equals(ncvar.getShortName())) { ncvar.addAttribute( new Attribute(_Coordinate.AliasForDimension, dim.getShortName())); } /* if (dim.getCoordinateVariables().size() == 1) { dim.addCoordinateVariable( ncvar); ncvar.setIsCoordinateAxis( true); } */ return true; }
public void testAggCoordVar(NetcdfFile ncfile, int n) throws IOException { Variable time = ncfile.findVariable("time"); assert null != time; assert time.getShortName().equals("time"); assert time.getRank() == 1; assert time.getSize() == n : time.getSize() +" != " + n; assert time.getShape()[0] == n; assert time.getDimension(0) == ncfile.findDimension("time"); Array data = time.read(); assert data.getRank() == 1; assert data.getSize() == n; assert data.getShape()[0] == n; }
static public void testVarMatchesData( Variable v, boolean showStatus) throws IOException { Array data = v.read(); assert data.getSize() == v.getSize(); assert data.getElementType() == v.getDataType().getPrimitiveClassType(); assert data.getRank() == v.getRank(); int[] dataShape = data.getShape(); int[] varShape = v.getShape(); for (int i=0; i<data.getRank(); i++) assert dataShape[i] == varShape[i]; if (showStatus) logger.debug( "**** testReadData done on {}", v.getFullName()); } }
@Test public void testGini() throws IOException, InvalidRangeException { String fileIn = TestDir.cdmUnitTestDir + "formats/gini/n0r_20041013_1852-compress"; try (ucar.nc2.NetcdfFile ncf = ucar.nc2.NetcdfFile.open(fileIn)) { logger.debug("open {}", ncf.getLocation()); ucar.nc2.Variable v = ncf.findVariable("Reflectivity"); assert v != null; assert v.getDataType() == DataType.FLOAT; Array data = v.read(); assert Arrays.equals(data.getShape(), new int[]{1, 3000, 4736}); } }
@Test public void code5_assume_WGS84() throws IOException { String filename = dir + "Albers_viirs_s.grb2"; try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Variable v = ncfile.findVariable("AlbersEqualArea_Projection"); Attribute axis = v.findAttribute("semi_major_axis"); Assert.assertEquals(6378137., axis.getNumericValue().doubleValue(), 0.1); } }
private void setMeanElevation() { if(isVolume) { try{ Variable sp = ds.findVariable("sweep"); Array spData = sp.read(); float [] spArray = (float [])spData.get1DJavaArray(float.class); meanElevation = spArray[sweepno]; } catch (IOException e) { e.printStackTrace(); meanElevation = 0.0; } } else { Attribute data = ds.findGlobalAttribute("Elevation"); meanElevation = data.getNumericValue().doubleValue(); } }
private void addLongName(NetcdfDataset ds, String varName, String longName) { Variable v = ds.findVariable(varName); if (v != null) v.addAttribute(new Attribute(CDM.LONG_NAME, longName)); }
public Variable findVariableByAttribute(Group g, String attName, String attValue) { if (g == null) g = getRootGroup(); for (Variable v : variables) { for (Attribute att : v.getAttributes()) if (attName.equals(att.getShortName()) && attValue.equals(att.getStringValue())) return v; } for (Group nested : g.getGroups()) { Variable v = findVariableByAttribute(nested, attName, attValue); if (v != null) return v; } return null; }
private ucar.ma2.Array readNestedData(ucar.nc2.Variable v2, Section section) throws java.io.IOException, ucar.ma2.InvalidRangeException { N3header.Vinfo vinfo = (N3header.Vinfo) v2.getSPobject(); DataType dataType = v2.getDataType(); // construct the full shape for use by RegularIndexer int[] fullShape = new int[v2.getRank() + 1]; fullShape[0] = header.numrecs; // the first dimension System.arraycopy(v2.getShape(), 0, fullShape, 1, v2.getRank()); // the remaining dimensions Layout layout = new LayoutRegularSegmented(vinfo.begin, v2.getElementSize(), header.recsize, fullShape, section); Object dataObject = readData(layout, dataType); return Array.factory(dataType, section.getShape(), dataObject); }
private void makeEnsembleCoordinate(NetcdfFile ncfile, Group g, CoordinateEns ec) { int n = ec.getSize(); String ecName = ec.getName().toLowerCase(); ncfile.addDimension(g, new Dimension(ecName, n)); Variable v = new Variable(ncfile, g, null, ecName, DataType.INT, ecName); ncfile.addVariable(g, v); v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Ensemble.toString())); int[] data = new int[n]; int count = 0; for (EnsCoord.Coord ecc : ec.getEnsSorted()) data[count++] = ecc.getEnsMember(); v.setCachedData(Array.factory(DataType.INT, new int[]{n}, data)); }