-2
       implicit none

   real, dimension(:) ,allocatable :: t1, t2, t3
   real :: t4, t5, t6 
   integer:: i, n ,io

  CHARACTER(LEN=30) :: Format
      Format ="(3X,7F8.2)"

   open (unit=22,file='non-pol-0mm-300-conf-x-density.dat',status = 'old', action = 'read')
   open (unit=50,file='non-pol-0mm-300-conf-x-fin-den.dat',status='unknown')

  t4 = 1.66054
  t5 = 2782.70




    n = 0 
      DO
   READ(22,iostat=io)
 IF (io/=0) EXIT
  n = n + 1
 END DO
  print*, n


   allocate( t1(n) ,t2(n), t3(n) )
   rewind(3)   
    DO i =1,n
   READ(22,*) t1(i), t2(i), t3(i)
     END DO



   do i=1,n 

   t6(i) = (t2(i)* t4) / t5
    
     end do


   do i=1,n
  write(50,Format) t1(i),t6(i)


   end do


   stop
   end

my data file is

-27.7500       0.0000       0.0000
-27.2500       0.8333       2.3407
-26.7500      99.7305      21.9321
-26.2500     123.1351      26.8580
-25.7500     172.4804      35.9525
-25.2500     239.6032      44.6065
-24.7500     279.7892      43.8637
-24.2500     390.2245      45.5373
-23.7500     452.6671      81.7495
-23.2500     525.5753      67.6686
-22.7500     545.1488      60.7696
-22.2500     589.7524      49.3679
-21.7500     617.3149      38.4744
-21.2500     638.5726      39.6387
sacky0003
  • 25
  • 7
  • Does this answer your question? [How to read number of lines in Fortran 90 from a text file?](https://stackoverflow.com/questions/30692424/how-to-read-number-of-lines-in-fortran-90-from-a-text-file) – veryreverie Apr 14 '21 at 08:06
  • 2
    rewind(3) shouldn't this be rewind(22)? – albert Apr 14 '21 at 08:13
  • 2
    Please read [ask]. When you post some code, you should actually ask some question about that code. If there is something wrong with your code, you should describe the problem. Are there any error messages? Or are the results wrong? Also, you should use tag [tag:fortran] for Fortran questions. – Vladimir F Героям слава Apr 14 '21 at 08:26

1 Answers1

0

This is the correct code.

program densityplot
       implicit none

       real, dimension(:) ,allocatable :: t1, t2, t3
       real :: t4, t5
       integer:: i, n ,io

      CHARACTER(LEN=30) :: Format
          Format ="(3X,7F8.3)"

       open (unit=22,file='non-pol-0mm-300-conf-x-density.dat',status = 'old', action = 'read')
       open (unit=50,file='non-pol-0mm-300-conf-x-fin-den.dat',status='unknown')

        n = 0 
          DO
       READ(22,*,iostat=io)
       IF (io/=0) EXIT
       n = n + 1
       END DO
!       print*, n

       allocate( t1(n) , t2(n), t3(n) )
       rewind(22)   
        DO i =1,n
          READ(22,*) t1(i), t2(i), t3(i)
        END DO

      t4 = 1.66054
      t5 = 2782.70

       do i=1,n
            write(50,Format) t1(i),(t2(i)*t4)/t5
       end do

       end program densityplot
sacky0003
  • 25
  • 7
  • 1
    You could change hardcoded file units to `newunit` ones. Fortran2008 standard should be used whenever possible. – jack Apr 14 '21 at 14:11
  • I did not even realize that you are the OP! You know that you can actually edit your question? – jack Apr 14 '21 at 14:12
  • `IF (io/=0) EXIT` does not necessarily indicate the end of file error. For this purpose, there is an intrinsic Fortran function, `is_iostat_end(io)`, which returns the value `.true.` if `io` is an I/O status value that corresponds to an end-of-file condition, and `.false.` otherwise. – Scientist Apr 14 '21 at 18:09