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

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

Create single meta data writer for both 1D and 2D NXcanSAS data writers.

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