
IDL for IDLers IDL for IDLers An introductory course in programming techniques in IDL Steve Milan ([email protected]) Department of Physics and Astronomy, University of Leicester Steve Milan, September 2005 1 IDL for IDLers IDL for IDLers An introductory course in programming techniques in IDL Steve Milan ([email protected]) Department of Physics and Astronomy, University of Leicester 0. Introduction · 1. Getting started · 2. IDL basics · 3. Writing programs: Procedures and Functions · 4. Variables, arrays and matrices · 5. Loops · 6. Decision-making · 7. Input and output · 8. Plotting · 9. A worked example: Pogo · 10. Programming tasks §0. Introduction IDL (Interactive Data Language) is very similar to many other programming lan- guages, including C, FORTRAN, BASIC, and PASCAL. It contains similar con- structs: variables, loops, logic statements, subroutines, etc., though the syntax of the language will at first be unfamiliar and may appear idiosyncratic. However, once one computer language has been mastered it becomes straightforward to understand and program in another language. Instead of being related like English and Japanese, in which the words and grammar are very different, computer languages are rather more related like Cockney and Scouse: the words are all the same, they just sound different. So, if you are familiar with another programming language, chances are you will find IDL similar. Alternatively, if you have not programmed before, learning IDL will give you a head-start in any other language you will learn in the future. This course is designed, then, to give a taster in some basic programming techniques which are applicable to all computer languages; it just so happens the language we use will be IDL. Some languages such as C and FORTRAN are relatively prim-and- proper (the Queen’s English of languages) and are very fussy about how their data structures are set up. IDL on the other hand is more flexible and consequently is an easy language in which to get code up and running. In addition, IDL has many built- in graph-plotting facilities, removing from the programmer the burden of creating di- agrams from scratch. IDL is not only a computer language, it is also a programming and data analysis envi- ronment. In other languages you tend to write code and compile it in the computer’s basic operating system (or OS), often a variant of UNIX. Once a program is compiled it is executed or run from the operating system, and once the program has finished execution the user is dumped back in the OS. IDL is different. You enter the IDL environment, and within it write programs, compile them, execute them. This distinc- tion will become clear once you begin the course. Each chapter of the course (from 2 onwards) contains several simple tasks for you to complete. As you finish each chapter ask the demonstrator to tick off these tasks. The final chapter has several longer tasks for you to attempt; get the demonstrator to tick off on each of these tasks completed. Steve Milan, September 2005 2 IDL for IDLers §1. Getting started IDL has been implemented on several different computer platforms, including the UNIX and LINUX family of operating systems and Microsoft Windows™. §1.1 gives a brief introduction to UNIX-type operating systems and tells you how to start IDL in them. Ignore this if you are using Windows™. §1.2 gives a brief introduction to IDL in Windows™. Ignore this if you are using UNIX or LINUX. Finally, §1.3 tells you what files you should expect to find in your home directory when you log on to allow you to complete the course. Ignore this at your peril. §1.1. A quick introduction to UNIX and LINUX When you initially log into a machine and open a shell you will be at the mercy of the operating system (OS), probably UNIX or LINUX (for the purposes of this course they behave the same). You will be greeted with a prompt, maybe of one of the fol- lowing forms, depending on the set-up of the machine: bash-3.00$ /home/tom > (though do not be concerned if it is different) followed by a blinking cursor. You will start in your home directory, which if your username is tom might be called some- thing like /home/tom or /people/tom (the second example of prompt above shows the current directory that you are in; in what follows it is assumed that the prompt is of this form1). This is somewhere to store programs and data, though you might want to create subdirectories to help organise your files. The higher directory, e.g. /home, will contain subdirectories for each of the users registered on the system, of which the subdirectory /home/tom is one. /home is itself a subdirectory of the very highest (or root) directory /, which will contain other subdirectories which store machine-specific information, and with which you should not need to concern your- self. The diagram below shows the structure of a fictitious directory tree which might help you follow the description below. The fictitious UNIX directory structure described in this section At the prompt you can enter OS commands. Useful ones include pwd (print working directory name) 1 To set up your prompt to look like this, type (or this could be added to your .profile file): export PS1=’`echo $PWD` >’ Steve Milan, September 2005 3 IDL for IDLers /home/tom > pwd /home/tom /home/tom > which gives the current directory you are in (initially your home directory), ls (list) /home/tom > ls automaton.pro data model /home/tom > which lists the contents of the current directory. You can specify other directories to list, for instance the root directory, which may look something like: /home/tom > ls / bin etc lib64 model project software temp boot home lost+found opt qnx srv tftpboot cutlass initrd media people root staged tmp data ion misc people_rsl sbin staging usr dev lib mnt proc selinux sys var /home/tom > Alternatively, a useful shortcut can be /home/tom > ls .. bob sue tom which lists the contents of the directory above the current one, i.e. /home. Even more complex paths can be specified, for instance /home/tom > ls ../sue refers to the subdirectory sue in the directory above the current one, i.e. /home/sue. In the ls of /home/tom (see above) data and model refer to subdirectories (i.e. /home/tom/data and /home/tom/model), while automaton.pro is a pro- gram file. It can sometimes be difficult to distinguish between directories and other files, so ls –l gives more information: /home/tom > ls -l -rw-r--r-- 1 tom ph_ion 854 Apr 4 2003 automaton.pro drwxr-xr-x 2 tom ph_ion 4096 Apr 12 1999 data drwxr-xr-x 2 tom ph_ion 4096 Aug 25 2000 model /home/tom > The d in the first column indicates a subdirectory. Other information is contained here, including the date of creation of the file or directory and its size (854 bytes in the case of automaton.pro). To enter a subdirectory use cd (change directory): /home/tom > cd data /home/tom/data > would enter the subdirectory data assuming that it was within the current directory (/home/tom), or the complete path can be specified /home/tom > cd /home/tom/model /home/tom/model > which enters the directory /home/tom/model directly, irrespective of which direc- tory you are currently in. To make a new directory use mkdir, e.g.: Steve Milan, September 2005 4 IDL for IDLers /home/tom > mkdir programs /home/tom > cd programs /home/tom/programs > It is possible to edit an existing file, or make a new file and edit its contents, with the 2 nedit editor , e.g.: /home/tom > nedit my_program.pro & which will open a new window, and allow text to be entered. If the file my_program.pro already exists the existing text can be modified; if my_program.pro does not already exist it will be created once the file is saved. The & is UNIX jargon which means that the nedit window does not have to be closed before new commands can be entered at the prompt. Copies of existing files can be made using cp: /home/tom > cp automaton.pro copy_of_automaton.pro The name of a file can be modified or its location changed with mv (move): /home/tom > mv automaton.pro robot.pro /home/tom > mv automaton.pro programs/automaton.pro The first example would change the name of the file automaton.pro to ro- bot.pro, the second would remove the file automaton.pro from the current di- rectory (/home/tom) and place it in the directory /home/tom/programs. Last- ly, unwanted files can be removed with rm: /home/tom > rm copy_of_automaton.pro Beware: rm is irreversible! These UNIX commands should be all that you need to complete this course. §1.1.1. Starting IDL To start IDL simply type idl at the UNIX prompt: /home/tom > idl IDL> The IDL> prompt indicates that IDL is now running and you are within the IDL envi- ronment. Within the IDL environment you can still run UNIX commands by prefac- ing them with a $: IDL> $ls automaton.pro data model IDL> $nedit my_program.pro & The rest of this handbook will assume that you are within the IDL environment. 2 The moving cursor writes; and having writ / Moves on: nor all thy piety nor wit / Shall lure back to cancel half a line, / Nor all your tears wash out a word of it. — from the Rubaiyat of Omar Khayyām Steve Milan, September 2005 5 IDL for IDLers Finally, to end the IDL session and return to the UNIX operating system type IDL> exit /home/tom > §1.2.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages67 Page
-
File Size-