Java Code Examples for ucar.ma2.Array#hasNext()

The following examples show how to use ucar.ma2.Array#hasNext() . 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: TestSequence.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testSequence() {
  String url = "http://tsds.net/tsds/test/Scalar";
  try {
    NetcdfDataset ds = NetcdfDatasets.openDataset(url);
    System.out.println(ds);
    Structure struct = (Structure) ds.findVariable("TimeSeries");
    Variable var = struct.findVariable("time");
    Array arr = var.read();
    int n = (int) arr.getSize();
    int i;
    for (i = 0; arr.hasNext() && i < n; i++)
      System.out.println(arr.nextDouble());
    if (i != n) {
      System.err.println("short sequence");
      System.exit(1);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 2
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array convertEnums(Map<Integer, String> map, DataType dataType, Array values) {
  Array result = Array.factory(DataType.STRING, values.getShape());
  IndexIterator ii = result.getIndexIterator();
  values.resetLocalIterator();
  while (values.hasNext()) {
    int ival;
    if (dataType == DataType.ENUM1)
      ival = (int) DataType.unsignedByteToShort(values.nextByte());
    else if (dataType == DataType.ENUM2)
      ival = DataType.unsignedShortToInt(values.nextShort());
    else
      ival = values.nextInt();
    String sval = map.get(ival);
    if (sval == null)
      sval = "Unknown enum value=" + ival;
    ii.setObjectNext(sval);
  }
  return result;
}
 
Example 3
Source File: Table.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void init() {
  if (startVarName == null) { // read numRecords when startVar is not known LOOK this should be deffered
    try {
      Variable v = ds.findVariable(numRecordsVarName);
      Array numRecords = v.read();
      int n = (int) numRecords.getSize();

      // construct the start variable
      this.numRecords = new int[n];
      this.startIndex = new int[n];
      int i = 0;
      int count = 0;
      while (numRecords.hasNext()) {
        this.startIndex[i] = count;
        this.numRecords[i] = numRecords.nextInt();
        count += this.numRecords[i];
        i++;
      }
      isInit = true;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
 
Example 4
Source File: Table.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
TableParentIndex(NetcdfDataset ds, TableConfig config) {
  super(ds, config);
  this.parentIndexName = config.parentIndex;

  // construct the map
  try {
    Variable rpIndex = ds.findVariable(config.parentIndex);
    Array index = rpIndex.read();

    int childIndex = 0;
    this.indexMap = new HashMap<>((int) (2 * index.getSize()));
    while (index.hasNext()) {
      int parent = index.nextInt();
      List<Integer> list = indexMap.computeIfAbsent(parent, k -> new ArrayList<>());
      list.add(childIndex);
      childIndex++;
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  addNonDataVariable(config.parentIndex);
}
 
Example 5
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUnsigned() throws IOException {
  try (NetcdfFile ncfile = NetcdfDatasets.openDataset(TestDir.cdmLocalTestDataDir + "testUnsignedByte.ncml")) {
    Variable v = ncfile.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.FLOAT, v.getDataType()); // has float scale_factor

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 6
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUnsignedWrap() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='byte'>\n"
      + "    <attribute name='_Unsigned' value='true' />\n"
      + "    <attribute name='scale_factor' type='float' value='2.0' />\n" + "   </variable>\n" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.FLOAT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 7
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testVarWithUnsignedAttribute() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='byte'>\n"
      + "    <attribute name='_Unsigned' value='true' />\n" + "   </variable>\n" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.USHORT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 8
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testVarWithUnsignedType() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='ubyte'/>"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.USHORT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 9
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAttWithUnsignedType() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <attribute name='gatt' type='ubyte'>1 0 -1</attribute>"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 10
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAttWithUnsignedAtt() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n"
      + "  <attribute name='gatt' type='byte' isUnsigned='true'>1 0 -1</attribute>" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 11
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAttWithUnsignedType2() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <attribute name='gatt' type='ubyte' value='1 0 -1' />"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 12
Source File: Hdf5DataTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void showVlen(Formatter f, VarBean bean) {
  if (!bean.v.isVariableLength()) {
    f.format("Variable %s must be variable length", bean.v.getFullName());
    return;
  }

  try {
    int countRows = 0;
    long countElems = 0;
    Array result = bean.v.read();
    f.format("class = %s%n", result.getClass().getName());
    while (result.hasNext()) {
      Array line = (Array) result.next();
      countRows++;
      long size = line.getSize();
      countElems += size;
      f.format("  row %d size=%d%n", countRows, size);
    }
    float avg = (countRows == 0) ? 0 : ((float) countElems) / countRows;
    f.format("%n  nrows = %d totalElems=%d avg=%f%n", countRows, countElems, avg);
  } catch (IOException e) {
    e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates.
  }
}
 
Example 13
Source File: Hdf5NewDataTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void showVlen(Formatter f, VarBean bean) {
  if (!bean.v.isVariableLength()) {
    f.format("Variable %s must be variable length", bean.v.getFullName());
    return;
  }

  try {
    int countRows = 0;
    long countElems = 0;
    Array result = bean.v.read();
    f.format("class = %s%n", result.getClass().getName());
    while (result.hasNext()) {
      Array line = (Array) result.next();
      countRows++;
      long size = line.getSize();
      countElems += size;
      f.format("  row %d size=%d%n", countRows, size);
    }
    float avg = (countRows == 0) ? 0 : ((float) countElems) / countRows;
    f.format("%n  nrows = %d totalElems=%d avg=%f%n", countRows, countElems, avg);
  } catch (IOException e) {
    e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates.
  }
}
 
Example 14
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Print array as undifferentiated sequence of values.
 *
 * @param ma any Array except ArrayStructure
 */
public static String printArrayPlain(Array ma) {
  StringWriter sw = new StringWriter();
  PrintWriter pw = new PrintWriter(sw);
  ma.resetLocalIterator();
  while (ma.hasNext()) {
    pw.print(ma.next());
    pw.print(' ');
  }
  return sw.toString();
}
 
Example 15
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void printArray(Formatter out, Array array, String name, String units, Indent ilev, CancelTask ct,
    boolean printSeq) {
  if (ct != null && ct.isCancel())
    return;

  if (name != null)
    out.format("%s%s = ", ilev, name);
  ilev.incr();

  if (array == null) {
    out.format("null array for %s", name);
    ilev.decr();
    return;
  }

  if ((array instanceof ArrayChar) && (array.getRank() > 0)) {
    printStringArray(out, (ArrayChar) array, ilev, ct);

  } else if (array.getElementType() == String.class) {
    printStringArray(out, array, ilev, ct);

  } else if (array instanceof ArraySequence) {
    if (printSeq)
      printSequence(out, (ArraySequence) array, ilev, ct);

  } else if (array instanceof ArrayStructure) {
    printStructureDataArray(out, (ArrayStructure) array, ilev, ct);

  } else if (array.getElementType() == ByteBuffer.class) { // opaque type
    array.resetLocalIterator();
    while (array.hasNext()) {
      printByteBuffer(out, (ByteBuffer) array.next(), ilev);
      out.format("%s%n", array.hasNext() ? "," : ";"); // peek ahead
      if (ct != null && ct.isCancel())
        return;
    }
  } else if (array instanceof ArrayObject) {
    printVariableArray(out, (ArrayObject) array, ilev, ct);
  } else {
    printArray(out, array, ilev, ct);
  }

  if (units != null)
    out.format(" %s", units);
  out.format("%n");
  ilev.decr();
  out.flush();
}