natcap.invest.crop_production_regression

InVEST Crop Production Regression Model.

class natcap.invest.crop_production_regression.CropToPathTables(climate_bin, observed_yield, percentile_yield, regression_yield)

Bases: tuple

climate_bin

Alias for field number 0

observed_yield

Alias for field number 1

percentile_yield

Alias for field number 2

regression_yield

Alias for field number 3

natcap.invest.crop_production_regression.aggregate_regression_results_to_polygons(base_aggregate_vector_path, target_aggregate_vector_path, aggregate_results_table_path, landcover_raster_projection, crop_names, nutrient_df, pixel_area_ha, file_registry)

Write table with aggregate results of yield and nutrient values.

Use zonal statistics to summarize total observed and interpolated production and nutrient information for each polygon in base_aggregate_vector_path.

Parameters:
  • base_aggregate_vector_path (string) – path to polygon vector

  • target_aggregate_vector_path (string) – path to re-projected copy of polygon vector

  • aggregate_results_table_path (string) – path to CSV file where aggregate results will be reported.

  • landcover_raster_projection (string) – a WKT projection string

  • crop_names (list) – list of crop names

  • nutrient_df (pandas.DataFrame) – a table of nutrient values by crop

  • pixel_area_ha (float) – area of lulc raster cells (hectares)

  • file_registry (FileRegistry) – used to look up target file paths

Returns:

None

natcap.invest.crop_production_regression.execute(args)

Crop Production Regression.

This model will take a landcover (crop cover?), N, P, and K map and produce modeled yields, and a nutrient table.

Parameters:
  • args['workspace_dir'] (string) – output directory for intermediate, temporary, and final files

  • args['results_suffix'] (string) – (optional) string to append to any output file names

  • args['landcover_raster_path'] (string) – path to landcover raster

  • args['landcover_to_crop_table_path'] (string) –

    path to a table that converts landcover types to crop names that has two headers:

    • lucode: integer value corresponding to a landcover code in

      args[‘landcover_raster_path’].

    • crop_name: a string that must match one of the crops in

      CROP_OPTIONS. A ValueError is raised if no corresponding climate bin raster path is found in the Climate Bin Raster Table.

  • args['fertilization_rate_table_path'] (string) – path to CSV table that contains fertilization rates for the crops in the simulation, though it can contain additional crops not used in the simulation. The headers must be ‘crop_name’, ‘nitrogen_rate’, ‘phosphorus_rate’, and ‘potassium_rate’, where ‘crop_name’ is the name string used to identify crops in the ‘landcover_to_crop_table_path’, and rates are in units kg/Ha.

  • args['aggregate_polygon_path'] (string) – path to polygon vector that will be used to aggregate crop yields and total nutrient value. (optional, if value is None, then skipped)

  • args['regression_yield_csv_table'] (string) – path to a table that maps each crop name to a path to its corresponding regression yield table.

  • args['climate_bin_raster_table'] (string) – path to a table that maps each crop name to a path to its corresponding climate bin raster.

  • args['observed_yield_raster_table'] (string) – path to a table that maps each crop name to a path to its corresponding observed yield raster.

  • args['crop_nutrient_table'] (string) – path to a table that lists amounts of nutrients in each crop.

Returns:

File registry dictionary mapping MODEL_SPEC output ids to absolute paths

natcap.invest.crop_production_regression.get_full_path_from_crop_table(model_spec: ModelSpec, table_id: str, table_path: str, crop_name: str) str | None

Given a crop-to-path table, look up a path and expand it if appropriate.

Parameters:
  • table_id (str) – the id of the table as defined in the model spec. One of CROP_TO_PATH_TABLES.

  • table_path (str) – the path to the table as defined in the model args.

  • crop_name (str) – the name of the crop to look up in the table. One of CROP_OPTIONS.

Returns:

The full path (str), as an absolute path if it’s local, or

normalized if it’s remote.

None if crop_name is not in the table, or if the path

found in the table is empty or not a string.

Return type:

One of the following

Raises:

KeyError` if table_id is not one of CROP_TO_PATH_TABLES

natcap.invest.crop_production_regression.tabulate_regression_results(nutrient_df, crop_names, pixel_area_ha, landcover_raster_path, landcover_nodata, file_registry, target_table_path)

Write table with total yield and nutrient results by crop.

This function includes all the operations that write to results_table.csv.

Parameters:
  • nutrient_df (pandas.DataFrame) – a table of nutrient values by crop

  • crop_names (list) – list of crop names

  • pixel_area_ha (float) – area of lulc raster cells (hectares)

  • landcover_raster_path (string) – path to landcover raster

  • landcover_nodata (float) – landcover raster nodata value

  • file_registry (FileRegistry) – used to look up target file paths

  • target_table_path (string) – path to ‘result_table.csv’ in the output workspace

Returns:

None