source: trunk/headers/cyclone_mask.sh @ 34

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/trunk/headers/cyclone_mask.sh@34
Revision 34, 8.2 KB checked in by lalibert, 8 years ago (diff)

Initial import

Line 
1##################### HELP ####################################
2#
3#
4#THE CDB DEFINES THE FOLLOWING VARIABLES IN THE FILE cdb_header:
5#
6# PROGRAMS:
7#
8# CDB_CDO_PATH
9# CDB_NCO_PATH
10# CDB_PYTHON_PATH
11#
12# DIRECTORIES:
13#
14# CDB_TEMP_DIR
15# CDB_OUT_DIR
16# CDB_DIAG_DIR
17# CDB_SCRIPT_DIR
18# CDB_IN_DIR
19# CDB_SOURCE_DIR
20#
21#
22#THE DRIVER PASSES THE FOLLOWING INPUTS TO THIS SCRIPT:
23#
24# CDB_MODEL = MODEL NAME, E.G. CanESM2
25# CDB_RUN_ID = RUN_ID, E.G. r1i1p1
26# CDB_EXPT = EXPERIMENT NAME, E.G. rcp45
27# CDB_YEARS = COMMA-SEPARATED YEAR BOUNDS, E.G. 2050,2099
28#
29# CDB_DIAG_NAME
30#
31#THIS SCRIPT RECOVERS REQUESTED VARIABLES AS AN ARRAY OF PATHNAMES
32#
33# IT ALSO FORMATS A NATURAL OUTPUT FILENAME:
34# CDB_OUT_FILE with all run characteristics
35#
36################## END HELP ####################################
37
38################ MODELS USED IN THIS SCRIPT  ##############
39#
40CDB_MODEL_LIST="\
41"
42#
43############# END MODELS USED IN THIS SCRIPT  #############
44
45################ EXPERIMENTS AND YEARS TO USE THIS SCRIPT ON  ##############
46#
47CDB_EXPT_LIST="\
48historical:1965,2004;\
49rcp45:2060,2099;\
50"
51#
52#Can use the same experiment but for two time periods, ie:
53#
54#CDB_EXPT_LIST="\
55#historical:1965,1984;\
56#historical:1985,2004;\
57#rcp45:2060,2099;\
58#"
59#
60################ END EXPERIMENTS AND YEARS TO USE THIS SCRIPT ON  ##############
61
62################ VARIABLES NEEDED IN THIS SCRIPT  ##############
63#
64# A space-separated list of variables names, frequencies, realm and MIP code must be
65# stored in the variable CDB_VAR_LIST. For example, to recover
66# daily surface temperature AND 3-hourly surface specific humidity:
67# CDB_VAR_LIST="tas,day,atmos,day huss,3hr,atmos,3hr"
68# To recover daily cloud area fraction AND monthly sea surface salinity:
69# CDB_VAR_LIST="cl,day,atmos,cfDay sos,mon,atmos,Omon"
70CDB_VAR_LIST="\
71psl,day,atmos,day;\
72"
73#
74# For each element 'var_name,freq' in CDB_VAR_LIST, the driver will
75# create a bash variable 'CDB_VAR_var_name_freq' containing the
76# path to the data.
77#
78############# END VARIABLES NEEDED IN THIS SCRIPT  #############
79
80################ DO NOT ERASE BELOW  ##############
81#
82# SETUP THE QUERY
83while getopts :hvme opt; do
84  case $opt in
85    h)
86      echo "Help"
87      echo "h: help"
88      echo "v: prints the variables needed "
89      echo "m: prints the models needed"
90      echo "e: prints the experiments needed"
91      exit
92    ;;
93    v)
94        echo ${CDB_VAR_LIST}
95        exit
96    ;;
97    m)
98        echo ${CDB_MODEL_LIST}
99        exit
100    ;;
101    e)
102        echo ${CDB_EXPT_LIST}
103        exit
104    ;;
105    \?)
106      echo "Unknown option -$OPTARG use -h for help"
107    ;;
108  esac
109done
110#
111#
112#!START YEAR LOOP
113######DO NOT ERASE PREVIOUS LINE
114######It tells the driver to start a loop over the variable YEAR from
115######YEAR_START to YEAR_END, included. It also produces the appropriate
116######bash variables pointing towards the data file for each requested
117######variables
118
119############# END DO NOT ERASE  #############
120
121###################  INSERT YEARLY SCRIPT BELOW #################
122#THIS SCRIPT COMPUTES THE DAILY CYCLONE MASK IN PSL USING THE WERNLI AND SCHWIERZ (2006) ALGORITHM.
123for MONTH in 01 02 03 04 05 06 07 08 09 10 11 12
124do
125    CDB_TEMP_FILE=${CDB_OUT_FILE}_${CDB_YEAR}_${MONTH}
126
127    echo "Retrieving ${CDB_YEAR} month ${MONTH}"
128    #Retrieve the daily sea-level pressure:
129    cdo -s -f nc -selyear,${CDB_YEAR} -selmon,${MONTH} ${CDB_VAR_psl_day}\
130                                      ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve
131
132    #Fix its axis description to "surface". This makes cdo behave better.
133    cdo -s -selzaxis,surface ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf
134    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve
135
136##############################################################################
137#This instruction is for advanced user. It won't do anything unless the --dim_async switch is used in
138#the compiler "diag_driver_comp.py"
139#!PARALLEL time
140
141    #This loop creates a list of pressure contour levels used in the contour-based minimum finding algorithm
142    #We use a 2hPa spacing, as in Wernli and Schwierz
143    PRES=95000
144    PRES_LIST=""
145    while [ "$PRES" -lt "103000" ]; do PRES_LIST="${PRES_LIST},$PRES"; let "PRES+=200"; done
146
147    #Select sea-level pressure:
148    cdo -s -selname,psl ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl
149    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf
150
151    echo "Computing min ${CDB_YEAR} month ${MONTH}"
152    #Apply the contour-base minimum-finding algorithm
153    python ${CDB_SCRIPT_DIR}/cyclone_tracking/WS/ws0.1/contour_min_final.py -a compute$PRES_LIST -j 1 -i ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl -o ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl.min
154    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl
155
156    #The algorithm outputs the contours as regions with a constant slp value. That slp value corresponds to the slp value at
157    #the minimum. When points are not part of a cyclone contour, they are set to 0.0.
158    #Here, we set set all the regions with an identified cyclone 1.0 and regions outside cyclones to 0.0:
159    cdo -s -setname,PSL_MASK -gtc,0.0 ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl.min \
160               ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.tmp
161    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl.min
162
163    #Because the output will be daily data, we wish to minimize the size of the output. To do so,
164    #we convert the 1.0 and 0.0 floats to shorts, dramatically reducing the data size.
165    ncap2 -s 'PSL_MASK=short(PSL_MASK);' ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.tmp \
166                                         ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask
167    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.tmp
168
169#!END PARALLEL time
170#The previous instruction is for advanced user. It won't do anything unless the --dim_async switch is used in
171#the compiler "diag_driver_comp.py"
172##############################################################################
173
174done #MONTH
175
176#Merge the monthly outputs. If one month was missing, output a data quality flag:
177MONTHLY_CAT_LIST="psl_mask"
178#Only the previous line should need to be changed if more than one file needs to be output.
179#Be careful if modifying the next few lines
180for VAR_ID in ${MONTHLY_CAT_LIST}; do
181    NUMBER_OF_FILES=$(ls ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} | wc -l) 
182    if [ "$NUMBER_OF_FILES" -eq "12" ]; then
183        cdo -Q -s mergetime ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
184        mv ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
185        rm ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID}
186    else
187        echo "QUALITY_ASSURANCE_FLAG: Some months are missing for ${VAR_ID} in year ${CDB_YEAR}."
188    fi
189done
190
191###############  END INSERT YEARLY SCRIPT #################
192
193#!END YEAR LOOP
194######DO NOT ERASE PREVIOUS LINE
195######It tells the driver to end the year loop
196
197###############  INSERT POSTPROCESSING SCRIPT BELOW ############
198#Note that bash variables defined in the yearly script will not carry over
199#
200#Merge the yearly files back together
201YEARLY_CAT_LIST="psl_mask"
202#Even if this list was defined previously, it still has to be defined here.
203#Only the previous line of code should need to be changed if more than one file needs to be output.
204#Be careful if modifying the next few lines.
205for VAR_ID in ${YEARLY_CAT_LIST}; do
206    NUMBER_OF_FILES=$(ls ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} | wc -l) 
207    if [ "$NUMBER_OF_FILES" -eq "$((${CDB_YEAR_END}-${CDB_YEAR_START}+1))" ]; then
208        cdo -s mergetime ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
209        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID}
210    else
211        echo "QUALITY_ASSURANCE_FLAG: Some years are missing for ${VAR_ID}"
212    fi
213done
214
215#This script outputs the following file for each model:
216#${CDB_OUT_DIR}/${CDB_OUT_FILE}.psl_mask
217
218#Finally, we list the output files that we wish will be structured as in a typical ESG, essentially creating
219# new CMIP5 type variables. These will then be easily recoverable from another script.
220#This instruction will take the file ${CDB_OUT_DIR}/${CDB_OUT_FILE}.psl_mask and output each of the variables it contains to
221#a separate file, with a naming convention that assumes that the data it contains is at the daily frequency.
222CDB_CMIP5_COMP_LIST="\
223${CDB_OUT_DIR}/${CDB_OUT_FILE}.psl_mask;day,atmos,day\
224"
Note: See TracBrowser for help on using the repository browser.