Repository: DHI/mikeio
Pull request #480 (2022-11-21 17:41:01)

Read time slice (by position)


Patch:
diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile
deleted file mode 100644
index 9643e605..00000000
--- a/.devcontainer/Dockerfile
+++ /dev/null
@@ -1,19 +0,0 @@
-FROM mcr.microsoft.com/devcontainers/miniconda:0-3
-
-ARG NODE_VERSION="lts/*"
-RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
-
-# Copy environment.yml (if found) to a temp location so we update the environment. Also
-# copy "noop.txt" so the COPY instruction does not fail if no environment.yml exists.
-COPY environment.yml* .devcontainer/noop.txt /tmp/conda-tmp/
-RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then umask 0002 && /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \
-    && rm -rf /tmp/conda-tmp
-
-# [Optional] Uncomment to install a different version of Python than the default
-# RUN conda install -y python=3.6 \
-#     && pip install --no-cache-dir pipx \
-#     && pipx reinstall-all
-
-# [Optional] Uncomment this section to install additional OS packages.
-# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
-#     && apt-get -y install --no-install-recommends <your-package-list-here>
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
deleted file mode 100644
index c6dccd7f..00000000
--- a/.devcontainer/devcontainer.json
+++ /dev/null
@@ -1,21 +0,0 @@
-{
-	"name": "Miniconda (Python 3)",
-	"build": {
-		"context": "..",
-		"dockerfile": "Dockerfile"
-	},
-	"features": {
-		"ghcr.io/devcontainers-contrib/features/black:1": {},
-		"ghcr.io/devcontainers-contrib/features/flake8:1": {}
-	},
-	// Features to add to the dev container. More info: https://containers.dev/features.
-	// "features": {},
-	// Use 'forwardPorts' to make a list of ports inside the container available locally.
-	// "forwardPorts": [],
-	// Use 'postCreateCommand' to run commands after the container is created.
-	"postCreateCommand": "pip install -e ."
-	// Configure tool-specific properties.
-	// "customizations": {},
-	// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
-	// "remoteUser": "root"
-}
\ No newline at end of file
diff --git a/.devcontainer/noop.txt b/.devcontainer/noop.txt
deleted file mode 100644
index abee1954..00000000
--- a/.devcontainer/noop.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-This file is copied into the container along with environment.yml* from the
-parent folder. This is done to prevent the Dockerfile COPY instruction from 
-failing if no environment.yml is found.
\ No newline at end of file
diff --git a/mikeio/dfsutil.py b/mikeio/dfsutil.py
index f5fe6f63..4b6049eb 100644
--- a/mikeio/dfsutil.py
+++ b/mikeio/dfsutil.py
@@ -90,9 +90,11 @@ def _valid_timesteps(dfsFileInfo: DfsFileInfo, time_steps) -> Tuple[bool, List[i
         time = pd.date_range(start_time_file, periods=n_steps_file, freq=freq)
 
     if isinstance(time_steps, slice):
-
-        s = time.slice_indexer(time_steps.start, time_steps.stop)
-        time_steps = list(range(s.start, s.stop))
+        if isinstance(time_steps.start, int) or isinstance(time_steps.stop, int):
+            time_steps = list(range(*time_steps.indices(n_steps_file)))
+        else:
+            s = time.slice_indexer(time_steps.start, time_steps.stop)
+            time_steps = list(range(s.start, s.stop))
     elif isinstance(time_steps, Iterable) and isinstance(time_steps[0], int):
         time_steps = np.array(time_steps)
         time_steps[time_steps < 0] = n_steps_file + time_steps[time_steps < 0]


Test patch:
diff --git a/tests/test_consistency.py b/tests/test_consistency.py
index 4ecd0faa..d8e0c43c 100644
--- a/tests/test_consistency.py
+++ b/tests/test_consistency.py
@@ -471,6 +471,34 @@ def test_read_dfs_time_list_int():
         assert all(dsr[0].time == dsgetitem.time)
         assert dsr[0].shape == dsgetitem.shape
 
+def test_read_dfs_time_slice_int():
+
+    extensions = ["dfsu", "dfs2", "dfs1", "dfs0"]
+    for ext in extensions:
+        filename = f"tests/testdata/consistency/oresundHD.{ext}"
+        
+        time = slice(1,3) # 1,2 (not 3)
+        dssel = mikeio.read(filename=filename).isel(time=time)
+        dsr = mikeio.read(filename=filename, time=time)
+        assert all(dsr.time == dssel.time)
+        assert dssel.n_timesteps == 2
+
+        time = slice(None,-1) # Skip last step
+        dssel = mikeio.read(filename=filename).isel(time=time)
+        dsr = mikeio.read(filename=filename, time=time)
+        assert all(dsr.time == dssel.time)
+        assert dssel.n_timesteps == 4
+        
+        time = slice(1,None) # Skip first step
+        dssel = mikeio.read(filename=filename).isel(time=time)
+        dsr = mikeio.read(filename=filename, time=time)
+        assert all(dsr.time == dssel.time)
+        assert dssel.n_timesteps == 4
+
+        dsr = mikeio.read(filename=filename, time=time)
+        assert all(dsr.time == dssel.time)
+        assert dsr.shape == dssel.shape
+
 
 def test_filter_items_dfs0():