Coyote's Guide to IDL Programming

Suppressing READF Jump to Next Line

QUESTION: John Kwiatkowski (johnmk@shell.clark.net) asked this question in a recent IDL newsgroup article:

I have an ASCII data set that is giving me problems. I need to suppress the return when using READF since I only want to read in part of the line. Depending on the value of one of the fields I read in there may be more information to read on that line. Does anyone know how to stop READF from moving to the next line in the file on the next read?

ANSWER: Good question, John! Suppose you have a data set that looks like this:

   1 2 3 4 5 6 7 8 9 10
   11 12 13 14 15 

You want to read the first five values on the the first line, and if the last value is the number "5", read 5 more values.

You certainly can't do it like this:

   firstfive = IntArr(5)
   nextfive = IntArr(5)
   OpenR, lun, 'example.dat', /Get_Lun
   ReadF, lun, firstfive
   IF firstfive[4] EQ 5 THEN ReadF, lun, nextfive
   Print, firstfive, nextfive

Because, as you have noticed the file pointer starts on the next line when you do the second read. A FORMAT keyword doesn't help either.

What you have to do instead is read the whole line into a string variable, and then read the actual values out of that string with the READS command. The READS command allows you to read data from a string variable in just the same way as you might read the information out of a file. Your code will look something like this:

   firstfive = IntArr(5)
   nextfive = IntArr(5)
   line = ''
   OpenR, lun, 'example.dat', /Get_Lun
   ReadF, lun, line
   ReadS, line, firstfive
   IF firstfive[4] EQ 5 THEN ReadS, line, firstfive, nextfive
   Print, firstfive, nextfive

Google
 
Web Coyote's Guide to IDL Programming