Skip to content

Commit

Permalink
Merge pull request #152 from dafyddstephenson/inputdataset_unit_tests
Browse files Browse the repository at this point in the history
InputDataset unit tests
  • Loading branch information
dafyddstephenson authored Nov 28, 2024
2 parents 6e20778 + ee43e17 commit 644f179
Show file tree
Hide file tree
Showing 4 changed files with 1,106 additions and 11 deletions.
10 changes: 7 additions & 3 deletions cstar/base/input_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
self.file_hash: Optional[str] = file_hash
self.working_path: Optional[Path | List[Path]] = None

if (self.file_hash is None) and (self.source.location_type == "url"):
if (self.source.location_type == "url") and (self.file_hash is None):
raise ValueError(
f"Cannot create InputDataset for \n {self.source.location}:\n "
+ "InputDataset.file_hash cannot be None if InputDataset.source.location_type is 'url'.\n"
Expand Down Expand Up @@ -100,12 +100,16 @@ def __repr__(self) -> str:
repr_str = f"{self.__class__.__name__}("
repr_str += f"\nlocation = {self.source.location!r},"
repr_str += f"\nfile_hash = {self.file_hash}"
if self.start_date is not None:
repr_str += f"\nstart_date = {self.start_date!r}"
if self.end_date is not None:
repr_str += f"\nend_date = {self.end_date!r}"
repr_str += "\n)"
info_str = ""
if self.working_path is not None:
info_str += f"working_path = {self.working_path},"
info_str += f"working_path = {self.working_path}"
if not self.exists_locally:
info_str += "(does not exist)"
info_str += " (does not exist)"
if len(info_str) > 0:
repr_str += f"\nState: <{info_str}>"
# Additional info
Expand Down
24 changes: 16 additions & 8 deletions cstar/roms/input_dataset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import yaml
import shutil
import dateutil
import datetime as dt
import roms_tools

from abc import ABC
from pathlib import Path
Expand Down Expand Up @@ -36,20 +38,23 @@ def __str__(self) -> str:
def __repr__(self) -> str:
repr_str = super().__repr__()
if hasattr(self, "partitioned_files") and len(self.partitioned_files) > 0:
repr_str = repr_str.strip(",>")
repr_str += "\n" + (" " * 8) + "partitioned_files = "
repr_str += _list_to_concise_str(
info_str = "partitioned_files = "
info_str += _list_to_concise_str(
[str(f) for f in self.partitioned_files], pad=29
)
repr_str += "\n>"
if "State:" in repr_str:
repr_str = repr_str.strip(",>")
repr_str += ",\n" + (" " * 8) + info_str + "\n>"
else:
repr_str += f"\nState: <{info_str}>"

return repr_str

def get_from_yaml(
self,
local_dir: str | Path,
start_date: Optional[dt.datetime] = None,
end_date: Optional[dt.datetime] = None,
start_date: Optional[dt.datetime] | str = None,
end_date: Optional[dt.datetime] | str = None,
np_xi: Optional[int] = None,
np_eta: Optional[int] = None,
) -> None:
Expand Down Expand Up @@ -116,7 +121,10 @@ def get_from_yaml(
f"roms tools yaml file has {len(yaml_keys)} sections. "
+ "Expected 'Grid' and one other class"
)

if isinstance(start_date, str):
start_date = dateutil.parser.parse(start_date)
if isinstance(end_date, str):
end_date = dateutil.parser.parse(end_date)
start_time = start_date.isoformat() if start_date is not None else None
end_time = end_date.isoformat() if end_date is not None else None

Expand All @@ -134,7 +142,7 @@ def get_from_yaml(
F.write(f"---{header}---\n" + yaml.dump(yaml_dict))

# Finally, make a roms-tools object from the modified yaml
import roms_tools
# import roms_tools

roms_tools_class = getattr(roms_tools, roms_tools_class_name)

Expand Down
Loading

0 comments on commit 644f179

Please sign in to comment.