source: trunk/libIGCM/AA_clean_year @ 781

Last change on this file since 781 was 766, checked in by labetoulle, 11 years ago

Add AA_clear_year : same cleaning as clean_month but works on the whole
current year. If a year is given as argument, cleans every month back to
January of that given year.

File size: 10.5 KB
Line 
1#!/bin/ksh
2#**************************************************************
3# Author: Martial Mancip
4# Contact: Martial.Mancip__at__ipsl.jussieu.fr
5# $Revision::                                          $ Revision of last commit
6# $Author::                                            $ Author of last commit
7# $Date::                                              $ Date of last commit
8# IPSL (2006)
9#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
10#
11#**************************************************************
12
13#set -eu
14#set -vx
15
16libIGCM=::modipsl::/libIGCM
17
18#D- Task type (computing or post-processing)
19TaskType=post-processing
20
21#D--------------------------------------------------------------------==
22#D-                   -1. User modification place
23#D--------------------------------------------------------------------==
24
25#D- Increased verbosity (1, 2, 3)
26Verbosity=3
27
28#D- Low level debug : to bypass lib test checks and stack construction
29#D- Default=true
30DEBUG_debug=false
31
32#D--------------------------------------------------------------------==
33
34echo "clean year script :"
35echo "Script used to clean a year on SAVE directory !"
36echo "This script will erase some files !!! Be careful and read all informations !"
37echo "----"
38echo "It must be called in Experience directory (so-called SUBMIT_DIR)."
39echo "----"
40
41#D--------------------------------------------------------------------==
42
43. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh
44       ( ${DEBUG_debug} ) && IGCM_debug_Check
45. ${libIGCM}/libIGCM_card/libIGCM_card.ksh
46       ( ${DEBUG_debug} ) && IGCM_card_Check
47. ${libIGCM}/libIGCM_date/libIGCM_date.ksh
48       ( ${DEBUG_debug} ) && IGCM_date_Check
49#-------
50SUBMIT_DIR=$( pwd )
51. ${libIGCM}/libIGCM_sys/libIGCM_sys.ksh
52. ${libIGCM}/libIGCM_config/libIGCM_config.ksh
53
54#==================================
55# First of all
56#
57# Read libIGCM compatibility version in config.card
58# Read UserChoices section
59# Read Ensemble section
60# Read Post section
61# Define all netcdf output directories
62#==================================
63IGCM_config_CommonConfiguration ${SUBMIT_DIR}/config.card
64
65#==================================
66# Define default value to keep compatibility with previous card: means before changes due to TGCC
67if [ X${PackDefault} = Xtrue ] ; then
68  [ X${config_Post_PackFrequency} = X ] && config_Post_PackFrequency=${config_Post_RebuildFrequency}
69else
70  config_Post_PackFrequency=NONE
71fi
72
73#==================================
74# Is the pack active
75if ( [ ! X${config_Post_PackFrequency} = X${NULL_STR} ] && \
76     [ ! X${config_Post_PackFrequency} = XNONE ] ) ; then
77  Pack=true
78fi
79
80IGCM_sys_TestDirArchive ${R_SAVE}
81[ $? != 0 ] && IGCM_debug_Exit "IGCM_sys_TestDirArchive"
82
83if [ ! -f ${SUBMIT_DIR}/run.card ]; then
84    echo "You seem NOT to have a run.card in this directory !!"
85    echo "This script will fail because it doesn't know what to do without run.card available."
86    exit 1
87fi
88
89#===================================#
90#        Read updated run.card      #
91#===================================#
92IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration PeriodState
93IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration OldPrefix
94IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration PeriodDateBegin
95IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration PeriodDateEnd
96IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration CumulPeriod
97if [ X${run_Configuration_CumulPeriod} == X ] ; then
98  ${run_Configuration_CumulPeriod} = 0
99fi
100
101
102PreviousPeriodDateEnd=$( IGCM_date_ConvertFormatToGregorian ${run_Configuration_PeriodDateEnd} )
103PreviousPeriodDateBegin=$( IGCM_date_ConvertFormatToGregorian ${run_Configuration_PeriodDateBegin} )
104
105IGCM_date_GetYearMonth ${PreviousPeriodDateEnd} PreviousPeriodYear PreviousPeriodMonth
106
107if [ $# == 1 ] ; then
108  if [ ${#1} != 4 ] ; then
109    echo "You must give a year (SSAA) as argument."
110    exit 1
111  fi
112  ReferenceYear=$1
113else
114  ReferenceYear=${PreviousPeriodYear}
115fi
116RemovedPeriods=0
117
118# Colors
119Red="\033[1;31m"
120Green="\033[1;32m"
121NoColor="\033[0m"
122
123while [ ${PreviousPeriodYear} -ge ${ReferenceYear} ] ; do
124
125  PeriodDateEnd=${PreviousPeriodDateEnd}
126  PeriodDateBegin=${PreviousPeriodDateBegin}
127  PeriodYear=${PreviousPeriodYear}
128  PeriodMonth=${PreviousPeriodMonth}
129
130  echo "Search files with ${PeriodDateEnd} in ${R_SAVE}"
131  unset ListFiles
132  set +A ListFiles -- $( IGCM_sys_RshArchive "find ${R_SAVE} -name \"*${PeriodDateEnd}*\" " )
133  CountListFiles=${#ListFiles[@]}
134
135  if [ X${Pack} = Xtrue ] ; then
136    echo "Search files with ${PeriodDateEnd} in ${R_BUFR}"
137    unset ListFilesBuf
138    set +A ListFilesBuf -- $( IGCM_sys_RshMaster "find ${R_BUFR} -name \"*${PeriodDateEnd}*\" " )
139    CountListFilesBuf=${#ListFilesBuf[@]}
140  else
141    CountListFilesBuf=0
142  fi
143
144  # If exist, erase REBUILD DIR
145  CountRebuildDir=0
146  if [ X${config_Post_RebuildFrequency} != X ] ; then
147    echo "Search directories with ${PeriodDateBegin} in REBUILD_DIR"
148    unset RebuildDir
149    if [ X${config_Post_RebuildFromArchive} == Xtrue ] ; then
150      REBUILD_DIR=${R_SAVE}/TMP
151      set +A RebuildDir -- $( IGCM_sys_RshArchive "find ${REBUILD_DIR} -type d -name \"*${PeriodDateBegin}*\" " )
152    else
153      REBUILD_DIR=${BIG_DIR}/${config_UserChoices_TagName}/${config_UserChoices_JobName}
154      set +A RebuildDir -- $( find ${REBUILD_DIR} -type d -name  "*${PeriodDateBegin}*" )
155    fi
156    CountRebuildDir=${#RebuildDir[@]}
157  fi
158
159
160  if ( [ ${CountListFiles} -gt 0 ] || [ ${CountListFilesBuf} -gt 0 ] ) ; then
161    IGCM_debug_Print 1 "Number of files to be erased from ${R_SAVE} : ${CountListFiles}."
162    for file in ${ListFiles[@]} ; do
163      IGCM_debug_Print 2 -e "${Red} ${file}${NoColor}"
164    done
165    if [ X${Pack} = Xtrue ] ; then
166      IGCM_debug_Print 1 "Number of files to be erased from ${R_BUFR} : ${CountListFilesBuf}."
167      for file in ${ListFilesBuf[@]} ; do
168        IGCM_debug_Print 2 -e "${Red} ${file}${NoColor}"
169      done
170    fi
171
172    echo -e "${Green}Do you REALLY want to erase all those files ?"
173    echo -n -e " Your answer (y/n) : ${NoColor}"
174
175    read reponse
176    case ${reponse} in
177      oui|OUI|o|y|yes|YES)
178        echo "OK. It will be erased."
179        IGCM_sys_RshArchive "find ${R_SAVE} -name \"*${PeriodDateEnd}*\" -exec rm -f '{}' \;"
180        if [ X${Pack} = Xtrue ] ; then
181          IGCM_sys_RshMaster  "find ${R_BUFR} -name \"*${PeriodDateEnd}*\" -exec rm -f '{}' \;"
182        fi
183        echo "done."
184        ;;
185      non|NON|n|no|NO)
186        echo "Nothing to do !"
187        ;;
188    esac
189  fi
190
191  if [ ${CountRebuildDir} -gt 0 ] ; then
192    IGCM_debug_Print 1 "Number of REBUILD Dirs to be erased : ${CountRebuildDir}."
193    for Dir in ${RebuildDir[@]} ; do
194      IGCM_debug_Print 2 -e "${Red} ${Dir}${NoColor}"
195    done
196
197    echo -e "${Green}Do you REALLY want to erase all those Directories ?"
198    echo -n -e " Your answer (y/n) : ${NoColor}"
199
200    read reponse
201    case ${reponse} in
202     
203      oui|OUI|o|y|yes|YES)
204        echo "OK. It will be erased."
205        if [ X${config_Post_RebuildFromArchive} = Xtrue ] ; then
206          IGCM_sys_RshArchive "find ${REBUILD_DIR} -type d -name \"*${PeriodDateBegin}*\" -exec rm -Rf '{}' \;"
207        else
208          find ${REBUILD_DIR} -type d -name "*${PeriodDateBegin}*" -exec rm -Rf '{}' \;
209        fi
210        echo "done."
211        ;;
212      non|NON|n|no|NO)
213        echo "Nothing to do !"
214        ;;
215    esac
216  fi
217
218  (( RemovedPeriods = ${RemovedPeriods} + 1 ))
219
220  # Get previous period begin and end dates
221  PreviousPeriodDateEnd=$( IGCM_date_AddDaysToGregorianDate ${PeriodDateEnd} \
222                               -$( IGCM_date_DaysInMonth ${PeriodYear} ${PeriodMonth} ) )
223  IGCM_date_GetYearMonth ${PreviousPeriodDateEnd} PreviousPeriodYear PreviousPeriodMonth
224 
225  PreviousPeriodDateBegin=$( IGCM_date_AddDaysToGregorianDate ${PeriodDateBegin} \
226                         -$( IGCM_date_DaysInMonth ${PreviousPeriodYear} ${PreviousPeriodMonth} ) )
227done
228
229if [ ${RemovedPeriods} == 0 ] ; then
230  echo "Nothing done, exit"
231  exit 0
232fi
233
234IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration PeriodState
235if ( [ ${run_Configuration_PeriodState} != "Running" ] && \
236     [ ${run_Configuration_PeriodState} != "Continue" ] ) ; then
237
238  (( NewCumulPeriod = ${run_Configuration_CumulPeriod} - ${RemovedPeriods} + 1 ))
239
240  if [ ${NewCumulPeriod} -le 1 ] ; then
241    RemoveRunCard=true
242  else
243    RemoveRunCard=false
244  fi
245
246  Suffix=$( printf '%06i' ${NewCumulPeriod} )
247
248  printf "${Green}Your run.card and Job_${config_UserChoices_JobName} will now be updated\n"
249  printf " - PeriodState = OnQueue ;\n"
250  printf " - PeriodDateBegin, PeriodDateEnd, CumulPeriod and OldPrefix ;\n"
251  printf "   %-10s       %-10s          %6i     %s\n" \
252         $( IGCM_date_HumanDigit ${PeriodDateBegin} ) \
253         $( IGCM_date_HumanDigit ${PeriodDateEnd} ) \
254         ${NewCumulPeriod} \
255         ${config_UserChoices_JobName}_${PreviousPeriodDateEnd}
256  printf " - ScriptOutput = %s" \
257         "Script_Output_${config_UserChoices_JobName}.${Suffix}"
258  printf " Are you OK ? (y/n) ${NoColor}"
259
260  read reponse
261  case ${reponse} in
262    oui|OUI|o|y|yes|YES)
263      if ( ${RemoveRunCard} ) ; then
264        echo "CumulPeriod=1 so we erase run.card to start properly."
265        IGCM_sys_Rm ${SUBMIT_DIR}/run.card
266      else
267        # Update run.card
268        cp ${SUBMIT_DIR}/run.card ${SUBMIT_DIR}/run.card.old
269        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "OnQueue"
270        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration CumulPeriod ${NewCumulPeriod}
271        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodDateBegin \
272                              $( IGCM_date_HumanDigit ${PeriodDateBegin} )
273        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodDateEnd \
274                              $( IGCM_date_HumanDigit ${PeriodDateEnd} )
275        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration OldPrefix \
276                              ${config_UserChoices_JobName}_${PreviousPeriodDateEnd}
277        # Update job
278        cp Job_${config_UserChoices_JobName} Job_${config_UserChoices_JobName}.old
279        sed -e "s/\(#.*Script_Output_${config_UserChoices_JobName}\.\)\([0-9]*\)\(.*\)/\1${Suffix}\3/" \
280            Job_${config_UserChoices_JobName} > Job_${config_UserChoices_JobName}.tmp
281        mv Job_${config_UserChoices_JobName}.tmp Job_${config_UserChoices_JobName}
282      fi
283      echo "OK. Now you can submit the Job : ${SUBMIT_DIR}/Job_${config_UserChoices_JobName}."
284      ;;
285    non|NON|n|no|NO)
286      echo "Nothing to do !"
287      ;;
288  esac
289else
290  echo "You can submit the Job : ${SUBMIT_DIR}/Job_${config_UserChoices_JobName}."
291fi
292
293exit 0
Note: See TracBrowser for help on using the repository browser.