Bug in TIMEGEN

QUESTION: I found a bug in the TIMEGEN function. The dangerous thing is that the function returns a valid date, usually within a year or two of the correct date, so some programs (like mine) can hum along just fine while subtly producing incorrect results!

Here is a description of the problem. TIMEGEN returns an incorrect sequence of Julian dates when STEP_SIZE=-1, and UNITS="Months" and the data crosses a year boundary. Here is an IDL main-level program that demostrates the problem.

   theTimes = TIMEGEN( UNITS="Months", STEP_SIZE=-1, $
       START=Julday(1,15,2007), FINAL=Julday(10,15,2006))
   FOR j=0, N_Elements(theTimes)-1 DO BEGIN
        CALDAT, theTimes[j], month, day, year
        Print, month, year
   ENDFOR
   END

Running the program results in this output.

IDL> .go
Compiled module: JULDAY.
Compiled module: CALDAT.
           1        2007
          12        2005
          11        2006
          10        2006

Note the second, December, value is the wrong year.

ANSWER: The folks at ITTVIS confirm this is a bug in all versions of IDL through IDL 6.4. The good news is, TIMEGEN is a library routine you can fix yourself. Chris Torrence of ITTVIS suggests the following solution.

First, find the timegen.pro file in the IDL library. (This should be the lib folder or sub-directory in your IDL distribution.) Go to line 400 or thereabouts (it will vary slightly with your IDL version number) and find these two bad lines of code:

   monthArray = (monthArray MOD 12) + 1
   IF (step_size LT 0) THEN monthArray = (monthArray + 12) MOD 12

Completely replace those two line with these five good lines of code:

   IF (step_size ge 0) THEN BEGIN
      monthArray = (monthArray mod 12) + 1
   ENDIF ELSE BEGIN
      monthArray = (((monthArray mod 12) + 12) mod 12) + 1
   ENDELSE

This fix will be available in the next version of IDL.

Google
 
Web Coyote's Guide to IDL Programming