idlwave: Lesson I---Development Cycle

 
 3.1 Lesson I: Development Cycle
 ===============================
 
 The purpose of this tutorial is to guide you through a very basic
 development cycle using IDLWAVE.  We will paste a simple program into a
 buffer and use the shell to compile, debug and run it.  On the way we
 will use many of the important IDLWAVE commands.  Note, however, that
 IDLWAVE has many more capabilities than covered here, which can be
 discovered by reading the entire manual, or hovering over the shoulder
 of your nearest IDLWAVE guru for a few days.
 
    It is assumed that you have access to Emacs or XEmacs with the full
 IDLWAVE package including online help.  We also assume that you are
 familiar with Emacs and can read the nomenclature of key presses in
 Emacs (in particular, ‘C’ stands for <CONTROL> and ‘M’ for <META> (often
 the <ALT> key carries this functionality)).
 
    Open a new source file by typing:
 
      C-x C-f tutorial.pro <RET>
 
    A buffer for this file will pop up, and it should be in IDLWAVE mode,
 indicated in the mode line just below the editing window.  Also, the
 menu bar should contain ‘IDLWAVE’.
 
    Now cut-and-paste the following code, also available as
 ‘tutorial.pro’ in the IDLWAVE distribution.
 
      function daynr,d,m,y
        ;; compute a sequence number for a date
        ;; works 1901-2099.
        if y lt 100 then y = y+1900
        if m le 2 then delta = 1 else delta = 0
        m1 = m + delta*12 + 1
        y1 = y * delta
        return, d + floor(m1*30.6)+floor(y1*365.25)+5
      end
 
      function weekday,day,month,year
        ;; compute weekday number for date
        nr = daynr(day,month,year)
        return, nr mod 7
      end
 
      pro plot_wday,day,month
        ;; Plot the weekday of a date in the first 10 years of this century.
        years = 2000,+indgen(10)
        wdays = intarr(10)
        for i=0,n_elements(wdays)-1 do begin
            wdays[i] =  weekday(day,month,years[i])
        end
        plot,years,wdays,YS=2,YT="Wday (0=Sunday)"
      end
 
    The indentation probably looks funny, since it’s different from the
 settings you use, so use the <TAB> key in each line to automatically
 line it up (or, more quickly, _select_ the entire buffer with ‘C-x h’,
 and indent the whole region with ‘C-M-\’).  Notice how different
 syntactical elements are highlighted in different colors, if you have
 set up support for font-lock.
 
    Let’s check out two particular editing features of IDLWAVE.  Place
 the cursor after the ‘end’ statement of the ‘for’ loop and press <SPC>.
 IDLWAVE blinks back to the beginning of the block and changes the
 generic ‘end’ to the specific ‘endfor’ automatically (as long as the
 variable ‘idlwave-expand-generic-end’ is turned on; SeeLesson
 II---Customization).  Now place the cursor in any line you would like
 to split and press ‘M-<RET>’.  The line is split at the cursor position,
 with the continuation ‘$’ and indentation all taken care of.  Use ‘C-/’
 to undo the last change.
 
    The procedure ‘plot_wday’ is supposed to plot the day of the week of
 a given date for the first 10 years of the 21st century.  As in most
 code, there are a few bugs, which we are going to use IDLWAVE to help us
 fix.
 
    First, let’s launch the IDLWAVE shell.  You do this with the command
 ‘C-c C-s’.  The Emacs window will split or another window will popup to
 display IDL running in a shell interaction buffer.  Type a few commands
 like ‘print,!PI’ to convince yourself that you can work there just as
 well as in a terminal, or the IDLDE.  Use the arrow keys to cycle
 through your command history.  Are we having fun now?
 
    Now go back to the source window and type ‘C-c C-d C-c’ to compile
 the program.  If you watch the shell buffer, you see that IDLWAVE types
 ‘.run "tutorial.pro"’ for you.  But the compilation fails because there
 is a comma in the line ‘years=...’.  The line with the error is
 highlighted and the cursor positioned at the error, so remove the comma
 (you should only need to hit ‘Delete’!).  Compile again, using the same
 keystrokes as before.  Notice that the file is automatically saved for
 you.  This time everything should work fine, and you should see the
 three routines compile.
 
    Now we want to use the command to plot the day of the week on January
 1st.  We could type the full command ourselves, but why do that?  Go
 back to the shell window, type ‘plot_’ and hit <TAB>.  After a bit of a
 delay (while IDLWAVE initializes its routine info database, if
 necessary), the window will split to show all procedures it knows
 starting with that string, and ‘plot_wday’ should be one of them.
 Saving the buffer alerted IDLWAVE about this new routine.  Click with
 the middle mouse button on ‘plot_wday’ and it will be copied to the
 shell buffer, or if you prefer, add ‘w’ to ‘plot_’ to make it
 unambiguous (depending on what other routines starting with ‘plot_’ you
 have installed on your system), hit <TAB> again, and the full routine
 name will be completed.  Now provide the two arguments:
 
      plot_wday,1,1
 
 and press <RET>.  This fails with an error message telling you the ‘YT’
 keyword to plot is ambiguous.  What are the allowed keywords again?  Go
 back to the source window and put the cursor into the “plot” line and
 press ‘C-c ?’.  This shows the routine info window for the plot routine,
 which contains a list of keywords, along with the argument list.  Oh, we
 wanted ‘YTITLE’.  Fix that up.  Recompile with ‘C-c C-d C-c’.  Jump back
 into the shell with ‘C-c C-s’, press the <UP> arrow to recall the
 previous command and execute again.
 
    This time we get a plot, but it is pretty ugly: the points are all
 connected with a line.  Hmm, isn’t there a way for ‘plot’ to use symbols
 instead?  What was that keyword?  Position the cursor on the plot line
 after a comma (where you’d normally type a keyword), and hit ‘M-<Tab>’.
 A long list of plot’s keywords appears.  Aha, there it is, ‘PSYM’.
 Middle click to insert it.  An ‘=’ sign is included for you too.  Now
 what were the values of ‘PSYM’ supposed to be?  With the cursor on or
 after the keyword, press ‘M-?’ for online help (alternatively, you could
 have right clicked on the colored keyword itself in the completion
 list).  A browser will pop up showing the HTML documentation for the
 ‘PYSM’ keyword.  OK, let’s use diamonds=4.  Fix this, recompile (you
 know the command by now: ‘C-c C-d C-c’), go back to the shell (if it’s
 vanished, you know what to do: ‘C-c C-s’) and execute again.  Now things
 look pretty good.
 
    Let’s try a different day.  How about April fool’s day?
 
      plot_wday,1,4
 
    Oops, this looks very wrong.  All April Fool’s days cannot be
 Fridays!  We’ve got a bug in the program, perhaps in the ‘daynr’
 function.  Let’s put a breakpoint on the last line there.  Position the
 cursor on the ‘return, d+...’ line and press ‘C-c C-d C-b’.  IDL sets a
 breakpoint (as you see in the shell window), and the break line is
 indicated.  Back to the shell buffer, re-execute the previous command.
 IDL stops at the line with the breakpoint.  Now hold down the SHIFT key
 and click with the middle mouse button on a few variables there: ‘d’,
 ‘y’, ‘m’, ‘y1’, etc.  Maybe ‘d’ isn’t the correct type.  CONTROL-SHIFT
 middle-click on it for help.  Well, it’s an integer, so that’s not the
 problem.  Aha, ‘y1’ is zero, but it should be the year, depending on
 delta.  Shift click ‘delta’ to see that it’s 0.  Below, we see the
 offending line: ‘y1=y*delta...’ the multiplication should have been a
 minus sign!  Hit ‘q’ to exit the debugging mode, and fix the line to
 read:
 
      y1 = y - delta
 
    Now remove all breakpoints: ‘C-c C-d C-a’.  Recompile and rerun the
 command.  Everything should now work fine.  How about those leap years?
 Change the code to plot 100 years and see that every 28 years, the
 sequence of weekdays repeats.