Stack output concept - fortran

I am new FORTRAN user.
I want to write the output in stack way without deleting the previous one. Suppose we have three outputs A,B,C for one one one "ELECTRON1". When we run the code for another "ELECTRON2" then all previous outputs are over written. So I want to write in a stack way with one blank line.
Please suggest me how I can do it.......
I am very greatful to you...
Regards

if you do
write (*,*) a, b,c
then later
write (*, *)
write (*, *) a, b, c
you should see six numbers on your screen, in two lines, separated by a blank line.
Or if you do this in a loop:
do i=1, N
... computations
write (*, *)
write (*, *) a, b, c
end do
You should get N lines of 3 numbers separated by blank lines.
Is this what you want?
If not, please clarify your question or post some code.

Related

write output on a text file in Fortran code

I have a matrix A(3,4) in Fortran , I want to write it on a text file like this:
A(1,1) A(2,1) A(3,1)
A(1,2) A(2,2) A(3,2)
A(1,3) A(2,3) A(3,3)
A(1,4) A(2,4) A(3,4)
I use below code. It has two problems at first it is overwritten for each i and it is written in rows. I would be gratful to guide me to solve it. Thanks
do i=1,4
open (unit=10,file="out.txt",action="write")
write (10,*) A(1,i) , A(2,i) , A(3,i)
close (10)
As mentioned by Ian, your file is overwritten for each i because your open statement is inside the loop. Fortran is reopening the file fresh for each i. Move the open statement to before the loop so it is only opened once.
Of course it is written in rows because the first index in a 2-D array is the row index. You can switch the indices if you wish. On the other hand, according to your first box, it appears as though you want the rows across the columns.
You say you need to write just some elements. As long as they are in a contiguous block, you will want to use an implied do loop in the write statement. It is much more concise and you can write large blocks without typing out a lot of variables specifically. It would look like this:
open (unit=10,file="out.txt",action="write")
do i=1,4
write (10,*) (A(j,i), j=1,3)
end do
close (10)
Again, this reverses rows and columns, if you want traditional representation, switch the i and j.

Writing a fortran file in columns

i'm trying to write a Fortran file with arrays of 2000 elements each, every 1000 program steps. At first, I tried to write it in the following way:
if(i.eq.1000)
open (21,file='eedf.res',status='unknown',position='append')
write(21,121) (eedf(le),le=1,2000)
close (21)
i=0
.
.
.
(eedf is then put equal to zero and the array is rebuilt in the following 1000 steps; we are inside a do-loop).
It works,producing a file with the arrays printed on rows, but the program that i use to plot these functions tells me that there are too much columns, and so the last half of columns are lost...
So, I want to write eedf in columns, the first with eedf after the first 1000 program step, the second with eedf after the following 1000 program step, and so on. How can i do that?
eedf(1) eedf(1)
eedf(2) eedf(2)
.
.
eedf(2000) eedf(2000)
Sorry if i've been verbose, i tried to put it in the clearest way.
Thanks a lot!

Reading text file and ordering data into two columns in Fortran

I've been trying to solve the problem in the title. Specifically, I have a .txt file with a few hundred real numbers between 0 and 100, and I need to:
Read the file
Separate the numbers in two groups (one for numbers >= 50, other for numbers < 50)
Write two parallel, compact (no white spaces or zeroes) columns so that each one contains one list.
I've been trying to do that by using WRITE(*,*) statements and using the advance="no" parameter because using arrays didn't work for me. The thing is, I can't get the two columns to be parallel. How can that be done? I don't need the code, just a guideline on how to proceed.

How do I read data from a file with description and blank lines with Fortran 77?

I am new to Fortran 77. I need to read the data from a given text file into two arrays, but there are some lines that either are blank or contain descriptive information on the data set before the lines containing the data I need to read. How do I skip those lines?
Also, is there a way my code can count the number of lines containing the data I'm interested in in that file? Or do I necessarily have to count them by hand to build my do-loops for reading the data?
I have tried to find examples online and in Schaum's Programming with Fortran 77, but couldn't find anything too specific on that.
Part of the file I need to read data from follows below. I need to build an array with the entries under each column.
Data from fig. 3 in Klapdor et al., MPLA_17(2002)2409
E(keV) counts_in_bin
2031.5 5.4
2032.5 0
2033.5 0
I am assuming this question is very basic, but I've been fighting with this for a while now, so I thought I would ask.
If you know where the lines are that you don't need/want to read, you can advance the IO with a call to read with no input items.
You can use:
read(input-unit,*)
to read a line from your input file, discard its contents and advance IO to the next line.
It has been a long time since I have looked at F77 code, but in general if your read statement in a DO loop can deal with finding empty lines, or even a record that contains only blanks, then you could write logic to trap that condition and go to a break or continue statement. I just don't recall if read can deal with the situation intelligently.
Alternatively, if you are using a UNIX shell and coreutils, you can use sed to remove empty line, /^$/
or /^ *$/ to preprocess the file before you send it onto F77
Something like
$ sed infile -e 'd/^$/;d/^ *$/' > outfile
It should look something like this:-
C Initialise
integer i
character*80 t1,t2,t3
real*8 x,y
open(unit=1,file='qdata.txt')
C Read headers
read(1,100)t1
100 format(A80)
write(6,*) t1
read(1,100)t2
write(6,*) t2
read(1,100)t3
write(6,*) t3
write(6,*)
C Read data
do 10 i=1,10
read(1,*,end=99) x,y
write(6,*) x,y
10 continue
99 continue
end
So I've used a classic formatted read to read in the header lines, then free-format to read the numbers. The free-format read with the asterisk skips white space including blank lines so it does what you want, and when there is no more data it will go to statement 99 and finish.
The output looks like this:-
Data from fig. 3 in Klapdor et al., MPLA_17(2002)2409
E(keV) counts_in_bin
2031.5000000000000 5.4000000000000004
2032.5000000000000 0.0000000000000000
2033.5000000000000 0.0000000000000000

Unclassifiable statement at 1 , Non-numeric character in statement label at 1

i'm totally new to Fortran, and i want to write a test program using a real*8 function called NeQuick, so i've written the following program :
program test
implicit real*8 (a-h,o-z)
aNe=NeQuick(400.0D0,45.0D0,15.0D0,10,1.929D2,15.0D0)
write(6,'(A,E12.5,A)')
& ' NeQuick electron density =',aNE,' m^-3'
call sleep(10)
end program
At the end when i compile it I have the following errors in each line of the little program : -Non-numeric character in statement label at 1 or
-Unclassifiable statement at 1
Can you guys please explain me what's wrong with my program ?
The way this code is written tells me that it was intended to be fixed-form source. This requires that all of the code start in column 7, except for the & in the second line of the WRITE statement which should be in column 6. Often when such code is pasted into an editor, the leading blanks are removed. If you do this, though, you will have to rename the source file to have a .f or .for file type so that the compiler knows it is fixed-form.
Another, perhaps easier solution is to put an & at the end of the first line of the WRITE - this will then make the source as you have it valid free-form.
For some perspective on this, please read Source Form Just Wants to be Free.