public float getT(String tName, int swpNumber, int ray) throws IOException { Array timeData = ds.findVariable(tName).read(); Index timeIndex = timeData.getIndex(); return timeData.getFloat(timeIndex.set(swpNumber, ray)); }
public static Array convert2Unpacked(Array packed, ScaleOffset scaleOffset) { Array result = Array.factory(DataType.DOUBLE, packed.getShape()); IndexIterator riter = result.getIndexIterator(); while (packed.hasNext()) { riter.setDoubleNext( packed.nextDouble() * scaleOffset.scale + scaleOffset.offset); } return result; }
private CoordinateAxis makeCoordAxis(NetcdfDataset ds, String axisName) throws IOException { Variable stagV = ds.findVariable(axisName + "_stag"); Array data_stag = stagV.read(); int n = (int) data_stag.getSize() - 1; Array data = Array.factory(data_stag.getElementType(), new int[]{n}); Index stagIndex = data_stag.getIndex(); Index dataIndex = data.getIndex(); for (int i = 0; i < n; i++) { double val = data_stag.getDouble(stagIndex.set(i)) + data_stag.getDouble(stagIndex.set(i + 1)); data.setDouble(dataIndex.set(i), 0.5 * val); } DataType dtype = DataType.getType(data.getElementType()); String units = ds.findAttValueIgnoreCase(stagV, CDM.UNITS, "m"); CoordinateAxis v = new CoordinateAxis1D(ds, null, axisName, dtype, axisName, units, "synthesized non-staggered " + axisName + " coordinate"); v.setCachedData(data, true); return v; }
MStationObsImpl(ucar.unidata.geoloc.Station s, int stationIndex, int obsIndex, List<VariableSimpleIF> dataVariables, StructureMembers sm) throws IOException { super(s, 0, 0); this.stationIndex = stationIndex; this.obsIndex = obsIndex; this.dataVariables = dataVariables; this.sm = sm; // must read the time Array timeData = readData(timeVar, stationIndex, obsIndex); obsTime = timeData.getDouble( timeData.getIndex()); }
/** * Add extra outermost dimension with len = 1. * * @param org original array * @return rank1 array of rank + 1 */ static public Array makeArrayRankPlusOne(Array org) { int[] shape = new int[org.getRank() + 1]; System.arraycopy(org.getShape(), 0, shape, 1, org.getRank()); shape[0] = 1; return factory(org.getDataType(), shape, org.getStorage()); }
final IndexIterator it = array.getIndexIterator(); for (int y = ymax; --y >= ymin; ) { for (int x = xmin; x < xmax; x++) { array.getDataAsByteBuffer().asDoubleBuffer(); double[] samples = new double[destRegion.width * destRegion.height]; doubleBuffer.get(samples); FloatBuffer floatBuffer = array.getDataAsByteBuffer().asFloatBuffer(); floatBuffer.get(samples); raster.setSamples( destRegion.width, destRegion.height, array.getDataAsByteBuffer().array()); break; case DataBuffer.TYPE_INT: IntBuffer intBuffer = array.getDataAsByteBuffer().asIntBuffer(); int[] intSamples = new int[destRegion.width * destRegion.height]; intBuffer.get(intSamples); default: final IndexIterator it = array.getIndexIterator(); for (int y = ymin; y < ymax; y++) { for (int x = xmin; x < xmax; x++) {
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); }
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); }
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 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); }
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; }
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()); } }
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; }
@Test public void testCachedData() throws IOException { NetcdfFile ncd = null; try { ncd = NetcdfDataset.openFile(TestDir.cdmLocalTestDataDir + "point/profileMultidim.ncml", null); Variable v = ncd.findVariable("data"); assert v != null; Array data = v.read(); assert data.getSize() == 50 : data.getSize(); } finally { if (ncd != null) ncd.close(); } }
@Test public void testThinGridAtEndofFile() throws IOException { final String testfile = "../grib/src/test/data/thinGrid.grib1"; try (NetcdfFile nc = NetcdfFile.open(testfile)) { Variable var = nc.findVariable("Temperature_isobaric"); Array data = var.read(); Assert.assertEquals(73*73, data.getSize()); float first = data.getFloat(0); float last = data.getFloat((73*73)-1); Assert.assertEquals(291.0, first, 1e-6); Assert.assertEquals(278.0, last, 1e-6); } }
@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}); } }
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)); }
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); }