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

The following examples show how to use ucar.nc2.Variable#getUnitsString() . 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: UnidataObsConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Variable hasUnits(NetcdfDataset ds, String unitList) {
  List<Variable> varList = ds.getVariables();
  StringTokenizer stoker = new StringTokenizer(unitList, ",");
  while (stoker.hasMoreTokens()) {
    String unit = stoker.nextToken();

    for (Variable ve : varList) {
      String hasUnit = ve.getUnitsString();
      if (hasUnit == null)
        continue;
      if (hasUnit.equalsIgnoreCase(unit))
        return ve;
    }
  }
  return null;
}
 
Example 2
Source File: TestSimpleGeometryCSAll.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSimpleGeometryBuildCS() throws IOException, InvalidRangeException {
  NetcdfDataset data =
      NetcdfDataset.openDataset(TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_3hru_5timestep.nc");
  List<CoordinateSystem> csl = data.getCoordinateSystems();
  SimpleGeometryCSBuilder builder = new SimpleGeometryCSBuilder(data, csl.get(0), null);
  Variable hru_test = data.findVariable("hru_soil_moist");

  SimpleGeometryFeature sgc =
      new SimpleGeometryFeature(hru_test.getFullNameEscaped(), hru_test.getDataType(), hru_test.attributes(),
          csl.get(0).getName(), hru_test.getUnitsString(), hru_test.getDescription(), null, GeometryType.POLYGON);
  sgc.setCoordSys(builder.makeCoordSys());

  // Test retrieval of axis
  Assert.assertEquals("catchments_x", sgc.getXAxis().getFullNameEscaped());
  Assert.assertEquals("catchments_y", sgc.getYAxis().getFullNameEscaped());
  Assert.assertEquals("hruid", sgc.getIDAxis().getFullNameEscaped());
  Assert.assertEquals(null, sgc.getZAxis());

  // Now test reading a geometry.
  SimpleGeometry testGeometry = sgc.readGeometry(0);
  boolean testInstancePolygon = false;

  if (testGeometry instanceof Polygon)
    testInstancePolygon = true;
  Assert.assertEquals(true, testInstancePolygon);

  Polygon polygonTestGeometry = (Polygon) testGeometry;
  Assert.assertEquals(6233, polygonTestGeometry.getPoints().size());
}
 
Example 3
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates and returns an IGeometryInfo object for the given variable in the given netcdfFile.
 * If no geometry info available, then returns null.
 *
 * @param variable
 * @param netcdfFile
 * @return geometryInfo or null.
 */
public static IArrayGeometryInfo createGeometryInfo(Variable variable, NetcdfFile netcdfFile) {

	Variable latitudeVariable = findLatitudeVariableForVariable(variable, netcdfFile);
	Variable longitudeVariable = findLongitudeVariableForVariable(variable, netcdfFile);
	//currently only 2D grids are supported.
	if (latitudeVariable == null || longitudeVariable == null) {//if data does not depend on space.
		return null;
	}

	//if 2D grid.
	IQuantityInfo latitudeQuantityInfo = new QuantityInfo(LATITUDE_STANDARD_NAME, latitudeVariable.getUnitsString());
	IQuantityInfo longitudeQuantityInfo = new QuantityInfo(LONGITUDE_STANDARD_NAME, longitudeVariable.getUnitsString());
	List<Dimension> latitudeVariableDimensions = latitudeVariable.getDimensions();
	int[] latitudeValueIndices = new int[latitudeVariableDimensions.size()];
	for (int n = 0; n < latitudeValueIndices.length; n++) {
		latitudeValueIndices[n] = variable.findDimensionIndex(latitudeVariableDimensions.get(n).getShortName());
	}
	List<Dimension> longitudeVariableDimensions = longitudeVariable.getDimensions();
	int[] longitudeValueIndices = new int[longitudeVariableDimensions.size()];
	for (int n = 0; n < longitudeValueIndices.length; n++) {
		longitudeValueIndices[n] = variable.findDimensionIndex(longitudeVariableDimensions.get(n).getShortName());
	}
	IArray latitudeArray = (IArray) readData(latitudeVariable);
	IArray longitudeArray = (IArray) readData(longitudeVariable);
       //the latitude and longitude coordinates are stored in the same order as in the netcdf file.
	ArrayGeometryInfo geometryInfo = new ArrayGeometryInfo(latitudeArray, latitudeValueIndices,
			latitudeQuantityInfo, longitudeArray, longitudeValueIndices, longitudeQuantityInfo,null,null,null);
	return geometryInfo;
}
 
Example 4
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if the given variable is a valid time (coordinate) variable.
 *
 * @param variable
 * @return whether the given variable is a valid time (coordinate) variable.
 */
public static boolean isTimeVariable(Variable variable) {
	//check if axis is "T".
	if ("T".equalsIgnoreCase(getAttributeStringValue(variable, AXIS_ATTRIBUTE_NAME))) {
		return true;
	}

	// check if the variable is frequency (the unit is Hz); if so, return false.
	// This is explicitly written here because getDateUnitFromDimension(variable) does not give null
	// for frequency, as if frequency is a time coordinate variable.
	if ("Hz".equalsIgnoreCase(variable.getUnitsString())){
		return false;
	}

	//check if unit of time.
	//according to the CF-convention (see http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/cf-conventions.html#time-coordinate):
	//"A time coordinate is identifiable from its units string alone.
	//The Udunits routines utScan() and utIsTime() can be used to make this determination."
	//TODO use Udunits routines utScan() and utIsTime() instead of ucar.nc2.units.DateUnit. AK
	if (variable.getUnitsString() != null && !variable.getUnitsString().isEmpty()
			&& getDateUnitFromDimension(variable) != null) {
		//if variable has a valid unit of time.
		return true;
	}

	return false;
}
 
Example 5
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param var
 * @return DateUnit converted unit for this <code>var</code>
 *		 null if conversion to DateUnit throws an exception
 */
private static DateUnit getDateUnitFromDimension(Variable var) {
	try {
		String unitString = var.getUnitsString();
		DateUnit unit = new DateUnit(unitString);
		return unit;
	} catch (Exception e) {
		//if the given variable does not have a valid unit of time.
		return null;
	}
}
 
Example 6
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if the given variable is a valid latitude variable.
 *
 * @param variable
 * @return whether the given variable is a valid latitude variable.
 */
private static boolean isLatitudeVariable(Variable variable) {
	//check if axis is "Y".
	String axisAttributeStringValue = getAttributeStringValue(variable, AXIS_ATTRIBUTE_NAME);
	if ("Y".equalsIgnoreCase(axisAttributeStringValue)) {
		return true;
	}

	//check if axis is "N".
	if ("N".equalsIgnoreCase(axisAttributeStringValue)) {
		return true;
	}

	//check if standard name is "latitude".
	String standardNameAttributeStringValue = getAttributeStringValue(variable, STANDARD_NAME_ATTRIBUTE_NAME);
	if (LATITUDE_STANDARD_NAME.equalsIgnoreCase(standardNameAttributeStringValue)) {
		return true;
	}

	//check if standard name is "projection_y_coordinate".
	if (LATITUDE_STANDARD_NAME_2.equalsIgnoreCase(standardNameAttributeStringValue)) {
		return true;
	}

	//check if unit of latitude.
	//according to the CF-convention (see http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/cf-conventions.html#latitude-coordinate):
	//"The recommended unit of latitude is degrees_north.
	//Also acceptable are degree_north, degree_N, degrees_N, degreeN, and degreesN."
	String unitsString = variable.getUnitsString();
	if (unitsString != null && (unitsString.equalsIgnoreCase("degrees_north") || unitsString.equalsIgnoreCase("degree_north")
								|| unitsString.equalsIgnoreCase("degree_N") || unitsString.equalsIgnoreCase("degrees_N")
								|| unitsString.equalsIgnoreCase("degreeN") || unitsString.equalsIgnoreCase("degreesN"))) {
		//if variable has a valid unit of latitude.
		return true;
	}

	return false;
}
 
Example 7
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if the given variable is a valid longitude variable.
 *
 * @param variable
 * @return whether the given variable is a valid longitude variable.
 */
private static boolean isLongitudeVariable(Variable variable) {
	//check if axis is "X".
	if ("X".equalsIgnoreCase(getAttributeStringValue(variable, AXIS_ATTRIBUTE_NAME))) {
		return true;
	}

	//check if axis is "N".
	if ("N".equalsIgnoreCase(getAttributeStringValue(variable, AXIS_ATTRIBUTE_NAME))) {
		return true;
	}

	//check if standard name is "longitude".
	if (LONGITUDE_STANDARD_NAME.equalsIgnoreCase(getAttributeStringValue(variable, STANDARD_NAME_ATTRIBUTE_NAME))) {
		return true;
	}

	//check if standard name is "projection_x_coordinate".
	if (LONGITUDE_STANDARD_NAME_2.equalsIgnoreCase(getAttributeStringValue(variable, STANDARD_NAME_ATTRIBUTE_NAME))) {
		return true;
	}

	//check if unit of longitude.
	//according to the CF-convention (see http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/cf-conventions.html#longitude-coordinate):
	//"The recommended unit of longitude is degrees_east.
	//Also acceptable are degree_east, degree_E, degrees_E, degreeE, and degreesE."
	String unitsString = variable.getUnitsString();
	if (unitsString != null && (unitsString.equalsIgnoreCase("degrees_east") || unitsString.equalsIgnoreCase("degree_east")
								|| unitsString.equalsIgnoreCase("degree_E") || unitsString.equalsIgnoreCase("degrees_N")
								|| unitsString.equalsIgnoreCase("degreeE") || unitsString.equalsIgnoreCase("degreesE"))) {
		//if variable has a valid unit of longitude.
		return true;
	}

	return false;
}
 
Example 8
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void setTimeUnits() throws Exception {
  Variable t = ds.findVariable("time");
  String ut = t.getUnitsString();
  dateUnits = new DateUnit(ut);
  calDateUnits = CalendarDateUnit.of(null, ut);
}
 
Example 9
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void transferGroup(Group srcGroup, Group targetGroup, NetcdfDataset target) throws IOException {
  // group attributes
  DatasetConstructor.transferGroupAttributes(srcGroup, targetGroup);

  // dimensions
  for (Dimension d : srcGroup.getDimensions()) {
    if (null == targetGroup.findDimensionLocal(d.getShortName())) {
      Dimension newd =
          new Dimension(d.getShortName(), d.getLength(), d.isShared(), d.isUnlimited(), d.isVariableLength());
      targetGroup.addDimension(newd);
    }
  }

  // transfer variables - eliminate any references to component files
  for (Variable v : srcGroup.getVariables()) {
    Variable targetV = targetGroup.findVariableLocal(v.getShortName());

    if (null == targetV) { // add it
      if (v instanceof Structure) {
        targetV = new StructureDS(target, targetGroup, null, v.getShortName(), v.getDimensionsString(),
            v.getUnitsString(), v.getDescription());
        // LOOK - not adding the members here - what to do ??

      } else {
        targetV = new VariableDS(target, targetGroup, null, v.getShortName(), v.getDataType(),
            v.getDimensionsString(), v.getUnitsString(), v.getDescription());
      }

      DatasetConstructor.transferVariableAttributes(v, targetV);
      VariableDS vds = (VariableDS) v;
      targetV.setSPobject(vds); // temporary, for non-agg variables when proto is made
      if (vds.hasCachedDataRecurse()) {
        if (vds.getSize() > 1000 * 1000) {
          boolean wtf = vds.hasCachedDataRecurse();
        }
        targetV.setCachedData(vds.read()); //
      }
      targetGroup.addVariable(targetV);
    }
  }

  // nested groups - check if target already has it
  for (Group srcNested : srcGroup.getGroups()) {
    Group nested = targetGroup.findGroupLocal(srcNested.getShortName());
    if (null == nested) {
      nested = new Group(target, targetGroup, srcNested.getShortName());
      targetGroup.addGroup(nested);
      for (EnumTypedef et : srcNested.getEnumTypedefs()) {
        targetGroup.addEnumeration(et);
      }
    }
    transferGroup(srcNested, nested, target);
  }
}