-
Notifications
You must be signed in to change notification settings - Fork 0
/
odb.py
120 lines (95 loc) · 2.76 KB
/
odb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
'''
LOBSTER v1.0 (Local OBServation-based Tracking of Emission that are misReported)
ECMWF Jerome Barre 2020-2021
ODB functions
https://acp.copernicus.org/articles/21/5117/2021/
'''
import sys
import os
import numpy as np
import metview as mv
import pandas as pd
#db retrieve from mars
class odb(object):
def get_odb(self,list_date,list_file,exp,stype,stream,repor,redo):
for date,ofile in zip(list_date,list_file):
if not os.path.isfile(ofile):
request=("retrieve,\n"+
"class=rd,\n"+
"date="+date+",\n"+
"expver="+exp+",\n"+
"reportype="+repor+",\n"+
"stream="+stream+",\n"+
"type="+stype+",\n"+
"time=00:00:00/12:00:00,\n"+
"format=odb,\n"+
"target='"+ofile+"'")
os.system("rm -f request")
f= open("request","w")
f.write(request)
f.close()
os.system('mars request')
elif redo==True or os.path.getsize(ofile)==0:
request=("retrieve,\n"+
"class=rd,\n"+
"date="+date+",\n"+
"expver="+exp+",\n"+
"reportype="+repor+",\n"+
"stream="+stream+",\n"+
"type="+stype+",\n"+
"time=00:00:00/12:00:00,\n"+
"format=odb,\n"+
"target='"+ofile+"'")
os.system("rm -f request")
f= open("request","w")
f.write(request)
f.close()
os.system('mars request')
#clean odb
def clean_odb(self,list_file):
for ofile in list_file:
os.system('rm -f '+ofile)
#db read
def get_db(self,file_odb,satid,varno):
db = mv.read(file_odb)
try:
db = mv.odb_filter( odb_query = "select "+
"varno@body, satellite_instrument@sat, satellite_identifier@sat, "+
"lat@hdr, lon@hdr, time@hdr, date@hdr, "+
"obs_error@errstat, "+
"obsvalue@body, fg_depar@body, "+
" where varno@body == "+str(varno)+" and satellite_identifier@sat == "+str(satid),
odb_data = db )
tim = mv.values(db,"time@hdr")
dat = mv.values(db,"date@hdr")
lat = mv.values(db,"lat@hdr")
lon = mv.values(db,"lon@hdr")
obs = mv.values(db,"obsvalue@body")
err = mv.values(db,"obs_error@errstat")
fgd = mv.values(db,"fg_depar@body")
except:
tim = []
dat = []
lat = []
lon = []
obs = []
err = []
fgd = []
df = pd.DataFrame({'latitude':lat,'longitude':lon,'date':dat,'time':tim,'obsval':obs,'obserr':err,'departure':fgd})
return df
#db concat
def concat_db(self,list_f,sat,var):
#concatenate ODB
cf=0
#sat=64
#var=188
for file_odb in list_f:
df=self.get_db(file_odb,sat,var)
#day = get_lintime(dat,tim)
if cf == 0:
c_df=df
else:
c_df=pd.concat([df,c_df])
cf+=1
return c_df