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 }
public CoordinateAxisTimeHelper(Calendar calendar, String unitString) { this.calendar = calendar; this.dateUnit = (unitString == null) ? null : CalendarDateUnit.withCalendar(calendar, unitString); // this will throw exception on failure }
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); }
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); }
static public TimeHelper factory(String units, AttributeContainer atts) { if (units == null) units = atts.findAttValueIgnoreCase(CDM.UDUNITS, null); if (units == null) units = atts.findAttValueIgnoreCase(CDM.UNITS, null); if (units == null) throw new IllegalStateException("No units"); Calendar cal = getCalendarFromAttribute(atts); CalendarDateUnit dateUnit; try { dateUnit = CalendarDateUnit.withCalendar(cal, units); // this will throw exception on failure return new TimeHelper( dateUnit); } catch (IllegalArgumentException e) { return null; } }
@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; }
@Override public CalendarDateRange makeCalendarDateRange(ucar.nc2.time.Calendar cal) { CalendarDateUnit cdu = CalendarDateUnit.withCalendar(cal, periodName + " since " + refDate.toString()); CalendarDate start = cdu.makeCalendarDate(timeUnit.getValue() * offsetSorted.get(0)); CalendarDate end = cdu.makeCalendarDate(timeUnit.getValue() * offsetSorted.get(getSize() - 1)); return CalendarDateRange.of(start, end); }
@Test public void test360bug() { CalendarDateUnit unit = CalendarDateUnit.withCalendar(Calendar.uniform30day, "days since 2289-12-1"); CalendarDate cd1 = unit.makeCalendarDate(25200.0); // 70 years = 2359-12-1 CalendarDate cd2 = unit.makeCalendarDate(46800.0); // 130 = 2419-12-1 System.out.printf("%s%n", unit); System.out.printf("%s%n", cd1); System.out.printf("%s%n", cd2); assert cd1.toString().equals("2359-12-01T00:00:00Z"); assert cd2.toString().equals("2419-12-01T00:00:00Z"); }
@Test public void testNemoDate() { for (Calendar cal : Calendar.values()) { CalendarDateUnit unit = CalendarDateUnit.withCalendar(cal, "days since -4713-01-01T00:00:00Z"); CalendarDate cd1 = unit.makeCalendarDate(2434567); System.out.printf("%s with Calendar %s%n", cd1, cal); } }
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"); }
timeUnit = CalendarDateUnit.withCalendar(cal, timeUnitString);
CalendarDateUnit dateUnit = CalendarDateUnit.withCalendar(calendar, getUnitsString()); // this will throw exception on failure
@Test public void checkNCSSDatesInNetcdf() throws JDOMException, IOException { String endpoint = TestOnLocalServer.withHttpPath("/ncss/grid/cdmUnitTest/ncss/climatology/PF5_SST_Climatology_Monthly_1985_2001.nc?var=sst&latitude=45&longitude=-20&temporal=all&accept=netcdf"); byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.netcdf); NetcdfFile nf = NetcdfFile.openInMemory("test_data.ncs", result); NetcdfDataset ds = new NetcdfDataset(nf); CoordinateAxis1D tAxis = (CoordinateAxis1D) ds.findCoordinateAxis("time"); Attribute calendarAtt = tAxis.findAttribute(CF.CALENDAR); Calendar calendar; if (calendarAtt == null) { calendar = Calendar.getDefault(); } else { calendar = Calendar.get(calendarAtt.getStringValue()); } Attribute units = tAxis.findAttribute(CDM.UNITS); double[] values = tAxis.getCoordValues(); System.out.printf("actual%n"); List<String> ccdd = new ArrayList<>(); CalendarDateUnit dateUnit = CalendarDateUnit.withCalendar(calendar, units.getStringValue()); for (double value : values) { CalendarDate cd = dateUnit.makeCalendarDate(value); System.out.printf(" \"%s\",%n", cd); ccdd.add(cd.toString()); } assertEquals(expectedDatesAsDateTime, ccdd); //FAIL!!! ??? //CoordinateAxis1DTime tAxis2 = CoordinateAxis1DTime.factory(ds, ds.findCoordinateAxis("time") , null); //assertTrue(tAxis2.getCalendarDates().equals(expectedDatesAsDateTime)); }