odb_manger.sh 5.65 KB
Newer Older
saeed's avatar
saeed committed
1
2
3
4
5
6
#!/bin/bash
##SBATCH -N 1
##SBATCH -J manage_odb_data
##SBATCH -e err
##SBATCH -o log

saeed's avatar
saeed committed
7
8
module add odb/1.0.3
module add odb_api/0.19.1
saeed's avatar
saeed committed
9
10
11
12
13
14
15
16
17
18
19
20
21

function getyear(){
    time_d=${1} 
    year=`echo ${time_d}|cut -d- -f1`
    echo ${year}
}

function getmon(){
    time_d=${1} 
    mon=`echo ${time_d}| cut -d- -f2`
    echo ${mon}
}

saeed's avatar
saeed committed
22
23
24
25
26
27
function getday(){
    time_d=${1} 
    day=`echo ${time_d}| cut -d- -f3`
    echo ${day}
}

saeed's avatar
saeed committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function yearmonday(){
    time_d=${1} 
    year=`echo ${time_d}|cut -d- -f1`
    mon=`echo ${time_d}| cut -d- -f2`
    day=`echo ${time_d}| cut -d- -f3`
    echo ${year}${mon}${day}
}

function addbackslash(){
    str=${1}
    echo ${str/,/\\\/}
}

function output_name(){
    fin=${1}
    fout=${fin/".odb"/".txt"}
    echo ${fout}
}

function ismonthly(){

    result=0
    date1=$1
    date2=$2
    y1=`getyear ${date1}`
    y2=`getyear ${date2}`
    m1=`getmon ${date1}`
    m2=`getmon ${date2}`
    
    if [ ${y1} -eq ${y2} -a ${m1} -eq ${m2} ];then
	result=1
    fi
    echo ${result}
}

function do_filter(){
    fin=${1}
    fout=${fin/".odb"/".txt"}
    if [ -f ${fin} ];then
	odb sql 'SELECT                                       
         date@hdr,                                                                                                  
         time@hdr,                                                                                                  
         lat@hdr,
         lon@hdr,                                                                                                  
         obsvalue@body,                                                                                               
         varno@body,                                                                                                  
         codetype@hdr,
         count(*)                                                                                                
         WHERE                                                                                               
         datum_status.active@body == 1' -i ${fin} -o ${fout}
    fi
}

saeed's avatar
saeed committed
81
do_era5=1
saeed's avatar
saeed committed
82
do_precise=1
saeed's avatar
saeed committed
83
use_report_file=1
saeed's avatar
saeed committed
84

saeed's avatar
saeed committed
85
86
PRECISE_ODB_DIR="/scratch/ms/se/smdk/scratchdir/"
OUTPUT_DIR="/home/ms/se/smdk/odb_panel/OUTPUT"
saeed's avatar
saeed committed
87

saeed's avatar
saeed committed
88
89
90
91
92
93
94
95
96
97
list_report=[]
if [ ${use_report_file} -eq 1 ];then
    if [ ! -f report_type_list ];then
	echo "please provide the file named report_type_list"
	exit
    fi
    list_report=(`cat report_type_list`)
else
    list_report=(16001 16005)
fi
saeed's avatar
saeed committed
98
startdate="2017-01-01"
saeed's avatar
saeed committed
99
enddate="2017-01-05"
saeed's avatar
saeed committed
100
101
102
103
104
105
106
107
108
109
110
111
112
time_cycle="00,12"

checkmonthly=`ismonthly ${startdate} ${enddate}`

if [ ${checkmonthly} -eq 0 ];then
    echo "we only support the monthly data retrieval from mars"
    echo "please adjust the startdate ${startdate} or enddate ${enddate}"
    exit
fi

time_cycle_out=`addbackslash ${time_cycle}`
startdate_out=`yearmonday ${startdate}`
enddate_out=`yearmonday ${enddate}`
saeed's avatar
saeed committed
113
114
115
116
117
118
119
120
121
122
123

day_1=`getday ${startdate}`
day_2=`getday ${enddate}`

drange=[]
ii=0
for j in `seq -w ${day_1} ${day_2}`;do
    drange[${ii}]=${j}
    ii=$((ii +1))
done

saeed's avatar
saeed committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
HERE=`pwd`

echo ${startdate_out}
echo ${enddate_out}

if [ ${do_era5} -eq 1 ];then

    for j in ${!list_report[*]};do
	vr=${list_report[j]}
	fin="odb_era5_${vr}_${startdate_out}_${enddate_out}.odb"
	fout=`output_name ${fin}`
	if [ ! -f ${OUTPUT_DIR}/${fout} ];then
	    echo "we are extracting odb for repportype ${vr}"
	    sed -e "s/<reptype>/${vr}/g" -e "s/<time_cycle>/${time_cycle_out}/g" -e "s/<startdate_out>/${startdate_out}/g" -e "s/<enddate_out>/${enddate_out}/g" -e "s/<startdate>/${startdate}/g" -e "s/<enddate>/${enddate}/g" request_odb_temp_era5 > request_odb_temp
	    mars request_odb_temp
            do_filter ${fin}
saeed's avatar
saeed committed
140
	    rm -f ${fin} 2>/dev/null
saeed's avatar
saeed committed
141
	    mv ${fout} ${OUTPUT_DIR} 2>/dev/null
saeed's avatar
saeed committed
142
	    rm -f request_odb_temp 2>/dev/null
saeed's avatar
saeed committed
143
144
145
146
147
148
149
150
	else
	    echo "${OUTPUT_DIR}/${fout} already exists"
	fi
    done
fi


if [ ${do_precise} -eq 1 ];then    
saeed's avatar
saeed committed
151
    
saeed's avatar
saeed committed
152
153
154
155
156
157
158
159
160
161
162
163
164
    if [ ! -d ${PRECISE_ODB_DIR} ];then
	echo "directory ${PRECISE_ODB_DIR} does not exist"
	exit
    fi

    year="`getyear ${startdate}`"
    mon="`getmon ${startdate}`"
    if [ ! -d ${PRECISE_ODB_DIR}/${year}/${mon} ];then
	echo "directory ${PRECISE_ODB_DIR}/${year}/${mon} does not exist"
	exit
    fi
    
    cd ${PRECISE_ODB_DIR}
saeed's avatar
saeed committed
165
        
saeed's avatar
saeed committed
166
167
168
    cd ${year}
    cd ${mon}
    HERE_P=`pwd`
saeed's avatar
saeed committed
169
    
saeed's avatar
saeed committed
170
171
172
173
174
175
176
177
178
179
180
181
182
    for j in ${!list_report[*]};do
	report_type=${list_report[j]}
	nc=`echo ${time_cycle} | awk -F',' '{print NF}'`
	for nf in `seq 1 ${nc}`;do
	    vb=`echo ${time_cycle} | cut -d, -f${nf}`
	    fout="odb_precise_${report_type}_${startdate_out}_${enddate_out}_cycle${vb}.txt"
	    if [ -f ${OUTPUT_DIR}/${fout} ];then
		echo "file ${OUTPUT_DIR}/${fout} already exist"
	    fi
	done
    done

   
saeed's avatar
saeed committed
183
    for j in ${!list_report[*]};do
saeed's avatar
saeed committed
184
	cd ${HERE_P}
saeed's avatar
saeed committed
185
186
	report_type=${list_report[j]}
	nc=`echo ${time_cycle} | awk -F',' '{print NF}'`
saeed's avatar
saeed committed
187
188
189
190
191
192
	for nf in `seq 1 ${nc}`;do
	    vb=`echo ${time_cycle} | cut -d, -f${nf}`
	    fout="odb_precise_${report_type}_${startdate_out}_${enddate_out}_cycle${vb}.txt"
	    if [ ! -f ${OUTPUT_DIR}/${fout} ];then
 		for va in ${drange[*]};do
		    echo "va " ${va}
saeed's avatar
saeed committed
193
194
195
196
197
198
199
200
201
202
203
204
205
		    if [ -d "${va}"/"${vb}"/odb_ccma/CCMA ];then
			cd "${va}"/"${vb}"/odb_ccma/CCMA
			dcagen
			odbsql -q "select andate,antime,degrees(lat),degrees(lon),varno,codetype, count(*) from hdr,desc,body where\
 obsvalue/=\"NULL\" and reportype in (${report_type})" | grep -v "obsvalue" > odb_precise_${report_type}_${vb}.txt
		    fi
		    cd ${HERE_P}
		done
		cd ${HERE_P}
		for va in ${drange[*]};do
		    if [ -d ${va}/${vb}/odb_ccma/CCMA ];then
			cd ${va}/${vb}/odb_ccma/CCMA
			if [ -f odb_precise_${report_type}_${vb}.txt ];then
saeed's avatar
saeed committed
206
			    sed '1d' odb_precise_${report_type}_${vb}.txt >> ${OUTPUT_DIR}/${fout}
saeed's avatar
saeed committed
207
208
			fi
		    fi
saeed's avatar
saeed committed
209
		    cd ${HERE_P}
saeed's avatar
saeed committed
210
		done
saeed's avatar
saeed committed
211
212
213
	    fi
	done
	cd ${HERE_P}
saeed's avatar
saeed committed
214
215
216
    done
fi

saeed's avatar
saeed committed
217
cd ${HERE}