Re: Selecting odd/even numbered files [message #29971] |
Wed, 03 April 2002 07:19  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Irene Dumkow (irene.dumkow@uni-essen.de) writes:
> I have been writing an IDL routine to plot some of our data in a certain
> way. The measuring programm saves the data using a given prefix, adding
> a number starting from 001 until the user stops the measurement (eq
> xxxxx001.ext to xxxxx323.ext). The measuring program can do different
> kinds of measurements, but it will save the datafiles all in one
> directory. But for the data-plotting, I sometimes only want to select
> the odd or even numbers (for a start, the most ideal case would be to
> give a starting value and an increment). I have been using
> dialog_pickfile() for the fileselection, which is not suited for the
> task on hand. I was wondering if somebody has written a routine which
> does something like I want (selecting only even numbered or only odd
> numbered files) or can give me some ideas, because I am completely
> stumped on even how to get started.
Here is a little program that should do what you want it to
do:
************************************************************ **
FUNCTION PickSomeFiles, Filter=filter, EVEN=even, ODD=odd, Count=count
IF N_Elements(filter) EQ 0 THEN filter="*.ext"
files = FindFile(filter, Count=count)
slen = StrLen(files)
IF Keyword_Set(even) THEN BEGIN
numbers = IntArr(N_Elements(files))
FOR j=0,N_Elements(files)-1 DO BEGIN
stringpart = StrMid(files[j], 5, 3)
numbers[j] = Fix(stringpart)
ENDFOR
index = Where(numbers MOD 2 EQ 0, count)
IF count GT 0 THEN RETURN, files[index] ELSE RETURN, ""
ENDIF
IF Keyword_Set(odd) THEN BEGIN
numbers = IntArr(N_Elements(files))
FOR j=0,N_Elements(files)-1 DO BEGIN
stringpart = StrMid(files[j], 5, 3)
numbers[j] = Fix(stringpart)
ENDFOR
index = Where(numbers MOD 2 EQ 1, count)
IF count GT 0 THEN RETURN, files[index] ELSE RETURN, ""
ENDIF
RETURN, files
END
************************************************************ ***
You would call it like this. It assumes that all the files
in the directory will be named as you have described above,
with 8 characters in the first part of the file (last three
characters are digits), and a three character extension. The
program will have to be modified if that is not the case.
IDL> filenames = PickSomeFiles(Filter='*.txt', Count=count)
IDL> IF count GT 0 THEN Print, filenames
david001.txt david002.txt david003.txt david004.txt
To select even numbered files, set the EVEN keyword. To select
odd numbered files, set the ODD keyword.
Here is what my test session looked like:
IDL> print, picksomefiles(filter='*.txt',count=c) & print, c
david001.txt david002.txt david003.txt david004.txt
4
IDL> print, picksomefiles(/even)
david002.txt david004.txt
IDL> print, picksomefiles(/odd)
david001.txt david003.txt
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: Selecting odd/even numbered files [message #30058 is a reply to message #29971] |
Wed, 03 April 2002 11:20  |
Wayne Landsman
Messages: 117 Registered: January 1997
|
Senior Member |
|
|
David Fanning wrote:
>
> Here is a little program that should do what you want it to
> do:
>
A minor footnote to the little program: In IDL V5.3 or later, the 5 lines
numbers = IntArr(N_Elements(files))
FOR j=0,N_Elements(files)-1 DO BEGIN
stringpart = StrMid(files[j], 5, 3)
numbers[j] = Fix(stringpart)
ENDFOR
can be replaced by the single vectorized line:
numbers = fix(strmid(files,5,3))
--Wayne Landsman landsman@mpb.gsfc.nasa.gov
|
|
|