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

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

adding patch procedure to adjust the number of attenuators - which may have been incorrect for one of Yun's experiments.

Adding updated beam center offsets for the Front carriage. These values are fitted to the AgBeh? arcs, and should be an improvement. Similar values were derived for the graphite monchromator. Separate procedures are necessary for the graphite vs. Velocity selector since the beam axis is shifted when the graphite is in place. These new graphite values are also included.

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