# Install TauDEM

Installing TauDEM actually means installing 3 tools:

- GDAL that TauDEM uses under the hood;
- MPI that R will call to launch TauDEM;
- TauDEM itself.

Please follow instructions on how to install TauDEM, GDAL and MPI depending on your operating system, then finalize your installation as specified below.

No matter your operating system, take note of the steps you follow. This way, if anything goes wrong, you will be able to open an informative bug report and get help more easily.

## Windows

You’re in luck! The TauDEM Windows installer will install all three tools at once! Refer to https://hydrology.usu.edu/taudem/taudem5/downloads.html.

## macOS

To install the dependencies, Homebrew is recommended. It is a package manager, similar to APT on Linux systems.

1. Open a terminal.

2. Download and install GDAL, MPICH and CMake: `sh brew install gdal mpich cmake`

3. Go to the Github page of TauDEM and download the code as ZIP

4. Extract it and move the folder `TauDEM-Develop` to a convenient place, e.g.: `sh /Users/$USER/Documents/TauDEM`

5. Go back to the terminal and go into the directory with: `sh cd /Users/$USER/Documents/TauDEM/TauDEM-Develop`

6. Create a folder where the executables will be installed in: `sh mkdir bin`

7. Go into the source folder `src`: `sh cd src`

8. Create the `build` directory: and go into with: `sh mkdir build`

9. Go into the `build` directory: `sh cd build`

10. Build with `cmake ..`

11. Finalize with:

    make && sudo make install

The executable files will end up in `/usr/local/taudem/`.

12. Add the location of the executable files to your path environment variable. This is a bit tricky on macOS, since RStudio and the package only see the path added to `/etc/paths.d/`. So add it
    with the following two commands:

        echo "/usr/local/taudem/" > taudem
        sudo mv taudem /etc/paths.d/

## Linux

### GDAL

Do you need to install GDAL? In a terminal try `gdalinfo --version`, if it fails `gdal-config --version`. If it works and return something you do not need to install GDAL unless you want a newer version. You might have installed GDAL in the past for, say, using the sf R package.

If you need to, install GDAL using the official GDAL docs (or refer to TauDEM README for further suggestions).

### MPI

First try running `mpiexec --version` in a terminal. If it works and returns something, you do not need to install MPI unless you want a newer version.

If you need to, install MPI using the official MPI docs (or refer to TauDEM README for further suggestions).

### TauDEM

First you need to obtain TauDEM source for GitHub https://github.com/dtarb/TauDEM/. If you are at ease with this, you can clone the repository. If not, find the green button that allows you to clone, open (with GitHub CLI) or download, and download the ZIP version.

We recommend using CMake because that is what we did but TauDEM README also has a method using Make. Therefore,

- install CMake or Make (check it’s not already installed);
- follow TauDEM README for further suggestions) for either CMake or Make depending on what you decided to use.

> If the installation shows errors, try using https://github.com/maelle/TauDEM/tree/more-mpi-deprecations instead.

> Even for a successful installation you might see many warnings.

In particular, the following error has been observed in some systems:

    linklib.h: In function ‘bool sendLink(int32_t, int)’:
    linklib.h:281:9: error: ‘MPI_Type_extent’ was not declared in this scope; did you mean ‘MPI_Type_get_extent’?

In such a case, execute the following commands on the terminal:

    mkdir bin
    cd src
    sed -i -e 's/MPI_Type_struct/MPI_Type_create_struct/g' linklib.h
    sed -i -e 's/MPI_Type_extent(MPI_LONG, \&extent)/MPI_Aint lb\;MPI_Type_get_extent(MPI_LONG, \&lb, \&extent)/g' linklib.h
    make

See also https://github.com/lucarraro/traudem/issues/48#issue-2109770271 for details.

## Finalize your installation

Once you are done, you need to register TauDEM in R and check your installation.

### Register TauDEM in R

#### Add TauDEM to the PATH

- The Windows installer might have done this automatically.
- For other operating systems (macOS, Linux) use your usual method to add a directory to the PATH.

#### OR add an environmental variable

Add an R environmental variable called `TAUDEM_PATH` that points to the location where the TauDEM executables can be found (on Windows it might be `C:\Program Files\TauDEM`, on Ubuntu `/usr/local/taudem`).

For instance on Ubuntu it could be adding this line in `.Renviron` (see `usethis::edit_r_environ()`) and then re-starting R:

    TAUDEM_PATH='/usr/local/taudem'

or, for just the session, running this line of R code:

    Sys.setenv(TAUDEM_PATH = "/usr/local/taudem")

### Check your installation

Run `traudem::taudem_sitrep()` and read its output carefully. Pay attention to any error that might indicate part of the installation was not successful.

If all is well (no error, only normal output from TauDEM), congratulations! Your system is ready to apply TauDEM to your own data.