-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveStereoImages.py
216 lines (179 loc) · 9.96 KB
/
SaveStereoImages.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
# -*- coding: utf-8 -*-
# Save stereo images .h5 file
#v1 17/3/21
#Original
#Catches bug if can't find image in ImageData
#Ignore 0 slice images
#Error with probe can cause particles to repeat themselves
#Catches bug when ImageSlices==0
import numpy as np
import h5py
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.colors as colors
#import matplotlib.colors.Colormap
import datetime
import os
from FlightInfo2DS import GetFlightInfo2DS
#__________________________________________________________________________________
#
#ImageCH0[ImageCH0 == 0 ] = 1
#ImageCH0[ImageCH0 == 255 ] = 0
# for each base.h5 file create 1 .h5 with stereo images
def SaveStereoImagesh5(Info2DS,FlightNumberStr,SizeThreshold):
Path2DS = Info2DS[FlightNumberStr,'Path2DS']
Path2DSsave = Info2DS[FlightNumberStr,'Path2DSsave']
ThresholdDeltaDimaterY = Info2DS[FlightNumberStr,'ThresholdDeltaDiameterY']
tmp = [F for F in os.listdir(Path2DSsave) if F.endswith(".h5") and F.startswith('dNdD_L_Colocate_')]
files = [x.replace('dNdD_L_Colocate_', '') for x in tmp]
for filena in files :
print(filena)
#Load colocation pbp statistics
Data_h5 = h5py.File(Path2DSsave + 'Colocate_'+filena, 'r')
ColocationParticleBufferTimeS_Ch0=np.array(Data_h5['ColocationParticleBufferTimeS_Ch0'])
ColocationParticleBufferTimeS_Ch1=np.array(Data_h5['ColocationParticleBufferTimeS_Ch1'])
ColocationImageID_Ch0=np.array(Data_h5['ColocationImageID_Ch0'])
ColocationImageID_Ch1=np.array(Data_h5['ColocationImageID_Ch1'])
ColocationEdgeCH0=np.array(Data_h5['ColocationEdgeCh0'])
ColocationEdgeCH1=np.array(Data_h5['ColocationEdgeCh1'])
ColocationMeanXYDiameter_Ch1 =np.array(Data_h5['ColocationMeanXYDiameter_Ch1'])
ColocationMeanXYDiameter_Ch0 =np.array(Data_h5['ColocationMeanXYDiameter_Ch0'])
ColocationSlicesY_Ch0 = np.array(Data_h5['ColocationSlicesY_Ch0'])
ColocationSlicesY_Ch1 = np.array(Data_h5['ColocationSlicesY_Ch1'])
ColocationSecondsCh0 = np.array(Data_h5['ColocationSecondsCh0'])
ColocationSecondsCh1 = np.array(Data_h5['ColocationSecondsCh1'])
Data_h5.close()
#Load images
Data_h5 = h5py.File(Path2DS+ 'Export_'+filena, 'r')
ImageTimes=np.array(Data_h5['ImageTimes'][:,0])
ImageSlices =np.array(Data_h5['ImageTimes'][:,1])
ImageID_Ch0 =np.array(Data_h5['ImageTimes'][:,2])
ImageID_Ch1 =np.array(Data_h5['ImageTimes'][:,2])
ImageSlices[ImageSlices<0] = np.nan
#Find start position of image within ImageData
ImagePosition = np.cumsum(ImageSlices, axis = 0)
ImagePosition = np.append(0, ImagePosition)
#Indexes to save images
Stereo_Idxs = np.nonzero(((ColocationMeanXYDiameter_Ch1 > SizeThreshold) | (ColocationMeanXYDiameter_Ch0 > SizeThreshold))
& ((ColocationEdgeCH0 == 0) | (ColocationEdgeCH1 == 0))
& (np.absolute(ColocationSlicesY_Ch0 - ColocationSlicesY_Ch1) < ThresholdDeltaDimaterY))
Stereo_Idxs=Stereo_Idxs[0]
# Number of slices per stereo images
OutputSlicesY_Ch0 = ColocationSlicesY_Ch0[Stereo_Idxs] /10 # needs to be in pixels not size
OutputSlicesY_Ch1 = ColocationSlicesY_Ch1[Stereo_Idxs] /10
#Position within output image array
OutputImagePositionCh0 = np.cumsum(OutputSlicesY_Ch0, axis = 0)
OutputImagePositionCh0 = np.append(0, OutputImagePositionCh0)
OutputImagePositionCh1 = np.cumsum(OutputSlicesY_Ch1, axis = 0)
OutputImagePositionCh1 = np.append(0, OutputImagePositionCh1)
# Set up output array
OutputImageCh0 = np.ones([128,int(np.nansum(OutputSlicesY_Ch0))], dtype=np.uint8)*255
OutputImageCh1 = np.ones([128,int(np.nansum(OutputSlicesY_Ch1))], dtype=np.uint8)*255
# select particles and put images in OutputImage
for j, Idx in enumerate(Stereo_Idxs) :
# find each image in array
#channel 0
Ch0i = np.nonzero((ImageTimes == ColocationParticleBufferTimeS_Ch0[Idx]) & (ImageID_Ch0 == ColocationImageID_Ch0[Idx]))
i = Ch0i[0]
if (len(i)==0):
print('Missing =' + str(i)) # if can't find particle
else :
if (len(i) > 1):
print('Multiple particles with same ID=' + str(i)) #repeat particle
i=i[0]
if (ImagePosition[i+1]-ImagePosition[i] != (ColocationSlicesY_Ch0[Idx]/10) ):
print('zero slice =' + str(i)) # 0 slice particle
else:
ImageCH0 = np.array(Data_h5['ImageData'][:,int(ImagePosition[i]):int(ImagePosition[i+1])])
#Add to output array
OutputImageCh0[:,int(OutputImagePositionCh0[j]):int(OutputImagePositionCh0[j+1])] = ImageCH0
#Channel 1
Ch1i = np.nonzero((ImageTimes == ColocationParticleBufferTimeS_Ch1[Idx]) & (ImageID_Ch1 == ColocationImageID_Ch1[Idx]))
i = Ch1i[0]
if (len(i)==0):
print('Missing =' + str(i)) # if can't find particle
else :
if (len(i) > 1):
print('Multiple particles with same ID=' + str(i)) #repeat particle
i=i[0]
if (ImagePosition[i+1]-ImagePosition[i] != (ColocationSlicesY_Ch1[Idx]/10) ):
print('zero slice =' + str(i)) # 0 slice particle
else:
ImageCH1 = np.array(Data_h5['ImageData'][:,int(ImagePosition[i]):int(ImagePosition[i+1])])
#Add to output array
OutputImageCh1[:,int(OutputImagePositionCh1[j]):int(OutputImagePositionCh1[j+1])] = ImageCH1
Data_h5.close()
# # Save the images as .h5
h5f = h5py.File(Path2DSsave+'StereoImages_'+filena, 'w')
h5f.create_dataset('ImageCh0', data=OutputImageCh0)
h5f.create_dataset('ImagePositionCh0', data=OutputImagePositionCh0)
h5f.create_dataset('SecondsCh0', data=ColocationSecondsCh0[Stereo_Idxs])
h5f.create_dataset('SlicesY_Ch0', data=OutputSlicesY_Ch0)
h5f.create_dataset('ImageCh1', data=OutputImageCh1)
h5f.create_dataset('ImagePositionCh1', data=OutputImagePositionCh1)
h5f.create_dataset('SecondsCh1', data=ColocationSecondsCh1[Stereo_Idxs])
h5f.create_dataset('SlicesY_Ch1', data=OutputSlicesY_Ch1)
h5f.close()
#__________________________________________________________________________________
#
# create one stereo image file per flight
#Flights = ['C174_dataPC', 'C172_dataPC', 'C171_dataPC', 'C170_dataPC', 'C169_dataPC',
# 'C098_dataPC', 'C097_dataPC']
Flights = ['C097_dataPC']
for FlightNumberStr in Flights :
print(FlightNumberStr)
Info2DS = GetFlightInfo2DS()
#FlightNumberStr = 'C171_dataPC'
SizeThreshold = 40 # min size particle to save
SaveStereoImagesh5(Info2DS,FlightNumberStr,SizeThreshold)
vnumber = 0
rnumber = 0
Path2DSsave = Info2DS[FlightNumberStr,'Path2DSsave']
FlightNumber = Info2DS[FlightNumberStr,'FlightNumber']
FlightDate = (Info2DS[FlightNumberStr,'FlightDate']).astype(datetime.datetime)
#Combine files
files = [F for F in os.listdir(Path2DSsave) if F.endswith(".h5") and F.startswith('StereoImages_')]
for i,F in enumerate(files) :
Data_h5 = h5py.File(Path2DSsave + F, 'r')
tmpImageCh0=np.array(Data_h5['ImageCh0'])
tmpImagePositionCh0=np.array(Data_h5['ImagePositionCh0'])
tmpSecondsCh0=np.array(Data_h5['SecondsCh0'])
tmpSlicesY_Ch0=np.array(Data_h5['SlicesY_Ch0'])
tmpImageCh1=np.array(Data_h5['ImageCh1'])
tmpImagePositionCh1=np.array(Data_h5['ImagePositionCh1'])
tmpSecondsCh1=np.array(Data_h5['SecondsCh1'])
tmpSlicesY_Ch1=np.array(Data_h5['SlicesY_Ch1'])
Data_h5.close()
if i == 0 :
ImageCh0 = tmpImageCh0
ImagePositionCh0= tmpImagePositionCh0
SecondsCh0=tmpSecondsCh0
SlicesY_Ch0=tmpSlicesY_Ch0
ImageCh1=tmpImageCh1
ImagePositionCh1=tmpImagePositionCh1
SecondsCh1=tmpSecondsCh1
SlicesY_Ch1=tmpSlicesY_Ch1
else:
ImageCh0= np.append(ImageCh0,tmpImageCh0, axis=1)
ImagePositionCh0= np.append(ImagePositionCh0,tmpImagePositionCh0[1:]+ImagePositionCh0[-1], axis=0)
SecondsCh0= np.append(SecondsCh0, tmpSecondsCh0, axis=0)
SlicesY_Ch0= np.append(SlicesY_Ch0,tmpSlicesY_Ch0, axis=0)
ImageCh1= np.append(ImageCh1,tmpImageCh1, axis=1)
ImagePositionCh1= np.append(ImagePositionCh1,tmpImagePositionCh1[1:]+ImagePositionCh1[-1], axis=0)
SecondsCh1= np.append(SecondsCh1, tmpSecondsCh1, axis=0)
SlicesY_Ch1= np.append(SlicesY_Ch1, tmpSlicesY_Ch1, axis=0)
# plt.pcolormesh(ImageCh1)
# plt.vlines(ImagePositionCh1, ymin=0,ymax=128)
# plt.xlim([102000,102500])
Mergedfilename='uman-2ds_faam_'+FlightDate.strftime("%Y%m%d")+'_v'+str(vnumber)+'_r'+str(rnumber)+'_'+FlightNumber+'_stereo_images.h5'
#save to file
h5f = h5py.File(Path2DSsave+Mergedfilename, 'w')
h5f.create_dataset('ImageCh0', data=ImageCh0)
h5f.create_dataset('ImagePositionCh0', data=ImagePositionCh0)
h5f.create_dataset('SecondsCh0', data=SecondsCh0)
h5f.create_dataset('ImageWidth_Ch0', data=SlicesY_Ch0)
h5f.create_dataset('ImageCh1', data=ImageCh1)
h5f.create_dataset('ImagePositionCh1', data=ImagePositionCh1)
h5f.create_dataset('SecondsCh1', data=SecondsCh1)
h5f.create_dataset('ImageWidthCh1', data=SlicesY_Ch1)
h5f.close()