Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conda_package/mpas_tools/viz/mpas_to_xdmf/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<Domain>
<Grid Name="TimeSeries" GridType="Collection" CollectionType="Temporal">
{% if times|length > 0 %}
{% set t_index = 0 %}
{% for t_val in times %}
<Grid Name="Step{{ t_index }}" GridType="Uniform">
{% for t_val in times %}
{% set t_index = loop.index0 %}
<Grid Name="Step{{ t_index }}" GridType="Uniform">
<Time Value="{{ t_val }}"/>
<Topology TopologyType="Polygon" NumberOfElements="{{ num_elements }}" NodesPerElement="{{ num_verts }}">
<DataItem Format="HDF" Dimensions="{{ num_elements }} {{ num_verts }}">{{ h5_basename }}:/Cells</DataItem>
Expand All @@ -16,7 +16,7 @@
{% for var in variables %}
{% if var.has_time %}
<Attribute Name="{{ var.name }}" AttributeType="Scalar" Center="Cell">
<DataItem Format="HDF" Dimensions="{{ num_elements }}">{{ h5_basename }}:/{{ var.name }}_t{{ t_index }}</DataItem>
<DataItem Format="HDF" Dimensions="{{ num_elements }}">{{ h5_basename }}:/{{ var.name }}_t{{ t_index }}</DataItem>
</Attribute>
{% else %}
<Attribute Name="{{ var.name }}" AttributeType="Scalar" Center="Cell">
Expand All @@ -25,7 +25,6 @@
{% endif %}
{% endfor %}
</Grid>
{% set t_index = t_index + 1 %}
{% endfor %}
{% else %}
<Grid Name="Static" GridType="Uniform">
Expand Down
13 changes: 8 additions & 5 deletions conda_package/mpas_tools/viz/mpas_to_xdmf/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ 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
-------
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
Expand Down
Loading