LCOV - code coverage report
Current view: | top level - physics/clubb/src/CLUBB_core - stats_type_utilities.F90 (source / functions) |  |  | Hit | Total | Coverage
Test: | coverage.info |  | Lines: | 0 | 62 | 0.0 %
Date: | 2024-12-17 17:57:11 |  | Functions: | 0 | 9 | 0.0 %

              Line data    Source code
           1             : !-----------------------------------------------------------------------
           2             : ! $Id$
           3             : !===============================================================================
           4             : module stats_type_utilities
           5             :
           6             :   ! Description:
           7             :   !   Contains subroutines for interfacing with type, stats
           8             :   !-----------------------------------------------------------------------
           9             :
          10             :   use stats_type, only: &
          11             :     stats ! type
          12             :
          13             :   use clubb_precision, only: &
          14             :     core_rknd
          15             :
          16             :   implicit none
          17             :
          18             :   private ! Set Default Scope
          19             :
          20             :   public :: stat_assign, &
          21             :             stat_update_var, &
          22             :             stat_update_var_pt, &
          23             :             stat_begin_update, &
          24             :             stat_begin_update_pt, &
          25             :             stat_end_update, &
          26             :             stat_end_update_pt, &
          27             :             stat_modify, &
          28             :             stat_modify_pt
          29             :   contains
          30             :
          31             :   !=============================================================================
          32           0 :   subroutine stat_assign( var_index, var_name,  &
          33             :                           var_description, var_units, &
          34             :                           l_silhs, &
          35             :                           grid_kind )
          36             :
          37             :     ! Description:
          38             :     !   Assigns pointers for statistics variables in grid. There is an
          39             :     !   option to make the variable a SILHS variable (updated n_microphys_calls
          40             :     !   times per timestep rather than just once).
          41             :
          42             :     !
          43             :     ! References:
          44             :     !   None
          45             :     !-----------------------------------------------------------------------
          46             :
          47             :     implicit none
          48             :
          49             :     ! Input Variables
          50             :
          51             :     integer,intent(in) :: var_index                   ! Variable index       [#]
          52             :     character(len = *), intent(in) :: var_name        ! Variable name        []
          53             :     character(len = *), intent(in) :: var_description ! Variable description []
          54             :     character(len = *), intent(in) :: var_units       ! Variable units       []
          55             :
          56             :     logical, intent(in) :: l_silhs                    ! SILHS variable       [boolean]
          57             :
          58             :     ! Input/Output Variable
          59             :
          60             :     ! Which grid the variable is located on (e.g., zt, zm, sfc)
          61             :     type(stats), target, intent(inout) :: grid_kind
          62             :
          63           0 :     grid_kind%file%grid_avg_var(var_index)%ptr => grid_kind%accum_field_values(:,:,:,var_index)
          64           0 :     grid_kind%file%grid_avg_var(var_index)%name = var_name
          65           0 :     grid_kind%file%grid_avg_var(var_index)%description = var_description
          66           0 :     grid_kind%file%grid_avg_var(var_index)%units = var_units
          67             :
          68           0 :     grid_kind%file%grid_avg_var(var_index)%l_silhs = l_silhs
          69             :
          70             :     !Example of the old format
          71             :     !changed by Joshua Fasching 23 August 2007
          72             :
          73             :     !stats_zt%file%var(stats_metadata%ithlm)%ptr => stats_zt%accum_field_values(:,k)
          74             :     !stats_zt%file%var(stats_metadata%ithlm)%name = "thlm"
          75             :     !stats_zt%file%var(stats_metadata%ithlm)%description = "thetal (K)"
          76             :     !stats_zt%file%var(stats_metadata%ithlm)%units = "K"
          77             :
          78           0 :     return
          79             :
          80           0 :   end subroutine stat_assign
          81             :
          82             :   !=============================================================================
          83           0 :   subroutine stat_update_var( var_index, value, &
          84             :                               grid_kind )
          85             :
          86             :     ! Description:
          87             :     ! This updates the value of a statistics variable located at var_index
          88             :     ! associated with grid type 'grid_kind' (zt, zm, or sfc).
          89             :     !
          90             :     ! This subroutine is used when a statistical variable needs to be updated
          91             :     ! only once during a model timestep.
          92             :     !
          93             :     ! In regards to budget terms, this subroutine is used for variables that
          94             :     ! are either completely implicit (e.g. wprtp_ma) or completely explicit
          95             :     ! (e.g. wp2_pr3).  For completely implicit terms, once the variable has been
          96             :     ! solved for, the implicit contribution can be finalized.  The finalized
          97             :     ! implicit contribution is sent into stat_update_var_pt.  For completely
          98             :     ! explicit terms, the explicit contribution is sent into stat_update_var_pt
          99             :     ! once it has been calculated.
         100             :     !---------------------------------------------------------------------
         101             :
         102             :     use clubb_precision, only: &
         103             :         stat_rknd ! Constant
         104             :
         105             :     use stat_file_module, only: &
         106             :         clubb_i, clubb_j ! Variable(s)
         107             :
         108             :     implicit none
         109             :
         110             :     ! Input Variables(s)
         111             :
         112             :     integer, intent(in) ::  &
         113             :       var_index ! The index at which the variable is stored  []
         114             :
         115             :     ! Input/Output Variable(s)
         116             :     type(stats), intent(inout) ::  &
         117             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc )
         118             :
         119             :     ! Input Variable(s) NOTE: Due to the implicit none above, these must
         120             :     ! be declared below to allow the use of grid_kind
         121             :
         122             :     real( kind = core_rknd ), dimension(grid_kind%kk), intent(in) :: &
         123             :       value ! Value of field being added to the statistic    [Units Vary]
         124             :
         125             :     integer :: k
         126             :
         127           0 :     if ( var_index > 0 ) then
         128           0 :       do k = 1, grid_kind%kk
         129           0 :         grid_kind%accum_field_values(clubb_i,clubb_j,k,var_index) =  &
         130           0 :              grid_kind%accum_field_values(clubb_i,clubb_j,k,var_index) + real( value(k), &
         131           0 :                 kind=stat_rknd )
         132           0 :         grid_kind%accum_num_samples(clubb_i,clubb_j,k,var_index) =  &
         133           0 :              grid_kind%accum_num_samples(clubb_i,clubb_j,k,var_index) + 1
         134             :       end do
         135             :     endif
         136             :
         137           0 :     return
         138             :   end subroutine stat_update_var
         139             :
         140             :   !=============================================================================
         141           0 :   subroutine stat_update_var_pt( var_index, grid_level, value, &
         142             :                                  grid_kind )
         143             :
         144             :     ! Description:
         145             :     ! This updates the value of a statistics variable located at var_index
         146             :     ! associated with grid type 'grid_kind' at a specific grid_level.
         147             :     !
         148             :     ! See the description of stat_update_var for more details.
         149             :     !---------------------------------------------------------------------
         150             :
         151             :     use clubb_precision, only: &
         152             :         stat_rknd ! Constant
         153             :
         154             :     use stat_file_module, only: &
         155             :         clubb_i, clubb_j ! Variable(s)
         156             :
         157             :     implicit none
         158             :
         159             :     ! Input Variables(s)
         160             :
         161             :     integer, intent(in) ::  &
         162             :       var_index,    & ! The index at which the variable is stored           []
         163             :       grid_level      ! The level at which the variable is to be modified   []
         164             :
         165             :     real( kind = core_rknd ), intent(in) :: &
         166             :       value ! Value of field being added to the statistic         [Units Vary]
         167             :
         168             :     ! Input/Output Variable(s)
         169             :     type(stats), intent(inout) ::  &
         170             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         171             :
         172           0 :     if ( var_index > 0 ) then
         173             :
         174           0 :       grid_kind%accum_field_values(clubb_i,clubb_j,grid_level,var_index) = &
         175             :         grid_kind%accum_field_values(clubb_i,clubb_j,grid_level,var_index) + &
         176           0 :           real( value, kind=stat_rknd )
         177             :
         178           0 :       grid_kind%accum_num_samples(clubb_i,clubb_j,grid_level,var_index) = &
         179           0 :         grid_kind%accum_num_samples(clubb_i,clubb_j,grid_level,var_index) + 1
         180             :
         181             :     endif
         182             :
         183           0 :     return
         184             :   end subroutine stat_update_var_pt
         185             :
         186             :   !=============================================================================
         187           0 :   subroutine stat_begin_update(nz, var_index, value, &
         188             :                                 grid_kind )
         189             :
         190             :     ! Description:
         191             :     ! This begins an update of the value of a statistics variable located at
         192             :     ! var_index on the (zt, zm, or sfc) grid.  It is used in conjunction with
         193             :     ! subroutine stat_end_update.
         194             :     !
         195             :     ! This subroutine is used when a statistical variable needs to be updated
         196             :     ! more than one time during a model timestep.  Commonly, this is used for
         197             :     ! beginning a budget term calculation.
         198             :     !
         199             :     ! In this type of stats calculation, we first subtract the field
         200             :     ! (e.g. rtm / dt ) from the statistic, then update rtm by a term
         201             :     ! (e.g. clip rtm), and then re-add the field (e.g. rtm / dt) to the
         202             :     ! statistic.
         203             :     !
         204             :     ! Example:
         205             :     !
         206             :     !  call stat_begin_update( stats_metadata%irtm_bt, real(rtm / dt), stats_zt )
         207             :     !
         208             :     !  !!! Perform clipping of rtm !!!
         209             :     !
         210             :     !  call stat_end_update( stats_metadata%irtm_bt, real(rtm / dt), stats_zt )
         211             :     !
         212             :     ! This subroutine is often used with stats budget terms for variables that
         213             :     ! have both implicit and explicit components (e.g. wp3_ta).  The explicit
         214             :     ! component is sent into stat_begin_update_pt (with the sign reversed
         215             :     ! because stat_begin_update_pt automatically subtracts the value sent into
         216             :     ! it).  Then, once the variable has been solved for, the implicit
         217             :     ! statistical contribution can be finalized.  The finalized implicit
         218             :     ! component is sent into stat_end_update_pt.
         219             :     !---------------------------------------------------------------------
         220             :
         221             :     implicit none
         222             :
         223             :     ! Input Variables(s)
         224             :     integer, intent(in) :: &
         225             : nz
         226             :
         227             :     integer, intent(in) ::  &
         228             :       var_index      ! The index at which the variable is stored           []
         229             :
         230             :     real( kind = core_rknd ), dimension(nz), intent(in) :: &
         231             :       value          ! Value of field being added to the statistic         [Units Vary]
         232             :
         233             :     ! Input/Output Variable(s)
         234             :     type(stats), intent(inout) ::  &
         235             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         236             :
         237             :     integer :: i
         238             :
         239           0 :     do i = 1,nz
         240             :
         241             :       call stat_begin_update_pt &
         242           0 :             ( var_index, i, value(i), & ! intent(in)
         243           0 :               grid_kind ) ! intent(inout)
         244             :
         245             :     enddo
         246             :
         247           0 :     return
         248             :   end subroutine stat_begin_update
         249             :
         250             :   !=============================================================================
         251           0 :   subroutine stat_begin_update_pt &
         252             :              ( var_index, grid_level, value, &
         253             :                grid_kind )
         254             :
         255             :     ! Description:
         256             :     !   This begins an update of the value of a statistics variable located at
         257             :     !   var_index associated with the grid type (grid_kind) at a specific
         258             :     !   grid_level.  It is used in conjunction with subroutine stat_end_update_pt.
         259             :     !
         260             :     ! Notes:
         261             :     !   Commonly this is used for beginning a budget.  See the description of
         262             :     !   stat_begin_update for more details.
         263             :     !
         264             :     ! References:
         265             :     !   None
         266             :     !---------------------------------------------------------------------
         267             :
         268             :     use clubb_precision, only: &
         269             :         stat_rknd ! Constant
         270             :
         271             :     use stat_file_module, only: &
         272             :         clubb_i, clubb_j ! Variable(s)
         273             :
         274             :     use constants_clubb, only: &
         275             :         fstderr   ! Constant(s)
         276             :
         277             :     use error_code, only: &
         278             :         clubb_at_least_debug_level   ! Procedure
         279             :
         280             :     implicit none
         281             :
         282             :     ! Input Variables(s)
         283             :
         284             :     integer, intent(in) ::  &
         285             :       var_index,    & ! The index at which the variable is stored           []
         286             :       grid_level      ! The level at which the variable is to be modified   []
         287             :
         288             :     real( kind = core_rknd ), intent(in) :: &
         289             :       value ! Value of field being added to the statistic                [Units Vary]
         290             :
         291             :     ! Input/Output Variable(s)
         292             :     type(stats), intent(inout) ::  &
         293             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         294             :
         295             :     ! ---- Begin Code ----
         296             :
         297           0 :     if ( var_index > 0 ) then  ! Are we storing this variable?
         298             :
         299             :       ! Can we begin an update?
         300           0 :       if ( .not. grid_kind%l_in_update(clubb_i,clubb_j,grid_level,var_index) ) then
         301             :
         302           0 :         grid_kind%accum_field_values(clubb_i,clubb_j,grid_level, var_index) =  &
         303             :                 grid_kind%accum_field_values(clubb_i,clubb_j,grid_level, var_index) - &
         304           0 :                   real( value, kind=stat_rknd )
         305             :
         306           0 :         grid_kind%l_in_update(clubb_i,clubb_j,grid_level, var_index) = .true.  ! Start Record
         307             :
         308           0 :       else if ( clubb_at_least_debug_level( 1 ) ) then
         309             :
         310             :             write(fstderr,*) "Beginning an update before finishing previous for variable: "// &
         311           0 :                               trim( grid_kind%file%grid_avg_var(var_index)%name )
         312             :       endif
         313             :
         314             :     endif
         315             :
         316           0 :     return
         317             :   end subroutine stat_begin_update_pt
         318             :
         319             :   !=============================================================================
         320           0 :   subroutine stat_end_update(nz, var_index, value, &
         321             :                               grid_kind )
         322             :
         323             :     ! Description:
         324             :     ! This ends an update of the value of a statistics variable located at
         325             :     ! var_index on the (zt, zm, or sfc) grid.  It is used in conjunction with
         326             :     ! subroutine stat_begin_update.
         327             :     !
         328             :     ! This subroutine is used when a statistical variable needs to be updated
         329             :     ! more than one time during a model timestep.  Commonly, this is used for
         330             :     ! finishing a budget term calculation.
         331             :     !
         332             :     ! In this type of stats calculation, we first subtract the field
         333             :     ! (e.g. rtm / dt ) from the statistic, then update rtm by a term
         334             :     ! (e.g. clip rtm), and then re-add the field (e.g. rtm / dt) to the
         335             :     ! statistic.
         336             :     !
         337             :     ! Example:
         338             :     !
         339             :     !  call stat_begin_update( stats_metadata%irtm_bt, real(rtm / dt), stats_zt )
         340             :     !
         341             :     !  !!! Perform clipping of rtm !!!
         342             :     !
         343             :     !  call stat_end_update( stats_metadata%irtm_bt, real(rtm / dt), stats_zt )
         344             :     !
         345             :     ! This subroutine is often used with stats budget terms for variables that
         346             :     ! have both implicit and explicit components (e.g. wp3_ta).  The explicit
         347             :     ! component is sent into stat_begin_update_pt (with the sign reversed
         348             :     ! because stat_begin_update_pt automatically subtracts the value sent into
         349             :     ! it).  Then, once the variable has been solved for, the implicit
         350             :     ! statistical contribution can be finalized.  The finalized implicit
         351             :     ! component is sent into stat_end_update_pt.
         352             :     !---------------------------------------------------------------------
         353             :
         354             :     implicit none
         355             :
         356             :     ! Input Variables(s)
         357             :     integer, intent(in) :: &
         358             : nz
         359             :
         360             :     integer, intent(in) ::  &
         361             :       var_index ! The index at which the variable is stored           []
         362             :
         363             :     real( kind = core_rknd ), dimension(nz), intent(in) :: &
         364             :       value ! Value of field being added to the statistic             [Units Vary]
         365             :
         366             :     ! Input/Output Variable(s)
         367             :     type(stats), intent(inout) ::  &
         368             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         369             :
         370             :     integer :: k
         371             :
         372             :     ! ---- Begin Code ----
         373             :
         374           0 :     do k = 1,nz
         375             :       call stat_end_update_pt &
         376           0 :                ( var_index, k, value(k), & ! intent(in)
         377           0 :                  grid_kind ) ! intent(inout)
         378             :     enddo
         379             :
         380           0 :     return
         381             :   end subroutine stat_end_update
         382             :
         383             :   !=============================================================================
         384           0 :   subroutine stat_end_update_pt &
         385             :                 ( var_index, grid_level, value, &
         386             :                   grid_kind )
         387             :
         388             :     ! Description:
         389             :     ! This ends an update of the value of a statistics variable located at
         390             :     ! var_index associated with the grid type (grid_kind) at a specific
         391             :     ! grid_level.  It is used in conjunction with subroutine
         392             :     ! stat_begin_update_pt.
         393             :     !
         394             :     ! Commonly this is used for finishing a budget.  See the description of
         395             :     ! stat_end_update for more details.
         396             :     !---------------------------------------------------------------------
         397             :
         398             :     use stat_file_module, only: &
         399             :         clubb_i, clubb_j ! Variable(s)
         400             :
         401             :     use constants_clubb, only: &
         402             :         fstderr   ! Constant(s)
         403             :
         404             :     use error_code, only: &
         405             :         clubb_at_least_debug_level   ! Procedure
         406             :
         407             :     implicit none
         408             :
         409             :     ! Input Variables(s)
         410             :
         411             :     integer, intent(in) ::  &
         412             :       var_index,   & ! The index at which the variable is stored           []
         413             :       grid_level     ! The level at which the variable is to be modified   []
         414             :
         415             :     real( kind = core_rknd ), intent(in) :: &
         416             :       value       ! Value of field being added to the statistic         [Units Vary]
         417             :
         418             :     ! Input/Output Variable(s)
         419             :     type(stats), intent(inout) ::  &
         420             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         421             :
         422             :     ! ---- Begin Code ----
         423             :
         424           0 :     if ( var_index > 0 ) then ! Are we storing this variable?
         425             :
         426             :       ! Can we end an update?
         427           0 :       if ( grid_kind%l_in_update(clubb_i,clubb_j,grid_level,var_index) ) then
         428             :
         429             :         call stat_update_var_pt &
         430             :                  ( var_index, grid_level, value, & ! intent(in)
         431           0 :                    grid_kind ) ! intent(inout)
         432             :
         433           0 :         grid_kind%l_in_update(clubb_i,clubb_j,grid_level,var_index) = .false. ! End Record
         434             :
         435           0 :       else if ( clubb_at_least_debug_level( 1 ) ) then
         436             :
         437           0 :         write(fstderr,*) "Ending before beginning update. For variable "// &
         438           0 :                           grid_kind%file%grid_avg_var(var_index)%name
         439             :       endif
         440             :
         441             :     endif
         442             :
         443           0 :     return
         444             :   end subroutine stat_end_update_pt
         445             :
         446             :   !=============================================================================
         447           0 :   subroutine stat_modify(nz, var_index, value, &
         448             :                           grid_kind )
         449             :
         450             :     ! Description:
         451             :     ! This modifies the value of a statistics variable located at var_index on
         452             :     ! the (zt, zm, or sfc) grid.  It does not increment the sampling count.
         453             :     !
         454             :     ! This subroutine is normally used when a statistical variable needs to be
         455             :     ! updated more than twice during a model timestep.  Commonly, this is used
         456             :     ! if a budget term calculation needs an intermediate modification between
         457             :     ! stat_begin_update and stat_end_update.
         458             :     !---------------------------------------------------------------------
         459             :
         460             :     implicit none
         461             :
         462             :     ! Input Variables(s)
         463             :     integer, intent(in) :: &
         464             : nz
         465             :
         466             :     integer, intent(in) ::  &
         467             :       var_index ! The index at which the variable is stored           []
         468             :
         469             :     real( kind = core_rknd ), dimension(nz), intent(in) :: &
         470             :      value     ! Value of field being added to the statistic         [Units Vary]
         471             :
         472             :     ! Input/Output Variable(s)
         473             :     type(stats), intent(inout) ::  &
         474             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         475             :
         476             :     integer :: k
         477             :
         478             :     ! ---- Begin Code ----
         479             :
         480           0 :     do k = 1,nz
         481             :
         482           0 :       call stat_modify_pt( var_index, k, value(k), & ! intent(in)
         483           0 :                            grid_kind ) ! intent(inout)
         484             :
         485             :     enddo
         486             :
         487           0 :     return
         488             :   end subroutine stat_modify
         489             :
         490             :   !=============================================================================
         491           0 :   subroutine stat_modify_pt( var_index, grid_level, value, &
         492             :                              grid_kind )
         493             :
         494             :     ! Description:
         495             :     ! This modifies the value of a statistics variable located at var_index on
         496             :     ! the grid at a specific point. It does not increment the sampling count.
         497             :     !
         498             :     ! Commonly this is used for intermediate updates to a budget.  See the
         499             :     ! description of stat_modify for more details.
         500             :     !---------------------------------------------------------------------
         501             :
         502             :     use clubb_precision, only: &
         503             :         stat_rknd ! Constant
         504             :
         505             :     use stat_file_module, only: &
         506             :         clubb_i, clubb_j ! Variable(s)
         507             :
         508             :     implicit none
         509             :
         510             :     ! Input Variables(s)
         511             :
         512             :     integer, intent(in) ::  &
         513             :       var_index ! The index at which the variable is stored            []
         514             :
         515             :
         516             :     real( kind = core_rknd ), intent(in) :: &
         517             :       value      ! Value of field being added to the statistic         [Units Vary]
         518             :
         519             :     integer, intent(in) ::  &
         520             :       grid_level ! The level at which the variable is to be modified   []
         521             :
         522             :     ! Input/Output Variable(s)
         523             :     type(stats), intent(inout) ::  &
         524             :       grid_kind ! Which grid the variable is located on (zt, zm, rad, or sfc).
         525             :
         526             :     ! ---- Begin Code ----
         527             :
         528           0 :     if ( var_index > 0 ) then
         529             :
         530           0 :       grid_kind%accum_field_values(clubb_i,clubb_j,grid_level,var_index )  &
         531             :          = grid_kind%accum_field_values(clubb_i,clubb_j,grid_level,var_index ) + &
         532           0 :           real( value, kind=stat_rknd )
         533             :
         534             :     end if
         535             :
         536           0 :     return
         537             :   end subroutine stat_modify_pt
         538             :
         539             : !===============================================================================
         540             :
         541             : end module stats_type_utilities

Generated by: LCOV version 1.14