natcap.invest.crop_production package¶
Submodules¶
natcap.invest.crop_production.crop_production module¶
The Crop Production module excutes the Crop Production model.
-
natcap.invest.crop_production.crop_production.
calc_area_costs
(lookup_dict, economics_dict, aoi_raster)¶ Calculate area-related costs (e.g. labor, seed, machine, irrigation).
Parameters: - lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- economics_dict (dict) – economic information.
- aoi_raster (str) – path to aoi raster.
Returns: area_cost_dict – {<code>: <total of area-related costs>}
Return type: dict
-
natcap.invest.crop_production.crop_production.
calc_fertilizer_costs
(code, code_dict, aoi_raster, fertilizer_dict)¶ Calculate fertilizer application rate costs.
Parameters: - code (int) – crop code.
- code_dict (dict) – economic information of crop.
- aoi_raster (str) – path to aoi raster.
- fertilizer_dict (dict) – mapping of fertilizers to their respective raster paths.
Returns: fertilizer_costs – total cost of fertilizer application for
a given crop.
Return type: float
-
natcap.invest.crop_production.crop_production.
check_inputs
(args)¶ Check user provides inputs necessary for particular yield functions.
Parameters: args (dict) – user-provided arguments dictionary.
-
natcap.invest.crop_production.crop_production.
compute_financial_analysis
(yield_dict, economics_table, aoi_raster, lookup_dict, fertilizer_dict, financial_analysis_table)¶ Compute financial analysis.
Parameters: - yield_dict (collections.Counter) – mapping from crop code to total yield.
- economics_table (str) – path to table containing economic information for each crop.
- aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- fertilizer_dict (dict) – mapping of fertilizers to their respective raster paths.
- financial_analysis_table (str) – path to output table.
-
natcap.invest.crop_production.crop_production.
compute_nutritional_contents
(yield_dict, lookup_dict, nutrient_table, nutritional_contents_table)¶ Compute nutritional contents of crop yields.
Parameters: - yield_dict (collections.Counter) – mapping from crop code to total yield.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- nutrient_table (str) – path to table containing information about the nutrient contents of each crop.
- nutritional_contents_table (str) – path to output table.
-
natcap.invest.crop_production.crop_production.
compute_observed_yield
(aoi_raster, lookup_dict, observed_yield_dict, yield_raster)¶ Compute observed yield.
Parameters: - aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- observed_yield_dict (dict) – mapping of crops to observed yield rasters.
- yield_raster (str) – path to output directory.
Returns: yield_dict – mapping from crop code to total
yield.
Return type: collections.Counter
-
natcap.invest.crop_production.crop_production.
compute_percentile_yield
(aoi_raster, lookup_dict, climate_bin_dict, percentile_yield_dict, yield_raster, percentile_yield)¶ Compute yield using percentile method.
Parameters: - aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- climate_bin_dict (dict) – mapping of codes to climate bin rasters.
- percentile_yields_dict (dict) – mapping of crops to their respective information.
- yield_raster (str) – path to output raster.
- percentile_yield (str) – selected yield percentile.
Returns: yield_dict – mapping from crop code to total
yield.
Return type: collections.Counter
-
natcap.invest.crop_production.crop_production.
compute_regression_yield
(aoi_raster, lookup_dict, climate_bin_dict, regression_coefficient_dict, fertilizer_dict, irrigation_raster, yield_raster)¶ Compute regression yield.
Parameters: - aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- climate_bin_dict (dict) – mapping of codes to climate bin rasters.
- fertilizer_dir (str) – path to directory containing fertilizer rasters.
- regression_coefficients_dict (dict) – nested dictionary of regression coefficients for each crop code.
- irrigation_raster (str) – path to is_irrigated raster.
- yield_raster (str) – path to output raster.
Returns: yield_dict – mapping from crop code to total
yield.
Return type: collections.Counter
-
natcap.invest.crop_production.crop_production.
create_map
(d, sub_dict_key)¶ “Shorten nested dictionary into a one-to-one mapping.
Parameters: - d (dict) – nested dictionary.
- sub_dict_key (object) – key in sub-dictionary whose value becomes value in return dictionary.
Returns: one_to_one_dict – dictionary that is a one-to-one mapping.
Return type: dict
-
natcap.invest.crop_production.crop_production.
execute
(args)¶ Crop Production.
Parameters: - args['workspace_dir'] (str) – location into which all intermediate and output files should be placed.
- args['results_suffix'] (str) – a string to append to output filenames
- args['lookup_table'] (str) – filepath to a CSV table used to convert the crop code provided in the Crop Map to the crop name that can be used for searching through inputs and formatting outputs.
- args['aoi_raster'] (str) – a GDAL-supported raster representing a crop management scenario.
- args['dataset_dir'] (str) – the provided folder should contain a set of folders and data specified in the ‘Running the Model’ section of the model’s User Guide.
- args['yield_function'] (str) – the method used to compute crop yield. Can be one of three: ‘observed’, ‘percentile’, and ‘regression’.
- args['percentile_column'] (str) – for percentile yield function, the table column name must be provided so that the program can fetch the correct yield values for each climate bin.
- args['fertilizer_dir'] (str) – path to folder that contains a set of GDAL-supported rasters representing the amount of Nitrogen (N), Phosphorous (P2O5), and Potash (K2O) applied to each area of land (kg/ha).
- args['irrigation_raster'] (str) – filepath to a GDAL-supported raster representing whether irrigation occurs or not. A zero value indicates that no irrigation occurs. A one value indicates that irrigation occurs. If any other values are provided, irrigation is assumed to occur within that cell area.
- args['compute_nutritional_contents'] (boolean) – if true, calculates nutrition from crop production and creates associated outputs.
- args['nutrient_table'] (str) – filepath to a CSV table containing information about the nutrient contents of each crop.
- args['compute_financial_analysis'] (boolean) – if true, calculates economic returns from crop production and creates associated outputs.
- args['economics_table'] (str) – filepath to a CSV table containing information related to market price of a given crop and the costs involved with producing that crop.
Example Args:
args = { 'workspace_dir': 'path/to/workspace_dir/', 'results_suffix': 'scenario_name', 'lookup_table': 'path/to/lookup_table', 'aoi_raster': 'path/to/aoi_raster', 'dataset_dir': 'path/to/dataset_dir/', 'yield_function': 'regression', 'percentile_column': 'yield_95th', 'fertilizer_dir':'path/to/fertilizer_rasters_dir/', 'irrigation_raster': 'path/to/is_irrigated_raster', 'compute_nutritional_contents': True, 'nutrient_table': 'path/to/nutrition_table', 'compute_financial_analysis': True, 'economics_table': 'path/to/economics_table' }
-
natcap.invest.crop_production.crop_production.
get_fertilizer_rasters
(fertilizer_dir, cache_dir, aoi_raster)¶ Get fertilizer rasters.
Parameters: - fertilizer_dir (str) – path to directory containing fertilizer rasters.
- cache_dir (str) – path to cache directory.
- aoi_raster (str) – path to aoi raster.
Returns: fertilizer_dict – mapping of fertilizers to their respective
raster paths.
Return type: dict
-
natcap.invest.crop_production.crop_production.
get_files_in_dir
(path)¶ Fetch mapping of files in directory.
- Each key in the mapping is the first part of the filename split by an
- underscore.
Each value in the mapping is the filepath.
Parameters: path (str) – path to directory. Returns: files_dict – dict([(filename, filepath), ...]). Return type: dict
-
natcap.invest.crop_production.crop_production.
get_global_dataset
(dataset_dir)¶ Get global dataset.
Parameters: dataset_dir (str) – path to spatial dataset. Returns: dataset_dict – tree-like structure of spatial dataset filenames and filepaths.Return type: dict
-
natcap.invest.crop_production.crop_production.
get_lookup_dict
(aoi_raster, lookup_table)¶ Get lookup information for AOI.
Parameters: - aoi_raster (str) – path to aoi raster.
- lookup_table (str) – path to lookup table.
Returns: lookup_dict – mapping of codes to lookup info for crops in aoi.
Return type: dict
-
natcap.invest.crop_production.crop_production.
get_percentile_yields
(percentile_tables, lookup_dict)¶ Get percentile yield information.
Parameters: - percentile_tables (dict) – mapping of crops to their respective tables. filepaths.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
Returns: percentile_yields_dict – mapping of crops to their respective
information.
Return type: dict
-
natcap.invest.crop_production.crop_production.
get_regression_coefficients
(regression_tables, lookup_dict)¶ Get regression coefficients.
Parameters: - regression_tables (dict) – mapping of codes to regression coeffeicent tables.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
Returns: regression_coefficients_dict – nested dictionary of regression
coefficients for each crop code.
Return type: dict
-
natcap.invest.crop_production.crop_production.
read_from_raster
(input_raster, offset_block)¶ Read numpy array from raster block.
Parameters: - input_raster (str) – filepath to input raster.
- offset_block (dict) – dictionary of offset information. Keys in the dictionary include ‘xoff’, ‘yoff’, ‘win_xsize’, and ‘win_ysize’.
Returns: array – a blocked array of the input raster.
Return type: np.array
-
natcap.invest.crop_production.crop_production.
reclass
(array, d, nodata=0.0)¶ Reclassify values in numpy ndarray.
Values in array that are not in d are reclassed to np.nan.
Parameters: - array (np.array) – input data.
- d (dict) – reclassification map.
- nodata (float) – reclass value for number not provided in reclassification map.
Returns: reclass_array – reclassified array.
Return type: np.array
-
natcap.invest.crop_production.crop_production.
reproject_global_rasters
(global_dataset_dict, cache_dir, aoi_raster, lookup_dict)¶ Reproject global rasters.
Parameters: - global_dataset_dict (dict) – mapping of crops to their respective data filepaths.
- cache_dir (str) – path to directory in which to store reprojected rasters.
- aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
Returns: observed_yield_dict – mapping of crops to observed yield rasters.
Return type: dict
-
natcap.invest.crop_production.crop_production.
reproject_raster
(src_path, template_path, dst_path)¶ Reproject raster.
Block-size set to 256 x 256.
Parameters: - src_path (str) – path to source raster.
- template_path (str) – path to template raster.
- dst_path (str) – path to destination raster.
-
natcap.invest.crop_production.crop_production.
run_observed_yield
(global_dataset_dict, cache_dir, aoi_raster, lookup_dict, yield_raster)¶ Run observed yield model.
Parameters: - global_dataset_dict (dict) – mapping of crops to their respective data filepaths.
- cache_dir (str) – path to cache directory.
- aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- yield_raster (str) – path to output raster.
Returns: yield_dict – mapping from crop code to total
yield.
Return type: collections.Counter
-
natcap.invest.crop_production.crop_production.
run_percentile_yield
(climate_bin_maps, percentile_tables, cache_dir, aoi_raster, lookup_dict, yield_raster, percentile_yield)¶ Run percentile yield model.
Parameters: - climate_bin_dict (dict) – mapping of codes to climate bin rasters.
- percentile_tables (dict) – mapping of crops to their respective tables. filepaths.
- cache_dir (str) – path to cache directory.
- aoi_raster (str) – path to aoi raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- yield_raster (str) – path to output raster.
- percentile_yield (str) – selected yield percentile.
Returns: yield_dict – mapping from crop code to total
yield.
Return type: collections.Counter
-
natcap.invest.crop_production.crop_production.
run_regression_yield
(climate_bin_maps, regression_tables, cache_dir, aoi_raster, fertilizer_dict, irrigation_raster, lookup_dict, yield_raster)¶ Run regression yield model.
Parameters: - climate_bin_maps (dict) – mapping of crops to climate bin rasters.
- regression_tables (dict) – mapping of codes to regression coeffeicent tables.
- cache_dir (str) – path to cache directory.
- aoi_raster (str) – path to aoi raster.
- fertilizer_dict (dict) – mapping of fertilizers to their respective raster paths.
- irrigation_raster (str) – path to intermediate is_irrigated raster.
- lookup_dict (dict) – mapping of codes to lookup info for crops in aoi.
- yield_raster (str) – path to output raster.
Returns: yield_dict – mapping from crop code to total
yield.
Return type: collections.Counter
-
natcap.invest.crop_production.crop_production.
write_to_raster
(output_raster, array, xoff, yoff)¶ Write numpy array to raster block.
Parameters: - output_raster (str) – filepath to output raster.
- array (np.array) – block to save to raster.
- xoff (int) – offset index for x-dimension.
- yoff (int) – offset index for y-dimension.