source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_IQ_Utilities.ipf @ 1076

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

woking on routines to correctly reduce data colloected with slit apertures. trying to get the data to absolute scale, with proper errro bars and resolution information.

Still not there yet.

File size: 18.2 KB
Line 
1#pragma TextEncoding = "MacRoman"               // For details execute DisplayHelpTopic "The TextEncoding Pragma"
2#pragma rtGlobals=3             // Use modern global access method and strict wave access.
3
4
5//
6// Operation does no scaling, only the basic (default) trim of the ends, concatenate, sort, and save
7// -- if data has been converted to WORK and the solid angle correction was done, then the data
8//   is per unit solid angle, and matches up - at least the simulated data does...
9//   It should match up in real VSANS data since the flux conditions are identical for
10//   all panels, only the geometry is different.
11//
12//
13// V_DataPlotting.ipf is where the I(q) panel is drawn and the binning is set
14//
15// see the VCALC BinAllMiddlePanels() for an example of this
16// see the binning routines in VC_DetectorBinning_Utils.ipf for the details
17//
18
19// TODO
20//
21// -- verify the binning for slit mode. Looks correct, but verify
22// -- DOCUMENT
23//
24// x- detector "B" is currently skipped since the calibration waves are not faked
25//    when the raw data is loaded. Then the qxqyqz waves are not generated.
26//
27// x- REDO the logic here. It's a mess, and will get the calculation wrong
28//
29// x- figure out the binning type (where is it set for VSANS?)
30// x- don't know, so currently VSANS binning type is HARD-WIRED
31// x- figure out when this needs to be called to (force) re-calculate I vs Q
32//
33
34
35
36//
37// NOTE
38// this is the master conversion function
39// ***Use no others
40// *** When other bin types are developed, DO NOT reassign these numbers.
41//  instead, skip the old numbers and assign new ones.
42//
43// - the numbers here in the switch can be out of order - it's fine
44//
45// old modes can be removed from the string constant ksBinTypeStr(n) (in V_Initialize.ipf), but the
46// mode numbers are what many different binning, plotting, and reduction functions are
47// switching on. In the future, it may be necessary to change the key (everywhere) to a string
48// switch, but for now, stick with the numbers.
49//
50// Strconstant ksBinTypeStr = "F4-M4-B;F2-M2-B;F1-M1-B;F2-M1-B;F1-M2xTB-B;F2-M2xTB-B;SLIT-F2-M2-B;"
51//
52//
53Function V_BinTypeStr2Num(binStr)
54        String binStr
55       
56        Variable binType
57        strswitch(binStr)       // string switch
58                case "F4-M4-B":
59                        binType = 1
60                        break           // exit from switch
61                case "F2-M2-B":
62                        binType = 2
63                        break           // exit from switch
64                case "F1-M1-B":
65                        binType = 3
66                        break           // exit from switch
67                case "SLIT-F2-M2-B":
68                        binType = 4
69                        break           // exit from switch
70
71                case "F2-M1-B":
72                        binType = 5
73                        break
74                case "F1-M2xTB-B":
75                        binType = 6
76                        break
77                case "F2-M2xTB-B":
78                        binType = 7
79                        break
80                       
81                default:                        // optional default expression executed
82                        binType = 0
83                        Abort "Binning mode not found"// when no case matches
84        endswitch       
85       
86        return(binType)
87end
88
89//
90// TODO -- binType == 4 (slit mode) should never end up here
91// -- new logic in calling routines to dispatch to proper routine
92// -- AND need to write the routine for binning_SlitMode
93//
94Function V_QBinAllPanels_Circular(folderStr,binType)
95        String folderStr
96        Variable binType
97
98        // do the back, middle, and front separately
99       
100//      figure out the binning type (where is it set?)
101        Variable ii,delQ
102        String detStr
103
104//      binType = V_GetBinningPopMode()
105
106        // set delta Q for binning (used later inside VC_fDoBinning_QxQy2D)
107        for(ii=0;ii<ItemsInList(ksDetectorListAll);ii+=1)
108                detStr = StringFromList(ii, ksDetectorListAll, ";")
109               
110                delQ = SetDeltaQ(folderStr,detStr)              // this sets (overwrites) the global value for each panel type
111        endfor
112       
113
114        switch(binType)
115                case 1:
116                        VC_fDoBinning_QxQy2D(folderStr,"FL")
117                        VC_fDoBinning_QxQy2D(folderStr,"FR")
118                        VC_fDoBinning_QxQy2D(folderStr,"FT")
119                        VC_fDoBinning_QxQy2D(folderStr,"FB")
120                        VC_fDoBinning_QxQy2D(folderStr,"ML")
121                        VC_fDoBinning_QxQy2D(folderStr,"MR")
122                        VC_fDoBinning_QxQy2D(folderStr,"MT")
123                        VC_fDoBinning_QxQy2D(folderStr,"MB")                   
124                        VC_fDoBinning_QxQy2D(folderStr, "B")           
125
126                        break
127                case 2:
128                        VC_fDoBinning_QxQy2D(folderStr,"FLR")
129                        VC_fDoBinning_QxQy2D(folderStr,"FTB")
130                        VC_fDoBinning_QxQy2D(folderStr,"MLR")
131                        VC_fDoBinning_QxQy2D(folderStr,"MTB")
132                        VC_fDoBinning_QxQy2D(folderStr, "B")           
133
134                        break
135                case 3:
136                        VC_fDoBinning_QxQy2D(folderStr,"MLRTB")
137                        VC_fDoBinning_QxQy2D(folderStr,"FLRTB")
138                        VC_fDoBinning_QxQy2D(folderStr, "B")           
139                       
140                        break
141                case 4:                         /// this is for a tall, narrow slit mode       
142                        VC_fBinDetector_byRows(folderStr,"FL")
143                        VC_fBinDetector_byRows(folderStr,"FR")
144                        VC_fBinDetector_byRows(folderStr,"ML")
145                        VC_fBinDetector_byRows(folderStr,"MR")
146                        VC_fBinDetector_byRows(folderStr,"B")
147
148                        break
149                case 5:
150                        VC_fDoBinning_QxQy2D(folderStr,"FTB")
151                        VC_fDoBinning_QxQy2D(folderStr,"FLR")
152                        VC_fDoBinning_QxQy2D(folderStr,"MLRTB")
153                        VC_fDoBinning_QxQy2D(folderStr, "B")           
154               
155                        break
156                case 6:
157                        VC_fDoBinning_QxQy2D(folderStr,"FLRTB")
158                        VC_fDoBinning_QxQy2D(folderStr,"MLR")
159                        VC_fDoBinning_QxQy2D(folderStr, "B")           
160               
161                        break
162                case 7:
163                        VC_fDoBinning_QxQy2D(folderStr,"FTB")
164                        VC_fDoBinning_QxQy2D(folderStr,"FLR")
165                        VC_fDoBinning_QxQy2D(folderStr,"MLR")
166                        VC_fDoBinning_QxQy2D(folderStr, "B")           
167               
168                        break
169                       
170                default:
171                        Abort "Binning mode not found in V_QBinAllPanels_Circular"// when no case matches       
172        endswitch
173       
174
175        return(0)
176End
177
178// concatenates and sorts the 1D data in "type" WORK folder
179// uses the current display if type==""
180//
181Function V_ConcatenateForSave(pathStr,type,tagStr,binType)
182        String pathStr,type,tagStr
183        Variable binType
184       
185// get the current display type, if null string passed in
186        SVAR curtype = root:Packages:NIST:VSANS:Globals:gCurDispType
187       
188        if(strlen(type)==0)
189                type = curType
190        endif
191
192// trim the data if needed
193        // remove the q=0 point from the back detector, if it's there
194        // does not need to know binType
195        NVAR gIgnoreDetB = root:Packages:NIST:VSANS:Globals:gIgnoreDetB
196        if(!gIgnoreDetB)
197                V_RemoveQ0_B(type)
198        endif
199
200// concatenate the data sets
201// TODO x- figure out which binning was used (this is done in V_1DConcatenate())
202        // clear the old tmp waves first, if they still exist
203//      SetDataFolder $("root:Packages:NIST:VSANS:"+type)
204        SetDataFolder $(pathStr+type)
205        Killwaves/Z tmp_q,tmp_i,tmp_s,tmp_sq,tmp_qb,tmp_fs
206        setDataFolder root:
207        V_1DConcatenate(pathStr,type,tagStr,binType)
208       
209// sort the data set
210        V_TmpSort1D(pathStr,type)
211       
212        return(0)
213End
214
215//
216// this is only called from the button on the data panel (**not anymore**)
217// so the type is the currently displayed type, and the binning is from the panel
218//
219Function V_SimpleSave1DData(pathStr,type,tagStr,saveName)
220        String pathStr,type,tagStr,saveName
221
222//
223// get the current display type, if null string passed in
224        SVAR curtype = root:Packages:NIST:VSANS:Globals:gCurDispType
225        Variable binType = V_GetBinningPopMode()
226       
227        V_ConcatenateForSave(pathStr,curType,tagStr,binType)
228       
229// write out the data set to a file
230        if(strlen(saveName)==0)
231                Execute "V_GetNameForSave()"
232                SVAR newName = root:saveName
233                saveName = newName
234        endif
235       
236        V_Write1DData(pathStr,curtype,saveName)
237
238End
239
240
241Proc V_GetNameForSave(str)
242        String str
243        String/G root:saveName=str
244End
245
246
247// blindly assumes that there is only one zero at the top of the wave
248// could be more sophisticated in the future...
249Function V_RemoveQ0_B(type)
250        String type
251       
252        SetDataFolder $("root:Packages:NIST:VSANS:"+type)
253
254        WAVE/Z qBin = qBin_qxqy_B
255        WAVE/Z iBin = iBin_qxqy_B
256        WAVE/Z eBin = eBin_qxqy_B
257        WAVE/Z nBin = nBin_qxqy_B
258        WAVE/Z iBin2 = iBin2_qxqy_B
259
260        // resolution waves
261        Wave/Z sigQ = sigmaQ_qxqy_B
262        Wave/Z qBar = qBar_qxqy_B
263        Wave/Z fSubS = fSubS_qxqy_B
264
265        if(qBin[0] == 0)
266                DeletePoints 0, 1, qBin,iBin,eBin,nBin,iBin2,sigQ,qBar,fSubS
267        endif
268       
269        SetDataFolder root:
270        return(0)
271end
272
273
274// concatentate data in folderStr
275//
276// TODO:
277// x- !!! Resolution waves are currently skipped - these must be added
278//
279// x- this currently ignores the binning type (one, two, etc. )
280// x- change the Concatenate call to use the waveList, to eliminate the need to declare all of the waves
281// -- this currently assumes that all of the waves exist
282// -- need robust error checking for wave existence
283// -- wave names are hard-wired and their name and location may be different in the future
284// x- if different averaging options were chosen (bin type of 2, 4 etc) then
285//    although waves may exist, they may not be the right ones to use. There
286//    will be a somewhat complex selection process
287// x- detector B is currently skipped
288//
289// this seems like a lot of extra work to do something so simple...but it's better than a loop
290//
291//  root:Packages:NIST:VSANS:RAW:iBin_qxqy_FB
292//
293// Now, the extensions needed for each binType are handled in a loop using the strings
294// defined globally for each of the numbered binTypes
295//
296// binType = 1 = one
297// binType = 2 = two
298// binType = 3 = four
299// binType = 4 = Slit Mode
300// binType = 5...
301//
302// if binType is passed in as -9999, get the binning mode from the popup
303// otherwise the value is assumed good (from a protocol)
304//
305// pathStr must have the trailing colon
306// tagStr is normally null, but is "_trim" for data to be trimmed
307//
308Function V_1DConcatenate(pathStr,folderStr,tagStr,binType)
309        String pathStr,folderStr,tagStr
310        Variable binType
311       
312
313        if(binType==-9999)
314                binType = V_GetBinningPopMode()
315        endif   
316       
317        String binTypeString = V_getBinTypeString(binType)
318        if(strlen(binTypeString) == 0)
319                DoAlert 0,"binTypeString is null in V_1DConcatenate"
320                return(0)
321        endif
322       
323//      SetDataFolder $("root:Packages:NIST:VSANS:"+folderStr)
324        SetDataFolder $(pathStr+folderStr)
325
326        //kill these waves before starting, or the new concatenation will be added to the old
327        KillWaves/Z tmp_q,tmp_i,tmp_s,tmp_qb,tmp_sq,tmp_fs
328
329        String q_waveListStr=""
330        String i_waveListStr=""
331        String s_waveListStr=""
332        String sq_waveListStr=""
333        String qb_waveListStr=""
334        String fs_waveListStr=""
335       
336        Variable num,ii
337        String item=""
338       
339        //Generate string lists of the waves to be concatenated based on the
340        // binTypeString (a global string constant with the extensions)
341        //
342       
343        NVAR gIgnoreDetB = root:Packages:NIST:VSANS:Globals:gIgnoreDetB
344        if(!gIgnoreDetB)
345                q_waveListStr =  "qBin_qxqy_B" + tagStr + ";"
346                i_waveListStr =  "iBin_qxqy_B" + tagStr + ";"
347                s_waveListStr =  "eBin_qxqy_B" + tagStr + ";"
348                sq_waveListStr =  "sigmaQ_qxqy_B" + tagStr + ";"
349                qb_waveListStr =  "qBar_qxqy_B" + tagStr + ";"
350                fs_waveListStr =  "fSubS_qxqy_B" + tagStr + ";"
351        endif
352
353        num = ItemsInList(binTypeString, ";")
354        for(ii=0;ii<num;ii+=1)
355                item = StringFromList(ii, binTypeString  ,";") 
356       
357        // "B" was handled outside the loop, be sure to skip here
358                if(cmpstr(item,"B") != 0)
359                        q_waveListStr +=  "qBin_qxqy_" + item + tagStr + ";"
360                        i_waveListStr +=  "iBin_qxqy_" + item + tagStr + ";"
361                        s_waveListStr +=  "eBin_qxqy_" + item + tagStr + ";"
362                        sq_waveListStr +=  "sigmaQ_qxqy_" + item + tagStr + ";"
363                        qb_waveListStr +=  "qBar_qxqy_" + item + tagStr + ";"
364                        fs_waveListStr +=  "fSubS_qxqy_" + item + tagStr + ";" 
365                endif
366        endfor
367       
368        // concatenate each of the sets
369
370        Concatenate/NP/O q_waveListStr, tmp_q
371       
372        Concatenate/NP/O i_waveListStr, tmp_i
373               
374        Concatenate/NP/O s_waveListStr, tmp_s
375               
376        Concatenate/NP/O sq_waveListStr, tmp_sq
377
378        Concatenate/NP/O qb_waveListStr, tmp_qb
379               
380        Concatenate/NP/O fs_waveListStr, tmp_fs
381                                                                               
382
383
384// Can't kill here, since they are still needed to sort and write out!
385//      KillWaves/Z tmp_q,tmp_i,tmp_s,tmp_res0,tmp_res1,tmp_res2,tmp_res3       
386       
387        SetDataFolder root:
388       
389        return(0)               
390End
391
392// TODO:
393// -- resolution waves are ignored, since they don't exist (yet)
394// -- only a sort is done, no rescaling of data sets
395//    (it's too late now anyways, since the data was concatenated)
396//
397// see Auto_Sort() in the SANS Automation ipf for the rest of the details of
398// how to combine the resolution waves (they also need to be concatenated, which is currently not done)
399//
400Function V_TmpSort1D(pathStr,folderStr)
401        String pathStr,folderStr
402       
403        SetDataFolder $(pathStr+folderStr)
404
405        Wave qw = tmp_q
406        Wave iw = tmp_i
407        Wave sw = tmp_s
408        Wave sq = tmp_sq
409        Wave qb = tmp_qb
410        Wave fs = tmp_fs
411       
412
413        Sort qw, qw,iw,sw,sq,qb,fs
414
415
416        SetDataFolder root:
417        return(0)
418End
419
420
421//
422Proc V_Load_Data_ITX()
423        V_Load_itx("","",0,0)
424end
425
426// TODO
427// -- fill in
428// -- link somewhere?
429//
430// a function to load in the individual I(q) sets which were written out to a single
431// file, in itx format.
432//
433// The data, like other 1D data sets, is to be loaded to its own folder under root
434//
435// Then, the data sets can be plotted as VSANS data sets, depending on which data extensions are present.
436// (and color coded)
437// (and used for setting the trimming)
438// (and...)
439//
440//
441// see A_LoadOneDDataToName(fileStr,outStr,doPlot,forceOverwrite)
442//
443Function V_Load_itx(fileStr,outStr,doPlot,forceOverwrite)
444        String fileStr, outstr
445        Variable doPlot,forceOverwrite
446
447        SetDataFolder root:             //build sub-folders for each data set under root
448
449        // if no fileStr passed in, display dialog now
450        if (cmpStr(fileStr,"") == 0)
451                fileStr = DoOpenFileDialog("Select a data file to load")
452                if (cmpstr(fileStr,"") == 0)
453                        String/G root:Packages:NIST:gLastFileName = ""
454                        return(0)               //get out if no file selected
455                endif
456        endif
457
458        //Load the waves, using default waveX names
459        //if no path or file is specified for LoadWave, the default Mac open dialog will appear
460        LoadWave/O/T fileStr
461//      LoadWave/G/D/A/Q fileStr
462        String fileNamePath = S_Path+S_fileName
463//              String basestr = ParseFilePath(3,ParseFilePath(5,fileNamePath,":",0,0),":",0,0)
464
465        String basestr
466        if (!cmpstr(outstr, ""))                //Outstr = "", cmpstr returns 0
467//                      enforce a short enough name here to keep Igor objects < 31 chars
468                baseStr = ShortFileNameString(CleanupName(S_fileName,0))
469                baseStr = CleanupName(baseStr,0)                //in case the user added odd characters
470                //baseStr = CleanupName(S_fileName,0)
471        else
472                baseStr = outstr                        //for output, hopefully correct length as passed in
473        endif
474
475//              print "basestr :"+basestr
476        String fileName =  ParseFilePath(0,ParseFilePath(5,filestr,":",0,0),":",1,0)
477//              print "filename :"+filename
478       
479        Variable ii,num=ItemsinList(S_waveNames)
480       
481        if(DataFolderExists("root:"+baseStr))
482                if (!forceOverwrite)
483                        DoAlert 1,"The file "+S_filename+" has already been loaded. Do you want to load the new data file, overwriting the data in memory?"
484                        if(V_flag==2)   //user selected No, don't load the data
485                                SetDataFolder root:
486                                for(ii=0;ii<num;ii+=1)         
487                                        KillWaves $(StringFromList(ii, S_waveNames))    // kill the waves that were loaded
488                                endfor
489                                if(DataFolderExists("root:Packages:NIST"))
490                                        String/G root:Packages:NIST:gLastFileName = filename
491                                endif
492                                return(0)       //quits the macro
493                        endif
494                endif
495                SetDataFolder $("root:"+baseStr)
496        else
497                NewDataFolder/S $("root:"+baseStr)
498        endif
499       
500//                      ////overwrite the existing data, if it exists
501
502// a semicolon-delimited list of wave names loaded
503//S_waveNames
504        for(ii=0;ii<num;ii+=1)         
505                Duplicate/O $("root:"+StringFromList(ii, S_waveNames)), $(StringFromList(ii, S_waveNames))
506        endfor
507
508
509// clean up
510        SetDataFolder root:
511
512        for(ii=0;ii<num;ii+=1)         
513                KillWaves $(StringFromList(ii, S_waveNames))    // kill the waves that were loaded
514        endfor
515//                      Duplicate/O $("root:"+n0), $w0
516//                      Duplicate/O $("root:"+n1), $w1
517//                      Duplicate/O $("root:"+n2), $w2
518       
519        // no resolution matrix to make
520
521       
522        return(0)
523End
524
525
526
527// string function to select the correct string constant
528// that corresponds to the selected binType. This string constant
529// contains the list of extensions to be used for plotting, saving, etc.
530//
531// returns null string if no match
532//
533Function/S V_getBinTypeString(binType)
534        Variable binType
535       
536        String detListStr=""
537        if(binType == 1)
538                detListStr = ksBinType1
539        endif
540        if(binType == 2)
541                detListStr = ksBinType2
542        endif
543        if(binType == 3)
544                detListStr = ksBinType3
545        endif
546        if(binType == 4)
547                detListStr = ksBinType4
548        endif
549        if(binType == 5)
550                detListStr = ksBinType5
551        endif
552        if(binType == 6)
553                detListStr = ksBinType6
554        endif
555        if(binType == 7)
556                detListStr = ksBinType7
557        endif
558       
559       
560        return(detListStr)
561End
562
563// given strings of the number of points to remove, loop over the detectors
564//
565// TODO
566// -- currently uses global strings or default strings
567// -- if proper strings (non-null) are passed in, they are used, otherwise global, then default
568Function V_Trim1DDataStr(folderStr,binType,nBegStr,nEndStr)
569        String folderStr
570        Variable binType
571        String nBegStr,nEndStr
572       
573        String detListStr=""
574
575        detListStr = V_getBinTypeString(binType)                //the list of extensions
576        if(strlen(detListStr)==0)
577                return(0)
578        endif
579
580       
581        //use global, then default values if null string passed in
582        if(strlen(nBegStr)==0)
583                SVAR/Z gBegPtsStr=root:Packages:NIST:VSANS:Globals:Protocols:gBegPtsStr
584                SVAR/Z gEndPtsStr=root:Packages:NIST:VSANS:Globals:Protocols:gEndPtsStr
585       
586                if(!SVAR_exists(gBegPtsStr) || !SVAR_exists(gEndPtsStr) || strlen(gBegPtsStr)==0 || strlen(gEndPtsStr)==0)
587                        nBegStr = ksBinTrimBegDefault
588                        nEndStr = ksBinTrimEndDefault
589                else
590                        nBegStr = gBegPtsStr
591                        nEndStr = gEndPtsStr
592                endif
593        endif   
594
595        Variable num, ii,nBeg,nEnd
596        String item,detstr
597       
598        num = ItemsInList(detListStr)
599        for(ii=0;ii<num;ii+=1)
600                detStr = StringFromList(ii, detListStr)
601                nBeg = NumberByKey(detStr, nBegStr,"=",";")
602                nEnd = NumberByKey(detStr, nEndStr,"=",";")
603                V_TrimOneSet(folderStr,detStr,nBeg,nEnd)
604        endfor
605
606        return(0)
607End
608
609// TODO
610// x- make this resolution-aware
611//
612Function V_TrimOneSet(folderStr,detStr,nBeg,nEnd)
613        String folderStr,detStr
614        Variable nBeg,nEnd
615       
616        SetDataFolder $("root:Packages:NIST:VSANS:"+folderStr)
617
618        Printf "%d points removed from beginning, %d points from the end  of %s \r",nbeg,nend,detStr
619
620// TODO
621// for each binType block:
622// --declare the waves
623// --make a copy of the waves??
624//      //--Break out resolution wave into separate waves
625// --delete the beginning points from everything
626        // --trim off the last nEnd points from everything
627//      --DeletePoints num-nEnd,nEnd, qw,iw,sw
628//      // --delete all points where the shadow is < 0.98
629////--Put resolution contents back???
630
631                Wave/Z qw = $("qBin_qxqy_"+detStr)
632                Wave/Z iw = $("iBin_qxqy_"+detStr)
633                Wave/Z ew = $("eBin_qxqy_"+detStr)
634                // resolution waves
635                Wave/Z sigQ = $("sigmaQ_qxqy_"+detStr)
636                Wave/Z qBar = $("qBar_qxqy_"+detStr)
637                Wave/Z fSubS = $("fSubS_qxqy_"+detStr)
638                       
639                DeletePoints 0,nBeg, qw,iw,ew,sigQ,qBar,fSubS
640
641                Variable npt
642                npt = numpnts(qw)
643                DeletePoints npt-nEnd,nEnd, qw,iw,ew,sigQ,qBar,fSubS
644       
645        return(0)
646End
647
648
649////
650//// returns 1 if the val is non-negative, other value
651//// indicates that the resoution data is USANS data.
652////
653//// TODO:
654//// -- this DUPLICATES a same-named SANS procedure, so there could be a clash at some point
655//// -- bigger issue - I'll need a better way to identify and load the different resolution
656////            conditions with VSANS
657////
658////
659//xFunction isSANSResolution(val)
660//      Variable val
661//     
662//      if(val >= 0)
663//              return(1)
664//      else
665//              return(0)
666//      endif
667//End
668
669
Note: See TracBrowser for help on using the repository browser.