Reading File knowing certain variables

It is difficult to understand your problem from the part of the code you posted.
As you have not provided muHA.dat, the alternative code below compiles with no compile errors, but has not been tested.

Rather than (as discussed in other posts) interpolate the best estimate of amur, the program intends to find the closest value for amur listed in the table provided, based on the values of cm and Tmu that are listed. This would also overcome any round-off problems with the required real values when you are testing for cm and Tmu to be read within 1.d-8 of the required values.
Your coded method of looking for exact T0,C0 to within 1.d-8 is problematic if 1) the values are not provided and 2) if the values provided are not exactly T0 and C0 as a real(wp) value.

  integer, parameter :: wp = selected_real_kind (10)
  real(wp), allocatable :: cm(:), Tmu(:), amur(:,:)
  real(wp) :: T0, C0
  integer :: ncm, ntmu, kc, kt, i, j, iostat

  ncm  = 11
  ntmu = 269
  allocate ( cm(ncm), Tmu(ntmu), amur(ncm,ntmu) )

  T0 = 300.0_wp
  c0 = 0.4_wp

  kc   = 1   !  closest cm(i)   for c0
  kt   = 1   !  closest Tmu(jt) for T0

  open(10,file='muHA.dat') 

  read (10,*, iostat = iostat) Tmu(1), (cm(i),i=1,ncm) 
  if ( iostat /= 0 ) then
    write (*,*) ' problem reading cm from muHA.dat'
    stop
  end if
  
  do j = 1,ntmu
    read (10,*, iostat = iostat) Tmu(j), (amur(i,j),i=1,ncm) 
     if ( iostat /= 0 ) then
       write (*,*) ' problem reading amur from muHA.dat at record',j
       stop
     end if
    if ( abs(Tmu(j)-T0) < abs(Tmu(kt)-T0) ) kt = j
  end do
  ntmu = j-1
  close (10)

  if ( j <= ntmu ) then
    print*, 'unable to read table' 
    stop 
  end if

  do i=1,ncm
    if ( abs(cm(i)-c0) < abs (cm(kc)-c0) ) kc = i
  end do

  write (*,*) 'Closest value for'
  write (*,*) ' C0 =',c0,' is', cm(kc)
  write (*,*) ' T0 =',t0,' is', Tmu(kt)
  write (*,*) ' amur =',amur(kc,kt)

  end

A next step could be to interpolate the best value about the point kc,kt in the supplied table.

1 Like