source: branches/libIGCM_MPI_OpenMP/AA_move-and-rename @ 559

Last change on this file since 559 was 509, checked in by mmaipsl, 13 years ago

Correct side effect when NEW_JobName and substring in Submit_dir PATH are very close. Protect some deletes. Add out_execution file.

  • Property svn:keywords set to Revision Author Date
File size: 16.4 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Martial Mancip
5# Contact: 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# Change SpaceName/ExperimentName/JobName names and path for a simulation during the run.
15# !! Be careful that the job is suspended before using this script !!
16# You may call this script with those four variables already defined, or modify directly
17# default values under here :
18
19# New Name Space of this experience
20NEW_SpaceName=${NEW_SpaceName:=DEVT}
21
22# New Expericence class of the run
23NEW_ExperimentName=${NEW_ExperimentName:=NEWExperimentName}
24
25# New JobName of the run
26NEW_JobName=${NEW_JobName:=NEWJobName}
27
28SUBMIT_DIR=${SUBMIT_DIR:=my_path_to_my_SUBMIT_DIR}
29
30# verbosity
31Verbosity=3
32
33########################################################################
34
35# Chemin vers MODIPSL
36MODIPSL=${MODIPSL:=::modipsl::}
37
38# Chemin vers libIGCM
39libIGCM=${libIGCM:=${MODIPSL}/libIGCM}
40# Attention : à changer si la machine de post-traitement n'est pas la frontale du serveur de calcul !
41#             voir précence de la variable MirrorlibIGCM dans votre couche systÚme.
42
43
44DEBUG_sys=false
45DEBUG_debug=false
46
47########################################################################
48
49. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh ;
50. ${libIGCM}/libIGCM_sys/libIGCM_sys.ksh     #; IGCM_debug_Check
51. ${libIGCM}/libIGCM_card/libIGCM_card.ksh   #; IGCM_card_Check
52. ${libIGCM}/libIGCM_date/libIGCM_date.ksh   #; IGCM_date_Check
53. ${libIGCM}/libIGCM_config/libIGCM_config.ksh
54
55########################################################################
56# define local functions
57
58# Function to replace OLD JobName in file
59
60function REPLACE_sed
61{
62    IGCM_debug_PushStack "REPLACE_sed"
63    typeset RET
64   
65    FILEIN=${1}
66    FILEOUT=$( basename ${FILEIN} )
67
68    sed -e "s&${R_SAVE}&${NEW_R_SAVE}&g" -e "s/${config_UserChoices_JobName}/${NEW_JobName}/g" \
69        ${1} > ${RUN_DIR_PATH}/${FILEOUT}
70    RET=$?
71
72    touch --reference=${1} ${RUN_DIR_PATH}/${FILEOUT}
73    IGCM_sys_Mv ${RUN_DIR_PATH}/${FILEOUT} ${2}
74    chmod --reference=${1} ${2}/${FILEOUT}
75
76    IGCM_sys_Rm -f ${1}
77
78    IGCM_debug_Print 3 "REPLACE_sed : ${1} ${2}"
79   
80    IGCM_debug_PopStack "REPLACE_sed"
81    return $RET
82}
83
84function MOVE_and_RENAME_files
85{
86    IGCM_debug_PushStack "MOVE_and_RENAME_files"
87
88    typeset VAR_files FILE_OUT file i RET
89
90    VAR_files=${1}
91    eval VAR1=\${${VAR_files[0]}}
92
93    if [ X$( echo ${NEW_R_SAVE} | grep ${config_UserChoices_JobName} ) != X ]; then
94        set +A FILE_OUT -- $( eval echo \${${VAR_files}[*]} | sed -e "s&${R_SAVE}&${NEW_R_SAVE}&g" )
95        ((i=0))
96        for file in ${FILE_OUT[@]} ; do
97            FILE_OUT[$i]=$( dirname $file )/$( echo $( basename $file ) | sed -e "s/${config_UserChoices_JobName}/${NEW_JobName}/g" )
98            (( i = i + 1 ))
99        done
100    else
101        set +A FILE_OUT -- $( eval echo \${${VAR_files}[*]} | sed -e "s&${R_SAVE}&${NEW_R_SAVE}&g" -e "s/${config_UserChoices_JobName}/${NEW_JobName}/g" )
102    fi
103    IGCM_debug_Print 3 "MOVE_and_RENAME_files all : ${VAR1}"
104    IGCM_debug_Print 3 "    to     ${FILE_OUT}." 
105
106    (( ifile = 0 ))
107    for file_o in ${FILE_OUT[@]} ; do
108        eval IGCM_sys_Mv \${${VAR_files}[${ifile}]} ${file_o}
109        RET=$?
110        (( ifile = ifile + 1 ))
111    done   
112
113    IGCM_debug_PopStack "MOVE_and_RENAME_files"
114    return $RET
115}
116
117function MY_DODS_Cp
118{
119    IGCM_debug_PushStack "MY_DODS_Cp"
120
121    typeset old_JobName old_R_DODS
122    IGCM_sys_Cd ${NEW_R_SAVE}
123    old_JobName=${config_UserChoices_JobName}
124    old_R_DODS=${R_DODS}
125    old_R_SAVE=${R_SAVE}
126
127    config_UserChoices_JobName=${NEW_JobName}
128    R_DODS=${NEW_R_DODS}
129    R_SAVE=${NEW_R_SAVE}
130
131    IGCM_sys_Dods_Cp ${1}
132    IGCM_debug_Print 3 "MY_DODS_Cp : ${config_UserChoices_JobName}/${1}"
133    IGCM_sys_Cd ${RUN_DIR_PATH}
134
135    config_UserChoices_JobName=${old_JobName}
136    R_DODS=${old_R_DODS}
137    R_SAVE=${old_R_SAVE}
138
139    IGCM_debug_PopStack "MY_DODS_Cp"
140}
141
142function MY_DODS_Rm
143{
144    IGCM_debug_PushStack "MY_DODS_Rm"
145    DEBUG_debug=false
146
147    IGCM_sys_Cd ${R_SAVE}
148    IGCM_sys_Dods_Rm ${1}
149    IGCM_debug_Print 3 "MY_DODS_Rm : ${config_UserChoices_JobName}/${1}"
150    IGCM_sys_Cd ${RUN_DIR_PATH}
151
152    IGCM_debug_PopStack "MY_DODS_Rm"
153}
154########################################################################
155
156if [ ! -d ${SUBMIT_DIR} ]; then
157    IGCM_debug_Print 1 "No ${SUBMIT_DIR}, we stop here"
158    exit 1
159fi
160
161# We temporary change ${run_Configuration_PeriodState} to
162if [ -f ${SUBMIT_DIR}/run.card ] ; then
163    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration PeriodState
164    if ( [ ${run_Configuration_PeriodState} != "Start" ] && [ ${run_Configuration_PeriodState} != "Running" ] && [ ${run_Configuration_PeriodState} != "OnQueue" ] && [ ${run_Configuration_PeriodState} != "Continue" ] ) ; then
165        old_run_Configuration_PeriodState=${run_Configuration_PeriodState}
166        IGCM_debug_PrintVariables 2 old_run_Configuration_PeriodState
167        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "OnQueue"
168    fi
169fi
170
171IGCM_config_Initialize
172
173if [ -f ${SUBMIT_DIR}/run.card ] ; then
174    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration OldPrefix
175    IGCM_debug_PrintVariables 2 run_Configuration_OldPrefix
176    if [ X${old_run_Configuration_PeriodState} != X"Completed" ] ; then
177        DateEnd=$( IGCM_date_ConvertFormatToGregorian $( echo ${run_Configuration_OldPrefix} | awk -F'_' '{print $2}' ) )
178    else
179        DateEnd=$( IGCM_date_ConvertFormatToGregorian ${config_UserChoices_DateEnd} )
180    fi
181else
182    DateEnd=$( IGCM_date_ConvertFormatToGregorian ${config_UserChoices_DateEnd} )
183fi
184
185IGCM_debug_Print 1 ""
186IGCM_debug_Print 1 "DateEnd for MoveJob_Checker : " ${DateEnd}
187IGCM_date_GetYearMonth ${DateEnd}   YearEnd   MonthEnd
188IGCM_debug_Print 1 "YearEnd MonthEnd for TimeSeries_Checker : " ${YearEnd} ${MonthEnd}
189IGCM_debug_Print 1 ""
190
191#set -vx
192
193#====================================================
194#DEFINE NEW_R_SAVE
195if [ X${config_UserChoices_SpaceName} != X ] ; then
196    NEW_SAVE=${config_UserChoices_TagName}/${NEW_SpaceName}
197else
198    NEW_SAVE=${config_UserChoices_TagName}
199fi
200if [ X${config_UserChoices_ExperimentName} != X ] ; then
201    NEW_SAVE=${NEW_SAVE}/${NEW_ExperimentName}
202fi
203
204NEW_R_SAVE=${R_OUT}/${NEW_SAVE}/${NEW_JobName}
205
206IGCM_debug_Print 1 "Move ${R_SAVE} to ${NEW_R_SAVE}"
207if [ -d ${NEW_R_SAVE} ] ; then
208    IGCM_debug_Print 1 "ERROR : ${NEW_R_SAVE} directory already exist !"
209    IGCM_debug_Print 1 "We won't try to  We stop here."
210    exit 1
211fi
212IGCM_sys_MkdirArchive ${NEW_R_SAVE}
213
214# ------------------------------------------------------------------
215# Test if all was right before proceeding further
216# ------------------------------------------------------------------
217IGCM_debug_Verif_Exit_Post
218
219#DEFINE NEW_R_DODS
220NEW_R_DODS=${NEW_SAVE}/${NEW_JobName}
221
222# Define NEW_REBUILD_DIR
223if [ -d ${REBUILD_DIR} ] ; then
224    if [ X${config_Post_RebuildFromArchive} = Xtrue ] ; then
225        NEW_REBUILD_DIR=${NEW_R_SAVE}/TMP
226        IGCM_sys_MkdirArchive ${NEW_REBUILD_DIR}
227    else
228        NEW_REBUILD_DIR=${BIG_DIR}/${config_UserChoices_TagName}/${NEW_JobName}
229        IGCM_sys_MkdirWork ${NEW_REBUILD_DIR}
230    fi
231fi
232
233#====================================================
234# SAVE LOCAL WORKING DIR
235IGCM_sys_Cd ${RUN_DIR_PATH}
236
237#====================================================
238#Exe
239IGCM_debug_Print 1 "Move Exe"
240set +A listfiles -- $( find ${R_SAVE}/Exe -type f )
241
242IGCM_sys_MkdirArchive ${NEW_R_SAVE}/Exe
243MOVE_and_RENAME_files "listfiles"
244
245#====================================================
246#Out
247IGCM_debug_Print 1 "Move Out"
248OutinDir=${R_SAVE}/Out
249OutNEWDir=${NEW_R_SAVE}/Out
250IGCM_sys_MkdirArchive ${OutNEWDir}
251
252# Change JobName and copy run.card
253if [ -f ${OutinDir}/run.card ] ; then
254    sed -e "s/OldPrefix=\ *${config_UserChoices_JobName}/OldPrefix= ${NEW_JobName}/g" \
255        ${OutinDir}/run.card > ${RUN_DIR_PATH}/run.card
256    RET=$?
257    if [ ${RET} = 0 ] ; then
258        IGCM_sys_Mv -f ${RUN_DIR_PATH}/run.card ${OutNEWDir}
259        IGCM_sys_Rm -f ${OutinDir}/run.card
260    fi
261fi
262
263# Just copy historical run.card
264unset listfiles
265set +A listfiles -- $( find ${OutinDir} -type f -name "run.card\.[0-9]*\.[0-9]*" )
266if [ ${#listfiles[*]} -gt 0 ] ; then
267    IGCM_sys_Cp ${listfiles[*]} ${OutNEWDir}
268    RET=$?
269    if [ ${RET} = 0 ] ; then
270        IGCM_sys_Rm -f ${listfiles[*]}
271    fi
272fi
273
274# Move and Rename OLD Script_Output
275unset listfiles
276set +A listfiles -- $( find ${OutinDir} -type f -name "Script_Output_*\.[0-9]*\.[0-9]*" )
277MOVE_and_RENAME_files "listfiles"
278
279# Move and Rename OLD Executable Output
280unset listfiles
281set +A listfiles -- $( find ${OutinDir} -type f -name "*_out_${config_Executable_Name}" )
282MOVE_and_RENAME_files "listfiles"
283unset listfiles
284set +A listfiles -- $( find ${OutinDir} -type f -name "*_out_execution" )
285MOVE_and_RENAME_files "listfiles"
286
287#====================================================
288IGCM_debug_Print 1 "For components"
289IGCM_debug_Print 1 "=============="
290
291#====================================================
292#Output
293# loop over components and frequencies
294IGCM_debug_Print 1 "Move Output"
295for comp in ${config_ListOfComponents[*]} ; do
296    IGCM_debug_Print 2 "------------------"
297    IGCM_debug_Print 2 "for component ${comp}"
298
299    unset FREQDIR
300    set +A FREQDIR -- $( find ${R_SAVE}/${comp}/Output -type d -not -name "*Output" )
301
302    IGCM_debug_Print 3 "List of WriteFrequencies : ${FREQDIR[*]}"
303
304    for ThisfreqDir in ${FREQDIR[*]} ; do
305        freq=$( basename ${ThisfreqDir} )
306        IGCM_debug_Print 2 "${freq}."
307
308        NEW_freqDir=${NEW_R_SAVE}/${comp}/Output/${freq}
309        IGCM_sys_MkdirArchive ${NEW_freqDir}
310
311        unset listfiles
312        set +A listfiles -- $( find ${ThisfreqDir} -type f )
313       
314        MOVE_and_RENAME_files "listfiles"
315    done
316
317    unset COMPFILES
318    set +A COMPFILES -- $( find ${R_SAVE}/${comp}/Output -type f )
319   
320    if [ ${#COMPFILES[*]} -gt 0 ] ; then
321        IGCM_debug_Print 2 "simple Output FILES : ${COMPFILES[*]}."
322
323        MOVE_and_RENAME_files "COMPFILES"
324    fi
325done
326
327#====================================================
328#Restart
329# loop over components
330IGCM_debug_Print 1 "Move Restart"
331for comp in ${config_ListOfComponents[*]} ; do
332    IGCM_debug_Print 2 "------------------"
333    IGCM_debug_Print 2 "for component ${comp}"
334
335    ThisRestDir=${R_SAVE}/${comp}/Restart
336    NEW_RestDir=${NEW_R_SAVE}/${comp}/Restart
337    IGCM_sys_MkdirArchive ${NEW_RestDir}
338
339    unset listfiles
340    set +A listfiles -- $( find ${ThisRestDir} -type f )
341   
342    MOVE_and_RENAME_files "listfiles"
343done
344
345#====================================================
346#DEBUG
347# Move and Rename OLD Executable Output
348# loop over components
349IGCM_debug_Print 1 "Move Debug"
350for comp in ${config_ListOfComponents[*]} ; do
351    IGCM_debug_Print 2 "------------------"
352    IGCM_debug_Print 2 "for component ${comp}"
353
354    ThisDebugDir=${R_SAVE}/${comp}/Debug
355    NEW_DebugDir=${NEW_R_SAVE}/${comp}/Debug
356    IGCM_sys_MkdirArchive ${NEW_DebugDir}
357
358    unset listfiles
359    set +A listfiles -- $( find ${ThisDebugDir} -type f )
360   
361    MOVE_and_RENAME_files "listfiles"
362done
363
364#====================================================
365#POST
366# loop over components and frequencies
367IGCM_debug_Print 1 "Move Analyse"
368for comp in ${config_ListOfComponents[*]} ; do
369    IGCM_debug_Print 2 "------------------"
370    IGCM_debug_Print 2 "for component ${comp}"
371   
372    if [ -d ${R_SAVE}/${comp}/Analyse ] ; then
373        unset TSDIR
374        set +A TSDIR -- $( find ${R_SAVE}/${comp}/Analyse -name "TS_*" -type d )
375       
376        for ThisTSDir in ${TSDIR[*]} ; do
377            TS_Dir=$( basename ${ThisTSDir} )
378            IGCM_debug_Print 2 "${TS_Dir}."
379           
380            NEW_TSDir=${NEW_R_SAVE}/${comp}/Analyse/${TS_Dir}
381            IGCM_sys_MkdirArchive ${NEW_TSDir}
382           
383            MY_DODS_Rm ${comp}/Analyse/${TS_Dir}
384           
385            unset listfiles
386            set +A listfiles -- $( find ${ThisTSDir} -type f )
387           
388            MOVE_and_RENAME_files "listfiles"
389           
390            MY_DODS_Cp ${comp}/Analyse/${TS_Dir}
391        done
392
393        ThisSEDir=${R_SAVE}/${comp}/Analyse/SE
394        IGCM_sys_TestDirArchive ${ThisSEDir}
395        if [ $? = 0 ] ; then
396            IGCM_debug_Print 2 "SE."
397            NEW_SEDir=${NEW_R_SAVE}/${comp}/Analyse/SE
398            IGCM_sys_MkdirArchive ${NEW_SEDir}
399           
400            MY_DODS_Rm ${comp}/Analyse/SE
401           
402            unset listfiles
403            set +A listfiles -- $( find ${ThisSEDir} -type f )
404           
405            MOVE_and_RENAME_files "listfiles"
406           
407            MY_DODS_Cp ${comp}/Analyse/SE
408        fi
409
410        MY_DODS_Rm ${comp}/Analyse
411        MY_DODS_Rm ${comp}
412    else
413        IGCM_debug_Print 2 "nothing to do without Analyse."
414    fi
415
416done
417
418#====================================================
419#GRAPHS
420IGCM_debug_Print 1 "Move ATLAS"
421R_ATLAS=ATLAS
422IGCM_sys_TestDirArchive ${R_SAVE}/${R_ATLAS}
423if [ $? = 0 ] ; then
424#    REPLACE_sed ${NEW_R_SAVE}/${R_ATLAS}/*/*/index.html ${NEW_R_SAVE}/${R_ATLAS}
425
426    MY_DODS_Rm ${R_ATLAS}
427
428    IGCM_sys_Mv ${R_SAVE}/${R_ATLAS} ${NEW_R_SAVE}/${R_ATLAS}
429
430    MY_DODS_Cp ${R_ATLAS}
431fi
432
433IGCM_debug_Print 1 "Move MONITORING"
434R_MONITORING=MONITORING
435IGCM_sys_TestDirArchive ${R_SAVE}/${R_MONITORING}
436if [ $? = 0 ] ; then
437    IGCM_sys_MkdirArchive ${NEW_R_SAVE}/${R_MONITORING}
438    REPLACE_sed ${R_SAVE}/${R_MONITORING}/index.html ${NEW_R_SAVE}/${R_MONITORING}
439    sed -e "s/OldPrefix=\ *${config_UserChoices_JobName}/OldPrefix= ${NEW_JobName}/g" \
440        ${R_SAVE}/${R_MONITORING}/run.card > ${RUN_DIR_PATH}/run.card
441    IGCM_sys_Mv -f ${RUN_DIR_PATH}/run.card ${NEW_R_SAVE}/${R_MONITORING}
442    RET=$?
443    if [ ${RET} = 0 ] ; then
444        IGCM_sys_Rm -f ${R_SAVE}/${R_MONITORING}/run.card
445    fi
446    REPLACE_sed ${R_SAVE}/${R_MONITORING}/config.card ${NEW_R_SAVE}/${R_MONITORING}
447    IGCM_sys_Cp -rp ${R_SAVE}/${R_MONITORING} ${NEW_R_SAVE}
448
449    MY_DODS_Rm ${R_MONITORING}
450    IGCM_sys_Rm -rf ${R_SAVE}/${R_MONITORING}
451
452    MY_DODS_Cp ${R_MONITORING}
453fi
454
455
456#====================================================
457#REBUILD
458
459if [ -d ${REBUILD_DIR} ] ; then
460
461    IGCM_debug_Print 1 "Move REBUILD"
462    set +A LISTREBUILDS -- $( find ${REBUILD_DIR} -type d  -not -name "*${config_UserChoices_JobName}" -a -not -name "*TMP" )
463
464    for ThisREBDir in ${LISTREBUILDS[*]} ; do
465        rebdir=$( basename ${ThisREBDir} )
466        IGCM_debug_Print 2 "${rebdir}."
467        if [ X${config_Post_RebuildFromArchive} = Xtrue ] ; then
468            IGCM_sys_MkdirArchive ${NEW_REBUILD_DIR}/${rebdir}
469        else
470            IGCM_sys_MkdirWork ${NEW_REBUILD_DIR}/${rebdir}
471        fi
472        REPLACE_sed ${REBUILD_DIR}/${rebdir}/rebuild.ksh ${NEW_REBUILD_DIR}/${rebdir}
473        IGCM_sys_Mv -f ${REBUILD_DIR}/${rebdir}/* ${NEW_REBUILD_DIR}/${rebdir}/
474    done
475fi
476
477#====================================================
478#List OLD R_SAVE and Ask for erase it !
479
480IGCM_debug_Print 1 "ls of SAVE DIR :"
481ls --color -lR ${R_SAVE}
482
483echo "\033[1;31mPlease verify that ALL OLD DIRECTORY LISTED IS COMPLETLY EMPTY !"
484echo "\033[1;32mIf It is NOT EMPTY, you can TRY AGAIN this script."
485echo "\033[1;31mDo you REALLY want to erase all this directory ?"
486echo -n " Your answer (y/n) : \033[m"
487if [ X${reponse} = X ] ; then
488    read reponse
489fi
490case ${reponse} in
491           
492    oui|OUI|o|y|yes|YES)
493        echo "OK. It will be erased."
494        IGCM_sys_Rm -Rf ${R_SAVE}
495        echo "done."
496        ;;
497    non|NON|n|no|NO)
498        echo "Nothing to do !"
499        ;;
500esac
501
502
503#############################################################################################################
504# Edit config.card, run.card and Job_${NewJobName}
505
506IGCM_debug_Print 1 "edit config.card"
507if [ X${config_UserChoices_SpaceName} != X ] ; then
508    IGCM_card_WriteOption ${SUBMIT_DIR}/config.card UserChoices SpaceName ${NEW_SpaceName}
509fi
510if [ X${config_UserChoices_ExperimentName} != X ] ; then
511    IGCM_card_WriteOption ${SUBMIT_DIR}/config.card UserChoices ExperimentName ${NEW_ExperimentName}
512fi
513IGCM_card_WriteOption ${SUBMIT_DIR}/config.card UserChoices JobName ${NEW_JobName}
514
515if [ -f ${SUBMIT_DIR}/run.card ] ; then
516    IGCM_debug_Print 1 "edit run.card"
517    sed -e "s/OldPrefix=\ *${config_UserChoices_JobName}/OldPrefix= ${NEW_JobName}/g" \
518        ${SUBMIT_DIR}/run.card > ${RUN_DIR_PATH}/run.card
519    IGCM_sys_Mv -f ${RUN_DIR_PATH}/run.card ${SUBMIT_DIR}/run.card
520
521    if [ X${old_run_Configuration_PeriodState} != X ] ; then
522        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState ${old_run_Configuration_PeriodState}
523    fi
524fi
525
526if [ -f ${SUBMIT_DIR}/Job_${config_UserChoices_JobName} ] ; then
527    IGCM_debug_Print 1 "edit Job_${NEW_JobName}"
528    sed -e "s&${R_SAVE}&${NEW_R_SAVE}&g" -e "s/${config_UserChoices_JobName}/${NEW_JobName}/g" \
529        ${SUBMIT_DIR}/Job_${config_UserChoices_JobName} > ${SUBMIT_DIR}/Job_${NEW_JobName}
530    chmod --reference=${SUBMIT_DIR}/Job_${config_UserChoices_JobName} ${SUBMIT_DIR}/Job_${NEW_JobName}
531    ls -la ${SUBMIT_DIR}/Job_${NEW_JobName}
532fi
Note: See TracBrowser for help on using the repository browser.