This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
PatchAuroraldet.py
executable file
·106 lines (77 loc) · 2.85 KB
/
PatchAuroraldet.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
#!/usr/bin/env python
"""
used to patch "auroraldet.h5" that has false detections from clouds or sunrise
"""
from pathlib import Path
from shutil import copy2
import h5py
from matplotlib.pyplot import figure, draw, pause, show
from argparse import ArgumentParser
def patchdet(infn, slic=None, vlim=None, quiet=False):
infn = Path(infn).expanduser()
if slic is None:
plotdet(infn, vlim=vlim)
return
assert isinstance(slic, slice), 'Must be a slice to patch file, or None to display contents'
# if outfn is None or (outfn.is_file() and outfn.samefile(infn)):
outfn = infn.parent/(infn.stem + '_patched.h5')
print(f'copying {infn} to {outfn}, setting "detect" slice {slic} to 0')
copy2(infn, outfn)
with h5py.File(outfn, 'r+') as f:
f['/detect'][slic] = 0
plotdet(infn, outfn, vlim, quiet)
def plotdet(infn, outfn=None, vlim=None, quiet=False):
with h5py.File(infn, 'r') as f:
indet = f['/detect'][:]
if 'preview' in f and not quiet:
print('plotting movie of', infn)
fg = figure()
ax = fg.gca()
if vlim is None:
h = ax.imshow(f['/preview'][1])
else:
h = ax.imshow(f['/preview'][1], vmin=vlim[0], vmax=vlim[1])
fg.colorbar(h, ax=ax)
ht = ax.set_title('')
Nfile = f['/nfile'][()]
decim = f['/previewDecim'][()]
step = f['/framestep'][()]
for i, I in enumerate(f['/preview']):
h.set_data(I)
ht.set_text(f'{step*decim*i} / {Nfile}')
draw()
pause(0.1)
# %%
if outfn is None:
return
with h5py.File(outfn, 'r') as f:
outdet = f['/detect'][:]
fg = figure()
ax = fg.gca()
ax.plot(indet, 'b', label='original')
ax.plot(outdet, 'r', label='patched')
ax.set_title(f'{outfn} Auroral Detections')
ax.legend()
outimg = outfn.with_suffix('.png')
print('saving', outimg)
fg.savefig(outimg, bbox_inches='tight', dpi=100)
def main():
p = ArgumentParser()
p.add_argument('fn', help='HDF5 file to manuall patch over (remove false detections due to sunrise)')
p.add_argument('-s', '--startstop', help='length 1 or 2 (start,stop) slice', nargs='+', type=int)
p.add_argument('-q', '--quiet', help='dont show preview movie', action='store_true')
p.add_argument('-vlim', help='preview brightness', nargs=2, type=int)
p = p.parse_args()
if p.startstop is not None:
if len(p.startstop) == 1:
slic = slice(p.startstop[0], None)
elif len(p.startstop) == 2:
slic = slice(p.startstop[0], p.startstop[1])
else:
raise ValueError('start or start stop')
else:
slic = None
patchdet(p.fn, slic, p.vlim, p.quiet)
show()
if __name__ == '__main__':
main()