diff --git a/src/rangeland_production/forage.py b/src/rangeland_production/forage.py index 2c551ee7c..8d2aeb4a5 100644 --- a/src/rangeland_production/forage.py +++ b/src/rangeland_production/forage.py @@ -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) @@ -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))) @@ -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 @@ -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( @@ -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'), @@ -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( diff --git a/src/rangeland_production/ui/forage.py b/src/rangeland_production/ui/forage.py index 408dc3c0a..fa67164e0 100644 --- a/src/rangeland_production/ui/forage.py +++ b/src/rangeland_production/ui/forage.py @@ -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=( @@ -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( @@ -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) @@ -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(), } diff --git a/tests/test_forage.py b/tests/test_forage.py index ea11d4d82..e3e5c99a6 100644 --- a/tests/test_forage.py +++ b/tests/test_forage.py @@ -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.""" @@ -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