source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/WriteHDF5.ipf @ 1065

Last change on this file since 1065 was 1065, checked in by krzywon, 5 years ago

Add 2D data output to NXcanSAS writer.

File size: 31.6 KB
Line 
1#pragma rtGlobals=3             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5#include <HDF5 Browser>
6
7//************************
8// Vers 0.75 20170717
9//
10//************************
11
12
13//
14// TODO: Handle all values
15//
16
17
18///////////////////////////////////////////////////////////////////////////
19// Main method to be called - WriteNxCansas
20// Creates an HDF5 file and populates it with real values in Igor memory
21// If dialog and fullpath are left blank (0 and "", resp.), a test will be run
22
23Function WriteNxCanSAS1D(type,fullpath,dialog)
24        // Define input variables
25        String type // data location, in memory, relative to root:Packages:NIST:
26        String fullpath // file path and name where data will be saved
27        Variable dialog // if 1, prompt user for file path, otherwise, use fullpath
28       
29        // Define local function variables
30        Variable fileID
31        String destStr=""
32        String parentBase = "/sasentry/" // HDF5 base path for all
33        String/G base = "root:NXcanSAS_file"
34       
35        KillDataFolder/Z $base
36       
37        // Define local waves
38        Wave/T vals,attr,attrVals,textw
39        Wave intw,rw,qvals,inten,sig,qbar,sigmaq,fsubs
40       
41        // Define folder for data heirarchy
42        NewDataFolder/O/S root:NXcanSAS_file
43       
44        // Check fullpath and dialog
45        if(dialog || stringmatch(fullpath, ""))
46                fileID = NxCansas_DoSaveFileDialog()
47        else
48                NxCansas_CreateFile(fullpath)
49        Endif
50        if(!fileID)
51                Print "Unable to create file at " + fullpath + "."
52        else
53                WAVE intw,rw,qvals,inten,sig,qbar,sigmaq,fsubs
54                WAVE/T textw
55                if(stringmatch(type,""))
56                        // Test values for each data set
57                        Make/N=9 intw = {0,180.0,23,6254,16547,6178,22,2,0}
58                        Make/N=5 rw = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
59                        Make/T/N=2 textw= {"","","","","","","","","","",""}
60                        Make/N=10 qvals = {1,1,1,1,1,1,1,1,1,1} // qvals, inten, siq, qbar, sigmaq must be same length
61                        Make/N=10 inten = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
62                        Make/N=10 sig = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
63                        Make/N=10 qbar = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
64                        Make/N=10 sigmaq = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
65                        Make/N=10 fsubs = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
66                else
67                        destStr = "root:Packages:NIST:"+type
68       
69                        Variable refNum
70                        Variable step=1
71       
72                        //*****these waves MUST EXIST, or IGOR Pro will crash, with a type 2 error****
73                        WAVE intw = $(destStr + ":integersRead")
74                        WAVE rw = $(destStr + ":realsRead")
75                        WAVE/T textw=$(destStr + ":textRead")
76                        WAVE qvals =$(destStr + ":qval")
77                        WAVE inten=$(destStr + ":aveint")
78                        WAVE sig=$(destStr + ":sigave")
79                        WAVE qbar = $(destStr + ":QBar")
80                        WAVE sigmaq = $(destStr + ":SigmaQ")
81                        WAVE fsubs = $(destStr + ":fSubS")
82                endif
83        endif
84
85       
86        ///////////////////////////////////////////////////////////////////////////
87        // Write all data
88       
89        // Define common attribute waves
90        Make/T/N=1 empty = {""}
91        Make/T/N=1 units = {"units"}
92        Make/T/N=1 m = {"m"}
93        Make/T/N=1 mm = {"mm"}
94        Make/T/N=1 cm = {"cm"}
95        Make/T/N=1 pixel = {"pixel"}
96        Make/T/N=1 angstrom = {"angstrom"}
97        Make/T/N=1 inv_cm = {"1/cm"}
98        Make/T/N=1 inv_angstrom = {"1/angstrom"}
99       
100        // Run Name and title
101        NewDataFolder/O/S $(base + ":entry1")
102        Make/T/N=1 $(base + ":entry1:title") = {textw[6]}
103        CreateStrNxCansas(fileID,parentBase,"","title",$(base + ":entry1:title"),empty,empty)
104        Make/T/N=1 $(base + ":entry1:run") = {textw[0]}
105        CreateStrNxCansas(fileID,parentBase,"","run",$(base + ":entry1:run"),empty,empty)
106       
107        // SASData
108        String dataParent = parentBase + "sasdata/"
109        // Create SASdata entry
110        String dataBase = base + ":entry1:sasdata"
111        NewDataFolder/O/S $(dataBase)
112        Make/O/T/N=5 $(dataBase + ":attr") = {"canSAS_class","signal","I_axes","NX_class","Q_indices"}
113        Make/O/T/N=5 $(dataBase + ":attrVals") = {"SASdata","I","Q","NXdata","0"}
114        CreateStrNxCansas(fileID,dataParent,"","",empty,$(dataBase + ":attr"),$(dataBase + ":attrVals"))
115        // Create q entry
116        NewDataFolder/O/S $(dataBase + ":q")
117        Make/T/N=2 $(dataBase + ":q:attr") = {"units","resolutions"}
118        Make/T/N=2 $(dataBase + ":q:attrVals") = {"1/angstrom","Qdev"}
119        CreateVarNxCansas(fileID,dataParent,"sasdata","Q",qvals,$(dataBase + ":q:attr"),$(dataBase + ":q:attrVals"))
120        // Create i entry
121        NewDataFolder/O/S $(dataBase + ":i")
122        Make/T/N=2 $(dataBase + ":i:attr") = {"units","uncertainties"}
123        Make/T/N=2 $(dataBase + ":i:attrVals") = {"1/cm","Idev"}
124        CreateVarNxCansas(fileID,dataParent,"sasdata","I",inten,$(dataBase + ":i:attr"),$(dataBase + ":i:attrVals"))
125        // Create idev entry
126        NewDataFolder/O/S $(dataBase + ":idev")
127        CreateVarNxCansas(fileID,dataParent,"sasdata","Idev",sig,units,$(dataBase + ":idev:attrVals"))
128        // Create qdev entry
129        NewDataFolder/O/S $(dataBase + ":qdev")
130        CreateVarNxCansas(fileID,dataParent,"sasdata","Qdev",sigmaq,units,inv_angstrom)
131        CreateVarNxCansas(fileID,dataParent,"sasdata","Qmean",qbar,units,inv_angstrom)
132       
133        // SASinstrument
134        String instrParent = parentBase + "sasinstrument/"
135        // Create SASinstrument entry
136        String instrumentBase = base + ":entry1:sasinstrument"
137        NewDataFolder/O/S $(instrumentBase)
138        Make/O/T/N=5 $(instrumentBase + ":attr") = {"canSAS_class","NX_class"}
139        Make/O/T/N=5 $(instrumentBase + ":attrVals") = {"SASinstrument","NXinstrument"}
140        CreateStrNxCansas(fileID,instrParent,"","",empty,$(instrumentBase + ":attr"),$(instrumentBase + ":attrVals"))
141       
142        // SASaperture
143        String apertureParent = instrParent + "sasaperture/"
144        // Create SASaperture entry
145        String apertureBase = instrumentBase + ":sasaperture"
146        NewDataFolder/O/S $(apertureBase)
147        Make/O/T/N=5 $(apertureBase + ":attr") = {"canSAS_class","NX_class"}
148        Make/O/T/N=5 $(apertureBase + ":attrVals") = {"SASaperture","NXaperture"}
149        CreateStrNxCansas(fileID,apertureParent,"","",empty,$(apertureBase + ":attr"),$(apertureBase + ":attrVals"))
150        // Create SASaperture shape entry
151        Make/O/T/N=1 $(apertureBase + ":shape") = {"pinhole"} // TODO: Where do I get rectangular dimensions from?
152        CreateStrNxCansas(fileID,apertureParent,"sasaperture","shape",$(apertureBase + ":shape"),empty,empty)
153        // Create SASaperture x_gap entry
154        Make/O/N=1 $(apertureBase + ":x_gap") = {rw[24]} // TODO: Where do I get rectangular dimensions from?
155        CreateVarNxCansas(fileID,apertureParent,"sasaperture","x_gap",$(apertureBase + ":x_gap"),units,cm)
156        // Create SASaperture y_gap entry
157        Make/O/N=1 $(apertureBase + ":y_gap") = {rw[24]} // TODO: Where do I get rectangular dimensions from?
158        CreateVarNxCansas(fileID,apertureParent,"sasaperture","y_gap",$(apertureBase + ":y_gap"),units,cm)
159       
160        // SAScollimation
161        String collimationParent = instrParent + "sascollimation/"
162        // Create SAScollimation entry
163        String collimationBase = instrumentBase + ":sascollimation"
164        NewDataFolder/O/S $(collimationBase)
165        Make/O/T/N=5 $(collimationBase + ":attr") = {"canSAS_class","NX_class"}
166        Make/O/T/N=5 $(collimationBase + ":attrVals") = {"SAScollimation","NXcollimation"}
167        CreateStrNxCansas(fileID,collimationParent,"","",empty,$(collimationBase + ":attr"),$(collimationBase + ":attrVals"))
168        // Create SAScollimation length entry
169        Make/O/N=1 $(collimationBase + ":length") = {15.3} // TODO: Get real value
170        CreateVarNxCansas(fileID,collimationParent,"sasaperture","length",$(collimationBase + ":length"),units,m)
171        // Create SAScollimation distance entry
172        Make/O/N=1 $(collimationBase + ":distance") = {rw[25]}
173        CreateVarNxCansas(fileID,collimationParent,"sasaperture","distance",$(collimationBase + ":distance"),units,m)
174       
175        // SASdetector
176        String detectorParent = instrParent + "sasdetector/"
177        // Create SASdetector entry
178        String detectorBase = instrumentBase + ":sasdetector"
179        NewDataFolder/O/S $(detectorBase)
180        Make/O/T/N=5 $(detectorBase + ":attr") = {"canSAS_class","NX_class"}
181        Make/O/T/N=5 $(detectorBase + ":attrVals") = {"SASdetector","NXdetector"}
182        CreateStrNxCansas(fileID,detectorParent,"","",empty,$(detectorBase + ":attr"),$(detectorBase + ":attrVals"))
183        // Create SASdetector name entry
184        Make/O/T/N=1 $(detectorBase + ":name") = {"ORNL"} // TODO: Get real value
185        CreateStrNxCansas(fileID,detectorParent,"","name",$(detectorBase + ":name"),empty,empty)
186        // Create SASdetector distance entry
187        Make/O/N=1 $(detectorBase + ":SDD") = {rw[18]}
188        CreateVarNxCansas(fileID,detectorParent,"","SDD",$(detectorBase + ":SDD"),units,m)
189        // Create SASdetector beam_center_x entry
190        Make/O/N=1 $(detectorBase + ":beam_center_x") = {rw[16]}
191        CreateVarNxCansas(fileID,detectorParent,"","beam_center_x",$(detectorBase + ":beam_center_x"),units,pixel)
192        // Create SASdetector beam_center_y entry
193        Make/O/N=1 $(detectorBase + ":beam_center_y") = {rw[17]}
194        CreateVarNxCansas(fileID,detectorParent,"","beam_center_y",$(detectorBase + ":beam_center_y"),units,pixel)
195        // Create SASdetector x_pixel_size entry
196        Make/O/N=1 $(detectorBase + ":x_pixel_size") = {rw[10]}
197        CreateVarNxCansas(fileID,detectorParent,"","x_pixel_size",$(detectorBase + ":x_pixel_size"),units,mm)
198        // Create SASdetector y_pixel_size entry
199        Make/O/N=1 $(detectorBase + ":y_pixel_size") = {rw[13]}
200        CreateVarNxCansas(fileID,detectorParent,"","y_pixel_size",$(detectorBase + ":y_pixel_size"),units,mm)
201       
202        // SASsource
203        String sourceParent = instrParent + "sassource/"
204        // Create SASdetector entry
205        String sourceBase = instrumentBase + ":sassource"
206        NewDataFolder/O/S $(sourceBase)
207        Make/O/T/N=5 $(sourceBase + ":attr") = {"canSAS_class","NX_class"}
208        Make/O/T/N=5 $(sourceBase + ":attrVals") = {"SASsource","NXsource"}
209        CreateStrNxCansas(fileID,sourceParent,"","",empty,$(sourceBase + ":attr"),$(sourceBase + ":attrVals"))
210        // Create SASsource radiation entry
211        Make/O/T/N=1 $(sourceBase + ":radiation") = {"Reactor Neutron Source"}
212        CreateStrNxCansas(fileID,sourceParent,"","radiation",$(sourceBase + ":radiation"),empty,empty)
213        // Create SASsource incident_wavelength entry
214        Make/O/N=1 $(sourceBase + ":incident_wavelength") = {rw[26]}
215        CreateVarNxCansas(fileID,sourceParent,"","incident_wavelength",$(sourceBase + ":incident_wavelength"),units,angstrom)
216        // Create SASsource incident_wavelength_spread entry
217        Make/O/N=1 $(sourceBase + ":incident_wavelength_spread") = {rw[27]}
218        CreateVarNxCansas(fileID,sourceParent,"","incident_wavelength_spread",$(sourceBase + ":incident_wavelength_spread"),units,angstrom)
219       
220        // SASsample
221        String sampleParent = parentBase + "sassample/"
222        // Create SASsample entry
223        String sampleBase = base + ":entry1:sassample"
224        NewDataFolder/O/S $(sampleBase)
225        Make/O/T/N=5 $(sampleBase + ":attr") = {"canSAS_class","NX_class"}
226        Make/O/T/N=5 $(sampleBase + ":attrVals") = {"SASsample","NXsample"}
227        CreateStrNxCansas(fileID,sampleParent,"","",empty,$(sampleBase + ":attr"),$(sampleBase + ":attrVals"))
228        // Create SASsample name entry
229        Make/O/T/N=1 $(sampleBase + ":name") = {textw[6]}
230        CreateStrNxCansas(fileID,sampleParent,"","name",$(sampleBase + ":name"),empty,empty)
231        // Create SASsample thickness entry
232        Make/O/N=1 $(sampleBase + ":thickness") = {rw[5]}
233        CreateVarNxCansas(fileID,sampleParent,"","thickness",$(sampleBase + ":thickness"),units,cm)
234        // Create SASsample transmission entry
235        Make/O/N=1 $(sampleBase + ":transmission") = {rw[4]}
236        CreateVarNxCansas(fileID,sampleParent,"","transmission",$(sampleBase + ":transmission"),empty,empty)
237       
238        //
239        ///////////////////////////////////////////////////////////////////////////
240       
241        // Close the file
242        if(fileID)
243                HDF5CloseFile /Z fileID
244        endif
245       
246        // KillDataFolder/Z $base
247       
248End
249
250//
251///////////////////////////////////////////////////////////////////////////
252
253Function WriteNxCanSAS2D(type,fullpath,dialog)
254        // Define input variables
255        String type // data location, in memory, relative to root:Packages:NIST:
256        String fullpath // file path and name where data will be saved
257        Variable dialog // if 1, prompt user for file path, otherwise, use fullpath
258       
259        // Define local function variables
260        Variable fileID
261        Variable step=1,refnum
262        String destStr="",typeStr=""
263        String parentBase = "/sasentry/" // HDF5 base path for all
264        String/G base = "root:NXcanSAS_file"
265       
266        KillDataFolder/Z $base
267       
268        // Define local waves
269        Wave/T vals,attr,attrVals,textw
270        Wave intw,rw,qvals,inten,sig,qbar,sigmaq,fsubs
271       
272        // Define folder for data heirarchy
273        NewDataFolder/O/S root:NXcanSAS_file
274       
275        // Check fullpath and dialog
276        if(dialog || stringmatch(fullpath, ""))
277                fileID = NxCansas_DoSaveFileDialog()
278        else
279                NxCansas_CreateFile(fullpath)
280        Endif
281        if(!fileID)
282                Print "Unable to create file at " + fullpath + "."
283        else
284                WAVE intw,rw,qvals,inten,sig,qbar,sigmaq,fsubs
285                WAVE/T textw
286                if(stringmatch(type,""))
287                        // Test values for each data set
288                        Make/N=9 intw = {0,180.0,23,6254,16547,6178,22,2,0}
289                        Make/N=5 rw = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
290                        Make/T/N=2 textw= {"","","","","","","","","","",""}
291                        Make/N=10 qvals = {1,1,1,1,1,1,1,1,1,1} // qvals, inten, siq, qbar, sigmaq must be same length
292                        Make/N=10 inten = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
293                        Make/N=10 sig = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
294                        Make/N=10 qbar = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
295                        Make/N=10 sigmaq = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
296                        Make/N=10 fsubs = {0,0,0,0,0,0,0,0,0,0} // qvals, inten, siq, qbar, sigmaq must be same length
297                else
298                        destStr = "root:Packages:NIST:"+type
299       
300                        //must select the linear_data to export
301                        NVAR isLog = $(destStr+":gIsLogScale")
302                        if(isLog==1)
303                                typeStr = ":linear_data"
304                        else
305                                typeStr = ":data"
306                        endif
307       
308                        NVAR pixelsX = root:myGlobals:gNPixelsX
309                        NVAR pixelsY = root:myGlobals:gNPixelsY
310       
311                        Wave data=$(destStr+typeStr)
312                        Wave data_err=$(destStr+":linear_data_error")
313                        WAVE intw=$(destStr + ":integersRead")
314                        WAVE rw=$(destStr + ":realsRead")
315                        WAVE/T textw=$(destStr + ":textRead")
316                       
317                endif
318        endif
319       
320        ///////////////////////////////////////////////////////////////////////////
321        // Compute Qx, Qy data from pixel space
322       
323        Duplicate/O data,qx_val,qy_val,z_val,qval,qz_val,phi,r_dist
324       
325        Variable xctr,yctr,sdd,lambda,pixSize
326        xctr = rw[16]
327        yctr = rw[17]
328        sdd = rw[18]
329        lambda = rw[26]
330        pixSize = rw[13]/10             //convert mm to cm (x and y are the same size pixels)
331       
332        qx_val = CalcQx(p+1,q+1,rw[16],rw[17],rw[18],rw[26],rw[13]/10)          //+1 converts to detector coordinate system
333        qy_val = CalcQy(p+1,q+1,rw[16],rw[17],rw[18],rw[26],rw[13]/10)
334       
335        Redimension/N=(pixelsX*pixelsY) qx_val,qy_val,z_val
336
337        Variable L2 = rw[18]
338        Variable BS = rw[21]
339        Variable S1 = rw[23]
340        Variable S2 = rw[24]
341        Variable L1 = rw[25]
342        Variable lambdaWidth = rw[27]   
343        Variable usingLenses = rw[28]           //new 2007
344
345        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
346        Variable g = 981.0                              //gravity acceleration [cm/s^2]
347        Variable m_h    = 252.8                 // m/h [=] s/cm^2
348
349        Variable acc,ssd,lambda0,yg_d,qstar
350               
351        G = 981.  //!   ACCELERATION OF GRAVITY, CM/SEC^2
352        acc = vz_1              //      3.956E5 //!     CONVERT WAVELENGTH TO VELOCITY CM/SEC
353        SDD = L2        *100    //1317
354        SSD = L1        *100    //1627          //cm
355        lambda0 = lambda                //              15
356        YG_d = -0.5*G*SDD*(SSD+SDD)*(LAMBDA0/acc)^2
357        Print "DISTANCE BEAM FALLS DUE TO GRAVITY (CM) = ",YG_d
358        //              Print "Gravity q* = ",-2*pi/lambda0*2*yg_d/sdd
359        qstar = -2*pi/lambda0*2*yg_d/sdd
360       
361
362        // the gravity center is not the resolution center
363        // gravity center = beam center
364        // resolution center = offset y = dy + (2)*yg_d
365        ///************
366        // do everything to write out the resolution too
367        // un-comment these if you want to write out qz_val and qval too, then use the proper save command
368        qval = CalcQval(p+1,q+1,rw[16],rw[17],rw[18],rw[26],rw[13]/10)
369        qz_val = CalcQz(p+1,q+1,rw[16],rw[17],rw[18],rw[26],rw[13]/10)
370        //      phi = FindPhi( pixSize*((p+1)-xctr) , pixSize*((q+1)-yctr))             //(dx,dy)
371        //      r_dist = sqrt(  (pixSize*((p+1)-xctr))^2 +  (pixSize*((q+1)-yctr))^2 )          //radial distance from ctr to pt
372        phi = FindPhi( pixSize*((p+1)-xctr) , pixSize*((q+1)-yctr)+(2)*yg_d)            //(dx,dy+yg_d)
373        r_dist = sqrt(  (pixSize*((p+1)-xctr))^2 +  (pixSize*((q+1)-yctr)+(2)*yg_d)^2 )         //radial distance from ctr to pt
374        Redimension/N=(pixelsX*pixelsY) qz_val,qval,phi,r_dist
375        //everything in 1D now
376        Duplicate/O qval SigmaQX,SigmaQY,fsubS
377
378
379        //Two parameters DDET and APOFF are instrument dependent.  Determine
380        //these from the instrument name in the header.
381        //From conversation with JB on 01.06.99 these are the current good values
382        Variable DDet
383        NVAR apOff = root:myGlobals:apOff               //in cm
384        DDet = rw[10]/10                        // header value (X) is in mm, want cm here
385
386        Variable ret1,ret2,ret3,nq
387        nq = pixelsX*pixelsY
388        Variable ii = 0
389       
390        do
391                get2DResolution(qval[ii],phi[ii],lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,pixSize,usingLenses,r_dist[ii],ret1,ret2,ret3)
392                SigmaQX[ii] = ret1     
393                SigmaQY[ii] = ret2     
394                fsubs[ii] = ret3       
395                ii+=1
396        while(ii<nq)
397        //
398        ///////////////////////////////////////////////////////////////////////////
399
400       
401        ///////////////////////////////////////////////////////////////////////////
402        // Write all data
403       
404        // Define common attribute waves
405        Make/T/N=1 empty = {""}
406        Make/T/N=1 units = {"units"}
407        Make/T/N=1 m = {"m"}
408        Make/T/N=1 mm = {"mm"}
409        Make/T/N=1 cm = {"cm"}
410        Make/T/N=1 pixel = {"pixel"}
411        Make/T/N=1 angstrom = {"angstrom"}
412        Make/T/N=1 inv_cm = {"1/cm"}
413        Make/T/N=1 inv_angstrom = {"1/angstrom"}
414       
415        // Run Name and title
416        NewDataFolder/O/S $(base + ":entry1")
417        Make/T/N=1 $(base + ":entry1:title") = {textw[6]}
418        CreateStrNxCansas(fileID,parentBase,"","title",$(base + ":entry1:title"),empty,empty)
419        Make/T/N=1 $(base + ":entry1:run") = {textw[0]}
420        CreateStrNxCansas(fileID,parentBase,"","run",$(base + ":entry1:run"),empty,empty)
421       
422        // TODO: Change the steps to save 2D data instead of 1D
423       
424        // SASData
425        String dataParent = parentBase + "sasdata/"
426        // Create SASdata entry
427        String dataBase = base + ":entry1:sasdata"
428        NewDataFolder/O/S $(dataBase)
429        Make/O/T/N=5 $(dataBase + ":attr") = {"canSAS_class","signal","I_axes","NX_class","Q_indices"}
430        Make/O/T/N=5 $(dataBase + ":attrVals") = {"SASdata","I","Qx,Qy","NXdata","0,1"}
431        CreateStrNxCansas(fileID,dataParent,"","",empty,$(dataBase + ":attr"),$(dataBase + ":attrVals"))
432        // Create i entry
433        NewDataFolder/O/S $(dataBase + ":i")
434        Make/T/N=2 $(dataBase + ":i:attr") = {"units","uncertainties"}
435        Make/T/N=2 $(dataBase + ":i:attrVals") = {"1/cm","Idev"}
436        CreateVarNxCansas(fileID,dataParent,"sasdata","I",data,$(dataBase + ":i:attr"),$(dataBase + ":i:attrVals"))
437        // Create qx and qy entry
438        // data,qx_val,qy_val,z_val,qval,qz_val,phi,r_dist
439        NewDataFolder/O/S $(dataBase + ":qx")
440        Make/T/N=2 $(dataBase + ":qx:attr") = {"units","resolutions"}
441        Make/T/N=2 $(dataBase + ":qx:attrVals") = {"1/angstrom","Qxdev"}
442        CreateVarNxCansas(fileID,dataParent,"sasdata","Qx",qx_val,$(dataBase + ":qx:attr"),$(dataBase + ":qx:attrVals"))
443        NewDataFolder/O/S $(dataBase + ":qy")
444        Make/T/N=2 $(dataBase + ":qy:attr") = {"units","resolutions"}
445        Make/T/N=2 $(dataBase + ":qy:attrVals") = {"1/angstrom","Qydev"}
446        CreateVarNxCansas(fileID,dataParent,"sasdata","Qy",qy_val,$(dataBase + ":qy:attr"),$(dataBase + ":qy:attrVals"))
447        // Create idev entry
448        NewDataFolder/O/S $(dataBase + ":idev")
449        CreateVarNxCansas(fileID,dataParent,"sasdata","Idev",data_err,units,$(dataBase + ":idev:attrVals"))
450        // Create qdev entry
451        NewDataFolder/O/S $(dataBase + ":qxdev")
452        CreateVarNxCansas(fileID,dataParent,"sasdata","Qxdev",SigmaQX,units,inv_angstrom)
453        CreateVarNxCansas(fileID,dataParent,"sasdata","Qydev",SigmaQY,units,inv_angstrom)
454       
455        // SASinstrument
456        String instrParent = parentBase + "sasinstrument/"
457        // Create SASinstrument entry
458        String instrumentBase = base + ":entry1:sasinstrument"
459        NewDataFolder/O/S $(instrumentBase)
460        Make/O/T/N=5 $(instrumentBase + ":attr") = {"canSAS_class","NX_class"}
461        Make/O/T/N=5 $(instrumentBase + ":attrVals") = {"SASinstrument","NXinstrument"}
462        CreateStrNxCansas(fileID,instrParent,"","",empty,$(instrumentBase + ":attr"),$(instrumentBase + ":attrVals"))
463       
464        // SASaperture
465        String apertureParent = instrParent + "sasaperture/"
466        // Create SASaperture entry
467        String apertureBase = instrumentBase + ":sasaperture"
468        NewDataFolder/O/S $(apertureBase)
469        Make/O/T/N=5 $(apertureBase + ":attr") = {"canSAS_class","NX_class"}
470        Make/O/T/N=5 $(apertureBase + ":attrVals") = {"SASaperture","NXaperture"}
471        CreateStrNxCansas(fileID,apertureParent,"","",empty,$(apertureBase + ":attr"),$(apertureBase + ":attrVals"))
472        // Create SASaperture shape entry
473        Make/O/T/N=1 $(apertureBase + ":shape") = {"pinhole"} // TODO: Where do I get rectangular dimensions from?
474        CreateStrNxCansas(fileID,apertureParent,"sasaperture","shape",$(apertureBase + ":shape"),empty,empty)
475        // Create SASaperture x_gap entry
476        Make/O/N=1 $(apertureBase + ":x_gap") = {rw[24]} // TODO: Where do I get rectangular dimensions from?
477        CreateVarNxCansas(fileID,apertureParent,"sasaperture","x_gap",$(apertureBase + ":x_gap"),units,cm)
478        // Create SASaperture y_gap entry
479        Make/O/N=1 $(apertureBase + ":y_gap") = {rw[24]} // TODO: Where do I get rectangular dimensions from?
480        CreateVarNxCansas(fileID,apertureParent,"sasaperture","y_gap",$(apertureBase + ":y_gap"),units,cm)
481       
482        // SAScollimation
483        String collimationParent = instrParent + "sascollimation/"
484        // Create SAScollimation entry
485        String collimationBase = instrumentBase + ":sascollimation"
486        NewDataFolder/O/S $(collimationBase)
487        Make/O/T/N=5 $(collimationBase + ":attr") = {"canSAS_class","NX_class"}
488        Make/O/T/N=5 $(collimationBase + ":attrVals") = {"SAScollimation","NXcollimation"}
489        CreateStrNxCansas(fileID,collimationParent,"","",empty,$(collimationBase + ":attr"),$(collimationBase + ":attrVals"))
490        // Create SAScollimation length entry
491        Make/O/N=1 $(collimationBase + ":length") = {15.3} // TODO: Get real value
492        CreateVarNxCansas(fileID,collimationParent,"sasaperture","length",$(collimationBase + ":length"),units,m)
493        // Create SAScollimation distance entry
494        Make/O/N=1 $(collimationBase + ":distance") = {rw[25]}
495        CreateVarNxCansas(fileID,collimationParent,"sasaperture","distance",$(collimationBase + ":distance"),units,m)
496       
497        // SASdetector
498        String detectorParent = instrParent + "sasdetector/"
499        // Create SASdetector entry
500        String detectorBase = instrumentBase + ":sasdetector"
501        NewDataFolder/O/S $(detectorBase)
502        Make/O/T/N=5 $(detectorBase + ":attr") = {"canSAS_class","NX_class"}
503        Make/O/T/N=5 $(detectorBase + ":attrVals") = {"SASdetector","NXdetector"}
504        CreateStrNxCansas(fileID,detectorParent,"","",empty,$(detectorBase + ":attr"),$(detectorBase + ":attrVals"))
505        // Create SASdetector name entry
506        Make/O/T/N=1 $(detectorBase + ":name") = {"ORNL"} // TODO: Get real value
507        CreateStrNxCansas(fileID,detectorParent,"","name",$(detectorBase + ":name"),empty,empty)
508        // Create SASdetector distance entry
509        Make/O/N=1 $(detectorBase + ":SDD") = {rw[18]}
510        CreateVarNxCansas(fileID,detectorParent,"","SDD",$(detectorBase + ":SDD"),units,m)
511        // Create SASdetector beam_center_x entry
512        Make/O/N=1 $(detectorBase + ":beam_center_x") = {rw[16]}
513        CreateVarNxCansas(fileID,detectorParent,"","beam_center_x",$(detectorBase + ":beam_center_x"),units,pixel)
514        // Create SASdetector beam_center_y entry
515        Make/O/N=1 $(detectorBase + ":beam_center_y") = {rw[17]}
516        CreateVarNxCansas(fileID,detectorParent,"","beam_center_y",$(detectorBase + ":beam_center_y"),units,pixel)
517        // Create SASdetector x_pixel_size entry
518        Make/O/N=1 $(detectorBase + ":x_pixel_size") = {rw[10]}
519        CreateVarNxCansas(fileID,detectorParent,"","x_pixel_size",$(detectorBase + ":x_pixel_size"),units,mm)
520        // Create SASdetector y_pixel_size entry
521        Make/O/N=1 $(detectorBase + ":y_pixel_size") = {rw[13]}
522        CreateVarNxCansas(fileID,detectorParent,"","y_pixel_size",$(detectorBase + ":y_pixel_size"),units,mm)
523       
524        // SASsource
525        String sourceParent = instrParent + "sassource/"
526        // Create SASdetector entry
527        String sourceBase = instrumentBase + ":sassource"
528        NewDataFolder/O/S $(sourceBase)
529        Make/O/T/N=5 $(sourceBase + ":attr") = {"canSAS_class","NX_class"}
530        Make/O/T/N=5 $(sourceBase + ":attrVals") = {"SASsource","NXsource"}
531        CreateStrNxCansas(fileID,sourceParent,"","",empty,$(sourceBase + ":attr"),$(sourceBase + ":attrVals"))
532        // Create SASsource radiation entry
533        Make/O/T/N=1 $(sourceBase + ":radiation") = {"Reactor Neutron Source"}
534        CreateStrNxCansas(fileID,sourceParent,"","radiation",$(sourceBase + ":radiation"),empty,empty)
535        // Create SASsource incident_wavelength entry
536        Make/O/N=1 $(sourceBase + ":incident_wavelength") = {rw[26]}
537        CreateVarNxCansas(fileID,sourceParent,"","incident_wavelength",$(sourceBase + ":incident_wavelength"),units,angstrom)
538        // Create SASsource incident_wavelength_spread entry
539        Make/O/N=1 $(sourceBase + ":incident_wavelength_spread") = {rw[27]}
540        CreateVarNxCansas(fileID,sourceParent,"","incident_wavelength_spread",$(sourceBase + ":incident_wavelength_spread"),units,angstrom)
541       
542        // SASsample
543        String sampleParent = parentBase + "sassample/"
544        // Create SASsample entry
545        String sampleBase = base + ":entry1:sassample"
546        NewDataFolder/O/S $(sampleBase)
547        Make/O/T/N=5 $(sampleBase + ":attr") = {"canSAS_class","NX_class"}
548        Make/O/T/N=5 $(sampleBase + ":attrVals") = {"SASsample","NXsample"}
549        CreateStrNxCansas(fileID,sampleParent,"","",empty,$(sampleBase + ":attr"),$(sampleBase + ":attrVals"))
550        // Create SASsample name entry
551        Make/O/T/N=1 $(sampleBase + ":name") = {textw[6]}
552        CreateStrNxCansas(fileID,sampleParent,"","name",$(sampleBase + ":name"),empty,empty)
553        // Create SASsample thickness entry
554        Make/O/N=1 $(sampleBase + ":thickness") = {rw[5]}
555        CreateVarNxCansas(fileID,sampleParent,"","thickness",$(sampleBase + ":thickness"),units,cm)
556        // Create SASsample transmission entry
557        Make/O/N=1 $(sampleBase + ":transmission") = {rw[4]}
558        CreateVarNxCansas(fileID,sampleParent,"","transmission",$(sampleBase + ":transmission"),empty,empty)
559       
560        //
561        ///////////////////////////////////////////////////////////////////////////
562       
563        // Close the file
564        if(fileID)
565                HDF5CloseFile /Z fileID
566        endif
567       
568        // KillDataFolder/Z $base
569       
570End
571
572//
573///////////////////////////////////////////////////////////////////////////
574
575///////////////////////////////////////////////////////////////////////////
576// Basic file open and initialization routines
577
578// Select/create file through prompt
579Function NxCansas_DoSaveFileDialog()
580        Variable refNum, fileID
581        String message = "Save a file"
582        String outputPath
583        String fileFilters = "Data Files (*.h5):.h5;"
584        fileFilters += "All Files:.*;"
585        Open /D /F=fileFilters /M=message refNum
586        outputPath = S_fileName
587        fileID = NxCansas_CreateFile(outputPath)
588        return fileID
589End
590
591// Create file with a known path
592Function NxCansas_CreateFile(fullpath)
593        String fullpath
594        Variable fileID
595        print fullPath
596        fullpath = ReplaceString(":\\", fullpath, ":")
597        print fullPath
598        fullpath = ReplaceString("\\", fullpath, ":")
599        print fullPath
600        HDF5CreateFile /Z fileID as fullpath
601        NXCansas_InitializeFile(fileID)
602        return fileID
603End
604
605// Initialize the file to a base state
606Function NxCansas_InitializeFile(fileID)
607        Variable fileID
608        String parent
609        String/G base = "root:NXcanSAS_file"
610        Make/T/N=1 $(base + ":vals") = {""}
611        Make/T/N=3 $(base + ":attr") = {"NX_class", "canSAS_class", "version"}
612        Make/T/N=3 $(base + ":attrVals") = {"NXentry", "SASentry", "1.0"}
613        parent = "/sasentry/"
614        CreateStrNxCansas(fileID,parent,"","",$(base + ":vals"),$(base + ":attr"),$(base + ":attrVals"))
615        Make/T/N=1 $(base + ":entryAttr") = {""}
616        Make/T/N=1 $(base + ":entryAttrVals") = {""}
617        CreateStrNxCansas(fileID,parent,"","definition",NxCansas_strPtToWave("NXcanSAS"),$(base + ":entryAttr"),$(base + ":entryAttrVals"))
618End
619
620//
621///////////////////////////////////////////////////////////////////////////
622
623///////////////////////////////////////////////////////////////////////////
624// Functions to put values into usable form for NxCansas
625
626// Convert a number to a string
627Function /WAVE NxCansas_varToWave(number)
628        Variable number
629        Wave returnWave
630        Make/N=1 returnWave = {number}
631        return returnWave
632End
633
634// Create a single point wave from a string
635Function /WAVE NxCansas_strPtToWave(str)
636        String str
637        Wave/T returnWave
638        Make/T/N=1 returnWave = {str}
639        return returnWave
640End
641
642//
643///////////////////////////////////////////////////////////////////////////
644
645///////////////////////////////////////////////////////////////////////////
646// Functions used to save data to file
647
648// Intermediate error handler for saving variable waves - this function should be called instead of saveNxCansas
649Function CreateVarNxCansas(fileID,parent,group,var,valueWave,attr,attrValues)
650        Variable fileID
651        String parent,group,var
652        Wave valueWave
653        Wave /T attr,attrValues
654        Variable err
655        err = saveNxCansasVars(fileID,parent,group,var,valueWave,attr,attrValues)
656        if(err)
657                Print "NxCansas write err = ",err
658        endif
659End
660// Intermediate error handler for saving string waves - this function should be called instead of saveNxCansas
661Function CreateStrNxCansas(fileID,parent,group,var,valueWave,attr,attrValues)
662        Variable fileID
663        String parent,group,var
664        Wave /T valueWave,attr,attrValues
665        Variable err
666        err = saveNxCansasStrs(fileID,parent,group,var,valueWave,attr,attrValues)
667        if(err)
668                Print "NxCansas write err = ",err
669        endif
670End
671
672Function NxCansas_writeAttributes(fileID,path,attrNames,attrVals)
673        Variable fileID
674        String path
675        Wave/T attrNames, attrVals
676        int numAttrs,i
677        numAttrs = numpnts(attrNames)
678        Duplicate/O/T attrNames, names
679        Duplicate/O/T attrVals, vals
680       
681        for(i=0; i < numAttrs; i += 1)
682                String name_i = names[i]
683                String vals_i = vals[i]
684                Make/T/N=1 vals_i_wave
685                vals_i_wave[0] = vals_i
686                if(!stringmatch(name_i,""))
687                        HDF5SaveData /A=name_i vals_i_wave, fileID, path
688                endif
689        endfor
690       
691End
692
693Function NxCansas_CreateGroup(fileID,parent)
694        Variable fileID
695        String parent
696        Variable groupID
697        try     
698                if(!fileID)
699                        abort "HDF5 file does not exist"
700                endif
701               
702                // Create the group if it doesn't already exist
703                HDF5CreateGroup /Z fileID, parent, groupID
704                       
705        catch
706                // DO something if error is thrown
707                Print "NxCansas write err in saveNxCansas = ",V_AbortCode
708        endtry
709        return groupID
710End
711
712// Write in a single NxCansas element (from the STRUCTURE)
713// This method should only be called by CreateVarNxCansas
714Function saveNxCansasVars(fileID,parent,group,var,valueWave,attr,attrValues)
715        Variable fileID
716        String parent,group,var
717        Wave valueWave
718        Wave /T attr,attrValues
719        int i, numAttrs
720       
721        variable err=0, groupID
722        String NXentry_name
723       
724        groupID = NxCansas_CreateGroup(fileID,parent)
725
726        // Save data to disk
727        if(!stringmatch(var,""))
728                HDF5SaveData /O /Z /IGOR=0 valueWave, groupID, var
729                if (V_flag != 0)
730                        err = 1
731                        abort "Cannot save wave to HDF5 dataset " + var + " with V_flag of " + num2str(V_flag)
732                endif
733        endif
734               
735        NxCansas_writeAttributes(fileID,parent+var,attr,attrValues)
736       
737        // Close group and file to release resources
738        if(groupID)
739                HDF5CloseGroup /Z groupID
740        endif
741
742        return err
743end
744
745// Write in a single NxCansas element
746// This method should only be called by CreateStrNxCansas
747Function saveNxCansasStrs(fileID,parent,group,var,valueWave,attr,attrValues)
748        Variable fileID
749        String parent,group,var
750        Wave /T attr,attrValues, valueWave
751        int i, numAttrs
752       
753        variable err=0, groupID
754        String NXentry_name
755       
756        groupID = NxCansas_CreateGroup(fileID,parent)
757
758        // Save data to disk
759        if(!stringmatch(var,""))
760                HDF5SaveData /O /Z /IGOR=0 valueWave, groupID, var
761                if (V_flag != 0)
762                        err = 1
763                        abort "Cannot save wave to HDF5 dataset " + var + " with V_flag of " + num2str(V_flag)
764                endif
765        endif
766               
767        NxCansas_writeAttributes(fileID,parent+var,attr,attrValues)
768       
769        // Close group and file to release resources
770        if(groupID)
771                HDF5CloseGroup /Z groupID
772        endif
773
774        return err
775end
776
777//
778///////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.