Input Kc Raster SWY

Hi all,

I have monthly rasters with Kc values from a phenology model. Is it possible to input them directly, instead of using the biophysical tables? If Not, How difficult would it be to modify the python code to do this?


Hi @sergio,

This is an interesting question. There are two general approaches you could take here:

  1. Reformat the data to fit the model
    The model assumes that each LULC category maps to one Kc value for each month, and one curve number (CN) value for each soil type. If you already have Kc rasters that are separate from the LULC, it’s likely that the area covered by one LULC category will contain multiple different Kc values.

    However, the LULC categories are arbitrary, and their level of granularity is up to you. One approach would be to create a new, higher-granularity LULC raster where each LULC value represents a unique combination of (original LULC category, Kc1, Kc2, Kc3,… Kc12). I wouldn’t recommend this unless you have very few possible LULC and Kc values, though, because you could easily end up with each pixel having its own category, and risk running out of memory when the biophysical table is read into a dictionary.

  2. Modifying the model to fit the data
    If you’re at all familiar with python, it’s definitely feasible to modify the model to accept your data. The model converts the biophysical table into monthly Kc rasters anyway, so you could essentially skip this step. You can check out the model code here. You’d need to modify the function execute. It consists of several tasks, including one kc_task for each month, which create the monthly Kc rasters (see line 683). You could remove this block and instead pass in your list of monthly Kc raster filepaths.

    I think this approach is more likely to work out well, and I’d be happy to help you with the details of modifying the model, if you have further questions!

1 Like

Hi Emily,

Thanks for your quick reply,
I prefer option 2, as it provides a more flexible and efficient way to incorporate the complex phenology in my study area. Having said that, I tried to modify the code, passing in a list of monthly kc rasters instead of the for loop from line 683. I did this directly in the venv using pycharm.

Unfortunately, when I tried to run the modified code it kept running the original code. I would appreciate it if you could provide some guidance regarding the easiest way to modify and then run the code.


Hi @Sergio,

Since this is a feature that will likely be helpful to others in the future, I’ve made a modified version of the model here. You can see what changes I made here.

I didn’t modify the UI for this, so you’ll need to run the model from a script (see example on my branch). To run this with your data:

  1. Clone my repo linked above and checkout the feature/kc-rasters branch
  2. In the invest/ directory, run python install. This will install InVEST and make it available to your script.
  3. Edit invest/ so that the args dict has the paths to your data and the other parameters that you want. These are the same values that you’d normally enter into the GUI form, except for kc_dir which is new.
  4. Run python

The model will execute and you should get all the usual outputs in whatever location you set for the workspace_dir.

Let me know if you have any troubles with that!

1 Like

Thanks @esoth ,

I really appreciate that you made a modified version of the model. Unfortunately, I am still experiencing an error while trying to install it.

/ src/natcap/invest/recreation/out_of_core_quadtree.cpp:4:10: fatal error: Python.h: No such file or directory
#include “Python.h”
compilation terminated.
error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1

These are the steps that I took:

#Clone the kc-rasters branch

git clone --single-branch --branch feature/kc-rasters

#cd to the invest folder where was cloned

#Install a to my virtualenv site package folder.

/home/sergio/PycharmProjects/PSA/venv/bin/python install

The process runs smoothly without specifying the kc-raster brach. However, it installs the original SWY model.

Any ideas on how should I troubleshoot this?


Hey @sergio, just wanted to chime in here. When installing natcap.invest from source, you’ll need a few extra packages installed so that things will compile correctly, which are documented for a few different linux distributions at In this particular case, the python-dev package (or equivalent on your distro) will be needed for addressing this compilation issue.

Hope this helps,