Skip to content

Commit

Permalink
Fix history features
Browse files Browse the repository at this point in the history
- Fix bug in history time axis when sec_init is not zero.
- Fix issue with time_beg and time_end uninitialized values.
- Add support for averaging with histfreq='1' by allowing histfreq_n to be any value
  in that case.  Extend and clean up construct_filename for history files.  More could
  be done, but wanted to preserve backwards compatibility.
- Add new calendar_sec2hms to converts daily seconds to hh:mm:ss.  Update the
  calchk calendar unit tester to check this method
- Remove abort test in bcstchk, this was just causing problems in regression testing
- Remove known problems documentation about problems writing when istep=1.  This issue
  does not exist anymore with the updated time manager.
- Add new tests with hist_avg = false.  Add set_nml.histinst.
  • Loading branch information
apcraig committed Aug 10, 2021
1 parent 0ccdea1 commit 441f693
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 132 deletions.
16 changes: 8 additions & 8 deletions cicecore/cicedynB/analysis/ice_history.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,7 @@ subroutine accum_hist (dt)
nstrm ! nstreams (1 if writing initial condition)

real (kind=dbl_kind) :: &
timedbl , & ! temporary dbl for time bounds
ravgct , & ! 1/avgct
ravgctz ! 1/avgct

Expand Down Expand Up @@ -1814,7 +1815,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
Expand Down Expand Up @@ -1862,11 +1863,10 @@ 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)
endif
endif
if (avgct(ns) == c1) then
timedbl = (timesecs-dt)/(secday)
time_beg(ns) = real(timedbl,kind=real_kind)
endif
enddo

Expand Down Expand Up @@ -3966,8 +3966,8 @@ subroutine accum_hist (dt)
enddo ! iblk
!$OMP END PARALLEL DO

time_end(ns) = timesecs/int(secday)
time_end(ns) = real(time_end(ns),kind=real_kind)
timedbl = timesecs/secday
time_end(ns) = real(timedbl,kind=real_kind)

!---------------------------------------------------------------
! write file
Expand Down
97 changes: 50 additions & 47 deletions cicecore/cicedynB/analysis/ice_history_shared.F90
Original file line number Diff line number Diff line change
Expand Up @@ -672,64 +672,67 @@ subroutine construct_filename(ncfile,suffix,ns)
iday = mday
isec = msec - dt

if (write_ic) isec = msec
! construct filename
if (write_ic) then
isec = msec
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
incond_file(1:lenstr(incond_file)),'.',iyear,'-', &
imonth,'-',iday,'-',isec,'.',suffix
imonth,'-',iday,'-',isec,'.',trim(suffix)
else

if (hist_avg .and. histfreq(ns) /= '1') then
if (histfreq(ns) == 'h'.or.histfreq(ns) == 'H') then
! do nothing
elseif (new_year) then
iyear = iyear - 1
imonth = 12
iday = daymo(imonth)
elseif (new_month) then
imonth = mmonth - 1
iday = daymo(imonth)
elseif (new_day) then
iday = iday - 1
endif
endif

cstream = ''
if (hist_avg) then
if (histfreq(ns) == '1' .or. histfreq(ns) == 'h'.or.histfreq(ns) == 'H') then
! do nothing
elseif (new_year) then
iyear = iyear - 1
imonth = 12
iday = daymo(imonth)
elseif (new_month) then
imonth = mmonth - 1
iday = daymo(imonth)
elseif (new_day) then
iday = iday - 1
endif
endif

cstream = ''
!echmod ! this was implemented for CESM but it breaks post-processing software
!echmod ! of other groups (including RASM which uses CESMCOUPLED)
!echmod if (ns > 1) write(cstream,'(i1.1)') ns-1

if (histfreq(ns) == '1') then ! instantaneous, write every dt
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'_inst.', &
iyear,'-',imonth,'-',iday,'-',msec,'.',suffix

elseif (hist_avg) then ! write averaged data

if (histfreq(ns) == 'd'.or.histfreq(ns) == 'D') then ! daily
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream), &
'.',iyear,'-',imonth,'-',iday,'.',suffix
elseif (histfreq(ns) == 'h'.or.histfreq(ns) == 'H') then ! hourly
write(ncfile,'(a,a,i2.2,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'_', &
histfreq_n(ns),'h.',iyear,'-',imonth,'-',iday,'-',msec,'.',suffix
elseif (histfreq(ns) == 'm'.or.histfreq(ns) == 'M') then ! monthly
write(ncfile,'(a,a,i4.4,a,i2.2,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'.', &
iyear,'-',imonth,'.',suffix
elseif (histfreq(ns) == 'y'.or.histfreq(ns) == 'Y') then ! yearly
write(ncfile,'(a,a,i4.4,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'.', &
iyear,'.',suffix
endif
if (hist_avg) then ! write averaged data
if (histfreq(ns) == '1' .and. histfreq_n(ns) == 1) then ! timestep
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'_inst.', &
iyear,'-',imonth,'-',iday,'-',msec,'.',trim(suffix)
elseif (histfreq(ns) == '1' .and. histfreq_n(ns) > 1) then ! timestep
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'.', &
iyear,'-',imonth,'-',iday,'-',msec,'.',trim(suffix)
elseif (histfreq(ns) == 'h'.or.histfreq(ns) == 'H') then ! hourly
write(ncfile,'(a,a,i2.2,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'_', &
histfreq_n(ns),'h.',iyear,'-',imonth,'-',iday,'-',msec,'.',trim(suffix)
elseif (histfreq(ns) == 'd'.or.histfreq(ns) == 'D') then ! daily
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'.', &
iyear,'-',imonth,'-',iday,'.',trim(suffix)
elseif (histfreq(ns) == 'm'.or.histfreq(ns) == 'M') then ! monthly
write(ncfile,'(a,a,i4.4,a,i2.2,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'.', &
iyear,'-',imonth,'.',trim(suffix)
elseif (histfreq(ns) == 'y'.or.histfreq(ns) == 'Y') then ! yearly
write(ncfile,'(a,a,i4.4,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'.', &
iyear,'.',trim(suffix)
endif

else ! instantaneous
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file))//trim(cstream),'_inst.', &
iyear,'-',imonth,'-',iday,'-',msec,'.',trim(suffix)
endif

else ! instantaneous with histfreq > dt
write(ncfile,'(a,a,i4.4,a,i2.2,a,i2.2,a,i5.5,a,a)') &
history_file(1:lenstr(history_file)),'_inst.', &
iyear,'-',imonth,'-',iday,'-',msec,'.',suffix
endif
endif

end subroutine construct_filename
Expand Down
30 changes: 18 additions & 12 deletions cicecore/cicedynB/infrastructure/io/io_netcdf/ice_history_write.F90
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ subroutine ice_write_hist (ns)
use ice_blocks, only: nx_block, ny_block
use ice_broadcast, only: broadcast_scalar
use ice_calendar, only: msec, timesecs, idate, idate0, write_ic, &
histfreq, days_per_year, use_leap_years, dayyr, &
year_init, month_init, day_init
histfreq, histfreq_n, days_per_year, use_leap_years, dayyr, &
hh_init, mm_init, ss_init
use ice_communicate, only: my_task, master_task
use ice_domain, only: distrb_info
use ice_domain_size, only: nx_global, ny_global, max_nstrm, max_blocks
Expand Down Expand Up @@ -205,7 +205,6 @@ subroutine ice_write_hist (ns)
! define coordinate variables
!-----------------------------------------------------------------

!sgl status = nf90_def_var(ncid,'time',nf90_float,timid,varid)
status = nf90_def_var(ncid,'time',nf90_double,timid,varid)
if (status /= nf90_noerr) call abort_ice(subname// &
'ERROR: defining var time')
Expand All @@ -215,8 +214,9 @@ subroutine ice_write_hist (ns)
'ice Error: time long_name')

write(cdate,'(i8.8)') idate0
write(title,'(a,a,a,a,a,a,a,a)') 'days since ', &
cdate(1:4),'-',cdate(5:6),'-',cdate(7:8),' 00:00:00'
write(title,'(a,a4,a1,a2,a1,a2,a1,i2.2,a1,i2.2,a1,i2.2)') 'days since ', &
cdate(1:4),'-',cdate(5:6),'-',cdate(7:8),' ', &
hh_init,':',mm_init,':',ss_init
status = nf90_put_att(ncid,varid,'units',title)
if (status /= nf90_noerr) call abort_ice(subname// &
'ERROR: time units')
Expand Down Expand Up @@ -258,8 +258,9 @@ subroutine ice_write_hist (ns)
if (status /= nf90_noerr) call abort_ice(subname// &
'ERROR: time_bounds long_name')
write(cdate,'(i8.8)') idate0
write(title,'(a,a,a,a,a,a,a,a)') 'days since ', &
cdate(1:4),'-',cdate(5:6),'-',cdate(7:8),' 00:00:00'
write(title,'(a,a4,a1,a2,a1,a2,a1,i2.2,a1,i2.2,a1,i2.2)') 'days since ', &
cdate(1:4),'-',cdate(5:6),'-',cdate(7:8),' ', &
hh_init,':',mm_init,':',ss_init
status = nf90_put_att(ncid,varid,'units',title)
if (status /= nf90_noerr) call abort_ice(subname// &
'ERROR: time_bounds units')
Expand Down Expand Up @@ -575,7 +576,8 @@ subroutine ice_write_hist (ns)
endif
endif

if (histfreq(ns) == '1' .or. .not. hist_avg &
if ((histfreq(ns) == '1' .and. histfreq_n(ns) == 1) &
.or..not. hist_avg &
.or. n==n_divu(ns) .or. n==n_shear(ns) & ! snapshots
.or. n==n_sig1(ns) .or. n==n_sig2(ns) &
.or. n==n_sigP(ns) .or. n==n_trsig(ns) &
Expand Down Expand Up @@ -640,7 +642,8 @@ subroutine ice_write_hist (ns)
'ERROR: defining cell methods for '//avail_hist_fields(n)%vname)
endif

if (histfreq(ns) == '1' .or. .not. hist_avg) then
if ((histfreq(ns) == '1' .and. histfreq_n(ns) == 1) &
.or..not. hist_avg) then
status = nf90_put_att(ncid,varid,'time_rep','instantaneous')
else
status = nf90_put_att(ncid,varid,'time_rep','averaged')
Expand Down Expand Up @@ -881,7 +884,8 @@ subroutine ice_write_hist (ns)
'ERROR: defining cell methods for '//avail_hist_fields(n)%vname)
endif

if (histfreq(ns) == '1' .or. .not. hist_avg) then
if ((histfreq(ns) == '1' .and. histfreq_n(ns) == 1) &
.or..not. hist_avg) then
status = nf90_put_att(ncid,varid,'time_rep','instantaneous')
else
status = nf90_put_att(ncid,varid,'time_rep','averaged')
Expand Down Expand Up @@ -942,7 +946,8 @@ subroutine ice_write_hist (ns)
'ERROR: defining cell methods for '//avail_hist_fields(n)%vname)
endif

if (histfreq(ns) == '1' .or. .not. hist_avg) then
if ((histfreq(ns) == '1' .and. histfreq_n(ns) == 1) &
.or..not. hist_avg) then
status = nf90_put_att(ncid,varid,'time_rep','instantaneous')
else
status = nf90_put_att(ncid,varid,'time_rep','averaged')
Expand Down Expand Up @@ -1003,7 +1008,8 @@ subroutine ice_write_hist (ns)
'ERROR: defining cell methods for '//avail_hist_fields(n)%vname)
endif

if (histfreq(ns) == '1' .or. .not. hist_avg) then
if ((histfreq(ns) == '1' .and. histfreq_n(ns) == 1) &
.or..not. hist_avg) then
status = nf90_put_att(ncid,varid,'time_rep','instantaneous')
else
status = nf90_put_att(ncid,varid,'time_rep','averaged')
Expand Down
Loading

0 comments on commit 441f693

Please sign in to comment.