diff --git a/conda_package/mpas_tools/viz/mpas_to_xdmf/io.py b/conda_package/mpas_tools/viz/mpas_to_xdmf/io.py index 25564948f..2a65ce1e4 100644 --- a/conda_package/mpas_tools/viz/mpas_to_xdmf/io.py +++ b/conda_package/mpas_tools/viz/mpas_to_xdmf/io.py @@ -71,6 +71,8 @@ def _load_dataset(mesh_filename, time_series_filenames, variables, xtime_var): data_vars='minimal', coords='minimal', compat='override', + decode_times=False, + decode_timedelta=False, ) ds_mesh = _get_ds_mesh(ds_mesh) @@ -91,6 +93,8 @@ def _load_dataset(mesh_filename, time_series_filenames, variables, xtime_var): ) else: selected_vars.add(var) + if xtime_var is not None: + selected_vars.add(xtime_var) ds = ds[list(selected_vars)] _set_time(ds=ds, xtime_var=xtime_var) diff --git a/conda_package/mpas_tools/viz/mpas_to_xdmf/templates/xdmf_template.xml b/conda_package/mpas_tools/viz/mpas_to_xdmf/templates/xdmf_template.xml index 6e05ff8dd..fbb9e6027 100644 --- a/conda_package/mpas_tools/viz/mpas_to_xdmf/templates/xdmf_template.xml +++ b/conda_package/mpas_tools/viz/mpas_to_xdmf/templates/xdmf_template.xml @@ -3,9 +3,9 @@ {% if times|length > 0 %} - {% set t_index = 0 %} - {% for t_val in times %} - + {% for t_val in times %} + {% set t_index = loop.index0 %} + - {% set t_index = t_index + 1 %} {% endfor %} {% else %} diff --git a/conda_package/mpas_tools/viz/mpas_to_xdmf/time.py b/conda_package/mpas_tools/viz/mpas_to_xdmf/time.py index ffc2059c5..aa71137c9 100644 --- a/conda_package/mpas_tools/viz/mpas_to_xdmf/time.py +++ b/conda_package/mpas_tools/viz/mpas_to_xdmf/time.py @@ -37,7 +37,7 @@ def _xtime_to_seconds(xtime: xr.DataArray) -> xr.DataArray: ---------- xtime : xr.DataArray An array of strings representing time in the format - 'YYYY-MM-DD_HH:MM:SS.sss'. + 'YYYY-MM-DD_HH:MM:SS' or 'YYYY-MM-DD_HH:MM:SS.sss'. Returns ------- @@ -45,10 +45,13 @@ def _xtime_to_seconds(xtime: xr.DataArray) -> xr.DataArray: An array of seconds since the first entry in `xtime`. """ # Convert xtime strings to datetime objects using datetime.strptime - timestamps = [ - datetime.strptime(time_str, '%Y-%m-%d_%H:%M:%S.%f') - for time_str in xtime.values.astype(str) - ] + timestamps = [] + for time_str in xtime.values.astype(str): + try: + timestamp = datetime.strptime(time_str, '%Y-%m-%d_%H:%M:%S.%f') + except ValueError: + timestamp = datetime.strptime(time_str, '%Y-%m-%d_%H:%M:%S') + timestamps.append(timestamp) # Calculate seconds since the first timestamp seconds_since_start = [ (ts - timestamps[0]).total_seconds() for ts in timestamps