Skip to content

Commit ca323c6

Browse files
authored
Merge pull request #137 from amoodie/feat_dictyaml
Feat dictyaml. Simple changes, admin merge.
2 parents 586148b + 42e4fa4 commit ca323c6

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

pyDeltaRCM/preprocessor.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ def preliminary_yaml_parsing(self):
5555
5656
"""
5757
# open the file, an error will be thrown if invalid yaml?
58-
user_file = open(self.input_file, mode='r')
59-
self.yaml_dict = yaml.load(user_file, Loader=yaml.FullLoader)
60-
user_file.close()
58+
if isinstance(self.input_file, dict):
59+
self.yaml_dict = self.input_file
60+
elif (isinstance(self.input_file, str) or
61+
isinstance(self.input_file, Path)):
62+
self.yaml_dict = self._open_yaml(self.input_file)
63+
else:
64+
raise ValueError('Invalid input file argument.')
6165

6266
if 'ensemble' in self.yaml_dict.keys():
6367
self._has_ensemble = True
@@ -75,6 +79,27 @@ def preliminary_yaml_parsing(self):
7579

7680
return self.yaml_dict
7781

82+
def _open_yaml(self, input_file):
83+
"""Safely open, read, and close a yaml file.
84+
85+
Parameters
86+
----------
87+
input file
88+
string or path to file
89+
90+
Returns
91+
-------
92+
yaml_dict
93+
yaml file, as a Python dict
94+
"""
95+
if (input_file is None):
96+
return {} # return an empty dict
97+
else:
98+
user_file = open(input_file, mode='r')
99+
yaml_dict = yaml.load(user_file, Loader=yaml.FullLoader)
100+
user_file.close()
101+
return yaml_dict
102+
78103
def _create_matrix(self):
79104
"""Create a matrix if not already in the yaml.
80105
@@ -310,6 +335,9 @@ def run_jobs(self):
310335
num_parallel_processes = _parallel_flag
311336
else:
312337
num_parallel_processes = 1
338+
# number of parallel processes is never greater than number of jobs
339+
num_parallel_processes = np.minimum(
340+
num_parallel_processes, num_total_processes)
313341

314342
_msg = 'Running %g parallel jobs' % num_parallel_processes
315343
if self.verbose >= 1:
@@ -324,10 +352,15 @@ def run_jobs(self):
324352
# loop and create and start all jobs
325353
for i in range(0, num_total_processes):
326354
s.acquire() # aquire resource from Semaphore
355+
356+
# open yaml file for specific job
357+
job_yaml = self._open_yaml(self.file_list[i])
358+
359+
# instantiate the job
327360
p = _ParallelJob(i=i, queue=q, sema=s,
328361
input_file=self.file_list[i],
329362
cli_dict=self.cli_dict,
330-
yaml_dict=self.yaml_dict)
363+
yaml_dict=job_yaml)
331364
self.job_list.append(p)
332365
p.start()
333366

@@ -350,10 +383,14 @@ def run_jobs(self):
350383

351384
# loop and create all jobs
352385
for i in range(0, num_total_processes):
386+
387+
# open yaml file for specific job
388+
job_yaml = self._open_yaml(self.file_list[i])
389+
353390
p = _SerialJob(i=i,
354391
input_file=self.file_list[i],
355392
cli_dict=self.cli_dict,
356-
yaml_dict=self.yaml_dict)
393+
yaml_dict=job_yaml)
357394
self.job_list.append(p)
358395

359396
# run the job(s)
@@ -890,10 +927,10 @@ def scale_relative_sea_level_rise_rate(mmyr, If=1):
890927
891928
.. math::
892929
893-
\widehat{RSLR} = (RSLR / 1000) \cdot \dfrac{1}{I_f \cdot 365.25 \cdot 86400}
930+
\\widehat{RSLR} = (RSLR / 1000) \\cdot \\dfrac{1}{I_f \\cdot 365.25 \\cdot 86400}
894931
895932
This conversion makes it such that when one real-world year has elapsed
896-
(:math:`I_f \cdot 365.25 \cdot 86400` seconds in model time), the relative
933+
(:math:`I_f \\cdot 365.25 \\cdot 86400` seconds in model time), the relative
897934
sea level has changed by the number of millimeters specified in the input
898935
:obj:`mmyr`.
899936

0 commit comments

Comments
 (0)