def write(self):
"""
Write all cached states to the NetCDF file, and clear the cache.
This will append to any existing NetCDF file.
Raises
------
InvalidStateError
If cached states do not all have the same quantities
as every other cached and written state.
"""
with nc4.Dataset(self._filename, self._write_mode) as dataset:
self._ensure_cached_state_keys_compatible_with_dataset(dataset)
time_list, state_list = self._get_ordered_times_and_states()
self._ensure_time_exists(dataset, time_list[0])
it_start = dataset.dimensions['time'].size
it_end = it_start + len(time_list)
append_times_to_dataset(time_list, dataset, self._time_units)
all_states = combine_states(state_list)
for name, value in all_states.items():
ensure_variable_exists(dataset, name, value)
dataset.variables[name][
it_start:it_end, :] = value.values[:, :]
self._cached_state_dict = {}
评论列表
文章目录