source: modipsl/trunk/util/ins_job @ 2246

Last change on this file since 2246 was 2246, checked in by sdipsl, 10 years ago

support more ensemble type

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 10.6 KB
Line 
1#!/bin/ksh
2# $Id$
3#---------------------------------------------------------------------
4#- Installation of jobs according to an environment
5#---------------------------------------------------------------------
6function ins_job_Usage
7{
8print - "
9ins_job installs the jobs in the directories
10which contain a file config.card
11
12ins_job must be launched on the host
13on which the job will be submitted
14
15Usage :
16  ${b_n} [-h] [-v] [-e] [-p project]
17
18Options :
19  -h          : help
20  -v          : verbose mode
21  -e          : turn on ensemble mode (hindcast/forecast or date restart)
22  -p project  : add default project on curie
23"
24}
25function ins_job_Warning
26{
27   [[ ${x_v} = 'verbose' ]] && print - "\n############### WARNING ###############";
28   [[ ${x_v} = 'verbose' ]] && print - "File ${n_f} already exists\nin directory ${j}";
29   [[ ${x_v} = 'verbose' ]] && print - "You must delete this file to update !";
30}
31
32function ins_job_Check_JobName
33{
34  verif=${JobName##[a-zA-Z]*(?([.\-])[a-zA-Z0-9])}
35
36  if [ ${#verif} -ne 0 ] ; then
37    echo "################ ERROR ################"
38    echo "${JobName} is invalid."
39    echo "- JobName can only contain alphanumeric characters, \".\" and \"-\""
40    echo "- JobName must start with a letter"
41
42    ((NbErr=NbErr+1))
43
44    Status=1
45  else
46    Status=0
47  fi
48
49  return ${Status}
50}
51
52#-
53#     dirname     and      basename
54#-
55d_n=$(dirname ${0}); b_n=$(basename ${0});
56#-
57# Retrieving and validation of the options
58#-
59x_v='silencious';
60x_e=false;
61x_p=false;
62while getopts :hvep: V ; do
63  case $V in
64  (h)  ins_job_Usage; exit 0;;
65  (v)  x_v='verbose';;
66  (e)  x_e=true;;
67  (p)  x_p=true
68       ProjectID=${OPTARG} ;;
69  (:)  echo ${b_n}" : -"${OPTARG}" option : missing value" 1>&2;
70       exit 2;;
71  (\?) echo ${b_n}" : -"${OPTARG}" option : not supported" 1>&2;
72       exit 2;;
73  esac
74done
75[ ${x_v} = 'silencious' ] && export DEBUG_sys=false
76shift $(($OPTIND-1));
77#-
78# Define working files
79#-
80F_MOD=$(cd ${d_n}'/..';/bin/pwd;)
81# [[ ${F_MOD##*/} != 'modipsl' ]] && \
82#  { print - "directory 'modipsl' unreachable"; exit 3; }
83W_W=${d_n}'/../libIGCM'
84[[ ! -d ${W_W} ]] && { print - "${W_W} unreachable"; exit 3; }
85libIGCM=$(cd ${W_W};/bin/pwd;)
86F_JOB=${libIGCM}'/AA_job';
87[[ ! -f ${F_JOB} ]] && { print - "${F_JOB} unreachable"; exit 3; }
88F_RCI=${libIGCM}'/run.card.init';
89[[ ! -f ${F_RCI} ]] && { print - "${F_RCI} unreachable"; exit 3; }
90#-
91# Host Name
92#-
93x_t=$(${d_n}/w_i_h) 2>/dev/null;
94{ [[ ${?} != 0 ]] && \
95   { print - "\nw_i_h or uname unreachable\n" 1>&2; exit 1; }; } || \
96  [[ ${x_t} = "Unknown" ]] && \
97   { print - "\nLocal_Host not supported\n" 1>&2; exit 1; };
98#-
99W_W=$(sed -n -e "s/^#-Q-  *\([^ ]*\).*$/\1/p" ${F_JOB} | \
100       sort -u | sed -e "/${x_t}/!d");
101[[ '\?'"${W_W}" != '\?'${x_t} ]] && \
102  {
103   print - "\nHost  "${x_t}"  not supported" 1>&2;
104   print - "'default' will be used" 1>&2;
105   x_t='default'
106  }
107#-
108[[ ${x_v} = 'verbose' ]] && \
109 {
110  print - "";
111  print - '--- Host        : '${x_t};
112  print - '--- modipsl     : '${F_MOD};
113  print - '--- libIGCM     : '${libIGCM};
114  print - '--- basic job   : '${F_JOB};
115  print - '--- basic card  : '${F_RCI};
116 }
117#-
118[[ ${x_v} = 'verbose' ]] && print - "\nInstallation of jobs for '${x_t}'";
119#-
120# Accessing to functions (without stack)
121#-
122DEBUG_debug=false
123MirrorlibIGCM=false
124TaskType=computing
125. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh
126. ${libIGCM}/libIGCM_card/libIGCM_card.ksh
127. ${libIGCM}/libIGCM_date/libIGCM_date.ksh
128. ${libIGCM}/libIGCM_sys/libIGCM_sys.ksh
129if [ $x_e = 'true' ] ; then
130  . ${libIGCM}/libIGCM_ensemble/libIGCM_ensemble.ksh
131fi
132
133NbErr=0
134
135#-
136# Define Project ID to set up jobs header for Curie (TGCC)
137#-
138if ( ! ${x_p} ) ; then
139  if [ X"${x_t}" == "Xcurie" ] ; then
140    ProjectID=$( ccc_myproject | gawk '{ if ( $3 ~ /^project$/ && $4 !~ /^tgcc/ ) { print $4 } }' | head -n 1 )
141    ProjectID=${ProjectID:="gen2211"}
142    answer=""
143    print - "Hit Enter or give project ID (default is ${ProjectID}), possible projects are $( echo $( ccc_myproject | gawk '{ if ( $3 ~ /^project$/ && $4 !~ /^tgcc/ ) { print $4 } }' | sort -u ) ) :"
144    read answer
145
146    if [ "X${answer}" != "X" ] ; then
147      ProjectID=${answer}
148    fi
149  fi
150else
151  if [ X"${x_t}" != "Xcurie" ] ; then
152    print - "\nOption -p invalid with this host (${x_t}): it will be ignored" 1>&2;
153  fi
154fi
155
156#-
157# Define the pattern string to substitute
158#-
159W_P='#-Q- '; W_W=${W_P}${x_t};
160#-
161# Extract list of 'config.card' files
162# and create jobs with AA_job
163#-
164F_CFG='config.card';
165for i in $(find ${d_n}/.. -name ${F_CFG} -print)
166do
167  j=$(cd ${i%/*};/bin/pwd;)
168  n_f=${F_RCI##*/};
169
170  if [ ! X$( echo ${j} | grep EXPERIMENTS ) = X ] ; then
171    # Do not treat config.card if it is in sub-directory of EXPERIMENTS
172    # Continue to next config.card
173    continue
174  else
175    [[ ${x_v} = 'verbose' ]] && print - "\nWorking with file ${F_CFG}\nin directory ${j}\nfor ${n_f}";
176  fi
177
178  # Find out if new structure and set .resol filename
179  if [ -d ${j}/EXPERIMENTS ] && [ -d ${j}/GENERAL ] ; then
180    # New Structure
181    [[ ${x_v} = 'verbose' ]] && echo "This is new configuration structure"
182    new_struct=yes
183    resolfile=$j/.resol
184  else
185    # Old Structure
186    new_struct=no
187    resolfile=$j/../.resol
188  fi
189
190  # Get all variables declared in section UserChoices in config.card
191  IGCM_card_DefineArrayFromSection ${j}'/'${F_CFG} UserChoices
192  # Set default values
193  config_UserChoices_JobClass=mono
194  config_UserChoices_JobNumProcTot=1
195  config_UserChoices_ExpType=""
196  RESOL_ATM_3D=this_is_a_test_string
197  RESOL=this_is_another_test_string
198  typeset option
199  for option in ${config_UserChoices[*]} ; do
200    IGCM_card_DefineVariableFromOption ${j}'/'${F_CFG} UserChoices ${option}
201  done
202
203  # Find the JobName : JobName might contain the variable RESOL_ATM_3D that will be replaced by what is in .resol file
204  if [ ! X$( echo ${config_UserChoices_JobName} | grep ${RESOL_ATM_3D} ) = X ] ; then
205    TRUERESOL=$( tail -1 $resolfile | awk "-F=" '{print $2}' )
206    echo TRUERESOL = $TRUERESOL
207    JobName=$( echo ${config_UserChoices_JobName} | sed -e "s/${RESOL_ATM_3D}/${TRUERESOL}/" )
208    IGCM_card_WriteOption ${j}'/'${F_CFG} UserChoices JobName ${JobName}
209  elif [ ! X$( echo ${config_UserChoices_JobName} | grep ${RESOL} ) = X ] ; then
210    TRUERESOL=$( head -1 $resolfile  )
211    JobName=$( echo ${config_UserChoices_JobName} | sed -e "s/${RESOL}/${TRUERESOL}/" )
212    IGCM_card_WriteOption ${j}'/'${F_CFG} UserChoices JobName ${JobName}
213  else
214    JobName=${config_UserChoices_JobName}
215  fi
216
217  # Check JobName validity : only alphanumerical characters, "-" and "." are authorized
218  ins_job_Check_JobName
219  RetCode=$?
220  [[ $RetCode -gt 0 ]] && continue
221
222  [[ ${x_v} = 'verbose' ]] && echo "JobName=${JobName}"
223
224  # Add specific treatment for new type of directory structure
225  if [ ${new_struct} == yes ] ; then
226
227    if [ "X${config_UserChoices_ExpType}" = X ] ; then
228      echo "\nERROR in ${j}/config.card"
229      echo "ins_job stops here"
230      echo "=> The variable ExpType must be added in config.card in section UserChoices"
231      echo "=> ExpType gives the directory for the .card configurtaion files for the wanted experiement. For exemple ExpType=IPSLCM5/historical"
232      exit 4
233    else
234      [[ ${x_v} = 'verbose' ]] && echo "ExpType= ${config_UserChoices_ExpType}"
235    fi
236
237    if [ -d ${j}/${JobName} ] ; then
238      echo "Directory ${j}/${JobName} exist already, continue next config.card"
239      continue
240    fi
241    echo "=> Submit directory ${JobName} will be created with cards from EXPERIMENTS/${config_UserChoices_ExpType}"
242    cp -r ${j}/EXPERIMENTS/${config_UserChoices_ExpType} ${j}/${JobName}
243    cp -r ${j}/GENERAL/* ${j}/${JobName}/.
244    cp -f ${j}/${F_CFG}  ${j}/${JobName}/.
245    rm -f ${j}/${F_CFG}
246    rm -f ${j}/${F_CFG}.bak
247    j=${j}/${JobName}
248    [[ ${x_v} = 'verbose' ]] && echo new j=$j
249  fi
250  # end specific treatment for new type directory structure
251
252  [[ -f ${j}'/'${n_f} ]] && { ins_job_Warning; } || \
253   {
254    [[ ${x_v} = 'verbose' ]] && print - "\nCopying file ${F_RCI}\nin directory ${j}";
255    \cp ${F_RCI} ${j};
256   }
257
258  # File name for Job
259  n_f='Job_'${JobName};
260  [[ -f ${j}'/'${n_f} ]] && { ins_job_Warning; continue; }
261  [[ ${x_v} = 'verbose' ]] && print - "\nWorking with file ${F_CFG}\nin directory ${j}\nfor ${n_f}";
262  sed -e "/^${W_W} */ s///" \
263      -e "/^${W_P}/d"       \
264      -e "s%::modipsl::%${F_MOD}%" \
265      -e "s/::Jobname::/${JobName}/" \
266      -e "s/::JobClass::/${config_UserChoices_JobClass}/" \
267      -e "s/::JobNumProcTot::/${config_UserChoices_JobNumProcTot}/" \
268      -e "s/::default_project::/${ProjectID}/" \
269      ${F_JOB} > ${j}'/'${n_f}
270  chmod u+x ${j}'/'${n_f}
271done
272#-
273# Extract list of AA_* files in libIGCM
274# and create jobs (for all except AA_job)
275#-
276for i in $(find ${libIGCM} -maxdepth 1 -name "AA_*" -print)
277do
278  i_f=${i##*/};
279  [[ ${i_f} = 'AA_job' ]] && { continue; }
280  j=${i%/*}; n_f=${i_f#AA_}'.job';
281  [[ -f ${j}'/'${n_f} ]] && { ins_job_Warning; continue; }
282  [[ ${x_v} = 'verbose' ]] && print - "\nIn directory ${j}\n${i_f} -> ${n_f}"
283  sed -e "/^${W_W} */ s///" \
284      -e "s%::modipsl::%${F_MOD}%" \
285      -e "/^${W_P}/d"       \
286      -e "s/::default_project::/${ProjectID}/" \
287      ${i} > ${j}'/'${n_f}
288  chmod u+x ${j}'/'${n_f}
289done
290#-
291# set default_project in libIGCM_sys_curie.ksh too.
292#-
293if [ X"${x_t}" == "Xcurie" ] ; then
294  i=${libIGCM}/libIGCM_sys/libIGCM_sys_curie.ksh
295  sed -i -e "s/::default_project::/${ProjectID}/" ${i} 
296fi
297#-
298# Limited to hindcast/forecast and date restart Ensemble for the time being
299if [ ${x_e} = 'true' ] ; then
300  #.. Read input data from ensemble.card ..
301  # TO BE CHANGED
302  FileName="ensemble.card"
303  SUBMIT_DIR=$( pwd )
304  RUN_DIR="${WORKDIR}/ENSEMBLE"
305  #
306  # Copy initial things around and define variables (hindcast/forecast case)
307  IGCM_ensemble_Init
308 
309  if [[ ${ensemble_Ens_PARAMETRIC_active} = 'y' ]] ; then
310    echo "WARNING: Parametric Ensemble is not implemented yet..."
311  fi
312
313  if [[ ${ensemble_Ens_DATE_active} = 'y' ]] ; then
314    IGCM_ensemble_DateInit
315    # As it says
316    IGCM_sys_Cd ${SUBMIT_DIR}
317    IGCM_ensemble_DatePeriodicStarts
318    # As it says
319    IGCM_sys_Cd ${SUBMIT_DIR}
320    IGCM_ensemble_DateNonPeriodicStarts
321    # Clean
322    IGCM_sys_Rm -rf ${RUN_DIR}
323  fi
324 
325  if [[ ${ensemble_Ens_PERTURB_active} = 'y' ]] ; then
326    IGCM_ensemble_CastInit
327    # As it says
328    IGCM_sys_Cd ${SUBMIT_DIR}
329    IGCM_ensemble_CastPeriodicStarts
330    # As it says
331    IGCM_sys_Cd ${SUBMIT_DIR}
332    IGCM_ensemble_CastNonPeriodicStarts
333    # Done
334    IGCM_sys_Cp ${RUN_DIR}/CreatedDir.txt ${SUBMIT_DIR}
335    IGCM_sys_Cd ${SUBMIT_DIR}
336    # Clean
337    IGCM_sys_Rm -rf ${RUN_DIR}
338  fi
339fi
340#-
341[[ ${x_v} = 'verbose' ]] && print - "";
342#-
343# That's all folks
344#-
345
346if [ ${NbErr} -ne 0 ] ; then
347  echo "################ ERROR ################"
348  echo "${NbErr} invalid JobName(s) found, check the log"
349fi
350
351
352exit 0;
Note: See TracBrowser for help on using the repository browser.