source: sans/Dev/branches/nxcansas_writer/NCNR_User_Procedures/Reduction/SANS/WriteHDF5.ipf @ 1189

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

Incorporate suggested changes for cleaner separation of VSANS, USANS, and SANS reduction methods. Plus fixes.

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