Skip to content

Commit c954c46

Browse files
committed
Make save_netCDF more robust with torax_path
1 parent 92b8be9 commit c954c46

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

torax/imas_tools/util.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
objects"""
1717
import datetime
1818
import importlib
19+
import pathlib
1920
import os
2021
from typing import Any
2122

@@ -25,7 +26,7 @@
2526

2627

2728
def save_netCDF(
28-
directory_path: str | None,
29+
directory_path: str | pathlib.Path | None,
2930
file_name: str | None,
3031
IDS: IDSToplevel,
3132
):
@@ -42,14 +43,27 @@ def save_netCDF(
4243
Returns:
4344
None
4445
"""
46+
from torax._src.config.config_loader import torax_path
47+
directory_path = pathlib.Path(directory_path) if isinstance(directory_path, str) else directory_path
48+
if not directory_path.is_dir():
49+
#Checks that the directory can be found.
50+
new_path = torax_path().joinpath(directory_path)
51+
if not new_path.is_dir():
52+
raise ValueError(f'Directory {directory_path} could not be found. If it is a relative path, it'
53+
' could not be resolved relative to the working directory'
54+
f' {os.getcwd()} or the Torax directory {torax_path()}.'
55+
)
56+
directory_path = new_path
57+
4558
if directory_path is None:
46-
directory_path = "torax/data/third_party/geo"
59+
directory_path = torax_path().joinpath('torax/data/third_party/geo')
4760
if file_name is None:
4861
date_str = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
4962
file_name = "IDS_file_" + date_str
5063
filepath = os.path.join(directory_path, file_name) + ".nc"
5164
with imas.DBEntry(uri=filepath, mode="w") as netcdf_entry:
5265
netcdf_entry.put(ids=IDS)
66+
print(f'Successfully saved file {filepath}')
5367

5468

5569
def load_IMAS_data(uri: str, ids_name: str) -> IDSToplevel:

0 commit comments

Comments
 (0)