Skip to content

Commit

Permalink
black reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ashjbarnes committed Nov 29, 2023
1 parent b6d894f commit a8541b1
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions regional_mom6/regional_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
from .utils import vecdot
import yaml

warnings.filterwarnings("ignore")

__all__ = [
Expand Down Expand Up @@ -1237,36 +1238,42 @@ def setup_run_directory(self, demo_run_dir=False, using_payu=False):
## Copy the default directory to the run directory
if demo_run_dir != False:
rundir_src = (

Check warning on line 1240 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1239-L1240

Added lines #L1239 - L1240 were not covered by tests
Path(__file__).parent.parent /
"demos" /
"premade_run_directories" /
f"{demo_run_dir}"
Path(__file__).parent.parent
/ "demos"
/ "premade_run_directories"
/ f"{demo_run_dir}"
)
else:
print("Setting up run directory without using a premade template. Will attempt to modify files in existing run directory. At a minimum, you'll need `MOM_input`, `SIS_input` and `input.nml` files.")
print(

Check warning on line 1247 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1247

Added line #L1247 was not covered by tests
"Setting up run directory without using a premade template. Will attempt to modify files in existing run directory. At a minimum, you'll need `MOM_input`, `SIS_input` and `input.nml` files."
)
shutil.copytree(rundir_src, self.mom_run_dir, dirs_exist_ok=True)

Check warning on line 1250 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1250

Added line #L1250 was not covered by tests
## Make symlinks between run and input directories
inputdir_in_rundir = self.mom_run_dir / "inputdir"
rundir_in_inputdir = self.mom_input_dir / "rundir"

Check warning on line 1253 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1252-L1253

Added lines #L1252 - L1253 were not covered by tests

inputdir_in_rundir.unlink(missing_ok=True)
inputdir_in_rundir.symlink_to(self.mom_input_dir)

Check warning on line 1256 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1255-L1256

Added lines #L1255 - L1256 were not covered by tests

rundir_in_inputdir.unlink(missing_ok=True)
rundir_in_inputdir.symlink_to(self.mom_run_dir)

Check warning on line 1259 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1258-L1259

Added lines #L1258 - L1259 were not covered by tests

## Get mask table information
mask_table = None
for p in self.mom_input_dir.glob("mask_table.*"):
if mask_table != None:
print(f"WARNING: Multiple mask tables found. Defaulting to {p}. If this is not what you want, remove it from the run directory and try again.")

_ , masked, layout = p.name.split(".")
print(

Check warning on line 1265 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1262-L1265

Added lines #L1262 - L1265 were not covered by tests
f"WARNING: Multiple mask tables found. Defaulting to {p}. If this is not what you want, remove it from the run directory and try again."
)

_, masked, layout = p.name.split(".")
mask_table = p.name
x, y = (int(v) for v in layout.split("x"))
ncpus = (x * y) - int(masked)
if mask_table == None:
print("No mask table found! This suggests your domain is mostly water, so there are no `non compute` cells that are entirely land. If this doesn't seem right, ensure you've already run .FRE_tools().")
print(

Check warning on line 1274 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1269-L1274

Added lines #L1269 - L1274 were not covered by tests
"No mask table found! This suggests your domain is mostly water, so there are no `non compute` cells that are entirely land. If this doesn't seem right, ensure you've already run .FRE_tools()."
)
ncpus = self.layout[0] * self.layout[1]
print("Number of CPUs required: ", ncpus)

Check warning on line 1278 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1277-L1278

Added lines #L1277 - L1278 were not covered by tests

Expand All @@ -1276,8 +1283,8 @@ def setup_run_directory(self, demo_run_dir=False, using_payu=False):
# inputfile.close()

## Modify the input namelists to give the correct layouts
for j in ["MOM_input","SIS_input"]:
with open(self.mom_run_dir / j , "r") as file:
for j in ["MOM_input", "SIS_input"]:
with open(self.mom_run_dir / j, "r") as file:
lines = file.readlines()
for jj in range(len(lines)):
if "MASKTABLE" in lines[jj]:
Expand All @@ -1294,40 +1301,45 @@ def setup_run_directory(self, demo_run_dir=False, using_payu=False):
if "NJGLOBAL" in lines[jj]:
lines[jj] = f"NJGLOBAL = {self.hgrid.ny.shape[0]//2}\n"

Check warning on line 1302 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1301-L1302

Added lines #L1301 - L1302 were not covered by tests

with open(self.mom_run_dir / j , "w") as f:
with open(self.mom_run_dir / j, "w") as f:
f.writelines(lines)

Check warning on line 1305 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1304-L1305

Added lines #L1304 - L1305 were not covered by tests


## If using payu to run the model, create a payu configuration file
if not using_payu and os.path.exists(f"{self.mom_run_dir}/config.yaml"):
os.remove(f"{self.mom_run_dir}/config.yaml")

Check warning on line 1309 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1308-L1309

Added lines #L1308 - L1309 were not covered by tests

else:
with open(f"{self.mom_run_dir}/config.yaml",'r') as file:
with open(f"{self.mom_run_dir}/config.yaml", "r") as file:
lines = file.readlines()

Check warning on line 1313 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1312-L1313

Added lines #L1312 - L1313 were not covered by tests

inputfile = open(f"{self.mom_run_dir}/config.yaml",'r')
inputfile = open(f"{self.mom_run_dir}/config.yaml", "r")
lines = inputfile.readlines()
inputfile.close()
for i in range(len(lines)):
if "ncpus" in lines[i]:
lines[i] = f'ncpus: {str(ncpus)}\n'
lines[i] = f"ncpus: {str(ncpus)}\n"
if "jobname" in lines[i]:
lines[i] = f"jobname: mom6_{self.mom_input_dir.name}\n"

Check warning on line 1322 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1315-L1322

Added lines #L1315 - L1322 were not covered by tests

if "input:" in lines[i]:
lines[i + 1] = f" - {self.mom_input_dir}\n"

Check warning on line 1325 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1324-L1325

Added lines #L1324 - L1325 were not covered by tests

with open(f"{self.mom_run_dir}/config.yaml",'w') as file:
with open(f"{self.mom_run_dir}/config.yaml", "w") as file:
file.writelines(lines)

Check warning on line 1328 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1327-L1328

Added lines #L1327 - L1328 were not covered by tests

# Modify input.nml
nml = f90nml.read(self.mom_run_dir / "input.nml")
nml["coupler_nml"]["current_date"] = [self.daterange[0].year, self.daterange[0].month,self.daterange[0].day , 0, 0, 0]
nml["coupler_nml"]["current_date"] = [

Check warning on line 1332 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1331-L1332

Added lines #L1331 - L1332 were not covered by tests
self.daterange[0].year,
self.daterange[0].month,
self.daterange[0].day,
0,
0,
0,
]
nml.write(self.mom_run_dir / "input.nml", force=True)
return

Check warning on line 1341 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1340-L1341

Added lines #L1340 - L1341 were not covered by tests


def setup_era5(self, era5_path):
"""
Sets up the ERA5 forcing files for your experiment. This assumes that you'd downloaded all of the ERA5 data in your daterange.
Expand Down Expand Up @@ -1383,11 +1395,7 @@ def setup_era5(self, era5_path):
humidity = (

Check warning on line 1395 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1394-L1395

Added lines #L1394 - L1395 were not covered by tests
(0.622 / rawdata["sp"]["sp"]) * (10**dewpoint) * 101325 / 760
)
q = xr.Dataset(
data_vars={
"q": humidity
}
)
q = xr.Dataset(data_vars={"q": humidity})

Check warning on line 1398 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1398

Added line #L1398 was not covered by tests

q.q.attrs = {"long_name": "Specific Humidity", "units": "kg/kg"}
q.to_netcdf(

Check warning on line 1401 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1400-L1401

Added lines #L1400 - L1401 were not covered by tests
Expand Down

0 comments on commit a8541b1

Please sign in to comment.