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

Last change on this file since 1095 was 1095, checked in by srkline, 4 years ago

Added two model functions for white beam smearing.

Many other small changes for processing of the back detector, shuffling of VSANS menu items, and consistent naming of V_ procedures.

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