Java Code Examples for ucar.nc2.units.DateUnit#makeValue()

The following examples show how to use ucar.nc2.units.DateUnit#makeValue() . 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 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 3
Source File: TimeUtils.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static double mjdToUdUnitsTime(double mjd, String udUnitsTimeUnitsString) {
	DateUnit dateUnit = createDateUnit(udUnitsTimeUnitsString);
	return dateUnit.makeValue(new Date(Time.mjdToMillies(mjd)));
}