Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28477 Discussions

Question: Reading a large column (1D-array) and writing slices of this array as rows

bhvj
Beginner
2,371 Views

I am trying to read a large single column (which I am trying to read in as a 1-D array), and then to write slices of this array as rows until the end of the data. Please suggest me the best way to accomplish this task. Thanks in advance.

0 Kudos
26 Replies
mecej4
Honored Contributor III
367 Views

You use the standard -g option to enable source level debugging.

Your program has no READ or WRITE statements that pertain to units other than '*', which stands for the console. Therefore, at line 9, the program waits for keyboard input from you before going further. If you want to read from / write to a file, the first argument to the READ/WRITE statement should be the unit-number of the file, not '*'.

You should definitely consult a Fortran book and learn the basics.

0 Kudos
bhvj
Beginner
367 Views

Thanks again for all your help. Attached is the modified program, where the following was done:

The unit numbers for the READ and the WRITE statements were changed with the corresponding variable the subroutine that is being called.

The complete reformatting process (opening the input files, reading the input files, and writing the output files) was placed as the body of the iterative, DO-loop, so as to execute repeatedly for the ten files.

The output shows that it is opening all the input files and the output files, and the output files are written, but it is only writing the first value in each file.

I will continue my learning process. Is there any obvious mistake that I am making?

Thank you very much once again for all the help provided so far.

0 Kudos
mecej4
Honored Contributor III
367 Views

The obvious mistake is that you are using the same set of unit numbers for reading and writing! If you do such things, there is the risk that your input files will get overwritten and you may lose data.

I suggest that you use unit numbers 11 to 20 for the input files in the OPEN and READ statements, and unit numbers 31 to 40 for the output files in the OPEN and WRITE statements.

It would also be a good idea to make all the input files read-only by removing write permission from those files at the OS level, and to keep backup copies of the input files. 

0 Kudos
bhvj
Beginner
367 Views

Thank you very much for pointing out my mistake and for your suggestions. I made the suggested changes and everything ran properly. Thanks a lot once again for all your help. Your responses taught me a lot and also built tremendous interest in me to learn FORTRAN programming.

0 Kudos
mecej4
Honored Contributor III
367 Views

Allow me one final comment on the program: the principles of Just-in-time Programming indicate that, since you process one pair of files at a time, it should suffice to open only one one input file and one output file, read the data in the former and output the modified data to the latter, and then close the two files.

If you make the slight changes to implement this idea, you will have just one main loop, in which you do (i) Open in-file and out-file pair; (ii) read in-file, reformat data, write to out-file; (iii) close both files. Then, you repeat these steps for the next pair of files, and so on, until all the files have been processed.

This way, you would need only two unit numbers instead of twenty. And, if your needs change and you need to process 200 file pairs, for example, you would not run out of unit numbers, and your program would not be limited by the maximum of simultaneously open files permitted by the compiler and OS.

See http://hcil2.cs.umd.edu/trs/93-18/93-18.html .

0 Kudos
bhvj
Beginner
367 Views

This was indeed a valuable comment again, I was looking forward to process more than 10 files. In order to implement this I included two CLOSE statements before the single iterative do-loop ended, each for the input file and the output file. I used two constant unit numbers each for input file and output file and everything worked fine. Thank you once again for all the detailed explanation.

I have some questions that might be unrelated to this thread, so I am planning to open a new thread titled: "creating a batch file within FORTRAN", as regards some suggestions regarding working with the output files created here, in a batch mode. I would appreciate it if you could provide your expert suggestions regarding those questions as well.

Thanks again for all your help.

0 Kudos
Reply