libsave_ic.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #================================================================================
  2. # save_ic functions - these are used to save EC-Earth states during a run,
  3. # which can be used as Initial Conditions (ICs) for other runs.
  4. #
  5. # save_ic_get_config : define the config to use based on the save_ic config parameter
  6. #
  7. # save_ic has two options available:
  8. # save_ic:end_leg (save ICs at the end of every leg)
  9. # save_ic:end_run (save ICs at the end of the run)
  10. #
  11. # By default, without an option, ICs are saved at given offsets from the leg
  12. # start, possibly if conditions are met. These offset(s) and condition(s) must
  13. # be coded in the "else" clause below. See commented example.
  14. #
  15. function save_ic_get_config ()
  16. {
  17. do_save_ic=false
  18. save_ic_custom=false
  19. if $(has_config save_ic)
  20. then
  21. if $(has_config save_ic:end_leg)
  22. then
  23. # save ICs at the end of every leg
  24. do_save_ic=true
  25. save_ic_date_offset=( "+ ${leg_length_sec} sec" )
  26. elif $(has_config save_ic:end_run)
  27. then
  28. # save ICs at the end of the run
  29. leg_end_date_yyyymmdd=$(date -u -d "${leg_end_date}" +%Y%m%d)
  30. run_end_date_yyyymmdd=$(date -u -d "${run_end_date}" +%Y%m%d)
  31. if [ ${leg_end_date_yyyymmdd} -eq ${run_end_date_yyyymmdd} ]
  32. then
  33. do_save_ic=true && save_ic_date_offset=( "+ ${leg_length_sec} sec" )
  34. fi
  35. else
  36. echo "WARNING: save_ic requires to define condition and offset here or in the runscript"
  37. save_ic_custom=true
  38. do_save_ic=false
  39. save_ic_date_offset=( )
  40. # edit here or in the runscript the conditional statement to decide which legs to save ICs and the offset from leg start
  41. # e.g. to save at the end of every 10 legs:
  42. #EXAMPLE if (( leg_number%10 == 0 ))
  43. #EXAMPLE then
  44. #EXAMPLE do_save_ic=true
  45. #EXAMPLE save_ic_date_offset=( "+ ${leg_length_sec} sec" )
  46. #EXAMPLE fi
  47. fi
  48. fi
  49. }
  50. # save_ic_define_vars : define variables used in the save_ic functions
  51. #
  52. # The following variables must be defined in the runscript before calling this function
  53. #
  54. # 1) do_save_ic : set to true/false to enable save_ic feature
  55. #
  56. # 2) save_ic_date_offset : array of dates (offset from start of chunk, at 00:00h) to save ICs.
  57. # e.g. save_ic_date_offset=( "+1 month" "+2 month" )
  58. # The following restrictions apply, it is up to the user to ensure compliance
  59. # - first timestep of run is not supported
  60. # - if requesting first timestep of leg start, NEMO restarts from previous legs will
  61. # be used if present
  62. # - only one IC per month is supported (to simplify the IFS filter)
  63. # - maximum 9 ICs per leg (because of current restrictions in NEMO namelist)
  64. # - if requesting model-level output for IFS (e.g. for PRIMAVERA),
  65. # there will be no model-level variables in output on the timestep which
  66. # ICs are requested, unless requesting the last timestep of the leg
  67. #
  68. function save_ic_define_vars ()
  69. {
  70. if true
  71. then
  72. # save original value of ifs_lastout
  73. has_config ifs && [[ -z "${save_ic_ifs_lastout_orig:-}" ]] && save_ic_ifs_lastout_orig=${ifs_lastout}
  74. save_ic_ifs_lastout=false
  75. # sanity checks
  76. [ ${#save_ic_date_offset[@]} -gt 9 ] && error "Cannot create more than 9 ICs in a given leg"
  77. # fill vars
  78. for (( i=0; i< ${#save_ic_date_offset[@]}; i++ ))
  79. do
  80. #save_ic_date1[$i]=${save_ic_date[$i]}
  81. save_ic_date1[$i]=$(date -u -d "${leg_start_date} ${save_ic_date_offset[$i]}" +%Y%m%d)
  82. save_ic_date[$i]=$(date -uR -d "${save_ic_date1[$i]}")
  83. save_ic_sec[$i]=$(( $(date -d "${save_ic_date[$i]}" +%s) - $(date -d "${run_start_date}" +%s) ))
  84. # Correct for leap days because NEMO standalone uses no-leap calendar
  85. has_config nemo && ! has_config ifs && save_ic_sec[$i]=$(( save_ic_sec[$i] - $(leap_days "${run_start_date}" "${save_ic_date[$i]}")*24*3600 ))
  86. save_ic_day[$i]=$(( ${save_ic_sec[$i]} / 86400 ))
  87. has_config ifs && save_ic_ppt_files[$i]="ppt"$(printf %06d ${save_ic_day[$i]})"0000"
  88. has_config nemo && save_ic_nemo_ts[$i]=$(( save_ic_sec[$i] / nem_time_step_sec ))
  89. # sanity checks
  90. if [ ${save_ic_sec[$i]} -eq 0 ]
  91. then
  92. info "Initial conditions cannot be created for first timestep of run"
  93. has_config ifs && save_ic_ppt_files[$i]=""
  94. has_config nemo && save_ic_nemo_ts[$i]=-1
  95. elif [ ${save_ic_sec[$i]} -eq $leg_start_sec ]
  96. then
  97. if has_config nemo
  98. then
  99. info "Cannot create NEMO ICs for first timestep of leg at save_ic_date1[$i], will use restarts from previous leg"
  100. save_ic_nemo_ts[$i]=-1
  101. fi
  102. fi
  103. # if saving ICs at the end of the leg, set ifs_lastout=true
  104. if [ ${save_ic_sec[$i]} -eq $leg_end_sec ]
  105. then
  106. if $(has_config ifs) && ! ${save_ic_ifs_lastout_orig}
  107. then
  108. ifs_lastout=true
  109. save_ic_ifs_lastout=true
  110. fi
  111. fi
  112. done
  113. fi
  114. }
  115. # prepare output configuration (NEMO namelist, IFS ppt file)
  116. function save_ic_prepare_output()
  117. {
  118. # For NEMO this is handled in the namelist variables ln_rst_list and nn_stocklist in namelist.nemo.ref.sh
  119. if has_config nemo
  120. then
  121. nemo_stocklist=""
  122. #for (( i=0; i< ${#save_ic_date[@]}; i++ )) ; do nemo_stocklist+=$(( save_ic_sec[$i] / nem_time_step_sec )), ; done
  123. for (( i=0; i< ${#save_ic_date[@]}; i++ ))
  124. do
  125. [ ${save_ic_nemo_ts[$i]} -ne -1 ] && nemo_stocklist+=${save_ic_nemo_ts[$i]},
  126. done
  127. nemo_stocklist+=$(( leg_end_sec / nem_time_step_sec )),
  128. for (( i=${#save_ic_date[@]}; i<9 ; i++ )) ; do nemo_stocklist+=0, ; done
  129. nemo_stocklist+=$'\n'
  130. fi
  131. # For IFS we need to create links to special ppt file
  132. if has_config ifs
  133. then
  134. # create the special ppt file
  135. save_ic_ppt_filename=pptdddddd0000_save_ic
  136. save_ic_ppt_ifile=pptdddddd0000 # change this for e.g. primavera
  137. # these are the codes which must be added to the special ppt file used for generating
  138. # required IFS model-level output at each requested timestep
  139. save_ic_MFP2DF_param_ids="129,152"
  140. save_ic_MFPPHY_param_ids="139,170,183,236,39,40,41,42,141,198,235,31,238,32,33,34,35,36,37,38,148,172,173,174,15,17,16,18,66,67,74,43,160,161,162,163,234,28,27,30,29" #all variables needed
  141. #save_ic_MFPPHY_param_ids="198,36,37,38,148,172,173,174,15,17,16,18,66,67,74,43,160,161,162,163,234,28,27,30,29" #all variables (old)
  142. #save_ic_MFPPHY_param_ids="198,36,37,38,148,172,173,174,15,17,16,18,74,43,160,161,162,163,234" #trunk
  143. #save_ic_MFPPHY_param_ids="173,174,15,17,16,18,66,67,74,43,160,161,162,163,234,28,27,30,29" #primavera
  144. save_ic_filter_param_ids=""
  145. # get requested output codes as a single line without space and without the last comma
  146. NFPPHY=`grep NFPPHY postins/$save_ic_ppt_ifile | sed -e 's/^\s*NFPPHY\s*=\s*//' -e 's/[,]$//'`
  147. MFPPHY=$(sed -e '1h;2,$H;$!d;g' -re 's/.*MFPPHY *=([0-9,\. \n]+).*/\1/' -e 's/[ \n]//g' -e 's/,$//' <postins/$save_ic_ppt_ifile)
  148. [[ $MFPPHY == "" ]] && error "IFS ppt file postins/$save_ic_ppt_ifile does not contain any entries in MFPPHY."
  149. NFP2DF=`grep NFP2DF postins/$save_ic_ppt_ifile | sed -e 's/^\s*NFP2DF\s*=\s*//' -e 's/[,]$//'`
  150. MFP2DF=$(sed -e '1h;2,$H;$!d;g' -re 's/.*MFP2DF *=([0-9,\. \n]+).*/\1/' -e 's/[ \n]//g' -e 's/,$//' <postins/$save_ic_ppt_ifile)
  151. [[ $MFP2DF == "" ]] && NFP2DF=0 && MFP2DF=","
  152. IFS=","
  153. old_MFPPHY_params=( ${MFPPHY} )
  154. old_MFP2DF_params=( ${MFP2DF} )
  155. new_MFPPHY_params=( ${save_ic_MFPPHY_param_ids} )
  156. new_MFP2DF_params=( ${save_ic_MFP2DF_param_ids} )
  157. unset IFS
  158. # update requested output codes by adding necessary variables
  159. for new_param_id in ${new_MFPPHY_params[@]}
  160. do
  161. new_param_id=`printf "%0*d" 3 $new_param_id`
  162. found=false
  163. for old_param_id in ${old_MFPPHY_params[@]}
  164. do
  165. old_param_id=`printf "%03g" $old_param_id`
  166. [ ${old_param_id} -eq ${new_param_id} ] && found=true && break
  167. done
  168. if ! $found
  169. then
  170. NFPPHY=$(( $NFPPHY + 1 ))
  171. MFPPHY="${MFPPHY},$new_param_id"
  172. save_ic_filter_param_ids="${save_ic_filter_param_ids}$new_param_id,"
  173. fi
  174. done
  175. for new_param_id in ${new_MFP2DF_params[@]}
  176. do
  177. new_param_id=`printf "%0*d" 3 $new_param_id`
  178. found=false
  179. for old_param_id in ${old_MFP2DF_params[@]}
  180. do
  181. old_param_id=`printf "%03g" $old_param_id`
  182. [ ${old_param_id} -eq ${new_param_id} ] && found=true && break
  183. done
  184. if ! $found
  185. then
  186. NFP2DF=$(( $NFP2DF + 1 ))
  187. MFP2DF="${MFP2DF},$new_param_id"
  188. fi
  189. done
  190. # create special ppt file with modified codes from original ppt file
  191. sed -r -e '1h;2,$H;$!d;g' \
  192. -e 's|(NFPPHY\s*=)[0-9 ]+|\1'${NFPPHY}'|' \
  193. -e 's|(MFPPHY\s*=)[0-9, \n]+\n|\1'${MFPPHY}',\n|' \
  194. -e 's|(NFP2DF\s*=)\s*[0-9]+|\1'${NFP2DF}'|' \
  195. -e 's|(MFP2DF\s*=)[0-9, \n]+\n|\1'${MFP2DF}',\n|' \
  196. -e 's|NFP3DFS\s*=\s*[0-9]+,||' \
  197. -e 's|MFP3DFS\s*=[0-9, \n]+\n|\n|' \
  198. -e 's|NRFP3S\s*=\s*\-?[0-9]+,||' \
  199. -e 's/&NAMFPC/\&NAMFPC\n NFP3DFS = 9,\n MFP3DFS = 133,75,76,246,247,248,138,155,130,\n NRFP3S = -99\n NFP2DF = 2,\n MFP2DF = 152,129,/' \
  200. <postins/$save_ic_ppt_ifile >postins/${save_ic_ppt_filename}
  201. # cleanup existing links from previous legs (required when save_ic_ifs_lastout=true)
  202. ls -ltr postins
  203. find -L postins/ -xtype l -samefile postins/${save_ic_ppt_filename} -exec rm {} \;
  204. # create links to ppt file for all requested timesteps
  205. for (( i=0; i< ${#save_ic_date[@]}; i++ ))
  206. do
  207. if [ ! -f postins/$save_ic_ppt_filename ]
  208. then
  209. error "IFS IC file $save_ic_ppt_filename does not exist."
  210. elif [ "${save_ic_ppt_files[$i]}" != "" ] ; then
  211. ln -sf $save_ic_ppt_filename postins/${save_ic_ppt_files[$i]}
  212. fi
  213. done
  214. /bin/ls -1 postins/* > dirlist
  215. ls -ltr postins
  216. fi
  217. }
  218. # post-process save_ic results
  219. function save_ic_postproc()
  220. {
  221. # cleanup existing links for subsequent legs (see issue #345)
  222. if has_config ifs
  223. then
  224. ls -ltr postins
  225. find -L postins/ -xtype l -samefile postins/${save_ic_ppt_filename} -exec rm {} \;
  226. /bin/ls -1 postins/* > dirlist
  227. ls -ltr postins
  228. fi
  229. for (( i=0; i< ${#save_ic_date[@]}; i++ ))
  230. do
  231. outdir="save_ic/"${save_ic_date1[$i]}
  232. # save IFS ICs
  233. if has_config ifs
  234. then
  235. [ "${save_ic_ppt_files[$i]}" != "" ] && save_ic_ifs_out2init $i
  236. fi
  237. # copy oasis restarts
  238. if has_config oasis
  239. then
  240. # copy oasis restarts if this is the last or first timestep of the leg
  241. # oasis files depend on the components and configuration
  242. # so they go in their own sub-directory, easier to separate from ifs and nemo files
  243. if [ ${save_ic_sec[$i]} -eq $leg_end_sec ]
  244. then
  245. mkdir -p ${outdir}/oasis
  246. for f in ${oas_rst_files} ; do test -f ${f} && cp ${f} ${outdir}/oasis ; done
  247. elif [ ${save_ic_sec[$i]} -eq $leg_start_sec ]
  248. then
  249. mkdir -p ${outdir}/oasis
  250. oas_rst_dir="restart/oasis/$(printf %03d $((leg_number)))"
  251. for f in ${oas_rst_files} ; do test -f ${oas_rst_dir}/${f} && cp ${oas_rst_dir}/${f} ${outdir}/oasis ; done
  252. fi
  253. fi
  254. # copy NEMO restarts
  255. if has_config nemo
  256. then
  257. nemo_ts=$(( save_ic_sec[$i] / nem_time_step_sec ))
  258. ns=$(printf %08d $(( nemo_ts - nem_restart_offset )))
  259. # copy each restart type seperately, depending on config
  260. extensions="oce"
  261. has_config lim3 && extensions+=" ice"
  262. has_config pisces && extensions+=" trc"
  263. for ext in $extensions
  264. do
  265. if ls ${exp_name}_${ns}_restart_${ext}_????.nc > /dev/null 2>&1
  266. then
  267. mkdir -p ${outdir}/nemo
  268. if [[ ${save_ic_sec[$i]} -eq $leg_start_sec ]] || [[ ${save_ic_sec[$i]} -eq $leg_end_sec ]]
  269. then
  270. cp -f ${exp_name}_${ns}_restart_${ext}_????.nc ${outdir}/nemo
  271. else
  272. mv -f ${exp_name}_${ns}_restart_${ext}_????.nc ${outdir}/nemo
  273. fi
  274. else
  275. info "Cannot find NEMO ($ext) initial conditions for date ${save_ic_date1[$i]}"
  276. fi
  277. done
  278. fi
  279. # copy TM5 restarts
  280. leg_end_date_yyyymmdd=$(date -u -d "${leg_end_date}" +%Y%m%d)
  281. if has_config tm5 && [[ $leg_end_date_yyyymmdd == ${save_ic_date1[$i]} ]]
  282. then
  283. found=false
  284. for f in TM5_restart_${leg_end_date_yyyymmdd}_0000_glb300x200.nc save_${leg_end_date_yyyymmdd}00_glb300x200.hdf
  285. do
  286. if [[ -f "$f" ]] ; then
  287. mkdir -p ${outdir}/tm5
  288. cp -f $f ${outdir}/tm5
  289. found=true
  290. fi
  291. done
  292. ! $found && info "Cannot find TM5 restarts for date ${save_ic_date1[$i]}" || true
  293. fi
  294. done
  295. }
  296. # Function to create IC files for IFS from special output based on a script from K. Wyser
  297. function save_ic_ifs_out2init()
  298. {
  299. icdate=${save_ic_date1[$1]}
  300. #srcdir=$2
  301. filter_output=true
  302. grib_filter=${GRIB_BIN_PATH}${GRIB_BIN_PATH:+/}grib_filter
  303. # temporary directory
  304. tmpdir=tmp_save_ic
  305. [ -d $tmpdir ] && rm -rf ${tmpdir}
  306. mkdir -p $tmpdir
  307. # first IFS timestep of a month is saved in last output file
  308. if [[ "$(date -d "${icdate}" +%d)" == "01" ]] ; then
  309. yyyymm=$(date -d "${icdate} - 6 hours" +%Y%m)
  310. else
  311. yyyymm=$(date -d "${icdate}" +%Y%m)
  312. fi
  313. # find the ICMSH/GG files containing the last timestep, which are in $srcdir/output/ifs/???
  314. #local ifile_sh=`find $srcdir -name ICMSH${exp_name}+$yyyymm -print -quit`
  315. #local ifile_gg=`find $srcdir -name ICMGG${exp_name}+$yyyymm -print -quit`
  316. ifile_sh=ICMSH${exp_name}+$yyyymm
  317. ifile_gg=ICMGG${exp_name}+$yyyymm
  318. if [[ ! -f "$ifile_sh" ]] ; then info "cannot find ICMSH file $ifile_sh to create initial conditions at $icdate!" ; return 0 ; fi
  319. if [[ ! -f "$ifile_gg" ]] ; then info "cannot find ICMGG file $ifile_gg to create initial conditions at $icdate!" ; return 0 ; fi
  320. if $filter_output
  321. then
  322. # define grib filters to separate data for IC from normal output
  323. # filter_time_init is to define which timestep contains the ICs, currently only one per month is supported
  324. filter_time_init="dataDate == ${icdate} && dataTime == 0"
  325. # filter_time_out is to filter any timesteps which might contain ICs
  326. filter_time_out="dataTime == 0 && ( 0 "
  327. for (( j=0; j< ${#save_ic_date[@]}; j++ )) ; do filter_time_out+=" || ( dataDate == ${save_ic_date1[$j]} )"; done
  328. filter_time_out+=" )"
  329. $save_ic_ifs_lastout && filter_last="( dataTime == 0 ) && ( dataDate == ${save_ic_date1[$((j-1))]} )" || filter_last="0"
  330. # build expression to filter the paramIDs which we have to filter out, set them in ini_filter_param_ids in the runscript
  331. #save_ic_filter_param_ids="198,36,37,38,148,172,173,174,15,17,16,18,74,43,160,161,162,163,234"
  332. filter_params="1"
  333. IFS=","
  334. for p in $save_ic_filter_param_ids ; do filter_params+=" && paramId != $p" ; done
  335. unset IFS
  336. # SH file
  337. ofile_sh_init=${tmpdir}/sh_init.grb
  338. ofile_sh_out=${tmpdir}/sh_out.grb
  339. ofile_sh_last=${tmpdir}/sh_last.grb
  340. filter_sh=${tmpdir}/filter_sh
  341. write_sh_init="if ( $filter_time_init ) { write \"${ofile_sh_init}\"; }"
  342. write_sh_out="if ( $filter_last ) { write \"${ofile_sh_last}\"; } else { write \"${ofile_sh_out}\"; }"
  343. cat > $filter_sh << EOT
  344. if ( ! ( $filter_time_out ) ) { write "${ofile_sh_out}"; }
  345. else {
  346. if ( typeOfLevel is "hybrid" ) { ${write_sh_init}; }
  347. else { ${write_sh_out}; }
  348. }
  349. EOT
  350. #GG file
  351. ofile_gg_init=${tmpdir}/gg_init.grb
  352. ofile_gg_out=${tmpdir}/gg_out.grb
  353. ofile_gg_last=${tmpdir}/gg_last.grb
  354. filter_gg=${tmpdir}/filter_gg
  355. write_gg_init="if ( $filter_time_init ) { write \"${ofile_gg_init}\"; }"
  356. write_gg_out="if ( $filter_last ) { write \"${ofile_gg_last}\"; } else { write \"${ofile_gg_out}\"; }"
  357. cat > $filter_gg << EOT
  358. if ( ! ( $filter_time_out ) ) { write "${ofile_gg_out}"; }
  359. else {
  360. if ( levelType is "ml" ) { ${write_gg_init}; }
  361. else {
  362. if ( levelType is "pl" ) { ${write_gg_out}; }
  363. else {
  364. if ( $filter_params ) {
  365. ${write_gg_init};
  366. ${write_gg_out};
  367. }
  368. else {
  369. ${write_gg_init};
  370. }
  371. }
  372. }
  373. }
  374. EOT
  375. #run grib_filter on SH and GG files
  376. $grib_filter $filter_sh $ifile_sh
  377. $grib_filter $filter_gg $ifile_gg
  378. if [[ ! -f "$ofile_sh_init" ]] ; then info "ICMSH file $ifile_sh does not contain data to create initial conditions at $icdate" ; return 0 ; fi
  379. if [[ ! -f "$ofile_gg_init" ]] ; then info "ICMGG file $ifile_gg does not contain data to create initial conditions at $icdate" ; return 0 ; fi
  380. #rename files
  381. ifile_sh=${tmpdir}/$(basename $ifile_sh)
  382. ifile_gg=${tmpdir}/$(basename $ifile_gg)
  383. mv ${ofile_sh_init} ${ifile_sh}
  384. mv ${ofile_gg_init} ${ifile_gg}
  385. #srcdir=${tmpdir}
  386. fi # $filter_output
  387. if [[ ! -f "$ifile_sh" ]] ; then info "cannot find ICMSH file $ifile_sh to create initial conditions at $icdate!" ; return 0 ; fi
  388. if [[ ! -f "$ifile_gg" ]] ; then info "cannot find ICMGG file $ifile_gg to create initial conditions at $icdate!" ; return 0 ; fi
  389. tgtdir=save_ic/$icdate/ifs
  390. ofile_sh=${tgtdir}/ICMSH${exp_name}INIT
  391. ofile_gg_init=${tgtdir}/ICMGG${exp_name}INIT
  392. ofile_gg_iniua=${tgtdir}/ICMGG${exp_name}INIUA
  393. # make sure the output folder is created and delete any existing files
  394. #[ -d $tgtdir ] && rm -rf ${tgtdir}/* || mkdir -p $tgtdir
  395. mkdir -p $tgtdir
  396. rm -f $ofile_sh $ofile_gg_init $ofile_gg_iniua
  397. cat > ${tmpdir}/gf1 << EOT
  398. if ( typeOfLevel is "hybrid" ) { write "${tmpdir}/shinit.[shortName].[level]"; }
  399. EOT
  400. $grib_filter ${tmpdir}/gf1 $ifile_sh
  401. cp -f ${tmpdir}/shinit.lnsp.1 $ofile_sh
  402. for lev in {1..91}
  403. do
  404. for var in vo d t
  405. do
  406. cat ${tmpdir}/shinit.$var.$lev >> $ofile_sh
  407. done
  408. done
  409. cat ${tmpdir}/shinit.z.1 >> $ofile_sh
  410. cat > ${tmpdir}/gf2 << EOT
  411. write "${tmpdir}/gginit.[shortName]";
  412. EOT
  413. $grib_filter ${tmpdir}/gf2 $ifile_gg
  414. for var in stl1 stl2 stl3 stl4 swvl1 swvl2 swvl3 swvl4 sd src skt ci tsn asn \
  415. rsn sst istl1 istl2 istl3 istl4 chnk lsm sr al aluvp alnip aluvd alnid \
  416. lai_lv lai_hv sdfor slt sdor isor anor slor lsrh cvh cvl tvh tvl
  417. do
  418. cat ${tmpdir}/gginit.$var >> $ofile_gg_init
  419. done
  420. cat > ${tmpdir}/gf3 << EOT
  421. write "${tmpdir}/gginiua.[shortName].[level]";
  422. EOT
  423. $grib_filter ${tmpdir}/gf3 $ifile_gg
  424. # ${tmpdir}/gginiua.o3 \ check this!
  425. for lev in {1..91}
  426. do
  427. for var in q
  428. do
  429. cat ${tmpdir}/gginiua.$var.$lev >> $ofile_gg_iniua
  430. done
  431. done
  432. for lev in {1..91}
  433. do
  434. for var in crwc cswc clwc ciwc cc
  435. do
  436. cat ${tmpdir}/gginiua.$var.$lev >> $ofile_gg_iniua
  437. done
  438. done
  439. # copy filtered files to the appropriate location
  440. if $filter_output
  441. then
  442. # move filter files to $tgtdir, in case something went wrong
  443. mv -f $filter_sh $filter_gg $tgtdir
  444. ifile_sh=$(basename $ifile_sh)
  445. ifile_gg=$(basename $ifile_gg)
  446. # keep the full output files as a backup
  447. mv -f $ifile_sh $tgtdir/${ifile_sh}-out+init
  448. mv -f $ifile_gg $tgtdir/${ifile_gg}-out+init
  449. # move the filtered output file to the runtime folder
  450. [ -f $ofile_sh_out ] && mv -f $ofile_sh_out $ifile_sh || error "save_ic_ifs_out2init - warning! SH file $ofile_sh_out missing!"
  451. [ -f $ofile_gg_out ] && mv -f $ofile_gg_out $ifile_gg || error "save_ic_ifs_out2init - warning! GG file $ofile_gg_out missing!"
  452. # keep a copy of the last timestep
  453. if $save_ic_ifs_lastout
  454. then
  455. [ -f $ofile_sh_last ] && mv -f $ofile_sh_last $tgtdir/${ifile_sh}-ifs_lastout
  456. [ -f $ofile_gg_last ] && mv -f $ofile_gg_last $tgtdir/${ifile_gg}-ifs_lastout
  457. fi
  458. fi #$filter_output
  459. # delete tmp folder
  460. rm -rf $tmpdir
  461. echo "save_ic_ifs_out2init ended successfully, results are in $tgtdir"
  462. }