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

Last change on this file since 1338 was 1338, checked in by sdipsl, 8 years ago

Call xios_parser.py and ensure field_compname.xml file and file_def_*_companme.xml are coherent. See #256

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