Java Code Examples for ucar.nc2.NetcdfFileWriter#addVariable()

The following examples show how to use ucar.nc2.NetcdfFileWriter#addVariable() . 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: TestGridClose.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void alterExistingFile(String url) throws IOException {
  NetcdfFileWriter file = null;
  try {
    file = NetcdfFileWriter.openExisting(url);
    file.setRedefineMode(true);
    // Group rootGroup = file.getRootGroup();
    // Group headerDataGroup = new Group(file, rootGroup, "header_data");
    // file.addGroup(rootGroup, headerDataGroup);
    file.addVariable(null, newVarName, DataType.FLOAT, "z y x");
  } finally {
    if (file != null) {
      file.setRedefineMode(false);
      file.flush();
      file.close();
    }
  }
}
 
Example 2
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a time dimension and variable.
 *
 * @param dataFile
 * @param timeCount      if timeCount is -1, then creates an unlimited timeDimension
 * @param timeUnitString
 * @return timeDimension
 */
public static Dimension createTimeVariable(NetcdfFileWriter dataFile, String timeVariableName, int timeCount, String timeUnitString) {
	//create time dimension.
	Dimension timeDimension;
	if (timeCount == -1 || timeCount == 0) {
		timeDimension = dataFile.addUnlimitedDimension(timeVariableName);
	} else {
		timeDimension = dataFile.addDimension(null,timeVariableName, timeCount);
	}

	//create time variable.
	Variable myVar = dataFile.addVariable(null,timeVariableName, ucar.ma2.DataType.DOUBLE, Arrays.asList(timeDimension) );
	dataFile.addVariableAttribute(myVar, new Attribute (STANDARD_NAME_ATTRIBUTE_NAME, TIME_VARIABLE_NAME) );
	dataFile.addVariableAttribute(myVar, new Attribute(LONG_NAME_ATTRIBUTE_NAME, TIME_VARIABLE_NAME));
	dataFile.addVariableAttribute(myVar, new Attribute(UNITS_ATTRIBUTE_NAME, timeUnitString));
	//use default calendar.
	dataFile.addVariableAttribute(myVar, new Attribute(CALENDAR_ATTRIBUTE_NAME, DEFAULT_CALENDAR_ATTRIBUTE_VALUE));
	dataFile.addVariableAttribute(myVar, new Attribute(AXIS_ATTRIBUTE_NAME, T_AXIS));
	return timeDimension;
}
 
Example 3
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void createDataVariable(NetcdfFileWriter netcdfFileWriter, IExchangeItem exchangeItem, Dimension timeDimension, Dimension realizationDimension, Dimension stationDimension,
			Map<IGeometryInfo, GridVariableProperties> geometryInfoGridVariablePropertiesMap) {
		List<Dimension> dimensions = new ArrayList<Dimension>();
		if (timeDimension != null) {
			dimensions.add(timeDimension);
		}
		if (realizationDimension != null) {
			dimensions.add(realizationDimension);
		}
		if (stationDimension != null) {
			dimensions.add(stationDimension);
		}
		if (geometryInfoGridVariablePropertiesMap != null) {
			GridVariableProperties gridVariableProperties = geometryInfoGridVariablePropertiesMap.get(exchangeItem.getGeometryInfo());
			if (gridVariableProperties != null) {
				dimensions.addAll(gridVariableProperties.getDimensions());
			}
		}

		DataType dataType = getDataType(exchangeItem.getValuesType());
		String variableName = getVariableName(exchangeItem);
		Variable myVar = netcdfFileWriter.addVariable(null,variableName, dataType, dimensions);
		//at this point standard_name of data is unknown, so cannot add the optional standard_name attribute here.
		netcdfFileWriter.addVariableAttribute(myVar, new Attribute(LONG_NAME_ATTRIBUTE_NAME, variableName));
		netcdfFileWriter.addVariableAttribute(myVar, new Attribute(UNITS_ATTRIBUTE_NAME, exchangeItem.getQuantityInfo().getUnit() ));
		netcdfFileWriter.addVariableAttribute(myVar, new Attribute(FILL_VALUE_ATTRIBUTE_NAME, DEFAULT_FILL_VALUE_DOUBLE));

		//TODO add grid mapping attribute. AK
//		if (gridVariableProperties != null) {
//			String gridMappingVariableName = gridVariableProperties.getGridMappingVariableName();
//			if (gridMappingVariableName != null) {
//				netcdfFile.addVariableAttribute(dataVariableName, GRID_MAPPING_ATTRIBUTE, gridMappingVariableName);
//			}
//		}
	}
 
Example 4
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void createZVariable(NetcdfFileWriter netcdfFileWriter, Dimension zDimension) {
	String zVariableName = zDimension.getShortName();
	ArrayList<Dimension> dimensions = new ArrayList<>();
	dimensions.add(zDimension);
	Variable myVar = netcdfFileWriter.addVariable(null,zVariableName, DataType.DOUBLE, dimensions);
	netcdfFileWriter.addVariableAttribute(myVar,new Attribute(UNITS_ATTRIBUTE_NAME, Z_UNITS));
	netcdfFileWriter.addVariableAttribute(myVar,new Attribute(AXIS_ATTRIBUTE_NAME, Z_AXIS));
	netcdfFileWriter.addVariableAttribute(myVar,new Attribute(POSITIVE_ATTRIBUTE_NAME, Z_POSITIVE));
	netcdfFileWriter.addVariableAttribute(myVar,new Attribute(FILL_VALUE_ATTRIBUTE_NAME, DEFAULT_FILL_VALUE_DOUBLE));
}
 
Example 5
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * General method to create a one dimensional coordinate variable with the given properties.
 *
 * @param dataFile
 * @param variableName
 * @param dimension
 * @param standardName
 * @param longName
 * @param units
 * @param axis
 */
private static void createCoordinateVariable(NetcdfFileWriter dataFile,
		String variableName, Dimension dimension, String standardName,
		String longName, String units, String axis, double fillValue) {

	ArrayList<Dimension> dimensions = new ArrayList<Dimension>();
	dimensions.add(dimension);
	Variable myVar = dataFile.addVariable(null, variableName, DataType.DOUBLE, dimensions);
	dataFile.addVariableAttribute(myVar,new Attribute(STANDARD_NAME_ATTRIBUTE_NAME, standardName));
	dataFile.addVariableAttribute(myVar,new Attribute(LONG_NAME_ATTRIBUTE_NAME, longName));
	dataFile.addVariableAttribute(myVar,new Attribute(UNITS_ATTRIBUTE_NAME, units));
	dataFile.addVariableAttribute(myVar,new Attribute(AXIS_ATTRIBUTE_NAME, axis));
	dataFile.addVariableAttribute(myVar,new Attribute( FILL_VALUE_ATTRIBUTE_NAME, fillValue));
}
 
Example 6
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Add variable for station_id.
 */
public static Dimension createStationsVariable(NetcdfFileWriter netcdfFileWriter, String stationNameVarName, String stationDimensionVarName, int stationCount) {
	Dimension stationDimension = netcdfFileWriter.addDimension(null,stationDimensionVarName, stationCount);
	Dimension charDimension = netcdfFileWriter.addDimension(null,CHAR_LEN_ID, CHARLENGTH_ID);
	ArrayList<Dimension> dimensions = new ArrayList<Dimension>();
	dimensions.add(stationDimension);
	dimensions.add(charDimension);

	Variable myVar = netcdfFileWriter.addVariable(null, stationNameVarName, DataType.CHAR, dimensions);
	netcdfFileWriter.addVariableAttribute( myVar, new Attribute( LONG_NAME_ATTRIBUTE_NAME, "station identification code"));
	netcdfFileWriter.addVariableAttribute( myVar, new Attribute(CF_ROLE_ATTRIBUTE_NAME, NetcdfUtils.TIME_SERIES_ID_CF_ROLE));

	return stationDimension;
}
 
Example 7
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a realization dimension and variable.
 */
public static Dimension createRealizationVariable(NetcdfFileWriter netcdfFileWriter, int ensembleMemberCount) {
	//See http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/18/cf-standard-name-table.html
	//standard name "realization":
	//Realization is used to label a dimension that can be thought of as a statistical sample,
	//e.g., labelling members of a model ensemble.
	//See http://www.clivar.org/sites/default/files/imported/organization/wgsip/chfp/CHFP_metadata.pdf
	//and http://www.ppt2txt.com/r/zd0ebf25/
	//int realization(realization) ;
	//realization:data_type = "int" ;
	//realization:units = "1" ;
	//realization:standard_name = "realization" ;
	//realization:long_name = "Number of the simulation in the ensemble" ;
	//float physical_field(time, realization, level, latitude, longitude) ;

	//create realization dimension.
	Dimension realizationDimension = netcdfFileWriter.addDimension(null, REALIZATION_VARIABLE_NAME, ensembleMemberCount);

	//create realization variable.
	ArrayList<Dimension> realizationDimensionList = new ArrayList<>();
	realizationDimensionList.add(realizationDimension);
	Variable myVar = netcdfFileWriter.addVariable(null, REALIZATION_VARIABLE_NAME, DataType.INT, realizationDimensionList);
	netcdfFileWriter.addVariableAttribute(myVar, new Attribute(STANDARD_NAME_ATTRIBUTE_NAME, REALIZATION_VARIABLE_NAME));
	netcdfFileWriter.addVariableAttribute(myVar, new Attribute(LONG_NAME_ATTRIBUTE_NAME, "Index of an ensemble member within an ensemble"));
	netcdfFileWriter.addVariableAttribute(myVar, new Attribute(UNITS_ATTRIBUTE_NAME, "1"));

	return realizationDimension;
}