Skip to content

Commit 26ff5fc

Browse files
authored
Make setup rundir more robust take #2 (#179)
* check that premade rundir folder exists. If not, try second option, if still not raise valueerror * typo * Force setup_run_directory function to raise AttributeError if it doesn't have access to a cpu layout rather than throw generic error * black
1 parent 578590f commit 26ff5fc

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

regional_mom6/regional_mom6.py

+39-9
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ def __init__(
437437
self.grid_type = grid_type
438438
self.repeat_year_forcing = repeat_year_forcing
439439
self.ocean_mask = None
440+
self.layout = None # This should be a tuple. Leaving in a dummy 'None' makes it easy to remind the user to provide a value later on.
440441
if read_existing_grids:
441442
try:
442443
self.hgrid = xr.open_dataset(self.mom_input_dir / "hgrid.nc")
@@ -1425,6 +1426,21 @@ def setup_run_directory(
14251426
premade_rundir_path = Path(
14261427
importlib.resources.files("regional_mom6") / "demos/premade_run_directories"
14271428
)
1429+
if not premade_rundir_path.exists():
1430+
print("Could not find premade run directories at ", premade_rundir_path)
1431+
print(
1432+
"Perhaps the package was imported directly rather than installed with conda. Checking if this is the case... "
1433+
)
1434+
1435+
premade_rundir_path = Path(
1436+
importlib.resources.files("regional_mom6").parent
1437+
/ "demos/premade_run_directories"
1438+
)
1439+
if not premade_rundir_path.exists():
1440+
raise ValueError(
1441+
f"Cannot find the premade run directory files at {premade_rundir_path} either.\n\n"
1442+
+ "There may be an issue with package installation. Check that the `premade_run_directory` folder is present in one of these two locations"
1443+
)
14281444

14291445
# Define the locations of the directories we'll copy files across from. Base contains most of the files, and overwrite replaces files in the base directory.
14301446
base_run_dir = premade_rundir_path / "common_files"
@@ -1490,18 +1506,32 @@ def setup_run_directory(
14901506
mask_table = p.name
14911507
x, y = (int(v) for v in layout.split("x"))
14921508
ncpus = (x * y) - int(masked)
1493-
if mask_table == None:
1509+
layout = (
1510+
x,
1511+
y,
1512+
) # This is a local variable keeping track of the layout as read from the mask table. Not to be confused with self.layout which is unchanged and may differ.
1513+
14941514
print(
1495-
"No mask table found! This suggests the domain is mostly water, so there are "
1496-
+ "no `non compute` cells that are entirely land. If this doesn't seem right, "
1497-
+ "ensure you've already run `FRE_tools`."
1515+
f"Mask table {p.name} read. Using this to infer the cpu layout {layout}, total masked out cells {masked}, and total number of CPUs {ncpus}."
14981516
)
1499-
if not hasattr(self, "layout"):
1517+
1518+
if mask_table == None:
1519+
if self.layout == None:
15001520
raise AttributeError(
1501-
"No layout information found. This suggests that `FRE_tools()` hasn't been called yet. "
1502-
+ "Please do so, in order for the number of processors required is computed."
1521+
"No mask table found, and the cpu layout has not been set. At least one of these is requiret to set up the experiment."
15031522
)
1504-
ncpus = self.layout[0] * self.layout[1]
1523+
print(
1524+
f"No mask table found, but the cpu layout has been set to {self.layout} This suggests the domain is mostly water, so there are "
1525+
+ "no `non compute` cells that are entirely land. If this doesn't seem right, "
1526+
+ "ensure you've already run the `FRE_tools` method which sets up the cpu mask table. Keep an eye on any errors that might print while"
1527+
+ "the FRE tools (which run C++ in the background) are running."
1528+
)
1529+
# Here we define a local copy of the layout just for use within this function.
1530+
# This prevents the layout from being overwritten in the main class in case
1531+
# in case the user accidentally loads in the wrong mask table.
1532+
layout = self.layout
1533+
ncpus = layout[0] * layout[1]
1534+
15051535
print("Number of CPUs required: ", ncpus)
15061536

15071537
## Modify the input namelists to give the correct layouts
@@ -1515,7 +1545,7 @@ def setup_run_directory(
15151545
else:
15161546
lines[jj] = "# MASKTABLE = no mask table"
15171547
if "LAYOUT =" in lines[jj] and "IO" not in lines[jj]:
1518-
lines[jj] = f"LAYOUT = {self.layout[1]},{self.layout[0]}\n"
1548+
lines[jj] = f"LAYOUT = {layout[1]},{layout[0]}\n"
15191549

15201550
if "NIGLOBAL" in lines[jj]:
15211551
lines[jj] = f"NIGLOBAL = {self.hgrid.nx.shape[0]//2}\n"

0 commit comments

Comments
 (0)