long computeFieldSize(BaseType bt, boolean isAscii) throws Exception { long fieldsize = 0; // Figure out what this field is (e.g. primitive or not) // Somewhat convoluted. if (bt instanceof DConstructor) { // simple struct, seq, or grid => recurse fieldsize = computeSize((DConstructor) bt, isAscii); } else if (bt instanceof DArray) { SDArray da = (SDArray) bt; // Separate structure arrays from primitive arrays if (da.getContainerVar() instanceof DPrimitive) { fieldsize = computeArraySize(da); } else if (da.getContainerVar() instanceof DStructure) { fieldsize = computeSize((DStructure) da.getContainerVar(), isAscii); // recurse } else { // Some kind of problem throw new NoSuchTypeException("Computesize: unexpected type for " + bt.getLongName()); } } else if (bt instanceof DPrimitive) { DPrimitive dp = (DPrimitive) bt; if (dp instanceof DString) { String v = ((DString) dp).getValue(); fieldsize = (v == null ? 0 : v.length()); } else { DataType dtype = DODSNetcdfFile.convertToNCType(bt); fieldsize = dtype.getSize(); } } else { // Some kind of problem throw new NoSuchTypeException("Computesize: unknown type for " + bt.getLongName()); } return fieldsize; }
// Recursively compute size of the dds to be returned private long computeSize(DConstructor ctor, boolean isAscii) throws Exception { long projectsize = 0; // accumulate size of projected variables long othersize = 0; // accumulate size of non-projected variables long fieldsize = 0; int projectedcount = 0; int fieldcount = 0; Enumeration vars = ctor.getVariables(); while (vars.hasMoreElements()) { fieldcount++; BaseType field = (BaseType) vars.nextElement(); fieldsize = computeFieldSize(field, isAscii); // accumulate the field sizes if (field.isProject()) { projectsize += fieldsize; projectedcount++; } else { othersize += fieldsize; } } // Cases to consider: // 1. If all of the fields of this ctor are projected, // then return projectsize // 2. If none of the fields of this ctor are projected, // then return othersize // 3. otherwise, at least one field, but not all, is projected, // => return projectsize; if (projectedcount == fieldcount) return projectsize; else if (projectedcount == 0) return othersize; else { assert (projectedcount > 0 && projectedcount < fieldcount); return projectsize; } }