forked from w2naf/grapeDRF_doppler_model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrapeDRF.py
More file actions
executable file
·232 lines (187 loc) · 8.28 KB
/
grapeDRF.py
File metadata and controls
executable file
·232 lines (187 loc) · 8.28 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
import os
import datetime
import logging
logger = logging.getLogger(__name__)
import pickle
import numpy as np
from scipy import signal
import matplotlib as mpl
from matplotlib import pyplot as plt
import digital_rf as drf
from eclipse_calc import solarContext
mpl.rcParams['font.size'] = 12
mpl.rcParams['font.weight'] = 'bold'
mpl.rcParams['axes.grid'] = True
mpl.rcParams['grid.linestyle'] = ':'
mpl.rcParams['figure.figsize'] = np.array([15, 8])
mpl.rcParams['axes.xmargin'] = 0
def load_grape_drf(sDate,eDate,data_dir,channel='ch0'):
# DATA LOADING #########################
meta_dir = os.path.join(data_dir,channel,'metadata')
do = drf.DigitalRFReader(data_dir)
dmr = drf.DigitalMetadataReader(meta_dir)
# 'center_frequencies': array([ 2.5 , 3.33, 5. , 7.85, 10. , 14.67, 15. , 20. , 25. ])
# Get first and last sample index of data.
# Index is the number of samples since the epoch (time_since_epoch*sample_rate)
s0, s1 = do.get_bounds(channel)
first_sample, last_sample = dmr.get_bounds()
print("metadata bounds are %i to %i" % (first_sample, last_sample))
start_idx = int(np.uint64(first_sample))
print('computed start_idx = ',start_idx)
fields = dmr.get_fields()
print("Available fields are <%s>" % (str(fields)))
print("first read - just get one column ")
data_dict = dmr.read(start_idx, start_idx + 2, "center_frequencies")
for key in data_dict.keys():
# print((key, data_dict[key]))
freqList = data_dict[key]
print("freq = ",freqList[0])
data_dict = dmr.read(start_idx, start_idx + 2, "lat")
for key in data_dict.keys():
# print((key, data_dict[key]))
theLatitude = data_dict[key]
print("Latitude: ",theLatitude)
data_dict = dmr.read(start_idx, start_idx + 2, "long")
for key in data_dict.keys():
# print((key, data_dict[key]))
theLongitude = data_dict[key]
print("Longitude: ",theLongitude)
latest_meta = dmr.read_latest()
latest_inx = list(latest_meta.keys())[0]
cntr_freqs = latest_meta[latest_inx]['center_frequencies']
properties = do.get_properties(channel)
fs = properties['samples_per_second']
sinx_0 = drf.util.time_to_sample(sDate,fs)
sinx_1 = drf.util.time_to_sample(eDate,fs)
blks = do.get_continuous_blocks(sinx_0,sinx_1,channel)
for sinx, nsamps in blks.items():
break # Get the first sample index and number of samples
bigarray_dct = {}
for cfreq in cntr_freqs:
bigarray_dct[cfreq] = np.zeros(nsamps,dtype=complex)
for cfreq_inx,(cfreq, bigarray) in enumerate(bigarray_dct.items()):
print()
print('Working on {!s} MHz....'.format(cfreq))
data = do.read_vector(sinx, nsamps, channel)
bigarray[:] = data[:,cfreq_inx]
result = {}
result['bigarray_dct'] = bigarray_dct
result['latest_meta'] = latest_meta[latest_inx]
result['properties'] = properties
t0 = drf.util.sample_to_datetime(sinx,fs)
result['timevec_utc'] = [t0+drf.util.samples_to_timedelta(x,fs) for x in range(nsamps)]
return result
class GrapeDRF(object):
def __init__(self,sDate,eDate,station,
output_dir=os.path.join('output','grapeDRF')):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
sDate_str = sDate.strftime('%Y%m%d.%H%M')
eDate_str = eDate.strftime('%Y%m%d.%H%M')
event_fname = '{!s}-{!s}_{!s}_grapeDRF'.format(sDate_str,eDate_str,station)
png_fname = event_fname+'.png'
png_fpath = os.path.join(output_dir,png_fname)
ba_fpath = os.path.join(output_dir,event_fname+'.ba.pkl')
data_dir = os.path.join('data','psws_grapeDRF',station)
if not os.path.exists(ba_fpath):
result = load_grape_drf(sDate,eDate,data_dir)
with open(ba_fpath,'wb') as fl:
pickle.dump(result,fl)
else:
print('Using cached file {!s}...'.format(ba_fpath))
with open(ba_fpath,'rb') as fl:
result = pickle.load(fl)
self.result = result
self.cfreqs = list(result['bigarray_dct'].keys())
self.fs = result['properties']['samples_per_second']
self.sDate = sDate
self.eDate = eDate
self.data_dir = data_dir
self.event_fname = event_fname
self.output_dir = output_dir
self.png_fpath = png_fpath
self.cmap = mpl.colors.LinearSegmentedColormap.from_list(" ", ["black","darkgreen","green","yellow","red"])
self.spectrum_timevec = None
def plot_figure(self,cfreqs=None,png_fpath=None,**kwargs):
print('Now plotting {!s}...'.format(self.event_fname))
if cfreqs is None:
cfreqs = self.cfreqs
ncols = 1
nrows = len(cfreqs)
ax_inx = 0
fig = plt.figure(figsize=(15,4*nrows))
for cfreq in cfreqs:
print(' {!s} MHz...'.format(cfreq))
ax_inx += 1
ax = fig.add_subplot(nrows,ncols,ax_inx)
self.plot_ax(cfreq,ax,**kwargs)
fig.tight_layout()
if png_fpath is None:
png_fpath = self.png_fpath
fig.savefig(png_fpath,bbox_inches='tight')
print(png_fpath)
def plot_ax(self,cfreq,ax,cmap=None,plot_colorbar=False,
xlim=None,
solar_lat=None,solar_lon=None,
overlaySolarElevation=True,overlayEclipse=False):
sDate = self.sDate
eDate = self.eDate
ylabel = []
# ylabel.append('{!s} MHz'.format(cfreq))
ylabel.append('Doppler Shift (Hz)')
ax.set_ylabel('\n'.join(ylabel))
ax.set_xlabel('UTC')
result = self.result
props = result['properties']
bigarray = self.result['bigarray_dct'].get(cfreq)
if bigarray is None:
msg = 'ERROR: No data for {!s} MHz'.format(cfreq)
ax.text(0.5,0.5,msg,ha='center',va='center',transform=ax.transAxes)
print(msg)
return
f, t_spec, Sxx = signal.spectrogram(bigarray,fs=self.fs,nfft=1024,window='hann',return_onesided=False)
if self.spectrum_timevec is None:
# TODO: Make this more clean in the future.
ts0 = min(result['timevec_utc']).timestamp()
ts1 = max(result['timevec_utc']).timestamp()
ts_vec = np.linspace(ts0,ts1,len(t_spec))
self.spectrum_timevec = [datetime.datetime.utcfromtimestamp(x) for x in ts_vec]
f = (np.fft.fftshift(f)).astype('float64') # Frequency needs to be in float64 for some reason...
Sxx = np.fft.fftshift(Sxx,axes=0)
Sxx_db = 10*np.log10(Sxx)
if cmap is None:
cmap = self.cmap
mpbl = ax.pcolormesh(self.spectrum_timevec,f,Sxx_db,cmap=cmap)
if plot_colorbar:
cbar = fig.colorbar(mpbl,label='PSD [dB]')
sts = solarContext.solarTimeseries(sDate,eDate,solar_lat,solar_lon)
odct = {'color':'white','lw':4,'alpha':0.75}
if overlaySolarElevation:
sts.overlaySolarElevation(ax,**odct)
if overlayEclipse:
sts.overlayEclipse(ax,**odct)
if xlim is None:
xlim = (sDate,eDate)
xticks = ax.get_xticks()
xtkls = []
for xtk in xticks:
dt = mpl.dates.num2date(xtk)
xtkl = dt.strftime('%H:%M')
xtkls.append(xtkl)
ax.set_xticks(xticks)
ax.set_xticklabels(xtkls)
if __name__ == '__main__':
station = 'w2naf'
sDate = datetime.datetime(2024,4,8)
eDate = datetime.datetime(2024,4,9)
figd = {}
figd['cfreqs'] = [20,15,10,5]
# figd['cfreqs'] = [10]
figd['solar_lat'] = 41.335116
figd['solar_lon'] = -75.600692
figd['overlaySolarElevation'] = True
figd['overlayEclipse'] = True
gDRF = GrapeDRF(sDate,eDate,station)
gDRF.plot_figure(**figd)
import ipdb; ipdb.set_trace()