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

The following examples show how to use ucar.nc2.NetcdfFileWriter#write() . 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: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Write values for all time variables that are present.
 *
 * @param netcdfFileWriter
 * @param timeInfoTimeDimensionMap
 * @throws Exception
 */
public static void writeTimeVariablesValues(NetcdfFileWriter netcdfFileWriter,
		Map<ITimeInfo, Dimension> timeInfoTimeDimensionMap) throws Exception {

	for (Map.Entry<ITimeInfo, Dimension> entry : timeInfoTimeDimensionMap.entrySet()) {
		ITimeInfo timeInfo = entry.getKey();
		Dimension timeDimension = entry.getValue();

		Variable timeVariable = netcdfFileWriter.findVariable(timeDimension.getShortName());
		String timeUnitString = timeVariable.findAttribute(UNITS_ATTRIBUTE_NAME).getStringValue();
		DateUnit dateUnit = new DateUnit(timeUnitString);

		double[] times = timeInfo.getTimes();
		ArrayDouble.D1 timesArray = new ArrayDouble.D1(times.length);
		for (int n = 0; n < times.length; n++) {
			double newTime = dateUnit.makeValue(new Date(Time.mjdToMillies(times[n])));
			timesArray.set(n, newTime);
		}

		netcdfFileWriter.write(timeVariable, timesArray);
	}
}
 
Example 2
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void writeSelectedData(NetcdfFileWriter netcdfFileWriter, Variable variable, int[] origin, int[] sizeArray, double[] values) {
	//replace NaN values with missing value for variable.
	double missingValue = getMissingValueDouble(variable);
	if (!Double.isNaN(missingValue)) {
		double[] newValues = new double[values.length];
		System.arraycopy(values, 0, newValues, 0, values.length);
		for (int n = 0; n < newValues.length; n++) {
			if (Double.isNaN(newValues[n])) {
				newValues[n] = missingValue;
			}
		}
		values = newValues;
	} else {//if missingValue is Double.NaN, then NaN values in values array are already equal to missingValue.
		//do nothing.
	}

	//prepare array for writing.
	ucar.ma2.Array array = ucar.ma2.Array.factory(DataType.DOUBLE, sizeArray, values);

	//write data.
	try {
		netcdfFileWriter.write(variable, origin, array);
	} catch (IOException | InvalidRangeException e) {
		throw new RuntimeException("Error while writing data to netcdf variable '" + variable.getShortName()
				+ "' in netcdf file " + netcdfFileWriter.getNetcdfFile().getLocation() + ". Message was: " + e.getMessage(), e);
	}
}
 
Example 3
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Fills the realization variable with values.
 */
private static void writeRealizationVariableValues(NetcdfFileWriter netcdfFileWriter, List<Integer> ensembleMemberIndexList) throws Exception {
	if (ensembleMemberIndexList == null || ensembleMemberIndexList.isEmpty()) return;

	ArrayInt.D1 ensembleMemberIndices = new ArrayInt.D1(ensembleMemberIndexList.size());
	for (int n = 0; n < ensembleMemberIndexList.size(); n++) {
		ensembleMemberIndices.set(n, ensembleMemberIndexList.get(n));
	}
	Variable myVar = netcdfFileWriter.findVariable(REALIZATION_VARIABLE_NAME);
	netcdfFileWriter.write(myVar, ensembleMemberIndices);
}
 
Example 4
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void writeTimeVariableSingleValue(NetcdfFileWriter netcdfFileWriter, Variable timeVariable, int timeIndex, double time) throws Exception {
	String timeUnitString = timeVariable.findAttribute(UNITS_ATTRIBUTE_NAME).getStringValue();
	DateUnit dateUnit = new DateUnit(timeUnitString);

	double newTime = dateUnit.makeValue(new Date(Time.mjdToMillies(time)));
	ArrayDouble.D1 timeArray = new ArrayDouble.D1(1);
	timeArray.set(0, newTime);

	int[] origin = new int[]{timeIndex};
	netcdfFileWriter.write(timeVariable, origin, timeArray);
}
 
Example 5
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Writes the values of the 1D y and x coordinate variables with the given names.
 * The values are taken from the given geometry and are written as coordinates
 * in the coordinate system that is used by the given geometry.
 *
 * @param netcdfFileWriter
 * @param geometryInfo
 * @param yVariableName
 * @param xVariableName
 * @throws Exception
 */
private static void writeYX1DVariableValues(NetcdfFileWriter netcdfFileWriter,
		ArrayGeometryInfo geometryInfo, String yVariableName, String xVariableName) throws Exception {

	IArray latitudeArray = geometryInfo.getLatitudeArray();
	int rowCount = latitudeArray.length();
	ArrayDouble.D1 yArray = new ArrayDouble.D1(rowCount);
	for (int index = 0; index < rowCount; index++) {
		double y = latitudeArray.getValueAsDouble(index);
		if (Double.isNaN(y)) {//should never happen.
			y = DEFAULT_FILL_VALUE_DOUBLE;
		}
		yArray.set(index, y);
	}
	Variable myYVar = netcdfFileWriter.findVariable(yVariableName);
	netcdfFileWriter.write(myYVar, yArray);

	IArray longitudeArray = geometryInfo.getLongitudeArray();
	int columnCount = longitudeArray.length();
	ArrayDouble.D1 xArray = new ArrayDouble.D1(columnCount);
	for (int index = 0; index < columnCount; index++) {
		double x = longitudeArray.getValueAsDouble(index);
		if (Double.isNaN(x)) {//should never happen.
			x = DEFAULT_FILL_VALUE_DOUBLE;
		}
		xArray.set(index, x);
	}
	Variable myXVar = netcdfFileWriter.findVariable(xVariableName);
	netcdfFileWriter.write(myXVar, xArray);
}
 
Example 6
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void writeZVariableValues(NetcdfFileWriter netcdfFileWriter, LayeredIrregularGridGeometryInfo geometryInfo, String zVariableName) throws Exception {
	IVector zCoordinates = geometryInfo.getZCoordinates();
	ArrayDouble.D1 zArray = new ArrayDouble.D1(zCoordinates.getSize());
	for (int n = 0; n < zArray.getSize(); n++) {
		zArray.set(n, zCoordinates.getValue(n));
	}
	Variable myZVar = netcdfFileWriter.findVariable(zVariableName);
	netcdfFileWriter.write(myZVar, zArray);
}
 
Example 7
Source File: NetcdfDataObject.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Writes all data for the given exchangeItems to the given netcdfFile.
 * Only executed when this.lazyWriting = true
    *
 * @param netcdfFileWriter
 * @param exchangeItems to write.
 */
//TODO remove. This is only used for SWAN state files (see SwanStateNetcdfFileTest.testSwanNetcdfStateFile_1). AK
private void writeData(NetcdfFileWriter netcdfFileWriter, List<IExchangeItem> exchangeItems) {
	for (IExchangeItem exchangeItem : exchangeItems){
		if (exchangeItem.getGeometryInfo() == null){
			//TODO Julius: please remove this hack for SWAN state files. AK
               //TODO: replace this SWAN specific implementation with the above generic ones.
			String exchangeItemId = exchangeItem.getId();
			//TODO: add netcdf writers for various data type / exchangeitems.
			//For SWAN state file, only wave_spectrum is modified and rewritten.
			if (exchangeItemId.equalsIgnoreCase("wave_spectrum")){
				double[] dblValues = exchangeItem.getValuesAsDoubles();
				int my=31;
				int mx=61;
				int wave_frequency=25;
				int wave_direction=18;
				// get dimensions:
				// TODO: in the future, dimensions should be available in the exchangeItem as part of meta data
				// This will avoid having to read the netcdf file for obtaining the dimensions.
				List<Dimension> dimensions = netcdfFileWriter.getNetcdfFile().getDimensions();
				int nDim = dimensions.size();
				for (Dimension dimension : dimensions) {
					if ("my".equalsIgnoreCase(dimension.getShortName())) {
						my = dimension.getLength();
					} else if ("mx".equalsIgnoreCase(dimension.getShortName())) {
						mx = dimension.getLength();
					} else if ("wave_frequency".equalsIgnoreCase(dimension.getShortName())) {
						wave_frequency = dimension.getLength();
					} else if ("wave_direction".equalsIgnoreCase(dimension.getShortName())) {
						wave_direction = dimension.getLength();
					} else {
						continue;
					}
				}
				ArrayDouble.D4 values = new ArrayDouble.D4(my,mx,wave_frequency,wave_direction);
				int iVal = 0;
				for (int iy=0; iy<my; iy++){
					for (int ix=0; ix<mx; ix++){
						for (int iwf=0; iwf<wave_frequency; iwf++){
							for (int iwd=0; iwd<wave_direction; iwd++){
								values.set(iy,ix,iwf,iwd,dblValues[iVal]);
								iVal++;
							}
						}
					}
				}
				try {
					Variable myVar = netcdfFileWriter.findVariable("wave_spectrum");
					netcdfFileWriter.write(myVar,values);
				} catch (IOException | InvalidRangeException e) {
					e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
				}
			}
		}
	}
}