LCOV - code coverage report
Current view: top level - physics/clubb/src/CLUBB_core - stats_clubb_utilities.F90 (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 1140 0.0 %
Date: 2025-03-13 18:42:46 Functions: 0 10 0.0 %

          Line data    Source code
       1             : !-----------------------------------------------------------------------
       2             : !  $Id$
       3             : !===============================================================================
       4             : module stats_clubb_utilities
       5             : 
       6             :   implicit none
       7             : 
       8             :   private ! Set Default Scope
       9             : 
      10             :   public :: stats_init, stats_begin_timestep, stats_end_timestep, & 
      11             :     stats_accumulate, stats_finalize, stats_accumulate_hydromet, &
      12             :     stats_accumulate_lh_tend
      13             : 
      14             :   private :: stats_zero, stats_avg, stats_check_num_samples
      15             : 
      16             :   contains
      17             : 
      18             :   !-----------------------------------------------------------------------
      19           0 :   subroutine stats_init( iunit, fname_prefix, fdir, l_stats_in, &
      20             :                          stats_fmt_in, stats_tsamp_in, stats_tout_in, fnamelist, &
      21           0 :                          nzmax, nlon, nlat, gzt, gzm, nnrad_zt, &
      22           0 :                          grad_zt, nnrad_zm, grad_zm, day, month, year, &
      23           0 :                          lon_vals, lat_vals, time_current, delt, l_silhs_out_in, &
      24             :                          stats_metadata, &
      25             :                          stats_zt, stats_zm, stats_sfc, &
      26             :                          stats_lh_zt, stats_lh_sfc, &
      27             :                          stats_rad_zt, stats_rad_zm )
      28             :     !
      29             :     ! Description:
      30             :     !   Initializes the statistics saving functionality of the CLUBB model.
      31             :     !
      32             :     ! References:
      33             :     !   None
      34             :     !-----------------------------------------------------------------------
      35             : 
      36             :     use stats_variables, only: &
      37             :         stats_metadata_type
      38             : 
      39             :     use clubb_precision, only: &
      40             :         time_precision, & ! Constant(s)
      41             :         core_rknd
      42             : 
      43             :     use output_grads, only: &
      44             :         open_grads ! Procedure
      45             : 
      46             : #ifdef NETCDF
      47             :     use output_netcdf, only: &
      48             :         open_netcdf_for_writing     ! Procedure
      49             : #endif
      50             : 
      51             :     use stats_zm_module, only: &
      52             :         nvarmax_zm, & ! Constant(s)
      53             :         stats_init_zm ! Procedure(s)
      54             : 
      55             :     use stats_zt_module, only: &
      56             :         nvarmax_zt, & ! Constant(s)
      57             :         stats_init_zt ! Procedure(s)
      58             : 
      59             :     use stats_lh_zt_module, only: &
      60             :         nvarmax_lh_zt, & ! Constant(s)
      61             :         stats_init_lh_zt ! Procedure(s)
      62             : 
      63             :     use stats_lh_sfc_module, only: &
      64             :         nvarmax_lh_sfc, & ! Constant(s)
      65             :         stats_init_lh_sfc ! Procedure(s)
      66             : 
      67             :     use stats_rad_zt_module, only: &
      68             :         nvarmax_rad_zt, & ! Constant(s)
      69             :         stats_init_rad_zt ! Procedure(s)
      70             : 
      71             :     use stats_rad_zm_module, only: &
      72             :         nvarmax_rad_zm, & ! Constant(s)
      73             :         stats_init_rad_zm ! Procedure(s)
      74             : 
      75             :     use stats_sfc_module, only: &
      76             :         nvarmax_sfc, & ! Constant(s)
      77             :         stats_init_sfc ! Procedure(s)
      78             : 
      79             :     use constants_clubb, only: &
      80             :         fstdout, fstderr, var_length ! Constants
      81             : 
      82             :     use parameters_model, only: &
      83             :         hydromet_dim, &  ! Variable(s)
      84             :         sclr_dim, &
      85             :         edsclr_dim
      86             : 
      87             :     use error_code, only: &
      88             :         clubb_at_least_debug_level, &   ! Procedure
      89             :         err_code, &                     ! Error Indicator
      90             :         clubb_fatal_error               ! Constant
      91             : 
      92             :     use stats_type, only: stats ! Type
      93             : 
      94             :     implicit none
      95             : 
      96             :     ! Local Constants
      97             :     integer, parameter :: &
      98             :       silhs_num_importance_categories = 8
      99             : 
     100             :     ! Input Variables
     101             :     integer, intent(in) :: iunit  ! File unit for fnamelist
     102             : 
     103             :     character(len=*), intent(in) ::  & 
     104             :       fname_prefix, & ! Start of the stats filenames
     105             :       fdir            ! Directory to output to
     106             : 
     107             :     logical, intent(in) :: &
     108             :       l_stats_in      ! Stats on? T/F
     109             : 
     110             :     character(len=*), intent(in) :: &
     111             :       stats_fmt_in    ! Format of the stats file output
     112             : 
     113             :     real( kind = core_rknd ), intent(in) ::  & 
     114             :       stats_tsamp_in,  & ! Sampling interval   [s]
     115             :       stats_tout_in      ! Output interval     [s]
     116             : 
     117             :     character(len=*), intent(in) :: &
     118             :       fnamelist          ! Filename holding the &statsnl
     119             : 
     120             :     integer, intent(in) :: &
     121             :       nlon, & ! Number of points in the X direction [-]
     122             :       nlat, & ! Number of points in the Y direction [-]
     123             :       nzmax   ! Grid points in the vertical         [-]
     124             : 
     125             :     real( kind = core_rknd ), intent(in), dimension(nzmax) ::  & 
     126             :       gzt, gzm  ! Thermodynamic and momentum levels           [m]
     127             : 
     128             :     integer, intent(in) :: nnrad_zt ! Grid points in the radiation grid [count]
     129             : 
     130             :     real( kind = core_rknd ), intent(in), dimension(nnrad_zt) :: grad_zt ! Radiation levels [m]
     131             : 
     132             :     integer, intent(in) :: nnrad_zm ! Grid points in the radiation grid [count]
     133             : 
     134             :     real( kind = core_rknd ), intent(in), dimension(nnrad_zm) :: grad_zm ! Radiation levels [m]
     135             : 
     136             :     integer, intent(in) :: day, month, year  ! Time of year
     137             : 
     138             :     real( kind = core_rknd ), dimension(nlon), intent(in) ::  & 
     139             :       lon_vals  ! Longitude values [Degrees E]
     140             : 
     141             :     real( kind = core_rknd ), dimension(nlat), intent(in) ::  & 
     142             :       lat_vals  ! Latitude values  [Degrees N]
     143             : 
     144             :     real( kind = time_precision ), intent(in) ::  & 
     145             :       time_current ! Model time                         [s]
     146             : 
     147             :     real( kind = core_rknd ), intent(in) ::  & 
     148             :       delt         ! Timestep (dt_main in CLUBB)         [s]
     149             : 
     150             :     logical, intent(in) :: &
     151             :       l_silhs_out_in  ! Whether to output SILHS files (stats_lh_zt, stats_lh_sfc)  [boolean]
     152             : 
     153             :     type (stats), target, intent(inout) :: &
     154             :       stats_zt, &
     155             :       stats_zm, &
     156             :       stats_sfc, &
     157             :       stats_lh_zt, &
     158             :       stats_lh_sfc, &
     159             :       stats_rad_zt, &
     160             :       stats_rad_zm
     161             : 
     162             :     type (stats_metadata_type), intent(inout) :: &
     163             :       stats_metadata
     164             : 
     165             :     ! Local Variables
     166             :     logical :: l_error
     167             : 
     168             :     character(len=200) :: fname
     169             : 
     170             :     integer :: ivar, ntot, read_status
     171             : 
     172             :     ! Namelist Variables
     173             : 
     174             :     character(len=10) :: stats_fmt  ! File storage convention
     175             : 
     176             :     character(len=var_length), dimension(nvarmax_zt) ::  & 
     177             :       vars_zt  ! Variables on the thermodynamic levels
     178             : 
     179             :     character(len=var_length), dimension(nvarmax_lh_zt) ::  & 
     180             :       vars_lh_zt  ! Latin Hypercube variables on the thermodynamic levels
     181             : 
     182             :     character(len=var_length), dimension(nvarmax_lh_sfc) ::  & 
     183             :       vars_lh_sfc  ! Latin Hypercube variables at the surface
     184             : 
     185             :     character(len=var_length), dimension(nvarmax_zm) ::  & 
     186             :       vars_zm  ! Variables on the momentum levels
     187             : 
     188             :     character(len=var_length), dimension(nvarmax_rad_zt) ::  & 
     189             :       vars_rad_zt  ! Variables on the radiation levels
     190             : 
     191             :     character(len=var_length), dimension(nvarmax_rad_zm) ::  & 
     192             :       vars_rad_zm  ! Variables on the radiation levels
     193             : 
     194             :     character(len=var_length), dimension(nvarmax_sfc) ::  &
     195             :       vars_sfc ! Variables at the model surface
     196             : 
     197             :     namelist /statsnl/ & 
     198             :       vars_zt, & 
     199             :       vars_zm, &
     200             :       vars_lh_zt, &
     201             :       vars_lh_sfc, &
     202             :       vars_rad_zt, &
     203             :       vars_rad_zm, & 
     204             :       vars_sfc
     205             : 
     206             :     ! ---- Begin Code ----
     207             : 
     208             :     ! Initialize
     209           0 :     l_error = .false.
     210             : 
     211             :     ! Set stats_variables variables with inputs from calling subroutine
     212           0 :     stats_metadata%l_stats = l_stats_in
     213             : 
     214           0 :     stats_metadata%stats_tsamp = stats_tsamp_in
     215           0 :     stats_metadata%stats_tout  = stats_tout_in
     216           0 :     stats_fmt   = trim( stats_fmt_in )
     217           0 :     stats_metadata%l_silhs_out = l_silhs_out_in
     218             : 
     219           0 :     if ( .not. stats_metadata%l_stats ) then
     220           0 :       stats_metadata%l_stats_samp  = .false.
     221           0 :       stats_metadata%l_stats_last  = .false.
     222           0 :       return
     223             :     end if
     224             : 
     225             :     ! Initialize namelist variables
     226             : 
     227           0 :     vars_zt  = ''
     228           0 :     vars_zm  = ''
     229           0 :     vars_lh_zt = ''
     230           0 :     vars_lh_sfc = ''
     231           0 :     vars_rad_zt = ''
     232           0 :     vars_rad_zm = ''
     233           0 :     vars_sfc = ''
     234             : 
     235             :     ! Reads list of variables that should be output to GrADS/NetCDF (namelist &statsnl)
     236             : 
     237           0 :     open(unit=iunit, file=fnamelist)
     238           0 :     read(unit=iunit, nml=statsnl, iostat=read_status, end=100)
     239           0 :     if ( read_status /= 0 ) then
     240           0 :       if ( read_status > 0 ) then
     241           0 :         write(fstderr,*) "Error reading stats namelist in file ",  &
     242           0 :                          trim( fnamelist )
     243             :       else ! Read status < 0
     244           0 :         write(fstderr,*) "End of file marker reached while reading stats namelist in file ", &
     245           0 :           trim( fnamelist )
     246             :       end if
     247           0 :       write(fstderr,*) "One cause is having more statistical variables ",  &
     248           0 :                        "listed in the namelist for var_zt, var_zm, or ",  &
     249           0 :                        "var_sfc than allowed by nvarmax_zt, nvarmax_zm, ",  &
     250           0 :                        "or nvarmax_sfc, respectively."
     251           0 :       write(fstderr,*) "Maximum variables allowed for var_zt = ", nvarmax_zt
     252           0 :       write(fstderr,*) "Maximum variables allowed for var_zm = ", nvarmax_zm
     253           0 :       write(fstderr,*) "Maximum variables allowed for var_rad_zt = ", nvarmax_rad_zt
     254           0 :       write(fstderr,*) "Maximum variables allowed for var_rad_zm = ", nvarmax_rad_zm
     255           0 :       write(fstderr,*) "Maximum variables allowed for var_sfc = ", nvarmax_sfc
     256           0 :       write(fstderr,*) "stats_init: Error reading stats namelist."
     257           0 :       err_code = clubb_fatal_error
     258           0 :       close(unit=iunit)
     259           0 :       return
     260             :     end if ! read_status /= 0
     261             : 
     262           0 :     close(unit=iunit)
     263             : 
     264           0 :     if ( clubb_at_least_debug_level( 1 ) ) then
     265           0 :       write(fstdout,*) "--------------------------------------------------"
     266             : 
     267           0 :       write(fstdout,*) "Statistics"
     268             : 
     269           0 :       write(fstdout,*) "--------------------------------------------------"
     270           0 :       write(fstdout,*) "vars_zt = "
     271           0 :       ivar = 1
     272           0 :       do while ( vars_zt(ivar) /= '' )
     273           0 :         write(fstdout,*) vars_zt(ivar)
     274           0 :         ivar = ivar + 1
     275             :       end do
     276             : 
     277           0 :       write(fstdout,*) "vars_zm = "
     278           0 :       ivar = 1
     279           0 :       do while ( vars_zm(ivar) /= '' )
     280           0 :         write(fstdout,*) vars_zm(ivar)
     281           0 :         ivar = ivar + 1
     282             :       end do
     283             : 
     284           0 :       if ( stats_metadata%l_silhs_out ) then
     285           0 :         write(fstdout,*) "vars_lh_zt = "
     286           0 :         ivar = 1
     287           0 :         do while ( vars_lh_zt(ivar) /= '' )
     288           0 :           write(fstdout,*) vars_lh_zt(ivar)
     289           0 :           ivar = ivar + 1
     290             :         end do
     291             : 
     292           0 :         write(fstdout,*) "vars_lh_sfc = "
     293           0 :         ivar = 1
     294           0 :         do while ( vars_lh_sfc(ivar) /= '' )
     295           0 :           write(fstdout,*) vars_lh_sfc(ivar)
     296           0 :           ivar = ivar + 1
     297             :         end do
     298             :       end if ! l_silhs_out
     299             : 
     300           0 :       if ( stats_metadata%l_output_rad_files ) then
     301           0 :         write(fstdout,*) "vars_rad_zt = "
     302           0 :         ivar = 1
     303           0 :         do while ( vars_rad_zt(ivar) /= '' )
     304           0 :           write(fstdout,*) vars_rad_zt(ivar)
     305           0 :           ivar = ivar + 1
     306             :         end do
     307             : 
     308           0 :         write(fstdout,*) "vars_rad_zm = "
     309           0 :         ivar = 1
     310           0 :         do while ( vars_rad_zm(ivar) /= '' )
     311           0 :           write(fstdout,*) vars_rad_zm(ivar)
     312           0 :           ivar = ivar + 1
     313             :         end do
     314             :       end if ! l_output_rad_files
     315             : 
     316           0 :       write(fstdout,*) "vars_sfc = "
     317           0 :       ivar = 1
     318           0 :       do while ( vars_sfc(ivar) /= '' )
     319           0 :         write(fstdout,*) vars_sfc(ivar)
     320           0 :         ivar = ivar + 1
     321             :       end do
     322             : 
     323           0 :       write(fstdout,*) "--------------------------------------------------"
     324             :     end if ! clubb_at_least_debug_level 1
     325             : 
     326             :     ! Determine file names for GrADS or NetCDF files
     327           0 :     stats_metadata%fname_zt  = trim( fname_prefix )//"_zt"
     328           0 :     stats_metadata%fname_zm  = trim( fname_prefix )//"_zm"
     329           0 :     stats_metadata%fname_lh_zt  = trim( fname_prefix )//"_lh_zt"
     330           0 :     stats_metadata%fname_lh_sfc  = trim( fname_prefix )//"_lh_sfc"
     331           0 :     stats_metadata%fname_rad_zt  = trim( fname_prefix )//"_rad_zt"
     332           0 :     stats_metadata%fname_rad_zm  = trim( fname_prefix )//"_rad_zm"
     333           0 :     stats_metadata%fname_sfc = trim( fname_prefix )//"_sfc"
     334             : 
     335             :     ! Parse the file type for stats output.  Currently only GrADS and
     336             :     ! netCDF > version 3.5 are supported by this code.
     337           0 :     select case ( trim( stats_fmt ) )
     338             :     case ( "GrADS", "grads", "gr" )
     339           0 :       stats_metadata%l_netcdf = .false.
     340           0 :       stats_metadata%l_grads  = .true.
     341             : 
     342             :     case ( "NetCDF", "netcdf", "nc" )
     343           0 :       stats_metadata%l_netcdf = .true.
     344           0 :       stats_metadata%l_grads  = .false.
     345             : 
     346             :     case default
     347           0 :       write(fstderr,*) "In module stats_clubb_utilities subroutine stats_init: "
     348           0 :       write(fstderr,*) "Invalid stats output format "//trim( stats_fmt )
     349           0 :       err_code = clubb_fatal_error
     350           0 :       return
     351             : 
     352             :     end select
     353             : 
     354             :     ! Check sampling and output frequencies
     355             : 
     356             :     ! The model time step length, delt (which is dt_main), should multiply
     357             :     ! evenly into the statistical sampling time step length, stats_tsamp.
     358           0 :     if ( abs( stats_metadata%stats_tsamp/delt &
     359             :               - real( floor( stats_metadata%stats_tsamp/delt ), kind=core_rknd ) )  & 
     360             :            > 1.e-8_core_rknd) then
     361           0 :       l_error = .true.  ! This will cause the run to stop.
     362           0 :       write(fstderr,*) 'Error:  stats_tsamp should be an even multiple of ',  &
     363           0 :                        'delt (which is dt_main).  Check the appropriate ',  &
     364           0 :                        'model.in file.'
     365           0 :       write(fstderr,*) 'stats_tsamp = ', stats_metadata%stats_tsamp
     366           0 :       write(fstderr,*) 'delt = ', delt
     367             :     end if
     368             : 
     369             :     ! The statistical sampling time step length, stats_tsamp, should multiply
     370             :     ! evenly into the statistical output time step length, stats_tout.
     371           0 :     if ( abs( stats_metadata%stats_tout/stats_metadata%stats_tsamp &
     372             :            - real( floor( stats_metadata%stats_tout    &
     373             :                           / stats_metadata%stats_tsamp ), kind=core_rknd) ) & 
     374             :          > 1.e-8_core_rknd) then
     375           0 :       l_error = .true.  ! This will cause the run to stop.
     376           0 :       write(fstderr,*) 'Error:  stats_tout should be an even multiple of ',  &
     377           0 :                        'stats_tsamp.  Check the appropriate model.in file.'
     378           0 :       write(fstderr,*) 'stats_tout = ', stats_metadata%stats_tout
     379           0 :       write(fstderr,*) 'stats_tsamp = ', stats_metadata%stats_tsamp
     380             :     end if
     381             : 
     382             :     ! Initialize zt (mass points)
     383             : 
     384           0 :     ivar = 1
     385           0 :     do while ( ichar(vars_zt(ivar)(1:1)) /= 0  & 
     386             :                .and. len_trim(vars_zt(ivar)) /= 0 & 
     387           0 :                .and. ivar <= nvarmax_zt )
     388           0 :       ivar = ivar + 1
     389             :     end do
     390           0 :     ntot = ivar - 1
     391             : 
     392           0 :     if ( any( vars_zt == "hm_i" ) ) then
     393             :        ! Correct for number of variables found under "hm_i".
     394             :        ! Subtract "hm_i" from the number of zt statistical variables.
     395           0 :        ntot = ntot - 1
     396             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     397             :        ! to the number of zt statistical variables.
     398           0 :        ntot = ntot + 2 * hydromet_dim
     399             :     endif
     400           0 :     if ( any( vars_zt == "mu_hm_i" ) ) then
     401             :        ! Correct for number of variables found under "mu_hm_i".
     402             :        ! Subtract "mu_hm_i" from the number of zt statistical variables.
     403           0 :        ntot = ntot - 1
     404             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     405             :        ! to the number of zt statistical variables.
     406           0 :        ntot = ntot + 2 * hydromet_dim
     407             :     endif
     408           0 :     if ( any( vars_zt == "mu_Ncn_i" ) ) then
     409             :        ! Correct for number of variables found under "mu_Ncn_i".
     410             :        ! Subtract "mu_Ncn_i" from the number of zt statistical variables.
     411           0 :        ntot = ntot - 1
     412             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     413             :        ! statistical variables.
     414           0 :        ntot = ntot + 2
     415             :     endif
     416           0 :     if ( any( vars_zt == "mu_hm_i_n" ) ) then
     417             :        ! Correct for number of variables found under "mu_hm_i_n".
     418             :        ! Subtract "mu_hm_i_n" from the number of zt statistical variables.
     419           0 :        ntot = ntot - 1
     420             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     421             :        ! to the number of zt statistical variables.
     422           0 :        ntot = ntot + 2 * hydromet_dim
     423             :     endif
     424           0 :     if ( any( vars_zt == "mu_Ncn_i_n" ) ) then
     425             :        ! Correct for number of variables found under "mu_Ncn_i_n".
     426             :        ! Subtract "mu_Ncn_i_n" from the number of zt statistical variables.
     427           0 :        ntot = ntot - 1
     428             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     429             :        ! statistical variables.
     430           0 :        ntot = ntot + 2
     431             :     endif
     432           0 :     if ( any( vars_zt == "sigma_hm_i" ) ) then
     433             :        ! Correct for number of variables found under "sigma_hm_i".
     434             :        ! Subtract "sigma_hm_i" from the number of zt statistical variables.
     435           0 :        ntot = ntot - 1
     436             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     437             :        ! to the number of zt statistical variables.
     438           0 :        ntot = ntot + 2 * hydromet_dim
     439             :     endif
     440           0 :     if ( any( vars_zt == "sigma_Ncn_i" ) ) then
     441             :        ! Correct for number of variables found under "sigma_Ncn_i".
     442             :        ! Subtract "sigma_Ncn_i" from the number of zt statistical variables.
     443           0 :        ntot = ntot - 1
     444             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     445             :        ! statistical variables.
     446           0 :        ntot = ntot + 2
     447             :     endif
     448           0 :     if ( any( vars_zt == "sigma_hm_i_n" ) ) then
     449             :        ! Correct for number of variables found under "sigma_hm_i_n".
     450             :        ! Subtract "sigma_hm_i_n" from the number of zt statistical variables.
     451           0 :        ntot = ntot - 1
     452             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     453             :        ! to the number of zt statistical variables.
     454           0 :        ntot = ntot + 2 * hydromet_dim
     455             :     endif
     456           0 :     if ( any( vars_zt == "sigma_Ncn_i_n" ) ) then
     457             :        ! Correct for number of variables found under "sigma_Ncn_i_n".
     458             :        ! Subtract "sigma_Ncn_i_n" from the number of zt statistical variables.
     459           0 :        ntot = ntot - 1
     460             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     461             :        ! statistical variables.
     462           0 :        ntot = ntot + 2
     463             :     endif
     464             : 
     465           0 :     if ( any( vars_zt == "corr_w_hm_i" ) ) then
     466             :        ! Correct for number of variables found under "corr_w_hm_i".
     467             :        ! Subtract "corr_w_hm_i" from the number of zt statistical variables.
     468           0 :        ntot = ntot - 1
     469             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     470             :        ! to the number of zt statistical variables.
     471           0 :        ntot = ntot + 2 * hydromet_dim
     472             :     endif
     473           0 :     if ( any( vars_zt == "corr_w_Ncn_i" ) ) then
     474             :        ! Correct for number of variables found under "corr_w_Ncn_i".
     475             :        ! Subtract "corr_w_Ncn_i" from the number of zt statistical variables.
     476           0 :        ntot = ntot - 1
     477             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     478             :        ! statistical variables.
     479           0 :        ntot = ntot + 2
     480             :     endif
     481           0 :     if ( any( vars_zt == "corr_chi_hm_i" ) ) then
     482             :        ! Correct for number of variables found under "corr_chi_hm_i".
     483             :        ! Subtract "corr_chi_hm_i" from the number of zt statistical variables.
     484           0 :        ntot = ntot - 1
     485             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     486             :        ! to the number of zt statistical variables.
     487           0 :        ntot = ntot + 2 * hydromet_dim
     488             :     endif
     489           0 :     if ( any( vars_zt == "corr_chi_Ncn_i" ) ) then
     490             :        ! Correct for number of variables found under "corr_chi_Ncn_i".
     491             :        ! Subtract "corr_chi_Ncn_i" from the number of zt statistical variables.
     492           0 :        ntot = ntot - 1
     493             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     494             :        ! statistical variables.
     495           0 :        ntot = ntot + 2
     496             :     endif
     497           0 :     if ( any( vars_zt == "corr_eta_hm_i" ) ) then
     498             :        ! Correct for number of variables found under "corr_eta_hm_i".
     499             :        ! Subtract "corr_eta_hm_i" from the number of zt statistical variables.
     500           0 :        ntot = ntot - 1
     501             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     502             :        ! to the number of zt statistical variables.
     503           0 :        ntot = ntot + 2 * hydromet_dim
     504             :     endif
     505           0 :     if ( any( vars_zt == "corr_eta_Ncn_i" ) ) then
     506             :        ! Correct for number of variables found under "corr_eta_Ncn_i".
     507             :        ! Subtract "corr_eta_Ncn_i" from the number of zt statistical variables.
     508           0 :        ntot = ntot - 1
     509             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     510             :        ! statistical variables.
     511           0 :        ntot = ntot + 2
     512             :     endif
     513           0 :     if ( any( vars_zt == "corr_Ncn_hm_i" ) ) then
     514             :        ! Correct for number of variables found under "corr_Ncn_hm_i".
     515             :        ! Subtract "corr_Ncn_hm_i" from the number of zt statistical variables.
     516           0 :        ntot = ntot - 1
     517             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     518             :        ! to the number of zt statistical variables.
     519           0 :        ntot = ntot + 2 * hydromet_dim
     520             :     endif
     521           0 :     if ( any( vars_zt == "corr_hmx_hmy_i" ) ) then
     522             :        ! Correct for number of variables found under "corr_hmx_hmy_i".
     523             :        ! Subtract "corr_hmx_hmy_i" from the number of zt statistical variables.
     524           0 :        ntot = ntot - 1
     525             :        ! Add 2 (1st PDF component and 2nd PDF component) multipled by the
     526             :        ! number of correlations of two hydrometeors, which is found by:
     527             :        ! (1/2) * hydromet_dim * ( hydromet_dim - 1 );
     528             :        ! to the number of zt statistical variables.
     529           0 :        ntot = ntot + hydromet_dim * ( hydromet_dim - 1 )
     530             :     endif
     531             : 
     532           0 :     if ( any( vars_zt == "corr_w_hm_i_n" ) ) then
     533             :        ! Correct for number of variables found under "corr_w_hm_i_n".
     534             :        ! Subtract "corr_w_hm_i_n" from the number of zt statistical variables.
     535           0 :        ntot = ntot - 1
     536             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     537             :        ! to the number of zt statistical variables.
     538           0 :        ntot = ntot + 2 * hydromet_dim
     539             :     endif
     540           0 :     if ( any( vars_zt == "corr_w_Ncn_i_n" ) ) then
     541             :        ! Correct for number of variables found under "corr_w_Ncn_i_n".
     542             :        ! Subtract "corr_w_Ncn_i_n" from the number of zt statistical variables.
     543           0 :        ntot = ntot - 1
     544             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     545             :        ! statistical variables.
     546           0 :        ntot = ntot + 2
     547             :     endif
     548           0 :     if ( any( vars_zt == "corr_chi_hm_i_n" ) ) then
     549             :        ! Correct for number of variables found under "corr_chi_hm_i_n".
     550             :        ! Subtract "corr_chi_hm_i_n" from the number of zt statistical variables.
     551           0 :        ntot = ntot - 1
     552             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     553             :        ! to the number of zt statistical variables.
     554           0 :        ntot = ntot + 2 * hydromet_dim
     555             :     endif
     556           0 :     if ( any( vars_zt == "corr_chi_Ncn_i_n" ) ) then
     557             :        ! Correct for number of variables found under "corr_chi_Ncn_i_n".
     558             :        ! Subtract "corr_chi_Ncn_i_n" from the number of zt statistical variables.
     559           0 :        ntot = ntot - 1
     560             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     561             :        ! statistical variables.
     562           0 :        ntot = ntot + 2
     563             :     endif
     564           0 :     if ( any( vars_zt == "corr_eta_hm_i_n" ) ) then
     565             :        ! Correct for number of variables found under "corr_eta_hm_i_n".
     566             :        ! Subtract "corr_eta_hm_i_n" from the number of zt statistical variables.
     567           0 :        ntot = ntot - 1
     568             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     569             :        ! to the number of zt statistical variables.
     570           0 :        ntot = ntot + 2 * hydromet_dim
     571             :     endif
     572           0 :     if ( any( vars_zt == "corr_eta_Ncn_i_n" ) ) then
     573             :        ! Correct for number of variables found under "corr_eta_Ncn_i_n".
     574             :        ! Subtract "corr_eta_Ncn_i_n" from the number of zt statistical variables.
     575           0 :        ntot = ntot - 1
     576             :        ! Add 2 (1st PDF component and 2nd PDF component) to the number of zt
     577             :        ! statistical variables.
     578           0 :        ntot = ntot + 2
     579             :     endif
     580           0 :     if ( any( vars_zt == "corr_Ncn_hm_i_n" ) ) then
     581             :        ! Correct for number of variables found under "corr_Ncn_hm_i_n".
     582             :        ! Subtract "corr_Ncn_hm_i_n" from the number of zt statistical variables.
     583           0 :        ntot = ntot - 1
     584             :        ! Add 2 (1st PDF component and 2nd PDF component) for each hydrometeor
     585             :        ! to the number of zt statistical variables.
     586           0 :        ntot = ntot + 2 * hydromet_dim
     587             :     endif
     588           0 :     if ( any( vars_zt == "corr_hmx_hmy_i_n" ) ) then
     589             :        ! Correct for number of variables found under "corr_hmx_hmy_i_n".
     590             :        ! Subtract "corr_hmx_hmy_i_n" from the number of zt statistical variables.
     591           0 :        ntot = ntot - 1
     592             :        ! Add 2 (1st PDF component and 2nd PDF component) multipled by the
     593             :        ! number of normal space correlations of two hydrometeors, which is
     594             :        ! found by:  (1/2) * hydromet_dim * ( hydromet_dim - 1 );
     595             :        ! to the number of zt statistical variables.
     596           0 :        ntot = ntot + hydromet_dim * ( hydromet_dim - 1 )
     597             :     endif
     598             : 
     599           0 :     if ( any( vars_zt == "hmp2_zt" ) ) then
     600             :        ! Correct for number of variables found under "hmp2_zt".
     601             :        ! Subtract "hmp2_zt" from the number of zt statistical variables.
     602           0 :        ntot = ntot - 1
     603             :        ! Add 1 for each hydrometeor to the number of zt statistical variables.
     604           0 :        ntot = ntot + hydromet_dim
     605             :     endif
     606             : 
     607           0 :     if ( any( vars_zt == "wp2hmp" ) ) then
     608             :        ! Correct for number of variables found under "wp2hmp".
     609             :        ! Subtract "wp2hmp" from the number of zt statistical variables.
     610           0 :        ntot = ntot - 1
     611             :        ! Add 1 for each hydrometeor to the number of zt statistical variables.
     612           0 :        ntot = ntot + hydromet_dim
     613             :     endif
     614             : 
     615           0 :     if ( any( vars_zt == "sclrm" ) ) then
     616             :        ! Correct for number of variables found under "sclrm".
     617             :        ! Subtract "sclrm" from the number of zt statistical variables.
     618           0 :        ntot = ntot - 1
     619             :        ! Add 1 for each scalar to the number of zt statistical variables.
     620           0 :        ntot = ntot + sclr_dim
     621             :     endif   
     622             : 
     623           0 :     if ( any( vars_zt == "sclrm_f" ) ) then
     624             :        ! Correct for number of variables found under "sclrm_f".
     625             :        ! Subtract "sclrm_f" from the number of zt statistical variables.
     626           0 :        ntot = ntot - 1
     627             :        ! Add 1 for each scalar to the number of zt statistical variables.
     628           0 :        ntot = ntot + sclr_dim
     629             :     endif
     630             : 
     631           0 :     if ( any( vars_zt == "edsclrm" ) ) then
     632             :        ! Correct for number of variables found under "edsclrm".
     633             :        ! Subtract "edsclrm" from the number of zt statistical variables.
     634           0 :        ntot = ntot - 1
     635             :        ! Add 1 for each scalar to the number of zt statistical variables.
     636           0 :        ntot = ntot + edsclr_dim
     637             :     endif
     638             : 
     639           0 :     if ( any( vars_zt == "edsclrm_f" ) ) then
     640             :        ! Correct for number of variables found under "edsclrm_f".
     641             :        ! Subtract "edsclrm_f" from the number of zt statistical variables.
     642           0 :        ntot = ntot - 1
     643             :        ! Add 1 for each scalar to the number of zt statistical variables.
     644           0 :        ntot = ntot + edsclr_dim
     645             :     endif
     646             : 
     647           0 :     if ( ntot >= nvarmax_zt ) then
     648           0 :       write(fstderr,*) "There are more statistical variables listed in ",  &
     649           0 :                        "vars_zt than allowed for by nvarmax_zt."
     650           0 :       write(fstderr,*) "Check the number of variables listed for vars_zt ",  &
     651           0 :                        "in the stats namelist, or change nvarmax_zt."
     652           0 :       write(fstderr,*) "nvarmax_zt = ", nvarmax_zt
     653           0 :       write(fstderr,*) "number of variables in vars_zt = ", ntot
     654           0 :       write(fstderr,*) "stats_init:  number of zt statistical variables exceeds limit"
     655           0 :       err_code = clubb_fatal_error
     656           0 :       return
     657             :     end if
     658             : 
     659           0 :     stats_zt%num_output_fields = ntot
     660           0 :     stats_zt%kk = nzmax
     661           0 :     stats_zt%ii = nlon
     662           0 :     stats_zt%jj = nlat
     663             : 
     664           0 :     allocate( stats_zt%z( stats_zt%kk ) )
     665           0 :     stats_zt%z = gzt
     666             : 
     667           0 :     allocate( stats_zt%accum_field_values( stats_zt%ii, stats_zt%jj, &
     668           0 :       stats_zt%kk, stats_zt%num_output_fields ) )
     669           0 :     allocate( stats_zt%accum_num_samples( stats_zt%ii, stats_zt%jj, &
     670           0 :       stats_zt%kk, stats_zt%num_output_fields ) )
     671           0 :     allocate( stats_zt%l_in_update( stats_zt%ii, stats_zt%jj, stats_zt%kk, &
     672           0 :       stats_zt%num_output_fields ) )
     673             :     call stats_zero( stats_zt%ii, stats_zt%jj, stats_zt%kk, stats_zt%num_output_fields, & ! In
     674           0 :       stats_zt%accum_field_values, stats_zt%accum_num_samples, stats_zt%l_in_update ) ! Out
     675             : 
     676           0 :     allocate( stats_zt%file%grid_avg_var( stats_zt%num_output_fields ) )
     677           0 :     allocate( stats_zt%file%z( stats_zt%kk ) )
     678             : 
     679           0 :     fname = trim( stats_metadata%fname_zt )
     680             : 
     681           0 :     if ( stats_metadata%l_grads ) then
     682             : 
     683             :       ! Open GrADS file
     684             :       call open_grads( iunit, fdir, fname,  &  ! intent(in)
     685             :                        1, stats_zt%kk, nlat, nlon, stats_zt%z, & ! intent(in) 
     686             :                        day, month, year, lat_vals, lon_vals, &  ! intent(in)
     687             :                        time_current+real(stats_metadata%stats_tout,kind=time_precision), & !intent(in) 
     688             :                        stats_metadata, stats_zt%num_output_fields, & ! intent(in)
     689           0 :                        stats_zt%file ) ! intent(inout)
     690             : 
     691             :     else ! Open NetCDF file
     692             : #ifdef NETCDF
     693             :       call open_netcdf_for_writing( nlat, nlon, fdir, fname, 1, stats_zt%kk, stats_zt%z, &  ! In
     694             :                         day, month, year, lat_vals, lon_vals, &  ! In
     695             :                         time_current, stats_metadata%stats_tout, stats_zt%num_output_fields, &  ! In
     696             :                         stats_zt%file ) ! InOut
     697             : 
     698             :       if ( err_code == clubb_fatal_error ) return
     699             : #else
     700           0 :       error stop "This CLUBB program was not compiled with netCDF support."
     701             : #endif
     702             : 
     703             :     end if
     704             : 
     705             :     ! Default initialization for array indices for zt
     706             : 
     707             :     call stats_init_zt( vars_zt,                    & ! intent(in)
     708             :                         l_error,                    & ! intent(inout)
     709           0 :                         stats_metadata, stats_zt )    ! intent(inout)
     710             : 
     711             : 
     712             :     ! Setup output file for stats_lh_zt (Latin Hypercube stats)
     713             : 
     714           0 :     if ( stats_metadata%l_silhs_out ) then
     715             : 
     716             :       ivar = 1
     717           0 :       do while ( ichar(vars_lh_zt(ivar)(1:1)) /= 0  & 
     718             :                  .and. len_trim(vars_lh_zt(ivar)) /= 0 & 
     719           0 :                  .and. ivar <= nvarmax_lh_zt )
     720           0 :         ivar = ivar + 1
     721             :       end do
     722           0 :       ntot = ivar - 1
     723           0 :       if ( any( vars_lh_zt == "silhs_variance_category" ) ) then
     724             :         ! Correct for number of variables found under "silhs_variance_category".
     725             :         ! Subtract "silhs_variance_category" from the number of lh_zt statistical
     726             :         ! variables.
     727           0 :         ntot = ntot - 1
     728             :         ! Add 1 for each SILHS category to the number of lh_zt statistical variables
     729           0 :         ntot = ntot + silhs_num_importance_categories
     730             :       end if
     731             : 
     732           0 :       if ( any( vars_lh_zt == "lh_samp_frac_category" ) ) then
     733             :         ! Correct for number of variables found under "lh_samp_frac_category".
     734             :         ! Subtract "lh_samp_frac_category" from the number of lh_zt statistical
     735             :         ! variables.
     736           0 :         ntot = ntot - 1
     737             :         ! Add 1 for each SILHS category to the number of lh_zt statistical variables
     738           0 :         ntot = ntot + silhs_num_importance_categories
     739             :       end if
     740             : 
     741           0 :       if ( ntot == nvarmax_lh_zt ) then
     742           0 :         write(fstderr,*) "There are more statistical variables listed in ",  &
     743           0 :                          "vars_lh_zt than allowed for by nvarmax_lh_zt."
     744           0 :         write(fstderr,*) "Check the number of variables listed for vars_lh_zt ",  &
     745           0 :                          "in the stats namelist, or change nvarmax_lh_zt."
     746           0 :         write(fstderr,*) "nvarmax_lh_zt = ", nvarmax_lh_zt
     747           0 :         write(fstderr,*) "number of variables in vars_lh_zt = ", ntot
     748           0 :         write(fstderr,*) "stats_init:  number of lh_zt statistical variables exceeds limit"
     749           0 :         err_code = clubb_fatal_error
     750           0 :         return
     751             :       end if
     752             : 
     753           0 :       stats_lh_zt%num_output_fields = ntot
     754           0 :       stats_lh_zt%kk = nzmax
     755           0 :       stats_lh_zt%ii = nlon
     756           0 :       stats_lh_zt%jj = nlat
     757             : 
     758           0 :       allocate( stats_lh_zt%z( stats_lh_zt%kk ) )
     759           0 :       stats_lh_zt%z = gzt
     760             : 
     761           0 :       allocate( stats_lh_zt%accum_field_values( stats_lh_zt%ii, stats_lh_zt%jj, &
     762           0 :         stats_lh_zt%kk, stats_lh_zt%num_output_fields ) )
     763           0 :       allocate( stats_lh_zt%accum_num_samples( stats_lh_zt%ii, stats_lh_zt%jj, &
     764           0 :         stats_lh_zt%kk, stats_lh_zt%num_output_fields ) )
     765           0 :       allocate( stats_lh_zt%l_in_update( stats_lh_zt%ii, stats_lh_zt%jj, stats_lh_zt%kk, &
     766           0 :         stats_lh_zt%num_output_fields ) )
     767             :       call stats_zero( stats_lh_zt%ii, stats_lh_zt%jj, stats_lh_zt%kk, & ! intent(in)
     768             :         stats_lh_zt%num_output_fields, & ! intent(in)
     769             :         stats_lh_zt%accum_field_values, stats_lh_zt%accum_num_samples, & ! intent(out)
     770           0 :         stats_lh_zt%l_in_update ) ! intent(out)
     771             : 
     772           0 :       allocate( stats_lh_zt%file%grid_avg_var( stats_lh_zt%num_output_fields ) )
     773           0 :       allocate( stats_lh_zt%file%z( stats_lh_zt%kk ) )
     774             : 
     775             : 
     776           0 :       fname = trim( stats_metadata%fname_lh_zt )
     777             : 
     778           0 :       if ( stats_metadata%l_grads ) then
     779             : 
     780             :         ! Open GrADS file
     781             :         call open_grads( iunit, fdir, fname,  & ! intent(in)
     782             :                          1, stats_lh_zt%kk, nlat, nlon, stats_lh_zt%z, & ! intent(in)
     783             :                          day, month, year, lat_vals, lon_vals, &  ! intent(in)
     784             :                          time_current+real(stats_metadata%stats_tout,kind=time_precision), & !intent(in) 
     785             :                          stats_metadata, stats_lh_zt%num_output_fields, & ! intent(in)
     786           0 :                          stats_lh_zt%file ) ! intent(inout)
     787             : 
     788             :       else ! Open NetCDF file
     789             : #ifdef NETCDF
     790             :         call open_netcdf_for_writing( nlat, nlon, fdir, fname, 1, stats_lh_zt%kk, &  ! In
     791             :                           stats_lh_zt%z, day, month, year, lat_vals, lon_vals, &  ! In
     792             :                           time_current, stats_metadata%stats_tout, stats_lh_zt%num_output_fields, &  ! In
     793             :                           stats_lh_zt%file ) ! InOut
     794             : 
     795             :         if ( err_code == clubb_fatal_error ) return
     796             : #else
     797           0 :         error stop "This CLUBB program was not compiled with netCDF support."
     798             : #endif
     799             : 
     800             :       end if
     801             : 
     802             :       call stats_init_lh_zt( vars_lh_zt,                    & ! intent(in)
     803             :                              l_error,                       & ! intent(inout)
     804           0 :                              stats_metadata, stats_lh_zt )    ! intent(inout)
     805             : 
     806           0 :       ivar = 1
     807           0 :       do while ( ichar(vars_lh_sfc(ivar)(1:1)) /= 0  & 
     808             :                  .and. len_trim(vars_lh_sfc(ivar)) /= 0 & 
     809           0 :                  .and. ivar <= nvarmax_lh_sfc )
     810           0 :         ivar = ivar + 1
     811             :       end do
     812           0 :       ntot = ivar - 1
     813           0 :       if ( ntot == nvarmax_lh_sfc ) then
     814           0 :         write(fstderr,*) "There are more statistical variables listed in ",  &
     815           0 :                          "vars_lh_sfc than allowed for by nvarmax_lh_sfc."
     816           0 :         write(fstderr,*) "Check the number of variables listed for vars_lh_sfc ",  &
     817           0 :                          "in the stats namelist, or change nvarmax_lh_sfc."
     818           0 :         write(fstderr,*) "nvarmax_lh_sfc = ", nvarmax_lh_sfc
     819           0 :         write(fstderr,*) "number of variables in vars_lh_sfc = ", ntot
     820           0 :         write(fstderr,*) "stats_init:  number of lh_sfc statistical variables exceeds limit"
     821           0 :         err_code = clubb_fatal_error
     822           0 :         return
     823             :       end if
     824             : 
     825           0 :       stats_lh_sfc%num_output_fields = ntot
     826           0 :       stats_lh_sfc%kk = 1
     827           0 :       stats_lh_sfc%ii = nlon
     828           0 :       stats_lh_sfc%jj = nlat
     829             : 
     830           0 :       allocate( stats_lh_sfc%z( stats_lh_sfc%kk ) )
     831           0 :       stats_lh_sfc%z = gzm(1)
     832             : 
     833           0 :       allocate( stats_lh_sfc%accum_field_values( stats_lh_sfc%ii, stats_lh_sfc%jj, &
     834           0 :         stats_lh_sfc%kk, stats_lh_sfc%num_output_fields ) )
     835           0 :       allocate( stats_lh_sfc%accum_num_samples( stats_lh_sfc%ii, stats_lh_sfc%jj, &
     836           0 :         stats_lh_sfc%kk, stats_lh_sfc%num_output_fields ) )
     837           0 :       allocate( stats_lh_sfc%l_in_update( stats_lh_sfc%ii, stats_lh_sfc%jj, &
     838           0 :         stats_lh_sfc%kk, stats_lh_sfc%num_output_fields ) )
     839             : 
     840             :       call stats_zero( stats_lh_sfc%ii, stats_lh_sfc%jj, stats_lh_sfc%kk, & ! intent(in)
     841             :           stats_lh_sfc%num_output_fields, & ! intent(in)
     842             :           stats_lh_sfc%accum_field_values, & ! intent(out)
     843           0 :           stats_lh_sfc%accum_num_samples, stats_lh_sfc%l_in_update ) ! intent(out)
     844             : 
     845           0 :       allocate( stats_lh_sfc%file%grid_avg_var( stats_lh_sfc%num_output_fields ) )
     846           0 :       allocate( stats_lh_sfc%file%z( stats_lh_sfc%kk ) )
     847             : 
     848           0 :       fname = trim( stats_metadata%fname_lh_sfc )
     849             : 
     850           0 :       if ( stats_metadata%l_grads ) then
     851             : 
     852             :         ! Open GrADS file
     853             :         call open_grads( iunit, fdir, fname,  & ! intent(in)
     854             :                          1, stats_lh_sfc%kk, nlat, nlon, stats_lh_sfc%z, & ! intent(in) 
     855             :                          day, month, year, lat_vals, lon_vals, &  ! intent(in)
     856             :                          time_current+real(stats_metadata%stats_tout,kind=time_precision), & !intent(in) 
     857             :                          stats_metadata, stats_lh_sfc%num_output_fields, & ! intent(in)
     858           0 :                          stats_lh_sfc%file ) ! intent(inout)
     859             : 
     860             :       else ! Open NetCDF file
     861             : #ifdef NETCDF
     862             :         call open_netcdf_for_writing( nlat, nlon, fdir, fname, 1, stats_lh_sfc%kk, &  ! In
     863             :                           stats_lh_sfc%z, day, month, year, lat_vals, lon_vals, &  ! In
     864             :                           time_current, stats_metadata%stats_tout, stats_lh_sfc%num_output_fields, &  ! In
     865             :                           stats_lh_sfc%file ) ! InOut
     866             : 
     867             :         if ( err_code == clubb_fatal_error ) return
     868             : #else
     869           0 :         error stop "This CLUBB program was not compiled with netCDF support."
     870             : #endif
     871             : 
     872             :       end if
     873             : 
     874             :       call stats_init_lh_sfc( vars_lh_sfc,                    & ! intent(in)
     875             :                               l_error,                        & ! intent(inout)
     876           0 :                               stats_metadata, stats_lh_sfc )    ! intent(inout)
     877             : 
     878             :     end if ! l_silhs_out
     879             : 
     880             :     ! Initialize stats_zm (momentum points)
     881             : 
     882           0 :     ivar = 1
     883           0 :     do while ( ichar(vars_zm(ivar)(1:1)) /= 0  & 
     884             :                .and. len_trim(vars_zm(ivar)) /= 0 & 
     885           0 :                .and. ivar <= nvarmax_zm )
     886           0 :       ivar = ivar + 1
     887             :     end do
     888           0 :     ntot = ivar - 1
     889             : 
     890           0 :     if ( any( vars_zm == "hydrometp2" ) ) then
     891             :        ! Correct for number of variables found under "hydrometp2".
     892             :        ! Subtract "hydrometp2" from the number of zm statistical variables.
     893           0 :        ntot = ntot - 1
     894             :        ! Add 1 for each hydrometeor to the number of zm statistical variables.
     895           0 :        ntot = ntot + hydromet_dim
     896             :     endif
     897             : 
     898           0 :     if ( any( vars_zm == "wphydrometp" ) ) then
     899             :        ! Correct for number of variables found under "wphydrometp".
     900             :        ! Subtract "wphydrometp" from the number of zm statistical variables.
     901           0 :        ntot = ntot - 1
     902             :        ! Add 1 for each hydrometeor to the number of zm statistical variables.
     903           0 :        ntot = ntot + hydromet_dim
     904             :     endif
     905             : 
     906           0 :     if ( any( vars_zm == "rtphmp" ) ) then
     907             :        ! Correct for number of variables found under "rtphmp".
     908             :        ! Subtract "rtphmp" from the number of zm statistical variables.
     909           0 :        ntot = ntot - 1
     910             :        ! Add 1 for each hydrometeor to the number of zm statistical variables.
     911           0 :        ntot = ntot + hydromet_dim
     912             :     endif
     913             : 
     914           0 :     if ( any( vars_zm == "thlphmp" ) ) then
     915             :        ! Correct for number of variables found under "thlphmp".
     916             :        ! Subtract "thlphmp" from the number of zm statistical variables.
     917           0 :        ntot = ntot - 1
     918             :        ! Add 1 for each hydrometeor to the number of zm statistical variables.
     919           0 :        ntot = ntot + hydromet_dim
     920             :     endif
     921             : 
     922           0 :     if ( any( vars_zm == "hmxphmyp" ) ) then
     923             :        ! Correct for number of variables found under "hmxphmyp".
     924             :        ! Subtract "hmxphmyp" from the number of zm statistical variables.
     925           0 :        ntot = ntot - 1
     926             :        ! Add the number of overall covariances of two hydrometeors, which is
     927             :        ! found by:  (1/2) * hydromet_dim * ( hydromet_dim - 1 );
     928             :        ! to the number of zm statistical variables.
     929           0 :        ntot = ntot + hydromet_dim * ( hydromet_dim - 1 ) / 2
     930             :     endif
     931             : 
     932           0 :     if ( any( vars_zm == "K_hm" ) ) then
     933             :        ! Correct for number of variables found under "K_hm".
     934             :        ! Subtract "K_hm" from the number of zm statistical variables.
     935           0 :        ntot = ntot - 1
     936             :        ! Add 1 for each hydrometeor to the number of zm statistical variables.
     937           0 :        ntot = ntot + hydromet_dim
     938             :     endif
     939             : 
     940           0 :     if ( any( vars_zm == "sclrprtp" ) ) then
     941             :        ! Correct for number of variables found under "sclrprtp".
     942             :        ! Subtract "sclrprtp" from the number of zm statistical variables.
     943           0 :        ntot = ntot - 1
     944             :        ! Add 1 for each scalar to the number of zm statistical variables.
     945           0 :        ntot = ntot + sclr_dim
     946             :     endif
     947             : 
     948           0 :     if ( any( vars_zm == "sclrp2" ) ) then
     949             :        ! Correct for number of variables found under "sclrp2".
     950             :        ! Subtract "sclrp2" from the number of zm statistical variables.
     951           0 :        ntot = ntot - 1
     952             :        ! Add 1 for each scalar to the number of zm statistical variables.
     953           0 :        ntot = ntot + sclr_dim
     954             :     endif
     955             : 
     956             : 
     957           0 :     if ( any( vars_zm == "sclrpthvp" ) ) then
     958             :        ! Correct for number of variables found under "sclrpthvp".
     959             :        ! Subtract "sclrpthvp" from the number of zm statistical variables.
     960           0 :        ntot = ntot - 1
     961             :        ! Add 1 for each scalar to the number of zm statistical variables.
     962           0 :        ntot = ntot + sclr_dim
     963             :     endif
     964             : 
     965             : 
     966           0 :     if ( any( vars_zm == "sclrpthlp" ) ) then
     967             :        ! Correct for number of variables found under "sclrpthlp".
     968             :        ! Subtract "sclrpthlp" from the number of zm statistical variables.
     969           0 :        ntot = ntot - 1
     970             :        ! Add 1 for each scalar to the number of zm statistical variables.
     971           0 :        ntot = ntot + sclr_dim
     972             :     endif
     973             : 
     974             : 
     975           0 :     if ( any( vars_zm == "sclrprcp" ) ) then
     976             :        ! Correct for number of variables found under "sclrprcp".
     977             :        ! Subtract "sclrprcp" from the number of zm statistical variables.
     978           0 :        ntot = ntot - 1
     979             :        ! Add 1 for each scalar to the number of zm statistical variables.
     980           0 :        ntot = ntot + sclr_dim
     981             :     endif
     982             : 
     983             : 
     984           0 :     if ( any( vars_zm == "wpsclrp" ) ) then
     985             :        ! Correct for number of variables found under "wpsclrp".
     986             :        ! Subtract "wpsclrp" from the number of zm statistical variables.
     987           0 :        ntot = ntot - 1
     988             :        ! Add 1 for each scalar to the number of zm statistical variables.
     989           0 :        ntot = ntot + sclr_dim
     990             :     endif
     991             : 
     992             : 
     993           0 :     if ( any( vars_zm == "wpsclrp2" ) ) then
     994             :        ! Correct for number of variables found under "wpsclrp2".
     995             :        ! Subtract "wpsclrp2" from the number of zm statistical variables.
     996           0 :        ntot = ntot - 1
     997             :        ! Add 1 for each scalar to the number of zm statistical variables.
     998           0 :        ntot = ntot + sclr_dim
     999             :     endif
    1000             : 
    1001             : 
    1002           0 :     if ( any( vars_zm == "wp2sclrp" ) ) then
    1003             :        ! Correct for number of variables found under "wp2sclrp".
    1004             :        ! Subtract "wp2sclrp" from the number of zm statistical variables.
    1005           0 :        ntot = ntot - 1
    1006             :        ! Add 1 for each scalar to the number of zm statistical variables.
    1007           0 :        ntot = ntot + sclr_dim
    1008             :     endif
    1009             : 
    1010             : 
    1011           0 :     if ( any( vars_zm == "wpsclrprtp" ) ) then
    1012             :        ! Correct for number of variables found under "wpsclrprtp".
    1013             :        ! Subtract "wpsclrprtp" from the number of zm statistical variables.
    1014           0 :        ntot = ntot - 1
    1015             :        ! Add 1 for each scalar to the number of zm statistical variables.
    1016           0 :        ntot = ntot + sclr_dim
    1017             :     endif
    1018             : 
    1019             : 
    1020           0 :     if ( any( vars_zm == "wpsclrpthlp" ) ) then
    1021             :        ! Correct for number of variables found under "wpsclrpthlp".
    1022             :        ! Subtract "wpsclrpthlp" from the number of zm statistical variables.
    1023           0 :        ntot = ntot - 1
    1024             :        ! Add 1 for each scalar to the number of zm statistical variables.
    1025           0 :        ntot = ntot + sclr_dim
    1026             :     endif
    1027             : 
    1028             : 
    1029           0 :     if ( any( vars_zm == "wpedsclrp" ) ) then
    1030             :        ! Correct for number of variables found under "wpedsclrp".
    1031             :        ! Subtract "wpedsclrp" from the number of zm statistical variables.
    1032           0 :        ntot = ntot - 1
    1033             :        ! Add 1 for each scalar to the number of zm statistical variables.
    1034           0 :        ntot = ntot + edsclr_dim
    1035             :     endif
    1036             : 
    1037             : 
    1038             : 
    1039           0 :     if ( ntot == nvarmax_zm ) then
    1040           0 :       write(fstderr,*) "There are more statistical variables listed in ",  &
    1041           0 :                        "vars_zm than allowed for by nvarmax_zm."
    1042           0 :       write(fstderr,*) "Check the number of variables listed for vars_zm ",  &
    1043           0 :                        "in the stats namelist, or change nvarmax_zm."
    1044           0 :       write(fstderr,*) "nvarmax_zm = ", nvarmax_zm
    1045           0 :       write(fstderr,*) "number of variables in vars_zm = ", ntot
    1046           0 :       write(fstderr,*) "stats_init:  number of zm statistical variables exceeds limit"
    1047           0 :       err_code = clubb_fatal_error
    1048           0 :       return
    1049             :     end if
    1050             : 
    1051           0 :     stats_zm%num_output_fields = ntot
    1052           0 :     stats_zm%kk = nzmax
    1053           0 :     stats_zm%ii = nlon
    1054           0 :     stats_zm%jj = nlat
    1055             : 
    1056           0 :     allocate( stats_zm%z( stats_zm%kk ) )
    1057           0 :     stats_zm%z = gzm
    1058             : 
    1059           0 :     allocate( stats_zm%accum_field_values( stats_zm%ii, stats_zm%jj, &
    1060           0 :       stats_zm%kk, stats_zm%num_output_fields ) )
    1061           0 :     allocate( stats_zm%accum_num_samples( stats_zm%ii, stats_zm%jj, &
    1062           0 :       stats_zm%kk, stats_zm%num_output_fields ) )
    1063           0 :     allocate( stats_zm%l_in_update( stats_zm%ii, stats_zm%jj, stats_zm%kk, &
    1064           0 :       stats_zm%num_output_fields ) )
    1065             : 
    1066             :     call stats_zero( stats_zm%ii, stats_zm%jj, stats_zm%kk, stats_zm%num_output_fields, & ! In
    1067           0 :       stats_zm%accum_field_values, stats_zm%accum_num_samples, stats_zm%l_in_update ) ! intent(out)
    1068             : 
    1069           0 :     allocate( stats_zm%file%grid_avg_var( stats_zm%num_output_fields ) )
    1070           0 :     allocate( stats_zm%file%z( stats_zm%kk ) )
    1071             : 
    1072           0 :     fname = trim( stats_metadata%fname_zm )
    1073           0 :     if ( stats_metadata%l_grads ) then
    1074             : 
    1075             :       ! Open GrADS files
    1076             :       call open_grads( iunit, fdir, fname,  & ! intent(in)
    1077             :                        1, stats_zm%kk, nlat, nlon, stats_zm%z, & ! intent(in)
    1078             :                        day, month, year, lat_vals, lon_vals, & ! intent(in)
    1079             :                        time_current+real(stats_metadata%stats_tout,kind=time_precision), & !intent(in) 
    1080             :                        stats_metadata, stats_zm%num_output_fields, & ! intent(in)
    1081           0 :                        stats_zm%file ) ! intent(inout)
    1082             : 
    1083             :     else ! Open NetCDF file
    1084             : #ifdef NETCDF
    1085             :       call open_netcdf_for_writing( nlat, nlon, fdir, fname, 1, stats_zm%kk, stats_zm%z, &  ! In
    1086             :                         day, month, year, lat_vals, lon_vals, &  ! In
    1087             :                         time_current, stats_metadata%stats_tout, stats_zm%num_output_fields, &  ! In
    1088             :                         stats_zm%file ) ! InOut
    1089             : 
    1090             :       if ( err_code == clubb_fatal_error ) return
    1091             : #else
    1092           0 :       error stop "This CLUBB program was not compiled with netCDF support."
    1093             : #endif
    1094             :     end if
    1095             : 
    1096             :     call stats_init_zm( vars_zm,                    & ! intent(in)
    1097             :                         l_error,                    & ! intent(inout)
    1098           0 :                         stats_metadata, stats_zm )    ! intent(inout)
    1099             : 
    1100             :     ! Initialize stats_rad_zt (radiation points)
    1101             : 
    1102           0 :     if ( stats_metadata%l_output_rad_files ) then
    1103             : 
    1104             :       ivar = 1
    1105           0 :       do while ( ichar(vars_rad_zt(ivar)(1:1)) /= 0  & 
    1106             :                  .and. len_trim(vars_rad_zt(ivar)) /= 0 & 
    1107           0 :                  .and. ivar <= nvarmax_rad_zt )
    1108           0 :         ivar = ivar + 1
    1109             :       end do
    1110           0 :       ntot = ivar - 1
    1111           0 :       if ( ntot == nvarmax_rad_zt ) then
    1112           0 :         write(fstderr,*) "There are more statistical variables listed in ",  &
    1113           0 :                          "vars_rad_zt than allowed for by nvarmax_rad_zt."
    1114           0 :         write(fstderr,*) "Check the number of variables listed for vars_rad_zt ",  &
    1115           0 :                          "in the stats namelist, or change nvarmax_rad_zt."
    1116           0 :         write(fstderr,*) "nvarmax_rad_zt = ", nvarmax_rad_zt
    1117           0 :         write(fstderr,*) "number of variables in vars_rad_zt = ", ntot
    1118           0 :         write(fstderr,*) "stats_init:  number of rad_zt statistical variables exceeds limit"
    1119           0 :         err_code = clubb_fatal_error
    1120           0 :         return
    1121             :       end if
    1122             : 
    1123           0 :       stats_rad_zt%num_output_fields = ntot
    1124           0 :       stats_rad_zt%kk = nnrad_zt
    1125           0 :       stats_rad_zt%ii = nlon
    1126           0 :       stats_rad_zt%jj = nlat
    1127           0 :       allocate( stats_rad_zt%z( stats_rad_zt%kk ) )
    1128           0 :       stats_rad_zt%z = grad_zt
    1129             : 
    1130           0 :       allocate( stats_rad_zt%accum_field_values( stats_rad_zt%ii, stats_rad_zt%jj, &
    1131           0 :         stats_rad_zt%kk, stats_rad_zt%num_output_fields ) )
    1132           0 :       allocate( stats_rad_zt%accum_num_samples( stats_rad_zt%ii, stats_rad_zt%jj, &
    1133           0 :         stats_rad_zt%kk, stats_rad_zt%num_output_fields ) )
    1134           0 :       allocate( stats_rad_zt%l_in_update( stats_rad_zt%ii, stats_rad_zt%jj, &
    1135           0 :         stats_rad_zt%kk, stats_rad_zt%num_output_fields ) )
    1136             : 
    1137             :       call stats_zero( stats_rad_zt%ii, stats_rad_zt%jj, stats_rad_zt%kk, & ! intent(in)
    1138             :                        stats_rad_zt%num_output_fields, & ! intent(in)
    1139             :                        stats_rad_zt%accum_field_values, & ! intent(out)
    1140           0 :                        stats_rad_zt%accum_num_samples, stats_rad_zt%l_in_update )! intent(out)
    1141             : 
    1142           0 :       allocate( stats_rad_zt%file%grid_avg_var( stats_rad_zt%num_output_fields ) )
    1143           0 :       allocate( stats_rad_zt%file%z( stats_rad_zt%kk ) )
    1144             : 
    1145           0 :       fname = trim( stats_metadata%fname_rad_zt )
    1146           0 :       if ( stats_metadata%l_grads ) then
    1147             : 
    1148             :         ! Open GrADS files
    1149             :         call open_grads( iunit, fdir, fname,  & ! intent(in)
    1150             :                          1, stats_rad_zt%kk, nlat, nlon, stats_rad_zt%z, & ! intent(in)
    1151             :                          day, month, year, lat_vals, lon_vals, & 
    1152             :                          time_current+real(stats_metadata%stats_tout, kind=time_precision), & !intent(in) 
    1153             :                          stats_metadata, stats_rad_zt%num_output_fields, & ! intent(in)
    1154           0 :                          stats_rad_zt%file ) ! intent(inout)
    1155             : 
    1156             :       else ! Open NetCDF file
    1157             : #ifdef NETCDF
    1158             :         call open_netcdf_for_writing( nlat, nlon, fdir, fname,  & ! intent(in)
    1159             :                           1, stats_rad_zt%kk, stats_rad_zt%z, & ! intent(in)
    1160             :                           day, month, year, lat_vals, lon_vals, & ! intent(in)
    1161             :                           time_current, stats_metadata%stats_tout, & ! intent(in)
    1162             :                           stats_rad_zt%num_output_fields, & ! intent(in)
    1163             :                           stats_rad_zt%file ) ! intent(inout)
    1164             : 
    1165             :         if ( err_code == clubb_fatal_error ) return
    1166             : #else
    1167           0 :         error stop "This CLUBB program was not compiled with netCDF support."
    1168             : #endif
    1169             :       end if
    1170             : 
    1171             :       call stats_init_rad_zt( vars_rad_zt,                    & ! intent(in)
    1172             :                               l_error,                        & ! intent(inout)
    1173           0 :                               stats_metadata, stats_rad_zt )    ! intent(inout)
    1174             : 
    1175             :       ! Initialize stats_rad_zm (radiation points)
    1176             : 
    1177           0 :       ivar = 1
    1178           0 :       do while ( ichar(vars_rad_zm(ivar)(1:1)) /= 0  & 
    1179             :                  .and. len_trim(vars_rad_zm(ivar)) /= 0 & 
    1180           0 :                  .and. ivar <= nvarmax_rad_zm )
    1181           0 :         ivar = ivar + 1
    1182             :       end do
    1183           0 :       ntot = ivar - 1
    1184           0 :       if ( ntot == nvarmax_rad_zm ) then
    1185           0 :         write(fstderr,*) "There are more statistical variables listed in ",  &
    1186           0 :                          "vars_rad_zm than allowed for by nvarmax_rad_zm."
    1187           0 :         write(fstderr,*) "Check the number of variables listed for vars_rad_zm ",  &
    1188           0 :                          "in the stats namelist, or change nvarmax_rad_zm."
    1189           0 :         write(fstderr,*) "nvarmax_rad_zm = ", nvarmax_rad_zm
    1190           0 :         write(fstderr,*) "number of variables in vars_rad_zm = ", ntot
    1191           0 :         write(fstderr,*) "stats_init:  number of rad_zm statistical variables exceeds limit"
    1192           0 :         err_code = clubb_fatal_error
    1193           0 :         return
    1194             :       end if
    1195             : 
    1196           0 :       stats_rad_zm%num_output_fields = ntot
    1197           0 :       stats_rad_zm%kk = nnrad_zm
    1198           0 :       stats_rad_zm%ii = nlon
    1199           0 :       stats_rad_zm%jj = nlat
    1200             : 
    1201           0 :       allocate( stats_rad_zm%z( stats_rad_zm%kk ) )
    1202           0 :       stats_rad_zm%z = grad_zm
    1203             : 
    1204           0 :       allocate( stats_rad_zm%accum_field_values( stats_rad_zm%ii, stats_rad_zm%jj, &
    1205           0 :         stats_rad_zm%kk, stats_rad_zm%num_output_fields ) )
    1206           0 :       allocate( stats_rad_zm%accum_num_samples( stats_rad_zm%ii, stats_rad_zm%jj, &
    1207           0 :         stats_rad_zm%kk, stats_rad_zm%num_output_fields ) )
    1208           0 :       allocate( stats_rad_zm%l_in_update( stats_rad_zm%ii, stats_rad_zm%jj, &
    1209           0 :         stats_rad_zm%kk, stats_rad_zm%num_output_fields ) )
    1210             : 
    1211             :       call stats_zero( stats_rad_zm%ii, stats_rad_zm%jj, stats_rad_zm%kk, & ! intent(in)
    1212             :                        stats_rad_zm%num_output_fields, & ! intent(in)
    1213             :                        stats_rad_zm%accum_field_values, & ! intent(out)
    1214           0 :                        stats_rad_zm%accum_num_samples, stats_rad_zm%l_in_update ) ! intent(out)
    1215             : 
    1216           0 :       allocate( stats_rad_zm%file%grid_avg_var( stats_rad_zm%num_output_fields ) )
    1217           0 :       allocate( stats_rad_zm%file%z( stats_rad_zm%kk ) )
    1218             : 
    1219           0 :       fname = trim( stats_metadata%fname_rad_zm )
    1220           0 :       if ( stats_metadata%l_grads ) then
    1221             : 
    1222             :         ! Open GrADS files
    1223             :         call open_grads( iunit, fdir, fname,  & ! intent(in)
    1224             :                          1, stats_rad_zm%kk, nlat, nlon, stats_rad_zm%z, & ! intent(in)
    1225             :                          day, month, year, lat_vals, lon_vals, & 
    1226             :                          time_current+real(stats_metadata%stats_tout,kind=time_precision), & !intent(in) 
    1227             :                          stats_metadata, stats_rad_zm%num_output_fields, & ! intent(in)
    1228           0 :                          stats_rad_zm%file ) ! intent(inout)
    1229             : 
    1230             :       else ! Open NetCDF file
    1231             : #ifdef NETCDF
    1232             :         call open_netcdf_for_writing( nlat, nlon, fdir, fname,  & ! intent(in)
    1233             :                           1, stats_rad_zm%kk, stats_rad_zm%z, & ! intent(in)
    1234             :                           day, month, year, lat_vals, lon_vals, & ! intent(in)
    1235             :                           time_current, stats_metadata%stats_tout, & ! intent(in)
    1236             :                           stats_rad_zm%num_output_fields, & ! intent(in)
    1237             :                           stats_rad_zm%file ) ! intent(inout)
    1238             : 
    1239             :         if ( err_code == clubb_fatal_error ) return
    1240             : #else
    1241           0 :         error stop "This CLUBB program was not compiled with netCDF support."
    1242             : #endif
    1243             :       end if
    1244             : 
    1245             :       call stats_init_rad_zm( vars_rad_zm,                    & ! intent(in)
    1246             :                               l_error,                        & ! intent(inout)
    1247           0 :                               stats_metadata, stats_rad_zm )    ! intent(inout)
    1248             :     end if ! l_output_rad_files
    1249             : 
    1250             : 
    1251             :     ! Initialize stats_sfc (surface point)
    1252             : 
    1253           0 :     ivar = 1
    1254           0 :     do while ( ichar(vars_sfc(ivar)(1:1)) /= 0  & 
    1255             :                .and. len_trim(vars_sfc(ivar)) /= 0 & 
    1256           0 :                .and. ivar <= nvarmax_sfc )
    1257           0 :       ivar = ivar + 1
    1258             :     end do
    1259           0 :     ntot = ivar - 1
    1260           0 :     if ( ntot == nvarmax_sfc ) then
    1261           0 :       write(fstderr,*) "There are more statistical variables listed in ",  &
    1262           0 :                        "vars_sfc than allowed for by nvarmax_sfc."
    1263           0 :       write(fstderr,*) "Check the number of variables listed for vars_sfc ",  &
    1264           0 :                        "in the stats namelist, or change nvarmax_sfc."
    1265           0 :       write(fstderr,*) "nvarmax_sfc = ", nvarmax_sfc
    1266           0 :       write(fstderr,*) "number of variables in vars_sfc = ", ntot
    1267           0 :       write(fstderr,*) "stats_init:  number of sfc statistical variables exceeds limit"
    1268           0 :       err_code = clubb_fatal_error
    1269           0 :       return
    1270             : 
    1271             :     end if
    1272             : 
    1273           0 :     stats_sfc%num_output_fields = ntot
    1274           0 :     stats_sfc%kk = 1
    1275           0 :     stats_sfc%ii = nlon
    1276           0 :     stats_sfc%jj = nlat
    1277             : 
    1278           0 :     allocate( stats_sfc%z( stats_sfc%kk ) )
    1279           0 :     stats_sfc%z = gzm(1)
    1280             : 
    1281           0 :     allocate( stats_sfc%accum_field_values( stats_sfc%ii, stats_sfc%jj, &
    1282           0 :       stats_sfc%kk, stats_sfc%num_output_fields ) )
    1283           0 :     allocate( stats_sfc%accum_num_samples( stats_sfc%ii, stats_sfc%jj, &
    1284           0 :       stats_sfc%kk, stats_sfc%num_output_fields ) )
    1285           0 :     allocate( stats_sfc%l_in_update( stats_sfc%ii, stats_sfc%jj, &
    1286           0 :       stats_sfc%kk, stats_sfc%num_output_fields ) )
    1287             : 
    1288             :     call stats_zero( stats_sfc%ii, stats_sfc%jj, stats_sfc%kk, stats_sfc%num_output_fields, & ! In
    1289           0 :       stats_sfc%accum_field_values, stats_sfc%accum_num_samples, stats_sfc%l_in_update ) ! out
    1290             : 
    1291           0 :     allocate( stats_sfc%file%grid_avg_var( stats_sfc%num_output_fields ) )
    1292           0 :     allocate( stats_sfc%file%z( stats_sfc%kk ) )
    1293             : 
    1294           0 :     fname = trim( stats_metadata%fname_sfc )
    1295             : 
    1296           0 :     if ( stats_metadata%l_grads ) then
    1297             : 
    1298             :       ! Open GrADS files
    1299             :       call open_grads( iunit, fdir, fname,  & ! intent(in)
    1300             :                        1, stats_sfc%kk, nlat, nlon, stats_sfc%z, & ! intent(in)
    1301             :                        day, month, year, lat_vals, lon_vals, & ! intent(in)
    1302             :                        time_current+real(stats_metadata%stats_tout,kind=time_precision), & !intent(in) 
    1303             :                        stats_metadata, stats_sfc%num_output_fields, & ! intent(in)
    1304           0 :                        stats_sfc%file ) ! intent(inout)
    1305             : 
    1306             :     else ! Open NetCDF files
    1307             : #ifdef NETCDF
    1308             :       call open_netcdf_for_writing( nlat, nlon, fdir, fname, 1, stats_sfc%kk, stats_sfc%z, &  ! In
    1309             :                         day, month, year, lat_vals, lon_vals, &  ! In
    1310             :                         time_current, stats_metadata%stats_tout, stats_sfc%num_output_fields, &  ! In
    1311             :                         stats_sfc%file ) ! InOut
    1312             : 
    1313             :       if ( err_code == clubb_fatal_error ) return
    1314             : #else
    1315           0 :       error stop "This CLUBB program was not compiled with netCDF support."
    1316             : #endif
    1317             :     end if
    1318             : 
    1319             :     call stats_init_sfc( vars_sfc,                    & ! intent(in)
    1320             :                          l_error,                     & ! intent(inout)
    1321           0 :                          stats_metadata, stats_sfc )    ! intent(inout)
    1322             : 
    1323             :     ! Check for errors
    1324             : 
    1325           0 :     if ( l_error ) then
    1326           0 :       write(fstderr,*) 'stats_init:  errors found'
    1327           0 :       err_code = clubb_fatal_error
    1328           0 :       return
    1329             :     endif
    1330             : 
    1331             :     return
    1332             : 
    1333             :     ! If namelist was not found in input file, turn off statistics
    1334             : 
    1335             :     100 continue
    1336           0 :     write(fstderr,*) 'Error with statsnl, statistics is turned off'
    1337           0 :     stats_metadata%l_stats       = .false.
    1338           0 :     stats_metadata%l_stats_samp  = .false.
    1339           0 :     stats_metadata%l_stats_last  = .false.
    1340             : 
    1341           0 :     return
    1342             :   end subroutine stats_init
    1343             : 
    1344             :   !-----------------------------------------------------------------------
    1345           0 :   subroutine stats_zero( ii, jj, kk, nn, &
    1346           0 :                          x, n, l_in_update )
    1347             : 
    1348             :     ! Description:
    1349             :     !   Initialize stats to zero
    1350             :     ! References:
    1351             :     !   None
    1352             :     !-----------------------------------------------------------------------
    1353             :     use clubb_precision, only: & 
    1354             :         stat_rknd,   & ! Variable(s)
    1355             :         stat_nknd
    1356             : 
    1357             :     implicit none
    1358             : 
    1359             :     ! Input Variable(s)
    1360             :     integer, intent(in) :: ii, jj, kk, nn
    1361             : 
    1362             :     ! Output Variable(s)
    1363             :     real(kind=stat_rknd), dimension(ii,jj,kk,nn), intent(out)    :: x
    1364             :     integer(kind=stat_nknd), dimension(ii,jj,kk,nn), intent(out) :: n
    1365             :     logical, dimension(ii,jj,kk,nn), intent(out) :: l_in_update
    1366             : 
    1367             :     ! Zero out arrays
    1368             : 
    1369           0 :     if ( nn > 0 ) then
    1370           0 :       x(:,:,:,:) = 0.0_stat_rknd
    1371           0 :       n(:,:,:,:) = 0_stat_nknd
    1372           0 :       l_in_update(:,:,:,:) = .false.
    1373             :     end if
    1374             : 
    1375           0 :     return
    1376             :   end subroutine stats_zero
    1377             : 
    1378             :   !-----------------------------------------------------------------------
    1379           0 :   subroutine stats_avg( ii, jj, kk, nn, n, &
    1380           0 :                         x )
    1381             : 
    1382             :     ! Description:
    1383             :     !   Compute the average of stats fields
    1384             :     ! References:
    1385             :     !   None
    1386             :     !-----------------------------------------------------------------------
    1387             :     use clubb_precision, only: & 
    1388             :         stat_rknd,   & ! Variable(s)
    1389             :         stat_nknd
    1390             : 
    1391             :     use stat_file_module, only: &
    1392             :         clubb_i, clubb_j ! Variable(s)
    1393             : 
    1394             :     implicit none
    1395             : 
    1396             :     ! External
    1397             :     intrinsic :: real
    1398             : 
    1399             :     ! Input Variable(s)
    1400             :     integer, intent(in) :: &
    1401             :       ii, & ! Number of points in X (i.e. latitude) dimension
    1402             :       jj, & ! Number of points in Y (i.e. longitude) dimension
    1403             :       kk, & ! Number of levels in vertical (i.e. Z) dimension
    1404             :       nn    ! Number of variables being output to disk (e.g. cloud_frac, rain rate, etc.)
    1405             : 
    1406             :     integer(kind=stat_nknd), dimension(ii,jj,kk,nn), intent(in) :: &
    1407             :       n ! n is the number of samples for each of the nn fields 
    1408             :         ! and each of the kk vertical levels
    1409             : 
    1410             :     ! Output Variable(s)
    1411             :     real(kind=stat_rknd), dimension(ii,jj,kk,nn), intent(inout) :: &
    1412             :       x ! The variable x contains the cumulative sums of n sample values of each of
    1413             :         ! the nn output fields (e.g. the sum of the sampled rain rate values)
    1414             : 
    1415             :     ! ---- Begin Code ----
    1416             : 
    1417             :     ! Compute averages
    1418           0 :     where ( n(1,1,1:kk,1:nn) > 0 )
    1419           0 :       x(clubb_i,clubb_j,1:kk,1:nn) = x(clubb_i,clubb_j,1:kk,1:nn) &
    1420             :          / real( n(clubb_i,clubb_j,1:kk,1:nn), kind=stat_rknd )
    1421             :     end where
    1422             : 
    1423           0 :     return
    1424             :   end subroutine stats_avg
    1425             : 
    1426             :   !-----------------------------------------------------------------------
    1427           0 :   subroutine stats_begin_timestep( itime, stats_nsamp, stats_nout, &
    1428             :                                    stats_metadata )
    1429             : 
    1430             :     !     Description:
    1431             :     !       Given the elapsed time, set flags determining specifics such as
    1432             :     !       if this time set should be sampled or if this is the first or
    1433             :     !       last time step.
    1434             :     !-----------------------------------------------------------------------
    1435             : 
    1436             :     use stats_variables, only: & 
    1437             :       stats_metadata_type
    1438             : 
    1439             :     implicit none
    1440             : 
    1441             :     ! External
    1442             :     intrinsic :: mod
    1443             : 
    1444             :     ! Input Variable(s)
    1445             :     integer, intent(in) ::  & 
    1446             :       itime, &       ! Elapsed model time       [timestep]
    1447             :       stats_nsamp, & ! Stats sampling interval  [timestep]
    1448             :       stats_nout     ! Stats output interval    [timestep]
    1449             : 
    1450             :     type (stats_metadata_type), intent(inout) :: &
    1451             :       stats_metadata
    1452             : 
    1453           0 :     if ( .not. stats_metadata%l_stats ) return
    1454             : 
    1455             :     ! Only sample time steps that are multiples of "stats_tsamp"
    1456             :     ! in a case's "model.in" file to shorten length of run
    1457           0 :     if ( mod( itime, stats_nsamp ) == 0 ) then
    1458           0 :       stats_metadata%l_stats_samp = .true.
    1459             :     else
    1460           0 :       stats_metadata%l_stats_samp = .false.
    1461             :     end if
    1462             : 
    1463             :     ! Indicates the end of the sampling time period. Signals to start writing to the file
    1464           0 :     if ( mod( itime, stats_nout ) == 0 ) then
    1465           0 :       stats_metadata%l_stats_last = .true.
    1466             :     else
    1467           0 :       stats_metadata%l_stats_last = .false.
    1468             :     end if
    1469             :    
    1470             :     return
    1471             : 
    1472             :   end subroutine stats_begin_timestep
    1473             : 
    1474             :   !-----------------------------------------------------------------------
    1475           0 :   subroutine stats_end_timestep( clubb_params, stats_metadata,  & ! intent(in)
    1476             :                                  stats_zt, stats_zm, stats_sfc, & ! intent(inout)
    1477             :                                  stats_lh_zt, stats_lh_sfc,     & ! intent(inout)
    1478             :                                  stats_rad_zt, stats_rad_zm     & ! intent(inout)
    1479             : #ifdef NETCDF
    1480             :                                  , l_uv_nudge, &
    1481             :                                  l_tke_aniso, &
    1482             :                                  l_standard_term_ta &
    1483             : #endif
    1484             :                                   )
    1485             : 
    1486             :     ! Description: 
    1487             :     !   Called when the stats timestep has ended. This subroutine
    1488             :     !   is responsible for calling statistics to be written to the output
    1489             :     !   format.
    1490             :     !
    1491             :     ! References:
    1492             :     !   None
    1493             :     !-----------------------------------------------------------------------
    1494             : 
    1495             :     use clubb_precision, only: &
    1496             :         core_rknd    ! Variable(s)
    1497             : 
    1498             :     use constants_clubb, only: &
    1499             :         fstderr ! Constant(s)
    1500             : 
    1501             :     use stats_variables, only: & 
    1502             :         stats_metadata_type
    1503             : 
    1504             :     use output_grads, only: &
    1505             :         write_grads ! Procedure(s)
    1506             : 
    1507             :     use stat_file_module, only: &
    1508             :         clubb_i, & ! Variable(s)
    1509             :         clubb_j
    1510             : 
    1511             :     use parameter_indices, only: &
    1512             :         nparams    ! Variable(s)
    1513             : 
    1514             : #ifdef NETCDF
    1515             :     use output_netcdf, only: & 
    1516             :         write_netcdf ! Procedure(s)
    1517             : #endif
    1518             : 
    1519             :     use error_code, only : &
    1520             :         err_code, &         ! Error Indicator
    1521             :         clubb_fatal_error   ! Constant
    1522             : 
    1523             :     use stats_type, only: stats ! Type
    1524             : 
    1525             :     implicit none
    1526             : 
    1527             :     real( kind = core_rknd ), dimension(nparams), intent(in) :: &
    1528             :       clubb_params    ! Array of CLUBB's tunable parameters    [units vary]
    1529             : 
    1530             :     type (stats_metadata_type), intent(in) :: &
    1531             :       stats_metadata
    1532             : 
    1533             :     type (stats), target, intent(inout) :: &
    1534             :       stats_zt, &
    1535             :       stats_zm, &
    1536             :       stats_sfc, &
    1537             :       stats_lh_zt, &
    1538             :       stats_lh_sfc, &
    1539             :       stats_rad_zt, &
    1540             :       stats_rad_zm
    1541             : 
    1542             :     ! External
    1543             :     intrinsic :: floor
    1544             : 
    1545             : #ifdef NETCDF
    1546             :     ! Input Variables
    1547             :     logical, intent(in) :: &
    1548             :       l_uv_nudge,         & ! For wind speed nudging.
    1549             :       l_tke_aniso,        & ! For anisotropic turbulent kinetic energy, i.e.
    1550             :                             ! TKE = 1/2 (u'^2 + v'^2 + w'^2)
    1551             :       l_standard_term_ta    ! Use the standard discretization for the turbulent advection terms.
    1552             :                             ! Setting to .false. means that a_1 and a_3 are pulled outside of the
    1553             :                             ! derivative in advance_wp2_wp3_module.F90 and in
    1554             :                             ! advance_xp2_xpyp_module.F90.
    1555             : #endif
    1556             : 
    1557             :     ! Local Variables
    1558             : 
    1559             :     logical :: l_error
    1560             : 
    1561             :     ! ---- Begin Code ----
    1562             : 
    1563             :     ! Check if it is time to write to file
    1564             : 
    1565           0 :     if ( .not. stats_metadata%l_stats_last ) return
    1566             : 
    1567             :     ! Initialize
    1568           0 :     l_error = .false.
    1569             : 
    1570             :     call stats_check_num_samples( stats_zt, stats_metadata,  & ! intent(in)
    1571           0 :                                   l_error )                    ! intent(inout)
    1572             :     call stats_check_num_samples( stats_zm, stats_metadata,  & ! intent(in)
    1573           0 :                                   l_error )                    ! intent(inout)
    1574             :     call stats_check_num_samples( stats_sfc, stats_metadata, & ! intent(in)
    1575           0 :                                   l_error )                    ! intent(inout)
    1576           0 :     if ( stats_metadata%l_silhs_out ) then
    1577             :       call stats_check_num_samples( stats_lh_zt, stats_metadata,  & ! intent(in)
    1578           0 :                                     l_error )                       ! intent(inout)
    1579             :       call stats_check_num_samples( stats_lh_sfc, stats_metadata, & ! intent(in)
    1580           0 :                                     l_error )                       ! intent(inout)
    1581             :     end if
    1582           0 :     if ( stats_metadata%l_output_rad_files ) then
    1583             :       call stats_check_num_samples( stats_rad_zt, stats_metadata, & ! intent(in)
    1584           0 :                                     l_error )                       ! intent(inout)
    1585             :       call stats_check_num_samples( stats_rad_zm, stats_metadata, & ! intent(in)
    1586           0 :                                     l_error )                       ! intent(inout)
    1587             :     end if
    1588             : 
    1589             :     ! Return if errors are found.
    1590           0 :     if ( l_error ) then
    1591           0 :       write(fstderr,*) 'Possible statistical sampling error'
    1592           0 :       write(fstderr,*) 'For details, set debug_level to a value of at ',  &
    1593           0 :                        'least 1 in the appropriate model.in file.'
    1594           0 :       write(fstderr,*) 'stats_end_timestep:  error(s) found'
    1595           0 :       err_code = clubb_fatal_error
    1596           0 :       return
    1597             :     end if ! l_error
    1598             : 
    1599             :     ! Compute averages
    1600             :     call stats_avg( stats_zt%ii, stats_zt%jj, stats_zt%kk, stats_zt%num_output_fields, & ! In
    1601             :                     stats_zt%accum_num_samples, & ! intent(in)
    1602           0 :                     stats_zt%accum_field_values ) ! intent(inout)
    1603             :     call stats_avg( stats_zm%ii, stats_zm%jj, stats_zm%kk, stats_zm%num_output_fields, & ! In
    1604             :                     stats_zm%accum_num_samples, & ! intent(in)
    1605           0 :                     stats_zm%accum_field_values ) ! intent(inout)
    1606           0 :     if ( stats_metadata%l_silhs_out ) then
    1607             :       call stats_avg( stats_lh_zt%ii, stats_lh_zt%jj, stats_lh_zt%kk, & ! intent(in)
    1608             :          stats_lh_zt%num_output_fields, stats_lh_zt%accum_num_samples, & ! intent(in)
    1609           0 :          stats_lh_zt%accum_field_values ) ! intent(inout)
    1610             :       call stats_avg( stats_lh_sfc%ii, stats_lh_sfc%jj, stats_lh_sfc%kk, & ! intent(in)
    1611             :         stats_lh_sfc%num_output_fields, stats_lh_sfc%accum_num_samples, & ! intent(in)
    1612           0 :         stats_lh_sfc%accum_field_values ) ! intent(inout)
    1613             :     end if
    1614           0 :     if ( stats_metadata%l_output_rad_files ) then
    1615             :       call stats_avg( stats_rad_zt%ii, stats_rad_zt%jj, stats_rad_zt%kk, & ! intent(in)
    1616             :         stats_rad_zt%num_output_fields, & ! intent(in)
    1617             :         stats_rad_zt%accum_num_samples, & ! intent(in)
    1618           0 :         stats_rad_zt%accum_field_values ) ! intent(inout)
    1619             :       call stats_avg( stats_rad_zm%ii, stats_rad_zm%jj, stats_rad_zm%kk, & ! intent(in)
    1620             :         stats_rad_zm%num_output_fields, & ! intent(in)
    1621             :         stats_rad_zm%accum_num_samples, & ! intent(in)
    1622           0 :         stats_rad_zm%accum_field_values ) ! intent(inout)
    1623             :     end if
    1624             :     call stats_avg( stats_sfc%ii, stats_sfc%jj, stats_sfc%kk, stats_sfc%num_output_fields, & ! In
    1625             :         stats_sfc%accum_num_samples, & ! intent(in)
    1626           0 :         stats_sfc%accum_field_values ) ! intent(inout)
    1627             : 
    1628             :     ! Only write to the file and zero out the stats fields if we've reach the horizontal
    1629             :     ! limits of the domain (this is always true in the single-column case because it's 1x1).
    1630           0 :     if ( clubb_i == stats_zt%ii .and. clubb_j == stats_zt%jj ) then
    1631             :       ! Write to file
    1632           0 :       if ( stats_metadata%l_grads ) then
    1633           0 :         call write_grads( stats_zt%file  ) ! intent(inout)
    1634           0 :         call write_grads( stats_zm%file  ) ! intent(inout)
    1635           0 :         if ( stats_metadata%l_silhs_out ) then
    1636           0 :           call write_grads( stats_lh_zt%file  ) ! intent(inout)
    1637           0 :           call write_grads( stats_lh_sfc%file  ) ! intent(inout)
    1638             :         end if
    1639           0 :         if ( stats_metadata%l_output_rad_files ) then
    1640           0 :           call write_grads( stats_rad_zt%file  ) ! intent(inout)
    1641           0 :           call write_grads( stats_rad_zm%file  ) ! intent(inout)
    1642             :         end if
    1643           0 :         call write_grads( stats_sfc%file  ) ! intent(inout)
    1644             :       else ! l_netcdf
    1645             : 
    1646             : #ifdef NETCDF
    1647             :         call write_netcdf( clubb_params, & ! intent(in)
    1648             :                            l_uv_nudge, & ! intent(in)
    1649             :                            l_tke_aniso, & ! intent(in)
    1650             :                            l_standard_term_ta, & ! intent(in)
    1651             :                            stats_zt%file  ) ! intent(inout)
    1652             :         call write_netcdf( clubb_params, & ! intent(in)
    1653             :                            l_uv_nudge, & ! intent(in)
    1654             :                            l_tke_aniso, & ! intent(in)
    1655             :                            l_standard_term_ta, & ! intent(in)
    1656             :                            stats_zm%file  ) ! intent(inout)
    1657             :         if ( stats_metadata%l_silhs_out ) then
    1658             :           call write_netcdf( clubb_params, & ! intent(in)
    1659             :                              l_uv_nudge, & ! intent(in)
    1660             :                              l_tke_aniso, & ! intent(in)
    1661             :                              l_standard_term_ta, & ! intent(in)
    1662             :                              stats_lh_zt%file  ) ! intent(inout)
    1663             :           call write_netcdf( clubb_params, & ! intent(in)
    1664             :                              l_uv_nudge, & ! intent(in)
    1665             :                              l_tke_aniso, & ! intent(in)
    1666             :                              l_standard_term_ta, & ! intent(in)
    1667             :                              stats_lh_sfc%file  ) ! intent(inout)
    1668             :         end if
    1669             :         if ( stats_metadata%l_output_rad_files ) then
    1670             :           call write_netcdf( clubb_params, & ! intent(in)
    1671             :                              l_uv_nudge, & ! intent(in)
    1672             :                              l_tke_aniso, & ! intent(in)
    1673             :                              l_standard_term_ta, & ! intent(in)
    1674             :                              stats_rad_zt%file  ) ! intent(inout)
    1675             :           call write_netcdf( clubb_params, & ! intent(in)
    1676             :                              l_uv_nudge, & ! intent(in)
    1677             :                              l_tke_aniso, & ! intent(in)
    1678             :                              l_standard_term_ta, & ! intent(in)
    1679             :                              stats_rad_zm%file  ) ! intent(inout)
    1680             :         end if
    1681             :         call write_netcdf( clubb_params, & ! intent(in)
    1682             :                            l_uv_nudge, & ! intent(in)
    1683             :                            l_tke_aniso, & ! intent(in)
    1684             :                            l_standard_term_ta, & ! intent(in)
    1685             :                            stats_sfc%file  ) ! intent(inout)
    1686             :             
    1687             :         if ( err_code == clubb_fatal_error ) return
    1688             : #else
    1689           0 :         error stop "This program was not compiled with netCDF support"
    1690             : #endif /* NETCDF */
    1691             :       end if ! l_grads
    1692             : 
    1693             :       ! Reset sample fields
    1694             :       call stats_zero( stats_zt%ii, stats_zt%jj, stats_zt%kk, stats_zt%num_output_fields, & ! In
    1695           0 :       stats_zt%accum_field_values, stats_zt%accum_num_samples, stats_zt%l_in_update ) ! out
    1696             :       call stats_zero( stats_zm%ii, stats_zm%jj, stats_zm%kk, stats_zm%num_output_fields, & ! In
    1697           0 :         stats_zm%accum_field_values, stats_zm%accum_num_samples, stats_zm%l_in_update ) ! Out
    1698           0 :       if ( stats_metadata%l_silhs_out ) then
    1699             :         call stats_zero( stats_lh_zt%ii, stats_lh_zt%jj, stats_lh_zt%kk, & ! intent(in)
    1700             :           stats_lh_zt%num_output_fields, & ! intent(in)
    1701             :           stats_lh_zt%accum_field_values, & ! intent(out)
    1702           0 :           stats_lh_zt%accum_num_samples, stats_lh_zt%l_in_update ) ! intent(out)
    1703             :         call stats_zero( stats_lh_sfc%ii, stats_lh_sfc%jj, stats_lh_sfc%kk, & ! intent(in)
    1704             :           stats_lh_sfc%num_output_fields, & ! intent(in)
    1705             :           stats_lh_sfc%accum_field_values, & ! intent(out)
    1706           0 :           stats_lh_sfc%accum_num_samples, stats_lh_sfc%l_in_update ) ! intent(out)
    1707             :       end if
    1708           0 :       if ( stats_metadata%l_output_rad_files ) then
    1709             :         call stats_zero( stats_rad_zt%ii, stats_rad_zt%jj, stats_rad_zt%kk, & ! intent(in)
    1710             :           stats_rad_zt%num_output_fields, & ! intent(in)
    1711             :           stats_rad_zt%accum_field_values, & ! intent(out)
    1712           0 :           stats_rad_zt%accum_num_samples, stats_rad_zt%l_in_update ) ! intent(out)
    1713             :         call stats_zero( stats_rad_zt%ii, stats_rad_zt%jj, stats_rad_zm%kk, & ! intent(in)
    1714             :           stats_rad_zm%num_output_fields, & ! intent(in)
    1715             :           stats_rad_zm%accum_field_values, & ! intent(out)
    1716           0 :           stats_rad_zm%accum_num_samples, stats_rad_zm%l_in_update ) ! intent(out)
    1717             :       end if
    1718             :       call stats_zero( stats_sfc%ii, stats_sfc%jj, stats_sfc%kk, stats_sfc%num_output_fields, & !IN
    1719             :         stats_sfc%accum_field_values, & ! intent(out)
    1720           0 :         stats_sfc%accum_num_samples, stats_sfc%l_in_update ) ! intent(out)
    1721             : 
    1722             :     end if ! clubb_i = stats_zt%ii .and. clubb_j == stats_zt%jj
    1723             : 
    1724             : 
    1725             :     return
    1726             :   end subroutine stats_end_timestep
    1727             : 
    1728             :   !----------------------------------------------------------------------
    1729           0 :   subroutine stats_accumulate( &
    1730           0 :                      nz, invrs_dzm, zt, dzm, dzt, dt, &
    1731           0 :                      um, vm, upwp, vpwp, up2, vp2, &
    1732           0 :                      thlm, rtm, wprtp, wpthlp, &
    1733           0 :                      wp2, wp3, rtp2, rtp3, thlp2, thlp3, rtpthlp, &
    1734           0 :                      wpthvp, wp2thvp, rtpthvp, thlpthvp, &
    1735           0 :                      p_in_Pa, exner, rho, rho_zm, &
    1736           0 :                      rho_ds_zm, rho_ds_zt, thv_ds_zm, thv_ds_zt, &
    1737           0 :                      wm_zt, wm_zm, rcm, wprcp, rc_coef, rc_coef_zm, &
    1738           0 :                      rcm_zm, rtm_zm, thlm_zm, cloud_frac, ice_supersat_frac, &
    1739           0 :                      cloud_frac_zm, ice_supersat_frac_zm, rcm_in_layer, &
    1740           0 :                      cloud_cover, rcm_supersat_adj, sigma_sqd_w, &
    1741           0 :                      thvm, ug, vg, Lscale, wpthlp2, wp2thlp, wprtp2, wp2rtp, &
    1742           0 :                      Lscale_up, Lscale_down, tau_zt, Kh_zt, wp2rcp, &
    1743           0 :                      wprtpthlp, sigma_sqd_w_zt, rsat, wp2_zt, thlp2_zt, &
    1744           0 :                      wpthlp_zt, wprtp_zt, rtp2_zt, rtpthlp_zt, up2_zt, &
    1745           0 :                      vp2_zt, upwp_zt, vpwp_zt, wpup2, wpvp2, & 
    1746           0 :                      wp2up2, wp2vp2, wp4, &
    1747           0 :                      tau_zm, Kh_zm, thlprcp, &
    1748           0 :                      rtprcp, rcp2, em, a3_coef, a3_coef_zt, &
    1749           0 :                      wp3_zm, wp3_on_wp2, wp3_on_wp2_zt, Skw_velocity, &
    1750           0 :                      w_up_in_cloud, w_down_in_cloud, cloudy_updraft_frac, &
    1751           0 :                      cloudy_downdraft_frac, pdf_params, pdf_params_zm, &
    1752           0 :                      sclrm, sclrp2, sclrprtp, sclrpthlp, sclrm_forcing, sclrpthvp, &
    1753           0 :                      wpsclrp, sclrprcp, wp2sclrp, wpsclrp2, wpsclrprtp, &
    1754           0 :                      wpsclrpthlp, wpedsclrp, edsclrm, edsclrm_forcing, &
    1755             :                      stats_metadata, &
    1756             :                      stats_zt, stats_zm, stats_sfc )
    1757             : 
    1758             :     ! Description:
    1759             :     !   Accumulate those stats variables that are preserved in CLUBB from timestep to
    1760             :     !   timestep, but not those stats that are not, (e.g. budget terms, longwave and
    1761             :     !   shortwave components, etc.)
    1762             :     !
    1763             :     ! References:
    1764             :     !   None
    1765             :     !----------------------------------------------------------------------
    1766             : 
    1767             :     use constants_clubb, only: &
    1768             :         cloud_frac_min, &  ! Constant
    1769             :         eps
    1770             : 
    1771             : 
    1772             :     use pdf_utilities, only: &
    1773             :         compute_variance_binormal    ! Procedure
    1774             : 
    1775             :     use stats_variables, only: & 
    1776             :         stats_metadata_type
    1777             : 
    1778             :     use grid_class, only: & 
    1779             :         zt2zm ! Procedure(s)
    1780             : 
    1781             :     use pdf_parameter_module, only: & 
    1782             :         pdf_parameter ! Type
    1783             : 
    1784             :     use T_in_K_module, only: & 
    1785             :         thlm2T_in_K ! Procedure
    1786             : 
    1787             :     use constants_clubb, only: & 
    1788             :         rc_tol, fstderr    ! Constant(s)
    1789             : 
    1790             :     use parameters_model, only: & 
    1791             :         sclr_dim,  &        ! Variable(s)
    1792             :         edsclr_dim
    1793             : 
    1794             :     use stats_type_utilities, only: & 
    1795             :         stat_update_var,  & ! Procedure(s)
    1796             :         stat_update_var_pt
    1797             : 
    1798             :     use advance_helper_module, only: &
    1799             :         vertical_avg, &     ! Procedure(s)
    1800             :         vertical_integral
    1801             : 
    1802             :     use interpolation, only: & 
    1803             :         lin_interpolate_two_points             ! Procedure
    1804             : 
    1805             :     use saturation, only: &
    1806             :         sat_mixrat_ice ! Procedure
    1807             : 
    1808             :     use clubb_precision, only: &
    1809             :         core_rknd ! Variable(s)
    1810             : 
    1811             :     use stats_type, only: stats ! Type
    1812             : 
    1813             :     implicit none
    1814             : 
    1815             :     ! Input Variable(s)
    1816             :     integer, intent(in) :: &
    1817             :       nz
    1818             :     
    1819             :     real( kind = core_rknd ), intent(in), dimension(nz) :: & 
    1820             :       invrs_dzm, & ! The inverse spacing between thermodynamic grid
    1821             :                    ! levels; centered over momentum grid levels.
    1822             :       zt,        & ! Thermo grid
    1823             :       dzm,       & ! Spacing between thermodynamic grid levels; centered over
    1824             :                    ! momentum grid levels
    1825             :       dzt          ! Spcaing between momentum grid levels; centered over
    1826             :                    ! thermodynamic grid levels
    1827             : 
    1828             :     real( kind = core_rknd ), intent(in) ::  &
    1829             :       dt           ! Model timestep                        [s]
    1830             : 
    1831             :     real( kind = core_rknd ), intent(in), dimension(nz) :: & 
    1832             :       um,       & ! u wind (thermodynamic levels)          [m/s]
    1833             :       vm,       & ! v wind (thermodynamic levels)          [m/s]
    1834             :       upwp,     & ! vertical u momentum flux (m levs.)     [m^2/s^2]
    1835             :       vpwp,     & ! vertical v momentum flux (m levs.)     [m^2/s^2]
    1836             :       up2,      & ! < u'^2 > (momentum levels)             [m^2/s^2]
    1837             :       vp2,      & ! < v'^2 > (momentum levels)             [m^2/s^2]
    1838             :       thlm,     & ! liquid potential temperature (t levs.) [K]
    1839             :       rtm,      & ! total water mixing ratio (t levs.)     [kg/kg]
    1840             :       wprtp,    & ! < w' r_t' > (momentum levels)          [m/s kg/kg]
    1841             :       wpthlp,   & ! < w' th_l' > (momentum levels)         [m/s K]
    1842             :       wp2,      & ! < w'^2 > (momentum levels)             [m^2/s^2]
    1843             :       wp3,      & ! < w'^3 > (thermodynamic levels)        [m^3/s^3]
    1844             :       rtp2,     & ! < r_t'^2 > (momentum levels)           [(kg/kg)^2]
    1845             :       rtp3,     & ! < r_t'^3 > (thermodynamic levels)      [(kg/kg)^3]
    1846             :       thlp2,    & ! < th_l'^2 > (momentum levels)          [K^2]
    1847             :       thlp3,    & ! < th_l'^3 > (thermodynamic levels)     [K^3]
    1848             :       rtpthlp,  & ! < r_t' th_l' > (momentum levels)       [kg/kg K]
    1849             :       wpthvp,   & ! < w' th_v' > (momentum levels)         [kg/kg K]
    1850             :       wp2thvp,  & ! < w'^2 th_v' > (thermodynamic levels)  [m^2/s^2 K]
    1851             :       rtpthvp,  & ! < r_t' th_v' > (momentum levels)       [kg/kg K]
    1852             :       thlpthvp    ! < th_l' th_v' > (momentum levels)      [K^2]
    1853             : 
    1854             :     real( kind = core_rknd ), intent(in), dimension(nz) :: & 
    1855             :       p_in_Pa,      & ! Pressure (Pa) on thermodynamic points    [Pa]
    1856             :       exner,        & ! Exner function = ( p / p0 ) ** kappa     [-]
    1857             :       rho,          & ! Density (thermodynamic levels)           [kg/m^3]
    1858             :       rho_zm,       & ! Density on momentum levels               [kg/m^3]
    1859             :       rho_ds_zm,    & ! Dry, static density (momentum levels)    [kg/m^3]
    1860             :       rho_ds_zt,    & ! Dry, static density (thermo. levs.)      [kg/m^3]
    1861             :       thv_ds_zm,    & ! Dry, base-state theta_v (momentum levs.) [K]
    1862             :       thv_ds_zt,    & ! Dry, base-state theta_v (thermo. levs.)  [K]
    1863             :       wm_zt,        & ! w on thermodynamic levels                [m/s]
    1864             :       wm_zm           ! w on momentum levels                     [m/s]
    1865             : 
    1866             :     real( kind = core_rknd ), intent(in), dimension(nz) :: & 
    1867             :       rcm_zm,               & ! Cloud water mixing ratio on m levs.      [kg/kg]
    1868             :       rtm_zm,               & ! Total water mixing ratio on m levs.      [kg/kg]
    1869             :       thlm_zm,              & ! Liquid potential temperature on m levs.  [K]
    1870             :       rcm,                  & ! Cloud water mixing ratio (t levs.)       [kg/kg]
    1871             :       wprcp,                & ! < w' r_c' > (momentum levels)            [m/s kg/kg]
    1872             :       rc_coef,              & ! Coefficient of X'r_c' (t-levs.)      [K/(kg/kg)]
    1873             :       rc_coef_zm,           & ! Coefficient of X'r_c' on m-levs.     [K/(kg/kg)]
    1874             :       cloud_frac,           & ! Cloud fraction (thermodynamic levels)    [-]
    1875             :       ice_supersat_frac,    & ! Ice cloud fracion (thermodynamic levels) [-]
    1876             :       cloud_frac_zm,        & ! Cloud fraction on zm levels              [-]
    1877             :       ice_supersat_frac_zm, & ! Ice cloud fraction on zm levels          [-]
    1878             :       rcm_in_layer,         & ! Cloud water mixing ratio in cloud layer  [kg/kg]
    1879             :       cloud_cover,          & ! Cloud cover                              [-]
    1880             :       rcm_supersat_adj        ! rcm adjustment due to supersaturation    [kg/kg]
    1881             : 
    1882             :     real( kind = core_rknd ), intent(in), dimension(nz) :: &
    1883             :       sigma_sqd_w    ! PDF width parameter (momentum levels)    [-]
    1884             : 
    1885             :     real( kind = core_rknd ), intent(in), dimension(nz) :: & 
    1886             :         thvm,           & ! Virtual potential temperature        [K]
    1887             :         ug,             & ! u geostrophic wind                   [m/s]
    1888             :         vg,             & ! v geostrophic wind                   [m/s]
    1889             :         Lscale,         & ! Length scale                         [m]
    1890             :         wpthlp2,        & ! w'thl'^2                             [m K^2/s]
    1891             :         wp2thlp,        & ! w'^2 thl'                            [m^2 K/s^2]
    1892             :         wprtp2,         & ! w'rt'^2                              [m/s kg^2/kg^2]
    1893             :         wp2rtp,         & ! w'^2rt'                              [m^2/s^2 kg/kg]
    1894             :         Lscale_up,      & ! Length scale (upwards component)     [m]
    1895             :         Lscale_down,    & ! Length scale (downwards component)   [m]
    1896             :         tau_zt,         & ! Eddy diss. time scale; thermo. levs. [s]
    1897             :         Kh_zt,          & ! Eddy diff. coef. on thermo. levels   [m^2/s]
    1898             :         wp2rcp,         & ! w'^2 rc'                             [m^2/s^2 kg/kg]
    1899             :         wprtpthlp,      & ! w'rt'thl'                            [m/s kg/kg K]
    1900             :         sigma_sqd_w_zt, & ! PDF width parameter (thermo. levels) [-]
    1901             :         rsat              ! Saturation mixing ratio              [kg/kg]
    1902             : 
    1903             :     real( kind = core_rknd ), intent(in), dimension(nz) :: & 
    1904             :       wp2_zt,                & ! w'^2 on thermo. grid                  [m^2/s^2]
    1905             :       thlp2_zt,              & ! thl'^2 on thermo. grid                [K^2]
    1906             :       wpthlp_zt,             & ! w'thl' on thermo. grid                [m K/s]
    1907             :       wprtp_zt,              & ! w'rt' on thermo. grid                 [m kg/(kg s)]
    1908             :       rtp2_zt,               & ! rt'^2 on therm. grid                  [(kg/kg)^2]
    1909             :       rtpthlp_zt,            & ! rt'thl' on thermo. grid               [kg K/kg]
    1910             :       up2_zt,                & ! u'^2 on thermo. grid                  [m^2/s^2]
    1911             :       vp2_zt,                & ! v'^2 on thermo. grid                  [m^2/s^2]
    1912             :       upwp_zt,               & ! u'w' on thermo. grid                  [m^2/s^2]
    1913             :       vpwp_zt,               & ! v'w' on thermo. grid                  [m^2/s^2]
    1914             :       wpup2,                 & ! w'u'^2 (thermodynamic levels)         [m^3/s^3]
    1915             :       wpvp2,                 & ! w'v'^2 (thermodynamic levels)         [m^3/s^3]
    1916             :       wp2up2,                & ! < w'^2u'^2 > (momentum levels)        [m^4/s^4]
    1917             :       wp2vp2,                & ! < w'^2v'^2 > (momentum levels)        [m^4/s^4]
    1918             :       wp4,                   & ! < w'^4 > (momentum levels)            [m^4/s^4]
    1919             :       tau_zm,                & ! Eddy diss. time scale; momentum levs. [s]
    1920             :       Kh_zm,                 & ! Eddy diff. coef. on momentum levels   [m^2/s]
    1921             :       thlprcp,               & ! thl'rc'                               [K kg/kg]
    1922             :       rtprcp,                & ! rt'rc'                                [kg^2/kg^2]
    1923             :       rcp2,                  & ! rc'^2                                 [kg^2/kg^2]
    1924             :       em,                    & ! Turbulent Kinetic Energy (TKE)        [m^2/s^2]
    1925             :       a3_coef,               & ! The a3 coefficient from CLUBB eqns    [-]
    1926             :       a3_coef_zt,            & ! The a3 coef. interp. to the zt grid   [-]
    1927             :       wp3_zm,                & ! w'^3 interpolated to momentum levels  [m^3/s^3]
    1928             :       wp3_on_wp2,            & ! w'^3 / w'^2 on the zm grid            [m/s]
    1929             :       wp3_on_wp2_zt,         & ! w'^3 / w'^2 on the zt grid            [m/s]
    1930             :       Skw_velocity,          & ! Skewness velocity                     [m/s]
    1931             :       w_up_in_cloud,         & ! Mean cloudy updraft speed             [m/s]
    1932             :       w_down_in_cloud,       & ! Mean cloudy downdraft speed           [m/s]
    1933             :       cloudy_updraft_frac,   & ! Cloudy updraft fraction               [-]
    1934             :       cloudy_downdraft_frac    ! Cloudy downdraft fraction             [-]
    1935             : 
    1936             :     type(pdf_parameter), intent(in) :: & 
    1937             :       pdf_params,    & ! PDF parameters (thermodynamic levels)    [units vary]
    1938             :       pdf_params_zm    ! PDF parameters on momentum levels        [units vary]
    1939             : 
    1940             :     real( kind = core_rknd ), intent(in), dimension(nz,sclr_dim) :: & 
    1941             :       sclrm,           & ! High-order passive scalar            [units vary]
    1942             :       sclrp2,          & ! High-order passive scalar variance   [units^2]
    1943             :       sclrprtp,        & ! High-order passive scalar covariance [units kg/kg]
    1944             :       sclrpthlp,       & ! High-order passive scalar covariance [units K]
    1945             :       sclrm_forcing,   & ! Large-scale forcing of scalar        [units/s]
    1946             :       sclrpthvp,       & ! High-order passive scalar covariance [units K]
    1947             :       wpsclrp            ! w'sclr'                              [units m/s]
    1948             : 
    1949             :     real( kind = core_rknd ), intent(in), dimension(nz,sclr_dim) :: & 
    1950             :       sclrprcp,    & ! sclr'rc'     [units vary]
    1951             :       wp2sclrp,    & ! w'^2 sclr'   [units vary]
    1952             :       wpsclrp2,    & ! w'sclr'^2    [units vary]
    1953             :       wpsclrprtp,  & ! w'sclr'rt'   [units vary]
    1954             :       wpsclrpthlp    ! w'sclr'thl'  [units vary]
    1955             : 
    1956             :     real( kind = core_rknd ), intent(in), dimension(nz,edsclr_dim) :: & 
    1957             :       wpedsclrp,       & ! w'edsclr'                        [units vary]
    1958             :       edsclrm,         & ! Eddy-diff passive scalar         [units vary] 
    1959             :       edsclrm_forcing    ! Large-scale forcing of edscalar  [units vary]
    1960             : 
    1961             :     type (stats_metadata_type), intent(in) :: &
    1962             :       stats_metadata
    1963             : 
    1964             :     type (stats), target, intent(inout) :: &
    1965             :       stats_zt, &
    1966             :       stats_zm, &
    1967             :       stats_sfc
    1968             : 
    1969             :     ! Local Variables
    1970             : 
    1971             :     integer :: isclr, k
    1972             :     integer :: grid_level = 1  ! grid level for stats where there is only one sensible level (eg timeseries)
    1973             : 
    1974             :     real( kind = core_rknd ), dimension(nz) :: &
    1975           0 :       T_in_K,      &  ! Absolute temperature         [K]
    1976           0 :       rsati,       &  ! Saturation w.r.t ice         [kg/kg]
    1977           0 :       shear,       &  ! Wind shear production term   [m^2/s^3]
    1978           0 :       chi,         &  ! Mellor's 's'                 [kg/kg]
    1979           0 :       chip2,         &  ! Variance of Mellor's 's'     [kg/kg]
    1980           0 :       rcm_in_cloud    ! rcm in cloud                 [kg/kg]
    1981             : 
    1982             :     real( kind = core_rknd ) :: xtmp
    1983             : 
    1984             :     ! ---- Begin Code ----
    1985             : 
    1986             :     ! Sample fields
    1987             : 
    1988           0 :     if ( stats_metadata%l_stats_samp ) then
    1989             : 
    1990             :       ! stats_zt variables
    1991             : 
    1992             : 
    1993           0 :       if ( stats_metadata%iT_in_K > 0 .or. stats_metadata%irsati > 0 ) then
    1994           0 :         T_in_K = thlm2T_in_K( nz, thlm, exner, rcm )
    1995             :       else
    1996           0 :         T_in_K = -999._core_rknd
    1997             :       end if
    1998             : 
    1999             :       call stat_update_var( stats_metadata%iT_in_K, T_in_K, & ! intent(in)
    2000           0 :                             stats_zt ) ! intent(inout)
    2001             :  
    2002             :       call stat_update_var( stats_metadata%ithlm, thlm, & ! intent(in)
    2003           0 :                             stats_zt ) ! intent(inout)
    2004             :       call stat_update_var( stats_metadata%ithvm, thvm, & ! intent(in)
    2005           0 :                             stats_zt ) ! intent(inout)
    2006             :       call stat_update_var( stats_metadata%irtm, rtm, & ! intent(in)
    2007           0 :                             stats_zt ) ! intent(inout)
    2008             :       call stat_update_var( stats_metadata%ircm, rcm, & ! intent(in)
    2009           0 :                             stats_zt ) ! intent(inout)
    2010             :       call stat_update_var( stats_metadata%ium, um, & ! intent(in)
    2011           0 :                             stats_zt ) ! intent(inout)
    2012             :       call stat_update_var( stats_metadata%ivm, vm, & ! intent(in)
    2013           0 :                             stats_zt ) ! intent(inout)
    2014             :       call stat_update_var( stats_metadata%iwm_zt, wm_zt, & ! intent(in)
    2015           0 :                             stats_zt ) ! intent(inout)
    2016             :       call stat_update_var( stats_metadata%iwm_zm, wm_zm, & ! intent(in)
    2017           0 :                             stats_zm ) ! intent(inout)
    2018             :       call stat_update_var( stats_metadata%iug, ug, & ! intent(in) 
    2019           0 :                             stats_zt ) ! intent(inout)
    2020             :       call stat_update_var( stats_metadata%ivg, vg, & ! intent(in)
    2021           0 :                             stats_zt ) ! intent(inout)
    2022             :       call stat_update_var( stats_metadata%icloud_frac, cloud_frac, & ! intent(in)
    2023           0 :                             stats_zt ) ! intent(inout)
    2024             :       call stat_update_var( stats_metadata%iice_supersat_frac, ice_supersat_frac, & ! intent(in)
    2025           0 :                             stats_zt) ! intent(inout)
    2026             :       call stat_update_var( stats_metadata%ircm_in_layer, rcm_in_layer, & ! intent(in)
    2027           0 :                             stats_zt ) ! intent(inout)
    2028             :       call stat_update_var( stats_metadata%icloud_cover, cloud_cover, & ! intent(in)
    2029           0 :                             stats_zt ) ! intent(inout)
    2030             :       call stat_update_var( stats_metadata%ircm_supersat_adj, rcm_supersat_adj, & ! intent(in)
    2031           0 :                             stats_zt ) ! intent(inout)
    2032             :       call stat_update_var( stats_metadata%ip_in_Pa, p_in_Pa, & ! intent(in)
    2033           0 :                             stats_zt ) ! intent(inout)
    2034             :       call stat_update_var( stats_metadata%iexner, exner, & ! intent(in)
    2035           0 :                             stats_zt ) ! intent(inout)
    2036             :       call stat_update_var( stats_metadata%irho_ds_zt, rho_ds_zt, & ! intent(in)
    2037           0 :                             stats_zt ) ! intent(inout)
    2038             :       call stat_update_var( stats_metadata%ithv_ds_zt, thv_ds_zt, & ! intent(in)
    2039           0 :                             stats_zt ) ! intent(inout)
    2040             :       call stat_update_var( stats_metadata%iLscale, Lscale, & ! intent(in)
    2041           0 :                             stats_zt ) ! intent(inout)
    2042             :       call stat_update_var( stats_metadata%iwpup2, wpup2, & ! intent(in)
    2043           0 :                             stats_zt ) ! intent(inout)
    2044             :       call stat_update_var( stats_metadata%iwpvp2, wpvp2, & ! intent(in)
    2045           0 :                             stats_zt ) ! intent(inout)
    2046             :       call stat_update_var( stats_metadata%iwp3, wp3, & ! intent(in)
    2047           0 :                             stats_zt ) ! intent(inout)
    2048             :       call stat_update_var( stats_metadata%iwpthlp2, wpthlp2, & ! intent(in)
    2049           0 :                             stats_zt ) ! intent(inout)
    2050             :       call stat_update_var( stats_metadata%iwp2thlp, wp2thlp, & ! intent(in)
    2051           0 :                             stats_zt ) ! intent(inout)
    2052             :       call stat_update_var( stats_metadata%iwprtp2, wprtp2, & ! intent(in)
    2053           0 :                             stats_zt ) ! intent(inout)
    2054             :       call stat_update_var( stats_metadata%iwp2rtp, wp2rtp, & ! intent(in)
    2055           0 :                             stats_zt ) ! intent(inout)
    2056             :       call stat_update_var( stats_metadata%iLscale_up, Lscale_up, & ! intent(in)
    2057           0 :                             stats_zt ) ! intent(inout)
    2058             :       call stat_update_var( stats_metadata%iLscale_down, Lscale_down, & ! intent(in)
    2059           0 :                             stats_zt ) ! intent(inout)
    2060             :       call stat_update_var( stats_metadata%itau_zt, tau_zt, & ! intent(in)
    2061           0 :                             stats_zt ) ! intent(inout)
    2062             :       call stat_update_var( stats_metadata%iKh_zt, Kh_zt, & ! intent(in)
    2063           0 :                             stats_zt ) ! intent(inout)
    2064             :       call stat_update_var( stats_metadata%iwp2thvp, wp2thvp, & ! intent(in)
    2065           0 :                             stats_zt ) ! intent(inout)
    2066             :       call stat_update_var( stats_metadata%iwp2rcp, wp2rcp, & ! intent(in)
    2067           0 :                             stats_zt ) ! intent(inout)
    2068             :       call stat_update_var( stats_metadata%iw_up_in_cloud, w_up_in_cloud, & ! intent(in)
    2069           0 :                             stats_zt ) ! intent(inout)
    2070             :       call stat_update_var( stats_metadata%iw_down_in_cloud, w_down_in_cloud, & ! intent(in)
    2071           0 :                             stats_zt ) ! intent(inout)
    2072             :       call stat_update_var( stats_metadata%icld_updr_frac, cloudy_updraft_frac, & ! intent(in)
    2073           0 :                             stats_zt ) ! intent(inout)
    2074             :       call stat_update_var( stats_metadata%icld_downdr_frac, cloudy_downdraft_frac, & ! intent(in)
    2075           0 :                             stats_zt ) ! intent(inout)
    2076             :       call stat_update_var( stats_metadata%iwprtpthlp, wprtpthlp, & ! intent(in)
    2077           0 :                             stats_zt ) ! intent(inout)
    2078             :       call stat_update_var( stats_metadata%irc_coef, rc_coef, & ! intent(in)
    2079           0 :                             stats_zt ) ! intent(inout)
    2080             :       call stat_update_var( stats_metadata%isigma_sqd_w_zt, sigma_sqd_w_zt, & ! intent(in)
    2081           0 :                             stats_zt ) ! intent(inout)
    2082             :       call stat_update_var( stats_metadata%irho, rho, & ! intent(in)
    2083           0 :                             stats_zt ) ! intent(inout)
    2084             :       call stat_update_var( stats_metadata%irsat, rsat, & ! intent(in)
    2085           0 :                             stats_zt ) ! intent(inout)
    2086           0 :       if ( stats_metadata%irsati > 0 ) then
    2087           0 :         do k = 1, nz
    2088           0 :           rsati(k) = sat_mixrat_ice( p_in_Pa(k), T_in_K(k) )
    2089             :         end do
    2090             :         call stat_update_var( stats_metadata%irsati, rsati, & ! intent(in)
    2091           0 :                               stats_zt ) ! intent(inout)
    2092             :       end if
    2093             : 
    2094           0 :       call stat_update_var( stats_metadata%imixt_frac, pdf_params%mixt_frac(1,:), & ! intent(in)
    2095           0 :                             stats_zt ) ! intent(inout)
    2096           0 :       call stat_update_var( stats_metadata%iw_1, pdf_params%w_1(1,:), & ! intent(in)
    2097           0 :                             stats_zt ) ! intent(inout)
    2098           0 :       call stat_update_var( stats_metadata%iw_2, pdf_params%w_2(1,:), & ! intent(in)
    2099           0 :                             stats_zt ) ! intent(inout)
    2100           0 :       call stat_update_var( stats_metadata%ivarnce_w_1, pdf_params%varnce_w_1(1,:), & ! intent(in)
    2101           0 :                             stats_zt ) ! intent(inout)
    2102           0 :       call stat_update_var( stats_metadata%ivarnce_w_2, pdf_params%varnce_w_2(1,:), & ! intent(in)
    2103           0 :                             stats_zt ) ! intent(inout)
    2104           0 :       call stat_update_var( stats_metadata%ithl_1, pdf_params%thl_1(1,:), & ! intent(in)
    2105           0 :                             stats_zt ) ! intent(inout)
    2106           0 :       call stat_update_var( stats_metadata%ithl_2, pdf_params%thl_2(1,:), & ! intent(in)
    2107           0 :                             stats_zt ) ! intent(inout)
    2108           0 :       call stat_update_var( stats_metadata%ivarnce_thl_1, pdf_params%varnce_thl_1(1,:), & ! intent(in)
    2109           0 :                             stats_zt ) ! intent(inout)
    2110           0 :       call stat_update_var( stats_metadata%ivarnce_thl_2, pdf_params%varnce_thl_2(1,:), & ! intent(in)
    2111           0 :                             stats_zt ) ! intent(inout)
    2112           0 :       call stat_update_var( stats_metadata%irt_1, pdf_params%rt_1(1,:), & ! intent(in)
    2113           0 :                             stats_zt ) ! intent(inout)
    2114           0 :       call stat_update_var( stats_metadata%irt_2, pdf_params%rt_2(1,:), & ! intent(in)
    2115           0 :                             stats_zt ) ! intent(inout)
    2116           0 :       call stat_update_var( stats_metadata%ivarnce_rt_1, pdf_params%varnce_rt_1(1,:), & ! intent(in)
    2117           0 :                             stats_zt ) ! intent(inout)
    2118           0 :       call stat_update_var( stats_metadata%ivarnce_rt_2, pdf_params%varnce_rt_2(1,:), & ! intent(in)
    2119           0 :                             stats_zt ) ! intent(inout )
    2120           0 :       call stat_update_var( stats_metadata%irc_1, pdf_params%rc_1(1,:), & ! intent(in)
    2121           0 :                             stats_zt ) ! intent(inout)
    2122           0 :       call stat_update_var( stats_metadata%irc_2, pdf_params%rc_2(1,:), & ! intent(in)
    2123           0 :                             stats_zt ) ! intent(inout)
    2124           0 :       call stat_update_var( stats_metadata%irsatl_1, pdf_params%rsatl_1(1,:), & ! intent(in)
    2125           0 :                             stats_zt ) ! intent(inout)
    2126           0 :       call stat_update_var( stats_metadata%irsatl_2, pdf_params%rsatl_2(1,:), & ! intent(in)
    2127           0 :                             stats_zt ) ! intent(inout)
    2128           0 :       call stat_update_var( stats_metadata%icloud_frac_1, pdf_params%cloud_frac_1(1,:), & ! intent(in)
    2129           0 :                             stats_zt ) ! intent(inout)
    2130           0 :       call stat_update_var( stats_metadata%icloud_frac_2, pdf_params%cloud_frac_2(1,:), & ! intent(in)
    2131           0 :                             stats_zt ) ! intent(inout)
    2132           0 :       call stat_update_var( stats_metadata%ichi_1, pdf_params%chi_1(1,:), & ! intent(in)
    2133           0 :                             stats_zt ) ! intent(inout)
    2134           0 :       call stat_update_var( stats_metadata%ichi_2, pdf_params%chi_2(1,:), & ! intent(in)
    2135           0 :                             stats_zt ) ! intent(inout)
    2136           0 :       call stat_update_var( stats_metadata%istdev_chi_1, pdf_params%stdev_chi_1(1,:), & ! intent(in)
    2137           0 :                             stats_zt ) ! intent(inout)
    2138           0 :       call stat_update_var( stats_metadata%istdev_chi_2, pdf_params%stdev_chi_2(1,:), & ! intent(in)
    2139           0 :                             stats_zt ) ! intent(inout)
    2140           0 :       call stat_update_var( stats_metadata%istdev_eta_1, pdf_params%stdev_eta_1(1,:), & ! intent(in)
    2141           0 :                             stats_zt ) ! intent(inout)
    2142           0 :       call stat_update_var( stats_metadata%istdev_eta_2, pdf_params%stdev_eta_2(1,:), & ! intent(in)
    2143           0 :                             stats_zt ) ! intent(inout)
    2144           0 :       call stat_update_var( stats_metadata%icovar_chi_eta_1, pdf_params%covar_chi_eta_1(1,:), & ! intent(in)
    2145           0 :                             stats_zt ) ! intent(inout)
    2146           0 :       call stat_update_var( stats_metadata%icovar_chi_eta_2, pdf_params%covar_chi_eta_2(1,:), & ! intent(in)
    2147           0 :                             stats_zt ) ! intent(inout)
    2148           0 :       call stat_update_var( stats_metadata%icorr_w_chi_1, pdf_params%corr_w_chi_1(1,:), & ! intent(in)
    2149           0 :                             stats_zt ) ! intent(inout)
    2150           0 :       call stat_update_var( stats_metadata%icorr_w_chi_2, pdf_params%corr_w_chi_2(1,:), & ! intent(in)
    2151           0 :                             stats_zt ) ! intent(inout)
    2152           0 :       call stat_update_var( stats_metadata%icorr_w_eta_1, pdf_params%corr_w_eta_1(1,:), & ! intent(in)
    2153           0 :                             stats_zt ) ! intent(inout)
    2154           0 :       call stat_update_var( stats_metadata%icorr_w_eta_2, pdf_params%corr_w_eta_2(1,:), & ! intent(in)
    2155           0 :                             stats_zt ) ! intent(inout)
    2156           0 :       call stat_update_var( stats_metadata%icorr_chi_eta_1, pdf_params%corr_chi_eta_1(1,:), & ! intent(in)
    2157           0 :                             stats_zt ) ! intent(inout)
    2158           0 :       call stat_update_var( stats_metadata%icorr_chi_eta_2, pdf_params%corr_chi_eta_2(1,:), & ! intent(in)
    2159           0 :                             stats_zt ) ! intent(inout)
    2160           0 :       call stat_update_var( stats_metadata%icorr_w_rt_1, pdf_params%corr_w_rt_1(1,:), & ! intent(in)
    2161           0 :                             stats_zt ) ! intent(inout)
    2162           0 :       call stat_update_var( stats_metadata%icorr_w_rt_2, pdf_params%corr_w_rt_2(1,:), & ! intent(in)
    2163           0 :                             stats_zt ) ! intent(inout)
    2164           0 :       call stat_update_var( stats_metadata%icorr_w_thl_1, pdf_params%corr_w_thl_1(1,:), & ! intent(in)
    2165           0 :                             stats_zt ) ! intent(inout)
    2166           0 :       call stat_update_var( stats_metadata%icorr_w_thl_2, pdf_params%corr_w_thl_2(1,:), & ! intent(in)
    2167           0 :                             stats_zt ) ! intent(inout)
    2168           0 :       call stat_update_var( stats_metadata%icorr_rt_thl_1, pdf_params%corr_rt_thl_1(1,:), & ! intent(in)
    2169           0 :                             stats_zt ) ! intent(inout)
    2170           0 :       call stat_update_var( stats_metadata%icorr_rt_thl_2, pdf_params%corr_rt_thl_2(1,:), & ! intent(in)
    2171           0 :                             stats_zt ) ! intent(inout)
    2172           0 :       call stat_update_var( stats_metadata%icrt_1, pdf_params%crt_1(1,:), & ! intent(in)
    2173           0 :                             stats_zt ) ! intent(inout)
    2174           0 :       call stat_update_var( stats_metadata%icrt_2, pdf_params%crt_2(1,:), & ! intent(in)
    2175           0 :                             stats_zt ) ! intent(inout)
    2176           0 :       call stat_update_var( stats_metadata%icthl_1, pdf_params%cthl_1(1,:), & ! intent(in)
    2177           0 :                             stats_zt ) ! intent(inout)
    2178           0 :       call stat_update_var( stats_metadata%icthl_2, pdf_params%cthl_2(1,:), & ! intent(in)
    2179           0 :                             stats_zt ) ! intent(inout)
    2180             :       call stat_update_var( stats_metadata%iwp2_zt, wp2_zt, & ! intent(in)
    2181           0 :                             stats_zt ) ! intent(inout)
    2182             :       call stat_update_var( stats_metadata%ithlp2_zt, thlp2_zt, & ! intent(in)
    2183           0 :                             stats_zt ) ! intent(inout)
    2184             :       call stat_update_var( stats_metadata%ithlp3, thlp3, & ! intent(in)
    2185           0 :                             stats_zt ) ! intent(inout)
    2186             :       call stat_update_var( stats_metadata%iwpthlp_zt, wpthlp_zt, & ! intent(in)
    2187           0 :                             stats_zt ) ! intent(inout)
    2188             :       call stat_update_var( stats_metadata%iwprtp_zt, wprtp_zt, & ! intent(in)
    2189           0 :                             stats_zt ) ! intent(inout)
    2190             :       call stat_update_var( stats_metadata%irtp2_zt, rtp2_zt, & ! intent(in)
    2191           0 :                             stats_zt ) ! intent(inout)
    2192             :       call stat_update_var( stats_metadata%irtp3, rtp3, & ! intent(in)
    2193           0 :                             stats_zt ) ! intent(inout)
    2194             :       call stat_update_var( stats_metadata%irtpthlp_zt, rtpthlp_zt, & ! intent(in)
    2195           0 :                             stats_zt ) ! intent(inout)
    2196             :       call stat_update_var( stats_metadata%iup2_zt, up2_zt, & ! intent(in)
    2197           0 :                             stats_zt ) ! intent(inout)
    2198             :       call stat_update_var( stats_metadata%ivp2_zt, vp2_zt, & ! intent(in)
    2199           0 :                             stats_zt ) ! intent(inout)
    2200             :       call stat_update_var( stats_metadata%iupwp_zt, upwp_zt, & ! intent(in)
    2201           0 :                             stats_zt ) ! intent(inout)
    2202             :       call stat_update_var( stats_metadata%ivpwp_zt, vpwp_zt, & ! intent(in)
    2203           0 :                             stats_zt ) ! intent(inout)
    2204             :       call stat_update_var( stats_metadata%ia3_coef_zt, a3_coef_zt, & ! intent(in)
    2205           0 :                             stats_zt ) ! intent(inout)
    2206             :       call stat_update_var( stats_metadata%iwp3_on_wp2_zt, wp3_on_wp2_zt, & ! intent(in)
    2207           0 :                             stats_zt ) ! intent(inout)
    2208             : 
    2209           0 :       if ( stats_metadata%ichi > 0 ) then
    2210             :         ! Determine 's' from Mellor (1977) (extended liquid water)
    2211           0 :         chi(:) = pdf_params%mixt_frac(1,:) * pdf_params%chi_1(1,:) &
    2212           0 :                     + (1.0_core_rknd-pdf_params%mixt_frac(1,:)) * pdf_params%chi_2(1,:)
    2213             :         call stat_update_var( stats_metadata%ichi, chi, & ! intent(in)
    2214           0 :                              stats_zt ) ! intent(inout)
    2215             :       end if 
    2216             : 
    2217             :       ! Calculate variance of chi
    2218           0 :       if ( stats_metadata%ichip2 > 0 ) then
    2219           0 :         chip2 = compute_variance_binormal( chi, pdf_params%chi_1(1,:), pdf_params%chi_2(1,:), &
    2220           0 :                                          pdf_params%stdev_chi_1(1,:), pdf_params%stdev_chi_2(1,:), &
    2221           0 :                                          pdf_params%mixt_frac(1,:) )
    2222             :         call stat_update_var( stats_metadata%ichip2, chip2, & ! intent(in)
    2223           0 :                               stats_zt ) ! intent(inout)
    2224             :       end if
    2225             : 
    2226           0 :       if ( sclr_dim > 0 ) then
    2227           0 :         do isclr=1, sclr_dim
    2228           0 :           call stat_update_var( stats_metadata%isclrm(isclr), sclrm(:,isclr), & ! intent(in)
    2229           0 :                                 stats_zt ) ! intent(inout)
    2230           0 :           call stat_update_var( stats_metadata%isclrm_f(isclr), sclrm_forcing(:,isclr),  & ! intent(in)
    2231           0 :                                 stats_zt ) ! intent(inout)
    2232             :         end do
    2233             :       end if
    2234             : 
    2235           0 :       if ( edsclr_dim > 0 ) then
    2236           0 :         do isclr = 1, edsclr_dim
    2237           0 :           call stat_update_var( stats_metadata%iedsclrm(isclr), edsclrm(:,isclr), & ! intent(in)
    2238           0 :                                 stats_zt ) ! intent(inout)
    2239           0 :           call stat_update_var( stats_metadata%iedsclrm_f(isclr), edsclrm_forcing(:,isclr), & ! intent(in)
    2240           0 :                                 stats_zt ) ! intent(inout)
    2241             :         end do
    2242             :       end if
    2243             : 
    2244             :       ! Calculate rcm in cloud
    2245           0 :       if ( stats_metadata%ircm_in_cloud > 0 ) then
    2246           0 :         where ( cloud_frac(:) > cloud_frac_min )
    2247             :             rcm_in_cloud(:) = rcm / cloud_frac
    2248             :         elsewhere
    2249             :             rcm_in_cloud(:) = rcm
    2250             :         endwhere
    2251             : 
    2252             :         call stat_update_var( stats_metadata%ircm_in_cloud, rcm_in_cloud, & ! intent(in)
    2253           0 :                               stats_zt ) ! intent(inout)
    2254             :       end if
    2255             : 
    2256             :       ! stats_zm variables
    2257             : 
    2258             :       call stat_update_var( stats_metadata%iwp2, wp2, & ! intent(in)
    2259           0 :                             stats_zm ) ! intent(inout)
    2260             :       call stat_update_var( stats_metadata%iwp3_zm, wp3_zm, & ! intent(in)
    2261           0 :                             stats_zm ) ! intent(inout)
    2262             :       call stat_update_var( stats_metadata%irtp2, rtp2, & ! intent(in)
    2263           0 :                             stats_zm ) ! intent(inout)
    2264             :       call stat_update_var( stats_metadata%ithlp2, thlp2, & ! intent(in)
    2265           0 :                             stats_zm ) ! intent(inout) 
    2266             :       call stat_update_var( stats_metadata%irtpthlp, rtpthlp, & ! intent(in)
    2267           0 :                             stats_zm ) ! intent(inout)
    2268             :       call stat_update_var( stats_metadata%iwprtp, wprtp, & ! intent(in)
    2269           0 :                             stats_zm ) ! intent(inout)
    2270             :       call stat_update_var( stats_metadata%iwpthlp, wpthlp, & ! intent(in)
    2271           0 :                             stats_zm ) ! intent(inout)
    2272             :       call stat_update_var( stats_metadata%iwp2up2, wp2up2, & ! intent(in)
    2273           0 :                             stats_zm ) ! intent(inout)
    2274             :       call stat_update_var( stats_metadata%iwp2vp2, wp2vp2, &  ! intent(in)
    2275           0 :                             stats_zm ) ! intent(inout)
    2276             :       call stat_update_var( stats_metadata%iwp4, wp4, & ! intent(in)
    2277           0 :                             stats_zm ) ! intent(inout)
    2278             :       call stat_update_var( stats_metadata%iwpthvp, wpthvp, & ! intent(in)
    2279           0 :                             stats_zm ) ! intent(inout)
    2280             :       call stat_update_var( stats_metadata%irtpthvp, rtpthvp, & ! intent(in)
    2281           0 :                             stats_zm ) ! intent(inout)
    2282             :       call stat_update_var( stats_metadata%ithlpthvp, thlpthvp, & ! intent(in)
    2283           0 :                             stats_zm ) ! intent(inout)
    2284             :       call stat_update_var( stats_metadata%itau_zm, tau_zm, & ! intent(in)
    2285           0 :                             stats_zm ) ! intent(inout)
    2286             :       call stat_update_var( stats_metadata%iKh_zm, Kh_zm, & ! intent(in)
    2287           0 :                             stats_zm ) ! intent(inout)
    2288             :       call stat_update_var( stats_metadata%iwprcp, wprcp, & ! intent(in)
    2289           0 :                             stats_zm ) ! intent(inout)
    2290             :       call stat_update_var( stats_metadata%irc_coef_zm, rc_coef_zm, & ! intent(in)
    2291           0 :                             stats_zm ) ! intent(inout)
    2292             :       call stat_update_var( stats_metadata%ithlprcp, thlprcp, & ! intent(in)
    2293           0 :                             stats_zm ) ! intent(inout)
    2294             :       call stat_update_var( stats_metadata%irtprcp, rtprcp, & ! intent(in)
    2295           0 :                             stats_zm ) ! intent(inout)
    2296             :       call stat_update_var( stats_metadata%ircp2, rcp2, & ! intent(in)
    2297           0 :                             stats_zm ) ! intent(inout)
    2298             :       call stat_update_var( stats_metadata%iupwp, upwp, & ! intent(in)
    2299           0 :                             stats_zm ) ! intent(inout)
    2300             :       call stat_update_var( stats_metadata%ivpwp, vpwp, & ! intent(in)
    2301           0 :                             stats_zm ) ! intent(inout)
    2302             :       call stat_update_var( stats_metadata%ivp2, vp2, & ! intent(in)
    2303           0 :                             stats_zm ) ! intent(inout)
    2304             :       call stat_update_var( stats_metadata%iup2, up2, & ! intent(in)
    2305           0 :                             stats_zm ) ! intent(inout)
    2306             :       call stat_update_var( stats_metadata%irho_zm, rho_zm, & ! intent(in)
    2307           0 :                             stats_zm ) ! intent(inout) 
    2308             :       call stat_update_var( stats_metadata%isigma_sqd_w, sigma_sqd_w, & ! intent(in)
    2309           0 :                             stats_zm ) ! intent(inout)
    2310             :       call stat_update_var( stats_metadata%irho_ds_zm, rho_ds_zm, & ! intent(in)
    2311           0 :                             stats_zm ) ! intent(inout)
    2312             :       call stat_update_var( stats_metadata%ithv_ds_zm, thv_ds_zm, & ! intent(in)
    2313           0 :                             stats_zm ) ! intent(inout)
    2314             :       call stat_update_var( stats_metadata%iem, em, & ! intent(in)
    2315           0 :                             stats_zm ) ! intent(inout)
    2316             :       call stat_update_var( stats_metadata%iSkw_velocity, Skw_velocity, & ! intent(in)
    2317           0 :                             stats_zm ) ! intent(inout)
    2318             :       call stat_update_var( stats_metadata%ia3_coef, a3_coef, & ! intent(in)
    2319           0 :                             stats_zm ) ! intent(inout)
    2320             :       call stat_update_var( stats_metadata%iwp3_on_wp2, wp3_on_wp2, & ! intent(in)
    2321           0 :                             stats_zm ) ! intent(inout)
    2322             :       call stat_update_var( stats_metadata%iwp3_on_wp2_cfl_num, wp3_on_wp2 * dt / dzm, & ! intent(in)
    2323           0 :                             stats_zm ) ! intent(inout)
    2324             : 
    2325             :       call stat_update_var( stats_metadata%icloud_frac_zm, cloud_frac_zm, & ! intent(in)
    2326           0 :                             stats_zm ) ! intent(inout)
    2327             :       call stat_update_var( stats_metadata%iice_supersat_frac_zm, ice_supersat_frac_zm, & ! intent(in)
    2328           0 :                             stats_zm ) ! intent(inout)
    2329             :       call stat_update_var( stats_metadata%ircm_zm, rcm_zm, & ! intent(in)
    2330           0 :                             stats_zm ) ! intent(inout)
    2331             :       call stat_update_var( stats_metadata%irtm_zm, rtm_zm, & ! intent(in)
    2332           0 :                             stats_zm ) ! intent(inout)
    2333             :       call stat_update_var( stats_metadata%ithlm_zm, thlm_zm, & ! intent(in)
    2334           0 :                             stats_zm ) ! intent(inout)
    2335           0 :       call stat_update_var( stats_metadata%iw_1_zm, pdf_params_zm%w_1(1,:), & ! intent(in)
    2336           0 :                             stats_zm ) ! intent(inout)
    2337           0 :       call stat_update_var( stats_metadata%iw_2_zm, pdf_params_zm%w_2(1,:), & ! intent(in)
    2338           0 :                             stats_zm ) ! intent(inout)
    2339           0 :       call stat_update_var( stats_metadata%ivarnce_w_1_zm, pdf_params_zm%varnce_w_1(1,:), & ! intent(in)
    2340           0 :                             stats_zm ) ! intent(inout)
    2341           0 :       call stat_update_var( stats_metadata%ivarnce_w_2_zm, pdf_params_zm%varnce_w_2(1,:), & ! intent(in)
    2342           0 :                             stats_zm ) ! intent(inout)
    2343           0 :       call stat_update_var( stats_metadata%imixt_frac_zm, pdf_params_zm%mixt_frac(1,:), & ! intent(in)
    2344           0 :                             stats_zm ) ! intent(inout)
    2345             : 
    2346           0 :       if ( sclr_dim > 0 ) then
    2347           0 :         do isclr=1, sclr_dim
    2348           0 :           call stat_update_var( stats_metadata%isclrp2(isclr), sclrp2(:,isclr), & ! intent(in)
    2349           0 :                                 stats_zm ) ! intent(inout)
    2350           0 :           call stat_update_var( stats_metadata%isclrprtp(isclr), sclrprtp(:,isclr), & ! intent(in)
    2351           0 :                                 stats_zm ) ! intent(inout)
    2352           0 :           call stat_update_var( stats_metadata%isclrpthvp(isclr), sclrpthvp(:,isclr), & ! intent(in)
    2353           0 :                                 stats_zm ) ! intent(inout)
    2354           0 :           call stat_update_var( stats_metadata%isclrpthlp(isclr), sclrpthlp(:,isclr), & ! intent(in)
    2355           0 :                                  stats_zm ) ! intent(inout)
    2356           0 :           call stat_update_var( stats_metadata%isclrprcp(isclr), sclrprcp(:,isclr), & ! intent(in)
    2357           0 :                                 stats_zm ) ! intent(inout)
    2358           0 :           call stat_update_var( stats_metadata%iwpsclrp(isclr), wpsclrp(:,isclr), & ! intent(in)
    2359           0 :                                stats_zm ) ! intent(inout)
    2360           0 :           call stat_update_var( stats_metadata%iwp2sclrp(isclr), wp2sclrp(:,isclr), & ! intent(in)
    2361           0 :                                 stats_zm ) ! intent(inout)
    2362           0 :           call stat_update_var( stats_metadata%iwpsclrp2(isclr), wpsclrp2(:,isclr), & ! intent(in)
    2363           0 :                                 stats_zm ) ! intent(inout)
    2364           0 :           call stat_update_var( stats_metadata%iwpsclrprtp(isclr), wpsclrprtp(:,isclr), & ! intent(in)
    2365           0 :                                 stats_zm ) ! intent(inout)
    2366           0 :           call stat_update_var( stats_metadata%iwpsclrpthlp(isclr), wpsclrpthlp(:,isclr), & ! intent(in)
    2367           0 :                                 stats_zm ) ! intent(inout)
    2368             :         end do
    2369             :       end if
    2370           0 :       if ( edsclr_dim > 0 ) then
    2371           0 :         do isclr = 1, edsclr_dim
    2372           0 :           call stat_update_var( stats_metadata%iwpedsclrp(isclr), wpedsclrp(:,isclr), & ! intent(in)
    2373           0 :                                 stats_zm ) ! intent(inout)
    2374             :         end do
    2375             :       end if
    2376             : 
    2377             :       ! Calculate shear production
    2378           0 :       if ( stats_metadata%ishear > 0 ) then
    2379           0 :         do k = 1, nz-1, 1
    2380           0 :           shear(k) = - upwp(k) * ( um(k+1) - um(k) ) * invrs_dzm(k)  &
    2381           0 :                      - vpwp(k) * ( vm(k+1) - vm(k) ) * invrs_dzm(k)
    2382             :         enddo
    2383           0 :         shear(nz) = 0.0_core_rknd
    2384             :       end if
    2385             :       call stat_update_var( stats_metadata%ishear, shear, & ! intent(in)
    2386           0 :                             stats_zm ) ! intent(inout)
    2387             : 
    2388             :       ! stats_sfc variables
    2389             : 
    2390             :       ! Cloud cover
    2391           0 :       call stat_update_var_pt( stats_metadata%icc, grid_level, maxval( cloud_frac(1:nz) ), & ! intent(in)
    2392           0 :                                stats_sfc ) ! intent(inout)
    2393             : 
    2394             :       ! Cloud base
    2395           0 :       if ( stats_metadata%iz_cloud_base > 0 ) then
    2396             : 
    2397             :         k = 1
    2398           0 :         do while ( rcm(k) < rc_tol .and. k < nz )
    2399           0 :           k = k + 1
    2400             :         enddo
    2401             : 
    2402           0 :         if ( k > 1 .and. k < nz) then
    2403             : 
    2404             :           ! Use linear interpolation to find the exact height of the
    2405             :           ! rc_tol kg/kg level.  Brian.
    2406             :           call stat_update_var_pt( stats_metadata%iz_cloud_base, grid_level, & ! intent(in)
    2407             :                                    lin_interpolate_two_points( rc_tol, rcm(k), & ! intent(in)
    2408           0 :                                    rcm(k-1), zt(k), zt(k-1) ), & ! intent(in)
    2409           0 :                                    stats_sfc ) ! intent(inout)
    2410             : 
    2411             :         else
    2412             : 
    2413             :           ! Set the cloud base output to -10m, if it's clear. 
    2414             :           ! Known magic number
    2415             :           call stat_update_var_pt( stats_metadata%iz_cloud_base, grid_level, -10.0_core_rknd , & ! intent(in)
    2416           0 :                                    stats_sfc ) ! intent(inout)
    2417             :  
    2418             :         end if ! k > 1 and k < nz
    2419             : 
    2420             :       end if ! stats_metadata%iz_cloud_base > 0
    2421             : 
    2422             :       ! Liquid Water Path
    2423           0 :       if ( stats_metadata%ilwp > 0 ) then
    2424             : 
    2425             :         xtmp &
    2426             :         = vertical_integral &
    2427           0 :                ( (nz - 2 + 1), rho_ds_zt(2:nz), &
    2428           0 :                  rcm(2:nz), dzt(2:nz) )
    2429             : 
    2430             :         call stat_update_var_pt( stats_metadata%ilwp, grid_level, xtmp, & ! intent(in)
    2431           0 :                                  stats_sfc ) ! intent(inout)
    2432             : 
    2433             :       end if
    2434             : 
    2435             :       ! Vapor Water Path (Precipitable Water)
    2436           0 :       if ( stats_metadata%ivwp > 0 ) then
    2437             : 
    2438             :         xtmp &
    2439             :         = vertical_integral &
    2440           0 :                ( (nz - 2 + 1), rho_ds_zt(2:nz), &
    2441           0 :                  ( rtm(2:nz) - rcm(2:nz) ), dzt(2:nz) )
    2442             : 
    2443             :         call stat_update_var_pt( stats_metadata%ivwp, grid_level, xtmp, & ! intent(in)
    2444           0 :                                  stats_sfc ) ! intent(inout)
    2445             : 
    2446             :       end if
    2447             : 
    2448             : 
    2449             :       ! Vertical average of thermodynamic level variables.
    2450             : 
    2451             :       ! Find the vertical average of thermodynamic level variables, averaged from
    2452             :       ! level 2 (the first thermodynamic level above model surface) through
    2453             :       ! level nz (the top of the model).  Use the vertical averaging function
    2454             :       ! found in advance_helper_module.F90.
    2455             : 
    2456             :       ! Vertical average of thlm.
    2457             :       call stat_update_var_pt( stats_metadata%ithlm_vert_avg, grid_level,  & ! intent(in)
    2458           0 :            vertical_avg( (nz-2+1), rho_ds_zt(2:nz), & ! intent(in)
    2459             :                          thlm(2:nz), dzt(2:nz) ), & ! intent(in)
    2460           0 :                                stats_sfc ) ! intent(inout)
    2461             : 
    2462             :       ! Vertical average of rtm.
    2463             :       call stat_update_var_pt( stats_metadata%irtm_vert_avg, grid_level,  & ! intent(in)
    2464             :            vertical_avg( (nz-2+1), rho_ds_zt(2:nz), & ! intent(in)
    2465             :                          rtm(2:nz), dzt(2:nz) ), & ! intent(in)
    2466           0 :                                stats_sfc ) ! intent(inout)
    2467             : 
    2468             :       ! Vertical average of um.
    2469             :       call stat_update_var_pt( stats_metadata%ium_vert_avg, grid_level,  & ! intent(in)
    2470             :            vertical_avg( (nz-2+1), rho_ds_zt(2:nz), & ! intent(in)
    2471             :                          um(2:nz), dzt(2:nz) ), & ! intent(in)
    2472           0 :                                stats_sfc ) ! intent(inout)
    2473             : 
    2474             :       ! Vertical average of vm.
    2475             :       call stat_update_var_pt( stats_metadata%ivm_vert_avg, grid_level,  & ! intent(in)
    2476             :            vertical_avg( (nz-2+1), rho_ds_zt(2:nz), & ! intent(in)
    2477             :                          vm(2:nz), dzt(2:nz) ), & ! intent(in)
    2478           0 :                                stats_sfc ) ! intent(inout)
    2479             : 
    2480             :       ! Vertical average of momentum level variables.
    2481             : 
    2482             :       ! Find the vertical average of momentum level variables, averaged over the
    2483             :       ! entire vertical profile (level 1 through level nz).  Use the vertical
    2484             :       ! averaging function found in advance_helper_module.F90.
    2485             : 
    2486             :       ! Vertical average of wp2.
    2487             :       call stat_update_var_pt( stats_metadata%iwp2_vert_avg, grid_level,  & ! intent(in)
    2488             :            vertical_avg( (nz-1+1), rho_ds_zm(1:nz), & ! intent(in)
    2489             :                          wp2(1:nz), dzm(1:nz) ), & ! intent(in)
    2490           0 :                                stats_sfc ) ! intent(inout)
    2491             : 
    2492             :       ! Vertical average of up2.
    2493             :       call stat_update_var_pt( stats_metadata%iup2_vert_avg, grid_level,  & ! intent(in)
    2494             :            vertical_avg( (nz-1+1), rho_ds_zm(1:nz), & ! intent(in)
    2495             :                          up2(1:nz), dzm(1:nz) ), & ! intent(in)
    2496           0 :                                stats_sfc ) ! intent(inout)
    2497             : 
    2498             :       ! Vertical average of vp2.
    2499             :       call stat_update_var_pt( stats_metadata%ivp2_vert_avg, grid_level,  & ! intent(in)
    2500             :            vertical_avg( (nz-1+1), rho_ds_zm(1:nz), & ! intent(in)
    2501             :                          vp2(1:nz), dzm(1:nz) ), & ! intent(in)
    2502           0 :                                stats_sfc ) ! intent(inout)
    2503             : 
    2504             :       ! Vertical average of rtp2.
    2505             :       call stat_update_var_pt( stats_metadata%irtp2_vert_avg, grid_level,  & ! intent(in)
    2506             :            vertical_avg( (nz-1+1), rho_ds_zm(1:nz), & ! intent(in)
    2507             :                          rtp2(1:nz), dzm(1:nz) ), & ! intent(in)
    2508           0 :                                stats_sfc ) ! intent(inout)
    2509             : 
    2510             :       ! Vertical average of thlp2.
    2511             :       call stat_update_var_pt( stats_metadata%ithlp2_vert_avg, grid_level,  & ! intent(in)
    2512             :            vertical_avg( (nz-1+1), rho_ds_zm(1:nz), & ! intent(in)
    2513             :                          thlp2(1:nz), dzm(1:nz) ), & ! intent(in)
    2514           0 :                                stats_sfc ) ! intent(inout)
    2515             :       
    2516             :       
    2517           0 :       if (stats_metadata%itot_vartn_normlzd_rtm > 0) then
    2518           0 :         if (abs(rtm(nz) - rtm(1)) < eps) then
    2519           0 :           write(fstderr, *) "Warning: tot_vartn_normlzd_rtm tried to divide by zero denominator ", &
    2520           0 :                             "(surface level value was equal to top level value)"
    2521           0 :           xtmp = -999_core_rknd  ! workaround to signify zero denominator 
    2522             :         else
    2523           0 :           xtmp = sum(abs(rtm(2 : nz) - rtm(1 : nz-1)) / abs(rtm(nz) - rtm(1)))
    2524             :         end if
    2525             :         
    2526             :         call stat_update_var_pt( stats_metadata%itot_vartn_normlzd_rtm, grid_level, xtmp, & ! intent(in)
    2527           0 :                                  stats_sfc ) ! intent(inout)
    2528             :       end if
    2529             :      
    2530           0 :       if (stats_metadata%itot_vartn_normlzd_thlm > 0) then
    2531           0 :         if (abs(thlm(nz) - thlm(1)) < eps) then
    2532           0 :           write(fstderr, *) "Warning: tot_vartn_normlzd_thlm tried to divide by zero denominator ", &
    2533           0 :                             "(surface level value was equal to top level value)"
    2534           0 :           xtmp = -999_core_rknd  ! workaround to signify zero denominator 
    2535             :         else
    2536           0 :           xtmp = sum(abs(thlm(2 : nz) - thlm(1 : nz-1)) / abs(thlm(nz) - thlm(1)))
    2537             :         end if
    2538             :         
    2539             :         call stat_update_var_pt( stats_metadata%itot_vartn_normlzd_thlm, grid_level, xtmp, & ! intent(in)
    2540           0 :                                  stats_sfc ) ! intent(inout)
    2541             :       end if
    2542             :      
    2543           0 :       if (stats_metadata%itot_vartn_normlzd_wprtp > 0) then
    2544           0 :         if (abs(wprtp(nz) - wprtp(1)) < eps) then
    2545           0 :           write(fstderr, *) "Warning: tot_vartn_normlzd_wprtp tried to divide by zero denominator ", &
    2546           0 :                             "(surface level value was equal to top level value)"
    2547           0 :           xtmp = -999_core_rknd  ! workaround to signify zero denominator 
    2548             :         else
    2549           0 :           xtmp = sum(abs(wprtp(2 : nz) - wprtp(1 : nz-1)) / abs(wprtp(nz) - wprtp(1)))
    2550             :         end if
    2551             :         
    2552             :         call stat_update_var_pt( stats_metadata%itot_vartn_normlzd_wprtp, grid_level, xtmp, & ! intent(in)
    2553           0 :                                  stats_sfc ) ! intent(inout)
    2554             :       end if
    2555             :     end if ! stats_metadata%l_stats_samp
    2556             : 
    2557             : 
    2558           0 :     return
    2559             :   end subroutine stats_accumulate
    2560             : !------------------------------------------------------------------------------
    2561           0 :   subroutine stats_accumulate_hydromet( gr, hydromet, rho_ds_zt, & ! intent(in)
    2562             :                                         stats_metadata,          & ! intent(in)
    2563             :                                         stats_zt, stats_sfc )      ! intent(inout)
    2564             : ! Description:
    2565             : !   Compute stats related the hydrometeors
    2566             : 
    2567             : ! References:
    2568             : !   None
    2569             : !------------------------------------------------------------------------------
    2570             :     use parameters_model, only: &
    2571             :         hydromet_dim ! Variable(s)
    2572             : 
    2573             :     use grid_class, only: &
    2574             :         grid ! Type
    2575             :         
    2576             :     use array_index, only:  & 
    2577             :         iirr, iirs, iiri, iirg, & ! Variable(s)
    2578             :         iiNr, iiNs, iiNi, iiNg
    2579             : 
    2580             :     use advance_helper_module, only: &
    2581             :         vertical_integral ! Procedure(s)
    2582             : 
    2583             :     use stats_type_utilities, only: & 
    2584             :         stat_update_var, & ! Procedure(s)
    2585             :         stat_update_var_pt
    2586             : 
    2587             :     use clubb_precision, only: &
    2588             :         core_rknd ! Variable(s)
    2589             : 
    2590             :     use stats_type, only: &
    2591             :         stats ! Type
    2592             : 
    2593             :     use stats_variables, only: & 
    2594             :         stats_metadata_type
    2595             : 
    2596             :     implicit none
    2597             : 
    2598             :     type (grid), target, intent(in) :: gr
    2599             : 
    2600             :     type (stats_metadata_type), intent(in) :: &
    2601             :       stats_metadata
    2602             : 
    2603             :     type (stats), target, intent(inout) :: &
    2604             :       stats_zt, &
    2605             :       stats_sfc
    2606             : 
    2607             :     ! Input Variables
    2608             :     real( kind = core_rknd ), dimension(gr%nz,hydromet_dim), intent(in) :: &
    2609             :       hydromet ! All hydrometeors except for rcm        [units vary]
    2610             : 
    2611             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
    2612             :       rho_ds_zt ! Dry, static density (thermo. levs.)      [kg/m^3]
    2613             : 
    2614             :     ! Local Variables
    2615             :     real(kind=core_rknd) :: xtmp
    2616             :     
    2617             :     integer :: grid_level = 1
    2618             : 
    2619             :     ! ---- Begin Code ----
    2620             : 
    2621           0 :     if ( stats_metadata%l_stats_samp ) then
    2622             : 
    2623           0 :       if ( iirr > 0 ) then
    2624             :         call stat_update_var( stats_metadata%irrm, hydromet(:,iirr), & ! intent(in)
    2625           0 :                               stats_zt ) ! intent(inout)
    2626             :       end if
    2627             : 
    2628           0 :       if ( iirs > 0 ) then
    2629             :         call stat_update_var( stats_metadata%irsm, hydromet(:,iirs), & ! intent(in)
    2630           0 :                               stats_zt ) ! intent(inout)
    2631             :       end if 
    2632             : 
    2633           0 :       if ( iiri > 0 ) then 
    2634             :         call stat_update_var( stats_metadata%irim, hydromet(:,iiri), & ! intent(in)
    2635           0 :                               stats_zt ) ! intent(inout)
    2636             :       end if
    2637             : 
    2638           0 :       if ( iirg > 0 ) then
    2639             :         call stat_update_var( stats_metadata%irgm,  &  ! intent(in)
    2640             :                               hydromet(:,iirg), & ! intent(in)
    2641           0 :                               stats_zt ) ! intent(inout)
    2642             :       end if
    2643             : 
    2644           0 :       if ( iiNi > 0 ) then
    2645             :         call stat_update_var( stats_metadata%iNim, hydromet(:,iiNi), & ! intent(in)
    2646           0 :                               stats_zt ) ! intent(inout)
    2647             :       end if
    2648             : 
    2649           0 :       if ( iiNr > 0 ) then
    2650             :         call stat_update_var( stats_metadata%iNrm, hydromet(:,iiNr), & ! intent(in)
    2651           0 :                               stats_zt ) ! intent(inout)
    2652             :       end if
    2653             : 
    2654           0 :       if ( iiNs > 0 ) then
    2655             :         call stat_update_var( stats_metadata%iNsm, hydromet(:,iiNs), & ! intent(in)
    2656           0 :                               stats_zt ) ! intent(inout)
    2657             :       end if
    2658             : 
    2659           0 :       if ( iiNg > 0 ) then
    2660             :         call stat_update_var( stats_metadata%iNgm, hydromet(:,iiNg), & ! intent(in)
    2661           0 :                               stats_zt ) ! intent(inout)
    2662             :       end if
    2663             : 
    2664             :       ! Snow Water Path
    2665           0 :       if ( stats_metadata%iswp > 0 .and. iirs > 0 ) then
    2666             : 
    2667             :         ! Calculate snow water path
    2668             :         xtmp &
    2669             :         = vertical_integral &
    2670           0 :                ( (gr%nz - 2 + 1), rho_ds_zt(2:gr%nz), &
    2671           0 :                  hydromet(2:gr%nz,iirs), gr%dzt(1,2:gr%nz) )
    2672             : 
    2673             :         call stat_update_var_pt( stats_metadata%iswp, grid_level, xtmp, & ! intent(in)
    2674           0 :                                  stats_sfc ) ! intent(inout)
    2675             : 
    2676             :       end if ! stats_metadata%iswp > 0 .and. iirs > 0
    2677             : 
    2678             :       ! Ice Water Path
    2679           0 :       if ( stats_metadata%iiwp > 0 .and. iiri > 0 ) then
    2680             : 
    2681             :         xtmp &
    2682             :         = vertical_integral &
    2683           0 :                ( (gr%nz - 2 + 1), rho_ds_zt(2:gr%nz), &
    2684           0 :                  hydromet(2:gr%nz,iiri), gr%dzt(1,2:gr%nz) )
    2685             : 
    2686             :         call stat_update_var_pt( stats_metadata%iiwp, grid_level, xtmp, & ! intent(in)
    2687           0 :                                  stats_sfc ) ! intent(inout)
    2688             : 
    2689             :       end if
    2690             : 
    2691             :       ! Rain Water Path
    2692           0 :       if ( stats_metadata%irwp > 0 .and. iirr > 0 ) then
    2693             : 
    2694             :         xtmp &
    2695             :         = vertical_integral &
    2696           0 :                ( (gr%nz - 2 + 1), rho_ds_zt(2:gr%nz), &
    2697           0 :                  hydromet(2:gr%nz,iirr), gr%dzt(1,2:gr%nz) )
    2698             : 
    2699             :         call stat_update_var_pt( stats_metadata%irwp, grid_level, xtmp, & ! intent(in)
    2700           0 :                                  stats_sfc ) ! intent(inout)
    2701             :  
    2702             :       end if ! stats_metadata%irwp > 0 .and. stats_metadata%irrm > 0
    2703             :     end if ! stats_metadata%l_stats_samp
    2704             : 
    2705           0 :     return
    2706             :   end subroutine stats_accumulate_hydromet
    2707             : !------------------------------------------------------------------------------
    2708           0 :   subroutine stats_accumulate_lh_tend( gr, lh_hydromet_mc, lh_Ncm_mc, &
    2709           0 :                                        lh_thlm_mc, lh_rvm_mc, lh_rcm_mc, &
    2710           0 :                                        lh_AKm, AKm, AKstd, AKstd_cld, &
    2711           0 :                                        lh_rcm_avg, AKm_rcm, AKm_rcc, &
    2712             :                                        stats_metadata, &
    2713             :                                        stats_lh_zt )
    2714             : 
    2715             : ! Description:
    2716             : !   Compute stats for the tendency of latin hypercube sample points.
    2717             : 
    2718             : ! References:
    2719             : !   None
    2720             : !------------------------------------------------------------------------------
    2721             : 
    2722             :     use parameters_model, only: &
    2723             :         hydromet_dim ! Variable(s)
    2724             : 
    2725             :     use grid_class, only: &
    2726             :         grid ! Type
    2727             : 
    2728             :     use array_index, only:  & 
    2729             :         iirr, iirs, iiri, iirg, & ! Variable(s)
    2730             :         iiNr, iiNs, iiNi, iiNg
    2731             : 
    2732             : 
    2733             :     use stats_type_utilities, only: & 
    2734             :         stat_update_var ! Procedure(s)
    2735             : 
    2736             :     use stats_variables, only: &
    2737             :         stats_metadata_type
    2738             : 
    2739             :     use clubb_precision, only: &
    2740             :         core_rknd ! Variable(s)
    2741             : 
    2742             :     use stats_type, only: stats ! Type
    2743             : 
    2744             :     implicit none
    2745             : 
    2746             :     type (grid), target, intent(in) :: gr
    2747             : 
    2748             :     ! Input Variables
    2749             :     real( kind = core_rknd ), dimension(gr%nz,hydromet_dim), intent(in) :: &
    2750             :       lh_hydromet_mc ! Tendency of hydrometeors except for rvm/rcm  [units vary]
    2751             : 
    2752             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
    2753             :       lh_Ncm_mc,  & ! Tendency of cloud droplet concentration  [num/kg/s]
    2754             :       lh_thlm_mc, & ! Tendency of liquid potential temperature [kg/kg/s]
    2755             :       lh_rcm_mc,  & ! Tendency of cloud water                  [kg/kg/s]
    2756             :       lh_rvm_mc     ! Tendency of vapor                        [kg/kg/s]
    2757             : 
    2758             :     real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
    2759             :       lh_AKm,     & ! Kessler ac estimate                 [kg/kg/s]
    2760             :       AKm,        & ! Exact Kessler ac                    [kg/kg/s]
    2761             :       AKstd,      & ! St dev of exact Kessler ac          [kg/kg/s]
    2762             :       AKstd_cld,  & ! Stdev of exact w/in cloud ac        [kg/kg/s]
    2763             :       lh_rcm_avg, & ! Monte Carlo rcm estimate            [kg/kg]
    2764             :       AKm_rcm,    & ! Kessler ac based on rcm             [kg/kg/s]
    2765             :       AKm_rcc       ! Kessler ac based on rcm/cloud_frac  [kg/kg/s]
    2766             :       
    2767             :     type (stats_metadata_type), intent(in) :: &
    2768             :       stats_metadata
    2769             : 
    2770             :     type (stats), target, intent(inout) :: &
    2771             :       stats_lh_zt
    2772             : 
    2773           0 :     if ( stats_metadata%l_stats_samp ) then
    2774             : 
    2775             :       call stat_update_var( stats_metadata%ilh_thlm_mc, lh_thlm_mc, & ! intent(in)
    2776           0 :                             stats_lh_zt ) ! intent(inout)
    2777             :       call stat_update_var( stats_metadata%ilh_rcm_mc, lh_rcm_mc, & ! intent(in)
    2778           0 :                             stats_lh_zt ) ! intent(inout)
    2779             :       call stat_update_var( stats_metadata%ilh_rvm_mc, lh_rvm_mc, & ! intent(in)
    2780           0 :                             stats_lh_zt ) ! intent(inout)
    2781             : 
    2782             :       call stat_update_var( stats_metadata%ilh_Ncm_mc, lh_Ncm_mc, & ! intent(in)
    2783           0 :                             stats_lh_zt ) ! intent(inout)
    2784             : 
    2785           0 :       if ( iirr > 0 ) then
    2786             :         call stat_update_var( stats_metadata%ilh_rrm_mc, lh_hydromet_mc(:,iirr), & ! intent(in)
    2787           0 :                               stats_lh_zt ) ! intent(inout)
    2788             :       end if
    2789             : 
    2790           0 :       if ( iirs > 0 ) then
    2791             :         call stat_update_var( stats_metadata%ilh_rsm_mc, lh_hydromet_mc(:,iirs), & ! intent(in)
    2792           0 :                               stats_lh_zt ) ! intent(inout)
    2793             :       end if 
    2794             : 
    2795           0 :       if ( iiri > 0 ) then
    2796             :         call stat_update_var( stats_metadata%ilh_rim_mc, lh_hydromet_mc(:,iiri), & ! intent(in)
    2797           0 :                               stats_lh_zt ) ! intent(inout)
    2798             :       end if
    2799             : 
    2800           0 :       if ( iirg > 0 ) then
    2801             :         call stat_update_var( stats_metadata%ilh_rgm_mc, lh_hydromet_mc(:,iirg), & ! intent(in)
    2802           0 :                               stats_lh_zt ) ! intent(inout)
    2803             :       end if
    2804             : 
    2805           0 :       if ( iiNi > 0 ) then
    2806             :         call stat_update_var( stats_metadata%ilh_Nim_mc, lh_hydromet_mc(:,iiNi), & ! intent(in)
    2807           0 :                               stats_lh_zt ) ! intent(inout)
    2808             :       end if
    2809             : 
    2810           0 :       if ( iiNr > 0 ) then
    2811             :         call stat_update_var( stats_metadata%ilh_Nrm_mc, lh_hydromet_mc(:,iiNr), & ! intent(in)
    2812           0 :                               stats_lh_zt ) ! intent(inout)
    2813             :       end if
    2814             : 
    2815           0 :       if ( iiNs > 0 ) then
    2816             :         call stat_update_var( stats_metadata%ilh_Nsm_mc, lh_hydromet_mc(:,iiNs), & ! intent(in)
    2817           0 :                               stats_lh_zt ) ! intent(inout)
    2818             :       end if
    2819             : 
    2820           0 :       if ( iiNg > 0 ) then
    2821             :         call stat_update_var( stats_metadata%ilh_Ngm_mc, lh_hydromet_mc(:,iiNg), & ! intent(in)
    2822           0 :                               stats_lh_zt ) ! intent(inout)
    2823             :       end if 
    2824             : 
    2825             :       call stat_update_var( stats_metadata%iAKm, AKm, & ! intent(in)
    2826           0 :                             stats_lh_zt ) ! intent(inout)
    2827             :       call stat_update_var( stats_metadata%ilh_AKm, lh_AKm, & ! intent(in)
    2828           0 :                             stats_lh_zt) ! intent(inout)
    2829             :       call stat_update_var( stats_metadata%ilh_rcm_avg, lh_rcm_avg, & ! intent(in)
    2830           0 :                             stats_lh_zt ) ! intent(inout)
    2831             :       call stat_update_var( stats_metadata%iAKstd, AKstd, & ! intent(in)
    2832           0 :                             stats_lh_zt ) ! intent(inout)
    2833             :       call stat_update_var( stats_metadata%iAKstd_cld, AKstd_cld, & ! intent(in)
    2834           0 :                             stats_lh_zt ) ! intent(inout)
    2835             : 
    2836             :       call stat_update_var( stats_metadata%iAKm_rcm, AKm_rcm, & ! intent(in)
    2837           0 :                             stats_lh_zt) ! intent(inout)
    2838             :       call stat_update_var( stats_metadata%iAKm_rcc, AKm_rcc, & ! intent(in)
    2839           0 :                             stats_lh_zt ) ! intent(inout)
    2840             : 
    2841             :     end if ! stats_metadata%l_stats_samp
    2842             : 
    2843           0 :     return
    2844             :   end subroutine stats_accumulate_lh_tend
    2845             :     
    2846             :   !-----------------------------------------------------------------------
    2847           0 :   subroutine stats_finalize( stats_metadata, &
    2848             :                              stats_zt, stats_zm, stats_sfc, &
    2849             :                              stats_lh_zt, stats_lh_sfc, &
    2850             :                              stats_rad_zt, stats_rad_zm )
    2851             : 
    2852             :     !     Description:
    2853             :     !     Close NetCDF files and deallocate scratch space and
    2854             :     !     stats file structures.
    2855             :     !-----------------------------------------------------------------------
    2856             : 
    2857             :     use stats_variables, only: & 
    2858             :         stats_metadata_type
    2859             : 
    2860             : #ifdef NETCDF
    2861             :     use output_netcdf, only:  & 
    2862             :         close_netcdf ! Procedure
    2863             : #endif
    2864             : 
    2865             :     use stats_type, only: stats ! Type
    2866             : 
    2867             :     implicit none
    2868             : 
    2869             :     type (stats), target, intent(inout) :: &
    2870             :       stats_zt, &
    2871             :       stats_zm, &
    2872             :       stats_sfc, &
    2873             :       stats_lh_zt, &
    2874             :       stats_lh_sfc, &
    2875             :       stats_rad_zt, &
    2876             :       stats_rad_zm
    2877             : 
    2878             :     type (stats_metadata_type), intent(inout) :: &
    2879             :       stats_metadata
    2880             : 
    2881           0 :     if ( stats_metadata%l_stats .and. stats_metadata%l_netcdf ) then
    2882             : #ifdef NETCDF
    2883             :       call close_netcdf( stats_zt%file ) ! intent(inout)
    2884             :       call close_netcdf( stats_lh_zt%file ) ! intent(inout)
    2885             :       call close_netcdf( stats_lh_sfc%file ) ! intent(inout)
    2886             :       call close_netcdf( stats_zm%file ) ! intent(inout)
    2887             :       call close_netcdf( stats_rad_zt%file ) ! intent(inout)
    2888             :       call close_netcdf( stats_rad_zm%file ) ! intent(inout)
    2889             :       call close_netcdf( stats_sfc%file ) ! intent(inout)
    2890             : #else
    2891           0 :       error stop "This program was not compiled with netCDF support"
    2892             : #endif
    2893             :     end if
    2894             : 
    2895           0 :     if ( stats_metadata%l_stats ) then
    2896             :       ! De-allocate all stats_zt variables
    2897           0 :       if (allocated(stats_zt%z)) then
    2898           0 :         deallocate( stats_zt%z )
    2899             : 
    2900           0 :         deallocate( stats_zt%accum_field_values )
    2901           0 :         deallocate( stats_zt%accum_num_samples )
    2902           0 :         deallocate( stats_zt%l_in_update )
    2903             : 
    2904           0 :         deallocate( stats_zt%file%grid_avg_var )
    2905           0 :         deallocate( stats_zt%file%z )
    2906             :                
    2907             :         ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    2908           0 :         if ( allocated( stats_zt%file%lat_vals ) ) then
    2909           0 :           deallocate( stats_zt%file%lat_vals )
    2910             :         end if
    2911           0 :         if ( allocated( stats_zt%file%lon_vals ) ) then
    2912           0 :           deallocate( stats_zt%file%lon_vals )
    2913             :         end if
    2914             : 
    2915             :       end if
    2916             : 
    2917           0 :       if ( stats_metadata%l_silhs_out .and. allocated(stats_lh_zt%z) ) then
    2918             :         ! De-allocate all stats_lh_zt variables
    2919           0 :         deallocate( stats_lh_zt%z )
    2920             : 
    2921           0 :         deallocate( stats_lh_zt%accum_field_values )
    2922           0 :         deallocate( stats_lh_zt%accum_num_samples )
    2923           0 :         deallocate( stats_lh_zt%l_in_update )
    2924             : 
    2925           0 :         deallocate( stats_lh_zt%file%grid_avg_var )
    2926           0 :         deallocate( stats_lh_zt%file%z )
    2927             :         
    2928             :         ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    2929           0 :         if ( allocated(stats_lh_zt%file%lat_vals ) ) then
    2930           0 :           deallocate( stats_lh_zt%file%lat_vals )
    2931             :         end if
    2932           0 :         if ( allocated(stats_lh_zt%file%lon_vals ) ) then
    2933           0 :           deallocate( stats_lh_zt%file%lon_vals )
    2934             :         end if
    2935             : 
    2936             :         ! De-allocate all stats_lh_sfc variables
    2937           0 :         deallocate( stats_lh_sfc%z )
    2938             : 
    2939           0 :         deallocate( stats_lh_sfc%accum_field_values )
    2940           0 :         deallocate( stats_lh_sfc%accum_num_samples )
    2941           0 :         deallocate( stats_lh_sfc%l_in_update )
    2942             : 
    2943           0 :         deallocate( stats_lh_sfc%file%grid_avg_var )
    2944           0 :         deallocate( stats_lh_sfc%file%z )
    2945             :              
    2946             :         ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    2947           0 :         if ( allocated( stats_lh_sfc%file%lat_vals ) ) then
    2948           0 :           deallocate( stats_lh_sfc%file%lat_vals )
    2949             :         end if
    2950           0 :         if ( allocated( stats_lh_sfc%file%lon_vals ) ) then
    2951           0 :           deallocate( stats_lh_sfc%file%lon_vals )
    2952             :         end if
    2953             :       end if ! l_silhs_out
    2954             : 
    2955             :       ! De-allocate all stats_zm variables
    2956           0 :       if (allocated(stats_zm%z)) then
    2957           0 :         deallocate( stats_zm%z )
    2958             : 
    2959           0 :         deallocate( stats_zm%accum_field_values )
    2960           0 :         deallocate( stats_zm%accum_num_samples )
    2961           0 :         deallocate( stats_zm%l_in_update )
    2962             : 
    2963           0 :         deallocate( stats_zm%file%grid_avg_var )
    2964           0 :         deallocate( stats_zm%file%z )
    2965             :              
    2966             :         ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    2967           0 :         if ( allocated( stats_zm%file%lat_vals ) ) then
    2968           0 :           deallocate( stats_zm%file%lat_vals )
    2969             :         end if
    2970           0 :         if ( allocated( stats_zm%file%lon_vals ) ) then
    2971           0 :           deallocate( stats_zm%file%lon_vals )
    2972             :         end if
    2973             :         
    2974             :       end if
    2975             : 
    2976           0 :       if ( stats_metadata%l_output_rad_files ) then
    2977             :         ! De-allocate all stats_rad_zt variables
    2978           0 :         if (allocated(stats_rad_zt%z)) then
    2979           0 :           deallocate( stats_rad_zt%z )
    2980             : 
    2981           0 :           deallocate( stats_rad_zt%accum_field_values )
    2982           0 :           deallocate( stats_rad_zt%accum_num_samples )
    2983           0 :           deallocate( stats_rad_zt%l_in_update )
    2984             : 
    2985           0 :           deallocate( stats_rad_zt%file%grid_avg_var )
    2986           0 :           deallocate( stats_rad_zt%file%z )
    2987             :                
    2988             :           ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    2989           0 :           if ( allocated( stats_rad_zt%file%lat_vals ) ) then
    2990           0 :             deallocate( stats_rad_zt%file%lat_vals )
    2991             :           end if
    2992           0 :           if ( allocated( stats_rad_zt%file%lon_vals ) ) then
    2993           0 :             deallocate( stats_rad_zt%file%lon_vals )
    2994             :           end if
    2995             : 
    2996             :           ! De-allocate all stats_rad_zm variables
    2997           0 :           deallocate( stats_rad_zm%z )
    2998             : 
    2999           0 :           deallocate( stats_rad_zm%accum_field_values )
    3000           0 :           deallocate( stats_rad_zm%accum_num_samples )
    3001           0 :           deallocate( stats_rad_zm%l_in_update )
    3002             : 
    3003           0 :           deallocate( stats_rad_zm%file%grid_avg_var )
    3004           0 :           deallocate( stats_rad_zm%file%z )
    3005             : 
    3006             :           ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    3007           0 :           if ( allocated( stats_rad_zm%file%lat_vals ) ) then
    3008           0 :             deallocate( stats_rad_zm%file%lat_vals )
    3009             :           end if
    3010           0 :           if ( allocated( stats_rad_zm%file%lon_vals ) ) then
    3011           0 :             deallocate( stats_rad_zm%file%lon_vals )
    3012             :           end if
    3013             : 
    3014             :         end if
    3015             : 
    3016             :       end if ! l_output_rad_files
    3017             : 
    3018             :       ! De-allocate all stats_sfc variables
    3019           0 :       if (allocated(stats_sfc%z)) then
    3020           0 :         deallocate( stats_sfc%z )
    3021             : 
    3022           0 :         deallocate( stats_sfc%accum_field_values )
    3023           0 :         deallocate( stats_sfc%accum_num_samples )
    3024           0 :         deallocate( stats_sfc%l_in_update )
    3025             : 
    3026           0 :         deallocate( stats_sfc%file%grid_avg_var )
    3027           0 :         deallocate( stats_sfc%file%z )
    3028             :       end if
    3029             :              
    3030             :       ! Check if pointer is allocated to prevent crash in netcdf (ticket 765)
    3031           0 :       if ( allocated( stats_sfc%file%lat_vals ) ) then
    3032           0 :         deallocate( stats_sfc%file%lat_vals )
    3033             :       end if
    3034           0 :       if ( allocated( stats_sfc%file%lon_vals ) ) then
    3035           0 :         deallocate( stats_sfc%file%lon_vals )
    3036             :       end if
    3037             : 
    3038             :       ! De-allocate scalar indices
    3039           0 :       if (allocated(stats_metadata%isclrm)) then
    3040           0 :         deallocate( stats_metadata%isclrm )
    3041           0 :         deallocate( stats_metadata%isclrm_f )
    3042           0 :         deallocate( stats_metadata%iedsclrm )
    3043           0 :         deallocate( stats_metadata%iedsclrm_f )
    3044           0 :         deallocate( stats_metadata%isclrprtp )
    3045           0 :         deallocate( stats_metadata%isclrp2 )
    3046           0 :         deallocate( stats_metadata%isclrpthvp )
    3047           0 :         deallocate( stats_metadata%isclrpthlp )
    3048           0 :         deallocate( stats_metadata%isclrprcp )
    3049           0 :         deallocate( stats_metadata%iwpsclrp )
    3050           0 :         deallocate( stats_metadata%iwp2sclrp )
    3051           0 :         deallocate( stats_metadata%iwpsclrp2 )
    3052           0 :         deallocate( stats_metadata%iwpsclrprtp )
    3053           0 :         deallocate( stats_metadata%iwpsclrpthlp )
    3054           0 :         deallocate( stats_metadata%iwpedsclrp )
    3055             :       end if
    3056             : 
    3057             :       ! De-allocate hyderometeor statistical variables
    3058           0 :       if (allocated(stats_metadata%ihm_1)) then
    3059           0 :         deallocate( stats_metadata%ihm_1 )
    3060           0 :         deallocate( stats_metadata%ihm_2 )
    3061           0 :         deallocate( stats_metadata%imu_hm_1 )
    3062           0 :         deallocate( stats_metadata%imu_hm_2 )
    3063           0 :         deallocate( stats_metadata%imu_hm_1_n )
    3064           0 :         deallocate( stats_metadata%imu_hm_2_n )
    3065           0 :         deallocate( stats_metadata%isigma_hm_1 )
    3066           0 :         deallocate( stats_metadata%isigma_hm_2 )
    3067           0 :         deallocate( stats_metadata%isigma_hm_1_n )
    3068           0 :         deallocate( stats_metadata%isigma_hm_2_n )
    3069           0 :         deallocate( stats_metadata%icorr_w_hm_1 )
    3070           0 :         deallocate( stats_metadata%icorr_w_hm_2 )
    3071           0 :         deallocate( stats_metadata%icorr_chi_hm_1 )
    3072           0 :         deallocate( stats_metadata%icorr_chi_hm_2 )
    3073           0 :         deallocate( stats_metadata%icorr_eta_hm_1 )
    3074           0 :         deallocate( stats_metadata%icorr_eta_hm_2 )
    3075           0 :         deallocate( stats_metadata%icorr_Ncn_hm_1 )
    3076           0 :         deallocate( stats_metadata%icorr_Ncn_hm_2 )
    3077           0 :         deallocate( stats_metadata%icorr_hmx_hmy_1 )
    3078           0 :         deallocate( stats_metadata%icorr_hmx_hmy_2 )
    3079           0 :         deallocate( stats_metadata%icorr_w_hm_1_n )
    3080           0 :         deallocate( stats_metadata%icorr_w_hm_2_n )
    3081           0 :         deallocate( stats_metadata%icorr_chi_hm_1_n )
    3082           0 :         deallocate( stats_metadata%icorr_chi_hm_2_n )
    3083           0 :         deallocate( stats_metadata%icorr_eta_hm_1_n )
    3084           0 :         deallocate( stats_metadata%icorr_eta_hm_2_n )
    3085           0 :         deallocate( stats_metadata%icorr_Ncn_hm_1_n )
    3086           0 :         deallocate( stats_metadata%icorr_Ncn_hm_2_n )
    3087           0 :         deallocate( stats_metadata%icorr_hmx_hmy_1_n )
    3088           0 :         deallocate( stats_metadata%icorr_hmx_hmy_2_n )
    3089           0 :         deallocate( stats_metadata%ihmp2_zt )
    3090           0 :         deallocate( stats_metadata%iwp2hmp )
    3091           0 :         deallocate( stats_metadata%ihydrometp2 )
    3092           0 :         deallocate( stats_metadata%iwphydrometp )
    3093           0 :         deallocate( stats_metadata%irtphmp )
    3094           0 :         deallocate( stats_metadata%ithlphmp )
    3095           0 :         deallocate( stats_metadata%ihmxphmyp )
    3096           0 :         deallocate( stats_metadata%iK_hm )
    3097             :       end if
    3098             : 
    3099           0 :       if ( allocated( stats_metadata%isilhs_variance_category ) ) then
    3100           0 :         deallocate( stats_metadata%isilhs_variance_category )
    3101           0 :         deallocate( stats_metadata%ilh_samp_frac_category )
    3102             :       end if
    3103             : 
    3104             :     end if ! l_stats
    3105             : 
    3106           0 :     return
    3107             :   end subroutine stats_finalize
    3108             : 
    3109             : !===============================================================================
    3110             : 
    3111             : !-----------------------------------------------------------------------
    3112           0 : subroutine stats_check_num_samples( stats_grid, stats_metadata, &
    3113             :                                     l_error )
    3114             : 
    3115             : ! Description:
    3116             : !   Ensures that each variable in a stats grid is sampled the correct
    3117             : !   number of times.
    3118             : ! References:
    3119             : !   None
    3120             : !-----------------------------------------------------------------------
    3121             : 
    3122             :     use constants_clubb, only: &
    3123             :         fstderr ! Constant
    3124             : 
    3125             :     use stats_type, only: &
    3126             :         stats ! Type
    3127             : 
    3128             :     use stats_variables, only: &
    3129             :         stats_metadata_type
    3130             : 
    3131             :     use error_code, only: &
    3132             :         clubb_at_least_debug_level   ! Procedure
    3133             : 
    3134             :     implicit none
    3135             : 
    3136             :   ! Input Variables
    3137             :     type (stats), intent(in) :: &
    3138             :       stats_grid               ! Grid type              [grid]
    3139             : 
    3140             :     type (stats_metadata_type), intent(in) :: &
    3141             :       stats_metadata
    3142             : 
    3143             :   ! Input/Output Variables
    3144             :     logical, intent(inout) :: &
    3145             :       l_error                  ! Indicates an error     [boolean]
    3146             : 
    3147             :   ! Local Variables
    3148             :     integer :: ivar, kvar      ! Loop variable          [index]
    3149             : 
    3150             :     logical :: l_proper_sample
    3151             : 
    3152             : !-----------------------------------------------------------------------
    3153             : 
    3154             :   !----- Begin Code -----
    3155             : 
    3156             :   ! Look for errors by checking the number of sampling points
    3157             :   ! for each variable in the statistics grid at each vertical level.
    3158           0 :   do ivar = 1, stats_grid%num_output_fields
    3159           0 :     do kvar = 1, stats_grid%kk
    3160             : 
    3161           0 :       l_proper_sample = ( stats_grid%accum_num_samples(1,1,kvar,ivar) == 0 .or. &
    3162             :                           stats_grid%accum_num_samples(1,1,kvar,ivar) &
    3163             :                           == floor(  stats_metadata%stats_tout  &
    3164           0 :                                    / stats_metadata%stats_tsamp ) )
    3165             : 
    3166           0 :       if ( .not. l_proper_sample ) then
    3167             : 
    3168           0 :         l_error = .true.  ! This will stop the run
    3169             : 
    3170           0 :         if ( clubb_at_least_debug_level( 1 ) ) then
    3171           0 :           write(fstderr,*) 'Possible sampling error for variable ',  &
    3172           0 :                            trim(stats_grid%file%grid_avg_var(ivar)%name), ' in stats_grid ',  &
    3173           0 :                            'at k = ', kvar,  &
    3174           0 :                            '; stats_grid%accum_num_samples(',kvar,',',ivar,') = ', &
    3175           0 :                             stats_grid%accum_num_samples(1,1,kvar,ivar)
    3176             :         end if ! clubb_at_lest_debug_level 1
    3177             : 
    3178             : 
    3179             :       end if ! .not. l_proper_sample
    3180             : 
    3181             :     end do ! kvar = 1 .. stats_grid%kk
    3182             :   end do ! ivar = 1 .. stats_grid%num_output_fields
    3183             : 
    3184           0 :   return
    3185             : end subroutine stats_check_num_samples
    3186             : !-----------------------------------------------------------------------
    3187             : 
    3188             : end module stats_clubb_utilities

Generated by: LCOV version 1.14