Skip to content

Commit

Permalink
add check size to builder to fail faster
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyajohnson committed Jul 13, 2023
1 parent 59973b6 commit 3e011eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ private Result writeFile(CoverageCollection gdsOrg, List<String> gridNames, Subs
}
addCFAnnotations(subsetDataset, rootGroup, shouldAddLatLon2D);

// test if its too large
long totalSizeOfVars = writer.calcSize();
if (maxBytes > 0 && totalSizeOfVars > maxBytes) {
return Result.create(totalSizeOfVars, false, TOO_LARGE_MESSAGE);
}

// Actually create file and write variable data to it.
try (NetcdfFormatWriter ncwriter = writer.build()) {
// test if its too large
long totalSizeOfVars = ncwriter.calcSize();
if (maxBytes > 0 && totalSizeOfVars > maxBytes) {
return Result.create(totalSizeOfVars, false, TOO_LARGE_MESSAGE);
}

writeCoordinateData(subsetDataset, ncwriter);
writeCoverageData(gdsOrg, subsetParams, subsetDataset, ncwriter);

Expand Down
15 changes: 15 additions & 0 deletions cdm/core/src/main/java/ucar/nc2/write/NetcdfFormatWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ public Structure.Builder addStructure(String shortName, String dimString) {
rootGroup.addVariable(vb);
return vb;
}
public long calcSize() {
return calcSize(this.rootGroup);
}

// Note that we have enough info to try to estimate effects of compression, if its a Netcdf4 file.
private long calcSize(Group.Builder group) {
long totalSizeOfVars = 0;
for (Variable.Builder var : this.rootGroup.vbuilders) {
totalSizeOfVars += Dimensions.getSize(var.getDimensions()) * var.getElementSize();
}
for (Group.Builder nested : group.gbuilders) {
totalSizeOfVars += calcSize(nested);
}
return totalSizeOfVars;
}

/** Once this is called, do not use the Builder again. */
public NetcdfFormatWriter build() throws IOException {
Expand Down

0 comments on commit 3e011eb

Please sign in to comment.