2.5 Batch File Scripting




In the previous two sections, as will be the case in many of the subsequent sections, the top navigation bar contained two links, one to a LINUX script and the other to a WINDOWS batch file. Instead of following the step-by-step instructions to run the example through the GUI, it is possible to directly execute a series of commands that will accomplish the same task. Scripting will not be addressed in this tutorial. However, the advanced topics tutorial is entirely devoted to scripted examples. In this section, we will review the steps contained in the previous batch file that was used to run the concentration example. If you are running LINUX scripts, you first need to convert them from DOS format using the dos2unix command.

  1. All the batch files can be found in the \Tutorial\batch directory. The script can be run directly by double-clicking on the test_conc.bat file. However, if there are errors or other diagnostic messages resulting in a premature termination, you may not be able to read the messages prior to the window closing. Therefore, for easier script development, it is better to open a command line window to run the batch file. Under Windows-10, go to All Apps, Windows System and the open the Command Prompt window.

  2. From the command prompt change directory to the batch file directory: cd \Tutorial\batch. Then type the name of the batch file test_conc followed by Enter. The .bat suffix is assumed. This causes the command processor to execute each line of the file and the results are shown in the command prompt window. The script stops when the concentration graphic is displayed. All the files from the script are read from and written to the /hysplit/working directory. Closing the graphic X also terminates the script.

Open the main script test_conc.bat in notepad where you can see that it is composed of seven sections. The first section sets the batch file execution parameters. The second section sets the value of script variables relating to file locations. The third section creates the ASCDATA.CFG file which points to the directory that contains various default files used by HYSPLIT. The fourth section creates the main CONTROL file for HYSPLIT listing start times, locations, and other important parameters. The fifth section gives the command that actually runs the model. The sixth section creates the graphic label file and runs the Postscript concentration plotting program. The seventh, and last section, displays the concentration graphic, and deletes the label file before closing.

@echo off   turns off command echo
setLocal EnableDelayedExpansion   sets values at run time

set DIR=c:   assigns drive to variable DIR
set PGM=%DIR%\hysplit4   assigns hysplit to variable PGM
cd %PGM%\working   change directory to working

echo -90.0 -180.0 >ASCDATA.CFG   creates new file
echo 1.0 1.0 >>ASCDATA.CFG   appends line to file
echo ... >>ASCDATA.CFG   continuing adding lines to end

echo 95 10 16 00 >CONTROL   create CONTROL file with starting time
echo 1 >>CONTROL   number of starting locations
echo 40.0 -90.0 10.0 >>CONTROL   next line first starting location
echo ... >>CONTROL   continuing adding lines until the end

%PGM%\exec\hycs_std   run HYSPLIT executable

echo 'TITLE^&','### %0 ### ^&' >LABELS.CFG   create graphic label file with name of script
%PGM%\exec\concplot -icdump   run concentration plotting program

concplot.ps   display concentration graphic

Each of these commands could be typed line-by-line on the command line, or you could just simply type the name of the test_conc.bat file and windows knows to execute the commands in the file. When a procedure is to be run multiple times, there are clear advantages to scripting. However, a hidden advantage is that scripts permit customization of certain procedures using program options that are not always available through the GUI. All HYSPLIT programs are in the \exec directory. To see the options associated with concplot, type: ..\exec\concplot without any arguments which yields a very long list of command line options. Only some of these are available through the GUI, but all of them could be implemented through the command line or script.

Other scripting examples can be found in \hysplit4\testing\xrun.bat, which runs through a series of different calculations. Example scripts for Windows, TCL, and UNIX, can be found in \hysplit4\examples\scripts. Batch files to mimic other GUI applications, especially the programs called and their command line options, can be determined by examining the relevant tcl script in the guicode directory. Batch file commands can be displayed from the command prompt by typing help.