| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
|
| 4 |
import pycbf |
| 5 |
|
| 6 |
# Some cbf helper functions - obj would be a cbf_handle_struct object |
| 7 |
|
| 8 |
def writewavelength(obj,wavelength): |
| 9 |
obj.set_wavelength(float(wavelength)) |
| 10 |
|
| 11 |
def writecellpar(obj,cifname,value): |
| 12 |
obj.find_category("cell") |
| 13 |
obj.find_column(cifname) |
| 14 |
obj.set_value(value) |
| 15 |
|
| 16 |
def writecell(obj,cell): |
| 17 |
""" |
| 18 |
call with cell = (a,b,c,alpha,beta,gamma) |
| 19 |
""" |
| 20 |
obj.find_category("cell") |
| 21 |
obj.find_column("length_a") |
| 22 |
obj.set_value(str(cell[0])) |
| 23 |
obj.find_column("length_b") |
| 24 |
obj.set_value(str(cell[1])) |
| 25 |
obj.find_column("length_c") |
| 26 |
obj.set_value(str(cell[2])) |
| 27 |
obj.find_column("angle_alpha") |
| 28 |
obj.set_value(str(cell[3])) |
| 29 |
obj.find_column("angle_beta") |
| 30 |
obj.set_value(str(cell[4])) |
| 31 |
obj.find_column("angle_gamma") |
| 32 |
obj.set_value(str(cell[5])) |
| 33 |
|
| 34 |
def writeUB(obj,ub): |
| 35 |
""" |
| 36 |
call with ub that can be indexed ub[i][j] |
| 37 |
""" |
| 38 |
obj.find_category("diffrn_orient_matrix") |
| 39 |
for i in (1,2,3): |
| 40 |
for j in (1,2,3): |
| 41 |
obj.find_column("UB[%d][%d]"%(i,j)) |
| 42 |
obj.set_value(str(ub[i-1][j-1])) |
| 43 |
|
| 44 |
def writedistance(obj,distance): |
| 45 |
obj.set_axis_setting("DETECTOR_Z",float(distance),0.) |
| 46 |
|
| 47 |
|
| 48 |
def writebeam_x_mm(obj,cen): |
| 49 |
obj.set_axis_setting("DETECTOR_X",float(cen),0.) |
| 50 |
|
| 51 |
def writebeam_y_mm(obj,cen): |
| 52 |
obj.set_axis_setting("DETECTOR_Y",float(cen),0.) |
| 53 |
|
| 54 |
def writeSPECcmd(obj,s): |
| 55 |
obj.find_category("diffrn_measurement") |
| 56 |
obj.find_column("details") |
| 57 |
obj.set_value(s) |
| 58 |
|
| 59 |
def writeSPECscan(obj,s): |
| 60 |
obj.find_category("diffrn_scan") |
| 61 |
obj.find_column("id") |
| 62 |
obj.set_value("SCAN%s"%(s)) |
| 63 |
obj.find_category("diffrn_scan_axis") |
| 64 |
obj.find_column("scan_id") |
| 65 |
obj.rewind_row() |
| 66 |
for i in range(obj.count_rows()): |
| 67 |
obj.select_row(i) |
| 68 |
obj.set_value("SCAN%s"%(s)) |
| 69 |
obj.find_category("diffrn_scan_frame") |
| 70 |
obj.find_column("scan_id") |
| 71 |
obj.rewind_row() |
| 72 |
obj.set_value("SCAN%s"%(s)) |
| 73 |
|
| 74 |
|
| 75 |
def writepixelsize_y_mm(obj,s): |
| 76 |
""" |
| 77 |
Units are mm for cif |
| 78 |
""" |
| 79 |
# element number = assume this is first and only detector |
| 80 |
element_number = 0 |
| 81 |
# axis number = faster or slower... ? Need to check precedence ideally... |
| 82 |
obj.find_category("array_structure_list") |
| 83 |
obj.find_column("axis_set_id") |
| 84 |
obj.find_row("ELEMENT_Y") |
| 85 |
obj.find_column("precedence") |
| 86 |
axis_number = obj.get_integervalue() |
| 87 |
|
| 88 |
obj.set_pixel_size(element_number, axis_number, float(s) ) |
| 89 |
|
| 90 |
obj.find_category("array_structure_list_axis") |
| 91 |
obj.find_column("axis_id") |
| 92 |
obj.find_row("ELEMENT_Y") |
| 93 |
obj.find_column("displacement") |
| 94 |
obj.set_doublevalue("%.6g",float(s)/2.0) |
| 95 |
obj.find_column("displacement_increment") |
| 96 |
obj.set_doublevalue("%.6g",float(s)) |
| 97 |
|
| 98 |
def writepixelsize_x_mm(obj,s): |
| 99 |
# element number = assume this is first and only detector |
| 100 |
element_number = 0 |
| 101 |
# axis number = faster or slower... ? Need to check precedence ideally... |
| 102 |
obj.find_category("array_structure_list") |
| 103 |
obj.find_column("axis_set_id") |
| 104 |
obj.find_row("ELEMENT_X") |
| 105 |
obj.find_column("precedence") |
| 106 |
axis_number = obj.get_integervalue() |
| 107 |
|
| 108 |
obj.set_pixel_size(element_number, axis_number, float(s) ) |
| 109 |
|
| 110 |
obj.find_category("array_structure_list_axis") |
| 111 |
obj.find_column("axis_id") |
| 112 |
obj.find_row("ELEMENT_X") |
| 113 |
obj.find_column("displacement") |
| 114 |
obj.set_doublevalue("%.6g",float(s)/2.0) |
| 115 |
obj.find_column("displacement_increment") |
| 116 |
obj.set_doublevalue("%.6g",float(s)) |
| 117 |
|
| 118 |
def writeintegrationtime(obj,s): |
| 119 |
obj.find_category("diffrn_scan_frame") |
| 120 |
obj.find_column("integration_time") |
| 121 |
obj.set_value(str(s).replace("\000","")) |
| 122 |
|
| 123 |
def writenfast(obj,s): |
| 124 |
obj.find_category("array_structure_list") |
| 125 |
obj.find_column("index") |
| 126 |
obj.find_row("1") |
| 127 |
obj.find_column("dimension") |
| 128 |
obj.set_value(str(s)) |
| 129 |
|
| 130 |
def writenslow(obj,s): |
| 131 |
obj.find_category("array_structure_list") |
| 132 |
obj.find_column("index") |
| 133 |
obj.find_row("2") |
| 134 |
obj.find_column("dimension") |
| 135 |
obj.set_value(str(s)) |
| 136 |
|
| 137 |
|
| 138 |
functiondict = { |
| 139 |
"lambda" : writewavelength, |
| 140 |
"beam_x_mm" : writebeam_x_mm, |
| 141 |
"beam_y_mm" : writebeam_y_mm, |
| 142 |
"distance" : writedistance, |
| 143 |
"UB" : writeUB, |
| 144 |
"cell" : writecell, |
| 145 |
"cmd" : writeSPECcmd, |
| 146 |
"scan" : writeSPECscan, |
| 147 |
"nfast" : writenfast, |
| 148 |
"nslow" : writenslow, |
| 149 |
"pixelsize_y_mm" : writepixelsize_y_mm, |
| 150 |
"pixelsize_x_mm" : writepixelsize_x_mm, |
| 151 |
"integration_time_sec" : writeintegrationtime, |
| 152 |
"tth" : lambda obj,value : obj.set_axis_setting( |
| 153 |
"DETECTOR_TWO_THETA_VERTICAL",float(value),0.), |
| 154 |
"chi" : lambda obj,value : obj.set_axis_setting( |
| 155 |
"GONIOMETER_CHI",float(value),0.), |
| 156 |
"th" : lambda obj,value : obj.set_axis_setting( |
| 157 |
"GONIOMETER_THETA",float(value),0.), |
| 158 |
"phi" : lambda obj,value : obj.set_axis_setting( |
| 159 |
"GONIOMETER_PHI",float(value),0.), |
| 160 |
"lc_a" : lambda obj,value : writecellpar(obj,"length_a",value), |
| 161 |
"lc_b" : lambda obj,value : writecellpar(obj,"length_b",value), |
| 162 |
"lc_c" : lambda obj,value : writecellpar(obj,"length_c",value), |
| 163 |
"lc_al" : lambda obj,value : writecellpar(obj,"angle_alpha",value), |
| 164 |
"lc_be" : lambda obj,value : writecellpar(obj,"angle_beta",value), |
| 165 |
"lc_ga" : lambda obj,value : writecellpar(obj,"angle_gamma",value) |
| 166 |
} |
| 167 |
|
| 168 |
""" |
| 169 |
# |
| 170 |
# Not implementing these for now |
| 171 |
lc_ra |
| 172 |
lc_rc 0.4742 |
| 173 |
lc_rb 1.16 |
| 174 |
energy 13 |
| 175 |
cp_phi -180 |
| 176 |
alpha 7.3716 |
| 177 |
lc_ral 90 |
| 178 |
cp_tth -180 |
| 179 |
lc_rga 90 |
| 180 |
beta 17.572 |
| 181 |
omega -2.185 |
| 182 |
h 0.21539 |
| 183 |
k 0.01957 |
| 184 |
l 5.9763 |
| 185 |
cp_chi -180 |
| 186 |
lc_rbe 90 |
| 187 |
cp_th -180 |
| 188 |
azimuth 0 |
| 189 |
""" |
| 190 |
|
| 191 |
# Finally a class for creating header files. |
| 192 |
# It reads a template and then offers a processfile command |
| 193 |
# for running over a file series |
| 194 |
|
| 195 |
class cifheader: |
| 196 |
|
| 197 |
def __init__(self,templatefile): |
| 198 |
self.cbf=pycbf.cbf_handle_struct() |
| 199 |
self.cbf.read_template(templatefile) |
| 200 |
from readmarheader import marheaderreader |
| 201 |
self.marheaderreader = marheaderreader() |
| 202 |
|
| 203 |
|
| 204 |
def processfile(self,filename, outfile=None, |
| 205 |
format="mccd", |
| 206 |
**kwds): |
| 207 |
outfile=outfile.replace(format,"cif") |
| 208 |
|
| 209 |
if format == "mccd": |
| 210 |
items = self.marheaderreader.get_header(filename) |
| 211 |
|
| 212 |
if format == "bruker": |
| 213 |
pass |
| 214 |
if format == "edf": |
| 215 |
pass |
| 216 |
|
| 217 |
self.items=items |
| 218 |
|
| 219 |
# Take the image header items as default |
| 220 |
self.updateitems(items) |
| 221 |
|
| 222 |
# Allow them to be overridden |
| 223 |
self.updateitems(kwds) |
| 224 |
|
| 225 |
# Write the file |
| 226 |
self.writefile(outfile) |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
def writefile(self,filename): |
| 231 |
self.cbf.write_file(filename,pycbf.CIF,pycbf.MIME_HEADERS, |
| 232 |
pycbf.ENC_BASE64) |
| 233 |
|
| 234 |
|
| 235 |
def updateitems(self,dict): |
| 236 |
names = dict.keys() |
| 237 |
for name in names: |
| 238 |
value = dict[name] |
| 239 |
# use a dictionary of functions |
| 240 |
if functiondict.has_key(name): |
| 241 |
# print "calling",functiondict[name],value |
| 242 |
apply(functiondict[name],(self.cbf,value)) |
| 243 |
else: |
| 244 |
#print "ignoring",name,value |
| 245 |
pass |
| 246 |
|
| 247 |
|
| 248 |
if __name__=="__main__": |
| 249 |
import sys |
| 250 |
|
| 251 |
obj=cifheader("xmas_cif_template.cif") |
| 252 |
|
| 253 |
ub = [[0.11, 0.12, 0.13] , [0.21, 0.22, 0.23], [0.31, 0.32, 0.33]] |
| 254 |
|
| 255 |
for filename in sys.argv[1:]: |
| 256 |
fileout = filename.split("/")[-1] |
| 257 |
obj.processfile(filename, outfile=fileout, UB=ub, distance=123.456) |