IDL Lab #4: Batch Processes Name: IDL Lab #4: A Sub-component of FOR 504 Advanced Topics in Remote Sensing The objectives of this laboratory exercise are to introduce the student to • • •

The DO WHILE LOOP DIALOG_PICKFILE() and the dir/CD commands Writing a Batch Process

The tasks provided within this lab are designed to help the student better understand the practical details of programming in IDL and will help you prepare for the class assessment. If you have problems: ASK! Location:

RS/GIS Lab

Login: Password:

XXXX XXXX

For further reading in this lab exercise, please refer to pages 102 (DO WHILE) and 150 (DIALOG_PICKFILE) in Practical IDL Programming, by Liam Gumley, Academic Press, 2002.

1

Before you start: Double click the ENVI icon on the desktop:

ENVI 4.0.lnk

This starts both ENVI (The Environment for Visualizing Images) and the IDL (Integrated Development Language) programming interface Ignore the ENVI toolbar but don’t close it as this closes IDL as well. USING THE DO WHILE STATEMENT TO CREATE A BATCH PROCESS 1.

INTRODUCING THE DO WHILE STATEMENT

In the last lab exercise we used the FOR loop. In this lab, we use another form of loop; that of the DO WHILE loop. The DO WHILE loop tells IDL to perform loop, while a condition remains true. This condition is usually the maximum value of a counter. The following program demonstrates the functionality of the DO WHILE loop: 1 2 3 4 5 6 7 8 9 10 11

PRO simple_while

END

2.

THE USEFULNESS OF DIALOG_PICKFILE() AND SETTING FIXED DIRECTORIES

max = 5 count = 0 WHILE (count LT max) DO BEGIN countdown = max - count PRINT, ‘Steps to go: ‘, countdown count = count + 1 ENDWHILE

In earlier labs, when we inputted image files we always wrote out the full pathname in our program codes. However, we will now use two simple pieces of code that can each reduce the need to write out filenames again and again and again and yet again…

2

A)

THE DIALOG_PICKFILE FUNCTION

In the scenario that you are not doing a batch process, but want the versatility to select a file from the directory, rather than typing in a new pathname each time; you probably want to use the DIALOG_PICKFILE() function. DIALOG_PICKFILE is a built in Graphical User’s Interface (GUI) function that when executed brings up a classic windows browse menu that allows you to select the file you want to input. To use DIALOG_PICKFILE you need to replace the following lines in your program: OPENR, 1, 'C:\ATRS\image1' A = FLTARR(400,400) READU, 1, A CLOSE, 1 With: OPENR, 1, DIALOG_PICKFILE() image = FLTARR(400,400) READU,1,image CLOSE,1 You can also set up a file filter when using the DIALOG_PICKFILE() function (e.g typing DIALOG_PICKFILE(filter = ‘*.dat’) will display only *.dat files in the browse menu). B) THE DIR AND CD COMMANDS To avoid typing in the full pathname when opening files you can write: dir = ‘’ CD, dir This tells IDL that all the files after these lines are coming from the directory. To open a file you then need only write: OPENR, 1, filename One use of this command is to input files form one directory and save the output in a separate directory, which could be helpful if you are dealing with large quantities of images. Task #1 Modify you program array.pro from the previous lab such that you use the DIALOG_PICKFILE() function to select your input image.

3

3.

CREATING A BATCH PROCESS

A very useful piece of code to use in the analysis of remote sensing data is to create a batch process that can repeat a set of procedures for a set of individual images. Before we write this program we are going to use ENVI to create our set of images to analyze. Minimize the IDL window and select the ENVI toolbar:

By selecting File/Open Image File open the file texture_1000 used in lab3. Next use Basic Tools/Subset Image (spatial/spectral)/ by Image Option to create 5 new images of size 200 x 200 (you can choose what you select). Next, in the Available Bands List window use the File/Close All Files option. Next, Using the File/Open Image File option, delete all the header (.hdr) files from the five images as these will just confuse IDL are not needed. Next, in this directory create a new folder called ‘outputs’. Now minimize ENVI and bring up the IDL window again. Task #2 As an example of batch processes we will produce a new array program that reads in each of the files and prints out the minimum, maximum, mean, and standard deviation of each file in a simple Table. Firstly we will use the dir and CD commands to set the directory to where we saved the five image files: 1 2 3 4 5 6 7 8 9 10 11 12 13

PRO batch dir = ‘’ CD, dir ;; The Batch Process files = FILE_SEARCH(‘image*’, COUNT=numfiles) ;; This command searches for all files in the directory that start with the name image (i.e. ;; image1, image2, image3, etc). counter = 0

; This is a counter that tells IDL which file is being read – it starts at zero

4

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

WHILE (counter LT numfiles) DO BEGIN ;; This command tells IDL to start a loop and to only finish when the counter is equal to ;; the number of files with the name starting image name=files(counter) OPENR, 1, name ;; opens the first file A=INTARR(200, 200);; Assuming the data type is an integer! You can check this in ;; ENVI READU, 1, A ;; Reads to a temporary file high = MAX(A) low = MIN(A) avg = MEAN(A) sdev = STDDEV(A) PRINT, ‘The measures for image’, counter + 1, ‘ are:’ PRINT, ‘Max: ‘, high, ‘; Min’, low, ‘; Mean: ‘, avg, ‘; Standard Deviation: ‘, sdev PRINT, ‘’ CLOSE, 1 counter = counter +1 ENDWHILE END

Task #3 Alter the batch program to make a new set of five images (from you saved images) that are have values of 100 for the first; 200 for the second; and so on.

Next week we will make use of the CASE, SWITCH, BREAK and CONTINUE statements

5