public Attribute(String name, Number val, boolean isUnsigned) { super(name); if (name == null) throw new IllegalArgumentException("Trying to set name to null on "+this); int[] shape = new int[1]; shape[0] = 1; DataType dt = DataType.getType(val.getClass()); Array vala = Array.factory(dt.getPrimitiveClassType(), shape); Index ima = vala.getIndex(); vala.setDouble(ima.set0(0), val.doubleValue()); setValues(vala); this.isUnsigned = isUnsigned; if (isUnsigned) vala.setUnsigned(true); setImmutable(true); }
/** * Transcode a NetCDF Number into a proper Number instance. * * @param type the {@link DataType} to transcode. * @return the proper number instance */ public static Number transcodeNumber(final DataType type, Number value) { if (DataType.DOUBLE.equals(type)) { return Double.valueOf(value.doubleValue()); } else if (DataType.FLOAT.equals(type)) { return Float.valueOf(value.floatValue()); } else if (DataType.LONG.equals(type)) { return Long.valueOf(value.longValue()); } else if (DataType.INT.equals(type)) { return Integer.valueOf(value.intValue()); } else if (DataType.SHORT.equals(type)) { return Short.valueOf(value.shortValue()); } else if (DataType.BYTE.equals(type)) { return Byte.valueOf(value.byteValue()); } throw new IllegalArgumentException( "Unsupported type or value: type = " + type.toString() + " value = " + value); }
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();
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");
public static Element writeEnumTypedef(EnumTypedef etd, Namespace ns) { Element typeElem = new Element("enumTypedef", ns); typeElem.setAttribute("name", etd.getShortName()); typeElem.setAttribute("type", etd.getBaseType().toString()); Map<Integer, String> map = etd.getMap(); for (Map.Entry<Integer, String> entry : map.entrySet()) { typeElem.addContent(new Element("enum", ns) .setAttribute("key", Integer.toString(entry.getKey())) .addContent(entry.getValue())); } return typeElem; }
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); }
private void makeVariableNoCoords(NetcdfFile ncfile, int datatype, String shortName, String longName, Variable from) { Variable v = new Variable(ncfile, null, null, shortName); v.setDataType(DataType.BYTE); v.setDimensions( from.getDimensions()); ncfile.addVariable(null, v); v.addAttribute( new Attribute(CDM.UNITS, Cinrad2Record.getDatatypeUnits(datatype))); v.addAttribute( new Attribute(CDM.LONG_NAME, longName)); byte[] b = new byte[2]; b[0] = Cinrad2Record.MISSING_DATA; b[1] = Cinrad2Record.BELOW_THRESHOLD; Array missingArray = Array.factory(DataType.BYTE.getPrimitiveClassType(), new int[] {2}, b); v.addAttribute( new Attribute(CDM.MISSING_VALUE, missingArray)); v.addAttribute( new Attribute("signal_below_threshold", new Byte( Cinrad2Record.BELOW_THRESHOLD))); v.addAttribute( new Attribute(CDM.SCALE_FACTOR, new Float( Cinrad2Record.getDatatypeScaleFactor(datatype)))); v.addAttribute( new Attribute(CDM.ADD_OFFSET, new Float( Cinrad2Record.getDatatypeAddOffset(datatype)))); v.addAttribute( new Attribute(CDM.UNSIGNED, "true")); Attribute fromAtt = from.findAttribute(_Coordinate.Axes); v.addAttribute( new Attribute(_Coordinate.Axes, fromAtt)); Vgroup vgFrom = (Vgroup) from.getSPobject(); Vgroup vg = new Vgroup(datatype, vgFrom.map); v.setSPobject( vg); }
private Element writeGrid(GridDatatype grid) { Element varElem = new Element("grid"); varElem.setAttribute("name", grid.getFullName()); String desc = grid.getDescription() != null ? grid.getDescription() : "No description"; varElem.setAttribute("desc", desc); StringBuilder buff = new StringBuilder(); List dims = grid.getDimensions(); for (int i = 0; i < dims.size(); i++) { Dimension dim = (Dimension) dims.get(i); if (i > 0) buff.append(" "); if (dim.isShared()) buff.append(dim.getShortName()); else buff.append(dim.getLength()); } if (buff.length() > 0) varElem.setAttribute("shape", buff.toString()); DataType dt = grid.getDataType(); if (dt != null) varElem.setAttribute("type", dt.toString()); //GridCoordSystem cs = grid.getCoordinateSystem(); //varElem.setAttribute("coordSys", cs.getName()); // attributes for (ucar.nc2.Attribute att : grid.getAttributes()) { varElem.addContent(ucar.nc2.ncml.NcMLWriter.writeAttribute(att, "attribute", null)); } return varElem; }
public void testCoordVar(NetcdfFile ncfile, String name, int n, DataType type) throws IOException { Variable lat = ncfile.findVariable(name); assert null != lat; assert lat.getShortName().equals(name); assert lat.getRank() == 1; assert lat.getSize() == n; assert lat.getShape()[0] == n; assert lat.getDataType() == type; assert !lat.isUnlimited(); assert lat.getDimension(0).equals(ncfile.findDimension(name)); Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == n; assert data.getShape()[0] == n; assert data.getElementType() == type.getPrimitiveClassType(); int count = 0; IndexIterator dataI = data.getIndexIterator(); while (dataI.hasNext()) Assert2.assertNearlyEquals(dataI.getDoubleNext(), (double) count++); }
private Vinfo writeVar(DataOutputStream stream, Variable var, int offset) throws IOException { int hsize = 0; hsize += writeString(stream, N3iosp.makeValidNetcdfObjectName( var.getShortName())); // dimensions int vsize = var.getDataType().getSize(); List<Dimension> dims = var.getDimensions(); if (null != stream) stream.writeInt(dims.size()); hsize += 4; for (Dimension dim : dims) { int dimIndex = findDimensionIndex(dim); if (null != stream) stream.writeInt(dimIndex); hsize += 4; if (!dim.isUnlimited()) vsize *= dim.getLength(); } int pad = (usePadding) ? N3header.padding(vsize) : 0; vsize += pad; // variable attributes hsize += writeAtts(stream, var.getAttributes()); // data type, variable size, beginning file position int type = N3header.getType(var.getDataType()); if (null != stream) { stream.writeInt(type); stream.writeInt(vsize); stream.writeInt(offset); } hsize += 12; //if (debug) out.println(" name= "+name+" type="+type+" vsize="+vsize+" begin= "+begin+" isRecord="+isRecord+"\n"); return new Vinfo(var, hsize, vsize, offset, pad, var.isUnlimited()); }
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()); } }
NetcdfDataset ncd = NetcdfDataset.openDataset(filename)) { for (Variable v : ncfile.getVariables()) { if (!v.getDataType().isNumeric()) continue; System.out.printf("testImplicitFillValue for %s type=%s%n", v.getShortName(), v.getDataType()); VariableDS ve = (VariableDS) ncd.findVariable(v.getFullName()); Array data = v.read(); Array dataE = ve.read(); double valde = ((Number) vale).doubleValue(); if (ve.isFillValue(vald)) { if (v.getDataType().isFloatingPoint()) Assert.assertTrue(Double.toString(valde), Double.isNaN(valde)); else Assert.assertTrue(ve.hasFillValue()); Number fillValue = N3iosp.getFillValueDefault(v.getDataType()); Assert.assertNotNull(v.getDataType().toString(), fillValue); Array data = v.read(); Array dataE = ve.read(); if (v.getDataType().isFloatingPoint()) Assert.assertTrue(Double.toString(valde), Double.isNaN(valde)); else
public Array readData2(Variable v2, Section section, double[] values) { Array data = Array.factory(v2.getDataType().getPrimitiveClassType(), section.getShape()); IndexIterator ii = data.getIndexIterator(); Range radialRange = section.getRange(0); for (int r = radialRange.first(); r <= radialRange.last(); r += radialRange.stride()) { ii.setDoubleNext(values[r]); } return data; }
private void augment3D(NetcdfDataset ds, CancelTask cancelTask) throws IOException { ds.addAttribute(null, new Attribute(CDM.CONVENTIONS, "NSSL National Reflectivity Mosaic")); addLongName(ds, "mrefl_mosaic", "3-D reflectivity mosaic grid"); addCoordinateAxisType(ds, "Height", AxisType.Height); addCoordSystem(ds); Variable var = ds.findVariable("mrefl_mosaic"); assert var != null; float scale_factor = Float.NaN; Attribute att = var.findAttributeIgnoreCase("Scale"); if (att != null) { scale_factor = att.getNumericValue().floatValue(); var.addAttribute(new Attribute(CDM.SCALE_FACTOR, 1.0f / scale_factor)); } att = ds.findGlobalAttributeIgnoreCase("MissingData"); if (null != att) { float val = att.getNumericValue().floatValue(); if (!Float.isNaN(scale_factor)) val *= scale_factor; var.addAttribute(new Attribute(CDM.MISSING_VALUE, (short) val)); } // hack Array missingData = Array.factory(DataType.SHORT.getPrimitiveClassType(), new int[] {2}, new short[] {-990, -9990}); var.addAttribute(new Attribute(CDM.MISSING_VALUE, missingData)); var.addAttribute(new Attribute(_Coordinate.Axes, "Height Lat Lon")); }
throw new IllegalStateException("volScan.hasDifferentDopplarResolutions"); radialDim = new Dimension("radial", volScan.getMaxRadials()); ncfile.addDimension( null, radialDim); ncfile.addAttribute(null, new Attribute("Station", volScan.getStationId())); ncfile.addAttribute(null, new Attribute("StationName", volScan.getStationName())); ncfile.addAttribute(null, new Attribute("StationLatitude", new Double(volScan.getStationLatitude()))); ncfile.addAttribute(null, new Attribute("StationLongitude", new Double(volScan.getStationLongitude()))); ncfile.addAttribute(null, new Attribute("StationElevationInMeters", new Double(volScan.getStationElevation()))); Variable ct = new Variable(ncfile, null, null, "radialCoordinateTransform"); ct.setDataType(DataType.CHAR); ct.setDimensions(""); // scalar ct.addAttribute( new Attribute("transform_name", "Radial")); ct.addAttribute( new Attribute("center_latitude", new Double(volScan.getStationLatitude()))); ct.addAttribute( new Attribute(_Coordinate.AxisTypes, "RadialElevation RadialAzimuth RadialDistance")); Array data = Array.factory(DataType.CHAR.getPrimitiveClassType(), new int[0], new char[] {' '}); ct.setCachedData(data, true); ncfile.addVariable(null, ct);
protected Array convertEnums(Array values) { DataType dt = DataType.getType(values.getElementType()); if (!dt.isNumeric()) System.out.println("HEY !dt.isNumeric()"); Array result = Array.factory(DataType.STRING, values.getShape()); IndexIterator ii = result.getIndexIterator(); values.resetLocalIterator(); while (values.hasNext()) { String sval = lookupEnumString(values.nextInt()); ii.setObjectNext(sval); } return result; } }
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.getPrimitiveClassType(), section.getShape(), dataObject); }
private void makeCoordinateData(Variable elev, Variable azim, DoradeSweep mySweep) { Object ele = (Object) mySweep.getElevations(); Object azi = (Object) mySweep.getAzimuths(); Array elevData = Array.factory( elev.getDataType().getPrimitiveClassType(), elev.getShape(), ele); Array aziData = Array.factory( azim.getDataType().getPrimitiveClassType(), azim.getShape(), azi); elev.setCachedData( elevData, false); azim.setCachedData( aziData, false); }
if (originalDataType.compareTo(varDataType) > 0 && !varDataType.isFloatingPoint()) { switch (varDataType) { case BOOLEAN: if (var.findAttribute(NetCDFUtilities.UNITS) == null) { String unit = null; if (inputUoM != null) { writer.addVariableAttribute(var, new Attribute(NetCDFUtilities.UNITS, unit)); bandVariable.variableUoM = unit; var, new Attribute(NetCDFUtilities.STANDARD_NAME, bandName)); var, new Attribute(DataPacking.ADD_OFFSET, dataPacker.getOffset())); writer.addVariableAttribute( var, new Attribute(DataPacking.SCALE_FACTOR, dataPacker.getScale())); source.getLocation())); } else { for (Attribute att : sourceVar.getAttributes()) { if (var.findAttribute(att.getFullName()) == null && !isBlacklistedAttribute(att, dataPacking)) { writer.addVariableAttribute(var, att);
@Override protected Array read(DatasetOuterDimension dset, NetcdfFile ncfile) throws IOException { Array data = getData(dset.getId()); if (data != null) return data; Attribute att = ncfile.findGlobalAttribute(gattName); if (att == null) throw new IllegalArgumentException("Unknown attribute name= " + gattName); data = att.getValues(); if (dtype == null) dtype = DataType.getType(data.getElementType()); if (dset.ncoord == 1) // LOOK ?? putData(dset.getId(), data); else { // duplicate the value to each of the coordinates Array allData = Array.factory(dtype, new int[]{dset.ncoord}); for (int i = 0; i < dset.ncoord; i++) Array.arraycopy(data, 0, allData, i, 1); // LOOK generalize to vectors ?? putData(dset.getId(), allData); data = allData; } return data; }