static private void reflectArrayCopyOut(Object jArray, Array aa, IndexIterator aaIter) { Class cType = jArray.getClass().getComponentType(); //if (cType.isPrimitive()) { // Rob Weingruber <weingrub@rap.ucar.edu> May 11, 2011 if (!cType.isArray()) { aa.copyTo1DJavaArray(aaIter, jArray); // subclass does type-specific copy } else { for (int i = 0; i < java.lang.reflect.Array.getLength(jArray); i++) // recurse reflectArrayCopyOut(java.lang.reflect.Array.get(jArray, i), aa, aaIter); } }
private String arrayToString(Array values) { Object v = values.copyTo1DJavaArray(); if (v instanceof Object[]) { Object[] array = (Object[]) v; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.length; i++) { sb.append((String) array[i]); } return sb.toString().trim(); } return values.toString().trim(); }
static private void reflectArrayCopyOut(Object jArray, Array aa, IndexIterator aaIter) { Class cType = jArray.getClass().getComponentType(); //if (cType.isPrimitive()) { // Rob Weingruber <weingrub@rap.ucar.edu> May 11, 2011 if (!cType.isArray()) { aa.copyTo1DJavaArray(aaIter, jArray); // subclass does type-specific copy } else { for (int i = 0; i < java.lang.reflect.Array.getLength(jArray); i++) // recurse reflectArrayCopyOut(java.lang.reflect.Array.get(jArray, i), aa, aaIter); } }
static private void reflectArrayCopyOut(Object jArray, Array aa, IndexIterator aaIter) { Class cType = jArray.getClass().getComponentType(); //if (cType.isPrimitive()) { // Rob Weingruber <weingrub@rap.ucar.edu> May 11, 2011 if (!cType.isArray()) { aa.copyTo1DJavaArray(aaIter, jArray); // subclass does type-specific copy } else { for (int i = 0; i < java.lang.reflect.Array.getLength(jArray); i++) // recurse reflectArrayCopyOut(java.lang.reflect.Array.get(jArray, i), aa, aaIter); } }
private String arrayToString(Array values) { Object v = values.copyTo1DJavaArray(); if (v instanceof Object[]) { Object[] array = (Object[]) v; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.length; i++) { sb.append((String) array[i]); } return sb.toString().trim(); } return values.toString().trim(); }
public Array concatArray(Array array1, Array array2) { float[] data1 = (float[]) array1.copyTo1DJavaArray(); float[] data2 = (float[]) array2.copyTo1DJavaArray(); float[] result = new float[data1.length + data2.length]; System.arraycopy(data1, 0, result, 0, data1.length); System.arraycopy(data2, 0, result, data1.length, data2.length); // now put it back into an ucar.ma2.Array int[] resultShape = new int[] {result.length}; return Array.factory(DataType.FLOAT, resultShape, result); }
public ProductData readDataFlip(Variable variable) throws ProductIOException { final int dataType = getProductDataType(variable); Array array; Object storage; try { array = variable.read(); storage = array.flip(0).copyTo1DJavaArray(); } catch (IOException e) { throw new ProductIOException(e.getMessage(), e); } return ProductData.createInstance(dataType, storage); }
try { Array tArray = t.read(); time = (double[]) tArray.copyTo1DJavaArray(); Array eArray = ele.read(); elevation = (float[]) eArray.copyTo1DJavaArray(); Array aArray = azi.read(); azimuth = (float[]) aArray.copyTo1DJavaArray(); Array rArray = rng.read(); range = (float[]) rArray.copyTo1DJavaArray(); rayStartIdx = (int[]) sidx0.read().copyTo1DJavaArray(); rayEndIdx = (int[]) sidx1.read().copyTo1DJavaArray(); Array sn = snumber.read(); nsweeps = ((int[]) sn.copyTo1DJavaArray()).length;
Variable t = ds.findVariable("time"); Array tArray = t.read(); time = (double[]) tArray.copyTo1DJavaArray(); elevation = (float[]) eArray.copyTo1DJavaArray(); azimuth = (float[]) aArray.copyTo1DJavaArray(); range = (float[]) rArray.copyTo1DJavaArray(); rayStartIdx = (int[]) sidx0.read().copyTo1DJavaArray(); rayEndIdx = (int[]) sidx1.read().copyTo1DJavaArray(); ray_n_gates = (int[]) var.read().copyTo1DJavaArray(); ray_start_index = (int[]) var.read().copyTo1DJavaArray();
/** * Creates a ProductData instance for the given netcdf attribute. * * @param attribute A netcdf attribute. * * @return A new ProductData instance with the attribute's data type and value. */ public static ProductData createProductData(Attribute attribute) { ProductData attributeValue; int productDataType = DataTypeUtils.getEquivalentProductDataType(attribute.getDataType(), false, false); if (productDataType == ProductData.TYPE_ASCII) { attributeValue = ProductData.createInstance(attribute.getStringValue()); } else { attributeValue = ProductData.createInstance(productDataType, attribute.getValues().copyTo1DJavaArray()); } return attributeValue; }
/** * 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(); }
/** * 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(Class wantType) { if (wantType == getElementType()) { 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(); }
/** * 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(Class wantType) { if (wantType == getElementType()) { 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(); }
@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 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 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); } }
assert (shape[1] == Asection2.getShape()[1]); Assert2.assertArrayNearlyEquals((double[]) Asection.copyTo1DJavaArray(), (double[]) Asection2.copyTo1DJavaArray());
@Test public void checkPaddingOnWriteReadOneDimByteArrayOnlyRecordVar() throws IOException, InvalidRangeException { File tmpDataDir = tempFolder.newFolder(); File testFile = new File(tmpDataDir, "file.nc"); try( NetcdfFileWriter ncfWriteable = NetcdfFileWriter.createNew(testFile.getPath(), true)) { Dimension recDim = ncfWriteable.addUnlimitedDimension("v"); Variable var = ncfWriteable.addVariable("v", DataType.BYTE, "v"); assertEquals(1, var.getElementSize()); ncfWriteable.create(); N3header.Vinfo vinfo = (N3header.Vinfo) var.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -2, -3, -4, -5, -6, -7, -8, -9}; Array dataArray = Array.factory(DataType.BYTE, new int[]{data.length}, data); ncfWriteable.write(var.getFullNameEscaped(), dataArray); ncfWriteable.close(); NetcdfFile ncf = NetcdfFile.open(testFile.getPath()); Variable readVar = ncf.findVariable("v"); assertEquals(readVar.getDataType(), DataType.BYTE); assertEquals(1, readVar.getElementSize()); vinfo = (N3header.Vinfo) readVar.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); int[] org = {0}; byte[] readdata = (byte[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray(); assertArrayEquals(data, readdata); } }
@Test public void checkPaddingOnWriteReadOneDimCharArrayOnlyRecordVar() throws IOException, InvalidRangeException { File tmpDataDir = tempFolder.newFolder(); File testFile = new File(tmpDataDir, "file.nc"); try(NetcdfFileWriter ncfWriteable = NetcdfFileWriter.createNew(testFile.getPath(), true)) { Dimension recDim = ncfWriteable.addUnlimitedDimension("v"); Variable var = ncfWriteable.addVariable("v", DataType.CHAR, "v"); assertEquals(1, var.getElementSize()); ncfWriteable.create(); N3header.Vinfo vinfo = (N3header.Vinfo) var.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); char[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; Array dataArray = Array.factory(DataType.CHAR, new int[]{data.length}, data); ncfWriteable.write(var.getFullNameEscaped(), dataArray); ncfWriteable.close(); NetcdfFile ncf = NetcdfFile.open(testFile.getPath()); Variable readVar = ncf.findVariable("v"); assertEquals(readVar.getDataType(), DataType.CHAR); assertEquals(1, readVar.getElementSize()); vinfo = (N3header.Vinfo) readVar.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); int[] org = {0}; char[] readdata = (char[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray(); assertArrayEquals(data, readdata); } }
byte[] readdata = (byte[]) inv.read(org, inv.getShape()).copyTo1DJavaArray();