LCOV - code coverage report
Current view: top level - columnphysics - icepack_shortwave.F90 (source / functions) Hit Total Coverage
Test: 200804-015008:4c42a82e3d:3:base,travis,quick Lines: 1210 1668 72.54 %
Date: 2020-08-03 20:10:57 Functions: 20 21 95.24 %

          Line data    Source code
       1             : !=======================================================================
       2             : !
       3             : ! The albedo and absorbed/transmitted flux parameterizations for
       4             : ! snow over ice, bare ice and ponded ice.
       5             : !
       6             : ! Presently, two methods are included:
       7             : !   (1) CCSM3 
       8             : !   (2) Delta-Eddington 
       9             : ! as two distinct routines.
      10             : ! Either can be called from the ice driver.
      11             : !
      12             : ! The Delta-Eddington method is described here:
      13             : !
      14             : ! Briegleb, B. P., and B. Light (2007): A Delta-Eddington Multiple 
      15             : !    Scattering Parameterization for Solar Radiation in the Sea Ice 
      16             : !    Component of the Community Climate System Model, NCAR Technical 
      17             : !    Note  NCAR/TN-472+STR  February 2007
      18             : !
      19             : ! name: originally ice_albedo
      20             : !
      21             : ! authors:  Bruce P. Briegleb, NCAR
      22             : !           Elizabeth C. Hunke and William H. Lipscomb, LANL
      23             : ! 2005, WHL: Moved absorbed_solar from icepack_therm_vertical to this 
      24             : !            module and changed name from ice_albedo
      25             : ! 2006, WHL: Added Delta Eddington routines from Bruce Briegleb
      26             : ! 2006, ECH: Changed data statements in Delta Eddington routines (no 
      27             : !            longer hardwired)
      28             : !            Converted to free source form (F90)
      29             : ! 2007, BPB: Completely updated Delta-Eddington code, so that:
      30             : !            (1) multiple snow layers enabled (i.e. nslyr > 1)
      31             : !            (2) included SSL for snow surface absorption
      32             : !            (3) added Sswabs for internal snow layer absorption
      33             : !            (4) variable sea ice layers allowed (i.e. not hardwired)
      34             : !            (5) updated all inherent optical properties
      35             : !            (6) included algae absorption for sea ice lowest layer
      36             : !            (7) very complete internal documentation included
      37             : ! 2007, ECH: Improved efficiency
      38             : ! 2008, BPB: Added aerosols to Delta Eddington code
      39             : ! 2013, ECH: merged with NCAR version, cleaned up
      40             : 
      41             :       module icepack_shortwave
      42             : 
      43             :       use icepack_kinds
      44             :       use icepack_parameters, only: c0, c1, c1p5, c2, c3, c4, c10
      45             :       use icepack_parameters, only: p01, p1, p15, p25, p5, p75, puny
      46             :       use icepack_parameters, only: albocn, Timelt, snowpatch, awtvdr, awtidr, awtvdf, awtidf
      47             :       use icepack_parameters, only: kappav, hs_min, rhofresh, rhos, nspint
      48             :       use icepack_parameters, only: hi_ssl, hs_ssl, min_bgc, sk_l
      49             :       use icepack_parameters, only: z_tracers, skl_bgc, calc_tsfc, shortwave, kalg, heat_capacity
      50             :       use icepack_parameters, only: r_ice, r_pnd, r_snw, dt_mlt, rsnw_mlt, hs0, hs1, hp1
      51             :       use icepack_parameters, only: pndaspect, albedo_type, albicev, albicei, albsnowv, albsnowi, ahmax
      52             :       use icepack_tracers,    only: ntrcr, nbtrcr_sw
      53             :       use icepack_tracers,    only: tr_pond_cesm, tr_pond_lvl, tr_pond_topo
      54             :       use icepack_tracers,    only: tr_bgc_N, tr_aero
      55             :       use icepack_tracers,    only: nt_bgc_N, nt_zaero, tr_bgc_N
      56             :       use icepack_tracers,    only: tr_zaero, nlt_chl_sw, nlt_zaero_sw
      57             :       use icepack_tracers,    only: n_algae, n_aero, n_zaero
      58             :       use icepack_warnings,   only: warnstr, icepack_warnings_add
      59             :       use icepack_warnings,   only: icepack_warnings_setabort, icepack_warnings_aborted
      60             : 
      61             :       use icepack_zbgc_shared,only: R_chl2N, F_abs_chl
      62             :       use icepack_zbgc_shared,only: remap_zbgc
      63             :       use icepack_orbital, only: compute_coszen
      64             : 
      65             : 
      66             :       implicit none
      67             : 
      68             :       private
      69             :       public :: run_dEdd, &
      70             :                 shortwave_ccsm3, &
      71             :                 compute_shortwave_trcr, &
      72             :                 icepack_prep_radiation, &
      73             :                 icepack_step_radiation
      74             : 
      75             :       real (kind=dbl_kind), parameter :: &
      76             :          hpmin  = 0.005_dbl_kind, & ! minimum allowed melt pond depth (m)
      77             :          hp0    = 0.200_dbl_kind    ! pond depth below which transition to bare ice
      78             : 
      79             :       real (kind=dbl_kind), parameter :: & 
      80             :          exp_argmax = c10    ! maximum argument of exponential
      81             : 
      82             : !=======================================================================
      83             : 
      84             :       contains
      85             : 
      86             : !=======================================================================
      87             : !
      88             : ! Driver for basic solar radiation from CCSM3.  Albedos and absorbed solar.
      89             : 
      90      289620 :       subroutine shortwave_ccsm3 (aicen,    vicen,    &
      91      144810 :                                   vsnon,    Tsfcn,    &
      92             :                                   swvdr,    swvdf,    &
      93             :                                   swidr,    swidf,    &
      94             :                                   heat_capacity,      &
      95           0 :                                   albedo_type,        &
      96             :                                   albicev,  albicei,  &
      97             :                                   albsnowv, albsnowi, &
      98             :                                   ahmax,              &
      99      144810 :                                   alvdrn,   alidrn,   &
     100      144810 :                                   alvdfn,   alidfn,   &
     101      144810 :                                   fswsfc,   fswint,   &
     102      144810 :                                   fswthru,            &
     103      144810 :                                   fswthru_vdr,        &
     104      144810 :                                   fswthru_vdf,        &
     105      144810 :                                   fswthru_idr,        &
     106      144810 :                                   fswthru_idf,        &
     107      144810 :                                   fswpenl,            &
     108      289620 :                                   Iswabs,   SSwabs,   &
     109      144810 :                                   albin,    albsn,    &
     110             :                                   coszen,   ncat,     &
     111             :                                   nilyr)
     112             : 
     113             :       integer (kind=int_kind), intent(in) :: &
     114             :          nilyr    , & ! number of ice layers
     115             :          ncat         ! number of ice thickness categories
     116             : 
     117             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
     118             :          aicen    , & ! concentration of ice per category
     119             :          vicen    , & ! volume of ice per category
     120             :          vsnon    , & ! volume of ice per category
     121             :          Tsfcn        ! surface temperature
     122             : 
     123             :       real (kind=dbl_kind), intent(in) :: &
     124             :          swvdr    , & ! sw down, visible, direct  (W/m^2)
     125             :          swvdf    , & ! sw down, visible, diffuse (W/m^2)
     126             :          swidr    , & ! sw down, near IR, direct  (W/m^2)
     127             :          swidf        ! sw down, near IR, diffuse (W/m^2)
     128             : 
     129             :       ! baseline albedos for ccsm3 shortwave, set in namelist
     130             :       real (kind=dbl_kind), intent(in) :: &
     131             :          albicev , & ! visible ice albedo for h > ahmax
     132             :          albicei , & ! near-ir ice albedo for h > ahmax
     133             :          albsnowv, & ! cold snow albedo, visible
     134             :          albsnowi, & ! cold snow albedo, near IR
     135             :          ahmax       ! thickness above which ice albedo is constant (m)
     136             : 
     137             :       logical(kind=log_kind), intent(in) :: &
     138             :          heat_capacity! if true, ice has nonzero heat capacity
     139             : 
     140             :       character (len=char_len), intent(in) :: &
     141             :          albedo_type  ! albedo parameterization, 'ccsm3' or 'constant'
     142             : 
     143             :       real (kind=dbl_kind), dimension (:), intent(inout) :: &
     144             :          alvdrn   , & ! visible, direct, avg   (fraction)
     145             :          alidrn   , & ! near-ir, direct, avg   (fraction)
     146             :          alvdfn   , & ! visible, diffuse, avg  (fraction)
     147             :          alidfn   , & ! near-ir, diffuse, avg  (fraction)
     148             :          fswsfc   , & ! SW absorbed at ice/snow surface (W m-2)
     149             :          fswint   , & ! SW absorbed in ice interior, below surface (W m-2)
     150             :          fswthru  , & ! SW through ice to ocean (W m-2)
     151             :          albin    , & ! bare ice albedo
     152             :          albsn        ! snow albedo
     153             : 
     154             :       real (kind=dbl_kind), dimension (:), intent(out), optional :: &
     155             :          fswthru_vdr  , & ! vis dir SW through ice to ocean (W m-2)
     156             :          fswthru_vdf  , & ! vis dif SW through ice to ocean (W m-2)
     157             :          fswthru_idr  , & ! nir dir SW through ice to ocean (W m-2)
     158             :          fswthru_idf      ! nir dif SW through ice to ocean (W m-2)
     159             : 
     160             :       real (kind=dbl_kind), intent(inout) :: &
     161             :          coszen       ! cosine(zenith angle)
     162             : 
     163             :       real (kind=dbl_kind), dimension (:,:), intent(inout) :: &
     164             :          fswpenl  , & ! SW entering ice layers (W m-2)
     165             :          Iswabs   , & ! SW absorbed in particular layer (W m-2)
     166             :          Sswabs       ! SW absorbed in particular layer (W m-2)
     167             : 
     168             :       ! local variables
     169             : 
     170             :       integer (kind=int_kind) :: &
     171             :          n                  ! thickness category index
     172             : 
     173             :       ! ice and snow albedo for each category
     174             : 
     175             :       real (kind=dbl_kind) :: &
     176       52566 :          alvdrni, & ! visible, direct, ice    (fraction)
     177       52566 :          alidrni, & ! near-ir, direct, ice    (fraction)
     178       52566 :          alvdfni, & ! visible, diffuse, ice   (fraction)
     179       52566 :          alidfni, & ! near-ir, diffuse, ice   (fraction)
     180       52566 :          alvdrns, & ! visible, direct, snow   (fraction)
     181       52566 :          alidrns, & ! near-ir, direct, snow   (fraction)
     182       52566 :          alvdfns, & ! visible, diffuse, snow  (fraction)
     183       52566 :          alidfns    ! near-ir, diffuse, snow  (fraction)
     184             : 
     185             :       real (kind=dbl_kind), dimension(:), allocatable :: &
     186      144810 :          l_fswthru_vdr  , & ! vis dir SW through ice to ocean (W m-2)
     187      144810 :          l_fswthru_vdf  , & ! vis dif SW through ice to ocean (W m-2)
     188      144810 :          l_fswthru_idr  , & ! nir dir SW through ice to ocean (W m-2)
     189      144810 :          l_fswthru_idf      ! nir dif SW through ice to ocean (W m-2)
     190             : 
     191             :       character(len=*),parameter :: subname='(shortwave_ccsm3)'
     192             : 
     193             :       !-----------------------------------------------------------------
     194             :       ! Solar radiation: albedo and absorbed shortwave
     195             :       !-----------------------------------------------------------------
     196             : 
     197      144810 :       allocate(l_fswthru_vdr(ncat))
     198      144810 :       allocate(l_fswthru_vdf(ncat))
     199      144810 :       allocate(l_fswthru_idr(ncat))
     200      144810 :       allocate(l_fswthru_idf(ncat))
     201             : 
     202             :       ! For basic shortwave, set coszen to a constant between 0 and 1.
     203      144810 :       coszen = p5 ! sun above the horizon
     204             : 
     205      868860 :       do n = 1, ncat
     206             : 
     207     1448100 :          Sswabs(:,n) = c0
     208             : 
     209      724050 :       alvdrni = albocn
     210      724050 :       alidrni = albocn
     211      724050 :       alvdfni = albocn
     212      724050 :       alidfni = albocn
     213             :       
     214      724050 :       alvdrns = albocn
     215      724050 :       alidrns = albocn
     216      724050 :       alvdfns = albocn
     217      724050 :       alidfns = albocn
     218             :       
     219      724050 :       alvdrn(n) = albocn
     220      724050 :       alidrn(n) = albocn
     221      724050 :       alvdfn(n) = albocn
     222      724050 :       alidfn(n) = albocn
     223             :       
     224      724050 :       albin(n) = c0
     225      724050 :       albsn(n) = c0    
     226             : 
     227      724050 :       fswsfc(n)  = c0
     228      724050 :       fswint(n)  = c0
     229      724050 :       fswthru(n) = c0
     230     4344300 :       fswpenl(:,n)  = c0
     231     3620250 :       Iswabs (:,n) = c0
     232             : 
     233      868860 :       if (aicen(n) > puny) then
     234             : 
     235             :       !-----------------------------------------------------------------
     236             :       ! Compute albedos for ice and snow.
     237             :       !-----------------------------------------------------------------
     238             : 
     239      591299 :          if (trim(albedo_type) == 'constant') then
     240             : 
     241       81020 :             call constant_albedos (aicen(n),             &
     242       81020 :                                    vsnon(n),             &
     243       81020 :                                    Tsfcn(n),             &
     244             :                                    alvdrni,    alidrni,  &
     245             :                                    alvdfni,    alidfni,  &
     246             :                                    alvdrns,    alidrns,  &
     247             :                                    alvdfns,    alidfns,  &
     248       81020 :                                    alvdrn(n),            &
     249       81020 :                                    alidrn(n),            &
     250       81020 :                                    alvdfn(n),            &
     251       81020 :                                    alidfn(n),            &
     252       81020 :                                    albin(n),             &
     253      230918 :                                    albsn(n))
     254      230918 :             if (icepack_warnings_aborted(subname)) return
     255             : 
     256      360381 :          elseif (trim(albedo_type) == 'ccsm3') then
     257             : 
     258      130625 :             call compute_albedos (aicen(n),             &
     259      130625 :                                   vicen(n),             &
     260      130625 :                                   vsnon(n),             &
     261      130625 :                                   Tsfcn(n),             &
     262             :                                   albicev,    albicei,  &
     263             :                                   albsnowv,   albsnowi, &
     264             :                                   ahmax,                &
     265             :                                   alvdrni,    alidrni,  &
     266             :                                   alvdfni,    alidfni,  &
     267             :                                   alvdrns,    alidrns,  &
     268             :                                   alvdfns,    alidfns,  &
     269      130625 :                                   alvdrn(n),            &
     270      130625 :                                   alidrn(n),            &
     271      130625 :                                   alvdfn(n),            &
     272      130625 :                                   alidfn(n),            &
     273      130625 :                                   albin(n),             &
     274      360381 :                                   albsn(n))
     275      360381 :             if (icepack_warnings_aborted(subname)) return
     276             : 
     277             :          else
     278             : 
     279           0 :             call icepack_warnings_add(subname//' ERROR: albedo_type '//trim(albedo_type)//' unknown')
     280           0 :             call icepack_warnings_setabort(.true.,__FILE__,__LINE__)
     281           0 :             return
     282             : 
     283             :          endif
     284             : 
     285             :       !-----------------------------------------------------------------
     286             :       ! Compute solar radiation absorbed in ice and penetrating to ocean.
     287             :       !-----------------------------------------------------------------
     288             : 
     289             :          call absorbed_solar  (heat_capacity,        &
     290             :                                nilyr,                &
     291      211645 :                                aicen(n),             &
     292      211645 :                                vicen(n),             &
     293      211645 :                                vsnon(n),             &
     294             :                                swvdr,      swvdf,    &
     295             :                                swidr,      swidf,    &
     296             :                                alvdrni,    alvdfni,  &
     297             :                                alidrni,    alidfni,  &
     298             :                                alvdrns,    alvdfns,  &
     299             :                                alidrns,    alidfns,  &
     300      211645 :                                fswsfc=fswsfc(n),     &
     301      211645 :                                fswint=fswint(n),     &
     302      211645 :                                fswthru=fswthru(n),   &
     303           0 :                                fswthru_vdr=l_fswthru_vdr(n),&
     304           0 :                                fswthru_vdf=l_fswthru_vdf(n),&
     305           0 :                                fswthru_idr=l_fswthru_idr(n),&
     306           0 :                                fswthru_idf=l_fswthru_idf(n),&
     307           0 :                                fswpenl=fswpenl(:,n), &
     308      591299 :                                Iswabs=Iswabs(:,n))
     309             : 
     310      591299 :          if (icepack_warnings_aborted(subname)) return
     311             : 
     312             :       endif ! aicen > puny
     313             : 
     314             :       enddo                  ! ncat
     315             : 
     316      868860 :       if(present(fswthru_vdr)) fswthru_vdr = l_fswthru_vdr
     317      868860 :       if(present(fswthru_vdf)) fswthru_vdf = l_fswthru_vdf
     318      868860 :       if(present(fswthru_idr)) fswthru_idr = l_fswthru_idr
     319      868860 :       if(present(fswthru_idf)) fswthru_idf = l_fswthru_idf
     320             : 
     321      144810 :       deallocate(l_fswthru_vdr)
     322      144810 :       deallocate(l_fswthru_vdf)
     323      144810 :       deallocate(l_fswthru_idr)
     324      144810 :       deallocate(l_fswthru_idf)
     325             : 
     326      144810 :       end subroutine shortwave_ccsm3
     327             : 
     328             : !=======================================================================
     329             : !
     330             : ! Compute albedos for each thickness category
     331             : 
     332      360381 :       subroutine compute_albedos (aicen,    vicen,    &
     333             :                                   vsnon,    Tsfcn,    &
     334             :                                   albicev,  albicei,  &
     335             :                                   albsnowv, albsnowi, &
     336             :                                   ahmax,              &
     337             :                                   alvdrni,  alidrni,  &
     338             :                                   alvdfni,  alidfni,  &
     339             :                                   alvdrns,  alidrns,  &
     340             :                                   alvdfns,  alidfns,  &
     341             :                                   alvdrn,   alidrn,   &
     342             :                                   alvdfn,   alidfn,   &
     343             :                                   albin,    albsn)
     344             : 
     345             :       real (kind=dbl_kind), intent(in) :: &
     346             :          aicen   , & ! concentration of ice per category
     347             :          vicen   , & ! volume of ice per category
     348             :          vsnon   , & ! volume of ice per category
     349             :          Tsfcn       ! surface temperature
     350             : 
     351             :       ! baseline albedos for ccsm3 shortwave, set in namelist
     352             :       real (kind=dbl_kind), intent(in) :: &
     353             :          albicev , & ! visible ice albedo for h > ahmax
     354             :          albicei , & ! near-ir ice albedo for h > ahmax
     355             :          albsnowv, & ! cold snow albedo, visible
     356             :          albsnowi, & ! cold snow albedo, near IR
     357             :          ahmax       ! thickness above which ice albedo is constant (m)
     358             : 
     359             :       real (kind=dbl_kind), intent(out) :: &
     360             :          alvdrni  , & ! visible, direct, ice   (fraction)
     361             :          alidrni  , & ! near-ir, direct, ice   (fraction)
     362             :          alvdfni  , & ! visible, diffuse, ice  (fraction)
     363             :          alidfni  , & ! near-ir, diffuse, ice  (fraction)
     364             :          alvdrns  , & ! visible, direct, snow  (fraction)
     365             :          alidrns  , & ! near-ir, direct, snow  (fraction)
     366             :          alvdfns  , & ! visible, diffuse, snow (fraction)
     367             :          alidfns  , & ! near-ir, diffuse, snow (fraction)
     368             :          alvdrn   , & ! visible, direct, avg   (fraction)
     369             :          alidrn   , & ! near-ir, direct, avg   (fraction)
     370             :          alvdfn   , & ! visible, diffuse, avg  (fraction)
     371             :          alidfn   , & ! near-ir, diffuse, avg  (fraction)
     372             :          albin    , & ! bare ice 
     373             :          albsn        ! snow 
     374             : 
     375             :       ! local variables
     376             : 
     377             :       real (kind=dbl_kind), parameter :: &
     378             :          dT_melt    = c1          , & ! change in temp to give dalb_mlt 
     379             :                                      ! albedo change
     380             :          dalb_mlt  = -0.075_dbl_kind, & ! albedo change per dT_melt change
     381             :                                      ! in temp for ice
     382             :          dalb_mltv = -p1         , & ! albedo vis change per dT_melt change
     383             :                                      ! in temp for snow
     384             :          dalb_mlti = -p15            ! albedo nir change per dT_melt change
     385             :                                      ! in temp for snow
     386             : 
     387             :       real (kind=dbl_kind) :: &
     388      130625 :          hi  , & ! ice thickness  (m)
     389      130625 :          hs  , & ! snow thickness  (m)
     390      130625 :          albo, & ! effective ocean albedo, function of ice thickness
     391      130625 :          fh  , & ! piecewise linear function of thickness
     392      130625 :          fT  , & ! piecewise linear function of surface temperature
     393      130625 :          dTs , & ! difference of Tsfc and Timelt
     394      130625 :          fhtan,& ! factor used in albedo dependence on ice thickness
     395      130625 :          asnow   ! fractional area of snow cover
     396             : 
     397             :       character(len=*),parameter :: subname='(compute_albedos)'
     398             : 
     399      360381 :       fhtan = atan(ahmax*c4)
     400             : 
     401             :       !-----------------------------------------------------------------
     402             :       ! Compute albedo for each thickness category.
     403             :       !-----------------------------------------------------------------
     404             : 
     405      360381 :          hi = vicen / aicen
     406      360381 :          hs = vsnon / aicen            
     407             : 
     408             :          ! bare ice, thickness dependence
     409      360381 :          fh = min(atan(hi*c4)/fhtan,c1)
     410      360381 :          albo = albocn*(c1-fh)
     411      360381 :          alvdfni = albicev*fh + albo
     412      360381 :          alidfni = albicei*fh + albo
     413             : 
     414             :          ! bare ice, temperature dependence
     415      360381 :          dTs = Timelt - Tsfcn
     416      360381 :          fT = min(dTs/dT_melt-c1,c0)
     417      360381 :          alvdfni = alvdfni - dalb_mlt*fT
     418      360381 :          alidfni = alidfni - dalb_mlt*fT
     419             : 
     420             :          ! avoid negative albedos for thin, bare, melting ice
     421      360381 :          alvdfni = max (alvdfni, albocn)
     422      360381 :          alidfni = max (alidfni, albocn)
     423             : 
     424      360381 :          if (hs > puny) then
     425             : 
     426      341291 :             alvdfns = albsnowv
     427      341291 :             alidfns = albsnowi
     428             : 
     429             :             ! snow on ice, temperature dependence
     430      341291 :             alvdfns = alvdfns - dalb_mltv*fT
     431      341291 :             alidfns = alidfns - dalb_mlti*fT
     432             : 
     433             :          endif                  ! hs > puny
     434             : 
     435             :          ! direct albedos (same as diffuse for now)
     436      360381 :          alvdrni = alvdfni
     437      360381 :          alidrni = alidfni
     438      360381 :          alvdrns = alvdfns
     439      360381 :          alidrns = alidfns
     440             : 
     441             :          ! fractional area of snow cover
     442      360381 :          if (hs > puny) then
     443      341291 :             asnow = hs / (hs + snowpatch)
     444             :          else
     445       19090 :             asnow = c0
     446             :          endif
     447             : 
     448             :          ! combine ice and snow albedos (for coupler)
     449             :          alvdfn = alvdfni*(c1-asnow) + &
     450      360381 :                   alvdfns*asnow
     451             :          alidfn = alidfni*(c1-asnow) + &
     452      360381 :                   alidfns*asnow
     453             :          alvdrn = alvdrni*(c1-asnow) + &
     454      360381 :                   alvdrns*asnow
     455             :          alidrn = alidrni*(c1-asnow) + &
     456      360381 :                   alidrns*asnow
     457             : 
     458             :          ! save ice and snow albedos (for history)
     459             :          albin = awtvdr*alvdrni + awtidr*alidrni &
     460      360381 :                + awtvdf*alvdfni + awtidf*alidfni 
     461             :          albsn = awtvdr*alvdrns + awtidr*alidrns &
     462      360381 :                + awtvdf*alvdfns + awtidf*alidfns 
     463             : 
     464      360381 :       end subroutine compute_albedos
     465             : 
     466             : !=======================================================================
     467             : !
     468             : ! Compute albedos for each thickness category
     469             : 
     470      230918 :       subroutine constant_albedos (aicen,              &
     471             :                                    vsnon,    Tsfcn,    &
     472             :                                    alvdrni,  alidrni,  &
     473             :                                    alvdfni,  alidfni,  &
     474             :                                    alvdrns,  alidrns,  &
     475             :                                    alvdfns,  alidfns,  &
     476             :                                    alvdrn,   alidrn,   &
     477             :                                    alvdfn,   alidfn,   &
     478             :                                    albin,    albsn)
     479             : 
     480             :       real (kind=dbl_kind), intent(in) :: &
     481             :          aicen   , & ! concentration of ice per category
     482             :          vsnon   , & ! volume of ice per category
     483             :          Tsfcn       ! surface temperature
     484             : 
     485             :       real (kind=dbl_kind), intent(out) :: &
     486             :          alvdrni  , & ! visible, direct, ice   (fraction)
     487             :          alidrni  , & ! near-ir, direct, ice   (fraction)
     488             :          alvdfni  , & ! visible, diffuse, ice  (fraction)
     489             :          alidfni  , & ! near-ir, diffuse, ice  (fraction)
     490             :          alvdrns  , & ! visible, direct, snow  (fraction)
     491             :          alidrns  , & ! near-ir, direct, snow  (fraction)
     492             :          alvdfns  , & ! visible, diffuse, snow (fraction)
     493             :          alidfns  , & ! near-ir, diffuse, snow (fraction)
     494             :          alvdrn   , & ! visible, direct, avg   (fraction)
     495             :          alidrn   , & ! near-ir, direct, avg   (fraction)
     496             :          alvdfn   , & ! visible, diffuse, avg  (fraction)
     497             :          alidfn   , & ! near-ir, diffuse, avg  (fraction)
     498             :          albin    , & ! bare ice 
     499             :          albsn        ! snow 
     500             : 
     501             :       ! local variables
     502             : 
     503             :       real (kind=dbl_kind), parameter :: &
     504             :          warmice  = 0.68_dbl_kind, &
     505             :          coldice  = 0.70_dbl_kind, &
     506             :          warmsnow = 0.77_dbl_kind, &
     507             :          coldsnow = 0.81_dbl_kind
     508             : 
     509             :       real (kind=dbl_kind) :: &
     510       81020 :          hs      ! snow thickness  (m)
     511             : 
     512             :       character(len=*),parameter :: subname='(constant_albedos)'
     513             : 
     514             :       !-----------------------------------------------------------------
     515             :       ! Compute albedo for each thickness category.
     516             :       !-----------------------------------------------------------------
     517             : 
     518      230918 :          hs = vsnon / aicen
     519             : 
     520      230918 :          if (hs > puny) then
     521             :             ! snow, temperature dependence
     522       95564 :             if (Tsfcn >= -c2*puny) then
     523           0 :                alvdfn = warmsnow
     524           0 :                alidfn = warmsnow
     525             :             else
     526       95564 :                alvdfn = coldsnow
     527       95564 :                alidfn = coldsnow
     528             :             endif
     529             :          else      ! hs < puny
     530             :             ! bare ice, temperature dependence
     531      135354 :             if (Tsfcn >= -c2*puny) then
     532           0 :                alvdfn = warmice
     533           0 :                alidfn = warmice
     534             :             else
     535      135354 :                alvdfn = coldice
     536      135354 :                alidfn = coldice
     537             :             endif
     538             :          endif                  ! hs > puny
     539             : 
     540             :          ! direct albedos (same as diffuse for now)
     541      230918 :          alvdrn  = alvdfn
     542      230918 :          alidrn  = alidfn
     543             : 
     544      230918 :          alvdrni = alvdrn
     545      230918 :          alidrni = alidrn
     546      230918 :          alvdrns = alvdrn
     547      230918 :          alidrns = alidrn
     548      230918 :          alvdfni = alvdfn
     549      230918 :          alidfni = alidfn
     550      230918 :          alvdfns = alvdfn
     551      230918 :          alidfns = alidfn
     552             : 
     553             :          ! save ice and snow albedos (for history)
     554             :          albin = awtvdr*alvdrni + awtidr*alidrni &
     555      230918 :                + awtvdf*alvdfni + awtidf*alidfni 
     556             :          albsn = awtvdr*alvdrns + awtidr*alidrns &
     557      230918 :                + awtvdf*alvdfns + awtidf*alidfns 
     558             : 
     559      230918 :       end subroutine constant_albedos
     560             : 
     561             : !=======================================================================
     562             : !
     563             : ! Compute solar radiation absorbed in ice and penetrating to ocean
     564             : !
     565             : ! authors William H. Lipscomb, LANL
     566             : !         C. M. Bitz, UW
     567             : 
     568      591299 :       subroutine absorbed_solar (heat_capacity,      &
     569             :                                  nilyr,    aicen,    &
     570             :                                  vicen,    vsnon,    &
     571             :                                  swvdr,    swvdf,    &
     572             :                                  swidr,    swidf,    &
     573             :                                  alvdrni,  alvdfni,  &
     574             :                                  alidrni,  alidfni,  &
     575             :                                  alvdrns,  alvdfns,  &
     576             :                                  alidrns,  alidfns,  &
     577             :                                  fswsfc,   fswint,   &
     578             :                                  fswthru,            &
     579             :                                  fswthru_vdr,        &
     580             :                                  fswthru_vdf,        &
     581             :                                  fswthru_idr,        &
     582             :                                  fswthru_idf,        &
     583      591299 :                                  fswpenl,            &
     584      591299 :                                  Iswabs)
     585             : 
     586             :       logical(kind=log_kind), intent(in) :: &
     587             :          heat_capacity   ! if true, ice has nonzero heat capacity
     588             : 
     589             :       integer (kind=int_kind), intent(in) :: & 
     590             :          nilyr           ! number of ice layers
     591             : 
     592             :       real (kind=dbl_kind), intent(in) :: &
     593             :          aicen       , & ! fractional ice area
     594             :          vicen       , & ! ice volume
     595             :          vsnon       , & ! snow volume
     596             :          swvdr       , & ! sw down, visible, direct  (W/m^2)
     597             :          swvdf       , & ! sw down, visible, diffuse (W/m^2)
     598             :          swidr       , & ! sw down, near IR, direct  (W/m^2)
     599             :          swidf       , & ! sw down, near IR, diffuse (W/m^2)
     600             :          alvdrni     , & ! visible, direct albedo,ice
     601             :          alidrni     , & ! near-ir, direct albedo,ice
     602             :          alvdfni     , & ! visible, diffuse albedo,ice
     603             :          alidfni     , & ! near-ir, diffuse albedo,ice
     604             :          alvdrns     , & ! visible, direct albedo, snow
     605             :          alidrns     , & ! near-ir, direct albedo, snow
     606             :          alvdfns     , & ! visible, diffuse albedo, snow
     607             :          alidfns         ! near-ir, diffuse albedo, snow
     608             : 
     609             :       real (kind=dbl_kind), intent(out):: &
     610             :          fswsfc      , & ! SW absorbed at ice/snow surface (W m-2)
     611             :          fswint      , & ! SW absorbed in ice interior, below surface (W m-2)
     612             :          fswthru         ! SW through ice to ocean (W m-2)
     613             : 
     614             :       real (kind=dbl_kind), intent(out) :: &
     615             :          fswthru_vdr  , & ! vis dir SW through ice to ocean (W m-2)
     616             :          fswthru_vdf  , & ! vis dif SW through ice to ocean (W m-2)
     617             :          fswthru_idr  , & ! nir dir SW through ice to ocean (W m-2)
     618             :          fswthru_idf      ! nir dif SW through ice to ocean (W m-2)
     619             : 
     620             :       real (kind=dbl_kind), dimension (:), intent(out) :: &
     621             :          Iswabs      , & ! SW absorbed in particular layer (W m-2)
     622             :          fswpenl         ! visible SW entering ice layers (W m-2)
     623             : 
     624             :       ! local variables
     625             : 
     626             :       real (kind=dbl_kind), parameter :: &
     627             :          i0vis = 0.70_dbl_kind  ! fraction of penetrating solar rad (visible)
     628             : 
     629             :       integer (kind=int_kind) :: &
     630             :          k               ! ice layer index
     631             : 
     632             :       real (kind=dbl_kind) :: &
     633      211645 :          fswpen      , & ! SW penetrating beneath surface (W m-2)
     634      211645 :          trantop     , & ! transmitted frac of penetrating SW at layer top
     635      211645 :          tranbot         ! transmitted frac of penetrating SW at layer bot
     636             : 
     637             :       real (kind=dbl_kind) :: &
     638      211645 :          swabs       , & ! net SW down at surface (W m-2)
     639      211645 :          swabsv      , & ! swabs in vis (wvlngth < 700nm)  (W/m^2)
     640      211645 :          swabsi      , & ! swabs in nir (wvlngth > 700nm)  (W/m^2)
     641      211645 :          fswpenvdr   , & ! penetrating SW, vis direct
     642      211645 :          fswpenvdf   , & ! penetrating SW, vis diffuse
     643      211645 :          hi          , & ! ice thickness (m)
     644      211645 :          hs          , & ! snow thickness (m)
     645      211645 :          hilyr       , & ! ice layer thickness
     646      211645 :          asnow           ! fractional area of snow cover
     647             : 
     648             :       character(len=*),parameter :: subname='(absorbed_solar)'
     649             : 
     650             :       !-----------------------------------------------------------------
     651             :       ! Initialize
     652             :       !-----------------------------------------------------------------
     653             : 
     654      591299 :       trantop = c0
     655      591299 :       tranbot = c0
     656             : 
     657      591299 :          hs  = vsnon / aicen
     658             : 
     659             :       !-----------------------------------------------------------------
     660             :       ! Fractional snow cover
     661             :       !-----------------------------------------------------------------
     662      591299 :          if (hs > puny) then
     663      436855 :             asnow = hs / (hs + snowpatch)
     664             :          else
     665      154444 :             asnow = c0
     666             :          endif
     667             : 
     668             :       !-----------------------------------------------------------------
     669             :       ! Shortwave flux absorbed at surface, absorbed internally,
     670             :       !  and penetrating to mixed layer.
     671             :       ! This parameterization assumes that all IR is absorbed at the
     672             :       !  surface; only visible is absorbed in the ice interior or
     673             :       !  transmitted to the ocean.
     674             :       !-----------------------------------------------------------------
     675             : 
     676             :          swabsv  = swvdr * ( (c1-alvdrni)*(c1-asnow) &
     677             :                            + (c1-alvdrns)*asnow ) &
     678             :                  + swvdf * ( (c1-alvdfni)*(c1-asnow) &
     679      591299 :                            + (c1-alvdfns)*asnow )
     680             : 
     681             :          swabsi  = swidr * ( (c1-alidrni)*(c1-asnow) &
     682             :                            + (c1-alidrns)*asnow ) &
     683             :                  + swidf * ( (c1-alidfni)*(c1-asnow) &
     684      591299 :                            + (c1-alidfns)*asnow )
     685             : 
     686      591299 :          swabs   = swabsv + swabsi
     687             : 
     688      591299 :          fswpenvdr = swvdr * (c1-alvdrni) * (c1-asnow) * i0vis
     689      591299 :          fswpenvdf = swvdf * (c1-alvdfni) * (c1-asnow) * i0vis
     690             : 
     691             :           ! no penetrating radiation in near IR
     692             : !         fswpenidr = swidr * (c1-alidrni) * (c1-asnow) * i0nir
     693             : !         fswpenidf = swidf * (c1-alidfni) * (c1-asnow) * i0nir  
     694             : 
     695      591299 :          fswpen = fswpenvdr + fswpenvdf
     696             :                       
     697      591299 :          fswsfc = swabs - fswpen
     698             : 
     699      591299 :          trantop = c1  ! transmittance at top of ice
     700             : 
     701             :       !-----------------------------------------------------------------
     702             :       ! penetrating SW absorbed in each ice layer
     703             :       !-----------------------------------------------------------------
     704             : 
     705     3344884 :          do k = 1, nilyr
     706             : 
     707     2753585 :             hi  = vicen / aicen
     708     2753585 :             hilyr = hi / real(nilyr,kind=dbl_kind)
     709             : 
     710     2753585 :             tranbot = exp (-kappav * hilyr * real(k,kind=dbl_kind))
     711     2753585 :             Iswabs(k) = fswpen * (trantop-tranbot)
     712             : 
     713             :             ! bottom of layer k = top of layer k+1
     714     2753585 :             trantop = tranbot
     715             : 
     716             :             ! bgc layer model
     717     3344884 :             if (k == 1) then   ! surface flux
     718      591299 :                fswpenl(k)   = fswpen
     719      591299 :                fswpenl(k+1) = fswpen * tranbot
     720             :             else
     721     2162286 :                fswpenl(k+1) = fswpen * tranbot
     722             :             endif
     723             :          enddo                     ! nilyr
     724             : 
     725             :          ! SW penetrating thru ice into ocean
     726      591299 :          fswthru = fswpen * tranbot
     727      591299 :          fswthru_vdr = fswpenvdr * tranbot
     728      591299 :          fswthru_vdf = fswpenvdf * tranbot
     729      591299 :          fswthru_idr = c0
     730      591299 :          fswthru_idf = c0
     731             : 
     732             :          ! SW absorbed in ice interior
     733      591299 :          fswint  = fswpen - fswthru
     734             : 
     735             :       !----------------------------------------------------------------
     736             :       ! if zero-layer model (no heat capacity), no SW is absorbed in ice
     737             :       ! interior, so add to surface absorption
     738             :       !----------------------------------------------------------------
     739             :          
     740      591299 :          if (.not. heat_capacity) then
     741             : 
     742             :             ! SW absorbed at snow/ice surface
     743      230918 :             fswsfc = fswsfc + fswint
     744             : 
     745             :             ! SW absorbed in ice interior (nilyr = 1)
     746      230918 :             fswint    = c0
     747      230918 :             Iswabs(1) = c0
     748             : 
     749             :          endif                       ! heat_capacity
     750             : 
     751      591299 :       end subroutine absorbed_solar
     752             : 
     753             : ! End ccsm3 shortwave method
     754             : !=======================================================================
     755             : ! Begin Delta-Eddington shortwave method
     756             : 
     757             : ! Compute initial data for Delta-Eddington method, specifically, 
     758             : ! the approximate exponential look-up table.
     759             : !
     760             : ! author:  Bruce P. Briegleb, NCAR
     761             : ! 2011 ECH modified for melt pond tracers
     762             : ! 2013 ECH merged with NCAR version
     763             : 
     764      882945 :       subroutine run_dEdd(dt,       ncat,      &
     765             :                           dEdd_algae,          &
     766             :                           nilyr,    nslyr,     &
     767     1765890 :                           aicen,    vicen,     &
     768      882945 :                           vsnon,    Tsfcn,     &
     769     1765890 :                           alvln,    apndn,     &
     770     1765890 :                           hpndn,    ipndn,     &
     771      882945 :                           aeron,    kalg,      &
     772      882945 :                           trcrn_bgcsw,         &
     773             :                           heat_capacity,       &
     774             :                           tlat,     tlon,      & 
     775           0 :                           calendar_type,       &
     776             :                           days_per_year,       &
     777             :                           nextsw_cday,   yday, &
     778             :                           sec,      R_ice,     &
     779             :                           R_pnd,    R_snw,     &
     780             :                           dT_mlt,   rsnw_mlt,  &
     781             :                           hs0,      hs1,  hp1, &
     782             :                           pndaspect,           &
     783      882945 :                           kaer_tab, waer_tab,  &
     784      882945 :                           gaer_tab,            &
     785      882945 :                           kaer_bc_tab,         &
     786      882945 :                           waer_bc_tab,         &
     787      882945 :                           gaer_bc_tab,         &
     788      882945 :                           bcenh,               &
     789             :                           modal_aero,          &
     790             :                           swvdr,    swvdf,     &
     791             :                           swidr,    swidf,     &
     792             :                           coszen,   fsnow,     &
     793      882945 :                           alvdrn,   alvdfn,    &
     794      882945 :                           alidrn,   alidfn,    &
     795      882945 :                           fswsfcn,  fswintn,   &
     796      882945 :                           fswthrun,            &
     797      882945 :                           fswthrun_vdr,        &
     798      882945 :                           fswthrun_vdf,        &
     799      882945 :                           fswthrun_idr,        &
     800      882945 :                           fswthrun_idf,        &
     801      882945 :                           fswpenln,            &
     802      882945 :                           Sswabsn,  Iswabsn,   &
     803      882945 :                           albicen,  albsnon,   &
     804     1765890 :                           albpndn,  apeffn,    &
     805      882945 :                           snowfracn,           &
     806      882945 :                           dhsn,     ffracn,    &
     807             :                           l_print_point,       &
     808             :                           initonly)
     809             : 
     810             :       integer (kind=int_kind), intent(in) :: &
     811             :          ncat   , & ! number of ice thickness categories
     812             :          nilyr  , & ! number of ice layers
     813             :          nslyr      ! number of snow layers
     814             : 
     815             :       logical(kind=log_kind), intent(in) :: &
     816             :          heat_capacity,& ! if true, ice has nonzero heat capacity
     817             :          dEdd_algae,   & ! .true. use prognostic chla in dEdd
     818             :          modal_aero      ! .true. use modal aerosol treatment
     819             : 
     820             :       ! dEdd tuning parameters, set in namelist
     821             :       real (kind=dbl_kind), intent(in) :: &
     822             :          R_ice , & ! sea ice tuning parameter; +1 > 1sig increase in albedo
     823             :          R_pnd , & ! ponded ice tuning parameter; +1 > 1sig increase in albedo
     824             :          R_snw , & ! snow tuning parameter; +1 > ~.01 change in broadband albedo
     825             :          dT_mlt, & ! change in temp for non-melt to melt snow grain radius change (C)
     826             :          rsnw_mlt, & ! maximum melting snow grain radius (10^-6 m)
     827             :          hs0      , & ! snow depth for transition to bare sea ice (m)
     828             :          pndaspect, & ! ratio of pond depth to pond fraction
     829             :          hs1      , & ! tapering parameter for snow on pond ice
     830             :          hp1      , & ! critical parameter for pond ice thickness
     831             :          kalg         ! algae absorption coefficient
     832             : 
     833             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & 
     834             :          kaer_tab, & ! aerosol mass extinction cross section (m2/kg)
     835             :          waer_tab, & ! aerosol single scatter albedo (fraction)
     836             :          gaer_tab    ! aerosol asymmetry parameter (cos(theta))
     837             :    
     838             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & ! Modal aerosol treatment
     839             :          kaer_bc_tab, & ! aerosol mass extinction cross section (m2/kg)
     840             :          waer_bc_tab, & ! aerosol single scatter albedo (fraction)
     841             :          gaer_bc_tab    ! aerosol asymmetry parameter (cos(theta))
     842             :    
     843             :       real (kind=dbl_kind), dimension(:,:,:), intent(in) :: & ! Modal aerosol treatment
     844             :          bcenh          ! BC absorption enhancement factor
     845             : 
     846             :       character (len=char_len), intent(in) :: &
     847             :          calendar_type       ! differentiates Gregorian from other calendars
     848             : 
     849             :       integer (kind=int_kind), intent(in) :: &
     850             :          days_per_year, &    ! number of days in one year
     851             :          sec                 ! elapsed seconds into date
     852             : 
     853             :       real (kind=dbl_kind), intent(in) :: &
     854             :          nextsw_cday     , & ! julian day of next shortwave calculation
     855             :          yday                ! day of the year
     856             : 
     857             :       real(kind=dbl_kind), intent(in) :: &
     858             :            dt,    & ! time step (s)
     859             :            tlat,  & ! latitude of temp pts (radians)
     860             :            tlon,  & ! longitude of temp pts (radians)
     861             :            swvdr, & ! sw down, visible, direct  (W/m^2)
     862             :            swvdf, & ! sw down, visible, diffuse (W/m^2)
     863             :            swidr, & ! sw down, near IR, direct  (W/m^2)
     864             :            swidf, & ! sw down, near IR, diffuse (W/m^2)
     865             :            fsnow    ! snowfall rate (kg/m^2 s)
     866             : 
     867             :       real(kind=dbl_kind), dimension(:), intent(in) :: &
     868             :            aicen, & ! concentration of ice
     869             :            vicen, & ! volume per unit area of ice (m)
     870             :            vsnon, & ! volume per unit area of snow (m)
     871             :            Tsfcn, & ! surface temperature (deg C)
     872             :            alvln, & ! level-ice area fraction
     873             :            apndn, & ! pond area fraction
     874             :            hpndn, & ! pond depth (m)
     875             :            ipndn    ! pond refrozen lid thickness (m)
     876             : 
     877             :       real(kind=dbl_kind), dimension(:,:), intent(in) :: &
     878             :            aeron, & ! aerosols (kg/m^3)
     879             :            trcrn_bgcsw ! zaerosols (kg/m^3) + chlorophyll on shorthwave grid
     880             : 
     881             :       real(kind=dbl_kind), dimension(:), intent(inout) :: &
     882             :            ffracn,& ! fraction of fsurfn used to melt ipond
     883             :            dhsn     ! depth difference for snow on sea ice and pond ice
     884             : 
     885             :       real(kind=dbl_kind), intent(inout) :: &
     886             :            coszen   ! cosine solar zenith angle, < 0 for sun below horizon 
     887             : 
     888             :       real(kind=dbl_kind), dimension(:), intent(inout) :: &
     889             :            alvdrn,   & ! visible direct albedo (fraction)
     890             :            alvdfn,   & ! near-ir direct albedo (fraction)
     891             :            alidrn,   & ! visible diffuse albedo (fraction)
     892             :            alidfn,   & ! near-ir diffuse albedo (fraction)
     893             :            fswsfcn,  & ! SW absorbed at ice/snow surface (W m-2)
     894             :            fswintn,  & ! SW absorbed in ice interior, below surface (W m-2)
     895             :            fswthrun, & ! SW through ice to ocean (W/m^2) 
     896             :            albicen,  & ! albedo bare ice 
     897             :            albsnon,  & ! albedo snow 
     898             :            albpndn,  & ! albedo pond 
     899             :            apeffn,   & ! effective pond area used for radiation calculation
     900             :            snowfracn   ! snow fraction on each category used for radiation
     901             : 
     902             :       real(kind=dbl_kind), dimension(:), intent(out), optional :: &
     903             :            fswthrun_vdr, & ! vis dir SW through ice to ocean (W/m^2) 
     904             :            fswthrun_vdf, & ! vis dif SW through ice to ocean (W/m^2) 
     905             :            fswthrun_idr, & ! nir dir SW through ice to ocean (W/m^2) 
     906             :            fswthrun_idf    ! nir dif SW through ice to ocean (W/m^2) 
     907             : 
     908             :       real(kind=dbl_kind), dimension(:,:), intent(inout) :: &
     909             :            Sswabsn , & ! SW radiation absorbed in snow layers (W m-2)
     910             :            Iswabsn , & ! SW radiation absorbed in ice layers (W m-2) 
     911             :            fswpenln    ! visible SW entering ice layers (W m-2)
     912             : 
     913             :       logical (kind=log_kind), intent(in) :: &
     914             :            l_print_point
     915             : 
     916             :       logical (kind=log_kind), optional :: &
     917             :            initonly    ! flag to indicate init only, default is false
     918             : 
     919             :       ! local temporary variables
     920             : 
     921             :       ! other local variables
     922             :       ! snow variables for Delta-Eddington shortwave
     923             :       real (kind=dbl_kind) :: &
     924      303198 :          fsn         , & ! snow horizontal fraction
     925      303198 :          hsn             ! snow depth (m)
     926             : 
     927             :       real (kind=dbl_kind), dimension (nslyr) :: &
     928     1871022 :          rhosnwn     , & ! snow density (kg/m3)
     929     1871022 :          rsnwn           ! snow grain radius (micrometers)
     930             : 
     931             :       ! pond variables for Delta-Eddington shortwave
     932             :       real (kind=dbl_kind) :: &
     933      303198 :          fpn         , & ! pond fraction of ice cover
     934      303198 :          hpn             ! actual pond depth (m)
     935             : 
     936             :       integer (kind=int_kind) :: &
     937             :          n               ! thickness category index
     938             : 
     939             :       real (kind=dbl_kind) :: &
     940      303198 :          ipn         , & ! refrozen pond ice thickness (m), mean over ice fraction
     941      303198 :          hp          , & ! pond depth
     942      303198 :          hs          , & ! snow depth
     943      303198 :          asnow       , & ! fractional area of snow cover
     944      303198 :          rp          , & ! volume fraction of retained melt water to total liquid content
     945      303198 :          hmx         , & ! maximum available snow infiltration equivalent depth
     946      303198 :          dhs         , & ! local difference in snow depth on sea ice and pond ice
     947      303198 :          spn         , & ! snow depth on refrozen pond (m)
     948      303198 :          tmp             ! 0 or 1
     949             : 
     950             :       logical (kind=log_kind) :: &
     951             :          linitonly       ! local initonly value
     952             : 
     953             :       real (kind=dbl_kind), dimension(:), allocatable :: &
     954      882945 :          l_fswthrun_vdr  , & ! vis dir SW through ice to ocean (W m-2)
     955      882945 :          l_fswthrun_vdf  , & ! vis dif SW through ice to ocean (W m-2)
     956      882945 :          l_fswthrun_idr  , & ! nir dir SW through ice to ocean (W m-2)
     957      882945 :          l_fswthrun_idf      ! nir dif SW through ice to ocean (W m-2)
     958             : 
     959             :       character(len=*),parameter :: subname='(run_dEdd)'
     960             : 
     961      882945 :       allocate(l_fswthrun_vdr(ncat))
     962      882945 :       allocate(l_fswthrun_vdf(ncat))
     963      882945 :       allocate(l_fswthrun_idr(ncat))
     964      882945 :       allocate(l_fswthrun_idf(ncat))
     965             : 
     966      882945 :       linitonly = .false.
     967      882945 :       if (present(initonly)) then
     968      882945 :          linitonly = initonly
     969             :       endif
     970             : 
     971             :       ! cosine of the zenith angle
     972             : #ifdef CESMCOUPLED
     973             :       call compute_coszen (tlat,          tlon, &
     974             :                            yday,  sec, coszen,  &
     975             :                            days_per_year, nextsw_cday, calendar_type)
     976             : #else
     977             :       call compute_coszen (tlat,          tlon, &
     978      882945 :                            yday,  sec, coszen)
     979             : #endif
     980      882945 :       if (icepack_warnings_aborted(subname)) return
     981             : 
     982     5008050 :       do n = 1, ncat
     983             : 
     984             :       ! note that rhoswn, rsnw, fp, hp and Sswabs ARE NOT dimensioned with ncat
     985             :       ! BPB 19 Dec 2006
     986             : 
     987             :          ! set snow properties
     988     4125105 :          fsn        = c0
     989     4125105 :          hsn        = c0
     990     9698310 :          rhosnwn(:) = c0
     991     9698310 :          rsnwn(:)   = c0
     992     4125105 :          apeffn(n)    = c0 ! for history
     993     4125105 :          snowfracn(n) = c0 ! for history
     994             : 
     995     5008050 :          if (aicen(n) > puny) then
     996             : 
     997             :             call shortwave_dEdd_set_snow(nslyr,      R_snw,    &
     998             :                                          dT_mlt,     rsnw_mlt, &
     999     1275244 :                                          aicen(n),   vsnon(n), &
    1000     1275244 :                                          Tsfcn(n),   fsn,      &
    1001             :                                          hs0,        hsn,      &
    1002     3770075 :                                          rhosnwn,    rsnwn)    
    1003     3770075 :             if (icepack_warnings_aborted(subname)) return
    1004             : 
    1005             :             ! set pond properties
    1006     3770075 :             if (tr_pond_cesm) then
    1007             :                ! fraction of ice area
    1008      229795 :                fpn = apndn(n)
    1009             :                ! pond depth over fraction fpn 
    1010      229795 :                hpn = hpndn(n)
    1011             :                ! snow infiltration
    1012      229795 :                if (hsn >= hs_min .and. hs0 > puny) then
    1013           0 :                   asnow = min(hsn/hs0, c1) ! delta-Eddington formulation
    1014           0 :                   fpn = (c1 - asnow) * fpn
    1015           0 :                   hpn = pndaspect * fpn
    1016             :                endif
    1017             :                ! Zero out fraction of thin ponds for radiation only
    1018      229795 :                if (hpn < hpmin) fpn = c0
    1019      229795 :                fsn = min(fsn, c1-fpn)
    1020      229795 :                apeffn(n) = fpn ! for history
    1021     3540280 :             elseif (tr_pond_lvl) then
    1022     3010800 :                fpn = c0  ! fraction of ice covered in pond
    1023     3010800 :                hpn = c0  ! pond depth over fpn
    1024             :                ! refrozen pond lid thickness avg over ice
    1025             :                ! allow snow to cover pond ice
    1026     3010800 :                ipn = alvln(n) * apndn(n) * ipndn(n)
    1027     3010800 :                dhs = dhsn(n) ! snow depth difference, sea ice - pond
    1028             :                if (.not. linitonly .and. ipn > puny .and. &
    1029     3010800 :                     dhs < puny .and. fsnow*dt > hs_min) &
    1030       10898 :                     dhs = hsn - fsnow*dt ! initialize dhs>0
    1031     3010800 :                spn = hsn - dhs   ! snow depth on pond ice
    1032     3010800 :                if (.not. linitonly .and. ipn*spn < puny) dhs = c0
    1033     3010800 :                dhsn(n) = dhs ! save: constant until reset to 0
    1034             :                   
    1035             :                ! not using ipn assumes that lid ice is perfectly clear
    1036             :                ! if (ipn <= 0.3_dbl_kind) then
    1037             :                
    1038             :                ! fraction of ice area
    1039     3010800 :                fpn = apndn(n) * alvln(n) 
    1040             :                ! pond depth over fraction fpn
    1041     3010800 :                hpn = hpndn(n)
    1042             :                
    1043             :                ! reduce effective pond area absorbing surface heat flux
    1044             :                ! due to flux already having been used to melt pond ice
    1045     3010800 :                fpn = (c1 - ffracn(n)) * fpn
    1046             :                
    1047             :                ! taper pond area with snow on pond ice
    1048     3010800 :                if (dhs > puny .and. spn >= puny .and. hs1 > puny) then
    1049       18591 :                   asnow = min(spn/hs1, c1)
    1050       18591 :                   fpn = (c1 - asnow) * fpn
    1051             :                endif
    1052             :                
    1053             :                ! infiltrate snow
    1054     3010800 :                hp = hpn
    1055     3010800 :                if (hp > puny) then
    1056     1433285 :                   hs = hsn
    1057     1433285 :                   rp = rhofresh*hp/(rhofresh*hp + rhos*hs)
    1058     1433285 :                   if (rp < p15) then
    1059     1146071 :                      fpn = c0
    1060     1146071 :                      hpn = c0
    1061             :                   else
    1062      287214 :                      hmx = hs*(rhofresh - rhos)/rhofresh
    1063      287214 :                      tmp = max(c0, sign(c1, hp-hmx)) ! 1 if hp>=hmx, else 0
    1064             :                      hp = (rhofresh*hp + rhos*hs*tmp) &
    1065      287214 :                           / (rhofresh    - rhos*(c1-tmp))
    1066      287214 :                      hsn = hs - hp*fpn*(c1-tmp)
    1067      287214 :                      hpn = hp * tmp
    1068      287214 :                      fpn = fpn * tmp
    1069             :                   endif
    1070             :                endif ! hp > puny
    1071             : 
    1072             :                ! Zero out fraction of thin ponds for radiation only
    1073     3010800 :                if (hpn < hpmin) fpn = c0
    1074     3010800 :                fsn = min(fsn, c1-fpn)
    1075             : 
    1076             :                ! endif    ! masking by lid ice
    1077     3010800 :                apeffn(n) = fpn ! for history
    1078             : 
    1079      529480 :             elseif (tr_pond_topo) then
    1080             :                ! Lid effective if thicker than hp1
    1081      457081 :                if (apndn(n)*aicen(n) > puny .and. ipndn(n) < hp1) then
    1082      148413 :                   fpn = apndn(n)
    1083             :                else
    1084      308668 :                   fpn = c0
    1085             :                endif
    1086      457081 :                if (apndn(n) > puny) then
    1087      184923 :                   hpn = hpndn(n) 
    1088             :                else
    1089      272158 :                   fpn = c0
    1090      272158 :                   hpn = c0
    1091             :                endif
    1092             : 
    1093             :                ! Zero out fraction of thin ponds for radiation only
    1094      457081 :                if (hpn < hpmin) fpn = c0
    1095             : 
    1096             :                ! If ponds are present snow fraction reduced to
    1097             :                ! non-ponded part dEdd scheme 
    1098      457081 :                fsn = min(fsn, c1-fpn)
    1099             : 
    1100      457081 :                apeffn(n) = fpn
    1101             :             else
    1102       72399 :                fpn = c0
    1103       72399 :                hpn = c0
    1104       26280 :                call shortwave_dEdd_set_pond(Tsfcn(n),   &
    1105             :                                             fsn, fpn,   &
    1106       72399 :                                             hpn)
    1107       72399 :                if (icepack_warnings_aborted(subname)) return
    1108             :                
    1109       72399 :                apeffn(n) = fpn ! for history
    1110       72399 :                fpn = c0
    1111       72399 :                hpn = c0
    1112             :             endif ! pond type
    1113             :             
    1114     3770075 :          snowfracn(n) = fsn ! for history
    1115             : 
    1116             :          call shortwave_dEdd(dEdd_algae,                    &
    1117             :                              nslyr,         nilyr,          &
    1118             :                              coszen,        heat_capacity,  &
    1119     1275244 :                              aicen(n),      vicen(n),       &
    1120             :                              hsn,           fsn,            &
    1121           0 :                              rhosnwn,       rsnwn,          &
    1122             :                              fpn,           hpn,            &
    1123           0 :                              aeron(:,n),                    &
    1124             :                              R_ice,         R_pnd,          &
    1125           0 :                              kaer_tab,      waer_tab,       &
    1126           0 :                              gaer_tab,                      &
    1127           0 :                              kaer_bc_tab,                   &
    1128           0 :                              waer_bc_tab,                   &
    1129           0 :                              gaer_bc_tab,                   &
    1130           0 :                              bcenh,         modal_aero,     &   
    1131             :                              kalg,                          &
    1132             :                              swvdr,         swvdf,          &
    1133             :                              swidr,         swidf,          &
    1134     1275244 :                              alvdrn(n),     alvdfn(n),      &
    1135     1275244 :                              alidrn(n),     alidfn(n),      &
    1136     1275244 :                              fswsfcn(n),    fswintn(n),     &
    1137     1275244 :                              fswthru=fswthrun(n),           &
    1138           0 :                              fswthru_vdr=l_fswthrun_vdr(n),    &
    1139           0 :                              fswthru_vdf=l_fswthrun_vdf(n),    &
    1140           0 :                              fswthru_idr=l_fswthrun_idr(n),    &
    1141           0 :                              fswthru_idf=l_fswthrun_idf(n),    &
    1142           0 :                              Sswabs=Sswabsn(:,n),           &
    1143           0 :                              Iswabs=Iswabsn(:,n),           &
    1144     1275244 :                              albice=albicen(n),             &
    1145     1275244 :                              albsno=albsnon(n),             &
    1146     1275244 :                              albpnd=albpndn(n),             &
    1147           0 :                              fswpenl=fswpenln(:,n),         &
    1148           0 :                              zbio=trcrn_bgcsw(:,n),         &
    1149     6320563 :                              l_print_point=l_print_point)
    1150             : 
    1151     3770075 :             if (icepack_warnings_aborted(subname)) return
    1152             : 
    1153             :          endif ! aicen > puny
    1154             : 
    1155             :       enddo  ! ncat
    1156             : 
    1157     5008050 :       if(present(fswthrun_vdr)) fswthrun_vdr = l_fswthrun_vdr
    1158     5008050 :       if(present(fswthrun_vdf)) fswthrun_vdf = l_fswthrun_vdf
    1159     5008050 :       if(present(fswthrun_idr)) fswthrun_idr = l_fswthrun_idr
    1160     5008050 :       if(present(fswthrun_idf)) fswthrun_idf = l_fswthrun_idf
    1161             : 
    1162      882945 :       deallocate(l_fswthrun_vdr)
    1163      882945 :       deallocate(l_fswthrun_vdf)
    1164      882945 :       deallocate(l_fswthrun_idr)
    1165      882945 :       deallocate(l_fswthrun_idf)
    1166             : 
    1167      882945 :       end subroutine run_dEdd
    1168             :  
    1169             : !=======================================================================
    1170             : !
    1171             : !   Compute snow/bare ice/ponded ice shortwave albedos, absorbed and transmitted 
    1172             : !   flux using the Delta-Eddington solar radiation method as described in:
    1173             : !
    1174             : !   A Delta-Eddington Multiple Scattering Parameterization for Solar Radiation
    1175             : !        in the Sea Ice Component of the Community Climate System Model
    1176             : !            B.P.Briegleb and B.Light   NCAR/TN-472+STR  February 2007
    1177             : !
    1178             : !   Compute shortwave albedos and fluxes for three surface types: 
    1179             : !   snow over ice, bare ice and ponded ice. 
    1180             : !   
    1181             : !   Albedos and fluxes are output for later use by thermodynamic routines. 
    1182             : !   Invokes three calls to compute_dEdd, which sets inherent optical properties 
    1183             : !   appropriate for the surface type. Within compute_dEdd, a call to solution_dEdd 
    1184             : !   evaluates the Delta-Eddington solution. The final albedos and fluxes are then
    1185             : !   evaluated in compute_dEdd. Albedos and fluxes are transferred to output in 
    1186             : !   this routine.
    1187             : !
    1188             : !   NOTE regarding albedo diagnostics:  This method yields zero albedo values
    1189             : !   if there is no incoming solar and thus the albedo diagnostics are masked
    1190             : !   out when the sun is below the horizon.  To estimate albedo from the history 
    1191             : !   output (post-processing), compute ice albedo using
    1192             : !   (1 - albedo)*swdn = swabs. -ECH
    1193             : !
    1194             : ! author:  Bruce P. Briegleb, NCAR 
    1195             : !   2013:  E Hunke merged with NCAR version
    1196             : !
    1197     3770075 :       subroutine shortwave_dEdd  (dEdd_algae,            &
    1198             :                                   nslyr,    nilyr,       &
    1199             :                                   coszen,   heat_capacity,&
    1200             :                                   aice,     vice,        &
    1201             :                                   hs,       fs,          & 
    1202     3770075 :                                   rhosnw,   rsnw,        &
    1203             :                                   fp,       hp,          &
    1204     3770075 :                                   aero,                  &
    1205             :                                   R_ice,    R_pnd,       &
    1206     3770075 :                                   kaer_tab, waer_tab,    &
    1207     3770075 :                                   gaer_tab,              &
    1208     3770075 :                                   kaer_bc_tab,           &
    1209     3770075 :                                   waer_bc_tab,           &
    1210     3770075 :                                   gaer_bc_tab,           &
    1211     3770075 :                                   bcenh,  modal_aero,    &   
    1212             :                                   kalg,                  &
    1213             :                                   swvdr,    swvdf,       &
    1214             :                                   swidr,    swidf,       &
    1215             :                                   alvdr,    alvdf,       &
    1216             :                                   alidr,    alidf,       &
    1217             :                                   fswsfc,   fswint,      &
    1218             :                                   fswthru,               &
    1219             :                                   fswthru_vdr,           &
    1220             :                                   fswthru_vdf,           &
    1221             :                                   fswthru_idr,           &
    1222             :                                   fswthru_idf,           &
    1223     3770075 :                                   Sswabs,                &
    1224     3770075 :                                   Iswabs,   albice,      &
    1225             :                                   albsno,   albpnd,      &
    1226     7540150 :                                   fswpenl,  zbio,        &
    1227             :                                   l_print_point)
    1228             : 
    1229             :       integer (kind=int_kind), intent(in) :: &
    1230             :          nilyr   , & ! number of ice layers
    1231             :          nslyr       ! number of snow layers
    1232             : 
    1233             :       logical (kind=log_kind), intent(in) :: &
    1234             :          heat_capacity, & ! if true, ice has nonzero heat capacity
    1235             :          dEdd_algae,    & ! .true. use prognostic chla in dEdd
    1236             :          modal_aero       ! .true. use modal aerosol treatment
    1237             :  
    1238             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & ! Modal aerosol treatment
    1239             :          kaer_bc_tab, & ! aerosol mass extinction cross section (m2/kg)
    1240             :          waer_bc_tab, & ! aerosol single scatter albedo (fraction)
    1241             :          gaer_bc_tab    ! aerosol asymmetry parameter (cos(theta))
    1242             :    
    1243             :       real (kind=dbl_kind), dimension(:,:,:), intent(in) :: & ! Modal aerosol treatment
    1244             :          bcenh          ! BC absorption enhancement factor
    1245             : 
    1246             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & 
    1247             :          kaer_tab, & ! aerosol mass extinction cross section (m2/kg)
    1248             :          waer_tab, & ! aerosol single scatter albedo (fraction)
    1249             :          gaer_tab    ! aerosol asymmetry parameter (cos(theta))
    1250             : 
    1251             :       real (kind=dbl_kind), intent(in) :: &
    1252             :          kalg    , & ! algae absorption coefficient
    1253             :          R_ice , & ! sea ice tuning parameter; +1 > 1sig increase in albedo
    1254             :          R_pnd , & ! ponded ice tuning parameter; +1 > 1sig increase in albedo
    1255             :          aice    , & ! concentration of ice 
    1256             :          vice    , & ! volume of ice 
    1257             :          hs      , & ! snow depth
    1258             :          fs          ! horizontal coverage of snow
    1259             : 
    1260             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
    1261             :          rhosnw  , & ! density in snow layer (kg/m3)
    1262             :          rsnw    , & ! grain radius in snow layer (m)
    1263             :          aero    , & ! aerosol tracers
    1264             :          zbio        ! shortwave tracers (zaero+chla)
    1265             : 
    1266             :       real (kind=dbl_kind), intent(in) :: &
    1267             :          fp      , & ! pond fractional coverage (0 to 1) 
    1268             :          hp      , & ! pond depth (m) 
    1269             :          swvdr   , & ! sw down, visible, direct  (W/m^2)
    1270             :          swvdf   , & ! sw down, visible, diffuse (W/m^2)
    1271             :          swidr   , & ! sw down, near IR, direct  (W/m^2)
    1272             :          swidf       ! sw down, near IR, diffuse (W/m^2)
    1273             : 
    1274             :       real (kind=dbl_kind), intent(inout) :: &
    1275             :          coszen  , & ! cosine of solar zenith angle 
    1276             :          alvdr   , & ! visible, direct, albedo (fraction) 
    1277             :          alvdf   , & ! visible, diffuse, albedo (fraction) 
    1278             :          alidr   , & ! near-ir, direct, albedo (fraction) 
    1279             :          alidf   , & ! near-ir, diffuse, albedo (fraction) 
    1280             :          fswsfc  , & ! SW absorbed at snow/bare ice/pondedi ice surface (W m-2)
    1281             :          fswint  , & ! SW interior absorption (below surface, above ocean,W m-2)
    1282             :          fswthru     ! SW through snow/bare ice/ponded ice into ocean (W m-2)
    1283             : 
    1284             :       real (kind=dbl_kind), intent(out) :: &
    1285             :          fswthru_vdr , & ! vis dir SW through snow/bare ice/ponded ice into ocean (W m-2)
    1286             :          fswthru_vdf , & ! vis dif SW through snow/bare ice/ponded ice into ocean (W m-2)
    1287             :          fswthru_idr , & ! nir dir SW through snow/bare ice/ponded ice into ocean (W m-2)
    1288             :          fswthru_idf     ! nir dif SW through snow/bare ice/ponded ice into ocean (W m-2)
    1289             :  
    1290             :       real (kind=dbl_kind), dimension (:), intent(inout) :: &
    1291             :          fswpenl , & ! visible SW entering ice layers (W m-2)
    1292             :          Sswabs  , & ! SW absorbed in snow layer (W m-2)
    1293             :          Iswabs      ! SW absorbed in ice layer (W m-2)
    1294             : 
    1295             :       real (kind=dbl_kind), intent(out) :: &
    1296             :          albice  , & ! bare ice albedo, for history  
    1297             :          albsno  , & ! snow albedo, for history  
    1298             :          albpnd      ! pond albedo, for history  
    1299             : 
    1300             :       logical (kind=log_kind) , intent(in) :: &
    1301             :          l_print_point
    1302             : 
    1303             :       ! local variables
    1304             : 
    1305             :       real (kind=dbl_kind) :: &
    1306     1275244 :          netsw    , & ! net shortwave
    1307     1275244 :          fnidr    , & ! fraction of direct to total down surface flux in nir
    1308     1275244 :          hstmp    , & ! snow thickness (set to 0 for bare ice case)
    1309     1275244 :          hi       , & ! ice thickness (all sea ice layers, m)
    1310     1275244 :          fi           ! snow/bare ice fractional coverage (0 to 1)
    1311             : 
    1312             :       real (kind=dbl_kind), dimension (4*n_aero) :: &
    1313     9920831 :          aero_mp      ! aerosol mass path in kg/m2
    1314             : 
    1315             :       integer (kind=int_kind) :: &
    1316             :          srftyp       ! surface type over ice: (0=air, 1=snow, 2=pond)
    1317             :  
    1318             :       integer (kind=int_kind) :: &
    1319             :          k        , & ! level index
    1320             :          na       , & ! aerosol index
    1321             :          klev     , & ! number of radiation layers - 1
    1322             :          klevp        ! number of radiation interfaces - 1
    1323             :                       ! (0 layer is included also)
    1324             : 
    1325             :       real (kind=dbl_kind) :: &
    1326     1275244 :          vsno         ! volume of snow 
    1327             : 
    1328             :       real (kind=dbl_kind) :: &
    1329     1275244 :          swdn  , & ! swvdr(i,j)+swvdf(i,j)+swidr(i,j)+swidf(i,j)
    1330     1275244 :          swab  , & ! fswsfc(i,j)+fswint(i,j)+fswthru(i,j)
    1331     1275244 :          swalb     ! (1.-swab/(swdn+.0001))
    1332             : 
    1333             :       ! for history
    1334             :       real (kind=dbl_kind) :: &
    1335     1275244 :          avdrl   , & ! visible, direct, albedo (fraction) 
    1336     1275244 :          avdfl   , & ! visible, diffuse, albedo (fraction) 
    1337     1275244 :          aidrl   , & ! near-ir, direct, albedo (fraction) 
    1338     1275244 :          aidfl       ! near-ir, diffuse, albedo (fraction) 
    1339             : 
    1340             :       character(len=*),parameter :: subname='(shortwave_dEdd)'
    1341             : 
    1342             : !-----------------------------------------------------------------------
    1343             : 
    1344     3770075 :          klev    = nslyr + nilyr + 1   ! number of radiation layers - 1
    1345     3770075 :          klevp   = klev  + 1           ! number of radiation interfaces - 1
    1346             :                                        ! (0 layer is included also)
    1347             : 
    1348             :       ! zero storage albedos and fluxes for accumulation over surface types:
    1349     3770075 :       hstmp    = c0
    1350     3770075 :       hi       = c0
    1351     3770075 :       fi       = c0
    1352     3770075 :       alvdr    = c0
    1353     3770075 :       alvdf    = c0
    1354     3770075 :       alidr    = c0
    1355     3770075 :       alidf    = c0
    1356     3770075 :       avdrl    = c0
    1357     3770075 :       avdfl    = c0
    1358     3770075 :       aidrl    = c0
    1359     3770075 :       aidfl    = c0
    1360     3770075 :       fswsfc   = c0
    1361     3770075 :       fswint   = c0
    1362     3770075 :       fswthru  = c0
    1363     3770075 :       fswthru_vdr  = c0
    1364     3770075 :       fswthru_vdf  = c0
    1365     3770075 :       fswthru_idr  = c0
    1366     3770075 :       fswthru_idf  = c0
    1367             :       ! compute fraction of nir down direct to total over all points:
    1368     3770075 :       fnidr = c0
    1369     3770075 :       if( swidr + swidf > puny ) then
    1370     2909240 :          fnidr = swidr/(swidr+swidf)
    1371             :       endif
    1372     3770075 :       albice    = c0
    1373     3770075 :       albsno    = c0
    1374     3770075 :       albpnd    = c0
    1375    33496281 :       fswpenl(:) = c0
    1376     8449182 :       Sswabs(:)  = c0
    1377    29726206 :       Iswabs(:)  = c0
    1378             : 
    1379             :       ! compute aerosol mass path
    1380             :       
    1381    18018659 :          aero_mp(:) = c0
    1382     3770075 :          if( tr_aero ) then
    1383             :             ! check 4 layers for each aerosol, a snow SSL, snow below SSL,
    1384             :             ! sea ice SSL, and sea ice below SSL, in that order.
    1385      229795 :             if (size(aero) < 4*n_aero) then
    1386           0 :                call icepack_warnings_add(subname//' ERROR: size(aero) too small')
    1387           0 :                call icepack_warnings_setabort(.true.,__FILE__,__LINE__)
    1388           0 :                return
    1389             :             endif
    1390      459590 :             do na = 1, 4*n_aero, 4
    1391      229795 :                vsno = hs * aice
    1392      229795 :                netsw = swvdr + swidr + swvdf + swidf
    1393      459590 :                if (netsw > puny) then ! sun above horizon
    1394      181316 :                   aero_mp(na  ) = aero(na  )*vsno
    1395      181316 :                   aero_mp(na+1) = aero(na+1)*vsno
    1396      181316 :                   aero_mp(na+2) = aero(na+2)*vice
    1397      181316 :                   aero_mp(na+3) = aero(na+3)*vice
    1398             :                endif                  ! aice > 0 and netsw > 0
    1399             :             enddo      ! na
    1400             :          endif      ! if aerosols
    1401             : 
    1402             :          ! compute shortwave radiation accounting for snow/ice (both snow over 
    1403             :          ! ice and bare ice) and ponded ice (if any):
    1404             :          
    1405             :          ! sea ice points with sun above horizon
    1406     3770075 :          netsw = swvdr + swidr + swvdf + swidf
    1407     3770075 :          if (netsw > puny) then ! sun above horizon
    1408     2909240 :             coszen = max(puny,coszen)
    1409             :             ! evaluate sea ice thickness and fraction
    1410     2909240 :             hi  = vice / aice
    1411     2909240 :             fi  = c1 - fs - fp
    1412             :             ! bare sea ice points
    1413     2909240 :             if(fi > c0) then
    1414             :                ! calculate bare sea ice
    1415             : 
    1416      829017 :                srftyp = 0
    1417             :                call compute_dEdd(nilyr,       nslyr,   klev,   klevp,   & 
    1418           0 :                       zbio,      dEdd_algae,                            &
    1419             :                       heat_capacity,          fnidr,   coszen,          &
    1420             :                       R_ice,     R_pnd,                                 &
    1421           0 :                       kaer_tab,    waer_tab,    gaer_tab,               &
    1422           0 :                       kaer_bc_tab, waer_bc_tab, gaer_bc_tab,            &
    1423           0 :                       bcenh,     modal_aero,  kalg,                     &
    1424             :                       swvdr,     swvdf,       swidr,   swidf,  srftyp,  &
    1425           0 :                       hstmp,     rhosnw,      rsnw,    hi,     hp,      &
    1426           0 :                       fi,        aero_mp,     avdrl,   avdfl,           &
    1427             :                       aidrl,     aidfl,                                 &
    1428             :                       fswsfc,    fswint,                                &
    1429             :                       fswthru,                                          &
    1430             :                       fswthru_vdr,                                      &
    1431             :                       fswthru_vdf,                                      &
    1432             :                       fswthru_idr,                                      &
    1433             :                       fswthru_idf,                                      &
    1434           0 :                       Sswabs,                                           &
    1435      829017 :                       Iswabs,    fswpenl)
    1436      829017 :                if (icepack_warnings_aborted(subname)) return
    1437             :                
    1438      829017 :                alvdr   = alvdr   + avdrl *fi
    1439      829017 :                alvdf   = alvdf   + avdfl *fi
    1440      829017 :                alidr   = alidr   + aidrl *fi
    1441      829017 :                alidf   = alidf   + aidfl *fi
    1442             :                ! for history
    1443             :                albice = albice &
    1444             :                       + awtvdr*avdrl + awtidr*aidrl &
    1445      829017 :                       + awtvdf*avdfl + awtidf*aidfl 
    1446             :             endif
    1447             :          endif
    1448             :          
    1449             :          ! sea ice points with sun above horizon
    1450     3770075 :          netsw = swvdr + swidr + swvdf + swidf
    1451     3770075 :          if (netsw > puny) then ! sun above horizon
    1452     2909240 :             coszen = max(puny,coszen)
    1453             :             ! snow-covered sea ice points
    1454     2909240 :             if(fs > c0) then
    1455             :                ! calculate snow covered sea ice
    1456             : 
    1457     2092288 :                srftyp = 1
    1458             :                call compute_dEdd(nilyr,       nslyr,   klev,   klevp,   & 
    1459           0 :                       zbio,      dEdd_algae,                            &
    1460             :                       heat_capacity,          fnidr,   coszen,          &
    1461             :                       R_ice,     R_pnd,                                 &
    1462           0 :                       kaer_tab,    waer_tab,    gaer_tab,               &
    1463           0 :                       kaer_bc_tab, waer_bc_tab, gaer_bc_tab,            &
    1464           0 :                       bcenh,     modal_aero,  kalg,                     &
    1465             :                       swvdr,     swvdf,       swidr,   swidf,  srftyp,  &
    1466           0 :                       hs,        rhosnw,      rsnw,    hi,     hp,      &
    1467           0 :                       fs,        aero_mp,     avdrl,   avdfl,           &
    1468             :                       aidrl,     aidfl,                                 &
    1469             :                       fswsfc,    fswint,                                &
    1470             :                       fswthru,                                          &
    1471             :                       fswthru_vdr,                                      &
    1472             :                       fswthru_vdf,                                      &
    1473             :                       fswthru_idr,                                      &
    1474             :                       fswthru_idf,                                      &
    1475           0 :                       Sswabs,                                           &
    1476     2092288 :                       Iswabs,    fswpenl)
    1477     2092288 :                if (icepack_warnings_aborted(subname)) return
    1478             :                
    1479     2092288 :                alvdr   = alvdr   + avdrl *fs
    1480     2092288 :                alvdf   = alvdf   + avdfl *fs
    1481     2092288 :                alidr   = alidr   + aidrl *fs
    1482     2092288 :                alidf   = alidf   + aidfl *fs
    1483             :                ! for history
    1484             :                albsno = albsno &
    1485             :                       + awtvdr*avdrl + awtidr*aidrl &
    1486     2092288 :                       + awtvdf*avdfl + awtidf*aidfl 
    1487             :             endif
    1488             :          endif
    1489             :          
    1490     3770075 :          hi = c0
    1491             : 
    1492             :          ! sea ice points with sun above horizon
    1493     3770075 :          netsw = swvdr + swidr + swvdf + swidf
    1494     3770075 :          if (netsw > puny) then ! sun above horizon
    1495     2909240 :             coszen = max(puny,coszen)
    1496     2909240 :             hi  = vice / aice
    1497             :             ! if nonzero pond fraction and sufficient pond depth
    1498             :             ! if( fp > puny .and. hp > hpmin ) then
    1499     2909240 :             if (fp > puny) then
    1500             :                
    1501             :                ! calculate ponded ice
    1502             : 
    1503      297565 :                srftyp = 2
    1504             :                call compute_dEdd(nilyr,       nslyr,   klev,   klevp,   & 
    1505           0 :                       zbio,      dEdd_algae,                            &
    1506             :                       heat_capacity,          fnidr,   coszen,          &
    1507             :                       R_ice,     R_pnd,                                 &
    1508           0 :                       kaer_tab,    waer_tab,    gaer_tab,               &
    1509           0 :                       kaer_bc_tab, waer_bc_tab, gaer_bc_tab,            &
    1510           0 :                       bcenh,     modal_aero,  kalg,                     &
    1511             :                       swvdr,     swvdf,       swidr,   swidf,  srftyp,  & 
    1512           0 :                       hs,        rhosnw,      rsnw,    hi,     hp,      &
    1513           0 :                       fp,        aero_mp,     avdrl,   avdfl,           &
    1514             :                       aidrl,     aidfl,                                 &
    1515             :                       fswsfc,    fswint,                                &
    1516             :                       fswthru,                                          &
    1517             :                       fswthru_vdr,                                      &
    1518             :                       fswthru_vdf,                                      &
    1519             :                       fswthru_idr,                                      &
    1520             :                       fswthru_idf,                                      &
    1521           0 :                       Sswabs,                                           &
    1522      297565 :                       Iswabs,    fswpenl)
    1523      297565 :                if (icepack_warnings_aborted(subname)) return
    1524             :                
    1525      297565 :                alvdr   = alvdr   + avdrl *fp
    1526      297565 :                alvdf   = alvdf   + avdfl *fp
    1527      297565 :                alidr   = alidr   + aidrl *fp
    1528      297565 :                alidf   = alidf   + aidfl *fp
    1529             :                ! for history
    1530             :                albpnd = albpnd &
    1531             :                       + awtvdr*avdrl + awtidr*aidrl &
    1532      297565 :                       + awtvdf*avdfl + awtidf*aidfl 
    1533             :             endif
    1534             :          endif
    1535             : 
    1536             :          ! if no incoming shortwave, set albedos to 1
    1537     3770075 :          netsw = swvdr + swidr + swvdf + swidf
    1538     3770075 :          if (netsw <= puny) then ! sun above horizon
    1539      860835 :             alvdr = c1
    1540      860835 :             alvdf = c1
    1541      860835 :             alidr = c1
    1542      860835 :             alidf = c1
    1543             :          endif
    1544             : 
    1545     3770075 :       if (l_print_point .and. netsw > puny) then
    1546             : 
    1547           0 :          write(warnstr,*) subname, ' printing point'
    1548           0 :          call icepack_warnings_add(warnstr)
    1549           0 :          write(warnstr,*) subname, ' coszen = ', &
    1550           0 :                             coszen
    1551           0 :          call icepack_warnings_add(warnstr)
    1552           0 :          write(warnstr,*) subname, ' swvdr  swvdf = ', &
    1553           0 :                             swvdr,swvdf
    1554           0 :          call icepack_warnings_add(warnstr)
    1555           0 :          write(warnstr,*) subname, ' swidr  swidf = ', &
    1556           0 :                             swidr,swidf
    1557           0 :          call icepack_warnings_add(warnstr)
    1558           0 :          write(warnstr,*) subname, ' aice = ', &
    1559           0 :                             aice
    1560           0 :          call icepack_warnings_add(warnstr)
    1561           0 :          write(warnstr,*) subname, ' hs = ', &
    1562           0 :                             hs
    1563           0 :          call icepack_warnings_add(warnstr)
    1564           0 :          write(warnstr,*) subname, ' hp = ', &
    1565           0 :                             hp
    1566           0 :          call icepack_warnings_add(warnstr)
    1567           0 :          write(warnstr,*) subname, ' fs = ', &
    1568           0 :                             fs
    1569           0 :          call icepack_warnings_add(warnstr)
    1570           0 :          write(warnstr,*) subname, ' fi = ', &
    1571           0 :                             fi
    1572           0 :          call icepack_warnings_add(warnstr)
    1573           0 :          write(warnstr,*) subname, ' fp = ', &
    1574           0 :                             fp
    1575           0 :          call icepack_warnings_add(warnstr)
    1576           0 :          write(warnstr,*) subname, ' hi = ', &
    1577           0 :                             hi
    1578           0 :          call icepack_warnings_add(warnstr)
    1579           0 :          write(warnstr,*) subname, ' alvdr  alvdf = ', &
    1580           0 :                             alvdr,alvdf
    1581           0 :          call icepack_warnings_add(warnstr)
    1582           0 :          write(warnstr,*) subname, ' alidr  alidf = ', &
    1583           0 :                             alidr,alidf
    1584           0 :          call icepack_warnings_add(warnstr)
    1585           0 :          write(warnstr,*) subname, ' fswsfc fswint fswthru = ', &
    1586           0 :                             fswsfc,fswint,fswthru
    1587           0 :          call icepack_warnings_add(warnstr)
    1588           0 :          swdn  = swvdr+swvdf+swidr+swidf
    1589           0 :          swab  = fswsfc+fswint+fswthru
    1590           0 :          swalb = (1.-swab/(swdn+.0001))
    1591           0 :          write(warnstr,*) subname, ' swdn swab swalb = ',swdn,swab,swalb
    1592           0 :          do k = 1, nslyr               
    1593           0 :             write(warnstr,*) subname, ' snow layer k    = ', k, &
    1594           0 :                              ' rhosnw = ', &
    1595           0 :                                rhosnw(k), &
    1596           0 :                              ' rsnw = ', &
    1597           0 :                                rsnw(k)
    1598           0 :             call icepack_warnings_add(warnstr)
    1599             :          enddo
    1600           0 :          do k = 1, nslyr               
    1601           0 :             write(warnstr,*) subname, ' snow layer k    = ', k, &
    1602           0 :                              ' Sswabs(k)       = ', Sswabs(k)
    1603           0 :             call icepack_warnings_add(warnstr)
    1604             :          enddo
    1605           0 :          do k = 1, nilyr               
    1606           0 :             write(warnstr,*) subname, ' sea ice layer k = ', k, &
    1607           0 :                              ' Iswabs(k)       = ', Iswabs(k)
    1608           0 :             call icepack_warnings_add(warnstr)
    1609             :          enddo
    1610             : 
    1611             :       endif  ! l_print_point .and. coszen > .01
    1612             : 
    1613             :       end subroutine shortwave_dEdd
    1614             : 
    1615             : !=======================================================================
    1616             : !
    1617             : ! Evaluate snow/ice/ponded ice inherent optical properties (IOPs), and 
    1618             : ! then calculate the multiple scattering solution by calling solution_dEdd.
    1619             : !
    1620             : ! author:  Bruce P. Briegleb, NCAR 
    1621             : !   2013:  E Hunke merged with NCAR version
    1622             : 
    1623     3218870 :       subroutine compute_dEdd (nilyr,    nslyr,    klev,  klevp,  &
    1624     3218870 :                     zbio,     dEdd_algae,                         &
    1625             :                     heat_capacity,       fnidr,    coszen,        &
    1626             :                     R_ice,     R_pnd,                             &
    1627     3218870 :                     kaer_tab,    waer_tab,         gaer_tab,      &
    1628     3218870 :                     kaer_bc_tab, waer_bc_tab,      gaer_bc_tab,   &
    1629     3218870 :                     bcenh,     modal_aero,         kalg,          &
    1630             :                     swvdr,     swvdf,    swidr,    swidf, srftyp, &
    1631     6437740 :                     hs,        rhosnw,   rsnw,     hi,    hp,     &
    1632     3218870 :                     fi,        aero_mp,  alvdr,    alvdf,         &
    1633             :                                alidr,    alidf,         &
    1634             :                                fswsfc,   fswint,        &
    1635             :                                fswthru,                 &
    1636             :                                fswthru_vdr,             &
    1637             :                                fswthru_vdf,             &
    1638             :                                fswthru_idr,             &
    1639             :                                fswthru_idf,             &
    1640     3218870 :                                Sswabs,                  &
    1641     3218870 :                                Iswabs,   fswpenl)
    1642             : 
    1643             :       integer (kind=int_kind), intent(in) :: &
    1644             :          nilyr , & ! number of ice layers
    1645             :          nslyr , & ! number of snow layers
    1646             :          klev  , & ! number of radiation layers - 1
    1647             :          klevp     ! number of radiation interfaces - 1
    1648             :                    ! (0 layer is included also)
    1649             :  
    1650             :       logical (kind=log_kind), intent(in) :: &
    1651             :          heat_capacity,& ! if true, ice has nonzero heat capacity
    1652             :          dEdd_algae,   & ! .true. use prognostic chla in dEdd
    1653             :          modal_aero      ! .true. use modal aerosol treatment
    1654             :  
    1655             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & ! Modal aerosol treatment
    1656             :          kaer_bc_tab, & ! aerosol mass extinction cross section (m2/kg)
    1657             :          waer_bc_tab, & ! aerosol single scatter albedo (fraction)
    1658             :          gaer_bc_tab    ! aerosol asymmetry parameter (cos(theta))
    1659             :    
    1660             :       real (kind=dbl_kind), dimension(:,:,:), intent(in) :: & ! Modal aerosol treatment
    1661             :          bcenh          ! BC absorption enhancement factor
    1662             : 
    1663             :       ! dEdd tuning parameters, set in namelist
    1664             :       real (kind=dbl_kind), intent(in) :: &
    1665             :          R_ice , & ! sea ice tuning parameter; +1 > 1sig increase in albedo
    1666             :          R_pnd     ! ponded ice tuning parameter; +1 > 1sig increase in albedo
    1667             : 
    1668             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & 
    1669             :          kaer_tab, & ! aerosol mass extinction cross section (m2/kg)
    1670             :          waer_tab, & ! aerosol single scatter albedo (fraction)
    1671             :          gaer_tab    ! aerosol asymmetry parameter (cos(theta))
    1672             :    
    1673             :       real (kind=dbl_kind), intent(in) :: &
    1674             :          kalg    , & ! algae absorption coefficient
    1675             :          fnidr   , & ! fraction of direct to total down flux in nir
    1676             :          coszen  , & ! cosine solar zenith angle
    1677             :          swvdr   , & ! shortwave down at surface, visible, direct  (W/m^2)
    1678             :          swvdf   , & ! shortwave down at surface, visible, diffuse (W/m^2)
    1679             :          swidr   , & ! shortwave down at surface, near IR, direct  (W/m^2)
    1680             :          swidf       ! shortwave down at surface, near IR, diffuse (W/m^2)
    1681             :  
    1682             :       integer (kind=int_kind), intent(in) :: &
    1683             :          srftyp      ! surface type over ice: (0=air, 1=snow, 2=pond)
    1684             :  
    1685             :       real (kind=dbl_kind), intent(in) :: &
    1686             :          hs          ! snow thickness (m)
    1687             : 
    1688             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
    1689             :          rhosnw  , & ! snow density in snow layer (kg/m3)
    1690             :          rsnw    , & ! snow grain radius in snow layer (m)
    1691             :          zbio    , & ! zaerosol + chla shortwave tracers kg/m^3
    1692             :          aero_mp     ! aerosol mass path in kg/m2
    1693             : 
    1694             :       real (kind=dbl_kind), intent(in) :: &
    1695             :          hi      , & ! ice thickness (m)
    1696             :          hp      , & ! pond depth (m)
    1697             :          fi          ! snow/bare ice fractional coverage (0 to 1)
    1698             :  
    1699             :       real (kind=dbl_kind), intent(inout) :: &
    1700             :          alvdr   , & ! visible, direct, albedo (fraction) 
    1701             :          alvdf   , & ! visible, diffuse, albedo (fraction) 
    1702             :          alidr   , & ! near-ir, direct, albedo (fraction) 
    1703             :          alidf   , & ! near-ir, diffuse, albedo (fraction) 
    1704             :          fswsfc  , & ! SW absorbed at snow/bare ice/pondedi ice surface (W m-2)
    1705             :          fswint  , & ! SW interior absorption (below surface, above ocean,W m-2)
    1706             :          fswthru     ! SW through snow/bare ice/ponded ice into ocean (W m-2)
    1707             : 
    1708             :       real (kind=dbl_kind), intent(inout) :: &
    1709             :          fswthru_vdr , & ! vis dir SW through snow/bare ice/ponded ice into ocean (W m-2)
    1710             :          fswthru_vdf , & ! vis dif SW through snow/bare ice/ponded ice into ocean (W m-2)
    1711             :          fswthru_idr , & ! nir dir SW through snow/bare ice/ponded ice into ocean (W m-2)
    1712             :          fswthru_idf     ! nir dif SW through snow/bare ice/ponded ice into ocean (W m-2)
    1713             :  
    1714             :       real (kind=dbl_kind), dimension (:), intent(inout) :: &
    1715             :          fswpenl , & ! visible SW entering ice layers (W m-2)
    1716             :          Sswabs  , & ! SW absorbed in snow layer (W m-2)
    1717             :          Iswabs      ! SW absorbed in ice layer (W m-2)
    1718             : 
    1719             : !-----------------------------------------------------------------------
    1720             : !
    1721             : ! Set up optical property profiles, based on snow, sea ice and ponded 
    1722             : ! ice IOPs from:
    1723             : !
    1724             : ! Briegleb, B. P., and B. Light (2007): A Delta-Eddington Multiple 
    1725             : !    Scattering Parameterization for Solar Radiation in the Sea Ice 
    1726             : !    Component of the Community Climate System Model, NCAR Technical 
    1727             : !    Note  NCAR/TN-472+STR  February 2007
    1728             : !
    1729             : ! Computes column Delta-Eddington radiation solution for specific
    1730             : ! surface type: either snow over sea ice, bare sea ice, or ponded sea ice.
    1731             : !
    1732             : ! Divides solar spectrum into 3 intervals: 0.2-0.7, 0.7-1.19, and
    1733             : ! 1.19-5.0 micro-meters. The latter two are added (using an assumed
    1734             : ! partition of incident shortwave in the 0.7-5.0 micro-meter band between
    1735             : ! the 0.7-1.19 and 1.19-5.0 micro-meter band) to give the final output 
    1736             : ! of 0.2-0.7 visible and 0.7-5.0 near-infrared albedos and fluxes.
    1737             : !
    1738             : ! Specifies vertical layer optical properties based on input snow depth,
    1739             : ! density and grain radius, along with ice and pond depths, then computes
    1740             : ! layer by layer Delta-Eddington reflectivity, transmissivity and combines
    1741             : ! layers (done by calling routine solution_dEdd). Finally, surface albedos
    1742             : ! and internal fluxes/flux divergences are evaluated.
    1743             : !
    1744             : !  Description of the level and layer index conventions. This is
    1745             : !  for the standard case of one snow layer and four sea ice layers.
    1746             : !
    1747             : !  Please read the following; otherwise, there is 99.9% chance you
    1748             : !  will be confused about indices at some point in time........ :)
    1749             : !
    1750             : !  CICE4.0 snow treatment has one snow layer above the sea ice. This 
    1751             : !  snow layer has finite heat capacity, so that surface absorption must
    1752             : !  be distinguished from internal. The Delta-Eddington solar radiation
    1753             : !  thus adds extra surface scattering layers to both snow and sea ice.
    1754             : !  Note that in the following, we assume a fixed vertical layer structure
    1755             : !  for the radiation calculation. In other words, we always have the 
    1756             : !  structure shown below for one snow and four sea ice layers, but for 
    1757             : !  ponded ice the pond fills "snow" layer 1 over the sea ice, and for 
    1758             : !  bare sea ice the top layers over sea ice are treated as transparent air.
    1759             : !
    1760             : !  SSL = surface scattering layer for either snow or sea ice
    1761             : !  DL  = drained layer for sea ice immediately under sea ice SSL
    1762             : !  INT = interior layers for sea ice below the drained layer.
    1763             : !
    1764             : !  Notice that the radiation level starts with 0 at the top. Thus,
    1765             : !  the total number radiation layers is klev+1, where klev is the
    1766             : !  sum of nslyr, the number of CCSM snow layers, and nilyr, the
    1767             : !  number of CCSM sea ice layers, plus the sea ice SSL:
    1768             : !  klev = 1 + nslyr + nilyr
    1769             : !
    1770             : !  For the standard case illustrated below, nslyr=1, nilyr=4,
    1771             : !  and klev=6, with the number of layer interfaces klevp=klev+1.
    1772             : !  Layer interfaces are the surfaces on which reflectivities,
    1773             : !  transmissivities and fluxes are evaluated.
    1774             : !
    1775             : !  CCSM3 Sea Ice Model            Delta-Eddington Solar Radiation
    1776             : !                                     Layers and Interfaces
    1777             : !                             Layer Index             Interface Index
    1778             : !    ---------------------            ---------------------  0
    1779             : !                                  0  \\\   snow SSL    \\\
    1780             : !       snow layer 1                  ---------------------  1
    1781             : !                                  1    rest of snow layer
    1782             : !    +++++++++++++++++++++            +++++++++++++++++++++  2
    1783             : !                                  2  \\\ sea ice SSL   \\\
    1784             : !      sea ice layer 1                ---------------------  3
    1785             : !                                  3      sea ice  DL
    1786             : !    ---------------------            ---------------------  4
    1787             : !
    1788             : !      sea ice layer 2             4      sea ice INT
    1789             : !
    1790             : !    ---------------------            ---------------------  5
    1791             : !
    1792             : !      sea ice layer 3             5      sea ice INT
    1793             : !
    1794             : !    ---------------------            ---------------------  6
    1795             : !
    1796             : !      sea ice layer 4             6      sea ice INT
    1797             : !
    1798             : !    ---------------------            ---------------------  7
    1799             : !
    1800             : ! When snow lies over sea ice, the radiation absorbed in the
    1801             : ! snow SSL is used for surface heating, and that in the rest
    1802             : ! of the snow layer for its internal heating. For sea ice in
    1803             : ! this case, all of the radiant heat absorbed in both the
    1804             : ! sea ice SSL and the DL are used for sea ice layer 1 heating.
    1805             : !
    1806             : ! When pond lies over sea ice, and for bare sea ice, all of the
    1807             : ! radiant heat absorbed within and above the sea ice SSL is used
    1808             : ! for surface heating, and that absorbed in the sea ice DL is
    1809             : ! used for sea ice layer 1 heating.
    1810             : !
    1811             : ! Basically, vertical profiles of the layer extinction optical depth (tau), 
    1812             : ! single scattering albedo (w0) and asymmetry parameter (g) are required over
    1813             : ! the klev+1 layers, where klev+1 = 2 + nslyr + nilyr. All of the surface type
    1814             : ! information and snow/ice iop properties are evaulated in this routine, so
    1815             : ! the tau,w0,g profiles can be passed to solution_dEdd for multiple scattering
    1816             : ! evaluation. Snow, bare ice and ponded ice iops are contained in data arrays
    1817             : ! in this routine.
    1818             : !
    1819             : !-----------------------------------------------------------------------
    1820             : 
    1821             :       ! local variables
    1822             : 
    1823             :       integer (kind=int_kind) :: &
    1824             :          k       , & ! level index
    1825             :          ns      , & ! spectral index
    1826             :          nr      , & ! index for grain radius tables
    1827             :          ki      , & ! index for internal absorption
    1828             :          km      , & ! k starting index for snow, sea ice internal absorption
    1829             :          kp      , & ! k+1 or k+2 index for snow, sea ice internal absorption
    1830             :          ksrf    , & ! level index for surface absorption
    1831             :          ksnow   , & ! level index for snow density and grain size
    1832             :          kii         ! level starting index for sea ice (nslyr+1)
    1833             : 
    1834             :       integer (kind=int_kind), parameter :: & 
    1835             :          nmbrad  = 32        ! number of snow grain radii in tables
    1836             :  
    1837             :       real (kind=dbl_kind) :: & 
    1838     1142733 :          avdr    , & ! visible albedo, direct   (fraction)
    1839     1142733 :          avdf    , & ! visible albedo, diffuse  (fraction)
    1840     1142733 :          aidr    , & ! near-ir albedo, direct   (fraction)
    1841     1142733 :          aidf        ! near-ir albedo, diffuse  (fraction)
    1842             :  
    1843             :       real (kind=dbl_kind) :: & 
    1844     1142733 :          fsfc    , & ! shortwave absorbed at snow/bare ice/ponded ice surface (W m-2)
    1845     1142733 :          fint    , & ! shortwave absorbed in interior (W m-2)
    1846     1142733 :          fthru   , & ! shortwave through snow/bare ice/ponded ice to ocean (W/m^2)
    1847     1142733 :          fthruvdr, & ! vis dir shortwave through snow/bare ice/ponded ice to ocean (W/m^2)
    1848     1142733 :          fthruvdf, & ! vis dif shortwave through snow/bare ice/ponded ice to ocean (W/m^2)
    1849     1142733 :          fthruidr, & ! nir dir shortwave through snow/bare ice/ponded ice to ocean (W/m^2)
    1850     1142733 :          fthruidf    ! nir dif shortwave through snow/bare ice/ponded ice to ocean (W/m^2)
    1851             : 
    1852             :       real (kind=dbl_kind), dimension(nslyr) :: & 
    1853     6870812 :          Sabs        ! shortwave absorbed in snow layer (W m-2)
    1854             : 
    1855             :       real (kind=dbl_kind), dimension(nilyr) :: & 
    1856    13294138 :          Iabs        ! shortwave absorbed in ice layer (W m-2)
    1857             :  
    1858             :       real (kind=dbl_kind), dimension(nilyr+1) :: & 
    1859    14436871 :          fthrul      ! shortwave through to ice layers (W m-2)
    1860             : 
    1861             :       real (kind=dbl_kind), dimension (nspint) :: &
    1862     4570932 :          wghtns              ! spectral weights
    1863             :  
    1864             :       real (kind=dbl_kind), parameter :: & 
    1865             :          cp67    = 0.67_dbl_kind   , & ! nir band weight parameter
    1866             :          cp78    = 0.78_dbl_kind   , & ! nir band weight parameter
    1867             :          cp01    = 0.01_dbl_kind       ! for ocean visible albedo
    1868             :  
    1869             :       real (kind=dbl_kind), dimension (0:klev) :: &
    1870    17155409 :          tau     , & ! layer extinction optical depth
    1871    17155409 :          w0      , & ! layer single scattering albedo
    1872    17155409 :          g           ! layer asymmetry parameter
    1873             :  
    1874             :       ! following arrays are defined at model interfaces; 0 is the top of the
    1875             :       ! layer above the sea ice; klevp is the sea ice/ocean interface.
    1876             :       real (kind=dbl_kind), dimension (0:klevp) :: &
    1877    18298142 :          trndir  , & ! solar beam down transmission from top
    1878    18298142 :          trntdr  , & ! total transmission to direct beam for layers above
    1879    18298142 :          trndif  , & ! diffuse transmission to diffuse beam for layers above
    1880    18298142 :          rupdir  , & ! reflectivity to direct radiation for layers below
    1881    18298142 :          rupdif  , & ! reflectivity to diffuse radiation for layers below
    1882    18298142 :          rdndif      ! reflectivity to diffuse radiation for layers above
    1883             :  
    1884             :       real (kind=dbl_kind), dimension (0:klevp) :: &
    1885    18298142 :          dfdir   , & ! down-up flux at interface due to direct beam at top surface
    1886    20583608 :          dfdif       ! down-up flux at interface due to diffuse beam at top surface
    1887             : 
    1888             :       real (kind=dbl_kind) :: &
    1889     1142733 :          refk    , & ! interface k multiple scattering term
    1890     1142733 :          delr    , & ! snow grain radius interpolation parameter
    1891             :       ! inherent optical properties (iop) for snow
    1892     1142733 :          Qs      , & ! Snow extinction efficiency
    1893     1142733 :          ks      , & ! Snow extinction coefficient (/m)
    1894     1142733 :          ws      , & ! Snow single scattering albedo
    1895     1142733 :          gs          ! Snow asymmetry parameter
    1896             : 
    1897             :       real (kind=dbl_kind), dimension(nslyr) :: & 
    1898     6870812 :          frsnw       ! snow grain radius in snow layer * adjustment factor (m)
    1899             : 
    1900             :       ! actual used ice and ponded ice IOPs, allowing for tuning 
    1901             :       ! modifications of the above "_mn" value
    1902             :       real (kind=dbl_kind), dimension (nspint) :: &
    1903     4570932 :          ki_ssl       , & ! Surface-scattering-layer ice extinction coefficient (/m)
    1904     4570932 :          wi_ssl       , & ! Surface-scattering-layer ice single scattering albedo
    1905     4570932 :          gi_ssl       , & ! Surface-scattering-layer ice asymmetry parameter
    1906     4570932 :          ki_dl        , & ! Drained-layer ice extinction coefficient (/m)
    1907     4570932 :          wi_dl        , & ! Drained-layer ice single scattering albedo
    1908     4570932 :          gi_dl        , & ! Drained-layer ice asymmetry parameter
    1909     4570932 :          ki_int       , & ! Interior-layer ice extinction coefficient (/m)
    1910     4570932 :          wi_int       , & ! Interior-layer ice single scattering albedo
    1911     4570932 :          gi_int       , & ! Interior-layer ice asymmetry parameter
    1912     4570932 :          ki_p_ssl     , & ! Ice under pond srf scat layer extinction coefficient (/m)
    1913     4570932 :          wi_p_ssl     , & ! Ice under pond srf scat layer single scattering albedo
    1914     4570932 :          gi_p_ssl     , & ! Ice under pond srf scat layer asymmetry parameter
    1915     4570932 :          ki_p_int     , & ! Ice under pond extinction coefficient (/m)
    1916     4570932 :          wi_p_int     , & ! Ice under pond single scattering albedo
    1917     4570932 :          gi_p_int         ! Ice under pond asymmetry parameter
    1918             : 
    1919             :       real (kind=dbl_kind), dimension(0:klev) :: &
    1920    17155409 :          dzk              ! layer thickness
    1921             : 
    1922             :       real (kind=dbl_kind) :: &
    1923     1142733 :          dz           , & ! snow, sea ice or pond water layer thickness
    1924     1142733 :          dz_ssl       , & ! snow or sea ice surface scattering layer thickness
    1925     1142733 :          fs               ! scaling factor to reduce (nilyr<4) or increase (nilyr>4) DL
    1926             :                           ! extinction coefficient to maintain DL optical depth constant
    1927             :                           ! with changing number of sea ice layers, to approximately 
    1928             :                           ! conserve computed albedo for constant physical depth of sea
    1929             :                           ! ice when the number of sea ice layers vary
    1930             :       real (kind=dbl_kind) :: &
    1931     1142733 :          sig          , & ! scattering coefficient for tuning
    1932     1142733 :          kabs         , & ! absorption coefficient for tuning
    1933     1142733 :          sigp             ! modified scattering coefficient for tuning
    1934             : 
    1935             :       real (kind=dbl_kind), dimension(nspint, 0:klev) :: &
    1936    52736615 :          kabs_chl    , & ! absorption coefficient for chlorophyll (/m)
    1937    52736615 :          tzaer       , & ! total aerosol extinction optical depth
    1938    52736615 :          wzaer       , & ! total aerosol single scatter albedo
    1939    52736615 :          gzaer           ! total aerosol asymmetry parameter
    1940             : 
    1941             :       real (kind=dbl_kind) :: &
    1942     1142733 :          albodr       , & ! spectral ocean albedo to direct rad
    1943     1142733 :          albodf           ! spectral ocean albedo to diffuse rad
    1944             :       
    1945             :       ! for melt pond transition to bare sea ice for small pond depths 
    1946             :       real (kind=dbl_kind) :: &
    1947     1142733 :          sig_i        , & ! ice scattering coefficient (/m)
    1948     1142733 :          sig_p        , & ! pond scattering coefficient (/m)
    1949     1142733 :          kext             ! weighted extinction coefficient (/m)
    1950             : 
    1951             :       ! aerosol optical properties from Mark Flanner, 26 June 2008
    1952             :       ! order assumed: hydrophobic black carbon, hydrophilic black carbon,
    1953             :       ! four dust aerosols by particle size range:
    1954             :       ! dust1(.05-0.5 micron), dust2(0.5-1.25 micron),
    1955             :       ! dust3(1.25-2.5 micron), dust4(2.5-5.0 micron)
    1956             :       ! spectral bands same as snow/sea ice: (0.3-0.7 micron, 0.7-1.19 micron
    1957             :       ! and 1.19-5.0 micron in wavelength)
    1958             : 
    1959             :       integer (kind=int_kind) :: &
    1960             :          na , n                        ! aerosol index
    1961             : 
    1962             :       real (kind=dbl_kind) :: &
    1963     1142733 :          taer                     , & ! total aerosol extinction optical depth
    1964     1142733 :          waer                     , & ! total aerosol single scatter albedo
    1965     1142733 :          gaer                     , & ! total aerosol asymmetry parameter
    1966     1142733 :          swdr                     , & ! shortwave down at surface, direct  (W/m^2)
    1967     1142733 :          swdf                     , & ! shortwave down at surface, diffuse (W/m^2)
    1968     1142733 :          rnilyr                   , & ! real(nilyr)
    1969     1142733 :          rnslyr                   , & ! real(nslyr)
    1970     1142733 :          rns                      , & ! real(ns)
    1971     2285466 :          tmp_0, tmp_ks, tmp_kl        ! temp variables
    1972             : 
    1973             :       integer(kind=int_kind), dimension(0:klev) :: &
    1974     6437740 :          k_bcini        , & !
    1975     6437740 :          k_bcins        , &
    1976     4361603 :          k_bcexs
    1977             :       real(kind=dbl_kind)::  &
    1978     1142733 :           tmp_gs, tmp1               ! temp variables
    1979             : 
    1980             :       ! snow grain radii (micro-meters) for table
    1981             :       real (kind=dbl_kind), dimension(nmbrad), parameter :: &
    1982             :          rsnw_tab = (/ &   ! snow grain radius for each table entry (micro-meters)
    1983             :           5._dbl_kind,    7._dbl_kind,   10._dbl_kind,   15._dbl_kind, &
    1984             :          20._dbl_kind,   30._dbl_kind,   40._dbl_kind,   50._dbl_kind, &
    1985             :          65._dbl_kind,   80._dbl_kind,  100._dbl_kind,  120._dbl_kind, &
    1986             :         140._dbl_kind,  170._dbl_kind,  200._dbl_kind,  240._dbl_kind, &
    1987             :         290._dbl_kind,  350._dbl_kind,  420._dbl_kind,  500._dbl_kind, &
    1988             :         570._dbl_kind,  660._dbl_kind,  760._dbl_kind,  870._dbl_kind, &
    1989             :        1000._dbl_kind, 1100._dbl_kind, 1250._dbl_kind, 1400._dbl_kind, &
    1990             :        1600._dbl_kind, 1800._dbl_kind, 2000._dbl_kind, 2500._dbl_kind/)
    1991             : 
    1992             :       ! snow extinction efficiency (unitless)
    1993             :       real (kind=dbl_kind), dimension (nspint,nmbrad), parameter :: &
    1994             :          Qs_tab = reshape((/ &
    1995             :           2.131798_dbl_kind,  2.187756_dbl_kind,  2.267358_dbl_kind, &
    1996             :           2.104499_dbl_kind,  2.148345_dbl_kind,  2.236078_dbl_kind, &
    1997             :           2.081580_dbl_kind,  2.116885_dbl_kind,  2.175067_dbl_kind, &
    1998             :           2.062595_dbl_kind,  2.088937_dbl_kind,  2.130242_dbl_kind, &
    1999             :           2.051403_dbl_kind,  2.072422_dbl_kind,  2.106610_dbl_kind, &
    2000             :           2.039223_dbl_kind,  2.055389_dbl_kind,  2.080586_dbl_kind, &
    2001             :           2.032383_dbl_kind,  2.045751_dbl_kind,  2.066394_dbl_kind, &
    2002             :           2.027920_dbl_kind,  2.039388_dbl_kind,  2.057224_dbl_kind, &
    2003             :           2.023444_dbl_kind,  2.033137_dbl_kind,  2.048055_dbl_kind, &
    2004             :           2.020412_dbl_kind,  2.028840_dbl_kind,  2.041874_dbl_kind, &
    2005             :           2.017608_dbl_kind,  2.024863_dbl_kind,  2.036046_dbl_kind, &
    2006             :           2.015592_dbl_kind,  2.022021_dbl_kind,  2.031954_dbl_kind, &
    2007             :           2.014083_dbl_kind,  2.019887_dbl_kind,  2.028853_dbl_kind, &
    2008             :           2.012368_dbl_kind,  2.017471_dbl_kind,  2.025353_dbl_kind, &
    2009             :           2.011092_dbl_kind,  2.015675_dbl_kind,  2.022759_dbl_kind, &
    2010             :           2.009837_dbl_kind,  2.013897_dbl_kind,  2.020168_dbl_kind, &
    2011             :           2.008668_dbl_kind,  2.012252_dbl_kind,  2.017781_dbl_kind, &
    2012             :           2.007627_dbl_kind,  2.010813_dbl_kind,  2.015678_dbl_kind, &
    2013             :           2.006764_dbl_kind,  2.009577_dbl_kind,  2.013880_dbl_kind, &
    2014             :           2.006037_dbl_kind,  2.008520_dbl_kind,  2.012382_dbl_kind, &
    2015             :           2.005528_dbl_kind,  2.007807_dbl_kind,  2.011307_dbl_kind, &
    2016             :           2.005025_dbl_kind,  2.007079_dbl_kind,  2.010280_dbl_kind, &
    2017             :           2.004562_dbl_kind,  2.006440_dbl_kind,  2.009333_dbl_kind, &
    2018             :           2.004155_dbl_kind,  2.005898_dbl_kind,  2.008523_dbl_kind, &
    2019             :           2.003794_dbl_kind,  2.005379_dbl_kind,  2.007795_dbl_kind, &
    2020             :           2.003555_dbl_kind,  2.005041_dbl_kind,  2.007329_dbl_kind, &
    2021             :           2.003264_dbl_kind,  2.004624_dbl_kind,  2.006729_dbl_kind, &
    2022             :           2.003037_dbl_kind,  2.004291_dbl_kind,  2.006230_dbl_kind, &
    2023             :           2.002776_dbl_kind,  2.003929_dbl_kind,  2.005700_dbl_kind, &
    2024             :           2.002590_dbl_kind,  2.003627_dbl_kind,  2.005276_dbl_kind, &
    2025             :           2.002395_dbl_kind,  2.003391_dbl_kind,  2.004904_dbl_kind, &
    2026             :           2.002071_dbl_kind,  2.002922_dbl_kind,  2.004241_dbl_kind/), &
    2027             :           (/nspint,nmbrad/))
    2028             : 
    2029             :       ! snow single scattering albedo (unitless)
    2030             :       real (kind=dbl_kind), dimension (nspint,nmbrad), parameter :: &
    2031             :         ws_tab = reshape((/ &
    2032             :          0.9999994_dbl_kind,  0.9999673_dbl_kind,  0.9954589_dbl_kind, &
    2033             :          0.9999992_dbl_kind,  0.9999547_dbl_kind,  0.9938576_dbl_kind, &
    2034             :          0.9999990_dbl_kind,  0.9999382_dbl_kind,  0.9917989_dbl_kind, &
    2035             :          0.9999985_dbl_kind,  0.9999123_dbl_kind,  0.9889724_dbl_kind, &
    2036             :          0.9999979_dbl_kind,  0.9998844_dbl_kind,  0.9866190_dbl_kind, &
    2037             :          0.9999970_dbl_kind,  0.9998317_dbl_kind,  0.9823021_dbl_kind, &
    2038             :          0.9999960_dbl_kind,  0.9997800_dbl_kind,  0.9785269_dbl_kind, &
    2039             :          0.9999951_dbl_kind,  0.9997288_dbl_kind,  0.9751601_dbl_kind, &
    2040             :          0.9999936_dbl_kind,  0.9996531_dbl_kind,  0.9706974_dbl_kind, &
    2041             :          0.9999922_dbl_kind,  0.9995783_dbl_kind,  0.9667577_dbl_kind, &
    2042             :          0.9999903_dbl_kind,  0.9994798_dbl_kind,  0.9621007_dbl_kind, &
    2043             :          0.9999885_dbl_kind,  0.9993825_dbl_kind,  0.9579541_dbl_kind, &
    2044             :          0.9999866_dbl_kind,  0.9992862_dbl_kind,  0.9541924_dbl_kind, &
    2045             :          0.9999838_dbl_kind,  0.9991434_dbl_kind,  0.9490959_dbl_kind, &
    2046             :          0.9999810_dbl_kind,  0.9990025_dbl_kind,  0.9444940_dbl_kind, &
    2047             :          0.9999772_dbl_kind,  0.9988171_dbl_kind,  0.9389141_dbl_kind, &
    2048             :          0.9999726_dbl_kind,  0.9985890_dbl_kind,  0.9325819_dbl_kind, &
    2049             :          0.9999670_dbl_kind,  0.9983199_dbl_kind,  0.9256405_dbl_kind, &
    2050             :          0.9999605_dbl_kind,  0.9980117_dbl_kind,  0.9181533_dbl_kind, &
    2051             :          0.9999530_dbl_kind,  0.9976663_dbl_kind,  0.9101540_dbl_kind, &
    2052             :          0.9999465_dbl_kind,  0.9973693_dbl_kind,  0.9035031_dbl_kind, &
    2053             :          0.9999382_dbl_kind,  0.9969939_dbl_kind,  0.8953134_dbl_kind, &
    2054             :          0.9999289_dbl_kind,  0.9965848_dbl_kind,  0.8865789_dbl_kind, &
    2055             :          0.9999188_dbl_kind,  0.9961434_dbl_kind,  0.8773350_dbl_kind, &
    2056             :          0.9999068_dbl_kind,  0.9956323_dbl_kind,  0.8668233_dbl_kind, &
    2057             :          0.9998975_dbl_kind,  0.9952464_dbl_kind,  0.8589990_dbl_kind, &
    2058             :          0.9998837_dbl_kind,  0.9946782_dbl_kind,  0.8476493_dbl_kind, &
    2059             :          0.9998699_dbl_kind,  0.9941218_dbl_kind,  0.8367318_dbl_kind, &
    2060             :          0.9998515_dbl_kind,  0.9933966_dbl_kind,  0.8227881_dbl_kind, &
    2061             :          0.9998332_dbl_kind,  0.9926888_dbl_kind,  0.8095131_dbl_kind, &
    2062             :          0.9998148_dbl_kind,  0.9919968_dbl_kind,  0.7968620_dbl_kind, &
    2063             :          0.9997691_dbl_kind,  0.9903277_dbl_kind,  0.7677887_dbl_kind/), &
    2064             :          (/nspint,nmbrad/))
    2065             : 
    2066             :       ! snow asymmetry parameter (unitless)
    2067             :       real (kind=dbl_kind), dimension (nspint,nmbrad), parameter :: &
    2068             :          gs_tab = reshape((/ &
    2069             :           0.859913_dbl_kind,  0.848003_dbl_kind,  0.824415_dbl_kind, &
    2070             :           0.867130_dbl_kind,  0.858150_dbl_kind,  0.848445_dbl_kind, &
    2071             :           0.873381_dbl_kind,  0.867221_dbl_kind,  0.861714_dbl_kind, &
    2072             :           0.878368_dbl_kind,  0.874879_dbl_kind,  0.874036_dbl_kind, &
    2073             :           0.881462_dbl_kind,  0.879661_dbl_kind,  0.881299_dbl_kind, &
    2074             :           0.884361_dbl_kind,  0.883903_dbl_kind,  0.890184_dbl_kind, &
    2075             :           0.885937_dbl_kind,  0.886256_dbl_kind,  0.895393_dbl_kind, &
    2076             :           0.886931_dbl_kind,  0.887769_dbl_kind,  0.899072_dbl_kind, &
    2077             :           0.887894_dbl_kind,  0.889255_dbl_kind,  0.903285_dbl_kind, &
    2078             :           0.888515_dbl_kind,  0.890236_dbl_kind,  0.906588_dbl_kind, &
    2079             :           0.889073_dbl_kind,  0.891127_dbl_kind,  0.910152_dbl_kind, &
    2080             :           0.889452_dbl_kind,  0.891750_dbl_kind,  0.913100_dbl_kind, &
    2081             :           0.889730_dbl_kind,  0.892213_dbl_kind,  0.915621_dbl_kind, &
    2082             :           0.890026_dbl_kind,  0.892723_dbl_kind,  0.918831_dbl_kind, &
    2083             :           0.890238_dbl_kind,  0.893099_dbl_kind,  0.921540_dbl_kind, &
    2084             :           0.890441_dbl_kind,  0.893474_dbl_kind,  0.924581_dbl_kind, &
    2085             :           0.890618_dbl_kind,  0.893816_dbl_kind,  0.927701_dbl_kind, &
    2086             :           0.890762_dbl_kind,  0.894123_dbl_kind,  0.930737_dbl_kind, &
    2087             :           0.890881_dbl_kind,  0.894397_dbl_kind,  0.933568_dbl_kind, &
    2088             :           0.890975_dbl_kind,  0.894645_dbl_kind,  0.936148_dbl_kind, &
    2089             :           0.891035_dbl_kind,  0.894822_dbl_kind,  0.937989_dbl_kind, &
    2090             :           0.891097_dbl_kind,  0.895020_dbl_kind,  0.939949_dbl_kind, &
    2091             :           0.891147_dbl_kind,  0.895212_dbl_kind,  0.941727_dbl_kind, &
    2092             :           0.891189_dbl_kind,  0.895399_dbl_kind,  0.943339_dbl_kind, &
    2093             :           0.891225_dbl_kind,  0.895601_dbl_kind,  0.944915_dbl_kind, &
    2094             :           0.891248_dbl_kind,  0.895745_dbl_kind,  0.945950_dbl_kind, &
    2095             :           0.891277_dbl_kind,  0.895951_dbl_kind,  0.947288_dbl_kind, &
    2096             :           0.891299_dbl_kind,  0.896142_dbl_kind,  0.948438_dbl_kind, &
    2097             :           0.891323_dbl_kind,  0.896388_dbl_kind,  0.949762_dbl_kind, &
    2098             :           0.891340_dbl_kind,  0.896623_dbl_kind,  0.950916_dbl_kind, &
    2099             :           0.891356_dbl_kind,  0.896851_dbl_kind,  0.951945_dbl_kind, &
    2100             :           0.891386_dbl_kind,  0.897399_dbl_kind,  0.954156_dbl_kind/), &
    2101             :           (/nspint,nmbrad/))
    2102             : 
    2103             :       ! inherent optical property (iop) arrays for ice and ponded ice
    2104             :       ! mn = specified mean (or base) value
    2105             :       ! ki = extinction coefficient (/m)
    2106             :       ! wi = single scattering albedo
    2107             :       ! gi = asymmetry parameter
    2108             : 
    2109             :       ! ice surface scattering layer (ssl) iops
    2110             :       real (kind=dbl_kind), dimension (nspint), parameter :: &
    2111             :          ki_ssl_mn = (/ 1000.1_dbl_kind, 1003.7_dbl_kind, 7042._dbl_kind/), &
    2112             :          wi_ssl_mn = (/ .9999_dbl_kind,  .9963_dbl_kind,  .9088_dbl_kind/), &
    2113             :          gi_ssl_mn = (/  .94_dbl_kind,     .94_dbl_kind,    .94_dbl_kind/)
    2114             : 
    2115             :       ! ice drained layer (dl) iops
    2116             :       real (kind=dbl_kind), dimension (nspint), parameter :: &
    2117             :          ki_dl_mn = (/ 100.2_dbl_kind, 107.7_dbl_kind,  1309._dbl_kind /), &
    2118             :          wi_dl_mn = (/ .9980_dbl_kind,  .9287_dbl_kind, .0305_dbl_kind /), &
    2119             :          gi_dl_mn = (/ .94_dbl_kind,     .94_dbl_kind,    .94_dbl_kind /)
    2120             : 
    2121             :       ! ice interior layer (int) iops
    2122             :       real (kind=dbl_kind), dimension (nspint), parameter :: &
    2123             :          ki_int_mn = (/  20.2_dbl_kind,  27.7_dbl_kind,  1445._dbl_kind /), &
    2124             :          wi_int_mn = (/ .9901_dbl_kind, .7223_dbl_kind,  .0277_dbl_kind /), &
    2125             :          gi_int_mn = (/ .94_dbl_kind,    .94_dbl_kind,     .94_dbl_kind /)
    2126             : 
    2127             :       ! ponded ice surface scattering layer (ssl) iops
    2128             :       real (kind=dbl_kind), dimension (nspint), parameter :: &
    2129             :          ki_p_ssl_mn = (/ 70.2_dbl_kind,  77.7_dbl_kind,  1309._dbl_kind/), &
    2130             :          wi_p_ssl_mn = (/ .9972_dbl_kind, .9009_dbl_kind, .0305_dbl_kind/), &
    2131             :          gi_p_ssl_mn = (/ .94_dbl_kind,   .94_dbl_kind,   .94_dbl_kind  /)
    2132             : 
    2133             :       ! ponded ice interior layer (int) iops
    2134             :       real (kind=dbl_kind), dimension (nspint), parameter :: &
    2135             :          ki_p_int_mn = (/  20.2_dbl_kind,  27.7_dbl_kind, 1445._dbl_kind/), &
    2136             :          wi_p_int_mn = (/ .9901_dbl_kind, .7223_dbl_kind, .0277_dbl_kind/), &
    2137             :          gi_p_int_mn = (/ .94_dbl_kind,   .94_dbl_kind,   .94_dbl_kind  /)
    2138             : 
    2139             :       ! inherent optical property (iop) arrays for pond water and underlying ocean
    2140             :       ! kw = Pond water extinction coefficient (/m)
    2141             :       ! ww = Pond water single scattering albedo
    2142             :       ! gw = Pond water asymmetry parameter
    2143             :       real (kind=dbl_kind), dimension (nspint), parameter :: &
    2144             :          kw = (/ 0.20_dbl_kind,   12.0_dbl_kind,   729._dbl_kind /), &
    2145             :          ww = (/ 0.00_dbl_kind,   0.00_dbl_kind,   0.00_dbl_kind /), &
    2146             :          gw = (/ 0.00_dbl_kind,   0.00_dbl_kind,   0.00_dbl_kind /)
    2147             : 
    2148             :       real (kind=dbl_kind), parameter :: &
    2149             :          rhoi   = 917.0_dbl_kind,& ! pure ice mass density (kg/m3)
    2150             :          fr_max = 1.00_dbl_kind, & ! snow grain adjustment factor max
    2151             :          fr_min = 0.80_dbl_kind, & ! snow grain adjustment factor min
    2152             :       ! tuning parameters
    2153             :       ! ice and pond scat coeff fractional change for +- one-sigma in albedo
    2154             :          fp_ice = 0.15_dbl_kind, & ! ice fraction of scat coeff for + stn dev in alb
    2155             :          fm_ice = 0.15_dbl_kind, & ! ice fraction of scat coeff for - stn dev in alb
    2156             :          fp_pnd = 2.00_dbl_kind, & ! ponded ice fraction of scat coeff for + stn dev in alb
    2157             :          fm_pnd = 0.50_dbl_kind    ! ponded ice fraction of scat coeff for - stn dev in alb
    2158             : 
    2159             :       real (kind=dbl_kind),  parameter :: &   !chla-specific absorption coefficient
    2160             :          kchl_tab = 0.01 !0.0023-0.0029 Perovich 1993, also 0.0067 m^2 (mg Chl)^-1
    2161             :                          ! found values of 0.006 to 0.023 m^2/ mg  (676 nm)  Neukermans 2014
    2162             :                          ! and averages over the 300-700nm of 0.0075 m^2/mg in ice Fritsen (2011)
    2163             :                          ! at 440nm values as high as 0.2 m^2/mg in under ice bloom (Balch 2014)
    2164             :                          ! Grenfell 1991 uses 0.004 (m^2/mg) which is (0.0078 * spectral weighting)
    2165             :                          !chlorophyll mass extinction cross section (m^2/mg chla)
    2166             : 
    2167             :       character(len=*),parameter :: subname='(compute_dEdd)'
    2168             : 
    2169             : !-----------------------------------------------------------------------
    2170             : ! Initialize and tune bare ice/ponded ice iops
    2171             : 
    2172    36638086 :       k_bcini(:) = c0
    2173    36638086 :       k_bcins(:) = c0
    2174    36638086 :       k_bcexs(:) = c0
    2175             : 
    2176     3218870 :       rnilyr = c1/real(nilyr,kind=dbl_kind)
    2177     3218870 :       rnslyr = c1/real(nslyr,kind=dbl_kind)
    2178     3218870 :       kii = nslyr + 1
    2179             : 
    2180             :       ! initialize albedos and fluxes to 0
    2181    28969830 :       fthrul = c0                
    2182    25750960 :       Iabs = c0
    2183   136895734 :       kabs_chl(:,:) = c0
    2184   136895734 :       tzaer(:,:) = c0
    2185   136895734 :       wzaer(:,:) = c0
    2186   136895734 :       gzaer(:,:) = c0
    2187             : 
    2188     3218870 :       avdr   = c0
    2189     3218870 :       avdf   = c0
    2190     3218870 :       aidr   = c0
    2191     3218870 :       aidf   = c0
    2192     3218870 :       fsfc   = c0
    2193     3218870 :       fint   = c0
    2194     3218870 :       fthru  = c0
    2195     3218870 :       fthruvdr  = c0
    2196     3218870 :       fthruvdf  = c0
    2197     3218870 :       fthruidr  = c0
    2198     3218870 :       fthruidf  = c0
    2199             :  
    2200             :       ! spectral weights
    2201             :       ! weights 2 (0.7-1.19 micro-meters) and 3 (1.19-5.0 micro-meters) 
    2202             :       ! are chosen based on 1D calculations using ratio of direct to total 
    2203             :       ! near-infrared solar (0.7-5.0 micro-meter) which indicates clear/cloudy 
    2204             :       ! conditions: more cloud, the less 1.19-5.0 relative to the 
    2205             :       ! 0.7-1.19 micro-meter due to cloud absorption.
    2206     3218870 :       wghtns(1) = c1
    2207     3218870 :       wghtns(2) = cp67 + (cp78-cp67)*(c1-fnidr)
    2208             : !      wghtns(3) = cp33 + (cp22-cp33)*(c1-fnidr)
    2209     3218870 :       wghtns(3) = c1 - wghtns(2)
    2210             : 
    2211             :       ! find snow grain adjustment factor, dependent upon clear/overcast sky
    2212             :       ! estimate. comparisons with SNICAR show better agreement with DE when
    2213             :       ! this factor is included (clear sky near 1 and overcast near 0.8 give
    2214             :       ! best agreement).  Multiply by rnsw here for efficiency.
    2215     7668256 :       do k = 1, nslyr
    2216     4449386 :          frsnw(k) = (fr_max*fnidr + fr_min*(c1-fnidr))*rsnw(k)
    2217     7668256 :          Sabs(k) = c0
    2218             :       enddo
    2219             : 
    2220             :       ! layer thicknesses
    2221             :       ! snow
    2222     3218870 :       dz = hs*rnslyr
    2223             :       ! for small enough snow thickness, ssl thickness half of top snow layer
    2224             : !ech: note this is highly resolution dependent!
    2225     3218870 :       dzk(0) = min(hs_ssl, dz/c2)
    2226     3218870 :       dzk(1) = dz - dzk(0)
    2227     3218870 :       if (nslyr > 1) then
    2228     1538145 :          do k = 2, nslyr
    2229     1538145 :             dzk(k) = dz
    2230             :          enddo
    2231             :       endif
    2232             :       
    2233             :       ! ice
    2234     3218870 :       dz = hi*rnilyr
    2235             :       ! empirical reduction in sea ice ssl thickness for ice thinner than 1.5m;
    2236             :       ! factor of 30 gives best albedo comparison with limited observations
    2237     3218870 :       dz_ssl = hi_ssl
    2238             : !ech: note hardwired parameters
    2239             : !         if( hi < 1.5_dbl_kind ) dz_ssl = hi/30._dbl_kind
    2240     3218870 :       dz_ssl = min(hi_ssl, hi/30._dbl_kind)
    2241             :       ! set sea ice ssl thickness to half top layer if sea ice thin enough
    2242             : !ech: note this is highly resolution dependent!
    2243     3218870 :       dz_ssl = min(dz_ssl, dz/c2)
    2244             :       
    2245     3218870 :       dzk(kii)   = dz_ssl
    2246     3218870 :       dzk(kii+1) = dz - dz_ssl
    2247     3218870 :       if (kii+2 <= klev) then
    2248    22532090 :          do k = kii+2, klev
    2249    22532090 :             dzk(k) = dz
    2250             :          enddo
    2251             :       endif
    2252             : 
    2253             :       ! adjust sea ice iops with tuning parameters; tune only the
    2254             :       ! scattering coefficient by factors of R_ice, R_pnd, where
    2255             :       ! R values of +1 correspond approximately to +1 sigma changes in albedo, and
    2256             :       ! R values of -1 correspond approximately to -1 sigma changes in albedo
    2257             :       ! Note: the albedo change becomes non-linear for R values > +1 or < -1
    2258     3218870 :       if( R_ice >= c0 ) then
    2259    12875480 :         do ns = 1, nspint
    2260     9656610 :           sigp       = ki_ssl_mn(ns)*wi_ssl_mn(ns)*(c1+fp_ice*R_ice)
    2261     9656610 :           ki_ssl(ns) = sigp+ki_ssl_mn(ns)*(c1-wi_ssl_mn(ns))
    2262     9656610 :           wi_ssl(ns) = sigp/ki_ssl(ns)
    2263     9656610 :           gi_ssl(ns) = gi_ssl_mn(ns)
    2264             : 
    2265     9656610 :           sigp       = ki_dl_mn(ns)*wi_dl_mn(ns)*(c1+fp_ice*R_ice)
    2266     9656610 :           ki_dl(ns)  = sigp+ki_dl_mn(ns)*(c1-wi_dl_mn(ns))
    2267     9656610 :           wi_dl(ns)  = sigp/ki_dl(ns)
    2268     9656610 :           gi_dl(ns)  = gi_dl_mn(ns)
    2269             : 
    2270     9656610 :           sigp       = ki_int_mn(ns)*wi_int_mn(ns)*(c1+fp_ice*R_ice)
    2271     9656610 :           ki_int(ns) = sigp+ki_int_mn(ns)*(c1-wi_int_mn(ns))
    2272     9656610 :           wi_int(ns) = sigp/ki_int(ns)
    2273    12875480 :           gi_int(ns) = gi_int_mn(ns)
    2274             :         enddo
    2275             :       else !if( R_ice < c0 ) then
    2276           0 :         do ns = 1, nspint
    2277           0 :           sigp       = ki_ssl_mn(ns)*wi_ssl_mn(ns)*(c1+fm_ice*R_ice)
    2278           0 :           sigp       = max(sigp, c0)
    2279           0 :           ki_ssl(ns) = sigp+ki_ssl_mn(ns)*(c1-wi_ssl_mn(ns))
    2280           0 :           wi_ssl(ns) = sigp/ki_ssl(ns)
    2281           0 :           gi_ssl(ns) = gi_ssl_mn(ns)
    2282             : 
    2283           0 :           sigp       = ki_dl_mn(ns)*wi_dl_mn(ns)*(c1+fm_ice*R_ice)
    2284           0 :           sigp       = max(sigp, c0)
    2285           0 :           ki_dl(ns)  = sigp+ki_dl_mn(ns)*(c1-wi_dl_mn(ns))
    2286           0 :           wi_dl(ns)  = sigp/ki_dl(ns)
    2287           0 :           gi_dl(ns)  = gi_dl_mn(ns)
    2288             : 
    2289           0 :           sigp       = ki_int_mn(ns)*wi_int_mn(ns)*(c1+fm_ice*R_ice)
    2290           0 :           sigp       = max(sigp, c0)
    2291           0 :           ki_int(ns) = sigp+ki_int_mn(ns)*(c1-wi_int_mn(ns))
    2292           0 :           wi_int(ns) = sigp/ki_int(ns)
    2293           0 :           gi_int(ns) = gi_int_mn(ns)
    2294             :         enddo
    2295             :       endif          ! adjust ice iops
    2296             : 
    2297             :       ! adjust ponded ice iops with tuning parameters
    2298     3218870 :       if( R_pnd >= c0 ) then
    2299    12875480 :         do ns = 1, nspint
    2300     9656610 :           sigp         = ki_p_ssl_mn(ns)*wi_p_ssl_mn(ns)*(c1+fp_pnd*R_pnd)
    2301     9656610 :           ki_p_ssl(ns) = sigp+ki_p_ssl_mn(ns)*(c1-wi_p_ssl_mn(ns))
    2302     9656610 :           wi_p_ssl(ns) = sigp/ki_p_ssl(ns)
    2303     9656610 :           gi_p_ssl(ns) = gi_p_ssl_mn(ns)
    2304             : 
    2305     9656610 :           sigp         = ki_p_int_mn(ns)*wi_p_int_mn(ns)*(c1+fp_pnd*R_pnd)
    2306     9656610 :           ki_p_int(ns) = sigp+ki_p_int_mn(ns)*(c1-wi_p_int_mn(ns))
    2307     9656610 :           wi_p_int(ns) = sigp/ki_p_int(ns)
    2308    12875480 :           gi_p_int(ns) = gi_p_int_mn(ns)
    2309             :         enddo
    2310             :       else !if( R_pnd < c0 ) then
    2311           0 :         do ns = 1, nspint
    2312           0 :           sigp         = ki_p_ssl_mn(ns)*wi_p_ssl_mn(ns)*(c1+fm_pnd*R_pnd)
    2313           0 :           sigp         = max(sigp, c0)
    2314           0 :           ki_p_ssl(ns) = sigp+ki_p_ssl_mn(ns)*(c1-wi_p_ssl_mn(ns))
    2315           0 :           wi_p_ssl(ns) = sigp/ki_p_ssl(ns)
    2316           0 :           gi_p_ssl(ns) = gi_p_ssl_mn(ns)
    2317             : 
    2318           0 :           sigp         = ki_p_int_mn(ns)*wi_p_int_mn(ns)*(c1+fm_pnd*R_pnd)
    2319           0 :           sigp         = max(sigp, c0)
    2320           0 :           ki_p_int(ns) = sigp+ki_p_int_mn(ns)*(c1-wi_p_int_mn(ns))
    2321           0 :           wi_p_int(ns) = sigp/ki_p_int(ns)
    2322           0 :           gi_p_int(ns) = gi_p_int_mn(ns)
    2323             :         enddo
    2324             :       endif            ! adjust ponded ice iops
    2325             : 
    2326             :       ! use srftyp to determine interface index of surface absorption
    2327     3218870 :       if (srftyp == 1) then
    2328             :          ! snow covered sea ice
    2329     2092288 :          ksrf = 1
    2330             :       else
    2331             :          ! bare sea ice or ponded ice
    2332     1126582 :          ksrf = nslyr + 2 
    2333             :       endif
    2334             : 
    2335     3218870 :       if (tr_bgc_N .and. dEdd_algae) then ! compute kabs_chl for chlorophyll
    2336           0 :           do k = 0, klev
    2337           0 :              kabs_chl(1,k) = kchl_tab*zbio(nlt_chl_sw+k)
    2338             :           enddo
    2339             :       else
    2340     3218870 :             k = klev
    2341     3218870 :             kabs_chl(1,k) = kalg*(0.50_dbl_kind/dzk(k)) 
    2342             :       endif        ! kabs_chl
    2343             : 
    2344             : !mgf++
    2345     3218870 :       if (modal_aero) then
    2346           0 :            do k=0,klev   
    2347           0 :               if (k < nslyr+1) then ! define indices for snow layer
    2348             :                  ! use top rsnw, rhosnw for snow ssl and rest of top layer
    2349           0 :                  ksnow = k - min(k-1,0)
    2350           0 :                  tmp_gs = frsnw(ksnow)
    2351             :                 
    2352             :                  ! get grain size index:
    2353             :                  ! works for 25 < snw_rds < 1625 um:
    2354           0 :                  if (tmp_gs < 125) then
    2355           0 :                    tmp1 = tmp_gs/50
    2356           0 :                    k_bcini(k) = nint(tmp1)
    2357           0 :                  elseif (tmp_gs < 175) then
    2358           0 :                    k_bcini(k) = 2
    2359             :                  else
    2360           0 :                    tmp1 = (tmp_gs/250)+2
    2361           0 :                    k_bcini(k) = nint(tmp1)
    2362             :                  endif
    2363             :               else                  ! use the largest snow grain size for ice
    2364           0 :                  k_bcini(k) = 8
    2365             :               endif
    2366             :               ! Set index corresponding to BC effective radius.  Here,
    2367             :               ! asssume constant BC effective radius of 100nm
    2368             :               ! (corresponding to index 2)
    2369           0 :               k_bcins(k) = 2
    2370           0 :               k_bcexs(k) = 2
    2371             : 
    2372             :               ! check bounds:
    2373           0 :               if (k_bcini(k) < 1)  k_bcini(k) = 1
    2374           0 :               if (k_bcini(k) > 8)  k_bcini(k) = 8
    2375           0 :               if (k_bcins(k) < 1)  k_bcins(k) = 1
    2376           0 :               if (k_bcins(k) > 10) k_bcins(k) = 10
    2377           0 :               if (k_bcexs(k) < 1)  k_bcexs(k) = 1
    2378           0 :               if (k_bcexs(k) > 10) k_bcexs(k) = 10
    2379             : 
    2380             :               ! print ice radius index:
    2381             :               ! write(warnstr,*) subname, "MGFICE2:k, ice index= ",k,  k_bcini(k)
    2382             :               ! call icepack_warnings_add(warnstr)
    2383             :             enddo   ! k
    2384             : 
    2385           0 :         if (tr_zaero .and. dEdd_algae) then ! compute kzaero for chlorophyll  
    2386           0 :         do n = 1,n_zaero        
    2387           0 :            if (n == 1) then ! interstitial BC
    2388           0 :             do k = 0, klev
    2389           0 :                do ns = 1,nspint   ! not weighted by aice
    2390           0 :                   tzaer(ns,k) = tzaer(ns,k)+kaer_bc_tab(ns,k_bcexs(k))* &
    2391           0 :                                 zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2392           0 :                   wzaer(ns,k) = wzaer(ns,k)+kaer_bc_tab(ns,k_bcexs(k))* &
    2393           0 :                                 waer_bc_tab(ns,k_bcexs(k))* &
    2394           0 :                                 zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2395           0 :                   gzaer(ns,k) = gzaer(ns,k)+kaer_bc_tab(ns,k_bcexs(k))* &
    2396           0 :                                 waer_bc_tab(ns,k_bcexs(k))* &
    2397           0 :                                 gaer_bc_tab(ns,k_bcexs(k))*zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2398             :                enddo  ! nspint
    2399             :             enddo
    2400           0 :            elseif (n==2) then ! within-ice BC
    2401           0 :             do k = 0, klev
    2402           0 :                do ns = 1,nspint  
    2403           0 :                   tzaer(ns,k) = tzaer(ns,k)+kaer_bc_tab(ns,k_bcins(k))  * &
    2404           0 :                                 bcenh(ns,k_bcins(k),k_bcini(k))* &
    2405           0 :                                 zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2406           0 :                   wzaer(ns,k) = wzaer(ns,k)+kaer_bc_tab(ns,k_bcins(k))* &
    2407           0 :                                 waer_bc_tab(ns,k_bcins(k))* &
    2408           0 :                                 zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2409           0 :                   gzaer(ns,k) = gzaer(ns,k)+kaer_bc_tab(ns,k_bcins(k))* &
    2410           0 :                                 waer_bc_tab(ns,k_bcins(k))* &
    2411           0 :                                 gaer_bc_tab(ns,k_bcins(k))*zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2412             :                enddo  ! nspint
    2413             :             enddo
    2414             :            else                ! dust
    2415           0 :             do k = 0, klev
    2416           0 :                do ns = 1,nspint   ! not weighted by aice
    2417           0 :                   tzaer(ns,k) = tzaer(ns,k)+kaer_tab(ns,n)* &
    2418           0 :                                    zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2419           0 :                   wzaer(ns,k) = wzaer(ns,k)+kaer_tab(ns,n)*waer_tab(ns,n)* &
    2420           0 :                                    zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2421           0 :                   gzaer(ns,k) = gzaer(ns,k)+kaer_tab(ns,n)*waer_tab(ns,n)* &
    2422           0 :                                    gaer_tab(ns,n)*zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2423             :                enddo  ! nspint
    2424             :             enddo
    2425             :            endif      !(n=1)
    2426             :         enddo         ! n_zaero
    2427             :         endif         ! tr_zaero and dEdd_algae
    2428             : 
    2429             :      else  ! Bulk aerosol treatment
    2430     3218870 :         if (tr_zaero .and. dEdd_algae) then ! compute kzaero for chlorophyll
    2431           0 :         do n = 1,n_zaero          ! multiply by aice?
    2432           0 :             do k = 0, klev
    2433           0 :                do ns = 1,nspint   ! not weighted by aice
    2434           0 :                   tzaer(ns,k) = tzaer(ns,k)+kaer_tab(ns,n)* &
    2435           0 :                                    zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2436           0 :                   wzaer(ns,k) = wzaer(ns,k)+kaer_tab(ns,n)*waer_tab(ns,n)* &
    2437           0 :                                    zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2438           0 :                   gzaer(ns,k) = gzaer(ns,k)+kaer_tab(ns,n)*waer_tab(ns,n)* &
    2439           0 :                                    gaer_tab(ns,n)*zbio(nlt_zaero_sw(n)+k)*dzk(k)
    2440             :                enddo  ! nspint
    2441             :             enddo
    2442             :         enddo
    2443             :         endif  !tr_zaero  
    2444             : 
    2445             :      endif  ! modal_aero
    2446             : 
    2447             : !-----------------------------------------------------------------------
    2448             :  
    2449             :       ! begin spectral loop
    2450    12875480 :       do ns = 1, nspint
    2451             :          
    2452             :          ! set optical properties of air/snow/pond overlying sea ice
    2453             :          ! air
    2454     9656610 :          if( srftyp == 0 ) then
    2455     9804729 :             do k=0,nslyr 
    2456     7317678 :                tau(k) = c0
    2457     7317678 :                w0(k)  = c0
    2458     9804729 :                g(k)   = c0
    2459             :             enddo
    2460             :             ! snow
    2461     7169559 :          else if( srftyp == 1 ) then
    2462             :             ! interpolate snow iops using input snow grain radius,
    2463             :             ! snow density and tabular data
    2464    19214112 :             do k=0,nslyr
    2465             :                ! use top rsnw, rhosnw for snow ssl and rest of top layer
    2466    12937248 :                ksnow = k - min(k-1,0)
    2467             :                ! find snow iops using input snow density and snow grain radius:
    2468    12937248 :                if( frsnw(ksnow) < rsnw_tab(1) ) then
    2469           0 :                   Qs     = Qs_tab(ns,1)
    2470           0 :                   ws     = ws_tab(ns,1)
    2471           0 :                   gs     = gs_tab(ns,1)
    2472    12937248 :                else if( frsnw(ksnow) >= rsnw_tab(nmbrad) ) then
    2473           0 :                   Qs     = Qs_tab(ns,nmbrad)
    2474           0 :                   ws     = ws_tab(ns,nmbrad)
    2475           0 :                   gs     = gs_tab(ns,nmbrad)
    2476             :                else
    2477             :                   ! linear interpolation in rsnw
    2478   413991936 :                   do nr=2,nmbrad
    2479   539686812 :                      if( rsnw_tab(nr-1) <= frsnw(ksnow) .and. &
    2480   151569372 :                          frsnw(ksnow) < rsnw_tab(nr)) then
    2481     8944008 :                         delr = (frsnw(ksnow) - rsnw_tab(nr-1)) / &
    2482    17409252 :                                (rsnw_tab(nr) - rsnw_tab(nr-1))
    2483     4472004 :                         Qs   = Qs_tab(ns,nr-1)*(c1-delr) + &
    2484    17409252 :                                Qs_tab(ns,nr)*delr
    2485     4472004 :                         ws   = ws_tab(ns,nr-1)*(c1-delr) + &
    2486    17409252 :                                ws_tab(ns,nr)*delr
    2487     4472004 :                         gs   = gs_tab(ns,nr-1)*(c1-delr) + &
    2488    17409252 :                                gs_tab(ns,nr)*delr
    2489             :                      endif
    2490             :                   enddo       ! nr
    2491             :                endif
    2492     4472004 :                ks = Qs*((rhosnw(ksnow)/rhoi)*3._dbl_kind / &
    2493    12937248 :                        (4._dbl_kind*frsnw(ksnow)*1.0e-6_dbl_kind))
    2494             : 
    2495    12937248 :                tau(k) = (ks + kabs_chl(ns,k))*dzk(k)
    2496    12937248 :                w0(k)  = ks/(ks + kabs_chl(ns,k)) *ws
    2497    19214112 :                g(k)   = gs
    2498             :             enddo       ! k
    2499             : 
    2500             : 
    2501             :             ! aerosol in snow
    2502     6276864 :              if (tr_zaero .and. dEdd_algae) then 
    2503           0 :                do k = 0,nslyr
    2504           0 :                   gzaer(ns,k) = gzaer(ns,k)/(wzaer(ns,k)+puny)
    2505           0 :                   wzaer(ns,k) = wzaer(ns,k)/(tzaer(ns,k)+puny)
    2506           0 :                   g(k)   = (g(k)*w0(k)*tau(k) + gzaer(ns,k)*wzaer(ns,k)*tzaer(ns,k)) / &
    2507           0 :                                   (w0(k)*tau(k) + wzaer(ns,k)*tzaer(ns,k))
    2508           0 :                   w0(k)  = (w0(k)*tau(k) + wzaer(ns,k)*tzaer(ns,k)) / &
    2509           0 :                                    (tau(k) + tzaer(ns,k))
    2510           0 :                   tau(k) = tau(k) + tzaer(ns,k)
    2511             :                enddo
    2512     6276864 :              elseif (tr_aero) then
    2513      440610 :                   k = 0  ! snow SSL
    2514      440610 :                taer = c0
    2515      440610 :                waer = c0
    2516      440610 :                gaer = c0
    2517             : 
    2518      881220 :                do na=1,4*n_aero,4
    2519             : ! mgf++
    2520      881220 :                if (modal_aero) then
    2521           0 :                   if (na == 1) then  
    2522             :                   !interstitial BC
    2523             :                      taer = taer + &
    2524           0 :                           aero_mp(na)*kaer_bc_tab(ns,k_bcexs(k))
    2525             :                      waer = waer + &
    2526           0 :                           aero_mp(na)*kaer_bc_tab(ns,k_bcexs(k))* &
    2527           0 :                           waer_bc_tab(ns,k_bcexs(k))
    2528             :                      gaer = gaer + &
    2529           0 :                           aero_mp(na)*kaer_bc_tab(ns,k_bcexs(k))* &
    2530           0 :                            waer_bc_tab(ns,k_bcexs(k))*gaer_bc_tab(ns,k_bcexs(k))
    2531           0 :                   elseif (na == 5)then  
    2532             :                   !within-ice BC
    2533             :                       taer = taer + &
    2534           0 :                            aero_mp(na)*kaer_bc_tab(ns,k_bcins(k))* &
    2535           0 :                            bcenh(ns,k_bcins(k),k_bcini(k))
    2536             :                       waer = waer + &
    2537           0 :                            aero_mp(na)*kaer_bc_tab(ns,k_bcins(k))* &
    2538           0 :                            waer_bc_tab(ns,k_bcins(k))
    2539             :                       gaer = gaer + &
    2540           0 :                            aero_mp(na)*kaer_bc_tab(ns,k_bcins(k))* &
    2541           0 :                            waer_bc_tab(ns,k_bcins(k))*gaer_bc_tab(ns,k_bcins(k))
    2542             :                    else
    2543             :                       ! other species (dust)
    2544             :                       taer = taer + &
    2545           0 :                            aero_mp(na)*kaer_tab(ns,(1+(na-1)/4))
    2546             :                       waer = waer + &
    2547           0 :                            aero_mp(na)*kaer_tab(ns,(1+(na-1)/4))* &
    2548           0 :                            waer_tab(ns,(1+(na-1)/4))
    2549             :                       gaer = gaer + &
    2550           0 :                            aero_mp(na)*kaer_tab(ns,(1+(na-1)/4))* &
    2551           0 :                            waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2552             :                    endif
    2553             :                else
    2554             :                   taer = taer + &
    2555      440610 :                        aero_mp(na)*kaer_tab(ns,(1+(na-1)/4))
    2556             :                   waer = waer + &
    2557           0 :                        aero_mp(na)*kaer_tab(ns,(1+(na-1)/4))* &
    2558      440610 :                        waer_tab(ns,(1+(na-1)/4))
    2559             :                   gaer = gaer + &
    2560           0 :                        aero_mp(na)*kaer_tab(ns,(1+(na-1)/4))* &
    2561      440610 :                        waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2562             :                endif  !modal_aero
    2563             : !mgf--
    2564             :                enddo       ! na
    2565      440610 :                gaer = gaer/(waer+puny)
    2566      440610 :                waer = waer/(taer+puny)
    2567             : 
    2568      881220 :                do k=1,nslyr
    2569      440610 :                   taer = c0
    2570      440610 :                   waer = c0
    2571      440610 :                   gaer = c0
    2572      881220 :                   do na=1,4*n_aero,4
    2573      881220 :                   if (modal_aero) then
    2574             : !mgf++
    2575           0 :                     if (na==1) then
    2576             :                       ! interstitial BC
    2577             :                       taer = taer + &
    2578           0 :                            (aero_mp(na+1)/rnslyr)*kaer_bc_tab(ns,k_bcexs(k))
    2579             :                       waer = waer + &
    2580           0 :                            (aero_mp(na+1)/rnslyr)*kaer_bc_tab(ns,k_bcexs(k))* &
    2581           0 :                            waer_bc_tab(ns,k_bcexs(k))
    2582             :                       gaer = gaer + &
    2583           0 :                            (aero_mp(na+1)/rnslyr)*kaer_bc_tab(ns,k_bcexs(k))* &
    2584           0 :                            waer_bc_tab(ns,k_bcexs(k))*gaer_bc_tab(ns,k_bcexs(k))
    2585           0 :                     elseif (na==5) then
    2586             :                       ! within-ice BC
    2587             :                       taer = taer + &
    2588           0 :                            (aero_mp(na+1)/rnslyr)*kaer_bc_tab(ns,k_bcins(k))*&
    2589           0 :                            bcenh(ns,k_bcins(k),k_bcini(k))
    2590             :                       waer = waer + &
    2591           0 :                            (aero_mp(na+1)/rnslyr)*kaer_bc_tab(ns,k_bcins(k))* &
    2592           0 :                            waer_bc_tab(ns,k_bcins(k))
    2593             :                       gaer = gaer + &
    2594           0 :                            (aero_mp(na+1)/rnslyr)*kaer_bc_tab(ns,k_bcins(k))* &
    2595           0 :                            waer_bc_tab(ns,k_bcins(k))*gaer_bc_tab(ns,k_bcins(k))
    2596             :                       
    2597             :                     else
    2598             :                       ! other species (dust)
    2599             :                       taer = taer + &
    2600           0 :                            (aero_mp(na+1)/rnslyr)*kaer_tab(ns,(1+(na-1)/4))
    2601             :                       waer = waer + &
    2602           0 :                            (aero_mp(na+1)/rnslyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2603           0 :                            waer_tab(ns,(1+(na-1)/4))
    2604             :                       gaer = gaer + &
    2605           0 :                            (aero_mp(na+1)/rnslyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2606           0 :                            waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2607             :                     endif   !(na==1)
    2608             : 
    2609             :                   else
    2610             :                      taer = taer + &
    2611      440610 :                             (aero_mp(na+1)*rnslyr)*kaer_tab(ns,(1+(na-1)/4))
    2612             :                      waer = waer + &
    2613           0 :                             (aero_mp(na+1)*rnslyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2614      440610 :                             waer_tab(ns,(1+(na-1)/4))
    2615             :                      gaer = gaer + &
    2616           0 :                             (aero_mp(na+1)*rnslyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2617      440610 :                             waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2618             :                   endif       ! modal_aero
    2619             : !mgf--
    2620             :                   enddo       ! na
    2621      440610 :                   gaer = gaer/(waer+puny)
    2622      440610 :                   waer = waer/(taer+puny)
    2623           0 :                   g(k)   = (g(k)*w0(k)*tau(k) + gaer*waer*taer) / &
    2624      440610 :                            (w0(k)*tau(k) + waer*taer)
    2625           0 :                   w0(k)  = (w0(k)*tau(k) + waer*taer) / &
    2626      440610 :                            (tau(k) + taer)
    2627      881220 :                   tau(k) = tau(k) + taer
    2628             :                enddo       ! k
    2629             :             endif     ! tr_aero
    2630             : 
    2631             :             ! pond
    2632             :          else !if( srftyp == 2 ) then
    2633             :             ! pond water layers evenly spaced
    2634      892695 :             dz = hp/(c1/rnslyr+c1)
    2635     3642537 :             do k=0,nslyr
    2636     2749842 :                tau(k) = kw(ns)*dz
    2637     2749842 :                w0(k)  = ww(ns)
    2638     3642537 :                g(k)   = gw(ns)
    2639             :                ! no aerosol in pond
    2640             :             enddo       ! k
    2641             :          endif        ! srftyp
    2642             :          
    2643             :          ! set optical properties of sea ice
    2644             :          
    2645             :          ! bare or snow-covered sea ice layers
    2646     9656610 :          if( srftyp <= 1 ) then
    2647             :             ! ssl
    2648     8763915 :             k = kii
    2649     8763915 :             tau(k) = (ki_ssl(ns)+kabs_chl(ns,k))*dzk(k)
    2650     8763915 :             w0(k)  = ki_ssl(ns)/(ki_ssl(ns) + kabs_chl(ns,k))*wi_ssl(ns)
    2651     8763915 :             g(k)   = gi_ssl(ns)
    2652             :             ! dl
    2653     8763915 :             k = kii + 1
    2654             :             ! scale dz for dl relative to 4 even-layer-thickness 1.5m case
    2655     8763915 :             fs = p25/rnilyr
    2656     8763915 :             tau(k) = (ki_dl(ns) + kabs_chl(ns,k)) *dzk(k)*fs
    2657     8763915 :             w0(k)  = ki_dl(ns)/(ki_dl(ns) + kabs_chl(ns,k)) *wi_dl(ns)
    2658     8763915 :             g(k)   = gi_dl(ns)
    2659             :             ! int above lowest layer
    2660     8763915 :             if (kii+2 <= klev-1) then
    2661    52583490 :                do k = kii+2, klev-1
    2662    43819575 :                   tau(k) = (ki_int(ns) + kabs_chl(ns,k))*dzk(k)
    2663    43819575 :                   w0(k)  = ki_int(ns)/(ki_int(ns) + kabs_chl(ns,k)) *wi_int(ns)
    2664    52583490 :                   g(k)   = gi_int(ns)
    2665             :                enddo
    2666             :             endif
    2667             :             ! lowest layer
    2668     8763915 :             k = klev
    2669             :             ! add algae to lowest sea ice layer, visible only:
    2670     8763915 :             kabs = ki_int(ns)*(c1-wi_int(ns))
    2671     8763915 :             if( ns == 1 ) then
    2672             :                ! total layer absorption optical depth fixed at value
    2673             :                ! of kalg*0.50m, independent of actual layer thickness
    2674     2921305 :                kabs = kabs + kabs_chl(ns,k) 
    2675             :             endif
    2676     8763915 :             sig        = ki_int(ns)*wi_int(ns)
    2677     8763915 :             tau(k) = (kabs+sig)*dzk(k)
    2678     8763915 :             w0(k)  = (sig/(sig+kabs))
    2679     8763915 :             g(k)   = gi_int(ns)
    2680             :             ! aerosol in sea ice
    2681     8763915 :             if (tr_zaero .and. dEdd_algae) then
    2682           0 :                do k = kii, klev                  
    2683           0 :                   gzaer(ns,k) = gzaer(ns,k)/(wzaer(ns,k)+puny)
    2684           0 :                   wzaer(ns,k) = wzaer(ns,k)/(tzaer(ns,k)+puny)
    2685           0 :                   g(k)   = (g(k)*w0(k)*tau(k) + gzaer(ns,k)*wzaer(ns,k)*tzaer(ns,k)) / &
    2686           0 :                                   (w0(k)*tau(k) + wzaer(ns,k)*tzaer(ns,k))
    2687           0 :                   w0(k)  = (w0(k)*tau(k) + wzaer(ns,k)*tzaer(ns,k)) / &
    2688           0 :                                    (tau(k) + tzaer(ns,k))
    2689           0 :                   tau(k) = tau(k) + tzaer(ns,k)
    2690             :                enddo
    2691     8763915 :             elseif (tr_aero) then 
    2692      551421 :                k = kii   ! sea ice SSL
    2693      551421 :                taer = c0
    2694      551421 :                waer = c0
    2695      551421 :                gaer = c0
    2696     1102842 :                do na=1,4*n_aero,4
    2697             : !mgf++
    2698     1102842 :                if (modal_aero) then
    2699           0 :                   if (na==1) then
    2700             :                   ! interstitial BC
    2701             :                      taer = taer + &
    2702           0 :                           aero_mp(na+2)*kaer_bc_tab(ns,k_bcexs(k))
    2703             :                      waer = waer + &
    2704           0 :                           aero_mp(na+2)*kaer_bc_tab(ns,k_bcexs(k))* &
    2705           0 :                           waer_bc_tab(ns,k_bcexs(k))
    2706             :                      gaer = gaer + &
    2707           0 :                           aero_mp(na+2)*kaer_bc_tab(ns,k_bcexs(k))* &
    2708           0 :                           waer_bc_tab(ns,k_bcexs(k))*gaer_bc_tab(ns,k_bcexs(k))
    2709           0 :                   elseif (na==5) then
    2710             :                   ! within-ice BC
    2711             :                      taer = taer + &
    2712           0 :                           aero_mp(na+2)*kaer_bc_tab(ns,k_bcins(k))* &
    2713           0 :                           bcenh(ns,k_bcins(k),k_bcini(k))
    2714             :                      waer = waer + &
    2715           0 :                           aero_mp(na+2)*kaer_bc_tab(ns,k_bcins(k))* &
    2716           0 :                           waer_bc_tab(ns,k_bcins(k))
    2717             :                      gaer = gaer + &
    2718           0 :                           aero_mp(na+2)*kaer_bc_tab(ns,k_bcins(k))* &
    2719           0 :                           waer_bc_tab(ns,k_bcins(k))*gaer_bc_tab(ns,k_bcins(k))    
    2720             :                   else
    2721             :                   ! other species (dust)
    2722             :                      taer = taer + &
    2723           0 :                           aero_mp(na+2)*kaer_tab(ns,(1+(na-1)/4))
    2724             :                      waer = waer + &
    2725           0 :                           aero_mp(na+2)*kaer_tab(ns,(1+(na-1)/4))* &
    2726           0 :                           waer_tab(ns,(1+(na-1)/4))
    2727             :                      gaer = gaer + &
    2728           0 :                           aero_mp(na+2)*kaer_tab(ns,(1+(na-1)/4))* &
    2729           0 :                           waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2730             :                   endif
    2731             :                else      !bulk
    2732             :                   taer = taer + &
    2733      551421 :                          aero_mp(na+2)*kaer_tab(ns,(1+(na-1)/4))
    2734             :                   waer = waer + &
    2735           0 :                          aero_mp(na+2)*kaer_tab(ns,(1+(na-1)/4))* &
    2736      551421 :                          waer_tab(ns,(1+(na-1)/4))
    2737             :                   gaer = gaer + &
    2738           0 :                          aero_mp(na+2)*kaer_tab(ns,(1+(na-1)/4))* &
    2739      551421 :                          waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2740             :                 endif     ! modal_aero
    2741             : !mgf--
    2742             :                enddo      ! na
    2743             : 
    2744      551421 :                gaer = gaer/(waer+puny)
    2745      551421 :                waer = waer/(taer+puny)
    2746           0 :                g(k)   = (g(k)*w0(k)*tau(k) + gaer*waer*taer) / &
    2747      551421 :                     (w0(k)*tau(k) + waer*taer)
    2748           0 :                w0(k)  = (w0(k)*tau(k) + waer*taer) / &
    2749      551421 :                     (tau(k) + taer)
    2750      551421 :                tau(k) = tau(k) + taer
    2751     4411368 :                do k = kii+1, klev
    2752     3859947 :                   taer = c0
    2753     3859947 :                   waer = c0
    2754     3859947 :                   gaer = c0
    2755     7719894 :                   do na=1,4*n_aero,4
    2756             : !mgf++
    2757     7719894 :                   if (modal_aero) then
    2758           0 :                      if (na==1) then
    2759             :                         ! interstitial BC
    2760             :                         taer = taer + &
    2761           0 :                              (aero_mp(na+3)/rnilyr)*kaer_bc_tab(ns,k_bcexs(k))
    2762             :                         waer = waer + &
    2763           0 :                              (aero_mp(na+3)/rnilyr)*kaer_bc_tab(ns,k_bcexs(k))* &
    2764           0 :                              waer_bc_tab(ns,k_bcexs(k))
    2765             :                         gaer = gaer + &
    2766           0 :                              (aero_mp(na+3)/rnilyr)*kaer_bc_tab(ns,k_bcexs(k))* &
    2767           0 :                              waer_bc_tab(ns,k_bcexs(k))*gaer_bc_tab(ns,k_bcexs(k))
    2768           0 :                      elseif (na==5) then
    2769             :                         ! within-ice BC
    2770             :                         taer = taer + &
    2771           0 :                              (aero_mp(na+3)/rnilyr)*kaer_bc_tab(ns,k_bcins(k))* &
    2772           0 :                              bcenh(ns,k_bcins(k),k_bcini(k))
    2773             :                         waer = waer + &
    2774           0 :                              (aero_mp(na+3)/rnilyr)*kaer_bc_tab(ns,k_bcins(k))* &
    2775           0 :                              waer_bc_tab(ns,k_bcins(k))
    2776             :                         gaer = gaer + &
    2777           0 :                              (aero_mp(na+3)/rnilyr)*kaer_bc_tab(ns,k_bcins(k))* &
    2778           0 :                              waer_bc_tab(ns,k_bcins(k))*gaer_bc_tab(ns,k_bcins(k))
    2779             :                         
    2780             :                      else
    2781             :                         ! other species (dust)
    2782             :                         taer = taer + &
    2783           0 :                              (aero_mp(na+3)/rnilyr)*kaer_tab(ns,(1+(na-1)/4))
    2784             :                         waer = waer + &
    2785           0 :                              (aero_mp(na+3)/rnilyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2786           0 :                              waer_tab(ns,(1+(na-1)/4))
    2787             :                         gaer = gaer + &
    2788           0 :                              (aero_mp(na+3)/rnilyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2789           0 :                              waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2790             :                      endif
    2791             :                   else       !bulk
    2792             : 
    2793             :                      taer = taer + &
    2794     3859947 :                           (aero_mp(na+3)*rnilyr)*kaer_tab(ns,(1+(na-1)/4))
    2795             :                      waer = waer + &
    2796           0 :                           (aero_mp(na+3)*rnilyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2797     3859947 :                           waer_tab(ns,(1+(na-1)/4))
    2798             :                      gaer = gaer + &
    2799           0 :                           (aero_mp(na+3)*rnilyr)*kaer_tab(ns,(1+(na-1)/4))* &
    2800     3859947 :                           waer_tab(ns,(1+(na-1)/4))*gaer_tab(ns,(1+(na-1)/4))
    2801             :                   endif       ! modal_aero
    2802             : !mgf--
    2803             :                   enddo       ! na
    2804     3859947 :                   gaer = gaer/(waer+puny)
    2805     3859947 :                   waer = waer/(taer+puny)
    2806           0 :                   g(k)   = (g(k)*w0(k)*tau(k) + gaer*waer*taer) / &
    2807     3859947 :                            (w0(k)*tau(k) + waer*taer)
    2808           0 :                   w0(k)  = (w0(k)*tau(k) + waer*taer) / &
    2809     3859947 :                            (tau(k) + taer)
    2810     4411368 :                   tau(k) = tau(k) + taer
    2811             :                enddo ! k
    2812             :             endif ! tr_aero
    2813             : 
    2814             :             ! sea ice layers under ponds
    2815             :          else !if( srftyp == 2 ) then
    2816      892695 :             k = kii
    2817      892695 :             tau(k) = ki_p_ssl(ns)*dzk(k)
    2818      892695 :             w0(k)  = wi_p_ssl(ns)
    2819      892695 :             g(k)   = gi_p_ssl(ns)
    2820      892695 :             k = kii + 1
    2821      892695 :             tau(k) = ki_p_int(ns)*dzk(k)
    2822      892695 :             w0(k)  = wi_p_int(ns)
    2823      892695 :             g(k)   = gi_p_int(ns)
    2824      892695 :             if (kii+2 <= klev) then
    2825     6248865 :                do k = kii+2, klev
    2826     5356170 :                   tau(k) = ki_p_int(ns)*dzk(k)
    2827     5356170 :                   w0(k)  = wi_p_int(ns)
    2828     6248865 :                   g(k)   = gi_p_int(ns)
    2829             :                enddo       ! k
    2830             :             endif
    2831             :             ! adjust pond iops if pond depth within specified range
    2832      892695 :             if( hpmin <= hp .and. hp <= hp0 ) then
    2833      651600 :                k = kii
    2834      651600 :                sig_i      = ki_ssl(ns)*wi_ssl(ns)
    2835      651600 :                sig_p      = ki_p_ssl(ns)*wi_p_ssl(ns)
    2836      651600 :                sig        = sig_i + (sig_p-sig_i)*(hp/hp0)
    2837      651600 :                kext       = sig + ki_p_ssl(ns)*(c1-wi_p_ssl(ns))
    2838      651600 :                tau(k) = kext*dzk(k)
    2839      651600 :                w0(k) = sig/kext
    2840      651600 :                g(k)  = gi_p_int(ns)
    2841      651600 :                k = kii + 1
    2842             :                ! scale dz for dl relative to 4 even-layer-thickness 1.5m case
    2843      651600 :                fs = p25/rnilyr
    2844      651600 :                sig_i      = ki_dl(ns)*wi_dl(ns)*fs
    2845      651600 :                sig_p      = ki_p_int(ns)*wi_p_int(ns)
    2846      651600 :                sig        = sig_i + (sig_p-sig_i)*(hp/hp0)
    2847      651600 :                kext       = sig + ki_p_int(ns)*(c1-wi_p_int(ns))
    2848      651600 :                tau(k) = kext*dzk(k)
    2849      651600 :                w0(k) = sig/kext
    2850      651600 :                g(k)  = gi_p_int(ns)
    2851      651600 :                if (kii+2 <= klev) then
    2852     4561200 :                   do k = kii+2, klev
    2853     3909600 :                      sig_i      = ki_int(ns)*wi_int(ns)
    2854     3909600 :                      sig_p      = ki_p_int(ns)*wi_p_int(ns)
    2855     3909600 :                      sig        = sig_i + (sig_p-sig_i)*(hp/hp0)
    2856     3909600 :                      kext       = sig + ki_p_int(ns)*(c1-wi_p_int(ns))
    2857     3909600 :                      tau(k) = kext*dzk(k)
    2858     3909600 :                      w0(k) = sig/kext
    2859     4561200 :                      g(k)  = gi_p_int(ns)
    2860             :                   enddo       ! k
    2861             :                endif
    2862             :             endif        ! small pond depth transition to bare sea ice
    2863             :          endif         ! srftyp  
    2864             :          
    2865             :          ! set reflectivities for ocean underlying sea ice
    2866     9656610 :          rns = real(ns-1, kind=dbl_kind)
    2867     9656610 :          albodr = cp01 * (c1 - min(rns, c1))
    2868     9656610 :          albodf = cp01 * (c1 - min(rns, c1))
    2869             :          
    2870             :          ! layer input properties now completely specified: tau, w0, g,
    2871             :          ! albodr, albodf; now compute the Delta-Eddington solution 
    2872             :          ! reflectivities and transmissivities for each layer; then,
    2873             :          ! combine the layers going downwards accounting for multiple
    2874             :          ! scattering between layers, and finally start from the 
    2875             :          ! underlying ocean and combine successive layers upwards to
    2876             :          ! the surface; see comments in solution_dEdd for more details.
    2877             :          
    2878             :          call solution_dEdd &
    2879             :                (coszen,     srftyp,     klev,       klevp,      nslyr,     &
    2880             :                 tau,        w0,         g,          albodr,     albodf,    &
    2881             :                 trndir,     trntdr,     trndif,     rupdir,     rupdif,    &
    2882     9656610 :                 rdndif)   
    2883     9656610 :          if (icepack_warnings_aborted(subname)) return
    2884             : 
    2885             :          ! the interface reflectivities and transmissivities required
    2886             :          ! to evaluate interface fluxes are returned from solution_dEdd;
    2887             :          ! now compute up and down fluxes for each interface, using the 
    2888             :          ! combined layer properties at each interface:
    2889             :          !
    2890             :          !              layers       interface
    2891             :          !
    2892             :          !       ---------------------  k
    2893             :          !                 k
    2894             :          !       --------------------- 
    2895             :          
    2896   119570868 :          do k = 0, klevp 
    2897             :             ! interface scattering
    2898   109914258 :             refk          = c1/(c1 - rdndif(k)*rupdif(k))
    2899             :             ! dir tran ref from below times interface scattering, plus diff
    2900             :             ! tran and ref from below times interface scattering
    2901             :             ! fdirup(k) = (trndir(k)*rupdir(k) + &
    2902             :             !                 (trntdr(k)-trndir(k))  &
    2903             :             !                 *rupdif(k))*refk
    2904             :             ! dir tran plus total diff trans times interface scattering plus
    2905             :             ! dir tran with up dir ref and down dif ref times interface scattering 
    2906             :             ! fdirdn(k) = trndir(k) + (trntdr(k) &
    2907             :             !               - trndir(k) + trndir(k)  &
    2908             :             !               *rupdir(k)*rdndif(k))*refk
    2909             :             ! diffuse tran ref from below times interface scattering
    2910             :             ! fdifup(k) = trndif(k)*rupdif(k)*refk
    2911             :             ! diffuse tran times interface scattering
    2912             :             ! fdifdn(k) = trndif(k)*refk
    2913             : 
    2914             :             ! dfdir = fdirdn - fdirup
    2915    39009405 :             dfdir(k) = trndir(k) &
    2916    39009405 :                         + (trntdr(k)-trndir(k)) * (c1 - rupdif(k)) * refk &
    2917   109914258 :                         -  trndir(k)*rupdir(k)  * (c1 - rdndif(k)) * refk
    2918   109914258 :             if (dfdir(k) < puny) dfdir(k) = c0 !echmod necessary?
    2919             :             ! dfdif = fdifdn - fdifup
    2920   109914258 :             dfdif(k) = trndif(k) * (c1 - rupdif(k)) * refk
    2921   119570868 :             if (dfdif(k) < puny) dfdif(k) = c0 !echmod necessary?
    2922             :          enddo       ! k 
    2923             :          
    2924             :          ! calculate final surface albedos and fluxes-
    2925             :          ! all absorbed flux above ksrf is included in surface absorption
    2926             :          
    2927    12875480 :          if( ns == 1) then      ! visible
    2928             :             
    2929     3218870 :             swdr = swvdr
    2930     3218870 :             swdf = swvdf
    2931     3218870 :             avdr  = rupdir(0)
    2932     3218870 :             avdf  = rupdif(0)
    2933             :             
    2934     3218870 :             tmp_0  = dfdir(0    )*swdr + dfdif(0    )*swdf
    2935     3218870 :             tmp_ks = dfdir(ksrf )*swdr + dfdif(ksrf )*swdf
    2936     3218870 :             tmp_kl = dfdir(klevp)*swdr + dfdif(klevp)*swdf
    2937             : 
    2938             :             ! for layer biology: save visible only
    2939    28969830 :             do k = nslyr+2, klevp ! Start at DL layer of ice after SSL scattering
    2940    28969830 :                fthrul(k-nslyr-1) = dfdir(k)*swdr + dfdif(k)*swdf
    2941             :             enddo
    2942             :             
    2943     3218870 :             fsfc  = fsfc  + tmp_0  - tmp_ks
    2944     3218870 :             fint  = fint  + tmp_ks - tmp_kl
    2945     3218870 :             fthru = fthru + tmp_kl
    2946     3218870 :             fthruvdr = fthruvdr + dfdir(klevp)*swdr
    2947     3218870 :             fthruvdf = fthruvdf + dfdif(klevp)*swdf
    2948             :             
    2949             :             ! if snow covered ice, set snow internal absorption; else, Sabs=0
    2950     3218870 :             if( srftyp == 1 ) then
    2951     2092288 :                ki = 0
    2952     4312416 :                do k=1,nslyr
    2953             :                   ! skip snow SSL, since SSL absorption included in the surface
    2954             :                   ! absorption fsfc above
    2955     2220128 :                   km  = k
    2956     2220128 :                   kp  = km + 1
    2957     2220128 :                   ki  = ki + 1
    2958      777294 :                   Sabs(ki) = Sabs(ki) &
    2959      777294 :                            +  dfdir(km)*swdr + dfdif(km)*swdf &
    2960     4312416 :                            - (dfdir(kp)*swdr + dfdif(kp)*swdf)
    2961             :                enddo       ! k
    2962             :             endif
    2963             :             
    2964             :             ! complex indexing to insure proper absorptions for sea ice
    2965     3218870 :             ki = 0
    2966    25750960 :             do k=nslyr+2,nslyr+1+nilyr
    2967             :                ! for bare ice, DL absorption for sea ice layer 1
    2968    22532090 :                km = k  
    2969    22532090 :                kp = km + 1
    2970             :                ! modify for top sea ice layer for snow over sea ice
    2971    22532090 :                if( srftyp == 1 ) then
    2972             :                   ! must add SSL and DL absorption for sea ice layer 1
    2973    14646016 :                   if( k == nslyr+2 ) then
    2974     2092288 :                      km = k  - 1
    2975     2092288 :                      kp = km + 2
    2976             :                   endif
    2977             :                endif
    2978    22532090 :                ki = ki + 1
    2979     7999131 :                Iabs(ki) = Iabs(ki) &
    2980     7999131 :                         +  dfdir(km)*swdr + dfdif(km)*swdf &
    2981    25750960 :                         - (dfdir(kp)*swdr + dfdif(kp)*swdf)
    2982             :             enddo       ! k
    2983             :             
    2984             :          else !if(ns > 1) then  ! near IR
    2985             :             
    2986     6437740 :             swdr = swidr
    2987     6437740 :             swdf = swidf
    2988             : 
    2989             :             ! let fr1 = alb_1*swd*wght1 and fr2 = alb_2*swd*wght2 be the ns=2,3
    2990             :             ! reflected fluxes respectively, where alb_1, alb_2 are the band
    2991             :             ! albedos, swd = nir incident shortwave flux, and wght1, wght2 are
    2992             :             ! the 2,3 band weights. thus, the total reflected flux is:
    2993             :             ! fr = fr1 + fr2 = alb_1*swd*wght1 + alb_2*swd*wght2  hence, the
    2994             :             ! 2,3 nir band albedo is alb = fr/swd = alb_1*wght1 + alb_2*wght2
    2995             : 
    2996     6437740 :             aidr   = aidr + rupdir(0)*wghtns(ns)
    2997     6437740 :             aidf   = aidf + rupdif(0)*wghtns(ns)
    2998             : 
    2999     6437740 :             tmp_0  = dfdir(0    )*swdr + dfdif(0    )*swdf
    3000     6437740 :             tmp_ks = dfdir(ksrf )*swdr + dfdif(ksrf )*swdf
    3001     6437740 :             tmp_kl = dfdir(klevp)*swdr + dfdif(klevp)*swdf
    3002             : 
    3003     6437740 :             tmp_0  = tmp_0  * wghtns(ns)
    3004     6437740 :             tmp_ks = tmp_ks * wghtns(ns)
    3005     6437740 :             tmp_kl = tmp_kl * wghtns(ns)
    3006             : 
    3007     6437740 :             fsfc  = fsfc  + tmp_0  - tmp_ks
    3008     6437740 :             fint  = fint  + tmp_ks - tmp_kl
    3009     6437740 :             fthru = fthru + tmp_kl
    3010     6437740 :             fthruidr = fthruidr + dfdir(klevp)*swdr*wghtns(ns)
    3011     6437740 :             fthruidf = fthruidf + dfdif(klevp)*swdf*wghtns(ns)
    3012             : 
    3013             :             ! if snow covered ice, set snow internal absorption; else, Sabs=0
    3014     6437740 :             if( srftyp == 1 ) then
    3015     4184576 :                ki = 0
    3016     8624832 :                do k=1,nslyr
    3017             :                   ! skip snow SSL, since SSL absorption included in the surface
    3018             :                   ! absorption fsfc above
    3019     4440256 :                   km  = k
    3020     4440256 :                   kp  = km + 1
    3021     4440256 :                   ki  = ki + 1
    3022     1554588 :                   Sabs(ki) = Sabs(ki) &
    3023     1554588 :                            + (dfdir(km)*swdr + dfdif(km)*swdf   &
    3024     1554588 :                            - (dfdir(kp)*swdr + dfdif(kp)*swdf)) & 
    3025     8624832 :                            * wghtns(ns)
    3026             :                enddo       ! k
    3027             :             endif
    3028             :             
    3029             :             ! complex indexing to insure proper absorptions for sea ice
    3030     6437740 :             ki = 0
    3031    51501920 :             do k=nslyr+2,nslyr+1+nilyr
    3032             :                ! for bare ice, DL absorption for sea ice layer 1
    3033    45064180 :                km = k  
    3034    45064180 :                kp = km + 1
    3035             :                ! modify for top sea ice layer for snow over sea ice
    3036    45064180 :                if( srftyp == 1 ) then
    3037             :                   ! must add SSL and DL absorption for sea ice layer 1
    3038    29292032 :                   if( k == nslyr+2 ) then
    3039     4184576 :                      km = k  - 1
    3040     4184576 :                      kp = km + 2
    3041             :                   endif
    3042             :                endif
    3043    45064180 :                ki = ki + 1
    3044    15998262 :                Iabs(ki) = Iabs(ki) &
    3045    15998262 :                         + (dfdir(km)*swdr + dfdif(km)*swdf &
    3046    15998262 :                         - (dfdir(kp)*swdr + dfdif(kp)*swdf)) &
    3047    51501920 :                         * wghtns(ns)
    3048             :             enddo       ! k
    3049             :             
    3050             :          endif        ! ns = 1, ns > 1
    3051             :          
    3052             :       enddo         ! end spectral loop  ns
    3053             : 
    3054             :       ! accumulate fluxes over bare sea ice
    3055     3218870 :       alvdr   = avdr
    3056     3218870 :       alvdf   = avdf
    3057     3218870 :       alidr   = aidr
    3058     3218870 :       alidf   = aidf
    3059     3218870 :       fswsfc  = fswsfc  + fsfc *fi
    3060     3218870 :       fswint  = fswint  + fint *fi
    3061     3218870 :       fswthru = fswthru + fthru*fi
    3062     3218870 :       fswthru_vdr = fswthru_vdr + fthruvdr*fi
    3063     3218870 :       fswthru_vdf = fswthru_vdf + fthruvdf*fi
    3064     3218870 :       fswthru_idr = fswthru_idr + fthruidr*fi
    3065     3218870 :       fswthru_idf = fswthru_idf + fthruidf*fi
    3066             : 
    3067     7668256 :       do k = 1, nslyr
    3068     7668256 :          Sswabs(k) = Sswabs(k) + Sabs(k)*fi
    3069             :       enddo                     ! k
    3070             :       
    3071    25750960 :       do k = 1, nilyr
    3072    22532090 :          Iswabs(k) = Iswabs(k) + Iabs(k)*fi
    3073             :          
    3074             :          ! bgc layer 
    3075    22532090 :          fswpenl(k) = fswpenl(k) + fthrul(k)* fi
    3076    25750960 :          if (k == nilyr) then
    3077     3218870 :             fswpenl(k+1) = fswpenl(k+1) + fthrul(k+1)*fi
    3078             :          endif
    3079             :       enddo                     ! k
    3080             :       
    3081             :       !----------------------------------------------------------------
    3082             :       ! if ice has zero heat capacity, no SW can be absorbed 
    3083             :       ! in the ice/snow interior, so add to surface absorption.
    3084             :       ! Note: nilyr = nslyr = 1 for this case
    3085             :       !----------------------------------------------------------------
    3086             : 
    3087     3218870 :       if (.not. heat_capacity) then
    3088             :          
    3089             :          ! SW absorbed at snow/ice surface
    3090           0 :          fswsfc = fswsfc + Iswabs(1) + Sswabs(1)
    3091             :          
    3092             :          ! SW absorbed in ice interior
    3093           0 :          fswint   = c0
    3094           0 :          Iswabs(1) = c0
    3095           0 :          Sswabs(1) = c0
    3096             : 
    3097             :       endif                       ! heat_capacity
    3098             : 
    3099             :       end subroutine compute_dEdd
    3100             : 
    3101             : !=======================================================================
    3102             : !
    3103             : ! Given input vertical profiles of optical properties, evaluate the 
    3104             : ! monochromatic Delta-Eddington solution.
    3105             : !
    3106             : ! author:  Bruce P. Briegleb, NCAR
    3107             : !   2013:  E Hunke merged with NCAR version
    3108     9656610 :       subroutine solution_dEdd                                 &
    3109             :             (coszen,     srftyp,    klev,      klevp,  nslyr,  &
    3110     9656610 :              tau,        w0,        g,         albodr, albodf, &
    3111     9656610 :              trndir,     trntdr,    trndif,    rupdir, rupdif, &
    3112     9656610 :              rdndif)
    3113             : 
    3114             :       real (kind=dbl_kind), intent(in) :: &
    3115             :          coszen      ! cosine solar zenith angle
    3116             : 
    3117             :       integer (kind=int_kind), intent(in) :: &
    3118             :          srftyp   , & ! surface type over ice: (0=air, 1=snow, 2=pond)
    3119             :          klev     , & ! number of radiation layers - 1
    3120             :          klevp    , & ! number of radiation interfaces - 1
    3121             :                       ! (0 layer is included also)
    3122             :          nslyr        ! number of snow layers
    3123             :  
    3124             :       real (kind=dbl_kind), dimension(0:klev), intent(in) :: &
    3125             :          tau     , & ! layer extinction optical depth
    3126             :          w0      , & ! layer single scattering albedo
    3127             :          g           ! layer asymmetry parameter
    3128             :  
    3129             :       real (kind=dbl_kind), intent(in) :: &
    3130             :          albodr  , & ! ocean albedo to direct rad
    3131             :          albodf      ! ocean albedo to diffuse rad
    3132             :  
    3133             :       ! following arrays are defined at model interfaces; 0 is the top of the
    3134             :       ! layer above the sea ice; klevp is the sea ice/ocean interface.
    3135             :       real (kind=dbl_kind), dimension (0:klevp), intent(out) :: &
    3136             :          trndir  , & ! solar beam down transmission from top
    3137             :          trntdr  , & ! total transmission to direct beam for layers above
    3138             :          trndif  , & ! diffuse transmission to diffuse beam for layers above
    3139             :          rupdir  , & ! reflectivity to direct radiation for layers below
    3140             :          rupdif  , & ! reflectivity to diffuse radiation for layers below
    3141             :          rdndif      ! reflectivity to diffuse radiation for layers above
    3142             : 
    3143             : !-----------------------------------------------------------------------
    3144             : !
    3145             : ! Delta-Eddington solution for snow/air/pond over sea ice
    3146             : !
    3147             : ! Generic solution for a snow/air/pond input column of klev+1 layers,
    3148             : ! with srftyp determining at what interface fresnel refraction occurs.
    3149             : !
    3150             : ! Computes layer reflectivities and transmissivities, from the top down
    3151             : ! to the lowest interface using the Delta-Eddington solutions for each
    3152             : ! layer; combines layers from top down to lowest interface, and from the
    3153             : ! lowest interface (underlying ocean) up to the top of the column.
    3154             : !
    3155             : ! Note that layer diffuse reflectivity and transmissivity are computed
    3156             : ! by integrating the direct over several gaussian angles. This is
    3157             : ! because the diffuse reflectivity expression sometimes is negative,
    3158             : ! but the direct reflectivity is always well-behaved. We assume isotropic
    3159             : ! radiation in the upward and downward hemispheres for this integration.
    3160             : !
    3161             : ! Assumes monochromatic (spectrally uniform) properties across a band
    3162             : ! for the input optical parameters.
    3163             : !
    3164             : ! If total transmission of the direct beam to the interface above a particular 
    3165             : ! layer is less than trmin, then no further Delta-Eddington solutions are
    3166             : ! evaluated for layers below.
    3167             : !
    3168             : ! The following describes how refraction is handled in the calculation.
    3169             : !
    3170             : ! First, we assume that radiation is refracted when entering either
    3171             : ! sea ice at the base of the surface scattering layer, or water (i.e. melt
    3172             : ! pond); we assume that radiation does not refract when entering snow, nor 
    3173             : ! upon entering sea ice from a melt pond, nor upon entering the underlying 
    3174             : ! ocean from sea ice.
    3175             : !
    3176             : ! To handle refraction, we define a "fresnel" layer, which physically
    3177             : ! is of neglible thickness and is non-absorbing, which can be combined to 
    3178             : ! any sea ice layer or top of melt pond. The fresnel layer accounts for 
    3179             : ! refraction of direct beam and associated reflection and transmission for
    3180             : ! solar radiation. A fresnel layer is combined with the top of a melt pond 
    3181             : ! or to the surface scattering layer of sea ice if no melt pond lies over it. 
    3182             : !
    3183             : ! Some caution must be exercised for the fresnel layer, because any layer
    3184             : ! to which it is combined is no longer a homogeneous layer, as are all other
    3185             : ! individual layers. For all other layers for example, the direct and diffuse
    3186             : ! reflectivities/transmissivities (R/T) are the same for radiation above or
    3187             : ! below the layer. This is the meaning of homogeneous! But for the fresnel
    3188             : ! layer this is not so. Thus, the R/T for this layer must be distinguished
    3189             : ! for radiation above from that from radiation below. For generality, we
    3190             : ! treat all layers to be combined as inhomogeneous.
    3191             : !
    3192             : !-----------------------------------------------------------------------
    3193             : 
    3194             :       ! local variables
    3195             : 
    3196             :       integer (kind=int_kind) :: &
    3197             :          kfrsnl      ! radiation interface index for fresnel layer
    3198             :  
    3199             :       ! following variables are defined for each layer; 0 refers to the top
    3200             :       ! layer. In general we must distinguish directions above and below in 
    3201             :       ! the diffuse reflectivity and transmissivity, as layers are not assumed
    3202             :       ! to be homogeneous (apart from the single layer Delta-Edd solutions); 
    3203             :       ! the direct is always from above.
    3204             :       real (kind=dbl_kind), dimension (0:klev) :: &
    3205    51466227 :          rdir    , & ! layer reflectivity to direct radiation
    3206    51466227 :          rdif_a  , & ! layer reflectivity to diffuse radiation from above
    3207    51466227 :          rdif_b  , & ! layer reflectivity to diffuse radiation from below
    3208    51466227 :          tdir    , & ! layer transmission to direct radiation (solar beam + diffuse)
    3209    51466227 :          tdif_a  , & ! layer transmission to diffuse radiation from above
    3210    51466227 :          tdif_b  , & ! layer transmission to diffuse radiation from below
    3211    51466227 :          trnlay      ! solar beam transm for layer (direct beam only)
    3212             : 
    3213             :       integer (kind=int_kind) :: & 
    3214             :          k           ! level index
    3215             :  
    3216             :       real (kind=dbl_kind), parameter :: &
    3217             :          trmin = 0.001_dbl_kind   ! minimum total transmission allowed
    3218             :       ! total transmission is that due to the direct beam; i.e. it includes
    3219             :       ! both the directly transmitted solar beam and the diffuse downwards
    3220             :       ! transmitted radiation resulting from scattering out of the direct beam 
    3221             :       real (kind=dbl_kind) :: &
    3222     3428199 :          tautot   , & ! layer optical depth
    3223     3428199 :          wtot     , & ! layer single scattering albedo
    3224     3428199 :          gtot     , & ! layer asymmetry parameter
    3225     3428199 :          ftot     , & ! layer forward scattering fraction
    3226     3428199 :          ts       , & ! layer scaled extinction optical depth
    3227     3428199 :          ws       , & ! layer scaled single scattering albedo
    3228     3428199 :          gs       , & ! layer scaled asymmetry parameter
    3229     3428199 :          rintfc   , & ! reflection (multiple) at an interface
    3230     3428199 :          refkp1   , & ! interface multiple scattering for k+1
    3231     3428199 :          refkm1   , & ! interface multiple scattering for k-1
    3232     3428199 :          tdrrdir  , & ! direct tran times layer direct ref 
    3233     3428199 :          tdndif       ! total down diffuse = tot tran - direct tran
    3234             :  
    3235             :       ! perpendicular and parallel relative to plane of incidence and scattering
    3236             :       real (kind=dbl_kind) :: &
    3237     3428199 :          R1       , & ! perpendicular polarization reflection amplitude
    3238     3428199 :          R2       , & ! parallel polarization reflection amplitude
    3239     3428199 :          T1       , & ! perpendicular polarization transmission amplitude
    3240     3428199 :          T2       , & ! parallel polarization transmission amplitude
    3241     3428199 :          Rf_dir_a , & ! fresnel reflection to direct radiation
    3242     3428199 :          Tf_dir_a , & ! fresnel transmission to direct radiation
    3243     3428199 :          Rf_dif_a , & ! fresnel reflection to diff radiation from above
    3244     3428199 :          Rf_dif_b , & ! fresnel reflection to diff radiation from below
    3245     3428199 :          Tf_dif_a , & ! fresnel transmission to diff radiation from above
    3246     3428199 :          Tf_dif_b     ! fresnel transmission to diff radiation from below
    3247             :  
    3248             :       ! refractive index for sea ice, water; pre-computed, band-independent,
    3249             :       ! diffuse fresnel reflectivities
    3250             :       real (kind=dbl_kind), parameter :: & 
    3251             :          refindx = 1.310_dbl_kind  , & ! refractive index of sea ice (water also)
    3252             :          cp063   = 0.063_dbl_kind  , & ! diffuse fresnel reflectivity from above
    3253             :          cp455   = 0.455_dbl_kind      ! diffuse fresnel reflectivity from below
    3254             :  
    3255             :       real (kind=dbl_kind) :: &
    3256     3428199 :          mu0      , & ! cosine solar zenith angle incident
    3257     3428199 :          mu0nij       ! cosine solar zenith angle in medium below fresnel level
    3258             :  
    3259             :       real (kind=dbl_kind) :: &
    3260     3428199 :          mu0n         ! cosine solar zenith angle in medium
    3261             :  
    3262             :       real (kind=dbl_kind) :: &
    3263     3428199 :          alp      , & ! temporary for alpha
    3264     3428199 :          gam      , & ! temporary for agamm
    3265     3428199 :          lm       , & ! temporary for el
    3266     3428199 :          mu       , & ! temporary for gauspt
    3267     3428199 :          ne       , & ! temporary for n
    3268     3428199 :          ue       , & ! temporary for u
    3269     3428199 :          extins   , & ! extinction
    3270     3428199 :          amg      , & ! alp - gam
    3271     3428199 :          apg          ! alp + gam
    3272             : 
    3273             :       integer (kind=int_kind), parameter :: &
    3274             :          ngmax = 8    ! number of gaussian angles in hemisphere
    3275             : 
    3276             :       real (kind=dbl_kind), dimension (ngmax), parameter :: &
    3277             :          gauspt     & ! gaussian angles (radians)
    3278             :             = (/ .9894009_dbl_kind,  .9445750_dbl_kind, &
    3279             :                  .8656312_dbl_kind,  .7554044_dbl_kind, &
    3280             :                  .6178762_dbl_kind,  .4580168_dbl_kind, &
    3281             :                  .2816036_dbl_kind,  .0950125_dbl_kind/), &
    3282             :          gauswt     & ! gaussian weights
    3283             :             = (/ .0271525_dbl_kind,  .0622535_dbl_kind, &
    3284             :                  .0951585_dbl_kind,  .1246290_dbl_kind, &
    3285             :                  .1495960_dbl_kind,  .1691565_dbl_kind, &
    3286             :                  .1826034_dbl_kind,  .1894506_dbl_kind/)
    3287             : 
    3288             :       integer (kind=int_kind) :: &
    3289             :          ng           ! gaussian integration index
    3290             : 
    3291             :       real (kind=dbl_kind) :: &
    3292     3428199 :          gwt      , & ! gaussian weight
    3293     3428199 :          swt      , & ! sum of weights
    3294     3428199 :          trn      , & ! layer transmission
    3295     3428199 :          rdr      , & ! rdir for gaussian integration
    3296     3428199 :          tdr      , & ! tdir for gaussian integration
    3297     3428199 :          smr      , & ! accumulator for rdif gaussian integration
    3298     3428199 :          smt          ! accumulator for tdif gaussian integration
    3299             : 
    3300             :       real (kind=dbl_kind) :: &
    3301     3428199 :          exp_min                    ! minimum exponential value
    3302             : 
    3303             :       character(len=*),parameter :: subname='(solution_dEdd)'
    3304             : 
    3305             : !-----------------------------------------------------------------------
    3306             : 
    3307   119570868 :       do k = 0, klevp 
    3308   109914258 :          trndir(k) = c0
    3309   109914258 :          trntdr(k) = c0
    3310   109914258 :          trndif(k) = c0
    3311   109914258 :          rupdir(k) = c0
    3312   109914258 :          rupdif(k) = c0
    3313   119570868 :          rdndif(k) = c0
    3314             :       enddo
    3315             : 
    3316             :       ! initialize top interface of top layer 
    3317     9656610 :       trndir(0) =   c1
    3318     9656610 :       trntdr(0) =   c1
    3319     9656610 :       trndif(0) =   c1
    3320     9656610 :       rdndif(0) =   c0
    3321             : 
    3322             :       ! mu0 is cosine solar zenith angle above the fresnel level; make 
    3323             :       ! sure mu0 is large enough for stable and meaningful radiation
    3324             :       ! solution: .01 is like sun just touching horizon with its lower edge
    3325     9656610 :       mu0  = max(coszen,p01)
    3326             : 
    3327             :       ! mu0n is cosine solar zenith angle used to compute the layer
    3328             :       ! Delta-Eddington solution; it is initially computed to be the
    3329             :       ! value below the fresnel level, i.e. the cosine solar zenith 
    3330             :       ! angle below the fresnel level for the refracted solar beam:
    3331     9656610 :       mu0nij = sqrt(c1-((c1-mu0**2)/(refindx*refindx)))
    3332             : 
    3333             :       ! compute level of fresnel refraction
    3334             :       ! if ponded sea ice, fresnel level is the top of the pond.
    3335     9656610 :       kfrsnl = 0
    3336             :       ! if snow over sea ice or bare sea ice, fresnel level is
    3337             :       ! at base of sea ice SSL (and top of the sea ice DL); the
    3338             :       ! snow SSL counts for one, then the number of snow layers,
    3339             :       ! then the sea ice SSL which also counts for one:
    3340     9656610 :       if( srftyp < 2 ) kfrsnl = nslyr + 2 
    3341             : 
    3342             :       ! proceed down one layer at a time; if the total transmission to
    3343             :       ! the interface just above a given layer is less than trmin, then no
    3344             :       ! Delta-Eddington computation for that layer is done.
    3345             : 
    3346             :       ! begin main level loop
    3347   109914258 :       do k = 0, klev
    3348             : 
    3349             :          ! initialize all layer apparent optical properties to 0
    3350   100257648 :          rdir  (k) = c0
    3351   100257648 :          rdif_a(k) = c0
    3352   100257648 :          rdif_b(k) = c0
    3353   100257648 :          tdir  (k) = c0
    3354   100257648 :          tdif_a(k) = c0
    3355   100257648 :          tdif_b(k) = c0
    3356   100257648 :          trnlay(k) = c0
    3357             : 
    3358             :          ! compute next layer Delta-eddington solution only if total transmission
    3359             :          ! of radiation to the interface just above the layer exceeds trmin.
    3360             :          
    3361   100257648 :          if (trntdr(k) > trmin ) then
    3362             : 
    3363             :             ! calculation over layers with penetrating radiation
    3364             : 
    3365    52146940 :             tautot  = tau(k)
    3366    52146940 :             wtot    = w0(k)
    3367    52146940 :             gtot    = g(k)
    3368    52146940 :             ftot    = gtot*gtot
    3369             : 
    3370    52146940 :             ts   = taus(wtot,ftot,tautot)
    3371    52146940 :             ws   = omgs(wtot,ftot)
    3372    52146940 :             gs   = asys(gtot,ftot)
    3373    52146940 :             lm   = el(ws,gs)
    3374    52146940 :             ue   = u(ws,gs,lm)
    3375             : 
    3376    52146940 :             mu0n = mu0nij
    3377             :             ! if level k is above fresnel level and the cell is non-pond, use the
    3378             :             ! non-refracted beam instead
    3379    52146940 :             if( srftyp < 2 .and. k < kfrsnl ) mu0n = mu0
    3380             : 
    3381    52146940 :             exp_min = min(exp_argmax,lm*ts)
    3382    52146940 :             extins = exp(-exp_min)
    3383    52146940 :             ne = n(ue,extins)
    3384             : 
    3385             :             ! first calculation of rdif, tdif using Delta-Eddington formulas
    3386             : !            rdif_a(k) = (ue+c1)*(ue-c1)*(c1/extins - extins)/ne
    3387    52146940 :             rdif_a(k) = (ue**2-c1)*(c1/extins - extins)/ne
    3388    52146940 :             tdif_a(k) = c4*ue/ne
    3389             : 
    3390             :             ! evaluate rdir,tdir for direct beam
    3391    52146940 :             exp_min = min(exp_argmax,ts/mu0n)
    3392    52146940 :             trnlay(k) = exp(-exp_min)
    3393    52146940 :             alp = alpha(ws,mu0n,gs,lm)
    3394    52146940 :             gam = agamm(ws,mu0n,gs,lm)
    3395    52146940 :             apg = alp + gam
    3396    52146940 :             amg = alp - gam
    3397    52146940 :             rdir(k) = apg*rdif_a(k) +  amg*(tdif_a(k)*trnlay(k) - c1)
    3398    52146940 :             tdir(k) = apg*tdif_a(k) + (amg* rdif_a(k)-apg+c1)*trnlay(k)
    3399             :             
    3400             :             ! recalculate rdif,tdif using direct angular integration over rdir,tdir,
    3401             :             ! since Delta-Eddington rdif formula is not well-behaved (it is usually
    3402             :             ! biased low and can even be negative); use ngmax angles and gaussian
    3403             :             ! integration for most accuracy:
    3404    52146940 :             R1 = rdif_a(k) ! use R1 as temporary
    3405    52146940 :             T1 = tdif_a(k) ! use T1 as temporary
    3406    52146940 :             swt = c0
    3407    52146940 :             smr = c0
    3408    52146940 :             smt = c0
    3409   469322460 :             do ng=1,ngmax
    3410   417175520 :                mu  = gauspt(ng)
    3411   417175520 :                gwt = gauswt(ng)
    3412   417175520 :                swt = swt + mu*gwt
    3413   417175520 :                exp_min = min(exp_argmax,ts/mu)
    3414   417175520 :                trn = exp(-exp_min)
    3415   417175520 :                alp = alpha(ws,mu,gs,lm)
    3416   417175520 :                gam = agamm(ws,mu,gs,lm)
    3417   417175520 :                apg = alp + gam
    3418   417175520 :                amg = alp - gam
    3419   417175520 :                rdr = apg*R1 + amg*T1*trn - amg
    3420   417175520 :                tdr = apg*T1 + amg*R1*trn - apg*trn + trn
    3421   417175520 :                smr = smr + mu*rdr*gwt
    3422   469322460 :                smt = smt + mu*tdr*gwt
    3423             :             enddo      ! ng
    3424    52146940 :             rdif_a(k) = smr/swt
    3425    52146940 :             tdif_a(k) = smt/swt
    3426             :             
    3427             :             ! homogeneous layer
    3428    52146940 :             rdif_b(k) = rdif_a(k)
    3429    52146940 :             tdif_b(k) = tdif_a(k)
    3430             : 
    3431             :             ! add fresnel layer to top of desired layer if either 
    3432             :             ! air or snow overlies ice; we ignore refraction in ice 
    3433             :             ! if a melt pond overlies it:
    3434             : 
    3435    52146940 :             if( k == kfrsnl ) then
    3436             :                ! compute fresnel reflection and transmission amplitudes
    3437             :                ! for two polarizations: 1=perpendicular and 2=parallel to
    3438             :                ! the plane containing incident, reflected and refracted rays.
    3439             :                R1 = (mu0 - refindx*mu0n) / & 
    3440     5612695 :                     (mu0 + refindx*mu0n)
    3441             :                R2 = (refindx*mu0 - mu0n) / &
    3442     5612695 :                     (refindx*mu0 + mu0n)
    3443             :                T1 = c2*mu0 / &
    3444     5612695 :                     (mu0 + refindx*mu0n)
    3445             :                T2 = c2*mu0 / &
    3446     5612695 :                     (refindx*mu0 + mu0n)
    3447             : 
    3448             :                ! unpolarized light for direct beam
    3449     5612695 :                Rf_dir_a = p5 * (R1*R1 + R2*R2)
    3450     5612695 :                Tf_dir_a = p5 * (T1*T1 + T2*T2)*refindx*mu0n/mu0
    3451             : 
    3452             :                ! precalculated diffuse reflectivities and transmissivities
    3453             :                ! for incident radiation above and below fresnel layer, using
    3454             :                ! the direct albedos and accounting for complete internal
    3455             :                ! reflection from below; precalculated because high order
    3456             :                ! number of gaussian points (~256) is required for convergence:
    3457             : 
    3458             :                ! above
    3459     5612695 :                Rf_dif_a = cp063
    3460     5612695 :                Tf_dif_a = c1 - Rf_dif_a
    3461             :                ! below
    3462     5612695 :                Rf_dif_b = cp455
    3463     5612695 :                Tf_dif_b = c1 - Rf_dif_b
    3464             : 
    3465             :                ! the k = kfrsnl layer properties are updated to combined 
    3466             :                ! the fresnel (refractive) layer, always taken to be above
    3467             :                ! the present layer k (i.e. be the top interface):
    3468             : 
    3469     5612695 :                rintfc   = c1 / (c1-Rf_dif_b*rdif_a(k))
    3470     2073335 :                tdir(k)   = Tf_dir_a*tdir(k) + &
    3471     2073335 :                     Tf_dir_a*rdir(k) * &
    3472     5612695 :                     Rf_dif_b*rintfc*tdif_a(k)
    3473     2073335 :                rdir(k)   = Rf_dir_a + &
    3474     2073335 :                     Tf_dir_a*rdir(k) * &
    3475     5612695 :                     rintfc*Tf_dif_b
    3476     2073335 :                rdif_a(k) = Rf_dif_a + &
    3477     2073335 :                     Tf_dif_a*rdif_a(k) * &
    3478     5612695 :                     rintfc*Tf_dif_b
    3479     2073335 :                rdif_b(k) = rdif_b(k) + &
    3480     2073335 :                     tdif_b(k)*Rf_dif_b * &
    3481     5612695 :                     rintfc*tdif_a(k)
    3482     5612695 :                tdif_a(k) = tdif_a(k)*rintfc*Tf_dif_a
    3483     5612695 :                tdif_b(k) = tdif_b(k)*rintfc*Tf_dif_b
    3484             : 
    3485             :                ! update trnlay to include fresnel transmission
    3486     5612695 :                trnlay(k) = Tf_dir_a*trnlay(k)
    3487             : 
    3488             :             endif      ! k = kfrsnl
    3489             : 
    3490             :          endif ! trntdr(k) > trmin
    3491             :          
    3492             :          ! initialize current layer properties to zero; only if total
    3493             :          ! transmission to the top interface of the current layer exceeds the
    3494             :          ! minimum, will these values be computed below:
    3495             :          ! Calculate the solar beam transmission, total transmission, and
    3496             :          ! reflectivity for diffuse radiation from below at interface k, 
    3497             :          ! the top of the current layer k:
    3498             :          !
    3499             :          !              layers       interface
    3500             :          !         
    3501             :          !       ---------------------  k-1 
    3502             :          !                k-1
    3503             :          !       ---------------------  k
    3504             :          !                 k
    3505             :          !       ---------------------  
    3506             :          !       For k = klevp
    3507             :          ! note that we ignore refraction between sea ice and underlying ocean:
    3508             :          !
    3509             :          !              layers       interface
    3510             :          !
    3511             :          !       ---------------------  k-1 
    3512             :          !                k-1
    3513             :          !       ---------------------  k
    3514             :          !       \\\\\\\ ocean \\\\\\\
    3515             :          
    3516   100257648 :          trndir(k+1) = trndir(k)*trnlay(k)
    3517   100257648 :          refkm1         = c1/(c1 - rdndif(k)*rdif_a(k))
    3518   100257648 :          tdrrdir        = trndir(k)*rdir(k)
    3519   100257648 :          tdndif         = trntdr(k) - trndir(k)
    3520    35581206 :          trntdr(k+1) = trndir(k)*tdir(k) + &
    3521   135838854 :               (tdndif + tdrrdir*rdndif(k))*refkm1*tdif_a(k)
    3522    35581206 :          rdndif(k+1) = rdif_b(k) + &
    3523   135838854 :               (tdif_b(k)*rdndif(k)*refkm1*tdif_a(k))
    3524   109914258 :          trndif(k+1) = trndif(k)*refkm1*tdif_a(k)
    3525             : 
    3526             :       enddo       ! k   end main level loop
    3527             : 
    3528             :       ! compute reflectivity to direct and diffuse radiation for layers 
    3529             :       ! below by adding succesive layers starting from the underlying 
    3530             :       ! ocean and working upwards:
    3531             :       !
    3532             :       !              layers       interface
    3533             :       !
    3534             :       !       ---------------------  k
    3535             :       !                 k
    3536             :       !       ---------------------  k+1
    3537             :       !                k+1
    3538             :       !       ---------------------
    3539             : 
    3540     9656610 :       rupdir(klevp) = albodr
    3541     9656610 :       rupdif(klevp) = albodf 
    3542             : 
    3543   109914258 :       do k=klev,0,-1
    3544             :          ! interface scattering
    3545   100257648 :          refkp1        = c1/( c1 - rdif_b(k)*rupdif(k+1))
    3546             :          ! dir from top layer plus exp tran ref from lower layer, interface
    3547             :          ! scattered and tran thru top layer from below, plus diff tran ref
    3548             :          ! from lower layer with interface scattering tran thru top from below
    3549    35581206 :          rupdir(k) = rdir(k) &
    3550    71162412 :               + (        trnlay(k)  *rupdir(k+1) &
    3551   135838854 :               +  (tdir(k)-trnlay(k))*rupdif(k+1))*refkp1*tdif_b(k)
    3552             :          ! dif from top layer from above, plus dif tran upwards reflected and
    3553             :          ! interface scattered which tran top from below
    3554   109914258 :          rupdif(k) = rdif_a(k) + tdif_a(k)*rupdif(k+1)*refkp1*tdif_b(k)
    3555             :       enddo       ! k
    3556             : 
    3557     9656610 :       end subroutine solution_dEdd
    3558             : 
    3559             : !=======================================================================
    3560             : !
    3561             : !   Set snow horizontal coverage, density and grain radius diagnostically 
    3562             : !   for the Delta-Eddington solar radiation method.
    3563             : !
    3564             : ! author:  Bruce P. Briegleb, NCAR 
    3565             : !   2013:  E Hunke merged with NCAR version
    3566             : 
    3567     3770075 :       subroutine shortwave_dEdd_set_snow(nslyr,    R_snw,    &
    3568             :                                          dT_mlt,   rsnw_mlt, &
    3569             :                                          aice,     vsno,     &
    3570             :                                          Tsfc,     fs,       &
    3571             :                                          hs0,      hs,       &
    3572     3770075 :                                          rhosnw,   rsnw)
    3573             : 
    3574             :       integer (kind=int_kind), intent(in) :: & 
    3575             :          nslyr      ! number of snow layers
    3576             : 
    3577             :       real (kind=dbl_kind), intent(in) :: &
    3578             :          R_snw , & ! snow tuning parameter; +1 > ~.01 change in broadband albedo
    3579             :          dT_mlt, & ! change in temp for non-melt to melt snow grain radius change (C)
    3580             :          rsnw_mlt  ! maximum melting snow grain radius (10^-6 m)
    3581             : 
    3582             :       real (kind=dbl_kind), intent(in) :: &
    3583             :          aice   , & ! concentration of ice
    3584             :          vsno   , & ! volume of snow
    3585             :          Tsfc   , & ! surface temperature 
    3586             :          hs0        ! snow depth for transition to bare sea ice (m)
    3587             : 
    3588             :      real (kind=dbl_kind), intent(inout) :: &
    3589             :          fs     , & ! horizontal coverage of snow
    3590             :          hs         ! snow depth
    3591             : 
    3592             :       real (kind=dbl_kind), dimension (:), intent(out) :: &
    3593             :          rhosnw , & ! density in snow layer (kg/m3)
    3594             :          rsnw       ! grain radius in snow layer (micro-meters)
    3595             : 
    3596             :       ! local variables
    3597             : 
    3598             :       integer (kind=int_kind) :: &
    3599             :          ks           ! snow vertical index
    3600             : 
    3601             :       real (kind=dbl_kind) :: &
    3602     1275244 :          fT  , & ! piecewise linear function of surface temperature
    3603     1275244 :          dTs , & ! difference of Tsfc and Timelt
    3604     1275244 :          rsnw_nm ! actual used nonmelt snow grain radius (micro-meters)
    3605             : 
    3606             :       real (kind=dbl_kind), parameter :: &
    3607             :          ! units for the following are 1.e-6 m (micro-meters)
    3608             :          rsnw_fresh    =  100._dbl_kind, & ! freshly-fallen snow grain radius 
    3609             :          rsnw_nonmelt  =  500._dbl_kind, & ! nonmelt snow grain radius
    3610             :          rsnw_sig      =  250._dbl_kind    ! assumed sigma for snow grain radius
    3611             : 
    3612             :       character(len=*),parameter :: subname='(shortwave_dEdd_set_snow)'
    3613             : 
    3614             : !-----------------------------------------------------------------------
    3615             : 
    3616             :       ! set snow horizontal fraction
    3617     3770075 :       hs = vsno / aice
    3618             :       
    3619     3770075 :       if (hs >= hs_min) then
    3620     2808328 :          fs = c1
    3621     2808328 :          if (hs0 > puny) fs = min(hs/hs0, c1)
    3622             :       endif
    3623             :       
    3624             :       ! bare ice, temperature dependence
    3625     3770075 :       dTs = Timelt - Tsfc
    3626     3770075 :       fT  = -min(dTs/dT_mlt-c1,c0)
    3627             :       ! tune nonmelt snow grain radius if desired: note that
    3628             :       ! the sign is negative so that if R_snw is 1, then the
    3629             :       ! snow grain radius is reduced and thus albedo increased.
    3630     3770075 :       rsnw_nm = rsnw_nonmelt - R_snw*rsnw_sig
    3631     3770075 :       rsnw_nm = max(rsnw_nm, rsnw_fresh)
    3632     3770075 :       rsnw_nm = min(rsnw_nm, rsnw_mlt) 
    3633     8449182 :       do ks = 1, nslyr
    3634             :          ! snow density ccsm3 constant value
    3635     4679107 :          rhosnw(ks) = rhos
    3636             :          ! snow grain radius between rsnw_nonmelt and rsnw_mlt
    3637     4679107 :          rsnw(ks) = rsnw_nm + (rsnw_mlt-rsnw_nm)*fT
    3638     4679107 :          rsnw(ks) = max(rsnw(ks), rsnw_fresh)
    3639     8449182 :          rsnw(ks) = min(rsnw(ks), rsnw_mlt)
    3640             :       enddo        ! ks
    3641             : 
    3642     3770075 :       end subroutine shortwave_dEdd_set_snow
    3643             : 
    3644             : !=======================================================================
    3645             : !
    3646             : !   Set pond fraction and depth diagnostically for
    3647             : !   the Delta-Eddington solar radiation method.
    3648             : !
    3649             : ! author:  Bruce P. Briegleb, NCAR 
    3650             : !   2013:  E Hunke merged with NCAR version
    3651             : 
    3652       72399 :       subroutine shortwave_dEdd_set_pond(Tsfc,               &
    3653             :                                          fs,       fp,       &
    3654             :                                          hp)
    3655             : 
    3656             :       real (kind=dbl_kind), intent(in) :: &
    3657             :          Tsfc   , & ! surface temperature
    3658             :          fs         ! horizontal coverage of snow
    3659             : 
    3660             :       real (kind=dbl_kind), intent(out) :: &
    3661             :          fp     , & ! pond fractional coverage (0 to 1)
    3662             :          hp         ! pond depth (m)
    3663             : 
    3664             :       ! local variables
    3665             : 
    3666             :       real (kind=dbl_kind) :: &
    3667       26280 :          fT  , & ! piecewise linear function of surface temperature
    3668       26280 :          dTs     ! difference of Tsfc and Timelt
    3669             : 
    3670             :       real (kind=dbl_kind), parameter :: &
    3671             :          dT_pnd = c1   ! change in temp for pond fraction and depth
    3672             : 
    3673             :       character(len=*),parameter :: subname='(shortwave_dEdd_set_pond)'
    3674             : 
    3675             : !-----------------------------------------------------------------------
    3676             : 
    3677             :       ! bare ice, temperature dependence
    3678       72399 :       dTs = Timelt - Tsfc
    3679       72399 :       fT  = -min(dTs/dT_pnd-c1,c0)
    3680             :       ! pond
    3681       72399 :       fp = 0.3_dbl_kind*fT*(c1-fs)
    3682       72399 :       hp = 0.3_dbl_kind*fT*(c1-fs)
    3683             :       
    3684       72399 :       end subroutine shortwave_dEdd_set_pond
    3685             : 
    3686             : ! End Delta-Eddington shortwave method
    3687             : 
    3688             : !=======================================================================
    3689             : !
    3690             : ! authors     Nicole Jeffery, LANL
    3691             : 
    3692           0 :       subroutine compute_shortwave_trcr(nslyr,               &
    3693           0 :                                     bgcN,         zaero,     &
    3694           0 :                                     trcrn_bgcsw,             &
    3695           0 :                                     sw_grid,      hin,       &
    3696             :                                     hbri,                    &
    3697             :                                     nilyr,        nblyr,     &
    3698           0 :                                     i_grid,                  &
    3699             :                                     skl_bgc,      z_tracers  )
    3700             :       
    3701             :       integer (kind=int_kind), intent(in) :: &
    3702             :          nslyr          ! number of snow layers
    3703             : 
    3704             :       integer (kind=int_kind), intent(in) :: &
    3705             :          nblyr      , & ! number of bio layers
    3706             :          nilyr          ! number of ice layers 
    3707             : 
    3708             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
    3709             :          bgcN       , & ! Nit tracer
    3710             :          zaero          ! zaero tracer
    3711             : 
    3712             :       real (kind=dbl_kind), dimension (:), intent(out):: &
    3713             :          trcrn_bgcsw    ! ice on shortwave grid tracers
    3714             : 
    3715             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
    3716             :          sw_grid     , & ! 
    3717             :          i_grid          ! CICE bio grid
    3718             :          
    3719             :       real(kind=dbl_kind), intent(in) :: &
    3720             :          hin         , & ! CICE ice thickness
    3721             :          hbri            ! brine height
    3722             : 
    3723             :       logical (kind=log_kind), intent(in) :: &
    3724             :          skl_bgc     , & ! skeletal layer bgc
    3725             :          z_tracers       ! zbgc
    3726             : 
    3727             :       !  local variables
    3728             : 
    3729             :       integer (kind=int_kind) :: k, n, nn
    3730             : 
    3731             :       real (kind=dbl_kind), dimension (ntrcr+2) :: &
    3732           0 :          trtmp0, &      ! temporary, remapped tracers
    3733           0 :          trtmp
    3734             : 
    3735             :       real (kind=dbl_kind), dimension (nilyr+1):: &
    3736           0 :          icegrid        ! correct for large ice surface layers
    3737             : 
    3738             :       real (kind=dbl_kind):: &
    3739           0 :          top_conc       ! 1% (min_bgc) of surface concentration 
    3740             :                         ! when hin > hbri:  just used in sw calculation
    3741             : 
    3742             :       character(len=*),parameter :: subname='(compute_shortwave_trcr)'
    3743             : 
    3744             :       !-----------------------------------------------------------------
    3745             :       ! Compute aerosols and algal chlorophyll on shortwave grid
    3746             :       !-----------------------------------------------------------------
    3747             : 
    3748           0 :       trtmp0(:) = c0
    3749           0 :       trtmp(:) = c0
    3750           0 :       trcrn_bgcsw(:) = c0
    3751             : 
    3752           0 :       do k = 1,nilyr+1
    3753           0 :          icegrid(k) = sw_grid(k)
    3754             :       enddo    
    3755           0 :       if (sw_grid(1)*hin*c2 > hi_ssl) then
    3756           0 :          icegrid(1) = hi_ssl/c2/hin
    3757             :       endif
    3758             : 
    3759           0 :       if (z_tracers) then
    3760           0 :       if (tr_bgc_N)  then
    3761           0 :          if (size(bgcN) < n_algae*(nblyr+3)) then
    3762           0 :             call icepack_warnings_add(subname//' ERROR: size(bgcN) too small')
    3763           0 :             call icepack_warnings_setabort(.true.,__FILE__,__LINE__)
    3764           0 :             return
    3765             :          endif
    3766             : 
    3767           0 :          do k = 1, nblyr+1
    3768           0 :             do n = 1, n_algae
    3769           0 :                trtmp0(nt_bgc_N(1) + k-1) = trtmp0(nt_bgc_N(1) + k-1) + &
    3770           0 :                                 R_chl2N(n)*F_abs_chl(n)*bgcN(nt_bgc_N(n)-nt_bgc_N(1)+1 + k-1)
    3771             :             enddo ! n
    3772             :          enddo    ! k
    3773             :  
    3774           0 :          top_conc = trtmp0(nt_bgc_N(1))*min_bgc
    3775             :          call remap_zbgc (nilyr+1, &
    3776             :                           nt_bgc_N(1),                &
    3777           0 :                           trtmp0(1:ntrcr  ),          &
    3778           0 :                           trtmp (1:ntrcr+2),          &
    3779             :                           1,                 nblyr+1, &
    3780             :                           hin,               hbri,    &
    3781           0 :                           icegrid(1:nilyr+1),         &
    3782           0 :                           i_grid(1:nblyr+1), top_conc ) 
    3783           0 :          if (icepack_warnings_aborted(subname)) return
    3784             : 
    3785           0 :          do k = 1, nilyr+1
    3786           0 :             trcrn_bgcsw(nlt_chl_sw+nslyr+k) = trtmp(nt_bgc_N(1) + k-1)
    3787             :          enddo       ! k
    3788             : 
    3789           0 :          do n = 1, n_algae   ! snow contribution
    3790           0 :             trcrn_bgcsw(nlt_chl_sw)= trcrn_bgcsw(nlt_chl_sw) &
    3791           0 :                      + R_chl2N(n)*F_abs_chl(n)*bgcN(nt_bgc_N(n)-nt_bgc_N(1)+1+nblyr+1)
    3792             :                               ! snow surface layer
    3793           0 :             trcrn_bgcsw(nlt_chl_sw+1:nlt_chl_sw+nslyr) = &
    3794           0 :                      trcrn_bgcsw(nlt_chl_sw+1:nlt_chl_sw+nslyr) &
    3795           0 :                      + R_chl2N(n)*F_abs_chl(n)*bgcN(nt_bgc_N(n)-nt_bgc_N(1)+1+nblyr+2)
    3796             :                               ! only 1 snow layer in zaero
    3797             :          enddo ! n
    3798             :       endif    ! tr_bgc_N
    3799             : 
    3800           0 :       if (tr_zaero) then
    3801           0 :          if (size(zaero) < n_zaero*(nblyr+3)) then
    3802           0 :             call icepack_warnings_add(subname//' ERROR: size(zaero) too small')
    3803           0 :             call icepack_warnings_setabort(.true.,__FILE__,__LINE__)
    3804           0 :             return
    3805             :          endif
    3806             : 
    3807           0 :          do n = 1, n_zaero
    3808             : 
    3809           0 :             trtmp0(:) = c0
    3810           0 :             trtmp(:) = c0
    3811             : 
    3812           0 :             do k = 1, nblyr+1
    3813           0 :                trtmp0(nt_zaero(n) + k-1) = zaero(nt_zaero(n)-nt_zaero(1)+1+k-1)
    3814             :             enddo
    3815             : 
    3816           0 :             top_conc = trtmp0(nt_zaero(n))*min_bgc
    3817             :             call remap_zbgc (nilyr+1, &
    3818           0 :                              nt_zaero(n),                &
    3819           0 :                              trtmp0(1:ntrcr  ),          &
    3820           0 :                              trtmp (1:ntrcr+2),          &
    3821             :                              1,                 nblyr+1, &
    3822             :                              hin,               hbri,    &
    3823           0 :                              icegrid(1:nilyr+1),         &
    3824           0 :                              i_grid(1:nblyr+1), top_conc )
    3825           0 :             if (icepack_warnings_aborted(subname)) return
    3826             : 
    3827           0 :             do k = 1,nilyr+1
    3828           0 :                trcrn_bgcsw(nlt_zaero_sw(n)+nslyr+k) = trtmp(nt_zaero(n) + k-1)
    3829             :             enddo
    3830           0 :             trcrn_bgcsw(nlt_zaero_sw(n))= zaero(nt_zaero(n)-nt_zaero(1)+1+nblyr+1) !snow ssl
    3831           0 :             trcrn_bgcsw(nlt_zaero_sw(n)+1:nlt_zaero_sw(n)+nslyr)= zaero(nt_zaero(n)-nt_zaero(1)+1+nblyr+2)
    3832             :          enddo ! n
    3833             :       endif    ! tr_zaero
    3834           0 :       elseif (skl_bgc) then
    3835             : 
    3836           0 :          do nn = 1,n_algae
    3837           0 :             trcrn_bgcsw(nbtrcr_sw) = trcrn_bgcsw(nbtrcr_sw) &
    3838           0 :                                 + F_abs_chl(nn)*R_chl2N(nn) &
    3839           0 :                                 * bgcN(nt_bgc_N(nn)-nt_bgc_N(1)+1)*sk_l/hin &
    3840           0 :                                 * real(nilyr,kind=dbl_kind)
    3841             :          enddo 
    3842             : 
    3843             :       endif
    3844             :       end subroutine compute_shortwave_trcr
    3845             : 
    3846             : !=======================================================================
    3847             : !autodocument_start icepack_prep_radiation
    3848             : ! Scales radiation fields computed on the previous time step.
    3849             : !
    3850             : ! authors: Elizabeth Hunke, LANL
    3851             : 
    3852     1370160 :       subroutine icepack_prep_radiation (ncat, nilyr, nslyr,   &
    3853     1370160 :                                         aice,        aicen,    &
    3854             :                                         swvdr,       swvdf,    &
    3855             :                                         swidr,       swidf,    &
    3856             :                                         alvdr_ai,    alvdf_ai, &
    3857             :                                         alidr_ai,    alidf_ai, &
    3858             :                                         scale_factor,          &
    3859     1370160 :                                         fswsfcn,     fswintn,  &
    3860     1370160 :                                         fswthrun,              &
    3861     1370160 :                                         fswthrun_vdr,          &
    3862     1370160 :                                         fswthrun_vdf,          &
    3863     1370160 :                                         fswthrun_idr,          &
    3864     1370160 :                                         fswthrun_idf,          &
    3865     1370160 :                                         fswpenln,              &
    3866     1370160 :                                         Sswabsn,     Iswabsn)
    3867             : 
    3868             :       integer (kind=int_kind), intent(in) :: &
    3869             :          ncat    , & ! number of ice thickness categories
    3870             :          nilyr   , & ! number of ice layers
    3871             :          nslyr       ! number of snow layers
    3872             : 
    3873             :       real (kind=dbl_kind), intent(in) :: &
    3874             :          aice        , & ! ice area fraction
    3875             :          swvdr       , & ! sw down, visible, direct  (W/m^2)
    3876             :          swvdf       , & ! sw down, visible, diffuse (W/m^2)
    3877             :          swidr       , & ! sw down, near IR, direct  (W/m^2)
    3878             :          swidf       , & ! sw down, near IR, diffuse (W/m^2)
    3879             :          ! grid-box-mean albedos aggregated over categories (if calc_Tsfc)
    3880             :          alvdr_ai    , & ! visible, direct   (fraction)
    3881             :          alidr_ai    , & ! near-ir, direct   (fraction)
    3882             :          alvdf_ai    , & ! visible, diffuse  (fraction)
    3883             :          alidf_ai        ! near-ir, diffuse  (fraction)
    3884             : 
    3885             :       real (kind=dbl_kind), dimension(:), intent(in) :: &
    3886             :          aicen           ! ice area fraction in each category
    3887             : 
    3888             :       real (kind=dbl_kind), intent(inout) :: &
    3889             :          scale_factor    ! shortwave scaling factor, ratio new:old
    3890             : 
    3891             :       real (kind=dbl_kind), dimension(:), intent(inout) :: &
    3892             :          fswsfcn     , & ! SW absorbed at ice/snow surface (W m-2)
    3893             :          fswintn     , & ! SW absorbed in ice interior, below surface (W m-2)
    3894             :          fswthrun        ! SW through ice to ocean (W/m^2)
    3895             : 
    3896             :       real (kind=dbl_kind), dimension(:), intent(inout), optional :: &
    3897             :          fswthrun_vdr , & ! vis dir SW through ice to ocean (W/m^2)
    3898             :          fswthrun_vdf , & ! vis dif SW through ice to ocean (W/m^2)
    3899             :          fswthrun_idr , & ! nir dir SW through ice to ocean (W/m^2)
    3900             :          fswthrun_idf     ! nir dif SW through ice to ocean (W/m^2)
    3901             : 
    3902             :       real (kind=dbl_kind), dimension(:,:), intent(inout) :: &
    3903             :          fswpenln    , & ! visible SW entering ice layers (W m-2)
    3904             :          Iswabsn     , & ! SW radiation absorbed in ice layers (W m-2)
    3905             :          Sswabsn         ! SW radiation absorbed in snow layers (W m-2)
    3906             : 
    3907             : !autodocument_end
    3908             : 
    3909             :       ! local variables
    3910             : 
    3911             :       integer (kind=int_kind) :: &
    3912             :          k           , & ! vertical index       
    3913             :          n               ! thickness category index
    3914             : 
    3915      474288 :       real (kind=dbl_kind) :: netsw 
    3916             : 
    3917             :       character(len=*),parameter :: subname='(icepack_prep_radiation)'
    3918             : 
    3919             :       !-----------------------------------------------------------------
    3920             :       ! Compute netsw scaling factor (new netsw / old netsw)
    3921             :       !-----------------------------------------------------------------
    3922             : 
    3923     1370160 :          if (aice > c0 .and. scale_factor > puny) then
    3924             :             netsw = swvdr*(c1 - alvdr_ai) &
    3925             :                   + swvdf*(c1 - alvdf_ai) &
    3926             :                   + swidr*(c1 - alidr_ai) &
    3927      741605 :                   + swidf*(c1 - alidf_ai)
    3928      741605 :             scale_factor = netsw / scale_factor
    3929             :          else
    3930      628555 :             scale_factor = c1
    3931             :          endif
    3932             : 
    3933     7834848 :          do n = 1, ncat
    3934             : 
    3935     7834848 :             if (aicen(n) > puny) then
    3936             : 
    3937             :       !-----------------------------------------------------------------
    3938             :       ! Scale absorbed solar radiation for change in net shortwave
    3939             :       !-----------------------------------------------------------------
    3940             : 
    3941     4360794 :                fswsfcn(n)  = scale_factor*fswsfcn (n)
    3942     4360794 :                fswintn(n)  = scale_factor*fswintn (n)
    3943     4360794 :                fswthrun(n) = scale_factor*fswthrun(n)
    3944     4360794 :                if (present(fswthrun_vdr)) fswthrun_vdr(n) = scale_factor*fswthrun_vdr(n)
    3945     4360794 :                if (present(fswthrun_vdf)) fswthrun_vdf(n) = scale_factor*fswthrun_vdf(n)
    3946     4360794 :                if (present(fswthrun_idr)) fswthrun_idr(n) = scale_factor*fswthrun_idr(n)
    3947     4360794 :                if (present(fswthrun_idf)) fswthrun_idf(n) = scale_factor*fswthrun_idf(n)
    3948    37427508 :                do k = 1,nilyr+1
    3949    37427508 :                   fswpenln(k,n) = scale_factor*fswpenln(k,n)
    3950             :                enddo       !k
    3951     9630500 :                do k=1,nslyr
    3952     9630500 :                   Sswabsn(k,n) = scale_factor*Sswabsn(k,n)
    3953             :                enddo
    3954    33066714 :                do k=1,nilyr
    3955    33066714 :                   Iswabsn(k,n) = scale_factor*Iswabsn(k,n)
    3956             :                enddo
    3957             : 
    3958             :             endif
    3959             :          enddo                  ! ncat
    3960             : 
    3961     1370160 :       end subroutine icepack_prep_radiation
    3962             : 
    3963             : !=======================================================================
    3964             : !autodocument_start icepack_step_radiation
    3965             : ! Computes radiation fields
    3966             : !
    3967             : ! authors: William H. Lipscomb, LANL
    3968             : !          David Bailey, NCAR
    3969             : !          Elizabeth C. Hunke, LANL
    3970             : 
    3971     1027755 :       subroutine icepack_step_radiation (dt,       ncat,     &
    3972             :                                         nblyr,               &
    3973             :                                         nilyr,    nslyr,     &
    3974             :                                         dEdd_algae,          &
    3975     1027755 :                                         swgrid,   igrid,     &
    3976     1027755 :                                         fbri,                &
    3977     1027755 :                                         aicen,    vicen,     &
    3978     1027755 :                                         vsnon,    Tsfcn,     &
    3979     1027755 :                                         alvln,    apndn,     &
    3980     1027755 :                                         hpndn,    ipndn,     &
    3981     1027755 :                                         aeron,               &
    3982     1027755 :                                         bgcNn,    zaeron,    &
    3983     1027755 :                                         trcrn_bgcsw,         &
    3984             :                                         TLAT,     TLON,      &
    3985           0 :                                         calendar_type,       &
    3986             :                                         days_per_year,       &
    3987             :                                         nextsw_cday,         &
    3988             :                                         yday,     sec,       &
    3989     1027755 :                                         kaer_tab, waer_tab,  &
    3990     1027755 :                                         gaer_tab,            &
    3991     1027755 :                                         kaer_bc_tab,         &
    3992     1027755 :                                         waer_bc_tab,         &
    3993     1027755 :                                         gaer_bc_tab,         &
    3994     1027755 :                                         bcenh,               &
    3995             :                                         modal_aero,          &
    3996             :                                         swvdr,    swvdf,     &
    3997             :                                         swidr,    swidf,     &
    3998             :                                         coszen,   fsnow,     &
    3999     2055510 :                                         alvdrn,   alvdfn,    &
    4000     2055510 :                                         alidrn,   alidfn,    &
    4001     1027755 :                                         fswsfcn,  fswintn,   &
    4002     1027755 :                                         fswthrun,            &
    4003     1027755 :                                         fswthrun_vdr,        &
    4004     1027755 :                                         fswthrun_vdf,        &
    4005     1027755 :                                         fswthrun_idr,        &
    4006     1027755 :                                         fswthrun_idf,        &
    4007     1027755 :                                         fswpenln,            &
    4008     1027755 :                                         Sswabsn,  Iswabsn,   &
    4009     1027755 :                                         albicen,  albsnon,   &
    4010     1027755 :                                         albpndn,  apeffn,    &
    4011     1027755 :                                         snowfracn,           &
    4012     1027755 :                                         dhsn,     ffracn,    &
    4013             :                                         l_print_point, &
    4014             :                                         initonly)
    4015             : 
    4016             :       integer (kind=int_kind), intent(in) :: &
    4017             :          ncat      , & ! number of ice thickness categories
    4018             :          nilyr     , & ! number of ice layers
    4019             :          nslyr     , & ! number of snow layers
    4020             :          nblyr         ! number of bgc layers
    4021             : 
    4022             :       real (kind=dbl_kind), intent(in) :: &
    4023             :          dt        , & ! time step (s)
    4024             :          swvdr     , & ! sw down, visible, direct  (W/m^2)
    4025             :          swvdf     , & ! sw down, visible, diffuse (W/m^2)
    4026             :          swidr     , & ! sw down, near IR, direct  (W/m^2)
    4027             :          swidf     , & ! sw down, near IR, diffuse (W/m^2)
    4028             :          fsnow     , & ! snowfall rate (kg/m^2 s)
    4029             :          TLAT, TLON    ! latitude and longitude (radian)
    4030             : 
    4031             :       character (len=char_len), intent(in) :: &
    4032             :          calendar_type       ! differentiates Gregorian from other calendars
    4033             : 
    4034             :       integer (kind=int_kind), intent(in) :: &
    4035             :          days_per_year, &    ! number of days in one year
    4036             :          sec                 ! elapsed seconds into date
    4037             : 
    4038             :       real (kind=dbl_kind), intent(in) :: &
    4039             :          nextsw_cday     , & ! julian day of next shortwave calculation
    4040             :          yday                ! day of the year
    4041             : 
    4042             :       real (kind=dbl_kind), intent(inout) :: &
    4043             :          coszen        ! cosine solar zenith angle, < 0 for sun below horizon 
    4044             : 
    4045             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
    4046             :          igrid              ! biology vertical interface points
    4047             :  
    4048             :       real (kind=dbl_kind), dimension (:), intent(in) :: &
    4049             :          swgrid                ! grid for ice tracers used in dEdd scheme
    4050             :         
    4051             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & 
    4052             :          kaer_tab, & ! aerosol mass extinction cross section (m2/kg)
    4053             :          waer_tab, & ! aerosol single scatter albedo (fraction)
    4054             :          gaer_tab    ! aerosol asymmetry parameter (cos(theta))
    4055             : 
    4056             :       real (kind=dbl_kind), dimension(:,:), intent(in) :: & 
    4057             :          kaer_bc_tab, & ! aerosol mass extinction cross section (m2/kg)
    4058             :          waer_bc_tab, & ! aerosol single scatter albedo (fraction)
    4059             :          gaer_bc_tab    ! aerosol asymmetry parameter (cos(theta))
    4060             : 
    4061             :       real (kind=dbl_kind), dimension(:,:,:), intent(in) :: & 
    4062             :          bcenh 
    4063             : 
    4064             :       real (kind=dbl_kind), dimension(:), intent(in) :: &
    4065             :          aicen     , & ! ice area fraction in each category
    4066             :          vicen     , & ! ice volume in each category (m)
    4067             :          vsnon     , & ! snow volume in each category (m)
    4068             :          Tsfcn     , & ! surface temperature (deg C)
    4069             :          alvln     , & ! level-ice area fraction
    4070             :          apndn     , & ! pond area fraction
    4071             :          hpndn     , & ! pond depth (m)
    4072             :          ipndn     , & ! pond refrozen lid thickness (m)
    4073             :          fbri           ! brine fraction 
    4074             : 
    4075             :       real(kind=dbl_kind), dimension(:,:), intent(in) :: &
    4076             :          aeron     , & ! aerosols (kg/m^3)
    4077             :          bgcNn     , & ! bgc Nit tracers
    4078             :          zaeron        ! bgcz aero tracers
    4079             : 
    4080             :       real(kind=dbl_kind), dimension(:,:), intent(inout) :: &
    4081             :          trcrn_bgcsw   ! zaerosols (kg/m^3) and chla (mg/m^3)
    4082             : 
    4083             :       real (kind=dbl_kind), dimension(:), intent(inout) :: &
    4084             :          alvdrn    , & ! visible, direct  albedo (fraction)
    4085             :          alidrn    , & ! near-ir, direct   (fraction)
    4086             :          alvdfn    , & ! visible, diffuse  (fraction)
    4087             :          alidfn    , & ! near-ir, diffuse  (fraction)
    4088             :          fswsfcn   , & ! SW absorbed at ice/snow surface (W m-2)
    4089             :          fswintn   , & ! SW absorbed in ice interior, below surface (W m-2)
    4090             :          fswthrun  , & ! SW through ice to ocean (W/m^2)
    4091             :          snowfracn , & ! snow fraction on each category
    4092             :          dhsn      , & ! depth difference for snow on sea ice and pond ice
    4093             :          ffracn    , & ! fraction of fsurfn used to melt ipond
    4094             :                        ! albedo components for history
    4095             :          albicen   , & ! bare ice 
    4096             :          albsnon   , & ! snow 
    4097             :          albpndn   , & ! pond 
    4098             :          apeffn        ! effective pond area used for radiation calculation
    4099             : 
    4100             :       real (kind=dbl_kind), dimension(:), intent(inout), optional :: &
    4101             :          fswthrun_vdr , & ! vis dir SW through ice to ocean (W/m^2)
    4102             :          fswthrun_vdf , & ! vis dif SW through ice to ocean (W/m^2)
    4103             :          fswthrun_idr , & ! nir dir SW through ice to ocean (W/m^2)
    4104             :          fswthrun_idf     ! nir dif SW through ice to ocean (W/m^2)
    4105             : 
    4106             :       real (kind=dbl_kind), dimension(:,:), intent(inout) :: &
    4107             :          fswpenln  , & ! visible SW entering ice layers (W m-2)
    4108             :          Iswabsn   , & ! SW radiation absorbed in ice layers (W m-2)
    4109             :          Sswabsn       ! SW radiation absorbed in snow layers (W m-2)
    4110             : 
    4111             :       logical (kind=log_kind), intent(in) :: &
    4112             :          l_print_point, & ! flag for printing diagnostics
    4113             :          dEdd_algae   , & ! .true. use prognostic chla in dEdd
    4114             :          modal_aero       ! .true. use modal aerosol optical treatment
    4115             : 
    4116             :       logical (kind=log_kind), optional :: &
    4117             :          initonly         ! flag to indicate init only, default is false
    4118             : 
    4119             : !autodocument_end
    4120             : 
    4121             :       ! local variables
    4122             : 
    4123             :       integer (kind=int_kind) :: &
    4124             :          n                  ! thickness category index
    4125             : 
    4126             :       logical (kind=log_kind) :: &
    4127             :          linitonly       ! local flag for initonly
    4128             : 
    4129             :       real(kind=dbl_kind) :: &
    4130      355764 :         hin,         & ! Ice thickness (m)
    4131      355764 :         hbri           ! brine thickness (m)
    4132             : 
    4133             :       real (kind=dbl_kind), dimension(:), allocatable :: &
    4134     1027755 :          l_fswthrun_vdr , & ! vis dir SW through ice to ocean (W/m^2)
    4135     1027755 :          l_fswthrun_vdf , & ! vis dif SW through ice to ocean (W/m^2)
    4136     1027755 :          l_fswthrun_idr , & ! nir dir SW through ice to ocean (W/m^2)
    4137     1027755 :          l_fswthrun_idf     ! nir dif SW through ice to ocean (W/m^2)
    4138             : 
    4139             :       character(len=*),parameter :: subname='(icepack_step_radiation)'
    4140             : 
    4141     1027755 :       allocate(l_fswthrun_vdr(ncat))
    4142     1027755 :       allocate(l_fswthrun_vdf(ncat))
    4143     1027755 :       allocate(l_fswthrun_idr(ncat))
    4144     1027755 :       allocate(l_fswthrun_idf(ncat))
    4145             : 
    4146     1027755 :         hin = c0
    4147     1027755 :         hbri = c0
    4148     1027755 :         linitonly = .false.
    4149     1027755 :         if (present(initonly)) then
    4150         135 :            linitonly = initonly
    4151             :         endif
    4152             : 
    4153             :          ! Initialize
    4154     5876910 :          do n = 1, ncat
    4155     4849155 :             alvdrn  (n) = c0
    4156     4849155 :             alidrn  (n) = c0
    4157     4849155 :             alvdfn  (n) = c0
    4158     4849155 :             alidfn  (n) = c0
    4159     4849155 :             fswsfcn (n) = c0
    4160     4849155 :             fswintn (n) = c0
    4161     5876910 :             fswthrun(n) = c0
    4162             :          enddo   ! ncat
    4163    42063570 :          fswpenln (:,:) = c0
    4164    37214415 :          Iswabsn  (:,:) = c0
    4165    12174165 :          Sswabsn  (:,:) = c0
    4166     5876910 :          trcrn_bgcsw(:,:) = c0
    4167             : 
    4168             :          ! Interpolate z-shortwave tracers to shortwave grid
    4169     1027755 :          if (dEdd_algae) then
    4170           0 :          do n = 1, ncat
    4171           0 :               if (aicen(n) .gt. puny) then
    4172           0 :                  hin = vicen(n)/aicen(n)
    4173           0 :                  hbri= fbri(n)*hin
    4174             :                  call compute_shortwave_trcr(nslyr,           &
    4175           0 :                                      bgcNn(:,n),              &
    4176           0 :                                      zaeron(:,n),             &
    4177           0 :                                      trcrn_bgcsw(:,n),        &
    4178           0 :                                      swgrid,       hin,       &
    4179             :                                      hbri,                    &
    4180             :                                      nilyr,        nblyr,     &
    4181           0 :                                      igrid,                   &
    4182           0 :                                      skl_bgc,      z_tracers  )
    4183           0 :                  if (icepack_warnings_aborted(subname)) return
    4184             :               endif
    4185             :          enddo
    4186             :          endif
    4187             : 
    4188     1027755 :          if (calc_Tsfc) then
    4189     1027755 :          if (trim(shortwave) == 'dEdd') then ! delta Eddington
    4190             :             
    4191             :             call run_dEdd(dt,           ncat,           &
    4192             :                           dEdd_algae,                   &
    4193             :                           nilyr,        nslyr,          &
    4194           0 :                           aicen,        vicen,          &
    4195           0 :                           vsnon,        Tsfcn,          &
    4196           0 :                           alvln,        apndn,          &
    4197           0 :                           hpndn,        ipndn,          &
    4198           0 :                           aeron,        kalg,           &
    4199           0 :                           trcrn_bgcsw,                  &
    4200             :                           heat_capacity,                &
    4201             :                           TLAT,         TLON,           &
    4202             :                           calendar_type,days_per_year,  &
    4203             :                           nextsw_cday,  yday,           &
    4204             :                           sec,          R_ice,          &
    4205             :                           R_pnd,        R_snw,          &
    4206             :                           dT_mlt,       rsnw_mlt,       &
    4207             :                           hs0,          hs1,            &
    4208             :                           hp1,          pndaspect,      &
    4209           0 :                           kaer_tab,     waer_tab,       &
    4210           0 :                           gaer_tab,                     &
    4211           0 :                           kaer_bc_tab,                  &
    4212           0 :                           waer_bc_tab,                  &
    4213           0 :                           gaer_bc_tab,                  &
    4214           0 :                           bcenh,                        &
    4215             :                           modal_aero,                   &
    4216             :                           swvdr,        swvdf,          &
    4217             :                           swidr,        swidf,          &
    4218             :                           coszen,       fsnow,          &
    4219           0 :                           alvdrn,       alvdfn,         &
    4220           0 :                           alidrn,       alidfn,         &
    4221           0 :                           fswsfcn,      fswintn,        &
    4222           0 :                           fswthrun=fswthrun,            &
    4223             :                           fswthrun_vdr=l_fswthrun_vdr,  &
    4224             :                           fswthrun_vdf=l_fswthrun_vdf,  &
    4225             :                           fswthrun_idr=l_fswthrun_idr,  &
    4226             :                           fswthrun_idf=l_fswthrun_idf,  &
    4227           0 :                           fswpenln=fswpenln,            &
    4228           0 :                           Sswabsn=Sswabsn,              &
    4229           0 :                           Iswabsn=Iswabsn,              &
    4230           0 :                           albicen=albicen,              &
    4231           0 :                           albsnon=albsnon,              &
    4232           0 :                           albpndn=albpndn,              &
    4233           0 :                           apeffn=apeffn,                &
    4234           0 :                           snowfracn=snowfracn,          &
    4235           0 :                           dhsn=dhsn,                    &
    4236           0 :                           ffracn=ffracn,                &
    4237             :                           l_print_point=l_print_point,  &
    4238      882945 :                           initonly=linitonly)
    4239      882945 :             if (icepack_warnings_aborted(subname)) return
    4240             :  
    4241      144810 :          elseif (trim(shortwave) == 'ccsm3') then
    4242             : 
    4243           0 :             call shortwave_ccsm3(aicen,      vicen,      &
    4244           0 :                                  vsnon,                  &
    4245           0 :                                  Tsfcn,                  &
    4246             :                                  swvdr,      swvdf,      &
    4247             :                                  swidr,      swidf,      &
    4248             :                                  heat_capacity,          &
    4249             :                                  albedo_type,            &
    4250             :                                  albicev,    albicei,    &
    4251             :                                  albsnowv,   albsnowi,   &
    4252             :                                  ahmax,                  &
    4253           0 :                                  alvdrn,     alidrn,     &
    4254           0 :                                  alvdfn,     alidfn,     &
    4255           0 :                                  fswsfcn,    fswintn,    &
    4256           0 :                                  fswthru=fswthrun,       &
    4257             :                                  fswthru_vdr=l_fswthrun_vdr,&
    4258             :                                  fswthru_vdf=l_fswthrun_vdf,&
    4259             :                                  fswthru_idr=l_fswthrun_idr,&
    4260             :                                  fswthru_idf=l_fswthrun_idf,&
    4261           0 :                                  fswpenl=fswpenln,       &
    4262           0 :                                  Iswabs=Iswabsn,         &
    4263           0 :                                  Sswabs=Sswabsn,         &
    4264           0 :                                  albin=albicen,          &
    4265           0 :                                  albsn=albsnon,          &
    4266             :                                  coszen=coszen,          &
    4267             :                                  ncat=ncat,              &
    4268      144810 :                                  nilyr=nilyr)
    4269      144810 :             if (icepack_warnings_aborted(subname)) return
    4270             : 
    4271             :          else
    4272             : 
    4273           0 :             call icepack_warnings_add(subname//' ERROR: shortwave '//trim(shortwave)//' unknown')
    4274           0 :             call icepack_warnings_setabort(.true.,__FILE__,__LINE__)
    4275           0 :             return
    4276             : 
    4277             :          endif   ! shortwave
    4278             : 
    4279             :       else    ! .not. calc_Tsfc
    4280             : 
    4281             :       ! Calculate effective pond area for HadGEM
    4282             : 
    4283           0 :          if (tr_pond_topo) then
    4284           0 :             do n = 1, ncat
    4285           0 :                apeffn(n) = c0 
    4286           0 :                if (aicen(n) > puny) then
    4287             :                ! Lid effective if thicker than hp1
    4288           0 :                  if (apndn(n)*aicen(n) > puny .and. ipndn(n) < hp1) then
    4289           0 :                     apeffn(n) = apndn(n)
    4290             :                  else
    4291           0 :                     apeffn(n) = c0
    4292             :                  endif
    4293           0 :                  if (apndn(n) < puny) apeffn(n) = c0
    4294             :                endif
    4295             :             enddo  ! ncat
    4296             :  
    4297             :          endif ! tr_pond_topo
    4298             : 
    4299             :          ! Initialize for safety
    4300           0 :          do n = 1, ncat
    4301           0 :             alvdrn(n) = c0
    4302           0 :             alidrn(n) = c0
    4303           0 :             alvdfn(n) = c0
    4304           0 :             alidfn(n) = c0
    4305           0 :             fswsfcn(n) = c0
    4306           0 :             fswintn(n) = c0
    4307           0 :             fswthrun(n) = c0
    4308             :          enddo   ! ncat
    4309           0 :          Iswabsn(:,:) = c0
    4310           0 :          Sswabsn(:,:) = c0
    4311             : 
    4312             :       endif    ! calc_Tsfc
    4313             : 
    4314     5876910 :       if (present(fswthrun_vdr)) fswthrun_vdr = l_fswthrun_vdr
    4315     5876910 :       if (present(fswthrun_vdf)) fswthrun_vdf = l_fswthrun_vdf
    4316     5876910 :       if (present(fswthrun_idr)) fswthrun_idr = l_fswthrun_idr
    4317     5876910 :       if (present(fswthrun_idf)) fswthrun_idf = l_fswthrun_idf
    4318             : 
    4319     1027755 :       deallocate(l_fswthrun_vdr)
    4320     1027755 :       deallocate(l_fswthrun_vdf)
    4321     1027755 :       deallocate(l_fswthrun_idr)
    4322     1027755 :       deallocate(l_fswthrun_idf)
    4323             : 
    4324     1027755 :       end subroutine icepack_step_radiation
    4325             : 
    4326             :       ! Delta-Eddington solution expressions
    4327             : 
    4328             : !=======================================================================
    4329             : 
    4330   469322460 :       real(kind=dbl_kind) function alpha(w,uu,gg,e)
    4331             : 
    4332             :       real(kind=dbl_kind), intent(in) :: w, uu, gg, e
    4333             : 
    4334   469322460 :       alpha = p75*w*uu*((c1 + gg*(c1-w))/(c1 - e*e*uu*uu))
    4335             : 
    4336   469322460 :       end function alpha
    4337             : 
    4338             : !=======================================================================
    4339             : 
    4340   469322460 :       real(kind=dbl_kind) function agamm(w,uu,gg,e)
    4341             : 
    4342             :       real(kind=dbl_kind), intent(in) :: w, uu, gg, e
    4343             : 
    4344   469322460 :       agamm = p5*w*((c1 + c3*gg*(c1-w)*uu*uu)/(c1-e*e*uu*uu))
    4345             : 
    4346   469322460 :       end function agamm
    4347             : 
    4348             : !=======================================================================
    4349             : 
    4350    52146940 :       real(kind=dbl_kind) function n(uu,et)
    4351             : 
    4352             :       real(kind=dbl_kind), intent(in) :: uu, et
    4353             : 
    4354    52146940 :       n = ((uu+c1)*(uu+c1)/et ) - ((uu-c1)*(uu-c1)*et)
    4355             : 
    4356    52146940 :       end function n
    4357             : 
    4358             : !=======================================================================
    4359             : 
    4360    52146940 :       real(kind=dbl_kind) function u(w,gg,e)
    4361             : 
    4362             :       real(kind=dbl_kind), intent(in) :: w, gg, e
    4363             : 
    4364    52146940 :       u = c1p5*(c1 - w*gg)/e
    4365             : 
    4366    52146940 :       end function u
    4367             : 
    4368             : !=======================================================================
    4369             : 
    4370    52146940 :       real(kind=dbl_kind) function el(w,gg)
    4371             : 
    4372             :       real(kind=dbl_kind), intent(in) :: w, gg
    4373             : 
    4374    52146940 :       el = sqrt(c3*(c1-w)*(c1 - w*gg))
    4375             : 
    4376    52146940 :       end function el
    4377             : 
    4378             : !=======================================================================
    4379             : 
    4380    52146940 :       real(kind=dbl_kind) function taus(w,f,t)
    4381             : 
    4382             :       real(kind=dbl_kind), intent(in) :: w, f, t
    4383             : 
    4384    52146940 :       taus = (c1 - w*f)*t
    4385             : 
    4386    52146940 :       end function taus
    4387             : 
    4388             : !=======================================================================
    4389             : 
    4390    52146940 :       real(kind=dbl_kind) function omgs(w,f)
    4391             : 
    4392             :       real(kind=dbl_kind), intent(in) :: w, f
    4393             : 
    4394    52146940 :       omgs = (c1 - f)*w/(c1 - w*f)
    4395             : 
    4396    52146940 :       end function omgs
    4397             : 
    4398             : !=======================================================================
    4399             : 
    4400    52146940 :       real(kind=dbl_kind) function asys(gg,f)
    4401             : 
    4402             :       real(kind=dbl_kind), intent(in) :: gg, f
    4403             : 
    4404    52146940 :       asys = (gg - f)/(c1 - f)
    4405             : 
    4406    52146940 :       end function asys
    4407             :  
    4408             : !=======================================================================
    4409             : 
    4410             :       end module icepack_shortwave
    4411             : 
    4412             : !=======================================================================

Generated by: LCOV version 1.14-6-g40580cd