Java Code Examples for ucar.nc2.Variable#read()

The following examples show how to use ucar.nc2.Variable#read() . 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: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 2
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 3
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 4
Source File: AggregationOuter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array read(AggDatasetOuter dset, NetcdfFile ncfile) throws IOException {
  invocation++;

  Array data = getData(dset.getId());
  if (data != null)
    return data;
  if (type == Type.joinNew)
    return null;

  Variable v = ncfile.findVariable(varName);
  data = v.read();
  putData(dset.getId(), data);
  if (debugCache)
    System.out.println("caching " + varName + " complete data");
  return data;
}
 
Example 5
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isStationary() {
  // only check once
  if (!isStationaryChecked) {
    Variable lat = ds.findVariable("latitude");
    if (lat != null) {
      if (lat.isScalar())
        isStationary = lat.getSize() == 1;
      else {
        // if array, check to see if all of the values are
        // approximately the same
        Array gar;
        try {
          gar = lat.read();
          Object firstVal = gar.getObject(0);
          Array gar2 = gar.copy();
          for (int i = 1; i < gar.getSize(); i++) {
            gar2.setObject(i, firstVal);
          }
          isStationary = nearlyEquals(gar, gar2);
        } catch (IOException e) {
          log.error("Error reading latitude variable {}. Cannot determine if "
              + "platform is stationary. Setting to default (false).", lat.getFullName());
        }
      }
    }
    isStationaryChecked = true;
  }

  return isStationary;
}
 
Example 6
Source File: TestH5OddTypes.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@Category(NeedsCdmUnitTest.class)
public void testCompoundEnum() throws IOException {
  H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header"));
  try (NetcdfFile ncfile = TestH5.openH5("support/cenum.h5")) {
    Variable v = ncfile.findVariable("enum");
    Array data = v.read();
    logger.debug("enum data = {}", Ncdump.printArray(data));
    System.out.println("\n**** testReadNetcdf4 done\n\n" + ncfile);
  }
  H5header.setDebugFlags(new DebugFlagsImpl(""));
}
 
Example 7
Source File: NidsRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public float getTime(int ray) throws IOException {
  Variable sp = ds.findVariable("rays_time");
  Array timeData = sp.read();
  sp.setCachedData(timeData, false);
  Index index = timeData.getIndex();
  return timeData.getFloat(index.set(ray));
}
 
Example 8
Source File: LCMSNetCDFParser.java    From act with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Print information about a specific Variable from a NetCDF file.  Used for debugging.
 * @param name A human-readable name for this variable.
 * @param v The variable whose details to print.
 * @throws IOException
 */
public static void printVariableDetails(String name, Variable v) throws IOException {
  System.out.format("%s name and dimensions: %s\n", name, v.getNameAndDimensions());
  Array a = v.read();
  System.out.format("  rank: %d\n", a.getRank());
  System.out.format("  shape size: %d\n", a.getShape().length);
  System.out.format("  shape: %s\n", a.shapeToString());

  System.out.format("  array data type: %s\n", a.getDataType());
}
 
Example 9
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Builder encodeVar(Variable var, int sizeToCache) throws IOException {
  Builder builder = NcStreamProto.Variable.newBuilder();
  builder.setName(var.getShortName());
  builder.setDataType(convertDataType(var.getDataType()));
  if (var.getDataType().isEnum()) {
    EnumTypedef enumType = var.getEnumTypedef();
    if (enumType != null)
      builder.setEnumType(enumType.getShortName());
  }

  for (Dimension dim : var.getDimensions()) {
    builder.addShape(encodeDim(dim));
  }

  for (Attribute att : var.attributes()) {
    builder.addAtts(encodeAtt(att));
  }

  // put small amounts of data in header "immediate mode"
  if (var.isCaching() && var.getDataType().isNumeric()) {
    if (var.isCoordinateVariable() || var.getSize() * var.getElementSize() < sizeToCache) {
      Array data = var.read();
      ByteBuffer bb = data.getDataAsByteBuffer();
      builder.setData(ByteString.copyFrom(bb.array()));
    }
  }

  return builder;
}
 
Example 10
Source File: TestCurvilinearGridPointMapping.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test CoverageCoordSys.HorizCoordSys.getLatLon()
 *
 * @throws IOException if ...
 * @throws InvalidRangeException if ...
 */
@Test
public void checkGridCoordSystem_getLatLon() throws IOException, InvalidRangeException {
  int[] origin = new int[] {j, i};
  int[] shape = new int[] {1, 1};

  NetcdfFile ncf = NetcdfFiles.open(datasetLocation);
  Variable latVar = ncf.findVariable("lat");
  Array latArray = latVar.read(origin, shape);
  Variable lonVar = ncf.findVariable("lon");
  Array lonArray = lonVar.read(origin, shape);

  double latVal = latArray.getDouble(0);
  double lonVal = lonArray.getDouble(0);
  logger.debug("{}, {}", latVal, lonVal);

  try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(datasetLocation)) {
    Assert.assertNotNull(datasetLocation, cc);
    CoverageCollection gcs = cc.findCoverageDataset(FeatureType.CURVILINEAR);
    Assert.assertNotNull("gcs", gcs);
    Coverage cover = gcs.findCoverage(covName);
    Assert.assertNotNull(covName, cover);

    CoverageCoordSys gridCoordSys = cover.getCoordSys();
    Assert.assertNotNull("CoverageCoordSys", gridCoordSys);
    HorizCoordSys hcs = gridCoordSys.getHorizCoordSys();
    Assert.assertNotNull("HorizCoordSys", hcs);

    LatLonPoint llPnt = hcs.getLatLon(j, i);
    Assert2.assertNearlyEquals(lat, llPnt.getLatitude());
    Assert2.assertNearlyEquals(lon, llPnt.getLongitude());
  }
}
 
Example 11
Source File: NidsRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public float[] readAllData() throws IOException {
  Array allData;
  Sweep spn = sweeps.get(0);
  Variable v = spn.getsweepVar();
  try {
    allData = v.read();
  } catch (IOException e) {
    throw new IOException(e.getMessage());
  }
  return (float[]) allData.get1DJavaArray(float.class);
}
 
Example 12
Source File: TestGrib1Unpack.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEcmwfExtendedComplexData2() throws IOException {
  final String testfile = "../grib/src/test/data/complex_packing2.grib1";
  try (NetcdfFile nc = NetcdfFiles.open(testfile)) {

    Variable var = nc.findVariable("Snowfall_surface");
    Array data = var.read();

    float first = data.getFloat(0);

    Assert.assertEquals(.326607, first, 1e-6);
  }
}
 
Example 13
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example 14
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example 15
Source File: TestAggExistingNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example 16
Source File: TestS3Read.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 17
Source File: TestH5.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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 18
Source File: TestSinglePointGds.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void checkVal(Variable variable, double expectedValue, double tol) throws IOException {
  Array array = variable.read();
  assertThat(array.getSize()).isEqualTo(1);
  double val = array.getDouble(0);
  assertThat(val).isWithin(tol).of(expectedValue);
}
 
Example 19
Source File: TestGini.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testGiniRead() throws IOException {
  try (NetcdfFile ncfile = NetcdfFiles.open(TestDir.cdmUnitTestDir + "formats/gini/" + fname)) {
    Variable v = ncfile.findVariable(varName);

    // Make sure we can get the expected variable and that it is at-least 2D
    Assert.assertNotNull(v);
    Assert.assertNotNull(v.getDimension(0));
    Assert.assertNotNull(v.getDimension(1));

    // Make sure the variable has a grid mapping set and that it points
    // to a valid variable
    Attribute grid_mapping = v.findAttribute("grid_mapping");
    Assert.assertNotNull(grid_mapping);
    Variable proj_var = ncfile.findVariable(grid_mapping.getStringValue());
    Assert.assertNotNull(proj_var);
    Assert.assertNotNull(proj_var.findAttribute("grid_mapping_name"));

    // Check size
    Assert.assertEquals(ySize, v.getDimension(v.findDimensionIndex("y")).getLength());
    Assert.assertEquals(xSize, v.getDimension(v.findDimensionIndex("x")).getLength());

    // Check projection info
    Assert.assertEquals(min_lon, ncfile.findAttribute("@geospatial_lon_min").getNumericValue().doubleValue(), 1e-6);
    Assert.assertEquals(max_lon, ncfile.findAttribute("@geospatial_lon_max").getNumericValue().doubleValue(), 1e-6);
    Assert.assertEquals(min_lat, ncfile.findAttribute("@geospatial_lat_min").getNumericValue().doubleValue(), 1e-6);
    Assert.assertEquals(max_lat, ncfile.findAttribute("@geospatial_lat_max").getNumericValue().doubleValue(), 1e-6);

    // Read the array and check that its size matches the variable's
    Array a = v.read();
    Assert.assertNotNull(a);
    Assert.assertEquals(v.getSize(), a.getSize());

    // For byte data, make sure it is specified as unsigned and
    // check that the actual number of bytes is proper
    if (v.getDataType() == DataType.BYTE) {
      byte[] arr = (byte[]) a.getStorage();
      Assert.assertEquals(v.getSize(), arr.length);
      Assert.assertNotNull(v.findAttribute(CDM.UNSIGNED));
    }
  }
}
 
Example 20
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies data from {@code oldVar} to {@code newVar}. The writes are done in a series of chunks no larger than
 * {@code maxChunkSize}
 * bytes.
 *
 * @param oldVar a variable from the original file to copy data from.
 * @param newVar a variable from the original file to copy data from.
 * @param maxChunkSize the size, <b>in bytes</b>, of the largest chunk to write.
 * @param cancel allow user to cancel, may be null.
 * @throws IOException if an I/O error occurs.
 */
private void copySome(NetcdfFormatWriter ncwriter, Variable oldVar, Variable newVar, long maxChunkSize,
    CancelTask cancel) throws IOException {
  long maxChunkElems = maxChunkSize / oldVar.getElementSize();
  long byteWriteTotal = 0;

  ChunkingIndex index = new ChunkingIndex(oldVar.getShape());
  while (index.currentElement() < index.getSize()) {
    try {
      int[] chunkOrigin = index.getCurrentCounter();
      int[] chunkShape = index.computeChunkShape(maxChunkElems);

      cancel.setProgress(
          "Reading chunk " + new Section(chunkOrigin, chunkShape) + " from variable: " + oldVar.getShortName(), -1);

      Array data = oldVar.read(chunkOrigin, chunkShape);
      if (!getOutputFormat().isNetdf4format() && oldVar.getDataType() == DataType.STRING) {
        data = convertDataToChar(newVar, data);
      }

      if (data.getSize() > 0) { // zero when record dimension = 0
        cancel.setProgress(
            "Writing chunk " + new Section(chunkOrigin, chunkShape) + " from variable: " + oldVar.getShortName(), -1);

        ncwriter.write(newVar, chunkOrigin, data);
        if (debugWrite) {
          System.out.println(" write " + data.getSize() + " bytes at " + new Section(chunkOrigin, chunkShape));
        }

        byteWriteTotal += data.getSize();
      }

      index.setCurrentCounter(index.currentElement() + (int) Index.computeSize(chunkShape));
      if (cancel.isCancel()) {
        return;
      }

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IOException(e.getMessage());
    }
  }
}