source: trunk/scripts/tas_zonal.sh @ 34

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/trunk/scripts/tas_zonal.sh@1252
Revision 34, 6.7 KB checked in by lalibert, 7 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,1970;\
49historical:1965,2004;\
50rcp45:2060,2099;\
51"
52#
53#
54################ END EXPERIMENTS AND YEARS TO USE THIS SCRIPT ON  ##############
55
56################ VARIABLES NEEDED IN THIS SCRIPT  ##############
57#
58# A space-separated list of variables names, frequencies, realm and MIP code must be
59# stored in the variable CDB_VAR_LIST. For example, to recover
60# daily surface temperature AND 3-hourly surface specific humidity:
61# CDB_VAR_LIST="tas,day,atmos,day huss,3hr,atmos,3hr"
62# To recover daily cloud area fraction AND monthly sea surface salinity:
63# CDB_VAR_LIST="cl,day,atmos,cfDay sos,mon,atmos,Omon"
64CDB_VAR_LIST="\
65tas,day,atmos,day;\
66"
67#
68# For each element 'var_name,freq' in CDB_VAR_LIST, the driver will
69# create a bash variable 'CDB_VAR_var_name_freq' containing the
70# path to the data.
71#
72############# END VARIABLES NEEDED IN THIS SCRIPT  #############
73
74################ DO NOT ERASE BELOW  ##############
75#
76# SETUP THE QUERY
77while getopts :hvme opt; do
78  case $opt in
79    h)
80      echo "Help"
81      echo "h: help"
82      echo "v: prints the variables needed "
83      echo "m: prints the models needed"
84      echo "e: prints the experiments needed"
85      exit
86    ;;
87    v)
88        echo ${CDB_VAR_LIST}
89        exit
90    ;;
91    m)
92        echo ${CDB_MODEL_LIST}
93        exit
94    ;;
95    e)
96        echo ${CDB_EXPT_LIST}
97        exit
98    ;;
99    \?)
100      echo "Unknown option -$OPTARG use -h for help"
101    ;;
102  esac
103done
104#
105#
106#!START YEAR LOOP
107######DO NOT ERASE PREVIOUS LINE
108######It tells the driver to start a loop over the variable YEAR from
109######YEAR_START to YEAR_END, included. It also produces the appropriate
110######bash variables pointing towards the data file for each requested
111######variables
112
113############# END DO NOT ERASE  #############
114
115###################  INSERT YEARLY SCRIPT BELOW #################
116#This script computes:
117#
118#1) daily zonal-mean surface temperature TAS_ZON
119#2) daily zonal-mean surface temperature variance DTAS2_ZON
120#3) monthly surface temperature variance DTAS2_MON
121#4) monthly-mean surface temperature TAS_MON
122
123#Perform the computation month per month:
124for MONTH in 01 02 03 04 05 06 07 08 09 10 11 12
125do
126    CDB_TEMP_FILE=${CDB_OUT_FILE}_${CDB_YEAR}_${MONTH}
127
128    echo "Retrieving ${CDB_YEAR} month ${MONTH}"
129    #Retrieve the file from the archive:
130    cdo -s -f nc -selyear,${CDB_YEAR} -selmon,${MONTH} ${CDB_VAR_tas_day}\
131                                      ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf
132
133    #Compute the zonal-mean daily quantities:
134    cdo merge\
135              -setname,TAS_ZON -zonmean -selname,tas ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf \
136              -setname,DTAS2_ZON -zonvar -selname,tas ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf \
137              ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.daily
138
139    #compute the monthly-mean quantities:
140    cdo merge\
141              -setname,TAS_MON   -monmean -selname,tas ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf \
142              -setname,DTAS2_MON -monvar -selname,tas ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf \
143              ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.monthly
144
145    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.surf
146
147done #MONTH
148
149#Merge the monthly outputs. If one month was missing, output a data quality flag:
150MONTHLY_CAT_LIST="daily monthly"
151#Only the previous line should need to be changed if more than one file needs to be output.
152#Be careful if modifying the next few lines
153for VAR_ID in ${MONTHLY_CAT_LIST}; do
154    NUMBER_OF_FILES=$(ls ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} | wc -l) 
155    if [ "$NUMBER_OF_FILES" -eq "12" ]; then
156        cdo -Q -s mergetime ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
157        mv ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
158        rm ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID}
159    else
160        echo "QUALITY_ASSURANCE_FLAG: Some months are missing for ${VAR_ID} in year ${CDB_YEAR}."
161    fi
162done
163
164###############  END INSERT YEARLY SCRIPT #################
165
166#!END YEAR LOOP
167######DO NOT ERASE PREVIOUS LINE
168######It tells the driver to end the year loop
169
170###############  INSERT POSTPROCESSING SCRIPT BELOW ############
171#Note that bash variables defined in the yearly script will not carry over
172#
173#Merge the yearly files back together and we perform a MULTI-YEAR SEASONAL MEAN
174YEARLY_CAT_LIST="daily monthly"
175#Even if this list was defined previously, it still has to be defined here.
176#Only the previous line of code should need to be changed if more than one file needs to be output.
177#Be careful if modifying the next few lines.
178for VAR_ID in ${YEARLY_CAT_LIST}; do
179    NUMBER_OF_FILES=$(ls ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} | wc -l) 
180    if [ "$NUMBER_OF_FILES" -eq "$((${CDB_YEAR_END}-${CDB_YEAR_START}+1))" ]; then
181        cdo -s mergetime ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
182        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID}
183    else
184        echo "QUALITY_ASSURANCE_FLAG: Some years are missing for ${VAR_ID}"
185    fi
186done
187
188#This script outputs the following file for each model:
189#${CDB_OUT_DIR}/${CDB_OUT_FILE}.psl_mask
190
191#Finally, we list the output files that we wish will be structured as in a typical ESG, essentially creating
192# new CMIP5 type variables. These will then be easily recoverable from another script.
193#This instruction will take the file ${CDB_OUT_DIR}/${CDB_OUT_FILE}.psl_mask and output each of the variables it contains to
194#a separate file, with a naming convention that assumes that the data it contains is at the daily frequency.
195#Do not forget to leave a blank space between entries.
196CDB_CMIP5_COMP_LIST="\
197${CDB_OUT_DIR}/${CDB_OUT_FILE}.daily;day,atmos,day \
198${CDB_OUT_DIR}/${CDB_OUT_FILE}.monthly;mon,atmos,Amon \
199"
Note: See TracBrowser for help on using the repository browser.