Skip to content

Commit

Permalink
Merge pull request #12 from AquaticEcoDynamics/next-release
Browse files Browse the repository at this point in the history
Merge next-release for 0.2.1
  • Loading branch information
gilesknight committed Aug 2, 2024
2 parents ca383bf + 0f1ab79 commit 1ad5b3a
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions glmpy/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from typing import Union
from fastapi import UploadFile



class GLMSim:
"""Prepare inputs and run a GLM simulation.
Expand Down Expand Up @@ -61,7 +60,8 @@ class GLMSim:
"""
def __init__(
self, input_files: Union[UploadFile, dict], api: bool, inputs_dir: str
self,
input_files: Union[UploadFile, dict], api: bool, inputs_dir: str
):
self.input_files = input_files
self.fast_api = api
Expand All @@ -79,35 +79,55 @@ def prepare_inputs(self) -> str:
File path to directory with input files required for a GLM
simulation.
"""
aed_files = [
"aed.nml", "aed_phyto_pars.nml", "aed_zoop_pars.nml",
"aed_bivalve_pars.nml", "aed_macrophyte_pars.nml",
"aed_malgae_pars.nml", "aed_sed_candi_pars.nml", "aed_sdg_pars.nml"
]
if self.fast_api:
if os.path.isdir(self.inputs_dir):
shutil.rmtree(self.inputs_dir)
os.mkdir(self.inputs_dir)
os.mkdir(os.path.join(self.inputs_dir, "bcs"))

for f in self.input_files:
if f.filename == "glm3.nml":
nml_path = os.path.join(self.inputs_dir, f.filename)

with open(nml_path, "wb") as f_tmp:
f_tmp.write(f.file.read())
elif f.filename in aed_files:
os.makedirs(
os.path.join(self.inputs_dir, "aed"), exist_ok=True
)
aed_path = os.path.join(self.inputs_dir, "aed", f.filename)
with open(aed_path, "wb") as f_tmp:
f_tmp.write(f.file.read())
else:
os.makedirs(
os.path.join(self.inputs_dir, "bcs"), exist_ok=True
)
bcs_path = os.path.join(self.inputs_dir, "bcs", f.filename)

with open(bcs_path, "wb") as f_tmp:
f_tmp.write(f.file.read())
else:
if os.path.isdir(self.inputs_dir):
shutil.rmtree(self.inputs_dir)
os.mkdir(self.inputs_dir)
os.mkdir(os.path.join(self.inputs_dir, "bcs"))
os.mkdir(self.inputs_dir)

input_fnames = self.input_files.keys()
for f in input_fnames:
if f == "glm3.nml":
nml_path = os.path.join(self.inputs_dir, f)
shutil.copy(self.input_files[f], nml_path)
elif f in aed_files:
os.makedirs(
os.path.join(self.inputs_dir, "aed"), exist_ok=True
)
aed_path = os.path.join(self.inputs_dir, "aed", f)
shutil.copy(self.input_files[f], aed_path)
else:
os.makedirs(
os.path.join(self.inputs_dir, "bcs"), exist_ok=True
)
bcs_path = os.path.join(self.inputs_dir, "bcs", f)
shutil.copy(self.input_files[f], bcs_path)

Expand Down Expand Up @@ -339,4 +359,4 @@ def csv_to_json(self, csv_lake_fname: str, variables: list):
for c in cols:
tmp_dict[c] = df.loc[:, c].tolist()

return tmp_dict
return tmp_dict

0 comments on commit 1ad5b3a

Please sign in to comment.