program fouling_factor
    implicit none
    integer::fh,fc,iostat_val,i,j
    double precision::Uc,Rf_h,Rf_c,Rf_tot,Ud,CF,OD_pct
    double precision::Rf_vals(10),Ud_ij
    character(len=30)::fname(10),fhname,fcname
    fname(1)='Distilled Water';     Rf_vals(1)=0.00009d0
    fname(2)='River/Lake Water';    Rf_vals(2)=0.00035d0
    fname(3)='Seawater (treated)';  Rf_vals(3)=0.00025d0
    fname(4)='Boiler Feedwater';    Rf_vals(4)=0.00018d0
    fname(5)='Fuel Oil (#6)';       Rf_vals(5)=0.00088d0
    fname(6)='Transformer Oil';     Rf_vals(6)=0.00018d0
    fname(7)='Refrigerant (liquid)';Rf_vals(7)=0.00018d0
    fname(8)='Refrigerant (vapor)'; Rf_vals(8)=0.00035d0
    fname(9)='Steam (clean)';       Rf_vals(9)=0.00009d0
    fname(10)='Exhaust Gas';        Rf_vals(10)=0.00176d0
    read(*,*,iostat=iostat_val) fh; read(*,*,iostat=iostat_val) fc
    read(*,*,iostat=iostat_val) Uc
    if(iostat_val/=0)then;write(*,*)'ERROR: Bad input.';stop;end if
    if(fh<1.or.fh>10)fh=1; if(fc<1.or.fc>10)fc=1; if(Uc<=0)Uc=500.0d0
    fhname=fname(fh); fcname=fname(fc)
    Rf_h=Rf_vals(fh); Rf_c=Rf_vals(fc)
    Rf_tot=Rf_h+Rf_c
    Ud=1.0d0/(1.0d0/Uc+Rf_tot)
    CF=Ud/Uc
    OD_pct=(Uc/Ud-1.0d0)*100.0d0
    write(*,'(A)')'============================================================'
    write(*,'(A)')'   TEMA FOULING FACTOR GUIDE'
    write(*,'(A)')'============================================================'
    write(*,*)
    write(*,'(A)')'--- INPUTS --------------------------------------------------'
    write(*,'(A,A)')       '  Hot-Side Fluid            = ',trim(fhname)
    write(*,'(A,A)')       '  Cold-Side Fluid           = ',trim(fcname)
    write(*,'(A,F10.2,A)') '  Clean U_overall           = ',Uc,' W/(m2.K)'
    write(*,*)
    write(*,'(A)')'--- FOULING RESISTANCES -------------------------------------'
    write(*,'(A,ES12.4,A)')'  Rf_hot                    = ',Rf_h,' m2.K/W'
    write(*,'(A,ES12.4,A)')'  Rf_cold                   = ',Rf_c,' m2.K/W'
    write(*,'(A,ES12.4,A)')'  Rf_total                  = ',Rf_tot,' m2.K/W'
    write(*,*)
    write(*,'(A)')'--- DESIGN COEFFICIENT --------------------------------------'
    write(*,'(A,F10.2,A)') '  U_design (fouled)         = ',Ud,' W/(m2.K)'
    write(*,'(A,F10.4)')   '  Cleanliness Factor CF     = ',CF
    write(*,'(A,F10.2,A)') '  Required Overdesign       = ',OD_pct,' percent'
    write(*,*)
    write(*,'(A)')'--- TEMA FOULING TABLE (Rf in m2.K/W) -----------------------'
    write(*,'(A)',advance='no')'  Fluid                     '
    write(*,'(A)')'   Rf [m2.K/W]'
    write(*,'(A)')'  -----------------------------------------------------------'
    do i=1,10
      write(*,'(A30,2X,ES12.4)') fname(i),Rf_vals(i)
    end do
    write(*,*)
    write(*,'(A)')'--- U_DESIGN MATRIX (selected pairs) ------------------------'
    write(*,'(A)',advance='no')'  Hot\\Cold       '
    do j=1,5; write(*,'(A12)',advance='no') trim(fname(j)); end do
    write(*,*)
    do i=1,5
      write(*,'(A16)',advance='no') trim(fname(i))
      do j=1,5
        Ud_ij=1.0d0/(1.0d0/Uc+Rf_vals(i)+Rf_vals(j))
        write(*,'(F12.1)',advance='no') Ud_ij
      end do
      write(*,*)
    end do
    write(*,*)
    write(*,'(A)')'--- CORRELATIONS USED ---------------------------------------'
    write(*,'(A)')'  U_design = 1/(1/U_clean + Rf_hot + Rf_cold)'
    write(*,'(A)')'  Rf values from TEMA 9th/10th Edition tables.'
end program fouling_factor
