1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import h5py |
---|
4 | import numpy as np |
---|
5 | import os |
---|
6 | #from lxml import etree |
---|
7 | import xml.dom.minidom |
---|
8 | from datetime import date |
---|
9 | |
---|
10 | csvFileName = "filelist.csv" |
---|
11 | |
---|
12 | currentFolder = os.getcwd() |
---|
13 | folder2m = "proc-2m-complexmask" |
---|
14 | folder6m = "proc-6m-complexmask" |
---|
15 | outfolder = "converted" |
---|
16 | |
---|
17 | |
---|
18 | def write_node(doc, parent, name, value, attr={}): |
---|
19 | """ |
---|
20 | :param doc: document DOM |
---|
21 | :param parent: parent node |
---|
22 | :param name: tag of the element |
---|
23 | :param value: value of the child text node |
---|
24 | :param attr: attribute dictionary |
---|
25 | |
---|
26 | :return: True if something was appended, otherwise False |
---|
27 | """ |
---|
28 | if value is not None: |
---|
29 | node = doc.createElement(name) |
---|
30 | node.appendChild(doc.createTextNode(str(value))) |
---|
31 | for item in attr: |
---|
32 | node.setAttribute(item, attr[item]) |
---|
33 | parent.appendChild(node) |
---|
34 | return True |
---|
35 | return False |
---|
36 | |
---|
37 | def writeASCII(q,iq,header,outfilename): |
---|
38 | #Write out ascii to disk |
---|
39 | with open(outfilename, 'wb') as f: |
---|
40 | f.write(header) |
---|
41 | np.savetxt(f,zip(q,iq)) |
---|
42 | f.close() |
---|
43 | |
---|
44 | def writeCANSAS(q,iq,title,run,comment,sdd,origfile,filedate,outfile): |
---|
45 | """ |
---|
46 | Create an XML document to contain the data |
---|
47 | """ |
---|
48 | doc = xml.dom.minidom.Document() |
---|
49 | main_node = doc.createElement("SASroot") |
---|
50 | main_node.setAttribute("version", '1.0') |
---|
51 | main_node.setAttribute("xmlns", "cansas1d/%s" % '1.0') |
---|
52 | main_node.setAttribute("xmlns:xsi", |
---|
53 | "http://www.w3.org/2001/XMLSchema-instance") |
---|
54 | main_node.setAttribute("xsi:schemaLocation", |
---|
55 | "cansas1d/%s http://www.cansas.org/formats/1.0/cansas1d.xsd" % '1.0') |
---|
56 | |
---|
57 | doc.appendChild(main_node) |
---|
58 | |
---|
59 | entry_node = doc.createElement("SASentry") |
---|
60 | main_node.appendChild(entry_node) |
---|
61 | |
---|
62 | write_node(doc, entry_node, "Title", title) |
---|
63 | # for item in datainfo.run: |
---|
64 | # runname = {} |
---|
65 | # if item in datainfo.run_name and \ |
---|
66 | # len(str(datainfo.run_name[item])) > 1: |
---|
67 | # runname = {'name': datainfo.run_name[item]} |
---|
68 | write_node(doc, entry_node, "Run", run) |
---|
69 | |
---|
70 | # Data info |
---|
71 | node = doc.createElement("SASdata") |
---|
72 | entry_node.appendChild(node) |
---|
73 | |
---|
74 | for i in range(len(q)): |
---|
75 | pt = doc.createElement("Idata") |
---|
76 | node.appendChild(pt) |
---|
77 | write_node(doc, pt, "Q", q[i], {'unit': '1/A'}) |
---|
78 | write_node(doc, pt, "I", iq[i],{'unit': '1/cm'}) |
---|
79 | |
---|
80 | # Sample info |
---|
81 | sample = doc.createElement("SASsample") |
---|
82 | entry_node.appendChild(sample) |
---|
83 | write_node(doc, sample, "ID", title) |
---|
84 | write_node(doc, sample, "details", title+" "+comment) |
---|
85 | |
---|
86 | # Instrument info |
---|
87 | instr = doc.createElement("SASinstrument") |
---|
88 | entry_node.appendChild(instr) |
---|
89 | write_node(doc, instr, "name", "I22 SAXS") |
---|
90 | |
---|
91 | # Source |
---|
92 | source = doc.createElement("SASsource") |
---|
93 | source.setAttribute("name", "Diamond") |
---|
94 | instr.appendChild(source) |
---|
95 | |
---|
96 | write_node(doc, source, "radiation", "Synchrotron X-ray Source") |
---|
97 | |
---|
98 | # Collimation |
---|
99 | coll = doc.createElement("SAScollimation") |
---|
100 | instr.appendChild(coll) |
---|
101 | |
---|
102 | # Detectors |
---|
103 | det = doc.createElement("SASdetector") |
---|
104 | instr.appendChild(det) |
---|
105 | write_node(doc, det, "name", "Pilatus2M") |
---|
106 | |
---|
107 | # Processes info |
---|
108 | node = doc.createElement("SASprocess") |
---|
109 | entry_node.appendChild(node) |
---|
110 | |
---|
111 | write_node(doc, node, "name", "GDA/DAWN/Python") |
---|
112 | write_node(doc, node, "date", filedate) |
---|
113 | write_node(doc, node, "SASprocessnote", "Data originally converted to 1D with GDA/DAWN.\n canSAS XML generated from original nexus file :\n\t "+origfile+"\n by AJJ Script on "+date.today().isoformat()+"\n") |
---|
114 | |
---|
115 | #SASNote |
---|
116 | node = doc.createElement("SASnote") |
---|
117 | entry_node.appendChild(node) |
---|
118 | |
---|
119 | # Return the document, and the SASentry node associated with |
---|
120 | # the data we just wrote |
---|
121 | #return doc, entry_node |
---|
122 | |
---|
123 | # Write the file |
---|
124 | |
---|
125 | fd = open(outfile, 'w') |
---|
126 | fd.write(doc.toprettyxml()) |
---|
127 | fd.close() |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | #Read in file list |
---|
134 | inputFile = open(csvFileName,'r') |
---|
135 | fileList = inputFile.read().splitlines() |
---|
136 | inputFile.close() |
---|
137 | |
---|
138 | #print fileList |
---|
139 | |
---|
140 | #On each file, load HDF and extract Q, I(q) |
---|
141 | for item in fileList: |
---|
142 | #print item |
---|
143 | (fileName,sample,pressure,description,sdd) = item.split(',') |
---|
144 | |
---|
145 | if (pressure == '' or sample == "Bad Run"): |
---|
146 | continue |
---|
147 | |
---|
148 | if sdd == '2': |
---|
149 | folder = folder2m |
---|
150 | elif sdd == '6': |
---|
151 | folder = folder6m |
---|
152 | else: |
---|
153 | continue |
---|
154 | |
---|
155 | fileNamePath = currentFolder+"/"+folder+"/"+fileName |
---|
156 | |
---|
157 | print "Processing ",folder+"/"+fileName |
---|
158 | |
---|
159 | outfilename = currentFolder+"/"+outfolder+"/"+fileName[12:18] |
---|
160 | filedate = "20"+fileName[33:35]+"-"+fileName[31:33]+"-"+fileName[29:31] |
---|
161 | #print outfilename |
---|
162 | |
---|
163 | dataFile = h5py.File(fileNamePath,"r") |
---|
164 | q = dataFile['/entry1/Pilatus2M_result/q'].value |
---|
165 | iq = dataFile['/entry1/Pilatus2M_result/data'][0,0,...] |
---|
166 | dataFile.close() |
---|
167 | |
---|
168 | header = "Title:"+sample+" at "+pressure+" bar\n" |
---|
169 | header += "Comment:"+description+"\n" |
---|
170 | header += "SDD:"+sdd+"\n" |
---|
171 | header += "Original reduced file:"+fileName+"\n" |
---|
172 | |
---|
173 | writeASCII(q,iq,header,outfilename+".txt") |
---|
174 | print "Done with ASCII!" |
---|
175 | writeCANSAS(q,iq,sample,fileName[12:18],description,sdd,fileName,filedate,outfilename+".xml") |
---|
176 | print "Done with canSAS!" |
---|