title: Error: Cannot write to file opened for READ
body: <p>So I'm writting this post processing code, where the code opens some files, reads them and store its values in arrays, to do some calculations.</p>
  <p>I've put some <code>write(*,*) 'Loaded ***.txt'</code>  within the open (...) close(...) to determinate which one is causing trouble.</p>
  <p>The code:</p>
  <pre><code>001 program Post_Processing
  002     implicit none
  003     real*8 :: radius, u_d, nu, cond, u_mean, gx, Porosity, kk, press_in, press_out, dpdx
  004     real*8 :: Pi=4*atan(1.d0)
  005     integer :: i,j
  006     integer, parameter :: ni=257,nj=257,n=9
  007     integer,dimension(ni,nj) :: wall=0.d0
  008     real*8,dimension(ni,nj) :: ux=0.d0,uy=0.d0, T=0.d0, rho=0.d0
  009     real*8,dimension(ni) :: x(ni)=0.d0, T_bulk=0.d0, T_bulk_sum=0.d0, h=0.d0, q=0.d0
  010     real*8,dimension(nj) :: y(nj)=0.d0
  011
  012     open(UNIT=1, FILE='Porosity.txt', STATUS='OLD', ACTION='READ')
  013     read(1,*) ((wall(i,j), j=nj, 1, -1), i=1,ni)
  014     write(*,*) 'Loaded Porosity.txt'
  015     close(1)
  016     open(UNIT=2, FILE='Data_porosity.txt', STATUS='OLD', ACTION='READ')
  017     read(2,*) Porosity, radius
  018     write(*,*) 'Loaded Data_porosity.txt'
  019     close(2)
  020     open(UNIT=3, FILE='Data_main.txt', STATUS='OLD', ACTION='READ')
  021     read(3,*) gx, nu
  022     write(*,*) 'Loaded Data_main.txt'
  023     close(3)
  024     open(UNIT=4, FILE='Ux_excel.txt', STATUS='OLD', ACTION='READ')
  025     read(4,*) ((ux(i,j), j=nj, 1, -1), i=1,ni)
  026     write(*,*) 'Loaded Ux_excel.txt'
  027     close(4)
  028     open(UNIT=5, FILE='Uy_excel.txt', STATUS='OLD', ACTION='READ')
  029     read(5,*) ((uy(i,j), j=nj, 1, -1), i=1,ni)
  030     write(*,*) 'Loaded Uy_excel.txt'
  031     close(5)
  032     open(UNIT=6, FILE='T_excel.txt', STATUS='OLD', ACTION='READ')    This is
  033     read(6,*) ((T(i,j), j=nj, 1, -1), i=1,ni)                          the
  034     write(*,*) 'Loaded T_excel.txt'                                  troubled
  035     close(6)                                                           one.
  036     open(UNIT=7, FILE='Rho.txt', STATUS='OLD', ACTION='READ')
  037     read(7,*) ((rho(i,j), j=nj,1,-1),i=1,ni)
  038     write(*,*) 'Loaded Rho.txt'
  039     close(7)
  040
  041     x(1) = 0.d0
  042     do i = 2, ni
  043         x(i) = x(i-1) +1.d0
  044     enddo
  045
  046     y(1) = 0.d0
  047     do j = 2, nj
  048         y(j) = y(j-1) +1.d0
  049     enddo
  050
  051     if (Porosity == 1.d0) then
  052         !   ================================================================
  053         !   =                 Temperature Bulk Definition                  =
  054         !   ================================================================
  055         u_mean = gx * (nj-1)**2 / (12 * nu)
  056         do i = 1, ni
  057             do j = 1, nj
  058                 T_bulk_sum(i) = (ux(i,j)-gx) * T(i,j) + T_bulk_sum(i)
  059             enddo
  060         T_bulk(i) = T_bulk_sum(i) / (u_mean * (nj-1))
  061         enddo
  062         !   ================================================================
  063         !   =                   Heat Transfer Coefficient                  =
  064         !   ================================================================
  065         cond = (1.d0 / 3.d0) * 5.d-1
  066         do i = 1, ni
  067         q(i) = cond * (T(i,1) - T(i,2)) / 1.d0
  068         h(i) = q(i) / (T(i,1) - T_bulk(i))
  069         enddo
  070
  071         open(10,File='Heat Transfer Coefficient.txt')
  072         do i = 1, ni
  073         if (i==ni) then
  074             write(10,01,advance='yes') x(i)
  075         else
  076             write(10,01,advance='no') x(i)
  077         endif
  078         enddo
  079         do i = 1, ni
  080         if (i==ni) then
  081             write(10,01,advance='yes') h(i)
  082         else
  083             write(10,01,advance='no') h(i)
  084         endif
  085         enddo
  086         close(10)
  087     endif
  088     !   ================================================================
  089     !   =                        Permeability                          =
  090     !   ================================================================
  091     if (Porosity /= 1) then
  092         U_d = 0.d0
  093         do j = 1, nj
  094             do i = 1, ni
  095                 if (wall(i,j)==1) cycle
  096                 U_d = ux(i,j) + U_d
  097             enddo
  098         enddo
  099         U_d = U_d / (dble(ni)*dble(nj))
  100         write(*,*) ' '
  101         write(*,*) 'Velocidade de Darcy=',U_d
  102
  103         press_in = sum(rho(1,2:nj-1))/(3*nj-1)
  104         press_out = sum(rho(ni,2:nj-1))/(3*nj-1)
  105         dpdx=(press_in - press_out)/dble(ni-1)
  106
  107         kk = nu*U_d/gx
  108         kk = kk / (2*radius)**2
  109         write(*,*) ' '
  110         write(*,*) 'Permebilidade= ', kk
  111         write(*,*) ' '
  112         write(*,*) 'Variacao de pressao em x =', dpdx
  113         write(*,*) ' '
  114         write(*,*) 'Permeabilidade do Kaviany, correlação 1(K/L^2)= ', 6.06d-2*Porosity**5.1
  115         write(*,*) ' '
  116         write(*,*) 'Permeabilidade do Kaviany, correlação 2(K/d^2)= ', 6.06d-2*(Pi/4.d0)*Porosity**5.1/(1-Porosity)
  117         open(10,File='Data_post_processing(Ud,K).txt')
  118         write(10,02) u_d
  119         write(10,03) kk
  120         close(10)
  121     endif
  122     !   =======================================================================
  123     !   =                              Output                                 =
  124     !   =======================================================================
  125     01 format (F16.8)
  126     02 format (1X,'Darcy Velocity=',F18.12)
  127     03 format (1X,'Permeability=',F18.12)
  128
  129     if(Porosity == 1.d0) then
  130         
  131     endif
  132 end program Post_Processing
  </code></pre>
  <p>The error:</p>
  <pre><code> Loaded Porosity.txt
   Loaded Data_porosity.txt
   Loaded Data_main.txt
   Loaded Ux_excel.txt
   Loaded Uy_excel.txt
  At line 34 of file Post_Processing.f95 (unit = 6, file = 'T_excel.txt')
  Fortran runtime error: Cannot write to file opened for READ
  
  Error termination. Backtrace:
  
  Could not print backtrace: libbacktrace could not find executable to open
  #0  0x65754044
  #1  0x656ed55e
  #2  0x6565f842
  #3  0x65764853
  #4  0x401e82
  #5  0x402dee
  #6  0x401235
  </code></pre>
  <p>The thing I'm asking to write in the prompt, not in the file. When I comment line 032 - 035, the program works fine, giving results (some are wrong because of the Temperature matrix not being loaded). But I kinda need the T_excel.txt to be loaded.</p>
  <p>The matrix are 257x257 each one.</p>
  <p>Any lead? (If needed, can give more info). Can I attach files?</p>