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

Last change on this file since 1151 was 1151, checked in by sdipsl, 9 years ago

adressing #237

  • 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: 68.8 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 CompatibilityTag 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    # Read libIGCM compatibility version in ${compname}.card
36    card=${SUBMIT_DIR}/COMP/${compname}.card
37    IGCM_card_DefineVariableFromOption ${card} Compatibility libIGCM
38
39    eval CompatibilityTag=\${${compname}_Compatibility_libIGCM} > /dev/null 2>&1
40    if [ ! "${CompatibilityTag}" = "${libIGCM_CurrentTag}" ] ; then
41      IGCM_debug_Exit "${compname}.card is not compatible with libIGCM version ${libIGCM_CurrentTag} see libIGCM FAQ http://wiki.ipsl.jussieu.fr/wiki_ipsl/IGCMG/libIGCM/DocUtilisateur/FAQ ."
42    fi
43
44    # Manage component executable
45    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/config.card Executable ${comp}
46
47    # Define all options in section [comp]
48    IGCM_debug_Print 3 " DefineArrayFromSection : ${comp}"
49    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/config.card ${comp}
50    eval config_comp=\${config_${comp}[*]} > /dev/null 2>&1
51    for option in ${config_comp[*]} ; do
52        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} ${option}
53    done
54    IGCM_debug_Print 3 " Found in section config_${comp} :  ${config_comp[*]} "
55
56    # Debug Print :
57    eval auxprint=\${config_${comp}_WriteFrequency}
58    IGCM_debug_Print 1 "Write frequency for ${compname} : ${auxprint} "
59    #2> /dev/null
60
61    # Debug Print :
62    IGCM_debug_Print 2 "Initialize following component library"
63
64    # Source drivers in directory DRIVER if it exist
65    # else source them from directory COMP
66    if [ -d ${SUBMIT_DIR}/DRIVER ] ; then
67      IGCM_debug_Print 2 ${SUBMIT_DIR}/DRIVER/${compname}.driver
68      # Source component library
69      . ${SUBMIT_DIR}/DRIVER/${compname}.driver
70    else
71      IGCM_debug_Print 2 ${SUBMIT_DIR}/COMP/${compname}.driver
72      # Source component library
73      . ${SUBMIT_DIR}/COMP/${compname}.driver
74    fi
75    IGCM_debug_Print 3 "With tag : ${comptagname}"
76
77    # Debug Print
78    IGCM_debug_Print 3 "Initialize ${comp} output directory."
79
80    # Define ARCHIVED Dirs
81    eval R_OUT_${comp}=${R_SAVE}/${comp}
82    eval IGCM_sys_MkdirArchive \${R_OUT_${comp}}
83
84    eval R_OUT_${comp}_O=\${R_OUT_${comp}}/Output
85    eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_O}
86
87    eval R_OUT_${comp}_R=\${R_OUT_${comp}}/Restart
88    [ ${config_Post_PackFrequency} = NONE ] && eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_R}
89
90    eval R_OUT_${comp}_D=\${R_OUT_${comp}}/Debug
91    [ ${config_Post_PackFrequency} = NONE ] && eval IGCM_sys_MkdirArchive \${R_OUT_${comp}_D}
92
93    eval R_OUT_${comp}_O_I=\${R_OUT_${comp}_O}/INS
94    eval R_OUT_${comp}_O_H=\${R_OUT_${comp}_O}/HF
95    eval R_OUT_${comp}_O_D=\${R_OUT_${comp}_O}/DA
96    eval R_OUT_${comp}_O_M=\${R_OUT_${comp}_O}/MO
97    eval R_OUT_${comp}_O_Y=\${R_OUT_${comp}_O}/YE
98
99    # Define BUFFERED Dirs
100    if ( [ ! ${config_Post_PackFrequency} = NONE ] || [ X${config_UserChoices_SpaceName} = XTEST ] ) ; then
101      eval R_BUF_${comp}=${R_BUFR}/${comp}
102      eval IGCM_sys_Mkdir \${R_BUF_${comp}}
103
104      eval R_BUF_${comp}_O=\${R_BUF_${comp}}/Output
105      eval IGCM_sys_Mkdir \${R_BUF_${comp}_O}
106
107      eval R_BUF_${comp}_R=\${R_BUF_${comp}}/Restart
108      eval IGCM_sys_Mkdir \${R_BUF_${comp}_R}
109
110      eval R_BUF_${comp}_D=\${R_BUF_${comp}}/Debug
111      eval IGCM_sys_Mkdir \${R_BUF_${comp}_D}
112
113      eval R_BUF_${comp}_O_I=\${R_BUF_${comp}_O}/INS
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    fi
119
120    # Read UserChoices section of component card
121    IGCM_debug_Print 2 "DefineArrayFromSection : ${compname}_UserChoices ${card}"
122    IGCM_card_DefineArrayFromSection ${card} UserChoices
123    eval first_option=\${${compname}_UserChoices[0]} > /dev/null 2>&1
124
125    # If section is not empty we define corresponding variables
126    if [ X${first_option} != X"Error:" ] ; then
127      if [ X${card_UserChoices[0]} != X ] ; then
128        unset card_UserChoices
129      fi
130      eval set +A card_UserChoices -- \${${compname}_UserChoices[*]} > /dev/null 2>&1
131      IGCM_debug_Print 3 "${compname}_UserChoices_values:"
132      for option in ${card_UserChoices[*]} ; do
133        IGCM_card_DefineVariableFromOption ${card} UserChoices ${option}
134        eval IGCM_debug_Print 3 "${option}=\${${compname}_UserChoices_${option}}"
135      done
136    fi
137
138    # Read and Build Output File stuff
139    IGCM_debug_Print 2 "DefineArrayFromOption  : ${compname}_OutputFiles ${card}"
140    IGCM_card_DefineArrayFromOption ${card} OutputFiles List
141    ListFilesName=${compname}_OutputFiles_List
142    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
143    #
144    if [ X${FileName0} != X${NULL_STR} ] ; then
145      #
146      #IGCM_debug_Print 1 "Component      : ${compname}"
147      #
148      # INITIALISATION
149      #
150      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
151      #
152      i=2
153      #
154      until [ $i -ge $NbFiles ]; do
155        #
156        eval flag_post=\${${ListFilesName}[$i]} > /dev/null 2>&1
157        #
158        if [ X${flag_post} != XNONE ] ; then
159          #
160          # First of all
161          #
162          IGCM_card_DefineArrayFromSection ${card} ${flag_post}
163          # This section is mandatory
164          if [ "X$( eval echo \${${compname}_${flag_post}[@]} )" = "X" ] ; then
165            IGCM_debug_Print 1 "IGCM_card_DefineArrayFromSection ${card} ${flag_post}"
166            IGCM_debug_Exit "${flag_post} section do not exist in ${card}. Please check your card."
167            IGCM_debug_Verif_Exit
168          fi
169          #
170          # Seasonal case : If option Seasonal is not found (old cards) put SEASONAL ON by default
171          #
172          # variable option allready typeset above
173          for option in $( eval echo \${${compname}_${flag_post}[*]} ) ; do
174            if [ ${option} = Seasonal ] ; then
175              FoundSeasonal=true
176              IGCM_card_DefineVariableFromOption ${card} ${flag_post} Seasonal
177            fi
178          done
179          #
180          if [ ! X${FoundSeasonal} = Xtrue ] ; then
181            eval ${compname}_${flag_post}_Seasonal=ON
182          fi
183          #
184          if [ $( eval echo \${${compname}_${flag_post}_Seasonal} ) = ON ] ; then
185            Seasonal=true
186          fi
187
188          # Dimension = vide si vieille card.
189          IGCM_card_DefineArrayFromOption ${card} ${flag_post} TimeSeriesVars
190          IGCM_card_DefineArrayFromOption ${card} ${flag_post} Patches
191          if [ X"$( eval echo \${${compname}_${flag_post}_TimeSeriesVars[*]} )" = X"Option not" ] ; then
192            # New TimeSeriesVar description, with 2D, 3D and associate ChunckJob.
193            ListDimension[0]=2D
194            ListDimension[1]=3D
195            TimeSeries=false
196            iLoop=${#ListDimension[*]}
197            j=0
198            until [ $j -ge ${iLoop} ]; do
199              Dimension=${ListDimension[${j}]}
200              IGCM_card_DefineArrayFromOption ${card} ${flag_post} TimeSeriesVars${Dimension}
201              IGCM_card_DefineVariableFromOption ${card} ${flag_post} ChunckJob${Dimension}
202              #
203              # Time series WITHOUT chunk
204              #
205              if [ ! $( eval echo \${${compname}_${flag_post}_TimeSeriesVars${Dimension}} ) = ${NULL_STR} ] ; then
206                if [ $( eval echo \${${compname}_${flag_post}_ChunckJob${Dimension}} ) = NONE ] ; then
207                  IGCM_debug_Print 3 "${Dimension} time series activated for ${flag_post}"
208                  eval TimeSeries${Dimension}=true
209                fi
210              fi
211              #
212              # Time series WITH chunk
213              #
214              if [ ! $( eval echo \${${compname}_${flag_post}_TimeSeriesVars${Dimension}} ) = ${NULL_STR} ] ; then
215                chunck_size=$( eval echo \${${compname}_${flag_post}_ChunckJob${Dimension}} )
216                if [ ! ${chunck_size} = NONE ] &&  [ ! ${chunck_size} = OFF ] ; then
217                  IGCM_debug_Print 3 "${Dimension} time series activated with chunck for ${flag_post}"
218                  eval TimeSeriesChunck${Dimension}=true
219                  eval CHUNCK${Dimension}_COMP[\${#CHUNCK${Dimension}_COMP[*]}]=${comp}
220                  eval CHUNCK${Dimension}_FLAG[\${#CHUNCK${Dimension}_FLAG[*]}]=${i}
221                  eval CHUNCK${Dimension}_NAME[\${#CHUNCK${Dimension}_NAME[*]}]=${flag_post}
222                  eval CHUNCK${Dimension}_SIZE[\${#CHUNCK${Dimension}_SIZE[*]}]=${chunck_size}
223                fi
224              fi
225              (( j=j+1 ))
226            done
227          else
228            ListDimension[0]=""
229            TimeSeries=true
230            TimeSeries2D=false
231            TimeSeries3D=false
232            TimeSeriesChunck2D=false
233            TimeSeriesChunck3D=false
234          fi
235        fi
236        (( i=i+3 ))
237      done
238    fi
239    # Debug Print
240    IGCM_debug_Print 3 "Initialize ${compname} with driver."
241    # INIT component
242    ${comp}_Initialize
243    echo
244  done
245
246  IGCM_debug_PopStack "IGCM_comp_Initialize"
247}
248
249#=======================================================================
250function IGCM_comp_PrepareDeletedFiles
251{
252  IGCM_debug_PushStack "IGCM_comp_PrepareDeletedFiles" $@
253
254  if [ X${2} != X. ] ; then
255    eval FileToBeDeleted[${#FileToBeDeleted[@]}]=$( basename ${2} ) > /dev/null 2>&1
256  else
257    eval FileToBeDeleted[${#FileToBeDeleted[@]}]=$( basename ${1} ) > /dev/null 2>&1
258  fi
259
260  IGCM_debug_PopStack "IGCM_comp_PrepareDeletedFiles"
261}
262
263#=======================================================================
264function IGCM_comp_GetInputInitialStateFiles
265{
266  IGCM_debug_PushStack "IGCM_comp_GetInputInitialStateFiles"
267
268  # Debug Print :
269  echo
270  IGCM_debug_Print 1 "IGCM_comp_GetInputInitialStateFiles"
271  echo
272
273  # Only the first time step need InitialStateFiles
274  # otherwise it's BoundaryConditions
275  if ( ${FirstInitialize} ) ; then
276    typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_
277    typeset file_in_ file_in file_out_ file_out do_init
278    for comp in ${config_ListOfComponents[*]} ; do
279      # Initialize
280      do_init="y"
281      # Do we need to bring initial state file for this component
282      if [ "${config_Restarts_OverRule}" = "y" ] ; then
283        eval do_init="n"
284      else
285        # Read component Restarts parameters
286        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} Restart
287        eval do_start=\${config_${comp}_Restart} > /dev/null 2>&1
288        if [ "${do_start}" = "y" ] ; then
289          do_init="n"
290        else
291          do_init="y"
292        fi
293      fi
294
295      if [ "${do_init}" = "y" ] ; then
296        # Define component
297        eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
298        eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
299
300        # Debug Print :
301        IGCM_debug_Print 3 "Initialisation files ${compname}"
302
303        card=${SUBMIT_DIR}/COMP/${compname}.card
304
305        IGCM_card_DefineArrayFromOption ${card} InitialStateFiles List
306        ListFilesName=${compname}_InitialStateFiles_List
307
308        eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
309        if [ X${FileName0} != X${NULL_STR} ] ; then
310          eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
311          (( i=0 ))
312          until [ $i -ge $NbFiles ]; do
313            eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
314            eval file_in=${file_in_}
315            (( i_ = i+1 ))
316            eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
317            eval file_out=${file_out_}
318
319            IGCM_sys_IsFileArchived ${file_in}
320            if [ $? = 0 ] ; then
321              IGCM_sys_Get ${file_in} ${file_out}
322              #IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
323            else
324              IGCM_sys_Cp ${file_in} ${file_out}
325            fi
326            (( i=i+2 ))
327          done
328        fi
329      fi
330    done
331  fi
332  IGCM_debug_PopStack "IGCM_comp_GetInputInitialStateFiles"
333}
334
335#=======================================================================
336# Definition of Smooth modulo function
337# usage :
338# IGCM_SmoothModulo StringModulo value
339#
340# StringModulo : A string of min max and modulo like definition of Scilab vectors.
341# [min]:[modulo:][max]
342# where :
343# [] value are optionnals;
344# empty min equal 1
345# empty max equal infinity
346# modulo not given or empty equal 1
347# empty string or just ':' equal always.
348#
349# value : the value to test with the definition
350#
351# return : true(1)/false(0)
352function IGCM_SmoothModulo
353{
354  IGCM_debug_PushStack "IGCM_SmoothModulo"
355  typeset defVector ModValue
356
357  eval set +A defVector -- $( echo "${1}" | \
358    gawk -F ':' '{print ($1 == "" ? 1 : $1) " " (NF==3 ? ($2 == "" ? 1 : $2) : 1) " " (NF==3 ? ($3 == "" ? -1 : $3) : ($2 == "" ? -1 : $2))}' )
359
360  # Save Smooth Min and Max. Needed to call IGCM_sys_Get when appropriate
361  arr[1]=${defVector[0]}
362  arr[2]=${defVector[2]}
363
364  # Test limits :
365  # ${defVector[0]} <= ${2} <= ${defVector[2]}
366  #          or ${defVector[2]} == -1
367  if ( [ ${2} -ge ${defVector[0]} ] && ( [ ${2} -le ${defVector[2]} ] || [ ${defVector[2]} -lt 0 ] ) ) ; then
368    # Test modulo
369    ModValue=$( expr \( ${2} - ${defVector[0]} \) % ${defVector[1]} )
370    if [ ${ModValue} -eq 0 ] ;  then
371      arr[3]=true
372      echo ${arr[@]}
373      IGCM_debug_PopStack "IGCM_SmoothModulo"
374      return 1
375    else
376      arr[3]=false
377      echo ${arr[@]}
378      IGCM_debug_PopStack "IGCM_SmoothModulo"
379      return 0
380    fi
381  else
382    arr[3]=false
383    echo ${arr[@]}
384    IGCM_debug_PopStack "IGCM_SmoothModulo"
385    return 0
386  fi
387}
388
389#=======================================================================
390function IGCM_comp_GetInputSmoothFiles
391{
392  IGCM_debug_PushStack "IGCM_comp_GetInputSmoothFiles"
393
394  # Debug Print :
395  echo
396  IGCM_debug_Print 1 "IGCM_comp_GetInputSmoothFiles"
397  echo
398
399  typeset comp compname comptagname card ListFilesName FileName0 NbFiles j i i_ i__
400  typeset file_in_ file_in file_out_ file_out ret SmoothDef aux val
401
402  for comp in ${config_ListOfComponents[*]} ; do
403    # Define component
404    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
405    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
406
407    # Debug Print :
408    IGCM_debug_Print 3 "Smooth files ${compname}"
409
410    card=${SUBMIT_DIR}/COMP/${compname}.card
411
412    IGCM_card_DefineArrayFromOption ${card} SmoothFiles List
413    ListFilesName=${compname}_SmoothFiles_List
414    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
415
416    if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != X"Section" ] ) ; then
417      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
418
419      (( i=0 ))
420      until [ $i -ge $NbFiles ]; do
421        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
422        eval file_in=${file_in_}
423        (( i_ = i+1 ))
424        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
425        eval file_out=${file_out_}
426
427        # define CumulPeriod definition for this file
428        (( i__ = i+2 ))
429        eval SmoothDef=\${${ListFilesName}[$i__]}
430        IGCM_debug_Print 3 "  ${file_in} ${SmoothDef}"
431        aux=$( IGCM_SmoothModulo ${SmoothDef} ${CumulPeriod} )
432        j=1
433        for val in ${aux} ; do
434          [ ${j} -eq 1 ] && SmoothMin=${val}
435          [ ${j} -eq 2 ] && SmoothMax=${val}
436          [ ${j} -eq 3 ] && ret=${val}
437          (( j=j+1 ))
438        done
439        [ ${SmoothMax} -eq -1 ] && SmoothMax=${CumulPeriod}
440        if ( [ X${ret} = Xtrue ] || ( [ ${Period} -eq 1 ] && [ ${CumulPeriod} -ge ${SmoothMin} ] && [ ${CumulPeriod} -le ${SmoothMax} ] ) ) ; then
441
442          IGCM_sys_IsFileArchived ${file_in}
443          if [ $? = 0 ] ; then
444            IGCM_sys_Get ${file_in} ${file_out}
445            #IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
446          else
447            IGCM_sys_Cp ${file_in} ${file_out}
448          fi
449        fi
450        (( i=i+3 ))
451      done
452    fi
453  done
454
455  IGCM_debug_PopStack "IGCM_comp_GetInputSmoothFiles"
456}
457
458#=======================================================================
459function IGCM_comp_GetInputBoundaryFiles
460{
461  IGCM_debug_PushStack "IGCM_comp_GetInputBoundaryFiles"
462
463  # Debug Print :
464  echo
465  IGCM_debug_Print 1 "IGCM_comp_GetInputBoundaryFiles"
466  echo
467
468  typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_
469  typeset file_in_ file_in file_out_ file_out
470
471  if [ ${Period} = 1 ]; then
472    ListFixBoundary=" "
473  fi
474
475  for comp in ${config_ListOfComponents[*]} ; do
476
477    # Define component
478    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
479    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
480
481    # Debug Print :
482    IGCM_debug_Print 3 "Boundary files ${compname}"
483
484    card=${SUBMIT_DIR}/COMP/${compname}.card
485
486    IGCM_card_DefineArrayFromOption ${card} BoundaryFiles List
487    ListFilesName=${compname}_BoundaryFiles_List
488    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
489
490    if [ X${FileName0} != X${NULL_STR} ] ; then
491      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
492
493      (( i=0 ))
494      until [ $i -ge $NbFiles ]; do
495        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
496        eval file_in=${file_in_}
497        (( i_ = i+1 ))
498        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
499        eval file_out=${file_out_}
500
501        IGCM_sys_IsFileArchived ${file_in}
502        if [ $? = 0 ] ; then
503          IGCM_sys_Get ${file_in} ${file_out}
504          IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
505        else
506          IGCM_sys_Cp ${file_in} ${file_out}
507        fi
508
509        (( i=i+2 ))
510      done
511    fi
512
513    # Get non deleted files
514    if [ ${Period} = 1 ]; then
515
516      IGCM_card_DefineArrayFromOption ${card} BoundaryFiles ListNonDel
517      ListFilesName=${compname}_BoundaryFiles_ListNonDel
518      eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
519
520      if [ X${FileName0} != X${NULL_STR} ] ; then
521        eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
522
523        (( i=0 ))
524        until [ $i -ge $NbFiles ]; do
525          eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
526          eval file_in=${file_in_}
527          (( i_ = i+1 ))
528          eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
529          eval file_out=${file_out_}
530
531          IGCM_sys_IsFileArchived ${file_in}
532          if [ $? = 0 ] ; then
533            IGCM_sys_Get ${file_in} ${file_out}
534            #IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
535          else
536            IGCM_sys_Cp ${file_in} ${file_out}
537          fi
538
539          if [ X${file_out} != X. ] ; then
540            ListFixBoundary=${ListFixBoundary}" "${file_out}
541          else
542            ListFixBoundary=${ListFixBoundary}" "$( basename ${file_in} )
543          fi
544
545          (( i=i+2 ))
546        done
547      fi
548    fi
549  done
550
551  IGCM_debug_PopStack "IGCM_comp_GetInputBoundaryFiles"
552}
553
554#=======================================================================
555function IGCM_comp_DelFixeBoundaryFiles
556{
557  IGCM_debug_PushStack "IGCM_comp_DelFixeBoundaryFiles"
558
559  # Debug Print :
560  echo
561  IGCM_debug_Print 1 "IGCM_comp_DelFixeBoundaryFiles"
562  echo
563
564  ls -l ${ListFixBoundary}
565  rm -f ${ListFixBoundary}
566
567  IGCM_debug_PopStack "IGCM_comp_DelFixeBoundaryFiles"
568}
569
570#=======================================================================
571function IGCM_comp_GetInputParametersFiles
572{
573  IGCM_debug_PushStack "IGCM_comp_GetInputParametersFiles"
574
575  # Debug Print :
576  echo
577  IGCM_debug_Print 1 "IGCM_comp_GetInputParametersFiles"
578  echo
579
580  typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_ file_in file_out
581  for comp in ${config_ListOfComponents[*]} ; do
582    # Define component
583    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
584    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
585
586    # Debug Print :
587    IGCM_debug_Print 3 "Parameters ${compname}"
588
589    card=${SUBMIT_DIR}/COMP/${compname}.card
590
591    IGCM_card_DefineArrayFromOption ${card} ParametersFiles List
592    ListFilesName=${compname}_ParametersFiles_List
593    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
594
595    if [ X${FileName0} != X${NULL_STR} ] ; then
596      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
597
598      (( i=0 ))
599      until [ $i -ge $NbFiles ]; do
600        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
601        eval file_in=${file_in_}
602        (( i_ = i+1 ))
603        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
604        eval file_out=${file_out_}
605
606        IGCM_sys_Cp ${file_in} ${file_out}
607        IGCM_comp_PrepareDeletedFiles ${file_in} ${file_out}
608
609        (( i=i+2 ))
610      done
611    fi
612  done
613
614  IGCM_debug_PopStack "IGCM_comp_GetInputParametersFiles"
615}
616
617#=======================================================================
618function IGCM_comp_GetInputRestartFiles
619{
620  IGCM_debug_PushStack "IGCM_comp_GetInputRestartFiles"
621
622  # Debug Print :
623  echo
624  IGCM_debug_Print 1 "IGCM_comp_GetInputRestartFiles"
625  echo
626
627  typeset Date_tmp Date_r Path_r do_start CompOldName Path_OUT Path_BUF
628  typeset Buffered Archived Tared PotentialTarFile IsMatching TarFileFound
629  typeset comp compname comptagname card ListFilesName FileName0 NbFiles i i_
630  typeset file_in file_out file_in_ file_out_ file_in_Name
631  typeset -Z4 j4
632  #BASH declare j4
633
634  IsMatching=""
635
636  for comp in ${config_ListOfComponents[*]} ; do
637    # Define component
638    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
639    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
640    #
641    card=${SUBMIT_DIR}/COMP/${compname}.card
642    #
643    IGCM_card_DefineArrayFromOption ${card} RestartFiles List
644    ListFilesName=${compname}_RestartFiles_List
645    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
646
647    # Debug Print :
648    IGCM_debug_Print 3 "restart ${compname}"
649
650    if ( ${FirstInitialize} ) ; then
651
652      if [ "${config_Restarts_OverRule}" = "y" ] ; then
653        eval config_${comp}_Restart="y"
654        eval config_${comp}_RestartDate=${config_Restarts_RestartDate}
655        eval config_${comp}_RestartJobName=${config_Restarts_RestartJobName}
656        eval config_${comp}_RestartPath=${config_Restarts_RestartPath}
657        eval do_start=\${config_${comp}_Restart} > /dev/null 2>&1
658        eval CompOldName=${comp}
659      else
660        # Read component Restarts parameters
661        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} Restart
662        eval do_start=\${config_${comp}_Restart} > /dev/null 2>&1
663
664        if [ "${do_start}" = "y" ] ; then
665          IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} RestartDate
666          IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} RestartJobName
667          IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card ${comp} RestartPath
668        else
669          eval config_${comp}_RestartDate=-1
670          eval config_${comp}_RestartJobName=${NULL_STR}
671          eval config_${comp}_RestartPath=${NULL_STR}
672        fi
673
674        eval CompOldName=\${config_${comp}_OldName}
675        if [ X${CompOldName} = X ] ; then
676          eval CompOldName=${comp}
677        fi
678
679        # Reinitialize IsMatching to allow searching for a different tar file for each component.
680        IsMatching=""
681        TarFileFound=""
682      fi
683
684      if [ "${do_start}" = "y" ] ; then
685
686        # Restore Restarts files
687        #-----------------------
688        if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != XNONE ] ) ; then
689          eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
690
691          (( i=1 ))
692          until [ $i -gt $NbFiles ]; do
693            eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
694            eval file_in=${file_in_}
695            (( i_ = i+1 ))
696            eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
697            eval file_out=${file_out_}
698
699            eval Date_tmp=\${config_${comp}_RestartDate}
700            Date_r=$( IGCM_date_ConvertFormatToGregorian ${Date_tmp} )
701            # will be re-use
702            eval RestartPath=\${config_${comp}_RestartPath}
703            eval RestartJobName=\${config_${comp}_RestartJobName}
704            #
705            Path_r=${RestartPath}/${RestartJobName}/${CompOldName}/Restart
706            file_in_Name=${RestartJobName}_${Date_r}_${file_in}
707
708            extension_in=$( echo ${file_in_Name##*.} )
709            extension_out=$( echo ${file_out##*.} )
710
711            generic_restart_file_name_in=$( basename ${file_in_Name} .${extension_in} )
712            generic_restart_file_name_out=$( basename ${file_out} .${extension_out} )
713
714            Path_OUT=${Path_r}/${generic_restart_file_name_in}
715
716            if [ $( IGCM_sys_TestFileBuffer ${Path_OUT}*.${extension_in} ; echo $? ) = 0 ] ; then
717              IGCM_debug_Print 3 "Buffered restart ${Path_OUT}*.${extension_in} "
718              Buffered=true
719              Archived=false
720              Tared=false
721              nb_restart_file=$(IGCM_sys_CountFileBuffer ${Path_OUT}_????.${extension_in})
722            elif [ $( IGCM_sys_TestFileArchive ${Path_OUT}*.${extension_in} ; echo $? ) = 0 ] ; then
723              IGCM_debug_Print 3 "Archived restart ${Path_OUT}*.${extension_in}"
724              Buffered=false
725              Archived=true
726              Tared=false
727              nb_restart_file=$(IGCM_sys_CountFileArchive ${Path_OUT}_????.${extension_in})
728            else
729              IGCM_debug_Print 3 "No restart file in the buffer nor in the archive directory"
730              IGCM_debug_Print 3 "${Path_OUT}*.${extension_in} do not exist"
731              IGCM_debug_Print 3 "Restart files will now be searched for in : ${RestartPath}/${RestartJobName}/RESTART"
732              Buffered=false
733              Archived=false
734              Tared=true
735              # Look after the tar file we want if we did not found it already
736              if [ X${IsMatching} = X ] ; then
737                IGCM_sys_TestDirArchive ${RestartPath}/${RestartJobName}/RESTART
738                if [ $? ] ; then
739                  for PotentialTarFile in $( IGCM_sys_RshArchive "find ${RestartPath}/${RestartJobName}/RESTART -name "${RestartJobName}_*restart*.tar" -print" ) ; do
740                    IsMatching=$( echo ${PotentialTarFile##*/} | \
741                      sed "s:_restart::" | \
742                      sed "s:^${RestartJobName}_::" | \
743                      sed "s:\.tar$::" | \
744                      gawk -F_ -v restartdate=${Date_r} \
745                      '{if (($1 <= restartdate) && ($2 >= restartdate)) {print $1"_"$2}}' )
746                    if [ ! X${IsMatching} = X ] ; then
747                      TarFileFound=${PotentialTarFile}
748                      break
749                    fi
750                  done
751                fi
752                # Stop here if nothing has been found
753                if [ X${TarFileFound} = X ] ; then
754                  IGCM_debug_Print 3 "Restart files were not found!"
755                  IGCM_debug_Print 3 "Restart files have been searched for in buffer and archive directory."
756                  IGCM_debug_Print 3 "They have been searched for in packed and unpacked format."
757                  IGCM_debug_Exit "Please double check restart settings in config.card"
758                  IGCM_debug_Verif_Exit
759                fi
760              fi
761              IGCM_sys_PrepareTaredRestart ${TarFileFound}
762              TarFileLocation=$( basename ${TarFileFound} )
763              IGCM_debug_Print 1 "tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}"
764              tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}
765              nb_restart_file=$( IGCM_sys_CountFileBuffer ${comp}_${generic_restart_file_name_in}_????.${extension_in} )
766            fi
767
768            if [ ${nb_restart_file} -gt 1 ] ; then
769              j=0                                      # BASH LINE NOT NEEDED
770              # BASH for j4 in in $( eval echo {0000..$(( nb_restart_file - 1 ))} ) ; do
771              until [ $j -ge ${nb_restart_file} ]; do  # BASH LINE NOT NEEDED
772                j4=${j}                                # BASH LINE NOT NEEDED
773                if [ X${Buffered} = Xtrue ] ; then
774                  IGCM_sys_GetBuffer ${Path_OUT}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
775                elif [ X${Archived} = Xtrue ] ; then
776                  IGCM_sys_Get ${Path_OUT}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
777                elif [ X${Tared} = Xtrue ] ; then
778                  IGCM_sys_Mv ${comp}_${generic_restart_file_name_in}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
779                fi
780                (( j=j+1 ))                            #BASH LINE NOT NEEDED
781              done
782
783              if [ X${NUM_PROC_OCE} != X ] ; then
784                if [ ${NUM_PROC_OCE} -ne ${nb_restart_file} ] ; then
785                  IGCM_sys_rebuild ${generic_restart_file_name_out}.${extension_out} ${generic_restart_file_name_out}_????.${extension_out}
786                  IGCM_sys_Rm ${generic_restart_file_name_out}_????.${extension_out}
787                fi
788              fi
789            else
790              if [ X${Buffered} = Xtrue ] ; then
791                IGCM_sys_GetBuffer ${Path_r}/${file_in_Name} ${file_out}
792              elif [ X${Archived} = Xtrue ] ; then
793                IGCM_sys_Get ${Path_r}/${file_in_Name} ${file_out}
794              elif [ X${Tared} = Xtrue ] ; then
795                IGCM_sys_Mv ${comp}_${file_in_Name} ${file_out}
796              fi
797            fi
798            (( i=i+3 ))
799          done
800        else
801          if [ X${FileName0} != XNONE ] ; then
802            IGCM_debug_Exit "IGCM_comp_GetInputRestartFiles : No file in list for ${compname}."
803          else
804            IGCM_debug_Print 1 "IGCM_comp_GetInputRestartFiles : NONE specified in Restart List ${compname}."
805          fi
806        fi
807      fi
808    elif ( [ ${Period} -eq 1 ] && [ ${DRYRUN} -eq 0 ] ) ; then
809      # if not FirstInitialize and first loop of this job
810
811      # Restore Restarts files
812      #-----------------------
813      if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != XNONE ] ) ; then
814        eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
815
816        (( i=1 ))
817        until [ $i -gt $NbFiles ]; do
818          eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
819          eval file_in=${file_in_}
820          (( i_ = i+1 ))
821          eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
822          eval file_out=${file_out_}
823
824          file_in_Name=${config_UserChoices_JobName}_${LastPeriodDateEnd}_${file_in}
825
826          extension_in=$( echo ${file_in_Name##*.} )
827          extension_out=$( echo ${file_out##*.} )
828
829          generic_restart_file_name_in=$( basename ${file_in_Name} .${extension_in} )
830          generic_restart_file_name_out=$( basename ${file_out} .${extension_out} )
831
832          eval Path_BUF=\${R_BUF_${comp}_R}/${generic_restart_file_name_in}
833          eval Path_OUT=\${R_OUT_${comp}_R}/${generic_restart_file_name_in}
834
835          if [ $( IGCM_sys_TestFileBuffer ${Path_BUF}*.${extension_in} ; echo $? ) = 0 ] ; then
836            IGCM_debug_Print 3 "Buffered restart ${Path_BUF}*.${extension_in}"
837            Buffered=true
838            Archived=false
839            Tared=false
840            nb_restart_file=$(IGCM_sys_CountFileBuffer ${Path_BUF}_????.${extension_in})
841          elif [ $( IGCM_sys_TestFileArchive ${Path_OUT}*.${extension_in} ; echo $? ) = 0 ] ; then
842            IGCM_debug_Print 3 "Archived restart ${Path_OUT}*.${extension_in}"
843            Buffered=false
844            Archived=true
845            Tared=false
846            nb_restart_file=$(IGCM_sys_CountFileArchive ${Path_OUT}_????.${extension_in})
847          else
848            IGCM_debug_Print 3 "No restart file in the buffer nor in the archive directory"
849            IGCM_debug_Print 3 "Restart files will now be searched for in : ${R_SAVE}/RESTART"
850            Buffered=false
851            Archived=false
852            Tared=true
853            # Look after the tar file we want if we did not found it already
854            if [ X${IsMatching} = X ] ; then
855              for PotentialTarFile in $( IGCM_sys_RshArchive "find ${R_SAVE}/RESTART -name "${config_UserChoices_JobName}_*_restart.tar" -print" ) ; do
856                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}}' )
857                if [ ! X${IsMatching} = X ] ; then
858                  TarFileFound=${PotentialTarFile}
859                  break
860                fi
861              done
862            fi
863            # Stop here if nothing has been found
864            if [ X${TarFileFound} = X ] ; then
865              IGCM_debug_Print 3 "Restart files were not found!"
866              IGCM_debug_Print 3 "Restart files have been searched for in buffer and archive directory."
867              IGCM_debug_Print 3 "They have been searched for in packed and unpacked format."
868              IGCM_debug_Exit "Please double check restart settings in config.card"
869              IGCM_debug_Verif_Exit
870            fi
871            IGCM_sys_PrepareTaredRestart ${TarFileFound}
872            TarFileLocation=$( basename ${TarFileFound} )
873            IGCM_debug_Print 1 "tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}"
874            tar xvf ${TarFileLocation} ${comp}_${generic_restart_file_name_in}*.${extension_in}
875            nb_restart_file=$( IGCM_sys_CountFileBuffer ${comp}_${generic_restart_file_name_in}_????.${extension_in} )
876          fi
877
878          if [ ${nb_restart_file} -gt 1 ] ; then
879            j=0                                     # BASH LINE NOT NEEDED
880            #BASH for j4 in in $( eval echo {0000..$(( nb_restart_file - 1 ))} ) ; do
881            until [ $j -ge ${nb_restart_file} ]; do # BASH LINE NOT NEEDED
882              j4=${j}                               # BASH LINE NOT NEEDED
883              if [ X${Buffered} = Xtrue ] ; then
884                IGCM_sys_GetBuffer ${Path_BUF}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
885              elif [ X${Archived} = Xtrue ] ; then
886                IGCM_sys_Get ${Path_OUT}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
887              elif [ X${Tared} = Xtrue ] ; then
888                IGCM_sys_Mv ${comp}_${generic_restart_file_name_in}_${j4}.${extension_in} ${generic_restart_file_name_out}_${j4}.${extension_out}
889              fi
890              (( j=j+1 ))                           # BASH LINE NOT NEEDED
891            done
892          else
893            if [ X${Buffered} = Xtrue ] ; then
894              eval IGCM_sys_GetBuffer \${R_BUF_${comp}_R}/${file_in_Name} ${file_out}
895            elif [ X${Archived} = Xtrue ] ; then
896              eval IGCM_sys_Get \${R_OUT_${comp}_R}/${file_in_Name} ${file_out}
897            elif [ X${Tared} = Xtrue ] ; then
898              IGCM_sys_Mv ${comp}_${file_in_Name} ${file_out}
899            fi
900          fi
901          (( i=i+3 ))
902        done
903      else
904        if [ X${FileName0} != XNONE ] ; then
905          IGCM_debug_Exit "IGCM_comp_GetInputRestartFiles : No file in list for ${compname}."
906        else
907          IGCM_debug_Print 1 "IGCM_comp_GetInputRestartFiles : NONE specified in Restart List ${compname}."
908        fi
909      fi
910    fi
911  done
912
913  NbFiles=$( ls * 2> /dev/null | wc -l )
914  if [ ${NbFiles} -gt 0 ] ; then
915    IGCM_sys_Chmod u+rw *
916  fi
917
918  IGCM_debug_PopStack "IGCM_comp_GetInputRestartFiles"
919}
920
921#=======================================================================
922function IGCM_comp_PeriodStart
923{
924  IGCM_debug_PushStack "IGCM_comp_PeriodStart"
925
926  # Debug Print :
927  echo
928  IGCM_debug_Print 1 "IGCM_comp_PeriodStart"
929  echo
930
931  typeset ExeNameIn ExeNameOut
932  typeset comp compname comptagname
933  for comp in ${config_ListOfComponents[*]} ; do
934    # Define component
935    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
936    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
937
938    # Copy executable for this component
939    eval ExeNameIn=\${config_Executable_${comp}[0]}
940    eval ExeNameOut=\${config_Executable_${comp}[1]}
941
942    # Debug Print
943    IGCM_debug_Print 3 "PeriodStart ${compname} Driver Function (if any)."
944    # UPDATE component
945    ${comp}_PeriodStart 2> /dev/null
946
947  done
948
949  IGCM_debug_PopStack "IGCM_comp_PeriodStart"
950}
951
952#=======================================================================
953function IGCM_comp_modifyDefFile
954{
955#
956# syntax:     IGCM_comp_modifyDefFile  type  filein  key  [value]
957#
958# For example : IGCM_comp_modifyDefFile blocker run.def day_step 1200
959#
960# This function is used to modify a parameter file for a specific variable.
961# The file must be a ".def" file, i.e. with IOIPSL parameter file syntax.
962# This function can be used in the comp.driver files for the components.
963#
964# Arguments:
965# - type   : first argument must be blocker, nonblocker or force
966#            For "blocker" case, the variable must be attributed the keyworld AUTO
967#            otherwise this function will exit.
968#            For "nonblocker" case, the user can remove or modify the variable. For
969#            this case, as long as AUTO is not set, no modification will be done.
970#            For "force" case, the variable will be modified even if it is not set to AUTO
971# - filein : the file in run directory of .def type in which the variable should be set
972# - key    : the variable to modify
973# - value  : the value to set the key equal to, optional. If value is not set or if
974#            value=DEFAULT, then a default value must be given in filein using syntax :
975#            key= AUTO : DEFAULT=def_value
976#
977  IGCM_debug_PushStack "IGCM_comp_modifyDefFile"
978
979  # Set local variables and test the arguments
980  if [ $# = 4 ] ; then
981    # Normal case with 4 arguments
982    type=$1 ; filein=$2 ; key=$3 ; value=$4
983  elif [ $# = 3 ] ; then
984    # Normal case with 3 arguments
985    type=$1 ; filein=$2 ;       key=$3; value="DEFAULT"
986  else
987    IGCM_debug_Exit "IGCM_comp_modifyDefFile: Bad number of arguments."
988    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
989    return
990  fi
991  IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: $type $filein $key $value"
992
993
994  # Test if first argument is correct
995  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then
996    IGCM_debug_Exit "IGCM_comp_modifyDefFile: Error in first argument must be blocker, nonblocker or force"
997    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
998    return
999  fi
1000
1001  # Test if the file exist
1002  if [ ! -f ${filein} ] ; then
1003    IGCM_debug_Exit "IGCM_comp_modifyDefFile: $filein does not exist."
1004    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1005    return
1006  fi
1007
1008  # Define list of files to test using all files with suffix .def (except used*.def)
1009  filelist=$( ls *def | grep -v used )
1010
1011  # Count number of occurances for the key in all files
1012  nb_occ=$( grep -w $key $filelist |grep -v "#"  |wc -l )
1013
1014  # Test if key is set several times
1015  if [ $nb_occ -gt 1 ] ; then
1016    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $filein: Variable=$key is set ${nb_occ} times"
1017    IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1018    return
1019  fi
1020
1021  # Treatement according to different cases
1022  if [ $nb_occ -eq 0 ] && [ $type = blocker ] ; then
1023    # Stop if the key is never set and the function is blocker
1024    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $filein: Variable=$key has been removed but this function is blocker. "
1025    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore $filein for variable $key."
1026  elif [ $nb_occ -eq 0 ] && [ $type = nonblocker ] ; then
1027    # The key is not set but it is a nonblocker call so nothing is done.
1028    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key is not set in $filein. This is a nonblocker call so nothing is done."
1029    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for $key from the model will be used."
1030    modify=no
1031  elif [ $( grep $key $filein | grep -v "\#"  |wc -l ) = 0 ] ; then
1032    # Variable key is not set in filein, stop.
1033    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable $key is not set in correct file. It should be set in $filein."
1034  fi
1035 
1036
1037  # Check if AUTO is set in the filein on the same line as the key variable
1038  if [ $( grep -w $key $filein | grep -v "\#" | grep AUTO | wc -l ) = 1 ] ; then
1039      # Modification will be done for all cases
1040      modify=yes
1041  else
1042      # The variable was not set to AUTO
1043      if [ $type = blocker ] ; then
1044          # Exit because this is a blocker call
1045          IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $key cannot be modified. It should be set to AUTO."
1046          return
1047       elif [ $type = nonblocker ] ; then
1048          # Do nothing. Suppose that the user did set the variable correct
1049          IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key is set by the user. Nothing done."
1050          modify=no
1051       elif [ $type = force ] ; then
1052          # Force modification
1053          IGCM_debug_Print 1 "IGCM_comp_modifyDefFile : Variabl=$key was not set to AUTO. Modification will be forced."
1054          modify=yes
1055      fi
1056  fi
1057
1058  # Do the modifcation now
1059  if [ $modify = yes ] ; then
1060
1061      # For option DEFAULT, read default value from file.
1062      if [ X"$value" = XDEFAULT ] || [ X"$value" = X ] ; then
1063        # Case to set DEFAULT value
1064        # Read default value from filein
1065        value=$( grep $key $filein | grep -v "\#" | awk  -F"DEFAULT *=" '{print $2}')
1066
1067        if [ X"$value" = X ] ; then
1068          IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $key needs a DEFAULT value in $filein."
1069          IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in $filein should be:"
1070          IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key=_AUTO_:DEFAULT=def_value"
1071          IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1072          return
1073        fi
1074      fi
1075
1076      # Now change key in filein
1077      sed -e "s/^${key}\ *=.*/${key}= ${value}/" $filein > $filein.tmp
1078      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In $filein set $key=$value"
1079      \mv $filein.tmp $filein
1080  fi
1081  IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
1082}
1083
1084
1085#=======================================================================
1086function IGCM_comp_modifyNamelist
1087{
1088#
1089# syntax:     IGCM_comp_modifyNamelist  type  filein  key  [value]
1090#
1091# For example : IGCM_comp_modifyNamelist blocker run.def day_step 1200
1092#
1093# This function is used to modify a parameter file for a specific variable.
1094# The file must be a ".def" file, i.e. with IOIPSL parameter file syntax.
1095# This function can be used in the comp.driver files for the components.
1096#
1097# Arguments:
1098# - type   : first argument must be blocker, nonblocker or force
1099#            For "blocker" case, the variable must be attributed the keyworld AUTO
1100#            otherwise this function will exit.
1101#            For "nonblocker" case, the user can remove or modify the variable. For
1102#            this case, as long as AUTO is not set, no modification will be done.
1103#            For "force" case, the variable will be modified even if it is not set to AUTO
1104# - filein : the file in run directory of .def type in which the variable should be set
1105# - key    : the variable to modify
1106# - value  : the value to set the key equal to, optional. If value is not set or if
1107#            value=DEFAULT, then a default value must be given in filein using syntax :
1108#            key= AUTO : DEFAULT=def_value
1109#
1110  IGCM_debug_PushStack "IGCM_comp_modifyNamelist"
1111
1112  # Set local variables and test the arguments
1113  if [ $# = 4 ] ; then
1114    # Normal case with 4 arguments
1115    type=$1 ; filein=$2 ; key=$3 ; value=$4
1116  elif [ $# = 3 ] ; then
1117    # Normal case with 3 arguments
1118    type=$1 ; filein=$2 ;       key=$3; value="DEFAULT"
1119  else
1120    IGCM_debug_Exit "IGCM_comp_modifyNamelist: Bad number of arguments."
1121    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1122    return
1123  fi
1124  IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: $type $filein $key $value"
1125
1126
1127  # Test if first argument is correct
1128  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then
1129    IGCM_debug_Exit "IGCM_comp_modifyNamelist: Error in first argument must be blocker, nonblocker or force"
1130    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1131    return
1132  fi
1133
1134  # Test if the file exist
1135  if [ ! -f ${filein} ] ; then
1136    IGCM_debug_Exit "IGCM_comp_modifyNamelist: $filein does not exist."
1137    IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1138    return
1139  fi
1140
1141  # Read the line with key in the file without the comments
1142  pattern=$( grep "^ *$key *=" $filein | sed -e "s% *\!.*%%" )
1143 
1144  # Verify the existance of the pattern
1145  if [ X"$pattern" = X ] ; then
1146      # Variable key is not set in filein, stop.
1147      IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable $key is not set in correct file. It should be set in $filein."
1148      IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1149      return
1150  fi
1151 
1152  # Check if the variable is set to AUTO in the filein
1153  if [ $( echo $pattern | grep AUTO | wc -l ) = 1 ] ; then
1154      # Modification will be done for all cases
1155      modify=yes
1156  else
1157      # The variable was not set to AUTO
1158      if [ $type = blocker ] ; then
1159          # Exit because this is a blocker call
1160          IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $key cannot be modified. It should be set to AUTO."
1161          IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1162          return
1163       elif [ $type = nonblocker ] ; then
1164          # Do nothing. Suppose that the user did set the variable correct
1165          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $key is set by the user. Nothing done."
1166          modify=no
1167       elif [ $type = force ] ; then
1168          # Force modification
1169          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=$key was not set to AUTO. Modification will be forced."
1170          modify=yes
1171      fi
1172  fi
1173
1174  # Do the modifcation now
1175  if [ $modify = yes ] ; then
1176
1177      # For option DEFAULT, read default value from file.
1178      if [ X"$value" = XDEFAULT ] || [ X"$value" = X ] ; then
1179        # Case to set DEFAULT value
1180        # Read default value from filein
1181        value=$( echo $pattern | awk  -F"DEFAULT *=" '{print $2}')
1182
1183        if [ X"$value" = X ] ; then
1184          IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $key needs a DEFAULT value in $filein."
1185          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in $filein should be:"
1186          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $key=_AUTO_:DEFAULT=def_value"
1187          IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1188          return
1189        fi
1190      fi
1191
1192      # Now change key in filein
1193      sed -e "s/${pattern}/       $key=${value}/" $filein > $filein.tmp
1194      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In $filein set $key=$value"
1195      \mv $filein.tmp $filein
1196  fi
1197  IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
1198}
1199
1200#=======================================================================
1201function IGCM_comp_modifyXmlFile
1202{
1203#
1204# syntax:     IGCM_comp_modifyXmlFile  type  filein  keyid  keyattrib  value
1205#
1206# For example : IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 enabled .TRUE.
1207#          or   IGCM_comp_modifyXmlFile blocker iodef.xml using_server NONE false
1208#
1209# This function is used to modify the value for a specific attribute and variable id.
1210# The file must be a of xml syntax.
1211# This function can be used in the comp.driver files for the components.
1212#
1213# Arguments:
1214# - type      : first argument must be blocker, nonblocker or force.
1215#               For "blocker" case, the variable must be attributed the keyworld AUTO
1216#               otherwise this function will exit.
1217#               For "nonblocker" case, the user can remove or modify the variable. For
1218#               this case, as long as AUTO is not set, no modification will be done.
1219#               For "force" case, the variable will be modified even if it is not set to AUTO
1220# - filein    : the file in run directory of .xml type in which the variable should be set
1221# - keyid     : the variable to modify
1222# - keyattrib : the attribute name to modify. If NONE, then the variable itself will be modified
1223# - value     : the value to set in the filein
1224#
1225  IGCM_debug_PushStack "IGCM_comp_modifyXmlFile"
1226
1227  # Set local variables and test the arguments
1228  if [ $# = 5 ] ; then
1229    # Normal case with 4 arguments
1230    type=$1 ; filein=$2 ; keyid=$3 ; keyattrib=$4 ; value=$5
1231  else
1232    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Bad number of arguments."
1233    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1234    return
1235  fi
1236  IGCM_debug_Print 1 "Entering IGCM_comp_modifyXmlFile with arguments: type=$type file=$filein, id=$keyid attribute=$keyattrib, value=$value"
1237
1238  # Test if first argument is correct
1239  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then
1240    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Error in first argument must be blocker, nonblocker or force"
1241    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1242    return
1243  fi
1244
1245  # Test if the file exist
1246  if [ ! -f ${filein} ] ; then
1247    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: $filein does not exist."
1248    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1249    return
1250  fi
1251
1252  # Test if keyid is set in filein, otherwise exit
1253  if [ $( grep -w $keyid $filein | wc -l ) = 0 ] ; then
1254    # Variable key is not set in filein, stop.
1255    IGCM_debug_Exit "IGCM_comp_modifyXmlFile : $keyid is not set in the file. Bad syntax of $filein file."
1256    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1257    return
1258  fi
1259
1260  # Check if AUTO is set on the same line as keyid and keyattrib
1261  if [  $( grep -w $keyid $filein | grep AUTO | wc -l ) = 1 ] ; then
1262    # Modifification will be done
1263    modify=yes
1264  else
1265    if [ $type = blocker ] ; then
1266      # Exit, the variable must be set to AUTO
1267      IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The $keyattrib for $keyid must be set to AUTO in $filein."
1268      IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1269      return
1270    elif [ $type = nonblocker ] ; then
1271      # Nothing will be done
1272      IGCM_debug_Print 1 "Nonblocker nothing is done for ${filein}, id=${keyid} and attribute ${keyattrib}"
1273      modify=no
1274    elif [ $type = force ] ; then
1275      # Force modification
1276      IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=$keyattrib for id=$keyid was not set to AUTO. Modification will be forced."
1277      modify=yes
1278    fi
1279  fi
1280
1281  # Do the modifcation now
1282  if [ $modify = yes ] ; then
1283    if [ $keyattrib = NONE ] ; then
1284      # Case to modify the variable itself
1285      IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting the variable=${value}"
1286      sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" $filein > $filein.tmp
1287    else
1288          # Check if keyattrib is set on the same line as keyid
1289        if [  $( grep -w $keyid $filein | grep $keyattrib | wc -l ) = 1 ] ; then
1290            # Case to modify the attribute value
1291            IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting attribute to ${keyattrib}=${value}"
1292            sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" $filein > $filein.tmp
1293        else
1294            # Case to add the attribute and its value
1295             IGCM_debug_Print 1 "Now add in ${filein} for id=${keyid} the attribute ${keyattrib} to the value ${value}"
1296             sed -e "/id=\"${keyid}\"/s/\/>/ ${keyattrib}=\"${value}\"\/>/" $filein > $filein.tmp
1297        fi
1298    fi
1299    \mv $filein.tmp $filein
1300  fi
1301  IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
1302}
1303
1304#=======================================================================
1305function IGCM_comp_Update
1306{
1307  IGCM_debug_PushStack "IGCM_comp_Update"
1308
1309    # Debug Print :
1310  echo
1311  IGCM_debug_Print 1 "IGCM_comp_Update"
1312  echo
1313
1314  typeset ExeNameIn ExeNameOut
1315  typeset comp compname comptagname
1316  for comp in ${config_ListOfComponents[*]} ; do
1317    # Define component
1318    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
1319    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
1320
1321    # Copy executable for this component
1322    eval ExeNameIn=\${config_Executable_${comp}[0]}
1323    eval ExeNameOut=\${config_Executable_${comp}[1]}
1324
1325    # If missing executable and DRYRUN is set to 0 or 1  then stop!
1326    if [ ${DRYRUN} -le 1 ] && [ X${ExeNameIn} != X\"\" ] &&  [ ! -f ${R_EXE}/${ExeNameIn} ] ; then
1327      IGCM_debug_Exit "IGCM_comp_Update missing executable ${ExeNameIn}"
1328    fi
1329
1330    if [ ${Period} -eq 1 ] && [ -f ${R_EXE}/${ExeNameIn} ] ; then
1331      eval IGCM_sys_Cp ${R_EXE}/${ExeNameIn} ${ExeNameOut}
1332      if [ -f ${RUN_DIR}/${ExeNameOut} ] ; then
1333        eval IGCM_sys_Chmod +rx ${RUN_DIR}/${ExeNameOut}
1334      fi
1335    elif [ -f ${R_EXE}/${ExeNameIn} ] && [ ! -f ${RUN_DIR}/${ExeNameOut} ] ; then
1336      eval IGCM_sys_Cp ${R_EXE}/${ExeNameIn} ${ExeNameOut}
1337      if [ -f ${RUN_DIR}/${ExeNameOut} ] ; then
1338        eval IGCM_sys_Chmod +rx ${RUN_DIR}/${ExeNameOut}
1339      fi
1340    fi
1341
1342    # Debug Print
1343    IGCM_debug_Print 1 "Update ${compname} Parameter Files."
1344    # UPDATE component
1345    ${comp}_Update
1346
1347  done
1348
1349  IGCM_debug_PopStack "IGCM_comp_Update"
1350}
1351
1352#=======================================================================
1353function IGCM_comp_Finalize
1354{
1355  IGCM_debug_PushStack "IGCM_comp_Finalize"
1356
1357  # Debug Print :
1358  echo
1359  IGCM_debug_Print 1 "IGCM_comp_Finalize"
1360  echo
1361
1362  typeset ListTextName TextName0
1363  typeset comp compname comptagname card ListFilesName FileName0 NbFiles SaveOnArchive
1364  typeset i i_ file_in file_in_ file_out file_out_ file_outin file_outin_ generic_file_name nb_rebuild_file
1365  typeset -Z4 j4
1366  #BASH declare j4
1367  typeset list_file nlist_file
1368  typeset compactoutputs
1369
1370  compactoutputs=false
1371  if [ X${JobType} != XRUN ] ; then
1372    compactoutputs=true
1373  elif [ X${config_UserChoices_CompactText} != Xn ] ; then
1374    compactoutputs=true
1375  fi
1376
1377  # Prepare headers for the shell dedicated to offline rebuild
1378  if [ X${AsynchronousRebuild} = Xtrue ] ; then
1379    [ ! -d ${RUN_DIR}/REBUILD_${PeriodDateBegin} ] && IGCM_sys_Mkdir ${RUN_DIR}/REBUILD_${PeriodDateBegin}
1380    if [ ${DRYRUN} -le 1 ] ; then
1381      echo "#!/bin/ksh                                        " >  ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1382      echo "function IGCM_FlushRebuild                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1383      echo "{                                                 " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1384      echo "IGCM_debug_PushStack \"IGCM_FlushRebuild\"        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1385      echo "echo                                              " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1386      echo "IGCM_debug_Print 1 \"IGCM_FlushRebuild\"          " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1387      echo "echo                                              " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1388      echo "export R_SAVE=${R_SAVE}                           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1389      echo "export R_BUFR=${R_BUFR}                           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1390      echo "export R_OUT_KSH=${R_OUT_KSH}                     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1391      echo "export R_BUF_KSH=${R_BUF_KSH}                     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1392      echo "export config_UserChoices_JobName=${config_UserChoices_JobName}     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1393      echo "export config_UserChoices_SpaceName=${config_UserChoices_SpaceName} " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1394    fi
1395  fi
1396
1397  for comp in ${config_ListOfComponents[*]} ; do
1398    # Define component
1399    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
1400    eval comptagname=\${config_ListOfComponents_${comp}[1]} > /dev/null 2>&1
1401
1402    # Debug Print
1403    IGCM_debug_Print 1 "Finalize ${comp} : ${compname} component."
1404    # FINALIZE component
1405    ${comp}_Finalize
1406
1407    card=${SUBMIT_DIR}/COMP/${compname}.card
1408
1409    # Save Output Text files of models
1410    #---------------------------------
1411    IGCM_debug_Print 2 "Save Output Text files for ${comp} : ${compname} component."
1412    IGCM_card_DefineArrayFromOption ${card} OutputText List
1413    ListTextName=${compname}_OutputText_List
1414
1415    eval TextName0=\${${ListTextName}[0]} > /dev/null 2>&1
1416    if [ X${TextName0} != X${NULL_STR} ] ; then
1417      eval NbFiles=\${#${ListTextName}[@]} > /dev/null 2>&1
1418
1419      (( i=0 ))
1420      until [ $i -eq $NbFiles ]; do
1421        eval file_in=\${${ListTextName}[$i]} > /dev/null 2>&1
1422        eval file_out=${PREFIX}_${file_in}
1423
1424        (( i=i+1 ))
1425
1426        unset list_file
1427        #set +A list_file -- $( ls ${file_in}* | sort 2>/dev/null )
1428        # result for a a1 a10 a2 with file_in=a a a1 a2 a10
1429        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 )
1430        nlist_file=${#list_file[@]}
1431        if [ ${nlist_file} -gt 1 ] ; then
1432          if ( ${compactoutputs} ) ; then
1433            IGCM_debug_Print 2 "Parallelism of Text Output with ${nlist_file} files."
1434            IGCM_debug_Print 2 "Compact files in ${file_out} : " ${list_file[*]}
1435            echo ${list_file[*]} > ${file_out}
1436            echo "" >> ${file_out}
1437
1438            (( i_ = 0 ))
1439            for file in ${list_file[@]} ; do
1440              echo "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " >> ${file_out}
1441              echo "| " ${i_} " " ${file} >> ${file_out}
1442              echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " >> ${file_out}
1443              cat ${file} | sed "s/\(.*\)/${i_}\1/" >> ${file_out}
1444              echo "" >> ${file_out}
1445              eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file}" > /dev/null 2>&1
1446                  (( i_ = i_ + 1 ))
1447            done
1448            if ( ${ExecutionFail} ) ; then
1449              IGCM_sys_Cp ${file_out} ${SUBMIT_DIR}/Debug
1450            fi
1451
1452            if [ X${Pack} = Xtrue ] ; then
1453              eval IGCM_sys_PutBuffer_Out ${file_out} \${R_BUF_${comp}_D}/${file_out}
1454            else
1455              eval IGCM_sys_Put_Out ${file_out} \${R_OUT_${comp}_D}/${file_out}
1456            fi
1457
1458            eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file_out}" > /dev/null 2>&1
1459          else
1460            for file in ${list_file[@]} ; do
1461              if ( ${ExecutionFail} ) ; then
1462                IGCM_sys_Cp ${file} ${SUBMIT_DIR}/Debug/${PREFIX}_${file}
1463              fi
1464
1465              if [ X${Pack} = Xtrue ] ; then
1466                eval IGCM_sys_PutBuffer_Out ${file} \${R_BUF_${comp}_D}/${PREFIX}_${file}
1467              else
1468                eval IGCM_sys_Put_Out ${file} \${R_OUT_${comp}_D}/${PREFIX}_${file}
1469              fi
1470
1471              eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file}" > /dev/null 2>&1
1472            done
1473          fi
1474        else
1475          if ( [ -f ${file_in}_0000 ] || [ -f ${file_in}0 ] ) ; then
1476            eval IGCM_sys_Mv ${file_in}* ${file_in}
1477          fi
1478
1479          if ( ${ExecutionFail} ) ; then
1480            IGCM_sys_Cp ${file_in} ${SUBMIT_DIR}/Debug/${file_out}
1481          fi
1482
1483          if [ X${Pack} = Xtrue ] ; then
1484            eval IGCM_sys_PutBuffer_Out ${file_in} \${R_BUF_${comp}_D}/${file_out}
1485          else
1486            eval IGCM_sys_Put_Out ${file_in} \${R_OUT_${comp}_D}/${file_out}
1487          fi
1488          eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file_in}" > /dev/null 2>&1
1489        fi
1490      done
1491    fi
1492
1493    # Save Restarts files
1494    #--------------------
1495    IGCM_debug_Print 2 "Save Restart files for ${comp} : ${compname} component."
1496    IGCM_card_DefineArrayFromOption ${card} RestartFiles List
1497    ListFilesName=${compname}_RestartFiles_List
1498    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
1499
1500    if ( [ X${FileName0} != X${NULL_STR} ] && [ X${FileName0} != XNONE ] ) ; then
1501      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
1502
1503      (( i=0 ))
1504      until [ $i -ge $NbFiles ]; do
1505        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
1506        eval file_in=${file_in_}
1507
1508        (( i_ = i+1 ))
1509        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1510        eval file_out=${file_out_}
1511
1512        (( i_ = i+2 ))
1513        eval file_outin_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1514        eval file_outin=${file_outin_}
1515
1516        generic_restart_file_name_in=$(    basename ${file_in} .nc )
1517        generic_restart_file_name_out=$(   basename ${config_UserChoices_JobName}_${PeriodDateEnd}_${file_out} .nc )
1518        generic_restart_file_name_outin=$( basename ${file_outin} .nc )
1519
1520        nb_restart_file=$( ls ${generic_restart_file_name_in}_????.nc 2>/dev/null | wc -l )
1521        if [ ${nb_restart_file} -gt 1 ] ; then
1522          j=0                                     # BASH LINE NOT NEEDED
1523          # BASH for j4 in in $( eval echo {0000..$(( nb_restart_file - 1 ))} ) ; do
1524          until [ $j -ge ${nb_restart_file} ]; do # BASH LINE NOT NEEDED
1525            j4=${j}                               # BASH LINE NOT NEEDED
1526            if [ X${Pack} = Xtrue ] ; then
1527              eval IGCM_sys_PutBuffer_Rest ${generic_restart_file_name_in}_${j4}.nc \${R_BUF_${comp}_R}/${generic_restart_file_name_out}_${j4}.nc
1528            else
1529              eval IGCM_sys_Put_Rest ${generic_restart_file_name_in}_${j4}.nc \${R_OUT_${comp}_R}/${generic_restart_file_name_out}_${j4}.nc
1530            fi
1531            if [ ! ${file_in} = ${file_outin} ] ; then
1532              if ( ${ExitFlag} ) ; then
1533                echo "IGCM_sys_Mv ${generic_restart_file_name_in}_${j4}.nc ${generic_restart_file_name_outin}_${j4}.nc not executed."
1534              else
1535                IGCM_sys_Mv ${generic_restart_file_name_in}_${j4}.nc ${generic_restart_file_name_outin}_${j4}.nc
1536              fi
1537            fi
1538            (( j=j+1 ))                           # BASH LINE NOT NEEDED
1539          done
1540        else
1541          if [ X${Pack} = Xtrue ] ; then
1542            eval IGCM_sys_PutBuffer_Rest ${file_in} \${R_BUF_${comp}_R}/${config_UserChoices_JobName}_${PeriodDateEnd}_${file_out}
1543          else
1544            eval IGCM_sys_Put_Rest ${file_in} \${R_OUT_${comp}_R}/${config_UserChoices_JobName}_${PeriodDateEnd}_${file_out}
1545          fi
1546          if [ ! ${file_in} = ${file_outin} ] ; then
1547            if ( ${ExitFlag} ) ; then
1548              echo "IGCM_sys_Mv ${file_in} ${file_outin} not executed."
1549            else
1550              IGCM_sys_Mv ${file_in} ${file_outin}
1551            fi
1552          fi
1553        fi
1554
1555        (( i=i+3 ))
1556      done
1557    else
1558      if [ X${FileName0} != XNONE ] ; then
1559        IGCM_debug_Exit "IGCM_comp_Finalize : No file in restart list for ${compname}."
1560      else
1561        IGCM_debug_Print 1 "IGCM_comp_Finalize : NONE specified in Restart List ${compname}."
1562      fi
1563    fi
1564
1565    # Save Output files
1566    #------------------
1567    IGCM_debug_Print 2 "Save Output files for ${comp} : ${compname} component."
1568    IGCM_card_DefineArrayFromOption ${card} OutputFiles List
1569    ListFilesName=${compname}_OutputFiles_List
1570    eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
1571
1572    if [ X${FileName0} != X${NULL_STR} ] ; then
1573      eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
1574
1575      (( i=0 ))
1576      until [ $i -ge $NbFiles ]; do
1577        SaveOnArchive=true
1578        eval file_in_=\${${ListFilesName}[$i]} > /dev/null 2>&1
1579        eval file_in=${file_in_}
1580        (( i_ = i+1 ))
1581        eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1582        eval file_out=${file_out_}
1583        #
1584        # Override file_out path remplacing R_SAVE by R_BUFR
1585        #
1586        if [ X${Pack} = Xtrue ] ; then
1587          file_out=$( echo $file_out | sed "s:^$R_SAVE:$R_BUFR:" )
1588        fi
1589        #
1590        # Not necessarily the best option. /!\ Potential side effects /!\
1591        #
1592        (( i_ = i+2 ))
1593        eval flag_post=\${${ListFilesName}[$i_]} > /dev/null 2>&1
1594        #
1595        generic_file_name=$( basename ${file_in} .nc )
1596        nb_rebuild_file=$( ls | grep "^${generic_file_name}_[0-9]*.nc" | wc -l )
1597        #
1598        if ( [ ${nb_rebuild_file} -eq 1 ] && [ -f ${generic_file_name}_0000.nc ] ) ; then
1599          IGCM_debug_Print 2 "Parallelism with 1 file. Rebuilding ${file_in} not needed"
1600          IGCM_sys_Mv ${generic_file_name}_0000.nc ${file_in}
1601        elif [ ${nb_rebuild_file} -gt 1 ] ; then
1602          IGCM_debug_Print 2 "Parallelism detected and rebuilding ${file_in} is needed"
1603          if [ X${AsynchronousRebuild} = Xfalse ] ; then
1604            IGCM_debug_Print 2 "Rebuilding ${file_in} online"
1605            IGCM_sys_rebuild ${file_in} ${generic_file_name}_[0-9]*.nc
1606          else
1607            IGCM_debug_Print 2 "Preparing offline rebuild for ${file_in}"
1608            IGCM_sys_Mv ${generic_file_name}_[0-9]*.nc ${RUN_DIR}/REBUILD_${PeriodDateBegin}
1609
1610            # Prepare the shell dedicated to offline rebuild
1611            if [ $DRYRUN -le 1 ]; then
1612              if [ ${file_in} = histstn.nc ] ; then
1613                echo "IGCM_sys_rebuild_station ${file_in} ${generic_file_name}_*.nc" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1614              else
1615                echo "IGCM_sys_rebuild ${file_in} ${generic_file_name}_[0-9]*.nc" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1616              fi
1617              echo "IGCM_debug_Verif_Exit_Post" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1618            fi
1619            #
1620            # Load Patch we need to apply and apply
1621            if [ $DRYRUN -le 1 ]; then
1622              if [ X$( eval echo \${${compname}_${flag_post}_Patches[0]} ) !=  X${NULL_STR} ]; then
1623                for Patch in $( eval echo \${${compname}_${flag_post}_Patches[*]} ) ; do
1624                  echo ". ${libIGCM_POST}/libIGCM_post/IGCM_${Patch}.ksh" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1625                  echo "IGCM_${Patch} ${file_in}                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1626                  echo "IGCM_debug_Verif_Exit_Post                      " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1627                done
1628              fi
1629            fi
1630            #
1631            if [ $DRYRUN -le 1 ]; then
1632              if [ X${Pack} = Xtrue ] ; then
1633                echo "IGCM_sys_PutBuffer_Out ${file_in} ${file_out}     " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1634              else
1635                echo "IGCM_sys_Put_Out ${file_in} ${file_out}           " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1636              fi
1637              echo "IGCM_debug_Verif_Exit_Post                          " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1638              echo "IGCM_sys_Rm ${generic_file_name}_[0-9]*.nc          " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1639            fi
1640            SaveOnArchive=false
1641          fi
1642        fi
1643        #
1644        if [ ${SaveOnArchive} = true ] ; then
1645          #
1646          # Rebuild has been done online or it was not needed
1647          #
1648          # If we need to apply a patch we use TMP DIRECTORY before ARCHIVING if asynchronous rebuild is on
1649          #
1650          thereisapatch=$( eval echo \${${compname}_${flag_post}_Patches[0]} )
1651          if ( [ ! X${thereisapatch} = X${NULL_STR} ] && [ X${AsynchronousRebuild} = Xtrue ] && [ -f ${file_in} ] ) ; then
1652            IGCM_sys_Mv ${file_in} ${RUN_DIR}/REBUILD_${PeriodDateBegin}
1653            eval FileToBeDeleted[${#FileToBeDeleted[@]}]=REBUILD_${PeriodDateBegin}/${file_in} > /dev/null 2>&1
1654            #
1655            if [ $DRYRUN -le 1 ]; then
1656              for Patch in $( eval echo \${${compname}_${flag_post}_Patches[*]} ) ; do
1657                echo ". ${libIGCM_POST}/libIGCM_post/IGCM_${Patch}.ksh" >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1658                echo "IGCM_${Patch} ${file_in}                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1659                echo "IGCM_debug_Verif_Exit_Post                      " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1660              done
1661              #
1662              if [ X${Pack} = Xtrue ] ; then
1663                echo "IGCM_sys_PutBuffer_Out ${file_in} ${file_out}   " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1664              else
1665                echo "IGCM_sys_Put_Out ${file_in} ${file_out}         " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1666              fi
1667              echo "IGCM_debug_Verif_Exit_Post                        " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
1668              #
1669            fi
1670          else
1671            #
1672            # No Patch, No Asynchronous rebuild, online rebuild has been done or was not needed
1673            #
1674            if [ X${Pack} = Xtrue ] ; then
1675              IGCM_sys_PutBuffer_Out ${file_in} ${file_out}
1676            else
1677              IGCM_sys_Put_Out ${file_in} ${file_out}
1678            fi
1679            eval FileToBeDeleted[${#FileToBeDeleted[@]}]="${file_in}" > /dev/null 2>&1
1680            if [ ${nb_rebuild_file} -gt 1 ] ; then
1681              for DelFile in $( ls | grep "${generic_file_name}[_0-9]*.nc" ) ; do
1682                eval FileToBeDeleted[${#FileToBeDeleted[@]}]=${DelFile} > /dev/null 2>&1
1683              done
1684            fi
1685          fi
1686        fi
1687        (( i=i+3 ))
1688      done
1689    fi
1690    echo
1691  done
1692  IGCM_debug_PopStack "IGCM_comp_Finalize"
1693}
Note: See TracBrowser for help on using the repository browser.