From a42a71124f4b2c6d874df5099190ee76e4adf6c6 Mon Sep 17 00:00:00 2001 From: Philippe Blain Date: Tue, 12 Jan 2021 14:48:59 -0500 Subject: [PATCH] ice_history: fix history averaging over multiple time steps The code at the beginning of subroutine 'accum_hist' is responsible for resetting the accumulation arrays a2D, a3Dc, etc. to zero when writing snapshots, but it also resets them if "histfreq(ns) == '1'". This has been the case since ab1c53e (Many thanks to Dave Bailey (NCAR) for getting this ball rolling., 2009-06-01) [1], which implemented the multiple-stream history functionality in CICE 5. This means that averaging over multiple time steps, i.e. histfreq = '1' histfreq_n = m ! m > 1 hist_avg = .true. does not work: the accumulation arrays will always be reset to zero at the beginning of 'accum_hist' and no accumulation will happen. This also means that the variable 'time_beg' is not initialized in that configuration, since this it is initialized in the 'else' branch of that 'if' statement. Commit ab1c53e also *removed* the following line in ice_init::input_data: if (histfreq == '1') hist_avg = .false. ! potential conflict so it's unclear if that configuration was forgotten at the time. Allow overaging over multiple time steps by only resetting the accumulation arrays if 'hist_avg' is false. As a side effect, this correctly initializes the 'time_beg' variable, which avoids writing that variable uninitialized to the output files, which can lead to analysis tools failing if compiling with '-init=snan,arrays' (or similar [2]. While at it, remove a commented line in the 'else' branch since it's implemented just below. This commit does not modify neither ice_history_shared::construct_filename nor the different ice_history_write::ice_write_hist subroutines, that also assume that 'histfreq = 1' implies 'histfreq_n = 1'. They will be modified in subsequent commits. [1] https://github.com/CICE-Consortium/CICE-svn-trunk/commit/ab1c53eaf0c4a0b0dbc65e582a5e1b35c2208a88?branch=ab1c53eaf0c4a0b0dbc65e582a5e1b35c2208a88&diff=unified#diff-aebd77751eba77ed6b6c194d4cd2e4b353258f7e903cc7877343911c9b25c283L1296-L1306 [2] Closes https://github.com/CICE-Consortium/CICE/issues/551 --- cicecore/cicedynB/analysis/ice_history.F90 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cicecore/cicedynB/analysis/ice_history.F90 b/cicecore/cicedynB/analysis/ice_history.F90 index f91562449..268ed5267 100644 --- a/cicecore/cicedynB/analysis/ice_history.F90 +++ b/cicecore/cicedynB/analysis/ice_history.F90 @@ -1814,7 +1814,7 @@ subroutine accum_hist (dt) n4Dfcum = n4Dscum + num_avail_hist_fields_4Df ! should equal num_avail_hist_fields_tot do ns = 1,nstreams - if (.not. hist_avg .or. histfreq(ns) == '1') then ! write snapshots + if (.not. hist_avg) then ! write snapshots do n = 1,n2D if (avail_hist_fields(n)%vhistfreq == histfreq(ns)) & a2D(:,:,n,:) = c0 @@ -1862,7 +1862,6 @@ subroutine accum_hist (dt) avgct(ns) = c1 else ! write averages over time histfreq avgct(ns) = avgct(ns) + c1 -! if (avgct(ns) == c1) time_beg(ns) = (time-dt)/int(secday) if (avgct(ns) == c1) then time_beg(ns) = (timesecs-dt)/int(secday) time_beg(ns) = real(time_beg(ns),kind=real_kind)