program order
  real, dimension(:,:), allocatable :: rdat, rord


  idat = 11
  open(idat, file = 'res.gn', status= 'OLD')
  n_max = 100
  allocate(rdat(1:n_max, 1:4) )

  m_max = 0
  do i=1, n_max
     read(idat, *, end=100) nelem, rdat(i, 1:4)

     write(*, '(i5, 4es12.4)') nelem, rdat(i, 1:4)
     m_max = i
  enddo

100 continue
  n_max = i -1

  allocate(rord(1:m_max, 1:4) )
  
  itex = 12
  open(itex, file='table.tex', status = 'UNKNOWN')

  write(itex, *) '\begin{tabular}{c|c|cc|c}'
  write(itex, *) '\hline'
  write(itex, *) ' & $ h $  & $\|e_h\|_{L^2(\Omega)}$ ', &
       '& $|e_h|_{H^1(\Omega)}$ & time(s)\\ '
  write(itex, *) '\hline'

  do i=1, m_max
     write(itex, 200) i, rdat(i, 1:4)
     if(i > 1) then
        do j=2, 3
           rord(i,j) = log(rdat(i, j)/rdat(i-1, j))/ log(rdat(i, 1)/rdat(i-1, 1))
        enddo

        write(itex, 201) rord(i, 2:3)
     endif
     
  enddo

  write(itex, *) '\hline'
  write(itex, *) '\end{tabular}'

200 format ( i5 , 3('&' es12.4), '&', f8.1, '\\')
201 format ( '&'  2('& {\small (' f4.2' )}'), '&' '\\')
  
  
end program order
