Close open netcdf files

3 次查看(过去 30 天)
Kelly Kearney
Kelly Kearney 2014-7-3
评论: Cedric 2017-10-5
Short question:
Is there a way to explicitly close all open netcdf files, similar to
fclose('all')
for open binary files?
Longer explanation:
I'm currently running a series of simulations where data is saved time slice by time slice to netcdf files as the model integrates forward over time. Opening and closing files before and after each write is way too time-consuming, so I open all the files at the beginning of the simulation and close them at the end. No issues if everything runs smoothly, but if a simulation crashes mid-integration (which happens a few times per 100 simulations, due to numerical issues that are difficult to prevent) I'm left with a few hundred open file handles.
I currently deal with this via a lot of try/catch blocks in various parts of my code, and an onCleanup object, but I still once in a blue moon end up with some orphaned open files with no accessible NetCDF ID to use to close them (running a few hundred simulations in parallel, so it can be difficult to track down why these few instances escape my traps). At that point, the only way to solve the issue is a
clear all
which of course clears away much more than necessary (and also isn't really documented). Doing so leads to a warning message
Warning: Closing open netCDF file handles.
which seems to imply that there is a way, under the hood of clear, to recognize the presence of open file handles, and close them. Is that available without the accompanying wiping out of everything else in the workspace?
  6 个评论
David Ahrens
David Ahrens 2017-10-5
编辑:David Ahrens 2017-10-5
Found that
clear mex
Works at closing left over file handles from netcdf calls.
Cedric
Cedric 2017-10-5
Thanks David! I'm saving this thread with the feeling that it will be useful once.

请先登录,再进行评论。

回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by