source: trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh @ 1649

Last change on this file since 1649 was 1649, checked in by aclsce, 2 months ago

Fixed bug in case of RESTART tar file is targeted.

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