Dimension ncDim = new Dimension(name, size, true);
ncFile.addDimension(null, ncDim);
if (name.equals(ENS_VAR)) {
v = new Variable(ncFile, null, null, name, DataType.STRING,
name);
v.addAttribute(new Attribute("standard_name", "ensemble"));
v.addAttribute(new Attribute(_Coordinate.AxisType,
AxisType.Ensemble.toString()));
List<String> names =
gradsDDF.getEnsembleDimension().getEnsembleNames();
String[] nameArray = new String[names.size()];
for (int i = 0; i < nameArray.length; i++) {
nameArray[i] = names.get(i);
}
Array dataArray = Array.factory(DataType.STRING,
new int[] { nameArray.length },
nameArray);
v.setCachedData(dataArray, false);
} else {
double[] vals = dim.getValues();
v = new Variable(ncFile, null, null, name, DataType.DOUBLE,
name);
v.addAttribute(new Attribute("units", dim.getUnit()));
if (name.equals(Y_VAR)) {
v.addAttribute(new Attribute("long_name", "latitude"));
v.addAttribute(new Attribute("standard_name",
"latitude"));
v.addAttribute(new Attribute("axis", "Y"));
sizeY = dim.getSize();
v.addAttribute(new Attribute(_Coordinate.AxisType,
AxisType.Lat.toString()));
} else if (name.equals(X_VAR)) {
v.addAttribute(new Attribute("long_name", "longitude"));
v.addAttribute(new Attribute("standard_name",
"longitude"));
v.addAttribute(new Attribute("axis", "X"));
v.addAttribute(new Attribute(_Coordinate.AxisType,
AxisType.Lon.toString()));
sizeX = dim.getSize();
} else if (name.equals(Z_VAR)) {
numZ = size;
zDims.put(name, ncDim);
v.addAttribute(new Attribute("long_name", "level"));
addZAttributes(dim, v);
} else if (name.equals(TIME_VAR)) {
v.addAttribute(new Attribute("long_name", "time"));
v.addAttribute(new Attribute(_Coordinate.AxisType,
AxisType.Time.toString()));
}
ArrayDouble.D1 varArray = new ArrayDouble.D1(size);
for (int i = 0; i < vals.length; i++) {
varArray.set(i, vals[i]);
}
v.setCachedData(varArray, false);
}
ncFile.addVariable(null, v);
}
if (numZ > 0) {
GradsDimension zDim = gradsDDF.getZDimension();
double[] vals = zDim.getValues();
for (GradsVariable var : vars) {
int nl = var.getNumLevels();
if ((nl > 0) && (nl != numZ)) {
String name = Z_VAR + nl;
if (zDims.get(name) == null) {
Dimension ncDim = new Dimension(name, nl, true);
ncFile.addDimension(null, ncDim);
Variable vz = new Variable(ncFile, null, null, name,
DataType.DOUBLE, name);
vz.addAttribute(new Attribute("long_name", name));
vz.addAttribute(new Attribute("units",
zDim.getUnit()));
addZAttributes(zDim, vz);
ArrayDouble.D1 varArray = new ArrayDouble.D1(nl);
for (int i = 0; i < nl; i++) {
varArray.set(i, vals[i]);
}
vz.setCachedData(varArray, false);
ncFile.addVariable(null, vz);
zDims.put(name, ncDim);
}
}
}
}
zDims = null;
for (GradsVariable var : vars) {
String coords = "latitude longitude";
int nl = var.getNumLevels();
if (nl > 0) {
if (nl == numZ) {
coords = "level " + coords;
} else {
coords = Z_VAR + nl + " " + coords;
}
}
coords = "time " + coords;
if (gradsDDF.getEnsembleDimension() != null) {
coords = "ensemble " + coords;
}
v = new Variable(ncFile, null, null, var.getName(),
DataType.FLOAT, coords);
v.addAttribute(new Attribute("long_name", var.getDescription()));
if (var.getUnitName() != null) {
v.addAttribute(new Attribute("units", var.getUnitName()));
}
v.addAttribute(
new Attribute(
"_FillValue", new Float(gradsDDF.getMissingValue())));
v.addAttribute(
new Attribute(
"missing_value", new Float(gradsDDF.getMissingValue())));
for (GradsAttribute attr : attrs) {
if (attr.getVariable().equalsIgnoreCase(var.getName())) {
// TODO: what to do about a UINT16/32
if (attr.getType().equalsIgnoreCase(
GradsAttribute.STRING)) {
v.addAttribute(new Attribute(attr.getName(),
attr.getValue()));
} else if (attr.getType().equalsIgnoreCase(
GradsAttribute.BYTE)) {
try {
v.addAttribute(new Attribute(attr.getName(),
new Byte(attr.getValue())));
} catch (NumberFormatException nfe) {}
} else if (attr.getType().equalsIgnoreCase(
GradsAttribute.INT16)) {
try {
v.addAttribute(new Attribute(attr.getName(),
new Short(attr.getValue())));
} catch (NumberFormatException nfe) {}
} else if (attr.getType().equalsIgnoreCase(
GradsAttribute.INT32)) {
try {
v.addAttribute(new Attribute(attr.getName(),
new Integer(attr.getValue())));
} catch (NumberFormatException nfe) {}
} else if (attr.getType().equalsIgnoreCase(
GradsAttribute.FLOAT32)) {
try {
v.addAttribute(new Attribute(attr.getName(),
new Float(attr.getValue())));
} catch (NumberFormatException nfe) {}
} else if (attr.getType().equalsIgnoreCase(
GradsAttribute.FLOAT64)) {
try {
v.addAttribute(new Attribute(attr.getName(),
new Double(attr.getValue())));
} catch (NumberFormatException nfe) {}
}
}
}
ncFile.addVariable(null, v);
}
// Global Attributes
ncFile.addAttribute(null, new Attribute("Conventions", "CF-1.0"));
ncFile.addAttribute(
null,
new Attribute(
"history",
"Direct read of GrADS binary grid into NetCDF-Java 4 API"));
String title = gradsDDF.getTitle();
if ((title != null) && !title.isEmpty()) {
ncFile.addAttribute(null, new Attribute("title", title));
}
for (GradsAttribute attr : attrs) {
if (attr.getVariable().equalsIgnoreCase(GradsAttribute.GLOBAL)) {
ncFile.addAttribute(null,
new Attribute(attr.getName(),
attr.getValue()));
}
}
}