source: branches/libIGCM_concurrent/libIGCM_comp/libIGCM_comp.ksh @ 1628

Last change on this file since 1628 was 1628, checked in by aclsce, 3 months ago

Modified to add the functionality to run many instances in concurrent mode.
Only available on Irene SKL with slurm.

  • Property licence set to
    The following licence information concerns ONLY the libIGCM tools
    ==================================================================

    Copyright © Centre National de la Recherche Scientifique CNRS
    Commissariat à l'Énergie Atomique CEA

    libIGCM : Library for Portable Models Computation of IGCM Group.

    IGCM Group is the french IPSL Global Climate Model Group.

    This library is a set of shell scripts and functions whose purpose is
    the management of the initialization, the launch, the transfer of
    output files, the post-processing and the monitoring of datas produce
    by any numerical program on any plateforme.

    This software is governed by the CeCILL license under French law and
    abiding by the rules of distribution of free software. You can use,
    modify and/ or redistribute the software under the terms of the CeCILL
    license as circulated by CEA, CNRS and INRIA at the following URL
    "http://www.cecill.info".

    As a counterpart to the access to the source code and rights to copy,
    modify and redistribute granted by the license, users are provided only
    with a limited warranty and the software's author, the holder of the
    economic rights, and the successive licensors have only limited
    liability.

    In this respect, the user's attention is drawn to the risks associated
    with loading, using, modifying and/or developing or reproducing the
    software by the user in light of its specific status of free software,
    that may mean that it is complicated to manipulate, and that also
    therefore means that it is reserved for developers and experienced
    professionals having in-depth computer knowledge. Users are therefore
    encouraged to load and test the software's suitability as regards their
    requirements in conditions enabling the security of their systems and/or
    data to be ensured and, more generally, to use and operate it in the
    same conditions as regards security.

    The fact that you are presently reading this means that you have had
    knowledge of the CeCILL license and that you accept its terms.
  • Property svn:keywords set to Revision Author Date
File size: 85.9 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Sebastien Denvil, Martial Mancip
5# Contact: Sebastien.Denvil__at__ipsl.jussieu.fr Martial.Mancip__at__ipsl.jussieu.fr
6# $Revision::                                          $ Revision of last commit
7# $Author::                                            $ Author of last commit
8# $Date::                                              $ Date of last commit
9# IPSL (2006)
10#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
11#
12#**************************************************************
13
14#========================================================================
15function IGCM_comp_Initialize
16{
17  IGCM_debug_PushStack "IGCM_comp_Initialize"
18
19  # Debug Print :
20  echo
21  IGCM_debug_Print 1 "IGCM_comp_Initialize"
22  echo
23
24  typeset comp compname comptagname auxprint card_UserChoices first_option option i j
25  for comp in ${config_ListOfComponents[*]} ; do
26
27    # Define component
28    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/config.card ListOfComponents ${comp}
29    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
30    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
31
32    # Debug Print
33    IGCM_debug_Print 1 "Initialize ${comp} : ${compname} component."
34
35    # ${compname}.card PATH
36    card=${SUBMIT_DIR}/COMP/${compname}.card
37
38    # Manage component executable
39    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/config.card Executable ${comp}
40
41    # Define all options in section [comp]
42    IGCM_debug_Print 3 " DefineArrayFromSection : ${comp}"
43    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/config.card ${comp}
44    eval config_comp=\${config_${comp}[*]} > /dev/null 2>&1
45    for option in ${config_comp[*]} ; do
46        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} ${option}
47    done
48    IGCM_debug_Print 3 " Found in section config_${comp} :  ${config_comp[*]} "
49
50    # Debug Print :
51    eval auxprint=\${config_${comp}_WriteFrequency}
52    IGCM_debug_Print 1 "Write frequency for ${compname} : ${auxprint} "
53    #2> /dev/null
54
55    # Debug Print :
56    IGCM_debug_Print 2 "Initialize following component library"
57
58    # Source drivers in directory DRIVER if it exist
59    # else source them from directory COMP
60    if [ -d ${SUBMIT_DIR}/DRIVER ] ; then
61      IGCM_debug_Print 2 ${SUBMIT_DIR}/DRIVER/${compname}.driver
62      # Source component library
63      . ${SUBMIT_DIR}/DRIVER/${compname}.driver
64    else
65      IGCM_debug_Print 2 ${SUBMIT_DIR}/COMP/${compname}.driver
66      # Source component library
67      . ${SUBMIT_DIR}/COMP/${compname}.driver
68    fi
69    IGCM_debug_Print 3 "With tag : ${comptagname}"
70
71    # Debug Print
72    IGCM_debug_Print 3 "Initialize ${comp} output directory."
73
74    # Define ARCHIVED Dirs
75    eval R_OUT_${comp}=${R_SAVE}/${comp}
76    eval IGCM_sys_MkdirArchive \${R_OUT_${comp}}
77
78    eval R_OUT_${comp}_O=\${R_OUT_${comp}}/Output
79    eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_O}
80
81    eval R_OUT_${comp}_A=\${R_OUT_${comp}}/Analyse
82    eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_A}
83
84    eval R_OUT_${comp}_R=\${R_OUT_${comp}}/Restart
85    [ ${config_Post_PackFrequency} = NONE ] && eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_R}
86
87    eval R_OUT_${comp}_D=\${R_OUT_${comp}}/Debug
88    [ ${config_Post_PackFrequency} = NONE ] && eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_D}
89
90    eval R_OUT_${comp}_O_H=\${R_OUT_${comp}_O}/HF
91    eval R_OUT_${comp}_O_D=\${R_OUT_${comp}_O}/DA
92    eval R_OUT_${comp}_O_M=\${R_OUT_${comp}_O}/MO
93    eval R_OUT_${comp}_O_Y=\${R_OUT_${comp}_O}/YE
94
95    eval R_OUT_${comp}_A_H=\${R_OUT_${comp}_A}/TS_HF
96    eval R_OUT_${comp}_A_D=\${R_OUT_${comp}_A}/TS_DA
97    eval R_OUT_${comp}_A_M=\${R_OUT_${comp}_A}/TS_MO
98    eval R_OUT_${comp}_A_Y=\${R_OUT_${comp}_A}/TS_YE
99
100    # Define BUFFERED Dirs
101    if ( [ ! ${config_Post_PackFrequency} = NONE ] || [ X${config_UserChoices_SpaceName} = XTEST ] ) ; then
102      eval R_BUF_${comp}=${R_BUFR}/${comp}
103      eval IGCM_sys_Mkdir \${R_BUF_${comp}}
104
105      eval R_BUF_${comp}_O=\${R_BUF_${comp}}/Output
106      eval IGCM_sys_Mkdir \${R_BUF_${comp}_O}
107
108      eval R_BUF_${comp}_A=\${R_BUF_${comp}}/Analyse
109      eval IGCM_sys_Mkdir \${R_BUF_${comp}_A}
110
111      eval R_BUF_${comp}_R=\${R_BUF_${comp}}/Restart
112      eval IGCM_sys_Mkdir \${R_BUF_${comp}_R}
113
114      eval R_BUF_${comp}_D=\${R_BUF_${comp}}/Debug
115      eval IGCM_sys_Mkdir \${R_BUF_${comp}_D}
116
117      eval R_BUF_${comp}_O_H=\${R_BUF_${comp}_O}/HF
118      eval R_BUF_${comp}_O_D=\${R_BUF_${comp}_O}/DA
119      eval R_BUF_${comp}_O_M=\${R_BUF_${comp}_O}/MO
120      eval R_BUF_${comp}_O_Y=\${R_BUF_${comp}_O}/YE
121
122      eval R_BUF_${comp}_A_H=\${R_BUF_${comp}_A}/TS_HF
123      eval R_BUF_${comp}_A_D=\${R_BUF_${comp}_A}/TS_DA
124      eval R_BUF_${comp}_A_M=\${R_BUF_${comp}_A}/TS_MO
125      eval R_BUF_${comp}_A_Y=\${R_BUF_${comp}_A}/TS_YE
126
127      # Define CMIP6 Dirs
128      eval CMIP6_BUF_${comp}=${R_CMIP}/CMIP6/${comp}
129    fi
130
131    # Read UserChoices section of component card
132    IGCM_debug_Print 2 "DefineArrayFromSection : ${compname}_UserChoices ${card}"
133    IGCM_card_DefineArrayFromSection ${card} UserChoices
134    eval first_option=\${${compname}_UserChoices[0]} > /dev/null 2>&1
135
136    # If section is not empty we define corresponding variables
137    if [ X${first_option} != X"Error:" ] ; then
138      if [ X${card_UserChoices[0]} != X ] ; then
139        unset card_UserChoices
140      fi
141      eval set +A card_UserChoices -- \${${compname}_UserChoices[*]} > /dev/null 2>&1
142      IGCM_debug_Print 3 "${compname}_UserChoices_values:"
143      for option in ${card_UserChoices[*]} ; do
144        IGCM_card_DefineVariableFromOption ${card} UserChoices ${option}
145        eval IGCM_debug_Print 3 "${option}=\${${compname}_UserChoices_${option}}"
146      done
147    fi
148
149    # Read and Build Output File stuff
150    IGCM_debug_Print 2 "DefineArrayFromOption  : ${compname}_OutputFiles ${card}"
151    IGCM_card_DefineArrayFromOption ${card} OutputFiles List
152    ListFilesName=${compname}_OutputFiles_List
153    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
154    #
155    if [ X${FileName0} != X${NULL_STR} ] ; then
156      #
157      #IGCM_debug_Print 1 "Component      : ${compname}"
158      #
159      # INITIALISATION
160      #
161      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
162      #
163      i=2
164      #
165      until [ $i -ge $NbFiles ]; do
166        #
167        eval flag_post=\${${ListFilesName}[$i]} > /dev/null 2>&1
168        #
169        if [ X${flag_post} != XNONE ] ; then
170          #
171          # First of all
172          #
173          IGCM_card_DefineArrayFromSection ${card} ${flag_post}
174          # This section is mandatory
175          if [ "X$( eval echo \${${compname}_${flag_post}[@]} )" = "X" ] ; then
176            IGCM_debug_Print 1 "IGCM_card_DefineArrayFromSection ${card} ${flag_post}"
177            IGCM_debug_Exit "${flag_post} section do not exist in ${card}. Please check your card."
178            IGCM_debug_Verif_Exit
179          fi
180          #
181          # Seasonal case : If option Seasonal is not found (old cards) put SEASONAL ON by default
182          #
183          # variable option allready typeset above
184          for option in $( eval echo \${${compname}_${flag_post}[*]} ) ; do
185            if [ ${option} = Seasonal ] ; then
186              FoundSeasonal=true
187              IGCM_card_DefineVariableFromOption ${card} ${flag_post} Seasonal
188            fi
189          done
190          #
191          if [ ! X${FoundSeasonal} = Xtrue ] ; then
192            eval ${compname}_${flag_post}_Seasonal=ON
193          fi
194          #
195          if [ $( eval echo \${${compname}_${flag_post}_Seasonal} ) = ON ] ; then
196            Seasonal=true
197          fi
198
199          # Dimension = vide si vieille card.
200          IGCM_card_DefineArrayFromOption ${card} ${flag_post} TimeSeriesVars
201          IGCM_card_DefineArrayFromOption ${card} ${flag_post} Patches
202          if [ X"$( eval echo \${${compname}_${flag_post}_TimeSeriesVars[*]} )" = X"Option not" ] ; then
203            # New TimeSeriesVar description, with 2D, 3D and associate ChunckJob.
204            ListDimension[0]=2D
205            ListDimension[1]=3D
206            TimeSeries=false
207            iLoop=${#ListDimension[*]}
208            j=0
209            until [ $j -ge ${iLoop} ]; do
210              Dimension=${ListDimension[${j}]}
211              IGCM_card_DefineArrayFromOption ${card} ${flag_post} TimeSeriesVars${Dimension}
212              IGCM_card_DefineVariableFromOption ${card} ${flag_post} ChunckJob${Dimension}
213              #
214              # Time series WITHOUT chunk
215              #
216              if [ ! $( eval echo \${${compname}_${flag_post}_TimeSeriesVars${Dimension}} ) = ${NULL_STR} ] ; then
217                if [ $( eval echo \${${compname}_${flag_post}_ChunckJob${Dimension}} ) = NONE ] ; then
218                  IGCM_debug_Print 3 "${Dimension} time series activated for ${flag_post}"
219                  eval TimeSeries${Dimension}=true
220                fi
221              fi
222              #
223              # Time series WITH chunk
224              #
225              if [ ! $( eval echo \${${compname}_${flag_post}_TimeSeriesVars${Dimension}} ) = ${NULL_STR} ] ; then
226                chunck_size=$( eval echo \${${compname}_${flag_post}_ChunckJob${Dimension}} )
227                if [ ! ${chunck_size} = NONE ] &&  [ ! ${chunck_size} = OFF ] ; then
228                  IGCM_debug_Print 3 "${Dimension} time series activated with chunck for ${flag_post}"
229                  eval TimeSeriesChunck${Dimension}=true
230                  eval CHUNCK${Dimension}_COMP[\${#CHUNCK${Dimension}_COMP[*]}]=${comp}
231                  eval CHUNCK${Dimension}_FLAG[\${#CHUNCK${Dimension}_FLAG[*]}]=${i}
232                  eval CHUNCK${Dimension}_NAME[\${#CHUNCK${Dimension}_NAME[*]}]=${flag_post}
233                  eval CHUNCK${Dimension}_SIZE[\${#CHUNCK${Dimension}_SIZE[*]}]=${chunck_size}
234                fi
235              fi
236              (( j=j+1 ))
237            done
238          else
239            ListDimension[0]=""
240            TimeSeries=true
241            TimeSeries2D=false
242            TimeSeries3D=false
243            TimeSeriesChunck2D=false
244            TimeSeriesChunck3D=false
245          fi
246        fi
247        ((i_ = i +1))
248        eval Testvar_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
249        eval Testvar=${Testvar_}
250        if [[ ${Testvar} =~ [0-9][DMY]$ ]] ; then
251            ((i = i+4))
252        else
253        (( i=i+3 ))
254        fi
255      done
256    fi
257    # Debug Print
258    IGCM_debug_Print 3 "Initialize ${compname} with driver."
259    # INIT component
260    ${comp}_Initialize
261    echo
262  done
263
264  IGCM_debug_PopStack "IGCM_comp_Initialize"
265}
266
267#=======================================================================
268function IGCM_comp_PrepareDeletedFiles
269{
270  IGCM_debug_PushStack "IGCM_comp_PrepareDeletedFiles" $@
271
272  if [ X${2} != X. ] ; then
273    eval FileToBeDeleted[${#FileToBeDeleted[@]}]=$( basename ${2} ) > /dev/null 2>&1
274  else
275    eval FileToBeDeleted[${#FileToBeDeleted[@]}]=$( basename ${1} ) > /dev/null 2>&1
276  fi
277
278  IGCM_debug_PopStack "IGCM_comp_PrepareDeletedFiles"
279}
280
281#=======================================================================
282function IGCM_comp_PrepareXiosFiles
283{
284  IGCM_debug_PushStack "IGCM_comp_PrepareXiosFiles" $@
285
286  typeset file
287  [ X${2} != X. ] && file=$( basename ${2} ) || file=$( basename ${1} )
288
289  if [ $( echo ${file} | grep "^field_def_" | wc -l ) = 1 ] ; then
290    eval FieldDef[${#FieldDef[@]}]=${file} > /dev/null 2>&1
291  fi
292  if [ $( echo ${file} | grep "^file_def_" | wc -l ) = 1 ] ; then
293    eval FileDef[${#FileDef[@]}]=${file} > /dev/null 2>&1
294  fi
295  if [ $( echo ${file} | grep "^timeseries_def_" | wc -l ) = 1 ] ; then
296    eval FileDef[${#FileDef[@]}]=${file} > /dev/null 2>&1
297  fi
298 
299  IGCM_debug_PopStack "IGCM_comp_PrepareXiosFiles"
300}
301
302#=======================================================================
303function IGCM_comp_GetInputInitialStateFiles
304{
305  IGCM_debug_PushStack "IGCM_comp_GetInputInitialStateFiles"
306
307  # Debug Print :
308  echo
309  IGCM_debug_Print 1 "IGCM_comp_GetInputInitialStateFiles"
310  echo
311
312  # Only the first time step need InitialStateFiles
313  # otherwise it's BoundaryConditions
314  if ( ${FirstInitialize} ) ; then
315    typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_
316    typeset file_in_ file_in file_out_ file_out do_init
317    for comp in ${config_ListOfComponents[*]} ; do
318        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
319        if [ X${number_rundir} != X ] ; then 
320            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
321            cd RUNDIR_${number_rundir} ;
322        fi
323      # Initialize
324      do_init="y"
325      # Do we need to bring initial state file for this component
326      if ( [ "${config_Restarts_OverRule}" = "y" ] || [ "${config_Restarts_OverRule}" = "Y" ] ) ; then
327        eval do_init="n"
328      else
329        # Read component Restarts parameters
330        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} Restart
331        eval do_start=\${config_${comp}_Restart} > /dev/null 2>&1
332        if [ "${do_start}" = "y" ] ; then
333          do_init="n"
334        else
335          do_init="y"
336        fi
337      fi
338
339      if [ "${do_init}" = "y" ] ; then
340        # Define component
341        eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
342        eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
343
344        # Debug Print :
345        IGCM_debug_Print 3 "Initialisation files ${compname}"
346
347        card=${SUBMIT_DIR}/COMP/${compname}.card
348
349        IGCM_card_DefineArrayFromOption ${card} InitialStateFiles List
350        ListFilesName=${compname}_InitialStateFiles_List
351
352        eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
353        if [ X${FileName0} != X${NULL_STR} ] ; then
354          eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
355          (( i=0 ))
356          until [ $i -ge $NbFiles ]; do
357            eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
358            eval file_in=${file_in_}
359            (( i_ = i+1 ))
360            eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
361            eval file_out=${file_out_}
362
363            IGCM_sys_IsFileArchived ${file_in}
364            if [ $? = 0 ] ; then
365              IGCM_sys_Get ${file_in} ${file_out}
366              #IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
367            else
368              IGCM_sys_Cp ${file_in} ${file_out}
369            fi
370            (( i=i+2 ))
371          done
372        fi
373      fi
374      if [ X${number_rundir} != X ] ; then
375          cd $RUN_DIR 
376      fi
377    done
378  fi
379  IGCM_debug_PopStack "IGCM_comp_GetInputInitialStateFiles"
380}
381
382#=======================================================================
383# Definition of Smooth modulo function
384# usage :
385# IGCM_SmoothModulo StringModulo value
386#
387# StringModulo : A string of min max and modulo like definition of Scilab vectors.
388# [min]:[modulo:][max]
389# where :
390# [] value are optionnals;
391# empty min equal 1
392# empty max equal infinity
393# modulo not given or empty equal 1
394# empty string or just ':' equal always.
395#
396# value : the value to test with the definition
397#
398# return : true(1)/false(0)
399function IGCM_SmoothModulo
400{
401  IGCM_debug_PushStack "IGCM_SmoothModulo"
402  typeset defVector ModValue
403
404  eval set +A defVector -- $( echo "${1}" | \
405    gawk -F ':' '{print ($1 == "" ? 1 : $1) " " (NF==3 ? ($2 == "" ? 1 : $2) : 1) " " (NF==3 ? ($3 == "" ? -1 : $3) : ($2 == "" ? -1 : $2))}' )
406
407  # Save Smooth Min and Max. Needed to call IGCM_sys_Get when appropriate
408  arr[1]=${defVector[0]}
409  arr[2]=${defVector[2]}
410
411  # Test limits :
412  # ${defVector[0]} <= ${2} <= ${defVector[2]}
413  #          or ${defVector[2]} == -1
414  if ( [ ${2} -ge ${defVector[0]} ] && ( [ ${2} -le ${defVector[2]} ] || [ ${defVector[2]} -lt 0 ] ) ) ; then
415    # Test modulo
416    ModValue=$( expr \( ${2} - ${defVector[0]} \) % ${defVector[1]} )
417    if [ ${ModValue} -eq 0 ] ;  then
418      arr[3]=true
419      echo ${arr[@]}
420      IGCM_debug_PopStack "IGCM_SmoothModulo"
421      return 1
422    else
423      arr[3]=false
424      echo ${arr[@]}
425      IGCM_debug_PopStack "IGCM_SmoothModulo"
426      return 0
427    fi
428  else
429    arr[3]=false
430    echo ${arr[@]}
431    IGCM_debug_PopStack "IGCM_SmoothModulo"
432    return 0
433  fi
434}
435
436#=======================================================================
437function IGCM_comp_GetInputSmoothFiles
438{
439  IGCM_debug_PushStack "IGCM_comp_GetInputSmoothFiles"
440
441  # Debug Print :
442  echo
443  IGCM_debug_Print 1 "IGCM_comp_GetInputSmoothFiles"
444  echo
445
446  typeset comp compname comptagname card ListFilesName FileName0 NbFiles j i i_ i__
447  typeset file_in_ file_in file_out_ file_out ret SmoothDef aux val
448
449  for comp in ${config_ListOfComponents[*]} ; do
450        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
451        if [ X${number_rundir} != X ] ; then 
452            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
453            cd RUNDIR_${number_rundir} ;
454           fi
455    # Define component
456    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
457    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
458
459    # Debug Print :
460    IGCM_debug_Print 3 "Smooth files ${compname}"
461
462    card=${SUBMIT_DIR}/COMP/${compname}.card
463
464    IGCM_card_DefineArrayFromOption ${card} SmoothFiles List
465    ListFilesName=${compname}_SmoothFiles_List
466    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
467
468    if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != X"Section" ] ) ; then
469      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
470
471      (( i=0 ))
472      until [ $i -ge $NbFiles ]; do
473        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
474        eval file_in=${file_in_}
475        (( i_ = i+1 ))
476        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
477        eval file_out=${file_out_}
478
479        # define CumulPeriod definition for this file
480        (( i__ = i+2 ))
481        eval SmoothDef=\${${ListFilesName}[$i__]}
482        IGCM_debug_Print 3 "  ${file_in} ${SmoothDef}"
483        aux=$( IGCM_SmoothModulo ${SmoothDef} ${CumulPeriod} )
484        j=1
485        for val in ${aux} ; do
486          [ ${j} -eq 1 ] && SmoothMin=${val}
487          [ ${j} -eq 2 ] && SmoothMax=${val}
488          [ ${j} -eq 3 ] && ret=${val}
489          (( j=j+1 ))
490        done
491        [ ${SmoothMax} -eq -1 ] && SmoothMax=${CumulPeriod}
492        if ( [ X${ret} = Xtrue ] || ( [ ${Period} -eq 1 ] && [ ${CumulPeriod} -ge ${SmoothMin} ] && [ ${CumulPeriod} -le ${SmoothMax} ] ) ) ; then
493
494          IGCM_sys_IsFileArchived ${file_in}
495          if [ $? = 0 ] ; then
496            IGCM_sys_Get ${file_in} ${file_out}
497            #IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
498          else
499            IGCM_sys_Cp ${file_in} ${file_out}
500          fi
501        fi
502        (( i=i+3 ))
503      done
504    fi
505    if [ X${number_rundir} != X ] ; then
506        cd $RUN_DIR 
507    fi
508  done
509
510  IGCM_debug_PopStack "IGCM_comp_GetInputSmoothFiles"
511}
512
513#=======================================================================
514function IGCM_comp_GetInputBoundaryFiles
515{
516  IGCM_debug_PushStack "IGCM_comp_GetInputBoundaryFiles"
517
518  # Debug Print :
519  echo
520  IGCM_debug_Print 1 "IGCM_comp_GetInputBoundaryFiles"
521  echo
522
523  typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_
524  typeset file_in_ file_in file_out_ file_out
525
526  if [ ${Period} = 1 ]; then
527    ListFixBoundary=" "
528  fi
529
530  for comp in ${config_ListOfComponents[*]} ; do
531        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
532        if [ X${number_rundir} != X ] ; then 
533            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
534            cd RUNDIR_${number_rundir} ;
535        fi
536
537    # Define component
538    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
539    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
540
541    # Debug Print :
542    IGCM_debug_Print 3 "Boundary files ${compname}"
543
544    card=${SUBMIT_DIR}/COMP/${compname}.card
545
546    IGCM_card_DefineArrayFromOption ${card} BoundaryFiles List
547    ListFilesName=${compname}_BoundaryFiles_List
548    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
549
550    if [ X${FileName0} != X${NULL_STR} ] ; then
551      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
552
553      (( i=0 ))
554      until [ $i -ge $NbFiles ]; do
555        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
556        eval file_in=${file_in_}
557        (( i_ = i+1 ))
558        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
559        eval file_out=${file_out_}
560
561        IGCM_sys_IsFileArchived ${file_in}
562        if [ $? = 0 ] ; then
563          IGCM_sys_Get ${file_in} ${file_out}
564          IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
565        else
566          IGCM_sys_Cp ${file_in} ${file_out}
567        fi
568
569        (( i=i+2 ))
570      done
571    fi
572
573    # Get non deleted files
574    if [ ${Period} = 1 ]; then
575
576      IGCM_card_DefineArrayFromOption ${card} BoundaryFiles ListNonDel
577      ListFilesName=${compname}_BoundaryFiles_ListNonDel
578      eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
579
580      if [ X${FileName0} != X${NULL_STR} ] ; then
581        eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
582
583        (( i=0 ))
584        until [ $i -ge $NbFiles ]; do
585          eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
586          eval file_in=${file_in_}
587          (( i_ = i+1 ))
588          eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
589          eval file_out=${file_out_}
590
591          IGCM_sys_IsFileArchived ${file_in}
592          if [ $? = 0 ] ; then
593            IGCM_sys_Get ${file_in} ${file_out}
594            #IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
595          else
596            IGCM_sys_Cp ${file_in} ${file_out}
597          fi
598
599          if [ X${file_out} != X. ] ; then
600            ListFixBoundary=${ListFixBoundary}" "${file_out}
601          else
602            ListFixBoundary=${ListFixBoundary}" "$( basename ${file_in} )
603          fi
604
605          (( i=i+2 ))
606        done
607      fi
608    fi
609    if [ X${number_rundir} != X ] ; then
610        cd $RUN_DIR 
611    fi
612  done
613
614  IGCM_debug_PopStack "IGCM_comp_GetInputBoundaryFiles"
615}
616
617#=======================================================================
618function IGCM_comp_DelFixeBoundaryFiles
619{
620  IGCM_debug_PushStack "IGCM_comp_DelFixeBoundaryFiles"
621
622  # Debug Print :
623  echo
624  IGCM_debug_Print 1 "IGCM_comp_DelFixeBoundaryFiles"
625  echo
626
627  ls -l ${ListFixBoundary}
628  rm -f ${ListFixBoundary}
629
630  IGCM_debug_PopStack "IGCM_comp_DelFixeBoundaryFiles"
631}
632
633#=======================================================================
634function IGCM_comp_GetInputParametersFiles
635{
636  IGCM_debug_PushStack "IGCM_comp_GetInputParametersFiles"
637
638  # Debug Print :
639  echo
640  IGCM_debug_Print 1 "IGCM_comp_GetInputParametersFiles"
641  echo
642
643  typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_ file_in file_out
644  for comp in ${config_ListOfComponents[*]} ; do
645        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
646        if [ X${number_rundir} != X ] ; then 
647            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
648            cd RUNDIR_${number_rundir} ;
649        fi
650    # Define component
651    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
652    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
653
654    # Initialize array hosting file_def and field_def files to check and/or modify
655    unset FieldDef
656    unset FileDef
657
658    # Debug Print :
659    IGCM_debug_Print 3 "Parameters ${compname}"
660
661    card=${SUBMIT_DIR}/COMP/${compname}.card
662
663    IGCM_card_DefineArrayFromOption ${card} ParametersFiles List
664    ListFilesName=${compname}_ParametersFiles_List
665    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
666
667    if [ X${FileName0} != X${NULL_STR} ] ; then
668      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
669
670      (( i=0 ))
671      until [ $i -ge $NbFiles ]; do
672        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
673        eval file_in=${file_in_}
674        (( i_ = i+1 ))
675        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
676        eval file_out=${file_out_}
677
678        IGCM_sys_Cp ${file_in} ${file_out}
679        IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
680        IGCM_comp_PrepareXiosFiles    ${file_in} ${file_out}
681
682        (( i=i+2 ))
683      done
684    fi
685    if [ X${number_rundir} != X ] ; then
686        cd $RUN_DIR 
687    fi
688
689  done
690
691  IGCM_debug_PopStack "IGCM_comp_GetInputParametersFiles"
692}
693
694#=======================================================================
695function IGCM_comp_GetInputRestartFiles
696{
697  IGCM_debug_PushStack "IGCM_comp_GetInputRestartFiles"
698
699  # Debug Print :
700  echo
701  IGCM_debug_Print 1 "IGCM_comp_GetInputRestartFiles"
702  echo
703
704  typeset Date_tmp Date_r Path_r do_start CompOldName Path_OUT Path_BUF
705  typeset Buffered Archived Tared PotentialTarFile IsMatching TarFileFound
706  typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_
707  typeset file_in file_out file_in_ file_out_ file_in_Name
708  typeset -Z4 j4
709  #BASH declare j4
710
711  IsMatching=""
712
713  for comp in ${config_ListOfComponents[*]} ; do
714        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
715        if [ X${number_rundir} != X ] ; then 
716            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
717            cd RUNDIR_${number_rundir} ;
718        fi
719 
720    # Define component
721    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
722    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
723    #
724    card=${SUBMIT_DIR}/COMP/${compname}.card
725    #
726    IGCM_card_DefineArrayFromOption ${card} RestartFiles List
727    ListFilesName=${compname}_RestartFiles_List
728    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
729
730    # Debug Print :
731    IGCM_debug_Print 3 "restart ${compname}"
732
733    if ( ${FirstInitialize} ) ; then
734
735      if ( [ "${config_Restarts_OverRule}" = "y" ] || [ "${config_Restarts_OverRule}" = "Y" ] ) ; then
736        # Adhoc exception for CMIP6 : exclude XIOS from the restart overrule mechanism.
737        if ( [ X${comp} = XIOS ] ) && ( [ X"$( echo ${config_UserChoices_ExpType} | grep CMIP6 )" != "X" ] || [ X${config_Post_dr2xmlIPSL} = XTRUE ] ) ; then
738          CompOldName=IOS
739          config_IOS_Restart="n"
740          do_start="n"
741          config_IOS_RestartDate=-1
742          config_IOS_RestartJobName=${NULL_STR}
743          config_IOS_RestartPath=${NULL_STR}
744        else
745          eval config_${comp}_Restart="y"
746          eval config_${comp}_RestartDate=${config_Restarts_RestartDate}
747          eval config_${comp}_RestartJobName=${config_Restarts_RestartJobName}
748          eval config_${comp}_RestartPath=${config_Restarts_RestartPath}
749          eval do_start=\${config_${comp}_Restart} > /dev/null 2>&1
750          eval CompOldName=${comp}
751        fi
752      else
753        # Read component Restarts parameters
754        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} Restart
755        eval do_start=\${config_${comp}_Restart} > /dev/null 2>&1
756
757        if [ "${do_start}" = "y" ] ; then
758          IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} RestartDate
759          IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} RestartJobName
760          IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} RestartPath
761        else
762          eval config_${comp}_RestartDate=-1
763          eval config_${comp}_RestartJobName=${NULL_STR}
764          eval config_${comp}_RestartPath=${NULL_STR}
765        fi
766
767        eval CompOldName=\${config_${comp}_OldName}
768        if [ X${CompOldName} = X ] ; then
769          eval CompOldName=${comp}
770        fi
771
772        # Reinitialize IsMatching to allow searching for a different tar file for each component.
773        IsMatching=""
774        TarFileFound=""
775      fi
776
777      if [ "${do_start}" = "y" ] ; then
778
779        # Restore Restarts files
780        #-----------------------
781        if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != XNONE ] ) ; then
782          eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
783
784          (( i=1 ))
785          until [ $i -gt $NbFiles ]; do
786            eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
787            eval file_in=${file_in_}
788            (( i_ = i+1 ))
789            eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
790            eval file_out=${file_out_}
791
792            (( i_ = i+2 ))
793            eval is_opt=\${${ListFilesName}[$i_]} > /dev/null 2>&1
794            if [ X${is_opt} = XOPTIONAL ] ; then
795                echo ${file_in} is optional
796                (( i=i+4 ))
797            else
798                is_opt="MANDATORY"
799                echo ${file_in} is mandatory
800                (( i=i+3 ))
801            fi
802            IGCM_debug_Print 3 "Now search for restart file ${file_in} which is ${is_opt} (in section do_start=y)"
803
804            eval Date_tmp=\${config_${comp}_RestartDate}
805            Date_r=$( IGCM_date_ConvertFormatToGregorian ${Date_tmp} )
806            # will be re-use
807            eval RestartPath=\${config_${comp}_RestartPath}
808            eval RestartJobName=\${config_${comp}_RestartJobName}
809            #
810            Path_r=${RestartPath}/${RestartJobName}/${CompOldName}/Restart
811            file_in_Name=${RestartJobName}_${Date_r}_${file_in}
812
813            extension_in=$( echo ${file_in_Name##*.} )
814            extension_out=$( echo ${file_out##*.} )
815
816            generic_restart_file_name_in=$( basename ${file_in_Name} .${extension_in} )
817            generic_restart_file_name_out=$( basename ${file_out} .${extension_out} )
818
819            Path_OUT=${Path_r}/${generic_restart_file_name_in}
820
821            if [ $( IGCM_sys_TestFileBuffer ${Path_OUT}*.${extension_in} ; echo $? ) = 0 ] ; then
822              IGCM_debug_Print 3 "Buffered restart ${Path_OUT}*.${extension_in} "
823              Buffered=true
824              Archived=false
825              Tared=false
826              nb_restart_file=$(IGCM_sys_CountFileBuffer ${Path_OUT}_????.${extension_in})
827            elif [ $( IGCM_sys_TestFileArchive ${Path_OUT}*.${extension_in} ; echo $? ) = 0 ] ; then
828              IGCM_debug_Print 3 "Archived restart ${Path_OUT}*.${extension_in}"
829              Buffered=false
830              Archived=true
831              Tared=false
832              nb_restart_file=$(IGCM_sys_CountFileArchive ${Path_OUT}_????.${extension_in})
833            else
834              IGCM_debug_Print 3 "No restart file in the buffer nor in the archive directory"
835              IGCM_debug_Print 3 "${Path_OUT}*.${extension_in} do not exist"
836              IGCM_debug_Print 3 "Restart files will now be searched for in : ${RestartPath}/${RestartJobName}/RESTART"
837              Buffered=false
838              Archived=false
839              Tared=true
840              # Look after the tar file we want if we did not found it already
841              if [ X${IsMatching} = X ] ; then
842                IGCM_sys_TestDirArchive ${RestartPath}/${RestartJobName}/RESTART
843                if [ $? ] ; then
844                  for PotentialTarFile in $( IGCM_sys_RshArchive "find ${RestartPath}/${RestartJobName}/RESTART -name "${RestartJobName}_*restart*.tar" -print" ) ; do
845                    IsMatching=$( echo ${PotentialTarFile##*/} | \
846                      sed "s:_restart::" | \
847                      sed "s:^${RestartJobName}_::" | \
848                      sed "s:\.tar$::" | \
849                      gawk -F_ -v restartdate=${Date_r} \
850                      '{if (($1 <= restartdate) && ($2 >= restartdate)) {print $1"_"$2}}' )
851                    if [ ! X${IsMatching} = X ] ; then
852                      TarFileFound=${PotentialTarFile}
853                      break
854                    fi
855                  done
856                fi
857                # Stop here if nothing has been found
858                if [ X${TarFileFound} = X ] ; then
859                  IGCM_debug_Print 3 "Restart files were not found!"
860                  IGCM_debug_Print 3 "Restart files have been searched for in buffer and archive directory."
861                  IGCM_debug_Print 3 "They have been searched for in packed and unpacked format."
862                  if [ ${is_opt} = 'OPTIONAL' ] ; then
863                    IGCM_debug_Print 3 "The restart file is optional. Now continue."
864                    nb_restart_file=0
865                    Tared=false
866                  else
867                    IGCM_debug_Exit "Please double check restart settings in config.card"
868                    IGCM_debug_Verif_Exit
869                  fi
870                fi     
871              fi
872
873              if [ X${Tared} = Xtrue ] ; then
874                # The tar file exist but not sure all files are inside
875                IGCM_sys_PrepareTaredRestart ${TarFileFound}
876                TarFileLocation=$( basename ${TarFileFound} )
877                nb_files=$( tar tvf ${TarFileLocation} | grep ${comp}_${generic_restart_file_name_in}*.${extension_in} | wc -l )
878                if [ ${nb_files} -gt 0 ] ; then
879                    IGCM_debug_Print 1 "tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}"
880                    tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}
881                    nb_restart_file=$( IGCM_sys_CountFileBuffer ${comp}_${generic_restart_file_name_in}_????.${extension_in} )
882                else
883                    # No file was found
884                    if [ ${is_opt} = 'OPTIONAL' ] ; then
885                        IGCM_debug_Print 3 "The restart file is not found in the tar file but it is optional. Now continue."
886                        nb_restart_file=0
887                        Tared=false
888                    else
889                        IGCM_debug_Print 3 "The restart file was not found in the tar file but it is mandatory."
890                        IGCM_debug_Exit "Please double check restart settings in config.card"
891                        IGCM_debug_Verif_Exit
892                    fi
893                fi
894              fi
895            fi
896           
897            if [ ${nb_restart_file} -gt 1 ] ; then
898              j=0                                      # BASH LINE NOT NEEDED
899              # BASH for j4 in in $( eval echo {0000..$(( nb_restart_file - 1 ))} ) ; do
900              until [ $j -ge ${nb_restart_file} ]; do  # BASH LINE NOT NEEDED
901                j4=${j}                                # BASH LINE NOT NEEDED
902                if [ X${Buffered} = Xtrue ] ; then
903                  IGCM_sys_GetBuffer ${Path_OUT}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
904                elif [ X${Archived} = Xtrue ] ; then
905                  IGCM_sys_Get ${Path_OUT}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
906                elif [ X${Tared} = Xtrue ] ; then
907                  IGCM_sys_Mv ${comp}_${generic_restart_file_name_in}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
908                fi
909                (( j=j+1 ))                            #BASH LINE NOT NEEDED
910              done
911
912              # OCE SPECIFIC TO REBUILD RESTART WHEN NUMBER OF RESTART FILES DONT MATCH MPI PROCESS
913              if [ X${OCE_PROC_MPI} != X ] ; then
914                if [ ${OCE_PROC_MPI} -ne ${nb_restart_file} ] ; then
915                  IGCM_sys_rebuild_nemo ${generic_restart_file_name_out} ${nb_restart_file} ${extension_out} ${generic_restart_file_name_out}_????.${extension_out}
916                  IGCM_sys_Rm ${generic_restart_file_name_out}_????.${extension_out}
917                fi
918              fi
919            else
920              if [ X${Buffered} = Xtrue ] ; then
921                IGCM_sys_GetBuffer ${Path_r}/${file_in_Name} ${file_out}
922              elif [ X${Archived} = Xtrue ] ; then
923                IGCM_sys_Get ${Path_r}/${file_in_Name} ${file_out}
924              elif [ X${Tared} = Xtrue ] ; then
925                if [ -f ${comp}_${file_in_Name} ] ; then
926                   # The file exixt
927                   IGCM_sys_Mv ${comp}_${file_in_Name} ${file_out}
928                elif [ ${is_opt} = 'OPTIONAL' ] ; then
929                   IGCM_debug_Print 3 "The restart file is not found in the tar file but it is optional. Now continue."
930                else
931                   IGCM_debug_Print 3 "The restart file was not found in the tar file but it is mandatory."
932                   IGCM_debug_Exit "Please double check restart settings in config.card"
933                   IGCM_debug_Verif_Exit
934                fi
935              fi
936            fi
937          done
938        else
939          if [ X${FileName0} != XNONE ] ; then
940            IGCM_debug_Exit "IGCM_comp_GetInputRestartFiles : No file in list for ${compname}."
941          else
942            IGCM_debug_Print 1 "IGCM_comp_GetInputRestartFiles : NONE specified in Restart List ${compname}."
943          fi
944        fi
945      fi
946    elif ( [ ${Period} -eq 1 ] && [ ${DRYRUN} -eq 0 ] ) ; then
947      # if not FirstInitialize and first loop of this job
948
949      # Restore Restarts files
950      #-----------------------
951      if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != XNONE ] ) ; then
952        eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
953
954        (( i=1 ))
955        until [ $i -gt $NbFiles ]; do
956          eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
957          eval file_in=${file_in_}
958
959          (( i_ = i+1 ))
960          eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
961          eval file_out=${file_out_}
962
963          (( i_ = i+2 ))
964          eval is_opt=\${${ListFilesName}[$i_]} > /dev/null 2>&1
965          if [ X${is_opt} = XOPTIONAL ] ; then
966              # Read a forth argument
967              echo ${file_in} is optional
968              (( i=i+4 ))
969          else
970              is_opt="MANDATORY"
971              echo ${file_in} is mandatory
972              (( i=i+3 ))
973          fi
974
975          IGCM_debug_Print 3 "Now search for restart file ${file_in} which is ${is_opt} (in section do_start=n)"
976
977          file_in_Name=${config_UserChoices_JobName}_${LastPeriodDateEnd}_${file_in}
978
979          extension_in=$( echo ${file_in_Name##*.} )
980          extension_out=$( echo ${file_out##*.} )
981
982          generic_restart_file_name_in=$( basename ${file_in_Name} .${extension_in} )
983          generic_restart_file_name_out=$( basename ${file_out} .${extension_out} )
984
985          eval Path_BUF=\${R_BUF_${comp}_R}/${generic_restart_file_name_in}
986          eval Path_OUT=\${R_OUT_${comp}_R}/${generic_restart_file_name_in}
987
988          if [ $( IGCM_sys_TestFileBuffer ${Path_BUF}*.${extension_in} ; echo $? ) = 0 ] ; then
989            IGCM_debug_Print 3 "Buffered restart ${Path_BUF}*.${extension_in}"
990            Buffered=true
991            Archived=false
992            Tared=false
993            nb_restart_file=$(IGCM_sys_CountFileBuffer ${Path_BUF}_????.${extension_in})
994          elif [ $( IGCM_sys_TestFileArchive ${Path_OUT}*.${extension_in} ; echo $? ) = 0 ] ; then
995            IGCM_debug_Print 3 "Archived restart ${Path_OUT}*.${extension_in}"
996            Buffered=false
997            Archived=true
998            Tared=false
999            nb_restart_file=$(IGCM_sys_CountFileArchive ${Path_OUT}_????.${extension_in})
1000          else
1001            IGCM_debug_Print 3 "No restart file in the buffer nor in the archive directory"
1002            IGCM_debug_Print 3 "Restart files will now be searched for in : ${R_SAVE}/RESTART"
1003            Buffered=false
1004            Archived=false
1005            Tared=true
1006            # Look after the tar file we want if we did not found it already
1007            if [ X${IsMatching} = X ] ; then
1008              for PotentialTarFile in $( IGCM_sys_RshArchive "find ${R_SAVE}/RESTART -name "${config_UserChoices_JobName}_*_restart.tar" -print" ) ; do
1009                IsMatching=$( echo ${PotentialTarFile##*/} | sed "s:^${config_UserChoices_JobName}_::" | sed "s:\.restart\.tar$::" | gawk -F_ -v restartdate=${LastPeriodDateEnd} '{if (($1 < restartdate) && ($2 >= restartdate)) {print $1"_"$2}}' )
1010                if [ ! X${IsMatching} = X ] ; then
1011                  TarFileFound=${PotentialTarFile}
1012                  break
1013                fi
1014              done
1015            fi
1016            # Stop here if nothing has been found
1017            if [ X${TarFileFound} = X ] ; then
1018              IGCM_debug_Print 3 "Restart files were not found!"
1019              IGCM_debug_Print 3 "Restart files have been searched for in buffer and archive directory."
1020              IGCM_debug_Print 3 "They have been searched for in packed and unpacked format."
1021              if [ ${is_opt} = 'OPTIONAL' ] ; then
1022                 IGCM_debug_Print 3 "The restart file is optional. Now continue."
1023                 nb_restart_file=0
1024                 Tared=false
1025              else
1026                 IGCM_debug_Print 3 "The restart file is mandatory."
1027                 IGCM_debug_Exit "Please double check restart settings in config.card"
1028                 IGCM_debug_Verif_Exit
1029              fi
1030            else
1031              # The tar file exist but not sure all files are inside
1032              IGCM_sys_PrepareTaredRestart ${TarFileFound}
1033              TarFileLocation=$( basename ${TarFileFound} )
1034              nb_files=$( tar tvf ${TarFileLocation} | grep ${comp}_${generic_restart_file_name_in}*.${extension_in} | wc -l )
1035              if [ ${nb_files} -gt 0 ] ; then
1036                IGCM_debug_Print 1 "tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}"
1037                tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}
1038                nb_restart_file=$( IGCM_sys_CountFileBuffer ${comp}_${generic_restart_file_name_in}_????.${extension_in} )
1039              else
1040                # No file was found
1041                if [ ${is_opt} = 'OPTIONAL' ] ; then
1042                  IGCM_debug_Print 3 "The restart file is not found in the tar file but it is optional. Now continue."
1043                  nb_restart_file=0
1044                  Tared=false
1045                else
1046                  IGCM_debug_Print 3 "The restart file was not found in the tar file but it is mandatory."
1047                  IGCM_debug_Exit "Please double check restart settings in config.card"
1048                  IGCM_debug_Verif_Exit
1049                fi
1050              fi
1051
1052            fi
1053          fi
1054
1055          if [ ${nb_restart_file} -gt 1 ] ; then
1056            j=0                                     # BASH LINE NOT NEEDED
1057            #BASH for j4 in in $( eval echo {0000..$(( nb_restart_file - 1 ))} ) ; do
1058            until [ $j -ge ${nb_restart_file} ]; do # BASH LINE NOT NEEDED
1059              j4=${j}                               # BASH LINE NOT NEEDED
1060              if [ X${Buffered} = Xtrue ] ; then
1061                IGCM_sys_GetBuffer ${Path_BUF}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
1062              elif [ X${Archived} = Xtrue ] ; then
1063                IGCM_sys_Get ${Path_OUT}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
1064              elif [ X${Tared} = Xtrue ] ; then
1065                IGCM_sys_Mv ${comp}_${generic_restart_file_name_in}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
1066              fi
1067              (( j=j+1 ))                           # BASH LINE NOT NEEDED
1068            done
1069          else
1070            if [ X${Buffered} = Xtrue ] ; then
1071              eval IGCM_sys_GetBuffer \${R_BUF_${comp}_R}/${file_in_Name} ${file_out}
1072            elif [ X${Archived} = Xtrue ] ; then
1073              eval IGCM_sys_Get \${R_OUT_${comp}_R}/${file_in_Name} ${file_out}
1074            elif [ X${Tared} = Xtrue ] ; then
1075              IGCM_sys_Mv ${comp}_${file_in_Name} ${file_out}
1076            fi
1077          fi
1078        done
1079      else
1080        if [ X${FileName0} != XNONE ] ; then
1081          IGCM_debug_Exit "IGCM_comp_GetInputRestartFiles : No file in list for ${compname}."
1082        else
1083          IGCM_debug_Print 1 "IGCM_comp_GetInputRestartFiles : NONE specified in Restart List ${compname}."
1084        fi
1085      fi
1086    fi
1087    if [ X${number_rundir} != X ] ; then
1088        cd $RUN_DIR 
1089    fi
1090  done
1091
1092  NbFiles=$( ls * 2> /dev/null | wc -l )
1093  if [ ${NbFiles} -gt 0 ] ; then
1094    IGCM_sys_Chmod u+rw *
1095  fi
1096
1097  IGCM_debug_PopStack "IGCM_comp_GetInputRestartFiles"
1098}
1099
1100#=======================================================================
1101function IGCM_comp_GetInputBinaryFiles
1102{
1103  IGCM_debug_PushStack "IGCM_comp_GetInputBinaryFiles"
1104
1105  # Debug Print :
1106  echo
1107  IGCM_debug_Print 1 "IGCM_comp_GetInputBinaryFiles"
1108  echo
1109
1110  # Clean up previous pass
1111  [ -f ${RUN_DIR}/compiler.txt ] && rm -f ${RUN_DIR}/compiler.txt
1112
1113  typeset comp ExeNameIn ExeNameOut byPass
1114  typeset compilerNmbr compilerUnit compilerFull
1115  byPass=false
1116  for comp in ${config_ListOfComponents[*]} ; do
1117        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
1118        if [ X${number_rundir} != X ] ; then 
1119            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
1120            cd RUNDIR_${number_rundir} ;
1121        fi
1122
1123    # Define component
1124
1125    # Copy executable for this component
1126    eval ExeNameIn=\${config_Executable_${comp}[0]}
1127    eval ExeNameOut=\${config_Executable_${comp}[1]}
1128    # Test if missing executable and DRYRUN is set to 0 or 1, then stop!
1129    if [ ${DRYRUN} -le 1 ] && [ X${ExeNameIn} != X\"\" ] ; then
1130        # The executable is set in config.card and is different from ""
1131        # Use eval to expand ExeNameIn if variables are used in the name
1132        eval ExeNameIn=${ExeNameIn}
1133        if [ ! -f ${R_EXE}/${ExeNameIn} ] ; then
1134            IGCM_debug_Exit "IGCM_comp_GetInputBinaryFiles missing executable ${ExeNameIn}"
1135        fi
1136    fi
1137
1138    if [ ${Period} -eq 1 ] && [ -f ${R_EXE}/${ExeNameIn} ] ; then
1139      eval IGCM_sys_Cp ${R_EXE}/${ExeNameIn} ${ExeNameOut}
1140      if [ -f ${RUN_DIR}/${ExeNameOut} ] ; then
1141        eval IGCM_sys_Chmod +rx ${RUN_DIR}/${ExeNameOut}
1142      fi
1143    elif [ -f ${R_EXE}/${ExeNameIn} ] && [ ! -f ${RUN_DIR}/${ExeNameOut} ] ; then
1144      eval IGCM_sys_Cp ${R_EXE}/${ExeNameIn} ${ExeNameOut}
1145      if [ -f ${RUN_DIR}/${ExeNameOut} ] ; then
1146        eval IGCM_sys_Chmod +rx ${RUN_DIR}/${ExeNameOut}
1147      fi
1148    fi
1149
1150    if [ -f ${RUN_DIR}/${ExeNameOut} ] ; then
1151      # Get the compiler version
1152      compilerFull=$( strings ${RUN_DIR}/${ExeNameOut}  | grep "RTL Message Catalog" | gawk '{print $6}' | gawk -F. '{print $1}' )
1153      compilerVersion=$( strings ${RUN_DIR}/${ExeNameOut}  | grep "RTL Message Catalog" | gawk '{print $6}' | gawk -F. '{print $1}' )
1154      # Save it
1155      echo $compilerFull >> ${RUN_DIR}/compiler.txt
1156      [ X${compilerFull} = X ] && byPass=true
1157      IGCM_debug_Print 1 "${RUN_DIR}/${ExeNameOut} has been compiled with ${compilerFull}"
1158      IGCM_debug_Print 1 "Compiler is ${compilerVersion}"
1159    fi
1160    if [ X${number_rundir} != X ] ; then
1161        cd $RUN_DIR 
1162    fi 
1163
1164  done
1165
1166  compilerNmbr=$( cat ${RUN_DIR}/compiler.txt | wc -l )
1167  compilerUnit=$( cat ${RUN_DIR}/compiler.txt | sort | uniq -c )
1168
1169  if ( [ ${compilerNmbr} -ne ${compilerUnit} ] && [ ! X${byPass} = Xtrue  ] ); then
1170    IGCM_debug_Exit "Binaries has not been compiled with the same compiler version"
1171  fi
1172
1173
1174  IGCM_debug_PopStack "IGCM_comp_GetInputBinaryFiles"
1175}
1176
1177#=======================================================================
1178function IGCM_comp_PeriodStart
1179{
1180  IGCM_debug_PushStack "IGCM_comp_PeriodStart"
1181
1182  # Debug Print :
1183  echo
1184  IGCM_debug_Print 1 "IGCM_comp_PeriodStart"
1185  echo
1186
1187  typeset ExeNameIn ExeNameOut
1188  typeset comp compname comptagname
1189  for comp in ${config_ListOfComponents[*]} ; do
1190    # Define component
1191    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
1192    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
1193
1194    # Copy executable for this component
1195    eval ExeNameIn=\${config_Executable_${comp}[0]}
1196    eval ExeNameIn=${ExeNameIn}
1197    eval ExeNameOut=\${config_Executable_${comp}[1]}
1198
1199    # Debug Print
1200    IGCM_debug_Print 3 "PeriodStart ${compname} Driver Function (if any)."
1201    # UPDATE component
1202    ${comp}_PeriodStart 2> /dev/null
1203
1204  done
1205
1206  IGCM_debug_PopStack "IGCM_comp_PeriodStart"
1207}
1208
1209
1210
1211#=======================================================================
1212function IGCM_comp_modifyFile
1213{
1214#
1215# syntax:     IGCM_comp_modifyFile  filein  key  [value]
1216#
1217# For example : IGCM_comp_modifyFile metrics_template.py case_id \'SE_${YEARS}\'
1218#
1219# This function is used to replace a pattern in a file for a specific variable.
1220#
1221# Arguments:
1222# - filein : the file in run directory in which the variable should be set
1223# - key    : the variable to modify
1224# - value  : the value to set the key equal to
1225#
1226  IGCM_debug_PushStack "IGCM_comp_modifyFile"
1227
1228  typeset filein key value pattern
1229
1230  # Set local variables and test the arguments
1231  if [ $# = 3 ] ; then
1232    # Normal case with 3 arguments
1233    filein=$1 ; key=$2 ; value=$3
1234  else
1235    IGCM_debug_Exit "IGCM_comp_modifyFile: Bad number of arguments."
1236    IGCM_debug_PopStack "IGCM_comp_modifyFile"
1237    return
1238  fi
1239  IGCM_debug_Print 1 "Entering IGCM_comp_modifyFile with arguments: ${filein} ${key} ${value}"
1240
1241  # Test if the file exist
1242  if [ ! -f ${filein} ] ; then
1243    IGCM_debug_Exit "IGCM_comp_modifyFile: ${filein} does not exist."
1244    IGCM_debug_PopStack "IGCM_comp_modifyFile"
1245    return
1246  fi
1247
1248  # Read the line with key in the file without the comments
1249  pattern=$( grep "^ *${key} *=" ${filein} | sed -e "s% *\!.*%%" )
1250
1251  # Verify the existance of the pattern
1252  if [ X"${pattern}" = X ] ; then
1253    # Variable key is not set in filein, stop.
1254    IGCM_debug_Exit "IGCM_comp_modifyFile : Variable ${key} is not set in correct file. It should be set in ${filein}."
1255    IGCM_debug_PopStack "IGCM_comp_modifyFile"
1256    return
1257  fi
1258
1259  # Now change key in filein
1260  #sed -e "s:${pattern}:${key}=${value}:" ${filein} > ${filein}.tmp
1261  sed -e "s:^ *${key} *=.*:${key}=${value}:" ${filein} > ${filein}.tmp
1262  IGCM_debug_Print 1 "IGCM_comp_modifyFile: In ${filein} set ${key}=${value}"
1263  \mv ${filein}.tmp ${filein}
1264 
1265  IGCM_debug_PopStack "IGCM_comp_modifyFile"
1266}
1267
1268#=======================================================================
1269function IGCM_comp_modifyDefFile
1270{
1271#
1272# syntax:     IGCM_comp_modifyDefFile  type  filein  key  [value]
1273#
1274# For example : IGCM_comp_modifyDefFile blocker run.def day_step 1200
1275#
1276# This function is used to modify a parameter file for a specific variable.
1277# The file must be a ".def" file, i.e. with IOIPSL parameter file syntax.
1278# This function can be used in the comp.driver files for the components.
1279#
1280# Arguments:
1281# - type   : first argument must be blocker, nonblocker or force
1282#            For "blocker" case, the variable must be attributed the keyworld AUTO
1283#            otherwise this function will exit.
1284#            For "nonblocker" case, the user can remove or modify the variable. For
1285#            this case, as long as AUTO is not set, no modification will be done.
1286#            For "force" case, the variable will be modified even if it is not set to AUTO
1287# - filein : the file in run directory of .def type in which the variable should be set
1288# - key    : the variable to modify
1289# - value  : the value to set the key equal to, optional. If value is not set or if
1290#            value=DEFAULT, then a default value must be given in filein using syntax :
1291#            key= AUTO : DEFAULT=def_value
1292#
1293  IGCM_debug_PushStack "IGCM_comp_modifyDefFile"
1294
1295  typeset type filein key value
1296  typeset filelist nb_occ modify
1297
1298  # Set local variables and test the arguments
1299  if [ $# = 4 ] ; then
1300    # Normal case with 4 arguments
1301    type=$1 ; filein=$2 ; key=$3 ; value=$4
1302  elif [ $# = 3 ] ; then
1303    # Normal case with 3 arguments
1304    type=$1 ; filein=$2 ;       key=$3; value="DEFAULT"
1305  else
1306    IGCM_debug_Exit "IGCM_comp_modifyDefFile: Bad number of arguments."
1307    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1308    return
1309  fi
1310  IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: ${type} ${filein} ${key} ${value}"
1311
1312  # Test if first argument is correct
1313  if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then
1314    IGCM_debug_Exit "IGCM_comp_modifyDefFile: Error in first argument must be blocker, nonblocker or force"
1315    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1316    return
1317  fi
1318
1319  # Test if the file exist.
1320  # Exit with error if the file does not exist for the case blocker or force.
1321  # Only return for the case nonblocker.
1322  if [ ! -f ${filein} ] ; then
1323    if [ ${type} = blocker ] || [ ${type} = force ] ; then
1324      IGCM_debug_Exit "IGCM_comp_modifyDefFile: ${filein} does not exist."
1325    else
1326      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${filein} does not exist. Nothing will be done for this file."
1327    fi
1328    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1329    return
1330  fi
1331
1332  # Define list of files to test using all files with suffix .def (except used*.def)
1333  filelist=$( ls *def | grep -v used )
1334
1335  # Count number of occurances for the key in all files
1336  nb_occ=$( grep -w ${key} ${filelist} | grep -v "#"  | wc -l )
1337
1338  # Test if key is set several times
1339  if [ ${nb_occ} -gt 1 ] ; then
1340    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in ${filein}: Variable=${key} is set ${nb_occ} times"
1341    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1342    return
1343  fi
1344
1345  # Treatement according to different cases
1346  if [ ${nb_occ} -eq 0 ] && [ ${type} = blocker ] ; then
1347    # Stop if the key is never set and the function is blocker
1348    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in ${filein}: Variable=${key} has been removed but this function is blocker. "
1349    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore ${filein} for variable ${key}."
1350  elif [ ${nb_occ} -eq 0 ] && [ ${type} = nonblocker ] ; then
1351    # The key is not set but it is a nonblocker call so nothing is done.
1352    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key} is not set in ${filein}. This is a nonblocker call so nothing is done."
1353    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for ${key} from the model will be used."
1354    modify=no
1355  elif [ $( grep ${key} ${filein} | grep -v "\#"  |wc -l ) = 0 ] ; then
1356    # Variable key is not set in filein, stop.
1357    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable ${key} is not set in correct file. It should be set in ${filein}."
1358  fi
1359
1360  # Check if AUTO is set in the filein on the same line as the key variable
1361  if [ $( grep -w ${key} ${filein} | grep -v "\#" | grep AUTO | wc -l ) = 1 ] ; then
1362    # Modification will be done for all cases
1363    modify=yes
1364  else
1365    # The variable was not set to AUTO
1366    if [ ${type} = blocker ] ; then
1367      # Exit because this is a blocker call
1368      IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable ${key} cannot be modified. It should be set to AUTO."
1369      IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1370      return
1371    elif [ ${type} = nonblocker ] ; then
1372      # Do nothing. Suppose that the user did set the variable correct
1373      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key} is set by the user. Nothing done."
1374      modify=no
1375    elif [ ${type} = force ] ; then
1376      # Force modification
1377      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile : Variabl=${key} was not set to AUTO. Modification will be forced."
1378      modify=yes
1379    fi
1380  fi
1381
1382  # Do the modifcation now
1383  if [ ${modify} = yes ] ; then
1384
1385    # For option DEFAULT, read default value from file.
1386    if [ X"${value}" = XDEFAULT ] || [ X"${value}" = X ] ; then
1387      # Case to set DEFAULT value
1388      # Read default value from filein
1389      value=$( grep -w ${key} ${filein} | grep -v "\#" | awk  -F"DEFAULT *=" '{print $2}')
1390
1391      if [ X"${value}" = X ] ; then
1392        IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable ${key} needs a DEFAULT value in ${filein}."
1393        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in ${filein} should be:"
1394        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key}=_AUTO_:DEFAULT=def_value"
1395        IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1396        return
1397      fi
1398    fi
1399
1400    # Now change key in filein
1401    sed -e "s/^${key}\ *=.*/${key}= ${value}/" ${filein} > ${filein}.tmp
1402    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In ${filein} set ${key}=${value}"
1403    \mv ${filein}.tmp ${filein}
1404  fi
1405  IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1406}
1407
1408#=======================================================================
1409function IGCM_comp_modifyNamelist
1410{
1411#
1412# syntax:     IGCM_comp_modifyNamelist  type  filein  key  [value]
1413#
1414# For example : IGCM_comp_modifyNamelist blocker run.def day_step 1200
1415#
1416# This function is used to modify a parameter file for a specific variable.
1417# The file must be a "namelist" file, i.e. with fortran namelist syntax.
1418# This function can be used in the comp.driver files for the components.
1419#
1420# Arguments:
1421# - type   : first argument must be blocker, nonblocker or force
1422#            For "blocker" case, the variable must be attributed the keyworld AUTO
1423#            otherwise this function will exit.
1424#            For "nonblocker" case, the user can remove or modify the variable. For
1425#            this case, as long as AUTO is not set, no modification will be done.
1426#            For "force" case, the variable will be modified even if it is not set to AUTO
1427# - filein : the file in run directory of .def type in which the variable should be set
1428# - key    : the variable to modify
1429# - value  : the value to set the key equal to, optional. If value is not set or if
1430#            value=DEFAULT, then a default value must be given in filein using syntax :
1431#            key= AUTO : DEFAULT=def_value
1432#
1433  IGCM_debug_PushStack "IGCM_comp_modifyNamelist"
1434
1435  typeset type filein key value pattern modify
1436
1437  # Set local variables and test the arguments
1438  if [ $# = 4 ] ; then
1439    # Normal case with 4 arguments
1440    type=$1 ; filein=$2 ; key=$3 ; value=$4
1441  elif [ $# = 3 ] ; then
1442    # Normal case with 3 arguments
1443    type=$1 ; filein=$2 ;       key=$3; value="DEFAULT"
1444  else
1445    IGCM_debug_Exit "IGCM_comp_modifyNamelist: Bad number of arguments."
1446    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1447    return
1448  fi
1449  IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: ${type} ${filein} ${key} ${value}"
1450
1451  # Test if first argument is correct
1452  if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then
1453    IGCM_debug_Exit "IGCM_comp_modifyNamelist: Error in first argument must be blocker, nonblocker or force"
1454    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1455    return
1456  fi
1457
1458  # Test if the file exist.
1459  # Exit with error if the file does not exist for the case blocker or force.
1460  # Only return for the case nonblocker.
1461  if [ ! -f ${filein} ] ; then
1462    if [ ${type} = blocker ] || [ ${type} = force ] ; then
1463      IGCM_debug_Exit "IGCM_comp_modifyNamelist: ${filein} does not exist."
1464    else
1465      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${filein} does not exist. Nothing will be done for this file."
1466    fi
1467    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1468    return
1469  fi
1470
1471  # Read the line with key in the file without the comments
1472  pattern=$( grep "^ *${key} *=" ${filein} | sed -e "s% *\!.*%%" )
1473
1474  # Verify the existance of the pattern
1475  if [ X"$pattern" = X ] ; then
1476    # Variable key is not set in filein, stop.
1477    IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable ${key} is not set in correct file. It should be set in ${filein}."
1478    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1479    return
1480  fi
1481
1482  # Check if the variable is set to AUTO in the filein
1483  if [ $( echo $pattern | grep AUTO | wc -l ) = 1 ] ; then
1484    # Modification will be done for all cases
1485    modify=yes
1486  else
1487    # The variable was not set to AUTO
1488    if [ ${type} = blocker ] ; then
1489      # Exit because this is a blocker call
1490      IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable ${key} cannot be modified. It should be set to AUTO."
1491      IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1492      return
1493    elif [ ${type} = nonblocker ] ; then
1494      # Do nothing. Suppose that the user did set the variable correct
1495      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${key} is set by the user. Nothing done."
1496      modify=no
1497    elif [ ${type} = force ] ; then
1498      # Force modification
1499      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=${key} was not set to AUTO. Modification will be forced."
1500      modify=yes
1501    fi
1502  fi
1503
1504  # Do the modifcation now
1505  if [ ${modify} = yes ] ; then
1506
1507    # For option DEFAULT, read default value from file.
1508    if [ X"${value}" = XDEFAULT ] || [ X"${value}" = X ] ; then
1509      # Case to set DEFAULT value
1510      # Read default value from filein
1511      value=$( echo $pattern | awk  -F"DEFAULT *=" '{print $2}')
1512
1513      if [ X"${value}" = X ] ; then
1514        IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable ${key} needs a DEFAULT value in ${filein}."
1515        IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in ${filein} should be:"
1516        IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${key}=_AUTO_:DEFAULT=def_value"
1517        IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1518        return
1519      fi
1520    fi
1521
1522    # Now change key in filein
1523    sed -e "s/${pattern}/       ${key}=${value}/" ${filein} > ${filein}.tmp
1524    IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In ${filein} set ${key}=${value}"
1525    \mv ${filein}.tmp ${filein}
1526  fi
1527  IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1528}
1529
1530#=======================================================================
1531function IGCM_comp_modifyXmlFile
1532{
1533#
1534# syntax:     IGCM_comp_modifyXmlFile  type  filein  keyid  keyattrib  value
1535#
1536# For example : IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 enabled .TRUE.
1537#          or   IGCM_comp_modifyXmlFile blocker iodef.xml using_server NONE false
1538#
1539# This function is used to modify the value for a specific attribute and variable id.
1540# The file must be a valid xml file.
1541# This function can be used in the comp.driver files for the components.
1542#
1543# Arguments:
1544# - type      : first argument must be blocker, nonblocker or force.
1545#               For "blocker" case, the variable must be attributed the keyworld AUTO
1546#               otherwise this function will exit.
1547#               For "nonblocker" case, the user can remove or modify the variable. For
1548#               this case, as long as AUTO is not set, no modification will be done.
1549#               For "force" case, the variable will be modified even if it is not set to AUTO
1550# - filein    : the file in run directory of .xml type in which the variable should be set
1551# - keyid     : the variable to modify
1552# - keyattrib : the attribute name to modify. If NONE, then the variable itself will be modified
1553# - value     : the value to set in the filein
1554#
1555  IGCM_debug_PushStack "IGCM_comp_modifyXmlFile"
1556
1557  typeset type filein keyid keyattrib value modify
1558
1559  # Set local variables and test the arguments
1560  if [ $# = 5 ] ; then
1561    # Normal case with 4 arguments
1562    type=$1 ; filein=$2 ; keyid=$3 ; keyattrib=$4 ; value=$5
1563  else
1564    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Bad number of arguments."
1565    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1566    return
1567  fi
1568  IGCM_debug_Print 1 "Entering IGCM_comp_modifyXmlFile with arguments: type=${type} file=${filein}, id=${keyid} attribute=${keyattrib}, value=${value}"
1569
1570  # Test if first argument is correct
1571  if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then
1572    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Error in first argument must be blocker, nonblocker or force"
1573    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1574    return
1575  fi
1576
1577  # Test if the file exist.
1578  # Exit with error if the file does not exist for the case blocker or force.
1579  # Only return for the case nonblocker.
1580  if [ ! -f ${filein} ] ; then
1581    if [ ${type} = blocker ] || [ ${type} = force ] ; then
1582      IGCM_debug_Exit "IGCM_comp_modifyXmlFile: ${filein} does not exist."
1583    else
1584      IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile: ${filein} does not exist. Nothing will be done for this file."
1585    fi
1586    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1587    return
1588  fi
1589
1590  # Test if keyid is set in filein. If not exit for case all cases(blocker, force) except nonblocker.
1591  if [ $( grep -w ${keyid} ${filein} | wc -l ) = 0 ] ; then
1592      if [ ${type} = nonblocker ] ; then
1593          # This is a nonblocker case, print warning but do nothing else
1594          IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile: ${keyid} is not set in ${filein}. This is a nonblocker call so nothing is done."
1595      else
1596          # This is a blocker or force case : stop now
1597          IGCM_debug_Exit "IGCM_comp_modifyXmlFile : ${keyid} is not set in the file. Bad syntax of ${filein} file."
1598          IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1599          return
1600      fi
1601  fi
1602
1603  # Check if AUTO is set on the same line as keyid and keyattrib
1604  if [  $( grep -w ${keyid} ${filein} | grep AUTO | wc -l ) = 1 ] ; then
1605    # Modification will be done
1606    modify=yes
1607  else
1608    if [ ${type} = blocker ] ; then
1609      # Exit, the variable must be set to AUTO
1610      IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The ${keyattrib} for ${keyid} must be set to AUTO in ${filein}."
1611      IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1612      return
1613    elif [ ${type} = nonblocker ] ; then
1614      # Nothing will be done
1615      IGCM_debug_Print 1 "Nonblocker nothing is done for ${filein}, id=${keyid} and attribute ${keyattrib}"
1616      modify=no
1617    elif [ ${type} = force ] ; then
1618      # Force modification
1619      IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=${keyattrib} for id=${keyid} was not set to AUTO. Modification will be forced."
1620      modify=yes
1621    fi
1622  fi
1623
1624  # Do the modifcation now
1625  if [ ${modify} = yes ] ; then
1626    if [ ${keyattrib} = NONE ] ; then
1627      # Case to modify the variable itself
1628      IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting the variable=${value}"
1629      sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" ${filein} > ${filein}.tmp
1630    else
1631      # Check if keyattrib is set on the same line as keyid
1632      if [  $( grep -w ${keyid} ${filein} | grep ${keyattrib} | wc -l ) = 1 ] ; then
1633        # Case to modify the attribute value
1634        IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting attribute to ${keyattrib}=${value}"
1635        sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" ${filein} > ${filein}.tmp
1636      else
1637        # Case to add the attribute and its value
1638        IGCM_debug_Print 1 "Now add in ${filein} for id=${keyid} the attribute ${keyattrib} to the value ${value}"
1639        sed -e "/id=\"${keyid}\"/s/\/>/ ${keyattrib}=\"${value}\"\/>/" ${filein} > ${filein}.tmp
1640      fi
1641    fi
1642    \mv ${filein}.tmp ${filein}
1643  fi
1644  IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1645}
1646
1647#=======================================================================
1648function IGCM_comp_Update
1649{
1650  IGCM_debug_PushStack "IGCM_comp_Update"
1651
1652  # Debug Print :
1653  echo
1654  IGCM_debug_Print 1 "IGCM_comp_Update"
1655  echo
1656
1657  typeset comp compname comptagname
1658  for comp in ${config_ListOfComponents[*]} ; do
1659        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
1660        if [ X${number_rundir} != X ] ; then 
1661            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
1662            cd RUNDIR_${number_rundir} ;
1663        fi
1664    # Define component
1665    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
1666    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
1667
1668    # Debug Print
1669    IGCM_debug_Print 1 "Update ${compname} Parameter Files."
1670    # UPDATE component
1671    ${comp}_Update
1672
1673    # Read TimeSeries information from XML files, prepare TS directories and do modifications in timeseries_def_${compname}.xml
1674    if [ -f timeseries_def_${compname}.xml ]; then
1675      ${libIGCM}/libIGCM_post/xios_parser.py tsquery --file timeseries_def_${compname}.xml > ts.temp.${compname}.txt
1676      for line in $( cat ts.temp.${compname}.txt ); do
1677        output_freq=$( echo ${line} | awk -F "," '{print $1}' | awk -F "=" '{print $2}' )
1678        id=$(          echo ${line} | awk -F "," '{print $2}' | awk -F "=" '{print $2}' )
1679        case ${output_freq} in
1680        *Y|*y)
1681          eval IGCM_sys_Mkdir \${R_BUF_${comp}_A_Y}
1682          eval IGCM_comp_modifyXmlFile nonblocker timeseries_def_${compname}.xml ${id} ts_prefix \${R_BUF_${comp}_A_Y}/${config_UserChoices_JobName}
1683          ;;
1684        *MO|*mo)
1685          eval IGCM_sys_Mkdir \${R_BUF_${comp}_A_M}
1686          eval IGCM_comp_modifyXmlFile nonblocker timeseries_def_${compname}.xml ${id} ts_prefix \${R_BUF_${comp}_A_M}/${config_UserChoices_JobName}
1687          ;;
1688        *D|*d)
1689          eval IGCM_sys_Mkdir \${R_BUF_${comp}_A_D}
1690          eval IGCM_comp_modifyXmlFile nonblocker timeseries_def_${compname}.xml ${id} ts_prefix \${R_BUF_${comp}_A_D}/${config_UserChoices_JobName}
1691          ;;
1692        *S|*s)
1693          eval IGCM_sys_Mkdir \${R_BUF_${comp}_A_H}
1694          eval IGCM_comp_modifyXmlFile nonblocker timeseries_def_${compname}.xml ${id} ts_prefix \${R_BUF_${comp}_A_H}/${config_UserChoices_JobName}
1695          ;;
1696        esac
1697      done
1698    fi
1699
1700    # Read TimeSeries information from XML files, prepare CMIP6 TS directories and do modifications in dr2xml_${compname}.xml
1701    if [ -f dr2xml_${compname}.xml ]; then
1702      if ( [ X"$( echo ${config_UserChoices_ExpType} | grep CMIP6 )" != "X" ] || [ X${config_Post_dr2xmlIPSL} = XTRUE ] ) ; then
1703        eval IGCM_sys_Mkdir \${CMIP6_BUF_${comp}}
1704        eval CMIP6_DIR=\${CMIP6_BUF_${comp}}
1705        # Modify path from dr2xml_{compname}.xml
1706        if [ X${config_Post_dr2xmlIPSL} = XTRUE ]; then
1707          ${libIGCM}/libIGCM_post/xios_parser.py -v modifydr2xmlIPSL --newPath ${CMIP6_DIR} --splitLastDate ${EndYear_p1} --JobName ${config_UserChoices_JobName} --ModelName ${config_UserChoices_ModelName} --ModelVersion ${config_UserChoices_LongName} --ExperimentName ${config_UserChoices_ExperimentName} --file dr2xml_${compname}.xml
1708        else
1709          ${libIGCM}/libIGCM_post/xios_parser.py -v modifyPath --newPath ${CMIP6_DIR} --file dr2xml_${compname}.xml
1710        fi
1711        # Overwrite the original file
1712        IGCM_sys_Mv modified.dr2xml_${compname}.xml dr2xml_${compname}.xml
1713      fi
1714    fi
1715    if [ X${number_rundir} != X ] ; then
1716        cd $RUN_DIR 
1717    fi
1718  done
1719
1720  IGCM_debug_PopStack "IGCM_comp_Update"
1721}
1722
1723#=======================================================================
1724function IGCM_comp_Finalize
1725{
1726  IGCM_debug_PushStack "IGCM_comp_Finalize"
1727
1728  # Debug Print :
1729  echo
1730  IGCM_debug_Print 1 "IGCM_comp_Finalize"
1731  echo
1732
1733  typeset ListTextName TextName0
1734  typeset comp compname comptagname card ListFilesName FileName0 NbFiles SaveOnArchive
1735  typeset i i_ file_in file_in_ file_out file_out_ file_outin file_outin_ generic_file_name nb_rebuild_file
1736  typeset -Z4 j4 #BASH declare j4
1737  typeset list_file nlist_file
1738  typeset compactoutputs
1739
1740  # Initialize array hosting list of rebuilded files to copy
1741  unset rebuildedActionsList
1742
1743  # Text compacting options
1744  compactoutputs=false
1745  if [ X${JobType} != XRUN ] ; then
1746    compactoutputs=true
1747  elif [ X${config_UserChoices_CompactText} != Xn ] ; then
1748    compactoutputs=true
1749  fi
1750
1751  # Prepare headers for the shell dedicated to offline rebuild
1752  if [ X${AsynchronousRebuild} = Xtrue ] ; then
1753    [ ! -d ${RUN_DIR}/REBUILD_${PeriodDateBegin} ] && IGCM_sys_Mkdir ${RUN_DIR}/REBUILD_${PeriodDateBegin}
1754    if [ ${DRYRUN} -le 1 ] ; then
1755      echo "#!/bin/ksh                                        " >  ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1756      echo "function IGCM_FlushRebuild                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1757      echo "{                                                 " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1758      echo "IGCM_debug_PushStack \"IGCM_FlushRebuild\"        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1759      echo "echo                                              " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1760      echo "IGCM_debug_Print 1 \"IGCM_FlushRebuild\"          " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1761      echo "echo                                              " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1762      echo "export R_SAVE=${R_SAVE}                           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1763      echo "export R_BUFR=${R_BUFR}                           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1764      echo "export R_OUT_KSH=${R_OUT_KSH}                     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1765      echo "export R_BUF_KSH=${R_BUF_KSH}                     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1766      echo "export config_UserChoices_JobName=${config_UserChoices_JobName}     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1767      echo "export config_UserChoices_SpaceName=${config_UserChoices_SpaceName} " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1768    fi
1769  fi
1770for comp in ${config_ListOfComponents[*]} ; do
1771        number_rundir=$(echo ${comp} | sed 's/[^0-9]*//g')
1772        if [ X${number_rundir} != X ] ; then 
1773            [ ! -d RUNDIR_${number_rundir} ] && mkdir RUNDIR_${number_rundir}
1774            cd RUNDIR_${number_rundir} ;
1775        fi
1776
1777    # Define component
1778    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
1779    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
1780
1781    # Debug Print
1782    IGCM_debug_Print 1 "Finalize ${comp} : ${compname} component."
1783    # FINALIZE component
1784    ${comp}_Finalize
1785
1786    card=${SUBMIT_DIR}/COMP/${compname}.card
1787
1788    # Save Output Text files of models
1789    #---------------------------------
1790    IGCM_debug_Print 2 "Save Output Text files for ${comp} : ${compname} component."
1791    IGCM_card_DefineArrayFromOption ${card} OutputText List
1792    ListTextName=${compname}_OutputText_List
1793
1794    eval TextName0=\${${ListTextName}[0]} > /dev/null 2>&1
1795    if [ X${TextName0} != X${NULL_STR} ] ; then
1796      eval NbFiles=\${#${ListTextName}[@]} > /dev/null 2>&1
1797
1798      (( i=0 ))
1799      until [ $i -eq $NbFiles ]; do
1800        eval file_in=\${${ListTextName}[$i]} > /dev/null 2>&1
1801        eval file_out=${PREFIX}_${file_in}
1802
1803        (( i=i+1 ))
1804
1805        unset list_file
1806        #set +A list_file -- $( ls ${file_in}* | sort 2>/dev/null )
1807        # result for a a1 a10 a2 with file_in=a a a1 a2 a10
1808        set +A list_file -- $( [ -f ${file_in} ] && ls ${file_in} ; for i in $(ls ${file_in}* 2>/dev/null | sed "s/${file_in}//" | sort -n) ; do ls ${file_in}$i ; done )
1809        nlist_file=${#list_file[@]}
1810        if [ ${nlist_file} -gt 1 ] ; then
1811          if ( ${compactoutputs} ) ; then
1812            IGCM_debug_Print 2 "Parallelism of Text Output with ${nlist_file} files."
1813            IGCM_debug_Print 2 "Compact files in ${file_out} : " ${list_file[*]}
1814            echo ${list_file[*]} > ${file_out}
1815            echo "" >> ${file_out}
1816
1817            (( i_ = 0 ))
1818            for file in ${list_file[@]} ; do
1819              echo "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " >> ${file_out}
1820              echo "| " ${i_} " " ${file} >> ${file_out}
1821              echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " >> ${file_out}
1822              cat ${file} | sed "s/\(.*\)/${i_}\1/" >> ${file_out}
1823              echo "" >> ${file_out}
1824              eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file}" > /dev/null 2>&1
1825                  (( i_ = i_ + 1 ))
1826            done
1827            if ( ${ExecutionFail} ) ; then
1828              IGCM_sys_Cp ${file_out} ${SUBMIT_DIR}/Debug
1829            fi
1830
1831            if [ X${Pack} = Xtrue ] ; then
1832              eval IGCM_sys_PutBuffer_Out ${file_out} \${R_BUF_${comp}_D}/${file_out}
1833            else
1834              eval IGCM_sys_Put_Out ${file_out} \${R_OUT_${comp}_D}/${file_out}
1835            fi
1836
1837            eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file_out}" > /dev/null 2>&1
1838          else
1839            for file in ${list_file[@]} ; do
1840              if ( ${ExecutionFail} ) ; then
1841                IGCM_sys_Cp ${file} ${SUBMIT_DIR}/Debug/${PREFIX}_${file}
1842              fi
1843
1844              if [ X${Pack} = Xtrue ] ; then
1845                eval IGCM_sys_PutBuffer_Out ${file} \${R_BUF_${comp}_D}/${PREFIX}_${file}
1846              else
1847                eval IGCM_sys_Put_Out ${file} \${R_OUT_${comp}_D}/${PREFIX}_${file}
1848              fi
1849
1850              eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file}" > /dev/null 2>&1
1851            done
1852          fi
1853        else
1854          if ( [ -f ${file_in}_0000 ] || [ -f ${file_in}0 ] ) ; then
1855            eval IGCM_sys_Mv ${file_in}* ${file_in}
1856          fi
1857
1858          if ( ${ExecutionFail} ) ; then
1859            IGCM_sys_Cp ${file_in} ${SUBMIT_DIR}/Debug/${file_out}
1860          fi
1861
1862          if [ X${Pack} = Xtrue ] ; then
1863            eval IGCM_sys_PutBuffer_Out ${file_in} \${R_BUF_${comp}_D}/${file_out}
1864          else
1865            eval IGCM_sys_Put_Out ${file_in} \${R_OUT_${comp}_D}/${file_out}
1866          fi
1867          eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file_in}" > /dev/null 2>&1
1868        fi
1869      done
1870    fi
1871
1872    # Save Restarts files
1873    #--------------------
1874    IGCM_debug_Print 2 "Save Restart files for ${comp} : ${compname} component."
1875    IGCM_card_DefineArrayFromOption ${card} RestartFiles List
1876    ListFilesName=${compname}_RestartFiles_List
1877    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
1878   
1879    if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != XNONE ] ) ; then
1880      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
1881      (( i=0 ))
1882      until [ $i -ge $NbFiles ]; do
1883        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
1884        eval file_in=${file_in_}
1885
1886        (( i_ = i+1 ))
1887        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1888        eval file_out=${file_out_}
1889
1890        (( i_ = i+2 ))
1891        eval file_outin_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1892        eval file_outin=${file_outin_}
1893
1894        (( i_ = i+3 ))
1895        eval is_opt=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1896        if [ X${is_opt} = XOPTIONAL ] ; then
1897            echo ${file_in} is optional
1898            (( i=i+4 ))
1899        else
1900            is_opt=""
1901            echo ${file_in} is mandatory
1902            (( i=i+3 ))
1903        fi
1904       
1905        generic_restart_file_name_in=$(    basename ${file_in} .nc )
1906        generic_restart_file_name_out=$(   basename ${config_UserChoices_JobName}_${PeriodDateEnd}_${file_out} .nc )
1907        generic_restart_file_name_outin=$( basename ${file_outin} .nc )
1908
1909        nb_restart_file=$( ls ${generic_restart_file_name_in}_????.nc 2>/dev/null | wc -l )
1910        if [ ${nb_restart_file} -gt 1 ] ; then
1911          j=0                                     # BASH LINE NOT NEEDED
1912          # BASH for j4 in in $( eval echo {0000..$(( nb_restart_file - 1 ))} ) ; do
1913          until [ $j -ge ${nb_restart_file} ]; do # BASH LINE NOT NEEDED
1914            j4=${j}                               # BASH LINE NOT NEEDED
1915            if [ X${Pack} = Xtrue ] ; then
1916              eval IGCM_sys_PutBuffer_Rest ${generic_restart_file_name_in}_${j4}.nc \${R_BUF_${comp}_R}/${generic_restart_file_name_out}_${j4}.nc ${is_opt}
1917            else
1918              eval IGCM_sys_Put_Rest ${generic_restart_file_name_in}_${j4}.nc \${R_OUT_${comp}_R}/${generic_restart_file_name_out}_${j4}.nc ${is_opt}
1919            fi
1920
1921            if [ -f  ${generic_restart_file_name_in}_${j4}.nc ] ; then
1922                if [ ! ${file_in} = ${file_outin} ] ; then
1923                    if ( ${ExitFlag} ) ; then
1924                        echo "IGCM_sys_Mv ${generic_restart_file_name_in}_${j4}.nc ${generic_restart_file_name_outin}_${j4}.nc not executed."
1925                    else
1926                        IGCM_sys_Mv ${generic_restart_file_name_in}_${j4}.nc ${generic_restart_file_name_outin}_${j4}.nc
1927                    fi
1928                fi
1929            else
1930                # This is the case where the restart file no not exist.
1931                # It has already been checked if the model should stop for this case.
1932                IGCM_debug_Print 1 "Restart file does not exist, no move in RUN_DIR done :  ${generic_restart_file_name_in}_${j4}.nc"
1933            fi
1934            (( j=j+1 ))                           # BASH LINE NOT NEEDED
1935          done
1936        else
1937          if [ X${Pack} = Xtrue ] ; then
1938            eval IGCM_sys_PutBuffer_Rest ${file_in} \${R_BUF_${comp}_R}/${config_UserChoices_JobName}_${PeriodDateEnd}_${file_out} ${is_opt}
1939          else
1940            eval IGCM_sys_Put_Rest ${file_in} \${R_OUT_${comp}_R}/${config_UserChoices_JobName}_${PeriodDateEnd}_${file_out} ${is_opt}
1941          fi
1942
1943          if [ -f ${file_in} ] ; then
1944              if [ ! ${file_in} = ${file_outin} ] ; then
1945                  if ( ${ExitFlag} ) ; then
1946                      echo "IGCM_sys_Mv ${file_in} ${file_outin} not executed."
1947                  else
1948                      IGCM_sys_Mv ${file_in} ${file_outin}
1949                  fi
1950              fi
1951          else
1952              # This is the case where the restart file no not exist.
1953              # It has already been checked if the model should stop for this case.
1954              IGCM_debug_Print 1 "Restart file does not exist, no move in RUN_DIR done :   ${file_in}"
1955          fi
1956        fi
1957
1958      done
1959    else
1960      if [ X${FileName0} != XNONE ] ; then
1961        IGCM_debug_Exit "IGCM_comp_Finalize : No file in restart list for ${compname}."
1962      else
1963        IGCM_debug_Print 1 "IGCM_comp_Finalize : NONE specified in Restart List ${compname}."
1964      fi
1965    fi
1966
1967    # Save Output files
1968    #------------------
1969    IGCM_debug_Print 2 "Save Output files for ${comp} : ${compname} component."
1970    IGCM_card_DefineArrayFromOption ${card} OutputFiles List
1971    ListFilesName=${compname}_OutputFiles_List
1972    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
1973
1974    if [ X${FileName0} != X${NULL_STR} ] ; then
1975      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
1976
1977      (( i=0 ))
1978      until [ $i -ge $NbFiles ]; do
1979        SaveOnArchive=true
1980        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
1981        eval file_in=${file_in_}
1982        (( i_ = i+1 ))
1983        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1984        eval file_out=${file_out_}
1985        #
1986        # Override file_out path remplacing R_SAVE by R_BUFR
1987        #
1988        if [ X${Pack} = Xtrue ] ; then
1989          file_out=$( echo $file_out | sed "s:^$R_SAVE:$R_BUFR:" )
1990        fi
1991        #
1992        # Not necessarily the best option. /!\ Potential side effects /!\
1993        #
1994        (( i_ = i+2 ))
1995        eval flag_post=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1996        #
1997        generic_file_name=$( basename ${file_in} .nc )
1998        nb_rebuild_file=$( ls | grep "^${generic_file_name}_[0-9]*.nc" | wc -l )
1999        #
2000        if ( [ ${nb_rebuild_file} -eq 1 ] && [ -f ${generic_file_name}_0000.nc ] ) ; then
2001          IGCM_debug_Print 2 "Parallelism with 1 file. Rebuilding ${file_in} not needed"
2002          IGCM_sys_Mv ${generic_file_name}_0000.nc ${file_in}
2003        elif [ ${nb_rebuild_file} -gt 1 ] ; then
2004          IGCM_debug_Print 2 "Parallelism detected and rebuilding ${file_in} is needed"
2005          if [ X${AsynchronousRebuild} = Xfalse ] ; then
2006            IGCM_debug_Print 2 "Rebuilding ${file_in} online"
2007            #
2008            # for output.abort file, let use rebuild_NEMO : 13s instead of 20 mn.
2009            if [ ${file_in} = output.abort.nc ] ; then
2010              IGCM_sys_rebuild_nemo ${generic_file_name} ${nb_rebuild_file} "nc" ${generic_file_name}_[0-9]*.nc
2011            else
2012              IGCM_sys_rebuild ${file_in} ${generic_file_name}_????.nc
2013            fi
2014          else
2015            IGCM_debug_Print 2 "Preparing offline rebuild for ${file_in}"
2016            IGCM_sys_Mv ${generic_file_name}_????.nc ${RUN_DIR}/REBUILD_${PeriodDateBegin}
2017
2018            # Prepare the shell dedicated to offline rebuild
2019            if [ $DRYRUN -le 1 ]; then
2020              if [ ${file_in} = histstn.nc ] ; then
2021                echo "IGCM_sys_rebuild_station ${file_in} ${generic_file_name}_*.nc" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2022              else
2023                echo "IGCM_sys_rebuild ${file_in} ${generic_file_name}_[0-9]*.nc" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2024              fi
2025              echo "IGCM_debug_Verif_Exit" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2026            fi
2027            #
2028            # Load Patch we need to apply and apply
2029            if [ $DRYRUN -le 1 ]; then
2030              if [ X$( eval echo \${${compname}_${flag_post}_Patches[0]} ) !=  X${NULL_STR} ]; then
2031                for Patch in $( eval echo \${${compname}_${flag_post}_Patches[*]} ) ; do
2032                  echo ". ${libIGCM_POST}/libIGCM_post/IGCM_${Patch}.ksh" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2033                  echo "IGCM_${Patch} ${file_in}                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2034                  echo "IGCM_debug_Verif_Exit                           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2035                done
2036              fi
2037            fi
2038            #
2039            if [ $DRYRUN -le 1 ]; then
2040              if [ X${Pack} = Xtrue ] ; then
2041                rebuildedActionsList[${#rebuildedActionsList[*]}]="IGCM_sys_PutBuffer_Out ${file_in} ${file_out}"
2042              else
2043                rebuildedActionsList[${#rebuildedActionsList[*]}]="IGCM_sys_Put_Out ${file_in} ${file_out}"
2044              fi
2045              rebuildedActionsList[${#rebuildedActionsList[*]}]="IGCM_debug_Verif_Exit"
2046              rebuildedActionsList[${#rebuildedActionsList[*]}]="IGCM_sys_Rm ${generic_file_name}_[0-9]*.nc"
2047            fi
2048            SaveOnArchive=false
2049          fi
2050        fi
2051        #
2052        if [ ${SaveOnArchive} = true ] ; then
2053          #
2054          # Rebuild has been done online or it was not needed
2055          #
2056          # If we need to apply a patch we use TMP DIRECTORY before ARCHIVING if asynchronous rebuild is on
2057          #
2058          thereisapatch=$( eval echo \${${compname}_${flag_post}_Patches[0]} )
2059          if ( [ ! X${thereisapatch} = X${NULL_STR} ] && [ ! X${thereisapatch} = X ] && [ X${AsynchronousRebuild} = Xtrue ] && [ -f ${file_in} ] ) ; then
2060            IGCM_sys_Mv ${file_in} ${RUN_DIR}/REBUILD_${PeriodDateBegin}
2061            eval FileToBeDeleted[${#FileToBeDeleted[@]}]=REBUILD_${PeriodDateBegin}/${file_in} > /dev/null 2>&1
2062            #
2063            if [ $DRYRUN -le 1 ]; then
2064              for Patch in $( eval echo \${${compname}_${flag_post}_Patches[*]} ) ; do
2065                echo ". ${libIGCM_POST}/libIGCM_post/IGCM_${Patch}.ksh" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2066                echo "IGCM_${Patch} ${file_in}                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2067                echo "IGCM_debug_Verif_Exit                           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2068              done
2069              #
2070              if [ X${Pack} = Xtrue ] ; then
2071                echo "IGCM_sys_PutBuffer_Out ${file_in} ${file_out}   " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2072              else
2073                echo "IGCM_sys_Put_Out ${file_in} ${file_out}         " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2074              fi
2075              echo "IGCM_debug_Verif_Exit                             " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2076              #
2077            fi
2078          else
2079            #
2080            # No Patch, No Asynchronous rebuild, online rebuild has been done or was not needed
2081            #
2082            if [ X${Pack} = Xtrue ] ; then
2083              IGCM_sys_PutBuffer_Out ${file_in} ${file_out}
2084            else
2085              IGCM_sys_Put_Out ${file_in} ${file_out}
2086            fi
2087            eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file_in}" > /dev/null 2>&1
2088            if [ ${nb_rebuild_file} -gt 1 ] ; then
2089              for DelFile in $( ls | grep "${generic_file_name}[_0-9]*.nc" ) ; do
2090                eval FileToBeDeleted[${#FileToBeDeleted[@]}]=${DelFile} > /dev/null 2>&1
2091              done
2092            fi
2093          fi
2094        fi
2095        ((i_ = i_ +1))
2096        eval Testvar_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
2097        eval Testvar=${Testvar_}
2098        if [[ ${Testvar} =~ [0-9][DMY]$ ]] ; then
2099            ((i = i+4))
2100        else
2101        (( i=i+3 ))
2102        fi
2103      done
2104    fi
2105    echo
2106    if [ X${number_rundir} != X ] ; then
2107        cd $RUN_DIR 
2108    fi
2109  done
2110  # Append the sync call and the copy sequence to the IGCM_FlushRebuild function if needed
2111  if [ ${#rebuildedActionsList[*]} -ne 0 ] ; then
2112    echo "IGCM_sys_sync              " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2113    i=0
2114    until [ ${i} -ge ${#rebuildedActionsList[*]} ]; do
2115      echo ${rebuildedActionsList[$i]} >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
2116      (( i=i+1 ))
2117    done
2118  fi
2119  IGCM_debug_PopStack "IGCM_comp_Finalize"
2120}
Note: See TracBrowser for help on using the repository browser.