comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » can i read multiple files?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
can i read multiple files? [message #50196] Mon, 18 September 2006 17:46 Go to next message
zircola is currently offline  zircola
Messages: 8
Registered: September 2006
Junior Member
hi..i am relatively new to idl. i got to this newsgroup through
dfanning website..now i was thinking if i can read multiple files at
the same time in one procedure. can this be done in idl?
thanks
Re: can i read multiple files? [message #50253 is a reply to message #50196] Thu, 21 September 2006 19:46 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
zircola writes:

> Thanks every one, i am learning a lot, i tried all of them. using file
> search i put all the files in the array but didn't know how to invoke
> it with for loop, i tried like this
>
> file_array=file_search('filepath', '*.dat', count=num_file)
> for i=0,num_file-1 do begin
> file=file_array(i)
>
> openr, lun, /get_lun, file
>
> (read file here)
> free_lun, lun
> (do all the stuffs like plotting etc here)
>
> endfor
>
> is this how i use the file stored in the array?
>
> for the similar named files, the one suggested by paul works
> great,..thanks
> i just hope my question is not so stupid

Humm. Questions are rarely stupid. I think the problem
here is that we are assuming you know a bit more about
this than I suspect you do. Most of the people who come
to IDL come having written some kind of computer program
before. I get the impression you have not. If this is
so, I would HIGHLY recommend Ken Bowman's book _An
Introduction to Programming with IDL_. It's great
for teaching you what a FOR loop is, etc.

You have mostly the right idea here, but I don't think
you have really tried this code out. For example, this
code is not likely to work:

file_array=file_search('filepath', '*.dat', count=num_file)

To see if it worked you could test how many files you found:

IDL> Print, num_file

Or, you could see how big your file_array variable is:

IDL> Help, file_array

The reason it doesn't work is that 'filepath' is not
likely to be the path to YOUR files. Your path is
going to look more like "C:\RSI\ZIRCOLA\DATA" or
"/usr/zircola/data", or something. We can't help you
with this because you haven't told us anything about
what machine you are running IDL on, etc. The syntax
'filepath' is what we refer to as "pseudo code". We
mean you will have to replace that with what is right
for YOUR situation. As a beginning user, it is often
difficult to tell pseudo code from real code.

Then, if you put all your plotting stuff inside the
loop, you are going to have some trouble because
your plots will appear and disappear so fast you
won't be able to enjoy them. You will end up looking
at just the LAST plot. So, typically, you will do all
your file reading, and THEN you will plot your graphics.

I suggest you maybe start with one file, and see if
you can read that and plot it. Once you have that
under your belt you can extend it to more than a
single file. This way you will know which part of
your code works and which part still has problems.

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: can i read multiple files? [message #50254 is a reply to message #50196] Thu, 21 September 2006 18:05 Go to previous messageGo to next message
zircola is currently offline  zircola
Messages: 8
Registered: September 2006
Junior Member
Thanks every one, i am learning a lot, i tried all of them. using file
search i put all the files in the array but didn't know how to invoke
it with for loop, i tried like this

file_array=file_search('filepath', '*.dat', count=num_file)
for i=0,num_file-1 do begin
file=file_array(i)

openr, lun, /get_lun, file

(read file here)
free_lun, lun
(do all the stuffs like plotting etc here)

endfor

is this how i use the file stored in the array?

for the similar named files, the one suggested by paul works
great,..thanks
i just hope my question is not so stupid
Re: can i read multiple files? [message #50255 is a reply to message #50196] Thu, 21 September 2006 12:53 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
Braedley wrote:
> zircola wrote:
>> it is fine, but i wanted to do it automatically not that i have to
>> select the file individually. I want to read the file do the action and
>> again read another file do the same action and read another file and do
>> the same action and so on in the same procedure. My files are in same
>> format and same data attributes but different data set. How can this be
>> done?
>
> My appologies if this caused confusion. Dialog_Pickfile should be used
> when the you want the user to select a file (or directory) to open. If
> there is a need to get the user to open more than one file, then
> include the multiple keyword. If instead you don't want the user to
> have any choice in the matter, you can either hard code the files into
> the code (in your case, in the form of an array) or use file_search
> like David suggested to find files in a directory that match a certain
> criterion. In either case, you'll still probably use a loop to keep
> your code more flexible.

Or you could generate your filenames on the fly if they follow some sort of pattern (like
file001.dat, file002.dat, etc...)e.g.:

for i=1, 100 do begin
filename=string(i,format='("file",i3.3,".dat")')
....
open filename, read, write, etc...
....
endfor

paulv

--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
Ph: (301)763-8000 x7748
Fax:(301)763-8545
Re: can i read multiple files? [message #50257 is a reply to message #50196] Thu, 21 September 2006 11:31 Go to previous messageGo to next message
Braedley is currently offline  Braedley
Messages: 57
Registered: September 2006
Member
zircola wrote:
> it is fine, but i wanted to do it automatically not that i have to
> select the file individually. I want to read the file do the action and
> again read another file do the same action and read another file and do
> the same action and so on in the same procedure. My files are in same
> format and same data attributes but different data set. How can this be
> done?

My appologies if this caused confusion. Dialog_Pickfile should be used
when the you want the user to select a file (or directory) to open. If
there is a need to get the user to open more than one file, then
include the multiple keyword. If instead you don't want the user to
have any choice in the matter, you can either hard code the files into
the code (in your case, in the form of an array) or use file_search
like David suggested to find files in a directory that match a certain
criterion. In either case, you'll still probably use a loop to keep
your code more flexible.

I only suggested using dialog_pickfile because often use it during
development stages so I can test many different data files without
changing the program. It's only after I've added a shell program to do
batch processing (once I know the main program is working) that I stop
making calls to dialog_pickfile.
Re: can i read multiple files? [message #50278 is a reply to message #50196] Tue, 19 September 2006 21:22 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
zircola writes:

> it is fine, but i wanted to do it automatically not that i have to
> select the file individually. I want to read the file do the action and
> again read another file do the same action and read another file and do
> the same action and so on in the same procedure. My files are in same
> format and same data attributes but different data set. How can this be
> done?

I'm not sure what you are after here. Do you want FILE_SEARCH
instead of DIALOG_PICKFILE? Otherwise, it seems your question
has pretty much been answered.

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: can i read multiple files? [message #50279 is a reply to message #50196] Tue, 19 September 2006 20:38 Go to previous messageGo to next message
zircola is currently offline  zircola
Messages: 8
Registered: September 2006
Junior Member
it is fine, but i wanted to do it automatically not that i have to
select the file individually. I want to read the file do the action and
again read another file do the same action and read another file and do
the same action and so on in the same procedure. My files are in same
format and same data attributes but different data set. How can this be
done?
Re: can i read multiple files? [message #50280 is a reply to message #50196] Tue, 19 September 2006 20:18 Go to previous messageGo to next message
zircola is currently offline  zircola
Messages: 8
Registered: September 2006
Junior Member
thanks david and braedley...works great...this group is really
amazing...
Re: can i read multiple files? [message #50289 is a reply to message #50196] Tue, 19 September 2006 05:08 Go to previous messageGo to next message
Braedley is currently offline  Braedley
Messages: 57
Registered: September 2006
Member
zircola wrote:
> hi..i am relatively new to idl. i got to this newsgroup through
> dfanning website..now i was thinking if i can read multiple files at
> the same time in one procedure. can this be done in idl?
> thanks

An addendum to David's post, setting the multiple keyword in
dialog_pickfile will return a string array of all the selected files,
then it's just a matter of for looping through the array and opening
each one, such as:

files=dialog_pickfile(/multi, filter=filter, path=path, title=title)
n_files=n_elements(files)
for i=0, n_files-1 do begin
;Read the file. In this case, we'll say it's one line of ASCII
openr, lun, /get_lun, files[i]
readf, lun, data
data=fix(data)
free_lun, lun
;Perform the remainder of the analysis for this data
endfor

It's probably not a good idea to load every file into a variable at
once unless each file is small and uniform, as that can quickly take up
memory.
Re: can i read multiple files? [message #50335 is a reply to message #50253] Mon, 25 September 2006 00:07 Go to previous message
zircola is currently offline  zircola
Messages: 8
Registered: September 2006
Junior Member
Thanks David. :)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: eventlog for iTools
Next Topic: Re: MPFIT for 21st Century

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 10:08:32 PDT 2025

Total time taken to generate the page: 0.40100 seconds