source: sans/Dev/trunk/NCNR_User_Procedures/Common/writeNISTXML.ipf @ 599

Last change on this file since 599 was 599, checked in by ajj, 13 years ago

Functions to handle writing out to canSAS 1D XML standard.

File size: 10.2 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3
4//AJJ 12/5/08
5
6//Define struct for file contents
7Structure NISTXMLfile
8//      string filename
9        string Run
10        string title
11
12        //<SASdata>
13        Wave Q,I,Idev,Qdev,Qmean,Shadowfactor,dQl
14        string unitsQ,unitsI,unitsIdev,unitsQdev,unitsQmean,unitsShadowfactor,unitsdQl
15
16//      Variable flux_monitor
17//      string Q_resolution
18
19        //<SASsample>
20//      variable sample_thickness
21        string sample_ID
22//      string unitssample_thickness
23//      variable sample_transmission
24
25        //SASinstrument
26        string nameSASinstrument
27        // SASinstrument/SASsource
28        string radiation
29//      string beam_shape
30//      variable wavelength
31//      string unitswavelength
32//      variable wavelength_spread
33//      string unitswavelength_spread
34 
35        //<SAScollimation>
36//      variable distance_coll
37//      string unitsdistance_coll
38//      variable source_aperture
39//      string unitssource_aperture
40//      string typesource_aperture
41//      variable sample_aperture
42//      string unitssample_aperture
43//      string typesample_aperture
44
45        //SASdetector         <SASdetector>
46        string detector_name
47//      variable offset_angle
48//      string unitsoffset_angle
49//      variable  distance_SD
50//      string unitsdistance_SD
51//      variable beam_centreX
52//      string unitsbeam_centreX
53//      variable beam_centreY
54//      string unitsbeam_centreY
55//      variable pixel_sizeX
56//      string unitspixel_sizeX
57//      variable pixel_sizeY
58//      string unitspixel_sizeY
59//      string detectortype
60
61        // <SASprocess name="NCNR-IGOR">
62        string nameSASprocess
63        string SASprocessnote
64//      string SASprocessdate
65//      string average_type
66//      string SAM_file
67//      string BKD_file
68//      string EMP_file
69//      string DIV_file
70//      string MASK_file
71//      string ABS_parameters
72//      variable TSTAND
73//      variable DSTAND
74//      string unitsDSTAND
75//      variable IZERO
76//      variable XSECT
77//      string unitsXSECT
78        string SASnote
79Endstructure
80
81
82//Function to write NIST canSAS XML files
83//Minimalist XML file - AJJ Dec 2008
84function writeNISTXML(fileName, NISTfile)
85        String fileName
86        Struct NISTXMLfile &NISTfile
87
88        variable fileID
89       
90        //create the sasXML file with SASroot
91        //no namespace, no prefix
92        fileID = xmlcreatefile(fileName,"SASroot","cansas1d/1.0","")
93
94        //create a version attribute for the root element
95        xmlsetAttr(fileID,"/SASroot","","version","1.0")
96        xmlsetAttr(fileID,"/SASroot","","xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
97        xmlsetAttr(fileID,"/SASroot","","xsi:schemaLocation","cansas1d/1.0 http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd")
98
99
100        //create the SASentry node
101        xmladdnode(fileID,"/SASroot","","SASentry","",1)
102               
103        //create the Title node
104        xmladdnode(fileID,"/SASroot/SASentry","","Title",NISTfile.Title,1)
105
106        //create the Run node
107        xmladdnode(fileID,"/SASroot/SASentry","","Run",NISTfile.Run,1)
108       
109        //create the SASdata node
110        xmladdnode(fileID,"/SASroot/SASentry","","SASdata","",1)
111
112        variable ii
113       
114        if (WaveExists(NISTfile.dQl) == 1)
115                for(ii=0 ; ii<numpnts(NISTfile.Q) ; ii+=1)
116                        xmladdnode(fileID,"/SASroot/SASentry/SASdata","","Idata","",1)
117                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Q",num2str(NISTfile.Q[ii]),1)
118                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Q","","unit",NISTfile.unitsQ)
119       
120                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","I",num2str(NISTfile.I[ii]),1)
121                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/I","","unit",NISTfile.unitsI)
122       
123                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Idev",num2str(NISTfile.Idev[ii]),1)
124                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Idev","","unit",NISTfile.unitsIdev)     
125       
126                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Idev",num2str(NISTfile.dQl[ii]),1)
127                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Idev","","unit",NISTfile.unitsdQl)               
128                endfor
129        else
130                for(ii=0 ; ii<numpnts(NISTfile.Q) ; ii+=1)
131                        xmladdnode(fileID,"/SASroot/SASentry/SASdata","","Idata","",1)
132                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Q",num2str(NISTfile.Q[ii]),1)
133                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Q","","unit",NISTfile.unitsQ)
134       
135                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","I",num2str(NISTfile.I[ii]),1)
136                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/I","","unit",NISTfile.unitsI)
137       
138                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Idev",num2str(NISTfile.Idev[ii]),1)
139                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Idev","","unit",NISTfile.unitsIdev)
140       
141                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Qdev",num2str(NISTfile.Qdev[ii]),1)
142                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Qdev","","unit",NISTfile.unitsQdev)
143       
144                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Qmean",num2str(NISTfile.Qmean[ii]),1)
145                        xmlsetAttr(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]/Qmean","","unit",NISTfile.unitsQmean)
146       
147                        xmladdnode(fileID,"/SASroot/SASentry/SASdata/Idata["+num2istr(ii+1)+"]","","Shadowfactor",num2str(NISTfile.shadowfactor[ii]),1)
148                endfor
149        endif
150
151        //SASsample node
152        xmladdnode(fileID,"/SASroot/SASentry","","SASsample","",1)
153        xmladdnode(fileID,"/SASroot/SASentry/SASsample","","ID",NISTfile.sample_ID,1)
154
155        //SASInstrument node
156        xmladdnode(fileID,"/SASroot/SASentry","","SASinstrument","",1)
157        xmladdnode(fileID,"/SASroot/SASentry/SASinstrument","","name",NISTfile.nameSASinstrument,1)
158       
159        //SASsource
160        xmladdnode(fileID,"/SASroot/SASentry/SASinstrument","","SASsource","",1)
161        xmladdnode(fileID,"/SASroot/SASentry/SASinstrument/SASsource","","radiation",NISTfile.radiation,1)
162
163        //SAScollimation
164        xmladdnode(fileID,"/SASroot/SASentry/SASinstrument","","SAScollimation","",1)
165
166        //SASdetector
167        xmladdnode(fileID,"/SASroot/SASentry/SASinstrument","","SASdetector","",1)
168        xmladdnode(fileID,"/SASroot/SASentry/SASinstrument/SASdetector","","name",NISTfile.detector_name,1)
169
170
171        //SASprocess
172        xmladdnode(fileID,"/SASroot/SASentry","","SASprocess","",1)
173        xmlsetAttr(fileID,"/SASroot/SASentry/SASprocess","","name",NISTfile.nameSASprocess)     
174        xmladdnode(fileID,"/SASroot/SASentry/SASprocess","","SASprocessnote",NISTfile.SASprocessnote,1)
175       
176        //SASnote
177        xmladdnode(fileID,"/SASroot/SASentry","","SASnote",NISTfile.SASnote,1)
178       
179        xmlsavefile(fileID)
180        xmlclosefile(fileID,0)
181       
182end
183
184Function convertNISTtoNISTXML(fileStr)
185        String fileStr
186       
187        Struct NISTXMLfile nf
188       
189        Variable rr,gg,bb,refnum,dQv
190        SetDataFolder root:     
191       
192        if (cmpStr(fileStr,"") == 0)
193                //No filename given, open dialog
194                Open/D/R  refnum
195                if (cmpstr(S_filename,"") == 0)
196                        return 0
197                else
198                        fileStr = S_filename
199                endif
200        endif
201
202        //Load the waves, using default waveX names
203        //if no path or file is specified for LoadWave, the default Mac open dialog will appear
204        LoadWave/G/D/A/Q fileStr
205        String fileNamePath = S_Path+S_fileName
206        String basestr = CleanupName(ParseFilePath(3,ParseFilePath(5,fileNamePath,":",0,0),":",0,0),0)
207//      String baseStr = CleanupName(S_fileName,0)
208//              print "basestr :"+basestr
209        String fileName =  ParseFilePath(0,ParseFilePath(5,filestr,":",0,0),":",1,0)
210//              print "filename :"+filename
211        Variable numCols = V_flag
212        String outfileName = S_Path+basestr+".xml"
213
214       
215        if(numCols==3)          //simple 3-column data with no resolution information
216               
217                Wave nf.Q = $(StringFromList(0, S_waveNames ,";" ))
218                Wave nf.I = $(StringFromList(1, S_waveNames ,";" ))
219                Wave nf.Idev = $(StringFromList(2, S_waveNames ,";" ))
220                               
221                //Set units
222                nf.unitsQ = "1/A"
223                nf.unitsI = "1/cm"
224                nf.unitsIdev = "1/cm"
225                               
226        endif           //3-col data
227       
228        if(numCols == 6)                //6-column SANS or USANS data that has resolution information
229               
230                // put the names of the (default named) loaded waves into local names
231                Wave nf.Q = $(StringFromList(0, S_waveNames ,";" ))
232                Wave nf.I  = $(StringFromList(1, S_waveNames ,";" ))
233                Wave nf.Idev  = $(StringFromList(2, S_waveNames ,";" ))
234
235                //Set units
236                nf.unitsQ = "1/A"
237                nf.unitsI = "1/cm"
238                nf.unitsIdev = "1/cm"
239
240                WAVE resTest = $(StringFromList(3, S_waveNames ,";" ))
241
242                // need to switch based on SANS/USANS
243                if (isSANSResolution(resTest[0]))               //checks to see if the first point of the wave is <0]
244                        Wave nf.Qdev  = $(StringFromList(3, S_waveNames ,";" ))
245                        Wave nf.Qmean  = $(StringFromList(4, S_waveNames ,";" ))
246                        Wave nf.Shadowfactor  = $(StringFromList(5, S_waveNames ,";" ))
247                       
248                        //Set units
249                        nf.unitsQdev = "1/A"
250                        nf.unitsQmean = "1/A"
251                else
252                        Wave nf.dQl = $(StringFromList(3, S_waveNames ,";" ))
253                       
254                        //Set units
255                        nf.unitsdQl = "1/A"
256                       
257                endif
258               
259                //Tidy up
260                Variable i = 0
261                do
262                        WAVE wv= $(StringFromList(i,S_waveNames,";"))
263                        if( WaveExists(wv) == 0 )
264                                break
265                        endif
266                        KillWaves wv
267                        i += 1
268                while (1)       // exit is via break statement
269
270       
271        endif   //6-col data
272
273        //Get file header
274        setmetadataFromASCHeader(fileStr,nf)
275
276        //Set required metadata that we can't get from these files
277        nf.detector_name = "Ordela 128x128"
278        nf.nameSASinstrument = "NIST NG3/NG7 SANS"
279        nf.radiation = "neutron"
280        nf.sample_ID = nf.title
281        nf.nameSASProcess = "NIST Data Converter"
282        nf.sasnote = "Data converted from previous NIST format. SASProcessnote contains header from original text file."
283
284        writeNISTXML(outfileName, nf)
285
286end
287
288function setmetadataFromASCHeader(fileStr,NISTfile)
289        String fileStr
290        Struct NISTXMLfile &NISTfile
291
292        String hdr="",buffer=""
293        Variable lineNum = 0, fileref
294        Variable num
295       
296        Open/R fileref as fileStr
297        do
298                FReadLine fileref, buffer
299                if (stringmatch(buffer,"*The 6 columns are*") == 1)
300                        break
301                endif
302                buffer = RemoveEnding(buffer)
303                print buffer
304                //Get run value
305                if (stringmatch(buffer,"*file:*") == 1)
306                        NISTfile.run = TrimWS(StringFromList(0,StringFromList(1, buffer, ":"),"C"))
307                elseif (stringmatch(buffer,"combined file*") == 1)
308                        NISTfile.run = "Combined Data"
309                endif
310               
311                //Get title value
312                if (stringmatch(buffer,"*FIRST File LABEL:*") == 1)
313                        NISTfile.title = TrimWS(StringFromList(1,buffer, ":"))
314                elseif(stringmatch(buffer,"*LABEL:*") == 1)
315                        NISTfile.title = TrimWS(StringFromList(1,buffer, ":"))
316                endif
317               
318                hdr += buffer+"\n"
319        while(strlen(buffer) > 0)
320       
321        if (strlen(NISTfile.title) == 0)
322                NISTfile.title = CleanupName(ParseFilePath(3,ParseFilePath(5,fileStr,":",0,0),":",0,0),0)
323        endif
324        if (strlen(NISTfile.run) == 0)
325                NISTfile.run = "Unknown"
326        endif
327       
328        NISTfile.sasprocessnote = RemoveEnding(hdr)
329       
330end
Note: See TracBrowser for help on using the repository browser.