MinGW Installation Guide for SWAT Debugging

Please note that the administrative privilege is not needed in the whole processes. MinGW would not add entries into register system or change the environment variables.

  1. Download the automated GUI installer: mingw-get-setup.exe and run. Accept all the default settings and

  1. Choose MinGW packages to install. Open MinGW Installation Manager installed in previous step, select Basic Setup in the left panel and choose package mingw32-base and mingw32-gcc-fortran. Then select menu Installation -> Apply Changes to start installation. The installer would download selected package and extract them into the given folder.

  1. Go to the installation folder (C:\MinGW\bin) to check mingw32-make.exe and gfortran.exe.

  1. Want to compile SWAT outside of Eclipse? Add folder C:\MinGW\bin to the PATH variable.

Advertisements

Debug SWAT in Eclipse – Utilize Makefile

Don’t Want to Go through all these Processes?

No problem. The Eclipse project created in this post is uploaded to Google Code: https://swat-eclipse.googlecode.com/svn/trunk/SWAT_Makefile. Just check out and open in Eclipse. It’s done!

This version also add Make Target, which is a quick way to build and clean the project. Just double-click to build or clean specific version.

It’s recommended to use SVN Plugin for Eclipse to do this work. I would have another post on this topic in the near future. Please stay tuned.

Contents

  • Find Fortran Project… Menu
  • Create an Empty Makefile Project
  • Setup the Project
    • Binary Parser
    • Error Parsers
    • Fortran Build Configuration
  • Copy SWAT Source Codes and Generate Makefile
  • Build Project
  • Debug/Run SWAT.exe

Please note

  1. To compile SWAT, MinGW needs to be installed first. Please refer to another post MinGW Installation Guide for SWAT Debugging
  2. Eclipse KEPLER and PTP 7.0.3 was used in this post. These steps should also be able to be used in other version of Eclipse and PTP. For more information, please refer to my previous post Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT.

A Makefile is created in previous post. To use it in Eclipse (Photran/PTP), instead of Executable (Gnu Fortran on Windows) project, we should use the Makefile project, which would allow use of user-defined Makefile.

This post will show you how to create and configure a SWAT Makefile project to utilize the makefile.

Find Fortran Project… Menu

The Fortran Project is not list in menu File -> New when PTP is used the first time. In this case, select Projects… to open New Project window. In this window, you would find Fortran project under Fotran folder. Select it and click Next to create the project. When it’s done, you will be asked if you want to open Fortran perspective. Select Yes. The Fotran Project option would be list in menu File -> New.

Create an Empty Makefile Project

File -> New -> Fortran Project

The project name is SWAT. Select Empty Project under Makefile project and select — Other Toolchain —. Click finish to create the project..

Setup the Project

Open the Property window through right click on the project and select “Properties” to setup the project.

Binary Parser

In Fotran -> Build -> Setting, select PE Windows Parser for Binary Parsers (I’m working a Windows machine)..

Error Parsers

Select Photran Error Parser for GNU Fortran (gfortran) for Error Parsers and move it to the top. This option is not selected when the project is created.

Fortran Build Configuration

Select Fortran Build in the left panel, un-check Use default build command and change Build Command to mingw32-make.

Add debug/ to build directory as there will be two versions of Makefile, one is located in debug folder and another is located in release folder. This one is for debug version.

Change configuration name from Default to Debug.

The default configuration name is Default. It would be nice to rename it to debug. Click Manage Configurations… to open Manage Configurations window. Select default and click Rename… to change name to Debug.

Create Release Configuration

As mentioned before, there is also a release version of Makefile. It’s necessary to make a configuration for it. Here, we could copy all the settings from debug configuration to save time.

Click New… on Manage Configurations window to open Create New Configuration window. Input name as Release and select to copy settings from Debug configuration.

Change the Build directory to ${workspace_loc:/SWAT}/release/.

Copy SWAT Source Codes and Generate Makefile

Copy all the SWAT source codes to the project folder and refresh the project in Eclipse. It would show all the files. Remember to comment the first line in main.f.

Download the GenerateMakefile program and save it into the project folder. Run it to generate the Makefile for debug and release version.

Build Project

Now, it’s ready to build the project, i.e. compile SWAT source codes. Right-click the project and select Build Project to start compile SWAT source codes. You would see some output information from Console window in the bottom. From there, you would know whether or not the process is finished. All the warnings and errors would also be found in this window.

Debug/Run SWAT.exe

After SWAT is compiled successfully, it’s ready to run SWAT. Before that, there is one thing to do: setup Run/Debug Settings, which could be completed in Properties window.

Select Run/Debug Settings in the left panel and click New… to create a Run/Debug Settings. We will first create the settings for debug version. Same as the build configuration, the setting for release version could be also copied from debug version.

In the Main tab, input Fortran Application as debug\SWAT.exe.

In Arguments table, change Working directory to the model txtinout folder.

In Debugger tab, select MinGW gdb for Debugger and uncheck Stop on startup at: option.

In Common tab, select Debug in Display in favorites menu to show the setting in debug menu.

Click debug menu in the toolbar to start running SWAT.

The output information would be display in the Console window.

If some breakpoints are set, SWAT would stop running on breakpoints and ask if want to change to Debug perspective. Click yes to change to Debug perspective, which would re-arrange the windows to facilitate debugging.

You would has change to check all the variable values in Variables window under debugging mode.

Click Fortran button on the up right corner, Eclipse would go back to Fortran perspective, which is designed for coding.

Click the Debug button in the up right corner to return to Debug perspective.

SWAT Makefile Updated – Stop running when overflow happens

Note: This version is updated in next post SWAT Makefile Updated – Ignore Underflow, where underflow flag is removed.

The Makefile published in previous blog Makefile – Compile SWAT using gfortran without modification is updated. It’s highly recommended to update.

Download Link

Makefile Generator, Debug Makefile, Release Makefile

Updates

  1. More gfortran flags are added to make sure the SWAT will stop running when errors happen.

In previous version, SWAT would ignore all the errors (e.g. overflow) and run to the end. It’s impossible to know if there are some errors. For some cases, the results are not safe to use.

The official SWAT would stop whenever an error happens and give where the error comes from. It’s the right way to compile SWAT.

In gfortran, the flag -ffpe-trap could used to do this work. The following command flag is added to the Makefile, which would stop the program whenever invalid, zero, overflow or underflow happens.

-ffpe-trap=invalid,zero,overflow,underflow

  1. rm command is used to replace del command in the clean target.

The del command could only be called from cmd.exe. Eclipse don’t know where is this command and would fail to clean the project.

rm.exe comes along with MSYS in MinGW (package mingw-developer-toolkit and msys-base) and is usually located at C:\MinGW\msys\1.0\bin. Adding this location to the PATH variable in Windows setting or Eclipse project setting would allow Eclipse to call this function when “Clear Project” is used.

Makefile – Compile SWAT using gfortran without modification

Note: The makefile here is an older version. For the latest version, please visit another post SWAT Makefile Updated – Stop running when overflow happens.

Quick Download Link

Makefile Generator, Debug Makefile, Release Makefile

Next post will be: Use Makefile in Photran to Debug SWAT

Please note that the modparm.f doesn’t need to be compiled as parm.mod any more. That process is already included in the Makefile.

Contents

  • Why some files fail to be compiled in Photran?
  • Use additional gfortran command options to compile problematic files
  • Makefile
  • Generate Makefile
  • The only modification
  • Use Makefile

In my previous post Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT, several swat source code files need to be modified to be able to compilable within Photran. It’s nice, but it’s even better if the source codes could be compiled without any modification.

Why some files fail to be compiled in Photran?

Compare the original source codes and the modified source codes through Google codes, and find that only following files need to be modified.

  • biozone.f
  • bmpinit.f
  • carbon_zhang2.f90
  • main.f
  • modparm.f
  • ovr_sed.f
  • percmain.f
  • rthsed.f

Except for main.f, these files have a common problem: one or more lines exceed the default line length set for Fotran fix format file (*.f) and free format file (*.f90). The default limitation of the line length for *.f is 72 and for *.f90 is 132. The default Fortran compile command (shown below) in Photran doesn’t consider this situable. If the length of one line exceed the limitation, the compiler (gfortran) would give errors.

  • Debug version: gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0
  • Release version: gfortran -funderscoring -O3 -Wall -c -fmessage-length=0

Use additional gfortran command options to compile problematic files

To solve this problem, these files are modified to make sure they meet the requirement on line length in previous post. Besides this, there is also another solution: change the line length limitation using gfortran command options.

  • Fixed format (*.f): add -ffixed-line-length-132 option to set the line length limitation of fixed format Fortran file to 132
  • Freeformat (*.f): add -ffree-line-length-200
    option to set the line length limitation of free format Fortran file to 200

For example, the gfortran command for debug version of biozone.f would be

gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -ffixed-line-length-132

And the gfortran command for release version of carbon_zhang2.f90 would be

gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -ffree-line-length-200

These files could be compiled successfully with the new command.

Makefile

In default Photran settings, it’s impossible to use different command options for different source files. Applying -ffixed-line-length-132 to other *.f files would also generate compile errors. How to solve this? Using Makefile.

Makefile defines how to compile and link a program. With it, it’s possible to define different commands for different source files and compile SWAT source codes without modification. For more information, please refer to GNU make document.

Generate Makefile

There are more than 300 Fortran source files in SWAT. It’s time-consuming work to write the Makefile manually. We already know the gfortran commands and the files which need to re-set the line length limitation. A program, which would get all *.f and *.f90 in the SWAT source codes folder and generate the compile commands in the makefile, is a quite straightforward and simple way to do the work.

Based on this, a .NET console application is created (source codes, executable). It’s fairly easy to use. Copy the program file to the source codes folder and double-click.

A prompt window would pop-up to ask which version should be the Makefile generated for, debug or release. Input d or debug for debug version, r or release for release version. Then the program would start to work and generate the corresponding Makefile in debug or release folder. The debug/release folder would be created if it doesn’t exist.

The two version of Makefile for rev 615 are also available to be download directly (debug version, release version). These two files could be also used for also version as long as the number and name of source codes files is same. Create the debug/release folder in the source codes folder and put the file into the corresponding folder.

The only modification

The only modification is to comment the first line of main.f (include modparm.f). There should be some command option to ignore this line but haven’t found it:)

Use Makefile

Makefile is fairly easy to use. Just use the GNU make command which comes along with MinGW. There are two version of make in MinGW: mingw32-make and make. Either one is ok.

Open cmd.exe and navigate to the Makefile folder. Execute mingw32-make or make. It’s done!

Interesting Test – Install ArcSWAT for ArcGIS 10.0/10.1 on ArcGIS 9.3

Interesting Test – Install ArcSWAT for ArcGIS 10.0/10.1 on ArcGIS 9.3

Method in post “Manually Install ArcSWAT” was used to install ArcSWAT for ArcGIS 10.0/10.1 on ArcGIS 9.3. The installation is successful but there are some problem in stream network generation (no problem in flow direction and accumulation calculation). The reason is the new version manager system in ArcGIS 10 (new file ArcGISVersion.dll located in C:\Program Files (x86)\Common Files\ArcGIS\bin). However, it would be enough for an existing model.

Manually Install ArcSWAT

Quick Download Link

Extract Installation File

ArcGIS 9.3: Register, Unregister

ArcGIS 10.0 and higher: Register, Unregister

Note:

  1. The bat file needs to be saved in the same folder as installation file or .NET assemblies.
  2. The register and unregister bat file needs to be run as administrator.
  3. Since there will be some errors, the method in this post could also be used to install ArcSWAT for ArcGIS 10.0/10.1 on ArcGIS 9.3. 

Contents

  • Why bother to install ArcSWAT manually?
  • Important Facts
  • Extract ArcSWAT program files from installation file
  • Copy new files to existing ArcSWAT installation folder
  • Re-register ArcSWAT .NET assemblies
  • Unregister previous ArcSWAT if necessary

Why bother to install ArcSWAT manually?

  1. ArcSWAT is updated frequently and must be re-installed to use the new functions. Uninstalling the previous version and installing the new version would need around ten minutes.
  2. You may experience problems when trying to uninstall and install ArcSWAT. These problems are usually difficult to solve. Having seen some these problems in ArcSWAT user group. In some cases, it couldn’t be solved until restore the entire Windows system.

Important Facts

  1. There are usually two steps involved in program installation: 1) copy files to specific folder and 2) register some file in the system if necessary.
  2. The core of ArcSWAT is six .NET assemblies and SWAT executable file. When a new version comes out, it means one or more of these assemblies are updated. Other files are usually kept same between different versions. Of course, they would be updated once a while.

So, most of the time, upgrading ArcSWAT only means updating these six .NET assemblies and SWAT executable file. Knowing this, it’s possible to upgrade ArcSWAT without normal installation. It could be done use following steps:

  1. Get the core files
  2. Replace old files
  3. Re-register the six .NET assemblies

Extract ArcSWAT program files from installation file

There is a MSI file, namely SWAT_Install.msi, in the installation package downloaded from SWAT website. It contains all the files which will be copied to the installation folder. We will extract all the files from it.

Execute following command in cmd window.

msiexec /a [SWAT_Install.msi_path] /qb TARGETDIR=[extraction_folder_path]

Where

SWAT_Install.msi_path is the path to SWAT_install.msi

extraction_folder_path is the path to the extraction folder where the extracted files will be saved.

Following window will show up to display the progress.

Wait it to finish and all the files will be extracted to the given folder. The extracted files are around 567 MB.

A bat file could be used to do this work. Create a text file in the folder, copy following commands to the file and save it as a bat file in the same folder as the installation file. Then double-click to run the commands. This bat file also could be downloaded here.

msiexec /a SWAT_Install.msi /qb TARGETDIR=%CD%\extract

Copy new files to existing ArcSWAT installation folder

If a previous version is installed in the system, copy following files into the installation folder to replace the existing files.

  • SWAT_PM2.dll
  • SWAT_WD.dll
  • SWAT_HRU.dll
  • SWAT_InputFileGen.dll
  • SWAT_InputFileEdit.dll
  • SWAT_RunSWAT.dll
  • SWAT_32debug.exe
  • SWAT_32rel.exe
  • SWAT_64debug.exe
  • SWAT_64rel.exe
  • swat2012.exe

Please note that the name of SWAT model executable files may be different for different version. There usually four versions: 32-bit debug, 32-bit release, 64-bit debug, 64-bit release.

Re-register ArcSWAT .NET assemblies

As mentioned in my previous post ArcSWAT Structure – Developer view, the ArcSWAT extension, toolbar, menus and windows are defined in six .NET assemblies. To use the new function in the new version, they must be registered in the system.

The register process is based on the version of ArcGIS.

  • ArcGIS 9.3

Run cmd as administrator and navigate to .NET folder (which is usually located at c:\Windows\Microsoft.NET\Framework\v2.0.50727). Execute following command for each of the six ArcSWAT .NET assemblies. It’s done when you see “Type registered successfully”.

regasm [assembly_path] /codebase

where

assembly_path is the path to the ArcSWAT .NET assembly, e.g. c:\swat\ArcSWAT\SWAT_PM2.dll

The same as extracting files, a bat file could be created using following commands to do the work. An existing bat file could be downloaded here. Please not that the bat file needs to be in the same folder and run as administrator.

SET DLL_DIR=%~dp0

CD %SystemRoot%\Microsoft.NET\Framework\v2.0.50727

regasm %DLL_DIR%SWAT_HRUs.dll /codebase

regasm %DLL_DIR%SWAT_InputFileEdit.dll /codebase

regasm %DLL_DIR%SWAT_InputFileGen.dll /codebase

regasm %DLL_DIR%SWAT_PM2.dll /codebase

regasm %DLL_DIR%SWAT_RunSwat.dll /codebase

regasm %DLL_DIR%SWAT_WD.dll /codebase

  • ArcGIS 10.0 and higher

Run cmd as administrator and navigate to ArcGIS bin folder (which is usually located at C:\Program Files (x86)\Common Files\ArcGIS\bin). Execute following command for each of the six ArcSWAT .NET assemblies. It’s done when you see a window saying “Registration succeeded”.

esriregasm /p:desktop
[assembly_path]

where

assembly_path is the path to the ArcSWAT .NET assembly, e.g. c:\swat\ArcSWAT\SWAT_PM2.dll

Similar as ArcGIS 9.3, a bat file could be utilized to do the work on just a click. Please note the location of esriregasm.exe may be different on your computer. For 32-bit system, it should be C:\Program Files\Common Files\ArcGIS\bin. Check the file location and modify this file before use if necessary.

SET DLL_DIR=%~dp0

CD C:\Program Files (x86)\Common Files\ArcGIS\bin

esriregasm /p:desktop %DLL_DIR%SWAT_HRUs.dll

esriregasm /p:desktop %DLL_DIR%SWAT_InputFileEdit.dll

esriregasm /p:desktop %DLL_DIR%SWAT_InputFileGen.dll

esriregasm /p:desktop %DLL_DIR%SWAT_PM2.dll

esriregasm /p:desktop %DLL_DIR%SWAT_RunSwat.dll

esriregasm /p:desktop %DLL_DIR%SWAT_WD.dll

Unregister previous ArcSWAT if necessary

If there is a previous version installed, it needs to be unregistered first. This process also depends on ArcGIS version.

  • ArcGIS 9.3

Run cmd as administrator and navigate to .NET folder (which is usually located at c:\Windows\Microsoft.NET\Framework\v2.0.50727). Execute following command for each of the six ArcSWAT .NET assemblies. It’s done when you see “Type un-registered successfully”.

regasm [assembly_path] /unregister

where

assembly_path is the path to the ArcSWAT .NET assembly, e.g. c:\swat\ArcSWAT\SWAT_PM2.dll

A bat file is also available here with following commands, which should also be executed as administrator.

SET DLL_DIR=%~dp0

CD %SystemRoot%\Microsoft.NET\Framework\v2.0.50727

regasm %DLL_DIR%\SWAT_HRUs.dll /unregister

regasm %DLL_DIR%\SWAT_InputFileEdit.dll /unregister

regasm %DLL_DIR%\SWAT_InputFileGen.dll /unregister

regasm %DLL_DIR%\SWAT_PM2.dll /unregister

regasm %DLL_DIR%\SWAT_RunSwat.dll /unregister

regasm %DLL_DIR%\SWAT_WD.dll /unregister

  • ArcGIS 10.0 and higher

Run cmd as administrator and navigate to ArcGIS bin folder (which is usually located at C:\Program Files (x86)\Common Files\ArcGIS\bin). Execute following command for each of the six ArcSWAT .NET assemblies. It’s done when you see a window saying “Unregistration succeeded”.

esriregasm /p:desktop /u
[assembly_path]

where

assembly_path is the path to the ArcSWAT .NET assembly, e.g. c:\swat\ArcSWAT\SWAT_PM2.dll

Similarly, a bat file could be used.

SET DLL_DIR=%~dp0

CD C:\Program Files (x86)\Common Files\ArcGIS\bin

esriregasm /p:desktop /u %DLL_DIR%SWAT_HRUs.dll

esriregasm /p:desktop /u %DLL_DIR%SWAT_InputFileEdit.dll

esriregasm /p:desktop /u %DLL_DIR%SWAT_InputFileGen.dll

esriregasm /p:desktop /u %DLL_DIR%SWAT_PM2.dll

esriregasm /p:desktop /u %DLL_DIR%SWAT_RunSwat.dll

esriregasm /p:desktop /u %DLL_DIR%SWAT_WD.dll