Close netcdf files on errors

Fixes two functions leaving netcdf files open on errors.