static private double[] doubleArrayAttribute_( NetcdfFile ncFile, String name ) { double[] result = null; Attribute attribute = ncFile.findGlobalAttribute( name ); if ( attribute != null && attribute.isArray() && attribute.getLength() > 1 ) { final int count = attribute.getLength(); result = new double[ count ]; for ( int index = 0; index < count; ++index ) { result[ index ] = attribute.getNumericValue( index ).doubleValue(); } } return result; }
@Test public void testVlengthAttribute() throws IOException { try (NetcdfFile ncfile = TestH5.openH5("support/vlstra.h5")) { Attribute att = ncfile.findGlobalAttribute("test_scalar"); assert (null != att); assert (!att.isArray()); assert (att.isString()); assert (att.getStringValue().equals("This is the string for the attribute")); } }
public void testGlobalAtt() { Attribute att = ncfile.findGlobalAttribute("Conventions"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("Metapps"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; }
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; }
/** * 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()); }
/** * 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()); }
/** * 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()); }
@org.junit.Test public void testReadH5attributeString() throws IOException { try (NetcdfFile ncfile = TestH5.openH5("support/attstr.h5")) { Group g = ncfile.getRootGroup().findGroup("MyGroup"); assert null != g; Attribute att = g.findAttribute("data_contents"); assert (null != att); assert (!att.isArray()); assert (att.isString()); assert (att.getStringValue().equals("important_data")); } }
/** * Write a Variable attribute to the file. * * @param varName name of variable to attach attribute to * @param att take attribute name, value, from here */ public void writeAttribute(String varName, Attribute att) { String attName = N3iosp.makeValidNetcdfObjectName(att.getShortName()); varName = N3iosp.makeValidNetcdfObjectName(varName); if (att.isArray()) ncfile.addVariableAttribute(varName, attName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, attName, att.getStringValue()); else ncfile.addVariableAttribute(varName, attName, att.getNumericValue()); }
/** * Write a Variable attribute to the file. * * @param varName name of variable to attach attribute to * @param att take attribute name, value, from here */ public void writeAttribute(String varName, Attribute att) { String attName = N3iosp.makeValidNetcdfObjectName(att.getShortName()); varName = N3iosp.makeValidNetcdfObjectName(varName); if (att.isArray()) ncfile.addVariableAttribute(varName, attName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, attName, att.getStringValue()); else ncfile.addVariableAttribute(varName, attName, att.getNumericValue()); }
/** * Write a Variable attribute to the file. * * @param varName name of variable to attach attribute to * @param att take attribute name, value, from here */ public void writeAttribute(String varName, Attribute att) { String attName = N3iosp.makeValidNetcdfObjectName(att.getShortName()); varName = N3iosp.makeValidNetcdfObjectName(varName); if (att.isArray()) ncfile.addVariableAttribute(varName, attName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, attName, att.getStringValue()); else ncfile.addVariableAttribute(varName, attName, att.getNumericValue()); }
public void testVarAtt() { Variable v = ncfile.findVariable("rh"); assert null != v; Attribute att = v.findAttribute(CDM.LONG_NAME); assert null == att; att = v.findAttribute("units"); assert null == att; att = v.findAttribute("UNITS"); assert null != att; assert att.getStringValue().equals("percent"); att = v.findAttribute("longer_name"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("Abe said what?"); }
public void testStructure() { 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("Union cldc and lflx"); 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() == 21; assert !latDim.isUnlimited(); Dimension timeDim = ncfile.findDimension("time"); assert null != timeDim; assert timeDim.getShortName().equals("time"); assert timeDim.getLength() == 456; assert timeDim.isUnlimited(); }
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(); }
MetadataAttribute attributeToMetadata(Attribute attribute) { final int productDataType = getProductDataType(attribute.getDataType(), false, false); if (productDataType != -1) { ProductData productData; if (attribute.isString()) { productData = ProductData.createInstance(attribute.getStringValue()); } else if (attribute.isArray()) { productData = ProductData.createInstance(productDataType, attribute.getLength()); productData.setElems(attribute.getValues().getStorage()); } else { productData = ProductData.createInstance(productDataType, 1); productData.setElems(attribute.getValues().getStorage()); } return new MetadataAttribute(attribute.getShortName(), productData, true); } return null; }
public void readByte2Short() throws Exception { Variable t2 = null; assert(null != (t2 = ncfileRead.findVariable("t2"))); assert( t2.getDataType() == DataType.BYTE); Attribute att = t2.findAttribute(CDM.SCALE_FACTOR); assert( null != att); assert( !att.isArray()); assert( 1 == att.getLength()); assert( 2 == att.getNumericValue().doubleValue()); assert( DataType.SHORT == att.getDataType()); assert(null != (t2 = dsRead.findVariable("t2"))); assert t2 instanceof VariableEnhanced; VariableDS vs = (VariableDS) t2; assert( vs.getDataType() == DataType.SHORT) : vs.getDataType(); assert( vs.hasMissing()); Array A = vs.read(); assert( A.getElementType() == short.class) : A.getElementType(); Index ima = A.getIndex(); int[] shape = A.getShape(); int i,j; for (i=0; i<shape[0]; i++) { for (j=0; j<shape[1]; j++) { assert( A.getShort(ima.set(i,j)) == (2 * (i*10+j) + 77)); } } }
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) {} }
assert( !att.isArray()); assert( 1 == att.getLength()); assert( 2.0 == att.getNumericValue().doubleValue());
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); }
private void testCoordVar(NetcdfFile ncfile, int n) throws IOException { 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().isFloatingPoint(); 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; Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == n; assert data.getShape()[0] == n; assert data.getElementType() == double.class || data.getElementType() == float.class; int last = (int) data.getSize() - 1; Assert2.assertNearlyEquals(data.getDouble(0), -832.2073364257812); Assert2.assertNearlyEquals(data.getDouble(last), 4369.20068359375); }