IDL Lab 1 - The IDL Interface

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

IDL Lab #1 Introduction to IDL and ENVI for Remote

Sensing and Image Processing Analysis

Name:

Lab #1: FOR 504 Advanced Topics in Remote Sensing

Objectives of this laboratory exercise:

Introduce the student to the initial concepts and use of IDL and ENVI to:
import, analyze, and output image data
learn to interface between ENVI input/outputs and IDL code

The questions provided within this lab are designed to help the student better understand the
practical details of programming in IDL and are recommended but are not for assessment.

Location: RS/GIS Lab

Login: XXXX
Password: XXXX

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 dont close it as this closes IDL as well.

Practical #1: Navigating around IDL

This task introduces the user to the IDL interface:

The menu and toolbars are explanatory but we will typically only be making use of the
File and Run menu bars.
Programs are written within the editor box
When a program is run, all the subroutines that are called or errors identified are
displayed in the log area
The command line allows direct commands to be typed into IDL without the need of a
written program

Menu and Toolbars

Editor Box
Objects
and
Groups

Log Area

Variable Information Area

Command Line

2
Task #1: Importing a file in IDL and outputting a modified file to be read by ENVI

Step 1 - Starting an IDL Program

In IDL press CTRL+N or go to FILE/NEW/Editor to open a new program editor.

Each program in IDL must be called from the saved files. This is done by starting each
program with the command PRO followed by its program name. All programs must be finished
with the command END.

To work in IDL this program needs to be saved as the identical word typed after PRO: i.e. in this
case intro. This is done using FILE/SAVE AS from the menu bar.

Step 2 Using the Print Command

To create the archetypical Hello World! program we need to use the PRINT to screen
command.

The SYNTAX NOTES below shows you how the command and text should be written within
IDL.

SYNTAX NOTE for Print to Screen:

PRINT, Enter Text Here

SYNTAX NOTES like this will be used throughout this and future labs as a source of future
reference.

In the editor box write the text: PRINT, Hello World!

Once you have written the text you would like to print within the program you first need to save
the changes to the program by pressing the SAVE icon in the menu bar.

3
Press CTRL+F5 (OR RUN/COMPILE from the menu bar) to compile the program.

Press F5 (OR RUN/ RUN <program name>


from the menu bar) to run the program.

If any syntax errors exist in the program these


will be highlighted by the line number in the
Log area. Try this out by entering random code
into your basic program:

Errors are highlighted on


each line by a blue arrow

Errors are also highlighted in


the LOG area

If the program is successful, then the output should be presented in the LOG area:

After each program is compiled and run it is necessary to clear the computers temporary
memory. This is achieved by typing retall into the command line and pressing enter.

To end a program that has hung the system, use RUN/stop followed by RUN/Resolve
dependencies from the menu bar.

Well done, you have just completed your first IDL program!

4
Step 3 Entering Image Data into IDL for Analysis

To open an image file in IDL that can be read or modified you need to use the OPEN command,
of which there are three variations in IDL:

Namely,

OPENR (OPEN Read) - opens a file that can be read only


OPENW (OPEN Write) - opens a file that an be written to
OPENU (OPEN Update) opens a file that can be read or written to

SYNTAX NOTE for OPEN:


OPENR, Unit, File Path
OPENW, Unit, File Path
OPENU, Unit, File Path

EXAMPLE: OPENR, 1, C:\folder\filename

Each separate file must have a unique unit number so that is can be distinguished. The file
path includes the directory.

We want to make our Hello World! program more impressive by taking in an image file and
creating a modifed copy that ENVI can display.

In the editor box type the following lines (the comments are optional):

OPENR, 1, 'C:\ATRS\image1' ;; This line opens the file


A = FLTARR(400,400) ;; This line creates an array of size 400 (rows) by

;; 400 (columns). Placing FLT before ARR tells IDL that the file contains real (floating point)
;; numbers. You would type INTARR for an array of integers and BYTARR for an array of
;; bytes. There are many many more ARR types that can be found in the IDL help file.

READU, 1, A ;; This line tells IDL to place the image into the array A
CLOSE, 1 ;; This line tells IDL that we are finished reading the file.

You will use these 4 lines of code again and again when programing in IDL.

Your editor should now look like this:

5
We now want to create an exact copy of the image, add 100 to each value and then write the
result to a new file so that it can be read by ENVI. To do this we are going to create two other
arrays called B and C:

Type the following in the editor box:

B=A ;; Creates a new array B that is identical to A


C = B + 100 ;; Each value in B has 100 added to it and this result in
;; saved as C
OPENW, 2, 'C:\ATRS\output1' ;; Creates a new file called output1
WRITEU, 2, C ;; Writes the data in array C into the file output1
CLOSE, 2 ;; This line tells IDL that we are finished reading the file.

The editor box should now look like this:

6
This is our complete IDL program. We now want to save, compile and run the program
following the instructions in Step 2.

If any errors occur check that all the commas are where they should be; the file name; and that
the original file is in the correct directory.

Well done, you have completed your 2nd IDL program!

We are no going to turn away from IDL and use ENVI for the second half of this Lab.

7
Task #2: Importing the modified IDL file into ENVI

Minimize the IDL work area and select the ENVI toolbar:

To input the modified file we just created in IDL we select FILE/Open Image File and then
select the file name (not the .hdr file) from the open file window:

This file currently does not have a header file (.hdr). These files contain information on the size
and type of numbers contained within the file. Each time a new file such as this is entered into
ENVI you need to manually tell ENVI this information by using the following box that appears
once you press open in the box above.

8
The file we outputted in IDL had 400
rows (lines) and 400 columns (samples).

It contained only 1 band. If this was a true


color image (i.e RGB), then the number of
bands would equal 3.

The second line of values are only used if


this image needs to be shifted by rows or
columns and is not used here. The default
is 0, 1, 1.

The data type depends on what type of


ARR you used in IDL or what type of
numbers your unknown image file is. We
used FLTARR so select Floating Point.

The file type and the Interleave will be


provided in the image header files. We are
Using BSQ for our IDL file.

Pressing OK brings up the Available Bands List, which tells you what files are currently open
in ENVI. If you double click on Band1 of ouput1 you will get to view the IDL file you created:

9
To open the original unaltered file, you can also use the Available Bands List.

Press File/Open Image File and select the file image1. The file image1 will appear in the
Available Bands List box.

To display this image alongside the modified file you first need to create a new display to view it
in. This is done by clicking on the button Display #1 and selected New Display.

You will see that the Display #1 button now says Display #2 and that a blank image box has
appeared. If you now double click on band 1 of image 1 this should display image 1 in this
display box:

Main Image Window

Zoom
Window

As you can see these images look identical so how do we know if we actually did anything with
the IDL program.

To assess pixel values within an image double left click on the main image window.

10
This produces the cursor Location/Value box, which tell you information of the pixel the cursor
is on.

The 1st line tells you the column and row of the
pixel, and the RGB value of the pixel
The 2nd 4th lines tells you the projection and
datum the image and also provides the map
coordinates
The 5th line gives you the data value of the pixel

By moving the cursor between the two displays, the cursor location/value box will change
depending which image is selected.

However, this is not very helpful, is you want to actually compare to exact pixel values. To do
this Press: Tools/Link/Link Displays from the main image menu bar:

This box allows you to link two or more


image together such that is you move around
one image, the other will also move.

This feature also allows you to directly


compare pixel values between two or more
images.

Make sure all the values are 1 and press OK.

Now when you move the cursor around either image the values of both the images will be
displayed in the Cursor Location/Value box.

As you can see the IDL program worked as the


output1 (Display #1) has a value that is exactly
100 greater then image1 (Display #2).

Well Done! You have completed the first Lab.

11

You might also like