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

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

Fix final issues with USANS data and copy changes to Demsmeared writer.

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