@@ -98,6 +98,59 @@ const std::string& ugrid_writer::store_path() const
9898 return _store_path;
9999}
100100
101+ size_t ugrid_writer::probe_time_index ()
102+ {
103+ if (!store_exists ())
104+ {
105+ return 0 ;
106+ }
107+
108+ // Open the existing ugrid file read-only to determine how many timesteps are present.
109+ int fid = -1 ;
110+ MPI_Info info_used;
111+ MPI_Comm_get_info (_comm_world, &info_used);
112+
113+ nc_chk_ret (nc_open_par (_fname.c_str (), NC_NOWRITE, _comm_world, info_used, &fid));
114+ size_t time_len = read_time_index (fid);
115+
116+ nc_chk_ret (nc_close (fid));
117+ MPI_Info_free (&info_used);
118+
119+ return time_len;
120+ }
121+
122+ size_t ugrid_writer::read_time_index (int fid) const
123+ {
124+ int unlimdimidp;
125+ nc_chk_ret (nc_inq_unlimdim (fid, &unlimdimidp));
126+
127+ size_t time_len = 0 ;
128+ nc_chk_ret (nc_inq_dimlen (fid, unlimdimidp, &time_len));
129+
130+ if (_global->from_checkpoint () && time_len > 0 )
131+ {
132+ // Trim to the restart time so we overwrite any outputs past the checkpoint.
133+ int time_varid = -1 ;
134+ int status = nc_inq_varid (fid, " time" , &time_varid);
135+ if (status == NC_NOERR)
136+ {
137+ std::vector<double > time_vals (time_len, 0.0 );
138+ nc_chk_ret (nc_get_var_double (fid, time_varid, time_vals.data ()));
139+
140+ const double restart_time_minutes = _global->posix_time_double () / 60.0 ;
141+ auto it = std::lower_bound (time_vals.begin (), time_vals.end (), restart_time_minutes);
142+ auto idx = static_cast <size_t >(std::distance (time_vals.begin (), it));
143+
144+ if (idx < time_len)
145+ {
146+ time_len = idx;
147+ }
148+ }
149+ }
150+
151+ return time_len;
152+ }
153+
101154std::string ugrid_writer::build_store_uri (const std::string& store_path) const
102155{
103156 if (!_use_zarr)
@@ -394,30 +447,7 @@ void ugrid_writer::open_ugrid(const std::vector<std::string>& output_variables)
394447 nc_chk_ret (nc_var_par_access (_ugrid_fid, p.second , NC_COLLECTIVE));
395448 }
396449
397- int unlimdimidp;
398-
399- nc_chk_ret (nc_inq_unlimdim (_ugrid_fid, &unlimdimidp)); // get the time /dimension/. it's the only unlimited
400- nc_chk_ret (nc_inq_dimlen (_ugrid_fid, unlimdimidp, &_time_index));
401-
402- // If we are resuming from a checkpoint, make sure we overwrite any timesteps
403- // beyond the checkpoint instead of blindly appending to the existing file.
404- if (_global->from_checkpoint () && _time_index > 0 )
405- {
406- // chm outputs minutes since as time unit
407- const double restart_time_minutes = _global->posix_time_double () / 60.0 ;
408- std::vector<double > time_vals (_time_index, 0.0 );
409-
410- nc_chk_ret (nc_get_var_double (_ugrid_fid, _ugrid_id_var[" time" ], time_vals.data ()));
411-
412- auto it = std::lower_bound (time_vals.begin (), time_vals.end (), restart_time_minutes);
413- auto idx = static_cast <size_t >(std::distance (time_vals.begin (), it));
414-
415- if (idx < _time_index)
416- {
417- SPDLOG_DEBUG (" Resuming ugrid at time index {} (was {})." , idx, _time_index);
418- _time_index = idx;
419- }
420- }
450+ _time_index = read_time_index (_ugrid_fid);
421451
422452 SPDLOG_DEBUG (" Existing ugrid output has {} timesteps already" , _time_index);
423453
0 commit comments