/** * Get CalendarDate from udunit date string * @param calendarName get Calendar from Calendar.get(calendarName). may be null * @param udunits must be value (space) udunits string * @return CalendarDate */ public static CalendarDate parseUdunits(String calendarName, String udunits) { int pos = udunits.indexOf(' '); if (pos < 0) return null; String valString = udunits.substring(0, pos).trim(); String unitString = udunits.substring(pos+1).trim(); CalendarDateUnit cdu = CalendarDateUnit.of(calendarName, unitString); double val = Double.parseDouble(valString); return cdu.makeCalendarDate(val); }
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); }
public TimeCoord(int code, String udunitString, List coords) { this.code = code; this.units = udunitString; CalendarDateUnit cdu = CalendarDateUnit.of(null, udunitString); this.runDate = cdu.getBaseCalendarDate(); this.calendarPeriod = cdu.getCalendarPeriod(); Object atom = (coords.size() > 0) ? coords.get(0) : null; if (atom instanceof Tinv) { this.coords = null; this.intervals = coords; } else { this.coords = coords; this.intervals = null; } }
public TimeHelper setReferenceDate(CalendarDate refDate) { CalendarDateUnit cdUnit = CalendarDateUnit.of(dateUnit.getCalendar(), dateUnit.getCalendarField(), refDate); return new TimeHelper(cdUnit); }
public CalendarDate makeCalendarDateFromOffset(double offset) { return dateUnit.makeCalendarDate(offset); }
public static void main(String[] args) { CalendarDateUnit cdu; String s = "calendar Month since 2012-01-19T18:00:00.000Z"; cdu = CalendarDateUnit.of(null, s); System.out.printf("%s == %s%n", s, cdu); }
@Test public void testZeroYear() { double[] times = new double[] {366.0, 1096.485, 1826.97, 2557.455, 3287.94, 4018.425, 4748.91, 5479.395, 6209.88, 6940.365, 7670.85, 8401.335}; CalendarDateUnit unit = CalendarDateUnit.withCalendar(null, "hour since 0000-01-01 00:00:00"); for (double time : times) { CalendarDate cd1 = unit.makeCalendarDate(time); System.out.printf("%s with Calendar %s%n", cd1, null); } // year0 fails in gregorian. kluge in CalendarDateFormatter CalendarDateUnit unit2 = CalendarDateUnit.withCalendar(Calendar.gregorian, "hour since 0000-01-01 00:00:00"); assert unit2.getCalendar() == Calendar.proleptic_gregorian; }
public CoordinateAxisTimeHelper(Calendar calendar, String unitString) { this.calendar = calendar; if (unitString == null) { this.dateUnit = null; return; } this.dateUnit = CalendarDateUnit.withCalendar(calendar, unitString); // this will throw exception on failure }
private Element writeTimeUnit(CalendarDateUnit dateUnit) { Element elem = new Element("TimeUnit"); elem.addContent(dateUnit.getUdUnit()); elem.setAttribute("calendar", dateUnit.getCalendar().toString()); return elem; }
@Test public void testBase() throws Exception { Date base = getBase(datestring); CalendarDateUnit cdu = CalendarDateUnit.of(calendar, datestring); Assert.assertEquals("Difference (ms): " + (base.getTime() - cdu.getBaseDate().getTime()), cdu.getBaseDate(), base); Assert.assertEquals("Difference (ms): " + (CalendarDate.of(base).getMillis() - cdu.getBaseCalendarDate().getMillis()), cdu.getBaseCalendarDate(), CalendarDate.of(base)); }
@Test public void testBestOffsetGE() throws IOException { FeatureCollectionConfig config = new FeatureCollectionConfig("espresso_2013_da_his_fmrc_offset_with_Offset_in_filename", "DavidRobertson", FeatureCollectionType.FMRC, TestDir.cdmUnitTestDir + "ft/fmrc/espresso/.*.nc$", null, "#espresso_his_#yyyyMMdd_HHmm", null, null, null); config.fmrcConfig.regularize=false; config.fmrcConfig.addBestDataset("Best", 26); Formatter errlog = new Formatter(); Fmrc fmrc = Fmrc.open(config, errlog); assert (fmrc != null) : errlog; FeatureCollectionConfig.BestDataset bd = new FeatureCollectionConfig.BestDataset("Best", 26); try (ucar.nc2.dt.GridDataset gridDs = fmrc.getDatasetBest(bd)) { GridDatatype grid = gridDs.findGridByShortName("salt"); GridCoordSystem gcs = grid.getCoordinateSystem(); CoordinateAxis1D timeAxis = gcs.getTimeAxis1D(); logger.debug("timeAxis = {} {}", NCdumpW.toString(timeAxis.read()), timeAxis.getUnitsString()); CoordinateAxis1D runAxis = gcs.getRunTimeAxis(); logger.debug("runAxis = {} {}", NCdumpW.toString( runAxis.read()), runAxis.getUnitsString()); CalendarDate expected = CalendarDate.parseISOformat(null, "2013-05-05T00:00:00"); CalendarDateUnit cdu = CalendarDateUnit.of(null, timeAxis.getUnitsString()); Assert.assertEquals(expected, cdu.getBaseCalendarDate()); CalendarDateUnit cdu2 = CalendarDateUnit.of(null, runAxis.getUnitsString()); Assert.assertEquals(expected, cdu2.getBaseCalendarDate()); for (int i=0; i<runAxis.getSize(); i++) { Assert.assertEquals("run coord", i<12 ? 0 : 24, runAxis.getCoordValue(i), 1.0e-8); } } }
try { this.altUnits = pfc.getAltUnit().length() > 0 ? pfc.getAltUnit() : null; this.timeUnit = CalendarDateUnit.of(null, pfc.getTimeUnit()); } catch (IllegalArgumentException e) { String message = String.format("Invalid time unit found in stream (%s). Using default (%s).", pfc.getTimeUnit(), this.timeUnit.getUdUnit()); logger.error(message, e);
if (unit == null) throw new IllegalStateException("Null units"); CalendarDateUnit cdUnit = CalendarDateUnit.of(null, unit); return new CoordinateRuntime(pc.getMsecsList(), cdUnit.getCalendarPeriod());
/** * Create a CalendarDateUnit from a calendar, a CalendarPeriod.Field, and a base date * @param calt use this Calendar, or null for default calendar * @param periodField a CalendarPeriod.Field like Hour or second * @param baseDate "since baseDate" * @return CalendarDateUnit */ static public CalendarDateUnit of(Calendar calt, CalendarPeriod.Field periodField, CalendarDate baseDate) { if (calt == null) calt = Calendar.getDefault(); return new CalendarDateUnit(calt, periodField, baseDate); }
public CalendarDate getRefDate() { return dateUnit.getBaseCalendarDate(); }
public Calendar getCalendar() { return dateUnit.getCalendar(); }
public CalendarDate makeDateInTimeUnits(CalendarDate start, double addTo) { return start.add(CalendarPeriod.of((int) addTo, dateUnit.getCalendarField())); }
public CalendarDate makeCalendarDateFromOffset(double offset) { return dateUnit.makeCalendarDate(offset); }
public static void main(String[] args) { CalendarDateUnit cdu; String s = "calendar Month since 2012-01-19T18:00:00.000Z"; cdu = CalendarDateUnit.of(null, s); System.out.printf("%s == %s%n", s, cdu); }
private void testCalendar(Calendar cal, String s) { CalendarDateUnit cdu; try { cdu = CalendarDateUnit.withCalendar(cal, s); } catch (Exception e) { e.printStackTrace(); return; } System.out.printf("%s == %s unit (cal=%s)%n", s, cdu, cdu.getCalendar()); CalendarDate base = null; for (int i = 0; i < 13; i++) { CalendarDate cd = cdu.makeCalendarDate(i); if (base == null) base = cd; double diff = cd.getDifferenceInMsecs(base) * 1.0e-6; System.out.printf(" %d %s == %s diff = %f%n", i, cdu, CalendarDateFormatter.toDateTimeStringISO(cd), diff); } System.out.printf("%n"); }