-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.py
executable file
·134 lines (129 loc) · 7.47 KB
/
reader.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
# Read header
import numpy as np
import os
import matplotlib
def read_header(infile):
"""Read image header (first 512 bytes)
"""
h = dict()
fid = open(infile, 'r+b')
h['filename'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 20))
h['parent_filename'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 20))
h['comments1'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 80))
h['comments2'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 80))
h['energy_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['config_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['file_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['trans_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['scan_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['data_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['date_modified'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 16))
h['frequency'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['mat_velocity'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['num_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)
h['num_polarization_channels'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['spare00'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['adc_min_voltage'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['adc_max_voltage'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['band_width'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['spare01'] = np.fromfile(fid, dtype = np.int16, count = 5)
h['polarization_type'] = np.fromfile(fid, dtype = np.int16, count = 4)
h['record_header_size'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['word_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['word_precision'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['min_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['max_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['avg_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['data_scale_factor'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['data_units'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['surf_removal'] = np.fromfile(fid, dtype = np.uint16, count = 1)
h['edge_weighting'] = np.fromfile(fid, dtype = np.uint16, count = 1)
h['x_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)
h['y_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)
h['z_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)
h['t_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)
h['spare02'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['x_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['scan_orientation'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['scan_direction'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['data_storage_order'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['scanner_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['x_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['t_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['num_x_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)
h['num_y_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)
h['num_z_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)
h['num_t_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)
h['x_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['x_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['x_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['x_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['date_processed'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 8))
h['time_processed'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 8))
h['depth_recon'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['x_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['elevation_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['roll_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['azimuth_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['adc_type'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['spare06'] = np.fromfile(fid, dtype = np.int16, count = 1)
h['scanner_radius'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['x_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['y_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['z_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['t_delay'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['range_gate_start'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['range_gate_end'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['ahis_software_version'] = np.fromfile(fid, dtype = np.float32, count = 1)
h['spare_end'] = np.fromfile(fid, dtype = np.float32, count = 10)
return h
# Read image data
def read_data(infile):
"""Read any of the 4 types of image files, returns a numpy array of the image contents
"""
extension = os.path.splitext(infile)[1]
h = read_header(infile)
nx = int(h['num_x_pts'])
ny = int(h['num_y_pts'])
nt = int(h['num_t_pts'])
fid = open(infile, 'rb')
fid.seek(512) #skip header
if extension == '.aps' or extension == '.a3daps':
if(h['word_type']==7): #float32
data = np.fromfile(fid, dtype = np.float32, count = nx * ny * nt)
elif(h['word_type']==4): #uint16
data = np.fromfile(fid, dtype = np.uint16, count = nx * ny * nt)
data = data * h['data_scale_factor'] #scaling factor
data = data.reshape(nx, ny, nt, order='F').copy() #make N-d image
elif extension == '.a3d':
if(h['word_type']==7): #float32
data = np.fromfile(fid, dtype = np.float32, count = nx * ny * nt)
elif(h['word_type']==4): #uint16
data = np.fromfile(fid, dtype = np.uint16, count = nx * ny * nt)
data = data * h['data_scale_factor'] #scaling factor
data = data.reshape(nx, nt, ny, order='F').copy() #make N-d image
elif extension == '.ahi':
data = np.fromfile(fid, dtype = np.float32, count = 2* nx * ny * nt)
data = data.reshape(2, ny, nx, nt, order='F').copy()
real = data[0,:,:,:].copy()
imag = data[1,:,:,:].copy()
fid.close()
if extension != '.ahi':
# print h['data_scale_factor']
return data
else:
return real, imag