Java Code Examples for ucar.ma2.Array#getShape()
The following examples show how to use
ucar.ma2.Array#getShape() .
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: 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 2
Source File: AtmosSigma.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Get the 3D vertical coordinate array for this time step. * * @param timeIndex the time index. Ignored if !isTimeDependent(). * @return vertical coordinate array * @throws IOException problem reading data * @throws InvalidRangeException _more_ */ public ArrayDouble.D3 getCoordinateArray(int timeIndex) throws IOException, InvalidRangeException { Array ps = readArray(psVar, timeIndex); Index psIndex = ps.getIndex(); int nz = sigma.length; int[] shape2D = ps.getShape(); int ny = shape2D[0]; int nx = shape2D[1]; ArrayDouble.D3 result = new ArrayDouble.D3(nz, ny, nx); for (int y = 0; y < ny; y++) { for (int x = 0; x < nx; x++) { double psVal = ps.getDouble(psIndex.set(y, x)); for (int z = 0; z < nz; z++) { result.set(z, y, x, ptop + sigma[z] * (psVal - ptop)); } } } return result; }
Example 3
Source File: GeotiffWriter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void dump(Array data, int col) { int[] shape = data.getShape(); Index ima = data.getIndex(); for (int j = 0; j < shape[0]; j++) { float dd = data.getFloat(ima.set(j, col)); System.out.println(j + " value= " + dd); } }
Example 4
Source File: TestS3Read.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public <T extends NetcdfFile> void testG16RadVar(T nc) throws IOException { // find variable "Rad" Variable radiance = nc.findVariable("Rad"); Assert.assertNotNull(radiance); // read full array Array array = radiance.read(); assertThat(array.getRank()).isEqualTo(2); // check shape of array is the same as the shape of the variable int[] variableShape = radiance.getShape(); int[] arrayShape = array.getShape(); assertThat(variableShape).isEqualTo(arrayShape); }
Example 5
Source File: Ncdump.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static void printStringArray(Formatter out, Array ma, Indent indent, CancelTask ct) { if (ct != null && ct.isCancel()) return; int rank = ma.getRank(); Index ima = ma.getIndex(); if (rank == 0) { out.format(" \"%s\"", ma.getObject(ima)); return; } if (rank == 1) { boolean first = true; for (int i = 0; i < ma.getSize(); i++) { if (!first) out.format(", "); out.format(" \"%s\"", ma.getObject(ima.set(i))); first = false; } return; } int[] dims = ma.getShape(); int last = dims[0]; out.format("%n%s{", indent); indent.incr(); for (int ii = 0; ii < last; ii++) { ArrayObject slice = (ArrayObject) ma.slice(0, ii); if (ii > 0) out.format(","); printStringArray(out, slice, indent, ct); } indent.decr(); out.format("%n%s}", indent); }
Example 6
Source File: TestAggExisting.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testStartAndEnd(Variable timeVar, String start, String end) throws IOException { Array vals = timeVar.read(); int[] shape = vals.getShape(); // check start date of aggregation CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar); CalendarDate calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(0)); CalendarDate expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), start); assertThat(calendarDate).isEqualTo(expected); // check end date of aggregation calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(shape[0] - 1)); expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), end); assertThat(calendarDate).isEqualTo(expected); }
Example 7
Source File: TestAggExistingNew.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testStartAndEnd(Variable timeVar, String start, String end) throws IOException { Array vals = timeVar.read(); int[] shape = vals.getShape(); // check start date of aggregation CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar); CalendarDate calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(0)); CalendarDate expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), start); assertThat(calendarDate).isEqualTo(expected); // check end date of aggregation calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(shape[0] - 1)); expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), end); assertThat(calendarDate).isEqualTo(expected); }
Example 8
Source File: TestAggExisting.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testStartAndEnd(Variable timeVar, String start, String end) throws IOException { Array vals = timeVar.read(); int[] shape = vals.getShape(); // check start date of aggregation CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar); CalendarDate calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(0)); CalendarDate expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), start); assertThat(calendarDate).isEqualTo(expected); // check end date of aggregation calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(shape[0] - 1)); expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), end); assertThat(calendarDate).isEqualTo(expected); }
Example 9
Source File: TestAggExistingNew.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testStartAndEnd(Variable timeVar, String start, String end) throws IOException { Array vals = timeVar.read(); int[] shape = vals.getShape(); // check start date of aggregation CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar); CalendarDate calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(0)); CalendarDate expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), start); assertThat(calendarDate).isEqualTo(expected); // check end date of aggregation calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(shape[0] - 1)); expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), end); assertThat(calendarDate).isEqualTo(expected); }
Example 10
Source File: WRFEta.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Get the 3D vertical coordinate array for this time step. * * @param timeIndex the time index. Ignored if !isTimeDependent(). * @return vertical coordinate array * @throws IOException problem reading data */ public ArrayDouble.D3 getCoordinateArray(int timeIndex) throws IOException { ArrayDouble.D3 array; Array pertArray = getTimeSlice(pertVar, timeIndex); Array baseArray = getTimeSlice(baseVar, timeIndex); // ADD: use MAMath? // ADD: use IndexIterator from getIndexIteratorFast? int[] shape = pertArray.getShape(); // ADD: assert that rank = 3 // ADD: assert that both arrays are same shape int ni = shape[0]; int nj = shape[1]; int nk = shape[2]; array = new ArrayDouble.D3(ni, nj, nk); Index index = array.getIndex(); for (int i = 0; i < ni; i++) { for (int j = 0; j < nj; j++) { for (int k = 0; k < nk; k++) { index.set(i, j, k); double d = pertArray.getDouble(index) + baseArray.getDouble(index); if (isZStag) { d = d / 9.81; // convert geopotential to height } array.setDouble(index, d); } } } if (isXStag) { array = addStagger(array, 2); // assuming x dim index is 2 } if (isYStag) { array = addStagger(array, 1); // assuming y dim index is 1 } return array; }
Example 11
Source File: TestBufferedImage.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
public byte[] convert(String srcPath, double a, double b) throws IOException { try (NetcdfFile ncfile = NetcdfFiles.open(srcPath)) { Variable v = ncfile.findVariable("image1/image_data"); Array array = v.read(); int[] cmap = new int[256]; // palette cmap[0] = 0x00FFFFFF; // transparent and white for (int i = 1; i != 256; i++) { // 1 to 255 renders as (almost) white to black cmap[i] = 0xFF000000 | ((0xFF - i) * 0x010101); } IndexColorModel colorModel = new IndexColorModel(8, cmap.length, cmap, 0, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); int[] shape = array.getShape(); BufferedImage bi = new BufferedImage(shape[1], shape[0], BufferedImage.TYPE_BYTE_INDEXED, colorModel); Index index = array.getIndex(); for (int y = 0; y < shape[0]; y++) { for (int x = 0; x < shape[1]; x++) { index.set(y, x); byte bval = array.getByte(index); double dval = v.getDataType().isUnsigned() ? (double) DataType.unsignedByteToShort(bval) : (double) bval; // double dval = array.getDouble(index); // Fix for NetCDF returning all values larger than 127 as (value - 256): // if (dval < -1) { // dval += 256; // } int pval = (int) Math.round(a * dval + b); pval = Math.min(Math.max(pval, 0), 255); bi.getRaster().setSample(x, y, 0, pval); } } ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(bi, "png", os); return os.toByteArray(); } }
Example 12
Source File: TestCoverageHorizSubset.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test @Category(NeedsCdmUnitTest.class) public void testLongitudeSubsetWithHorizontalStride() throws IOException, InvalidRangeException { String filename = TestDir.cdmUnitTestDir + "tds/ncep/GFS_Global_onedeg_20100913_0000.grib2"; String gribId = "VAR_0-3-0_L1"; try (FeatureDatasetCoverage featureDatasetCoverage = CoverageDatasetFactory.open(filename)) { CoverageCollection coverageCollection = featureDatasetCoverage.findCoverageDataset(FeatureType.GRID); Coverage coverage = coverageCollection.findCoverageByAttribute(Grib.VARIABLE_ID_ATTNAME, gribId); final CalendarDate validTime = CalendarDate.parseISOformat(null, "2010-09-21T00:00:00Z"); HorizCoordSys origHcs = coverage.getCoordSys().getHorizCoordSys(); // Next, create the subset param and make the request SubsetParams params = new SubsetParams(); // subset Time axis params.setTime(validTime); // subset across the seam final LatLonRect subsetLatLonRequest = new LatLonRect(LatLonPoint.create(-15, -10), 30, 20); params.setLatLonBoundingBox(subsetLatLonRequest); // set a horizontal stride final int stride = 2; params.setHorizStride(stride); // make subset GeoReferencedArray geo = coverage.readData(params); // Check that TimeAxis is 1D, has one coordinate, and it's equal to the time we requested CoverageCoordAxis timeAxis = geo.getCoordSysForData().getTimeAxis(); assertThat(timeAxis).isInstanceOf(CoverageCoordAxis1D.class); CoverageCoordAxis1D timeAxis1d = (CoverageCoordAxis1D) timeAxis; assertThat(timeAxis1d.getNcoords()).isEqualTo(1); assertThat(timeAxis1d.makeDate((double) timeAxis1d.getCoordObject(0))).isEqualTo(validTime); // make sure the bounding box requested by subset is contained within the // horizontal coordinate system of the GeoReferencedArray produced by the // subset HorizCoordSys subsetHcs = geo.getCoordSysForData().getHorizCoordSys(); assertThat(subsetLatLonRequest.containedIn(subsetHcs.calcLatLonBoundingBox())).isTrue(); // make sure resolution of the lat and lon grids of the subset take into account the stride // by comparing the resolution CoverageCoordAxis1D origLonAxis = origHcs.getXAxis(); CoverageCoordAxis1D origLatAxis = origHcs.getYAxis(); CoverageCoordAxis1D subsetLonAxis = subsetHcs.getXAxis(); CoverageCoordAxis1D subsetLatAxis = subsetHcs.getYAxis(); final double tol = 0.001; assertThat(origLonAxis.getResolution()).isNotWithin(tol).of(subsetLonAxis.getResolution()); assertThat(origLonAxis.getResolution()).isWithin(tol).of(subsetLonAxis.getResolution() / stride); assertThat(origLatAxis.getResolution()).isNotWithin(tol).of(subsetLatAxis.getResolution()); assertThat(origLatAxis.getResolution()).isWithin(tol).of(subsetLatAxis.getResolution() / stride); // check to make sure we get data from both sides of the seam by testing that // half of the array isn't empty. // slice along longitude in the middle of the array. Array geoData = geo.getData(); int middle = geoData.getShape()[1] / 2; Array data = geo.getData().slice(2, middle).reduce(); // flip the array int numValsToSum = 3; Array dataFlip = data.flip(0); Section sec = Section.builder().appendRange(0, numValsToSum).build(); IndexIterator dii = data.getIndexIterator(); IndexIterator diiFlip = dataFlip.getIndexIterator(); final double initialSumVal = 0; double sumData = initialSumVal; double sumDataFlip = initialSumVal; for (int i = 0; i < numValsToSum - 1; i++) { double val = dii.getDoubleNext(); double valFlip = diiFlip.getDoubleNext(); // only sum if not missing if (!geo.isMissing(val)) sumData += val; if (!geo.isMissing(valFlip)) sumDataFlip += valFlip; } assertThat(sumData).isNotEqualTo(initialSumVal); assertThat(sumDataFlip).isNotEqualTo(initialSumVal); } }
Example 13
Source File: NetCDFReadTask.java From mzmine3 with GNU General Public License v2.0 | 4 votes |
/** * Reads one scan from the file. Requires that general information has already been read. */ private Scan readNextScan() throws IOException { // Get scan starting position and length int[] scanStartPosition = new int[1]; int[] scanLength = new int[1]; Integer[] startAndLength = scansIndex.get(scanNum); // End of file if (startAndLength == null) { return null; } scanStartPosition[0] = startAndLength[0]; scanLength[0] = startAndLength[1]; // Get retention time of the scan Double retentionTime = scansRetentionTimes.get(scanNum); if (retentionTime == null) { logger.severe("Could not find retention time for scan " + scanNum); throw (new IOException("Could not find retention time for scan " + scanNum)); } // An empty scan needs special attention.. if (scanLength[0] == 0) { scanNum++; return new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0.0, 0, 0, null, new DataPoint[0], MassSpectrumType.CENTROIDED, PolarityType.UNKNOWN, "", null); } // Is there any way how to extract polarity from netcdf? PolarityType polarity = PolarityType.UNKNOWN; // Is there any way how to extract scan definition from netcdf? String scanDefinition = ""; // Read mass and intensity values Array massValueArray; Array intensityValueArray; try { massValueArray = massValueVariable.read(scanStartPosition, scanLength); intensityValueArray = intensityValueVariable.read(scanStartPosition, scanLength); } catch (Exception e) { logger.log(Level.SEVERE, "Could not read from variables mass_values and/or intensity_values.", e); throw (new IOException("Could not read from variables mass_values and/or intensity_values.")); } Index massValuesIndex = massValueArray.getIndex(); Index intensityValuesIndex = intensityValueArray.getIndex(); int arrayLength = massValueArray.getShape()[0]; DataPoint dataPoints[] = new DataPoint[arrayLength]; for (int j = 0; j < arrayLength; j++) { Index massIndex0 = massValuesIndex.set0(j); Index intensityIndex0 = intensityValuesIndex.set0(j); double mz = massValueArray.getDouble(massIndex0) * massValueScaleFactor; double intensity = intensityValueArray.getDouble(intensityIndex0) * intensityValueScaleFactor; dataPoints[j] = new SimpleDataPoint(mz, intensity); } scanNum++; // Auto-detect whether this scan is centroided MassSpectrumType spectrumType = ScanUtils.detectSpectrumType(dataPoints); SimpleScan buildingScan = new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0.0, 0,0,null,dataPoints, spectrumType, polarity, scanDefinition, null); return buildingScan; }
Example 14
Source File: NetCDFReadTask.java From mzmine2 with GNU General Public License v2.0 | 4 votes |
/** * Reads one scan from the file. Requires that general information has already been read. */ private Scan readNextScan() throws IOException { // Get scan starting position and length int[] scanStartPosition = new int[1]; int[] scanLength = new int[1]; Integer[] startAndLength = scansIndex.get(scanNum); // End of file if (startAndLength == null) { return null; } scanStartPosition[0] = startAndLength[0]; scanLength[0] = startAndLength[1]; // Get retention time of the scan Double retentionTime = scansRetentionTimes.get(scanNum); if (retentionTime == null) { logger.severe("Could not find retention time for scan " + scanNum); throw (new IOException("Could not find retention time for scan " + scanNum)); } // An empty scan needs special attention.. if (scanLength[0] == 0) { scanNum++; return new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0, 0, null, new DataPoint[0], MassSpectrumType.CENTROIDED, PolarityType.UNKNOWN, "", null); } // Is there any way how to extract polarity from netcdf? PolarityType polarity = PolarityType.UNKNOWN; // Is there any way how to extract scan definition from netcdf? String scanDefinition = ""; // Read mass and intensity values Array massValueArray; Array intensityValueArray; try { massValueArray = massValueVariable.read(scanStartPosition, scanLength); intensityValueArray = intensityValueVariable.read(scanStartPosition, scanLength); } catch (Exception e) { logger.log(Level.SEVERE, "Could not read from variables mass_values and/or intensity_values.", e); throw (new IOException("Could not read from variables mass_values and/or intensity_values.")); } Index massValuesIndex = massValueArray.getIndex(); Index intensityValuesIndex = intensityValueArray.getIndex(); int arrayLength = massValueArray.getShape()[0]; DataPoint dataPoints[] = new DataPoint[arrayLength]; for (int j = 0; j < arrayLength; j++) { Index massIndex0 = massValuesIndex.set0(j); Index intensityIndex0 = intensityValuesIndex.set0(j); double mz = massValueArray.getDouble(massIndex0) * massValueScaleFactor; double intensity = intensityValueArray.getDouble(intensityIndex0) * intensityValueScaleFactor; dataPoints[j] = new SimpleDataPoint(mz, intensity); } scanNum++; // Auto-detect whether this scan is centroided MassSpectrumType spectrumType = ScanUtils.detectSpectrumType(dataPoints); SimpleScan buildingScan = new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0, 0, null, dataPoints, spectrumType, polarity, scanDefinition, null); return buildingScan; }