Debian Codegraph @

doris_stack/functions/concatenate_decatenate.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 from doris.doris_stack.main_code.resdata import ResData
2 import numpy as np
3 import os, sys
4
5
6 def decatenate(date_folder, image_file, burst_file, datatype, multilooked='none', res_type='master'):
7 # Decatenate data.
8 # Multilooks can be none > not concatenated, only > only multilooked images are concatenated or >
9 # all > both original and multilooked images are concatenated.
10
11 if len(multilooked) == 7:
12 master = os.path.join(date_folder, image_file[:-4] + '_' + multilooked + '.raw')
13 else:
14 master = os.path.join(date_folder, image_file)
15
16 # Load .res files
17 image_res, burst_res = read_res(date_folder, type=res_type)
18
19 # Read image size
20 bursts = burst_res.keys()
21 if multilooked != 'none':
22 try:
23 no_lines = int(burst_res[bursts[0]].processes['readfiles']['Number_of_ml_lines_output_image'])
24 no_pixels = int(burst_res[bursts[0]].processes['readfiles']['Number_of_ml_pixels_output_image'])
25 except:
26 print('Not able to load multilooking parameters for ' + image_file)
27 return
28 else:
29 no_lines = int(burst_res[bursts[0]].processes['readfiles']['Number_of_lines_output_image'])
30 no_pixels = int(burst_res[bursts[0]].processes['readfiles']['Number_of_pixels_output_image'])
31
32 # First use memmap to get a memory map of the full file.
33 full_image = np.memmap(master, dtype=datatype, mode='r', shape=(no_lines, no_pixels))
34
35 for burst in bursts:
36 # Finally write all data from individual bursts to master file. We assume a simple 20 pixel offset from
37 # the side to prevent copying data without information.
38
39 burst_dat, line_0, line_1, pix_0, pix_1, burst_pix, burst_line, az_offset, ra_offset = \
40 burst_info(burst, burst_file, burst_res, multilooked)
41
42 # Cut out data with border of 20 px and write to file.
43 burst_image = np.memmap(burst_dat, dtype=datatype, mode='w+', shape=(burst_line, burst_pix))
44 burst_image[:, :] = full_image[line_0-1:line_1, pix_0-1:pix_1]
45 burst_image.flush()
46
47
48 def concatenate(date_folder, image_file, burst_file, datatype, multilooked='none', res_type='master'):
49 # Concatenate data.
50
51 if len(multilooked) == 7:
52 master = os.path.join(date_folder, image_file[:-4] + '_' + multilooked + '.raw')
53 else:
54 master = os.path.join(date_folder, image_file)
55
56 # Load .res files
57 image_res, burst_res = read_res(date_folder, type=res_type)
58
59 # Read image size
60 bursts = burst_res.keys()
61 if multilooked != 'none':
62 try:
63 no_lines = int(burst_res[bursts[0]].processes['readfiles']['Number_of_ml_lines_output_image'])
64 no_pixels = int(burst_res[bursts[0]].processes['readfiles']['Number_of_ml_pixels_output_image'])
65 except:
66 print('Not able to load multilooking parameters for ' + image_file)
67 return
68 else:
69 no_lines = int(burst_res[bursts[0]].processes['readfiles']['Number_of_lines_output_image'])
70 no_pixels = int(burst_res[bursts[0]].processes['readfiles']['Number_of_pixels_output_image'])
71
72 # First use memmap to get a memory map of the full file.
73 full_image = np.memmap(master, dtype=datatype, mode='w+', shape=(no_lines, no_pixels))
74
75 for burst in bursts:
76 # Finally write all data from individual bursts to master file. We assume a simple 20 pixel offset from
77 # the side to prevent copying data without information. (corrected with multilooking factor)
78
79 burst_dat, line_0, line_1, pix_0, pix_1, burst_pix, burst_line, daz, dra = \
80 burst_info(burst, burst_file, burst_res, multilooked)
81
82 # Cut out data with border of 20 px and write to file.
83 burst_image = np.memmap(burst_dat, dtype=datatype, mode='r', shape=(burst_line,burst_pix))
84 full_image[(line_0+(daz-1)):(line_1-daz), (pix_0+(dra-1)):(pix_1-dra)] = burst_image[daz:-daz, dra:-dra]
85
86
87 def burst_info(burst, burst_file, burst_res, multilooked='none'):
88 # Information about this specific burst
89
90 if burst_file == 'master.raw':
91 if multilooked:
92 string = '_iw_' + burst[6] + '_burst_' + burst[17:] + '_' + multilooked + '.raw'
93 else:
94 string = '_iw_' + burst[6] + '_burst_' + burst[17:] + '.raw'
95 elif burst_file == 'master_deramped.raw':
96 if multilooked:
97 string = '_iw_' + burst[6] + '_burst_' + burst[17:] + '_deramped' + '_' + multilooked + '.raw'
98 else:
99 string = '_iw_' + burst[6] + '_burst_' + burst[17:] + '_deramped.raw'
100 else:
101 string = burst_file
102
103 if len(multilooked) == 7:
104 burst_dat = os.path.join(date_folder, burst[0:7], burst[8:], string[:-4] + '_' + multilooked + '.raw')
105 line_0 = int(burst_res[burst].processes['readfiles']['First_line_' + multilooked])
106 line_1 = int(burst_res[burst].processes['readfiles']['Last_line_ml_' + multilooked])
107 pix_0 = int(burst_res[burst].processes['readfiles']['First_pixel_ml_' + multilooked])
108 pix_1 = int(burst_res[burst].processes['readfiles']['Last_pixel_ml_' + multilooked])
109 ra = int(burst_res[burst].processes['readfiles']['Multilook_range_' + multilooked])
110 az = int(burst_res[burst].processes['readfiles']['Multilook_azimuth_' + multilooked])
111 else:
112 burst_dat = os.path.join(date_folder, burst[0:7], burst[8:], string)
113 line_0 = int(burst_res[burst].processes['readfiles']['First_line (w.r.t. output_image)'])
114 line_1 = int(burst_res[burst].processes['readfiles']['Last_line (w.r.t. output_image)'])
115 pix_0 = int(burst_res[burst].processes['readfiles']['First_pixel (w.r.t. output_image)'])
116 pix_1 = int(burst_res[burst].processes['readfiles']['Last_pixel (w.r.t. output_image)'])
117 ra = 1
118 az = 1
119
120 az_offset = int(np.ceil(20 / float(az)))
121 ra_offset = int(np.ceil(100 / float(ra)))
122
123 burst_pix = pix_1 - pix_0 + 1
124 burst_line = line_1 - line_0 + 1
125
126 return burst_dat, line_0, line_1, pix_0, pix_1, burst_pix, burst_line, az_offset, ra_offset
127
128
129 def read_res(date_folder, type='master'):
130 # Read .res data to the burst objects. Generally done after a processing step.
131
132 swaths = next(os.walk(date_folder))[1]
133 swaths = [fol for fol in swaths if len(fol) == 7]
134
135 res_burst = dict()
136
137 for swath in swaths:
138
139 bursts = next(os.walk(os.path.join(date_folder, swath)))[1]
140 bursts = [burst for burst in bursts if burst.startswith('burst')]
141
142 for burst in bursts:
143 slave_res = os.path.join(date_folder, swath, burst, 'slave.res')
144 master_res = os.path.join(date_folder, swath, burst, 'master.res')
145
146 burst_name = swath + '_' + burst
147
148 if type == 'master' and os.path.exists(master_res):
149 res_burst[burst_name] = ResData(filename=master_res)
150 elif type == 'slave' and os.path.exists(slave_res):
151 res_burst[burst_name]= ResData(filename=slave_res)
152 elif os.path.exists(master_res):
153 res_burst[burst_name] = ResData(filename=master_res)
154 elif os.path.exists(slave_res):
155 res_burst[burst_name]= ResData(filename=slave_res)
156 else:
157 print('No burst master or slave image available')
158 return
159
160 slave_res = os.path.join(date_folder, 'slave.res')
161 master_res = os.path.join(date_folder, 'master.res')
162
163 if type == 'master' and os.path.exists(master_res):
164 res_image = ResData(filename=master_res)
165 elif type == 'slave' and os.path.exists(slave_res):
166 res_image = ResData(filename=slave_res)
167 elif os.path.exists(master_res):
168 res_image = ResData(filename=master_res)
169 elif os.path.exists(slave_res):
170 res_image = ResData(filename=slave_res)
171 else:
172 print('No image master or slave image available')
173 return
174
175 return res_image, res_burst
176
177 # Actually execute the code...
178 if __name__ == "__main__":
179
180 date_folder = sys.argv[1]
181 type = sys.argv[2]
182 burst_file = sys.argv[3]
183 datatype = sys.argv[4]
184 if len(sys.argv) > 5:
185 multilooked = sys.argv[5]
186 else:
187 multilooked = 'False'
188 if len(sys.argv) > 6:
189 res_type = sys.argv[6]
190 else:
191 res_type = 'master'
192
193 print('concatenate folder is ' + date_folder)
194 print('burst_file is ' + burst_file)
195 print('datatype is ' + datatype)
196 print('concatenating multilooked image is ' + multilooked)
197
198 if len(multilooked) == 7:
199 # Multilooked should be given in a 7 length string azimuth multilook _ range multiloop
200 # example '004_020'
201 # The script detects whether this multilooking is available. Otherwise it will produce an error.
202 multilooked = multilooked
203 else:
204 multilooked = 'none'
205 image_file = burst_file
206
207 if datatype == 'cpxint16' or datatype == 'complex_short':
208 datatype = np.dtype([('re', np.int16), ('im', np.int16)])
209
210 if type == 'decatenate':
211 decatenate(date_folder, image_file, burst_file, datatype, multilooked, res_type)
212 elif type == 'concatenate':
213 concatenate(date_folder, image_file, burst_file, datatype, multilooked, res_type)
214 else:
215 sys.exit('type should either be decatenate or concatenate')