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

The following examples show how to use ucar.nc2.units.DateUnit#makeDate() . 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
/**
 * Reads the times from the given timeVariable and converts them to MJD
 * in the returned array.
 *
 * @param timeVariable
 * @return times array.
 * @throws IOException
 */
private static double[] readTimes(Variable timeVariable) throws IOException {
	double[] convertedTimes = new double[0];

	if ((timeVariable != null) && timeVariable.isCoordinateVariable()) {
		//read times.
		ucar.ma2.Array timesArray = timeVariable.read();
		double[] times = (double[]) timesArray.get1DJavaArray(double.class);

		//convert times.
		convertedTimes = new double[times.length];
		DateUnit dateUnit = getDateUnitFromDimension(timeVariable);
		for (int n = 0; n < times.length; n++) {
			Date date = dateUnit.makeDate(times[n]);
			if (date == null) {
				convertedTimes[n] = 0;
				continue;
			}

			long time = date.getTime();
			convertedTimes[n] = Time.milliesToMjd(time);
		}
	}

	return convertedTimes;
}
 
Example 2
Source File: MetadataExtractor.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static CalendarDateRange extractCalendarDateRange(GridDataset gridDataset) {
  CalendarDateRange maxDateRange = null;

  for (GridDataset.Gridset gridset : gridDataset.getGridsets()) {
    GridCoordSystem gsys = gridset.getGeoCoordSystem();
    CalendarDateRange dateRange;

    CoordinateAxis1DTime time1D = gsys.getTimeAxis1D();
    if (time1D != null) {
      dateRange = time1D.getCalendarDateRange();
    } else {
      CoordinateAxis time = gsys.getTimeAxis();
      if (time == null)
        continue;

      try {
        DateUnit du = new DateUnit(time.getUnitsString());
        Date minDate = du.makeDate(time.getMinValue());
        Date maxDate = du.makeDate(time.getMaxValue());
        dateRange = CalendarDateRange.of(minDate, maxDate);
      } catch (Exception e) {
        logger.warn("Illegal Date Unit " + time.getUnitsString());
        continue;
      }
    }

    if (maxDateRange == null)
      maxDateRange = dateRange;
    else
      maxDateRange = maxDateRange.extend(dateRange);
  }

  return maxDateRange;
}
 
Example 3
Source File: TimeUtils.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static double udUnitsTimeToMjd(double udUnitsTime, String udUnitsTimeUnitsString) {
	DateUnit dateUnit = createDateUnit(udUnitsTimeUnitsString);
	try {
		Date date = dateUnit.makeDate(udUnitsTime);
		return Time.milliesToMjd(date.getTime());
	} catch (Exception e) {
		throw new RuntimeException("Cannot parse time " + udUnitsTime + " using UDUNITS time units '" + udUnitsTimeUnitsString + "'. Message was: " + e.getMessage(), e);
	}
}