String timeCoordName = "time";
Variable taxis = new Variable(ncfile, null, null, timeCoordName);
taxis.setDataType(DataType.DOUBLE);
taxis.setDimensions("time");
taxis.addAttribute(new Attribute("long_name", "time since base date"));
taxis.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
double[] tdata = new double[1];
tdata[0] = dd.getTime();
Array dataT = Array.factory(DataType.DOUBLE.getPrimitiveClassType(), new int[] { 1 }, tdata);
taxis.setCachedData(dataT, false);
DateFormatter formatter = new DateFormatter();
taxis.addAttribute(new Attribute("units", "msecs since " + formatter.toDateTimeStringISO(new Date(0))));
ncfile.addVariable(null, taxis);
dims.add(dimT);
Dimension jDim = new Dimension("y", numY, true, false, false);
Dimension iDim = new Dimension("x", numX, true, false, false);
dims.add(jDim);
dims.add(iDim);
ncfile.addDimension(null, iDim);
ncfile.addDimension(null, jDim);
ncfile.addAttribute(null, new Attribute("cdm_data_type", FeatureType.GRID.toString()));
String coordinates = "time y x";
Variable v = new Variable(ncfile, null, null, cname);
v.setDataType(DataType.BYTE);
v.setDimensions(dims);
ncfile.addVariable(null, v);
v.addAttribute(new Attribute("long_name", ctitle));
v.addAttribute(new Attribute("units", cunit));
v.setSPobject(new Vinfo(numX, numY, hoff, false));
v.addAttribute(new Attribute(_Coordinate.Axes, coordinates));
// create coordinate variables
Variable xaxis = new Variable(ncfile, null, null, "x");
xaxis.setDataType(DataType.DOUBLE);
xaxis.setDimensions("x");
xaxis.addAttribute(new Attribute("standard_name", "projection x coordinate"));
xaxis.addAttribute(new Attribute("units", "km"));
xaxis.addAttribute(new Attribute(_Coordinate.AxisType, "GeoX"));
double[] data1 = new double[numX];
ProjectionImpl projection = new LambertConformal(clat, clon, lat1, lat2);
double ullat = 51.8294;
double ullon = -135.8736;
double lrlat = 17.2454;
double lrlon = -70.1154;
ProjectionPointImpl ptul = (ProjectionPointImpl) projection.latLonToProj(new LatLonPointImpl(ullat, ullon));
ProjectionPointImpl ptlr = (ProjectionPointImpl) projection.latLonToProj(new LatLonPointImpl(lrlat, lrlon));
ProjectionPointImpl ptc = (ProjectionPointImpl) projection.latLonToProj(new LatLonPointImpl(clat, clon));
double startX = ptul.getX();
double startY = ptlr.getY();
double dx = (ptlr.getX() - ptul.getX())/(numX-1);
for (int i = 0; i < numX; i++) {
data1[i] = startX + i*dx;
}
Array dataA = Array.factory(DataType.DOUBLE.getPrimitiveClassType(), new int[] { numX }, data1);
xaxis.setCachedData(dataA, false);
ncfile.addVariable(null, xaxis);
Variable yaxis = new Variable(ncfile, null, null, "y");
yaxis.setDataType(DataType.DOUBLE);
yaxis.setDimensions("y");
yaxis.addAttribute(new Attribute("standard_name", "projection y coordinate"));
yaxis.addAttribute(new Attribute("units", "km"));
yaxis.addAttribute(new Attribute(_Coordinate.AxisType, "GeoY"));
data1 = new double[numY];
double dy = (ptul.getY() - ptlr.getY())/(numY-1);
for (int i = 0; i < numY; i++) {
data1[i] = startY + i*dy;
}
dataA = Array.factory(DataType.DOUBLE.getPrimitiveClassType(), new int[] { numY }, data1);
yaxis.setCachedData(dataA, false);
ncfile.addVariable(null, yaxis);
// projection
// lower left and upper right corner lat/lons
// modified cylind. equidistant or CED with lat/lon ration != 1
Variable ct = new Variable(ncfile, null, null, projection.getClassName());
ct.setDataType(DataType.CHAR);
ct.setDimensions("");
List params = projection.getProjectionParameters();
for (int i = 0; i < params.size(); i++) {
Parameter p = (Parameter) params.get(i);
ct.addAttribute(new Attribute(p));
}
ct.addAttribute(new Attribute(_Coordinate.TransformType, "Projection"));
//ct.addAttribute(new Attribute(_Coordinate.Axes, "lat lon"));
ct.addAttribute( new Attribute(_Coordinate.Axes, "x y "));
// fake data
dataA = Array.factory(DataType.CHAR.getPrimitiveClassType(), new int[] {});
dataA.setChar(dataA.getIndex(), ' ');
ct.setCachedData(dataA, false);
ncfile.addVariable(null, ct);