Creating an Unique File Name

QUESTION: I would like to create an unique file name in IDL. I thought I could append the output from the SYSTIME function to a name, creating a sort of time stamp, but I find in some circumstances, such as creating file names in a loop, I don't get unique names. I've even tried to use the RANDOMU function to append a random number to the file name, but this doesn't always work, either. Is there a trick I don't know about?

ANSWER: Using the SYSTIME and RANDOMU functions to create a unique time-stamped file name is a good idea, but you are right. If filenames are created quickly, as in a loop, the file names are not always unique. Mostly, this is because to get a truly random number sequence you have to maintain the seed for the random number generator in a common block or in some other way. If the seed is not maintained, then the random number generator is, uh, not very random.

To create a unique file name in IDL, I use the cgTimeStamp program from the Coyote Library. This program gives me nearly a dozen ways to format a time stamp, including time stamps that include a sequence of random numbers of any specified length. The cgTimeStamp program uses the RandomNumberGenerator object to maintain the seed and guarantee a truly random sequence of digits for time stamp use.

For example, I can use the program like this to calculate a time stamp with 6 random digits appended to the time stamp to guarantee its uniqueness.

   IDL> FOR j=0,5 DO Print, 'test' + cgTimeStamp(RANDOM_DIGITS=6)
        test_sun_feb_07_22:25:54_2010_214099
        test_sun_feb_07_22:25:54_2010_884085
        test_sun_feb_07_22:25:54_2010_808354
        test_sun_feb_07_22:25:54_2010_561073
        test_sun_feb_07_22:25:54_2010_250340
        test_sun_feb_07_22:25:54_2010_317126 

The cgTimeStamp program has 11 different ways to format the time stamp. For example, if I want a mmddyyyy format, I can call the program like this.

   IDL> FOR j=0,5 DO Print, 'test' + cgTimeStamp(10, RANDOM_DIGITS=4)
        test_02072010_4933         test_02072010_0044         test_02072010_0975
        test_02072010_3827         test_02072010_9143         test_02072010_8998

The cgTimeStamp program uses local time by default, but you can also specify UTC time, by using the UTC keyword. Here is UTC time in another cgTimeStamp format.

    IDL> FOR j=0,5 DO Print, 'test' + cgTimeStamp(8, RANDOM_DIGITS=4, /UTC)
        test_02_08_2010_05:32:59_9888         test_02_08_2010_05:32:59_0406
        test_02_08_2010_05:32:59_4945         test_02_08_2010_05:32:59_1500
        test_02_08_2010_05:32:59_4103         test_02_08_2010_05:32:59_5946

Finally, you may want to use this time stamp for variable names, and so on. In the example above the colons would be illegal characters in a variable name. By setting the VALID keyword, all illegal variable name characters are turned into legal or valid characters. For example, here are valid IDL variable names.

   IDL> FOR j=0,5 DO Print, 'test' + cgTimeStamp(8, RANDOM_DIGITS=4, /UTC, /VALID)
     test_02_08_2010_05_36_14_2706      test_02_08_2010_05_36_14_2365
     test_02_08_2010_05_36_14_2328      test_02_08_2010_05_36_14_3148
     test_02_08_2010_05_36_14_0973      test_02_08_2010_05_36_14_4026 

Using some version of the cgTimeStamp program, it is quite easy to produce unique file names in IDL.

Version of IDL used to prepare this article: IDL 7.1.2

Google
 
Web Coyote's Guide to IDL Programming