public Variable findVariableByAttribute(Group g, String attName, String attValue) { if (g == null) g = getRootGroup(); for (Variable v : variables) { for (Attribute att : v.getAttributes()) if (attName.equals(att.getShortName()) && attValue.equals(att.getStringValue())) return v; } for (Group nested : g.getGroups()) { Variable v = findVariableByAttribute(nested, attName, attValue); if (v != null) return v; } return null; }
public Variable findVariableByAttribute(Group g, String attName, String attValue) { if (g == null) g = getRootGroup(); for (Variable v : g.getVariables()) { for (Attribute att : v.getAttributes()) if (attName.equals(att.getShortName()) && attValue.equals(att.getStringValue())) return v; } for (Group nested : g.getGroups()) { Variable v = findVariableByAttribute(nested, attName, attValue); if (v != null) return v; } return null; }
public Variable findVariableByAttribute(Group g, String attName, String attValue) { if (g == null) g = getRootGroup(); for (Variable v : g.getVariables()) { for (Attribute att : v.getAttributes()) if (attName.equals(att.getShortName()) && attValue.equals(att.getStringValue())) return v; } for (Group nested : g.getGroups()) { Variable v = findVariableByAttribute(nested, attName, attValue); if (v != null) return v; } return null; }
@Test public void pdsGenType() throws Exception { // this one has a analysis and forecast in same variable String filename = TestDir.cdmUnitTestDir + "formats/grib2/08Aug08.12z.cras45_NA.grib2"; logger.debug("{}", filename); try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Variable v = ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_0-0-0_L1"); assert v != null : ncfile.getLocation(); } // this one has a forecast and error = must be seperate variables filename = TestDir.cdmUnitTestDir + "formats/grib2/RTMA_CONUS_2p5km_20111225_0000.grib2"; logger.debug("{}", filename); try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { assert ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_0-3-0_L1") != null; // Pressure_Surface assert ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_0-0-0_error_L103") != null; // Temperature_error_height_above_ground } }
System.out.println(); Variable V2 = ncfile.findVariableByAttribute(null, "units", "K");
@Test public void thinGrid() throws Exception { // this one has a analysis and forecast in same variable String filename = TestDir.cdmUnitTestDir + "formats/grib2/thinGrid.grib2"; logger.debug("{}", filename); try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Variable v = ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_0-0-0_L105"); assert v != null : ncfile.getLocation(); Array data = v.read(); int[] shape = data.getShape(); assert shape.length == 4; assert shape[shape.length - 2] == 1024; assert shape[shape.length - 1] == 2048; } }
@Test public void testNBits0() throws IOException { // Tests of GRIB2 nbits=0; should be reference value (0.0), not missing value String filename = TestDir.cdmUnitTestDir + "formats/grib2/SingleRecordNbits0.grib2"; logger.debug("testNBits0: {}", filename); try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Variable v = ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_0-1-194_L1"); assert v != null : ncfile.getLocation(); Array vals = v.read(); while (vals.hasNext()) { assert 0.0 == vals.nextDouble(); } } }
@Test public void testJPEG2K() throws Exception { // Tests specifically if the land-sea mask from GFS is decoded properly. // Not only does this test generally if the decoding works, but covers // a corner case with a single-bit field; this case was broken in the // original jj2000 code String filename = TestDir.cdmUnitTestDir + "tds/ncep/GFS_Global_onedeg_20100913_0000.grib2"; logger.debug("testJPEG2K: {}", filename); try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Variable v = ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_2-0-0_L1"); // Land_cover_0__sea_1__land_surface int[] origin = {0, 38, 281}; int[] shape = {1, 1, 2}; Array vals = v.read(origin, shape); Assert2.assertNearlyEquals(vals.getFloat(0), 0.0); Assert2.assertNearlyEquals(vals.getFloat(1), 1.0); } }
Variable var = ncf.findVariableByAttribute(best, Grib.VARIABLE_ID_ATTNAME, parameter); assert var != null : parameter; System.out.printf(" using variable %s%n", var.getFullName());
Variable v = ncfile.findVariableByAttribute(null, Grib.VARIABLE_ID_ATTNAME, "VAR_0-0-0_L1"); assert v != null : ncfile.getLocation(); ArrayFloat vals = (ArrayFloat) (v.read("0,:,0").reduce()); // read first column - its flipped