/* Structure {
      int LAT[0];
      ...
      int LAT[149];
  } IMAGE_LAT_ARRAY(3600);
     type = Layout(8);  type= 2 (chunked) storageSize = (1,600) dataSize=0 dataAddress=2548046
  */
  @Test
  public void testReadOneAtATime() throws java.io.IOException, InvalidRangeException {
    try (NetcdfFile ncfile = TestH5.openH5("IASI/IASI.h5")) {

      Variable dset = ncfile.findVariable("U-MARF/EPS/IASI_xxx_1C/DATA/IMAGE_LAT_ARRAY");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 3600);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 3600);

      Structure s = (Structure) dset;

      // read last one - chunked
      StructureData sd = s.readStructure(3599);
      assert sd.getScalarInt("LAT[0]") == 70862722;
      assert sd.getScalarInt("LAT[149]") == 85302263;

      // read one at a time
      for (int i = 3590; i < d.getLength(); i++) {
        s.readStructure(i);
        System.out.println(" read structure " + i);
      }
    }
    System.out.println("*** testReadIASI ok");
  }
  /*
   Structure {
    int a_name;
    byte b_name(3);
    byte c_name(3);
    short d_name(3);
    int e_name(3);
    long f_name(3);
    int g_name(3);
    short h_name(3);
    int i_name(3);
    long j_name(3);
    float k_name(3);
    double l_name(3);
  } CompoundNative(15);
      type = Layout(8);  type= 1 (contiguous) storageSize = (15,144) dataSize=0 dataAddress=2048
  */
  @Test
  public void testReadH5StructureArrayMembers() throws java.io.IOException {
    try (NetcdfFile ncfile = TestH5.openH5("complex/compound_native.h5")) {

      Variable dset = ncfile.findVariable("CompoundNative");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 15);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 15);

      Structure s = (Structure) dset;

      // read all with the iterator
      StructureDataIterator iter = s.getStructureIterator();
      while (iter.hasNext()) {
        StructureData sd = iter.next();

        for (StructureMembers.Member m : sd.getMembers()) {
          Array data = sd.getArray(m);
          NCdumpW.printArray(data, m.getName(), out, null);
        }
      }
    }
    System.out.println("*** testReadH5StructureArrayMembers ok");
  }
  /*
      Structure {
      int a_name;
      String b_name(4);
      char c_name(6);
      short d_name(5, 4);
      float e_name;
      double f_name(10);
      byte g_name;
    } CompoundComplex(6);
     type = Layout(8);  type= 1 (contiguous) storageSize = (6,224) dataSize=0 dataAddress=2048
  */
  @Test
  public void testReadH5Structure() throws java.io.IOException {
    int a_name = 0;
    String[] b_name =
        new String[] {
          "A fight is a contract that takes two people to honor.",
          "A combative stance means that you've accepted the contract.",
          "In which case, you deserve what you get.",
          "  --  Professor Cheng Man-ch'ing"
        };
    String c_name = "Hello!";

    // H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header"));
    try (NetcdfFile ncfile = TestH5.openH5("complex/compound_complex.h5")) {

      Variable dset = ncfile.findVariable("CompoundComplex");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 6);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 6);

      Structure s = (Structure) dset;

      // read all with the iterator
      StructureDataIterator iter = s.getStructureIterator();
      while (iter.hasNext()) {
        StructureData sd = iter.next();
        assert sd.getScalarInt("a_name") == a_name;
        a_name++;
        assert sd.getScalarString("c_name").equals(c_name);
        String[] results = sd.getJavaArrayString(sd.findMember("b_name"));
        assert results.length == b_name.length;
        int count = 0;
        for (String r : results) assert r.equals(b_name[count++]);

        for (StructureMembers.Member m : sd.getMembers()) {
          Array data = sd.getArray(m);
          NCdumpW.printArray(data, m.getName(), out, null);
        }
      }
    }
    System.out.println("*** testReadH5Structure ok");
  }
Esempio n. 4
0
  private void compareDataset(CompareDialog.Data data) {
    if (data.name == null) return;

    NetcdfFile compareFile = null;
    try {
      compareFile = NetcdfDataset.openFile(data.name, null);

      Formatter f = new Formatter();
      CompareNetcdf2 cn = new CompareNetcdf2(f, data.showCompare, data.showDetails, data.readData);
      if (data.howMuch == CompareDialog.HowMuch.All) cn.compare(ds, compareFile);
      else {
        NestedTable nested = nestedTableList.get(0);
        Variable org = getCurrentVariable(nested.table);
        if (org == null) return;
        Variable ov = compareFile.findVariable(org.getFullNameEscaped());
        if (ov != null) cn.compareVariable(org, ov);
      }

      infoTA.setText(f.toString());
      infoTA.gotoTop();
      infoWindow.setTitle("Compare");
      infoWindow.show();

    } catch (Throwable ioe) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);
      ioe.printStackTrace(new PrintStream(bos));
      infoTA.setText(bos.toString());
      infoTA.gotoTop();
      infoWindow.show();

    } finally {
      if (compareFile != null)
        try {
          compareFile.close();
        } catch (Exception eek) {
        }
    }
  }
  /*  Structure {
    char EntryName(64);
    char Definition(1024);
    char Unit(1024);
    char Scale Factor(1024);
  } TIME_DESCR(60);
     type = Layout(8);  type= 2 (chunked) storageSize = (1,3136) dataSize=0 dataAddress=684294
  */
  @Test
  public void testReadManyAtATime() throws java.io.IOException, InvalidRangeException {
    try (NetcdfFile ncfile = TestH5.openH5("IASI/IASI.h5")) {

      Variable dset = ncfile.findVariable("U-MARF/EPS/IASI_xxx_1C/DATA/TIME_DESCR");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 60);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 60);

      ArrayStructure data = (ArrayStructure) dset.read();
      StructureMembers.Member m = data.getStructureMembers().findMember("EntryName");
      assert m != null;
      for (int i = 0; i < dset.getSize(); i++) {
        String r = data.getScalarString(i, m);
        if (i % 2 == 0) assert r.equals("TIME[" + i / 2 + "]-days") : r + " at " + i;
        else assert r.equals("TIME[" + i / 2 + "]-milliseconds") : r + " at " + i;
      }
    }
    System.out.println("*** testReadManyAtATime ok");
  }
Esempio n. 6
0
  public void testNC3Read() throws IOException {
    NetcdfFile ncfile = TestDir.openFileLocal("testWrite.nc");

    assert (null != ncfile.findDimension("lat"));
    assert (null != ncfile.findDimension("lon"));

    Variable temp = null;
    assert (null != (temp = ncfile.findVariable("temperature")));

    // read entire array
    Array A;
    try {
      A = temp.read();
    } catch (IOException e) {
      System.err.println("ERROR reading file");
      assert (false);
      return;
    }
    assert (A.getRank() == 2);

    int i, j;
    Index ima = A.getIndex();
    int[] shape = A.getShape();
    assert shape[0] == 64;
    assert shape[1] == 128;

    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        double dval = A.getDouble(ima.set(i, j));
        assert (dval == (double) (i * 1000000 + j * 1000)) : dval;
      }
    }

    // read part of array
    int[] origin2 = new int[2];
    int[] shape2 = new int[2];
    shape2[0] = 1;
    shape2[1] = temp.getShape()[1];
    try {
      A = temp.read(origin2, shape2);
    } catch (InvalidRangeException e) {
      System.err.println("ERROR reading file " + e);
      assert (false);
      return;
    } catch (IOException e) {
      System.err.println("ERROR reading file");
      assert (false);
      return;
    }
    assert (A.getRank() == 2);

    for (j = 0; j < shape2[1]; j++) {
      assert (A.getDouble(ima.set(0, j)) == (double) (j * 1000));
    }

    // rank reduction
    Array Areduce = A.reduce();
    Index ima2 = Areduce.getIndex();
    assert (Areduce.getRank() == 1);

    for (j = 0; j < shape2[1]; j++) {
      assert (Areduce.getDouble(ima2.set(j)) == (double) (j * 1000));
    }

    // read char variable
    Variable c = null;
    assert (null != (c = ncfile.findVariable("svar")));
    try {
      A = c.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac = (ArrayChar) A;
    String val = ac.getString(ac.getIndex());
    assert val.equals("Testing 1-2-3") : val;
    // System.out.println( "val = "+ val);

    // read char variable 2
    Variable c2 = null;
    assert (null != (c2 = ncfile.findVariable("svar2")));
    try {
      A = c2.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac2 = (ArrayChar) A;
    assert (ac2.getString().equals("Two pairs of ladies stockings!"));

    // read String Array
    Variable c3 = null;
    assert (null != (c3 = ncfile.findVariable("names")));
    try {
      A = c3.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac3 = (ArrayChar) A;
    ima = ac3.getIndex();

    assert (ac3.getString(ima.set(0)).equals("No pairs of ladies stockings!"));
    assert (ac3.getString(ima.set(1)).equals("One pair of ladies stockings!"));
    assert (ac3.getString(ima.set(2)).equals("Two pairs of ladies stockings!"));

    // read String Array - 2
    Variable c4 = null;
    assert (null != (c4 = ncfile.findVariable("names2")));
    try {
      A = c4.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac4 = (ArrayChar) A;
    ima = ac4.getIndex();

    assert (ac4.getString(0).equals("0 pairs of ladies stockings!"));
    assert (ac4.getString(1).equals("1 pair of ladies stockings!"));
    assert (ac4.getString(2).equals("2 pairs of ladies stockings!"));

    // System.out.println( "ncfile = "+ ncfile);
    ncfile.close();
    System.out.println("**************TestRead done");
  }