source: tags/libIGCM_v1_12/AA_move-and-rename @ 1456

Last change on this file since 1456 was 508, 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.

  • Property svn:keywords set to Revision Author Date
File size: 16.3 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"
283
284#====================================================
285IGCM_debug_Print 1 "For components"
286IGCM_debug_Print 1 "=============="
287
288#====================================================
289#Output
290# loop over components and frequencies
291IGCM_debug_Print 1 "Move Output"
292for comp in ${config_ListOfComponents[*]} ; do
293    IGCM_debug_Print 2 "------------------"
294    IGCM_debug_Print 2 "for component ${comp}"
295
296    unset FREQDIR
297    set +A FREQDIR -- $( find ${R_SAVE}/${comp}/Output -type d -not -name "*Output" )
298
299    IGCM_debug_Print 3 "List of WriteFrequencies : ${FREQDIR[*]}"
300
301    for ThisfreqDir in ${FREQDIR[*]} ; do
302        freq=$( basename ${ThisfreqDir} )
303        IGCM_debug_Print 2 "${freq}."
304
305        NEW_freqDir=${NEW_R_SAVE}/${comp}/Output/${freq}
306        IGCM_sys_MkdirArchive ${NEW_freqDir}
307
308        unset listfiles
309        set +A listfiles -- $( find ${ThisfreqDir} -type f )
310       
311        MOVE_and_RENAME_files "listfiles"
312    done
313
314    unset COMPFILES
315    set +A COMPFILES -- $( find ${R_SAVE}/${comp}/Output -type f )
316   
317    if [ ${#COMPFILES[*]} -gt 0 ] ; then
318        IGCM_debug_Print 2 "simple Output FILES : ${COMPFILES[*]}."
319
320        MOVE_and_RENAME_files "COMPFILES"
321    fi
322done
323
324#====================================================
325#Restart
326# loop over components
327IGCM_debug_Print 1 "Move Restart"
328for comp in ${config_ListOfComponents[*]} ; do
329    IGCM_debug_Print 2 "------------------"
330    IGCM_debug_Print 2 "for component ${comp}"
331
332    ThisRestDir=${R_SAVE}/${comp}/Restart
333    NEW_RestDir=${NEW_R_SAVE}/${comp}/Restart
334    IGCM_sys_MkdirArchive ${NEW_RestDir}
335
336    unset listfiles
337    set +A listfiles -- $( find ${ThisRestDir} -type f )
338   
339    MOVE_and_RENAME_files "listfiles"
340done
341
342#====================================================
343#DEBUG
344# Move and Rename OLD Executable Output
345# loop over components
346IGCM_debug_Print 1 "Move Debug"
347for comp in ${config_ListOfComponents[*]} ; do
348    IGCM_debug_Print 2 "------------------"
349    IGCM_debug_Print 2 "for component ${comp}"
350
351    ThisDebugDir=${R_SAVE}/${comp}/Debug
352    NEW_DebugDir=${NEW_R_SAVE}/${comp}/Debug
353    IGCM_sys_MkdirArchive ${NEW_DebugDir}
354
355    unset listfiles
356    set +A listfiles -- $( find ${ThisDebugDir} -type f )
357   
358    MOVE_and_RENAME_files "listfiles"
359done
360
361#====================================================
362#POST
363# loop over components and frequencies
364IGCM_debug_Print 1 "Move Analyse"
365for comp in ${config_ListOfComponents[*]} ; do
366    IGCM_debug_Print 2 "------------------"
367    IGCM_debug_Print 2 "for component ${comp}"
368   
369    if [ -d ${R_SAVE}/${comp}/Analyse ] ; then
370        unset TSDIR
371        set +A TSDIR -- $( find ${R_SAVE}/${comp}/Analyse -name "TS_*" -type d )
372       
373        for ThisTSDir in ${TSDIR[*]} ; do
374            TS_Dir=$( basename ${ThisTSDir} )
375            IGCM_debug_Print 2 "${TS_Dir}."
376           
377            NEW_TSDir=${NEW_R_SAVE}/${comp}/Analyse/${TS_Dir}
378            IGCM_sys_MkdirArchive ${NEW_TSDir}
379           
380            MY_DODS_Rm ${comp}/Analyse/${TS_Dir}
381           
382            unset listfiles
383            set +A listfiles -- $( find ${ThisTSDir} -type f )
384           
385            MOVE_and_RENAME_files "listfiles"
386           
387            MY_DODS_Cp ${comp}/Analyse/${TS_Dir}
388        done
389
390        ThisSEDir=${R_SAVE}/${comp}/Analyse/SE
391        IGCM_sys_TestDirArchive ${ThisSEDir}
392        if [ $? = 0 ] ; then
393            IGCM_debug_Print 2 "SE."
394            NEW_SEDir=${NEW_R_SAVE}/${comp}/Analyse/SE
395            IGCM_sys_MkdirArchive ${NEW_SEDir}
396           
397            MY_DODS_Rm ${comp}/Analyse/SE
398           
399            unset listfiles
400            set +A listfiles -- $( find ${ThisSEDir} -type f )
401           
402            MOVE_and_RENAME_files "listfiles"
403           
404            MY_DODS_Cp ${comp}/Analyse/SE
405        fi
406
407        MY_DODS_Rm ${comp}/Analyse
408        MY_DODS_Rm ${comp}
409    else
410        IGCM_debug_Print 2 "nothing to do without Analyse."
411    fi
412
413done
414
415#====================================================
416#GRAPHS
417IGCM_debug_Print 1 "Move ATLAS"
418R_ATLAS=ATLAS
419IGCM_sys_TestDirArchive ${R_SAVE}/${R_ATLAS}
420if [ $? = 0 ] ; then
421#    REPLACE_sed ${NEW_R_SAVE}/${R_ATLAS}/*/*/index.html ${NEW_R_SAVE}/${R_ATLAS}
422
423    MY_DODS_Rm ${R_ATLAS}
424
425    IGCM_sys_Mv ${R_SAVE}/${R_ATLAS} ${NEW_R_SAVE}/${R_ATLAS}
426
427    MY_DODS_Cp ${R_ATLAS}
428fi
429
430IGCM_debug_Print 1 "Move MONITORING"
431R_MONITORING=MONITORING
432IGCM_sys_TestDirArchive ${R_SAVE}/${R_MONITORING}
433if [ $? = 0 ] ; then
434    IGCM_sys_MkdirArchive ${NEW_R_SAVE}/${R_MONITORING}
435    REPLACE_sed ${R_SAVE}/${R_MONITORING}/index.html ${NEW_R_SAVE}/${R_MONITORING}
436    sed -e "s/OldPrefix=\ *${config_UserChoices_JobName}/OldPrefix= ${NEW_JobName}/g" \
437        ${R_SAVE}/${R_MONITORING}/run.card > ${RUN_DIR_PATH}/run.card
438    IGCM_sys_Mv -f ${RUN_DIR_PATH}/run.card ${NEW_R_SAVE}/${R_MONITORING}
439    RET=$?
440    if [ ${RET} = 0 ] ; then
441        IGCM_sys_Rm -f ${R_SAVE}/${R_MONITORING}/run.card
442    fi
443    REPLACE_sed ${R_SAVE}/${R_MONITORING}/config.card ${NEW_R_SAVE}/${R_MONITORING}
444    IGCM_sys_Cp -rp ${R_SAVE}/${R_MONITORING} ${NEW_R_SAVE}
445
446    MY_DODS_Rm ${R_MONITORING}
447    IGCM_sys_Rm -rf ${R_SAVE}/${R_MONITORING}
448
449    MY_DODS_Cp ${R_MONITORING}
450fi
451
452
453#====================================================
454#REBUILD
455
456if [ -d ${REBUILD_DIR} ] ; then
457
458    IGCM_debug_Print 1 "Move REBUILD"
459    set +A LISTREBUILDS -- $( find ${REBUILD_DIR} -type d  -not -name "*${config_UserChoices_JobName}" -a -not -name "*TMP" )
460
461    for ThisREBDir in ${LISTREBUILDS[*]} ; do
462        rebdir=$( basename ${ThisREBDir} )
463        IGCM_debug_Print 2 "${rebdir}."
464        if [ X${config_Post_RebuildFromArchive} = Xtrue ] ; then
465            IGCM_sys_MkdirArchive ${NEW_REBUILD_DIR}/${rebdir}
466        else
467            IGCM_sys_MkdirWork ${NEW_REBUILD_DIR}/${rebdir}
468        fi
469        REPLACE_sed ${REBUILD_DIR}/${rebdir}/rebuild.ksh ${NEW_REBUILD_DIR}/${rebdir}
470        IGCM_sys_Mv -f ${REBUILD_DIR}/${rebdir}/* ${NEW_REBUILD_DIR}/${rebdir}/
471    done
472fi
473
474#====================================================
475#List OLD R_SAVE and Ask for erase it !
476
477IGCM_debug_Print 1 "ls of SAVE DIR :"
478ls --color -lR ${R_SAVE}
479
480echo "\033[1;31mPlease verify that ALL OLD DIRECTORY LISTED IS COMPLETLY EMPTY !"
481echo "\033[1;32mIf It is NOT EMPTY, you can TRY AGAIN this script."
482echo "\033[1;31mDo you REALLY want to erase all this directory ?"
483echo -n " Your answer (y/n) : \033[m"
484if [ X${reponse} = X ] ; then
485    read reponse
486fi
487case ${reponse} in
488           
489    oui|OUI|o|y|yes|YES)
490        echo "OK. It will be erased."
491        IGCM_sys_Rm -Rf ${R_SAVE}
492        echo "done."
493        ;;
494    non|NON|n|no|NO)
495        echo "Nothing to do !"
496        ;;
497esac
498
499
500#############################################################################################################
501# Edit config.card, run.card and Job_${NewJobName}
502
503IGCM_debug_Print 1 "edit config.card"
504if [ X${config_UserChoices_SpaceName} != X ] ; then
505    IGCM_card_WriteOption ${SUBMIT_DIR}/config.card UserChoices SpaceName ${NEW_SpaceName}
506fi
507if [ X${config_UserChoices_ExperimentName} != X ] ; then
508    IGCM_card_WriteOption ${SUBMIT_DIR}/config.card UserChoices ExperimentName ${NEW_ExperimentName}
509fi
510IGCM_card_WriteOption ${SUBMIT_DIR}/config.card UserChoices JobName ${NEW_JobName}
511
512if [ -f ${SUBMIT_DIR}/run.card ] ; then
513    IGCM_debug_Print 1 "edit run.card"
514    sed -e "s/OldPrefix=\ *${config_UserChoices_JobName}/OldPrefix= ${NEW_JobName}/g" \
515        ${SUBMIT_DIR}/run.card > ${RUN_DIR_PATH}/run.card
516    IGCM_sys_Mv -f ${RUN_DIR_PATH}/run.card ${SUBMIT_DIR}/run.card
517
518    if [ X${old_run_Configuration_PeriodState} != X ] ; then
519        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState ${old_run_Configuration_PeriodState}
520    fi
521fi
522
523if [ -f ${SUBMIT_DIR}/Job_${config_UserChoices_JobName} ] ; then
524    IGCM_debug_Print 1 "edit Job_${NEW_JobName}"
525    sed -e "s&${R_SAVE}&${NEW_R_SAVE}&g" -e "s/${config_UserChoices_JobName}/${NEW_JobName}/g" \
526        ${SUBMIT_DIR}/Job_${config_UserChoices_JobName} > ${SUBMIT_DIR}/Job_${NEW_JobName}
527    chmod --reference=${SUBMIT_DIR}/Job_${config_UserChoices_JobName} ${SUBMIT_DIR}/Job_${NEW_JobName}
528    ls -la ${SUBMIT_DIR}/Job_${NEW_JobName}
529fi
Note: See TracBrowser for help on using the repository browser.