@Override public void setDataToZero() { float[] data = (float[]) dataArray.get1DJavaArray(dataArray.getDataType()); Arrays.fill(data, 0.0f); }
/** * Make a vlen array * @param shape the outer shape, ie excluding the vlen dimension * @param storage must be an Array type. must not be null * @return ArrayObject */ static public Array makeVlenArray(int[] shape, @Nonnull Array[] storage) { Index index = Index.factory(shape); return ArrayObject.factory(storage[0].getDataType(), storage[0].getClass(), true, index, storage); }
/** * Construct attribute with Array of values. * * @param name name of attribute * @param values array of values. */ public Attribute(String name, Array values) { this(name,values.getDataType()); setValues(values); setImmutable(); }
public static ByteString copyArrayToByteString(Array data) { int nbytes = (int) data.getSizeBytes(); if (nbytes < 0) { System.out.printf("copyArrayToByteString neg byte size %d dataType = %d data size %d shape = %s%n", nbytes, data.getDataType().getSize(), data.getSize(), Misc.showInts(data.getShape())); } ByteBuffer bb = ByteBuffer.allocate(nbytes); bb.order(ByteOrder.nativeOrder()); copyArrayToBB(data, false, bb); bb.flip(); return ByteString.copyFrom(bb); }
/** * Create a copy of this Array, copying the data so that physical order is the same as * logical order * * @return the new Array */ public Array copy() { Array newA = factory(getDataType(), getShape()); MAMath.copy(newA, this); return newA; }
/** * Reshape this array without copying data * * @param shape the new shape * @return the new Array, using same backing object * @throws IllegalArgumentException new shape is not conformable */ public Array reshapeNoCopy(int[] shape) { Array result = factory(this.getDataType(), shape, getStorage()); if (result.getSize() != getSize()) throw new IllegalArgumentException("reshape arrays must have same total size"); return result; }
/** * 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()); }
/** * Add extra 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()); }
/** * Add elements of two arrays together, allocating the result array. * The result type and the operation type are taken from the type of a. * * @param a add values from here * @param b add values from here * @return result = a + b * @throws IllegalArgumentException a and b are not conformable * @throws UnsupportedOperationException dont support this data type yet */ public static Array add(Array a, Array b) throws IllegalArgumentException { Array result = Array.factory(a.getDataType(), a.getShape()); if (a.getElementType() == double.class) { addDouble(result, a, b); } else throw new UnsupportedOperationException(); return result; }
/** * Create a new Array by copying this Array to a new one with given shape * * @param shape the new shape * @return the new Array * @throws IllegalArgumentException new shape is not conformable */ public Array reshape(int[] shape) { Array result = factory(this.getDataType(), shape); if (result.getSize() != getSize()) throw new IllegalArgumentException("reshape arrays must have same total size"); Array.arraycopy(this, 0, result, 0, (int) getSize()); return result; }
/** * An implementation of {@link Object#hashCode} that is consistent with {@link #equals(Array, Array)}. * * @param array an array to hash. * @return a hash code value for the array. */ // TODO: Should we add this to Array as the Object.hashCode() implementation? How much work is that? public static int hashCode(Array array) { if (array == null) { return 0; } int hash = 3; hash = 29 * hash + array.getDataType().hashCode(); hash = 29 * hash + Arrays.hashCode(array.getShape()); // We can't simply hash array.getStorage(), because array may be a "view" that doesn't include all of the // elements in the backing store. for (IndexIterator iter = array.getIndexIterator(); iter.hasNext(); ) { hash = 29 * hash + iter.next().hashCode(); hash = 29 * hash + Arrays.hashCode(iter.getCurrentCounter()); } return hash; } }
/** * Gets the data from symmetric mapping. One geolocation maps one observation. * * @param array the array * @param i the i * @param j the j * @return the data sym */ public static Double getDataSym(Array array, int i, int j) { Index dataIndex = Index.factory(array.getShape()); int[] location = {i,j}; dataIndex.set(location); switch (array.getDataType()) { case INT: return new Double((Integer)array.getObject(dataIndex)); case SHORT: return new Double((Short)array.getObject(dataIndex)); case FLOAT: return new Double((Float)array.getObject(dataIndex)); case DOUBLE: return new Double((Double)array.getObject(dataIndex)); case LONG: return new Double((Long)array.getObject(dataIndex)); default: return (Double) array.getObject(dataIndex); } }
/** * Cover for System.arraycopy(). Works with the underlying data arrays. * ArraySrc and ArrayDst must be the same primitive type. * Exposed for efficiency; use at your own risk. * * @param arraySrc copy from here : if not in canonical order, an extra copy will be done * @param srcPos starting at * @param arrayDst copy to here : must be in canonical order * @param dstPos starting at * @param len number of elements to copy */ static public void arraycopy(Array arraySrc, int srcPos, Array arrayDst, int dstPos, int len) { // deal with special case if (arraySrc.isConstant()) { double d = arraySrc.getDouble(0); for (int i = dstPos; i < dstPos + len; i++) arrayDst.setDouble(i, d); return; } Object src = arraySrc.get1DJavaArray(arraySrc.getDataType()); // ensure canonical order Object dst = arrayDst.getStorage(); System.arraycopy(src, srcPos, dst, dstPos, len); }
void encodeVlenData(NcStreamProto.DataRow.Builder builder, Section section, Array data) { if (!(data instanceof ArrayObject)) throw new IllegalStateException("Unknown class for OPAQUE =" + data.getClass().getName()); IndexIterator iter = data.getIndexIterator(); int count = 0; while (iter.hasNext()) { Array varray = (Array) iter.next(); int vlensize = (int) varray.getSize(); builder.addVlens(vlensize); count += vlensize; } builder.setNelems(count); Section ssection = section.removeLast(); builder.setSection(NcStream.encodeSection(ssection)); assert section.computeSize() == count; int nbytes = count * data.getDataType().getSize(); ByteBuffer bb = ByteBuffer.allocate(nbytes); // copyArrayToBB(data, bb); iter = data.getIndexIterator(); while (iter.hasNext()) { Array varray = (Array) iter.next(); copyArrayToBB(varray, bb); } }
/** * This gets the equivalent java array of the wanted type, in correct order. * It avoids copying if possible. * * @param wantType returned object will be an array of this type. This must be convertible to it. * @return java array of type want */ public Object get1DJavaArray(DataType wantType) { if (wantType == getDataType()) { if (indexCalc.isFastIterator()) return getStorage(); // already in order else return copyTo1DJavaArray(); // gotta copy } // gotta convert to new type Array newA = factory(wantType, getShape()); MAMath.copy(newA, this); return newA.getStorage(); }
private Array convertEnums(Array values) { if (!values.getDataType().isIntegral()) { return values; // Nothing to do! } 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; }
@Test public void testRequestNetCDFNaNDataPacking() throws Exception { // http response from the request inside the string MockHttpServletResponse response = getAsServletResponse( "ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageId=wcs__visibilityNaNPacked&format=application/x-netcdf"); assertNotNull(response); byte[] netcdfOut = getBinary(response); File file = File.createTempFile("netcdf", "outNaNPK.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, netcdfOut); NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath()); Variable var = dataset.findVariable(STANDARD_NAME); assertNotNull(var); Array readData = var.read(NETCDF_SECTION); assertEquals(DataType.SHORT, readData.getDataType()); // Check the fix on dataPacking NaN management assertNotEquals(readData.getShort(0), -32768, 1E-6); dataset.close(); }
@Test public void testReadMrutpTimeRangeWithMultipleVerticalLevel() throws IOException, InvalidRangeException { // read more than one time coordinate at a time in a MRUTP. multiple verticals try (NetcdfDataset ds = NetcdfDataset.openDataset(TestDir.cdmUnitTestDir + "gribCollections/tp/GFSonedega.ncx4")) { Variable v = ds.findVariable(null, "Relative_humidity_isobaric"); assert v != null; Array data = v.read("0:1, 10:20:2, 50, 50"); assert data != null; assert data.getRank() == 4; assert data.getDataType() == DataType.FLOAT; assert data.getSize() == 12; logger.debug("{}", NCdumpW.toString(data)); while (data.hasNext()) { float val = data.nextFloat(); assert !Float.isNaN(val); } float[] got = (float []) data.copyTo1DJavaArray(); float[] expect = new float[] {57.8f, 53.1f, 91.3f, 85.5f, 80.0f, 69.3f, 32.8f, 41.8f, 88.9f, 81.3f, 70.9f, 70.6f}; Assert.assertArrayEquals(expect, got, (float) Misc.defaultMaxRelativeDiffFloat); } }
@Test public void testReadMrutpTimeRangeWithSingleVerticalLevel() throws IOException, InvalidRangeException { // read more than one time coordinate at a time in a MRUTP, with vertical try (NetcdfDataset ds = NetcdfDataset.openDataset(TestDir.cdmUnitTestDir + "gribCollections/tp/GFSonedega.ncx4")) { Variable v = ds.findVariable(null, "Relative_humidity_sigma"); assert v != null; Array data = v.read("0:1, 0, 50, 50"); assert data != null; assert data.getRank() == 4; assert data.getDataType() == DataType.FLOAT; assert data.getSize() == 2; logger.debug("{}", NCdumpW.toString(data)); while (data.hasNext()) { float val = data.nextFloat(); assert !Float.isNaN(val); } float[] got = (float []) data.copyTo1DJavaArray(); float[] expect = new float[] {68.0f, 74.0f}; Assert.assertArrayEquals(expect, got, (float) Misc.defaultMaxRelativeDiffFloat); } }
@Test public void testReadMrutpTimeRange() throws IOException, InvalidRangeException { // read more than one time coordinate at a time in a MRUTP, no vertical try (NetcdfDataset ds = NetcdfDataset.openDataset(TestDir.cdmUnitTestDir + "gribCollections/tp/GFSonedega.ncx4")) { Variable v = ds.findVariable(null, "Pressure_surface"); assert v != null; Array data = v.read("0:1,50,50"); assert data != null; assert data.getRank() == 3; assert data.getDataType() == DataType.FLOAT; assert data.getSize() == 2; float[] got = (float []) data.copyTo1DJavaArray(); float[] expect = new float[] {103031.914f, 103064.164f}; Assert.assertArrayEquals(expect, got, (float) Misc.defaultMaxRelativeDiffFloat); } }