source: trunk/headers/combine_cyclone_mask.sh @ 34

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/trunk/headers/combine_cyclone_mask.sh@892
Revision 34, 6.8 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_MASK,day,atmos,day;\
72tas,day,atmos,day;\
73ta,day,atmos,day;\
74"
75#
76# For each element 'var_name,freq' in CDB_VAR_LIST, the driver will
77# create a bash variable 'CDB_VAR_var_name_freq' containing the
78# path to the data.
79#
80############# END VARIABLES NEEDED IN THIS SCRIPT  #############
81
82################ DO NOT ERASE BELOW  ##############
83#
84# SETUP THE QUERY
85while getopts :hvme opt; do
86  case $opt in
87    h)
88      echo "Help"
89      echo "h: help"
90      echo "v: prints the variables needed "
91      echo "m: prints the models needed"
92      echo "e: prints the experiments needed"
93      exit
94    ;;
95    v)
96        echo ${CDB_VAR_LIST}
97        exit
98    ;;
99    m)
100        echo ${CDB_MODEL_LIST}
101        exit
102    ;;
103    e)
104        echo ${CDB_EXPT_LIST}
105        exit
106    ;;
107    \?)
108      echo "Unknown option -$OPTARG use -h for help"
109    ;;
110  esac
111done
112#
113#
114#!START YEAR LOOP
115######DO NOT ERASE PREVIOUS LINE
116######It tells the driver to start a loop over the variable YEAR from
117######YEAR_START to YEAR_END, included. It also produces the appropriate
118######bash variables pointing towards the data file for each requested
119######variables
120
121############# END DO NOT ERASE  #############
122
123###################  INSERT YEARLY SCRIPT BELOW #################
124#THIS SCRIPT COMBINES THE DAILY CYCLONE MASK IN PSL COMPUTED USING THE cyclone_mask SCRIPT WITH SURFACE TEMPERATURE
125#AND 3D TEMPERATURE
126for MONTH in 01 02 03 04 05 06 07 08 09 10 11 12
127do
128    CDB_TEMP_FILE=${CDB_OUT_FILE}_${CDB_YEAR}_${MONTH}
129
130    echo "Retrieving ${CDB_YEAR} month ${MONTH}"
131    #Retrieve the daily sea-level pressure:
132    cdo -s -f nc merge \
133                        -selname,tas -selyear,${CDB_YEAR} -selmon,${MONTH} ${CDB_VAR_tas_day}\
134                        -selname,PSL_MASK -selyear,${CDB_YEAR} -selmon,${MONTH} ${CDB_VAR_PSL_MASK_day}\
135                        ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.surf
136
137    cdo -s -f nc merge \
138                        -selname,ta -selyear,${CDB_YEAR} -selmon,${MONTH} ${CDB_VAR_ta_day}\
139                        ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.full
140
141    #Fix its axis description to "surface". This makes cdo behave better.
142    cdo -expr,'PSL_MASK=PSL_MASK;tas=PSL_MASK*tas' \
143                  -selzaxis,surface ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.surf\
144                  ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.surf
145
146    cdo -mul -selname,ta ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.full \
147             -selname,PSL_MASK ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.surf \
148                  ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.full
149
150    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.surf
151    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.retrieve.full
152
153    #Finally, compute the expresssion and perform a monthly mean:
154    cdo -f nc merge -monmean ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.full\
155                    -monmean ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.surf \
156                    ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.combine_psl_mask
157    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.full
158    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.psl_mask.surf
159
160done #MONTH
161
162#Merge the monthly outputs. If one month was missing, output a data quality flag:
163MONTHLY_CAT_LIST="combine_psl_mask"
164#Only the previous line should need to be changed if more than one file needs to be output.
165#Be careful if modifying the next few lines
166for VAR_ID in ${MONTHLY_CAT_LIST}; do
167    NUMBER_OF_FILES=$(ls ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} | wc -l) 
168    if [ "$NUMBER_OF_FILES" -eq "12" ]; then
169        cdo -Q -s mergetime ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
170        mv ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
171        rm ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID}
172    else
173        echo "QUALITY_ASSURANCE_FLAG: Some months are missing for ${VAR_ID} in year ${CDB_YEAR}."
174    fi
175done
176
177###############  END INSERT YEARLY SCRIPT #################
178
179#!END YEAR LOOP
180######DO NOT ERASE PREVIOUS LINE
181######It tells the driver to end the year loop
182
183###############  INSERT POSTPROCESSING SCRIPT BELOW ############
184#Note that bash variables defined in the yearly script will not carry over
185#
186#Merge the yearly files back together
187YEARLY_CAT_LIST="combine_psl_mask"
188#Even if this list was defined previously, it still has to be defined here.
189#Only the previous line of code should need to be changed if more than one file needs to be output.
190#Be careful if modifying the next few lines.
191for VAR_ID in ${YEARLY_CAT_LIST}; do
192    NUMBER_OF_FILES=$(ls ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} | wc -l) 
193    if [ "$NUMBER_OF_FILES" -eq "$((${CDB_YEAR_END}-${CDB_YEAR_START}+1))" ]; then
194        cdo -s mergetime ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
195        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID}
196    else
197        echo "QUALITY_ASSURANCE_FLAG: Some years are missing for ${VAR_ID}"
198    fi
199done
200
201#This script outputs the following file for each model:
202#${CDB_OUT_DIR}/${CDB_OUT_FILE}.combine_psl_mask
Note: See TracBrowser for help on using the repository browser.