source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_PatchFiles.ipf @ 1037

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

re-wrote raw data reader for significant speed boost (mostly by skipping the read of DAS_logs)

added centroid calculation to the Marquee

  • Property svn:executable set to *
File size: 63.0 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5
6//////////////////////////////////
7//
8// TODO --  January 2107
9//
10// This is experimental, making the patch work with list boxes as "groups" of the Nexus file structure
11//
12// some of the groupings are more natural, some will need to be re-organized for the
13// more natural needs of what will typically be patched in the most common cases.
14//
15///////////////////////////////
16
17// TODOs have been inserted to comment out all of the calls that don't compile and need to be replaced
18
19// TODO
20// -- not all of the functions here have been prefixed with "V_", especially the action procedures from the panel
21//   so this cannot be opened with the SANS Reduction, or there will be clashes
22// -- same file load/reload issue as with other operations that read a field from the file. ANY read requires
23//   that the entire file is read in, even just to check and see if it's raw data... then there is a local
24//   copy present to confuse matters of what was actually written
25//
26// -- for the batch entering of fields, when all of the proper beam center values are determined, then
27//    all (2 x 9 = 18) of these values will need to be entered in all of the data files that "match" this
28//    "configuration" - however a configuration is to be defined and differentiated from other configurations.
29//
30// -- there may be other situations where batch entering needs are
31//               different, and this may lead to different interface choices
32//
33// -- need to add some mechanism (new panel?) to enter:
34//    -- box coordinates
35//    -- ABS parameters
36//    -- averaging options -- these will have new options versus SANS (binning panels, slit mode, etc.)
37//
38
39
40//**************************
41//
42//procedures required to allow patching of raw vSANS data headers
43//information for the Patch Panel is stored in the root:Packages:NIST:VSANS:Globals:Patch subfolder
44//
45// writes changes directly to the raw data headers as requested
46// * note that if a data file is currently in a work folder, the (real) header on disk
47// will be updated, but the data in the (WORK) folder will not reflect these changes, unless
48// the data folder is first cleared and the data is re-loaded
49//
50//**************************
51
52//main entry procedure for displaying the Patch Panel
53//
54Proc V_PatchFiles()
55       
56        DoWindow/F V_Patch_Panel
57        If(V_flag == 0)
58                V_InitializePatchPanel()
59                //draw panel
60                V_Patch_Panel()
61        Endif
62End
63
64//initialization of the panel, creating the necessary data folder and global
65//variables if necessary -
66//
67// root:Packages:NIST:VSANS:Globals:
68Proc V_InitializePatchPanel()
69        //create the global variables needed to run the Patch Panel
70        //all are kept in root:Packages:NIST:VSANS:Globals:Patch
71        If( ! (DataFolderExists("root:Packages:NIST:VSANS:Globals:Patch"))  )
72                //create the data folder and the globals for BOTH the Patch and Trans panels
73                NewDataFolder/O root:Packages:NIST:VSANS:Globals:Patch
74        Endif
75        V_CreatePatchGlobals()          //re-create them every time (so text and radio buttons are correct)
76End
77
78//the data folder root:Packages:NIST:VSANS:Globals:Patch must exist
79//
80Proc V_CreatePatchGlobals()
81        //ok, create the globals
82        String/G root:Packages:NIST:VSANS:Globals:Patch:gPatchMatchStr = "*"
83        String/G root:Packages:NIST:VSANS:Globals:Patch:gPatchCurLabel = "no file selected"
84       
85        PathInfo catPathName
86        If(V_flag==1)
87                String dum = S_path
88                String/G root:Packages:NIST:VSANS:Globals:Patch:gCatPathStr = dum
89        else
90                String/G root:Packages:NIST:VSANS:Globals:Patch:gCatPathStr = "no path selected"
91        endif
92        String/G root:Packages:NIST:VSANS:Globals:Patch:gPatchList = "none"
93
94        Variable/G root:Packages:NIST:VSANS:Globals:Patch:gRadioVal = 1
95       
96
97        SetDataFolder root:Packages:NIST:VSANS:Globals:Patch:   
98        Make/O/T/N=(10,3) PP_ListWave
99        Make/O/B/N=(10,3) PP_SelWave
100        Make/O/T/N=3 PP_TitleWave
101       
102        PP_TitleWave = {"Ch?","Label","Value"}
103       
104        PP_SelWave[][0] = 2^5           // checkboxes
105        PP_SelWave[][2] = 2^1           // 3rd column editable
106       
107       
108        SetDataFolder root:
109       
110End
111
112
113//panel recreation macro for the PatchPanel...
114//
115Proc V_Patch_Panel()
116        PauseUpdate; Silent 1      // building window...
117        NewPanel /W=(533,50,1140,588)/K=2 as "Patch Raw VSANS Data Files"
118        DoWindow/C V_Patch_Panel
119//      ShowTools/A
120        SetDataFolder root:Packages:NIST:VSANS:Globals:Patch:
121
122       
123        ModifyPanel cbRGB=(11291,48000,3012)
124        ModifyPanel fixedSize=1
125        SetDrawLayer UserBack
126        DrawLine 7,30,422,30
127
128       
129        SetVariable PathDisplay,pos={77,7},size={310,13},title="Path"
130        SetVariable PathDisplay,help={"This is the path to the folder that will be used to find the SANS data while patching. If no files appear in the popup, make sure that this folder is set correctly"}
131        SetVariable PathDisplay,font="Courier",fSize=10
132        SetVariable PathDisplay,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Patch:gCatPathStr
133        Button PathButton,pos={2,3},size={70,20},proc=V_PickPathButton,title="Pick Path"
134        Button PathButton,help={"Select the folder containing the raw SANS data files"}
135        Button helpButton,pos={400,3},size={25,20},proc=V_ShowPatchHelp,title="?"
136        Button helpButton,help={"Show the help file for patching raw data headers"}
137        PopupMenu PatchPopup,pos={4,37},size={156,19},proc=V_PatchPopMenuProc,title="File(s) to Patch"
138        PopupMenu PatchPopup,help={"The displayed file is the one that will be edited. The entire list will be edited if \"Change All..\" is selected. \r If no items, or the wrong items appear, click on the popup to refresh. \r List items are selected from the file based on MatchString"}
139        PopupMenu PatchPopup,mode=1,popvalue="none",value= #"root:Packages:NIST:VSANS:Globals:Patch:gPatchList"
140
141        Button CHButton,pos={314,37},size={110,20},proc=V_ChangeHeaderButtonProc,title="Change Header"
142        Button CHButton,help={"This will change the checked values (ONLY) in the single file selected in the popup."}
143        SetVariable PMStr,pos={6,63},size={174,13},proc=V_SetMatchStrProc,title="Match String"
144        SetVariable PMStr,help={"Enter the search string to narrow the list of files. \"*\" is the wildcard character. After entering, \"pop\" the menu to refresh the file list."}
145        SetVariable PMStr,font="Courier",fSize=10
146        SetVariable PMStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Patch:gPatchMatchStr
147        Button ChAllButton,pos={245,60},size={180,20},proc=V_ChAllHeadersButtonProc,title="Change All Headers in List"
148        Button ChAllButton,help={"This will change the checked values (ONLY) in ALL of the files in the popup list, not just the top file. If the \"change\" checkbox for the item is not checked, nothing will be changed for that item."}
149        Button DoneButton,pos={450,60},size={110,20},proc=V_DoneButtonProc,title="Done Patching"
150        Button DoneButton,help={"When done Patching files, this will close this control panel."}
151        CheckBox check0,pos={18,80},size={40,15},title="Run #",value= 1,mode=1,proc=V_MatchCheckProc
152        CheckBox check1,pos={78,80},size={40,15},title="Text",value= 0,mode=1,proc=V_MatchCheckProc
153        CheckBox check2,pos={138,80},size={40,15},title="Group_ID",value= 0,mode=1,proc=V_MatchCheckProc
154
155        SetVariable curStr,pos={50,112},size={350,20},title="File Label:"
156        SetVariable curStr,help={"Label of current file in popup list"}
157        SetVariable curStr,font="Courier",fSize=10
158        SetVariable curStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Patch:gPatchCurLabel
159       
160        PopupMenu popup_0,pos={450,112},size={109,20},title="Detector Panel",proc=V_PatchPopMenuProc
161        PopupMenu popup_0,mode=1,popvalue="FL",value= #"\"FL;FR;FT;FB;ML;MR;MT;MB;B;\""
162
163
164        TabControl PatchTab,pos={20,140},size={570,380}
165        TabControl PatchTab,tabLabel(0)="Control",tabLabel(1)="Reduction",tabLabel(2)="Sample"
166        TabControl PatchTab,tabLabel(3)="Instrument",tabLabel(4)="Detectors",tabLabel(5)="PolSANS"
167        TabControl PatchTab,value=0,labelBack=(47748,57192,54093),proc=V_PatchTabProc
168
169
170        ListBox list0,pos={30,170.00},size={550.00,330},proc=V_PatchListBoxProc,frame=1
171        ListBox list0,fSize=10,userColumnResize= 1,listWave=PP_ListWave,selWave=PP_SelWave,titleWave=PP_TitleWave
172        ListBox list0,mode=2,widths={30,200}
173
174
175// put these in a tabbed? section for the 9 different panels
176// will it be able to patch all "FL" with the proper values, then all "FR", etc. to batchwise correct files?
177
178// TODO: add functions for these, make the intent a popup (since it's an enumerated type)
179
180//      PopupMenu popup_1,pos={42,base+14*step},size={109,20},title="File intent"
181//      PopupMenu popup_1,mode=1,popvalue="SCATTER",value= #"\"SCATTER;EMPTY;BLOCKED BEAM;TRANS;EMPTY BEAM;\""
182
183
184
185        SetDataFolder root:
186End
187
188//
189// function to control the display of the list box, based on the selection of the tab
190//
191Function V_PatchTabProc(name,tab)
192        String name
193        Variable tab
194       
195//      Print "name,number",name,tab
196        SetDataFolder root:Packages:NIST:VSANS:Globals:Patch:
197       
198        Wave/T PP_listWave = PP_ListWave
199        Wave PP_selWave = PP_selWave
200       
201        //clear the listWave and SelWave
202        PP_ListWave = ""
203        PP_SelWave = 0
204       
205        Variable nRows=1
206        // switch based on the tab number
207        switch(tab)     
208                case 0:
209                        //Print "tab 0 = CONTROL"
210                       
211                        V_FillListBox0(PP_ListWave,PP_SelWave)
212                        break           
213                case 1:
214                        //Print "tab 1 = REDUCTION"
215                       
216                        V_FillListBox1(PP_ListWave,PP_SelWave)
217                        break
218                case 2:
219                        //Print "tab 2 = SAMPLE"
220                       
221                        V_FillListBox2(PP_ListWave,PP_SelWave)
222                        break
223                case 3:
224                        //Print "tab 3 = INSTRUMENT"
225                       
226                        V_FillListBox3(PP_ListWave,PP_SelWave)
227                        break
228                case 4:
229                        //Print "tab 4 = DETECTORS"
230                       
231                        V_FillListBox4(PP_ListWave,PP_SelWave)
232                        break
233                case 5:
234                        //Print "tab 5 = POL_SANS"
235
236                        V_FillListBox5(PP_ListWave,PP_SelWave)
237                        break
238                default:                        // optional default expression executed
239                        SetDataFolder root:
240                        Abort "No tab found -- PatchTabProc"            // when no case matches
241        endswitch
242
243
244        SetDataFolder root:
245        return(0)
246End
247
248// fill list boxes based on the tab
249//
250// *** if the number of elements is changed, then be sure that the variable nRows is updated
251//    * this is the same procedure for all of the tabs
252//    * then be sure that the new listWave assignments are properly indexed
253//
254// CONTROL
255//
256Function V_FillListBox0(listWave,selWave)
257        Wave/T listWave
258        Wave selWave
259       
260        // trust that I'm getting a valid raw data file name from the popup
261        String fname
262        ControlInfo/W=V_Patch_Panel PatchPopup
263        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
264                Abort "no file selected in popup menu"          //null selection
265        else
266                fname = S_value                         //selection not null
267        Endif
268        //prepend path for read routine
269        PathInfo catPathName
270        fname = S_path + fname
271
272        Variable nRows = 3
273        Redimension/N=(nRows,3) ListWave
274        Redimension/N=(nRows,3) selWave
275        // clear the contents
276        listWave = ""
277        selWave = 0
278        SelWave[][0] = 2^5              // checkboxes
279        SelWave[][2] = 2^1              // 3rd column editable
280       
281       
282       
283        listWave[0][1] = "count_time"
284        listWave[0][2] = num2str(V_getCount_time(fname))
285       
286        listWave[1][1] = "detector_counts"
287        listWave[1][2] = num2str(V_getDetector_counts(fname))
288       
289        listWave[2][1] = "monitor_counts"
290        listWave[2][2] = num2str(V_getMonitorCount(fname))
291       
292        return(0)
293End
294
295// fill list boxes based on the tab
296//
297// REDUCTION items
298//
299Function V_FillListBox1(listWave,selWave)
300        Wave/T listWave
301        Wave selWave
302
303        // trust that I'm getting a valid raw data file name from the popup
304        String fname
305        ControlInfo/W=V_Patch_Panel PatchPopup
306        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
307                Abort "no file selected in popup menu"          //null selection
308        else
309                fname = S_value                         //selection not null
310        Endif
311        //prepend path for read routine
312        PathInfo catPathName
313        fname = S_path + fname
314
315        Variable nRows = 14
316        Redimension/N=(nRows,3) ListWave
317        Redimension/N=(nRows,3) selWave
318        // clear the contents
319        listWave = ""
320        selWave = 0
321        SelWave[][0] = 2^5              // checkboxes
322        SelWave[][2] = 2^1              // 3rd column editable
323       
324       
325        listWave[0][1] = "empty_beam_file_name"
326        listWave[0][2] = V_getEmptyBeamFileName(fname)
327       
328        listWave[1][1] = "background_file_name"
329        listWave[1][2] = V_getBackgroundFileName(fname)
330       
331        listWave[2][1] = "empty_file_name"
332        listWave[2][2] = V_getEmptyFileName(fname)
333       
334        listWave[3][1] = "sensitivity_file_name"
335        listWave[3][2] = V_getSensitivityFileName(fname)
336       
337        listWave[4][1] = "mask_file_name"
338        listWave[4][2] = V_getMaskFileName(fname)
339       
340        listWave[5][1] = "transmission_file_name"
341        listWave[5][2] = V_getTransmissionFileName(fname)
342       
343        listWave[6][1] = "intent"
344        listWave[6][2] = V_getReduction_intent(fname)
345       
346        listWave[7][1] = "file_purpose (polSANS)"
347        listWave[7][2] = V_getReduction_polSANSPurpose(fname)
348       
349        listWave[8][1] = "group_id (sample)"
350        listWave[8][2] = num2str(V_getSample_groupID(fname))
351       
352        listWave[9][1] = "Box Coordinates"
353        WAVE boxCoord = V_getBoxCoordinates(fname)
354        listWave[9][2] = V_NumWave2List(boxCoord,";")
355
356        listWave[10][1] = "box_count"
357        listWave[10][2] = num2str(V_getBoxCounts(fname))
358       
359        listWave[11][1] = "box_count_error"
360        listWave[11][2] = num2str(V_getBoxCountsError(fname))
361       
362        listWave[12][1] = "whole_trans"
363        listWave[12][2] = num2str(V_getSampleTransWholeDetector(fname))
364       
365        listWave[13][1] = "whole_trans_error"
366        listWave[13][2] = num2str(V_getSampleTransWholeDetErr(fname))   
367               
368       
369
370        return(0)
371End
372
373// fill list boxes based on the tab
374//
375// SAMPLE
376//
377Function V_FillListBox2(listWave,selWave)
378        Wave/T listWave
379        Wave selWave
380       
381        // trust that I'm getting a valid raw data file name from the popup
382        String fname
383        ControlInfo/W=V_Patch_Panel PatchPopup
384        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
385                Abort "no file selected in popup menu"          //null selection
386        else
387                fname = S_value                         //selection not null
388        Endif
389        //prepend path for read routine
390        PathInfo catPathName
391        fname = S_path + fname
392
393        Variable nRows = 4
394        Redimension/N=(nRows,3) ListWave
395        Redimension/N=(nRows,3) selWave
396        // clear the contents
397        listWave = ""
398        selWave = 0
399        SelWave[][0] = 2^5              // checkboxes
400        SelWave[][2] = 2^1              // 3rd column editable
401       
402       
403        listWave[0][1] = "description"
404        listWave[0][2] = V_getSampleDescription(fname)
405       
406        listWave[1][1] = "thickness"
407        listWave[1][2] = num2str(V_getSampleThickness(fname))
408       
409        listWave[2][1] = "transmission"
410        listWave[2][2] = num2str(V_getSampleTransmission(fname))
411       
412        listWave[3][1] = "transmission_error"
413        listWave[3][2] = num2str(V_getSampleTransError(fname))
414       
415
416
417        return(0)
418End
419
420// fill list boxes based on the tab
421//
422// INSTRUMENT
423//
424Function V_FillListBox3(listWave,selWave)
425        Wave/T listWave
426        Wave selWave
427       
428        // trust that I'm getting a valid raw data file name from the popup
429        String fname
430        ControlInfo/W=V_Patch_Panel PatchPopup
431        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
432                Abort "no file selected in popup menu"          //null selection
433        else
434                fname = S_value                         //selection not null
435        Endif
436        //prepend path for read routine
437        PathInfo catPathName
438        fname = S_path + fname
439
440        Variable nRows = 6
441        Redimension/N=(nRows,3) ListWave
442        Redimension/N=(nRows,3) selWave
443        // clear the contents
444        listWave = ""
445        selWave = 0
446        SelWave[][0] = 2^5              // checkboxes
447        SelWave[][2] = 2^1              // 3rd column editable
448       
449       
450        listWave[0][1] = "attenuator_transmission"
451        listWave[0][2] = num2str(V_getAttenuator_transmission(fname))   
452       
453        listWave[1][1] = "attenuator_transmission_error"
454        listWave[1][2] = num2str(V_getAttenuator_trans_err(fname))     
455
456        listWave[2][1] = "monochromator type"
457        listWave[2][2] = V_getMonochromatorType(fname)
458       
459        listWave[3][1] = "wavelength"
460        listWave[3][2] = num2str(V_getWavelength(fname))       
461       
462        listWave[4][1] = "wavelength_spread"
463        listWave[4][2] = num2str(V_getWavelength_spread(fname))
464
465        listWave[5][1] = "distance (source aperture)"
466        listWave[5][2] = num2str(V_getSourceAp_distance(fname))         
467               
468        return(0)
469End
470
471
472// fill list boxes based on the tab
473//
474// TODO -- is this all of the fields that I want to edit?
475//
476// DETECTORS
477//
478Function V_FillListBox4(listWave,selWave)
479        Wave/T listWave
480        Wave selWave
481       
482        // trust that I'm getting a valid raw data file name from the popup
483        String fname
484        ControlInfo/W=V_Patch_Panel PatchPopup
485        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
486                Abort "no file selected in popup menu"          //null selection
487        else
488                fname = S_value                         //selection not null
489        Endif
490        //prepend path for read routine
491        PathInfo catPathName
492        fname = S_path + fname
493
494        Variable nRows = 12
495        Redimension/N=(nRows,3) ListWave
496        Redimension/N=(nRows,3) selWave
497        // clear the contents
498        listWave = ""
499        selWave = 0
500        SelWave[][0] = 2^5              // checkboxes
501        SelWave[][2] = 2^1              // 3rd column editable
502       
503        ControlInfo popup_0                     // which detector panel?
504        String detStr = S_value
505       
506        listWave[0][1] = "beam_center_x"
507        listWave[0][2] = num2str(V_getDet_Beam_center_x(fname,detStr)) 
508
509        listWave[1][1] = "beam_center_y"
510        listWave[1][2] = num2str(V_getDet_Beam_center_y(fname,detStr)) 
511
512        listWave[2][1] = "distance (nominal)"
513        listWave[2][2] = num2str(V_getDet_NominalDistance(fname,detStr))       
514
515        listWave[3][1] = "integrated_count"
516        listWave[3][2] = num2str(V_getDet_IntegratedCount(fname,detStr))       
517
518        listWave[4][1] = "pixel_fwhm_x"
519        listWave[4][2] = num2str(V_getDet_pixel_fwhm_x(fname,detStr))   
520
521        listWave[5][1] = "pixel_fwhm_y"
522        listWave[5][2] = num2str(V_getDet_pixel_fwhm_y(fname,detStr))   
523
524        listWave[6][1] = "pixel_num_x"
525        listWave[6][2] = num2str(V_getDet_pixel_num_x(fname,detStr))   
526
527        listWave[7][1] = "pixel_num_y"
528        listWave[7][2] = num2str(V_getDet_pixel_num_y(fname,detStr))   
529
530        listWave[8][1] = "setback"
531        listWave[8][2] = num2str(V_getDet_TBSetback(fname,detStr))     
532
533        if(cmpstr(detStr,"B") == 0 ||cmpstr(detStr,"FR") == 0 || cmpstr(detStr,"FL") == 0 || cmpstr(detStr,"MR") == 0 || cmpstr(detStr,"ML") == 0)
534                listWave[9][1] = "lateral_offset"                       // "B" detector drops here
535                listWave[9][2] = num2str(V_getDet_LateralOffset(fname,detStr)) 
536        else   
537                listWave[9][1] = "vertical_offset"     
538                listWave[9][2] = num2str(V_getDet_VerticalOffset(fname,detStr))
539        endif   
540
541        listWave[10][1] = "x_pixel_size"
542        listWave[10][2] = num2str(V_getDet_x_pixel_size(fname,detStr)) 
543
544        listWave[11][1] = "y_pixel_size"
545        listWave[11][2] = num2str(V_getDet_y_pixel_size(fname,detStr)) 
546
547
548        return(0)
549End
550
551
552// fill list boxes based on the tab
553//
554// TODO -- this all needs to be filled in, once I figure out what is needed
555//
556// PolSANS
557//
558Function V_FillListBox5(listWave,selWave)
559        Wave/T listWave
560        Wave selWave
561       
562        // trust that I'm getting a valid raw data file name from the popup
563        String fname
564        ControlInfo/W=V_Patch_Panel PatchPopup
565        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
566                Abort "no file selected in popup menu"          //null selection
567        else
568                fname = S_value                         //selection not null
569        Endif
570        //prepend path for read routine
571        PathInfo catPathName
572        fname = S_path + fname
573
574        Variable nRows = 3
575        Redimension/N=(nRows,3) ListWave
576        Redimension/N=(nRows,3) selWave
577        // clear the contents
578        listWave = ""
579        selWave = 0
580        SelWave[][0] = 2^5              // checkboxes
581        SelWave[][2] = 2^1              // 3rd column editable
582       
583       
584        listWave[0][1] = "count_time"
585        listWave[0][2] = num2str(V_getCount_time(fname))       
586
587        return(0)
588End
589
590
591// TODO -- determine if I really need this --- I don't
592//  think I really have any reason to respond to events from list box actions
593//  or edits. the final action of patching is done with the button
594//
595Function V_PatchListBoxProc(lba) : ListBoxControl
596        STRUCT WMListboxAction &lba
597
598        Variable row = lba.row
599        Variable col = lba.col
600        WAVE/T/Z listWave = lba.listWave
601        WAVE/Z selWave = lba.selWave
602
603        switch( lba.eventCode )
604                case -1: // control being killed
605                        break
606                case 1: // mouse down
607                        break
608                case 3: // double click
609                        break
610                case 4: // cell selection
611                case 5: // cell selection plus shift key
612                        break
613                case 6: // begin edit
614                        break
615                case 7: // finish edit
616                        break
617                case 13: // checkbox clicked (Igor 6.2 or later)
618                        break
619        endswitch
620
621        return 0
622End
623
624
625
626
627
628
629//button action procedure to select the local path to the folder that
630//contains the vSANS data
631//sets catPathName, updates the path display and the popup of files (in that folder)
632//
633Function V_PickPathButton(PathButton) : ButtonControl
634        String PathButton
635       
636        // call the main procedure to set the data path
637        V_PickPath()
638       
639        //set the global string to the selected pathname
640        //set a local copy of the path for Patch
641        PathInfo/S catPathName
642   String dum = S_path
643        if (V_flag == 0)
644                //path does not exist - no folder selected
645                String/G root:Packages:NIST:VSANS:Globals:Patch:gCatPathStr = "no folder selected"
646        else
647                String/G root:Packages:NIST:VSANS:Globals:Patch:gCatPathStr = dum
648        endif
649       
650        //Update the pathStr variable box
651        ControlUpdate/W=V_Patch_Panel $"PathDisplay"
652       
653        //then update the popup list
654        // (don't update the list - not until someone enters a search critera) -- Jul09
655        //
656        V_SetMatchStrProc("",0,"*","")          //this is equivalent to finding everything, typical startup case
657
658        return(0)
659End
660
661//
662//returns a list of valid files (raw data, no version numbers, no averaged files)
663//that is semicolon delimited, and is suitable for display in a popup menu
664//
665Function/S xGetValidPatchPopupList()
666
667        //make sure that path exists
668        PathInfo catPathName
669        String path = S_path
670        if (V_flag == 0)
671                Abort "folder path does not exist - use Pick Path button"
672        Endif
673       
674        String newList = ""
675
676        newList = V_GetRawDataFileList()
677
678        //trim list to include only selected files
679        SVAR match = root:Packages:NIST:VSANS:Globals:Patch:gPatchMatchStr
680        if(strlen(match) == 0)          //if nothing is entered for a match string, return everything, rather than nothing
681                match = "*"
682        endif
683
684        newlist = V_MyMatchList(match,newlist,";")
685       
686        newList = SortList(newList,";",0)
687        Return(newList)
688End
689
690
691
692//
693// TODO:
694// -- test all of the filters to be sure they actually work properly.
695//   Run # filter works
696//   Text filter works
697//
698// -- SDD filter does not apply -- what is a better filter choice?
699// -- can I filter intent? group_id?
700// -- can't just search for "sample" - this returns everything
701//
702//
703//
704//
705//
706//returns a list of valid files (raw data, no version numbers, no averaged files)
707//that is semicolon delimited, and is suitable for display in a popup menu
708//
709// Uses Grep to look through the any text in the file, which includes the sample label
710// can be very slow across the network, as it re-pops the menu on a selection (since some folks don't hit
711// enter when inputing a filter string)
712//
713// - or -
714// a list or range of run numbers
715// - or -
716// a SDD (to within 0.001m)
717// - or -
718// * to get everything
719//
720//      NVAR gRadioVal= root:Packages:NIST:VSANS:Globals:Patch:gRadioVal
721 // 1== Run # (comma range OK)
722 // 2== Grep the text (SLOW)
723 // 3== filter by SDD (within 0.001 m)
724Function/S V_GetValidPatchPopupList()
725
726        //make sure that path exists
727        PathInfo catPathName
728        String path = S_path
729        if (V_flag == 0)
730                Abort "folder path does not exist - use Pick Path button"
731        Endif
732       
733        String newList = ""
734
735        newList = V_GetRawDataFileList()
736
737        //trim list to include only selected files
738        SVAR match = root:Packages:NIST:VSANS:Globals:Patch:gPatchMatchStr
739        if(strlen(match) == 0 || cmpstr(match,"*")==0)          //if nothing or "*" entered for a match string, return everything, rather than nothing
740                match = "*"
741        // old way, with simply a wildcard
742                newlist = V_MyMatchList(match,newlist,";")
743                newList = SortList(newList,";",0)
744                return(newList)
745        endif
746       
747        //loop through all of the files as needed
748
749       
750        String list="",item="",fname,runList="",numStr=""
751        Variable ii,num=ItemsInList(newList),val,group_id
752        NVAR gRadioVal= root:Packages:NIST:VSANS:Globals:Patch:gRadioVal
753       
754        // run number list
755        if(gRadioVal == 1)
756                       
757                list = V_ExpandNumRanges(match)         //now simply comma delimited
758                num=ItemsInList(list,",")
759                for(ii=0;ii<num;ii+=1)
760                        item = StringFromList(ii,list,",")
761                        val=str2num(item)
762
763                        runList += V_GetFileNameFromPathNoSemi(V_FindFileFromRunNumber(val)) + ";"             
764                endfor
765                newlist = runList
766               
767        endif
768       
769        //grep through what text I can find in the VAX binary
770        // Grep Note: the \\b sequences limit matches to a word boundary before and after
771        // "boondoggle", so "boondoggles" and "aboondoggle" won't match.
772        if(gRadioVal == 2)
773                for(ii=0;ii<num;ii+=1)
774                        item=StringFromList(ii, newList , ";")
775//                      Grep/P=catPathName/Q/E=("(?i)\\b"+match+"\\b") item
776                        Grep/P=catPathName/Q/E=("(?i)"+match) item
777                        if( V_value )   // at least one instance was found
778//                              Print "found ", item,ii
779                                list += item + ";"
780                        endif
781                endfor
782
783                newList = list
784        endif
785       
786        // group_id
787        // replace this with: V_getSample_GroupID(fname)
788        Variable pos
789        String IDStr=""
790        if(gRadioVal == 3)
791                pos = strsearch(match, "*", 0)
792                if(pos == -1)           //no wildcard
793                        val = str2num(match)
794                else
795                        val = str2num(match[0,pos-1])
796                endif
797               
798//              print val
799                for(ii=0;ii<num;ii+=1)
800                        item=StringFromList(ii, newList , ";")
801                        fname = path + item
802                        group_id = V_getSample_GroupID(fname)
803                        if(group_id == val)
804                                list += item + ";"
805                        endif
806       
807                endfor
808               
809                newList = list
810        endif
811
812        newList = SortList(newList,";",0)
813        Return(newList)
814End
815
816
817
818
819// -- no longer refreshes the list - this seems redundant, and can be slow if grepping
820//
821//updates the popup list when the menu is "popped" so the list is
822//always fresh, then automatically displays the header of the popped file
823//value of match string is used in the creation of the list - use * to get
824//all valid files
825//
826Function V_PatchPopMenuProc(PatchPopup,popNum,popStr) : PopupMenuControl
827        String PatchPopup
828        Variable popNum
829        String popStr
830
831       
832//      String/G root:Packages:NIST:VSANS:Globals:Patch:gPatchList = list
833//      ControlUpdate PatchPopup
834        V_ShowHeaderButtonProc("SHButton")
835       
836        return(0)
837End
838
839//when text is entered in the match string, the popup list is refined to
840//include only the selected files, useful for trimming a lengthy list, or selecting
841//a range of files to patch
842//only one wildcard (*) is allowed
843//
844Function V_SetMatchStrProc(ctrlName,varNum,varStr,varName) : SetVariableControl
845        String ctrlName
846        Variable varNum
847        String varStr
848        String varName
849
850        //change the contents of gPatchList that is displayed
851        //based on selected Path, match str, and
852        //further trim list to include only RAW SANS files
853        //this will exclude version numbers, .AVE, .ABS files, etc. from the popup (which can't be patched)
854       
855        String list = V_GetValidPatchPopupList()
856       
857        String/G root:Packages:NIST:VSANS:Globals:Patch:gPatchList = list
858        ControlUpdate PatchPopup
859        PopupMenu PatchPopup,mode=1
860       
861        if(strlen(list) > 0)
862                V_ShowHeaderButtonProc("SHButton")
863        endif
864        return(0)
865End
866
867
868//displays the header of the selected file (top in the popup) when the button is clicked
869//sort of a redundant button, since the procedure is automatically called (as if it were
870//clicked) when a new file is chosen from the popup
871//
872// TODO - make sure this is tab-aware
873//
874Function V_ShowHeaderButtonProc(SHButton) : ButtonControl
875        String SHButton
876
877        //displays (editable) header information about current file in popup control
878        //putting the values in the SetVariable displays (resetting the global variables)
879       
880        //get the popup string
881        String partialName, tempName
882        Variable ok
883        ControlInfo/W=V_Patch_Panel PatchPopup
884        If(strlen(S_value)==0 || cmpstr(S_Value,"none")==0)
885                //null selection
886                Abort "no file selected in popup menu"
887        else
888                //selection not null
889                partialName = S_value
890                //Print partialName
891        Endif
892        //get a valid file based on this partialName and catPathName
893        tempName = V_FindValidFilename(partialName)
894       
895        //prepend path to tempName for read routine
896        PathInfo catPathName
897        tempName = S_path + tempName
898       
899        //make sure the file is really a RAW data file
900        ok = V_CheckIfRawData(tempName)                 //--- This loads the whole file to read the instrument string
901        if (!ok)
902                Abort "this file is not recognized as a RAW SANS data file"
903        Endif
904       
905        //Print tempName
906       
907        V_ReadHeaderForPatch(tempName)
908       
909        ControlUpdate/A/W=V_Patch_Panel
910       
911        // no matter what tab is selected, show the file label
912        SVAR fileLabel = root:Packages:NIST:VSANS:Globals:Patch:gPatchCurLabel
913        fileLabel = V_getSampleDescription(tempName)
914       
915        return(0)
916End
917
918
919
920//simple function to get the string value from the popup list of filenames
921//returned string is only the text in the popup, a partial name with no path
922//or VAX version number.
923//
924Function/S V_GetPatchPopupString()
925
926        String str=""
927       
928        ControlInfo patchPopup
929        If(cmpstr(S_value,"")==0)
930                //null selection
931                Abort "no file selected in popup menu"
932        else
933                //selection not null
934                str = S_value
935                //Print str
936        Endif
937       
938        Return str
939End
940
941//Changes (writes to disk!) the specified changes to the (single) file selected in the popup
942//reads the checkboxes to determine which (if any) values need to be written
943//
944// This currently makes sure the name is valid,
945// determines the active tab,
946// and dispatches to the correct (numbered) writer
947//
948Function V_ChangeHeaderButtonProc(CHButton) : ButtonControl
949        String CHButton
950
951        String partialName="", tempName = ""
952        Variable ok
953        //get the popup string
954        partialName = V_GetPatchPopupString()
955       
956        //get a valid file based on this partialName and catPathName
957        tempName = V_FindValidFilename(partialName)
958       
959        //prepend path to tempName for read routine
960        PathInfo catPathName
961        tempName = S_path + tempName
962       
963        //make sure the file is really a RAW data file
964        ok = V_CheckIfRawData(tempName)
965        if (!ok)
966                Abort "this file is not recognized as a RAW SANS data file"
967        Endif
968       
969        // which tab is active?
970        ControlInfo/W=V_Patch_Panel PatchTab
971       
972        switch(V_Value) // numeric switch
973                case 0: // execute if case matches expression
974                        V_WriteHeaderForPatch_0(tempName)               //control
975                        break           // exit from switch
976                case 1:
977                        V_WriteHeaderForPatch_1(tempName)               //reduction
978                        break
979                case 2:
980                        V_WriteHeaderForPatch_2(tempName)               // sample
981                        break
982                case 3:
983                        V_WriteHeaderForPatch_3(tempName)               // instrument
984                        break
985                case 4:
986                        V_WriteHeaderForPatch_4(tempName)               //detectors
987                        break
988                case 5:
989                        V_WriteHeaderForPatch_5(tempName)               // polSANS
990                        break
991                default:                        // optional default expression executed
992                        Abort "Tab not found - V_ChangeHeaderButtonProc"
993        endswitch
994
995       
996        //after writing the changes to the file
997        // clean up, to force a reload from disk
998        V_CleanupData_w_Progress(0,1)
999       
1000        return(0)
1001End
1002
1003//     
1004//*****this function actually writes the data to disk*****
1005//
1006// TODO - re-write a series of these function to mirror the "fill" functions
1007//   specific to each tab
1008//
1009// TODO x- clear out the old data and force a re-load from disk, or the old data
1010//    will be read in from the RawVSANS folder, and it will look like nothing was written
1011//                      (done in the calling function)
1012//
1013// currently, all errors are printed out by the writer, but ignored here
1014//
1015Function V_WriteHeaderForPatch_0(fname)
1016        String fname
1017       
1018        Variable val,err
1019        String textstr
1020               
1021        Wave/T listWave = root:Packages:NIST:VSANS:Globals:Patch:PP_ListWave
1022        Wave selWave = root:Packages:NIST:VSANS:Globals:Patch:PP_selWave
1023
1024        // test bit 4 to see if the checkbox is selected
1025        if ((selWave[0][0] & 2^4) != 0)         // Test if bit 4 is set
1026                val = str2num(listWave[0][2])
1027                err = V_writeCount_time(fname,val)              // count_time
1028        endif
1029
1030        if ((selWave[1][0] & 2^4) != 0)         // "detector_counts"
1031                val = str2num(listWave[1][2])
1032                err = V_writeDetector_counts(fname,val)
1033        endif   
1034       
1035        if ((selWave[2][0] & 2^4) != 0)         //"monitor_counts"
1036                val = str2num(listWave[2][2])
1037                err = V_writeMonitorCount(fname,val)
1038        endif   
1039       
1040
1041
1042
1043        Return(0)
1044End
1045
1046//
1047// tab 1
1048//
1049Function V_WriteHeaderForPatch_1(fname)
1050        String fname
1051
1052        Variable val,err
1053        String str
1054               
1055        Wave/T listWave = root:Packages:NIST:VSANS:Globals:Patch:PP_ListWave
1056        Wave selWave = root:Packages:NIST:VSANS:Globals:Patch:PP_selWave
1057
1058        // test bit 4 to see if the checkbox is selected
1059        if ((selWave[0][0] & 2^4) != 0)         // Test if bit 4 is set
1060                str = listWave[0][2]                    // empty_beam_file_name
1061                err = V_writeEmptyBeamFileName(fname,str)               
1062        endif
1063
1064        if ((selWave[1][0] & 2^4) != 0)         // "background_file_name"
1065                str = listWave[1][2]
1066                err = V_writeBackgroundFileName(fname,str)
1067        endif   
1068       
1069        if ((selWave[2][0] & 2^4) != 0)         //"empty_file_name"
1070                str = listWave[2][2]
1071                err = V_writeEmptyFileName(fname,str)
1072        endif   
1073       
1074        if ((selWave[3][0] & 2^4) != 0)         //"sensitivity_file_name"
1075                str = listWave[3][2]
1076                err = V_writeSensitivityFileName(fname,str)
1077        endif   
1078       
1079        if ((selWave[4][0] & 2^4) != 0)         //"mask_file_name"
1080                str = listWave[4][2]
1081                err = V_writeMaskFileName(fname,str)
1082        endif   
1083       
1084        if ((selWave[5][0] & 2^4) != 0)         //"transmission_file_name"
1085                str = listWave[5][2]
1086                err = V_writeTransmissionFileName(fname,str)
1087        endif   
1088
1089        if ((selWave[6][0] & 2^4) != 0)         //"intent"
1090                str = listWave[6][2]
1091                err = V_writeReductionIntent(fname,str)
1092        endif   
1093       
1094        if ((selWave[7][0] & 2^4) != 0)         //"file_purpose (polSANS)"
1095                str = listWave[7][2]
1096                err = V_writePolReduction_purpose(fname,str)
1097        endif           
1098
1099        if ((selWave[8][0] & 2^4) != 0)         //"group_id (sample)"
1100                val = str2num(listWave[8][2])
1101                err = V_writeSample_GroupID(fname,val)
1102        endif   
1103
1104
1105
1106        if ((selWave[9][0] & 2^4) != 0)         //"box coordinates"
1107                str = listWave[9][2]
1108                err = V_writeBoxCoordinates(fname,V_List2NumWave(str,";","inW"))
1109        endif   
1110       
1111
1112       
1113        if ((selWave[10][0] & 2^4) != 0)                //"box_count"
1114                val = str2num(listWave[10][2])
1115                err = V_writeBoxCounts(fname,val)
1116        endif   
1117       
1118        if ((selWave[11][0] & 2^4) != 0)                //"box_count_error"
1119                val = str2num(listWave[11][2])
1120                err = V_writeBoxCountsError(fname,val)
1121        endif   
1122       
1123        if ((selWave[12][0] & 2^4) != 0)                //"whole_trans"
1124                val = str2num(listWave[12][2])
1125                err = V_writeSampleTransWholeDetector(fname,val)
1126        endif   
1127       
1128        if ((selWave[13][0] & 2^4) != 0)                //"whole_trans_error"
1129                val = str2num(listWave[13][2])
1130                err = V_writeSampleTransWholeDetErr(fname,val)
1131        endif   
1132       
1133       
1134               
1135               
1136        return(0)
1137End
1138
1139// SAMPLE
1140Function V_WriteHeaderForPatch_2(fname)
1141        String fname
1142       
1143        Variable val,err
1144        String str
1145               
1146        Wave/T listWave = root:Packages:NIST:VSANS:Globals:Patch:PP_ListWave
1147        Wave selWave = root:Packages:NIST:VSANS:Globals:Patch:PP_selWave
1148
1149        // test bit 4 to see if the checkbox is selected
1150        if ((selWave[0][0] & 2^4) != 0)         // Test if bit 4 is set
1151                str = listWave[0][2]                    // "description"
1152                err = V_writeSampleDescription(fname,str)               
1153        endif
1154
1155        if ((selWave[1][0] & 2^4) != 0)         // "thickness"
1156                val = str2num(listWave[1][2])
1157                err = V_writeSampleThickness(fname,val)
1158        endif   
1159       
1160        if ((selWave[2][0] & 2^4) != 0)         //"transmission"
1161                val = str2num(listWave[2][2])
1162                err = V_writeSampleTransmission(fname,val)
1163        endif   
1164       
1165        if ((selWave[3][0] & 2^4) != 0)         //"transmission_error"
1166                val = str2num(listWave[3][2])
1167                err = V_writeSampleTransError(fname,val)
1168        endif   
1169       
1170        return(0)
1171End
1172
1173// INSTRUMENT
1174Function V_WriteHeaderForPatch_3(fname)
1175        String fname
1176
1177        Variable val,err
1178        String str
1179               
1180        Wave/T listWave = root:Packages:NIST:VSANS:Globals:Patch:PP_ListWave
1181        Wave selWave = root:Packages:NIST:VSANS:Globals:Patch:PP_selWave
1182
1183        // test bit 4 to see if the checkbox is selected
1184        if ((selWave[0][0] & 2^4) != 0)         // Test if bit 4 is set
1185                val = str2num(listWave[0][2])                   // "attenuator_transmission"
1186                err = V_writeAttenuator_transmission(fname,val)         
1187        endif
1188
1189        if ((selWave[1][0] & 2^4) != 0)         // "attenuator_transmission_error"
1190                val = str2num(listWave[1][2])
1191                err = V_writeAttenuator_trans_err(fname,val)
1192        endif   
1193       
1194        if ((selWave[2][0] & 2^4) != 0)         //"monochromator type"
1195                str = listWave[2][2]
1196                err = V_writeMonochromatorType(fname,str)
1197        endif   
1198       
1199        if ((selWave[3][0] & 2^4) != 0)         //"wavelength"
1200                val = str2num(listWave[3][2])
1201                err = V_writeWavelength(fname,val)
1202        endif   
1203
1204        if ((selWave[4][0] & 2^4) != 0)         //"wavelength_spread"
1205                val = str2num(listWave[4][2])
1206                err = V_writeWavelength_spread(fname,val)
1207        endif   
1208       
1209        if ((selWave[5][0] & 2^4) != 0)         //"distance (source aperture)"
1210                val = str2num(listWave[5][2])
1211                err = V_writeSourceAp_distance(fname,val)
1212        endif           
1213       
1214       
1215        return(0)
1216End
1217
1218// DETECTOR
1219Function V_WriteHeaderForPatch_4(fname)
1220        String fname
1221
1222        Variable val,err
1223        String str
1224               
1225        Wave/T listWave = root:Packages:NIST:VSANS:Globals:Patch:PP_ListWave
1226        Wave selWave = root:Packages:NIST:VSANS:Globals:Patch:PP_selWave
1227
1228        ControlInfo popup_0
1229        String detStr = S_Value
1230
1231        // test bit 4 to see if the checkbox is selected
1232        if ((selWave[0][0] & 2^4) != 0)         // Test if bit 4 is set
1233                val = str2num(listWave[0][2])                   // "beam_center_x"
1234                err = V_writeDet_beam_center_x(fname,detStr,val)       
1235        endif
1236
1237        if ((selWave[1][0] & 2^4) != 0)         // "beam_center_y"
1238                val = str2num(listWave[1][2])
1239                err = V_writeDet_beam_center_y(fname,detStr,val)
1240        endif   
1241       
1242        if ((selWave[2][0] & 2^4) != 0)         //"distance (nominal)"
1243                val = str2num(listWave[2][2])
1244                err = V_writeDet_distance(fname,detStr,val)
1245        endif   
1246       
1247        if ((selWave[3][0] & 2^4) != 0)         //"integrated_count"
1248                val = str2num(listWave[3][2])
1249                err = V_writeDet_IntegratedCount(fname,detStr,val)
1250        endif   
1251       
1252        if ((selWave[4][0] & 2^4) != 0)         //"pixel_fwhm_x"
1253                val = str2num(listWave[4][2])
1254                err = V_writeDet_pixel_fwhm_x(fname,detStr,val)
1255        endif   
1256       
1257        if ((selWave[5][0] & 2^4) != 0)         //"pixel_fwhm_y"
1258                val = str2num(listWave[5][2])
1259                err = V_writeDet_pixel_fwhm_y(fname,detStr,val)
1260        endif   
1261
1262        if ((selWave[6][0] & 2^4) != 0)         //"pixel_num_x"
1263                val = str2num(listWave[6][2])
1264                err = V_writeDet_pixel_num_x(fname,detStr,val)
1265        endif   
1266       
1267        if ((selWave[7][0] & 2^4) != 0)         //"pixel_num_y"
1268                val = str2num(listWave[7][2])
1269                err = V_writeDet_pixel_num_y(fname,detStr,val)
1270        endif           
1271       
1272        if ((selWave[8][0] & 2^4) != 0)         //"setback" -- only for TB detectors
1273                val = str2num(listWave[8][2])
1274                if(cmpstr(detStr,"FT") == 0 || cmpstr(detStr,"FB") == 0 || cmpstr(detStr,"MT") == 0 || cmpstr(detStr,"MB") == 0)
1275                        err = V_writeDet_TBSetback(fname,detStr,val)
1276                endif
1277        endif   
1278
1279        if ((selWave[9][0] & 2^4) != 0)         //"lateral_offset" or "vertical_offset"
1280                val = str2num(listWave[9][2])
1281                if(cmpstr(detStr,"B") == 0 ||cmpstr(detStr,"FR") == 0 || cmpstr(detStr,"FL") == 0 || cmpstr(detStr,"MR") == 0 || cmpstr(detStr,"ML") == 0)
1282                        err = V_writeDet_LateralOffset(fname,detStr,val)
1283                else
1284                        err = V_writeDet_VerticalOffset(fname,detStr,val)
1285                endif
1286        endif   
1287       
1288        if ((selWave[10][0] & 2^4) != 0)                //"x_pixel_size"
1289                val = str2num(listWave[10][2])
1290                err = V_writeDet_x_pixel_size(fname,detStr,val)
1291        endif   
1292       
1293        if ((selWave[11][0] & 2^4) != 0)                //"y_pixel_size"
1294                val = str2num(listWave[11][2])
1295                err = V_writeDet_y_pixel_size(fname,detStr,val)
1296        endif   
1297       
1298
1299
1300       
1301        return(0)
1302End
1303
1304// TODO -- not yet implemented
1305Function V_WriteHeaderForPatch_5(fname)
1306        String fname
1307       
1308        return(0)
1309End
1310
1311
1312// control the display of the radio buttons
1313Function V_MatchCheckProc(name,value)
1314        String name
1315        Variable value
1316       
1317        NVAR gRadioVal= root:Packages:NIST:VSANS:Globals:Patch:gRadioVal
1318       
1319        strswitch (name)
1320                case "check0":
1321                        gRadioVal= 1
1322                        break
1323                case "check1":
1324                        gRadioVal= 2
1325                        break
1326                case "check2":
1327                        gRadioVal= 3
1328                        break
1329        endswitch
1330        CheckBox check0,value= gRadioVal==1
1331        CheckBox check1,value= gRadioVal==2
1332        CheckBox check2,value= gRadioVal==3
1333        return(0)
1334End
1335
1336//This function will read only the selected values editable in the patch panel
1337//
1338// DONE
1339// x- re-write this to be tab-aware. ShowHeaderForPatch() calls this, but does nothing
1340//    to update the tab content. Figure out which function is in charge, and update the content.
1341//
1342Function V_ReadHeaderForPatch(fname)
1343        String fname
1344       
1345       
1346        // figure out which is the active tab, then let PatchTabProc fill it in
1347        ControlInfo/W=V_Patch_Panel PatchTab
1348        V_PatchTabProc("",V_Value)     
1349       
1350        Return 0
1351End
1352
1353Function V_ShowPatchHelp(ctrlName) : ButtonControl
1354        String ctrlName
1355//      DisplayHelpTopic/Z/K=1 "VSANS Data Reduction Tutorial[Patch File Headers]"
1356//      if(V_flag !=0)
1357                DoAlert 0,"The VSANS Data Reduction Tutorial Help file could not be found"
1358//      endif
1359        return(0)
1360End
1361
1362//button action procedure to change the selected information (checked values)
1363//in each file in the popup list. This will change multiple files, and as such,
1364//the user is given a chance to bail out before the whole list of files
1365//is modified
1366//useful for patching a series of runs with the same beamcenters, or transmissions
1367//
1368Function V_ChAllHeadersButtonProc(ctrlName) : ButtonControl
1369        String ctrlName
1370       
1371        String msg
1372        msg = "Do you really want to write all of these values to each data file in the popup list? "
1373        msg += "- clicking NO will leave all files unchanged"
1374        DoAlert 1,msg
1375        If(V_flag == 2)
1376                Abort "no files were changed"
1377        Endif
1378       
1379        //this will change (checked) values in ALL of the headers in the popup list
1380        SVAR list = root:Packages:NIST:VSANS:Globals:Patch:gPatchList
1381        Variable numitems,ii
1382        String partialName="", tempName = ""
1383        Variable ok
1384       
1385        numitems = ItemsInList(list,";")
1386       
1387        if(numitems == 0)
1388                Abort "no items in list for multiple patch"
1389        Endif
1390       
1391        // loop through all of the files
1392        ii=0
1393        do
1394                //get current item in the list
1395                partialName = StringFromList(ii, list, ";")
1396                   
1397                //get a valid file based on this partialName and catPathName
1398                tempName = V_FindValidFilename(partialName)
1399       
1400                //prepend path to tempName for read routine
1401                PathInfo catPathName
1402                tempName = S_path + tempName
1403       
1404                //make sure the file is really a RAW data file
1405                ok = V_CheckIfRawData(tempName)
1406                if (!ok)
1407                   Print "this file is not recognized as a RAW SANS data file = ",tempName
1408                else
1409                   //go write the changes to the file
1410                        // which tab is active?
1411                        ControlInfo/W=V_Patch_Panel PatchTab
1412                       
1413                        switch(V_Value) // numeric switch
1414                                case 0: // execute if case matches expression
1415                                        V_WriteHeaderForPatch_0(tempName)
1416                                        break           // exit from switch
1417                                case 1:
1418                                        V_WriteHeaderForPatch_1(tempName)
1419                                        break
1420                                case 2:
1421                                        V_WriteHeaderForPatch_2(tempName)
1422                                        break
1423                                case 3:
1424                                        V_WriteHeaderForPatch_3(tempName)
1425                                        break
1426                                case 4:
1427                                        V_WriteHeaderForPatch_4(tempName)
1428                                        break
1429                                case 5:
1430                                        V_WriteHeaderForPatch_5(tempName)
1431                                        break
1432                                default:                        // optional default expression executed
1433                                        Abort "Tab not found - V_ChAllHeadersButtonProc"
1434                        endswitch
1435                Endif
1436               
1437                ii+=1
1438        while(ii<numitems)
1439
1440
1441        //after writing the changes to the file
1442        // clean up, to force a reload from disk
1443        V_CleanupData_w_Progress(0,1)
1444
1445        return(0)
1446End
1447
1448
1449//simple action for button to close the panel
1450//
1451// cleans out the RawVSANS folder on closing
1452//
1453Function V_DoneButtonProc(ctrlName) : ButtonControl
1454        String ctrlName
1455
1456        DoWindow/K V_Patch_Panel
1457
1458//      V_CleanOutRawVSANS()
1459// present a progress window
1460        V_CleanupData_w_Progress(0,1)   
1461       
1462        return(0)
1463End
1464
1465
1466
1467////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1468////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1469///////////////
1470//
1471// this is a block to patch DEADTIME waves to the file headers, and can patch multiple files
1472//
1473// uses a simple panel to show what the table of values is.
1474// "read" will read only the first run number contents.
1475//
1476// TODO -- need to clear out the contents from RawVSANS, or else re-reading to check the values
1477//        will read locally, and it will look like nothing was written. Executing "save" will also
1478//        trigger a cleanout.
1479//
1480// TODO -- link this to a panel somewhere - a button? menu item? will there be a lot more of these little panels?
1481//
1482// TODO -- currently, this does not patch the deadtime for the back "B" detector. it is a single
1483//        value, not a wave see V_WritePerfectDeadTime(filename) for how the perfect (fake) values is written.
1484//
1485//
1486Proc V_PatchDetectorDeadtime(firstFile,lastFile,detStr,deadtimeStr)
1487        Variable firstFile=1,lastFile=100
1488        String detStr = "FL",deadtimeStr="deadTimeWave"
1489
1490        V_fPatchDetectorDeadtime(firstFile,lastFile,detStr,$deadtimeStr)
1491
1492End
1493
1494Proc V_ReadDetectorDeadtime(firstFile,lastFile,detStr)
1495        Variable firstFile=1,lastFile=100
1496        String detStr = "FL"
1497       
1498        V_fReadDetectorDeadtime(firstFile,lastFile,detStr)
1499       
1500End
1501
1502// simple utility to patch the detector deadtime in the file headers
1503// lo is the first file number
1504// hi is the last file number (inclusive)
1505//
1506Function V_fPatchDetectorDeadtime(lo,hi,detStr,deadtimeW)
1507        Variable lo,hi
1508        String detStr
1509        Wave deadtimeW
1510       
1511        Variable ii
1512        String fname
1513       
1514        // check the dimensions of the deadtimeW/N=48
1515        if (DimSize(deadtimeW, 0) != 48 )
1516                Abort "dead time wave is not of proper dimension (48)"
1517        endif
1518       
1519        //loop over all files
1520        for(ii=lo;ii<=hi;ii+=1)
1521                fname = V_FindFileFromRunNumber(ii)
1522                if(strlen(fname) != 0)
1523                        V_writeDetector_deadtime(fname,detStr,deadtimeW)                       
1524                else
1525                        printf "run number %d not found\r",ii
1526                endif
1527        endfor
1528       
1529        return(0)
1530End
1531
1532// simple utility to read the detector deadtime stored in the file header
1533Function V_fReadDetectorDeadtime(lo,hi,detStr)
1534        Variable lo,hi
1535        String detStr
1536       
1537        String fname
1538        Variable ii
1539       
1540        for(ii=lo;ii<=hi;ii+=1)
1541                fname = V_FindFileFromRunNumber(ii)
1542                if(strlen(fname) != 0)
1543                        Wave deadtimeW = V_getDetector_deadtime(fname,detStr)
1544                        Duplicate/O deadTimeW root:Packages:NIST:VSANS:Globals:Patch:deadtimeWave
1545//                      printf "File %d:  Detector Dead time (s) = %g\r",ii,deadtime
1546                else
1547                        printf "run number %d not found\r",ii
1548                endif
1549        endfor
1550       
1551        return(0)
1552End
1553
1554
1555
1556Proc V_PatchDetectorDeadtimePanel()
1557        DoWindow/F DeadtimePanel
1558        if(V_flag==0)
1559       
1560                NewDataFolder/O/S root:Packages:NIST:VSANS:Globals:Patch
1561
1562                Make/O/D/N=48 deadTimeWave
1563               
1564                SetDataFolder root:
1565               
1566                Execute "V_DeadtimePatchPanel()"
1567        endif
1568End
1569
1570
1571// TODO:
1572// x- add method for generating "perfect" dead time to write
1573// x- check deadtime wave dimension before writing (check for bad paste operation)
1574// -- load from file? different ways to import?
1575// -- Dead time constants for "B" are different, and not handled here (yet)
1576// -- add help button/file
1577// -- add done button
1578// -- adjust after user testing
1579//
1580Proc V_DeadtimePatchPanel() : Panel
1581        PauseUpdate; Silent 1           // building window...
1582
1583
1584        NewPanel /W=(600,400,1000,1000)/N=DeadtimePanel /K=1
1585//      ShowTools/A
1586        ModifyPanel cbRGB=(16266,47753,2552,23355)
1587
1588        SetDrawLayer UserBack
1589        DrawText 85,99,"Current Values"
1590        DrawText 21,258,"Write to all files (inlcusive)"
1591        SetDrawEnv fsize= 14,fstyle= 1
1592        DrawText 209,30,"Dead Time Constants"
1593        DrawText 20,133,"Run Number(s)"
1594       
1595        PopupMenu popup_0,pos={20,40},size={109,20},title="Detector Panel"
1596        PopupMenu popup_0,mode=1,popvalue="FL",value= #"\"FL;FR;FT;FB;ML;MR;MT;MB;\""
1597       
1598        Button button0,pos={20,81},size={50.00,20.00},proc=V_ReadDTButtonProc,title="Read"
1599        Button button0_1,pos={20,220},size={50.00,20.00},proc=V_WriteDTButtonProc,title="Write"
1600        Button button0_2,pos={18.00,336.00},size={140.00,20.00},proc=V_GeneratePerfDTButton,title="Perfect Dead Time"
1601        Button button0_3,pos={18.00,370.00},size={140.00,20.00},proc=V_LoadCSVDTButton,title="Load Dead Time CSV"
1602        Button button0_4,pos={18.00,400.00},size={140.00,20.00},proc=V_WriteCSVDTButton,title="Write Dead Time CSV"
1603       
1604        SetVariable setvar0,pos={20,141},size={100.00,14.00},title="first"
1605        SetVariable setvar0,value= K0
1606        SetVariable setvar1,pos={20.00,167},size={100.00,14.00},title="last"
1607        SetVariable setvar1,value= K1
1608
1609
1610// display the wave     
1611        Edit/W=(180,40,380,550)/HOST=#  root:Packages:NIST:VSANS:Globals:Patch:deadTimeWave
1612        ModifyTable width(Point)=40
1613        ModifyTable width(root:Packages:NIST:VSANS:Globals:Patch:deadTimeWave)=120
1614        RenameWindow #,T0
1615        SetActiveSubwindow ##
1616
1617       
1618EndMacro
1619
1620
1621Function V_LoadCSVDTButton(ba) : ButtonControl
1622        STRUCT WMButtonAction &ba
1623
1624        switch( ba.eventCode )
1625                case 2: // mouse up
1626                        // click code here
1627
1628                        LoadWave/J/A/D/O/W/E=1/K=0                              //will prompt for the file, auto name
1629                       
1630                        break
1631                case -1: // control being killed
1632                        break
1633        endswitch
1634
1635        return 0
1636End
1637
1638//TODO
1639// -- currently this skips detector "B", since its dead time is not like the tubes
1640// -- fails miserably if the deadtime_** waves don't exist
1641// -- the writing may take a long time. Warn the user.
1642// -- if the data files are not "cleaned up", re-reading will pick up the rawVSANS copy and it
1643//    will look like nothing was written
1644//
1645// writes the entire content of the CSV file (all 8 panels) to each detector entry in each data file
1646// as specified by the run number range
1647//
1648Function V_WriteCSVDTButton(ba) : ButtonControl
1649        STRUCT WMButtonAction &ba
1650
1651        Variable ii
1652        String detStr
1653       
1654        switch( ba.eventCode )
1655                case 2: // mouse up
1656                        // click code here
1657                       
1658//                      ControlInfo popup_0
1659//                      String detStr = S_Value
1660                        ControlInfo setvar0
1661                        Variable lo=V_Value
1662                        ControlInfo setvar1
1663                        Variable hi=V_Value
1664                        Wave deadTimeW = root:Packages:NIST:VSANS:Globals:Patch:deadTimeWave
1665                       
1666                        for(ii=0;ii<ItemsInList(ksDetectorListNoB);ii+=1)
1667                                detStr = StringFromList(ii, ksDetectorListNoB, ";")
1668                                Wave tmpW = $("root:deadtime_"+detStr)
1669                                deadTimeW = tmpW
1670                                V_fPatchDetectorDeadtime(lo,hi,detStr,deadtimeW)
1671                        endfor
1672                       
1673                        break
1674                case -1: // control being killed
1675                        break
1676        endswitch
1677
1678        // TODO
1679        // -- clear out the data folders (from lo to hi?)
1680//
1681// root:Packages:NIST:VSANS:RawVSANS:sans1301:
1682        for(ii=lo;ii<=hi;ii+=1)
1683                KillDataFolder/Z $("root:Packages:NIST:VSANS:RawVSANS:sans"+num2str(ii))
1684        endfor
1685        return 0
1686End
1687
1688
1689Function V_GeneratePerfDTButton(ba) : ButtonControl
1690        STRUCT WMButtonAction &ba
1691
1692        switch( ba.eventCode )
1693                case 2: // mouse up
1694                        // click code here
1695
1696                        WAVE deadTimeWave = root:Packages:NIST:VSANS:Globals:Patch:deadTimeWave
1697                        ControlInfo popup_0
1698                        strswitch(S_Value)
1699                                case "FR":
1700                                case "FL":
1701                                case "MR":
1702                                case "ML":
1703                                case "FT":
1704                                case "FB":
1705                                case "MT":
1706                                case "MB":
1707                                        deadTimeWave = 1e-18
1708
1709                                        break
1710                                default:
1711                                        Print "Det type not found: V_GeneratePerfDTButton()"
1712                        endswitch
1713                       
1714                        break
1715                case -1: // control being killed
1716                        break
1717        endswitch
1718
1719        return 0
1720End
1721
1722
1723
1724
1725Function V_ReadDTButtonProc(ba) : ButtonControl
1726        STRUCT WMButtonAction &ba
1727
1728        switch( ba.eventCode )
1729                case 2: // mouse up
1730                        // click code here
1731                       
1732                        ControlInfo popup_0
1733                        String detStr = S_Value
1734                        ControlInfo setvar0
1735                        Variable lo=V_Value
1736                        Variable hi=lo
1737                       
1738                        V_fReadDetectorDeadtime(lo,hi,detStr)
1739                       
1740                        break
1741                case -1: // control being killed
1742                        break
1743        endswitch
1744
1745        return 0
1746End
1747
1748Function V_WriteDTButtonProc(ba) : ButtonControl
1749        STRUCT WMButtonAction &ba
1750
1751        switch( ba.eventCode )
1752                case 2: // mouse up
1753                        // click code here
1754                       
1755                        ControlInfo popup_0
1756                        String detStr = S_Value
1757                        ControlInfo setvar0
1758                        Variable lo=V_Value
1759                        ControlInfo setvar1
1760                        Variable hi=V_Value
1761                        Wave deadTimeW = root:Packages:NIST:VSANS:Globals:Patch:deadTimeWave
1762                       
1763                        V_fPatchDetectorDeadtime(lo,hi,detStr,deadtimeW)
1764                       
1765                        break
1766                case -1: // control being killed
1767                        break
1768        endswitch
1769
1770        return 0
1771End
1772
1773//////////////////////////////////////////////////////////////////////////////////////////////////
1774//////////////////////////////////////////////////////////////////////////////////////////////////
1775// this is a block to patch CALIBRATION waves to the file headers, and can patch multiple files
1776//
1777// uses a simple panel to show what the table of values is.
1778// "read" will read only the first run number contents.
1779//
1780// TODO -- need to clear out the contents from RawVSANS, or else re-reading to check the values
1781//        will read locally, and it will look like nothing was written. Executing "save" will also
1782//        trigger a cleanout.
1783//
1784// TODO -- link this to a panel somewhere - a button? menu item? will there be a lot more of these little panels?
1785//
1786// TODO -- currently this does not handle the back detector "B". see V_WritePerfectSpatialCalib(filename)
1787//         for how fake data is written to the files
1788//
1789// TODO -- verify that the calibration waves are not transposed
1790//
1791Proc V_PatchDetectorCalibration(firstFile,lastFile,detStr,calibStr)
1792        Variable firstFile=1,lastFile=100
1793        String detStr = "FL",calibStr="calibrationWave"
1794
1795        V_fPatchDetectorCalibration(firstFile,lastFile,detStr,$calibStr)
1796
1797End
1798
1799Proc V_ReadDetectorCalibration(firstFile,lastFile,detStr)
1800        Variable firstFile=1,lastFile=100
1801        String detStr = "FL"
1802       
1803        V_fReadDetectorCalibration(firstFile,lastFile,detStr)
1804End
1805
1806// simple utility to patch the detector calibration wave in the file headers
1807// lo is the first file number
1808// hi is the last file number (inclusive)
1809//
1810Function V_fPatchDetectorCalibration(lo,hi,detStr,calibW)
1811        Variable lo,hi
1812        String detStr
1813        Wave calibW
1814       
1815        Variable ii
1816        String fname
1817       
1818        // check the dimensions of the calibW (3,48)
1819        if (DimSize(calibW, 0) != 3 || DimSize(calibW, 1) != 48 )
1820                Abort "Calibration wave is not of proper dimension (3,48)"
1821        endif
1822       
1823        //loop over all files
1824        for(ii=lo;ii<=hi;ii+=1)
1825                fname = V_FindFileFromRunNumber(ii)
1826                if(strlen(fname) != 0)
1827                        V_writeDetTube_spatialCalib(fname,detStr,calibW)                       
1828                else
1829                        printf "run number %d not found\r",ii
1830                endif
1831        endfor
1832       
1833        return(0)
1834End
1835
1836// simple utility to read the detector deadtime stored in the file header
1837Function V_fReadDetectorCalibration(lo,hi,detStr)
1838        Variable lo,hi
1839        String detStr
1840       
1841        String fname
1842        Variable ii
1843       
1844        for(ii=lo;ii<=hi;ii+=1)
1845                fname = V_FindFileFromRunNumber(ii)
1846                if(strlen(fname) != 0)
1847                        Wave calibW = V_getDetTube_spatialCalib(fname,detStr)
1848                        Duplicate/O calibW root:Packages:NIST:VSANS:Globals:Patch:calibrationWave
1849                else
1850                        printf "run number %d not found\r",ii
1851                endif
1852        endfor
1853       
1854        return(0)
1855End
1856
1857
1858Proc V_PatchDetectorCalibrationPanel()
1859        DoWindow/F CalibrationPanel
1860        if(V_flag==0)
1861       
1862                NewDataFolder/O/S root:Packages:NIST:VSANS:Globals:Patch
1863
1864                Make/O/D/N=(3,48) calibrationWave
1865               
1866                SetDataFolder root:
1867               
1868                Execute "V_CalibrationPatchPanel()"
1869        endif
1870End
1871
1872
1873//
1874// TODO:
1875// x- add method for generating "perfect" calibration to write
1876// x- check Nx3 dimension before writing (check for bad paste operation)
1877// -- load from file? different ways to import?
1878// -- calibration constants for "B" are different, and not handled here (yet)
1879// -- add help button/file
1880// -- add done button
1881// -- adjust after user testing
1882//
1883Proc V_CalibrationPatchPanel() : Panel
1884        PauseUpdate; Silent 1           // building window...
1885
1886
1887        NewPanel /W=(600,400,1200,1000)/N=CalibrationPanel /K=1
1888//      ShowTools/A
1889        ModifyPanel cbRGB=(16266,47753,2552,23355)
1890
1891        SetDrawLayer UserBack
1892        DrawText 85,99,"Current Values"
1893        DrawText 21,258,"Write to all files (inlcusive)"
1894        SetDrawEnv fsize= 14,fstyle= 1
1895        DrawText 227,28,"Quadratic Calibration Constants per Tube"
1896        DrawText 20,133,"Run Number(s)"
1897               
1898        PopupMenu popup_0,pos={20,40},size={109,20},title="Detector Panel"
1899        PopupMenu popup_0,mode=1,popvalue="FL",value= #"\"FL;FR;FT;FB;ML;MR;MT;MB;\""
1900       
1901        Button button0,pos={20,81},size={50.00,20.00},proc=V_ReadCalibButtonProc,title="Read"
1902        Button button0_1,pos={20,220},size={50.00,20.00},proc=V_WriteCalibButtonProc,title="Write"
1903        Button button0_2,pos={18.00,336.00},size={140.00,20.00},proc=V_GeneratePerfCalibButton,title="Perfect Calibration"
1904        Button button0_3,pos={18.00,370.00},size={140.00,20.00},proc=V_LoadCSVCalibButton,title="Load Calibration CSV"
1905        Button button0_4,pos={18.00,400.00},size={140.00,20.00},proc=V_WriteCSVCalibButton,title="Write Calibration CSV"
1906               
1907        SetVariable setvar0,pos={20,141},size={100.00,14.00},title="first"
1908        SetVariable setvar0,value= K0
1909        SetVariable setvar1,pos={20.00,167},size={100.00,14.00},title="last"
1910        SetVariable setvar1,value= K1
1911
1912
1913// display the wave     
1914        Edit/W=(180,40,580,550)/HOST=#  root:Packages:NIST:VSANS:Globals:Patch:calibrationWave
1915        ModifyTable width(Point)=40
1916        ModifyTable width(root:Packages:NIST:VSANS:Globals:Patch:calibrationWave)=110
1917        // the elements() command transposes the view in the table, but does not transpose the wave
1918        ModifyTable elements(root:Packages:NIST:VSANS:Globals:Patch:calibrationWave) = (-3, -2)
1919        RenameWindow #,T0
1920        SetActiveSubwindow ##
1921
1922       
1923EndMacro
1924
1925
1926Function V_LoadCSVCalibButton(ba) : ButtonControl
1927        STRUCT WMButtonAction &ba
1928
1929        switch( ba.eventCode )
1930                case 2: // mouse up
1931                        // click code here
1932
1933                        LoadWave/J/A/D/O/W/E=1/K=0                              //will prompt for the file, auto name
1934                       
1935                        break
1936                case -1: // control being killed
1937                        break
1938        endswitch
1939
1940        return 0
1941End
1942
1943//TODO
1944// -- currently this skips detector "B", since its calibration is not like the tubes
1945// -- fails miserably if the a,b,c_** waves don't exist
1946// -- the writing may take a long time. Warn the user.
1947// -- if the data files are not "cleaned up", re-reading will pick up the rawVSANS copy and it
1948//    will look like nothing was written
1949//
1950// writes the entire content of the CSV file (all 8 panels) to each detector entry in each data file
1951// as specified by the run number range
1952//
1953Function V_WriteCSVCalibButton(ba) : ButtonControl
1954        STRUCT WMButtonAction &ba
1955
1956        Variable ii
1957        String detStr
1958       
1959        switch( ba.eventCode )
1960                case 2: // mouse up
1961                        // click code here
1962                       
1963//                      ControlInfo popup_0
1964//                      String detStr = S_Value
1965                        ControlInfo setvar0
1966                        Variable lo=V_Value
1967                        ControlInfo setvar1
1968                        Variable hi=V_Value
1969                        WAVE calibrationWave = root:Packages:NIST:VSANS:Globals:Patch:calibrationWave
1970                       
1971                        for(ii=0;ii<ItemsInList(ksDetectorListNoB);ii+=1)
1972                                detStr = StringFromList(ii, ksDetectorListNoB, ";")
1973                                Wave tmp_a = $("root:a_"+detStr)
1974                                Wave tmp_b = $("root:b_"+detStr)
1975                                Wave tmp_c = $("root:c_"+detStr)
1976                                calibrationWave[0][] = tmp_a[q]
1977                                calibrationWave[1][] = tmp_b[q]
1978                                calibrationWave[2][] = tmp_c[q]
1979                                V_fPatchDetectorCalibration(lo,hi,detStr,calibrationWave)
1980                        endfor
1981                       
1982                        break
1983                case -1: // control being killed
1984                        break
1985        endswitch
1986
1987        // TODO
1988        // -- clear out the data folders (from lo to hi?)
1989//
1990// root:Packages:NIST:VSANS:RawVSANS:sans1301:
1991        for(ii=lo;ii<=hi;ii+=1)
1992                KillDataFolder/Z $("root:Packages:NIST:VSANS:RawVSANS:sans"+num2str(ii))
1993        endfor
1994        return 0
1995End
1996
1997
1998
1999//      // and for the back detector "B"
2000//      Make/O/D/N=3 tmpCalib
2001//      tmpCalib[0] = 1
2002//      tmpCalib[1] = 1
2003//      tmpcalib[2] = 10000
2004//      V_writeDet_cal_x(filename,"B",tmpCalib)
2005//      V_writeDet_cal_y(filename,"B",tmpCalib)
2006//
2007Function V_GeneratePerfCalibButton(ba) : ButtonControl
2008        STRUCT WMButtonAction &ba
2009
2010        switch( ba.eventCode )
2011                case 2: // mouse up
2012                        // click code here
2013
2014                        WAVE calibrationWave = root:Packages:NIST:VSANS:Globals:Patch:calibrationWave
2015                        ControlInfo popup_0
2016                        strswitch(S_Value)
2017                                case "FR":
2018                                case "FL":
2019                                case "MR":
2020                                case "ML":
2021                                        //      // for the "tall" L/R banks
2022                                        calibrationWave[0][] = -512
2023                                        calibrationWave[1][] = 8
2024                                        calibrationWave[2][] = 0
2025                                        break
2026                                case "FT":
2027                                case "FB":
2028                                case "MT":
2029                                case "MB":
2030                                        //      // for the "short" T/B banks
2031                                        calibrationWave[0][] = -256
2032                                        calibrationWave[1][] = 4
2033                                        calibrationWave[2][] = 0
2034
2035                                        break
2036                                default:
2037                                        Print "Det type not found: V_GeneratePerfCalibButton()"
2038                        endswitch
2039
2040
2041                       
2042                        break
2043                case -1: // control being killed
2044                        break
2045        endswitch
2046
2047        return 0
2048End
2049
2050
2051Function V_ReadCalibButtonProc(ba) : ButtonControl
2052        STRUCT WMButtonAction &ba
2053
2054        switch( ba.eventCode )
2055                case 2: // mouse up
2056                        // click code here
2057                       
2058                        ControlInfo popup_0
2059                        String detStr = S_Value
2060                        ControlInfo setvar0
2061                        Variable lo=V_Value
2062                        Variable hi=lo
2063                       
2064                        V_fReadDetectorCalibration(lo,hi,detStr)
2065                       
2066                        break
2067                case -1: // control being killed
2068                        break
2069        endswitch
2070
2071        return 0
2072End
2073
2074Function V_WriteCalibButtonProc(ba) : ButtonControl
2075        STRUCT WMButtonAction &ba
2076
2077        switch( ba.eventCode )
2078                case 2: // mouse up
2079                        // click code here
2080                       
2081                        ControlInfo popup_0
2082                        String detStr = S_Value
2083                        ControlInfo setvar0
2084                        Variable lo=V_Value
2085                        ControlInfo setvar1
2086                        Variable hi=V_Value
2087                        Wave calibW = root:Packages:NIST:VSANS:Globals:Patch:calibrationWave
2088                       
2089                        V_fPatchDetectorCalibration(lo,hi,detStr,calibW)
2090                       
2091                        break
2092                case -1: // control being killed
2093                        break
2094        endswitch
2095
2096        return 0
2097End
2098
2099
2100
2101//////////////////////////////////////////////////////////////////////////////////////////////////
2102//////////////////////////////////////////////////////////////////////////////////////////////////
2103
2104
2105
2106////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2107////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2108///////////////
2109//
2110// this is a block to patch beam centers to the file headers
2111// it will patch the headers for all 9 detectors
2112// and can patch multiple files
2113//
2114// uses a simple panel to show what the table of values is.
2115// "read" will read only the first run number contents. this is the "good" set of XY
2116//  that you want to write out to other files.
2117//
2118// TODO -- need to clear out the contents from RawVSANS, or else re-reading to check the values
2119//        will read locally, and it will look like nothing was written. Executing "save" will also
2120//        trigger a cleanout.
2121//
2122// TODO - link this to a panel somewhere - a button? menu item? will there be a lot more of these little panels?
2123//
2124Proc V_PatchDet_xyCenters(firstFile,lastFile)
2125        Variable firstFile=1,lastFile=100
2126
2127        V_fPatchDet_xyCenters(firstFile,lastFile)
2128
2129End
2130
2131Proc V_ReadDet_xyCenters(firstFile,lastFile)
2132        Variable firstFile=1,lastFile=100
2133
2134       
2135        V_fReadDet_xyCenters(firstFile,lastFile)
2136End
2137
2138// simple utility to patch the xy center in the file headers
2139// lo is the first file number
2140// hi is the last file number (inclusive)
2141//
2142Function V_fPatchDet_xyCenters(lo,hi)
2143        Variable lo,hi
2144
2145       
2146        Variable ii,jj
2147        String fname,detStr
2148       
2149        Wave xCtr_pix = root:Packages:NIST:VSANS:Globals:Patch:xCtr_pix
2150        Wave yCtr_pix = root:Packages:NIST:VSANS:Globals:Patch:yCtr_pix
2151        Wave/T panelW = root:Packages:NIST:VSANS:Globals:Patch:panelW
2152       
2153        // check the dimensions of the waves (9)
2154        if (DimSize(xCtr_pix, 0) != 9 || DimSize(yCtr_pix, 0) != 9 || DimSize(panelW, 0) != 9)
2155                Abort "waves are not of proper dimension (9)"
2156        endif
2157       
2158        //loop over all files
2159        for(jj=lo;jj<=hi;jj+=1)
2160                fname = V_FindFileFromRunNumber(jj)
2161                if(strlen(fname) != 0)
2162               
2163                        for(ii=0;ii<ItemsInList(ksDetectorListAll);ii+=1)
2164                                detStr = panelW[ii]
2165                                V_writeDet_beam_center_x(fname,detStr,xCtr_pix[ii])
2166                                V_writeDet_beam_center_y(fname,detStr,yCtr_pix[ii])             
2167                        endfor 
2168               
2169                else
2170                        printf "run number %d not found\r",ii
2171                endif
2172        endfor
2173       
2174        return(0)
2175End
2176
2177// simple utility to read the detector xy centers stored in the file header
2178Function V_fReadDet_xyCenters(lo,hi)
2179        Variable lo,hi
2180
2181       
2182        String fname,detStr
2183        Variable ii,jj
2184       
2185        Wave xCtr_pix = root:Packages:NIST:VSANS:Globals:Patch:xCtr_pix
2186        Wave yCtr_pix = root:Packages:NIST:VSANS:Globals:Patch:yCtr_pix
2187        Wave/T panelW = root:Packages:NIST:VSANS:Globals:Patch:panelW
2188       
2189        for(jj=lo;jj<=hi;jj+=1)
2190                fname = V_FindFileFromRunNumber(jj)
2191                if(strlen(fname) != 0)
2192               
2193                        for(ii=0;ii<ItemsInList(ksDetectorListAll);ii+=1)
2194                                detStr = StringFromList(ii, ksDetectorListAll, ";")
2195                                panelW[ii] = detStr
2196                                xCtr_pix[ii] = V_getDet_beam_center_x(fname,detStr)
2197                                yCtr_pix[ii] = V_getDet_beam_center_y(fname,detStr)
2198                        endfor
2199               
2200               
2201                else
2202                        printf "run number %d not found\r",jj
2203                endif
2204               
2205        endfor
2206
2207       
2208        return(0)
2209End
2210
2211
2212
2213Proc V_PatchDet_xyCenters_Panel()
2214        DoWindow/F Patch_XY_Panel
2215        if(V_flag==0)
2216       
2217                NewDataFolder/O/S root:Packages:NIST:VSANS:Globals:Patch
2218
2219                Make/O/D/N=9 xCtr_pix,yCtr_pix
2220                Make/O/T/N=9 panelW
2221               
2222                SetDataFolder root:
2223               
2224                Execute "V_Patch_xyCtr_Panel()"
2225        endif
2226End
2227
2228
2229// TODO:
2230// -- add method to read (import) from beam center panel
2231// x- check wave dimensions before writing (check for bad paste operation)
2232// -- load from file? different ways to import?
2233// -- add help button/file
2234// -- add done button
2235// -- adjust after user testing
2236//
2237Proc V_Patch_xyCtr_Panel() : Panel
2238        PauseUpdate; Silent 1           // building window...
2239
2240
2241        NewPanel /W=(600,400,1150,800)/N=Patch_XY_Panel /K=1
2242//      ShowTools/A
2243       
2244        ModifyPanel cbRGB=(16266,47753,2552,23355)
2245
2246        SetDrawLayer UserBack
2247        DrawText 85,99,"Current Values"
2248        DrawText 21,258,"Write to all files (inlcusive)"
2249        SetDrawEnv fsize= 14,fstyle= 1
2250        DrawText 262,30,"Beam Center (pixels)"
2251        DrawText 20,133,"Run Number(s)"
2252       
2253        Button button0,pos={20,81},size={50.00,20.00},proc=V_ReadXYButtonProc,title="Read"
2254        Button button0_1,pos={20,220},size={50.00,20.00},proc=V_WriteXYButtonProc,title="Write"
2255        SetVariable setvar0,pos={20,141},size={100.00,14.00},title="first"
2256        SetVariable setvar0,value= K0
2257        SetVariable setvar1,pos={20.00,167},size={100.00,14.00},title="last"
2258        SetVariable setvar1,value= K1
2259
2260       
2261        SetDataFolder root:Packages:NIST:VSANS:Globals:Patch
2262// display the wave     
2263        Edit/W=(180,40,500,370)/HOST=#  panelW,xCtr_pix,yCtr_pix
2264        ModifyTable width(Point)=0
2265        ModifyTable width(panelW)=80
2266        ModifyTable width(xCtr_pix)=100
2267        ModifyTable width(yCtr_pix)=100
2268        RenameWindow #,T0
2269        SetActiveSubwindow ##
2270
2271        SetDataFolder root:
2272       
2273EndMacro
2274
2275
2276Function V_ReadXYButtonProc(ba) : ButtonControl
2277        STRUCT WMButtonAction &ba
2278
2279        switch( ba.eventCode )
2280                case 2: // mouse up
2281                        // click code here
2282                       
2283//                      ControlInfo popup_0
2284//                      String detStr = S_Value
2285                        ControlInfo setvar0
2286                        Variable lo=V_Value
2287                        Variable hi=lo
2288                       
2289                        V_fReadDet_xyCenters(lo,hi)
2290                       
2291                        break
2292                case -1: // control being killed
2293                        break
2294        endswitch
2295
2296        return 0
2297End
2298
2299Function V_WriteXYButtonProc(ba) : ButtonControl
2300        STRUCT WMButtonAction &ba
2301
2302        switch( ba.eventCode )
2303                case 2: // mouse up
2304                        // click code here
2305                       
2306//                      ControlInfo popup_0
2307//                      String detStr = S_Value
2308                        ControlInfo setvar0
2309                        Variable lo=V_Value
2310                        ControlInfo setvar1
2311                        Variable hi=V_Value
2312//                      Wave deadTimeW = root:Packages:NIST:VSANS:Globals:Patch:deadTimeWave
2313                       
2314                        V_fPatchDet_xyCenters(lo,hi)
2315                       
2316                        break
2317                case -1: // control being killed
2318                        break
2319        endswitch
2320
2321        return 0
2322End
2323
2324//////////////////////////////////////////////////////////////////////////////////////////////////
2325//////////////////////////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.