-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpw.py
499 lines (401 loc) · 15.5 KB
/
lpw.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import numpy as np
import pylab as plt
import spiceypy
import celsius
from maven import sdc_interface
import cdflib
# CDFConverter = sdc_interface.CDFConverter
from functools import wraps
from matplotlib.colors import LogNorm
# from spacepy import pycdf
import os
from scipy.io.idl import readsav
def get_densities(start, finish=None, verbose=False, sweeps=True,
cleanup=False):
"""Routine to extract Dave's own processed densities. Ignore / don't use."""
raise RunTimeError("Not for use.")
if finish is None: finish = start + 86400. - 1.
if start > finish: raise ValueError("Start %f exceeds %f" % (start, finish))
directory = os.getenv("SC_DATA_DIR", os.path.expanduser("~/data/"))
directory += 'maven/ping/'
t = start
chunks = []
while t < finish:
try:
date = celsius.utcstr(t, 'ISOC')[:10]
fname = directory + date[:4] + '/' + date + '.sav'
tmp = readsav(fname)
if not 'sza' in tmp:
print(fname + ' out of date, skipping')
t+=86400.
continue
n = len(tmp['time'])
for k in ('sza', 'density', 'flag'):
if tmp[k].shape[-1] != n:
print('Malformed ', fname)
continue
chunks.append(tmp)
if verbose:
print(fname + ', ' + str(len(chunks[-1]['time'])))
except IOError as e:
if verbose:
print("Missing: " + fname)
t += 86400.
if not chunks:
print('No data found')
return chunks
banned_keys = ('probe', 'spec', 'spec_f', 'iv1', 'iv2') # The spectra are not retained
output = {}
for k in list(chunks[0].keys()):
# print k, chunks[0][k].shape
if k in banned_keys: continue
output[k] = np.hstack([c[k] for c in chunks])
print(output['sza'].shape == output['time'].shape)
if sweeps:
for k in ('iv1', 'iv2'):
output[k] = {}
for kk in chunks[0][k].dtype.names:
if kk.lower() in banned_keys: continue
output[k][kk.lower()] = np.hstack([c[k][kk][0] for c in chunks])
print(output['sza'].shape == output['time'].shape)
inx, = np.where((output['time'] > start) & (output['time'] < finish))
for k in list(output.keys()):
if k in banned_keys: continue
output[k] = output[k][...,inx]
inx = np.argsort(output['time'])
for k in list(output.keys()):
if k in banned_keys: continue
output[k] = output[k][...,inx]
if sweeps:
for k in ('iv1', 'iv2'):
inx, = np.where((output[k]['time'] > start) & (output[k]['time'] < finish))
for kk in list(output[k].keys()):
output[k][kk] = output[k][kk][inx]
inx = np.argsort(output[k]['time'])
for kk in list(output[k].keys()):
output[k][kk] = output[k][kk][inx]
if cleanup:
print("Cleaning up some timing error - any negative time steps being erased")
dt = np.diff(output['time'])
inx, = np.where(dt < 0.)
if inx.size != 0:
for k in list(output.keys()):
if k in banned_keys: continue
output[k][inx+1] *= np.nan
if sweeps:
for k in ('iv1', 'iv2'):
inx, = np.where(np.diff(output[k]['time']) < 0.)
if inx.size != 0:
for kk in list(output[k].keys()):
output[k][kk][inx + 1] *= np.nan
return output
def spice_wrapper(n=1):
"""Wrapper around spiceypy.spkpos that handles array inputs, and provides useful defaults"""
def actual_decorator(f):
def g(t):
try:
return f(t)
except spiceypy.SpiceException:
return np.repeat(np.nan, n)
@wraps(f)
def inner(time):
if hasattr(time, '__iter__'):
return np.vstack([g(t) for t in time]).T
else:
return g(time)
return inner
return actual_decorator
@spice_wrapper(n=3)
def ram_angles(time):
"""Return the angle between SC-Y and the ram direction (0. = Y to ram)"""
p = spiceypy.spkezr('MAVEN', time, 'IAU_MARS', 'NONE', 'MARS')[0][3:]
r = spiceypy.pxform('IAU_MARS', 'MAVEN_SPACECRAFT', time)
a = spiceypy.mxv(r, p)
e = np.arctan( np.sqrt(a[1]**2. + a[2]**2.) / a[0]) * 180./np.pi
f = np.arctan( np.sqrt(a[0]**2. + a[2]**2.) / a[1]) * 180./np.pi
g = np.arctan( np.sqrt(a[0]**2. + a[1]**2.) / a[2]) * 180./np.pi
if e < 0.: e = e + 180.
if f < 0.: f = f + 180.
if g < 0.: g = g + 180.
return np.array((e,f,g))
@spice_wrapper(n=3)
def sun_angles(time):
"""Return the angle between SC-Y and the sun direction (0. = Y to sun)"""
a = spiceypy.spkpos('SUN', time, 'MAVEN_SPACECRAFT', 'NONE', 'MAVEN')[0]
e = np.arctan( np.sqrt(a[1]**2. + a[2]**2.) / a[0]) * 180./np.pi
f = np.arctan( np.sqrt(a[0]**2. + a[2]**2.) / a[1]) * 180./np.pi
g = np.arctan( np.sqrt(a[0]**2. + a[1]**2.) / a[2]) * 180./np.pi
if e < 0.: e = e + 180.
if f < 0.: f = f + 180.
if g < 0.: g = g + 180.
return np.array((e,f,g))
def lpw_l2_load(start, finish, kind='lpnt', http_manager=None, cleanup=False,
verbose=None):
"""Finds and loads LPW L2 data"""
if http_manager is None: http_manager = sdc_interface.maven_http_manager
kind = kind.lower()
t = start
year, month = celsius.utcstr(t,'ISOC').split('-')[0:2]
year = int(year)
month = int(month)
# Each month:
files = []
while t < finish:
# print year, month
files.extend(
http_manager.query(
'lpw/l2/%04d/%02d/mvn_lpw_l2_%s_*_v*_r*.cdf' % \
(year, month, kind),
start=start, finish=finish,
version_function=\
lambda x: (x[0], float(x[1]) + float(x[2])/100.),
date_function=lambda x:
sdc_interface.yyyymmdd_to_spiceet(x[0]),
verbose=verbose
)
)
month += 1
if month > 12:
month = 1
year += 1
t = celsius.spiceet('%d-%02d-01T00:00' % (year, month))
# Check for duplicates:
if len(files) != len(set(files)):
raise ValueError("Duplicates appeared in files to load: " + ", ".join(files))
if cleanup:
print('LPW L2 cleanup complete')
return
if not files:
raise IOError("No data found")
for f in sorted(files):
if not os.path.exists(f):
raise IOError("%s does not exist" % f)
if kind == 'lpnt':
output = dict(time=None, ne=None, te=None, usc=None)
for f in sorted(files):
c = cdflib.CDF(f)
if output['time'] is None:
# inx =
output['time'] = c['time_unix']
output['ne'] = c['data'][:,0]
output['te'] = c['data'][:,1]
output['usc'] = c['data'][:,2]
else:
output['time'] = np.hstack((output['time'],
c['time_unix']))
for v, i in zip(('ne', 'te', 'usc'), (0,1,2)):
output[v] = np.hstack((output[v], c['data'][:,i]))
c.close()
elif kind == 'wn':
output = dict(time=None, ne=None)
for f in sorted(files):
print(f)
c = cdflib.CDF(f)
if output['time'] is None:
# inx =
output['time'] = c['time_unix']
output['ne'] = c['data']
else:
output['time'] = np.hstack((output['time'],
c['time_unix']))
output['ne'] = np.hstack((output['ne'],
c['data']))
# for v, i in zip(('ne', 'te', 'usc'), (0,1,2)):
# output[v] = np.hstack((output[v], c.varget('data'][:,i])))
c.close()
elif kind == 'wspecact':
output = dict(time=None, spec=None, freq=None)
for f in sorted(files):
print(f)
c = cdflib.CDF(f)
if output['time'] is None:
output['time'] = c['time_unix']
output['spec'] = c['data'].T
output['freq'] = c['freq'][0,:]
else:
output['time'] = np.hstack((output['time'],
c['time_unix']))
output['spec'] = np.hstack((output['spec'],
c['data'].T))
c.close()
# print 'Warning: spectra output is not interpolated!'
elif kind == 'wspecpas':
output = dict(time=None, spec=None, freq=None)
for f in sorted(files):
print(f)
c = cdflib.CDF(f)
if output['time'] is None:
output['time'] = c['time_unix']
output['spec'] = c['data'].T
output['freq'] = c['freq'][0,:]
else:
output['time'] = np.hstack((output['time'],
c['time_unix']))
output['spec'] = np.hstack((output['spec'],
c['data'].T))
# print 'Warning: spectra output is not interpolated!'
c.close()
elif kind == 'lpiv':
output = dict(time=None, current=None, volt=None)
for f in sorted(files):
c = cdflib.CDF(f)
if output['time'] is None:
output['time'] = c['time_unix']
output['current'] = c['data'].T
output['volt'] = c['volt'].T
else:
output['time'] = np.hstack((output['time'],
c['time_unix']))
output['current'] = np.hstack((
output['current'], c['data'].T))
output['volt'] = np.hstack((
output['volt'], c['volt'].T))
c.close()
else:
raise ValueError("Input kind='%s' not recognized" % kind)
output['time'] = output['time'] + celsius.spiceet("1970-01-01T00:00")
return output
def lpw_plot_spec(s, ax=None, cmap=None, norm=None,
max_frequencies=512, max_times=2048, fmin=None, fmax=None,
labels=True, colorbar=True, full_resolution=False):
"""Transform and plot a spectra dictionary generated by lpw_load.
Doesn't interpolate linearly, but just rebins data. Appropriate for presentation purposes, but don't do science with the results."""
if ax is None:
ax = plt.gca()
else:
plt.sca(ax)
if cmap is None: cmap = 'Spectral_r'
if norm is None: norm = LogNorm(1e-16, 1e-8)
img_obj = plt.pcolormesh(s['time'], s['freq'], s['spec'],
cmap=cmap, norm=norm)
plt.yscale('log')
# plt.xlim(t0, t1)
if labels:
plt.ylabel('f / Hz')
if colorbar:
cbar = plt.colorbar(cax=celsius.make_colorbar_cax())
cbar.set_label(r'V$^2$ m$^{-2}$ Hz$^{-1}$')
else:
cbar = None
return img_obj, cbar
def lpw_plot_iv(s, boom=1, ax=None, cmap=None, norm=None,
start=None, finish=None,
voltage=None,
labels=True, colorbar=True, log_abs=True):
"""Plot LP IV sweeps as a time series."""
if ax is None:
ax = plt.gca()
else:
plt.sca(ax)
if cmap is None:
plt.set_cmap('viridis')
if log_abs is False:
plt.set_cmap('RdBu_r')
cmap = plt.get_cmap()
cmap.set_bad('grey')
if not norm:
norm = plt.Normalize(1e-7, 1e-7)
if log_abs:
norm = LogNorm(1e-9, 1e-5)
d = s['current']
if log_abs:
d = np.abs(d)
img_obj = plt.pcolormesh(s['time'], s['volt'], d, cmap=cmap, norm=norm)
if labels:
plt.ylabel(r'U$_{Bias}$ / V')
if colorbar:
cbar = plt.colorbar(cax=celsius.make_colorbar_cax(ax))
cbar.set_label(r'i / A')
else:
cbar = None
return img_obj, cbar
def cleanup(start=None, finish=None):
if not start: start = celsius.spiceet("2014-09-22T00:00")
if not finish: finish = celsius.now()
# Cleanup commands
lpw_l2_load(start, finish, cleanup=True, verbose=True)
if __name__ == '__main__':
import maven
import mex
if False:
t0 = celsius.spiceet("2015-01-07T00:00")
c = get_hf_act_densities(t0, t0 + 86400.*2., verbose=True)
print(list(c.keys()))
inx = c['confidence'] > 95.
# plt.close('all')
fig, axs = plt.subplots(3,1, sharex=True)
plt.sca(axs[0])
plt.plot(c['time'][inx], c['density'][inx], 'r.')
# plt.plot(mexdata['time'], mexdata['ne'], 'b.')
plt.ylabel("ne / cm^-3")
plt.yscale('log')
plt.sca(axs[1])
time = np.linspace(c['time'][0], c['time'][-1], 500)
plt.plot(time, sun_z_angle(time), 'r-')
plt.plot(time, ram_z_angle(time), 'k-')
plt.fill_between(plt.xlim(), (55-10, 55-10), (55+10, 55+10),
facecolor='b', alpha=0.3, zorder=-99)
plt.ylabel(r"$\theta$ / deg")
# plt.ylim(-180., 180.)
plt.sca(axs[2])
mso, sza = maven.mso_r_lat_lon_position(time, sza=True)
plt.plot(time, sza)
plt.ylabel("SZA / deg")
celsius.setup_time_axis()
plt.show()
if True:
plt.close('all')
start = celsius.spiceet("2015-04-23T06:00")
finish = start + 86400. /2.
# finish = start + 86400. * 2. - 1.
xl = np.array((start, finish))
xo = np.array((1,1))
o = lpw_l2_load(kind='wspecact', start=start, finish=finish)
o2 = lpw_l2_load(kind='wn', start=start, finish=finish)
o3 = lpw_l2_load(kind='lpnt', start=start, finish=finish)
inx = np.isfinite(o2['ne'])
ne_w = np.interp(o3['time'], o2['time'][inx], o2['ne'][inx])
fig, axs = plt.subplots(4,1, sharex=True, figsize=(8,12), gridspec_kw=dict(height_ratios=(5,2,2,2)))
plt.subplots_adjust(hspace=0.01)
plt.sca(axs[0])
lpw_plot_spec(o, colorbar=False, full_resolution=True, fmin=2e4)
# plt.ylim(1e4, 2e6)
plt.plot(o2['time'], 8980*np.sqrt(o2['ne']), 'k+')
plt.plot(o3['time'], 8980*np.sqrt(o3['ne']), 'r+')
# plt.plot(o3['time'], 8980*np.sqrt(ne_w), 'b*')
plt.sca(axs[1])
plt.plot(o3['time'], np.sqrt(o3['ne']/ne_w), 'k.')
plt.plot(xl, xo * 1., 'b--')
plt.plot(xl, xo * 2., 'b--')
plt.plot(xl, xo * np.sqrt(2), 'b:')
plt.yscale('log')
plt.ylim(0.1, 10.)
plt.ylabel(r'$f_{pe,IV} / f_{pe,W}$')
plt.sca(axs[2])
plt.plot(o3['time'], o3['te']/11604., 'k.')
plt.yscale('log')
plt.ylabel('Te / eV')
plt.sca(axs[3])
plt.plot(o3['time'], 0.069 * np.sqrt(o3['te']/11604. / o3['ne']), 'k.')
plt.plot(o3['time'], 0.069 * np.sqrt(o3['te']/11604. / ne_w), 'r.')
plt.plot(xl, xo * 0.0063/2., 'b--')
plt.plot(xl, xo * 0.05/2., 'b:')
plt.plot(xl, xo * 0.4/2., 'b--')
plt.ylim(5e-5, 2e-3)
plt.yscale('log')
plt.ylabel(r'$\lambda_D$/m')
celsius.setup_time_axis()
plt.figure()
plt.scatter(
0.069 * np.sqrt(o3['te']/11604. / o3['ne']),
np.sqrt(o3['ne']/ne_w), c=o3['time'], marker='.', edgecolor='none'
)
plt.ylabel(r'$f_{pe,IV} / f_{pe,W}$')
plt.xlabel(r'$\lambda_D$/m')
plt.xscale('log')
plt.yscale('log')
plt.xlim(5e-5, 2e-3)
plt.ylim(0.1, 10.)
x = np.array((5e-5, 2e-3))
plt.plot(x, 10.**(-0.24*np.log10(x)-0.7))
plt.show()