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

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

Create a method to open or create a new file for all NXcanSAS writers.

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