import ucar.nc2.Variable; //导入方法依赖的package包/类
/**
* Add a Variable to the file. The data is also copied when finish() is called.
*
* @param var copy this Variable (not the data)
* @param varAttrToRemove variable attribute to remove
* @throws MotuException the motu exception
*/
public void writeVariable(Variable var, String[] varAttrToRemove) throws MotuException {
// Dimension[] dims = new Dimension[oldVar.getRank()];
List dims = new ArrayList();
List dimvList = var.getDimensions();
for (Dimension dim : dimvList) {
Dimension dimToWrite = dimHash.get(dim.getName());
if (dimToWrite == null) {
throw new MotuException(
ErrorType.NETCDF_VARIABLE,
String.format("Error in NetCdfWriter writeVariable - Variable %s - Dimension %s must be added first",
var.getName(),
dim.getName()));
}
dims.add(dimToWrite);
}
long d1 = System.nanoTime();
ncfile.addVariable(var.getName(), var.getDataType(), dims);
long d2 = System.nanoTime();
this.writingTime += (d2 - d1);
boolean removeAttr = false;
List attributeList = var.getAttributes();
for (Attribute attribute : attributeList) {
removeAttr = false;
if (varAttrToRemove != null) {
for (String attrToRemove : varAttrToRemove) {
if (attrToRemove.equalsIgnoreCase(attribute.getName())) {
removeAttr = true;
break;
}
}
}
if (!removeAttr) {
writeAttribute(var.getName(), attribute);
}
}
}