ucar.nc2.units.DateUnit Java Examples
The following examples show how to use
ucar.nc2.units.DateUnit.
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: NidsRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected void setStartDate() { String start_datetime = ds.getRootGroup().findAttributeString("time_coverage_start", null); if (start_datetime != null) { startDate = DateUnit.getStandardOrISO(start_datetime); return; } else { CoordinateAxis axis = ds.findCoordinateAxis(AxisType.Time); if (axis != null) { double val = axis.getMinValue(); startDate = dateUnits.makeDate(val); return; } } parseInfo.append("*** start_datetime not Found\n"); }
Example #2
Source File: NidsRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected void setEndDate() { String end_datetime = ds.getRootGroup().findAttributeString("time_coverage_end", null); if (end_datetime != null) { endDate = DateUnit.getStandardOrISO(end_datetime); } else { CoordinateAxis axis = ds.findCoordinateAxis(AxisType.Time); if (axis != null) { double val = axis.getMaxValue(); endDate = dateUnits.makeDate(val); return; } } parseInfo.append("*** end_datetime not Found\n"); }
Example #3
Source File: NetcdfUtils.java From OpenDA with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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 #4
Source File: NetcdfUtils.java From OpenDA with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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 #5
Source File: SortingStationPointFeatureCache.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public SortingStationPointFeatureCache(Comparator<StationPointFeature> comp, StationPointFeature proto, DateUnit dateUnit) throws IOException { this.inMemCache = new TreeMap<>(Preconditions.checkNotNull(comp, "comp == null")); if (proto != null && dateUnit != null) { this.stationFeatCopyFactory = new StationFeatureCopyFactory(proto); } }
Example #6
Source File: UnidataObsDatasetHelper.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static Date getStartDate(NetcdfDataset ds) { Attribute att = ds.findGlobalAttributeIgnoreCase("time_coverage_start"); if (null == att) throw new IllegalArgumentException("Must have a time_coverage_start global attribute"); if (att.getDataType() == DataType.STRING) { return DateUnit.getStandardOrISO(att.getStringValue()); } else { throw new IllegalArgumentException("time_coverage_start must be a ISO or udunit date string"); } }
Example #7
Source File: Dorade2RadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setTimeUnits() throws Exception { for (CoordinateAxis axis : ncd.getCoordinateAxes()) { if (axis.getAxisType() == AxisType.Time) { String units = axis.getUnitsString(); dateUnits = new DateUnit(units); return; } } parseInfo.append("*** Time Units not Found\n"); }
Example #8
Source File: NsslRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setTimeUnits() throws Exception { for (CoordinateAxis axis : ds.getCoordinateAxes()) { if (axis.getAxisType() == AxisType.Time) { String units = axis.getUnitsString(); dateUnits = new DateUnit(units); calDateUnits = CalendarDateUnit.of(null, units); return; } } parseInfo.append("*** Time Units not Found\n"); }
Example #9
Source File: NsslRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setStartDate() { String start_datetime = ds.getRootGroup().findAttributeString("time_coverage_start", null); if (start_datetime != null) startDate = DateUnit.getStandardOrISO(start_datetime); else parseInfo.append("*** start_datetime not Found\n"); }
Example #10
Source File: NsslRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setEndDate() { String end_datetime = ds.getRootGroup().findAttributeString("time_coverage_end", null); if (end_datetime != null) endDate = DateUnit.getStandardOrISO(end_datetime); else parseInfo.append("*** end_datetime not Found\n"); }
Example #11
Source File: AggregationOuterDimension.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
CoordValueVar(String varName, DataType dtype, String units) { super(varName, dtype); this.units = units; try { du = new DateUnit(units); } catch (Exception e) { // ok to fail - may not be a time coordinate } }
Example #12
Source File: NetcdfDatasetInfo.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private String isUdunits(String unit) { try { new DateUnit(unit); return "date"; } catch (Exception e) { // ok } SimpleUnit su = SimpleUnit.factory(unit); if (null == su) return "false"; return su.getCanonicalString(); }
Example #13
Source File: AggregationOuter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
CoordValueVar(String varName, DataType dtype, String units) { super(varName, dtype); this.units = units; try { du = new DateUnit(units); } catch (Exception e) { // ok to fail - may not be a time coordinate } }
Example #14
Source File: TimeCoord.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
double getValueInHours(Calendar cal, DateUnit unit, double value) { // CalendarDate d = unit.makeCalendarDate(value); // double secs = unit.getTimeUnit().getValueInSeconds(value); // CalendarDate d = CalendarDate.of(cal, unit.getDateOrigin().getTime() + (long)(1000*secs)); CalendarDateUnit dateUnit = CalendarDateUnit.withCalendar(cal, unit.getUnitsString()); // this will throw exception // on failure CalendarDate d = dateUnit.makeCalendarDate(value); return FmrcInv.getOffsetInHours(runDate, d); }
Example #15
Source File: Nexrad2RadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setTimeUnits() throws Exception { for (CoordinateAxis axis : ds.getCoordinateAxes()) { if (axis.getAxisType() == AxisType.Time) { String units = axis.getUnitsString(); dateUnits = new DateUnit(units); calDateUnits = CalendarDateUnit.of(null, units); return; } } parseInfo.append("*** Time Units not Found\n"); }
Example #16
Source File: TestCalendarDateUnit.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testDate(String udunits) { Date uddate = DateUnit.getStandardDate(udunits); CalendarDate cd = CalendarDate.parseUdunits(null, udunits); if (!uddate.equals(cd.toDate())) { System.out.printf(" BAD %s == %s != %s (diff = %d)%n", udunits, df.toDateTimeString(uddate), cd, cd.toDate().getTime() - uddate.getTime()); } }
Example #17
Source File: TestOffAggFmrcGrib.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testRagged() throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n" + " <aggregation dimName='run' type='forecastModelRunCollection' timeUnitsChange='true' " + "fmrcDefinition='" + TestDir.cdmUnitTestDir + "ncml/nc/c20ss/fmrcDefinition.xml'>\n" + " <scan location='" + TestDir.cdmUnitTestDir + "ncml/nc/c20ss/' suffix='.grib1' enhance='true' " + "dateFormatMark='NAM_CONUS_20km_selectsurface_#yyyyMMdd_HHmm'/>\n" + " </aggregation>\n" + "</netcdf>"; double[][] evals = new double[][] { {0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 36.0, 39.0, 42.0, 45.0, 48.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, {6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 36.0, 39.0, 42.0, 45.0, 48.0, 51.0, 54.0, 57.0, 60.0, 63.0, 66.0, 69.0, 72.0, 75.0, 78.0, 81.0, 84.0, 87.0, 90.0}, {12.0, 15.0, 18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 36.0, 39.0, 42.0, 45.0, 48.0, 51.0, 54.0, 57.0, 60.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, {18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 36.0, 39.0, 42.0, 45.0, 48.0, 51.0, 54.0, 57.0, 60.0, 63.0, 66.0, 69.0, 72.0, 75.0, 78.0, 81.0, 84.0, 87.0, 90.0, 93.0, 96.0, 99.0, 102.0}}; try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(xml), "AggFmrcGribRunseq.ncml", null)) { int naggs = 4; String timeVarName = "time"; String timeDimName = "time"; testDimensions(ncfile, naggs, timeDimName); testCoordVar(ncfile, 257); int[] runtimes = new int[] {0, 6, 12, 18}; testAggCoordVar(ncfile, naggs, new DateUnit("hours since 2006-07-29T18:00:00Z"), runtimes); testTimeCoordVar(ncfile, naggs, 29, timeVarName, evals); } }
Example #18
Source File: TestOffAggFmrcGrib.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testAggCoordVar(NetcdfFile ncfile, int nagg, DateUnit du, int[] runhours) { Variable time = ncfile.findVariable("run"); assert null != time; assert time.getShortName().equals("run"); assert time.getRank() == 1; assert time.getSize() == nagg; assert time.getShape()[0] == nagg; assert time.getDataType() == DataType.DOUBLE; DateFormatter formatter = new DateFormatter(); try { Array data = time.read(); assert data.getRank() == 1; assert data.getSize() == nagg; assert data.getShape()[0] == nagg; assert data.getElementType() == double.class; logger.debug(Ncdump.printArray(data)); int count = 0; IndexIterator dataI = data.getIndexIterator(); while (dataI.hasNext()) { double val = dataI.getDoubleNext(); assert val == runhours[count]; count++; } } catch (IOException io) { io.printStackTrace(); assert false; } }
Example #19
Source File: TimeUtils.java From OpenDA with GNU Lesser General Public License v3.0 | 5 votes |
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); } }
Example #20
Source File: TimeUtils.java From OpenDA with GNU Lesser General Public License v3.0 | 5 votes |
private static DateUnit createDateUnit(String udUnitsTimeUnitsString) { try { return new DateUnit(udUnitsTimeUnitsString); } catch (Exception e) { throw new RuntimeException("Cannot create DateUnit from UDUNITS time units '" + udUnitsTimeUnitsString + "'. Message was: " + e.getMessage(), e); } }
Example #21
Source File: NetcdfUtils.java From OpenDA with GNU Lesser General Public License v3.0 | 5 votes |
/** * @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 #22
Source File: NetcdfUtils.java From OpenDA with GNU Lesser General Public License v3.0 | 5 votes |
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 #23
Source File: VariableWrapper.java From sis with Apache License 2.0 | 5 votes |
/** * Parses the given unit symbol and set the {@link #epoch} if the parsed unit is a temporal unit. * This method is called by {@link #getUnit()}. This implementation delegates the work to the UCAR * library and converts the result to {@link Unit} and {@link java.time.Instant} objects. */ @Override protected Unit<?> parseUnit(final String symbols) throws Exception { if (TIME_UNIT_PATTERN.matcher(symbols).matches()) { /* * UCAR library has two methods for getting epoch: getDate() and getDateOrigin(). * The former adds to the origin the number that may appear before the unit, for example * "2 hours since 1970-01-01 00:00:00". If there is no such number, then the two methods * are equivalent. It is not clear that adding such number is the right thing to do. */ final DateUnit temporal = new DateUnit(symbols); epoch = temporal.getDateOrigin().toInstant(); return Units.SECOND.multiply(temporal.getTimeUnit().getValueInSeconds()); } else { /* * For all other units, we get the base unit (meter, radian, Kelvin, etc.) and multiply by the scale factor. * We also need to take the offset in account for constructing the °C unit as a unit shifted from its Kelvin * base. The UCAR library does not provide method giving directly this information, so we infer it indirectly * by converting the 0 value. */ final SimpleUnit ucar = SimpleUnit.factoryWithExceptions(symbols); if (ucar.isUnknownUnit()) { return Units.valueOf(symbols); } final String baseUnit = ucar.getUnitString(); Unit<?> unit = Units.valueOf(baseUnit); final double scale = ucar.getValue(); final double offset = ucar.convertTo(0, SimpleUnit.factoryWithExceptions(baseUnit)); unit = unit.shift(offset); if (!Double.isNaN(scale)) { unit = unit.multiply(scale); } return unit; } }
Example #24
Source File: NidsRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setTimeUnits() throws Exception { CoordinateAxis axis = ds.findCoordinateAxis(AxisType.Time); if (axis == null) { parseInfo.append("*** Time Units not Found\n"); } else { String units = axis.getUnitsString(); dateUnits = new DateUnit(units); calDateUnits = CalendarDateUnit.of(null, units); } }
Example #25
Source File: UF2RadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void setTimeUnits() throws Exception { for (CoordinateAxis axis : ds.getCoordinateAxes()) { if (axis.getAxisType() == AxisType.Time) { String units = axis.getUnitsString(); dateUnits = new DateUnit(units); calDateUnits = CalendarDateUnit.of(null, units); return; } } parseInfo.append("*** Time Units not Found\n"); }
Example #26
Source File: GradsTimeDimension.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Make a time struct from the index. * * @param timeIndex the time value index * * @return the corresponding TimeStruct */ public GradsTimeStruct makeTimeStruct(int timeIndex) { double tVal = getValues()[timeIndex]; Date d = DateUnit.getStandardDate(tVal + " " + getUnit()); Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); calendar.setTime(d); return makeTimeStruct(calendar); }
Example #27
Source File: UnidataObsDatasetHelper.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static Date getEndDate(NetcdfDataset ds) { Attribute att = ds.findGlobalAttributeIgnoreCase("time_coverage_end"); if (null == att) throw new IllegalArgumentException("Must have a time_coverage_end global attribute"); Date result; if (att.getDataType() == DataType.STRING) { result = DateUnit.getStandardOrISO(att.getStringValue()); } else { throw new IllegalArgumentException("time_coverage_end must be a ISO or udunit date string"); } return result; }
Example #28
Source File: MetadataExtractor.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 #29
Source File: Float10TrajectoryObsDataset.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static boolean isValidFile(NetcdfDataset ds) { // Check that has a time dimension and a trajectory dimension. List list = ds.getRootGroup().getDimensions(); if (list.size() != 2) return (false); Dimension d; for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } // Check that has a trajectory coordinate variable. Variable var = ds.getRootGroup().findVariableLocal(trajVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 1) return (false); d = (Dimension) list.get(0); if (!d.getShortName().equals(trajDimNameDefault)) return (false); // Check that has a time coordinate variable with units that are udunits time var = ds.getRootGroup().findVariableLocal(timeVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 1) return (false); d = (Dimension) list.get(0); if (!d.getShortName().equals(timeDimNameDefault)) return (false); String units = var.findAttribute("units").getStringValue(); Date date = DateUnit.getStandardDate("0 " + units); if (date == null) return (false); // Check for variable latitude(time) with units of "deg". var = ds.getRootGroup().findVariableLocal(latVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 2) return (false); for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } // units = var.findAttribute( "units").getStringValue(); // if ( ! SimpleUnit.isCompatible( units, "degrees_north")) return( false); // Check for variable longitude(time) with units of "deg". var = ds.getRootGroup().findVariableLocal(lonVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 2) return (false); for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } // units = var.findAttribute( "units").getStringValue(); // if ( ! SimpleUnit.isCompatible( units, "degrees_east")) return( false); // Check for variable altitude(time) with units of "m". var = ds.getRootGroup().findVariableLocal(elevVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 2) return (false); for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } units = var.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(units, "m")) return (false); return (true); }
Example #30
Source File: CFRadialAdapter.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected void setTimeUnits() throws Exception { Variable t = ds.findVariable("time"); String ut = t.getUnitsString(); dateUnits = new DateUnit(ut); calDateUnits = CalendarDateUnit.of(null, ut); }