source: sans/Dev/branches/nxcansas_writer/NCNR_User_Procedures/Reduction/USANS/WriteUSANSNXcanSAS.ipf @ 1172

Last change on this file since 1172 was 1172, checked in by krzywon, 3 years ago

Update the USANS writer to generalize the meta data and allow for multiple sasentry per file (untested).

File size: 15.7 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma Version=2.20
3#pragma IgorVersion=6.1
4
5#include <HDF5 Browser>
6
7//////////////////////////////////////////////////////////////////////////////////
8//
9// Write out an NXcanSAS compliant file using all known USANS information
10
11//************************
12// Vers 1.00 20190603
13//
14//************************
15
16//
17Function WriteUSANSNXcanSAS(type,fullpath,lo,hi,dialog)
18       
19        String type,fullpath
20        Variable lo,hi,dialog           //=1 will present dialog for name
21       
22        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
23        NVAR dQv = root:Packages:NIST:USANS:Globals:MainPanel:gDQv
24       
25        Variable fileID
26        String destStr=""
27        destStr = USANSFolder+":"+type
28        String dateStr=date()+" "+time()
29
30        String parentBase,nxcansasBase
31        String/G base = "root:NXcanSAS_USANS_file"
32       
33        Variable sasentry = NumVarOrDefault("root:Packages:NIST:gSASEntryNumber", 1)
34        sPrintf parentBase,"%s:sasentry%d",base,sasentry // Igor memory base path for all
35        sPrintf nxcansasBase,"/sasentry%d/",sasentry // HDF5 base path for all
36       
37        KillDataFolder/Z $base
38       
39        // Define common attribute waves
40        Make/T/N=1 empty = {""}
41        Make/T/N=1 units = {"units"}
42        Make/T/N=1 m = {"m"}
43        Make/T/N=1 mm = {"mm"}
44        Make/T/N=1 cm = {"cm"}
45        Make/T/N=1 angstrom = {"A"}
46        Make/T/N=1 inv_cm = {"1/cm"}
47        Make/T/N=1 inv_angstrom = {"1/A"}
48       
49        Variable refNum,integer,realval
50       
51        //*****these waves MUST EXIST, or IGOR Pro will crash, with a type 2 error****
52        WAVE qvals =$(destStr + ":Qvals")
53        WAVE inten=$(destStr + ":DetCts")
54        WAVE sig=$(destStr + ":ErrDetCts")
55               
56        if(dialog || stringmatch(fullpath, ""))
57                fileID = NxCansas_DoSaveFileDialog()
58        else
59                fileID = NxCansas_CreateFile(fullpath)
60        Endif
61       
62        //check each wave
63        If(!(WaveExists(qvals)))
64                Abort "qvals DNExist in WriteUSANSWaves()"
65        Endif
66        If(!(WaveExists(inten)))
67                Abort "inten DNExist in WriteUSANSWaves()"
68        Endif
69        If(!(WaveExists(sig)))
70                Abort "sig DNExist in WriteUSANSWaves()"
71        Endif
72       
73        //Use the evil extra column for the resolution "information". Should probably switch to using slit_length in collimation.
74        Duplicate/O qvals,dumWave
75        dumWave = dQv
76       
77        Make/N= (numpnts(dumWave)) dQw = 0
78       
79        // Run Name and title
80        NewDataFolder/O/S $(parentBase)
81        Make/T/N=1 $(parentBase + ":title") = {StringByKey("LABEL",note(inten),":",";")}
82        CreateStrNxCansas(fileID,nxcansasBase,"","title",$(parentBase + ":title"),empty,empty)
83        Make/T/N=1 $(parentBase + ":run") = {""}
84        CreateStrNxCansas(fileID,nxcansasBase,"","run",$(parentBase + ":run"),empty,empty)
85       
86        // SASData
87        String dataParent = nxcansasBase + "sasdata/"
88        // Create SASdata entry
89        String dataBase = parentBase + ":sasdata"
90        NewDataFolder/O/S $(dataBase)
91        Make/O/T/N=5 $(dataBase + ":attr") = {"canSAS_class","signal","I_axes","NX_class","Q_indices", "timestamp"}
92        Make/O/T/N=5 $(dataBase + ":attrVals") = {"SASdata","I","Q","NXdata","0",dateStr}
93        CreateStrNxCansas(fileID,dataParent,"","",empty,$(dataBase + ":attr"),$(dataBase + ":attrVals"))
94        // Create q entry
95        NewDataFolder/O/S $(dataBase + ":q")
96        Make/T/N=2 $(dataBase + ":q:attr") = {"units","resolutions"}
97        Make/T/N=2 $(dataBase + ":q:attrVals") = {"1/angstrom","dQl,dQw"}
98        CreateVarNxCansas(fileID,dataParent,"sasdata","Q",tq,$(dataBase + ":q:attr"),$(dataBase + ":q:attrVals"))
99        // Create i entry
100        NewDataFolder/O/S $(dataBase + ":i")
101        Make/T/N=2 $(dataBase + ":i:attr") = {"units","uncertainties"}
102        Make/T/N=2 $(dataBase + ":i:attrVals") = {"1/cm","Idev"}
103        CreateVarNxCansas(fileID,dataParent,"sasdata","I",ti,$(dataBase + ":i:attr"),$(dataBase + ":i:attrVals"))
104        // Create idev entry
105        CreateVarNxCansas(fileID,dataParent,"sasdata","Idev",te,units,inv_cm)
106        // Create qdev entry
107        CreateVarNxCansas(fileID,dataParent,"sasdata","dQl",dumWave,units,inv_angstrom)
108        CreateVarNxCansas(fileID,dataParent,"sasdata","dQw",dQw,units,inv_angstrom)
109        CreateVarNxCansas(fileID,dataParent,"sasdata","Qmean",res2,units,inv_angstrom)
110       
111        //////////////////////////////////////////////////////////////////////////////////////////////
112        // Write the meta data to the file
113        WriteUSANSNXcanSASMetaData(fileID,type,parentBase,nxcansasBase,dateStr)
114       
115        //write confirmation of write operation to history area
116        Print "Averaged NXcanSAS File written: ", GetFileNameFromPathNoSemi(fullPath)
117        KillWaves/Z dumWave
118       
119        // Close the file
120        if(fileID)
121                HDF5CloseFile /Z fileID
122        endif
123       
124End
125
126Function WriteNXcanSASUSANSDesmeared(fullpath,lo,hi,dialog)
127       
128        String fullpath
129        Variable lo,hi,dialog           //=1 will present dialog for name
130       
131        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
132        NVAR dQv = root:Packages:NIST:USANS:Globals:MainPanel:gDQv
133       
134        Variable fileID
135        String destStr=""
136        destStr = USANSFolder+":Smeared"
137        String dateStr=date()+" "+time()
138
139        String parentBase,nxcansasBase
140        String/G base = "root:NXcanSAS_USANS_file"
141       
142        Variable sasentry = NumVarOrDefault("root:Packages:NIST:gSASEntryNumber", 1)
143        sPrintf parentBase,"%s:sasentry%d",base,sasentry // Igor memory base path for all
144        sPrintf nxcansasBase,"/sasentry%d/",sasentry // HDF5 base path for all
145       
146        KillDataFolder/Z $base
147       
148        // Define common attribute waves
149        Make/T/N=1 empty = {""}
150        Make/T/N=1 units = {"units"}
151        Make/T/N=1 m = {"m"}
152        Make/T/N=1 mm = {"mm"}
153        Make/T/N=1 cm = {"cm"}
154        Make/T/N=1 angstrom = {"A"}
155        Make/T/N=1 inv_cm = {"1/cm"}
156        Make/T/N=1 inv_angstrom = {"1/A"}
157       
158        Variable refNum,integer,realval
159       
160        //*****these waves MUST EXIST, or IGOR Pro will crash, with a type 2 error****
161        WAVE qvals =$(destStr + "Q_dsm")
162        WAVE inten=$(destStr + "I_dsm")
163        WAVE sig=$(destStr + "S_dsm")
164               
165        if(dialog || stringmatch(fullpath, ""))
166                fileID = NxCansas_DoSaveFileDialog()
167        else
168                fileID = NxCansas_CreateFile(fullpath)
169        Endif
170       
171        //check each wave
172        If(!(WaveExists(qvals)))
173                Abort "qvals DNExist in WriteUSANSWaves()"
174        Endif
175        If(!(WaveExists(inten)))
176                Abort "inten DNExist in WriteUSANSWaves()"
177        Endif
178        If(!(WaveExists(sig)))
179                Abort "sig DNExist in WriteUSANSWaves()"
180        Endif
181       
182        //Use the evil extra column for the resolution "information". Should probably switch to using slit_length in collimation.
183        //write out partial set?
184        Duplicate/O qvals,tq,ti,te
185        ti=inten
186        te=sig
187        if( (lo!=hi) && (lo<hi))
188                redimension/N=(hi-lo+1) tq,ti,te,res1,res2,res3         //lo to hi, inclusive
189                tq=qvals[p+lo]
190                ti=inten[p+lo]
191                te=sig[p+lo]
192        endif
193       
194        Make/N= (numpnts(dumWave)) dQw = 0
195       
196        // Run Name and title
197        NewDataFolder/O/S $(parentBase)
198        Make/T/N=1 $(parentBase + ":title") = {StringByKey("LABEL",note(inten),":",";")}
199        CreateStrNxCansas(fileID,nxcansasBase,"","title",$(parentBase + ":title"),empty,empty)
200        Make/T/N=1 $(parentBase + ":run") = {""}
201        CreateStrNxCansas(fileID,nxcansasBase,"","run",$(parentBase + ":run"),empty,empty)
202       
203        // SASData
204        String dataParent = nxcansasBase + "sasdata/"
205        // Create SASdata entry
206        String dataBase = parentBase + ":sasdata"
207        NewDataFolder/O/S $(dataBase)
208        Make/O/T/N=5 $(dataBase + ":attr") = {"canSAS_class","signal","I_axes","NX_class","Q_indices", "timestamp"}
209        Make/O/T/N=5 $(dataBase + ":attrVals") = {"SASdata","I","Q","NXdata","0",dateStr}
210        CreateStrNxCansas(fileID,dataParent,"","",empty,$(dataBase + ":attr"),$(dataBase + ":attrVals"))
211        // Create q entry
212        NewDataFolder/O/S $(dataBase + ":q")
213        Make/T/N=2 $(dataBase + ":q:attr") = {"units","resolutions"}
214        Make/T/N=2 $(dataBase + ":q:attrVals") = {"1/angstrom","dQ"}
215        CreateVarNxCansas(fileID,dataParent,"sasdata","Q",tq,$(dataBase + ":q:attr"),$(dataBase + ":q:attrVals"))
216        // Create i entry
217        NewDataFolder/O/S $(dataBase + ":i")
218        Make/T/N=2 $(dataBase + ":i:attr") = {"units","uncertainties"}
219        Make/T/N=2 $(dataBase + ":i:attrVals") = {"1/cm","Idev"}
220        CreateVarNxCansas(fileID,dataParent,"sasdata","I",ti,$(dataBase + ":i:attr"),$(dataBase + ":i:attrVals"))
221        // Create idev entry
222        CreateVarNxCansas(fileID,dataParent,"sasdata","Idev",te,units,inv_cm)
223        // Create qdev entry
224        CreateVarNxCansas(fileID,dataParent,"sasdata","dQl",dumWave,units,inv_angstrom)
225        CreateVarNxCansas(fileID,dataParent,"sasdata","dQw",res1,units,inv_angstrom)
226        CreateVarNxCansas(fileID,dataParent,"sasdata","Qmean",res2,units,inv_angstrom)
227       
228        //
229        //////////////////////////////////////////////////////////////////////////////////////////////
230       
231        //////////////////////////////////////////////////////////////////////////////////////////////
232        //
233        // Write USANS meta data
234       
235        WriteUSANSNXcanSASMetaData(fileID,"SMEARED",parentBase,nxcansasBase,dateStr)
236       
237        //write confirmation of write operation to history area
238        Print "Averaged NXcanSAS File written: ", GetFileNameFromPathNoSemi(fullPath)
239        KillWaves/Z dumWave
240       
241        // Close the file
242        if(fileID)
243                HDF5CloseFile /Z fileID
244        endif
245       
246End     
247
248Function WriteUSANSNXcanSASMetaData(fileID,type,parentBase,nxcansasBase,dateStr)
249        Variable fileID
250        String type,parentBase,nxcansasBase,dateStr
251       
252        // tailor the output given the type of data written out...
253        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
254        WAVE inten_EMP=$(USANSFolder+":EMP:DetCts")
255        String samStr="",empStr="",samLabelStr="",paramStr="",empLevStr="",bkgLevStr=""
256        String pkStr="", processNote=""
257        NVAR TransWide = $(USANSFolder+":Globals:MainPanel:gTransWide")
258        NVAR TransRock = $(USANSFolder+":Globals:MainPanel:gTransRock")
259        NVAR empCts = $(USANSFolder+":Globals:MainPanel:gEmpCts")
260        NVAR bkgCts = $(USANSFolder+":Globals:MainPanel:gBkgCts")
261        NVAR thick = $(USANSFolder+":Globals:MainPanel:gThick")
262       
263        strswitch(type)
264                case "SAM":             
265                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
266                        empStr = "Uncorrected SAM data"
267                        empLevStr = "Uncorrected SAM data"
268                        bkgLevStr = "Uncorrected SAM data"
269                        paramStr = "Uncorrected SAM data"
270                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
271                        break                                           
272                case "EMP":     
273                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
274                        empStr = "Uncorrected EMP data"
275                        empLevStr = "Uncorrected EMP data"
276                        bkgLevStr = "Uncorrected EMP data"
277                        paramStr = "Uncorrected EMP data"
278                        pkStr += "EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))
279                        break
280                case "SMEARED":
281                        samStr = "SMEARED FILES: "+StringByKey("FILE",note(inten),":",";")
282                        empStr = "EMP FILES: "+StringByKey("FILE",note(inten_EMP),":",";")     
283                        empLevStr = "EMP LEVEL: " + num2str(empCts)
284                        bkgLevStr = "BKG LEVEL: " + num2str(bkgCts)
285                        paramStr = "Ds = "+num2str(thick)+" cm ; "
286                        paramStr += "Twide = "+num2Str(TransWide)+" ; "
287                        paramStr += "Trock = "+num2str(TransRock)       
288                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
289                        pkStr += " ; EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))
290                default:                //"COR" is the default 
291                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
292                        empStr = "EMP FILES: "+StringByKey("FILE",note(inten_EMP),":",";")     
293                        empLevStr = "EMP LEVEL: " + num2str(empCts)
294                        bkgLevStr = "BKG LEVEL: " + num2str(bkgCts)
295                        paramStr = "Ds = "+num2str(thick)+" cm ; "
296                        paramStr += "Twide = "+num2Str(TransWide)+" ; "
297                        paramStr += "Trock = "+num2str(TransRock)       
298                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
299                        pkStr += " ; EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))                             
300        endswitch
301
302        processNote = samStr+"\n"+dateStr+"\n"+samLabelStr+"\n"+empStr+"\n"+paramStr+"\n"+pkStr+"\n"
303        processNote += empLevStr + " ; "+bkglevStr+"\n"
304       
305        // SASinstrument
306        String instrParent = nxcansasBase + "sasinstrument/"
307        // Create SASinstrument entry
308        String instrumentBase = parentBase + ":sasinstrument"
309        NewDataFolder/O/S $(instrumentBase)
310        Make/O/T/N=5 $(instrumentBase + ":attr") = {"canSAS_class","NX_class"}
311        Make/O/T/N=5 $(instrumentBase + ":attrVals") = {"SASinstrument","NXinstrument"}
312        CreateStrNxCansas(fileID,instrParent,"","",empty,$(instrumentBase + ":attr"),$(instrumentBase + ":attrVals"))
313       
314        // SASaperture
315        String apertureParent = instrParent + "sasaperture/"
316        // Create SASaperture entry
317        String apertureBase = instrumentBase + ":sasaperture"
318        NewDataFolder/O/S $(apertureBase)
319        Make/O/T/N=5 $(apertureBase + ":attr") = {"canSAS_class","NX_class"}
320        Make/O/T/N=5 $(apertureBase + ":attrVals") = {"SASaperture","NXaperture"}
321        CreateStrNxCansas(fileID,apertureParent,"","",empty,$(apertureBase + ":attr"),$(apertureBase + ":attrVals"))
322       
323        // Create SASaperture shape entry
324        Make/O/T/N=1 $(apertureBase + ":shape") = {"slit"}
325        CreateStrNxCansas(fileID,apertureParent,"sasaperture","shape",$(apertureBase + ":shape"),empty,empty)
326        // Create SASaperture x_gap entry
327        Make/O/N=1 $(apertureBase + ":x_gap") = {0.1}
328        CreateVarNxCansas(fileID,apertureParent,"sasaperture","x_gap",$(apertureBase + ":x_gap"),units,cm)
329        // Create SASaperture y_gap entry
330        Make/O/N=1 $(apertureBase + ":y_gap") = {5.0}
331        CreateVarNxCansas(fileID,apertureParent,"sasaperture","y_gap",$(apertureBase + ":y_gap"),units,cm)
332
333        // SASdetector
334        String detectorParent = instrParent + "sasdetector/"
335        // Create SASdetector entry
336        String detectorBase = instrumentBase + ":sasdetector"
337        NewDataFolder/O/S $(detectorBase)
338        Make/O/T/N=5 $(detectorBase + ":attr") = {"canSAS_class","NX_class"}
339        Make/O/T/N=5 $(detectorBase + ":attrVals") = {"SASdetector","NXdetector"}
340        CreateStrNxCansas(fileID,detectorParent,"","",empty,$(detectorBase + ":attr"),$(detectorBase + ":attrVals"))
341        // Create SASdetector name entry
342        Make/O/T/N=1 $(detectorBase + ":name") = {"BT5 DETECTOR ARRAY"}
343        CreateStrNxCansas(fileID,detectorParent,"","name",$(detectorBase + ":name"),empty,empty)
344       
345        // SASsource
346        String sourceParent = instrParent + "sassource/"
347        // Create SASdetector entry
348        String sourceBase = instrumentBase + ":sassource"
349        NewDataFolder/O/S $(sourceBase)
350        Make/O/T/N=5 $(sourceBase + ":attr") = {"canSAS_class","NX_class"}
351        Make/O/T/N=5 $(sourceBase + ":attrVals") = {"SASsource","NXsource"}
352        CreateStrNxCansas(fileID,sourceParent,"","",empty,$(sourceBase + ":attr"),$(sourceBase + ":attrVals"))
353        // Create SASsource radiation entry
354        Make/O/T/N=1 $(sourceBase + ":radiation") = {"Reactor Neutron Source"}
355        CreateStrNxCansas(fileID,sourceParent,"","radiation",$(sourceBase + ":radiation"),empty,empty)
356        // Create SASsource incident_wavelength entry
357        Make/O/N=1 $(sourceBase + ":incident_wavelength") = {2.38}
358        CreateVarNxCansas(fileID,sourceParent,"","incident_wavelength",$(sourceBase + ":incident_wavelength"),units,angstrom)
359        // Create SASsource incident_wavelength_spread entry
360        Make/O/N=1 $(sourceBase + ":incident_wavelength_spread") = {0.06}
361        CreateVarNxCansas(fileID,sourceParent,"","incident_wavelength_spread",$(sourceBase + ":incident_wavelength_spread"),units,angstrom)
362       
363        // SASsample
364        String sampleParent = nxcansasBase + "sassample/"
365        // Create SASsample entry
366        String sampleBase = parentBase + ":sassample"
367        NewDataFolder/O/S $(sampleBase)
368        Make/O/T/N=5 $(sampleBase + ":attr") = {"canSAS_class","NX_class"}
369        Make/O/T/N=5 $(sampleBase + ":attrVals") = {"SASsample","NXsample"}
370        CreateStrNxCansas(fileID,sampleParent,"","",empty,$(sampleBase + ":attr"),$(sampleBase + ":attrVals"))
371        // Create SASsample name entry
372        Make/O/T/N=1 $(sampleBase + ":name") = {StringByKey("LABEL",note(inten),":",";")}
373        CreateStrNxCansas(fileID,sampleParent,"","name",$(sampleBase + ":name"),empty,empty)
374        // Create SASsample thickness entry
375        Make/O/N=1 $(sampleBase + ":thickness") = {thick}
376        CreateVarNxCansas(fileID,sampleParent,"","thickness",$(sampleBase + ":thickness"),units,cm)
377        // Create SASsample transmission entry
378        Make/O/N=1 $(sampleBase + ":transmission") = {TransWide}
379        CreateVarNxCansas(fileID,sampleParent,"","transmission",$(sampleBase + ":transmission"),empty,empty)
380       
381        // SASProcess
382        String processParent = nxcansasBase + "sasprocess/"
383        // Create SASsample entry
384        String processBase = parentBase + ":sasprocess"
385        NewDataFolder/O/S $(processBase)
386        Make/O/T/N=5 $(processBase + ":attr") = {"canSAS_class","NX_class"}
387        Make/O/T/N=5 $(processBase + ":attrVals") = {"SASprocess","NXprocess"}
388        CreateStrNxCansas(fileID,processParent,"","",empty,$(processBase + ":attr"),$(processBase + ":attrVals"))
389        // Create SASsample name entry
390        Make/O/T/N=1 $(processBase + ":name") = {"NIST IGOR"}
391        CreateStrNxCansas(fileID,processParent,"","name",$(processBase + ":name"),empty,empty)
392        // Create SASsample thickness entry
393        Make/O/T/N=1 $(sampleBase + ":note") = {processNote}
394        CreateVarNxCansas(fileID,processParent,"","note",$(processBase + ":note"),units,cm)
395End
Note: See TracBrowser for help on using the repository browser.