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(); metadata.add(property, String.valueOf(value)); 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"); for (Attribute element : attributes) { xhtml.element("li", element.toString());
protected void unravelStringMet(NetcdfFile ncFile, Group group, Metadata met) { if (group == null) { group = ncFile.getRootGroup(); } // get file type met.set("File-Type-Description", ncFile.getFileTypeDescription()); // unravel its string attrs for (Attribute attribute : group.getAttributes()) { if (attribute.isString()) { met.add(attribute.getFullName(), attribute.getStringValue()); } else { // try and cast its value to a string met.add(attribute.getFullName(), String.valueOf(attribute .getNumericValue())); } } for (Group g : group.getGroups()) { unravelStringMet(ncFile, g, met); } }
Attribute attribute = var.findAttribute(netCDFattributeName); if (attribute != null) { final int numValues = attribute.getLength(); if (numValues > 1) { ? 0 : 1; Number number = (Number) attribute.getValue(index); value = number.doubleValue(); } else { value = attribute.getNumericValue().doubleValue(); Attribute attribute = var.findAttribute(netCDFattributeName); if (attribute != null) { value = attribute.getNumericValue().doubleValue(); if (netCDFattributeName.contains("meridian") || netCDFattributeName.contains("longitude")) {
/** * Utility method for getting NoData from an input {@link Variable} * * @param var Variable instance * @return a Number representing NoData */ public static Number getNodata(Variable var) { if (var != null) { // Getting all the Variable attributes List<Attribute> attributes = var.getAttributes(); String fullName; // Searching for FILL_VALUE or MISSING_VALUE attributes for (Attribute attribute : attributes) { fullName = attribute.getFullName(); if (fullName.equalsIgnoreCase(FILL_VALUE) || fullName.equalsIgnoreCase(MISSING_VALUE)) { return attribute.getNumericValue(); } } } return null; }
double max = Double.NaN; DataType dataType = null; Attribute rangeAttribute = var.findAttribute(ACTUAL_RANGE); if (rangeAttribute == null) { rangeAttribute = var.findAttribute(VALID_RANGE); dataType = rangeAttribute.getDataType(); min = rangeAttribute.getNumericValue(0).doubleValue(); max = rangeAttribute.getNumericValue(1).doubleValue(); } else { Attribute minAttribute = var.findAttribute(VALID_MIN); Attribute maxAttribute = var.findAttribute(VALID_MAX); if (minAttribute != null && maxAttribute != null) { dataType = minAttribute.getDataType(); min = minAttribute.getNumericValue().doubleValue(); max = maxAttribute.getNumericValue().doubleValue();
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(); metadata.add(property, String.valueOf(value)); 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 double readAttributeDouble(Variable v, String attName, double defValue) { Attribute att; if (v == null) att = rootGroup.findAttributeIgnoreCase(attName); else att = v.findAttributeIgnoreCase(attName); if (att == null) return defValue; if (att.isString()) return Double.parseDouble(att.getStringValue()); else return att.getNumericValue().doubleValue(); }
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; }
/** * Write a global attribute to the file. * * @param att take attribute name, value, from here */ public void writeGlobalAttribute(Attribute att) { String useName = N3iosp.makeValidNetcdfObjectName(att.getShortName()); if (att.isArray()) // why rewrite them ?? ncfile.addGlobalAttribute(useName, att.getValues()); else if (att.isString()) ncfile.addGlobalAttribute(useName, att.getStringValue()); else ncfile.addGlobalAttribute(useName, att.getNumericValue()); }
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; }
public void testMetadata() { logger.debug("TestNested = \n{}", ncfile); Attribute att = ncfile.findGlobalAttribute("title"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("Example Data"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; Dimension latDim = ncfile.findDimension("lat"); assert null != latDim; assert latDim.getShortName().equals("lat"); assert latDim.getLength() == 3; assert !latDim.isUnlimited(); Dimension timeDim = ncfile.findDimension("time"); assert null != timeDim; assert timeDim.getShortName().equals("time"); assert timeDim.getLength() == 2; assert timeDim.isUnlimited(); }
@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 convertAttribute(Variable ctv, Attribute att, double scalef) { if (att == null) return; double val = scalef * att.getNumericValue().doubleValue(); ctv.addAttribute(new Attribute(att.getShortName(), val)); }
public void testReplaceAtt() { // System.out.println("\nncfile = "+ncfile); Attribute att = ncfile.findGlobalAttribute("title"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("replaced"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; }
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 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)); }
static private double getAttAsDouble( NetcdfDataset ds, String attname) { Attribute att = ds.findGlobalAttributeIgnoreCase(attname); if (null == att) throw new IllegalArgumentException("Must have a "+attname+" global attribute"); if (att.getDataType() == DataType.STRING) { return Double.parseDouble( att.getStringValue()); } else { return att.getNumericValue().doubleValue(); } }