Java Code Examples for ucar.ma2.IndexIterator#hasNext()
The following examples show how to use
ucar.ma2.IndexIterator#hasNext() .
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: WRFConvention.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
private Array convertToDegrees(Variable.Builder<?> vb) { VariableDS.Builder<?> vds = (VariableDS.Builder<?>) vb; Variable v = vds.orgVar; Array data; try { data = v.read(); data = data.reduce(); } catch (IOException ioe) { throw new RuntimeException("data read failed on " + v.getFullName() + "=" + ioe.getMessage()); } IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) { ii.setDoubleCurrent(Math.toDegrees(ii.getDoubleNext())); } return data; }
Example 2
Source File: TestVariableDSBuilder.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testMissingData() throws IOException { Group.Builder parent = Group.builder().addDimension(Dimension.builder("dim1", 7).setIsUnlimited(true).build()) .addDimension(new Dimension("dim2", 27)); VariableDS vds = VariableDS.builder().setName("name").setDataType(DataType.FLOAT).setUnits("units").setDesc("desc") .setEnhanceMode(NetcdfDataset.getEnhanceAll()).addAttribute(new Attribute("missing_value", 0.0f)) .setParentGroupBuilder(parent).setDimensionsByName("dim1").build(parent.build()); Array data = vds.read(); System.out.printf("data = %s%n", data); IndexIterator iter = data.getIndexIterator(); while (iter.hasNext()) { assertThat(iter.getFloatNext()).isEqualTo(Float.NaN); } }
Example 3
Source File: Ncdump.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static void printVariableArray(Formatter out, ArrayObject array, Indent indent, CancelTask ct) { out.format("%n%s{", indent); indent.incr(); IndexIterator iter = array.getIndexIterator(); boolean first = true; while (iter.hasNext()) { Array data = (Array) iter.next(); if (!first) { out.format(", "); } printArray(out, data, indent, ct); first = false; } indent.decr(); out.format("%n%s}", indent); }
Example 4
Source File: H5headerNew.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
private Array findReferenceObjectNames(Array data) throws IOException { IndexIterator ii = data.getIndexIterator(); Array newData = Array.factory(DataType.STRING, data.getShape()); IndexIterator ii2 = newData.getIndexIterator(); while (ii.hasNext()) { long objId = ii.getLongNext(); DataObject dobj = getDataObject(objId, null); if (dobj == null) { log.warn("readReferenceObjectNames cant find obj= {}", objId); } else { if (debugReference) { log.debug(" Referenced object= {}", dobj.who); } ii2.setObjectNext(dobj.who); } } return newData; }
Example 5
Source File: GeotiffWriter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
private double geoShiftGetXstart(Array lon, double inc) { Index ilon = lon.getIndex(); int[] lonShape = lon.getShape(); IndexIterator lonIter = lon.getIndexIterator(); double xlon; LatLonPoint p0 = LatLonPoint.create(0, lon.getFloat(ilon.set(0))); LatLonPoint pN = LatLonPoint.create(0, lon.getFloat(ilon.set(lonShape[0] - 1))); xlon = p0.getLongitude(); while (lonIter.hasNext()) { float l = lonIter.getFloatNext(); LatLonPoint pn = LatLonPoint.create(0, l); if (pn.getLongitude() < xlon) { xlon = pn.getLongitude(); } } if (p0.getLongitude() == pN.getLongitude()) { xlon = xlon - inc; } return xlon; }
Example 6
Source File: GeotiffWriter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Replace missing values with 0; scale other values between 1 and 255, return a byte data array. * * @param grid GridDatatype * @param data input data array * @return byte data array with missing values replaced and data scaled from 1- 255. */ private ArrayByte replaceMissingValuesAndScale(IsMissingEvaluator grid, Array data, MAMath.MinMax dataMinMax) { double scale = 254.0 / (dataMinMax.max - dataMinMax.min); ArrayByte byteArray = (ArrayByte) Array.factory(DataType.BYTE, data.getShape()); IndexIterator dataIter = data.getIndexIterator(); IndexIterator resultIter = byteArray.getIndexIterator(); byte bv; while (dataIter.hasNext()) { double v = dataIter.getDoubleNext(); if (grid.isMissing(v)) { bv = 0; } else { int iv = (int) ((v - dataMinMax.min) * scale + 1); bv = (byte) (iv & 0xff); } resultIter.setByteNext(bv); } return byteArray; }
Example 7
Source File: GeotiffWriter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Replace missing values with dataMinMax.min - 1.0; return a floating point data array. * * @param grid GridDatatype * @param data input data array * @return floating point data array with missing values replaced. */ private ArrayFloat replaceMissingValues(IsMissingEvaluator grid, Array data, MAMath.MinMax dataMinMax) { float minValue = (float) (dataMinMax.min - 1.0); ArrayFloat floatArray = (ArrayFloat) Array.factory(DataType.FLOAT, data.getShape()); IndexIterator dataIter = data.getIndexIterator(); IndexIterator floatIter = floatArray.getIndexIterator(); while (dataIter.hasNext()) { float v = dataIter.getFloatNext(); if (grid.isMissing((double) v)) { v = minValue; } floatIter.setFloatNext(v); } return floatArray; }
Example 8
Source File: AWIPSConvention.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private CoordinateAxis makeTimeCoordAxisFromReference(NetcdfDataset ds, Variable timeVar, Array vals) { Variable refVar = ds.findVariable("reftime"); if (refVar == null) return null; double refValue; try { Array refArray = refVar.read(); refValue = refArray.getDouble(refArray.getIndex()); // get the first value } catch (IOException ioe) { return null; } if (refValue == N3iosp.NC_FILL_DOUBLE) return null; // construct the values array - make it a double to be safe Array dvals = Array.factory(DataType.DOUBLE, vals.getShape()); IndexIterator diter = dvals.getIndexIterator(); IndexIterator iiter = vals.getIndexIterator(); while (iiter.hasNext()) diter.setDoubleNext(iiter.getDoubleNext() + refValue); // add reftime to each of the values String units = ds.findAttValueIgnoreCase(refVar, CDM.UNITS, "seconds since 1970-1-1 00:00:00"); units = normalize(units); String desc = "synthesized time coordinate from reftime, valtimeMINUSreftime"; CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, "timeCoord", DataType.DOUBLE, "record", units, desc); timeCoord.setCachedData(dvals, true); parseInfo.format("Created Time Coordinate Axis From Reference = "); timeCoord.getNameAndDimensions(parseInfo, true, false); parseInfo.format("%n"); return timeCoord; }
Example 9
Source File: CDMDSP.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected DapAttribute buildattribute(Attribute cdmattr) throws DapException { DapType attrtype = CDMTypeFcns.cdmtype2daptype(cdmattr.getDataType()); EnumTypedef cdmenum = cdmattr.getEnumType(); boolean enumfillvalue = (cdmattr.getShortName().equals(FILLVALUE) && cdmenum != null); DapEnumeration dapenum = null; // We need to handle _FillValue specially if the // the variable is enum typed. if (enumfillvalue) { cdmenum = findMatchingEnum(cdmenum); // Make sure the cdm attribute has type enumx if (!cdmenum.getBaseType().isEnum()) throw new DapException("CDM _FillValue attribute type is not enumX"); // Modify the attr cdmattr.setEnumType(cdmenum); // Now, map to a DapEnumeration dapenum = (DapEnumeration) this.nodemap.get(cdmenum); if (dapenum == null) throw new DapException("Illegal CDM variable attribute type: " + cdmenum); attrtype = dapenum; } if (attrtype == null) throw new DapException("DapFile: illegal CDM variable attribute type: " + cdmattr.getDataType()); DapAttribute dapattr = (DapAttribute) dmrfactory.newAttribute(cdmattr.getShortName(), attrtype); recordNode(cdmattr, dapattr); // Transfer the values Array values = cdmattr.getValues(); if (!validatecdmtype(cdmattr.getDataType(), values.getElementType())) throw new DapException("Attr type versus attribute data mismatch: " + values.getElementType()); IndexIterator iter = values.getIndexIterator(); String[] valuelist = null; Object vec = CDMTypeFcns.createVector(cdmattr.getDataType(), values.getSize()); for (int i = 0; iter.hasNext(); i++) { java.lang.reflect.Array.set(vec, i, iter.next()); } valuelist = (String[]) Convert.convert(DapType.STRING, attrtype, vec); dapattr.setValues(valuelist); return dapattr; }
Example 10
Source File: AWIPSConvention.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Nullable private CoordinateAxis.Builder makeTimeCoordAxisFromReference(Array vals) { if (!rootGroup.findVariableLocal("reftime").isPresent()) return null; VariableDS.Builder refVar = (VariableDS.Builder) rootGroup.findVariableLocal("reftime").get(); double refValue; try { Array refArray = refVar.orgVar.read(); refValue = refArray.getDouble(refArray.getIndex()); // get the first value } catch (IOException ioe) { return null; } if (refValue == N3iosp.NC_FILL_DOUBLE) // why? return null; // construct the values array - make it a double to be safe Array dvals = Array.factory(DataType.DOUBLE, vals.getShape()); IndexIterator diter = dvals.getIndexIterator(); IndexIterator iiter = vals.getIndexIterator(); while (iiter.hasNext()) diter.setDoubleNext(iiter.getDoubleNext() + refValue); // add reftime to each of the values String name = "timeCoord"; String units = refVar.getAttributeContainer().findAttributeString(CDM.UNITS, "seconds since 1970-1-1 00:00:00"); units = normalize(units); String desc = "synthesized time coordinate from reftime, valtimeMINUSreftime"; CoordinateAxis1D.Builder timeCoord = CoordinateAxis1D.builder().setName(name).setDataType(DataType.DOUBLE).setParentGroupBuilder(rootGroup) .setDimensionsByName("record").setUnits(units).setDesc(desc).setCachedData(dvals, true); parseInfo.format("Created Time Coordinate Axis From reftime Variable%n"); return timeCoord; }
Example 11
Source File: ZebraConvention.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected void augmentDataset(CancelTask cancelTask) throws IOException { NcMLReaderNew.wrapNcMLresource(datasetBuilder, CoordSystemFactory.resourcesDir + "Zebra.ncml", cancelTask); // special time handling // the time coord var is created in the NcML // set its values = base_time + time_offset(time) Dimension timeDim = rootGroup.findDimension("time").orElse(null); VariableDS.Builder base_time = (VariableDS.Builder) rootGroup.findVariableLocal("base_time").orElse(null); VariableDS.Builder time_offset = (VariableDS.Builder) rootGroup.findVariableLocal("time_offset").orElse(null); Variable.Builder time = rootGroup.findVariableLocal("time").orElse(null); if ((timeDim == null) || (base_time == null) || (time_offset == null) || (time == null)) return; String units = base_time.getAttributeContainer().findAttributeString(CDM.UNITS, "seconds since 1970-01-01 00:00 UTC"); time.addAttribute(new Attribute(CDM.UNITS, units)); Array data; try { double baseValue = base_time.orgVar.readScalarDouble(); data = time_offset.orgVar.read(); IndexIterator iter = data.getIndexIterator(); while (iter.hasNext()) iter.setDoubleCurrent(iter.getDoubleNext() + baseValue); } catch (IOException ioe) { parseInfo.format("ZebraConvention failed to create time Coord Axis for file %s err= %s%n", datasetBuilder.location, ioe); return; } time.setCachedData(data, true); }
Example 12
Source File: NetcdfCopier.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private Array convertDataToChar(Variable newVar, Array oldData) { ArrayChar newData = (ArrayChar) Array.factory(DataType.CHAR, newVar.getShape()); Index ima = newData.getIndex(); IndexIterator ii = oldData.getIndexIterator(); while (ii.hasNext()) { String s = (String) ii.getObjectNext(); int[] c = ii.getCurrentCounter(); for (int i = 0; i < c.length; i++) { ima.setDim(i, c[i]); } newData.setString(ima, s); } return newData; }
Example 13
Source File: SigmetIOServiceProvider.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Read data from a top level Variable and send data to a WritableByteChannel. * * @param v2 Variable * @param section wanted section of data of Variable. The section list is a list * of ucar.ma2.Range which define the requested data subset. * @param channel WritableByteChannel object - channel that can write bytes. * @return the number of bytes written, possibly zero. */ public long readToByteChannel11(ucar.nc2.Variable v2, Section section, WritableByteChannel channel) throws java.io.IOException { Array data = readData(v2, section); float[] ftdata = new float[(int) data.getSize()]; byte[] bytedata = new byte[(int) data.getSize() * 4]; IndexIterator iter = data.getIndexIterator(); int i = 0; ByteBuffer buffer = ByteBuffer.allocateDirect(bytedata.length); while (iter.hasNext()) { ftdata[i] = iter.getFloatNext(); bytedata[i] = new Float(ftdata[i]).byteValue(); buffer.put(bytedata[i]); i++; } buffer = ByteBuffer.wrap(bytedata); // write the bytes to the channel int count = channel.write(buffer); // check if all bytes where written if (buffer.hasRemaining()) { // if not all bytes were written, move the unwritten bytes to the beginning and // set position just after the last unwritten byte buffer.compact(); } else { buffer.clear(); } return (long) count; }
Example 14
Source File: CoverageDatasetCapabilities.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
private Element writeAxis(CoverageCoordAxis axis) { Element varElem = new Element("axis"); varElem.setAttribute("name", axis.getName()); varElem.setAttribute("shape", Arrays.toString(axis.getShape())); DataType dt = axis.getDataType(); varElem.setAttribute("type", dt.toString()); AxisType axisType = axis.getAxisType(); if (null != axisType) varElem.setAttribute("axisType", axisType.toString()); if (axis.getDependsOn() != null && !axis.getDependsOn().trim().isEmpty()) varElem.setAttribute("dependsOn", axis.getDependsOn().trim()); // attributes for (Attribute att : axis.getAttributes()) { varElem.addContent(ncmlWriter.makeAttributeElement(att)); } /* * f.format("%s npts: %d [%f,%f] spacing=%s", indent, ncoords, startValue, endValue, spacing); * if (getResolution() != 0.0) * f.format(" resolution=%f", resolution); * f.format(" %s :", getDependenceType()); * for (String s : dependsOn) */ Element values = new Element("values"); if (!axis.isRegular()) { Array array = axis.getCoordsAsArray(); boolean isRealType = (array.getDataType() == DataType.DOUBLE) || (array.getDataType() == DataType.FLOAT); IndexIterator iter = array.getIndexIterator(); StringBuilder buff = new StringBuilder(); buff.append(isRealType ? iter.getDoubleNext() : iter.getIntNext()); while (iter.hasNext()) { buff.append(" "); buff.append(isRealType ? iter.getDoubleNext() : iter.getIntNext()); } values.setText(buff.toString()); } values.setAttribute("spacing", axis.getSpacing().toString()); values.setAttribute("npts", Long.toString(axis.getNcoords())); values.setAttribute("start", Double.toString(axis.getStartValue())); values.setAttribute("end", Double.toString(axis.getEndValue())); if (axis.getResolution() != 0.0) values.setAttribute("resolution", Double.toString(axis.getResolution())); varElem.addContent(values); return varElem; }
Example 15
Source File: VariableEnhancer.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Array convert(Array in, boolean convertUnsigned, boolean applyScaleOffset, boolean convertMissing) { if (!in.getDataType().isNumeric() || (!convertUnsigned && !applyScaleOffset && !convertMissing)) { return in; // Nothing to do! } if (getSignedness() == Signedness.SIGNED) { convertUnsigned = false; } if (!hasScaleOffset()) { applyScaleOffset = false; } DataType outType = origDataType; if (convertUnsigned) { outType = getUnsignedConversionType(); } if (applyScaleOffset) { outType = getScaledOffsetType(); } if (outType != DataType.FLOAT && outType != DataType.DOUBLE) { convertMissing = false; } Array out = Array.factory(outType, in.getShape()); IndexIterator iterIn = in.getIndexIterator(); IndexIterator iterOut = out.getIndexIterator(); while (iterIn.hasNext()) { Number value = (Number) iterIn.getObjectNext(); if (convertUnsigned) { value = convertUnsigned(value); } if (applyScaleOffset) { value = applyScaleOffset(value); } if (convertMissing) { value = convertMissing(value); } iterOut.setObjectNext(value); } return out; }
Example 16
Source File: WRFConvention.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Nullable private CoordinateAxis.Builder makeZCoordAxis(String axisName, String dimName) { Optional<Dimension> dimOpt = rootGroup.findDimension(dimName); if (!dimOpt.isPresent()) { return null; } Dimension dim = dimOpt.get(); String fromWhere = axisName.endsWith("stag") ? "ZNW" : "ZNU"; CoordinateAxis.Builder v = CoordinateAxis1D.builder().setName(axisName).setDataType(DataType.DOUBLE).setParentGroupBuilder(rootGroup) .setDimensionsByName(dim.getShortName()).setUnits("").setDesc("eta values from variable " + fromWhere); v.addAttribute(new Attribute(CF.POSITIVE, CF.POSITIVE_DOWN)); // eta coordinate is 1.0 at bottom, 0 at top v.setAxisType(AxisType.GeoZ); v.addAttribute(new Attribute(_Coordinate.AxisType, "GeoZ")); if (!axisName.equals(dim.getShortName())) v.addAttribute(new Attribute(_Coordinate.AliasForDimension, dim.getShortName())); // create eta values from file variables: ZNU, ZNW // But they are a function of time though the values are the same in the sample file // NOTE: Use first time sample assuming all are the same!! Optional<Variable.Builder<?>> etaVarOpt = rootGroup.findVariableLocal(fromWhere); if (!etaVarOpt.isPresent()) { return makeFakeCoordAxis(axisName, dim); } else { VariableDS.Builder<?> etaVarDS = (VariableDS.Builder<?>) etaVarOpt.get(); Variable etaVar = etaVarDS.orgVar; int n = etaVar.getShape(1); // number of eta levels int[] origin = {0, 0}; int[] shape = {1, n}; try { Array array = etaVar.read(origin, shape);// read first time slice ArrayDouble.D1 newArray = new ArrayDouble.D1(n); IndexIterator it = array.getIndexIterator(); int count = 0; while (it.hasNext()) { double d = it.getDoubleNext(); newArray.set(count++, d); } v.setCachedData(newArray, true); } catch (Exception e) { e.printStackTrace(); } // ADD: error? return v; } }
Example 17
Source File: NetcdfCopier.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
private Variable.Builder copyVariable(Group.Builder parent, Variable oldVar) throws IOException { Variable.Builder vb; DataType newType = oldVar.getDataType(); String dimNames = Dimensions.makeDimensionsString(oldVar.getDimensions()); if (newType == DataType.STRUCTURE) { Structure oldStruct = (Structure) oldVar; Structure.Builder sb = Structure.builder().setName(oldVar.getShortName()); for (Variable nested : oldStruct.getVariables()) { sb.addMemberVariable(copyVariable(parent, nested)); } vb = sb; } else { vb = Variable.builder().setName(oldVar.getShortName()).setDataType(newType); if (!extended && newType == DataType.STRING) { // find maximum length Array data = oldVar.read(); IndexIterator ii = data.getIndexIterator(); int max_len = 0; while (ii.hasNext()) { String s = (String) ii.getObjectNext(); max_len = Math.max(max_len, s.length()); } // add last dimension String strlenDimName = oldVar.getShortName() + "_strlen"; parent.addDimension(Dimension.builder(strlenDimName, max_len).setIsShared(false).build()); newType = DataType.CHAR; vb.setDataType(DataType.CHAR); dimNames += " " + strlenDimName; } } vb.setParentGroupBuilder(parent).setDimensionsByName(dimNames); if (newType.isEnum()) { EnumTypedef en = oldVar.getEnumTypedef(); vb.setEnumTypeName(en.getShortName()); } // attributes for (Attribute att : oldVar.attributes()) { vb.addAttribute(convertAttribute(att)); if (debug) { System.out.println("add varAtt= " + att); } } return vb; }