source: CDB_diags/trunk/FBL/CO2_zonal.sh @ 6

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/CDB_diags/trunk/FBL/CO2_zonal.sh@6
Revision 6, 6.8 KB checked in by lalibert, 9 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="\
72co2,mon,atmos,Amon;\
73ta,mon,atmos,Amon;\
74hus,mon,atmos,Amon;\
75va,mon,atmos,Amon;\
76ps,mon,atmos,Amon;\
77"
78#va,mon,atmos,Amon;\
79#
80# For each element 'var_name,freq' in CDB_VAR_LIST, the driver will
81# create a bash variable 'CDB_VAR_var_name_freq' containing the
82# path to the data.
83#
84############# END VARIABLES NEEDED IN THIS SCRIPT  #############
85
86################ DO NOT ERASE BELOW  ##############
87#
88# SETUP THE QUERY
89while getopts :hvme opt; do
90  case $opt in
91    h)
92      echo "Help"
93      echo "h: help"
94      echo "v: prints the variables needed "
95      echo "m: prints the models needed"
96      echo "e: prints the experiments needed"
97      exit
98    ;;
99    v)
100        echo ${CDB_VAR_LIST}
101        exit
102    ;;
103    m)
104        echo ${CDB_MODEL_LIST}
105        exit
106    ;;
107    e)
108        echo ${CDB_EXPT_LIST}
109        exit
110    ;;
111    \?)
112      echo "Unknown option -$OPTARG use -h for help"
113    ;;
114  esac
115done
116#
117#
118#!START YEAR LOOP
119######DO NOT ERASE PREVIOUS LINE
120######It tells the driver to start a loop over the variable YEAR from
121######YEAR_START to YEAR_END, included. It also produces the appropriate
122######bash variables pointing towards the data file for each requested
123######variables
124
125############# END DO NOT ERASE  #############
126
127###################  INSERT YEARLY SCRIPT BELOW #################
128
129
130#ZONAL CO2 SCRIPT
131
132#Perform the computation one month at a time for an efficient memory usage:
133for MONTH in 01 02 03 04 05 06 07 08 09 10 11 12
134do
135    #Define a temp file from the CDB-provided CDB_OUT_FILE variable:
136    CDB_TEMP_FILE=${CDB_OUT_FILE}_${CDB_YEAR}_${MONTH}
137
138    #Define the remapping to gaussian grid. One can easily change the grid to another one by replacing
139    #the 'n48' by another CDO-accepted grid description.
140    REMAP="-remapcon,n48"
141    #
142    #Define the required pressure levels. These must be a subset of the standard level outputs.
143    PRES_LEVS="100000,92500,85000,70000,60000,50000,40000,30000,25000"
144
145    echo "Retrieving data from the archive"
146    #Retrieve the monthly surface pressure from the CDB-provided CDB_VAR_ps_mon:
147    cdo -selmon,$MONTH -selyear,${CDB_YEAR} -selname,co2 ${CDB_VAR_co2_mon} \
148             ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.co2.tmp
149
150    echo "Subsetting pressure levels and taking zonal mean:"
151    #Select the levels and invert the levels so that increasing level index correspond to increasing pressure.
152    #Note that this could have been put in the previous CDO call. However, a possible bug in CDO creates a
153    #different vertical axis for each variable, even thought the levels are the same. Doing in two steps solves this issue:
154    cdo -s -Q -setname,co2_zon -zonmean -invertlev -sellevel,${PRES_LEVS} ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.co2.tmp\
155                                                                          ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.co2_zon
156    rm ${CDB_TEMP_DIR}/${CDB_TEMP_FILE}.co2.tmp
157
158done #MONTH
159
160#Merge the monthly outputs. If one month was missing, output a data quality flag:
161MONTHLY_CAT_LIST="co2_zon"
162#Only the previous line should need to be changed if more than one file needs to be output.
163#Be careful if modifying the next few lines
164for VAR_ID in ${MONTHLY_CAT_LIST}; do
165    NUMBER_OF_FILES=$(ls ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} | wc -l) 
166    if [ "$NUMBER_OF_FILES" -eq "12" ]; then
167        cdo -Q -s mergetime ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID} ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
168        mv ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}.${VAR_ID}
169        rm ${CDB_TEMP_DIR}/${CDB_OUT_FILE}_${CDB_YEAR}_??.${VAR_ID}
170    else
171        echo "QUALITY_ASSURANCE_FLAG: Some months are missing for ${VAR_ID} in year ${CDB_YEAR}."
172    fi
173done
174###############  END INSERT YEARLY SCRIPT #################
175
176#!END YEAR LOOP
177######DO NOT ERASE PREVIOUS LINE
178######It tells the driver to end the year loop
179
180###############  INSERT POSTPROCESSING SCRIPT BELOW ############
181#Note that bash variables defined in the yearly script will not carry over
182#
183
184#Merge the yearly files back together and we perform a MULTI-YEAR SEASONAL MEAN
185YEARLY_CAT_LIST="co2_zon"
186#Even if this list was defined previously, it still has to be defined here.
187#Only the previous line of code should need to be changed if more than one file needs to be output.
188#Be careful if modifying the next few lines.
189for VAR_ID in ${YEARLY_CAT_LIST}; do
190    NUMBER_OF_FILES=$(ls ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} | wc -l) 
191    if [ "$NUMBER_OF_FILES" -eq "$((${CDB_YEAR_END}-${CDB_YEAR_START}+1))" ]; then
192        cdo -s mergetime ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
193        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}_????.${VAR_ID}
194       
195        #Multi-year seasonal mean:
196        cdo yseasmean ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID} ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}.yseas
197        rm ${CDB_OUT_DIR}/${CDB_OUT_FILE}.${VAR_ID}
198    else
199        echo "QUALITY_ASSURANCE_FLAG: Some years are missing for ${VAR_ID}"
200    fi
201done
202
203#This script outputs the following file for each model:
204#${CDB_OUT_DIR}/${CDB_OUT_FILE}.full.pt.yseas
Note: See TracBrowser for help on using the repository browser.