LCOV - code coverage report
Current view: top level - physics/clubb/src/CLUBB_core - clubb_api_module.F90 (source / functions) Hit Total Coverage
Test: coverage.info Lines: 69 757 9.1 %
Date: 2025-03-13 18:42:46 Functions: 11 47 23.4 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------
       2             : ! $Id$
       3             : !==================================================================================================
       4             : !
       5             : !       ########  ###       ###    ### #########  #########           ###     ######### ###########
       6             : !     ###    ### ###       ###    ### ###    ### ###    ###        ### ###   ###    ###    ###
       7             : !    ###        ###       ###    ### ###    ### ###    ###       ###   ###  ###    ###    ###
       8             : !   ###        ###       ###    ### #########  #########       ########### #########     ###
       9             : !  ###        ###       ###    ### ###    ### ###    ###      ###     ### ###           ###
      10             : ! ###    ### ###       ###    ### ###    ### ###    ###      ###     ### ###           ###
      11             : ! ########  ########## ########  #########  #########       ###     ### ###       ###########
      12             : !
      13             : ! The CLUBB API serves as the doorway through which external models can interact with CLUBB.
      14             : !
      15             : !               PLEASE REMEMBER, IF ANY CODE IS CHANGED IN THIS DOCUMENT,
      16             : !                   THE CHANGES MUST BE PROPOGATED TO ALL HOST MODELS.
      17             : !
      18             : module clubb_api_module
      19             : 
      20             : 
      21             :   use mt95, only : &
      22             :     assignment( = ), &
      23             :     genrand_state, & ! Internal representation of the RNG state.
      24             :     genrand_srepr, & ! Public representation of the RNG state. Should be used to save the RNG state
      25             :     genrand_intg, &
      26             :     genrand_init_api => genrand_init
      27             : 
      28             :   use array_index, only : &
      29             :     hydromet_list, &
      30             :     hydromet_tol, & ! Tolerance values for all hydrometeors    [units vary]
      31             :     iiNg, & ! Hydrometeor array index for graupel concentration, Ng
      32             :     iiNi, & ! Hydrometeor array index for ice concentration, Ni
      33             :     iiNr, & ! Hydrometeor array index for rain drop concentration, Nr
      34             :     iiNs, & ! Hydrometeor array index for snow concentration, Ns
      35             :     iirg, & ! Hydrometeor array index for graupel mixing ratio, rg
      36             :     iiri, & ! Hydrometeor array index for ice mixing ratio, ri
      37             :     iirr, & ! Hydrometeor array index for rain water mixing ratio, rr
      38             :     iirs, & ! Hydrometeor array index for snow mixing ratio, rs
      39             :     iiPDF_chi, &
      40             :     iiPDF_rr,  &
      41             :     iiPDF_w,   &
      42             :     iiPDF_Nr,  &
      43             :     iiPDF_ri,  &
      44             :     iiPDF_Ni,  &
      45             :     iiPDF_Ncn, &
      46             :     iiPDF_rs,  &
      47             :     iiPDF_Ns,  &
      48             :     iiPDF_rg,  &
      49             :     iiPDF_Ng,  &
      50             :     iisclr_rt, &
      51             :     iisclr_thl, &
      52             :     iisclr_CO2, &
      53             :     iiedsclr_rt, &
      54             :     iiedsclr_thl, &
      55             :     iiedsclr_CO2, &
      56             :     l_frozen_hm, & ! if true, then the hydrometeor is frozen; otherwise liquid
      57             :     l_mix_rat_hm ! if true, then the quantity is a hydrometeor mixing ratio
      58             : 
      59             :   use clubb_precision, only : &
      60             :     time_precision, &
      61             :     core_rknd, &
      62             :     stat_nknd, &
      63             :     stat_rknd, &
      64             :     dp  ! Double Precision
      65             : 
      66             :   use constants_clubb, only : &
      67             :     cloud_frac_min, & ! Threshold for cloud fractions
      68             :     cm3_per_m3, & ! Cubic centimeters per cubic meter
      69             :     Cp, & ! Dry air specific heat at constant p [J/kg/K]
      70             :     em_min, & ! Minimum value for em (turbulence kinetic energy)
      71             :     ep, & ! ep  = 0.622  [-]
      72             :     fstderr, & ! Fortran file unit I/O constant
      73             :     fstdout, & ! Fortran file unit I/O constant
      74             :     grav, & ! Gravitational acceleration     [m/s^2]
      75             :     Ls, & ! Latent heat of sublimation          [J/kg]
      76             :     Lv, & ! Latent heat of vaporization         [J/kg]
      77             :     Lf, & ! Latent heat of fusion               [J/kg]
      78             :     pi, & ! The ratio of radii to their circumference
      79             :     pi_dp, & ! pi in double precision
      80             :     radians_per_deg_dp, &
      81             :     Rd, & ! Dry air gas constant                [J/kg/K]
      82             :     Rv, & ! Water vapor gas constant            [J/kg/K]
      83             :     sec_per_day, & ! Seconds in a day.
      84             :     sec_per_hr, &  ! Seconds in an hour.
      85             :     sec_per_min, & ! Seconds in a minute.
      86             :     T_freeze_K, & ! Freezing point of water [K]
      87             :     var_length, & ! Maximum variable name length in CLUBB GrADS or netCDF output
      88             :     zero, & ! 0.0_core_rknd
      89             :     zero_threshold, & ! Defining a threshold on a physical quantity to be 0.
      90             :     ! Tolerances
      91             :     Nc_tol, & ! Tolerance value for N_c  [#/kg]
      92             :     Ng_tol, & ! Tolerance value for N_s [#/kg]
      93             :     Ni_tol, & ! Tolerance value for N_i [#/kg]
      94             :     Nr_tol, & ! Tolerance value for N_r [#/kg]
      95             :     Ns_tol, & ! Tolerance value for N_s [#/kg]
      96             :     rg_tol, & ! Tolerance value for r_g [kg/kg]
      97             :     rho_lw, &
      98             :     ri_tol, & ! Tolerance value for r_i [kg/kg]
      99             :     rr_tol, & ! Tolerance value for r_r [kg/kg]
     100             :     rs_tol, & ! Tolerance value for r_s [kg/kg]
     101             :     rt_tol, & ! [kg/kg]
     102             :     thl_tol, & ! [K]
     103             :     w_tol_sqd, & ! [m^2/s^2]
     104             :     num_hf_draw_points ! Neighboring points to use in hole filling algorithm
     105             : 
     106             :   use corr_varnce_module, only : &
     107             :       corr_array_n_cloud, & ! Variable(s)
     108             :       corr_array_n_below, &
     109             :       pdf_dim,        &
     110             :       hmp2_ip_on_hmm2_ip, &
     111             :       Ncnp2_on_Ncnm2,     &
     112             :       hmp2_ip_on_hmm2_ip_slope_type,      & ! Types
     113             :       hmp2_ip_on_hmm2_ip_intrcpt_type
     114             : 
     115             :   use error_code, only: &
     116             :       clubb_at_least_debug_level,  & ! Procedure
     117             :       err_code,                    & ! Error Indicator
     118             :       clubb_no_error,              & ! Constants
     119             :       clubb_fatal_error
     120             : 
     121             :   use hydromet_pdf_parameter_module, only : &
     122             :     hydromet_pdf_parameter, &
     123             :     precipitation_fractions
     124             : 
     125             :   use model_flags, only : &
     126             :       clubb_config_flags_type, & ! Type
     127             :       iiPDF_ADG1, &
     128             :       iiPDF_new_hybrid, &
     129             :       ipdf_pre_advance_fields, &
     130             :       ipdf_post_advance_fields, &
     131             :       l_use_boussinesq    ! Use Boussinesq form of predictive equations (default is Anelastic).
     132             : 
     133             :   use parameters_model, only : &
     134             :     hydromet_dim    ! Number of hydrometeor species
     135             : 
     136             :   use parameters_tunable, only : &
     137             :     params_list,         & ! Variable(s)
     138             :     nu_vertical_res_dep    ! Type(s)
     139             : 
     140             :   use parameter_indices, only:  &
     141             :     nparams, & ! Variable(s)
     142             :     iC1, iC1b, iC1c, &
     143             :     iC2rt, iC2thl, iC2rtthl, iC4, iC_uu_shr, iC_uu_buoy, &
     144             :     iC6rt, iC6rtb, iC6rtc, iC6thl, iC6thlb, iC6thlc, &
     145             :     iC7, iC7b, iC7c, iC8, iC8b, iC10, iC11, iC11b, iC11c, &
     146             :     iC12, iC13, iC14, iC_wp3_pr_turb, iC_wp3_pr_dfsn, iC_wp2_splat, &
     147             :     iC6rt_Lscale0, iC6thl_Lscale0, &
     148             :     iC7_Lscale0, iwpxp_L_thresh, ic_K, ic_K1, inu1, &
     149             :     ic_K2, inu2, ic_K6, inu6, ic_K8, inu8, ic_K9, inu9, &
     150             :     inu10, ic_K_hm, ic_K_hmb, iK_hm_min_coef, inu_hm, &
     151             :     islope_coef_spread_DG_means_w, ipdf_component_stdev_factor_w, &
     152             :     icoef_spread_DG_means_rt, icoef_spread_DG_means_thl, &
     153             :     ibeta, igamma_coef, igamma_coefb, igamma_coefc, ilmin_coef, &
     154             :     iomicron, izeta_vrnce_rat, iupsilon_precip_frac_rat, &
     155             :     ilambda0_stability_coef, imult_coef, itaumin, itaumax, imu, &
     156             :     iLscale_mu_coef, iLscale_pert_coef, ialpha_corr, iSkw_denom_coef, &
     157             :     ic_K10, ic_K10h, ithlp2_rad_coef, ithlp2_rad_cloud_frac_thresh, &
     158             :     iup2_sfc_coef, iSkw_max_mag, iC_invrs_tau_bkgnd, &
     159             :     iC_invrs_tau_sfc, iC_invrs_tau_shear, iC_invrs_tau_N2, &
     160             :     iC_invrs_tau_N2_wp2, iC_invrs_tau_N2_xp2, iC_invrs_tau_N2_wpxp, &
     161             :     iC_invrs_tau_N2_clear_wp3, ialtitude_threshold, irtp2_clip_coef, &
     162             :     iRichardson_num_min, iRichardson_num_max, iwpxp_Ri_exp, &
     163             :     ia3_coef_min, ia_const, iCx_min, iCx_max, ibv_efold, iz_displace
     164             : 
     165             : 
     166             :   use pdf_parameter_module, only : &
     167             : ! The CLUBB_CAM preprocessor directives are being commented out because this
     168             : ! code is now also used for WRF-CLUBB.
     169             : !#ifdef CLUBB_CAM /* Code for storing pdf_parameter structs in pbuf as array */
     170             :     num_pdf_params, &
     171             : !#endif
     172             :     pdf_parameter, &
     173             :     implicit_coefs_terms
     174             : 
     175             :   use sponge_layer_damping, only : &
     176             :     thlm_sponge_damp_settings,    & ! Variable(s)
     177             :     rtm_sponge_damp_settings,     &
     178             :     uv_sponge_damp_settings,      &
     179             :     wp2_sponge_damp_settings,     &
     180             :     wp3_sponge_damp_settings,     &
     181             :     up2_vp2_sponge_damp_settings, &
     182             :     thlm_sponge_damp_profile,     &
     183             :     rtm_sponge_damp_profile,      &
     184             :     uv_sponge_damp_profile,       &
     185             :     wp2_sponge_damp_profile,      &
     186             :     wp3_sponge_damp_profile,      &
     187             :     up2_vp2_sponge_damp_profile
     188             : 
     189             :   use stat_file_module, only : &
     190             :     clubb_i, &    ! Used to output multiple columns
     191             :     clubb_j       ! The indices must not exceed nlon (for i) or nlat (for j).
     192             : 
     193             :   use stats_rad_zm_module, only : &
     194             :     nvarmax_rad_zm ! Maximum variables allowed
     195             : 
     196             :   use stats_rad_zt_module, only : &
     197             :     nvarmax_rad_zt  ! Maximum variables allowed
     198             : 
     199             :   use stats_zm_module, only : &
     200             :     nvarmax_zm ! Maximum variables allowed
     201             : 
     202             :   use stats_zt_module, only : &
     203             :     nvarmax_zt ! Maximum variables allowed
     204             : 
     205             :   use stats_sfc_module, only : &
     206             :     nvarmax_sfc
     207             : 
     208             :   use stats_variables, only: & 
     209             :     stats_metadata_type
     210             : 
     211             :   use grid_class, only: grid ! Type
     212             :   
     213             :   use grid_class, only: &
     214             :     ! Interpolate momentum level variables to thermodynamic levels 
     215             :     zm2zt_api => zm2zt, &
     216             : 
     217             :     ! Interpolate thermodynamic level variables to momentum levels 
     218             :     zt2zm_api => zt2zm
     219             : 
     220             :   use saturation, only: &
     221             :     ! Used to compute the saturation mixing ratio of liquid water.
     222             :     sat_mixrat_liq_api => sat_mixrat_liq
     223             : 
     224             :   use T_in_K_module, only : &
     225             :     ! Calculates absolute temperature from liquid water potential
     226             :     ! temperature.  (Does not include ice.)
     227             :     thlm2T_in_K_api => thlm2T_in_K, &
     228             : 
     229             :     ! Calculates liquid water potential temperature from absolute temperature 
     230             :     T_in_K2thlm_api => T_in_K2thlm
     231             : 
     232             :   use advance_clubb_core_module, only: &
     233             :     setup_clubb_core_api => setup_clubb_core
     234             : 
     235             :   use fill_holes, only : &
     236             :     fill_holes_driver_api => fill_holes_driver
     237             : 
     238             :   use stats_rad_zm_module, only : &
     239             :     stats_init_rad_zm_api => stats_init_rad_zm
     240             : 
     241             :   use stats_rad_zt_module, only : &
     242             :    stats_init_rad_zt_api => stats_init_rad_zt
     243             : 
     244             :   use stats_zm_module, only : &
     245             :    stats_init_zm_api => stats_init_zm
     246             : 
     247             :   use stats_zt_module, only : &
     248             :    stats_init_zt_api => stats_init_zt
     249             : 
     250             :   use stats_sfc_module, only : &
     251             :    stats_init_sfc_api => stats_init_sfc
     252             : 
     253             :   use stats_clubb_utilities, only : &
     254             :     stats_begin_timestep_api => stats_begin_timestep
     255             : 
     256             :   use stats_type, only: stats ! Type
     257             : 
     258             :   implicit none
     259             : 
     260             :   private
     261             : 
     262             :   public &
     263             :     ! To Implement CLUBB:
     264             :     set_default_parameters_api, & ! Procedure(s)
     265             :     read_parameters_api, &
     266             :     setup_clubb_core_api, &
     267             :         ! CLUBB can be set more specifically using these flags:
     268             :         iiPDF_ADG1, &
     269             :         iiPDF_new_hybrid, &
     270             :         ipdf_pre_advance_fields, &
     271             :         ipdf_post_advance_fields, &
     272             :         l_use_boussinesq, &
     273             :         ! The parameters of CLUBB can be retrieved and tuned using these indices:
     274             :         iC1, iC1b, iC1c, &
     275             :         iC2rt, iC2thl, iC2rtthl, iC4, iC_uu_shr, iC_uu_buoy, &
     276             :         iC6rt, iC6rtb, iC6rtc, iC6thl, iC6thlb, iC6thlc, &
     277             :         iC7, iC7b, iC7c, iC8, iC8b, iC10, iC11, iC11b, iC11c, &
     278             :         iC12, iC13, iC14, iC_wp3_pr_turb, iC_wp3_pr_dfsn, iC_wp2_splat, & 
     279             :         iC6rt_Lscale0, iC6thl_Lscale0, &
     280             :         iC7_Lscale0, iwpxp_L_thresh, ic_K, ic_K1, inu1, &
     281             :         ic_K2, inu2, ic_K6, inu6, ic_K8, inu8, ic_K9, inu9, &
     282             :         inu10, ic_K_hm, ic_K_hmb, iK_hm_min_coef, inu_hm, &
     283             :         islope_coef_spread_DG_means_w, ipdf_component_stdev_factor_w, &
     284             :         icoef_spread_DG_means_rt, icoef_spread_DG_means_thl, &
     285             :         ibeta, igamma_coef, igamma_coefb, igamma_coefc, ilmin_coef, &
     286             :         iomicron, izeta_vrnce_rat, iupsilon_precip_frac_rat, &
     287             :         ilambda0_stability_coef, imult_coef, itaumin, itaumax, imu, &
     288             :         iLscale_mu_coef, iLscale_pert_coef, ialpha_corr, iSkw_denom_coef, &
     289             :         ic_K10, ic_K10h, ithlp2_rad_coef, ithlp2_rad_cloud_frac_thresh, &
     290             :         iup2_sfc_coef, iSkw_max_mag, iC_invrs_tau_bkgnd, &
     291             :         iC_invrs_tau_sfc, iC_invrs_tau_shear, iC_invrs_tau_N2, &
     292             :         iC_invrs_tau_N2_wp2, iC_invrs_tau_N2_xp2, iC_invrs_tau_N2_wpxp, &
     293             :         iC_invrs_tau_N2_clear_wp3, ialtitude_threshold, irtp2_clip_coef, &
     294             :         iRichardson_num_min, iRichardson_num_max, iwpxp_Ri_exp, &
     295             :         ia3_coef_min, ia_const, iCx_min, iCx_max, ibv_efold, iz_displace
     296             : 
     297             : 
     298             : 
     299             :   public &
     300             :         advance_clubb_core_api, &
     301             :         advance_clubb_core_api_single_col, &
     302             :         advance_clubb_core_api_multi_col, &
     303             :         pdf_parameter, &
     304             :         implicit_coefs_terms, &
     305             :         ! A hydromet array is required, and these variables are required for a hydromet array:
     306             :         hydromet_list, &
     307             :         hydromet_tol, &
     308             :         hydromet_dim, &
     309             :         iiNg, &
     310             :         iiNi, &
     311             :         iiNr, &
     312             :         iiNs, &
     313             :         iirg, &
     314             :         iiri, &
     315             :         iirr, &
     316             :         iirs, &
     317             :         iisclr_rt, &
     318             :         iisclr_thl, &
     319             :         iisclr_CO2, &
     320             :         iiedsclr_rt, &
     321             :         iiedsclr_thl, &
     322             :         iiedsclr_CO2, &
     323             :         l_frozen_hm, &
     324             :         l_mix_rat_hm, &
     325             :     cleanup_clubb_core_api
     326             : 
     327             :   public &
     328             :     ! To Implement SILHS:
     329             :     setup_corr_varnce_array_api, &
     330             :     setup_pdf_parameters_api, &
     331             :     hydromet_pdf_parameter, &
     332             :     init_pdf_hydromet_arrays_api, &
     333             :     ! generate_silhs_sample - SILHS API
     334             :     genrand_init_api, & ! if you are doing restarts)
     335             :     genrand_state, &
     336             :     genrand_srepr, &
     337             :     genrand_intg, &
     338             :     ! To use the results, you will need these variables:
     339             :     corr_array_n_cloud, &
     340             :     corr_array_n_below, &
     341             :     pdf_dim,        &
     342             :     iiPDF_chi,          &
     343             :     iiPDF_rr,           &
     344             :     iiPDF_w,            &
     345             :     iiPDF_Nr,           &
     346             :     iiPDF_ri,           &
     347             :     iiPDF_Ni,           &
     348             :     iiPDF_Ncn,          &
     349             :     iiPDF_rs,           &
     350             :     iiPDF_Ns,           &
     351             :     iiPDF_rg,           &
     352             :     iiPDF_Ng,           &
     353             :     hmp2_ip_on_hmm2_ip, &
     354             :     Ncnp2_on_Ncnm2,     &
     355             :     hmp2_ip_on_hmm2_ip_slope_type,      & ! Types
     356             :     hmp2_ip_on_hmm2_ip_intrcpt_type, &
     357             :     grid, &
     358             :     stats
     359             : 
     360             :   public &
     361             :     ! To Interact With CLUBB's Grid:
     362             :     ! For Varying Grids
     363             :     setup_grid_heights_api, &    ! if heights vary with time
     364             :     setup_grid_api
     365             : 
     366             :   public &
     367             :     ! To Obtain More Output from CLUBB for Diagnostics:
     368             :     stats_begin_timestep_api, &
     369             :     stats_end_timestep_api, &
     370             :     stats_finalize_api, &
     371             :     stats_init_api
     372             : 
     373             :   public :: &
     374             :     calculate_thlp2_rad_api, params_list, &
     375             :     update_xp2_mc_api, sat_mixrat_liq_api
     376             : 
     377             :   public :: &
     378             :     ! To Convert Between Common CLUBB-related quantities:
     379             :     lin_interpolate_two_points_api, & ! OR
     380             :     lin_interpolate_on_grid_api, &
     381             :     T_in_K2thlm_api, &
     382             :     thlm2T_in_K_api, &
     383             :     zm2zt_api, &
     384             :     zt2zm_api
     385             : 
     386             :   public &
     387             :     ! To Check For and Handle CLUBB's Errors:
     388             :     calculate_spurious_source_api, &
     389             :     clubb_at_least_debug_level_api, &
     390             :     clubb_fatal_error, &
     391             :     clubb_no_error, &
     392             :     fill_holes_driver_api, & ! OR
     393             :     fill_holes_vertical_api, &
     394             :     fill_holes_hydromet_api, &
     395             :     set_clubb_debug_level_api, &
     396             :     vertical_integral_api, &
     397             :     num_hf_draw_points
     398             : 
     399             :   public &
     400             :     ! Constants That May be Helpful:
     401             :     cloud_frac_min, &
     402             :     cm3_per_m3, &
     403             :     core_rknd, &
     404             :     Cp, &
     405             :     dp, &
     406             :     em_min, &
     407             :     ep, &
     408             :     fstderr, &
     409             :     fstdout, &
     410             :     grav, &
     411             :     Lf, &
     412             :     Ls, &
     413             :     Lv, &
     414             :     pi_dp, &
     415             :     pi, &
     416             :     radians_per_deg_dp, &
     417             :     Rd, &
     418             :     Rv, &
     419             :     sec_per_day, &
     420             :     sec_per_hr, &
     421             :     sec_per_min, &
     422             :     T_freeze_K, &
     423             :     time_precision, &
     424             :     var_length, &
     425             :     zero_threshold, &
     426             :     zero, &
     427             :     ! Tolerances
     428             :     Nc_tol, &
     429             :     Ng_tol, &
     430             :     Ni_tol, &
     431             :     Nr_tol, &
     432             :     Ns_tol, &
     433             :     rg_tol, &
     434             :     rho_lw, &
     435             :     ri_tol, &
     436             :     rr_tol, &
     437             :     rs_tol, &
     438             :     rt_tol, &
     439             :     thl_tol, &
     440             :     w_tol_sqd
     441             : 
     442             :   public &
     443             :     ! Attempt to Not Use the Following:
     444             : ! The CLUBB_CAM preprocessor directives are being commented out because this
     445             : ! code is now also used for WRF-CLUBB.
     446             : !#ifdef CLUBB_CAM /* Code for storing pdf_parameter structs in pbuf as array */
     447             :     pack_pdf_params_api, &
     448             :     unpack_pdf_params_api, &
     449             :     num_pdf_params, &
     450             : !#endif
     451             :     init_pdf_params_api, &
     452             :     init_precip_fracs_api, &
     453             :     precipitation_fractions, &
     454             :     init_pdf_implicit_coefs_terms_api, &
     455             :     adj_low_res_nu_api, &
     456             :     assignment( = ), &
     457             :     clubb_i, &
     458             :     clubb_j, &
     459             :     compute_current_date_api, &
     460             :     gregorian2julian_day_api, &
     461             :     leap_year_api, &
     462             :     nvarmax_rad_zm, &
     463             :     nvarmax_rad_zt, &
     464             :     nvarmax_sfc, &
     465             :     nvarmax_zm, &
     466             :     nvarmax_zt
     467             : 
     468             :   public &
     469             :     nparams, &
     470             :     nu_vertical_res_dep, &
     471             :     setup_parameters_api, &
     472             :     stat_nknd, &
     473             :     stat_rknd, &
     474             :     stats_accumulate_hydromet_api, &
     475             :     stats_init_rad_zm_api, &
     476             :     stats_init_rad_zt_api, &
     477             :     stats_init_sfc_api, &
     478             :     stats_init_zm_api, &
     479             :     stats_init_zt_api, &
     480             :     stats_metadata_type
     481             : 
     482             :   public &
     483             :     ! Needed to use the configurable CLUBB flags
     484             :     clubb_config_flags_type, &
     485             :     set_default_clubb_config_flags_api, &
     486             :     initialize_clubb_config_flags_type_api, &
     487             :     print_clubb_config_flags_api
     488             : 
     489             :   public &
     490             :     thlm_sponge_damp_settings,      & ! Variable(s)
     491             :     rtm_sponge_damp_settings,       &
     492             :     uv_sponge_damp_settings,        &
     493             :     wp2_sponge_damp_settings,       &
     494             :     wp3_sponge_damp_settings,       &
     495             :     up2_vp2_sponge_damp_settings,   &
     496             :     thlm_sponge_damp_profile,       &
     497             :     rtm_sponge_damp_profile,        &
     498             :     uv_sponge_damp_profile,         &
     499             :     wp2_sponge_damp_profile,        &
     500             :     wp3_sponge_damp_profile,        &
     501             :     up2_vp2_sponge_damp_profile,    &
     502             :     initialize_tau_sponge_damp_api, & ! Procedure(s)
     503             :     finalize_tau_sponge_damp_api
     504             :     
     505             :   public &
     506             :    copy_single_pdf_params_to_multi, &
     507             :    copy_multi_pdf_params_to_single
     508             :   
     509             :   interface setup_pdf_parameters_api
     510             :     module procedure setup_pdf_parameters_api_single_col
     511             :     module procedure setup_pdf_parameters_api_multi_col
     512             :   end interface
     513             :   
     514             :   interface advance_clubb_core_api
     515             :     module procedure advance_clubb_core_api_single_col
     516             :     module procedure advance_clubb_core_api_multi_col
     517             :   end interface
     518             :   
     519             :   interface setup_grid_api
     520             :     module procedure setup_grid_api_single_col
     521             :     module procedure setup_grid_api_multi_col
     522             :   end interface
     523             :   
     524             :   interface setup_parameters_api
     525             :     module procedure setup_parameters_api_single_col
     526             :     module procedure setup_parameters_api_multi_col
     527             :   end interface
     528             : 
     529             :   interface adj_low_res_nu_api
     530             :     module procedure adj_low_res_nu_api_single_col
     531             :     module procedure adj_low_res_nu_api_multi_col
     532             :   end interface
     533             :   
     534             :   interface setup_grid_heights_api
     535             :     module procedure setup_grid_heights_api_single_col
     536             :     module procedure setup_grid_heights_api_multi_col
     537             :   end interface
     538             :   
     539             :   interface update_xp2_mc_api
     540             :     module procedure update_xp2_mc_api_single_col
     541             :     module procedure update_xp2_mc_api_multi_col
     542             :   end interface
     543             :   
     544             : contains
     545             : 
     546             :   !================================================================================================
     547             :   ! advance_clubb_core - Advances the model one timestep.
     548             :   !================================================================================================
     549             : 
     550           0 :   subroutine advance_clubb_core_api_single_col( gr, &
     551             :     l_implemented, dt, fcor, sfc_elevation, hydromet_dim, & ! intent(in)
     552           0 :     thlm_forcing, rtm_forcing, um_forcing, vm_forcing, &    ! intent(in)
     553           0 :     sclrm_forcing, edsclrm_forcing, wprtp_forcing, &        ! intent(in)
     554           0 :     wpthlp_forcing, rtp2_forcing, thlp2_forcing, &          ! intent(in)
     555           0 :     rtpthlp_forcing, wm_zm, wm_zt, &                        ! intent(in)
     556             :     wpthlp_sfc, wprtp_sfc, upwp_sfc, vpwp_sfc, &            ! intent(in)
     557           0 :     wpsclrp_sfc, wpedsclrp_sfc, &                           ! intent(in)
     558             :     upwp_sfc_pert, vpwp_sfc_pert, &                         ! intent(in)
     559           0 :     rtm_ref, thlm_ref, um_ref, vm_ref, ug, vg, &            ! Intent(in)
     560           0 :     p_in_Pa, rho_zm, rho, exner, &                          ! intent(in)
     561           0 :     rho_ds_zm, rho_ds_zt, invrs_rho_ds_zm, &                ! intent(in)
     562           0 :     invrs_rho_ds_zt, thv_ds_zm, thv_ds_zt, hydromet, &      ! intent(in)
     563           0 :     rfrzm, radf, &                                          ! intent(in)
     564             : #ifdef CLUBBND_CAM
     565             :     varmu, &                                                ! intent(in)
     566             : #endif
     567           0 :     wphydrometp, wp2hmp, rtphmp, thlphmp, &                 ! intent(in)
     568             :     host_dx, host_dy, &                                     ! intent(in)
     569             :     clubb_params, nu_vert_res_dep, lmin, &                  ! intent(in)
     570             :     clubb_config_flags, &                                   ! intent(in)
     571             :     stats_metadata, &                                       ! intent(in)
     572             :     stats_zt, stats_zm, stats_sfc, &                        ! intent(inout)
     573           0 :     um, vm, upwp, vpwp, up2, vp2, up3, vp3, &               ! intent(inout)
     574           0 :     thlm, rtm, wprtp, wpthlp, &                             ! intent(inout)
     575           0 :     wp2, wp3, rtp2, rtp3, thlp2, thlp3, rtpthlp, &          ! intent(inout)
     576           0 :     sclrm,   &
     577             : #ifdef GFDL
     578             :                sclrm_trsport_only,  &  ! h1g, 2010-06-16    ! intent(inout)
     579             : #endif
     580           0 :     sclrp2, sclrp3, sclrprtp, sclrpthlp, &                  ! intent(inout)
     581           0 :     wpsclrp, edsclrm, err_code_api, &                       ! intent(inout)
     582           0 :     rcm, cloud_frac, &                                      ! intent(inout)
     583           0 :     wpthvp, wp2thvp, rtpthvp, thlpthvp, &                   ! intent(inout)
     584           0 :     sclrpthvp, &                                            ! intent(inout)
     585           0 :     wp2rtp, wp2thlp, uprcp, vprcp, rc_coef, wp4, &          ! intent(inout)
     586           0 :     wpup2, wpvp2, wp2up2, wp2vp2, ice_supersat_frac, &      ! intent(inout)
     587           0 :     um_pert, vm_pert, upwp_pert, vpwp_pert, &               ! intent(inout)
     588             :     pdf_params, pdf_params_zm, &                            ! intent(inout)
     589             :     pdf_implicit_coefs_terms, &                             ! intent(inout)
     590             : #ifdef GFDL
     591             :                RH_crit, & !h1g, 2010-06-16                  ! intent(inout)
     592             :                do_liquid_only_in_clubb, &                   ! intent(in)
     593             : #endif
     594           0 :     Kh_zm, Kh_zt, &                                         ! intent(out)
     595             : #ifdef CLUBB_CAM
     596           0 :     qclvar, &                                               ! intent(out)
     597             : #endif
     598           0 :     thlprcp, wprcp, w_up_in_cloud, w_down_in_cloud, &       ! intent(out)
     599           0 :     cloudy_updraft_frac, cloudy_downdraft_frac, &           ! intent(out)
     600           0 :     rcm_in_layer, cloud_cover, invrs_tau_zm )               ! intent(out)
     601             : 
     602             :     use advance_clubb_core_module, only : advance_clubb_core
     603             : 
     604             :     use pdf_parameter_module, only: &
     605             :         implicit_coefs_terms    ! Variable Type(s)
     606             : 
     607             :     use parameters_model, only: &
     608             :         sclr_dim, & ! Variable(s)
     609             :         edsclr_dim
     610             : 
     611             :     use model_flags, only: &
     612             :         clubb_config_flags_type
     613             :         
     614             :     use stats_zm_module, only: &
     615             :         stats_init_zm ! Procedure(s)
     616             :         
     617             :     use stats_zt_module, only: &
     618             :         stats_init_zt
     619             :         
     620             :     use stats_sfc_module, only: &
     621             :         stats_init_sfc ! Procedure(s)
     622             : 
     623             :     implicit none
     624             : 
     625             :     !------------------------- Input Variables -------------------------
     626             :     type(grid), target, intent(in) :: gr
     627             : 
     628             :     logical, intent(in) ::  &
     629             :       l_implemented ! Is this part of a larger host model (T/F) ?
     630             : 
     631             :     real( kind = core_rknd ), intent(in) ::  &
     632             :       dt  ! Current timestep duration    [s]
     633             : 
     634             :     real( kind = core_rknd ), intent(in) ::  &
     635             :       fcor,  &          ! Coriolis forcing             [s^-1]
     636             :       sfc_elevation     ! Elevation of ground level    [m AMSL]
     637             : 
     638             :     integer, intent(in) :: &
     639             :       hydromet_dim      ! Total number of hydrometeors          [#]
     640             : 
     641             :     ! Input Variables
     642             :     real( kind = core_rknd ), intent(in), dimension(gr%nz) ::  &
     643             :       thlm_forcing,    & ! theta_l forcing (thermodynamic levels)    [K/s]
     644             :       rtm_forcing,     & ! r_t forcing (thermodynamic levels)        [(kg/kg)/s]
     645             :       um_forcing,      & ! u wind forcing (thermodynamic levels)     [m/s/s]
     646             :       vm_forcing,      & ! v wind forcing (thermodynamic levels)     [m/s/s]
     647             :       wprtp_forcing,   & ! <w'r_t'> forcing (momentum levels)    [m*K/s^2]
     648             :       wpthlp_forcing,  & ! <w'th_l'> forcing (momentum levels)   [m*(kg/kg)/s^2]
     649             :       rtp2_forcing,    & ! <r_t'^2> forcing (momentum levels)    [(kg/kg)^2/s]
     650             :       thlp2_forcing,   & ! <th_l'^2> forcing (momentum levels)   [K^2/s]
     651             :       rtpthlp_forcing, & ! <r_t'th_l'> forcing (momentum levels) [K*(kg/kg)/s]
     652             :       wm_zm,           & ! w mean wind component on momentum levels  [m/s]
     653             :       wm_zt,           & ! w mean wind component on thermo. levels   [m/s]
     654             :       rho_zm,          & ! Air density on momentum levels            [kg/m^3]
     655             :       rho,             & ! Air density on thermodynamic levels       [kg/m^3]
     656             :       rho_ds_zm,       & ! Dry, static density on momentum levels    [kg/m^3]
     657             :       rho_ds_zt,       & ! Dry, static density on thermo. levels     [kg/m^3]
     658             :       invrs_rho_ds_zm, & ! Inv. dry, static density @ momentum levs. [m^3/kg]
     659             :       invrs_rho_ds_zt, & ! Inv. dry, static density @ thermo. levs.  [m^3/kg]
     660             :       thv_ds_zm,       & ! Dry, base-state theta_v on momentum levs. [K]
     661             :       thv_ds_zt,       & ! Dry, base-state theta_v on thermo. levs.  [K]
     662             :       rfrzm              ! Total ice-phase water mixing ratio        [kg/kg]
     663             : 
     664             :     real( kind = core_rknd ), dimension(gr%nz,hydromet_dim), intent(in) :: &
     665             :       hydromet           ! Collection of hydrometeors                [units vary]
     666             : 
     667             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
     668             :       radf          ! Buoyancy production at the CL top due to LW radiative cooling [m^2/s^3]
     669             : 
     670             : #ifdef CLUBBND_CAM 
     671             :     real( kind = core_rknd ), intent(in) :: & 
     672             :       varmu 
     673             : #endif 
     674             : 
     675             :     real( kind = core_rknd ), dimension(gr%nz, hydromet_dim), intent(in) :: &
     676             :       wphydrometp, & ! Covariance of w and a hydrometeor   [(m/s) <hm units>]
     677             :       wp2hmp,      & ! Third moment: <w'^2> * <hydro.'>    [(m/s)^2 <hm units>]
     678             :       rtphmp,      & ! Covariance of rt and a hydrometeor  [(kg/kg) <hm units>]
     679             :       thlphmp        ! Covariance of thl and a hydrometeor [K <hm units>]
     680             : 
     681             :     real( kind = core_rknd ), intent(in) ::  &
     682             :       wpthlp_sfc,   & ! w' theta_l' at surface   [(m K)/s]
     683             :       wprtp_sfc,    & ! w' r_t' at surface       [(kg m)/( kg s)]
     684             :       upwp_sfc,     & ! u'w' at surface          [m^2/s^2]
     685             :       vpwp_sfc        ! v'w' at surface          [m^2/s^2]
     686             : 
     687             :     ! Passive scalar variables
     688             :     real( kind = core_rknd ), intent(in), dimension(gr%nz,sclr_dim) :: &
     689             :       sclrm_forcing    ! Passive scalar forcing         [{units vary}/s]
     690             : 
     691             :     real( kind = core_rknd ), intent(in), dimension(sclr_dim) ::  &
     692             :       wpsclrp_sfc      ! Scalar flux at surface         [{units vary} m/s]
     693             : 
     694             :     real( kind = core_rknd ), intent(in) :: &
     695             :       upwp_sfc_pert, & ! pertubed u'w' at surface    [m^2/s^2]
     696             :       vpwp_sfc_pert    ! pertubed v'w' at surface    [m^2/s^2]
     697             : 
     698             :     ! Eddy passive scalar variables
     699             :     real( kind = core_rknd ), intent(in), dimension(gr%nz,edsclr_dim) :: &
     700             :       edsclrm_forcing  ! Eddy passive scalar forcing    [{units vary}/s]
     701             : 
     702             :     real( kind = core_rknd ), intent(in), dimension(edsclr_dim) ::  &
     703             :       wpedsclrp_sfc    ! Eddy-Scalar flux at surface    [{units vary} m/s]
     704             : 
     705             :     ! Reference profiles (used for nudging, sponge damping, and Coriolis effect)
     706             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) ::  &
     707             :       rtm_ref,  & ! Initial total water mixing ratio             [kg/kg]
     708             :       thlm_ref, & ! Initial liquid water potential temperature   [K]
     709             :       um_ref,   & ! Initial u wind; Michael Falk                 [m/s]
     710             :       vm_ref,   & ! Initial v wind; Michael Falk                 [m/s]
     711             :       ug,       & ! u geostrophic wind                           [m/s]
     712             :       vg          ! v geostrophic wind                           [m/s]
     713             : 
     714             :     ! Host model horizontal grid spacing, if part of host model.
     715             :     real( kind = core_rknd ), intent(in) :: &
     716             :       host_dx,  & ! East-West horizontal grid spacing     [m]
     717             :       host_dy     ! North-South horizontal grid spacing   [m]
     718             : 
     719             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
     720             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
     721             : 
     722             :     type(nu_vertical_res_dep), intent(in) :: &
     723             :       nu_vert_res_dep    ! Vertical resolution dependent nu values
     724             : 
     725             :     real( kind = core_rknd ), intent(in) :: &
     726             :       lmin    ! Min. value for the length scale    [m]
     727             : 
     728             :     type( clubb_config_flags_type ), intent(in) :: &
     729             :       clubb_config_flags ! Derived type holding all configurable CLUBB flags
     730             : 
     731             :     type (stats_metadata_type), intent(in) :: &
     732             :       stats_metadata
     733             : 
     734             :     !------------------------- Input/Output Variables -------------------------
     735             :     type(stats), target, intent(inout) :: &
     736             :       stats_zt, &
     737             :       stats_zm, &
     738             :       stats_sfc
     739             : 
     740             :     ! These are prognostic or are planned to be in the future
     741             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz) ::  &
     742             :       um,      & ! u mean wind component (thermodynamic levels)   [m/s]
     743             :       upwp,    & ! u'w' (momentum levels)                         [m^2/s^2]
     744             :       vm,      & ! v mean wind component (thermodynamic levels)   [m/s]
     745             :       vpwp,    & ! v'w' (momentum levels)                         [m^2/s^2]
     746             :       up2,     & ! u'^2 (momentum levels)                         [m^2/s^2]
     747             :       vp2,     & ! v'^2 (momentum levels)                         [m^2/s^2]
     748             :       up3,     & ! u'^3 (thermodynamic levels)                    [m^3/s^3]
     749             :       vp3,     & ! v'^3 (thermodynamic levels)                    [m^3/s^3]
     750             :       rtm,     & ! total water mixing ratio, r_t (thermo. levels) [kg/kg]
     751             :       wprtp,   & ! w' r_t' (momentum levels)                      [(kg/kg) m/s]
     752             :       thlm,    & ! liq. water pot. temp., th_l (thermo. levels)   [K]
     753             :       wpthlp,  & ! w' th_l' (momentum levels)                     [(m/s) K]
     754             :       rtp2,    & ! r_t'^2 (momentum levels)                       [(kg/kg)^2]
     755             :       rtp3,    & ! r_t'^3 (thermodynamic levels)                  [(kg/kg)^3]
     756             :       thlp2,   & ! th_l'^2 (momentum levels)                      [K^2]
     757             :       thlp3,   & ! th_l'^3 (thermodynamic levels)                 [K^3]
     758             :       rtpthlp, & ! r_t' th_l' (momentum levels)                   [(kg/kg) K]
     759             :       wp2,     & ! w'^2 (momentum levels)                         [m^2/s^2]
     760             :       wp3        ! w'^3 (thermodynamic levels)                    [m^3/s^3]
     761             : 
     762             :     ! Passive scalar variables
     763             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz,sclr_dim) :: &
     764             :       sclrm,     & ! Passive scalar mean (thermo. levels) [units vary]
     765             :       wpsclrp,   & ! w'sclr' (momentum levels)            [{units vary} m/s]
     766             :       sclrp2,    & ! sclr'^2 (momentum levels)            [{units vary}^2]
     767             :       sclrp3,    & ! sclr'^3 (thermodynamic levels)       [{units vary}^3]
     768             :       sclrprtp,  & ! sclr'rt' (momentum levels)           [{units vary} (kg/kg)]
     769             :       sclrpthlp    ! sclr'thl' (momentum levels)          [{units vary} K]
     770             : 
     771             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz) ::  &
     772             :       p_in_Pa, & ! Air pressure (thermodynamic levels)       [Pa]
     773             :       exner      ! Exner function (thermodynamic levels)     [-]
     774             : 
     775             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz) ::  &
     776             :       rcm,        & ! cloud water mixing ratio, r_c (thermo. levels) [kg/kg]
     777             :       cloud_frac, & ! cloud fraction (thermodynamic levels)          [-]
     778             :       wpthvp,     & ! < w' th_v' > (momentum levels)                 [kg/kg K]
     779             :       wp2thvp,    & ! < w'^2 th_v' > (thermodynamic levels)          [m^2/s^2 K]
     780             :       rtpthvp,    & ! < r_t' th_v' > (momentum levels)               [kg/kg K]
     781             :       thlpthvp      ! < th_l' th_v' > (momentum levels)              [K^2]
     782             : 
     783             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz,sclr_dim) :: &
     784             :       sclrpthvp     ! < sclr' th_v' > (momentum levels)   [units vary]
     785             : 
     786             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz) ::  &
     787             :       wp2rtp,            & ! w'^2 rt' (thermodynamic levels)      [m^2/s^2 kg/kg]
     788             :       wp2thlp,           & ! w'^2 thl' (thermodynamic levels)     [m^2/s^2 K]
     789             :       uprcp,             & ! < u' r_c' > (momentum levels)        [(m/s)(kg/kg)]
     790             :       vprcp,             & ! < v' r_c' > (momentum levels)        [(m/s)(kg/kg)]
     791             :       rc_coef,           & ! Coef of X'r_c' in Eq. (34) (t-levs.) [K/(kg/kg)]
     792             :       wp4,               & ! w'^4 (momentum levels)               [m^4/s^4]
     793             :       wpup2,             & ! w'u'^2 (thermodynamic levels)        [m^3/s^3]
     794             :       wpvp2,             & ! w'v'^2 (thermodynamic levels)        [m^3/s^3]
     795             :       wp2up2,            & ! w'^2 u'^2 (momentum levels)          [m^4/s^4]
     796             :       wp2vp2,            & ! w'^2 v'^2 (momentum levels)          [m^4/s^4]
     797             :       ice_supersat_frac    ! ice cloud fraction (thermo. levels)  [-]
     798             : 
     799             :     ! Variables used to track perturbed version of winds.
     800             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz) :: &
     801             :       um_pert,   & ! perturbed <u>       [m/s]
     802             :       vm_pert,   & ! perturbed <v>       [m/s]
     803             :       upwp_pert, & ! perturbed <u'w'>    [m^2/s^2]
     804             :       vpwp_pert    ! perturbed <v'w'>    [m^2/s^2]
     805             : 
     806             :     type(pdf_parameter), intent(inout) :: &
     807             :       pdf_params,    & ! PDF parameters (thermodynamic levels)    [units vary]
     808             :       pdf_params_zm    ! PDF parameters on momentum levels        [units vary]
     809             : 
     810             :     type(implicit_coefs_terms), intent(inout) :: &
     811             :       pdf_implicit_coefs_terms    ! Implicit coefs / explicit terms [units vary]
     812             : 
     813             : #ifdef GFDL
     814             :     real( kind = core_rknd ), intent(inout), dimension(gr%nz,sclr_dim) :: &  ! h1g, 2010-06-16
     815             :       sclrm_trsport_only  ! Passive scalar concentration due to pure transport [{units vary}/s]
     816             : #endif
     817             : 
     818             :       real( kind = core_rknd ), intent(inout), dimension(gr%nz,edsclr_dim) :: &
     819             :       edsclrm   ! Eddy passive scalar mean (thermo. levels)   [units vary]
     820             : 
     821             :     !------------------------- Output Variables -------------------------
     822             :     real( kind = core_rknd ), intent(out), dimension(gr%nz) ::  &
     823             :       rcm_in_layer, & ! rcm in cloud layer                              [kg/kg]
     824             :       cloud_cover     ! cloud cover                                     [-]
     825             : 
     826             :     ! Variables that need to be output for use in host models
     827             :     real( kind = core_rknd ), intent(out), dimension(gr%nz) ::  &
     828             :       wprcp,                 & ! w'r_c' (momentum levels)              [(kg/kg) m/s]
     829             :       w_up_in_cloud,         & ! Average cloudy updraft velocity       [m/s]
     830             :       w_down_in_cloud,       & ! Average cloudy downdraft velocity     [m/s]
     831             :       cloudy_updraft_frac,   & ! cloudy updraft fraction               [-]
     832             :       cloudy_downdraft_frac, & ! cloudy downdraft fraction             [-]
     833             :       invrs_tau_zm             ! One divided by tau on zm levels       [1/s]
     834             : 
     835             :     real( kind = core_rknd ), dimension(gr%nz), intent(out) :: &
     836             :       Kh_zt, & ! Eddy diffusivity coefficient on thermodynamic levels   [m^2/s]
     837             :       Kh_zm    ! Eddy diffusivity coefficient on momentum levels        [m^2/s]
     838             : 
     839             : #ifdef CLUBB_CAM
     840             :     real( kind = core_rknd), intent(out), dimension(gr%nz) :: &
     841             :       qclvar        ! cloud water variance
     842             : #endif
     843             : 
     844             :     real( kind = core_rknd ), dimension(gr%nz), intent(out) :: &
     845             :       thlprcp    ! thl'rc'              [K kg/kg]
     846             : 
     847             :     !!! Output Variable 
     848             :     integer, intent(inout) :: err_code_api ! Diagnostic, for if some calculation goes amiss.
     849             : 
     850             : #ifdef GFDL
     851             :     ! hlg, 2010-06-16
     852             :     real( kind = core_rknd ), intent(inOUT), dimension(gr%nz, min(1,sclr_dim) , 2) :: &
     853             :       RH_crit  ! critical relative humidity for droplet and ice nucleation
     854             :     logical, intent(in)                 ::  do_liquid_only_in_clubb
     855             : #endif
     856             : 
     857             : 
     858             :     !------------------------- Local Variables -------------------------
     859             :     type(stats), dimension(1) :: &
     860           0 :       stats_zt_col, &
     861           0 :       stats_zm_col, &
     862           0 :       stats_sfc_col
     863             : 
     864             :     real( kind = core_rknd ), dimension(1) ::  &
     865             :       fcor_col,  &          ! Coriolis forcing             [s^-1]
     866             :       sfc_elevation_col     ! Elevation of ground level    [m AMSL]
     867             : 
     868             :     ! Input Variables
     869             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
     870           0 :       thlm_forcing_col,    & ! theta_l forcing (thermodynamic levels)    [K/s]
     871           0 :       rtm_forcing_col,     & ! r_t forcing (thermodynamic levels)        [(kg/kg)/s]
     872           0 :       um_forcing_col,      & ! u wind forcing (thermodynamic levels)     [m/s/s]
     873           0 :       vm_forcing_col,      & ! v wind forcing (thermodynamic levels)     [m/s/s]
     874           0 :       wprtp_forcing_col,   & ! <w'r_t'> forcing (momentum levels)    [m*K/s^2]
     875           0 :       wpthlp_forcing_col,  & ! <w'th_l'> forcing (momentum levels)   [m*(kg/kg)/s^2]
     876           0 :       rtp2_forcing_col,    & ! <r_t'^2> forcing (momentum levels)    [(kg/kg)^2/s]
     877           0 :       thlp2_forcing_col,   & ! <th_l'^2> forcing (momentum levels)   [K^2/s]
     878           0 :       rtpthlp_forcing_col, & ! <r_t'th_l'> forcing (momentum levels) [K*(kg/kg)/s]
     879           0 :       wm_zm_col,           & ! w mean wind component on momentum levels  [m/s]
     880           0 :       wm_zt_col,           & ! w mean wind component on thermo. levels   [m/s]
     881           0 :       rho_zm_col,          & ! Air density on momentum levels            [kg/m^3]
     882           0 :       rho_col,             & ! Air density on thermodynamic levels       [kg/m^3]
     883           0 :       rho_ds_zm_col,       & ! Dry, static density on momentum levels    [kg/m^3]
     884           0 :       rho_ds_zt_col,       & ! Dry, static density on thermo. levels     [kg/m^3]
     885           0 :       invrs_rho_ds_zm_col, & ! Inv. dry, static density @ momentum levs. [m^3/kg]
     886           0 :       invrs_rho_ds_zt_col, & ! Inv. dry, static density @ thermo. levs.  [m^3/kg]
     887           0 :       thv_ds_zm_col,       & ! Dry, base-state theta_v on momentum levs. [K]
     888           0 :       thv_ds_zt_col,       & ! Dry, base-state theta_v on thermo. levs.  [K]
     889           0 :       rfrzm_col              ! Total ice-phase water mixing ratio        [kg/kg]
     890             : 
     891             :     real( kind = core_rknd ), dimension(1,gr%nz,hydromet_dim) :: &
     892           0 :       hydromet_col           ! Collection of hydrometeors                [units vary]
     893             : 
     894             :     real( kind = core_rknd ), dimension(1,gr%nz) :: &
     895           0 :       radf_col          ! Buoyancy production at the CL top due to LW radiative cooling [m^2/s^3]
     896             : 
     897             : #ifdef CLUBBND_CAM 
     898             :     real( kind = core_rknd ), dimension(1) :: & 
     899             :       varmu_col 
     900             : #endif 
     901             : 
     902             :     real( kind = core_rknd ), dimension(1,gr%nz, hydromet_dim) :: &
     903           0 :       wphydrometp_col, & ! Covariance of w and a hydrometeor   [(m/s) <hm units>]
     904           0 :       wp2hmp_col,      & ! Third moment: <w'^2> * <hydro.'>    [(m/s)^2 <hm units>]
     905           0 :       rtphmp_col,      & ! Covariance of rt and a hydrometeor  [(kg/kg) <hm units>]
     906           0 :       thlphmp_col        ! Covariance of thl and a hydrometeor [K <hm units>]
     907             : 
     908             :     real( kind = core_rknd ), dimension(1) ::  &
     909             :       wpthlp_sfc_col,   & ! w' theta_l' at surface   [(m K)/s]
     910             :       wprtp_sfc_col,    & ! w' r_t' at surface       [(kg m)/( kg s)]
     911             :       upwp_sfc_col,     & ! u'w' at surface          [m^2/s^2]
     912             :       vpwp_sfc_col        ! v'w' at surface          [m^2/s^2]
     913             : 
     914             :     ! Passive scalar variables
     915             :     real( kind = core_rknd ), dimension(1,gr%nz,sclr_dim) :: &
     916           0 :       sclrm_forcing_col    ! Passive scalar forcing         [{units vary}/s]
     917             : 
     918             :     real( kind = core_rknd ), dimension(1,sclr_dim) ::  &
     919           0 :       wpsclrp_sfc_col      ! Scalar flux at surface         [{units vary} m/s]
     920             : 
     921             :     ! Eddy passive scalar variables
     922             :     real( kind = core_rknd ), dimension(1,gr%nz,edsclr_dim) :: &
     923           0 :       edsclrm_forcing_col  ! Eddy passive scalar forcing    [{units vary}/s]
     924             : 
     925             :     real( kind = core_rknd ), dimension(1) :: &
     926             :       upwp_sfc_pert_col, & ! pertubed u'w' at surface    [m^2/s^2]
     927             :       vpwp_sfc_pert_col    ! pertubed v'w' at surface    [m^2/s^2]
     928             : 
     929             :     real( kind = core_rknd ), dimension(1,edsclr_dim) ::  &
     930           0 :       wpedsclrp_sfc_col    ! Eddy-Scalar flux at surface    [{units vary} m/s]
     931             : 
     932             :     ! Reference profiles (used for nudging, sponge damping, and Coriolis effect)
     933             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
     934           0 :       rtm_ref_col,  & ! Initial total water mixing ratio             [kg/kg]
     935           0 :       thlm_ref_col, & ! Initial liquid water potential temperature   [K]
     936           0 :       um_ref_col,   & ! Initial u wind; Michael Falk                 [m/s]
     937           0 :       vm_ref_col,   & ! Initial v wind; Michael Falk                 [m/s]
     938           0 :       ug_col,       & ! u geostrophic wind                           [m/s]
     939           0 :       vg_col          ! v geostrophic wind                           [m/s]
     940             : 
     941             :     ! Host model horizontal grid spacing, if part of host model.
     942             :     real( kind = core_rknd ), dimension(1) :: &
     943             :       host_dx_col,  & ! East-West horizontal grid spacing     [m]
     944             :       host_dy_col     ! North-South horizontal grid spacing   [m]
     945             : 
     946             :     ! These are prognostic or are planned to be in the future
     947             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
     948           0 :       um_col,      & ! u mean wind component (thermodynamic levels)   [m/s]
     949           0 :       upwp_col,    & ! u'w' (momentum levels)                         [m^2/s^2]
     950           0 :       vm_col,      & ! v mean wind component (thermodynamic levels)   [m/s]
     951           0 :       vpwp_col,    & ! v'w' (momentum levels)                         [m^2/s^2]
     952           0 :       up2_col,     & ! u'^2 (momentum levels)                         [m^2/s^2]
     953           0 :       vp2_col,     & ! v'^2 (momentum levels)                         [m^2/s^2]
     954           0 :       up3_col,     & ! u'^3 (thermodynamic levels)                    [m^3/s^3]
     955           0 :       vp3_col,     & ! v'^3 (thermodynamic levels)                    [m^3/s^3]
     956           0 :       rtm_col,     & ! total water mixing ratio, r_t (thermo. levels) [kg/kg]
     957           0 :       wprtp_col,   & ! w' r_t' (momentum levels)                      [(kg/kg) m/s]
     958           0 :       thlm_col,    & ! liq. water pot. temp., th_l (thermo. levels)   [K]
     959           0 :       wpthlp_col,  & ! w' th_l' (momentum levels)                     [(m/s) K]
     960           0 :       rtp2_col,    & ! r_t'^2 (momentum levels)                       [(kg/kg)^2]
     961           0 :       rtp3_col,    & ! r_t'^3 (thermodynamic levels)                  [(kg/kg)^3]
     962           0 :       thlp2_col,   & ! th_l'^2 (momentum levels)                      [K^2]
     963           0 :       thlp3_col,   & ! th_l'^3 (thermodynamic levels)                 [K^3]
     964           0 :       rtpthlp_col, & ! r_t' th_l' (momentum levels)                   [(kg/kg) K]
     965           0 :       wp2_col,     & ! w'^2 (momentum levels)                         [m^2/s^2]
     966           0 :       wp3_col        ! w'^3 (thermodynamic levels)                    [m^3/s^3]
     967             : 
     968             :     ! Passive scalar variables
     969             :     real( kind = core_rknd ), dimension(1,gr%nz,sclr_dim) :: &
     970           0 :       sclrm_col,     & ! Passive scalar mean (thermo. levels) [units vary]
     971           0 :       wpsclrp_col,   & ! w'sclr' (momentum levels)            [{units vary} m/s]
     972           0 :       sclrp2_col,    & ! sclr'^2 (momentum levels)            [{units vary}^2]
     973           0 :       sclrp3_col,    & ! sclr'^3 (thermodynamic levels)       [{units vary}^3]
     974           0 :       sclrprtp_col,  & ! sclr'rt' (momentum levels)           [{units vary} (kg/kg)]
     975           0 :       sclrpthlp_col    ! sclr'thl' (momentum levels)          [{units vary} K]
     976             : 
     977             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
     978           0 :       p_in_Pa_col, & ! Air pressure (thermodynamic levels)       [Pa]
     979           0 :       exner_col      ! Exner function (thermodynamic levels)     [-]
     980             : 
     981             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
     982           0 :       rcm_col,        & ! cloud water mixing ratio, r_c (thermo. levels) [kg/kg]
     983           0 :       cloud_frac_col, & ! cloud fraction (thermodynamic levels)          [-]
     984           0 :       wpthvp_col,     & ! < w' th_v' > (momentum levels)                 [kg/kg K]
     985           0 :       wp2thvp_col,    & ! < w'^2 th_v' > (thermodynamic levels)          [m^2/s^2 K]
     986           0 :       rtpthvp_col,    & ! < r_t' th_v' > (momentum levels)               [kg/kg K]
     987           0 :       thlpthvp_col      ! < th_l' th_v' > (momentum levels)              [K^2]
     988             : 
     989             :     real( kind = core_rknd ), dimension(1,gr%nz,sclr_dim) :: &
     990           0 :       sclrpthvp_col     ! < sclr' th_v' > (momentum levels)   [units vary]
     991             : 
     992             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
     993           0 :       wp2rtp_col,            & ! w'^2 rt' (thermodynamic levels)      [m^2/s^2 kg/kg]
     994           0 :       wp2thlp_col,           & ! w'^2 thl' (thermodynamic levels)     [m^2/s^2 K]
     995           0 :       uprcp_col,             & ! < u' r_c' > (momentum levels)        [(m/s)(kg/kg)]
     996           0 :       vprcp_col,             & ! < v' r_c' > (momentum levels)        [(m/s)(kg/kg)]
     997           0 :       rc_coef_col,           & ! Coef of X'r_c' in Eq. (34) (t-levs.) [K/(kg/kg)]
     998           0 :       wp4_col,               & ! w'^4 (momentum levels)               [m^4/s^4]
     999           0 :       wpup2_col,             & ! w'u'^2 (thermodynamic levels)        [m^3/s^3]
    1000           0 :       wpvp2_col,             & ! w'v'^2 (thermodynamic levels)        [m^3/s^3]
    1001           0 :       wp2up2_col,            & ! w'^2 u'^2 (momentum levels)          [m^4/s^4]
    1002           0 :       wp2vp2_col,            & ! w'^2 v'^2 (momentum levels)          [m^4/s^4]
    1003           0 :       ice_supersat_frac_col    ! ice cloud fraction (thermo. levels)  [-]
    1004             : 
    1005             :     ! Variables used to track perturbed version of winds.
    1006             :     real( kind = core_rknd ), dimension(1,gr%nz) :: &
    1007           0 :       um_pert_col,   & ! perturbed <u>       [m/s]
    1008           0 :       vm_pert_col,   & ! perturbed <v>       [m/s]
    1009           0 :       upwp_pert_col, & ! perturbed <u'w'>    [m^2/s^2]
    1010           0 :       vpwp_pert_col    ! perturbed <v'w'>    [m^2/s^2]
    1011             : 
    1012             : #ifdef GFDL
    1013             :     real( kind = core_rknd ), dimension(1,gr%nz,sclr_dim) :: &  ! h1g, 2010-06-16
    1014             :       sclrm_trsport_only_col  ! Passive scalar concentration due to pure transport [{units vary}/s]
    1015             : #endif
    1016             : 
    1017             :       real( kind = core_rknd ), dimension(1,gr%nz,edsclr_dim) :: &
    1018           0 :       edsclrm_col   ! Eddy passive scalar mean (thermo. levels)   [units vary]
    1019             : 
    1020             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
    1021           0 :       rcm_in_layer_col, & ! rcm in cloud layer                              [kg/kg]
    1022           0 :       cloud_cover_col     ! cloud cover                                     [-]
    1023             : 
    1024             :     ! Variables that need to be output for use in host models
    1025             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
    1026           0 :       wprcp_col,                 & ! w'r_c' (momentum levels)              [(kg/kg) m/s]
    1027           0 :       w_up_in_cloud_col,         & ! Average cloudy updraft velocity       [m/s]
    1028           0 :       w_down_in_cloud_col,       & ! Average cloudy downdraft velocity     [m/s]
    1029           0 :       cloudy_updraft_frac_col,   & ! cloudy updraft fraction               [-]
    1030           0 :       cloudy_downdraft_frac_col, & ! cloudy downdraft fraction             [-]
    1031           0 :       invrs_tau_zm_col             ! One divided by tau on zm levels       [1/s]
    1032             : 
    1033             :     real( kind = core_rknd ), dimension(1,gr%nz) :: &
    1034           0 :       Kh_zt_col, & ! Eddy diffusivity coefficient on thermodynamic levels   [m^2/s]
    1035           0 :       Kh_zm_col    ! Eddy diffusivity coefficient on momentum levels        [m^2/s]
    1036             : 
    1037             : #ifdef CLUBB_CAM
    1038             :     real( kind = core_rknd), dimension(1,gr%nz) :: &
    1039           0 :       qclvar_col        ! cloud water variance
    1040             : #endif
    1041             : 
    1042             :     real( kind = core_rknd ), dimension(1,gr%nz) :: &
    1043           0 :       thlprcp_col    ! thl'rc'              [K kg/kg]
    1044             :       
    1045             : #ifdef GFDL
    1046             :     ! hlg, 2010-06-16
    1047             :     real( kind = core_rknd ), dimension(1,gr%nz, min(1,sclr_dim) , 2) :: &
    1048             :       RH_crit_col  ! critical relative humidity for droplet and ice nucleation
    1049             :     logical, intent(in)                 ::  do_liquid_only_in_clubb
    1050             : #endif
    1051             : 
    1052             :     integer :: i
    1053             : 
    1054             :     !------------------------- Begin Code -------------------------
    1055             : 
    1056           0 :     fcor_col(1) = fcor
    1057           0 :     sfc_elevation_col(1) = sfc_elevation
    1058             :     
    1059           0 :     thlm_forcing_col(1,:) = thlm_forcing
    1060           0 :     rtm_forcing_col(1,:) = rtm_forcing
    1061           0 :     um_forcing_col(1,:) = um_forcing
    1062           0 :     vm_forcing_col(1,:) = vm_forcing
    1063           0 :     sclrm_forcing_col(1,:,:) = sclrm_forcing
    1064           0 :     edsclrm_forcing_col(1,:,:) = edsclrm_forcing
    1065           0 :     wprtp_forcing_col(1,:) = wprtp_forcing
    1066           0 :     wpthlp_forcing_col(1,:) = wpthlp_forcing
    1067           0 :     rtp2_forcing_col(1,:) = rtp2_forcing
    1068           0 :     thlp2_forcing_col(1,:) = thlp2_forcing
    1069           0 :     rtpthlp_forcing_col(1,:) = rtpthlp_forcing
    1070           0 :     wm_zm_col(1,:) = wm_zm
    1071           0 :     wm_zt_col(1,:) = wm_zt
    1072             :     
    1073           0 :     wpthlp_sfc_col(1) = wpthlp_sfc
    1074           0 :     wprtp_sfc_col(1) = wprtp_sfc
    1075           0 :     upwp_sfc_col(1) = upwp_sfc
    1076           0 :     vpwp_sfc_col(1) = vpwp_sfc
    1077             :     
    1078           0 :     wpsclrp_sfc_col(1,:) = wpsclrp_sfc
    1079           0 :     wpedsclrp_sfc_col(1,:) = wpedsclrp_sfc
    1080             :     
    1081           0 :     upwp_sfc_pert_col(1) = upwp_sfc_pert
    1082           0 :     vpwp_sfc_pert_col(1) = vpwp_sfc_pert
    1083             : 
    1084           0 :     rtm_ref_col(1,:) = rtm_ref
    1085           0 :     thlm_ref_col(1,:) = thlm_ref
    1086           0 :     um_ref_col(1,:) = um_ref
    1087           0 :     vm_ref_col(1,:) = vm_ref
    1088           0 :     ug_col(1,:) = ug
    1089           0 :     vg_col(1,:) = vg
    1090             :     
    1091           0 :     p_in_Pa_col(1,:) = p_in_Pa
    1092           0 :     rho_zm_col(1,:) = rho_zm
    1093           0 :     rho_col(1,:) = rho
    1094           0 :     exner_col(1,:) = exner
    1095           0 :     rho_ds_zm_col(1,:) = rho_ds_zm
    1096           0 :     rho_ds_zt_col(1,:) = rho_ds_zt
    1097           0 :     invrs_rho_ds_zm_col(1,:) = invrs_rho_ds_zm
    1098           0 :     invrs_rho_ds_zt_col(1,:) = invrs_rho_ds_zt
    1099           0 :     thv_ds_zm_col(1,:) = thv_ds_zm
    1100           0 :     thv_ds_zt_col(1,:) = thv_ds_zt
    1101           0 :     rfrzm_col(1,:) = rfrzm
    1102             :     
    1103           0 :     hydromet_col(1,:,:) = hydromet
    1104             :     
    1105           0 :     radf_col(1,:) = radf
    1106             : #ifdef CLUBBND_CAM
    1107             :     varmu_col(1) = varmu
    1108             : #endif
    1109           0 :     wphydrometp_col(1,:,:) = wphydrometp
    1110           0 :     wp2hmp_col(1,:,:) = wp2hmp
    1111           0 :     rtphmp_col(1,:,:) = rtphmp
    1112           0 :     thlphmp_col(1,:,:) = thlphmp
    1113             :     
    1114           0 :     host_dx_col(1) = host_dx
    1115           0 :     host_dy_col(1) = host_dy
    1116             :     
    1117           0 :     stats_zt_col(1) = stats_zt
    1118           0 :     stats_zm_col(1) = stats_zm
    1119           0 :     stats_sfc_col(1) = stats_sfc
    1120             :     
    1121           0 :     um_col(1,:) = um
    1122           0 :     vm_col(1,:) = vm
    1123           0 :     upwp_col(1,:) = upwp
    1124           0 :     vpwp_col(1,:) = vpwp
    1125           0 :     up2_col(1,:) = up2
    1126           0 :     vp2_col(1,:) = vp2
    1127           0 :     up3_col(1,:) = up3
    1128           0 :     vp3_col(1,:) = vp3
    1129           0 :     thlm_col(1,:) = thlm
    1130           0 :     rtm_col(1,:) = rtm
    1131           0 :     wprtp_col(1,:) = wprtp
    1132           0 :     wpthlp_col(1,:) = wpthlp
    1133           0 :     wp2_col(1,:) = wp2
    1134           0 :     wp3_col(1,:) = wp3
    1135           0 :     rtp2_col(1,:) = rtp2
    1136           0 :     rtp3_col(1,:) = rtp3
    1137           0 :     thlp2_col(1,:) = thlp2
    1138           0 :     thlp3_col(1,:) = thlp3
    1139           0 :     rtpthlp_col(1,:) = rtpthlp
    1140             :     
    1141           0 :     sclrm_col(1,:,:) = sclrm
    1142             : #ifdef GFDL
    1143             :     sclrm_trsport_only_col(1,:,:) = sclrm_trsport_only
    1144             : #endif
    1145           0 :     sclrp2_col(1,:,:) = sclrp2
    1146           0 :     sclrp3_col(1,:,:) = sclrp3
    1147           0 :     sclrprtp_col(1,:,:) = sclrprtp
    1148           0 :     sclrpthlp_col(1,:,:) = sclrpthlp
    1149           0 :     wpsclrp_col(1,:,:) = wpsclrp
    1150           0 :     edsclrm_col(1,:,:) = edsclrm
    1151             :     
    1152           0 :     rcm_col(1,:) = rcm
    1153           0 :     cloud_frac_col(1,:) = cloud_frac
    1154           0 :     wpthvp_col(1,:) = wpthvp
    1155           0 :     wp2thvp_col(1,:) = wp2thvp
    1156           0 :     rtpthvp_col(1,:) = rtpthvp
    1157           0 :     thlpthvp_col(1,:) = thlpthvp
    1158           0 :     sclrpthvp_col(1,:,:) = sclrpthvp
    1159           0 :     wp2rtp_col(1,:) = wp2rtp
    1160           0 :     wp2thlp_col(1,:) = wp2thlp
    1161           0 :     uprcp_col(1,:) = uprcp
    1162           0 :     vprcp_col(1,:) = vprcp
    1163           0 :     rc_coef_col(1,:) = rc_coef
    1164           0 :     wp4_col(1,:) = wp4
    1165           0 :     wpup2_col(1,:) = wpup2
    1166           0 :     wpvp2_col(1,:) = wpvp2
    1167           0 :     wp2up2_col(1,:) = wp2up2
    1168           0 :     wp2vp2_col(1,:) = wp2vp2
    1169           0 :     ice_supersat_frac_col(1,:) = ice_supersat_frac
    1170           0 :     um_pert_col(1,:) = um_pert
    1171           0 :     vm_pert_col(1,:) = vm_pert
    1172           0 :     upwp_pert_col(1,:) = upwp_pert
    1173           0 :     vpwp_pert_col(1,:) = vpwp_pert
    1174             : #ifdef GFDL
    1175             :     RH_crit_col(1,:,:,:) = RH_crit
    1176             : #endif
    1177           0 :     Kh_zm_col(1,:) = Kh_zm
    1178           0 :     Kh_zt_col(1,:) = Kh_zt
    1179             : #ifdef CLUBB_CAM
    1180           0 :     qclvar_col(1,:) = qclvar
    1181             : #endif
    1182           0 :     thlprcp_col(1,:) = thlprcp
    1183           0 :     wprcp_col(1,:) = wprcp
    1184           0 :     w_up_in_cloud_col(1,:) = w_up_in_cloud
    1185           0 :     w_down_in_cloud_col(1,:) = w_down_in_cloud
    1186           0 :     cloudy_updraft_frac_col(1,:) = cloudy_updraft_frac
    1187           0 :     cloudy_downdraft_frac_col(1,:) = cloudy_downdraft_frac
    1188           0 :     rcm_in_layer_col(1,:) = rcm_in_layer
    1189           0 :     cloud_cover_col(1,:) = cloud_cover
    1190           0 :     invrs_tau_zm_col(1,:) = invrs_tau_zm
    1191             : 
    1192             :     call advance_clubb_core( gr, gr%nz, 1,              &
    1193             :       l_implemented, dt, fcor_col, sfc_elevation_col, hydromet_dim, &         ! intent(in)
    1194             :       thlm_forcing_col, rtm_forcing_col, um_forcing_col, vm_forcing_col, &    ! intent(in)
    1195             :       sclrm_forcing_col, edsclrm_forcing_col, wprtp_forcing_col, &            ! intent(in)
    1196             :       wpthlp_forcing_col, rtp2_forcing_col, thlp2_forcing_col, &              ! intent(in)
    1197             :       rtpthlp_forcing_col, wm_zm_col, wm_zt_col, &                            ! intent(in)
    1198             :       wpthlp_sfc_col, wprtp_sfc_col, upwp_sfc_col, vpwp_sfc_col, &            ! intent(in)
    1199             :       wpsclrp_sfc_col, wpedsclrp_sfc_col, &                                   ! intent(in)
    1200             :       upwp_sfc_pert_col, vpwp_sfc_pert_col, &                                 ! intent(in)
    1201             :       rtm_ref_col, thlm_ref_col, um_ref_col, vm_ref_col, ug_col, vg_col, &    ! Intent(in)
    1202             :       p_in_Pa_col, rho_zm_col, rho_col, exner_col, &                          ! intent(in)
    1203             :       rho_ds_zm_col, rho_ds_zt_col, invrs_rho_ds_zm_col, &                    ! intent(in)
    1204             :       invrs_rho_ds_zt_col, thv_ds_zm_col, thv_ds_zt_col, hydromet_col, &      ! intent(in)
    1205             :       rfrzm_col, radf_col, &                                                  ! intent(in)
    1206             : #ifdef CLUBBND_CAM
    1207             :       varmu_col, &
    1208             : #endif
    1209             :       wphydrometp_col, wp2hmp_col, rtphmp_col, thlphmp_col, &                 ! intent(in)
    1210             :       host_dx_col, host_dy_col, &                                             ! intent(in)
    1211             :       clubb_params, nu_vert_res_dep, lmin, &                                  ! intent(in)
    1212             :       clubb_config_flags, &                                                   ! intent(in)
    1213             :       stats_metadata, &           ! intent(in)
    1214             :       stats_zt_col, stats_zm_col, stats_sfc_col, &                                ! intent(inout)
    1215             :       um_col, vm_col, upwp_col, vpwp_col, up2_col, vp2_col, up3_col, vp3_col, &   ! intent(inout)
    1216             :       thlm_col, rtm_col, wprtp_col, wpthlp_col, &                                 ! intent(inout)
    1217             :       wp2_col, wp3_col, rtp2_col, rtp3_col, thlp2_col, thlp3_col, rtpthlp_col, &  ! intent(inout)
    1218             :       sclrm_col,   &
    1219             : #ifdef GFDL
    1220             :       sclrm_trsport_only_col,  &  ! h1g, 2010-06-16                               ! intent(inout)
    1221             : #endif
    1222             :       sclrp2_col, sclrp3_col, sclrprtp_col, sclrpthlp_col, &                      ! intent(inout)
    1223             :       wpsclrp_col, edsclrm_col, &                                                 ! intent(inout)
    1224             :       rcm_col, cloud_frac_col, &                                                  ! intent(inout)
    1225             :       wpthvp_col, wp2thvp_col, rtpthvp_col, thlpthvp_col, &                       ! intent(inout)
    1226             :       sclrpthvp_col, &                                                            ! intent(inout)
    1227             :       wp2rtp_col, wp2thlp_col, uprcp_col, vprcp_col, rc_coef_col, wp4_col, &      ! intent(inout)
    1228             :       wpup2_col, wpvp2_col, wp2up2_col, wp2vp2_col, ice_supersat_frac_col, &      ! intent(inout)
    1229             :       um_pert_col, vm_pert_col, upwp_pert_col, vpwp_pert_col, &                   ! intent(inout)
    1230             :       pdf_params, pdf_params_zm, &                                                ! intent(inout)
    1231             :       pdf_implicit_coefs_terms, &                                                 ! intent(inout)
    1232             : #ifdef GFDL
    1233             :                RH_crit_col, & !h1g, 2010-06-16                                    ! intent(inout)
    1234             :                do_liquid_only_in_clubb, &                                         ! intent(in)
    1235             : #endif
    1236             :       Kh_zm_col, Kh_zt_col, &                                                     ! intent(out)
    1237             : #ifdef CLUBB_CAM
    1238             :                qclvar_col, &                                                      ! intent(out)
    1239             : #endif
    1240             :       thlprcp_col, wprcp_col, w_up_in_cloud_col, w_down_in_cloud_col, & ! intent(out)
    1241             :       cloudy_updraft_frac_col, cloudy_downdraft_frac_col, &                       ! intent(out)
    1242             :       rcm_in_layer_col, cloud_cover_col, invrs_tau_zm_col, &                      ! intent(out)
    1243           0 :       err_code_api )                                                              ! intent(out)
    1244             :     
    1245             :     
    1246             :     ! The following does not work for stats 
    1247             :     !     stats_zt = stats_zt_col(1)
    1248             :     !     stats_zm = stats_zm_col(1) 
    1249             :     !     stats_sfc = stats_sfc_col(1)
    1250             :     ! because of some mysterious pointer issue. However, the only thing that 
    1251             :     ! updates in stats is the field values, so we can copy only those instead.
    1252           0 :     if ( stats_metadata%l_stats ) then 
    1253           0 :       stats_zm%accum_field_values = stats_zm_col(1)%accum_field_values
    1254           0 :       stats_zm%accum_num_samples = stats_zm_col(1)%accum_num_samples
    1255             :       
    1256           0 :       stats_zt%accum_field_values = stats_zt_col(1)%accum_field_values
    1257           0 :       stats_zt%accum_num_samples = stats_zt_col(1)%accum_num_samples
    1258             :       
    1259           0 :       stats_sfc%accum_field_values = stats_sfc_col(1)%accum_field_values
    1260           0 :       stats_sfc%accum_num_samples = stats_sfc_col(1)%accum_num_samples
    1261             :     end if
    1262             :       
    1263             :       
    1264           0 :     um = um_col(1,:)
    1265           0 :     upwp = upwp_col(1,:)
    1266           0 :     vm = vm_col(1,:)
    1267           0 :     vpwp = vpwp_col(1,:)
    1268           0 :     up2 = up2_col(1,:)
    1269           0 :     vp2 = vp2_col(1,:)
    1270           0 :     up3 = up3_col(1,:)
    1271           0 :     vp3 = vp3_col(1,:)
    1272           0 :     rtm = rtm_col(1,:)
    1273           0 :     wprtp = wprtp_col(1,:)
    1274           0 :     thlm = thlm_col(1,:)
    1275           0 :     wpthlp = wpthlp_col(1,:)
    1276           0 :     rtp2 = rtp2_col(1,:)
    1277           0 :     rtp3 = rtp3_col(1,:)
    1278           0 :     thlp2 = thlp2_col(1,:)
    1279           0 :     thlp3 = thlp3_col(1,:)
    1280           0 :     rtpthlp = rtpthlp_col(1,:)
    1281           0 :     wp2 = wp2_col(1,:)
    1282           0 :     wp3 = wp3_col(1,:)
    1283           0 :     sclrm = sclrm_col(1,:,:)
    1284           0 :     wpsclrp = wpsclrp_col(1,:,:)
    1285           0 :     sclrp2 = sclrp2_col(1,:,:)
    1286           0 :     sclrp3 = sclrp3_col(1,:,:)
    1287           0 :     sclrprtp = sclrprtp_col(1,:,:)
    1288           0 :     sclrpthlp = sclrpthlp_col(1,:,:)
    1289           0 :     p_in_Pa = p_in_Pa_col(1,:)
    1290           0 :     exner = exner_col(1,:)
    1291           0 :     rcm = rcm_col(1,:)
    1292           0 :     cloud_frac = cloud_frac_col(1,:)
    1293           0 :     wpthvp = wpthvp_col(1,:)
    1294           0 :     wp2thvp = wp2thvp_col(1,:)
    1295           0 :     rtpthvp = rtpthvp_col(1,:)
    1296           0 :     thlpthvp = thlpthvp_col(1,:)
    1297           0 :     sclrpthvp = sclrpthvp_col(1,:,:)
    1298           0 :     wp2rtp = wp2rtp_col(1,:)
    1299           0 :     wp2thlp = wp2thlp_col(1,:)
    1300           0 :     uprcp = uprcp_col(1,:)
    1301           0 :     vprcp = vprcp_col(1,:)
    1302           0 :     rc_coef = rc_coef_col(1,:)
    1303           0 :     wp4 = wp4_col(1,:)
    1304           0 :     wpup2 = wpup2_col(1,:)
    1305           0 :     wpvp2 = wpvp2_col(1,:)
    1306           0 :     wp2vp2 = wp2vp2_col(1,:)
    1307           0 :     wp2up2 = wp2up2_col(1,:)
    1308           0 :     ice_supersat_frac = ice_supersat_frac_col(1,:)
    1309             : #ifdef GFDL
    1310             :     sclrm_trsport_only = sclrm_trsport_only_col(1,:,:)
    1311             : #endif
    1312           0 :     edsclrm = edsclrm_col(1,:,:)
    1313           0 :     rcm_in_layer = rcm_in_layer_col(1,:)
    1314           0 :     cloud_cover  = cloud_cover_col(1,:)
    1315           0 :     wprcp = wprcp_col(1,:)
    1316           0 :     w_up_in_cloud = w_up_in_cloud_col(1,:)
    1317           0 :     w_down_in_cloud = w_down_in_cloud_col(1,:)
    1318           0 :     cloudy_updraft_frac = cloudy_updraft_frac_col(1,:)
    1319           0 :     cloudy_downdraft_frac = cloudy_downdraft_frac_col(1,:)
    1320           0 :     invrs_tau_zm = invrs_tau_zm_col(1,:)
    1321           0 :     Kh_zt = Kh_zt_col(1,:)
    1322           0 :     Kh_zm = Kh_zm_col(1,:)
    1323             : #ifdef CLUBB_CAM
    1324           0 :     qclvar = qclvar_col(1,:)
    1325             : #endif
    1326           0 :     thlprcp = thlprcp_col(1,:)
    1327             : #ifdef GFDL
    1328             :     RH_crit = RH_crit_col(1,:,:,:)
    1329             : #endif
    1330             : 
    1331           0 :   end subroutine advance_clubb_core_api_single_col
    1332             :   
    1333      352944 :   subroutine advance_clubb_core_api_multi_col( gr, nz, ngrdcol, &
    1334      352944 :     l_implemented, dt, fcor, sfc_elevation, hydromet_dim, & ! intent(in)
    1335      352944 :     thlm_forcing, rtm_forcing, um_forcing, vm_forcing, &    ! intent(in)
    1336      352944 :     sclrm_forcing, edsclrm_forcing, wprtp_forcing, &        ! intent(in)
    1337      352944 :     wpthlp_forcing, rtp2_forcing, thlp2_forcing, &          ! intent(in)
    1338      352944 :     rtpthlp_forcing, wm_zm, wm_zt, &                        ! intent(in)
    1339      352944 :     wpthlp_sfc, wprtp_sfc, upwp_sfc, vpwp_sfc, &            ! intent(in)
    1340      352944 :     wpsclrp_sfc, wpedsclrp_sfc, &                           ! intent(in)
    1341      352944 :     upwp_sfc_pert, vpwp_sfc_pert, &                         ! intent(in)
    1342      352944 :     rtm_ref, thlm_ref, um_ref, vm_ref, ug, vg, &            ! Intent(in)
    1343      352944 :     p_in_Pa, rho_zm, rho, exner, &                          ! intent(in)
    1344      352944 :     rho_ds_zm, rho_ds_zt, invrs_rho_ds_zm, &                ! intent(in)
    1345      352944 :     invrs_rho_ds_zt, thv_ds_zm, thv_ds_zt, hydromet, &      ! intent(in)
    1346      352944 :     rfrzm, radf, &                                          ! intent(in)
    1347             : #ifdef CLUBBND_CAM
    1348             :     varmu, &                                                ! intent(in)
    1349             : #endif
    1350      352944 :     wphydrometp, wp2hmp, rtphmp, thlphmp, &                 ! intent(in)
    1351      352944 :     host_dx, host_dy, &                                     ! intent(in)
    1352             :     clubb_params, nu_vert_res_dep, lmin, &                  ! intent(in)
    1353             :     clubb_config_flags, &                                   ! intent(in)
    1354             :     stats_metadata, &                                       ! intent(in)
    1355      352944 :     stats_zt, stats_zm, stats_sfc, &                        ! intent(inout)
    1356      352944 :     um, vm, upwp, vpwp, up2, vp2, up3, vp3, &               ! intent(inout)
    1357      352944 :     thlm, rtm, wprtp, wpthlp, &                             ! intent(inout)
    1358      352944 :     wp2, wp3, rtp2, rtp3, thlp2, thlp3, rtpthlp, &          ! intent(inout)
    1359      352944 :     sclrm,   &
    1360             : #ifdef GFDL
    1361             :                sclrm_trsport_only,  &  ! h1g, 2010-06-16    ! intent(inout)
    1362             : #endif
    1363      352944 :     sclrp2, sclrp3, sclrprtp, sclrpthlp, &                  ! intent(inout)
    1364      352944 :     wpsclrp, edsclrm, err_code_api, &                       ! intent(inout)
    1365      352944 :     rcm, cloud_frac, &                                      ! intent(inout)
    1366      352944 :     wpthvp, wp2thvp, rtpthvp, thlpthvp, &                   ! intent(inout)
    1367      352944 :     sclrpthvp, &                                            ! intent(inout)
    1368      352944 :     wp2rtp, wp2thlp, uprcp, vprcp, rc_coef, wp4, &          ! intent(inout)
    1369      352944 :     wpup2, wpvp2, wp2up2, wp2vp2, ice_supersat_frac, &      ! intent(inout)
    1370      352944 :     um_pert, vm_pert, upwp_pert, vpwp_pert, &               ! intent(inout)
    1371             :     pdf_params, pdf_params_zm, &                            ! intent(inout)
    1372             :     pdf_implicit_coefs_terms, &                             ! intent(inout)
    1373             : #ifdef GFDL
    1374             :                RH_crit, & !h1g, 2010-06-16                  ! intent(inout)
    1375             :                do_liquid_only_in_clubb, &                   ! intent(in)
    1376             : #endif
    1377      352944 :     Kh_zm, Kh_zt, &                                         ! intent(out)
    1378             : #ifdef CLUBB_CAM
    1379      352944 :     qclvar, &                                               ! intent(out)
    1380             : #endif
    1381      352944 :     thlprcp, wprcp, w_up_in_cloud, w_down_in_cloud, &       ! intent(out)
    1382      352944 :     cloudy_updraft_frac, cloudy_downdraft_frac, &           ! intent(out)
    1383      352944 :     rcm_in_layer, cloud_cover, invrs_tau_zm )               ! intent(out)
    1384             : 
    1385             :     use advance_clubb_core_module, only : advance_clubb_core
    1386             : 
    1387             :     use pdf_parameter_module, only: &
    1388             :         implicit_coefs_terms    ! Variable Type(s)
    1389             : 
    1390             :     use parameters_model, only: &
    1391             :         sclr_dim, & ! Variable(s)
    1392             :         edsclr_dim
    1393             : 
    1394             :     use model_flags, only: &
    1395             :         clubb_config_flags_type
    1396             : 
    1397             :     implicit none
    1398             : 
    1399             :     !------------------------ Input Variables ------------------------
    1400             :     integer, intent(in) :: &
    1401             :       nz, &
    1402             :       ngrdcol   ! Number of grid columns
    1403             : 
    1404             :     type(grid), target, intent(in) :: &
    1405             :       gr
    1406             : 
    1407             :     logical, intent(in) ::  &
    1408             :       l_implemented ! Is this part of a larger host model (T/F) ?
    1409             : 
    1410             :     real( kind = core_rknd ), intent(in) ::  &
    1411             :       dt  ! Current timestep duration    [s]
    1412             :       
    1413             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol) ::  &
    1414             :       fcor, &           ! Coriolis forcing             [s^-1]
    1415             :       sfc_elevation     ! Elevation of ground level    [m AMSL]
    1416             : 
    1417             :     integer, intent(in) :: &
    1418             :       hydromet_dim      ! Total number of hydrometeors          [#]
    1419             : 
    1420             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,nz) ::  &
    1421             :       thlm_forcing,    & ! theta_l forcing (thermodynamic levels)    [K/s]
    1422             :       rtm_forcing,     & ! r_t forcing (thermodynamic levels)        [(kg/kg)/s]
    1423             :       um_forcing,      & ! u wind forcing (thermodynamic levels)     [m/s/s]
    1424             :       vm_forcing,      & ! v wind forcing (thermodynamic levels)     [m/s/s]
    1425             :       wprtp_forcing,   & ! <w'r_t'> forcing (momentum levels)    [m*K/s^2]
    1426             :       wpthlp_forcing,  & ! <w'th_l'> forcing (momentum levels)   [m*(kg/kg)/s^2]
    1427             :       rtp2_forcing,    & ! <r_t'^2> forcing (momentum levels)    [(kg/kg)^2/s]
    1428             :       thlp2_forcing,   & ! <th_l'^2> forcing (momentum levels)   [K^2/s]
    1429             :       rtpthlp_forcing, & ! <r_t'th_l'> forcing (momentum levels) [K*(kg/kg)/s]
    1430             :       wm_zm,           & ! w mean wind component on momentum levels  [m/s]
    1431             :       wm_zt,           & ! w mean wind component on thermo. levels   [m/s]
    1432             :       rho_zm,          & ! Air density on momentum levels            [kg/m^3]
    1433             :       rho,             & ! Air density on thermodynamic levels       [kg/m^3]
    1434             :       rho_ds_zm,       & ! Dry, static density on momentum levels    [kg/m^3]
    1435             :       rho_ds_zt,       & ! Dry, static density on thermo. levels     [kg/m^3]
    1436             :       invrs_rho_ds_zm, & ! Inv. dry, static density @ momentum levs. [m^3/kg]
    1437             :       invrs_rho_ds_zt, & ! Inv. dry, static density @ thermo. levs.  [m^3/kg]
    1438             :       thv_ds_zm,       & ! Dry, base-state theta_v on momentum levs. [K]
    1439             :       thv_ds_zt,       & ! Dry, base-state theta_v on thermo. levs.  [K]
    1440             :       rfrzm              ! Total ice-phase water mixing ratio        [kg/kg]
    1441             : 
    1442             :     real( kind = core_rknd ), dimension(ngrdcol,nz,hydromet_dim), intent(in) :: &
    1443             :       hydromet           ! Collection of hydrometeors                [units vary]
    1444             : 
    1445             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(in) :: &
    1446             :       radf          ! Buoyancy production at the CL top due to LW radiative cooling [m^2/s^3]
    1447             : 
    1448             : #ifdef CLUBBND_CAM 
    1449             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol) :: & 
    1450             :       varmu 
    1451             : #endif 
    1452             : 
    1453             :     real( kind = core_rknd ), dimension(ngrdcol,nz, hydromet_dim), intent(in) :: &
    1454             :       wphydrometp, & ! Covariance of w and a hydrometeor   [(m/s) <hm units>]
    1455             :       wp2hmp,      & ! Third moment: <w'^2> * <hydro.'>    [(m/s)^2 <hm units>]
    1456             :       rtphmp,      & ! Covariance of rt and a hydrometeor  [(kg/kg) <hm units>]
    1457             :       thlphmp        ! Covariance of thl and a hydrometeor [K <hm units>]
    1458             : 
    1459             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol) ::  &
    1460             :       wpthlp_sfc,   & ! w' theta_l' at surface   [(m K)/s]
    1461             :       wprtp_sfc,    & ! w' r_t' at surface       [(kg m)/( kg s)]
    1462             :       upwp_sfc,     & ! u'w' at surface          [m^2/s^2]
    1463             :       vpwp_sfc        ! v'w' at surface          [m^2/s^2]
    1464             : 
    1465             :     ! Passive scalar variables
    1466             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,nz,sclr_dim) :: &
    1467             :       sclrm_forcing    ! Passive scalar forcing         [{units vary}/s]
    1468             : 
    1469             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,sclr_dim) ::  &
    1470             :       wpsclrp_sfc      ! Scalar flux at surface         [{units vary} m/s]
    1471             : 
    1472             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol) :: &
    1473             :       upwp_sfc_pert, & ! pertubed u'w' at surface    [m^2/s^2]
    1474             :       vpwp_sfc_pert    ! pertubed v'w' at surface    [m^2/s^2]
    1475             : 
    1476             :     ! Eddy passive scalar variables
    1477             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,nz,edsclr_dim) :: &
    1478             :       edsclrm_forcing  ! Eddy passive scalar forcing    [{units vary}/s]
    1479             : 
    1480             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,edsclr_dim) ::  &
    1481             :       wpedsclrp_sfc    ! Eddy-Scalar flux at surface    [{units vary} m/s]
    1482             : 
    1483             :     ! Reference profiles (used for nudging, sponge damping, and Coriolis effect)
    1484             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(in) ::  &
    1485             :       rtm_ref,  & ! Initial total water mixing ratio             [kg/kg]
    1486             :       thlm_ref, & ! Initial liquid water potential temperature   [K]
    1487             :       um_ref,   & ! Initial u wind; Michael Falk                 [m/s]
    1488             :       vm_ref,   & ! Initial v wind; Michael Falk                 [m/s]
    1489             :       ug,       & ! u geostrophic wind                           [m/s]
    1490             :       vg          ! v geostrophic wind                           [m/s]
    1491             : 
    1492             :     ! Host model horizontal grid spacing, if part of host model.
    1493             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol) :: &
    1494             :       host_dx,  & ! East-West horizontal grid spacing     [m]
    1495             :       host_dy     ! North-South horizontal grid spacing   [m]
    1496             : 
    1497             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
    1498             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
    1499             : 
    1500             :     type(nu_vertical_res_dep), intent(in) :: &
    1501             :       nu_vert_res_dep    ! Vertical resolution dependent nu values
    1502             : 
    1503             :     real( kind = core_rknd ), intent(in) :: &
    1504             :       lmin    ! Min. value for the length scale    [m]
    1505             : 
    1506             :     type( clubb_config_flags_type ), intent(in) :: &
    1507             :       clubb_config_flags ! Derived type holding all configurable CLUBB flags
    1508             : 
    1509             :     type (stats_metadata_type), intent(in) :: &
    1510             :       stats_metadata
    1511             : 
    1512             :     !------------------------ Input/Output Variables ------------------------
    1513             :     type(stats), target, intent(inout), dimension(ngrdcol) :: &
    1514             :       stats_zt, &
    1515             :       stats_zm, &
    1516             :       stats_sfc
    1517             : 
    1518             :     ! These are prognostic or are planned to be in the future
    1519             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz) ::  &
    1520             :       um,      & ! u mean wind component (thermodynamic levels)   [m/s]
    1521             :       upwp,    & ! u'w' (momentum levels)                         [m^2/s^2]
    1522             :       vm,      & ! v mean wind component (thermodynamic levels)   [m/s]
    1523             :       vpwp,    & ! v'w' (momentum levels)                         [m^2/s^2]
    1524             :       up2,     & ! u'^2 (momentum levels)                         [m^2/s^2]
    1525             :       vp2,     & ! v'^2 (momentum levels)                         [m^2/s^2]
    1526             :       up3,     & ! u'^3 (thermodynamic levels)                    [m^3/s^3]
    1527             :       vp3,     & ! v'^3 (thermodynamic levels)                    [m^3/s^3]
    1528             :       rtm,     & ! total water mixing ratio, r_t (thermo. levels) [kg/kg]
    1529             :       wprtp,   & ! w' r_t' (momentum levels)                      [(kg/kg) m/s]
    1530             :       thlm,    & ! liq. water pot. temp., th_l (thermo. levels)   [K]
    1531             :       wpthlp,  & ! w' th_l' (momentum levels)                     [(m/s) K]
    1532             :       rtp2,    & ! r_t'^2 (momentum levels)                       [(kg/kg)^2]
    1533             :       rtp3,    & ! r_t'^3 (thermodynamic levels)                  [(kg/kg)^3]
    1534             :       thlp2,   & ! th_l'^2 (momentum levels)                      [K^2]
    1535             :       thlp3,   & ! th_l'^3 (thermodynamic levels)                 [K^3]
    1536             :       rtpthlp, & ! r_t' th_l' (momentum levels)                   [(kg/kg) K]
    1537             :       wp2,     & ! w'^2 (momentum levels)                         [m^2/s^2]
    1538             :       wp3        ! w'^3 (thermodynamic levels)                    [m^3/s^3]
    1539             : 
    1540             :     ! Passive scalar variables
    1541             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz,sclr_dim) :: &
    1542             :       sclrm,     & ! Passive scalar mean (thermo. levels) [units vary]
    1543             :       wpsclrp,   & ! w'sclr' (momentum levels)            [{units vary} m/s]
    1544             :       sclrp2,    & ! sclr'^2 (momentum levels)            [{units vary}^2]
    1545             :       sclrp3,    & ! sclr'^3 (thermodynamic levels)       [{units vary}^3]
    1546             :       sclrprtp,  & ! sclr'rt' (momentum levels)           [{units vary} (kg/kg)]
    1547             :       sclrpthlp    ! sclr'thl' (momentum levels)          [{units vary} K]
    1548             : 
    1549             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz) ::  &
    1550             :       p_in_Pa, & ! Air pressure (thermodynamic levels)       [Pa]
    1551             :       exner      ! Exner function (thermodynamic levels)     [-]
    1552             : 
    1553             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz) ::  &
    1554             :       rcm,        & ! cloud water mixing ratio, r_c (thermo. levels) [kg/kg]
    1555             :       cloud_frac, & ! cloud fraction (thermodynamic levels)          [-]
    1556             :       wpthvp,     & ! < w' th_v' > (momentum levels)                 [kg/kg K]
    1557             :       wp2thvp,    & ! < w'^2 th_v' > (thermodynamic levels)          [m^2/s^2 K]
    1558             :       rtpthvp,    & ! < r_t' th_v' > (momentum levels)               [kg/kg K]
    1559             :       thlpthvp      ! < th_l' th_v' > (momentum levels)              [K^2]
    1560             : 
    1561             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz,sclr_dim) :: &
    1562             :       sclrpthvp     ! < sclr' th_v' > (momentum levels)   [units vary]
    1563             : 
    1564             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz) ::  &
    1565             :       wp2rtp,            & ! w'^2 rt' (thermodynamic levels)      [m^2/s^2 kg/kg]
    1566             :       wp2thlp,           & ! w'^2 thl' (thermodynamic levels)     [m^2/s^2 K]
    1567             :       uprcp,             & ! < u' r_c' > (momentum levels)        [(m/s)(kg/kg)]
    1568             :       vprcp,             & ! < v' r_c' > (momentum levels)        [(m/s)(kg/kg)]
    1569             :       rc_coef,           & ! Coef of X'r_c' in Eq. (34) (t-levs.) [K/(kg/kg)]
    1570             :       wp4,               & ! w'^4 (momentum levels)               [m^4/s^4]
    1571             :       wpup2,             & ! w'u'^2 (thermodynamic levels)        [m^3/s^3]
    1572             :       wpvp2,             & ! w'v'^2 (thermodynamic levels)        [m^3/s^3]
    1573             :       wp2up2,            & ! w'^2 u'^2 (momentum levels)          [m^4/s^4]
    1574             :       wp2vp2,            & ! w'^2 v'^2 (momentum levels)          [m^4/s^4]
    1575             :       ice_supersat_frac    ! ice cloud fraction (thermo. levels)  [-]
    1576             : 
    1577             :     ! Variables used to track perturbed version of winds.
    1578             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz) :: &
    1579             :       um_pert,   & ! perturbed <u>       [m/s]
    1580             :       vm_pert,   & ! perturbed <v>       [m/s]
    1581             :       upwp_pert, & ! perturbed <u'w'>    [m^2/s^2]
    1582             :       vpwp_pert    ! perturbed <v'w'>    [m^2/s^2]
    1583             : 
    1584             :     type(pdf_parameter), intent(inout) :: &
    1585             :       pdf_params,    & ! PDF parameters (thermodynamic levels)    [units vary]
    1586             :       pdf_params_zm    ! PDF parameters on momentum levels        [units vary]
    1587             : 
    1588             :     type(implicit_coefs_terms), intent(inout) :: &
    1589             :       pdf_implicit_coefs_terms    ! Implicit coefs / explicit terms [units vary]
    1590             : 
    1591             : #ifdef GFDL
    1592             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz,sclr_dim) :: &  ! h1g, 2010-06-16
    1593             :       sclrm_trsport_only  ! Passive scalar concentration due to pure transport [{units vary}/s]
    1594             : #endif
    1595             : 
    1596             :     real( kind = core_rknd ), intent(inout), dimension(ngrdcol,nz,edsclr_dim) :: &
    1597             :     edsclrm   ! Eddy passive scalar mean (thermo. levels)   [units vary]
    1598             : 
    1599             : 
    1600             :     !------------------------ Output Variables ------------------------
    1601             :     real( kind = core_rknd ), intent(out), dimension(ngrdcol,nz) ::  &
    1602             :       rcm_in_layer, & ! rcm in cloud layer                              [kg/kg]
    1603             :       cloud_cover     ! cloud cover                                     [-]
    1604             : 
    1605             :     ! Variables that need to be output for use in host models
    1606             :     real( kind = core_rknd ), intent(out), dimension(ngrdcol,nz) ::  &
    1607             :       wprcp,                 & ! w'r_c' (momentum levels)              [(kg/kg) m/s]
    1608             :       w_up_in_cloud,         & ! Average cloudy updraft velocity       [m/s]
    1609             :       w_down_in_cloud,       & ! Average cloudy downdraft velocity     [m/s]
    1610             :       cloudy_updraft_frac,   & ! cloudy updraft fraction               [-]
    1611             :       cloudy_downdraft_frac, & ! cloudy downdraft fraction             [-]
    1612             :       invrs_tau_zm             ! One divided by tau on zm levels       [1/s]
    1613             : 
    1614             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(out) :: &
    1615             :       Kh_zt, & ! Eddy diffusivity coefficient on thermodynamic levels   [m^2/s]
    1616             :       Kh_zm    ! Eddy diffusivity coefficient on momentum levels        [m^2/s]
    1617             : 
    1618             : #ifdef CLUBB_CAM
    1619             :     real( kind = core_rknd), intent(out), dimension(ngrdcol,nz) :: &
    1620             :       qclvar        ! cloud water variance
    1621             : #endif
    1622             : 
    1623             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(out) :: &
    1624             :       thlprcp    ! thl'rc'              [K kg/kg]
    1625             : 
    1626             :     integer, intent(inout) :: err_code_api ! Diagnostic, for if some calculation goes amiss.
    1627             : 
    1628             : #ifdef GFDL
    1629             :     ! hlg, 2010-06-16
    1630             :     real( kind = core_rknd ), intent(inOUT), dimension(ngrdcol,nz, min(1,sclr_dim) , 2) :: &
    1631             :       RH_crit  ! critical relative humidity for droplet and ice nucleation
    1632             :     logical, intent(in)                 ::  do_liquid_only_in_clubb
    1633             : #endif
    1634             : 
    1635             :     call advance_clubb_core( gr, nz, ngrdcol, &
    1636             :       l_implemented, dt, fcor, sfc_elevation, hydromet_dim, & ! intent(in)
    1637             :       thlm_forcing, rtm_forcing, um_forcing, vm_forcing, &    ! intent(in)
    1638             :       sclrm_forcing, edsclrm_forcing, wprtp_forcing, &        ! intent(in)
    1639             :       wpthlp_forcing, rtp2_forcing, thlp2_forcing, &          ! intent(in)
    1640             :       rtpthlp_forcing, wm_zm, wm_zt, &                        ! intent(in)
    1641             :       wpthlp_sfc, wprtp_sfc, upwp_sfc, vpwp_sfc, &            ! intent(in)
    1642             :       wpsclrp_sfc, wpedsclrp_sfc, &                           ! intent(in)
    1643             :       upwp_sfc_pert, vpwp_sfc_pert, &                         ! intent(in)
    1644             :       rtm_ref, thlm_ref, um_ref, vm_ref, ug, vg, &            ! Intent(in)
    1645             :       p_in_Pa, rho_zm, rho, exner, &                          ! intent(in)
    1646             :       rho_ds_zm, rho_ds_zt, invrs_rho_ds_zm, &                ! intent(in)
    1647             :       invrs_rho_ds_zt, thv_ds_zm, thv_ds_zt, hydromet, &      ! intent(in)
    1648             :       rfrzm, radf, &                                          ! intent(in)
    1649             : #ifdef CLUBBND_CAM
    1650             :       varmu, &
    1651             : #endif
    1652             :       wphydrometp, wp2hmp, rtphmp, thlphmp, &                 ! intent(in)
    1653             :       host_dx, host_dy, &                                     ! intent(in)
    1654             :       clubb_params, nu_vert_res_dep, lmin, &                  ! intent(in)
    1655             :       clubb_config_flags, &                                   ! intent(in)
    1656             :       stats_metadata, &                                       ! intent(in)
    1657             :       stats_zt, stats_zm, stats_sfc, &                        ! intent(inout)
    1658             :       um, vm, upwp, vpwp, up2, vp2, up3, vp3, &               ! intent(inout)
    1659             :       thlm, rtm, wprtp, wpthlp, &                             ! intent(inout)
    1660             :       wp2, wp3, rtp2, rtp3, thlp2, thlp3, rtpthlp, &          ! intent(inout)
    1661             :       sclrm,   &
    1662             : #ifdef GFDL
    1663             :                sclrm_trsport_only,  &  ! h1g, 2010-06-16      ! intent(inout)
    1664             : #endif
    1665             :       sclrp2, sclrp3, sclrprtp, sclrpthlp, &                  ! intent(inout)
    1666             :       wpsclrp, edsclrm, &                                     ! intent(inout)
    1667             :       rcm, cloud_frac, &                                      ! intent(inout)
    1668             :       wpthvp, wp2thvp, rtpthvp, thlpthvp, &                   ! intent(inout)
    1669             :       sclrpthvp, &                                            ! intent(inout)
    1670             :       wp2rtp, wp2thlp, uprcp, vprcp, rc_coef, wp4, &          ! intent(inout)
    1671             :       wpup2, wpvp2, wp2up2, wp2vp2, ice_supersat_frac, &      ! intent(inout)
    1672             :       um_pert, vm_pert, upwp_pert, vpwp_pert, &               ! intent(inout)
    1673             :       pdf_params, pdf_params_zm, &                            ! intent(inout)
    1674             :       pdf_implicit_coefs_terms, &                             ! intent(inout)
    1675             : #ifdef GFDL
    1676             :                RH_crit, & !h1g, 2010-06-16                    ! intent(inout)
    1677             :                do_liquid_only_in_clubb, &                     ! intent(in)
    1678             : #endif
    1679             :       Kh_zm, Kh_zt, &                                         ! intent(out)
    1680             : #ifdef CLUBB_CAM
    1681             :                qclvar, &                                      ! intent(out)
    1682             : #endif
    1683             :       thlprcp, wprcp, w_up_in_cloud, w_down_in_cloud, &       ! intent(out)
    1684             :       cloudy_updraft_frac, cloudy_downdraft_frac, &           ! intent(out)
    1685             :       rcm_in_layer, cloud_cover, invrs_tau_zm, &              ! intent(out)
    1686      352944 :       err_code_api )                                          ! intent(out)
    1687             : 
    1688      352944 :   end subroutine advance_clubb_core_api_multi_col
    1689             : 
    1690             :   !================================================================================================
    1691             :   ! cleanup_clubb_core_api - Frees memory used by the model.
    1692             :   !================================================================================================
    1693             : 
    1694           0 :   subroutine cleanup_clubb_core_api( gr )
    1695             : 
    1696             :     use advance_clubb_core_module, only : cleanup_clubb_core
    1697             : 
    1698             :     
    1699             : 
    1700             :     implicit none
    1701             : 
    1702             :     type(grid), target, intent(inout) :: gr
    1703           0 :     call cleanup_clubb_core( gr ) ! intent(inout)
    1704             : 
    1705           0 :   end subroutine cleanup_clubb_core_api
    1706             : 
    1707             :   !================================================================================================
    1708             :   ! gregorian2julian_day - Computes the number of days since 1 January 4713 BC.
    1709             :   !================================================================================================
    1710             : 
    1711           0 :   integer function gregorian2julian_day_api( &
    1712             :     day, month, year )
    1713             : 
    1714             :     use calendar, only : gregorian2julian_day
    1715             : 
    1716             :     implicit none
    1717             : 
    1718             :     ! Input Variables
    1719             :     integer, intent(in) ::  &
    1720             :       day,        & ! Gregorian Calendar Day for given Month        [dd]
    1721             :       month,      & ! Gregorian Calendar Month for given Year       [mm]
    1722             :       year          ! Gregorian Calendar Year                       [yyyy]
    1723             : 
    1724             :     gregorian2julian_day_api = gregorian2julian_day( &
    1725           0 :       day, month, year )
    1726           0 :   end function gregorian2julian_day_api
    1727             : 
    1728             :   !================================================================================================
    1729             :   ! compute_current_date - Computes the current date and the seconds since that date.
    1730             :   !================================================================================================
    1731             : 
    1732           0 :   subroutine compute_current_date_api( &
    1733             :     previous_day, previous_month, &
    1734             :     previous_year,  &
    1735             :     seconds_since_previous_date, &
    1736             :     current_day, current_month, &
    1737             :     current_year, &
    1738             :     seconds_since_current_date )
    1739             : 
    1740             :     use calendar, only : compute_current_date
    1741             : 
    1742             :     implicit none
    1743             : 
    1744             :     ! Previous date
    1745             :     integer, intent(in) :: &
    1746             :       previous_day,    & ! Day of the month      [dd]
    1747             :       previous_month,  & ! Month of the year     [mm]
    1748             :       previous_year      ! Year                  [yyyy]
    1749             : 
    1750             :     real(kind=time_precision), intent(in) :: &
    1751             :       seconds_since_previous_date ! [s]
    1752             : 
    1753             :     ! Output Variable(s)
    1754             : 
    1755             :     ! Current date
    1756             :     integer, intent(out) :: &
    1757             :       current_day,     & ! Day of the month      [dd]
    1758             :       current_month,   & ! Month of the year     [mm]
    1759             :       current_year       ! Year                  [yyyy]
    1760             : 
    1761             :     real(kind=time_precision), intent(out) :: &
    1762             :       seconds_since_current_date
    1763             : 
    1764             :     call compute_current_date( &
    1765             :       previous_day, previous_month, & ! intent(in)
    1766             :       previous_year,  & ! intent(in)
    1767             :       seconds_since_previous_date, & ! intent(in)
    1768             :       current_day, current_month, & ! intent(out)
    1769             :       current_year, & ! intent(out)
    1770           0 :       seconds_since_current_date ) ! intent(out)
    1771           0 :   end subroutine compute_current_date_api
    1772             : 
    1773             :   !================================================================================================
    1774             :   ! leap_year - Determines if the given year is a leap year.
    1775             :   !================================================================================================
    1776             : 
    1777           0 :   logical function leap_year_api( &
    1778             :     year )
    1779             : 
    1780             :     use calendar, only : leap_year
    1781             : 
    1782             :     implicit none
    1783             : 
    1784             :     ! External
    1785             :     intrinsic :: mod
    1786             : 
    1787             :     ! Input Variable(s)
    1788             :     integer, intent(in) :: year ! Gregorian Calendar Year [yyyy]
    1789             : 
    1790             :     leap_year_api = leap_year( &
    1791           0 :       year )
    1792           0 :   end function leap_year_api
    1793             : 
    1794             :   !================================================================================================
    1795             :   ! setup_corr_varnce_array - Creates a correlation array with x'^2/xm^2 variables on the diagonal
    1796             :   !================================================================================================
    1797             : 
    1798           0 :   subroutine setup_corr_varnce_array_api( &
    1799             :     input_file_cloud, input_file_below, iunit, &
    1800             :     l_fix_w_chi_eta_correlations )
    1801             : 
    1802             :     use corr_varnce_module, only : setup_corr_varnce_array
    1803             : 
    1804             :     implicit none
    1805             : 
    1806             :     ! External
    1807             :     intrinsic :: max, epsilon, trim
    1808             : 
    1809             :     ! Input Variables
    1810             :     integer, intent(in) :: &
    1811             :       iunit ! The file unit
    1812             : 
    1813             :     character(len=*), intent(in) :: &
    1814             :       input_file_cloud, &  ! Path to the in cloud correlation file
    1815             :       input_file_below     ! Path to the out of cloud correlation file
    1816             : 
    1817             :     logical, intent(in) :: &
    1818             :       l_fix_w_chi_eta_correlations ! Use a fixed correlation for s and t Mellor(chi/eta)
    1819             : 
    1820             :     call setup_corr_varnce_array( &
    1821             :       input_file_cloud, input_file_below, iunit, & ! intent(in)
    1822           0 :       l_fix_w_chi_eta_correlations ) ! intent(in)
    1823             : 
    1824           0 :   end subroutine setup_corr_varnce_array_api
    1825             : 
    1826             :   !================================================================================================
    1827             :   ! set_clubb_debug_level - Controls the importance of error messages sent to the console.
    1828             :   !================================================================================================
    1829             : 
    1830        1536 :   subroutine set_clubb_debug_level_api( &
    1831             :     level )
    1832             : 
    1833             :     use error_code, only: &
    1834             :         set_clubb_debug_level ! Procedure
    1835             : 
    1836             :     implicit none
    1837             : 
    1838             :     ! Input variable
    1839             :     integer, intent(in) :: level ! The debug level being checked against the current setting
    1840             : 
    1841             :     call set_clubb_debug_level( &
    1842        1536 :       level ) ! intent(in)
    1843        1536 :   end subroutine set_clubb_debug_level_api
    1844             : 
    1845             :   !================================================================================================
    1846             :   ! clubb_at_least_debug_level - Checks to see if clubb has been set to a specified debug level.
    1847             :   !================================================================================================
    1848             : 
    1849           0 :   logical function clubb_at_least_debug_level_api( &
    1850             :     level )
    1851             :     
    1852             :     use error_code, only: &
    1853             :         clubb_at_least_debug_level ! Procedure
    1854             : 
    1855             :     implicit none
    1856             : 
    1857             :     ! Input variable
    1858             :     integer, intent(in) :: level   ! The debug level being checked against the current setting
    1859             : 
    1860             :     clubb_at_least_debug_level_api = clubb_at_least_debug_level( &
    1861           0 :       level )
    1862           0 :   end function clubb_at_least_debug_level_api
    1863             : 
    1864             :   !================================================================================================
    1865             :   ! fill_holes_vertical - clips values of 'field' that are below 'threshold' as much as possible.
    1866             :   !================================================================================================
    1867             : 
    1868           0 :   subroutine fill_holes_vertical_api( nz, ngrdcol, num_draw_pts, threshold, upper_hf_level, &
    1869           0 :                                       dz, rho_ds, &
    1870           0 :                                       field )
    1871             : 
    1872             :     use fill_holes, only : fill_holes_vertical
    1873             : 
    1874             :      ! Type
    1875             : 
    1876             :     implicit none
    1877             :     
    1878             :     ! --------------------- Input variables ---------------------
    1879             :     integer, intent(in) :: &
    1880             :       nz, &
    1881             :       ngrdcol
    1882             :     
    1883             :     real( kind = core_rknd ), dimension(ngrdcol,nz) :: &
    1884             :       dz      ! Spacing between thermodynamic grid levels; centered over
    1885             :               ! momentum grid levels
    1886             :               ! OR
    1887             :               ! Spcaing between momentum grid levels; centered over
    1888             :               ! thermodynamic grid levels
    1889             :                   
    1890             :     integer, intent(in) :: & 
    1891             :       num_draw_pts, & ! The number of points on either side of the hole;
    1892             :                       ! Mass is drawn from these points to fill the hole.  []
    1893             :       upper_hf_level  ! Upper grid level of global hole-filling range      []
    1894             : 
    1895             :     real( kind = core_rknd ), intent(in) :: & 
    1896             :       threshold  ! A threshold (e.g. w_tol*w_tol) below which field must not
    1897             :                  ! fall                           [Units vary; same as field]
    1898             : 
    1899             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(in) ::  & 
    1900             :       rho_ds       ! Dry, static density on thermodynamic or momentum levels    [kg/m^3]
    1901             : 
    1902             :     ! --------------------- Input/Output variable ---------------------
    1903             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(inout) :: & 
    1904             :       field  ! The field (e.g. wp2) that contains holes [Units same as threshold]
    1905             : 
    1906             :     call fill_holes_vertical( nz, ngrdcol, num_draw_pts, threshold, upper_hf_level, & ! intent(in)
    1907             :                               dz, rho_ds,                                          & ! intent(in)
    1908           0 :                               field )                                                 ! intent(inout)
    1909             :       
    1910           0 :   end subroutine fill_holes_vertical_api
    1911             : 
    1912             :   !=============================================================================
    1913             :   ! fill_holes_hydromet - fills holes in a hydrometeor using mass from another
    1914             :   ! hydrometeor that has the same phase.
    1915             :   !=============================================================================
    1916           0 :   subroutine fill_holes_hydromet_api( nz, hydromet_dim, hydromet, & ! Intent(in)
    1917           0 :                                       hydromet_filled ) ! Intent(out)
    1918             : 
    1919             :     use fill_holes, only: &
    1920             :         fill_holes_hydromet ! Procedure
    1921             : 
    1922             :     implicit none
    1923             : 
    1924             :     ! Input Variables
    1925             :     integer, intent(in) :: &
    1926             :       hydromet_dim, & ! Number of hydrometeor fields
    1927             :       nz              ! Number of vertical grid levels
    1928             : 
    1929             :     real( kind = core_rknd ), dimension(nz,hydromet_dim), intent(in) :: &
    1930             :       hydromet    ! Mean of hydrometeor fields    [units vary] 
    1931             : 
    1932             :     ! Output Variables
    1933             :     real( kind = core_rknd ), dimension(nz,hydromet_dim), intent(out) :: &
    1934             :       hydromet_filled ! Mean of hydrometeor fields after hole filling [un. vary]
    1935             : 
    1936             : 
    1937             :     call fill_holes_hydromet( nz, hydromet_dim, hydromet, & ! Intent(in)
    1938           0 :                               hydromet_filled ) ! Intent(out)
    1939             : 
    1940             : 
    1941           0 :   end subroutine fill_holes_hydromet_api
    1942             : 
    1943             :   !================================================================================================
    1944             :   ! vertical_integral - Computes the vertical integral.
    1945             :   !================================================================================================
    1946             : 
    1947           0 :   function vertical_integral_api( &
    1948           0 :     total_idx, rho_ds, &
    1949           0 :     field, dz )
    1950             : 
    1951             :     use advance_helper_module, only : vertical_integral
    1952             : 
    1953             :     implicit none
    1954             : 
    1955             :     ! Input variables
    1956             :     integer, intent(in) :: &
    1957             :       total_idx  ! The total numer of indices within the range of averaging
    1958             : 
    1959             :     real( kind = core_rknd ), dimension(total_idx), intent(in) ::  &
    1960             :       rho_ds,  & ! Dry, static density                   [kg/m^3]
    1961             :       field,   & ! The field to be vertically averaged   [Units vary]
    1962             :       dz         ! Level thickness                       [1/m]
    1963             :     ! Note:  The rho_ds and field points need to be arranged from
    1964             :     !        lowest to highest in altitude, with rho_ds(1) and
    1965             :     !        field(1) actually their respective values at level k = begin_idx.
    1966             : 
    1967             :     real( kind = core_rknd ) :: &
    1968             :       vertical_integral_api ! Integral in the numerator (see description)
    1969             : 
    1970             :     vertical_integral_api = vertical_integral( &
    1971             :       total_idx, rho_ds, &
    1972           0 :       field, dz )
    1973           0 :   end function vertical_integral_api
    1974             : 
    1975             :   !================================================================================================
    1976             :   ! setup_grid_heights - Sets the heights and interpolation weights of the column.
    1977             :   !================================================================================================
    1978             : 
    1979           0 :   subroutine setup_grid_heights_api_single_col( &
    1980             :     l_implemented, grid_type,  &
    1981           0 :     deltaz, zm_init, momentum_heights,  &
    1982           0 :     gr, thermodynamic_heights )
    1983             : 
    1984             :     use grid_class, only: & 
    1985             :         grid, & ! Type
    1986             :         setup_grid_heights
    1987             :     
    1988             :     use error_code, only : &
    1989             :         clubb_fatal_error       ! Constant
    1990             : 
    1991             :     implicit none
    1992             : 
    1993             :     ! Input Variables
    1994             :    
    1995             :     type(grid), target, intent(inout) :: gr
    1996             : 
    1997             :     ! Flag to see if CLUBB is running on it's own,
    1998             :     ! or if it's implemented as part of a host model.
    1999             :     logical, intent(in) :: l_implemented
    2000             : 
    2001             :     ! If CLUBB is running on it's own, this option determines if it is using:
    2002             :     ! 1) an evenly-spaced grid;
    2003             :     ! 2) a stretched (unevenly-spaced) grid entered on the thermodynamic grid
    2004             :     !    levels (with momentum levels set halfway between thermodynamic levels);
    2005             :     !    or
    2006             :     ! 3) a stretched (unevenly-spaced) grid entered on the momentum grid levels
    2007             :     !    (with thermodynamic levels set halfway between momentum levels).
    2008             :     integer, intent(in) :: grid_type
    2009             : 
    2010             :     ! If the CLUBB model is running by itself, and is using an evenly-spaced
    2011             :     ! grid (grid_type = 1), it needs the vertical grid spacing and
    2012             :     ! momentum-level starting altitude as input.
    2013             :     real( kind = core_rknd ), intent(in) ::  &
    2014             :       deltaz,   & ! Vertical grid spacing                  [m]
    2015             :       zm_init     ! Initial grid altitude (momentum level) [m]
    2016             : 
    2017             : 
    2018             :     ! If the CLUBB parameterization is implemented in a host model, it needs to
    2019             :     ! use the host model's momentum level altitudes and thermodynamic level
    2020             :     ! altitudes.
    2021             :     ! If the CLUBB model is running by itself, but is using a stretched grid
    2022             :     ! entered on thermodynamic levels (grid_type = 2), it needs to use the
    2023             :     ! thermodynamic level altitudes as input.
    2024             :     ! If the CLUBB model is running by itself, but is using a stretched grid
    2025             :     ! entered on momentum levels (grid_type = 3), it needs to use the momentum
    2026             :     ! level altitudes as input.
    2027             :     real( kind = core_rknd ), intent(in), dimension(gr%nz) ::  &
    2028             :       momentum_heights,   & ! Momentum level altitudes (input)      [m]
    2029             :       thermodynamic_heights ! Thermodynamic level altitudes (input) [m]
    2030             :       
    2031             :     ! ------------------- Local Variables -------------------
    2032             :     
    2033             :     real( kind = core_rknd ), dimension(1) ::  &
    2034             :       deltaz_col,   & ! Vertical grid spacing                  [m]
    2035             :       zm_init_col     ! Initial grid altitude (momentum level) [m]
    2036             :       
    2037             :     real( kind = core_rknd ), dimension(1,gr%nz) ::  &
    2038           0 :       momentum_heights_col,   & ! Momentum level altitudes (input)      [m]
    2039           0 :       thermodynamic_heights_col ! Thermodynamic level altitudes (input) [m]
    2040             :       
    2041           0 :     deltaz_col(1) = deltaz
    2042           0 :     zm_init_col(1) = zm_init
    2043           0 :     momentum_heights_col(1,:) = momentum_heights
    2044           0 :     thermodynamic_heights_col(1,:) = thermodynamic_heights
    2045             : 
    2046             :     call setup_grid_heights( &
    2047             :       gr%nz, 1, & ! intent(in)
    2048             :       l_implemented, grid_type,  & ! intent(in)
    2049             :       deltaz_col, zm_init_col, momentum_heights_col,  & ! intent(in)
    2050             :       thermodynamic_heights_col, & ! intent(in)
    2051           0 :       gr ) ! intent(inout)
    2052             : 
    2053           0 :     if ( err_code == clubb_fatal_error ) error stop
    2054             : 
    2055           0 :   end subroutine setup_grid_heights_api_single_col
    2056             :   
    2057             :   !================================================================================================
    2058             :   ! setup_grid_heights - Sets the heights and interpolation weights of the column.
    2059             :   !================================================================================================
    2060             : 
    2061           0 :   subroutine setup_grid_heights_api_multi_col( &
    2062             :       nz, ngrdcol, &
    2063             :       l_implemented, grid_type,  &
    2064           0 :       deltaz, zm_init, momentum_heights,  &
    2065           0 :       gr, thermodynamic_heights )
    2066             : 
    2067             :     use grid_class, only: & 
    2068             :         grid, & ! Type
    2069             :         setup_grid_heights
    2070             :     
    2071             :     use error_code, only : &
    2072             :         clubb_fatal_error       ! Constant
    2073             : 
    2074             :     implicit none
    2075             : 
    2076             :     ! Input Variables
    2077             :     integer, intent(in) :: &
    2078             :       nz, &
    2079             :       ngrdcol
    2080             : 
    2081             :     type (grid), target, intent(inout) :: gr
    2082             : 
    2083             :     ! Flag to see if CLUBB is running on it's own,
    2084             :     ! or if it's implemented as part of a host model.
    2085             :     logical, intent(in) :: l_implemented
    2086             : 
    2087             :     ! If CLUBB is running on it's own, this option determines if it is using:
    2088             :     ! 1) an evenly-spaced grid;
    2089             :     ! 2) a stretched (unevenly-spaced) grid entered on the thermodynamic grid
    2090             :     !    levels (with momentum levels set halfway between thermodynamic levels);
    2091             :     !    or
    2092             :     ! 3) a stretched (unevenly-spaced) grid entered on the momentum grid levels
    2093             :     !    (with thermodynamic levels set halfway between momentum levels).
    2094             :     integer, intent(in) :: grid_type
    2095             : 
    2096             :     ! If the CLUBB model is running by itself, and is using an evenly-spaced
    2097             :     ! grid (grid_type = 1), it needs the vertical grid spacing and
    2098             :     ! momentum-level starting altitude as input.
    2099             :     real( kind = core_rknd ), dimension(ngrdcol), intent(in) ::  &
    2100             :       deltaz,   & ! Vertical grid spacing                  [m]
    2101             :       zm_init     ! Initial grid altitude (momentum level) [m]
    2102             : 
    2103             : 
    2104             :     ! If the CLUBB parameterization is implemented in a host model, it needs to
    2105             :     ! use the host model's momentum level altitudes and thermodynamic level
    2106             :     ! altitudes.
    2107             :     ! If the CLUBB model is running by itself, but is using a stretched grid
    2108             :     ! entered on thermodynamic levels (grid_type = 2), it needs to use the
    2109             :     ! thermodynamic level altitudes as input.
    2110             :     ! If the CLUBB model is running by itself, but is using a stretched grid
    2111             :     ! entered on momentum levels (grid_type = 3), it needs to use the momentum
    2112             :     ! level altitudes as input.
    2113             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,nz) ::  &
    2114             :       momentum_heights,   & ! Momentum level altitudes (input)      [m]
    2115             :       thermodynamic_heights ! Thermodynamic level altitudes (input) [m]
    2116             : 
    2117             :     call setup_grid_heights( &
    2118             :       nz, ngrdcol, & ! intent(in)
    2119             :       l_implemented, grid_type,  & ! intent(in)
    2120             :       deltaz, zm_init, momentum_heights,  & ! intent(in)
    2121             :       thermodynamic_heights, & ! intent(in)
    2122           0 :       gr ) ! intent(inout)
    2123             : 
    2124           0 :     if ( err_code == clubb_fatal_error ) error stop
    2125             : 
    2126           0 :   end subroutine setup_grid_heights_api_multi_col
    2127             :   
    2128             :   !================================================================================================
    2129             :   ! setup_grid - This subroutine sets up the CLUBB vertical grid for a single column
    2130             :   !================================================================================================
    2131           0 :   subroutine setup_grid_api_single_col( nzmax, sfc_elevation, l_implemented, &
    2132             :                                         grid_type, deltaz, zm_init, zm_top, &
    2133           0 :                                         momentum_heights, thermodynamic_heights, &
    2134             :                                         gr )
    2135             :                             
    2136             :     use grid_class, only: & 
    2137             :         grid, & ! Type
    2138             :         setup_grid
    2139             : 
    2140             :     implicit none
    2141             : 
    2142             :     ! Input Variables
    2143             :     integer, intent(in) ::  & 
    2144             :       nzmax  ! Number of vertical levels in grid      [#]
    2145             : 
    2146             :     type(grid), target, intent(inout) :: gr
    2147             : 
    2148             :     real( kind = core_rknd ), intent(in) ::  &
    2149             :       sfc_elevation  ! Elevation of ground level    [m AMSL]
    2150             :       
    2151             :     logical, intent(in) :: l_implemented
    2152             :     
    2153             :     integer, intent(in) :: grid_type
    2154             :     
    2155             :     real( kind = core_rknd ), intent(in) ::  & 
    2156             :       deltaz,   & ! Vertical grid spacing                  [m]
    2157             :       zm_init,  & ! Initial grid altitude (momentum level) [m]
    2158             :       zm_top      ! Maximum grid altitude (momentum level) [m]
    2159             :       
    2160             :     real( kind = core_rknd ), intent(in), dimension(nzmax) ::  & 
    2161             :       momentum_heights,   & ! Momentum level altitudes (input)      [m]
    2162             :       thermodynamic_heights ! Thermodynamic level altitudes (input) [m]
    2163             : 
    2164             :     real( kind = core_rknd ), dimension(1) ::  &
    2165             :       sfc_elevation_col  ! Elevation of ground level    [m AMSL]
    2166             :       
    2167             :     real( kind = core_rknd ), dimension(1) ::  & 
    2168             :       deltaz_col,   & ! Vertical grid spacing                  [m]
    2169             :       zm_init_col,  & ! Initial grid altitude (momentum level) [m]
    2170             :       zm_top_col      ! Maximum grid altitude (momentum level) [m]
    2171             :       
    2172             :     real( kind = core_rknd ), dimension(1,nzmax) ::  & 
    2173           0 :       momentum_heights_col,   & ! Momentum level altitudes (input)      [m]
    2174           0 :       thermodynamic_heights_col ! Thermodynamic level altitudes (input) [m]
    2175             : 
    2176           0 :     sfc_elevation_col(1)            = sfc_elevation
    2177           0 :     deltaz_col(1)                   = deltaz
    2178           0 :     zm_init_col(1)                  = zm_init
    2179           0 :     zm_top_col(1)                   = zm_top
    2180           0 :     momentum_heights_col(1,:)       = momentum_heights
    2181           0 :     thermodynamic_heights_col(1,:)  = thermodynamic_heights
    2182             : 
    2183             :     call setup_grid( nzmax, 1, sfc_elevation_col, l_implemented,      & ! intent(in)
    2184             :                      grid_type, deltaz_col, zm_init_col, zm_top_col,  & ! intent(in)
    2185             :                      momentum_heights_col, thermodynamic_heights_col, & ! intent(in)
    2186           0 :                      gr )                                               ! intent(out)
    2187             : 
    2188           0 :   end subroutine setup_grid_api_single_col
    2189             :   
    2190             :   !================================================================================================
    2191             :   ! setup_grid - This subroutine sets up the CLUBB vertical grid.
    2192             :   !================================================================================================
    2193      176472 :   subroutine setup_grid_api_multi_col( nzmax, ngrdcol, sfc_elevation, l_implemented, &
    2194      176472 :                                        grid_type, deltaz, zm_init, zm_top, &
    2195      176472 :                                        momentum_heights, thermodynamic_heights, &
    2196             :                                        gr )
    2197             :                             
    2198             :     use grid_class, only: & 
    2199             :         grid, & ! Type
    2200             :         setup_grid
    2201             : 
    2202             :     implicit none
    2203             : 
    2204             :     ! Input Variables
    2205             :     integer, intent(in) ::  & 
    2206             :       nzmax,  & ! Number of vertical levels in grid      [#]
    2207             :       ngrdcol   ! Number of grid columns                 [#]
    2208             :       
    2209             :     type (grid), target, intent(inout) :: gr
    2210             : 
    2211             :     real( kind = core_rknd ), dimension(ngrdcol), intent(in) ::  &
    2212             :       sfc_elevation  ! Elevation of ground level    [m AMSL]
    2213             :       
    2214             :     logical, intent(in) :: l_implemented
    2215             :     
    2216             :     integer, intent(in) :: grid_type
    2217             :     
    2218             :     real( kind = core_rknd ), dimension(ngrdcol), intent(in) ::  & 
    2219             :       deltaz,   & ! Vertical grid spacing                  [m]
    2220             :       zm_init,  & ! Initial grid altitude (momentum level) [m]
    2221             :       zm_top      ! Maximum grid altitude (momentum level) [m]
    2222             :       
    2223             :     real( kind = core_rknd ), intent(in), dimension(ngrdcol,nzmax) ::  & 
    2224             :       momentum_heights,   & ! Momentum level altitudes (input)      [m]
    2225             :       thermodynamic_heights ! Thermodynamic level altitudes (input) [m]
    2226             : 
    2227             : 
    2228             :     call setup_grid( nzmax, ngrdcol, sfc_elevation, l_implemented, & ! intent(in)
    2229             :                      grid_type, deltaz, zm_init, zm_top,           & ! intent(in)
    2230             :                      momentum_heights, thermodynamic_heights,      & ! intent(in)
    2231      176472 :                      gr )                                            ! intent(out)
    2232             : 
    2233      176472 :   end subroutine setup_grid_api_multi_col
    2234             : 
    2235             :   !================================================================================================
    2236             :   ! lin_interpolate_two_points - Computes a linear interpolation of the value of a variable.
    2237             :   !================================================================================================
    2238             : 
    2239           0 :   function lin_interpolate_two_points_api( &
    2240             :     height_int, height_high, height_low, &
    2241             :     var_high, var_low )
    2242             : 
    2243             :     use interpolation, only : lin_interpolate_two_points
    2244             : 
    2245             :     implicit none
    2246             : 
    2247             :     real( kind = core_rknd ), intent(in) :: &
    2248             :       height_int,  & ! Height to be interpolated to     [m]
    2249             :       height_high, & ! Height above the interpolation   [m]
    2250             :       height_low,  & ! Height below the interpolation   [m]
    2251             :       var_high,    & ! Variable above the interpolation [units vary]
    2252             :       var_low        ! Variable below the interpolation [units vary]
    2253             : 
    2254             :     ! Output Variables
    2255             :     real( kind = core_rknd ) :: lin_interpolate_two_points_api
    2256             : 
    2257             :     lin_interpolate_two_points_api = lin_interpolate_two_points( &
    2258             :       height_int, height_high, height_low, &
    2259           0 :       var_high, var_low )
    2260             : 
    2261           0 :   end function lin_interpolate_two_points_api
    2262             : 
    2263             :   !================================================================================================
    2264             :   ! lin_interpolate_on_grid - Linear interpolation for 25 June 1996 altocumulus case.
    2265             :   !================================================================================================
    2266             : 
    2267           0 :   subroutine lin_interpolate_on_grid_api( &
    2268           0 :     nparam, xlist, tlist, xvalue, tvalue )
    2269             : 
    2270             :     use interpolation, only : lin_interpolate_on_grid
    2271             : 
    2272             :     implicit none
    2273             : 
    2274             :     ! Input Variables
    2275             :     integer, intent(in) :: nparam ! Number of parameters in xlist and tlist
    2276             : 
    2277             :     ! Input/Output Variables
    2278             :     real( kind = core_rknd ), intent(inout), dimension(nparam) ::  &
    2279             :       xlist,  & ! List of x-values (independent variable)
    2280             :       tlist     ! List of t-values (dependent variable)
    2281             : 
    2282             :     real( kind = core_rknd ), intent(in) ::  &
    2283             :       xvalue  ! x-value at which to interpolate
    2284             : 
    2285             :     real( kind = core_rknd ), intent(inout) ::  &
    2286             :       tvalue  ! t-value solved by interpolation
    2287             : 
    2288             :     call lin_interpolate_on_grid( &
    2289             :       nparam, xlist, tlist, xvalue, & ! intent(in)
    2290           0 :       tvalue ) ! intent(inout)
    2291             : 
    2292           0 :   end subroutine lin_interpolate_on_grid_api
    2293             : 
    2294             :   !================================================================================================
    2295             :   ! read_parameters - Read a namelist containing the model parameters.
    2296             :   !================================================================================================
    2297             : 
    2298        1536 :   subroutine read_parameters_api( iunit, filename, &
    2299             :                                   C1, C1b, C1c, C2rt, C2thl, C2rtthl, &
    2300             :                                   C4, C_uu_shr, C_uu_buoy, C6rt, C6rtb, C6rtc, &
    2301             :                                   C6thl, C6thlb, C6thlc, C7, C7b, C7c, C8, C8b, C10, &
    2302             :                                   C11, C11b, C11c, C12, C13, C14, C_wp2_pr_dfsn, C_wp3_pr_tp, &
    2303             :                                   C_wp3_pr_turb, C_wp3_pr_dfsn, C_wp2_splat, &
    2304             :                                   C6rt_Lscale0, C6thl_Lscale0, C7_Lscale0, wpxp_L_thresh, &
    2305             :                                   c_K, c_K1, nu1, c_K2, nu2, c_K6, nu6, c_K8, nu8, &
    2306             :                                   c_K9, nu9, nu10, c_K_hm, c_K_hmb, K_hm_min_coef, nu_hm, &
    2307             :                                   slope_coef_spread_DG_means_w, pdf_component_stdev_factor_w, &
    2308             :                                   coef_spread_DG_means_rt, coef_spread_DG_means_thl, &
    2309             :                                   gamma_coef, gamma_coefb, gamma_coefc, mu, beta, lmin_coef, &
    2310             :                                   omicron, zeta_vrnce_rat, upsilon_precip_frac_rat, &
    2311             :                                   lambda0_stability_coef, mult_coef, taumin, taumax, &
    2312             :                                   Lscale_mu_coef, Lscale_pert_coef, alpha_corr, &
    2313             :                                   Skw_denom_coef, c_K10, c_K10h, thlp2_rad_coef, &
    2314             :                                   thlp2_rad_cloud_frac_thresh, up2_sfc_coef, &
    2315             :                                   Skw_max_mag, xp3_coef_base, xp3_coef_slope, &
    2316             :                                   altitude_threshold, rtp2_clip_coef, C_invrs_tau_bkgnd, &
    2317             :                                   C_invrs_tau_sfc, C_invrs_tau_shear, C_invrs_tau_N2, & 
    2318             :                                   C_invrs_tau_N2_wp2, C_invrs_tau_N2_xp2, &
    2319             :                                   C_invrs_tau_N2_wpxp, C_invrs_tau_N2_clear_wp3, &
    2320             :                                   C_invrs_tau_wpxp_Ri, C_invrs_tau_wpxp_N2_thresh, &
    2321             :                                   Cx_min, Cx_max, Richardson_num_min, Richardson_num_max, &
    2322             :                                   wpxp_Ri_exp, a3_coef_min, a_const, bv_efold, z_displace, &
    2323             :                                   params )
    2324             : 
    2325             :     use parameters_tunable, only : read_parameters
    2326             : 
    2327             :     use parameter_indices, only:  &
    2328             :         nparams ! Variable(s)
    2329             : 
    2330             :     implicit none
    2331             : 
    2332             :     ! Input variables
    2333             :     integer, intent(in) :: iunit
    2334             : 
    2335             :     character(len=*), intent(in) :: filename
    2336             : 
    2337             :     ! Input/Output variables
    2338             :     real( kind = core_rknd ), intent(inout) :: & 
    2339             :       C1, C1b, C1c, C2rt, C2thl, C2rtthl, & 
    2340             :       C4, C_uu_shr, C_uu_buoy, C6rt, C6rtb, C6rtc, C6thl, C6thlb, C6thlc, & 
    2341             :       C7, C7b, C7c, C8, C8b, C10, & 
    2342             :       C11, C11b, C11c, C12, C13, C14, C_wp2_pr_dfsn, C_wp3_pr_tp, &
    2343             :       C_wp3_pr_turb, C_wp3_pr_dfsn, C_wp2_splat, & 
    2344             :       C6rt_Lscale0, C6thl_Lscale0, C7_Lscale0, wpxp_L_thresh, &
    2345             :       c_K, c_K1, nu1, c_K2, nu2, c_K6, nu6, c_K8, nu8,  & 
    2346             :       c_K9, nu9, nu10, c_K_hm, c_K_hmb, K_hm_min_coef, nu_hm, &
    2347             :       slope_coef_spread_DG_means_w, pdf_component_stdev_factor_w, &
    2348             :       coef_spread_DG_means_rt, coef_spread_DG_means_thl, &
    2349             :       gamma_coef, gamma_coefb, gamma_coefc, mu, beta, lmin_coef, &
    2350             :       omicron, zeta_vrnce_rat, upsilon_precip_frac_rat, &
    2351             :       lambda0_stability_coef, mult_coef, taumin, taumax, Lscale_mu_coef, &
    2352             :       Lscale_pert_coef, alpha_corr, Skw_denom_coef, c_K10, c_K10h, &
    2353             :       thlp2_rad_coef, thlp2_rad_cloud_frac_thresh, up2_sfc_coef, &
    2354             :       Skw_max_mag, xp3_coef_base, xp3_coef_slope, altitude_threshold, &
    2355             :       rtp2_clip_coef, C_invrs_tau_bkgnd, C_invrs_tau_sfc, &
    2356             :       C_invrs_tau_shear, C_invrs_tau_N2, C_invrs_tau_N2_wp2, &
    2357             :       C_invrs_tau_N2_xp2, C_invrs_tau_N2_wpxp, C_invrs_tau_N2_clear_wp3, &
    2358             :       C_invrs_tau_wpxp_Ri, C_invrs_tau_wpxp_N2_thresh, &
    2359             :       Cx_min, Cx_max, Richardson_num_min, Richardson_num_max, &
    2360             :       wpxp_Ri_exp, a3_coef_min, a_const, bv_efold, z_displace
    2361             : 
    2362             :     ! Output variables
    2363             :     real( kind = core_rknd ), intent(out), dimension(nparams) :: params
    2364             : 
    2365             :     call read_parameters( iunit, filename, & ! intent(in)
    2366             :                           C1, C1b, C1c, C2rt, C2thl, C2rtthl, &
    2367             :                           C4, C_uu_shr, C_uu_buoy, C6rt, C6rtb, C6rtc, &
    2368             :                           C6thl, C6thlb, C6thlc, C7, C7b, C7c, C8, C8b, C10, &
    2369             :                           C11, C11b, C11c, C12, C13, C14, C_wp2_pr_dfsn, C_wp3_pr_tp, &
    2370             :                           C_wp3_pr_turb, C_wp3_pr_dfsn, C_wp2_splat, &
    2371             :                           C6rt_Lscale0, C6thl_Lscale0, C7_Lscale0, wpxp_L_thresh, &
    2372             :                           c_K, c_K1, nu1, c_K2, nu2, c_K6, nu6, c_K8, nu8, &
    2373             :                           c_K9, nu9, nu10, c_K_hm, c_K_hmb, K_hm_min_coef, nu_hm, &
    2374             :                           slope_coef_spread_DG_means_w, pdf_component_stdev_factor_w, &
    2375             :                           coef_spread_DG_means_rt, coef_spread_DG_means_thl, &
    2376             :                           gamma_coef, gamma_coefb, gamma_coefc, mu, beta, lmin_coef, &
    2377             :                           omicron, zeta_vrnce_rat, upsilon_precip_frac_rat, &
    2378             :                           lambda0_stability_coef, mult_coef, taumin, taumax, &
    2379             :                           Lscale_mu_coef, Lscale_pert_coef, alpha_corr, &
    2380             :                           Skw_denom_coef, c_K10, c_K10h, thlp2_rad_coef, &
    2381             :                           thlp2_rad_cloud_frac_thresh, up2_sfc_coef, &
    2382             :                           Skw_max_mag, xp3_coef_base, xp3_coef_slope, &
    2383             :                           altitude_threshold, rtp2_clip_coef, C_invrs_tau_bkgnd, &
    2384             :                           C_invrs_tau_sfc, C_invrs_tau_shear, C_invrs_tau_N2, & 
    2385             :                           C_invrs_tau_N2_wp2, C_invrs_tau_N2_xp2, &
    2386             :                           C_invrs_tau_N2_wpxp, C_invrs_tau_N2_clear_wp3, &
    2387             :                           C_invrs_tau_wpxp_Ri, C_invrs_tau_wpxp_N2_thresh, &
    2388             :                           Cx_min, Cx_max, Richardson_num_min, Richardson_num_max, &
    2389             :                           wpxp_Ri_exp, a3_coef_min, a_const, bv_efold, z_displace, &
    2390        1536 :                           params ) ! intent(out)
    2391             : 
    2392        1536 :   end subroutine read_parameters_api
    2393             : 
    2394             :   !================================================================================================
    2395             :   ! setup_parameters - Sets up model parameters for a single column
    2396             :   !================================================================================================
    2397           0 :   subroutine setup_parameters_api_single_col( &
    2398             :              deltaz, params, gr, grid_type, &
    2399             :              l_prescribed_avg_deltaz, &
    2400             :              lmin, nu_vert_res_dep, err_code_api )
    2401             : 
    2402             :     use grid_class, only: &
    2403             :         grid    ! Type(s)
    2404             : 
    2405             :     use parameters_tunable, only: &
    2406             :         setup_parameters
    2407             : 
    2408             :     use parameter_indices, only:  &
    2409             :         nparams ! Variable(s)
    2410             : 
    2411             :     implicit none
    2412             : 
    2413             :     ! Input Variables
    2414             :     real( kind = core_rknd ), intent(in) ::  &
    2415             :       deltaz  ! Change per height level        [m]
    2416             : 
    2417             :     real( kind = core_rknd ), intent(in), dimension(nparams) :: &
    2418             :       params  ! Tuneable model parameters      [-]
    2419             : 
    2420             :     ! Grid definition
    2421             :     type(grid), target, intent(in) :: &
    2422             :       gr
    2423             : 
    2424             :     ! If CLUBB is running on its own, this option determines
    2425             :     ! if it is using:
    2426             :     ! 1) an evenly-spaced grid,
    2427             :     ! 2) a stretched (unevenly-spaced) grid entered on the
    2428             :     !    thermodynamic grid levels (with momentum levels set
    2429             :     !    halfway between thermodynamic levels), or
    2430             :     ! 3) a stretched (unevenly-spaced) grid entered on the
    2431             :     !    momentum grid levels (with thermodynamic levels set
    2432             :     !    halfway between momentum levels).
    2433             :     integer, intent(in) :: grid_type
    2434             : 
    2435             :     logical, intent(in) :: &
    2436             :       l_prescribed_avg_deltaz ! used in adj_low_res_nu. If .true., avg_deltaz = deltaz
    2437             : 
    2438             :     ! Output Variables 
    2439             :     real( kind = core_rknd ), intent(out) :: &
    2440             :       lmin    ! Min. value for the length scale    [m]
    2441             : 
    2442             :     type(nu_vertical_res_dep), intent(out) :: &
    2443             :       nu_vert_res_dep    ! Vertical resolution dependent nu values
    2444             : 
    2445             :     integer, intent(out) ::  &                    
    2446             :       err_code_api ! Error condition 
    2447             :       
    2448             :       
    2449             :     real( kind = core_rknd ), dimension(1) ::  &
    2450             :       deltaz_col  ! Change per height level        [m]
    2451             : 
    2452           0 :     deltaz_col(1) = deltaz
    2453             : 
    2454             :     call setup_parameters( & 
    2455             :               deltaz_col, params, gr, 1, grid_type, &
    2456             :               l_prescribed_avg_deltaz, &
    2457           0 :               lmin, nu_vert_res_dep, err_code_api )
    2458             : 
    2459           0 :   end subroutine setup_parameters_api_single_col
    2460             :   
    2461             :   !================================================================================================
    2462             :   ! setup_parameters - Sets up model parameters.
    2463             :   !================================================================================================
    2464             :   
    2465      176472 :   subroutine setup_parameters_api_multi_col( &
    2466      176472 :              deltaz, params, gr, ngrdcol, grid_type, &
    2467             :              l_prescribed_avg_deltaz, &
    2468             :              lmin, nu_vert_res_dep, err_code_api )
    2469             : 
    2470             :     use grid_class, only: &
    2471             :         grid
    2472             : 
    2473             :     use parameters_tunable, only: &
    2474             :         setup_parameters
    2475             : 
    2476             :     use parameter_indices, only:  &
    2477             :         nparams ! Variable(s)
    2478             : 
    2479             :     implicit none
    2480             : 
    2481             :     ! Input Variables
    2482             : 
    2483             :     ! Grid definition
    2484             :     type(grid), target, intent(in) :: &
    2485             :       gr
    2486             : 
    2487             :     integer, intent(in) :: &
    2488             :       ngrdcol   ! Number of grid columns          [#]
    2489             : 
    2490             :     real( kind = core_rknd ), dimension(ngrdcol), intent(in) ::  &
    2491             :       deltaz  ! Change per height level        [m]
    2492             : 
    2493             :     real( kind = core_rknd ), intent(in), dimension(nparams) :: &
    2494             :       params  ! Tuneable model parameters      [-]
    2495             :     ! If CLUBB is running on its own, this option determines
    2496             :     ! if it is using:
    2497             :     ! 1) an evenly-spaced grid,
    2498             :     ! 2) a stretched (unevenly-spaced) grid entered on the
    2499             :     !    thermodynamic grid levels (with momentum levels set
    2500             :     !    halfway between thermodynamic levels), or
    2501             :     ! 3) a stretched (unevenly-spaced) grid entered on the
    2502             :     !    momentum grid levels (with thermodynamic levels set
    2503             :     !    halfway between momentum levels).
    2504             :     integer, intent(in) :: grid_type
    2505             : 
    2506             :     logical, intent(in) :: &
    2507             :       l_prescribed_avg_deltaz ! used in adj_low_res_nu. If .true., avg_deltaz = deltaz
    2508             : 
    2509             :     ! Output Variables 
    2510             :     real( kind = core_rknd ), intent(out) :: &
    2511             :       lmin    ! Min. value for the length scale    [m]
    2512             : 
    2513             :     type(nu_vertical_res_dep), intent(out) :: &
    2514             :       nu_vert_res_dep    ! Vertical resolution dependent nu values
    2515             : 
    2516             :     integer, intent(out) ::  &                    
    2517             :       err_code_api ! Error condition 
    2518             : 
    2519             :     call setup_parameters( & 
    2520             :               deltaz, params, gr, ngrdcol, grid_type, &
    2521             :               l_prescribed_avg_deltaz, &
    2522      176472 :               lmin, nu_vert_res_dep, err_code_api )
    2523             : 
    2524      176472 :   end subroutine setup_parameters_api_multi_col
    2525             : 
    2526             :   !================================================================================================
    2527             :   ! adj_low_res_nu - Adjusts values of background eddy diffusivity based on vertical grid spacing.
    2528             :   !================================================================================================
    2529             : 
    2530           0 :   subroutine adj_low_res_nu_api_single_col( gr, grid_type, deltaz,  & ! Intent(in)
    2531             :                                  l_prescribed_avg_deltaz, mult_coef, &  ! Intent(in)
    2532             :                                  nu1, nu2, nu6, nu8, nu9, nu10, nu_hm, &  ! Intent(in)
    2533             :                                  nu_vert_res_dep )  ! Intent(out)
    2534             : 
    2535             :     use parameters_tunable, only : adj_low_res_nu
    2536             : 
    2537             :     implicit none
    2538             : 
    2539             :     ! Input Variables
    2540             : 
    2541             :     ! Grid definition
    2542             :     type(grid), target, intent(in) :: &
    2543             :       gr
    2544             : 
    2545             :     ! If CLUBB is running on it's own, this option determines
    2546             :     ! if it is using:
    2547             :     ! 1) an evenly-spaced grid,
    2548             :     ! 2) a stretched (unevenly-spaced) grid entered on the
    2549             :     !    thermodynamic grid levels (with momentum levels set
    2550             :     !    halfway between thermodynamic levels), or
    2551             :     ! 3) a stretched (unevenly-spaced) grid entered on the
    2552             :     !    momentum grid levels (with thermodynamic levels set
    2553             :     !    halfway between momentum levels).
    2554             :     integer, intent(in) :: grid_type
    2555             : 
    2556             :     real( kind = core_rknd ), intent(in) ::  &
    2557             :       deltaz  ! Change per height level        [m]
    2558             : 
    2559             :     logical, intent(in) :: &
    2560             :       l_prescribed_avg_deltaz ! used in adj_low_res_nu. If .true., avg_deltaz = deltaz
    2561             : 
    2562             :     real( kind = core_rknd ), intent(in) :: &
    2563             :       mult_coef, & ! CLUBB tunable parameter mult_coef
    2564             :       nu1,       & ! CLUBB tunable parameter nu1
    2565             :       nu2,       & ! CLUBB tunable parameter nu2
    2566             :       nu6,       & ! CLUBB tunable parameter nu6
    2567             :       nu8,       & ! CLUBB tunable parameter nu8
    2568             :       nu9,       & ! CLUBB tunable parameter nu9
    2569             :       nu10,      & ! CLUBB tunable parameter nu10
    2570             :       nu_hm        ! CLUBB tunable parameter nu_hm
    2571             : 
    2572             :     ! Output Variables
    2573             :     type(nu_vertical_res_dep), intent(out) :: &
    2574             :       nu_vert_res_dep    ! Vertical resolution dependent nu values
    2575             :       
    2576             :     ! Local variables
    2577             :     real( kind = core_rknd ), dimension(1) ::  &
    2578             :       deltaz_col  ! Change per height level        [m]
    2579             :       
    2580           0 :     deltaz_col(1) = deltaz
    2581             : 
    2582             :     call adj_low_res_nu( gr, 1, grid_type, deltaz_col, & ! Intent(in)
    2583             :                          l_prescribed_avg_deltaz, mult_coef, &  ! Intent(in)
    2584             :                          nu1, nu2, nu6, nu8, nu9, nu10, nu_hm, &  ! Intent(in)
    2585           0 :                          nu_vert_res_dep )  ! Intent(out)
    2586             : 
    2587           0 :   end subroutine adj_low_res_nu_api_single_col
    2588             :   
    2589             :   !================================================================================================
    2590             :   ! adj_low_res_nu - Adjusts values of background eddy diffusivity based on vertical grid spacing.
    2591             :   !================================================================================================
    2592             : 
    2593           0 :   subroutine adj_low_res_nu_api_multi_col( gr, ngrdcol, grid_type, deltaz,  & ! Intent(in)
    2594             :                                  l_prescribed_avg_deltaz, mult_coef, &  ! Intent(in)
    2595             :                                  nu1, nu2, nu6, nu8, nu9, nu10, nu_hm, &  ! Intent(in)
    2596             :                                  nu_vert_res_dep )  ! Intent(out)
    2597             : 
    2598             :     use parameters_tunable, only : adj_low_res_nu
    2599             : 
    2600             :     implicit none
    2601             : 
    2602             :     ! Input Variables
    2603             : 
    2604             :     ! Grid definition
    2605             :     type(grid), target, intent(in) :: &
    2606             :       gr
    2607             : 
    2608             :     integer, intent(in) :: &
    2609             :       ngrdcol
    2610             : 
    2611             :     ! If CLUBB is running on it's own, this option determines
    2612             :     ! if it is using:
    2613             :     ! 1) an evenly-spaced grid,
    2614             :     ! 2) a stretched (unevenly-spaced) grid entered on the
    2615             :     !    thermodynamic grid levels (with momentum levels set
    2616             :     !    halfway between thermodynamic levels), or
    2617             :     ! 3) a stretched (unevenly-spaced) grid entered on the
    2618             :     !    momentum grid levels (with thermodynamic levels set
    2619             :     !    halfway between momentum levels).
    2620             :     integer, intent(in) :: grid_type
    2621             : 
    2622             :     real( kind = core_rknd ), dimension(ngrdcol), intent(in) ::  &
    2623             :       deltaz  ! Change per height level        [m]
    2624             : 
    2625             :     logical, intent(in) :: &
    2626             :       l_prescribed_avg_deltaz ! used in adj_low_res_nu. If .true., avg_deltaz = deltaz
    2627             : 
    2628             :     real( kind = core_rknd ), intent(in) :: &
    2629             :       mult_coef, & ! CLUBB tunable parameter mult_coef
    2630             :       nu1,       & ! CLUBB tunable parameter nu1
    2631             :       nu2,       & ! CLUBB tunable parameter nu2
    2632             :       nu6,       & ! CLUBB tunable parameter nu6
    2633             :       nu8,       & ! CLUBB tunable parameter nu8
    2634             :       nu9,       & ! CLUBB tunable parameter nu9
    2635             :       nu10,      & ! CLUBB tunable parameter nu10
    2636             :       nu_hm        ! CLUBB tunable parameter nu_hm
    2637             : 
    2638             :     ! Output Variables
    2639             :     type(nu_vertical_res_dep), intent(out) :: &
    2640             :       nu_vert_res_dep    ! Vertical resolution dependent nu values
    2641             : 
    2642             :     call adj_low_res_nu( gr, ngrdcol, grid_type, deltaz, & ! Intent(in)
    2643             :                          l_prescribed_avg_deltaz, mult_coef, &  ! Intent(in)
    2644             :                          nu1, nu2, nu6, nu8, nu9, nu10, nu_hm, &  ! Intent(in)
    2645           0 :                          nu_vert_res_dep )  ! Intent(out)
    2646             : 
    2647           0 :   end subroutine adj_low_res_nu_api_multi_col
    2648             : 
    2649             : ! The CLUBB_CAM preprocessor directives are being commented out because this
    2650             : ! code is now also used for WRF-CLUBB.
    2651             : !#ifdef CLUBB_CAM /* Code for storing pdf_parameter structs in pbuf as array */
    2652             :   !================================================================================================
    2653             :   ! pack_pdf_params - Returns a two dimensional real array with all values.
    2654             :   !================================================================================================
    2655             : 
    2656           0 :   subroutine pack_pdf_params_api( pdf_params, nz, r_param_array, &
    2657             :                                   k_start, k_end )
    2658             : 
    2659             :     use pdf_parameter_module, only : pack_pdf_params
    2660             : 
    2661             :     !use statements
    2662             : 
    2663             :     implicit none
    2664             : 
    2665             :     integer, intent(in) :: nz ! Num Vert Model Levs
    2666             :     
    2667             :     ! Input a pdf_parameter array with nz instances of pdf_parameter
    2668             :     type (pdf_parameter), intent(inout) :: pdf_params
    2669             : 
    2670             :     ! Output a two dimensional real array with all values
    2671             :     real (kind = core_rknd), dimension(nz,num_pdf_params), intent(inout) :: &
    2672             :       r_param_array
    2673             :       
    2674             :     integer, optional, intent(in) :: k_start, k_end
    2675             :       
    2676           0 :     if( present( k_start ) .and. present( k_end ) ) then
    2677             :         call pack_pdf_params( pdf_params, nz, & ! intent(in)
    2678             :                               r_param_array, & ! intent(out)
    2679           0 :                               k_start, k_end ) ! intent(in/optional)
    2680             :     else 
    2681             :         call pack_pdf_params( pdf_params, nz, & ! intent(in)
    2682           0 :                               r_param_array ) ! intent(out)
    2683             :     end if
    2684             : 
    2685           0 :   end subroutine pack_pdf_params_api
    2686             : 
    2687             :   !================================================================================================
    2688             :   ! unpack_pdf_params - Returns a pdf_parameter array with nz instances of pdf_parameter.
    2689             :   !================================================================================================
    2690             : 
    2691           0 :   subroutine unpack_pdf_params_api( r_param_array, nz, pdf_params, &
    2692             :                                     k_start, k_end )
    2693             : 
    2694             :     use pdf_parameter_module, only : unpack_pdf_params
    2695             : 
    2696             :     implicit none
    2697             :     
    2698             :     integer, intent(in) :: nz ! Num Vert Model Levs
    2699             :     
    2700             :     ! Input a two dimensional real array with pdf values
    2701             :     real (kind = core_rknd), dimension(nz,num_pdf_params), intent(in) :: &
    2702             :       r_param_array
    2703             : 
    2704             :     ! Output a pdf_parameter array with nz instances of pdf_parameter
    2705             :     type (pdf_parameter), intent(inout) :: pdf_params
    2706             :     
    2707             :     integer, optional, intent(in) :: k_start, k_end
    2708             : 
    2709           0 :     if( present( k_start ) .and. present( k_end ) ) then
    2710             :         call unpack_pdf_params( r_param_array, nz, & ! intent(in)
    2711             :                                 pdf_params, & ! intent(inout)
    2712           0 :                                 k_start, k_end ) ! intent(in/optional)
    2713             :     else  
    2714             :         call unpack_pdf_params( r_param_array, nz, & ! intent(in)
    2715           0 :                                 pdf_params ) ! intent(inout)
    2716             :     end if
    2717             :     
    2718             :     
    2719           0 :   end subroutine unpack_pdf_params_api
    2720             :   
    2721             : !#endif
    2722             :   !================================================================================================
    2723             :   ! init_pdf_params - allocates arrays for pdf_params
    2724             :   !================================================================================================
    2725       12384 :   subroutine init_pdf_params_api( nz, ngrdcol, pdf_params )
    2726             :   
    2727             :     use pdf_parameter_module, only : init_pdf_params
    2728             :     
    2729             :     implicit none
    2730             : 
    2731             :     ! Input Variable(s)
    2732             :     integer, intent(in) :: &
    2733             :       nz, &   ! Number of vertical grid levels    [-]
    2734             :       ngrdcol ! Number of grid columns            [-]
    2735             : 
    2736             :     ! Output Variable(s)
    2737             :     type(pdf_parameter), intent(out) :: &
    2738             :       pdf_params    ! PDF parameters            [units vary]
    2739             :     
    2740             :     call init_pdf_params( nz, ngrdcol, & ! intent(in)
    2741       12384 :                           pdf_params ) ! intent(out)
    2742             :     
    2743       12384 :   end subroutine init_pdf_params_api
    2744             :   
    2745             :   !================================================================================================
    2746             :   ! copy_single_pdf_params_to_multi - copies values of a single column version of pdf_params
    2747             :   !   to a multiple column version for a specified column.
    2748             :   !
    2749             :   ! NOTE: THIS SUBROUTINE IS INTENDED TO BE TEMPORARY AND SHOULD BECOME UNNECESSARY ONCE 
    2750             :   !       CLUBB IS ABLE TO OPERATE OVER MULTIPLE COLUMNS.
    2751             :   !       See https://github.com/larson-group/cam/issues/129#issuecomment-827944454
    2752             :   !================================================================================================
    2753           0 :   subroutine copy_single_pdf_params_to_multi( pdf_params_single, icol, &
    2754             :                                               pdf_params_multi )
    2755             :     
    2756             :     implicit none
    2757             : 
    2758             :     ! Input Variable(s)
    2759             :     integer, intent(in) :: &
    2760             :       icol   ! Column number to copy to
    2761             :       
    2762             :     type(pdf_parameter), intent(in) :: &
    2763             :       pdf_params_single  ! PDF parameters            [units vary]
    2764             : 
    2765             :     ! Output Variable(s)
    2766             :     type(pdf_parameter), intent(inout) :: &
    2767             :       pdf_params_multi  ! PDF parameters            [units vary]
    2768             :       
    2769           0 :     pdf_params_multi%w_1(icol,:)                  = pdf_params_single%w_1(1,:) 
    2770           0 :     pdf_params_multi%w_2(icol,:)                  = pdf_params_single%w_2(1,:) 
    2771           0 :     pdf_params_multi%varnce_w_1(icol,:)           = pdf_params_single%varnce_w_1(1,:) 
    2772           0 :     pdf_params_multi%varnce_w_2(icol,:)           = pdf_params_single%varnce_w_2(1,:) 
    2773           0 :     pdf_params_multi%rt_1(icol,:)                 = pdf_params_single%rt_1(1,:) 
    2774           0 :     pdf_params_multi%rt_2(icol,:)                 = pdf_params_single%rt_2(1,:) 
    2775           0 :     pdf_params_multi%varnce_rt_1(icol,:)          = pdf_params_single%varnce_rt_1(1,:) 
    2776           0 :     pdf_params_multi%varnce_rt_2(icol,:)          = pdf_params_single%varnce_rt_2(1,:) 
    2777           0 :     pdf_params_multi%thl_1(icol,:)                = pdf_params_single%thl_1(1,:) 
    2778           0 :     pdf_params_multi%thl_2(icol,:)                = pdf_params_single%thl_2(1,:) 
    2779           0 :     pdf_params_multi%varnce_thl_1(icol,:)         = pdf_params_single%varnce_thl_1(1,:) 
    2780           0 :     pdf_params_multi%varnce_thl_2(icol,:)         = pdf_params_single%varnce_thl_2(1,:) 
    2781           0 :     pdf_params_multi%corr_w_rt_1(icol,:)          = pdf_params_single%corr_w_rt_1(1,:) 
    2782           0 :     pdf_params_multi%corr_w_rt_2(icol,:)          = pdf_params_single%corr_w_rt_2(1,:) 
    2783           0 :     pdf_params_multi%corr_w_thl_1(icol,:)         = pdf_params_single%corr_w_thl_1(1,:) 
    2784           0 :     pdf_params_multi%corr_w_thl_2(icol,:)         = pdf_params_single%corr_w_thl_2(1,:) 
    2785           0 :     pdf_params_multi%corr_rt_thl_1(icol,:)        = pdf_params_single%corr_rt_thl_1(1,:) 
    2786           0 :     pdf_params_multi%corr_rt_thl_2(icol,:)        = pdf_params_single%corr_rt_thl_2(1,:) 
    2787           0 :     pdf_params_multi%alpha_thl(icol,:)            = pdf_params_single%alpha_thl(1,:) 
    2788           0 :     pdf_params_multi%alpha_rt(icol,:)             = pdf_params_single%alpha_rt(1,:) 
    2789           0 :     pdf_params_multi%crt_1(icol,:)                = pdf_params_single%crt_1(1,:) 
    2790           0 :     pdf_params_multi%crt_2(icol,:)                = pdf_params_single%crt_2(1,:) 
    2791           0 :     pdf_params_multi%cthl_1(icol,:)               = pdf_params_single%cthl_1(1,:) 
    2792           0 :     pdf_params_multi%cthl_2(icol,:)               = pdf_params_single%cthl_2(1,:) 
    2793           0 :     pdf_params_multi%chi_1(icol,:)                = pdf_params_single%chi_1(1,:) 
    2794           0 :     pdf_params_multi%chi_2(icol,:)                = pdf_params_single%chi_2(1,:) 
    2795           0 :     pdf_params_multi%stdev_chi_1(icol,:)          = pdf_params_single%stdev_chi_1(1,:) 
    2796           0 :     pdf_params_multi%stdev_chi_2(icol,:)          = pdf_params_single%stdev_chi_2(1,:) 
    2797           0 :     pdf_params_multi%stdev_eta_1(icol,:)          = pdf_params_single%stdev_eta_1(1,:) 
    2798           0 :     pdf_params_multi%stdev_eta_2(icol,:)          = pdf_params_single%stdev_eta_2(1,:) 
    2799           0 :     pdf_params_multi%covar_chi_eta_1(icol,:)      = pdf_params_single%covar_chi_eta_1(1,:) 
    2800           0 :     pdf_params_multi%covar_chi_eta_2(icol,:)      = pdf_params_single%covar_chi_eta_2(1,:) 
    2801           0 :     pdf_params_multi%corr_w_chi_1(icol,:)         = pdf_params_single%corr_w_chi_1(1,:) 
    2802           0 :     pdf_params_multi%corr_w_chi_2(icol,:)         = pdf_params_single%corr_w_chi_2(1,:) 
    2803           0 :     pdf_params_multi%corr_w_eta_1(icol,:)         = pdf_params_single%corr_w_eta_1(1,:) 
    2804           0 :     pdf_params_multi%corr_w_eta_2(icol,:)         = pdf_params_single%corr_w_eta_2(1,:) 
    2805           0 :     pdf_params_multi%corr_chi_eta_1(icol,:)       = pdf_params_single%corr_chi_eta_1(1,:) 
    2806           0 :     pdf_params_multi%corr_chi_eta_2(icol,:)       = pdf_params_single%corr_chi_eta_2(1,:) 
    2807           0 :     pdf_params_multi%rsatl_1(icol,:)              = pdf_params_single%rsatl_1(1,:) 
    2808           0 :     pdf_params_multi%rsatl_2(icol,:)              = pdf_params_single%rsatl_2(1,:) 
    2809           0 :     pdf_params_multi%rc_1(icol,:)                 = pdf_params_single%rc_1(1,:) 
    2810           0 :     pdf_params_multi%rc_2(icol,:)                 = pdf_params_single%rc_2(1,:) 
    2811           0 :     pdf_params_multi%cloud_frac_1(icol,:)         = pdf_params_single%cloud_frac_1(1,:) 
    2812           0 :     pdf_params_multi%cloud_frac_2(icol,:)         = pdf_params_single%cloud_frac_2(1,:) 
    2813           0 :     pdf_params_multi%mixt_frac(icol,:)            = pdf_params_single%mixt_frac(1,:) 
    2814           0 :     pdf_params_multi%ice_supersat_frac_1(icol,:)  = pdf_params_single%ice_supersat_frac_1(1,:) 
    2815           0 :     pdf_params_multi%ice_supersat_frac_2(icol,:)  = pdf_params_single%ice_supersat_frac_2(1,:) 
    2816             :     
    2817           0 :   end subroutine copy_single_pdf_params_to_multi
    2818             :   
    2819             :   !================================================================================================
    2820             :   ! copy_multi_pdf_params_to_single - copies values of a multiple column version of pdf_params
    2821             :   !   at a specified column to a single column version.
    2822             :   !
    2823             :   ! NOTE: THIS SUBROUTINE IS INTENDED TO BE TEMPORARY AND SHOULD BECOME UNNECESSARY ONCE 
    2824             :   !       CLUBB IS ABLE TO OPERATE OVER MULTIPLE COLUMNS.
    2825             :   !       See https://github.com/larson-group/cam/issues/129#issuecomment-827944454
    2826             :   !================================================================================================
    2827           0 :   subroutine copy_multi_pdf_params_to_single( pdf_params_multi, icol, &
    2828             :                                               pdf_params_single )
    2829             :     
    2830             :     implicit none
    2831             : 
    2832             :     ! Input Variable(s)
    2833             :     integer, intent(in) :: &
    2834             :       icol   ! Column number to copy to
    2835             :       
    2836             :     type(pdf_parameter), intent(in) :: &
    2837             :       pdf_params_multi  ! PDF parameters            [units vary]
    2838             : 
    2839             :     ! Output Variable(s)
    2840             :     type(pdf_parameter), intent(inout) :: &
    2841             :       pdf_params_single   ! PDF parameters            [units vary]
    2842             :       
    2843           0 :     pdf_params_single%w_1(1,:)                  = pdf_params_multi%w_1(icol,:) 
    2844           0 :     pdf_params_single%w_2(1,:)                  = pdf_params_multi%w_2(icol,:) 
    2845           0 :     pdf_params_single%varnce_w_1(1,:)           = pdf_params_multi%varnce_w_1(icol,:) 
    2846           0 :     pdf_params_single%varnce_w_2(1,:)           = pdf_params_multi%varnce_w_2(icol,:) 
    2847           0 :     pdf_params_single%rt_1(1,:)                 = pdf_params_multi%rt_1(icol,:) 
    2848           0 :     pdf_params_single%rt_2(1,:)                 = pdf_params_multi%rt_2(icol,:) 
    2849           0 :     pdf_params_single%varnce_rt_1(1,:)          = pdf_params_multi%varnce_rt_1(icol,:) 
    2850           0 :     pdf_params_single%varnce_rt_2(1,:)          = pdf_params_multi%varnce_rt_2(icol,:) 
    2851           0 :     pdf_params_single%thl_1(1,:)                = pdf_params_multi%thl_1(icol,:) 
    2852           0 :     pdf_params_single%thl_2(1,:)                = pdf_params_multi%thl_2(icol,:) 
    2853           0 :     pdf_params_single%varnce_thl_1(1,:)         = pdf_params_multi%varnce_thl_1(icol,:) 
    2854           0 :     pdf_params_single%varnce_thl_2(1,:)         = pdf_params_multi%varnce_thl_2(icol,:) 
    2855           0 :     pdf_params_single%corr_w_rt_1(1,:)          = pdf_params_multi%corr_w_rt_1(icol,:) 
    2856           0 :     pdf_params_single%corr_w_rt_2(1,:)          = pdf_params_multi%corr_w_rt_2(icol,:) 
    2857           0 :     pdf_params_single%corr_w_thl_1(1,:)         = pdf_params_multi%corr_w_thl_1(icol,:) 
    2858           0 :     pdf_params_single%corr_w_thl_2(1,:)         = pdf_params_multi%corr_w_thl_2(icol,:) 
    2859           0 :     pdf_params_single%corr_rt_thl_1(1,:)        = pdf_params_multi%corr_rt_thl_1(icol,:) 
    2860           0 :     pdf_params_single%corr_rt_thl_2(1,:)        = pdf_params_multi%corr_rt_thl_2(icol,:) 
    2861           0 :     pdf_params_single%alpha_thl(1,:)            = pdf_params_multi%alpha_thl(icol,:) 
    2862           0 :     pdf_params_single%alpha_rt(1,:)             = pdf_params_multi%alpha_rt(icol,:) 
    2863           0 :     pdf_params_single%crt_1(1,:)                = pdf_params_multi%crt_1(icol,:) 
    2864           0 :     pdf_params_single%crt_2(1,:)                = pdf_params_multi%crt_2(icol,:) 
    2865           0 :     pdf_params_single%cthl_1(1,:)               = pdf_params_multi%cthl_1(icol,:) 
    2866           0 :     pdf_params_single%cthl_2(1,:)               = pdf_params_multi%cthl_2(icol,:) 
    2867           0 :     pdf_params_single%chi_1(1,:)                = pdf_params_multi%chi_1(icol,:) 
    2868           0 :     pdf_params_single%chi_2(1,:)                = pdf_params_multi%chi_2(icol,:) 
    2869           0 :     pdf_params_single%stdev_chi_1(1,:)          = pdf_params_multi%stdev_chi_1(icol,:) 
    2870           0 :     pdf_params_single%stdev_chi_2(1,:)          = pdf_params_multi%stdev_chi_2(icol,:) 
    2871           0 :     pdf_params_single%stdev_eta_1(1,:)          = pdf_params_multi%stdev_eta_1(icol,:) 
    2872           0 :     pdf_params_single%stdev_eta_2(1,:)          = pdf_params_multi%stdev_eta_2(icol,:) 
    2873           0 :     pdf_params_single%covar_chi_eta_1(1,:)      = pdf_params_multi%covar_chi_eta_1(icol,:) 
    2874           0 :     pdf_params_single%covar_chi_eta_2(1,:)      = pdf_params_multi%covar_chi_eta_2(icol,:) 
    2875           0 :     pdf_params_single%corr_w_chi_1(1,:)         = pdf_params_multi%corr_w_chi_1(icol,:) 
    2876           0 :     pdf_params_single%corr_w_chi_2(1,:)         = pdf_params_multi%corr_w_chi_2(icol,:) 
    2877           0 :     pdf_params_single%corr_w_eta_1(1,:)         = pdf_params_multi%corr_w_eta_1(icol,:) 
    2878           0 :     pdf_params_single%corr_w_eta_2(1,:)         = pdf_params_multi%corr_w_eta_2(icol,:) 
    2879           0 :     pdf_params_single%corr_chi_eta_1(1,:)       = pdf_params_multi%corr_chi_eta_1(icol,:) 
    2880           0 :     pdf_params_single%corr_chi_eta_2(1,:)       = pdf_params_multi%corr_chi_eta_2(icol,:) 
    2881           0 :     pdf_params_single%rsatl_1(1,:)              = pdf_params_multi%rsatl_1(icol,:) 
    2882           0 :     pdf_params_single%rsatl_2(1,:)              = pdf_params_multi%rsatl_2(icol,:) 
    2883           0 :     pdf_params_single%rc_1(1,:)                 = pdf_params_multi%rc_1(icol,:) 
    2884           0 :     pdf_params_single%rc_2(1,:)                 = pdf_params_multi%rc_2(icol,:) 
    2885           0 :     pdf_params_single%cloud_frac_1(1,:)         = pdf_params_multi%cloud_frac_1(icol,:) 
    2886           0 :     pdf_params_single%cloud_frac_2(1,:)         = pdf_params_multi%cloud_frac_2(icol,:) 
    2887           0 :     pdf_params_single%mixt_frac(1,:)            = pdf_params_multi%mixt_frac(icol,:) 
    2888           0 :     pdf_params_single%ice_supersat_frac_1(1,:)  = pdf_params_multi%ice_supersat_frac_1(icol,:) 
    2889           0 :     pdf_params_single%ice_supersat_frac_2(1,:)  = pdf_params_multi%ice_supersat_frac_2(icol,:) 
    2890             :     
    2891           0 :   end subroutine copy_multi_pdf_params_to_single
    2892             : 
    2893             :   
    2894             :   !================================================================================================
    2895             :   ! init_precip_fracs - allocates arrays for precip_fracs
    2896             :   !================================================================================================
    2897           0 :   subroutine init_precip_fracs_api( nz, ngrdcol, &
    2898             :                                     precip_fracs )
    2899             : 
    2900             :     use hydromet_pdf_parameter_module, only : init_precip_fracs
    2901             : 
    2902             :     implicit none
    2903             :     
    2904             :     ! Input Variable(s)
    2905             :     integer, intent(in) :: &
    2906             :       nz,     & ! Number of vertical grid levels    [-]
    2907             :       ngrdcol   ! Number of grid columns            [-]
    2908             : 
    2909             :     ! Output Variable
    2910             :     type(precipitation_fractions), intent(out) :: &
    2911             :       precip_fracs    ! Hydrometeor PDF parameters      [units vary]
    2912             : 
    2913             :     call init_precip_fracs( nz, ngrdcol, & ! intent(in)
    2914           0 :                             precip_fracs ) ! intent(out)
    2915             : 
    2916           0 :     return
    2917             : 
    2918             :   end subroutine init_precip_fracs_api
    2919             :   
    2920             :   !================================================================================================
    2921             :   ! init_pdf_implicit_coefs_terms - allocates arrays for the PDF implicit
    2922             :   ! coefficient and explicit terms.
    2923             :   !================================================================================================
    2924        6192 :   subroutine init_pdf_implicit_coefs_terms_api( nz, ngrdcol, sclr_dim, &
    2925             :                                                 pdf_implicit_coefs_terms )
    2926             : 
    2927             :     use pdf_parameter_module, only: &
    2928             :         init_pdf_implicit_coefs_terms    ! Procedure(s)
    2929             : 
    2930             :     implicit none
    2931             : 
    2932             :     ! Input Variables
    2933             :     integer, intent(in) :: &
    2934             :       nz,       & ! Number of vertical grid levels    [-]
    2935             :       ngrdcol,  & ! Number of grid columns            [-]
    2936             :       sclr_dim    ! Number of scalar variables        [-]
    2937             : 
    2938             :     ! Output Variable
    2939             :     type(implicit_coefs_terms), intent(out) :: &
    2940             :       pdf_implicit_coefs_terms    ! Implicit coefs / explicit terms [units vary]
    2941             : 
    2942             :     call init_pdf_implicit_coefs_terms( nz, ngrdcol, sclr_dim, & ! intent(in)
    2943        6192 :                                         pdf_implicit_coefs_terms ) ! intent(out)
    2944             : 
    2945        6192 :   end subroutine init_pdf_implicit_coefs_terms_api
    2946             : 
    2947             :   !================================================================================================
    2948             :   ! setup_pdf_parameters
    2949             :   !================================================================================================
    2950             : 
    2951           0 :   subroutine setup_pdf_parameters_api_single_col( gr, & ! intent(in)
    2952             :     nz, pdf_dim, dt, &                          ! Intent(in)
    2953           0 :     Nc_in_cloud, cloud_frac, Kh_zm, &           ! Intent(in)
    2954           0 :     ice_supersat_frac, hydromet, wphydrometp, & ! Intent(in)
    2955           0 :     corr_array_n_cloud, corr_array_n_below, &   ! Intent(in)
    2956             :     pdf_params, &                               ! Intent(in)
    2957             :     clubb_params, &                             ! Intent(in)
    2958             :     iiPDF_type, &                               ! Intent(in)
    2959             :     l_use_precip_frac, &                        ! Intent(in)
    2960             :     l_predict_upwp_vpwp, &                      ! Intent(in)
    2961             :     l_diagnose_correlations, &                  ! Intent(in)
    2962             :     l_calc_w_corr, &                            ! Intent(in)
    2963             :     l_const_Nc_in_cloud, &                      ! Intent(in)
    2964             :     l_fix_w_chi_eta_correlations, &             ! Intent(in)
    2965             :     stats_metadata, &                           ! Intent(in)
    2966             :     stats_zt, stats_zm, stats_sfc, &            ! intent(inout)
    2967           0 :     hydrometp2, &                               ! Intent(inout)
    2968           0 :     mu_x_1_n, mu_x_2_n, &                       ! Intent(out)
    2969           0 :     sigma_x_1_n, sigma_x_2_n, &                 ! Intent(out)
    2970           0 :     corr_array_1_n, corr_array_2_n, &           ! Intent(out)
    2971           0 :     corr_cholesky_mtx_1, corr_cholesky_mtx_2, & ! Intent(out)
    2972             :     precip_fracs,  &                            ! Intent(out)
    2973           0 :     hydromet_pdf_params )                       ! Intent(out)
    2974             : 
    2975             :     use setup_clubb_pdf_params, only : &
    2976             :         setup_pdf_parameters
    2977             : 
    2978             :     use parameter_indices, only: &
    2979             :         nparams    ! Variable(s)
    2980             : 
    2981             :     use error_code, only : &
    2982             :         err_code, &         ! Error Indicator
    2983             :         clubb_fatal_error   ! Constant
    2984             : 
    2985             :     implicit none
    2986             : 
    2987             :     !----------------------- Input Variables -----------------------
    2988             :     type(grid), target, intent(in) :: gr
    2989             : 
    2990             :     integer, intent(in) :: &
    2991             :       nz,          & ! Number of model vertical grid levels
    2992             :       pdf_dim   ! Number of variables in the correlation array
    2993             : 
    2994             :     real( kind = core_rknd ), intent(in) ::  &
    2995             :       dt    ! Model timestep                                           [s]
    2996             : 
    2997             :     real( kind = core_rknd ), dimension(nz), intent(in) :: &
    2998             :       Nc_in_cloud,       & ! Mean (in-cloud) cloud droplet conc.       [num/kg]
    2999             :       cloud_frac,        & ! Cloud fraction                            [-]
    3000             :       Kh_zm,             & ! Eddy diffusivity coef. on momentum levels [m^2/s]
    3001             :       ice_supersat_frac    ! Ice supersaturation fraction              [-]
    3002             : 
    3003             :     real( kind = core_rknd ), dimension(nz,hydromet_dim), intent(in) :: &
    3004             :       hydromet,    & ! Mean of hydrometeor, hm (overall) (t-levs.) [units]
    3005             :       wphydrometp    ! Covariance < w'h_m' > (momentum levels)     [(m/s)units]
    3006             : 
    3007             :     real( kind = core_rknd ), dimension(pdf_dim,pdf_dim), &
    3008             :       intent(in) :: &
    3009             :       corr_array_n_cloud, & ! Prescribed norm. space corr. array in cloud    [-]
    3010             :       corr_array_n_below    ! Prescribed norm. space corr. array below cloud [-]
    3011             : 
    3012             :     type(pdf_parameter), intent(in) :: &
    3013             :       pdf_params    ! PDF parameters                               [units vary]
    3014             : 
    3015             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
    3016             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
    3017             : 
    3018             :     integer, intent(in) :: &
    3019             :       iiPDF_type    ! Selected option for the two-component normal (double
    3020             :                     ! Gaussian) PDF type to use for the w, rt, and theta-l (or
    3021             :                     ! w, chi, and eta) portion of CLUBB's multivariate,
    3022             :                     ! two-component PDF.
    3023             : 
    3024             :     logical, intent(in) :: &
    3025             :       l_use_precip_frac,            & ! Flag to use precipitation fraction in KK microphysics. The
    3026             :                                       ! precipitation fraction is automatically set to 1 when this
    3027             :                                       ! flag is turned off.
    3028             :       l_predict_upwp_vpwp,          & ! Flag to predict <u'w'> and <v'w'> along with <u> and <v>
    3029             :                                       ! alongside the advancement of <rt>, <w'rt'>, <thl>,
    3030             :                                       ! <wpthlp>, <sclr>, and <w'sclr'> in subroutine
    3031             :                                       ! advance_xm_wpxp.  Otherwise, <u'w'> and <v'w'> are still
    3032             :                                       ! approximated by eddy diffusivity when <u> and <v> are
    3033             :                                       ! advanced in subroutine advance_windm_edsclrm.
    3034             :       l_diagnose_correlations,      & ! Diagnose correlations instead of using fixed ones
    3035             :       l_calc_w_corr,                & ! Calculate the correlations between w and the hydrometeors
    3036             :       l_const_Nc_in_cloud,          & ! Use a constant cloud droplet conc. within cloud (K&K)
    3037             :       l_fix_w_chi_eta_correlations    ! Use a fixed correlation for s and t Mellor(chi/eta)
    3038             : 
    3039             :     type (stats_metadata_type), intent(in) :: &
    3040             :       stats_metadata
    3041             : 
    3042             :     !----------------------- Input/Output Variables -----------------------
    3043             :     type(stats), target, intent(inout) :: &
    3044             :       stats_zt, &
    3045             :       stats_zm, &
    3046             :       stats_sfc
    3047             : 
    3048             :     real( kind = core_rknd ), dimension(nz,hydromet_dim), intent(inout) :: &
    3049             :       hydrometp2    ! Variance of a hydrometeor (overall) (m-levs.)   [units^2]
    3050             : 
    3051             :     !----------------------- Output Variables -----------------------
    3052             :     real( kind = core_rknd ), dimension(nz,pdf_dim), intent(out) :: &
    3053             :       mu_x_1_n,    & ! Mean array (normal space): PDF vars. (comp. 1) [un. vary]
    3054             :       mu_x_2_n,    & ! Mean array (normal space): PDF vars. (comp. 2) [un. vary]
    3055             :       sigma_x_1_n, & ! Std. dev. array (normal space): PDF vars (comp. 1) [u.v.]
    3056             :       sigma_x_2_n    ! Std. dev. array (normal space): PDF vars (comp. 2) [u.v.]
    3057             : 
    3058             :     real( kind = core_rknd ), dimension(nz,pdf_dim,pdf_dim), &
    3059             :       intent(out) :: &
    3060             :       corr_array_1_n, & ! Corr. array (normal space):  PDF vars. (comp. 1)   [-]
    3061             :       corr_array_2_n    ! Corr. array (normal space):  PDF vars. (comp. 2)   [-]
    3062             : 
    3063             :     real( kind = core_rknd ), dimension(nz,pdf_dim,pdf_dim), &
    3064             :       intent(out) :: &
    3065             :       corr_cholesky_mtx_1, & ! Transposed corr. cholesky matrix, 1st comp. [-]
    3066             :       corr_cholesky_mtx_2    ! Transposed corr. cholesky matrix, 2nd comp. [-]
    3067             : 
    3068             :     ! This is only an output, but it contains allocated arrays, so we need to treat it as inout
    3069             :     type(precipitation_fractions), intent(inout) :: &
    3070             :       precip_fracs           ! Precipitation fractions      [-]
    3071             : 
    3072             :     type(hydromet_pdf_parameter), dimension(nz), optional, intent(out) :: &
    3073             :       hydromet_pdf_params    ! Hydrometeor PDF parameters        [units vary]
    3074             :       
    3075             :     !----------------------- Local Variables -----------------------
    3076             :     real( kind = core_rknd ), dimension(1,nz) :: &
    3077           0 :       Nc_in_cloud_col,       & ! Mean (in-cloud) cloud droplet conc.       [num/kg]
    3078           0 :       cloud_frac_col,        & ! Cloud fraction                            [-]
    3079           0 :       Kh_zm_col,             & ! Eddy diffusivity coef. on momentum levels [m^2/s]
    3080           0 :       ice_supersat_frac_col    ! Ice supersaturation fraction              [-]
    3081             : 
    3082             :     real( kind = core_rknd ), dimension(1,nz,hydromet_dim) :: &
    3083           0 :       hydromet_col,    & ! Mean of hydrometeor, hm (overall) (t-levs.) [units]
    3084           0 :       wphydrometp_col    ! Covariance < w'h_m' > (momentum levels)     [(m/s)units]
    3085             :       
    3086             :     ! Input/Output Variables
    3087             :     real( kind = core_rknd ), dimension(1,nz,hydromet_dim) :: &
    3088           0 :       hydrometp2_col    ! Variance of a hydrometeor (overall) (m-levs.)   [units^2]
    3089             : 
    3090             :     ! Output Variables
    3091             :     real( kind = core_rknd ), dimension(1,nz,pdf_dim) :: &
    3092           0 :       mu_x_1_n_col,    & ! Mean array (normal space): PDF vars. (comp. 1) [un. vary]
    3093           0 :       mu_x_2_n_col,    & ! Mean array (normal space): PDF vars. (comp. 2) [un. vary]
    3094           0 :       sigma_x_1_n_col, & ! Std. dev. array (normal space): PDF vars (comp. 1) [u.v.]
    3095           0 :       sigma_x_2_n_col    ! Std. dev. array (normal space): PDF vars (comp. 2) [u.v.]
    3096             : 
    3097             :     real( kind = core_rknd ), dimension(1,nz,pdf_dim,pdf_dim) :: &
    3098           0 :       corr_array_1_n_col, & ! Corr. array (normal space):  PDF vars. (comp. 1)   [-]
    3099           0 :       corr_array_2_n_col    ! Corr. array (normal space):  PDF vars. (comp. 2)   [-]
    3100             : 
    3101             :     real( kind = core_rknd ), dimension(1,nz,pdf_dim,pdf_dim) :: &
    3102           0 :       corr_cholesky_mtx_1_col, & ! Transposed corr. cholesky matrix, 1st comp. [-]
    3103           0 :       corr_cholesky_mtx_2_col    ! Transposed corr. cholesky matrix, 2nd comp. [-]
    3104             : 
    3105             :     type(hydromet_pdf_parameter), dimension(1,nz) :: &
    3106           0 :       hydromet_pdf_params_col    ! Hydrometeor PDF parameters        [units vary]
    3107             :       
    3108             :     type(stats), dimension(1) :: &
    3109           0 :       stats_zt_col, &
    3110           0 :       stats_zm_col, &
    3111           0 :       stats_sfc_col
    3112             : 
    3113             :     !----------------------- Begin Code -----------------------
    3114             : 
    3115           0 :     Nc_in_cloud_col(1,:) = Nc_in_cloud
    3116           0 :     cloud_frac_col(1,:) = cloud_frac
    3117           0 :     Kh_zm_col(1,:) = Kh_zm
    3118           0 :     ice_supersat_frac_col(1,:) = ice_supersat_frac
    3119             :     
    3120           0 :     hydromet_col(1,:,:) = hydromet
    3121           0 :     wphydrometp_col(1,:,:) = wphydrometp
    3122             :     
    3123           0 :     stats_zt_col(1) = stats_zt
    3124           0 :     stats_zm_col(1) = stats_zm
    3125           0 :     stats_sfc_col(1) = stats_sfc
    3126             : 
    3127             :     call setup_pdf_parameters( gr, &                          ! intent(in)
    3128             :       nz, 1, pdf_dim, dt, &                                   ! Intent(in)
    3129             :       Nc_in_cloud_col, cloud_frac_col, Kh_zm_col, &           ! Intent(in)
    3130             :       ice_supersat_frac_col, hydromet_col, wphydrometp_col, & ! Intent(in)
    3131             :       corr_array_n_cloud, corr_array_n_below, &               ! Intent(in)
    3132             :       pdf_params, &                                           ! Intent(in)
    3133             :       clubb_params, &                                         ! Intent(in)
    3134             :       iiPDF_type, &                                           ! Intent(in)
    3135             :       l_use_precip_frac, &                                    ! Intent(in)
    3136             :       l_predict_upwp_vpwp, &                                  ! Intent(in)
    3137             :       l_diagnose_correlations, &                              ! Intent(in)
    3138             :       l_calc_w_corr, &                                        ! Intent(in)
    3139             :       l_const_Nc_in_cloud, &                                  ! Intent(in)
    3140             :       l_fix_w_chi_eta_correlations, &                         ! Intent(in)
    3141             :       stats_metadata, &                                       ! Intent(in)
    3142             :       stats_zt_col, stats_zm_col, stats_sfc_col, &            ! Intent(inout)
    3143             :       hydrometp2_col, &                                       ! Intent(inout)
    3144             :       mu_x_1_n_col, mu_x_2_n_col, &                           ! Intent(out)
    3145             :       sigma_x_1_n_col, sigma_x_2_n_col, &                     ! Intent(out)
    3146             :       corr_array_1_n_col, corr_array_2_n_col, &               ! Intent(out)
    3147             :       corr_cholesky_mtx_1_col, corr_cholesky_mtx_2_col, &     ! Intent(out)
    3148             :       precip_fracs, &                                         ! Intent(inout)
    3149           0 :       hydromet_pdf_params_col )                               ! Optional(out)
    3150             : 
    3151           0 :     if ( err_code == clubb_fatal_error ) error stop
    3152             :     
    3153             :     ! The following does not work for stats 
    3154             :     !     stats_zt = stats_zt_col(1)
    3155             :     !     stats_zm = stats_zm_col(1) 
    3156             :     !     stats_sfc = stats_sfc_col(1)
    3157             :     ! because of some mysterious pointer issue. However, the only thing that 
    3158             :     ! updates in stats is the field values, so we can copy only those instead.
    3159           0 :     if ( stats_metadata%l_stats ) then 
    3160           0 :       stats_zm%accum_field_values = stats_zm_col(1)%accum_field_values
    3161           0 :       stats_zm%accum_num_samples = stats_zm_col(1)%accum_num_samples
    3162             :       
    3163           0 :       stats_zt%accum_field_values = stats_zt_col(1)%accum_field_values
    3164           0 :       stats_zt%accum_num_samples = stats_zt_col(1)%accum_num_samples
    3165             :       
    3166           0 :       stats_sfc%accum_field_values = stats_sfc_col(1)%accum_field_values
    3167           0 :       stats_sfc%accum_num_samples = stats_sfc_col(1)%accum_num_samples
    3168             :     end if
    3169             :     
    3170           0 :     hydrometp2 = hydrometp2_col(1,:,:)
    3171           0 :     mu_x_1_n = mu_x_1_n_col(1,:,:)
    3172           0 :     mu_x_2_n = mu_x_2_n_col(1,:,:)
    3173           0 :     sigma_x_1_n = sigma_x_1_n_col(1,:,:)
    3174           0 :     sigma_x_2_n = sigma_x_2_n_col(1,:,:)
    3175           0 :     corr_array_1_n = corr_array_1_n_col(1,:,:,:)
    3176           0 :     corr_array_2_n = corr_array_2_n_col(1,:,:,:)
    3177           0 :     corr_cholesky_mtx_1 = corr_cholesky_mtx_1_col(1,:,:,:)
    3178           0 :     corr_cholesky_mtx_2 = corr_cholesky_mtx_2_col(1,:,:,:)
    3179           0 :     if ( present(hydromet_pdf_params) ) then
    3180           0 :       hydromet_pdf_params = hydromet_pdf_params_col(1,:)
    3181             :     end if
    3182             : 
    3183           0 :   end subroutine setup_pdf_parameters_api_single_col
    3184             : !===========================================================================! 
    3185           0 :   subroutine setup_pdf_parameters_api_multi_col( gr, &
    3186             :     nz, ngrdcol, pdf_dim, dt, &                 ! Intent(in)
    3187           0 :     Nc_in_cloud, cloud_frac, Kh_zm, &           ! Intent(in)
    3188           0 :     ice_supersat_frac, hydromet, wphydrometp, & ! Intent(in)
    3189           0 :     corr_array_n_cloud, corr_array_n_below, &   ! Intent(in)
    3190             :     pdf_params, &                               ! Intent(in)
    3191             :     clubb_params, &                             ! Intent(in)
    3192             :     iiPDF_type, &                               ! Intent(in)
    3193             :     l_use_precip_frac, &                        ! Intent(in)
    3194             :     l_predict_upwp_vpwp, &                      ! Intent(in)
    3195             :     l_diagnose_correlations, &                  ! Intent(in)
    3196             :     l_calc_w_corr, &                            ! Intent(in)
    3197             :     l_const_Nc_in_cloud, &                      ! Intent(in)
    3198             :     l_fix_w_chi_eta_correlations, &             ! Intent(in)
    3199             :     stats_metadata, &                           ! Intent(in)
    3200           0 :     stats_zt, stats_zm, stats_sfc, &            ! Intent(inout)
    3201           0 :     hydrometp2, &                               ! Intent(inout)
    3202           0 :     mu_x_1_n, mu_x_2_n, &                       ! Intent(out)
    3203           0 :     sigma_x_1_n, sigma_x_2_n, &                 ! Intent(out)
    3204           0 :     corr_array_1_n, corr_array_2_n, &           ! Intent(out)
    3205           0 :     corr_cholesky_mtx_1, corr_cholesky_mtx_2, & ! Intent(out)
    3206             :     precip_fracs, &                             ! Intent(out)
    3207           0 :     hydromet_pdf_params )                       ! Intent(out)
    3208             : 
    3209             :     use setup_clubb_pdf_params, only : setup_pdf_parameters
    3210             : 
    3211             :     use parameter_indices, only: &
    3212             :         nparams    ! Variable(s)
    3213             : 
    3214             :     use error_code, only : &
    3215             :         err_code, &         ! Error Indicator
    3216             :         clubb_fatal_error   ! Constant
    3217             : 
    3218             :     implicit none
    3219             : 
    3220             :     ! Input Variables
    3221             :     integer, intent(in) :: &
    3222             :       nz,          & ! Number of model vertical grid levels
    3223             :       pdf_dim,     & ! Number of variables in the correlation array
    3224             :       ngrdcol        ! Number of grid columns
    3225             :       
    3226             :     type (grid), target, intent(in) :: gr
    3227             : 
    3228             :     real( kind = core_rknd ), intent(in) ::  &
    3229             :       dt    ! Model timestep                                           [s]
    3230             : 
    3231             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(in) :: &
    3232             :       Nc_in_cloud,       & ! Mean (in-cloud) cloud droplet conc.       [num/kg]
    3233             :       cloud_frac,        & ! Cloud fraction                            [-]
    3234             :       Kh_zm,             & ! Eddy diffusivity coef. on momentum levels [m^2/s]
    3235             :       ice_supersat_frac    ! Ice supersaturation fraction              [-]
    3236             : 
    3237             :     real( kind = core_rknd ), dimension(ngrdcol,nz,hydromet_dim), intent(in) :: &
    3238             :       hydromet,    & ! Mean of hydrometeor, hm (overall) (t-levs.) [units]
    3239             :       wphydrometp    ! Covariance < w'h_m' > (momentum levels)     [(m/s)units]
    3240             : 
    3241             :     real( kind = core_rknd ), dimension(pdf_dim,pdf_dim), &
    3242             :       intent(in) :: &
    3243             :       corr_array_n_cloud, & ! Prescribed norm. space corr. array in cloud    [-]
    3244             :       corr_array_n_below    ! Prescribed norm. space corr. array below cloud [-]
    3245             : 
    3246             :     type(pdf_parameter), intent(in) :: &
    3247             :       pdf_params    ! PDF parameters                               [units vary]
    3248             : 
    3249             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
    3250             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
    3251             : 
    3252             :     integer, intent(in) :: &
    3253             :       iiPDF_type    ! Selected option for the two-component normal (double
    3254             :                     ! Gaussian) PDF type to use for the w, rt, and theta-l (or
    3255             :                     ! w, chi, and eta) portion of CLUBB's multivariate,
    3256             :                     ! two-component PDF.
    3257             : 
    3258             :     logical, intent(in) :: &
    3259             :       l_use_precip_frac,            & ! Flag to use precipitation fraction in KK microphysics. The
    3260             :                                       ! precipitation fraction is automatically set to 1 when this
    3261             :                                       ! flag is turned off.
    3262             :       l_predict_upwp_vpwp,          & ! Flag to predict <u'w'> and <v'w'> along with <u> and <v>
    3263             :                                       ! alongside the advancement of <rt>, <w'rt'>, <thl>,
    3264             :                                       ! <wpthlp>, <sclr>, and <w'sclr'> in subroutine
    3265             :                                       ! advance_xm_wpxp.  Otherwise, <u'w'> and <v'w'> are still
    3266             :                                       ! approximated by eddy diffusivity when <u> and <v> are
    3267             :                                       ! advanced in subroutine advance_windm_edsclrm.
    3268             :       l_diagnose_correlations,      & ! Diagnose correlations instead of using fixed ones
    3269             :       l_calc_w_corr,                & ! Calculate the correlations between w and the hydrometeors
    3270             :       l_const_Nc_in_cloud,          & ! Use a constant cloud droplet conc. within cloud (K&K)
    3271             :       l_fix_w_chi_eta_correlations    ! Use a fixed correlation for s and t Mellor(chi/eta)
    3272             : 
    3273             :     type (stats_metadata_type), intent(in) :: &
    3274             :       stats_metadata
    3275             : 
    3276             :     ! Input/Output Variables
    3277             :     real( kind = core_rknd ), dimension(ngrdcol,nz,hydromet_dim), intent(inout) :: &
    3278             :       hydrometp2    ! Variance of a hydrometeor (overall) (m-levs.)   [units^2]
    3279             : 
    3280             :     type(stats), target, dimension(ngrdcol), intent(inout) :: &
    3281             :       stats_zt, &
    3282             :       stats_zm, &
    3283             :       stats_sfc
    3284             : 
    3285             :     ! Output Variables
    3286             :     real( kind = core_rknd ), dimension(ngrdcol,nz,pdf_dim), intent(out) :: &
    3287             :       mu_x_1_n,    & ! Mean array (normal space): PDF vars. (comp. 1) [un. vary]
    3288             :       mu_x_2_n,    & ! Mean array (normal space): PDF vars. (comp. 2) [un. vary]
    3289             :       sigma_x_1_n, & ! Std. dev. array (normal space): PDF vars (comp. 1) [u.v.]
    3290             :       sigma_x_2_n    ! Std. dev. array (normal space): PDF vars (comp. 2) [u.v.]
    3291             : 
    3292             :     real( kind = core_rknd ), dimension(ngrdcol,nz,pdf_dim,pdf_dim), &
    3293             :       intent(out) :: &
    3294             :       corr_array_1_n, & ! Corr. array (normal space):  PDF vars. (comp. 1)   [-]
    3295             :       corr_array_2_n    ! Corr. array (normal space):  PDF vars. (comp. 2)   [-]
    3296             : 
    3297             :     real( kind = core_rknd ), dimension(ngrdcol,nz,pdf_dim,pdf_dim), &
    3298             :       intent(out) :: &
    3299             :       corr_cholesky_mtx_1, & ! Transposed corr. cholesky matrix, 1st comp. [-]
    3300             :       corr_cholesky_mtx_2    ! Transposed corr. cholesky matrix, 2nd comp. [-]
    3301             : 
    3302             :     type(hydromet_pdf_parameter), dimension(ngrdcol,nz), optional, intent(out) :: &
    3303             :       hydromet_pdf_params    ! Hydrometeor PDF parameters        [units vary]
    3304             :       
    3305             :     ! This is only an output, but it contains allocated arrays, so we need to treat it as inout
    3306             :     type(precipitation_fractions), intent(inout) :: &
    3307             :       precip_fracs           ! Precipitation fractions      [-]
    3308             : 
    3309             :     call setup_pdf_parameters( gr, &              ! intent(in)
    3310             :       nz, ngrdcol, pdf_dim, dt, &                 ! Intent(in)
    3311             :       Nc_in_cloud, cloud_frac, Kh_zm, &           ! Intent(in)
    3312             :       ice_supersat_frac, hydromet, wphydrometp, & ! Intent(in)
    3313             :       corr_array_n_cloud, corr_array_n_below, &   ! Intent(in)
    3314             :       pdf_params, &                               ! Intent(in)
    3315             :       clubb_params, &                             ! Intent(in)
    3316             :       iiPDF_type, &                               ! Intent(in)
    3317             :       l_use_precip_frac, &                        ! Intent(in)
    3318             :       l_predict_upwp_vpwp, &                      ! Intent(in)
    3319             :       l_diagnose_correlations, &                  ! Intent(in)
    3320             :       l_calc_w_corr, &                            ! Intent(in)
    3321             :       l_const_Nc_in_cloud, &                      ! Intent(in)
    3322             :       l_fix_w_chi_eta_correlations, &             ! Intent(in)
    3323             :       stats_metadata, &                           ! Intent(in)
    3324             :       stats_zt, stats_zm, stats_sfc, &            ! intent(inout)
    3325             :       hydrometp2, &                               ! Intent(inout)
    3326             :       mu_x_1_n, mu_x_2_n, &                       ! Intent(out)
    3327             :       sigma_x_1_n, sigma_x_2_n, &                 ! Intent(out)
    3328             :       corr_array_1_n, corr_array_2_n, &           ! Intent(out)
    3329             :       corr_cholesky_mtx_1, corr_cholesky_mtx_2, & ! Intent(out)
    3330             :       precip_fracs, &                             ! Intent(inout)
    3331           0 :       hydromet_pdf_params )                       ! Optional(out)
    3332             : 
    3333           0 :     if ( err_code == clubb_fatal_error ) error stop
    3334             : 
    3335           0 :   end subroutine setup_pdf_parameters_api_multi_col
    3336             : 
    3337             :   !================================================================================================
    3338             :   ! stats_init - Initializes the statistics saving functionality of the CLUBB model.
    3339             :   !================================================================================================
    3340             : 
    3341           0 :   subroutine stats_init_api( &
    3342             :     iunit, fname_prefix, fdir, l_stats_in, &
    3343             :     stats_fmt_in, stats_tsamp_in, stats_tout_in, fnamelist, &
    3344           0 :     nzmax, nlon, nlat, gzt, gzm, nnrad_zt, &
    3345           0 :     grad_zt, nnrad_zm, grad_zm, day, month, year, &
    3346           0 :     lon_vals, lat_vals, time_current, delt, l_silhs_out_in, &
    3347             :     stats_metadata, &
    3348             :     stats_zt, stats_zm, stats_sfc, &
    3349             :     stats_lh_zt, stats_lh_sfc, &
    3350             :     stats_rad_zt, stats_rad_zm )
    3351             : 
    3352             :     use stats_clubb_utilities, only : &
    3353             :       stats_init
    3354             : 
    3355             :     implicit none
    3356             : 
    3357             :     ! Input Variables
    3358             :     integer, intent(in) :: iunit  ! File unit for fnamelist
    3359             : 
    3360             :     character(len=*), intent(in) ::  &
    3361             :       fname_prefix, & ! Start of the stats filenames
    3362             :       fdir            ! Directory to output to
    3363             : 
    3364             :     logical, intent(in) :: &
    3365             :       l_stats_in      ! Stats on? T/F
    3366             : 
    3367             :     character(len=*), intent(in) :: &
    3368             :       stats_fmt_in    ! Format of the stats file output
    3369             : 
    3370             :     real( kind = core_rknd ), intent(in) ::  &
    3371             :       stats_tsamp_in,  & ! Sampling interval   [s]
    3372             :       stats_tout_in      ! Output interval     [s]
    3373             : 
    3374             :     character(len=*), intent(in) :: &
    3375             :       fnamelist          ! Filename holding the &statsnl
    3376             : 
    3377             :     integer, intent(in) :: &
    3378             :       nlon, & ! Number of points in the X direction [-]
    3379             :       nlat, & ! Number of points in the Y direction [-]
    3380             :       nzmax   ! Grid points in the vertical         [-]
    3381             : 
    3382             :     real( kind = core_rknd ), intent(in), dimension(nzmax) ::  &
    3383             :       gzt, gzm  ! Thermodynamic and momentum levels           [m]
    3384             : 
    3385             :     integer, intent(in) :: nnrad_zt ! Grid points in the radiation grid [count]
    3386             : 
    3387             :     real( kind = core_rknd ), intent(in), dimension(nnrad_zt) :: grad_zt ! Radiation levels [m]
    3388             : 
    3389             :     integer, intent(in) :: nnrad_zm ! Grid points in the radiation grid [count]
    3390             : 
    3391             :     real( kind = core_rknd ), intent(in), dimension(nnrad_zm) :: grad_zm ! Radiation levels [m]
    3392             : 
    3393             :     integer, intent(in) :: day, month, year  ! Time of year
    3394             : 
    3395             :     real( kind = core_rknd ), dimension(nlon), intent(in) ::  &
    3396             :       lon_vals  ! Longitude values [Degrees E]
    3397             : 
    3398             :     real( kind = core_rknd ), dimension(nlat), intent(in) ::  &
    3399             :       lat_vals  ! Latitude values  [Degrees N]
    3400             : 
    3401             :     real( kind = time_precision ), intent(in) ::  &
    3402             :       time_current ! Model time                         [s]
    3403             : 
    3404             :     real( kind = core_rknd ), intent(in) ::  &
    3405             :       delt         ! Timestep (dt_main in CLUBB)         [s]
    3406             : 
    3407             :     logical, intent(in) :: &
    3408             :       l_silhs_out_in  ! Whether to output SILHS files (stats_lh_zt,stats_lh_sfc) [dimensionless]
    3409             : 
    3410             :     type (stats_metadata_type), intent(inout) :: &
    3411             :       stats_metadata
    3412             : 
    3413             :     type(stats), target, intent(inout) :: &
    3414             :       stats_zt, &
    3415             :       stats_zm, &
    3416             :       stats_sfc, &
    3417             :       stats_lh_zt, &
    3418             :       stats_lh_sfc, &
    3419             :       stats_rad_zt, &
    3420             :       stats_rad_zm
    3421             : 
    3422             :     call stats_init( &
    3423             :       iunit, fname_prefix, fdir, l_stats_in, & ! intent(in)
    3424             :       stats_fmt_in, stats_tsamp_in, stats_tout_in, fnamelist, & ! intent(in)
    3425             :       nzmax, nlon, nlat, gzt, gzm, nnrad_zt, & ! intent(in)
    3426             :       grad_zt, nnrad_zm, grad_zm, day, month, year, & ! intent(in)
    3427             :       lon_vals, lat_vals, time_current, delt, l_silhs_out_in, & ! intent(in)
    3428             :       stats_metadata, & ! intent(inout)
    3429             :       stats_zt, stats_zm, stats_sfc, & ! intent(inout)
    3430             :       stats_lh_zt, stats_lh_sfc, & ! intent(inout)
    3431           0 :       stats_rad_zt, stats_rad_zm ) ! intent(inout)
    3432             : 
    3433           0 :     if ( err_code == clubb_fatal_error ) error stop
    3434             :     
    3435           0 :   end subroutine stats_init_api
    3436             : 
    3437             :   !================================================================================================
    3438             :   ! stats_end_timestep - Calls statistics to be written to the output format.
    3439             :   !================================================================================================
    3440             : 
    3441           0 :   subroutine stats_end_timestep_api( clubb_params, stats_metadata, &
    3442             :                                      stats_zt, stats_zm, stats_sfc, &
    3443             :                                      stats_lh_zt, stats_lh_sfc, &
    3444             :                                      stats_rad_zt, stats_rad_zm &
    3445             : #ifdef NETCDF
    3446             :                                      , l_uv_nudge, &
    3447             :                                      l_tke_aniso, &
    3448             :                                      l_standard_term_ta &
    3449             : #endif
    3450             :                                       )
    3451             : 
    3452             :     use stats_clubb_utilities, only : stats_end_timestep
    3453             : 
    3454             :     use parameter_indices, only: nparams
    3455             : 
    3456             :     implicit none
    3457             : 
    3458             :     ! Input Variables
    3459             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
    3460             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
    3461             : 
    3462             :     type (stats_metadata_type), intent(in) :: &
    3463             :       stats_metadata
    3464             : 
    3465             :     type(stats), target, intent(inout) :: &
    3466             :       stats_zt, &
    3467             :       stats_zm, &
    3468             :       stats_sfc, &
    3469             :       stats_lh_zt, &
    3470             :       stats_lh_sfc, &
    3471             :       stats_rad_zt, &
    3472             :       stats_rad_zm
    3473             : 
    3474             : #ifdef NETCDF
    3475             :     logical, intent(in) :: &
    3476             :       l_uv_nudge,         & ! For wind speed nudging.
    3477             :       l_tke_aniso,        & ! For anisotropic turbulent kinetic energy, i.e.
    3478             :                             ! TKE = 1/2 (u'^2 + v'^2 + w'^2)
    3479             :       l_standard_term_ta    ! Use the standard discretization for the turbulent advection terms.
    3480             :                             ! Setting to .false. means that a_1 and a_3 are pulled outside of the
    3481             :                             ! derivative in advance_wp2_wp3_module.F90 and in
    3482             :                             ! advance_xp2_xpyp_module.F90.
    3483             : #endif
    3484             : 
    3485             :     call stats_end_timestep( clubb_params, stats_metadata,  & ! intent(in)
    3486             :                              stats_zt, stats_zm, stats_sfc, & ! intent(inout)
    3487             :                              stats_lh_zt, stats_lh_sfc,     & ! intent(inout)
    3488             :                              stats_rad_zt, stats_rad_zm     & ! intent(inout)
    3489             : #ifdef NETCDF
    3490             :                              , l_uv_nudge,                  & ! Intent(in)
    3491             :                              l_tke_aniso,                   & ! Intent(in)
    3492             :                              l_standard_term_ta             & ! Intent(in)
    3493             : #endif
    3494           0 :                               )
    3495             : 
    3496           0 :     if ( err_code == clubb_fatal_error ) error stop
    3497             : 
    3498           0 :   end subroutine stats_end_timestep_api
    3499             : 
    3500             :   !================================================================================================
    3501             :   ! stats_accumulate_hydromet - Computes stats related the hydrometeors.
    3502             :   !================================================================================================
    3503             : 
    3504           0 :   subroutine stats_accumulate_hydromet_api( gr, &
    3505           0 :                                             hydromet, rho_ds_zt, &
    3506             :                                             stats_metadata, &
    3507             :                                             stats_zt, stats_sfc )
    3508             : 
    3509             :     use stats_clubb_utilities, only : &
    3510             :       stats_accumulate_hydromet
    3511             : 
    3512             :     implicit none
    3513             : 
    3514             :     type(grid), target, intent(in) :: &
    3515             :       gr
    3516             : 
    3517             :     type (stats_metadata_type), intent(in) :: &
    3518             :       stats_metadata
    3519             : 
    3520             :     type(stats), target, intent(inout) :: &
    3521             :       stats_zt, &
    3522             :       stats_sfc
    3523             : 
    3524             :     ! Input Variables
    3525             :     real( kind = core_rknd ), dimension(gr%nz,hydromet_dim), intent(in) :: &
    3526             :       hydromet ! All hydrometeors except for rcm        [units vary]
    3527             : 
    3528             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
    3529             :       rho_ds_zt ! Dry, static density (thermo. levs.)      [kg/m^3]
    3530             : 
    3531             :     call stats_accumulate_hydromet( gr, & ! intent(in)
    3532             :       hydromet, rho_ds_zt, & ! intent(in)
    3533             :       stats_metadata, & ! intent(in) 
    3534           0 :       stats_zt, stats_sfc ) ! intent(inout)
    3535             : 
    3536           0 :   end subroutine stats_accumulate_hydromet_api
    3537             : 
    3538             :   !================================================================================================
    3539             :   ! stats_finalize - Close NetCDF files and deallocate scratch space and stats file structures.
    3540             :   !================================================================================================
    3541             : 
    3542           0 :   subroutine stats_finalize_api ( stats_metadata, &
    3543             :                                   stats_zt, stats_zm, stats_sfc, &
    3544             :                                   stats_lh_zt, stats_lh_sfc, &
    3545             :                                   stats_rad_zt, stats_rad_zm )
    3546             : 
    3547             :     use stats_clubb_utilities, only : &
    3548             :       stats_finalize
    3549             : 
    3550             :     implicit none
    3551             : 
    3552             :     type(stats), target, intent(inout) :: &
    3553             :       stats_zt, &
    3554             :       stats_zm, &
    3555             :       stats_sfc, &
    3556             :       stats_lh_zt, &
    3557             :       stats_lh_sfc, &
    3558             :       stats_rad_zt, &
    3559             :       stats_rad_zm
    3560             : 
    3561             :     type (stats_metadata_type), intent(inout) :: &
    3562             :       stats_metadata
    3563             : 
    3564             :     call stats_finalize ( stats_metadata, & ! intent(inout)
    3565             :                           stats_zt, stats_zm, stats_sfc, & ! intent(inout)
    3566             :                           stats_lh_zt, stats_lh_sfc, & ! intent(inout)
    3567           0 :                           stats_rad_zt, stats_rad_zm ) ! intent(inout)
    3568             : 
    3569           0 :   end subroutine stats_finalize_api
    3570             : 
    3571             : 
    3572             :   !================================================================================================
    3573             :   ! calculate_spurious_source - Checks whether there is conservation within the column.
    3574             :   !================================================================================================
    3575           0 :   function calculate_spurious_source_api ( &
    3576             :     integral_after, integral_before, &
    3577             :     flux_top, flux_sfc, &
    3578             :     integral_forcing, dt ) result( spurious_source )
    3579             : 
    3580             :     use numerical_check, only : calculate_spurious_source
    3581             : 
    3582             :     implicit none
    3583             : 
    3584             :     ! Input Variables
    3585             :     real( kind = core_rknd ), intent(in) :: &
    3586             :       integral_after, &   ! Vertically-integrated quantity after dt time  [units vary]
    3587             :       integral_before, &  ! Vertically-integrated quantity before dt time [units vary]
    3588             :       flux_top, &         ! Total flux at the top of the domain           [units vary]
    3589             :       flux_sfc, &         ! Total flux at the bottom of the domain        [units vary]
    3590             :       integral_forcing, & ! Vertically-integrated forcing                 [units vary]
    3591             :       dt                  ! Timestep size                                 [s]
    3592             : 
    3593             :     ! Return Variable
    3594             :     real( kind = core_rknd ) :: spurious_source ! [units vary]
    3595             : 
    3596             :     spurious_source = calculate_spurious_source( &
    3597             :       integral_after, integral_before, &
    3598             :       flux_top, flux_sfc, &
    3599           0 :       integral_forcing, dt )
    3600             : 
    3601           0 :   end function calculate_spurious_source_api
    3602             : 
    3603             :   !================================================================================================
    3604             :   ! calculate_thlp2_rad - Computes the contribution of radiative cooling to thlp2
    3605             :   !================================================================================================
    3606           0 :   subroutine calculate_thlp2_rad_api &
    3607           0 :                   ( nz, rcm_zm, thlprcp, radht_zm, & ! Intent(in)
    3608             :                     clubb_params,                  & ! Intent(in)
    3609           0 :                     thlp2_forcing )                  ! Intent(inout)
    3610             : 
    3611             :     use clubb_precision, only: &
    3612             :         core_rknd                     ! Constant(s)
    3613             : 
    3614             :     use advance_clubb_core_module, only: &
    3615             :         calculate_thlp2_rad
    3616             : 
    3617             :     use parameter_indices, only: &
    3618             :         nparams
    3619             : 
    3620             :     implicit none
    3621             : 
    3622             :   ! Input Variables
    3623             :     integer, intent(in) :: &
    3624             :       nz                    ! Number of vertical levels                      [-]
    3625             : 
    3626             :     real( kind = core_rknd ), dimension(nz), intent(in) :: &
    3627             :       rcm_zm, &             ! Cloud water mixing ratio on momentum grid      [kg/kg]
    3628             :       thlprcp, &            ! thl'rc'                                        [K kg/kg]
    3629             :       radht_zm              ! SW + LW heating rate (on momentum grid)        [K/s]
    3630             : 
    3631             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
    3632             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
    3633             : 
    3634             :   ! Input/Output Variables
    3635             :     real( kind = core_rknd ), dimension(nz), intent(inout) :: &
    3636             :       thlp2_forcing         ! <th_l'^2> forcing (momentum levels)            [K^2/s]
    3637             :   !----------------------------------------------------------------------
    3638             : 
    3639             :     call calculate_thlp2_rad( nz, rcm_zm, thlprcp, radht_zm, & ! intent(in)
    3640             :                               clubb_params,                  & ! intent(in)
    3641           0 :                               thlp2_forcing )                  ! intent(inout)
    3642             : 
    3643           0 :     return
    3644             :   end subroutine calculate_thlp2_rad_api
    3645             : 
    3646             :   !================================================================================================
    3647             :   ! update_xp2_mc - Calculates the effects of rain evaporation on rtp2 and thlp2
    3648             :   !================================================================================================
    3649           0 :   subroutine update_xp2_mc_api_multi_col( gr, nz, ngrdcol, dt, cloud_frac, rcm, rvm, thlm, &
    3650           0 :                                           wm, exner, rrm_evap, pdf_params,        &
    3651           0 :                                           rtp2_mc, thlp2_mc, wprtp_mc, wpthlp_mc,    &
    3652           0 :                                           rtpthlp_mc )
    3653             : 
    3654             :     use advance_xp2_xpyp_module, only: &
    3655             :         update_xp2_mc
    3656             : 
    3657             :      ! Type
    3658             : 
    3659             :     implicit none
    3660             : 
    3661             :     ! -------------------- Input Variables --------------------
    3662             :     integer, intent(in) :: &
    3663             :       nz, & ! Points in the Vertical        [-]
    3664             :       ngrdcol
    3665             : 
    3666             :     type(grid), target, intent(in) :: gr
    3667             : 
    3668             :     real( kind = core_rknd ), intent(in) :: dt ! Model timestep        [s]
    3669             : 
    3670             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(in) :: &
    3671             :       cloud_frac, &       !Cloud fraction                        [-]
    3672             :       rcm, &              !Cloud water mixing ratio              [kg/kg]
    3673             :       rvm, &              !Vapor water mixing ratio              [kg/kg]
    3674             :       thlm, &             !Liquid potential temperature          [K]
    3675             :       wm, &               !Mean vertical velocity                [m/s]
    3676             :       exner, &            !Exner function                        [-]
    3677             :       rrm_evap         !Evaporation of rain                   [kg/kg/s]
    3678             :                           !It is expected that this variable is negative, as
    3679             :                           !that is the convention in Morrison microphysics
    3680             : 
    3681             :     type(pdf_parameter), intent(in) :: &
    3682             :       pdf_params ! PDF parameters
    3683             : 
    3684             :     ! -------------------- Input/Output Variables --------------------
    3685             :     real( kind = core_rknd ), dimension(ngrdcol,nz), intent(inout) :: &
    3686             :       rtp2_mc, &    !Tendency of <rt'^2> due to evaporation   [(kg/kg)^2/s]
    3687             :       thlp2_mc, &   !Tendency of <thl'^2> due to evaporation  [K^2/s]
    3688             :       wprtp_mc, &   !Tendency of <w'rt'> due to evaporation   [m*(kg/kg)/s^2]
    3689             :       wpthlp_mc, &  !Tendency of <w'thl'> due to evaporation  [m*K/s^2] 
    3690             :       rtpthlp_mc    !Tendency of <rt'thl'> due to evaporation [K*(kg/kg)/s]
    3691             :       
    3692             :     call update_xp2_mc( gr, nz, ngrdcol, dt, cloud_frac, rcm, rvm, thlm,        & ! intent(in)
    3693             :                         wm, exner, rrm_evap, pdf_params,        & ! intent(in)
    3694             :                         rtp2_mc, thlp2_mc, wprtp_mc, wpthlp_mc,    & ! intent(inout)
    3695           0 :                         rtpthlp_mc ) ! intent(inout)
    3696           0 :     return
    3697             :   end subroutine update_xp2_mc_api_multi_col
    3698             :   
    3699             :   !================================================================================================
    3700             :   ! update_xp2_mc - Calculates the effects of rain evaporation on rtp2 and thlp2
    3701             :   !================================================================================================
    3702           0 :   subroutine update_xp2_mc_api_single_col( gr, nz, dt, cloud_frac, rcm, rvm, thlm, &
    3703           0 :                                            wm, exner, rrm_evap, pdf_params,        &
    3704           0 :                                            rtp2_mc, thlp2_mc, wprtp_mc, wpthlp_mc,    &
    3705           0 :                                            rtpthlp_mc )
    3706             : 
    3707             :     use advance_xp2_xpyp_module, only: &
    3708             :         update_xp2_mc
    3709             : 
    3710             :      ! Type
    3711             : 
    3712             :     implicit none
    3713             : 
    3714             :     ! -------------------- Input Variables --------------------
    3715             :     integer, intent(in) :: &
    3716             :       nz ! Points in the Vertical        [-]
    3717             : 
    3718             :     type(grid), target, intent(in) :: gr
    3719             : 
    3720             :     real( kind = core_rknd ), intent(in) :: dt ! Model timestep        [s]
    3721             : 
    3722             :     real( kind = core_rknd ), dimension(nz), intent(in) :: &
    3723             :       cloud_frac, &       !Cloud fraction                        [-]
    3724             :       rcm, &              !Cloud water mixing ratio              [kg/kg]
    3725             :       rvm, &              !Vapor water mixing ratio              [kg/kg]
    3726             :       thlm, &             !Liquid potential temperature          [K]
    3727             :       wm, &               !Mean vertical velocity                [m/s]
    3728             :       exner, &            !Exner function                        [-]
    3729             :       rrm_evap         !Evaporation of rain                   [kg/kg/s]
    3730             :                           !It is expected that this variable is negative, as
    3731             :                           !that is the convention in Morrison microphysics
    3732             : 
    3733             :     type(pdf_parameter), intent(in) :: &
    3734             :       pdf_params ! PDF parameters
    3735             : 
    3736             :     ! -------------------- Input/Output Variables --------------------
    3737             :     real( kind = core_rknd ), dimension(nz), intent(inout) :: &
    3738             :       rtp2_mc, &    !Tendency of <rt'^2> due to evaporation   [(kg/kg)^2/s]
    3739             :       thlp2_mc, &   !Tendency of <thl'^2> due to evaporation  [K^2/s]
    3740             :       wprtp_mc, &   !Tendency of <w'rt'> due to evaporation   [m*(kg/kg)/s^2]
    3741             :       wpthlp_mc, &  !Tendency of <w'thl'> due to evaporation  [m*K/s^2] 
    3742             :       rtpthlp_mc    !Tendency of <rt'thl'> due to evaporation [K*(kg/kg)/s]
    3743             :       
    3744             :     ! -------------------- Local Variables --------------------
    3745             :     real( kind = core_rknd ), dimension(1,nz) :: &
    3746           0 :       cloud_frac_col, &       !Cloud fraction                        [-]
    3747           0 :       rcm_col, &              !Cloud water mixing ratio              [kg/kg]
    3748           0 :       rvm_col, &              !Vapor water mixing ratio              [kg/kg]
    3749           0 :       thlm_col, &             !Liquid potential temperature          [K]
    3750           0 :       wm_col, &               !Mean vertical velocity                [m/s]
    3751           0 :       exner_col, &            !Exner function                        [-]
    3752           0 :       rrm_evap_col         !Evaporation of rain                   [kg/kg/s]
    3753             :                           !It is expected that this variable is negative, as
    3754             :                           !that is the convention in Morrison microphysics
    3755             :                           
    3756             :     real( kind = core_rknd ), dimension(1,nz) :: &
    3757           0 :       rtp2_mc_col, &    !Tendency of <rt'^2> due to evaporation   [(kg/kg)^2/s]
    3758           0 :       thlp2_mc_col, &   !Tendency of <thl'^2> due to evaporation  [K^2/s]
    3759           0 :       wprtp_mc_col, &   !Tendency of <w'rt'> due to evaporation   [m*(kg/kg)/s^2]
    3760           0 :       wpthlp_mc_col, &  !Tendency of <w'thl'> due to evaporation  [m*K/s^2] 
    3761           0 :       rtpthlp_mc_col    !Tendency of <rt'thl'> due to evaporation [K*(kg/kg)/s]
    3762             :       
    3763           0 :     cloud_frac_col(1,:) = cloud_frac
    3764           0 :     rcm_col(1,:) = rcm
    3765           0 :     rvm_col(1,:) = rvm
    3766           0 :     thlm_col(1,:) = thlm
    3767           0 :     wm_col(1,:) = wm
    3768           0 :     exner_col(1,:) = exner
    3769           0 :     rrm_evap_col(1,:) = rrm_evap
    3770           0 :     rtp2_mc_col(1,:) = rtp2_mc
    3771           0 :     thlp2_mc_col(1,:) = thlp2_mc
    3772           0 :     wprtp_mc_col(1,:) = wprtp_mc
    3773           0 :     wpthlp_mc_col(1,:) = wpthlp_mc
    3774           0 :     rtpthlp_mc_col(1,:) = rtpthlp_mc
    3775             :       
    3776             :     call update_xp2_mc( gr, nz, 1, dt, cloud_frac_col, rcm_col, rvm_col, thlm_col, & ! intent(in)
    3777             :                         wm_col, exner_col, rrm_evap_col, pdf_params,        & ! intent(in)
    3778             :                         rtp2_mc_col, thlp2_mc_col, wprtp_mc_col, wpthlp_mc_col,    & ! intent(inout)
    3779           0 :                         rtpthlp_mc_col ) ! intent(inout)
    3780             :                         
    3781           0 :     rtp2_mc = rtp2_mc_col(1,:)
    3782           0 :     thlp2_mc = thlp2_mc_col(1,:)
    3783           0 :     wprtp_mc = wprtp_mc_col(1,:)
    3784           0 :     wpthlp_mc = wpthlp_mc_col(1,:)
    3785           0 :     rtpthlp_mc = rtpthlp_mc_col(1,:)
    3786             :     
    3787           0 :     return
    3788             :   end subroutine update_xp2_mc_api_single_col
    3789             :     
    3790             :   !================================================================================================
    3791             :   ! subroutine init_pdf_hydromet_arrays_api
    3792             :   ! 
    3793             :   ! DESCRIPTION: 
    3794             :   !     This subroutine intializes the hydromet arrays(iirr, iiNr, etc.) to the values specified by
    3795             :   !     the input arguements, this determines which hyrometeors are to be used by the microphysics
    3796             :   !     scheme. It also sets up the corresponding pdf and hydromet arrays, and calculates the 
    3797             :   !     subgrid variance ratio for each hydrometeor.
    3798             :   ! 
    3799             :   ! OPTIONAL FUNCTIONALITY:
    3800             :   !     The subgrid variance ratio for each hydrometeor is calculated based on the grid spacing 
    3801             :   !     defined by the host model. The calculation is a linear equation defined by a slope and
    3802             :   !     intercept, each of which may or may not be passed in to this subroutine. If the slope
    3803             :   !     and/or intercept are not passed in through the arguement list the default values, which 
    3804             :   !     are set in the corresponding type definitions, will be used. Otherwise the values
    3805             :   !     specified by the aruements will be used.
    3806             :   ! 
    3807             :   ! NOTES: 
    3808             :   !     'hmp2_ip_on_hmm2_ip_slope_in' is of type 'hmp2_ip_on_hmm2_ip_slope_type' and
    3809             :   !     'hmp2_ip_on_hmm2_ip_intrcpt_in' is of type 'hmp2_ip_on_hmm2_ip_intrcpt_in', both of which 
    3810             :   !     are deinfed in corr_vrnce_module.F90, and made public through this API.
    3811             :   ! 
    3812             :   !     If full control over the hydrometeor variance ratios is desired, pass in slopes that are
    3813             :   !     initialized to 0.0, this causes the ratios to no longer depend on the grid spacing. Then
    3814             :   !     pass in the intercepts set to the values of the desired ratios.
    3815             :   ! 
    3816             :   ! ARGUEMENTS:
    3817             :   !     host_dx (real) - Horizontal grid spacings
    3818             :   !     host_dy (real)
    3819             :   ! 
    3820             :   !     hydromet_dim (integer) - Number of enabled hydrometeors
    3821             :   ! 
    3822             :   !         Each of these is an index value corresponding to a hydrometeor,
    3823             :   !         used to index the hydrometeor arrays. Each index has to be unqiue
    3824             :   !         for each different hyrometeor that is enabled. Setting one of these
    3825             :   !         indices to -1 disables that hydrometeor
    3826             :   !     iirr_in (integer) - Index of rain water mixing ratio
    3827             :   !     iiri_in (integer) - Index of rain drop concentration
    3828             :   !     iirs_in (integer) - Index of ice mixing ratio
    3829             :   !     iirg_in (integer) - Index of ice crystal concentration
    3830             :   !     iiNr_in (integer) - Index of snow mixing ratio
    3831             :   !     iiNi_in (integer) - Index of snow flake concentration
    3832             :   !     iiNs_in (integer) - Index of graupel mixing ratio
    3833             :   !     iiNg_in (integer) - Index of graupel concentration
    3834             :   ! 
    3835             :   !     hmp2_ip_on_hmm2_ip_slope_in (hmp2_ip_on_hmm2_ip_slope_type) - Custom slope values
    3836             :   !     hmp2_ip_on_hmm2_ip_intrcpt_in (hmp2_ip_on_hmm2_ip_intrcpt_type) - Custom intercept values
    3837             :   ! 
    3838             :   !================================================================================================
    3839           0 :   subroutine init_pdf_hydromet_arrays_api( host_dx, host_dy, hydromet_dim_in,   & ! intent(in)
    3840             :                                            iirr_in, iiri_in, iirs_in, iirg_in,  & ! intent(in)
    3841             :                                            iiNr_in, iiNi_in, iiNs_in, iiNg_in,  & ! intent(in)
    3842             :                                            hmp2_ip_on_hmm2_ip_slope_in,         & ! optional(in)
    3843             :                                            hmp2_ip_on_hmm2_ip_intrcpt_in        ) ! optional(in)
    3844             : 
    3845             :     use array_index, only: &
    3846             :         iirr, & ! Indicies for the hydromet arrays
    3847             :         iiNr, &
    3848             :         iirs, &
    3849             :         iiri, &
    3850             :         iirg, &
    3851             :         iiNs, & 
    3852             :         iiNi, &
    3853             :         iiNg
    3854             : 
    3855             :     use corr_varnce_module, only: &
    3856             :         init_pdf_indices,                   & ! Procedures
    3857             :         init_hydromet_arrays,               &
    3858             :         hmp2_ip_on_hmm2_ip_slope_type,      & ! Types
    3859             :         hmp2_ip_on_hmm2_ip_intrcpt_type,    &
    3860             :         hmp2_ip_on_hmm2_ip                    ! Array of hydromet ratios
    3861             : 
    3862             :     use parameters_model, only: &
    3863             :         hydromet_dim
    3864             : 
    3865             :     implicit none
    3866             : 
    3867             :     ! Input Variables
    3868             :     integer, intent(in) :: &
    3869             :       hydromet_dim_in,  & ! Total number of hydrometeor species.
    3870             :       iirr_in,          & ! Index of rain water mixing ratio
    3871             :       iiNr_in,          & ! Index of rain drop concentration
    3872             :       iiri_in,          & ! Index of ice mixing ratio
    3873             :       iiNi_in,          & ! Index of ice crystal concentration
    3874             :       iirs_in,          & ! Index of snow mixing ratio
    3875             :       iiNs_in,          & ! Index of snow flake concentration
    3876             :       iirg_in,          & ! Index of graupel mixing ratio
    3877             :       iiNg_in             ! Index of graupel concentration
    3878             : 
    3879             :     real( kind = core_rknd ), intent(in) :: &
    3880             :       host_dx, host_dy  ! Horizontal grid spacing, defined by host model [m]
    3881             : 
    3882             : 
    3883             :     ! Optional Input Variables
    3884             : 
    3885             :     ! Used to overwrite default values of slope and intercept
    3886             :     type( hmp2_ip_on_hmm2_ip_slope_type ), optional, intent(in) :: &
    3887             :         hmp2_ip_on_hmm2_ip_slope_in     ! Custom slopes to overwrite defaults [1/m]
    3888             :       
    3889             :     type( hmp2_ip_on_hmm2_ip_intrcpt_type ), optional, intent(in) :: &
    3890             :         hmp2_ip_on_hmm2_ip_intrcpt_in   ! Custom intercepts to overwrite defaults [-]
    3891             : 
    3892             : 
    3893             :     ! Local Variables
    3894             : 
    3895             :     ! Slope and intercept are initialized with default values
    3896             :     type( hmp2_ip_on_hmm2_ip_slope_type ) :: &
    3897             :         hmp2_ip_on_hmm2_ip_slope        ! Slopes used to calculated hydromet variance [1/m]
    3898             :       
    3899             :     type( hmp2_ip_on_hmm2_ip_intrcpt_type ) :: &
    3900             :         hmp2_ip_on_hmm2_ip_intrcpt      ! Intercepts used to calculated hydromet variance [-]
    3901             : 
    3902             :     !----------------------- Begin Code -----------------------------
    3903             : 
    3904             :     ! If slope and intercept are present in call, then overwrite default values
    3905           0 :     if ( present( hmp2_ip_on_hmm2_ip_slope_in ) ) then
    3906           0 :         hmp2_ip_on_hmm2_ip_slope = hmp2_ip_on_hmm2_ip_slope_in
    3907             :     end if
    3908             : 
    3909           0 :     if ( present( hmp2_ip_on_hmm2_ip_intrcpt_in ) ) then
    3910           0 :         hmp2_ip_on_hmm2_ip_intrcpt = hmp2_ip_on_hmm2_ip_intrcpt_in
    3911             :     end if
    3912             : 
    3913             : 
    3914             :     ! Initialize the hydromet indices and hydromet_dim
    3915           0 :     hydromet_dim = hydromet_dim_in
    3916           0 :     iirr = iirr_in
    3917           0 :     iiri = iiri_in
    3918           0 :     iirs = iirs_in
    3919           0 :     iirg = iirg_in
    3920           0 :     iiNr = iiNr_in
    3921           0 :     iiNi = iiNi_in
    3922           0 :     iiNs = iiNs_in
    3923           0 :     iiNg = iiNg_in
    3924             : 
    3925             :     ! Calculate the subgrid variances of the hydrometeors
    3926           0 :     allocate( hmp2_ip_on_hmm2_ip(hydromet_dim) )
    3927             : 
    3928           0 :     if ( iirr > 0 ) then
    3929           0 :        hmp2_ip_on_hmm2_ip(iirr) = hmp2_ip_on_hmm2_ip_intrcpt%rr + &
    3930           0 :                                   hmp2_ip_on_hmm2_ip_slope%rr * max( host_dx, host_dy )
    3931             :     endif
    3932             : 
    3933           0 :     if ( iirs > 0 ) then
    3934           0 :        hmp2_ip_on_hmm2_ip(iirs) = hmp2_ip_on_hmm2_ip_intrcpt%rs + &
    3935           0 :                                   hmp2_ip_on_hmm2_ip_slope%rs * max( host_dx, host_dy )
    3936             :     endif
    3937             : 
    3938           0 :     if ( iiri > 0 ) then
    3939           0 :        hmp2_ip_on_hmm2_ip(iiri) = hmp2_ip_on_hmm2_ip_intrcpt%ri + &
    3940           0 :                                   hmp2_ip_on_hmm2_ip_slope%ri * max( host_dx, host_dy )
    3941             :     endif
    3942             : 
    3943           0 :     if ( iirg > 0 ) then
    3944           0 :        hmp2_ip_on_hmm2_ip(iirg) = hmp2_ip_on_hmm2_ip_intrcpt%rg + &
    3945           0 :                                   hmp2_ip_on_hmm2_ip_slope%rg * max( host_dx, host_dy )
    3946             :     endif
    3947             : 
    3948           0 :     if ( iiNr > 0 ) then
    3949           0 :        hmp2_ip_on_hmm2_ip(iiNr) = hmp2_ip_on_hmm2_ip_intrcpt%Nr + &
    3950           0 :                                   hmp2_ip_on_hmm2_ip_slope%Nr * max( host_dx, host_dy )
    3951             :     endif
    3952             : 
    3953           0 :     if ( iiNs > 0 ) then
    3954           0 :        hmp2_ip_on_hmm2_ip(iiNs) = hmp2_ip_on_hmm2_ip_intrcpt%Ns + &
    3955           0 :                                   hmp2_ip_on_hmm2_ip_slope%Ns * max( host_dx, host_dy )
    3956             :     endif
    3957             : 
    3958           0 :     if ( iiNi > 0 ) then
    3959           0 :        hmp2_ip_on_hmm2_ip(iiNi) = hmp2_ip_on_hmm2_ip_intrcpt%Ni + &
    3960           0 :                                   hmp2_ip_on_hmm2_ip_slope%Ni * max( host_dx, host_dy )
    3961             :     endif
    3962             : 
    3963           0 :     if ( iiNg > 0 ) then
    3964           0 :        hmp2_ip_on_hmm2_ip(iiNg) = hmp2_ip_on_hmm2_ip_intrcpt%Ng + &
    3965           0 :                                   hmp2_ip_on_hmm2_ip_slope%Ng * max( host_dx, host_dy )
    3966             :     endif
    3967             : 
    3968             :     ! Hydromet arrays are Initialized based on the hydromet indices
    3969             :     call init_hydromet_arrays( hydromet_dim, iirr, iiNr,    & ! intent(in)
    3970             :                                iiri, iiNi, iirs, iiNs,      & ! intent(in)
    3971           0 :                                iirg, iiNg )                   ! intent(in)
    3972             : 
    3973             :     
    3974             :     ! Initialize the PDF indices based on the hydromet indices
    3975             :     call init_pdf_indices( hydromet_dim,iirr, iiNr, & ! intent(in)
    3976             :                            iiri, iiNi, iirs, iiNs,  & ! intent(in)
    3977           0 :                            iirg, iiNg )               ! intent(in)
    3978             : 
    3979           0 :     return
    3980             : 
    3981           0 :   end subroutine init_pdf_hydromet_arrays_api
    3982             : 
    3983             :   !================================================================================================
    3984             :   ! set_default_parameters: Sets all CLUBB tunable parameters to a default setting
    3985             :   !================================================================================================
    3986        1536 :   subroutine set_default_parameters_api( &
    3987             :                C1, C1b, C1c, C2rt, C2thl, C2rtthl, &
    3988             :                C4, C_uu_shr, C_uu_buoy, C6rt, C6rtb, C6rtc, &
    3989             :                C6thl, C6thlb, C6thlc, C7, C7b, C7c, C8, C8b, C10, &
    3990             :                C11, C11b, C11c, C12, C13, C14, C_wp2_pr_dfsn, C_wp3_pr_tp, &
    3991             :                C_wp3_pr_turb, C_wp3_pr_dfsn, C_wp2_splat, &
    3992             :                C6rt_Lscale0, C6thl_Lscale0, C7_Lscale0, wpxp_L_thresh, &
    3993             :                c_K, c_K1, nu1, c_K2, nu2, c_K6, nu6, c_K8, nu8, &
    3994             :                c_K9, nu9, nu10, c_K_hm, c_K_hmb, K_hm_min_coef, nu_hm, &
    3995             :                slope_coef_spread_DG_means_w, pdf_component_stdev_factor_w, &
    3996             :                coef_spread_DG_means_rt, coef_spread_DG_means_thl, &
    3997             :                gamma_coef, gamma_coefb, gamma_coefc, mu, beta, lmin_coef, &
    3998             :                omicron, zeta_vrnce_rat, upsilon_precip_frac_rat, &
    3999             :                lambda0_stability_coef, mult_coef, taumin, taumax, &
    4000             :                Lscale_mu_coef, Lscale_pert_coef, alpha_corr, &
    4001             :                Skw_denom_coef, c_K10, c_K10h, thlp2_rad_coef, &
    4002             :                thlp2_rad_cloud_frac_thresh, up2_sfc_coef, &
    4003             :                Skw_max_mag, xp3_coef_base, xp3_coef_slope, &
    4004             :                altitude_threshold, rtp2_clip_coef, C_invrs_tau_bkgnd, &
    4005             :                C_invrs_tau_sfc, C_invrs_tau_shear, C_invrs_tau_N2, &
    4006             :                C_invrs_tau_N2_wp2, C_invrs_tau_N2_xp2, &
    4007             :                C_invrs_tau_N2_wpxp, C_invrs_tau_N2_clear_wp3, &
    4008             :                C_invrs_tau_wpxp_Ri, C_invrs_tau_wpxp_N2_thresh, &
    4009             :                Cx_min, Cx_max, Richardson_num_min, Richardson_num_max, &
    4010             :                wpxp_Ri_exp, a3_coef_min, a_const, bv_efold, z_displace )
    4011             : 
    4012             :     use parameters_tunable, only: &
    4013             :         set_default_parameters    ! Procedure(s)
    4014             : 
    4015             :     implicit none
    4016             : 
    4017             :     ! Output variables
    4018             :     real( kind = core_rknd ), intent(out) :: &
    4019             :       C1, C1b, C1c, C2rt, C2thl, C2rtthl, &
    4020             :       C4, C_uu_shr, C_uu_buoy, C6rt, C6rtb, C6rtc, C6thl, C6thlb, C6thlc, &
    4021             :       C7, C7b, C7c, C8, C8b, C10, &
    4022             :       C11, C11b, C11c, C12, C13, C14, C_wp2_pr_dfsn, C_wp3_pr_tp, &
    4023             :       C_wp3_pr_turb, C_wp3_pr_dfsn, C_wp2_splat, &
    4024             :       C6rt_Lscale0, C6thl_Lscale0, C7_Lscale0, wpxp_L_thresh, &
    4025             :       c_K, c_K1, nu1, c_K2, nu2, c_K6, nu6, c_K8, nu8,  &
    4026             :       c_K9, nu9, nu10, c_K_hm, c_K_hmb, K_hm_min_coef, nu_hm, &
    4027             :       slope_coef_spread_DG_means_w, pdf_component_stdev_factor_w, &
    4028             :       coef_spread_DG_means_rt, coef_spread_DG_means_thl, &
    4029             :       gamma_coef, gamma_coefb, gamma_coefc, mu, beta, lmin_coef, &
    4030             :       omicron, zeta_vrnce_rat, upsilon_precip_frac_rat, &
    4031             :       lambda0_stability_coef, mult_coef, taumin, taumax, Lscale_mu_coef, &
    4032             :       Lscale_pert_coef, alpha_corr, Skw_denom_coef, c_K10, c_K10h, &
    4033             :       thlp2_rad_coef, thlp2_rad_cloud_frac_thresh, up2_sfc_coef, &
    4034             :       Skw_max_mag, xp3_coef_base, xp3_coef_slope, altitude_threshold, &
    4035             :       rtp2_clip_coef, C_invrs_tau_bkgnd, C_invrs_tau_sfc, &
    4036             :       C_invrs_tau_shear, C_invrs_tau_N2, C_invrs_tau_N2_wp2, &
    4037             :       C_invrs_tau_N2_xp2, C_invrs_tau_N2_wpxp, C_invrs_tau_N2_clear_wp3, &
    4038             :       C_invrs_tau_wpxp_Ri, C_invrs_tau_wpxp_N2_thresh, &
    4039             :       Cx_min, Cx_max, Richardson_num_min, Richardson_num_max, &
    4040             :       wpxp_Ri_exp, a3_coef_min, a_const, bv_efold, z_displace
    4041             : 
    4042             :     call set_default_parameters( &
    4043             :                C1, C1b, C1c, C2rt, C2thl, C2rtthl, &
    4044             :                C4, C_uu_shr, C_uu_buoy, C6rt, C6rtb, C6rtc, &
    4045             :                C6thl, C6thlb, C6thlc, C7, C7b, C7c, C8, C8b, C10, &
    4046             :                C11, C11b, C11c, C12, C13, C14, C_wp2_pr_dfsn, C_wp3_pr_tp, &
    4047             :                C_wp3_pr_turb, C_wp3_pr_dfsn, C_wp2_splat, &
    4048             :                C6rt_Lscale0, C6thl_Lscale0, C7_Lscale0, wpxp_L_thresh, &
    4049             :                c_K, c_K1, nu1, c_K2, nu2, c_K6, nu6, c_K8, nu8, &
    4050             :                c_K9, nu9, nu10, c_K_hm, c_K_hmb, K_hm_min_coef, nu_hm, &
    4051             :                slope_coef_spread_DG_means_w, pdf_component_stdev_factor_w, &
    4052             :                coef_spread_DG_means_rt, coef_spread_DG_means_thl, &
    4053             :                gamma_coef, gamma_coefb, gamma_coefc, mu, beta, lmin_coef, &
    4054             :                omicron, zeta_vrnce_rat, upsilon_precip_frac_rat, &
    4055             :                lambda0_stability_coef, mult_coef, taumin, taumax, &
    4056             :                Lscale_mu_coef, Lscale_pert_coef, alpha_corr, &
    4057             :                Skw_denom_coef, c_K10, c_K10h, thlp2_rad_coef, &
    4058             :                thlp2_rad_cloud_frac_thresh, up2_sfc_coef, &
    4059             :                Skw_max_mag, xp3_coef_base, xp3_coef_slope, &
    4060             :                altitude_threshold, rtp2_clip_coef, C_invrs_tau_bkgnd, &
    4061             :                C_invrs_tau_sfc, C_invrs_tau_shear, C_invrs_tau_N2, &
    4062             :                C_invrs_tau_N2_wp2, C_invrs_tau_N2_xp2, &
    4063             :                C_invrs_tau_N2_wpxp, C_invrs_tau_N2_clear_wp3, &
    4064             :                C_invrs_tau_wpxp_Ri, C_invrs_tau_wpxp_N2_thresh, &
    4065             :                Cx_min, Cx_max, Richardson_num_min, Richardson_num_max, &
    4066        1536 :                wpxp_Ri_exp, a3_coef_min, a_const, bv_efold, z_displace )
    4067             : 
    4068        1536 :     return
    4069             : 
    4070             :   end subroutine set_default_parameters_api
    4071             : 
    4072             :   !================================================================================================
    4073             :   ! set_default_clubb_config_flags: Sets all CLUBB flags to a default setting
    4074             :   !================================================================================================
    4075        1536 :   subroutine set_default_clubb_config_flags_api( iiPDF_type, & ! Out
    4076             :                                                  ipdf_call_placement, & ! Out
    4077             :                                                  penta_solve_method, & ! Out
    4078             :                                                  tridiag_solve_method, & ! Out
    4079             :                                                  l_use_precip_frac, & ! Out
    4080             :                                                  l_predict_upwp_vpwp, & ! Out
    4081             :                                                  l_min_wp2_from_corr_wx, & ! Out
    4082             :                                                  l_min_xp2_from_corr_wx, & ! Out
    4083             :                                                  l_C2_cloud_frac, & ! Out
    4084             :                                                  l_diffuse_rtm_and_thlm, & ! Out
    4085             :                                                  l_stability_correct_Kh_N2_zm, & ! Out
    4086             :                                                  l_calc_thlp2_rad, & ! Out
    4087             :                                                  l_upwind_xpyp_ta, & ! Out
    4088             :                                                  l_upwind_xm_ma, & ! Out
    4089             :                                                  l_uv_nudge, & ! Out
    4090             :                                                  l_rtm_nudge, & ! Out
    4091             :                                                  l_tke_aniso, & ! Out
    4092             :                                                  l_vert_avg_closure, & ! Out
    4093             :                                                  l_trapezoidal_rule_zt, & ! Out
    4094             :                                                  l_trapezoidal_rule_zm, & ! Out
    4095             :                                                  l_call_pdf_closure_twice, & ! Out
    4096             :                                                  l_standard_term_ta, & ! Out
    4097             :                                                  l_partial_upwind_wp3, & ! Out
    4098             :                                                  l_godunov_upwind_wpxp_ta, & ! Out
    4099             :                                                  l_godunov_upwind_xpyp_ta, & ! Out
    4100             :                                                  l_use_cloud_cover, & ! Out
    4101             :                                                  l_diagnose_correlations, & ! Out
    4102             :                                                  l_calc_w_corr, & ! Out
    4103             :                                                  l_const_Nc_in_cloud, & ! Out
    4104             :                                                  l_fix_w_chi_eta_correlations, & ! Out
    4105             :                                                  l_stability_correct_tau_zm, & ! Out
    4106             :                                                  l_damp_wp2_using_em, & ! Out
    4107             :                                                  l_do_expldiff_rtm_thlm, & ! Out
    4108             :                                                  l_Lscale_plume_centered, & ! Out
    4109             :                                                  l_diag_Lscale_from_tau, & ! Out
    4110             :                                                  l_use_C7_Richardson, & ! Out
    4111             :                                                  l_use_C11_Richardson, & ! Out
    4112             :                                                  l_use_shear_Richardson, & ! Out
    4113             :                                                  l_brunt_vaisala_freq_moist, & ! Out
    4114             :                                                  l_use_thvm_in_bv_freq, & ! Out
    4115             :                                                  l_rcm_supersat_adj, & ! Out
    4116             :                                                  l_damp_wp3_Skw_squared, & ! Out
    4117             :                                                  l_prescribed_avg_deltaz, & ! Out
    4118             :                                                  l_lmm_stepping, & ! Out
    4119             :                                                  l_e3sm_config, & ! Out
    4120             :                                                  l_vary_convect_depth, & ! Out
    4121             :                                                  l_use_tke_in_wp3_pr_turb_term, & ! Out
    4122             :                                                  l_use_tke_in_wp2_wp3_K_dfsn, & ! Out
    4123             :                                                  l_use_wp3_lim_with_smth_Heaviside, & ! Out
    4124             :                                                  l_smooth_Heaviside_tau_wpxp, & ! Out
    4125             :                                                  l_modify_limiters_for_cnvg_test, & ! Out
    4126             :                                                  l_enable_relaxed_clipping, & ! Out
    4127             :                                                  l_linearize_pbl_winds, & ! Out
    4128             :                                                  l_mono_flux_lim_thlm, & ! Out
    4129             :                                                  l_mono_flux_lim_rtm, & ! Out
    4130             :                                                  l_mono_flux_lim_um, & ! Out
    4131             :                                                  l_mono_flux_lim_vm, & ! Out
    4132             :                                                  l_mono_flux_lim_spikefix ) ! Out
    4133             : 
    4134             :     use model_flags, only: &
    4135             :         set_default_clubb_config_flags  ! Procedure
    4136             : 
    4137             :     implicit none
    4138             : 
    4139             :     ! Output variables
    4140             :     integer, intent(out) :: &
    4141             :       iiPDF_type,          & ! Selected option for the two-component normal
    4142             :                              ! (double Gaussian) PDF type to use for the w, rt,
    4143             :                              ! and theta-l (or w, chi, and eta) portion of
    4144             :                              ! CLUBB's multivariate, two-component PDF.
    4145             :       ipdf_call_placement, & ! Selected option for the placement of the call to
    4146             :                              ! CLUBB's PDF.
    4147             :       penta_solve_method,  & ! Option to set the penta-diagonal matrix solving method
    4148             :       tridiag_solve_method   ! Option to set the tri-diagonal matrix solving method
    4149             : 
    4150             :     logical, intent(out) :: &
    4151             :       l_use_precip_frac,            & ! Flag to use precipitation fraction in KK microphysics. The
    4152             :                                       ! precipitation fraction is automatically set to 1 when this
    4153             :                                       ! flag is turned off.
    4154             :       l_predict_upwp_vpwp,          & ! Flag to predict <u'w'> and <v'w'> along with <u> and <v>
    4155             :                                       ! alongside the advancement of <rt>, <w'rt'>, <thl>,
    4156             :                                       ! <wpthlp>, <sclr>, and <w'sclr'> in subroutine
    4157             :                                       ! advance_xm_wpxp.  Otherwise, <u'w'> and <v'w'> are still
    4158             :                                       ! approximated by eddy diffusivity when <u> and <v> are
    4159             :                                       ! advanced in subroutine advance_windm_edsclrm.
    4160             :       l_min_wp2_from_corr_wx,       & ! Flag to base the threshold minimum value of wp2 on keeping
    4161             :                                       ! the overall correlation of w and x (w and rt, as well as w
    4162             :                                       ! and theta-l) within the limits of -max_mag_correlation_flux
    4163             :                                       ! to max_mag_correlation_flux.
    4164             :       l_min_xp2_from_corr_wx,       & ! Flag to base the threshold minimum value of xp2 (rtp2 and
    4165             :                                       ! thlp2) on keeping the overall correlation of w and x within
    4166             :                                       ! the limits of -max_mag_correlation_flux to
    4167             :                                       ! max_mag_correlation_flux.
    4168             :       l_C2_cloud_frac,              & ! Flag to use cloud fraction to adjust the value of the
    4169             :                                       ! turbulent dissipation coefficient, C2.
    4170             :       l_diffuse_rtm_and_thlm,       & ! Diffuses rtm and thlm
    4171             :       l_stability_correct_Kh_N2_zm, & ! Divides Kh_N2_zm by a stability factor
    4172             :       l_calc_thlp2_rad,             & ! Include the contribution of radiation to thlp2
    4173             :       l_upwind_xpyp_ta,             & ! This flag determines whether we want to use an upwind
    4174             :                                       ! differencing approximation rather than a centered
    4175             :                                       ! differencing for turbulent or mean advection terms. It
    4176             :                                       ! affects rtp2, thlp2, up2, vp2, sclrp2, rtpthlp, sclrprtp, &
    4177             :                                       ! sclrpthlp.
    4178             :       l_upwind_xm_ma,               & ! This flag determines whether we want to use an upwind
    4179             :                                       ! differencing approximation rather than a centered
    4180             :                                       ! differencing for turbulent or mean advection terms. It
    4181             :                                       ! affects rtm, thlm, sclrm, um and vm.
    4182             :       l_uv_nudge,                   & ! For wind speed nudging.
    4183             :       l_rtm_nudge,                  & ! For rtm nudging
    4184             :       l_tke_aniso,                  & ! For anisotropic turbulent kinetic energy, i.e.
    4185             :                                       ! TKE = 1/2 (u'^2 + v'^2 + w'^2)
    4186             :       l_vert_avg_closure,           & ! Use 2 calls to pdf_closure and the trapezoidal rule to
    4187             :                                       ! compute the varibles that are output from high order
    4188             :                                       ! closure
    4189             :       l_trapezoidal_rule_zt,        & ! If true, the trapezoidal rule is called for the
    4190             :                                       ! thermodynamic-level variables output from pdf_closure.
    4191             :       l_trapezoidal_rule_zm,        & ! If true, the trapezoidal rule is called for three
    4192             :                                       ! momentum-level variables - wpthvp, thlpthvp, and rtpthvp -
    4193             :                                       ! output from pdf_closure.
    4194             :       l_call_pdf_closure_twice,     & ! This logical flag determines whether or not to call
    4195             :                                       ! subroutine pdf_closure twice.  If true, pdf_closure is
    4196             :                                       ! called first on thermodynamic levels and then on momentum
    4197             :                                       ! levels so that each variable is computed on its native
    4198             :                                       ! level.  If false, pdf_closure is only called on
    4199             :                                       ! thermodynamic levels, and variables which belong on
    4200             :                                       ! momentum levels are interpolated.
    4201             :       l_standard_term_ta,           & ! Use the standard discretization for the turbulent advection
    4202             :                                       ! terms.  Setting to .false. means that a_1 and a_3 are
    4203             :                                       ! pulled outside of the derivative in
    4204             :                                       ! advance_wp2_wp3_module.F90 and in
    4205             :                                       ! advance_xp2_xpyp_module.F90.
    4206             :       l_partial_upwind_wp3,         & ! Flag to use an "upwind" discretization rather
    4207             :                                       ! than a centered discretization for the portion
    4208             :                                       ! of the wp3 turbulent advection term for ADG1
    4209             :                                       ! that is linearized in terms of wp3<t+1>.
    4210             :                                       ! (Requires ADG1 PDF and l_standard_term_ta).
    4211             :       l_godunov_upwind_wpxp_ta,     & ! This flag determines whether we want to use an upwind
    4212             :                                       ! differencing approximation rather than a centered 
    4213             :                                       ! differencing for turbulent advection terms. 
    4214             :                                       ! It affects  wpxp only.
    4215             :       l_godunov_upwind_xpyp_ta,     & ! This flag determines whether we want to use an upwind
    4216             :                                       ! differencing approximation rather than a centered 
    4217             :                                       ! differencing for turbulent advection terms. It affects
    4218             :                                       ! xpyp only.
    4219             :       l_use_cloud_cover,            & ! Use cloud_cover and rcm_in_layer to help boost cloud_frac
    4220             :                                       ! and rcm to help increase cloudiness at coarser grid
    4221             :                                       ! resolutions.
    4222             :       l_diagnose_correlations,      & ! Diagnose correlations instead of using fixed ones
    4223             :       l_calc_w_corr,                & ! Calculate the correlations between w and the hydrometeors
    4224             :       l_const_Nc_in_cloud,          & ! Use a constant cloud droplet conc. within cloud (K&K)
    4225             :       l_fix_w_chi_eta_correlations, & ! Use a fixed correlation for s and t Mellor(chi/eta)
    4226             :       l_stability_correct_tau_zm,   & ! Use tau_N2_zm instead of tau_zm in wpxp_pr1 stability
    4227             :                                       ! correction
    4228             :       l_damp_wp2_using_em,          & ! In wp2 equation, use a dissipation formula of
    4229             :                                       ! -(2/3)*em/tau_zm, as in Bougeault (1981)
    4230             :       l_do_expldiff_rtm_thlm,       & ! Diffuse rtm and thlm explicitly
    4231             :       l_Lscale_plume_centered,      & ! Alternate that uses the PDF to compute the perturbed values
    4232             :       l_diag_Lscale_from_tau,       & ! First diagnose dissipation time tau, and then diagnose the
    4233             :                                       ! mixing length scale as Lscale = tau * tke
    4234             :       l_use_C7_Richardson,          & ! Parameterize C7 based on Richardson number
    4235             :       l_use_C11_Richardson,         & ! Parameterize C11 and C16 based on Richardson number
    4236             :       l_use_shear_Richardson,       & ! Use shear in the calculation of Richardson number
    4237             :       l_brunt_vaisala_freq_moist,   & ! Use a different formula for the Brunt-Vaisala frequency in
    4238             :                                       ! saturated atmospheres (from Durran and Klemp, 1982)
    4239             :       l_use_thvm_in_bv_freq,        & ! Use thvm in the calculation of Brunt-Vaisala frequency
    4240             :       l_rcm_supersat_adj,           & ! Add excess supersaturated vapor to cloud water
    4241             :       l_damp_wp3_Skw_squared,       & ! Set damping on wp3 to use Skw^2 rather than Skw^4
    4242             :       l_prescribed_avg_deltaz,      & ! used in adj_low_res_nu. If .true., avg_deltaz = deltaz
    4243             :       l_lmm_stepping,               & ! Apply Linear Multistep Method (LMM) Stepping
    4244             :       l_e3sm_config,                & ! Run model with E3SM settings
    4245             :       l_vary_convect_depth,         & ! Flag used to calculate convective velocity using
    4246             :                                       ! a variable estimate of layer depth based on the depth
    4247             :                                       ! over which wpthlp is positive near the ground when true
    4248             :                                       ! More information can be found by
    4249             :                                       ! Looking at issue #905 on the clubb repo
    4250             :       l_use_tke_in_wp3_pr_turb_term,& ! Use TKE formulation for wp3 pr_turb term
    4251             :       l_use_tke_in_wp2_wp3_K_dfsn,  & ! Use TKE in eddy diffusion for wp2 and wp3
    4252             :       l_use_wp3_lim_with_smth_Heaviside, & ! Flag to activate mods on wp3 limiters for conv test
    4253             :       l_smooth_Heaviside_tau_wpxp,  & ! Use smoothed Heaviside 'Peskin' function
    4254             :                                       ! in the calculation of H_invrs_tau_wpxp_N2
    4255             :                                       ! in src/CLUBB_core/mixing_length.F90
    4256             :       l_modify_limiters_for_cnvg_test, & ! Flag to activate mods on limiters for conv test
    4257             :       l_enable_relaxed_clipping,    & ! Flag to relax clipping on wpxp
    4258             :                                       ! in xm_wpxp_clipping_and_stats
    4259             :       l_linearize_pbl_winds,        & ! Code to linearize PBL winds
    4260             :       l_mono_flux_lim_thlm,         & ! Flag to turn on monotonic flux limiter for thlm
    4261             :       l_mono_flux_lim_rtm,          & ! Flag to turn on monotonic flux limiter for rtm
    4262             :       l_mono_flux_lim_um,           & ! Flag to turn on monotonic flux limiter for um
    4263             :       l_mono_flux_lim_vm,           & ! Flag to turn on monotonic flux limiter for vm
    4264             :       l_mono_flux_lim_spikefix        ! Flag to implement monotonic flux limiter code that
    4265             :                                       ! eliminates spurious drying tendencies at model top
    4266             : 
    4267             :     call set_default_clubb_config_flags( iiPDF_type, & ! Out
    4268             :                                          ipdf_call_placement, & ! Out
    4269             :                                          penta_solve_method, & ! Out
    4270             :                                          tridiag_solve_method, & ! Out
    4271             :                                          l_use_precip_frac, & ! Out
    4272             :                                          l_predict_upwp_vpwp, & ! Out
    4273             :                                          l_min_wp2_from_corr_wx, & ! Out
    4274             :                                          l_min_xp2_from_corr_wx, & ! Out
    4275             :                                          l_C2_cloud_frac, & ! Out
    4276             :                                          l_diffuse_rtm_and_thlm, & ! Out
    4277             :                                          l_stability_correct_Kh_N2_zm, & ! Out
    4278             :                                          l_calc_thlp2_rad, & ! Out
    4279             :                                          l_upwind_xpyp_ta, & ! Out
    4280             :                                          l_upwind_xm_ma, & ! Out
    4281             :                                          l_uv_nudge, & ! Out
    4282             :                                          l_rtm_nudge, & ! Out
    4283             :                                          l_tke_aniso, & ! Out
    4284             :                                          l_vert_avg_closure, & ! Out
    4285             :                                          l_trapezoidal_rule_zt, & ! Out
    4286             :                                          l_trapezoidal_rule_zm, & ! Out
    4287             :                                          l_call_pdf_closure_twice, & ! Out
    4288             :                                          l_standard_term_ta, & ! Out
    4289             :                                          l_partial_upwind_wp3, & ! Out
    4290             :                                          l_godunov_upwind_wpxp_ta, & ! Out
    4291             :                                          l_godunov_upwind_xpyp_ta, & ! Out
    4292             :                                          l_use_cloud_cover, & ! Out
    4293             :                                          l_diagnose_correlations, & ! Out
    4294             :                                          l_calc_w_corr, & ! Out
    4295             :                                          l_const_Nc_in_cloud, & ! Out
    4296             :                                          l_fix_w_chi_eta_correlations, & ! Out
    4297             :                                          l_stability_correct_tau_zm, & ! Out
    4298             :                                          l_damp_wp2_using_em, & ! Out
    4299             :                                          l_do_expldiff_rtm_thlm, & ! Out
    4300             :                                          l_Lscale_plume_centered, & ! Out
    4301             :                                          l_diag_Lscale_from_tau, & ! Out
    4302             :                                          l_use_C7_Richardson, & ! Out
    4303             :                                          l_use_C11_Richardson, & ! Out
    4304             :                                          l_use_shear_Richardson, & ! Out
    4305             :                                          l_brunt_vaisala_freq_moist, & ! Out
    4306             :                                          l_use_thvm_in_bv_freq, & ! Out
    4307             :                                          l_rcm_supersat_adj, & ! Out
    4308             :                                          l_damp_wp3_Skw_squared, & ! Out
    4309             :                                          l_prescribed_avg_deltaz, & ! Out
    4310             :                                          l_lmm_stepping, & ! Out
    4311             :                                          l_e3sm_config, & ! Out
    4312             :                                          l_vary_convect_depth, & ! Out
    4313             :                                          l_use_tke_in_wp3_pr_turb_term, & ! Out
    4314             :                                          l_use_tke_in_wp2_wp3_K_dfsn, & ! Out
    4315             :                                          l_use_wp3_lim_with_smth_Heaviside, & ! Out
    4316             :                                          l_smooth_Heaviside_tau_wpxp, & ! Out
    4317             :                                          l_modify_limiters_for_cnvg_test, & ! Out
    4318             :                                          l_enable_relaxed_clipping, & ! Out
    4319             :                                          l_linearize_pbl_winds, & ! Out
    4320             :                                          l_mono_flux_lim_thlm, & ! Out
    4321             :                                          l_mono_flux_lim_rtm, & ! Out
    4322             :                                          l_mono_flux_lim_um, & ! Out
    4323             :                                          l_mono_flux_lim_vm, & ! Out
    4324        1536 :                                          l_mono_flux_lim_spikefix ) ! Out
    4325             : 
    4326        1536 :   end subroutine set_default_clubb_config_flags_api
    4327             : 
    4328             :   !================================================================================================
    4329             :   ! initialize_clubb_config_flags_type: Initialize the clubb_config_flags_type
    4330             :   !================================================================================================
    4331        1536 :   subroutine initialize_clubb_config_flags_type_api( iiPDF_type, & ! In
    4332             :                                                      ipdf_call_placement, & ! In
    4333             :                                                      penta_solve_method, & ! In
    4334             :                                                      tridiag_solve_method, & ! In
    4335             :                                                      l_use_precip_frac, & ! In
    4336             :                                                      l_predict_upwp_vpwp, & ! In
    4337             :                                                      l_min_wp2_from_corr_wx, & ! In
    4338             :                                                      l_min_xp2_from_corr_wx, & ! In
    4339             :                                                      l_C2_cloud_frac, & ! In
    4340             :                                                      l_diffuse_rtm_and_thlm, & ! In
    4341             :                                                      l_stability_correct_Kh_N2_zm, & ! In
    4342             :                                                      l_calc_thlp2_rad, & ! In
    4343             :                                                      l_upwind_xpyp_ta, & ! In
    4344             :                                                      l_upwind_xm_ma, & ! In
    4345             :                                                      l_uv_nudge, & ! In
    4346             :                                                      l_rtm_nudge, & ! In
    4347             :                                                      l_tke_aniso, & ! In
    4348             :                                                      l_vert_avg_closure, & ! In
    4349             :                                                      l_trapezoidal_rule_zt, & ! In
    4350             :                                                      l_trapezoidal_rule_zm, & ! In
    4351             :                                                      l_call_pdf_closure_twice, & ! In
    4352             :                                                      l_standard_term_ta, & ! In
    4353             :                                                      l_partial_upwind_wp3, & ! In
    4354             :                                                      l_godunov_upwind_wpxp_ta, & ! In
    4355             :                                                      l_godunov_upwind_xpyp_ta, & ! In
    4356             :                                                      l_use_cloud_cover, & ! In
    4357             :                                                      l_diagnose_correlations, & ! In
    4358             :                                                      l_calc_w_corr, & ! In
    4359             :                                                      l_const_Nc_in_cloud, & ! In
    4360             :                                                      l_fix_w_chi_eta_correlations, & ! In
    4361             :                                                      l_stability_correct_tau_zm, & ! In
    4362             :                                                      l_damp_wp2_using_em, & ! In
    4363             :                                                      l_do_expldiff_rtm_thlm, & ! In
    4364             :                                                      l_Lscale_plume_centered, & ! In
    4365             :                                                      l_diag_Lscale_from_tau, & ! In
    4366             :                                                      l_use_C7_Richardson, & ! In
    4367             :                                                      l_use_C11_Richardson, & ! In
    4368             :                                                      l_use_shear_Richardson, & ! In
    4369             :                                                      l_brunt_vaisala_freq_moist, & ! In
    4370             :                                                      l_use_thvm_in_bv_freq, & ! In
    4371             :                                                      l_rcm_supersat_adj, & ! In
    4372             :                                                      l_damp_wp3_Skw_squared, & ! In
    4373             :                                                      l_prescribed_avg_deltaz, & ! In
    4374             :                                                      l_lmm_stepping, & ! In
    4375             :                                                      l_e3sm_config, & ! In
    4376             :                                                      l_vary_convect_depth, & ! In
    4377             :                                                      l_use_tke_in_wp3_pr_turb_term, & ! In
    4378             :                                                      l_use_tke_in_wp2_wp3_K_dfsn, & ! In
    4379             :                                                      l_use_wp3_lim_with_smth_Heaviside, & ! In
    4380             :                                                      l_smooth_Heaviside_tau_wpxp, & ! In
    4381             :                                                      l_modify_limiters_for_cnvg_test, & ! In
    4382             :                                                      l_enable_relaxed_clipping, & ! In
    4383             :                                                      l_linearize_pbl_winds, & ! In
    4384             :                                                      l_mono_flux_lim_thlm, & ! In
    4385             :                                                      l_mono_flux_lim_rtm, & ! In
    4386             :                                                      l_mono_flux_lim_um, & ! In
    4387             :                                                      l_mono_flux_lim_vm, & ! In
    4388             :                                                      l_mono_flux_lim_spikefix, & ! In
    4389             :                                                      clubb_config_flags ) ! Out
    4390             : 
    4391             :     use model_flags, only: &
    4392             :         clubb_config_flags_type, &          ! Type
    4393             :         initialize_clubb_config_flags_type  ! Procedure
    4394             : 
    4395             :     implicit none
    4396             : 
    4397             :     ! Input variables
    4398             :     integer, intent(in) :: &
    4399             :       iiPDF_type,          & ! Selected option for the two-component normal
    4400             :                              ! (double Gaussian) PDF type to use for the w, rt,
    4401             :                              ! and theta-l (or w, chi, and eta) portion of
    4402             :                              ! CLUBB's multivariate, two-component PDF.
    4403             :       ipdf_call_placement, & ! Selected option for the placement of the call to
    4404             :                              ! CLUBB's PDF.
    4405             :       penta_solve_method,  & ! Option to set the penta-diagonal matrix solving method
    4406             :       tridiag_solve_method   ! Option to set the tri-diagonal matrix solving method
    4407             : 
    4408             :     logical, intent(in) :: &
    4409             :       l_use_precip_frac,            & ! Flag to use precipitation fraction in KK microphysics. The
    4410             :                                       ! precipitation fraction is automatically set to 1 when this
    4411             :                                       ! flag is turned off.
    4412             :       l_predict_upwp_vpwp,          & ! Flag to predict <u'w'> and <v'w'> along with <u> and <v>
    4413             :                                       ! alongside the advancement of <rt>, <w'rt'>, <thl>,
    4414             :                                       ! <wpthlp>, <sclr>, and <w'sclr'> in subroutine
    4415             :                                       ! advance_xm_wpxp.  Otherwise, <u'w'> and <v'w'> are still
    4416             :                                       ! approximated by eddy diffusivity when <u> and <v> are
    4417             :                                       ! advanced in subroutine advance_windm_edsclrm.
    4418             :       l_min_wp2_from_corr_wx,       & ! Flag to base the threshold minimum value of wp2 on keeping
    4419             :                                       ! the overall correlation of w and x (w and rt, as well as w
    4420             :                                       ! and theta-l) within the limits of -max_mag_correlation_flux
    4421             :                                       ! to max_mag_correlation_flux.
    4422             :       l_min_xp2_from_corr_wx,       & ! Flag to base the threshold minimum value of xp2 (rtp2 and
    4423             :                                       ! thlp2) on keeping the overall correlation of w and x within
    4424             :                                       ! the limits of -max_mag_correlation_flux to
    4425             :                                       ! max_mag_correlation_flux.
    4426             :       l_C2_cloud_frac,              & ! Flag to use cloud fraction to adjust the value of the
    4427             :                                       ! turbulent dissipation coefficient, C2.
    4428             :       l_diffuse_rtm_and_thlm,       & ! Diffuses rtm and thlm
    4429             :       l_stability_correct_Kh_N2_zm, & ! Divides Kh_N2_zm by a stability factor
    4430             :       l_calc_thlp2_rad,             & ! Include the contribution of radiation to thlp2
    4431             :       l_upwind_xpyp_ta,             & ! This flag determines whether we want to use an upwind
    4432             :                                       ! differencing approximation rather than a centered
    4433             :                                       ! differencing for turbulent or mean advection terms. It
    4434             :                                       ! affects rtp2, thlp2, up2, vp2, sclrp2, rtpthlp, sclrprtp, &
    4435             :                                       ! sclrpthlp.
    4436             :       l_upwind_xm_ma,               & ! This flag determines whether we want to use an upwind
    4437             :                                       ! differencing approximation rather than a centered
    4438             :                                       ! differencing for turbulent or mean advection terms. It
    4439             :                                       ! affects rtm, thlm, sclrm, um and vm.
    4440             :       l_uv_nudge,                   & ! For wind speed nudging.
    4441             :       l_rtm_nudge,                  & ! For rtm nudging
    4442             :       l_tke_aniso,                  & ! For anisotropic turbulent kinetic energy, i.e.
    4443             :                                       ! TKE = 1/2 (u'^2 + v'^2 + w'^2)
    4444             :       l_vert_avg_closure,           & ! Use 2 calls to pdf_closure and the trapezoidal rule to
    4445             :                                       ! compute the varibles that are output from high order
    4446             :                                       ! closure
    4447             :       l_trapezoidal_rule_zt,        & ! If true, the trapezoidal rule is called for the
    4448             :                                       ! thermodynamic-level variables output from pdf_closure.
    4449             :       l_trapezoidal_rule_zm,        & ! If true, the trapezoidal rule is called for three
    4450             :                                       ! momentum-level variables - wpthvp, thlpthvp, and rtpthvp -
    4451             :                                       ! output from pdf_closure.
    4452             :       l_call_pdf_closure_twice,     & ! This logical flag determines whether or not to call
    4453             :                                       ! subroutine pdf_closure twice.  If true, pdf_closure is
    4454             :                                       ! called first on thermodynamic levels and then on momentum
    4455             :                                       ! levels so that each variable is computed on its native
    4456             :                                       ! level.  If false, pdf_closure is only called on
    4457             :                                       ! thermodynamic levels, and variables which belong on
    4458             :                                       ! momentum levels are interpolated.
    4459             :       l_standard_term_ta,           & ! Use the standard discretization for the turbulent advection
    4460             :                                       ! terms.  Setting to .false. means that a_1 and a_3 are
    4461             :                                       ! pulled outside of the derivative in
    4462             :                                       ! advance_wp2_wp3_module.F90 and in
    4463             :                                       ! advance_xp2_xpyp_module.F90.
    4464             :       l_partial_upwind_wp3,         & ! Flag to use an "upwind" discretization rather
    4465             :                                       ! than a centered discretization for the portion
    4466             :                                       ! of the wp3 turbulent advection term for ADG1
    4467             :                                       ! that is linearized in terms of wp3<t+1>.
    4468             :                                       ! (Requires ADG1 PDF and l_standard_term_ta).
    4469             :       l_godunov_upwind_wpxp_ta,     & ! This flag determines whether we want to use an upwind
    4470             :                                       ! differencing approximation rather than a centered
    4471             :                                       ! differencing for turbulent advection terms. 
    4472             :                                       ! It affects  wpxp only.
    4473             :       l_godunov_upwind_xpyp_ta,     & ! This flag determines whether we want to use an upwind
    4474             :                                       ! differencing approximation rather than a centered 
    4475             :                                       ! differencing for turbulent advection terms. It affects
    4476             :                                       ! xpyp only.
    4477             :       l_use_cloud_cover,            & ! Use cloud_cover and rcm_in_layer to help boost cloud_frac
    4478             :                                       ! and rcm to help increase cloudiness at coarser grid
    4479             :                                       ! resolutions.
    4480             :       l_diagnose_correlations,      & ! Diagnose correlations instead of using fixed ones
    4481             :       l_calc_w_corr,                & ! Calculate the correlations between w and the hydrometeors
    4482             :       l_const_Nc_in_cloud,          & ! Use a constant cloud droplet conc. within cloud (K&K)
    4483             :       l_fix_w_chi_eta_correlations, & ! Use a fixed correlation for s and t Mellor(chi/eta)
    4484             :       l_stability_correct_tau_zm,   & ! Use tau_N2_zm instead of tau_zm in wpxp_pr1 stability
    4485             :                                       ! correction
    4486             :       l_damp_wp2_using_em,          & ! In wp2 equation, use a dissipation formula of
    4487             :                                       ! -(2/3)*em/tau_zm, as in Bougeault (1981)
    4488             :       l_do_expldiff_rtm_thlm,       & ! Diffuse rtm and thlm explicitly
    4489             :       l_Lscale_plume_centered,      & ! Alternate that uses the PDF to compute the perturbed values
    4490             :       l_diag_Lscale_from_tau,       & ! First diagnose dissipation time tau, and then diagnose the
    4491             :                                       ! mixing length scale as Lscale = tau * tke
    4492             :       l_use_C7_Richardson,          & ! Parameterize C7 based on Richardson number
    4493             :       l_use_C11_Richardson,         & ! Parameterize C11 and C16 based on Richardson number
    4494             :       l_use_shear_Richardson,       & ! Use shear in the calculation of Richardson number
    4495             :       l_brunt_vaisala_freq_moist,   & ! Use a different formula for the Brunt-Vaisala frequency in
    4496             :                                       ! saturated atmospheres (from Durran and Klemp, 1982)
    4497             :       l_use_thvm_in_bv_freq,        & ! Use thvm in the calculation of Brunt-Vaisala frequency
    4498             :       l_rcm_supersat_adj,           & ! Add excess supersaturated vapor to cloud water
    4499             :       l_damp_wp3_Skw_squared,       & ! Set damping on wp3 to use Skw^2 rather than Skw^4
    4500             :       l_prescribed_avg_deltaz,      & ! used in adj_low_res_nu. If .true., avg_deltaz = deltaz
    4501             :       l_lmm_stepping,               & ! Apply Linear Multistep Method (LMM) Stepping
    4502             :       l_e3sm_config,                & ! Run model with E3SM settings
    4503             :       l_vary_convect_depth,         & ! Flag used to calculate convective velocity using
    4504             :                                       ! a variable estimate of layer depth based on the depth
    4505             :                                       ! over which wpthlp is positive near the ground when true
    4506             :                                       ! More information can be found by
    4507             :                                       ! Looking at issue #905 on the clubb repo
    4508             :       l_use_tke_in_wp3_pr_turb_term,& ! Use TKE formulation for wp3 pr_turb term
    4509             :       l_use_tke_in_wp2_wp3_K_dfsn,  & ! Use TKE in eddy diffusion for wp2 and wp3
    4510             :       l_use_wp3_lim_with_smth_Heaviside, & ! Flag to activate mods on wp3 limiters for conv test
    4511             :       l_smooth_Heaviside_tau_wpxp,  & ! Use smoothed Heaviside 'Peskin' function
    4512             :                                       ! in the calculation of H_invrs_tau_wpxp_N2
    4513             :                                       ! in src/CLUBB_core/mixing_length.F90
    4514             :       l_modify_limiters_for_cnvg_test, & ! Flag to activate mods on limiters for conv test
    4515             :       l_enable_relaxed_clipping,    & ! Flag to relax clipping on wpxp
    4516             :                                       ! in xm_wpxp_clipping_and_stats
    4517             :       l_linearize_pbl_winds,        & ! Code to linearize PBL winds
    4518             :       l_mono_flux_lim_thlm,         & ! Flag to turn on monotonic flux limiter for thlm
    4519             :       l_mono_flux_lim_rtm,          & ! Flag to turn on monotonic flux limiter for rtm
    4520             :       l_mono_flux_lim_um,           & ! Flag to turn on monotonic flux limiter for um
    4521             :       l_mono_flux_lim_vm,           & ! Flag to turn on monotonic flux limiter for vm
    4522             :       l_mono_flux_lim_spikefix        ! Flag to implement monotonic flux limiter code that
    4523             :                                       ! eliminates spurious drying tendencies at model top
    4524             : 
    4525             :     ! Output variables
    4526             :     type(clubb_config_flags_type), intent(out) :: &
    4527             :       clubb_config_flags            ! Derived type holding all configurable CLUBB flags
    4528             : 
    4529             :     call initialize_clubb_config_flags_type( iiPDF_type, & ! In
    4530             :                                              ipdf_call_placement, & ! In 
    4531             :                                              penta_solve_method, & ! In
    4532             :                                              tridiag_solve_method, & ! In
    4533             :                                              l_use_precip_frac, & ! In
    4534             :                                              l_predict_upwp_vpwp, & ! In
    4535             :                                              l_min_wp2_from_corr_wx, & ! In
    4536             :                                              l_min_xp2_from_corr_wx, & ! In
    4537             :                                              l_C2_cloud_frac, & ! In
    4538             :                                              l_diffuse_rtm_and_thlm, & ! In
    4539             :                                              l_stability_correct_Kh_N2_zm, & ! In
    4540             :                                              l_calc_thlp2_rad, & ! In
    4541             :                                              l_upwind_xpyp_ta, & ! In
    4542             :                                              l_upwind_xm_ma, & ! In
    4543             :                                              l_uv_nudge, & ! In
    4544             :                                              l_rtm_nudge, & ! In
    4545             :                                              l_tke_aniso, & ! In
    4546             :                                              l_vert_avg_closure, & ! In
    4547             :                                              l_trapezoidal_rule_zt, & ! In
    4548             :                                              l_trapezoidal_rule_zm, & ! In
    4549             :                                              l_call_pdf_closure_twice, & ! In
    4550             :                                              l_standard_term_ta, & ! In
    4551             :                                              l_partial_upwind_wp3, & ! In
    4552             :                                              l_godunov_upwind_wpxp_ta, & ! In
    4553             :                                              l_godunov_upwind_xpyp_ta, & ! In
    4554             :                                              l_use_cloud_cover, & ! In
    4555             :                                              l_diagnose_correlations, & ! In
    4556             :                                              l_calc_w_corr, & ! In
    4557             :                                              l_const_Nc_in_cloud, & ! In
    4558             :                                              l_fix_w_chi_eta_correlations, & ! In
    4559             :                                              l_stability_correct_tau_zm, & ! In
    4560             :                                              l_damp_wp2_using_em, & ! In
    4561             :                                              l_do_expldiff_rtm_thlm, & ! In
    4562             :                                              l_Lscale_plume_centered, & ! In
    4563             :                                              l_diag_Lscale_from_tau, & ! In
    4564             :                                              l_use_C7_Richardson, & ! In
    4565             :                                              l_use_C11_Richardson, & ! In
    4566             :                                              l_use_shear_Richardson, & ! In
    4567             :                                              l_brunt_vaisala_freq_moist, & ! In
    4568             :                                              l_use_thvm_in_bv_freq, & ! In
    4569             :                                              l_rcm_supersat_adj, & ! In
    4570             :                                              l_damp_wp3_Skw_squared, & ! In
    4571             :                                              l_prescribed_avg_deltaz, & ! In
    4572             :                                              l_lmm_stepping, & ! In
    4573             :                                              l_e3sm_config, & ! In
    4574             :                                              l_vary_convect_depth, & ! In
    4575             :                                              l_use_tke_in_wp3_pr_turb_term, & ! In
    4576             :                                              l_use_tke_in_wp2_wp3_K_dfsn, & ! In
    4577             :                                              l_use_wp3_lim_with_smth_Heaviside, & ! In
    4578             :                                              l_smooth_Heaviside_tau_wpxp, & ! In
    4579             :                                              l_modify_limiters_for_cnvg_test, & ! In
    4580             :                                              l_enable_relaxed_clipping, & ! In
    4581             :                                              l_linearize_pbl_winds, & ! In
    4582             :                                              l_mono_flux_lim_thlm, & ! In
    4583             :                                              l_mono_flux_lim_rtm, & ! In
    4584             :                                              l_mono_flux_lim_um, & ! In
    4585             :                                              l_mono_flux_lim_vm, & ! In
    4586             :                                              l_mono_flux_lim_spikefix, & ! In
    4587        1536 :                                              clubb_config_flags ) ! Out
    4588             : 
    4589        1536 :   end subroutine initialize_clubb_config_flags_type_api
    4590             : 
    4591             :   !================================================================================================
    4592             :   ! print_clubb_config_flags: Prints the clubb_config_flags
    4593             :   !================================================================================================
    4594           2 :   subroutine print_clubb_config_flags_api( iunit, clubb_config_flags ) ! In
    4595             : 
    4596             :     use model_flags, only: &
    4597             :         clubb_config_flags_type, &          ! Type
    4598             :         print_clubb_config_flags            ! Procedure
    4599             : 
    4600             :     implicit none
    4601             : 
    4602             :     ! Input variables
    4603             :     integer, intent(in) :: &
    4604             :       iunit ! The file to write to
    4605             : 
    4606             :     type(clubb_config_flags_type), intent(in) :: &
    4607             :       clubb_config_flags ! Derived type holding all configurable CLUBB flags
    4608             : 
    4609           2 :     call print_clubb_config_flags( iunit, clubb_config_flags ) ! In
    4610             : 
    4611           2 :   end subroutine print_clubb_config_flags_api
    4612             : 
    4613             :   !================================================================================================
    4614             :   ! initialize_tau_sponge_damp
    4615             :   !================================================================================================
    4616           0 :   subroutine initialize_tau_sponge_damp_api( gr, dt, z, settings, damping_profile )
    4617             : 
    4618             :     use sponge_layer_damping, only: &
    4619             :         sponge_damp_settings,       & ! Variable(s)
    4620             :         sponge_damp_profile,        &
    4621             :         initialize_tau_sponge_damp    ! Procedure(s)
    4622             : 
    4623             :     
    4624             : 
    4625             :     implicit none
    4626             : 
    4627             :     type(grid), target, intent(in) :: gr
    4628             : 
    4629             :     ! Input Variable(s)
    4630             :     real( kind = core_rknd ), intent(in) :: &
    4631             :       dt    ! Model Timestep    [s]
    4632             : 
    4633             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
    4634             :       z    ! Height of model grid levels    [m]
    4635             : 
    4636             :     type(sponge_damp_settings), intent(in) :: &
    4637             :       settings
    4638             : 
    4639             :     ! Output Variable(s)
    4640             :     type(sponge_damp_profile), intent(out) :: &
    4641             :       damping_profile
    4642             : 
    4643             :     call initialize_tau_sponge_damp( gr, dt, z, settings, & ! intent(in)
    4644           0 :                                      damping_profile ) ! intent(inout)
    4645             : 
    4646           0 :   end subroutine initialize_tau_sponge_damp_api
    4647             : 
    4648             :   !================================================================================================
    4649             :   ! finalize_tau_sponge_damp
    4650             :   !================================================================================================
    4651           0 :   subroutine finalize_tau_sponge_damp_api( damping_profile )
    4652             : 
    4653             :     use sponge_layer_damping, only: &
    4654             :         sponge_damp_profile,      & ! Variable(s)
    4655             :         finalize_tau_sponge_damp    ! Procedure(s)
    4656             : 
    4657             :     implicit none
    4658             : 
    4659             :     ! Input/Output Variable(s)
    4660             :     type(sponge_damp_profile), intent(inout) :: &
    4661             :       damping_profile ! Information for damping the profile
    4662             : 
    4663           0 :     call finalize_tau_sponge_damp( damping_profile ) ! intent(inout)
    4664             : 
    4665           0 :   end subroutine finalize_tau_sponge_damp_api
    4666             :     
    4667             : end module clubb_api_module

Generated by: LCOV version 1.14