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");
newV.addAttribute(new Attribute("coordinates", sbuff.toString()));
// looking for coordinate transform variables
for (CoordinateTransform ct : gcs.getCoordinateTransforms()) {
Variable v = ncd.findVariable(ct.getName());
if (ct.getTransformType() == TransformType.Projection)
newV.addAttribute(new Attribute("grid_mapping", v.getFullName()));
}
}
for (CoordinateAxis axis : axisList) {
Variable newV = root.findVariable(axis.getShortName()); // LOOK ???
if ((axis.getAxisType() == AxisType.Height) || (axis.getAxisType() == AxisType.Pressure) || (axis.getAxisType() == AxisType.GeoZ)) {
if (null != axis.getPositive())
newV.addAttribute(new Attribute("positive", axis.getPositive()));
}
if (axis.getAxisType() == AxisType.Lat) {
newV.addAttribute(new Attribute("units", "degrees_north"));
newV.addAttribute(new Attribute("standard_name", "latitude"));
}
if (axis.getAxisType() == AxisType.Lon) {
newV.addAttribute(new Attribute("units", "degrees_east"));
newV.addAttribute(new Attribute("standard_name", "longitude"));
}
if (axis.getAxisType() == AxisType.GeoX) {
newV.addAttribute(new Attribute("standard_name", "projection_x_coordinate"));
}
if (axis.getAxisType() == AxisType.GeoY) {
newV.addAttribute(new Attribute("standard_name", "projection_y_coordinate"));
}
}
// coordinate transform variables : must convert false easting, northing to km
List<Variable> ctvList = new ArrayList<Variable>();
for (ucar.nc2.dt.GridDataset.Gridset gridSet : gds.getGridsets()) {
ucar.nc2.dt.GridCoordSystem gcs = gridSet.getGeoCoordSystem();
ProjectionCT pct = gcs.getProjectionCT();
if (pct != null) {
Variable v = root.findVariable(pct.getName()); // look for the ctv
if ((v != null) && !ctvList.contains(v)) {
convertProjectionCTV((NetcdfDataset) gds.getNetcdfFile(), v);
ctvList.add(v);