source: trunk/libIGCM/ins_job @ 1164

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

Add ins_job in libIGCM. see #238 Dont change the name for now.

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