reading in from multiple files, performing operations and printing them to multiple output file [message #86533] |
Sun, 17 November 2013 00:31  |
bhattacharjee12
Messages: 3 Registered: September 2013
|
Junior Member |
|
|
Hi ,
I have a silly question, I feel
I am trying to run a program where I read in bunch of files, all of which have 2 sets of data and then perform the same mathematical operation on those data from each files and then put them the new values into a different set of files. I have come up with so far with this. I am having a problem in creating and printing the data into new files.
pro read_write
;;; will find the input files
files= FINDFILE('day*')
numfiles=n_elements(files)
num=intarr(numfiles)
;;;;creating an array which will have the names of the output files, still havent figured out if this necessary or not
files_out=FINDFILE('new_day*')
FOR m=0, numfiles-1 DO BEGIN
READCOL, files[m],filename1,filename2,format='F'
openw,num[m],files_out,/get_lun ;;;;;;;;;;;;might be totally off on this one.
;;;;;;;;;;;;random math operation
new_data=filename1+2
printf,new_data[m],filename[m]
close,num[m]
ENDFOR
END
I am getting an error on this syntax . I can print the outputs onto screen without any problem. So I think way I am going up with the reading and doing the operations is ok.
Thanks for the help & suggestion in advance.
Anirban
|
|
|
Re: reading in from multiple files, performing operations and printing them to multiple output file [message #86535 is a reply to message #86533] |
Sun, 17 November 2013 07:35   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
bhattacharjee12@gmail.com writes:
> I have a silly question, I feel
> I am trying to run a program where I read in bunch of files, all of which have 2 sets of data and then perform the same mathematical operation on those data from each files and then put them the new values into a different set of files. I have come up with so far with this. I am having a problem in creating and printing the data into new files.
>
> pro read_write
>
> ;;; will find the input files
> files= FINDFILE('day*')
>
> numfiles=n_elements(files)
> num=intarr(numfiles)
> ;;;;creating an array which will have the names of the output files, still havent figured out if this necessary or not
> files_out=FINDFILE('new_day*')
> FOR m=0, numfiles-1 DO BEGIN
> READCOL, files[m],filename1,filename2,format='F'
>
> openw,num[m],files_out,/get_lun ;;;;;;;;;;;;might be totally off on this one.
> ;;;;;;;;;;;;random math operation
> new_data=filename1+2
> printf,new_data[m],filename[m]
> close,num[m]
> ENDFOR
>
> END
>
> I am getting an error on this syntax . I can print the outputs onto screen without any problem. So I think way I am going up with the reading and doing the operations is ok.
> Thanks for the help & suggestion in advance.
Oh, my goodness! Hard to know where to start here. :-)
Here are some pointers.
1. FINDFILE is an obsolete function for good reason: it returns
erroneous results. Use FILE_SEARCH instead.
2. The variable names "filename1" and "filename2" for floating point
variables are, uh, really poorly chosen. You will understand your code
better if you choose better names for variables.
3. If you choose a file logical unit number with GET_LUN, you will have
to not just close it, but free it up so it can be used over again. Use
the FREE_LUN command to both close and free it at the same time, not the
CLOSE command. If you don't do this you will run out of logical unit
numbers sooner or later.
4. Have a look at the on-line help for the PRINTF command. You are
missing the arguments you need to successfully write to a file.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|