| 1 |
#!/usr/bin/env python |
| 2 |
import struct |
| 3 |
|
| 4 |
# Convert mar c header file types to python struct module types |
| 5 |
mar_c_to_python_struct = { |
| 6 |
"INT32" : "i", |
| 7 |
"UINT32" : "I", |
| 8 |
"char" : "c", |
| 9 |
"UINT16" : "H" |
| 10 |
} |
| 11 |
|
| 12 |
# Sizes (bytes) of mar c header objects |
| 13 |
mar_c_sizes = { |
| 14 |
"INT32" : 4, |
| 15 |
"UINT32" : 4, |
| 16 |
"char" : 1, |
| 17 |
"UINT16" : 2 |
| 18 |
} |
| 19 |
|
| 20 |
# This was worked out by trial and error from a trial image I think |
| 21 |
MAXIMAGES=9 |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
def make_format(cdefinition): |
| 26 |
""" |
| 27 |
Reads the header definition in c and makes the format |
| 28 |
string to pass to struct.unpack |
| 29 |
""" |
| 30 |
lines = cdefinition.split("\n") |
| 31 |
fmt = "" |
| 32 |
names = [] |
| 33 |
expected = 0 |
| 34 |
for line in lines: |
| 35 |
if line.find(";")==-1: |
| 36 |
continue |
| 37 |
decl = line.split(";")[0].lstrip().rstrip() |
| 38 |
try: |
| 39 |
[type, name] = decl.split() |
| 40 |
except: |
| 41 |
#print("skipping:",line) |
| 42 |
continue |
| 43 |
# print("type:",type," name:",name) |
| 44 |
|
| 45 |
if name.find("[")>-1: |
| 46 |
# repeated ... times |
| 47 |
try: |
| 48 |
num = name.split("[")[1].split("]")[0] |
| 49 |
num = num.replace("MAXIMAGES",str(MAXIMAGES)) |
| 50 |
num = num.replace("sizeof(INT32)","4") |
| 51 |
times = eval(num) |
| 52 |
except: |
| 53 |
print("Please decode",decl) |
| 54 |
raise |
| 55 |
else: |
| 56 |
times=1 |
| 57 |
try: |
| 58 |
fmt += mar_c_to_python_struct[type]*times |
| 59 |
names += [name]*times |
| 60 |
expected += mar_c_sizes[type]*times |
| 61 |
except: |
| 62 |
#print("skipping",line) |
| 63 |
continue |
| 64 |
#print("%4d %4d"%(mar_c_sizes[type]*times,expected),name,":",times,line) |
| 65 |
#print(struct.calcsize(fmt),expected) |
| 66 |
return names, fmt |
| 67 |
|
| 68 |
def read_mar_header(filename): |
| 69 |
""" |
| 70 |
Get the header from a binary file |
| 71 |
""" |
| 72 |
f = open(filename,"rb") |
| 73 |
f.seek(1024) |
| 74 |
header=f.read(3072) |
| 75 |
f.close() |
| 76 |
return header |
| 77 |
|
| 78 |
|
| 79 |
def interpret_header(header, fmt, names): |
| 80 |
""" |
| 81 |
given a format and header interpret it |
| 82 |
""" |
| 83 |
values = struct.unpack(fmt,header) |
| 84 |
dict = {} |
| 85 |
i=0 |
| 86 |
for name in names: |
| 87 |
if name in dict: |
| 88 |
if type(values[i]) == type("string"): |
| 89 |
dict[name] = dict[name]+values[i] |
| 90 |
else: |
| 91 |
try: |
| 92 |
dict[name].append(values[i]) |
| 93 |
except: |
| 94 |
dict[name] = [dict[name],values[i]] |
| 95 |
else: |
| 96 |
dict[name] = values[i] |
| 97 |
i=i+1 |
| 98 |
|
| 99 |
return dict |
| 100 |
|
| 101 |
|
| 102 |
# Now for the c definition (found on mar webpage) |
| 103 |
# The following string is therefore copyrighted by Mar I guess |
| 104 |
|
| 105 |
cdefinition = """ |
| 106 |
typedef struct frame_header_type { |
| 107 |
/* File/header format parameters (256 bytes) */ |
| 108 |
UINT32 header_type; /* flag for header type |
| 109 |
(can be used as magic number) */ |
| 110 |
char header_name[16]; /* header name (MMX) */ |
| 111 |
UINT32 header_major_version; /* header_major_version (n.) */ |
| 112 |
UINT32 header_minor_version; /* header_minor_version (.n) */ |
| 113 |
UINT32 header_byte_order;/* BIG_ENDIAN (Motorola,MIPS); |
| 114 |
LITTLE_ENDIAN (DEC, Intel) */ |
| 115 |
UINT32 data_byte_order; /* BIG_ENDIAN (Motorola,MIPS); |
| 116 |
LITTLE_ENDIAN (DEC, Intel) */ |
| 117 |
UINT32 header_size; /* in bytes */ |
| 118 |
UINT32 frame_type; /* flag for frame type */ |
| 119 |
UINT32 magic_number; /* to be used as a flag - |
| 120 |
usually to indicate new file */ |
| 121 |
UINT32 compression_type; /* type of image compression */ |
| 122 |
UINT32 compression1; /* compression parameter 1 */ |
| 123 |
UINT32 compression2; /* compression parameter 2 */ |
| 124 |
UINT32 compression3; /* compression parameter 3 */ |
| 125 |
UINT32 compression4; /* compression parameter 4 */ |
| 126 |
UINT32 compression5; /* compression parameter 4 */ |
| 127 |
UINT32 compression6; /* compression parameter 4 */ |
| 128 |
UINT32 nheaders; /* total number of headers */ |
| 129 |
UINT32 nfast; /* number of pixels in one line */ |
| 130 |
UINT32 nslow; /* number of lines in image */ |
| 131 |
UINT32 depth; /* number of bytes per pixel */ |
| 132 |
UINT32 record_length; /* number of pixels between |
| 133 |
succesive rows */ |
| 134 |
UINT32 signif_bits; /* true depth of data, in bits */ |
| 135 |
UINT32 data_type; /* (signed,unsigned,float...) */ |
| 136 |
UINT32 saturated_value; /* value marks pixel as saturated */ |
| 137 |
UINT32 sequence; /* TRUE or FALSE */ |
| 138 |
UINT32 nimages; /* total number of images - size of |
| 139 |
each is nfast*(nslow/nimages) */ |
| 140 |
UINT32 origin; /* corner of origin */ |
| 141 |
UINT32 orientation; /* direction of fast axis */ |
| 142 |
UINT32 view_direction; /* direction to view frame */ |
| 143 |
UINT32 overflow_location;/* FOLLOWING_HEADER, FOLLOWING_DATA */ |
| 144 |
UINT32 over_8_bits; /* # of pixels with counts 255 */ |
| 145 |
UINT32 over_16_bits; /* # of pixels with count 65535 */ |
| 146 |
UINT32 multiplexed; /* multiplex flag */ |
| 147 |
UINT32 nfastimages; /* # of images in fast direction */ |
| 148 |
UINT32 nslowimages; /* # of images in slow direction */ |
| 149 |
UINT32 background_applied; /* flags correction has been applied - |
| 150 |
hold magic number ? */ |
| 151 |
UINT32 bias_applied; /* flags correction has been applied - |
| 152 |
hold magic number ? */ |
| 153 |
UINT32 flatfield_applied; /* flags correction has been applied - |
| 154 |
hold magic number ? */ |
| 155 |
UINT32 distortion_applied; /* flags correction has been applied - |
| 156 |
hold magic number ? */ |
| 157 |
UINT32 original_header_type; /* Header/frame type from file |
| 158 |
that frame is read from */ |
| 159 |
UINT32 file_saved; /* Flag that file has been saved, |
| 160 |
should be zeroed if modified */ |
| 161 |
char reserve1[(64-40)*sizeof(INT32)-16]; |
| 162 |
|
| 163 |
/* Data statistics (128) */ |
| 164 |
UINT32 total_counts[2]; /* 64 bit integer range = 1.85E19*/ |
| 165 |
UINT32 special_counts1[2]; |
| 166 |
UINT32 special_counts2[2]; |
| 167 |
UINT32 min; |
| 168 |
UINT32 max; |
| 169 |
UINT32 mean; |
| 170 |
UINT32 rms; |
| 171 |
UINT32 p10; |
| 172 |
UINT32 p90; |
| 173 |
UINT32 stats_uptodate; |
| 174 |
UINT32 pixel_noise[MAXIMAGES]; /* 1000*base noise value (ADUs) */ |
| 175 |
char reserve2[(32-13-MAXIMAGES)*sizeof(INT32)]; |
| 176 |
|
| 177 |
/* More statistics (256) */ |
| 178 |
UINT16 percentile[128]; |
| 179 |
|
| 180 |
|
| 181 |
/* Goniostat parameters (128 bytes) */ |
| 182 |
INT32 xtal_to_detector; /* 1000*distance in millimeters */ |
| 183 |
INT32 beam_x; /* 1000*x beam position (pixels) */ |
| 184 |
INT32 beam_y; /* 1000*y beam position (pixels) */ |
| 185 |
INT32 integration_time; /* integration time in milliseconds */ |
| 186 |
INT32 exposure_time; /* exposure time in milliseconds */ |
| 187 |
INT32 readout_time; /* readout time in milliseconds */ |
| 188 |
INT32 nreads; /* number of readouts to get this image */ |
| 189 |
INT32 start_twotheta; /* 1000*two_theta angle */ |
| 190 |
INT32 start_omega; /* 1000*omega angle */ |
| 191 |
INT32 start_chi; /* 1000*chi angle */ |
| 192 |
INT32 start_kappa; /* 1000*kappa angle */ |
| 193 |
INT32 start_phi; /* 1000*phi angle */ |
| 194 |
INT32 start_delta; /* 1000*delta angle */ |
| 195 |
INT32 start_gamma; /* 1000*gamma angle */ |
| 196 |
INT32 start_xtal_to_detector; /* 1000*distance in mm (dist in um)*/ |
| 197 |
INT32 end_twotheta; /* 1000*two_theta angle */ |
| 198 |
INT32 end_omega; /* 1000*omega angle */ |
| 199 |
INT32 end_chi; /* 1000*chi angle */ |
| 200 |
INT32 end_kappa; /* 1000*kappa angle */ |
| 201 |
INT32 end_phi; /* 1000*phi angle */ |
| 202 |
INT32 end_delta; /* 1000*delta angle */ |
| 203 |
INT32 end_gamma; /* 1000*gamma angle */ |
| 204 |
INT32 end_xtal_to_detector; /* 1000*distance in mm (dist in um)*/ |
| 205 |
INT32 rotation_axis; /* active rotation axis */ |
| 206 |
INT32 rotation_range; /* 1000*rotation angle */ |
| 207 |
INT32 detector_rotx; /* 1000*rotation of detector around X */ |
| 208 |
INT32 detector_roty; /* 1000*rotation of detector around Y */ |
| 209 |
INT32 detector_rotz; /* 1000*rotation of detector around Z */ |
| 210 |
char reserve3[(32-28)*sizeof(INT32)]; |
| 211 |
|
| 212 |
/* Detector parameters (128 bytes) */ |
| 213 |
INT32 detector_type; /* detector type */ |
| 214 |
INT32 pixelsize_x; /* pixel size (nanometers) */ |
| 215 |
INT32 pixelsize_y; /* pixel size (nanometers) */ |
| 216 |
INT32 mean_bias; /* 1000*mean bias value */ |
| 217 |
INT32 photons_per_100adu; /* photons / 100 ADUs */ |
| 218 |
INT32 measured_bias[MAXIMAGES]; /* 1000*mean bias value for each image*/ |
| 219 |
INT32 measured_temperature[MAXIMAGES]; /* Temperature of each |
| 220 |
detector in milliKelvins */ |
| 221 |
INT32 measured_pressure[MAXIMAGES]; /* Pressure of each chamber |
| 222 |
in microTorr */ |
| 223 |
/* Retired reserve4 when MAXIMAGES set to 9 from 16 and |
| 224 |
two fields removed, and temp and pressure added |
| 225 |
char reserve4[(32-(5+3*MAXIMAGES))*sizeof(INT32)] |
| 226 |
*/ |
| 227 |
|
| 228 |
/* X-ray source and optics parameters (128 bytes) */ |
| 229 |
/* X-ray source parameters (8*4 bytes) */ |
| 230 |
INT32 source_type; /* (code) - target, synch. etc */ |
| 231 |
INT32 source_dx; /* Optics param. - (size microns) */ |
| 232 |
INT32 source_dy; /* Optics param. - (size microns) */ |
| 233 |
INT32 source_wavelength; /* wavelength (femtoMeters) */ |
| 234 |
INT32 source_power; /* (Watts) */ |
| 235 |
INT32 source_voltage; /* (Volts) */ |
| 236 |
INT32 source_current; /* (microAmps) */ |
| 237 |
INT32 source_bias; /* (Volts) */ |
| 238 |
INT32 source_polarization_x; /* () */ |
| 239 |
INT32 source_polarization_y; /* () */ |
| 240 |
char reserve_source[4*sizeof(INT32)]; |
| 241 |
|
| 242 |
/* X-ray optics_parameters (8*4 bytes) */ |
| 243 |
INT32 optics_type; /* Optics type (code)*/ |
| 244 |
INT32 optics_dx; /* Optics param. - (size microns) */ |
| 245 |
INT32 optics_dy; /* Optics param. - (size microns) */ |
| 246 |
INT32 optics_wavelength; /* Optics param. - (size microns) */ |
| 247 |
INT32 optics_dispersion; /* Optics param. - (*10E6) */ |
| 248 |
INT32 optics_crossfire_x; /* Optics param. - (microRadians) */ |
| 249 |
INT32 optics_crossfire_y; /* Optics param. - (microRadians) */ |
| 250 |
INT32 optics_angle; /* Optics param. - (monoch. |
| 251 |
2theta - microradians) */ |
| 252 |
INT32 optics_polarization_x; /* () */ |
| 253 |
INT32 optics_polarization_y; /* () */ |
| 254 |
char reserve_optics[4*sizeof(INT32)]; |
| 255 |
|
| 256 |
char reserve5[((32-28)*sizeof(INT32))]; |
| 257 |
|
| 258 |
/* File parameters (1024 bytes) */ |
| 259 |
char filetitle[128]; /* Title */ |
| 260 |
char filepath[128]; /* path name for data file */ |
| 261 |
char filename[64]; /* name of data file */ |
| 262 |
char acquire_timestamp[32]; /* date and time of acquisition */ |
| 263 |
char header_timestamp[32]; /* date and time of header update */ |
| 264 |
char save_timestamp[32]; /* date and time file saved */ |
| 265 |
char file_comments[512]; /* comments, use as desired */ |
| 266 |
char reserve6[1024-(128+128+64+(3*32)+512)]; |
| 267 |
|
| 268 |
/* Dataset parameters (512 bytes) */ |
| 269 |
char dataset_comments[512]; /* comments, used as desired */ |
| 270 |
/* pad out to 3072 bytes */ |
| 271 |
char pad[3072-(256+128+256+(3*128)+1024+512)]; |
| 272 |
|
| 273 |
} frame_header; |
| 274 |
""" |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
class marheaderreader: |
| 279 |
""" |
| 280 |
Class to sit and read a series of images (makes format etc only once) |
| 281 |
""" |
| 282 |
def __init__(self): |
| 283 |
""" |
| 284 |
Initialise internal stuff |
| 285 |
""" |
| 286 |
self.names , self.fmt = make_format(cdefinition) |
| 287 |
def get_header(self,filename): |
| 288 |
""" |
| 289 |
Reads a header from file filename |
| 290 |
""" |
| 291 |
h=read_mar_header(filename) |
| 292 |
dict = interpret_header(h,self.fmt,self.names) |
| 293 |
# Append ESRF formatted stuff |
| 294 |
items = self.readesrfstring(dict["dataset_comments[512]"]) |
| 295 |
for pair in items: |
| 296 |
dict[pair[0]]=pair[1] |
| 297 |
items = self.readesrfstring(dict["file_comments[512]"]) |
| 298 |
for pair in items: |
| 299 |
dict[pair[0]]=pair[1] |
| 300 |
dict["pixelsize_x_mm"]= str(float(dict["pixelsize_x"])/1e6) |
| 301 |
dict["pixelsize_y_mm"]= str(float(dict["pixelsize_y"])/1e6) |
| 302 |
dict["integration_time_sec"]= str(float(dict["integration_time"])/1e3) |
| 303 |
dict["beam_y_mm"]= str(float(dict["pixelsize_y_mm"])* |
| 304 |
float(dict["beam_y"])/1000.) |
| 305 |
dict["beam_x_mm"]= str(float(dict["pixelsize_x_mm"])* |
| 306 |
float(dict["beam_x"])/1000.) |
| 307 |
|
| 308 |
return dict |
| 309 |
|
| 310 |
def readesrfstring(self,s): |
| 311 |
""" |
| 312 |
Interpret the so called "esrf format" header lines |
| 313 |
which are in comment sections |
| 314 |
""" |
| 315 |
s=s.replace("\000","") |
| 316 |
items = [_f for _f in [len(x)>1 and x or None for x in [ |
| 317 |
item.split("=") for item in s.split(";")]] if _f] |
| 318 |
return items |
| 319 |
|
| 320 |
|
| 321 |
if __name__=="__main__": |
| 322 |
""" |
| 323 |
Make a little program to process files |
| 324 |
""" |
| 325 |
import sys |
| 326 |
print("Starting") |
| 327 |
names,fmt = make_format(cdefinition) |
| 328 |
print("Names and format made") |
| 329 |
h = read_mar_header(sys.argv[1]) |
| 330 |
print("Read header, interpreting") |
| 331 |
d = interpret_header(h,fmt,names) |
| 332 |
printed = {} |
| 333 |
for name in names: |
| 334 |
if name in printed: |
| 335 |
continue |
| 336 |
print(name,":",d[name]) |
| 337 |
printed[name]=1 |
| 338 |
|