1 | import nxs |
---|
2 | from string import join |
---|
3 | from numpy import * |
---|
4 | |
---|
5 | def correctangle(angle): |
---|
6 | angle[angle>pi]-=2*pi |
---|
7 | angle[angle<-pi]+=2*pi |
---|
8 | return angle |
---|
9 | |
---|
10 | def MakePAR(): |
---|
11 | filestr='CNCS_7860_event.nxs' |
---|
12 | fid = nxs.open(filestr,'r') |
---|
13 | #determine the banks in the file |
---|
14 | fid.openpath('/entry/instrument') |
---|
15 | banks=array(fid.getentries().keys()) |
---|
16 | banks=banks[(banks>'bank')&(banks<'bank999')] |
---|
17 | banknums=cast['int'](join(banks).split('bank')[1:]) |
---|
18 | banknums.sort() |
---|
19 | pararray=array([1,2,3,4,5,6]).reshape(6,1) |
---|
20 | for b in banknums: |
---|
21 | print b |
---|
22 | #get distance |
---|
23 | fid.openpath('/entry/instrument/bank'+str(b)+'/distance') |
---|
24 | dist=fid.getdata().reshape(1,-1) |
---|
25 | #get angles |
---|
26 | fid.openpath('/entry/instrument/bank'+str(b)+'/azimuthal_angle') |
---|
27 | azi=degrees(fid.getdata()).reshape(1,-1) |
---|
28 | fid.openpath('/entry/instrument/bank'+str(b)+'/polar_angle') |
---|
29 | pol=degrees(fid.getdata()).reshape(1,-1) |
---|
30 | #get x and y pixel values in bank frame |
---|
31 | fid.openpath('/entry/instrument/bank'+str(b)+'/x_pixel_offset') |
---|
32 | xc=fid.getdata().reshape(1,-1) |
---|
33 | fid.openpath('/entry/instrument/bank'+str(b)+'/y_pixel_offset') |
---|
34 | yc=fid.getdata().reshape(1,-1) |
---|
35 | fid.openpath('/entry/instrument/bank'+str(b)+'/pixel_id') |
---|
36 | pid=fid.getdata().reshape(1,-1) |
---|
37 | #get xw yw |
---|
38 | fid.openpath('/entry/instrument/bank'+str(b)) |
---|
39 | k=fid.getentries().keys() |
---|
40 | if 'x_pixel_size' in k: |
---|
41 | fid.openpath('/entry/instrument/bank'+str(b)+'/x_pixel_size') |
---|
42 | xw=fid.getdata().reshape(1,-1) |
---|
43 | else: |
---|
44 | xw=xc[0,1]-xc[0,0] |
---|
45 | if 'y_pixel_size' in k: |
---|
46 | fid.openpath('/entry/instrument/bank'+str(b)+'/y_pixel_size') |
---|
47 | yw=fid.getdata().reshape(1,-1) |
---|
48 | else: |
---|
49 | yw=yc[0,1]-yc[0,0] |
---|
50 | #add information to the par file |
---|
51 | pararray=append(pararray,concatenate((dist,pol,azi,dist*0.+xw,dist*0+yw,pid)),axis=1) |
---|
52 | |
---|
53 | #get rid of the first line of the pararray |
---|
54 | pararray=pararray[:,1:].T |
---|
55 | #write it to a file |
---|
56 | f=open('SEQ-new.par','w') |
---|
57 | x,y=pararray.shape |
---|
58 | formatStr = " %0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%d" |
---|
59 | print >>f, x |
---|
60 | for i in range(x): |
---|
61 | print >>f,formatStr % (pararray[i,0],pararray[i,1],pararray[i,2],pararray[i,3],pararray[i,4],pararray[i,5]) |
---|
62 | f.close() |
---|
63 | |
---|
64 | if __name__=="__main__": |
---|
65 | MakePAR() |
---|
66 | |
---|
67 | |
---|
68 | |
---|