diff --git a/mediator/med_phases_history_mod.F90 b/mediator/med_phases_history_mod.F90 index 7d59a7fe..3dfad387 100644 --- a/mediator/med_phases_history_mod.F90 +++ b/mediator/med_phases_history_mod.F90 @@ -515,19 +515,20 @@ subroutine med_phases_history_write_med(gcomp, rc) end subroutine med_phases_history_write_med !=============================================================================== - subroutine med_phases_history_write_lnd2glc(gcomp, fldbun, rc) + subroutine med_phases_history_write_lnd2glc(gcomp, fldbun_lnd, rc, fldbun_glc) - ! Write yearly average of lnd -> glc fields + ! Write yearly average of lnd -> glc fields on both land and glc grids - use med_internalstate_mod, only : complnd + use med_internalstate_mod, only : complnd, compglc use med_constants_mod , only : SecPerDay => med_constants_SecPerDay use med_io_mod , only : med_io_write_time, med_io_define_time use med_io_mod , only : med_io_date2yyyymmdd, med_io_sec2hms, med_io_ymd2date ! input/output variables type(ESMF_GridComp) , intent(in) :: gcomp - type(ESMF_FieldBundle) , intent(in) :: fldbun + type(ESMF_FieldBundle) , intent(in) :: fldbun_lnd integer , intent(out) :: rc + type(ESMF_FieldBundle) , intent(in), optional :: fldbun_glc(:) ! local variables type(file_desc_t) :: io_file @@ -546,7 +547,7 @@ subroutine med_phases_history_write_lnd2glc(gcomp, fldbun, rc) real(r8) :: time_val ! time coordinate output real(r8) :: time_bnds(2) ! time bounds output character(len=CL) :: hist_file - integer :: m + integer :: m,n logical :: isPresent character(len=*), parameter :: subname='(med_phases_history_write_lnd2glc)' !--------------------------------------- @@ -619,9 +620,21 @@ subroutine med_phases_history_write_lnd2glc(gcomp, fldbun, rc) call med_io_write_time(io_file, time_val, time_bnds, nt=1, rc=rc) if (ChkErr(rc,__LINE__,u_FILE_u)) return end if - call med_io_write(io_file, fldbun, whead(m), wdata(m), is_local%wrap%nx(complnd), is_local%wrap%ny(complnd), & + + call med_io_write(io_file, fldbun_lnd, whead(m), wdata(m), & + is_local%wrap%nx(complnd), is_local%wrap%ny(complnd), & nt=1, pre=trim(compname(complnd))//'Imp', rc=rc) if (ChkErr(rc,__LINE__,u_FILE_u)) return + + if (present(fldbun_glc)) then + do n = 1,size(fldbun_glc) + call med_io_write(io_file, fldbun_glc(n), whead(m), wdata(m), & + is_local%wrap%nx(compglc(n)), is_local%wrap%ny(compglc(n)), & + nt=1, pre=trim(compname(compglc(n)))//'Exp', rc=rc) + if (ChkErr(rc,__LINE__,u_FILE_u)) return + end do + end if + end do ! end of loop over m ! Close history file diff --git a/mediator/med_phases_prep_glc_mod.F90 b/mediator/med_phases_prep_glc_mod.F90 index 920fb415..1fecf1a8 100644 --- a/mediator/med_phases_prep_glc_mod.F90 +++ b/mediator/med_phases_prep_glc_mod.F90 @@ -632,22 +632,6 @@ subroutine med_phases_prep_glc_avg(gcomp, rc) end if end do - ! Write auxiliary history file if flag is set and accumulation is being done - if (lndAccum2glc_cnt > 0) then - call NUOPC_CompAttributeGet(gcomp, name="histaux_l2x1yrg", value=cvalue, & - isPresent=isPresent, isSet=isSet, rc=rc) - if (ChkErr(rc,__LINE__,u_FILE_u)) return - if (isPresent .and. isSet) then - read(cvalue,*) write_histaux_l2x1yrg - else - write_histaux_l2x1yrg = .false. - end if - if (write_histaux_l2x1yrg) then - call med_phases_history_write_lnd2glc(gcomp, FBlndAccum2glc_l, rc) - if (ChkErr(rc,__LINE__,u_FILE_u)) return - end if - end if - if (is_local%wrap%ocn2glc_coupling) then ! Average import from accumulated ocn import data do n = 1, size(fldnames_fr_ocn) @@ -688,15 +672,39 @@ subroutine med_phases_prep_glc_avg(gcomp, rc) if (chkErr(rc,__LINE__,u_FILE_u)) return end if + ! Determine if auxiliary file will be written + write_histaux_l2x1yrg = .false. + if (lndAccum2glc_cnt > 0) then + call NUOPC_CompAttributeGet(gcomp, name="histaux_l2x1yrg", value=cvalue, & + isPresent=isPresent, isSet=isSet, rc=rc) + if (ChkErr(rc,__LINE__,u_FILE_u)) return + if (isPresent .and. isSet) then + read(cvalue,*) write_histaux_l2x1yrg + end if + end if + + ! Write auxiliary history file if flag is set and accumulation is being done if (is_local%wrap%lnd2glc_coupling) then ! Map accumulated field bundle from land grid (with elevation classes) to glc grid (without elevation classes) ! and set FBExp(compglc(ns)) data ! Zero land accumulator and accumulated field bundles on land grid call med_phases_prep_glc_map_lnd2glc(gcomp, rc) if (chkErr(rc,__LINE__,u_FILE_u)) return + + if (write_histaux_l2x1yrg) then + call med_phases_history_write_lnd2glc(gcomp, FBlndAccum2glc_l, & + fldbun_glc=is_local%wrap%FBExp(compglc(:)), rc=rc) + if (ChkErr(rc,__LINE__,u_FILE_u)) return + end if + lndAccum2glc_cnt = 0 call fldbun_reset(FBlndAccum2glc_l, value=czero, rc=rc) if (chkErr(rc,__LINE__,u_FILE_u)) return + else + if (write_histaux_l2x1yrg) then + call med_phases_history_write_lnd2glc(gcomp, FBlndAccum2glc_l, rc) + if (ChkErr(rc,__LINE__,u_FILE_u)) return + end if end if if (dbug_flag > 1) then