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

The following examples show how to use ucar.nc2.Variable#findAttribute() . 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: TestGribUnits.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_true_degrees() throws IOException {
  // Make sure we return grib units of "degree true" as "degree_true"
  String filename = "tds/ncep/NDFD_CONUS_5km_20140805_1200.grib2";
  try (NetcdfDataset ds = NetcdfDatasets.openDataset(TestDir.cdmUnitTestDir + filename)) {
    Group grp = ds.getRootGroup();
    Assert.assertNotNull(grp);

    Variable var = grp.findVariableLocal("Wind_direction_from_which_blowing_height_above_ground");
    Assert.assertNotNull(var);

    Attribute att = var.findAttribute("units");
    Assert.assertNotNull(att);
    Assert.assertEquals("degree_true", att.getStringValue());
  }
}
 
Example 2
Source File: HdfEosModisConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean checkGroup(Group g) {
  Variable crs = g.findVariableLocal(HdfEos.HDFEOS_CRS);
  Group dataG = g.findGroupLocal(DATA_GROUP);
  if (crs != null && dataG != null) {
    Attribute att = crs.findAttribute(HdfEos.HDFEOS_CRS_Projection);
    if (att == null) {
      return false;
    }
    if (!att.getStringValue().equals("GCTP_SNSOID") && !att.getStringValue().equals("GCTP_GEO")) {
      return false;
    }
    return !(dataG.findDimensionLocal(DIMX_NAME) == null || dataG.findDimensionLocal(DIMY_NAME) == null);
  }

  for (Group ng : g.getGroups()) {
    if (checkGroup(ng)) {
      return true;
    }
  }
  return false;
}
 
Example 3
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code1_spherical_specified() throws IOException {
  String filename = dir + "LDUE18.grib2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("LambertConformal_Projection");
    Attribute axis = v.findAttribute("earth_radius");
    Assert.assertEquals(6371200., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 4
Source File: TestMRMS.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void checkVariable() throws IOException {
  try (NetcdfFile nc = NetcdfFiles.open(testfile)) {
    Variable var = nc.findVariable("LowLevelCompositeReflectivity_altitude_above_msl");
    Assert.assertNotNull(var);

    Attribute att = var.findAttribute("missing_value");
    Assert.assertNotNull(att);
    Assert.assertEquals(-99., att.getNumericValue().doubleValue(), 1e-6);

    att = var.findAttribute("_FillValue");
    Assert.assertNotNull(att);
    Assert.assertEquals(-999., att.getNumericValue().doubleValue(), 1e-6);
  }
}
 
Example 5
Source File: TestGribUnits.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_ordered_sequence_units() throws IOException {
  // Make sure we return the udunits string of "count"
  String filename = "tds/ncep/WW3_Coastal_Alaska_20140804_0000.grib2";
  try (NetcdfDataset ds = NetcdfDatasets.openDataset(TestDir.cdmUnitTestDir + filename)) {
    Variable var = ds.getRootGroup().findVariableLocal("ordered_sequence_of_data");
    Attribute att = var.findAttribute("units");
    Assert.assertNotNull(att);
    Assert.assertEquals("count", att.getStringValue());
  }
}
 
Example 6
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code7_oblate_specified_m() throws IOException {
  String filename = dir + "TT_FC_INCA.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("LambertConformal_Projection");
    Attribute axis = v.findAttribute("semi_major_axis");
    Assert.assertEquals(6377397., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 7
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code6_assume_spherical() throws IOException {
  String filename = dir + "berkes.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Group grp = ncfile.getRootGroup().getGroups().get(0);
    Variable v = grp.findVariableLocal("LatLon_Projection");
    Attribute axis = v.findAttribute("earth_radius");
    Assert.assertEquals(6371229., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 8
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code5_assume_WGS84() throws IOException {
  String filename = dir + "Albers_viirs_s.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("AlbersEqualArea_Projection");
    Attribute axis = v.findAttribute("semi_major_axis");
    Assert.assertEquals(6378137., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 9
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code3_oblate_specified_km() throws IOException {
  String filename = dir + "Eumetsat.VerticalPerspective.grb";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("SpaceViewPerspective_Projection");
    Attribute axis = v.findAttribute("semi_major_axis");
    Assert.assertEquals(6378140., axis.getNumericValue().doubleValue(), 0.1);
    axis = v.findAttribute("semi_minor_axis");
    Assert.assertEquals(6356755., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 10
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param variable
 * @return value from the _FillValue (or missing_value) attribute for the given variable.
 */
public static double getMissingValueDouble(Variable variable) {
	Attribute attribute = variable.findAttribute(FILL_VALUE_ATTRIBUTE_NAME);
	if (attribute != null) {
		return attribute.getNumericValue().doubleValue();
	}
	attribute = variable.findAttribute(MISSING_VALUE_ATTRIBUTE_NAME);
	if (attribute != null) {
		return attribute.getNumericValue().doubleValue();
	}
	return Double.NaN;
}
 
Example 11
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code1_spherical_specified_bad() throws IOException {
  String filename = dir + "sfc_d01_20080430_1200_f00000.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("LambertConformal_Projection");
    Attribute axis = v.findAttribute("earth_radius");
    Assert.assertEquals(6371200., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 12
Source File: CFGridWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void convertProjectionCTV(NetcdfDataset ds, Variable ctv) {
  Attribute att = ctv.findAttribute(_Coordinate.TransformType);
  if ((null != att) && att.getStringValue().equals("Projection")) {
    Attribute east = ctv.findAttribute("false_easting");
    Attribute north = ctv.findAttribute("false_northing");
    if ((null != east) || (null != north)) {
      double scalef = AbstractTransformBuilder.getFalseEastingScaleFactor(ds, ctv);
      if (scalef != 1.0) {
        convertAttribute(ctv, east, scalef);
        convertAttribute(ctv, north, scalef);
      }
    }
  }
}
 
Example 13
Source File: TestGribSpheroids.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void code0_assume_spherical() throws IOException {
  String filename = dir + "grid174_scanmode_64_example.grb2";
  try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
    Variable v = ncfile.findVariable("LatLon_Projection");
    Attribute axis = v.findAttribute("earth_radius");
    Assert.assertEquals(6367470., axis.getNumericValue().doubleValue(), 0.1);
  }
}
 
Example 14
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the trimmed String value of the attribute with the given attributeName for the given variable.
 *
 * @param variable
 * @param attributeName
 * @return String or null.
 */
private static String getAttributeStringValue(Variable variable, String attributeName) {
	Attribute attribute = variable.findAttribute(attributeName);
	if (attribute == null) {
		return null;
	}

	String value = attribute.getStringValue();
	if (value == null) {
		return null;
	}

	return value.trim();
}
 
Example 15
Source File: TestSimpleGeom.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testLine() throws IOException {

  String failMessage, found, expected;
  boolean testCond;

  String tstFile = TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_vlen_3hru_5timestep.nc";

  // open the test file
  NetcdfDataset ncd = NetcdfDataset.openDataset(tstFile);

  // make sure this dataset used the cfConvention
  expected = cfConvention;
  found = ncd.getConventionUsed();
  testCond = found.equals(expected);
  failMessage =
      format("This dataset used the %s convention, but should have used the %s convention.", found, expected);
  Assert.assertTrue(failMessage, testCond);

  // check that attributes were filled in correctly
  List<Variable> vars = ncd.getVariables();
  for (Variable v : vars) {
    if (v.findAttribute(CF.GEOMETRY) != null) {
      Assert.assertNotNull(v.findAttribute(CF.NODE_COORDINATES));
      Assert.assertNotNull(v.findAttribute(_Coordinate.Axes));
    }
  }
  ncd.close();
}
 
Example 16
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param variable
 * @return value from the add_offset attribute for the given variable.
 */
public static double getOffSetDouble(Variable variable) {
       Attribute attribute = variable.findAttribute(ADD_OFFSET_ATTRIBUTE_NAME);
       if (attribute != null) {
           return attribute.getNumericValue().doubleValue();
       } else {
           return 0;
       }
}
 
Example 17
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param variable
 * @return value from the scale_factor attribute for the given variable.
 */
public static double getScaleFactorDouble(Variable variable) {
       Attribute attribute = variable.findAttribute(SCALE_FACTOR_ATTRIBUTE_NAME);
       if (attribute != null) {
           return attribute.getNumericValue().doubleValue();
       }
       return 1;
}
 
Example 18
Source File: NcMLReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void cmdRemove(Variable v, String type, String name) {
  boolean err = false;

  if (type.equals("attribute")) {
    ucar.nc2.Attribute a = v.findAttribute(name);
    if (a != null) {
      v.remove(a);
      if (debugCmd)
        System.out.println("CMD remove " + type + " " + name);
    } else
      err = true;

  } else if (type.equals("variable") && v instanceof Structure) {
    Structure s = (Structure) v;
    Variable nested = s.findVariable(name);
    if (nested != null) {
      s.removeMemberVariable(nested);
      if (debugCmd)
        System.out.println("CMD remove " + type + " " + name);
    } else
      err = true;

  }

  if (err) {
    Formatter f = new Formatter();
    f.format("CMD remove %s CANT find %s location %s%n", type, name, location);
    log.info(f.toString());
  }
}
 
Example 19
Source File: SimpleGeometryReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given a variable name, returns the geometry type which that variable is associated with.
 * If the variable has no simple geometry information, null will be returned.
 * 
 * @param name variable name which will have geometry type be checked
 * @return Geometry Type if holds geometry information, null if not
 */
public GeometryType getGeometryType(String name) {
  Variable geometryVar = ds.findVariable(name);
  if (geometryVar == null)
    return null;

  // CFConvention
  if (ds.findGlobalAttribute(CF.CONVENTIONS) != null)
    if (ucar.nc2.dataset.conv.CF1Convention
        .getVersion(ds.findGlobalAttribute(CF.CONVENTIONS).getStringValue()) >= 8) {
      Attribute geometryTypeAttr;
      String geometry_type;

      geometryTypeAttr = geometryVar.findAttribute(CF.GEOMETRY_TYPE);
      if (geometryTypeAttr == null)
        return null;
      geometry_type = geometryTypeAttr.getStringValue();

      switch (geometry_type) {
        case CF.POLYGON:
          return GeometryType.POLYGON;
        case CF.LINE:
          return GeometryType.LINE;
        case CF.POINT:
          return GeometryType.POINT;
        default:
          return null;
      }
    }

  return null;
}
 
Example 20
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));
    }
  }
}