From a40b6afa5413026265ca56ad5798690b2d2b93b2 Mon Sep 17 00:00:00 2001 From: braczka Date: Mon, 13 Nov 2023 10:09:08 -0700 Subject: [PATCH] Updating assimilat.csh for history file updates --- models/clm/dart_to_clm.f90 | 107 ++++++++++++++++-- .../clm/shell_scripts/cesm2_2/assimilate.csh | 86 ++++++++++++-- 2 files changed, 170 insertions(+), 23 deletions(-) diff --git a/models/clm/dart_to_clm.f90 b/models/clm/dart_to_clm.f90 index 4ca0b4f269..2ea3d64710 100644 --- a/models/clm/dart_to_clm.f90 +++ b/models/clm/dart_to_clm.f90 @@ -79,6 +79,8 @@ program dart_to_clm character(len=256) :: repartition_vhist_file = 'clm_vector_history.nc' character(len=256) :: repartition_analysis_file = 'dart_posterior_vector.nc' integer :: history_update = 0 +character(len=256) :: history_file = 'clm_history.nc' +character(len=256) :: history_analysis_file = 'dart_posterior_history.nc' integer :: verbose = 0 namelist /dart_to_clm_nml/ dart_to_clm_input_file, & @@ -87,13 +89,15 @@ program dart_to_clm repartition_vhist_file, & repartition_analysis_file, & history_update, & + history_file, & + history_analysis_file, & verbose !---------------------------------------------------------------------- integer :: iunit, io, dom_restart, dom_history, ivar, rank, irank integer :: nlevsno, ICE_varsize(2) -integer :: ncid_dart, ncid_clm +integer :: ncid_dart, ncid_clm, ncid_dart_hist, ncid_clm_hist type(time_type) :: dart_time, clm_time character(len=512) :: string1, string2, string3 @@ -205,26 +209,97 @@ program dart_to_clm endif - ! Default is for history files not to be updated, - ! but there are rare cases when updating history variables is useful. - ! This allows for the manual selection of history variable update +call nc_close_file(ncid_clm, source) +call nc_close_file(ncid_dart, source) + +! BMR + + write(string1,*)'restart ncid_dart: ', ncid_dart + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'restart ncid_clm: ', ncid_clm + call error_handler(E_MSG, source, string1, source) + + + +! Default is for history files not to be updated, +! but there are rare cases when updating history variables is useful. +! This allows for the manual selection of history variable updates. + + +if (history_update > 0) then dom_history = 2 +ncid_dart_hist = nc_open_file_readonly( history_analysis_file, 'opening to read posterior') +ncid_clm_hist = nc_open_file_readwrite(history_file, 'opening to write history') -UPDATE2 : do ivar=1, get_num_variables(dom_history) +write(string1,*)'reading updated values from DART history file "'//trim(history_analysis_file)//'"' +write(string2,*)'and writing to CLM history file "'//trim(history_file)//'"' +call error_handler(E_MSG,source,string1,text2=string2) - if (history_update > 0) then +UPDATE2 : do ivar=1, get_num_variables(dom_history) varname = get_variable_name(dom_history,ivar) + + ! BMR + write(string1,*)'Entered History UPDATE, current varname: ', varname + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'Current ncid_dart: ', ncid_dart + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'Current ncid_clm: ', ncid_clm + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'Current ncid_dart_hist: ', ncid_dart_hist + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'Current ncid_clm_hist: ', ncid_clm_hist + call error_handler(E_MSG, source, string1, source) + + select case (varname) case ('GPP') + + ! BMR + write(string1,*)'Entered case GPP, current varname: ', varname + call error_handler(E_MSG, source, string1, source) + + + rank = get_num_dims(dom_history,ivar) + ! BMR + write(string1,*)'Past get_num_dims rank call ' + call error_handler(E_MSG, source, string1, source) + if (rank == 1) then - call replace_values_1D(dom_history, ivar, ncid_dart, ncid_clm) + ! BMR + write(string1,*)'Entered rank 1 ' + call error_handler(E_MSG, source, string1, source) + + call replace_values_1D(dom_history, ivar, ncid_dart_hist, ncid_clm_hist) + elseif (rank == 2) then - call replace_values_2D(dom_history, ivar, ncid_dart, ncid_clm) + + ! BMR + write(string1,*)'Entered rank 2 ' + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'replace_values_2D variable dom_history: ',dom_history + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'replace_values_2D variable ivar: ', ivar + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'replace_values_2D variable ncid_dart_hist: ', ncid_dart_hist + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'replace_values_2D variable ncid_clm_hist: ', ncid_clm_hist + call error_handler(E_MSG, source, string1, source) + + call replace_values_2D(dom_history, ivar, ncid_dart_hist, ncid_clm_hist) else write(string1, *) 'no support for data array of dimension ', rank @@ -234,14 +309,13 @@ program dart_to_clm case default ! do nothing -- do not update this history variable end select - endif enddo UPDATE2 +endif - -call nc_close_file(ncid_clm, source) -call nc_close_file(ncid_dart, source) +call nc_close_file(ncid_clm_hist, source) +call nc_close_file(ncid_dart_hist, source) ! Log what we think we're doing, and exit. @@ -330,6 +404,15 @@ subroutine replace_values_2D(dom_id, ivar, ncid_dart, ncid_clm) if (get_has_missing_value(dom_id,ivar)) call get_FillValue( dom_id,ivar,special) ! Make sure variables in both files are identical in shape, etc. + + ! BMR + write(string1,*)'Compatible_Variables variable varname: ', varname + call error_handler(E_MSG, source, string1, source) + + write(string1,*)'Compatible_Variables variable varsize: ', varsize + call error_handler(E_MSG, source, string1, source) + + call Compatible_Variables(varname, ncid_dart, ncid_clm, varsize) allocate(dart_array(varsize(1),varsize(2)), clm_array(varsize(1),varsize(2))) diff --git a/models/clm/shell_scripts/cesm2_2/assimilate.csh b/models/clm/shell_scripts/cesm2_2/assimilate.csh index 7bd93aa75a..111612aecc 100755 --- a/models/clm/shell_scripts/cesm2_2/assimilate.csh +++ b/models/clm/shell_scripts/cesm2_2/assimilate.csh @@ -4,14 +4,12 @@ # by UCAR, "as is", without charge, subject to all terms of use at # http://www.image.ucar.edu/DAReS/DART/DART_download # -# This script performs an assimilation by directly reading and writing to -# the CLM restart file. +# The default behavior of this script performs an assimilation by +# reading and writing to the CLM restart file. +# +# The CLM history file can also be written to through the dart_to_clm +# namelist option history_update # -# NOTE: 'dart_to_clm' does not currently support updating the -# prognostic snow variables based on posterior SWE values. -# Consequently, snow DA is not currently supported. -# Implementing snow DA is high on our list of priorities. - #========================================================================= # This block is an attempt to localize all the machine-specific # changes to this script such that the same script can be used @@ -329,16 +327,20 @@ echo "`date` -- END FILTER" #========================================================================= # Block 7: Put the DART posterior into the CLM restart file. The CLM # restart file is also the prior for the next forecast. +# Optional to put DART posterior into the CLM history file. #========================================================================= # Unlink any potentially pre-existing links unlink clm_restart.nc unlink dart_posterior.nc -# Identify if SWE re-partitioning is necessary +# Identify if SWE re-partitioning or history updates are on set REPARTITION = `grep repartition_swe input.nml` set REPARTITION = `echo $REPARTITION | sed -e "s/repartition_swe//g"` set REPARTITION = `echo $REPARTITION | sed -e "s/=//g"` +set HISTORY = `grep history_update input.nml` +set HISTORY = `echo $HISTORY | sed -e "s/history_update//g"` +set HISTORY = `echo $HISTORY | sed -e "s/=//g"` if ($REPARTITION != 0) then unlink clm_vector_history @@ -354,7 +356,7 @@ unlink clm_vector_history # Confirm that H2OSNO prior/posterior files exist if (! -e $POSTERIOR_VECTOR || ! -e $CLM_VECTOR) then - echo "ERROR: assimilate.csh could not find $POSTERIOR_VECTOR or $CLM_VECTOR" + echo "ERROR: assimilate.csh could not find either $POSTERIOR_VECTOR or $CLM_VECTOR" echo "When SWE re-partitioning is enabled H2OSNO must be" echo "within vector history file (h2). Also the analysis" echo "stage must be output in 'stages_to_write' within filter_nml" @@ -366,6 +368,29 @@ unlink clm_vector_history ${LINK} $RESTART clm_restart.nc ${LINK} $CLM_VECTOR clm_vector_history.nc + if ($HISTORY != 0) then + + unlink clm_history.nc + + set POSTERIOR_HISTORY = `printf analysis_member_00%02d_d02.nc $enscount` + set CLM_HISTORY = `printf ${CASE}.clm2_00%02d.h0.${LND_DATE_EXT}.nc $enscount` + + # Confirm the necessary history files exist + + if (! -e $POSTERIOR_HISTORY || ! -e $CLM_HISTORY) then + echo "ERROR: assimilate.csh could not find either $POSTERIOR_HISTORY or $CLM_HISTORY" + echo "When the history_update is enabled the history file (h0)" + echo "must exist for each assimilation time step. Also the analysis" + echo "stage must be output in 'stages_to_write' within filter_nml" + exit 8 + endif + + ${LINK} $POSTERIOR_HISTORY dart_posterior_history.nc + ${LINK} $CLM_HISTORY clm_history.nc + + endif + + ${EXEROOT}/dart_to_clm >& /dev/null if ($status != 0) then @@ -378,19 +403,50 @@ unlink clm_vector_history unlink $LIST end + + if ($HISTORY != 0) then + unlink dart_history_posterior.nc + unlink clm_history.nc + endif + @ enscount ++ end - + else -foreach RESTART ( ${CASE}.clm2_*.r.${LND_DATE_EXT}.nc ) +@ enscount = 1 +foreach RESTART ( ${CASE}.clm2_*.r.${LND_DATE_EXT}.nc ) + set POSTERIOR = `echo $RESTART | sed -e "s/${CASE}.//"` ${LINK} $POSTERIOR dart_posterior.nc ${LINK} $RESTART clm_restart.nc + + if ($HISTORY != 0) then + + unlink clm_history.nc + + set POSTERIOR_HISTORY = `printf analysis_member_00%02d_d02.nc $enscount` + set CLM_HISTORY = `printf ${CASE}.clm2_00%02d.h0.${LND_DATE_EXT}.nc $enscount` + + # Confirm the necessary history files exist + + if (! -e $POSTERIOR_HISTORY || ! -e $CLM_HISTORY) then + echo "ERROR: assimilate.csh could not find either $POSTERIOR_HISTORY or $CLM_HISTORY" + echo "When the history_update is enabled the history file (h0)" + echo "must exist for each assimilation time step. Also the analysis" + echo "stage must be output in 'stages_to_write' within filter_nml" + exit 8 + endif + + ${LINK} $POSTERIOR_HISTORY dart_posterior_history.nc + ${LINK} $CLM_HISTORY clm_history.nc + + endif + ${EXEROOT}/dart_to_clm >& /dev/null if ($status != 0) then @@ -400,10 +456,18 @@ foreach RESTART ( ${CASE}.clm2_*.r.${LND_DATE_EXT}.nc ) unlink dart_posterior.nc unlink clm_restart.nc + + if ($HISTORY != 0) then + unlink dart_posterior_history.nc + unlink clm_history.nc + end +@ enscount ++ end endif + + # Remove the copies that we no longer need. The posterior values are # in the DART diagnostic files for the appropriate 'stage'. \rm -f clm2_*.r.${LND_DATE_EXT}.nc