if (varNameList.contains(gridName))
continue;
varNameList.add(gridName);
GridDatatype grid = gds.findGridDatatype(gridName);
GridCoordSystem gcsOrg = grid.getCoordinateSystem();
CoordinateAxis1DTime timeAxis = gcsOrg.getTimeAxis1D();
// make subset if needed
Range timeRange = null;
if ((dateRange != null) && (timeAxis != null)) {
int startIndex = timeAxis.findTimeIndexFromDate(dateRange.getStart().getDate());
int endIndex = timeAxis.findTimeIndexFromDate(dateRange.getEnd().getDate());
if (startIndex < 0)
throw new InvalidRangeException("start time=" + dateRange.getStart().getDate() + " must be >= " + timeAxis.getTimeDate(0));
if (endIndex < 0)
throw new InvalidRangeException("end time=" + dateRange.getEnd().getDate() + " must be >= " + timeAxis.getTimeDate(0));
timeRange = new Range(startIndex, endIndex);
}
if ((null != timeRange) || (zRange != null) || (llbb != null) || (horizStride > 1)) {
grid = grid.makeSubset(timeRange, zRange, llbb, 1, horizStride, horizStride);
}
Variable gridV = (Variable) grid.getVariable();
varList.add(gridV);
total_size += gridV.getSize() * gridV.getElementSize();
// add coordinate axes
GridCoordSystem gcs = grid.getCoordinateSystem();
for (CoordinateAxis axis : gcs.getCoordinateAxes()) {
if (!varNameList.contains(axis.getFullName())) {
varNameList.add(axis.getFullName());
varList.add(axis); // LOOK dont we have to subset these ??
axisList.add(axis);
}
}
// add coordinate transform variables
for (CoordinateTransform ct : gcs.getCoordinateTransforms()) {
Variable v = ncd.findVariable(ct.getName());
if (!varNameList.contains(ct.getName()) && (null != v)) {
varNameList.add(ct.getName());
varList.add(v);
}
}
// optional lat/lon
if (addLatLon) {
Projection proj = gcs.getProjection();
if ((null != proj) && !(proj instanceof LatLonProjection)) {
addLatLon2D(ncd, varList, proj, gcs.getXHorizAxis(), gcs.getYHorizAxis());
addLatLon = false;
}
}
}
// check size is ok
boolean isLargeFile = false;
long maxSize = 2 * 1000 * 1000 * 1000;
if (total_size > maxSize) {
log.info("Request size = {} Mbytes", total_size / 1000 / 1000);
isLargeFile = true;
}
FileWriter writer = new FileWriter(location, false, isLargeFile, -1);
// global attributes
for (Attribute att : gds.getGlobalAttributes())
writer.writeGlobalAttribute(att);
writer.writeGlobalAttribute(new Attribute("Conventions", "CF-1.0"));
writer.writeGlobalAttribute(new Attribute("History",
"Translated to CF-1.0 Conventions by Netcdf-Java CDM (NetcdfCFWriter)\n" +
"Original Dataset = " + gds.getLocationURI() + "; Translation Date = " + new Date()));
writer.writeVariables(varList);
// now add CF annotations as needed - dont change original ncd or gds
NetcdfFileWriteable ncfile = writer.getNetcdf();
Group root = ncfile.getRootGroup();
for (String gridName : gridList) {
GridDatatype grid = gds.findGridDatatype(gridName);
Variable newV = root.findVariable(gridName);
if (newV == null) {
log.warn("NetcdfCFWriter cant find "+gridName+" in gds "+gds.getLocationURI());
continue;
}
// annotate Variable for CF
StringBuilder sbuff = new StringBuilder();
GridCoordSystem gcs = grid.getCoordinateSystem();
for (Variable axis : gcs.getCoordinateAxes()) {
sbuff.append(axis.getFullName()).append(" ");
}
if (addLatLon)
sbuff.append("lat lon");