source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/USANS/WriteUSANSData.ipf @ 1103

Last change on this file since 1103 was 1091, checked in by srkline, 5 years ago

a number of changes, mostly to allow everything to compile.

added conditional compile to ensure that XML code would not be compiled if VSANS was present, since it's not XML-aware.

modified V_MainPanel to avoid conflicts with the SANS version. There still may be some functions hidden in procedures that do not have the V_ prefix yet, but these are either for functions that should point to a common file, or procedures that have been hidden from the VSANS panel

modified saving of VSANS mask files so that they can still be saved from teh deom version where home path is not defined.

  • Property eol-style set to native
  • Property svn:executable set to *
File size: 15.1 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma Version=2.20
3#pragma IgorVersion=6.1
4
5//////////////
6//for writing out data (q-i-s-dum-dum-dum) from the "type" folder
7//if fullpath is a complete HD path:filename, no dialog will be presented
8//if fullpath is just a filename, the save dialog will be presented (forced if dialog =1)
9//lo,hi are an (optional) range of the data[lo,hi] to save (in points)
10//if lo=hi=0, all of the data is written out
11//
12//////// 27 OCT 04
13// now writes 6-column data such that the last three columns are the divergence
14//  = a constant value, set in Init_MainUSANS()
15//
16Function WriteUSANSWaves(type,fullpath,lo,hi,dialog)
17        String type,fullpath
18        Variable lo,hi,dialog           //=1 will present dialog for name
19       
20        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
21
22       
23        String termStr="\r\n"           //VAX uses only <CR> as terminator, but only CRLF seems to FTP correctly to VAX
24        String destStr="",formatStr = "%15.6g %15.6g %15.6g %15.6g %15.6g %15.6g"+termStr
25        destStr = USANSFolder+":"+type
26       
27        Variable refNum,integer,realval
28       
29        //*****these waves MUST EXIST, or IGOR Pro will crash, with a type 2 error****
30        WAVE qvals =$(destStr + ":Qvals")
31        WAVE inten=$(destStr + ":DetCts")
32        WAVE sig=$(destStr + ":ErrDetCts")
33       
34        //check each wave
35        If(!(WaveExists(qvals)))
36                Abort "qvals DNExist in WriteUSANSWaves()"
37        Endif
38        If(!(WaveExists(inten)))
39                Abort "inten DNExist in WriteUSANSWaves()"
40        Endif
41        If(!(WaveExists(sig)))
42                Abort "sig DNExist in WriteUSANSWaves()"
43        Endif
44       
45        // 27 OCT 04 SRK
46        // make a dummy wave to hold the divergence, and write it as the last 3 columns
47        // and make the value negative as a flag for the analysis software
48        //
49        Duplicate/O qvals,dumWave
50        NVAR DQv=$(USANSFolder+":Globals:MainPanel:gDQv")
51        dumWave = - DQv
52        ///
53        if(dialog)
54                PathInfo/S savePathName
55                fullPath = DoSaveFileDialog("Save data as")
56                If(cmpstr(fullPath,"")==0)
57                        //user cancel, don't write out a file
58                        Close/A
59                        Abort "no data file was written"
60                Endif
61                //Print "dialog fullpath = ",fullpath
62        Endif
63       
64        //write out partial set?
65        Duplicate/O qvals,tq,ti,te
66        ti=inten
67        te=sig
68        if( (lo!=hi) && (lo<hi))
69                redimension/N=(hi-lo+1) tq,ti,te,dumWave                //lo to hi, inclusive
70                tq=qvals[p+lo]
71                ti=inten[p+lo]
72                te=sig[p+lo]
73        endif
74       
75        //tailor the output given the type of data written out...
76        WAVE inten_EMP=$(USANSFolder+":EMP:DetCts")
77        String samStr="",empStr="",dateStr="",samLabelStr="",paramStr="",empLevStr="",bkgLevStr=""
78        String pkStr=""
79        NVAR TransWide = $(USANSFolder+":Globals:MainPanel:gTransWide")
80        NVAR TransRock = $(USANSFolder+":Globals:MainPanel:gTransRock")
81        NVAR empCts = $(USANSFolder+":Globals:MainPanel:gEmpCts")
82        NVAR bkgCts = $(USANSFolder+":Globals:MainPanel:gBkgCts")
83        NVAR thick = $(USANSFolder+":Globals:MainPanel:gThick")
84       
85        strswitch(type)
86                case "SAM":             
87                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
88                        empStr = "Uncorrected SAM data"
89                        empLevStr = "Uncorrected SAM data"
90                        bkgLevStr = "Uncorrected SAM data"
91                        paramStr = "Uncorrected SAM data"
92                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
93                        break                                           
94                case "EMP":     
95                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
96                        empStr = "Uncorrected EMP data"
97                        empLevStr = "Uncorrected EMP data"
98                        bkgLevStr = "Uncorrected EMP data"
99                        paramStr = "Uncorrected EMP data"
100                        pkStr += "EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))
101                        break
102                default:                //"COR" is the default 
103                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
104                        empStr = "EMP FILES: "+StringByKey("FILE",note(inten_EMP),":",";")     
105                        empLevStr = "EMP LEVEL: " + num2str(empCts)
106                        bkgLevStr = "BKG LEVEL: " + num2str(bkgCts)
107                        paramStr = "Ds = "+num2str(thick)+" cm ; "
108                        paramStr += "Twide = "+num2Str(TransWide)+" ; "
109                        paramStr += "Trock = "+num2str(TransRock)       
110                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
111                        pkStr += " ; EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))                             
112        endswitch
113       
114        //these strings are always the same
115        dateStr="CREATED: "+date()+" at  "+time()
116        samLabelStr ="LABEL: "+StringByKey("LABEL",note(inten),":",";")
117       
118        //actually open the file
119        Open refNum as fullpath
120       
121        fprintf refnum,"%s"+termStr,samStr
122        fprintf refnum,"%s"+termStr,dateStr
123        fprintf refnum,"%s"+termStr,samLabelStr
124        fprintf refnum,"%s"+termStr,empStr
125        fprintf refnum,"%s"+termStr,paramStr
126        fprintf refnum,"%s"+termStr,pkStr
127        fprintf refnum,"%s"+termStr,empLevStr + " ; "+bkglevStr
128       
129        //
130        wfprintf refnum, formatStr, tq,ti,te,dumWave,dumWave,dumWave
131       
132        Close refnum
133       
134        Killwaves/Z ti,tq,te,dumWave
135       
136        Return(0)
137End
138
139
140// convert "old" 3-column .cor files to "new" 6-column files
141// Append the suffix="_6col.cor" to the new data files
142//
143// these "old" files will all have dQv = 0.037 (1/A) to match the
144// absolute scaling constant. "New" files written Nov 2004 or later
145// will have the appropriate values of dQv and scaling, as set in
146// the initialization routines.
147//
148// files were written out in the style above, 7 header lines, then the data
149//
150Function Convert3ColTo6Col()
151       
152        String termStr="\r\n"           //VAX uses only <CR> as terminator, but only CRLF seems to FTP correctly to VAX
153        String formatStr = "%15.6g %15.6g %15.6g %15.6g %15.6g %15.6g"+termStr
154        String newFName="",suffix="_6col.cor",fullpath=""
155       
156        Variable refNum_old,refnum_new,integer,realval
157       
158        // 08 NOV 04 SRK
159        Variable ii,dQv = -0.037                //hard-wired value for divergence (pre- NOV 2004 value)
160        ///
161       
162        Open/R/D/M="Select the 3-column data file"/T="????" refnum_old          //won't actually open the file
163        If(cmpstr(S_filename,"")==0)
164                //user cancel, don't write out a file
165                Close/A
166                Abort "no data file was written"
167        Endif
168        fullpath = S_filename
169        newFname = fullpath[0,strlen(fullpath)-5]+suffix
170        Print fullpath
171        Print newFname
172       
173        String tmpStr
174        String extraStr=""
175        sprintf extraStr,"%15.6g %15.6g %15.6g",dQv,dQv,dQv
176        extraStr += termStr
177        //actually open each of the files
178        Open/R refNum_old as fullpath
179        Open refNum_new as newFname
180       
181        //7 header lines
182        for(ii=0;ii<7;ii+=1)
183                FReadLine refNum_old, tmpStr            //returns only CR
184                fprintf refnum_new,tmpStr+"\n"          // add LF so file has CRLF
185        endfor
186       
187        do
188                FReadLine refNum_old, tmpStr
189                if(strlen(tmpStr)==0)
190                        break
191                endif
192                fprintf refnum_new,tmpStr[0,strlen(tmpStr)-2]+extraStr
193        while(1)
194       
195        Close refnum_old
196        Close refnum_new
197               
198        Return(0)
199End
200
201/////////XML Routines////////
202///AJJ Jan 2010
203///Have to put these here, annoyingly, because we depend on USANS specific functions
204///Need to think about consolidation of functions.
205
206//#if( Exists("XmlOpenFile") )
207#if( exists("XmlOpenFile") && exists("NCNR_VSANS") )
208
209
210Function WriteXMLUSANSWaves(type,fullpath,lo,hi,dialog)
211        String type,fullpath
212        Variable lo,hi,dialog           //=1 will present dialog for name
213       
214        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
215        NVAR dQv = root:Packages:NIST:USANS:Globals:MainPanel:gDQv
216       
217        Struct NISTXMLfile nf
218       
219        String termStr="\r\n"           //VAX uses only <CR> as terminator, but only CRLF seems to FTP correctly to VAX
220        String destStr="",formatStr = "%15.6g %15.6g %15.6g %15.6g %15.6g %15.6g"+termStr
221        destStr = USANSFolder+":"+type
222       
223        Variable refNum,integer,realval
224
225       
226        //*****these waves MUST EXIST, or IGOR Pro will crash, with a type 2 error****
227        WAVE qvals =$(destStr + ":Qvals")
228        WAVE inten=$(destStr + ":DetCts")
229        WAVE sig=$(destStr + ":ErrDetCts")
230       
231        //check each wave
232        If(!(WaveExists(qvals)))
233                Abort "qvals DNExist in WriteUSANSWaves()"
234        Endif
235        If(!(WaveExists(inten)))
236                Abort "inten DNExist in WriteUSANSWaves()"
237        Endif
238        If(!(WaveExists(sig)))
239                Abort "sig DNExist in WriteUSANSWaves()"
240        Endif
241       
242        //Use the evil extra column for the resolution "information". Should probably switch to using slit_length in collimation.
243        Duplicate/O qvals,dumWave
244        dumWave = dQv                   //written out as a positive value, since the column is identified by its label, dQl
245        ///
246       
247        if(dialog)
248                PathInfo/S catPathName
249                fullPath = DoSaveFileDialog("Save data as",fname="",suffix="."+type+"x")
250                If(cmpstr(fullPath,"")==0)
251                        //user cancel, don't write out a file
252                        Close/A
253                        Abort "no data file was written"
254                Endif
255                //Print "dialog fullpath = ",fullpath
256        Endif
257       
258        //write out partial set?
259        // duplicate the original data, all 3 waves
260        Duplicate/O qvals,tq,ti,te
261        ti=inten
262        te=sig
263        if( (lo!=hi) && (lo<hi))
264                redimension/N=(hi-lo+1) tq,ti,te,dumWave                //lo to hi, inclusive
265                tq=qvals[p+lo]
266                ti=inten[p+lo]
267                te=sig[p+lo]
268        endif
269       
270        //Data
271        Wave nf.Q = tq
272        nf.unitsQ = "1/A"
273        Wave nf.I = ti
274        nf.unitsI = "1/cm"
275        Wave nf.Idev = te
276        nf.unitsIdev = "1/cm"
277        // for slit-smeared USANS, set only a 4th column to  -dQv
278        Wave nf.dQl = dumWave
279        nf.unitsdQl= "1/A"
280
281        //write out the standard header information
282        //fprintf refnum,"FILE: %s\t\t CREATED: %s\r\n",textw[0],textw[1]
283       
284        //tailor the output given the type of data written out...
285        WAVE inten_EMP=$(USANSFolder+":EMP:DetCts")
286        String samStr="",empStr="",dateStr="",samLabelStr="",paramStr="",empLevStr="",bkgLevStr=""
287        String pkStr=""
288        NVAR TransWide = $(USANSFolder+":Globals:MainPanel:gTransWide")
289        NVAR TransRock = $(USANSFolder+":Globals:MainPanel:gTransRock")
290        NVAR empCts = $(USANSFolder+":Globals:MainPanel:gEmpCts")
291        NVAR bkgCts = $(USANSFolder+":Globals:MainPanel:gBkgCts")
292        NVAR thick = $(USANSFolder+":Globals:MainPanel:gThick")
293       
294        strswitch(type)
295                case "SAM":             
296                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
297                        empStr = "Uncorrected SAM data"
298                        empLevStr = "Uncorrected SAM data"
299                        bkgLevStr = "Uncorrected SAM data"
300                        paramStr = "Uncorrected SAM data"
301                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
302                        break                                           
303                case "EMP":     
304                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
305                        empStr = "Uncorrected EMP data"
306                        empLevStr = "Uncorrected EMP data"
307                        bkgLevStr = "Uncorrected EMP data"
308                        paramStr = "Uncorrected EMP data"
309                        pkStr += "EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))
310                        break
311                default:                //"COR" is the default 
312                        samStr = type +" FILES: "+StringByKey("FILE",note(inten),":",";")
313                        empStr = "EMP FILES: "+StringByKey("FILE",note(inten_EMP),":",";")     
314                        empLevStr = "EMP LEVEL: " + num2str(empCts)
315                        bkgLevStr = "BKG LEVEL: " + num2str(bkgCts)
316                        paramStr = "Ds = "+num2str(thick)+" cm ; "
317                        paramStr += "Twide = "+num2Str(TransWide)+" ; "
318                        paramStr += "Trock = "+num2str(TransRock)       
319                        pkStr += "SAM PEAK ANGLE: "+num2str(QpkFromNote("SAM"))
320                        pkStr += " ; EMP PEAK ANGLE: "+num2str(QpkFromNote("EMP"))                             
321        endswitch
322       
323       
324        //AJJ to fix with sensible values
325        nf.run = ""
326        nf.nameSASinstrument = "BT5 USANS"
327        nf.SASnote = ""
328        //
329        nf.sample_ID = ""
330        nf.title = StringByKey("LABEL",note(inten),":",";")
331        nf.radiation = "neutron"
332        nf.wavelength = 2.38
333        nf.unitswavelength = "A"
334        nf.sample_thickness = thick
335        nf.unitssample_thickness = "cm"
336       
337        //Do something with beamstop (rw[21])
338        nf.detector_name = "BT5 DETECTOR ARRAY"
339
340        nf.SASprocessnote = samStr+"\n"
341        nf.SASprocessnote += dateStr+"\n"
342        nf.SASprocessnote += samLabelStr+"\n"
343        nf.SASprocessnote += empStr+"\n"
344        nf.SASprocessnote += paramStr+"\n"
345        nf.SASprocessnote += pkStr+"\n"
346        nf.SASprocessnote += empLevStr + " ; "+bkglevStr+"\n"
347       
348        nf.nameSASProcess = "NIST IGOR"
349
350        //Close refnum
351       
352        writeNISTXML(fullpath, nf)
353       
354        SetDataFolder root:             //(redundant)
355       
356        //write confirmation of write operation to history area
357        Print "Averaged XML File written: ", GetFileNameFromPathNoSemi(fullPath)
358        KillWaves/Z dumWave
359        Return(0)
360End
361
362Function WriteXMLUSANSDesmeared(fullpath,lo,hi,dialog)
363        String fullpath
364        Variable lo,hi,dialog           //=1 will present dialog for name
365       
366        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder       
367       
368        Struct NISTXMLfile nf
369       
370        String termStr="\r\n"
371        String destStr = USANSFolder+":DSM:"
372        String formatStr = "%15.6g %15.6g %15.6g %15.6g %15.6g %15.6g"+termStr
373       
374        Variable refNum,integer,realval
375       
376        //*****these waves MUST EXIST, or IGOR Pro will crash, with a type 2 error****
377        WAVE Q_dsm =$(destStr + "Q_dsm")
378        WAVE I_dsm=$(destStr + "I_dsm")
379        WAVE S_dsm=$(destStr + "S_dsm")
380       
381        //check each wave
382        If(!(WaveExists(Q_dsm)))
383                Abort "Q_dsm DNExist in WriteUSANSDesmeared()"
384        Endif
385        If(!(WaveExists(I_dsm)))
386                Abort "I_dsm DNExist in WriteUSANSDesmeared()"
387        Endif
388        If(!(WaveExists(S_dsm)))
389                Abort "S_dsm DNExist in WriteUSANSDesmeared()"
390        Endif
391       
392        // 06 FEB 06 SRK
393        // make dummy waves to hold the "fake" resolution, and write it as the last 3 columns
394        //
395        Duplicate/O Q_dsm,res1,res2,res3
396        res3 = 1                // "fake" beamstop shadowing
397        res1 /= 100             //make the sigmaQ so small that there is no smearing
398       
399        if(dialog)
400                Open/D refnum as fullpath+".dsmx"               //won't actually open the file
401                If(cmpstr(S_filename,"")==0)
402                        //user cancel, don't write out a file
403                        Close/A
404                        Abort "no data file was written"
405                Endif
406                fullpath = S_filename
407        Endif
408       
409        //write out partial set?
410        Duplicate/O Q_dsm,tq,ti,te
411        ti=I_dsm
412        te=S_dsm
413        if( (lo!=hi) && (lo<hi))
414                redimension/N=(hi-lo+1) tq,ti,te,res1,res2,res3         //lo to hi, inclusive
415                tq=Q_dsm[p+lo]
416                ti=I_dsm[p+lo]
417                te=S_dsm[p+lo]
418        endif
419       
420                //Data
421        Wave nf.Q = tq
422        nf.unitsQ = "1/A"
423        Wave nf.I = ti
424        nf.unitsI = "1/cm"
425        Wave nf.Idev = te
426        nf.unitsIdev = "1/cm"
427        Wave nf.Qdev = res1
428        nf.unitsQdev = "1/A"
429        Wave nf.Qmean = res2
430        nf.unitsQmean = "1/A"
431        Wave nf.Shadowfactor = res3
432        nf.unitsShadowfactor = "none"
433       
434        //tailor the output given the type of data written out...
435        String samStr="",dateStr="",str1,str2
436       
437        NVAR m = $(USANSFolder+":DSM:gPowerM")                          // power law exponent
438        NVAR chiFinal = $(USANSFolder+":DSM:gChi2Final")                //chi^2 final
439        NVAR iter = $(USANSFolder+":DSM:gIterations")           //total number of iterations
440       
441        //get the number of spline passes from the wave note
442        String noteStr
443        Variable boxPass,SplinePass
444        noteStr=note(I_dsm)
445        BoxPass = NumberByKey("BOX", noteStr, "=", ";")
446        splinePass = NumberByKey("SPLINE", noteStr, "=", ";")
447       
448        samStr = fullpath
449        dateStr="CREATED: "+date()+" at  "+time()
450        sprintf str1,"Chi^2 = %g   PowerLaw m = %4.2f   Iterations = %d",chiFinal,m,iter
451        sprintf str2,"%d box smooth passes and %d smoothing spline passes",boxPass,splinePass
452       
453        //AJJ to fix with sensible values
454        nf.run = "Test"
455        nf.nameSASinstrument = "BT5 USANS"
456        nf.SASnote = ""
457        //
458        nf.sample_ID = ""
459        nf.title = samstr
460        nf.radiation = "neutron"
461        nf.wavelength = 2.38
462        nf.unitswavelength = "A"
463       
464        //Do something with beamstop (rw[21])
465        nf.detector_name = "BT5 DETECTOR ARRAY"
466
467        nf.SASprocessnote = samStr+"\n"
468        nf.SASprocessnote += str1+"\n"
469        nf.SASprocessnote += str2+"\n"
470        nf.SASprocessnote += datestr+"\n"
471
472       
473        nf.nameSASProcess = "NIST IGOR"
474
475        //Close refnum
476       
477        writeNISTXML(fullpath, nf)
478       
479        SetDataFolder root:             //(redundant)
480       
481        KillWaves/Z res1,res2,res2,ti,te,tq
482       
483        Return(0)
484End
485
486
487#else   // if( Exists("XmlOpenFile") )
488        // No XMLutils XOP: provide dummy function so that IgorPro can compile dependent support code
489       
490       
491        Function WriteXMLUSANSWaves(type,fullpath,lo,hi,dialog)
492                String type,fullpath
493                Variable lo,hi,dialog           //=1 will present dialog for name
494       
495            Abort  "XML function provided by XMLutils XOP is not available, get the XOP from : http://www.igorexchange.com/project/XMLutils (see http://www.smallangles.net/wgwiki/index.php/cansas1d_binding_IgorPro for details)"
496                return(-6)
497        end
498       
499        Function WriteXMLUSANSDesmeared(fullpath,lo,hi,dialog)
500                String fullpath
501                Variable lo,hi,dialog           //=1 will present dialog for name
502       
503            Abort  "XML function provided by XMLutils XOP is not available, get the XOP from : http://www.igorexchange.com/project/XMLutils (see http://www.smallangles.net/wgwiki/index.php/cansas1d_binding_IgorPro for details)"
504                return(-6)
505        end
506                       
507       
508       
509#endif
Note: See TracBrowser for help on using the repository browser.