Skip to content

Commit

Permalink
Merge pull request #601 from DHI/pathlib
Browse files Browse the repository at this point in the history
pathlib instead of os.path
  • Loading branch information
ecomodeller authored Oct 12, 2023
2 parents 47882c2 + b051de5 commit 41f7ec6
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 242 deletions.
10 changes: 0 additions & 10 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))


# -- Project information -----------------------------------------------------

Expand Down
12 changes: 6 additions & 6 deletions mikeio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path
from platform import architecture

# PEP0440 compatible formatted version, see:
Expand Down Expand Up @@ -117,7 +117,7 @@ def read(filename, *, items=None, time=None, keepdims=False, **kwargs) -> Datase
>>> ds = mikeio.read("HD2D.dfsu", error_bad_data=False, fill_bad_data_value=0.0) # replace corrupt data with 0.0
"""

ext = os.path.splitext(filename)[1].lower()
ext = Path(filename).suffix.lower()

if "dfs" not in ext:
raise ValueError("mikeio.read() is only supported for Dfs files")
Expand Down Expand Up @@ -156,7 +156,7 @@ def open(filename: str, **kwargs):
>>> dfs = mikeio.open("pt_spectra.dfs2", type="spectral")
"""
file_format = os.path.splitext(filename)[1].lower()[1:]
ext = Path(filename).suffix.lower()[1:]

READERS = {
"dfs0": Dfs0,
Expand All @@ -167,13 +167,13 @@ def open(filename: str, **kwargs):
"mesh": Mesh,
}

if file_format not in READERS:
if ext not in READERS:
valid_formats = ", ".join(READERS.keys())
raise Exception(
f"{file_format} is not a supported format for mikeio.open. Valid formats are {valid_formats}"
f"{ext} is not a supported format for mikeio.open. Valid formats are {valid_formats}"
)

reader_klass = READERS[file_format]
reader_klass = READERS[ext]

return reader_klass(filename, **kwargs)

Expand Down
30 changes: 16 additions & 14 deletions mikeio/_track.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
import os
from pathlib import Path
from datetime import datetime
from typing import Callable, Sequence, Tuple

Expand Down Expand Up @@ -36,20 +36,22 @@ def _extract_track(

if isinstance(track, str):
filename = track
if os.path.exists(filename):
ext = os.path.splitext(filename)[1].lower()
if ext == ".dfs0":
df = Dfs0(filename).to_dataframe()
elif ext == ".csv":
df = pd.read_csv(filename, index_col=0, parse_dates=True)
df.index = pd.DatetimeIndex(df.index)
else:
raise ValueError(f"{ext} files not supported (dfs0, csv)")

times = df.index
coords = df.iloc[:, 0:2].to_numpy(copy=True)
else:
path = Path(filename)
if not path.exists():
raise ValueError(f"{filename} does not exist")

ext = path.suffix.lower()
if ext == ".dfs0":
df = Dfs0(filename).to_dataframe()
elif ext == ".csv":
df = pd.read_csv(filename, index_col=0, parse_dates=True)
df.index = pd.DatetimeIndex(df.index)
else:
raise ValueError(f"{ext} files not supported (dfs0, csv)")

times = df.index
coords = df.iloc[:, 0:2].to_numpy(copy=True)

elif isinstance(track, Dataset):
times = track.time
coords = np.zeros(shape=(len(times), 2))
Expand Down
3 changes: 2 additions & 1 deletion mikeio/dataset/_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ def _is_compatible(self, other, raise_error=False):
problems.append("Number of timesteps must be the same")
if self.start_time != other.start_time:
problems.append("start_time must be the same")
if type(self.geometry) != type(other.geometry):
#if type(self.geometry) != type(other.geometry):
if not isinstance(self.geometry, other.geometry.__class__):
problems.append("The type of geometry must be the same")
if hasattr(self.geometry, "__eq__"):
if not (self.geometry == self.geometry):
Expand Down
5 changes: 3 additions & 2 deletions mikeio/dataset/_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
import os
from pathlib import Path
import warnings
from copy import deepcopy
from datetime import datetime
Expand Down Expand Up @@ -1838,7 +1838,8 @@ def to_dfs(self, filename, **kwargs):

@staticmethod
def _validate_extension(filename, valid_extension):
ext = os.path.splitext(filename)[1].lower()
path = Path(filename)
ext = path.suffix.lower()
if ext != valid_extension:
raise ValueError(f"File extension must be {valid_extension}")

Expand Down
2 changes: 1 addition & 1 deletion mikeio/dfs/_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class _Dfs123:

# TODO add all common arguments
def __init__(self, filename=None):
self._filename = str(filename)
self._filename = str(filename) if filename else None
self._projstr = None
self._start_time = None
self._end_time = None
Expand Down
23 changes: 12 additions & 11 deletions mikeio/dfs/_dfs0.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path
import warnings
from datetime import datetime, timedelta

Expand Down Expand Up @@ -104,12 +104,13 @@ def __init__(self, filename=None):
self._filename = str(filename)

if filename:
self._read_header()
self._read_header(Path(filename))

def __repr__(self):
out = ["<mikeio.Dfs0>"]

if os.path.isfile(self._filename):
# TODO does this make sense:
if self._filename:
out.append(f"timeaxis: {repr(self._timeaxistype)}")

if self._n_items is not None:
Expand All @@ -122,11 +123,11 @@ def __repr__(self):

return str.join("\n", out)

def _read_header(self):
if not os.path.exists(self._filename):
raise FileNotFoundError(self._filename)
def _read_header(self, path: Path):
if not path.exists():
raise FileNotFoundError(path)

dfs = DfsFileFactory.DfsGenericOpen(self._filename)
dfs = DfsFileFactory.DfsGenericOpen(str(path))
self._source = dfs
self._deletevalue = dfs.FileInfo.DeleteValueDouble # NOTE: changed in cutil

Expand Down Expand Up @@ -164,10 +165,10 @@ def read(self, items=None, time=None, keepdims=False) -> Dataset:
-------
Dataset
A Dataset with data dimensions [t]
"""

if not os.path.exists(self._filename):
raise FileNotFoundError(f"File {self._filename} not found.")
"""
path = Path(self._filename)
if not path.exists():
raise FileNotFoundError(f"File {path} not found")

# read data from file
fdata, ftime, fitems = self.__read(self._filename)
Expand Down
27 changes: 14 additions & 13 deletions mikeio/dfs/_dfs1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path

from mikecore.DfsBuilder import DfsBuilder # type: ignore
from mikecore.DfsFileFactory import DfsFileFactory # type: ignore
Expand All @@ -20,7 +20,7 @@ def __init__(self, filename=None):
self._x0 = 0

if filename:
self._read_dfs1_header()
self._read_dfs1_header(path=Path(filename))
origin = self._longitude, self._latitude
self.geometry = Grid1D(
x0=self._x0,
Expand All @@ -34,7 +34,8 @@ def __init__(self, filename=None):
def __repr__(self):
out = ["<mikeio.Dfs1>"]

if os.path.isfile(self._filename):
# TODO does this make sense
if self._filename:
out.append(f"dx: {self.dx:.5f}")

if self._n_items is not None:
Expand All @@ -44,20 +45,20 @@ def __repr__(self):
out.append(f" {i}: {item}")
else:
out.append(f"number of items: {self._n_items}")
if os.path.isfile(self._filename):
if self._n_timesteps == 1:
out.append("time: time-invariant file (1 step)")
else:
out.append(f"time: {self._n_timesteps} steps")
out.append(f"start time: {self._start_time}")

if self._n_timesteps == 1:
out.append("time: time-invariant file (1 step)")
else:
out.append(f"time: {self._n_timesteps} steps")
out.append(f"start time: {self._start_time}")

return str.join("\n", out)

def _read_dfs1_header(self):
if not os.path.isfile(self._filename):
raise FileNotFoundError(self._filename)
def _read_dfs1_header(self, path: Path):
if not path.exists():
raise FileNotFoundError(path)

self._dfs = DfsFileFactory.Dfs1FileOpen(self._filename)
self._dfs = DfsFileFactory.Dfs1FileOpen(str(path))
self._x0 = self._dfs.SpatialAxis.X0
self._dx = self._dfs.SpatialAxis.Dx
self._nx = self._dfs.SpatialAxis.XCount
Expand Down
12 changes: 4 additions & 8 deletions mikeio/dfs/_dfs2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from typing import List, Tuple

from copy import deepcopy
Expand Down Expand Up @@ -120,7 +119,7 @@ def __init__(self, filename=None, type: str = "horizontal"):

if filename:
is_spectral = type.lower() in ["spectral", "spectra", "spectrum"]
self._read_dfs2_header(read_x0y0=is_spectral)
self._read_dfs2_header(filename=filename, read_x0y0=is_spectral)
self._validate_no_orientation_in_geo()
origin, orientation = self._origin_and_orientation_in_CRS()

Expand All @@ -140,7 +139,7 @@ def __init__(self, filename=None, type: str = "horizontal"):
def __repr__(self):
out = ["<mikeio.Dfs2>"]

if os.path.isfile(self._filename):
if self._filename:
out.append(f"dx: {self.dx:.5f}")
out.append(f"dy: {self.dy:.5f}")

Expand All @@ -160,11 +159,8 @@ def __repr__(self):

return str.join("\n", out)

def _read_dfs2_header(self, read_x0y0: bool = False):
if not os.path.isfile(self._filename):
raise Exception(f"file {self._filename} does not exist!")

self._dfs = DfsFileFactory.Dfs2FileOpen(self._filename)
def _read_dfs2_header(self, filename, read_x0y0: bool = False):
self._dfs = DfsFileFactory.Dfs2FileOpen(str(filename))
self._source = self._dfs
if read_x0y0:
self._x0 = self._dfs.SpatialAxis.X0
Expand Down
8 changes: 2 additions & 6 deletions mikeio/dfs/_dfs3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from typing import Tuple

import numpy as np
Expand Down Expand Up @@ -142,7 +140,8 @@ def __init__(self, filename=None):
def __repr__(self):
out = ["<mikeio.Dfs3>"]

if os.path.isfile(self._filename):

if self._filename:
out.append(f"geometry: {self.geometry}")

if self._n_items is not None:
Expand All @@ -162,9 +161,6 @@ def __repr__(self):
return str.join("\n", out)

def _read_dfs3_header(self, read_x0y0z0: bool = False):
if not os.path.isfile(self._filename):
raise Exception(f"file {self._filename} does not exist!")

self._dfs = DfsFileFactory.Dfs3FileOpen(self._filename)

self._source = self._dfs
Expand Down
8 changes: 5 additions & 3 deletions mikeio/dfsu/_dfsu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
import os
from pathlib import Path
import warnings
from datetime import datetime, timedelta
from functools import wraps
Expand Down Expand Up @@ -175,10 +176,11 @@ def _read_header(self, input):
return

filename = input
if not os.path.isfile(filename):
raise Exception(f"file {filename} does not exist!")
path = Path(input)
if not path.exists():
raise FileNotFoundError(f"file {path} does not exist!")

ext = os.path.splitext(filename)[1].lower()
ext = path.suffix.lower()

if ext == ".mesh":
self._read_mesh_header(filename)
Expand Down
4 changes: 2 additions & 2 deletions mikeio/dfsu/_factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path

from mikecore.DfsuFile import DfsuFile, DfsuFileType # type: ignore

Expand All @@ -25,7 +25,7 @@ def __new__(self, filename, *args, **kwargs):

@staticmethod
def _get_DfsuFileType_n_Obj(filename: str):
ext = os.path.splitext(filename)[-1].lower()
ext = Path(filename).suffix.lower()
if "dfs" in ext:
dfs = DfsuFile.Open(filename)
type = DfsuFileType(dfs.DfsuFileType)
Expand Down
Loading

0 comments on commit 41f7ec6

Please sign in to comment.