Skip to content

Commit

Permalink
do not create folder for export zarr
Browse files Browse the repository at this point in the history
  • Loading branch information
jensdebruijn committed Sep 3, 2024
1 parent 3da4cc5 commit 4831a8d
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions honeybees/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,11 @@ def export_value(self, name: str, value: np.ndarray, conf: dict) -> None:
"""
if value is None:
return None
folder = os.path.join(self.export_folder, name)
try:
os.makedirs(folder)
except OSError:
pass
if "format" not in conf:
raise ValueError(
f"Export format must be specified for {name} in config file (npy/npz/csv/xlsx)."
)
fn = f"{self.timesteps[-1].isoformat().replace('-', '').replace(':', '')}"
if conf["format"] == "npy":
fn += ".npy"
fp = os.path.join(folder, fn)
np.save(fp, value)
elif conf["format"] == "npz":
fn += ".npz"
fp = os.path.join(folder, fn)
np.savez_compressed(fp, data=value)
elif conf["format"] == "csv":
fn += ".csv"
fp = os.path.join(folder, fn)
if len(value) > 100_000:
self.model.logger.info(
f"Exporting {len(value)} items to csv. This might take a long time and take a lot of space. Consider using NumPy (compressed) binary format (npy/npz)."
)
with open(fp, "w") as f:
f.write("\n".join([str(v) for v in value]))
elif conf["format"] == "zarr":
if conf["format"] == "zarr":
ds = conf["_file"]
if name not in ds:
if isinstance(value, (float, int)):
Expand All @@ -195,7 +172,28 @@ def export_value(self, name: str, value: np.ndarray, conf: dict) -> None:
index = conf["_time_index"].index(self.model.current_time)
ds[name][index] = value
else:
raise ValueError(f"{conf['format']} not recognized")
folder = os.path.join(self.export_folder, name)
os.makedirs(folder, exist_ok=True)
fn = f"{self.timesteps[-1].isoformat().replace('-', '').replace(':', '')}"
if conf["format"] == "npy":
fn += ".npy"
fp = os.path.join(folder, fn)
np.save(fp, value)
elif conf["format"] == "npz":
fn += ".npz"
fp = os.path.join(folder, fn)
np.savez_compressed(fp, data=value)
elif conf["format"] == "csv":
fn += ".csv"
fp = os.path.join(folder, fn)
if len(value) > 100_000:
self.model.logger.info(
f"Exporting {len(value)} items to csv. This might take a long time and take a lot of space. Consider using NumPy (compressed) binary format (npy/npz)."
)
with open(fp, "w") as f:
f.write("\n".join([str(v) for v in value]))
else:
raise ValueError(f"{conf['format']} not recognized")

def report_value(
self, name: Union[str, tuple[str, Any]], value: Any, conf: dict
Expand Down

0 comments on commit 4831a8d

Please sign in to comment.