-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.py
214 lines (192 loc) · 6.23 KB
/
process.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# coding:utf-8
"""Read IONEX file and draw a ion map."""
import os
import datetime
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
class TecMap(object):
"""Tec Map."""
latrange = None
lonrange = None
startepoch = None
endepoch = None
def __init__(self):
"""Initialize."""
self.LAT = list()
self.LON = list()
self.value = list()
self.epoch = None
def roundb(num, base=10):
return int(base * round(float(num) / base))
def process(targetpath, storepath, interval, start=None, end=None, bound=None, colorbar=100, ratio=10./8):
""""process.
Args:
targetpath:target file path.
storepath:store file path.
interval:plot interval.
bound:bound
colorbar:colorbar range
ratio:ratio
"""
if not os.path.exists(targetpath):
print 'Nont find %s' % targetpath
return
if not os.path.exists(storepath):
print 'Nont find %s' % storepath
print 'Try to make it......'
try:
os.makedirs(storepath)
except:
print 'Make falied!'
return
with open(targetpath) as f:
if not readheader(f):
return
if not start:
epoch = TecMap.startepoch
else:
hm = map(int, start.split(':'))
epoch = TecMap.startepoch
epoch = datetime.datetime(epoch.year, epoch.month, epoch.day, hm[0], hm[1], 0)
TecMap.startepoch = epoch
if end:
hm = map(int, start.split(':'))
endepoch = TecMap.startepoch
endepoch = datetime.datetime(epoch.year, epoch.month, epoch.day, hm[0], hm[1], 0)
TecMap.endepoch = endepoch
for line in f:
if 'START OF TEC MAP' in line:
tecmap = TecMap()
if not readtecmap(f, interval, tecmap):
continue
plottecmap(tecmap, storepath, bound, colorbar, ratio)
if TecMap.endepoch:
if epoch == TecMap.endepoch:
break
epoch += datetime.timedelta(seconds=interval)
if 'START OF RMS MAP' in line:
break
def readheader(f):
"""Read header.
Arg:
f:file handle.
"""
try:
for line in f:
if 'EPOCH OF FIRST MAP' in line:
datelst = map(int, line[:line.index('E')].split())
startepoch = datetime.datetime(*datelst)
TecMap.startepoch = startepoch
continue
if 'LAT1 / LAT2 / DLAT' in line:
lats = map(float, line[:line.index('L')].split())
TecMap.latrange = lats
continue
if 'LON1 / LON2 / DLON' in line:
lons = map(float, line[:line.index('L')].split())
TecMap.lonrange = lons
continue
if 'END OF HEADER' in line:
return 1
except:
return 0
def readtecmap(f, interval, tecmap):
"""read tec map data.
Arg:
f:file handle.
interval:interval.
tecmap:Tec Map.
"""
try:
for line in f:
if 'EPOCH OF CURRENT MAP' in line:
datelst = map(int, line[:line.index('E')].split())
cur_epoch = datetime.datetime(*datelst)
seconds = int((cur_epoch - TecMap.startepoch).total_seconds())
if seconds % interval != 0:
return 0
tecmap.epoch = cur_epoch
continue
if 'LAT/LON1/LON2/DLON/H' in line:
LAT = float(line[1:8])
LON1 = float(line[8:14])
LON2 = float(line[14:20])
DLON = float(line[20:26])
LONS = np.arange(LON1, LON2 + DLON, DLON).tolist()
tecmap.LAT.append([LAT] * len(LONS))
tecmap.LON.append(LONS)
values = list()
for i in range(int(np.ceil(len(LONS) / 16.))):
temp = next(f)
for value in temp.split():
values.append(float(value) * 0.1)
tecmap.value.append(values)
continue
if 'END OF TEC MAP' in line:
return 1
except:
return 0
def plottecmap(tecmap, storepath, bound=None, colorbar=100, ratio=10./8):
"""Plot tecmap.
Arg:
tecmap:Tec Map.
storepath:store file path.
bound:bound
ratio:axes ratio
"""
try:
plt.style.use('ggplot')
if not bound:
lonmin = TecMap.lonrange[0]
lonmax = TecMap.lonrange[1]
latmin = TecMap.latrange[1]
latmax = TecMap.latrange[0]
else:
lonmin = bound[0]
lonmax = bound[1]
latmin = bound[2]
latmax = bound[3]
m = Basemap(
projection='cyl',
llcrnrlon=lonmin,
urcrnrlon=lonmax,
llcrnrlat=latmin,
urcrnrlat=latmax,
fix_aspect=False)
m.aspect = ratio
m.drawparallels(
np.arange(roundb(latmin), latmax, 10),
labels=[1, 0, 0, 0],
linewidth=0,
size=10,
weight='bold')
m.drawmeridians(
np.arange(roundb(lonmin), lonmax, 10),
labels=[0, 0, 0, 1],
linewidth=0,
size=10,
weight='bold')
m.drawcoastlines()
m.drawcountries()
m.pcolormesh(
tecmap.LON,
tecmap.LAT,
tecmap.value,
vmin=0,
vmax=colorbar,
shading='gouraud',
latlon=True)
cb = m.colorbar(location='right', size="5%", pad='2%')
cb.ax.set_title('TECU', size=10, weight='bold')
plt.setp(cb.ax.yaxis.get_ticklabels(), size=10, weight='bold')
plt.title(str(tecmap.epoch), size=18, weight='bold')
fig_name = '%s.png' % tecmap.epoch.strftime('%Y-%m-%d-%H-%M-%S')
fig_path = os.path.join(storepath, fig_name)
plt.savefig(fig_path, bbox_inches='tight')
plt.clf()
plt.close()
except:
raise
return