program air_cooled_condenser_acc_steam
    implicit none
    integer :: iostat_val, num_bays, num_fans
    double precision :: m_dot_steam_tph, T_amb_C, P_back_mbar, fan_diam_m
    double precision :: m_dot_steam_kgs, T_sat_C, hfg_kJkg, Q_duty_MW, ITD_C
    double precision :: rho_air, cp_air, V_dot_air_m3s, delta_T_air, T_air_out_C
    double precision :: A_face_m2, A_total_finned_m2, U_overall, dp_air_Pa, P_fan_total_kW
    character(len=32) :: backpressure_status
    double precision, parameter :: PI = 3.141592653589793d0

    ! Read inputs
    read(*,*,iostat=iostat_val) m_dot_steam_tph ! Steam Flow from Turbine [t/h] (e.g. 150.0)
    read(*,*,iostat=iostat_val) T_amb_C         ! Ambient Dry-Bulb Temperature [deg C] (e.g. 30.0)
    read(*,*,iostat=iostat_val) P_back_mbar     ! Turbine Backpressure [mbar abs] (e.g. 120.0)
    read(*,*,iostat=iostat_val) num_bays        ! Number of A-frame ACC Bays (e.g. 12)
    read(*,*,iostat=iostat_val) num_fans        ! Number of Axial Fans (e.g. 12)
    read(*,*,iostat=iostat_val) fan_diam_m      ! Fan Diameter [m] (e.g. 9.14)

    if (iostat_val /= 0) then
        write(*,*) 'ERROR: Invalid input data for ACC steam condenser calculation.'
        stop
    end if

    if (m_dot_steam_tph <= 0.0d0 .or. P_back_mbar <= 0.0d0 .or. num_bays <= 0 .or. num_fans <= 0) then
        write(*,*) 'ERROR: Flow rate, backpressure, and bays count must be positive.'
        stop
    end if

    ! Steam saturation properties fit around 50 - 250 mbar
    ! P_back in bar = P_back_mbar / 1000.0
    ! T_sat in C approx from Antoine/IF97 fit
    T_sat_C = 45.81d0 + 17.5d0 * log(max(10.0d0, P_back_mbar) / 100.0d0)
    hfg_kJkg = 2410.0d0 - 2.3d0 * (T_sat_C - 45.0d0)

    m_dot_steam_kgs = (m_dot_steam_tph * 1000.0d0) / 3600.0d0
    Q_duty_MW = (m_dot_steam_kgs * hfg_kJkg) / 1000.0d0

    ITD_C = T_sat_C - T_amb_C

    if (P_back_mbar > 200.0d0) then
        backpressure_status = 'HIGH BACKPRESSURE (VACUUM LOSS)'
    else if (P_back_mbar < 70.0d0) then
        backpressure_status = 'EXCELLENT DEEP VACUUM'
    else
        backpressure_status = 'NORMAL COMBINED CYCLE VACUUM'
    end if

    ! Air side calculations
    rho_air = 101.325d3 / (287.05d0 * (T_amb_C + 273.15d0))
    cp_air = 1006.0d0 ! J/(kg.K)

    ! Sizing air flow: Air face velocity approx 2.5 m/s
    A_face_m2 = dble(num_fans) * (PI * (fan_diam_m**2) / 4.0d0)
    V_dot_air_m3s = A_face_m2 * 2.6d0
    delta_T_air = (Q_duty_MW * 1.0d6) / (rho_air * V_dot_air_m3s * cp_air)
    T_air_out_C = T_amb_C + delta_T_air

    A_total_finned_m2 = A_face_m2 * 28.0d0 ! Area ratio finned/face approx 28
    U_overall = (Q_duty_MW * 1.0d6) / (A_total_finned_m2 * (ITD_C - delta_T_air/2.0d0))

    ! Fan power consumption
    dp_air_Pa = 120.0d0 ! Approx 120 Pa air pressure drop across bundles
    P_fan_total_kW = (V_dot_air_m3s * dp_air_Pa) / (0.65d0 * 1000.0d0)

    ! Output Results
    write(*,'(A)') '============================================================'
    write(*,'(A)') ' THERMOFLUIDCALC — AIR-COOLED STEAM CONDENSER (ACC SIZER)'
    write(*,'(A)') '============================================================'
    write(*,'(A,F10.1,A,F10.2,A)') 'Ambient Temp / Saturation Temp = ', T_amb_C, ' deg C / ', T_sat_C, ' deg C'
    write(*,'(A,F10.1,A,A)')       'Turbine Backpressure (P_back)  = ', P_back_mbar, ' mbar abs | ', trim(backpressure_status)
    write(*,'(A,F10.2,A)')         'Initial Temp Difference (ITD)  = ', ITD_C, ' deg C'
    write(*,'(A)') '------------------------------------------------------------'
    write(*,'(A,F10.2,A)')  'CONDENSER THERMAL DUTY (Q)   = ', Q_duty_MW, ' MWth'
    write(*,'(A,F10.1,A)')  'Cooling Air Volumetric Flow  = ', V_dot_air_m3s, ' m3/s'
    write(*,'(A,F10.1,A)')  'Cooling Air Outlet Temp      = ', T_air_out_C, ' deg C'
    write(*,'(A,F10.0,A,I4,A)') 'Total Finned Bundle Area = ', A_total_finned_m2, ' m2 (', num_bays, ' A-frame bays)'
    write(*,'(A,F10.1,A)')  'Overall Heat Transfer Coeff U= ', U_overall, ' W/(m2.K)'
    write(*,'(A,F10.1,A)')  'Total Fan Electrical Power   = ', P_fan_total_kW, ' kWe'
    write(*,'(A)') '============================================================'

end program air_cooled_condenser_acc_steam
