int ncoords = (int) org.getSize();
timeDates = new Date[ncoords];
// see if it has a valid udunits unit
DateUnit dateUnit = null;
String units = org.getUnitsString();
if (units != null) {
try {
dateUnit = new DateUnit(units);
} catch (Exception e) {
// not a date unit - ok to fall through
}
}
// has a valid date unit - read data
if (dateUnit != null) {
Array data = org.read();
int count = 0;
IndexIterator ii = data.getIndexIterator();
for (int i = 0; i < ncoords; i++) {
double val = ii.getDoubleNext();
if (Double.isNaN(val)) continue;
Date d = dateUnit.makeDate(val);
timeDates[count++] = d;
}
// if we encountered NaNs, shorten it up
if (count != ncoords) {
Dimension localDim = new Dimension(getShortName(), count, false);
setDimension(0, localDim);
// set the shortened values
Array shortData = Array.factory(data.getElementType(), new int[]{count});
Index ima = shortData.getIndex();
int count2 = 0;
ii = data.getIndexIterator();
for (int i = 0; i < ncoords; i++) {
double val = ii.getDoubleNext();
if (Double.isNaN(val)) continue;
shortData.setDouble(ima.set0(count2), val);
count2++;
}
// here we have to decouple from the original variable
cache = new Cache();
setCachedData(shortData, true);
// shorten up the timeDate array
Date[] keep = timeDates;
timeDates = new Date[count];
System.arraycopy(keep, 0, timeDates, 0, timeDates.length);
}
return;
} // has valid date unit
// see if its a String, and if we can parse the values as an ISO date
if (org.getDataType() == DataType.STRING) {
ArrayObject data = (ArrayObject) org.read();
IndexIterator ii = data.getIndexIterator();
for (int i = 0; i < ncoords; i++) {
String coordValue = (String) ii.getObjectNext();
Date d = DateUnit.getStandardOrISO(coordValue);
if (d == null) {
if (errMessages != null)
errMessages.format("DateUnit cannot parse String= %s\n", coordValue);
else
System.out.println("DateUnit cannot parse String= " + coordValue + "\n");
throw new IllegalArgumentException();
} else {
timeDates[i] = d;
}
}
return;
}
// hack something in here so it doesnt fail
if (units != null) {
try {
// if in time unit, use CF convention "since 1-1-1 0:0:0"
dateUnit = new DateUnit(units+" since 0001-01-01 00:00:00");
} catch (Exception e) {
try {
if (errMessages != null)
errMessages.format("Time Coordinate must be udunits or ISO String: hack since 0001-01-01 00:00:00\n");
else
System.out.println("Time Coordinate must be udunits or ISO String: hack since 0001-01-01 00:00:00\n");
dateUnit = new DateUnit("secs since 0001-01-01 00:00:00");
} catch (Exception e1) {
// cant happpen
}
}
}
Array data = org.read();
IndexIterator ii = data.getIndexIterator();
for (int i = 0; i < ncoords; i++) {
double val = ii.getDoubleNext();
Date d = dateUnit.makeDate(val);
timeDates[i] = d;
}
}