Skip to content

Commit

Permalink
Merge pull request #7 from natcap/bugfix/5-initial-conditions-input
Browse files Browse the repository at this point in the history
Bugfix/5 initial conditions input
  • Loading branch information
vakowal authored Feb 24, 2020
2 parents 1ea7811 + 469e8bb commit 924cf9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/rangeland_production/forage.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ def execute(args):
"pixel size of aligned inputs: %s", target_pixel_size)

# temporary directory for intermediate files
global PROCESSING_DIR
PROCESSING_DIR = os.path.join(args['workspace_dir'], "temporary_files")
if not os.path.exists(PROCESSING_DIR):
os.makedirs(PROCESSING_DIR)
Expand All @@ -826,6 +827,8 @@ def execute(args):
# rasters to be used in raster calculations for the model.
aligned_raster_dir = os.path.join(
args['workspace_dir'], 'aligned_inputs')
if os.path.exists(aligned_raster_dir):
shutil.rmtree(aligned_raster_dir)
os.makedirs(aligned_raster_dir)
aligned_inputs = dict([(key, os.path.join(
aligned_raster_dir, 'aligned_%s' % os.path.basename(path)))
Expand Down Expand Up @@ -898,6 +901,7 @@ def execute(args):
if len(state_var_nodata) > 1:
raise ValueError(
"Initial state variable rasters contain >1 nodata value")
global _SV_NODATA
_SV_NODATA = list(state_var_nodata)[0]

# align initial values with inputs
Expand Down Expand Up @@ -1550,7 +1554,6 @@ def _check_pft_fractional_cover_sum(aligned_inputs, pft_id_set):

# clean up
os.remove(cover_sum_path)
os.remove(operand_temp_path)


def initial_conditions_from_tables(
Expand Down Expand Up @@ -13752,9 +13755,7 @@ def validate(args, limit_to=None):
'animal_grazing_areas_path',
'site_param_table',
'veg_trait_path',
'animal_trait_path',
'site_initial_table',
'pft_initial_table']
'animal_trait_path']

spatial_file_list = [
('aoi_path', gdal.OF_VECTOR, 'vector'),
Expand All @@ -13780,6 +13781,15 @@ def validate(args, limit_to=None):
elif args[key] in ['', None]:
no_value_list.append(key)

# if initial conditions dir is not supplied, initial tables are required
if 'initial_conditions_dir' not in args:
for key in ['site_initial_table', 'pft_initial_table']:
if limit_to is None or limit_to == key:
if key not in args:
missing_key_list.append(key)
elif args[key] in ['', None]:
no_value_list.append(key)

if missing_key_list:
# if there are missing keys, we have raise KeyError to stop hard
raise KeyError(
Expand Down
17 changes: 15 additions & 2 deletions src/rangeland_production/ui/forage.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ def __init__(self):
label=u'Animal Parameter Table (CSV)',
validator=self.validator)
self.add_input(self.animal_trait_path)
self.initial_conditions_dir = inputs.File(
args_key=u'initial_conditions_dir',
helptext=(
u"A path to a directory containing initial conditions rasters "
"for each site- and PFT-level state variable (optional). "
"See model documentation for required state variables and "
"file names. If this directory is not supplied, initial "
"values tables must be supplied."),
label=u'Initial Conditions Directory',
validator=self.validator)
self.add_input(self.initial_conditions_dir)
self.site_initial_table = inputs.File(
args_key=u'site_initial_table',
helptext=(
Expand All @@ -283,7 +294,7 @@ def __init__(self):
"modeled site type to their location within the study area. "
"See model documentation for state variables that are "
"required to be included in this table."),
label=u'Initial Conditions Site State Variables',
label=u'Initial Conditions Table: Site State Variables',
validator=self.validator)
self.add_input(self.site_initial_table)
self.pft_initial_table = inputs.File(
Expand All @@ -297,7 +308,7 @@ def __init__(self):
"their fractional cover in each pixel. See model "
"documentation for state variables that are required to be "
"included in this table."),
label=u'Initial Conditions: Plant Functional Type State Variables',
label=u'Initial Conditions Table: PFT State Variables',
validator=self.validator)
self.add_input(self.pft_initial_table)

Expand Down Expand Up @@ -337,6 +348,8 @@ def assemble_args(self):
self.site_param_table.args_key: self.site_param_table.value(),
self.veg_trait_path.args_key: self.veg_trait_path.value(),
self.animal_trait_path.args_key: self.animal_trait_path.value(),
self.initial_conditions_dir.args_key:
self.initial_conditions_dir.value(),
self.site_initial_table.args_key: self.site_initial_table.value(),
self.pft_initial_table.args_key: self.pft_initial_table.value(),
}
Expand Down
10 changes: 6 additions & 4 deletions tests/test_forage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,9 +1003,11 @@ class foragetests(unittest.TestCase):

def setUp(self):
"""Create temporary workspace directory."""
self.workspace_dir = tempfile.mkdtemp()
self.PROCESSING_DIR = os.path.join(
self.workspace_dir, "temporary_files")
self.workspace_dir = "C:/Users/ginge/Desktop/temp_test_dir"
os.makedirs(self.workspace_dir)
global PROCESSING_DIR
PROCESSING_DIR = os.path.join(self.workspace_dir, "temporary_files")
os.makedirs(PROCESSING_DIR)

def tearDown(self):
"""Clean up remaining files."""
Expand Down Expand Up @@ -1149,7 +1151,7 @@ def assert_sorted_lists_equal(self, string_list_1, string_list_2):
for i in range(len(string_list_1)):
self.assertEqual(string_list_1[i], string_list_2[i])

# @unittest.skip("did not run the whole model, running unit tests only")
@unittest.skip("did not run the whole model, running unit tests only")
def test_model_runs(self):
"""Test forage model."""
from rangeland_production import forage
Expand Down

0 comments on commit 924cf9d

Please sign in to comment.