# iMOD User Manual version 4.4 (html)

#### 8.1General introduction

This chapter contains a detailed description of a library of tools or functions that is available in iMOD-Batch mode.

##### 8.1.1What is an iMOD Batch Function?

An iMOD Function in general is a procedure to perform a specific task in iMOD or on iMOD files returning a new file or a series of files. Functions can be found on many places in the menu structure of the iMOD Graphical User Interface (GUI). For instance functions to:

• • create, edit or analyse files (IDF, IPF ect).

• • run processes like iMODFLOW and iMODPATH.

• • post process modelresults (waterbalance, timeseries or plots).

An iMOD Batch Function can be used outside the iMOD GUI and its operation is controlled by parameters in an *.INI file, the initialization file.

Below you find an example of the content of a PLOT.INI file. It describes the keywords and parameters for the Function PLOT, for plotting IDF or IPF files to a PNG file.

FUNCTION=PLOT
IDFFILE=D:\DATA\AHN.IDF
GENFILE=D:\DATA\PROV.GEN
OUTFILE=D:\DATA\PLOT.PNG

In general an *.INI file contains keywords some of which are compulsory (e.g. the Function name), others are optional. iMOD searches for the keywords in the entire file so there is no fixed order of keywords allowing you to insert white lines and remarks. Examples of the set up of such an *.INI file for all available iMOD Batch function is given in the following paragraphs.

The advantage of using the iMOD Batch mode instead of the iMOD Graphical User Interface (GUI) is manifold:

• • it saves time when iMOD functions must be performed on many files or many times.

• • the total set of *.INI files in a project can be seen as a logbook of your pre- and post-processing activities.

• • minor changes to a function are easily applied by editing the *.INI file.

• *.INI files are easily shared with colleagues or clients.

• over time the *.INI files form a library, easy to re-use.

• • you don’t need to click around in the GUI and repeatedly type file names.

The number of functions in iMOD Batch mode is expected to grow gradually. Not all functions in the iMOD GUI are (yet) available in iMOD Batch mode (e.g. create IDF from scratch). On the other hand, some functions are available in iMOD Batch mode but not in the GUI (e.g. Modelcopy).

##### 8.1.2How to run an iMOD Batch Function?

To start an iMOD Batch procedure first open a Dos command window. How? Search for ’CMD’ in ’All Programs’ under the Windows start button ). In that DOS window, simply enter the name of the iMOD executable followed by the name of the *.INI file, e.g. "iMOD_V4_3_X64R.exe IDFSCALE.INI", (see Figure 8.1) and press Enter. The program will stop after the function, described by the *.INI file, is executed.

An iMOD batch function can also be started from the maim menu. See Section 4.5 for an explanation.

##### 8.1.3Using DOS scripting (*.BAT file) to organize iMOD Batch Functions

The iMOD Batch functions can be used as described above, however, to introduce more options these functions can also be controlled in a DOS Batch file (*.BAT) using DOS scripting. Of course you can use your favourite scripting languages (e.g. Python or Matlab) to create and execute any iMOD Batch *.INI file.

This is an example of a *.BAT file creating (line 1-4) and executing (line 5) the PLOT.INI file:

ECHO FUNCTION=PLOT $>$ PLOT.INI
ECHO IDFFILE=D:\DATA\AHN.IDF $>$$>$ PLOT.INI
ECHO GENFILE=D:\DATA\PROV.GEN $>$$>$ PLOT.INI
ECHO OUTFILE=D:\DATA\PLOT.PNG $>$$>$ PLOT.INI
iMOD_V4_3_X64R.exe PLOT.INI

A *.BAT file is recognized in Windows as a Dos script and starts running by double clicking the *.BAT file.

##### 8.1.4Examples of advanced DOS scripting options

Advanced DOS scripting options enables you to make more complex use of iMOD Batch functions. This paragraph gives you some examples of advances options. On the internet there are plenty sites available giving basic or advanced DOS scripting tips and tricks in more detail (e.g. http://www.robvanderwoude.com/batchcommands.php).

With the installation of iMOD an example of a *.BAT file is copied to the Tutorial folder. Search for ..\TUTORIALS \TUT_Map_Analyse \SubsoilSystem \iMOD-Batch-example-IDFCALC.BAT. Open the file, read some tips and tricks, double click and experience that this iMOD Batch function calculates the thickness of a series of 6 aquifers.

Below we describe some examples of advanced DOS scripting options.

• • A (nested) loop over a list of Text elements
Not more than 6 lines in the next example are necessary to create and run the iMOD Batch Function PLOT producing 12 PNG files showing the HEAD for the first 4 month of 3 successive years.

FOR %%A IN (1994,1995,1996) DO (
FOR %%B IN (01,02,03,04) DO (
ECHO FUNCTION=PLOT $>$ PLOT.INI
ECHO IDFFILE=D:\MODEL\HEAD\HEAD_%%A%%B01_L1.IDF $>$$>$ PLOT.INI
ECHO OUTFILE=D:\MODEL\HEAD\HEAD_%%A%%B01_L1.PNG $>$$>$ PLOT.INI
iMOD_V4_3_X64R.exe PLOT.INI))

• • A loop over a series of Numbers
The next example of a FOR loop (including Command Extension /L) gives you the variable %%A between 1 upto 18 with steps of 2.

FOR /L %%A IN (1,2,18) DO (
ECHO %%A)
)

• • Using specific files from a folder
The next DOS script gives you the variable %%A for all IDF-files found in the folder D:\IMOD-MODEL\DBASE. Usage of ˜n gives you the filenames only without extension.

SET DIR=D:\IMOD-MODEL\DBASE
FOR %%A IN (%DIR%\*.IDF) DO (
ECHO %DIR%\%%˜nA.IDF
ECHO %%A
)

• • Introduce an arithmetic operator within a loop
Adding a looping parameter %%b might be interesting whenever computing the thickness of aquitards since each time you need to subtract the bottom of modellayer 1 minus the top of modellayer 2.

SETLOCAL ENABLEDELAYEDEXPANSION
SET /A B=1
FOR /L %%C IN (1,1,18) DO (
SET /A B=B+1
ECHO FUNCTION=IDFCALC $>$ IDFCALC.INI
ECHO FUNC=C=A-B $>$$>$ IDFCALC.INI
ECHO NREPEAT=1 $>$$>$ IDFCALC.INI
ECHO ABC1=BOT%%C.IDF TOP!B!.IDF TAQT%%C.IDF $>$$>$ IDFCALC.INI
iMOD_V4_3_X64R.exe IDFCALC.INI
)

Notice that the variable B is used by bracketing it by “!” and the variable C is used by adding “%%” in front. The statement SETLOCAL ENABLEDELAYEDEXPANSION is necessary to delay the interpretation of the variable B.

• • include IF-THEN-ELSE statements
It is often handy to include and if-then-else statement inside the batch structure, for example whenever you might want the generate a runfile for iMODPATH (see Section 8.6.6). Unfortunately, the MS-DOS IF statement does not support logical operators (AND and OR) so we have to find other ways.

In the example below we only write a budget term whenever it is not equal to the BDGFLF (flow-lower-face) and the layer number is not equal to the lowest one (5).
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,5) DO (
FOR %%B IN (FFF,FRF,FLF) DO (
SET /A Flag=0
IF %%B==FLF SET /A Flag=Flag + 1
IF %%A==5 SET /A Flag=Flag + 1
IF !Flag! NEQ 2 (
ECHO ..\BDG%%B\BDG%%B_STEADY-STATE_L%%A.IDF $>$$>$ IMODPATH.INI)
))

If an OR-statement need to be applied, do as follows:
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,5) DO (
FOR %%B IN (FFF,FRF,FLF) DO (
SET res=F
IF %%B==FLF SET res=T
IF %%B==FRF SET res=T
IF !res!==T (
ECHO ..\BDG%%B\BDG%%B_STEADY-STATE_L%%A.IDF $>$$>$ IMODPATH.INI)
))

• • Compare operators in DOS
Use the following compare operators:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

The following syntax is than valid:

FOR /L %%A IN (1,1,10) DO (
IF %%A LSS 6 (ECHO value %%A is less than 6
) ELSE IF %%A GTR 6 (ECHO value %%A is greater than 6
) ELSE (ECHO value %%A is equal to 6
))

• • Start execute process within a DOS script
Finally the iMOD executable can be started in different ways with different behavior. Any *.INI can be started with:

FOR %%A IN (1,2) DO (
iMOD_V4_3_X64R.exe {}.INI
)

however this will block the batch (*.BAT) structure that starts it until the process is ended. Below an example is given that starts iMOD and continues the batch and starts another iMOD session in another command-window.

FOR %%A IN (1,2) DO (
START iMOD_V4_3_X64R.exe {}.INI)

Batch array definition
In addition to the described batch looping commands, it is also possible to define arrays within the batch environment. This might be helpful in case e.g. there are multiple files with different filenames that can be divided into a number of subgroups and that need to be processed in a similar way. For example:

SETLOCAL ENABLEDELAYEDEXPANSION
SET N=0
FOR %%A IN (KHV, KVV) DO (
SET VAR[!N!]=%%A
SET /A N+=1
) FOR /L %%I IN (0,1,1) DO (
FOR /L %%J IN (1,1,12) DO (
ECHO FUNCTION=IDFCALC > CALC_IDF.INI
ECHO FUNC= "C=A*B" » CALC_IDF.INI
ECHO NREPEAT=1 » CALC_IDF.INI
ECHO ABC1= "D:\MODEL\FACTOR.IDF" "D:\MODEL\DATA\!VAR[%%I]!_L%%J.IDF"
"D:\MODEL\DATA\!VAR[%%I]!_L%%J_FACTOR.IDF" » CALC_IDF.INI

D:\IMOD_VERSIES\IMOD_V4.3\IMOD_X64R.EXE CALC_IDF.INI
))

In this example two type of files can be distinguished, files with 1. "KHV" and 2. "KVV" information. Each file category contains 12 files; one for each layer. All files need to be multiplied by specific factor grid, saved in Factor.idf. With the outer loop the file category is controlled and with the second loop the layers per category. Array definition can be applied to multiple cases and serve as a helpful tool to shorten your batch-scripts.

The iMOD Batch function are categorized into several topics related to IDF, IPF, ISG and GEN files and described on the following pages.

##### 8.1.5iMOD Python Package

The iMOD Python package is an addition to iMOD and iMODFLOW, intended to facilitate working with groundwater models from Python. It does this by supporting reading and writing of the different iMOD file formats to existing objects often used in Python data processing.

It can be installed with “pip install imod”. The source code is freely available at https://gitlab.com/deltares/imod/imod-python. Issues with the package can be reported there, and code contributions are also welcome. Documentation can be found at https://deltares.gitlab.io/imod/imod-python/.