Java Code Examples for ucar.ma2.Array#getIndex()
The following examples show how to use
ucar.ma2.Array#getIndex() .
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: TestGridSubset.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test @Ignore("Does this file exist in a shared location?") public void testAaron() throws Exception { // different scale/offset in aggregation try (GridDataset dataset = GridDataset.open("G:/work/braekel/dataset.ncml")) { GridDatatype grid = null; for (GridDatatype thisGrid : dataset.getGrids()) { if (thisGrid.getName().equals("cref")) { grid = thisGrid; } } List<Range> ranges = new ArrayList<Range>(); ranges.add(new Range(0, 0)); ranges.add(new Range(0, 0)); ranges.add(new Range(638, 638)); ranges.add(new Range(3750, 4622)); Array arr = grid.getVariable().read(ranges); Index index = arr.getIndex(); index.set(new int[] {0, 0, 0, 834}); logger.debug("index {} value {}", index.currentElement(), arr.getDouble(index)); } }
Example 3
Source File: ADASConvention.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void makeCoordAxis(String axisName) throws IOException { String name = axisName + "_stag"; if (!rootGroup.findVariableLocal(name).isPresent()) { return; } VariableDS.Builder stagV = (VariableDS.Builder) rootGroup.findVariableLocal(name).get(); Array data_stag = stagV.orgVar.read(); int n = (int) data_stag.getSize() - 1; DataType dt = DataType.getType(data_stag); Array data = Array.factory(dt, 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); String units = stagV.getAttributeContainer().findAttributeString(CDM.UNITS, "m"); CoordinateAxis.Builder cb = CoordinateAxis1D.builder().setName(axisName).setDataType(dtype) .setParentGroupBuilder(rootGroup).setDimensionsByName(axisName).setUnits(units) .setDesc("synthesized non-staggered " + axisName + " coordinate"); cb.setCachedData(data, true); datasetBuilder.replaceCoordinateAxis(rootGroup, cb); }
Example 4
Source File: Attribute.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Create a scalar numeric-valued Attribute, possibly unsigned. * * @param name name of Attribute * @param val value of Attribute * @param isUnsigned if value is unsigned, used only for integer types. */ public Attribute(String name, Number val, boolean isUnsigned) { super(name); if (name == null) throw new IllegalArgumentException("Trying to set name to null on " + this); int[] shape = new int[1]; shape[0] = 1; DataType dt = DataType.getType(val.getClass(), isUnsigned); this.dataType = dt; Array vala = Array.factory(dt, shape); Index ima = vala.getIndex(); vala.setObject(ima.set0(0), val); setValues(vala); // make private setImmutable(); }
Example 5
Source File: AtmosSigma.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Get the 1D vertical coordinate array for this time step and point * * (needds test!!!) * * @param timeIndex the time index. Ignored if !isTimeDependent(). * @param xIndex the x index * @param yIndex the y index * @return vertical coordinate array * @throws java.io.IOException problem reading data * @throws ucar.ma2.InvalidRangeException _more_ */ public D1 getCoordinateArray1D(int timeIndex, int xIndex, int yIndex) throws IOException, InvalidRangeException { Array ps = readArray(psVar, timeIndex); Index psIndex = ps.getIndex(); int nz = sigma.length; ArrayDouble.D1 result = new ArrayDouble.D1(nz); double psVal = ps.getDouble(psIndex.set(yIndex, xIndex)); for (int z = 0; z < nz; z++) { result.set(z, ptop + sigma[z] * (psVal - ptop)); } return result; }
Example 6
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 7
Source File: Attribute.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public Builder setNumericValue(Number val, boolean isUnsigned) { int[] shape = {1}; DataType dt = DataType.getType(val.getClass(), isUnsigned); setDataType(dt); Array vala = Array.factory(dt, shape); Index ima = vala.getIndex(); vala.setObject(ima.set0(0), val); setValues(vala); return this; }
Example 8
Source File: H5iospNew.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
Array convertReference(Array refArray) throws IOException { int nelems = (int) refArray.getSize(); Index ima = refArray.getIndex(); String[] result = new String[nelems]; for (int i = 0; i < nelems; i++) { long reference = refArray.getLong(ima.set(i)); String name = header.getDataObjectName(reference); result[i] = name != null ? name : Long.toString(reference); if (debugVlen) System.out.printf(" convertReference 0x%x to %s %n", reference, result[i]); } return Array.factory(DataType.STRING, new int[] {nelems}, result); }
Example 9
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 10
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 11
Source File: TestCoverageSubsetTime.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test // 1 runtime, 1 time (Time2DCoordSys case 1b) public void test1Runtime1TimeIntervalEdge() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "gribCollections/gfs_2p5deg/gfs_2p5deg.ncx4"; String covName = "Momentum_flux_u-component_surface_Mixed_intervals_Average"; logger.debug("test1Runtime1TimeInterval Dataset {} coverage {}", endpoint, covName); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { Assert.assertNotNull(endpoint, cc); CoverageCollection gcs = cc.findCoverageDataset(FeatureType.FMRC); Assert.assertNotNull("gcs", gcs); Coverage cover = gcs.findCoverage(covName); Assert.assertNotNull(covName, cover); SubsetParams params = new SubsetParams(); CalendarDate runtime = CalendarDate.parseISOformat(null, "2015-03-01T00:00:00Z"); params.set(SubsetParams.runtime, runtime); CalendarDate time = CalendarDate.parseISOformat(null, "2015-03-06T19:30:00Z"); // (6,12), (12,18) params.set(SubsetParams.time, time); logger.debug(" subset {}", params); GeoReferencedArray geo = cover.readData(params); testGeoArray(geo, runtime, time, null); Array data = geo.getData(); Index ai = data.getIndex(); float testValue = data.getFloat(ai.set(0, 0, 3, 0)); Assert2.assertNearlyEquals(0.244f, testValue); } }
Example 12
Source File: TestCoverageSubsetTime.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test // 1 runtime, 1 time (Time2DCoordSys case 1b) public void test1Runtime1TimeInterval() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "gribCollections/gfs_2p5deg/gfs_2p5deg.ncx4"; String covName = "Momentum_flux_u-component_surface_Mixed_intervals_Average"; logger.debug("test1Runtime1TimeInterval Dataset {} coverage {}", endpoint, covName); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { Assert.assertNotNull(endpoint, cc); CoverageCollection gcs = cc.findCoverageDataset(FeatureType.FMRC); Assert.assertNotNull("gcs", gcs); Coverage cover = gcs.findCoverage(covName); Assert.assertNotNull(covName, cover); SubsetParams params = new SubsetParams(); CalendarDate runtime = CalendarDate.parseISOformat(null, "2015-03-01T06:00:00Z"); params.set(SubsetParams.runtime, runtime); CalendarDate time = CalendarDate.parseISOformat(null, "2015-03-01T11:00:00Z"); // (6,12), (12,18) params.set(SubsetParams.time, time); logger.debug(" subset {}", params); GeoReferencedArray geo = cover.readData(params); testGeoArray(geo, runtime, time, null); Array data = geo.getData(); Index ai = data.getIndex(); float testValue = data.getFloat(ai.set(0, 0, 2, 2)); Assert2.assertNearlyEquals(0.073f, testValue); } }
Example 13
Source File: TestCoverageSubsetTime.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test // 1 runtime, 1 time (Time2DCoordSys case 1b) public void test1Runtime1Time() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "gribCollections/gfs_2p5deg/gfs_2p5deg.ncx4"; String covName = "Total_ozone_entire_atmosphere_single_layer"; logger.debug("test1Runtime1Time Dataset {} coverage {}", endpoint, covName); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { Assert.assertNotNull(endpoint, cc); CoverageCollection gcs = cc.findCoverageDataset(FeatureType.FMRC); Assert.assertNotNull("gcs", gcs); Coverage cover = gcs.findCoverage(covName); Assert.assertNotNull(covName, cover); SubsetParams params = new SubsetParams(); CalendarDate runtime = CalendarDate.parseISOformat(null, "2015-03-01T06:00:00Z"); params.set(SubsetParams.runtime, runtime); CalendarDate time = CalendarDate.parseISOformat(null, "2015-03-01T12:00:00Z"); params.set(SubsetParams.time, time); logger.debug(" subset {}", params); GeoReferencedArray geo = cover.readData(params); testGeoArray(geo, runtime, time, null); Array data = geo.getData(); Index ai = data.getIndex(); float testValue = data.getFloat(ai.set(0, 0, 1, 0)); Assert2.assertNearlyEquals(371.5, testValue); } }
Example 14
Source File: TestH5.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@org.junit.Test public void testSuperblockIsOffset() throws IOException { try (NetcdfFile ncfile = TestH5.openH5("superblockIsOffsetNPP.h5")) { Variable v = ncfile.findVariable("BeamTime"); System.out.printf("%s%n", v); Array data = v.read(); logger.debug("{}", Ncdump.printArray(data, "offset data", null)); Index ii = data.getIndex(); assert (data.getLong(ii.set(11, 93)) == 1718796166693743L); } }
Example 15
Source File: TestH5.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@org.junit.Test public void testOffsetCompactLayout() throws IOException { try (NetcdfFile ncfile = TestH5.openH5("matlab_cols.mat")) { Variable v = ncfile.findVariable("b"); System.out.printf("%s%n", v); Array data = v.read(); logger.debug("{}", Ncdump.printArray(data, "offset data", null)); Index ii = data.getIndex(); assert (data.getDouble(ii.set(3, 2)) == 12.0); } }
Example 16
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 17
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 18
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; }