source: trunk/headers/pot_temperature.sh @ 34

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/trunk/headers/pot_temperature.sh@892
Revision 34, 10.9 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"
70#CDB_VAR_LIST="tas,mon,atmos,Amon"
71CDB_VAR_LIST="\
72ta,mon,atmos,Amon;\
73hus,mon,atmos,Amon;\
74va,mon,atmos,Amon;\
75ps,mon,atmos,Amon;\
76"
77#va,mon,atmos,Amon;\
78#
79# For each element 'var_name,freq' in CDB_VAR_LIST, the driver will
80# create a bash variable 'CDB_VAR_var_name_freq' containing the
81# path to the data.
82#
83############# END VARIABLES NEEDED IN THIS SCRIPT  #############
84
85################ DO NOT ERASE BELOW  ##############
86#
87# SETUP THE QUERY
88while getopts :hvme opt; do
89  case $opt in
90    h)
91      echo "Help"
92      echo "h: help"
93      echo "v: prints the variables needed "
94      echo "m: prints the models needed"
95      echo "e: prints the experiments needed"
96      exit
97    ;;
98    v)
99        echo ${CDB_VAR_LIST}
100        exit
101    ;;
102    m)
103        echo ${CDB_MODEL_LIST}
104        exit
105    ;;
106    e)
107        echo ${CDB_EXPT_LIST}
108        exit
109    ;;
110    \?)
111      echo "Unknown option -$OPTARG use -h for help"
112    ;;
113  esac
114done
115#
116#
117#!START YEAR LOOP
118######DO NOT ERASE PREVIOUS LINE
119######It tells the driver to start a loop over the variable YEAR from
120######YEAR_START to YEAR_END, included. It also produces the appropriate
121######bash variables pointing towards the data file for each requested
122######variables
123
124############# END DO NOT ERASE  #############
125
126###################  INSERT YEARLY SCRIPT BELOW #################
127
128#Potential temperature script
129#
130#This script takes monthly 3D temperature and specific humidity and
131#computes the liquid water (or dry) potential temperature and equivalent potential temperature.
132#
133#It outputs the result as a monthly mean on
134#
135# 1000hpa, 925hpa, 850hpa, 700hpa, 600hpa, 500hpa, 400hpa, 300hpa and 250hpa pressure levels
136# on an n48 gaussian grid to facilitate subsequent intercomparison.
137
138#Perform the computation one month at a time for an efficient memory usage:
139for MONTH in 01 02 03 04 05 06 07 08 09 10 11 12
140do
141    #Define a temp file from the CDB-provided CDB_OUT_FILE variable:
142    CDB_TEMP_FILE=${CDB_OUT_FILE}_${CDB_YEAR}_${MONTH}
143
144    #Define the remapping to gaussian grid. One can easily change the grid to another one by replacing
145    #the 'n48' by another CDO-accepted grid description.
146    REMAP="-remapcon,n48"
147    #
148    #Define the required pressure levels. These must be a subset of the standard level outputs.
149    PRES_LEVS="100000,92500,85000,70000,60000,50000,40000,30000,25000"
150
151    echo "Retrieving data from the archive"
152    #Retrieve the monthly surface pressure from the CDB-provided CDB_VAR_ps_mon:
153    cdo -selmon,$MONTH -selyear,${CDB_YEAR} -selname,ps ${CDB_VAR_ps_mon} \
154             ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps
155
156    #Retrieve the monthly temperature, specific humidity and meridional velocity
157    #and merge them in the same file:
158    cdo -Q merge \
159             -selmon,$MONTH -selyear,${CDB_YEAR} -selname,ta ${CDB_VAR_ta_mon} \
160             -selmon,$MONTH -selyear,${CDB_YEAR} -selname,hus ${CDB_VAR_hus_mon} \
161             -selmon,$MONTH -selyear,${CDB_YEAR} -selname,va ${CDB_VAR_va_mon} \
162             ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.tmp
163
164    echo "Subsetting pressure levels:"
165    #Retrieve the monthly surface pressure from the CDB-provided CDB_VAR_ps_mon:
166    #Select the levels and invert the levels so that increasing level index correspond to increasing pressure.
167    #Note that this could have been put in the previous CDO call. However, a possible bug in CDO creates a
168    #different vertical axis for each variable, even thought the levels are the same. Doing in two steps solves this issue:
169    cdo -Q invertlev -sellevel,${PRES_LEVS} ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.tmp ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full
170    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.tmp
171
172
173    echo "Merging the surface pressure with other variables"
174
175    #Data quality: some models have different horizontal grids for different variables. The following code solves this and raises
176    #a data quality assurance flag.
177    #The compare_grid.sh script compares two files each with a single horizontal grid. If their grid differ, it creates a file
178    #with the first file's grid and echo's it. Here the variable NEW_GRID contains this filename:
179    NEW_GRID=$(bash ${CDB_SOURCE_DIR}/compare_grid.sh ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps)
180    if [ -z "$NEW_GRID" ]; then
181        #merge and change variables names, to use in other scripts.
182        cdo -Q -s merge \
183                  -chname,ta,T,hus,Q,va,V ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full \
184                  -chname,ps,PS ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps \
185                  ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres
186    else
187        echo "QUALITY_ASSURANCE_FLAG: grids differ in files ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps"
188        #Output their structure:
189        cdo sinfon ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full
190        cdo sinfon ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps
191
192        #Remap conservatively the surface presssure on the other grid before merging:
193    cdo -Q -s merge \
194              -chname,ta,T,hus,Q,va,V ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full \
195              -remapcon,${NEW_GRID} -chname,ps,PS ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps \
196              ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres
197    rm $NEW_GRID
198    fi
199    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.ps
200
201    #Using the surface pressure, find the layer pressure thickness and create a three dimensional PRES variable
202    #that can be used later
203    export SURF_PRES_VAR="PS"
204    ${CDB_SCRIPT_DIR}/pressure/pres_dpres_plevel.sh T ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres
205
206    #Mask the layers with pressure less than surface pressure:
207        #FIRST, CREATE A MASK:
208        cdo -Q merge -setname,DPRES_MASK \
209                   -ifthenc,1.0 -selname,DPRES ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres\
210                   ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres\
211                   ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mask
212        rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres
213
214        #THEN APPLY TO ALL FIELDS EXCEPT SURF PRES. CDO HAS TROUBLE WITH THAT SO WE HELP:
215        VAR_LIST=`cdo showname -delname,PS,DPRES_MASK ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mask`
216        CDO_MUL_OP="DPRES_MASK=DPRES_MASK;"
217        for VAR in $VAR_LIST; do
218            CDO_MUL_OP="${CDO_MUL_OP}${VAR}=DPRES_MASK*${VAR};"
219        done
220        #APPLY THE MULTIPLICATION OPERATOR AND REMAP:
221        cdo -Q ${REMAP} -expr,${CDO_MUL_OP} ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mask ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mod
222        rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mask
223        #It is important to remap AFTER the missing values were introduced since the remapping will change depending on the missing values.
224
225    #FIND POTENTIAL TEMPERATURES:
226    cd $CDB_SCRIPT_DIR/moisture/moisture_nco
227    ncap2 -S out_both_theta_e_fusion.nco ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mod\
228                                         ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pt
229    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.full.pres_mod
230
231done #MONTH
232
233#Merge the monthly outputs. If one month was missing, output a data quality flag:
234MONTHLY_CAT_LIST="full.pt"
235#Only the previous line should need to be changed if more than one file needs to be output.
236#Be careful if modifying the next few lines
237for VAR_ID in ${MONTHLY_CAT_LIST}; do
238    NUMBER_OF_FILES=$(ls ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} | wc -l) 
239    if [ "$NUMBER_OF_FILES" -eq "12" ]; then
240        cdo -Q -s mergetime ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
241        mv ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
242        rm ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID}
243    else
244        echo "QUALITY_ASSURANCE_FLAG: Some months are missing for ${VAR_ID} in year ${CDB_YEAR}."
245    fi
246done
247###############  END INSERT YEARLY SCRIPT #################
248
249#!END YEAR LOOP
250######DO NOT ERASE PREVIOUS LINE
251######It tells the driver to end the year loop
252
253###############  INSERT POSTPROCESSING SCRIPT BELOW ############
254#Note that bash variables defined in the yearly script will not carry over
255#
256
257#Merge the yearly files back together and we perform a MULTI-YEAR SEASONAL MEAN
258YEARLY_CAT_LIST="full.pt"
259#Even if this list was defined previously, it still has to be defined here.
260#Only the previous line of code should need to be changed if more than one file needs to be output.
261#Be careful if modifying the next few lines.
262for VAR_ID in ${YEARLY_CAT_LIST}; do
263    NUMBER_OF_FILES=$(ls ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} | wc -l) 
264    if [ "$NUMBER_OF_FILES" -eq "$((${CDB_YEAR_END}-${CDB_YEAR_START}+1))" ]; then
265        cdo -s mergetime ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
266        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID}
267       
268        #Multi-year seasonal mean:
269        cdo yseasmean ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}.yseas
270        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
271    else
272        echo "QUALITY_ASSURANCE_FLAG: Some years are missing for ${VAR_ID}"
273    fi
274done
275
276#This script outputs the following file for each model:
277#${CDB_OUT_DIR}/${CDB_OUT_FILE}.full.pt.yseas
Note: See TracBrowser for help on using the repository browser.