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

Last change on this file since 1150 was 1150, checked in by srkline, 3 years ago

added button for avg mask on main display panel

added more aperture fields to the patch panel

added time comparisons for potential use in aperture dimension change done in NICE in May shutdown

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