source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_MaskUtils.ipf @ 1000

Last change on this file since 1000 was 1000, checked in by srkline, 7 years ago

read/write for lateral + vertical offset of detectors, eliminating some extra unused fields.

more additions to the mask editor to give some basic functionality (add/del tubes, save mask)

File size: 15.3 KB
Line 
1#pragma TextEncoding = "MacRoman"
2#pragma rtGlobals=3             // Use modern global access method and strict wave access.
3
4
5// TODO:
6// x- CHANGE the mask behavior to a more logical choice - and consistent with SANS
7//   x- CHANGE to:
8// 1 == mask == discard data
9// 0 == no Mask == keep data
10// x- and then make the corresponding changes in the I(Q) routines
11
12
13// also, move the mask generating utilities from VC_HDF5_Utils into this procedure - to keep
14// all of the mask procedures together
15
16
17//
18// -- this is to be a very simple editor to generate masks row/column wise, not drawing
19//
20
21
22//TODO
23// -- draw a mask
24// -- save a mask (all panels)
25// -- be able to save the mask name to the file
26// -- be able to read a mask based on what is in the data file
27//
28// x- biggest thing now is to re-write the DrawDetPanel() routine from the beamCenter.ipf
29//    to do what this panel needs
30//
31// -- lots to clean up
32// -- add this to the list of includes, move the file to SVN, and add it.
33//
34// -- for working with VCALC -- maybe have an automatic generator (if val < -2e6, mask = 0)
35//    this can be checked column-wise to go faster (1st index)
36//
37// x- re-write V_OverlayMask to make the "overlay" wave that has the NaNs, and then the drawing
38//    routines need to be aware of this
39
40Macro Edit_a_Mask()
41        V_EditMask()
42end
43
44Function V_EditMask()
45        DoWindow/F MaskEditPanel
46        if(V_flag==0)
47                Execute "MaskEditorPanel()"
48        endif
49End
50
51//
52// TODO - may need to adjust the display for the different pixel dimensions
53//      ModifyGraph width={Plan,1,bottom,left}
54//
55Proc MaskEditorPanel() : Panel
56        PauseUpdate; Silent 1           // building window...
57
58        NewPanel /W=(662,418,1300,960)/N=MaskEditPanel   /K=1
59//      ShowTools/A
60       
61        PopupMenu popup_0,pos={20,50},size={109,20},proc=SetMaskPanelPopMenuProc,title="Detector Panel"
62        PopupMenu popup_0,mode=1,popvalue="FT",value= #"\"FL;FR;FT;FB;MR;ML;MT;MB;B;\""
63        PopupMenu popup_2,pos={20,20},size={109,20},title="Data Source"//,proc=SetFldrPopMenuProc
64        PopupMenu popup_2,mode=1,popvalue="RAW",value= #"\"RAW;SAM;VCALC;\""
65
66        SetVariable setvar0,pos={247.00,20.00},size={150.00,14.00},title="tube number"
67        SetVariable setvar0,limits={0,128,1},value= _NUM:0
68        Button button_0,pos={249.00,46.00},size={50.00,20.00},proc=AddToMaskButtonProc,title="Add"
69        Button button_1,pos={309.00,46.00},size={50.00,20.00},proc=RemoveFromMaskButtonProc,title="Del"
70        Button button_2,pos={389.00,46.00},size={90.00,20.00},proc=ToggleMaskButtonProc,title="Toggle"
71        Button button_3,pos={489.00,46.00},size={80.00,20.00},proc=SaveMaskButtonProc,title="Save"
72
73// TODO -- need buttons for save? quit?
74// setVariable to add a row to the mask, column to the mask, toggle the mask on/off to see what's
75//     happening with the data
76
77
78// TODO - get rid of the hard-wired panel choice
79//      duplicate/O root:Packages:NIST:VSANS:VCALC:entry:instrument:detector_FL:det_FL curDispPanel
80        Make/O/D/N=(48,128) curDispPanel                //will this work?
81        SetScale/P x 0,1, curDispPanel
82        SetScale/P y 0,1, curDispPanel
83
84        // draw the correct images
85        //draw the detector panel
86        DrawPanelToMask("FT")
87       
88        // overlay the current mask
89        V_OverlayMask("FT",1)
90//      OverlayMaskPanel("FL")
91
92EndMacro
93
94
95Function ToggleMaskButtonProc(ba) : ButtonControl
96        STRUCT WMButtonAction &ba
97
98        switch( ba.eventCode )
99                case 2: // mouse up
100                        // click code here
101                       
102                        ControlInfo popup_0
103                        String str=S_Value
104
105                        wave/Z overlay = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":overlay")
106                       
107                        CheckDisplayed/W=MaskEditPanel#DetData overlay
108                        Variable state = !(V_flag)              //if V_flag == 0, then set to 1 (and vice versa)
109                        V_OverlayMask(str,state)
110
111                        break
112                case -1: // control being killed
113                        break
114        endswitch
115
116        return 0
117End
118
119
120Function AddToMaskButtonProc(ba) : ButtonControl
121        STRUCT WMButtonAction &ba
122
123        switch( ba.eventCode )
124                case 2: // mouse up
125                        // click code here
126                        ControlInfo popup_0
127                        String str=S_Value
128                       
129                        wave/Z maskData = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":data")
130                       
131                        Variable val
132                        ControlInfo setvar0
133                        val = V_Value
134                        maskData[val][] = 1
135                       
136                        V_OverlayMask(str,1)
137                       
138//                      Print "add tube to mask"
139                        break
140                case -1: // control being killed
141                        break
142        endswitch
143
144        return 0
145End
146
147Function RemoveFromMaskButtonProc(ba) : ButtonControl
148        STRUCT WMButtonAction &ba
149
150        switch( ba.eventCode )
151                case 2: // mouse up
152                        // click code here
153                        ControlInfo popup_0
154                        String str=S_Value
155                       
156                        wave/Z maskData = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":data")
157                       
158                        Variable val
159                        ControlInfo setvar0
160                        val = V_Value
161                        maskData[val][] = 0
162                       
163                        V_OverlayMask(str,1)                   
164//                      Print "remove tube from mask"
165                        break
166                case -1: // control being killed
167                        break
168        endswitch
169
170        return 0
171End
172
173//
174// function to choose which detector panel to display, and then to actually display it
175//
176Function SetMaskPanelPopMenuProc(pa) : PopupMenuControl
177        STRUCT WMPopupAction &pa
178
179        switch( pa.eventCode )
180                case 2: // mouse up
181                        Variable popNum = pa.popNum
182                        String popStr = pa.popStr
183                                               
184                        // remove the old image (it may not be the right shape)
185                        // -- but make sure it exists first...
186                        String childList = ChildWindowList("MaskEditPanel")
187                        Variable flag
188                       
189                        flag = WhichListItem("DetData", ChildList)              //returns -1 if not in list, 0+ otherwise
190                        if(flag != -1)
191                                KillWindow MaskEditPanel#DetData
192                        endif
193                       
194                        flag = WhichListItem("ModelData", ChildList)
195                        if(flag != -1)
196                                KillWindow MaskEditPanel#ModelData
197                        endif
198       
199                        // draw the correct images
200                        DrawPanelToMask(popStr)
201
202                        //overlay the mask
203                        V_OverlayMask(popStr,1)
204//                      OverlayMaskPanel(popStr)
205                       
206                        break
207                case -1: // control being killed
208                        break
209        endswitch
210
211        return 0
212End
213
214//
215// SEE DrawDetPanel() in the BeamCenter file
216//
217// TODO - currently is hard-wired for the simulation path!
218//     need to make it more generic, especially for RAW data
219//
220// -- need to adjust the size of the image subwindows to keep the model
221//    calculation from spillon over onto the table (maybe just move the table)
222// -- need to do something for panel "B". currently ignored
223// -- currently the pixel sizes for "real" data is incorrect in the file
224//     and this is why the plots are incorrectly sized
225//
226// draw the selected panel and the model calculation, adjusting for the
227// orientation of the panel and the number of pixels, and pixel sizes
228Function DrawPanelToMask(str)
229        String str
230       
231        // from the selection, find the path to the data
232
233        Variable xDim,yDim
234        Variable left,top,right,bottom
235        Variable height, width
236        Variable left2,top2,right2,bottom2
237        Variable nPix_X,nPix_Y,pixSize_X,pixSize_Y
238
239       
240        Wave dispW=root:curDispPanel
241
242        //plot it in the subwindow with the proper aspect and positioning
243        // for 48x256 (8mm x 4mm), aspect = (256/2)/48 = 2.67 (LR panels)
244        // for 128x48 (4mm x 8 mm), aspect = 48/(128/2) = 0.75 (TB panels)
245       
246       
247        // using two switches -- one to set the panel-specific dimensions
248        // and the other to set the "common" values, some of which are based on the panel dimensions
249
250// set the source of the data. not always VCALC anymore
251        String folder
252        ControlInfo popup_2
253        folder = S_Value
254
255        // TODO -- fix all of this mess
256        if(cmpstr(folder,"VCALC") == 0)
257                // panel-specific values
258                Variable VC_nPix_X = VCALC_get_nPix_X(str)
259                Variable VC_nPix_Y = VCALC_get_nPix_Y(str)
260                Variable VC_pixSize_X = VCALC_getPixSizeX(str)
261                Variable VC_pixSize_Y = VCALC_getPixSizeY(str)
262
263       
264        // if VCALC declare this way   
265                wave newW = $("root:Packages:NIST:VSANS:VCALC:entry:instrument:detector_"+str+":det_"+str)
266                nPix_X = VC_nPix_X
267                nPix_Y = VC_nPix_Y
268                pixSize_X = VC_pixSize_X
269                pixSize_Y = VC_pixSize_Y
270       
271        else
272        // TODO: if real data, need new declaration w/ data as the wave name
273                wave newW = $("root:Packages:NIST:VSANS:"+folder+":entry:instrument:detector_"+str+":data")
274
275                nPix_X = V_getDet_pixel_num_x(folder,str)
276                nPix_Y = V_getDet_pixel_num_Y(folder,str)
277                pixSize_X = V_getDet_x_pixel_size(folder,str)/10
278                pixSize_Y = V_getDet_y_pixel_size(folder,str)/10
279        endif
280       
281
282        Variable scale = 10
283       
284        // common values (panel position, etc)
285        // TODO -- units are absolute, based on pixels in cm. make sure this is always correct
286        strswitch(str)
287                case "FL":
288                case "FR":
289                case "ML":
290                case "MR":
291                        width = trunc(nPix_X*pixSize_X *scale*1.15)                     //48 tubes @ 8 mm
292                        height = trunc(nPix_Y*pixSize_Y *scale*0.8)                     //128 pixels @ 8 mm
293                       
294                        left = 20
295                        top = 80
296                        right = left+width
297                        bottom = top+height
298                       
299                        left2 = right + 20
300                        right2 = left2 + width
301                        top2 = top
302                        bottom2 = bottom
303                       
304                        break                   
305                case "FT":
306                case "FB":
307                case "MT":
308                case "MB":
309                        width = trunc(nPix_X*pixSize_X *scale*1.)                       //128 pix @ 4 mm
310                        height = trunc(nPix_Y*pixSize_Y *scale)                 // 48 tubes @ 8 mm
311                                               
312                        left = 20
313                        top = 80
314                        right = left+width
315                        bottom = top+height
316                       
317                        left2 = left
318                        right2 = right
319                        top2 = top + height + 20
320                        bottom2 = bottom + height + 20
321                       
322                        break
323                case "B":
324                        return(0)               //just exit
325                        break                                           
326                default:
327                        return(0)               //just exit
328        endswitch
329
330        // generate the new panel display
331        duplicate/O newW curDispPanel
332        SetScale/P x 0,1, curDispPanel
333        SetScale/P y 0,1, curDispPanel
334       
335        //draw the detector panel
336        Display/W=(left,top,right,bottom)/HOST=#
337        AppendImage curDispPanel
338        ModifyImage curDispPanel ctab= {*,*,ColdWarm,0}
339        Label left "Y pixels"
340        Label bottom "X pixels"
341        RenameWindow #,DetData
342        SetActiveSubwindow ##   
343       
344        DoUpdate
345       
346        return(0)
347End
348
349//
350// overlay the mask
351//
352// TODO
353// -- make it work
354// -- remove the old mask first
355// -- make the mask "toggle" to remove it
356// -- go see SANS for color, implementation, etc.
357// -- un-comment the (two) calls
358//
359//
360//toggles a mask on/off of the SANS_Data window
361// points directly to window, doesn't need current display type
362//
363// if state==1, show the mask, if ==0, hide the mask
364Function V_OverlayMask(str,state)
365        String str
366        Variable state
367
368
369        String maskPath = "root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":data"
370        if(WaveExists($maskPath) == 1)
371                if(state == 1)
372                        //duplicate the mask, which is named "data"
373                        wave maskW = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":data")
374       
375                        Duplicate/O maskW $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":overlay")
376                        wave overlay = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":overlay")
377       
378                        Redimension/D overlay
379                        SetScale/P x 0,1, overlay
380                        SetScale/P y 0,1, overlay
381               
382                        String tempStr = "root:Packages:NIST:MSK:overlay"
383        //              overlay = (maskW == 1) ? 1 : NaN                        //no need to do this - simply adjust the coloring
384       
385                        CheckDisplayed/W=MaskEditPanel#DetData overlay
386                        if(V_flag==0)           //so the overlay doesn't get appended more than once
387                                AppendImage/W=MaskEditPanel#DetData overlay
388                                ModifyImage/W=MaskEditPanel#DetData overlay ctab= {0.9,1,BlueRedGreen,0}        ,minRGB=NaN,maxRGB=0
389                //              ModifyImage/W=MaskEditPanel#DetData overlay ctab= {0,*,BlueRedGreen,0} 
390                        endif
391                endif
392
393                if(state == 0)
394                        wave overlay = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":overlay")
395                        CheckDisplayed/W=MaskEditPanel#DetData overlay
396//                      Print "V_flag = ",V_flag
397       
398                        If(V_Flag == 1)         //overlay is present
399                                RemoveImage/W=MaskEditPanel#DetData overlay
400                        endif
401                endif
402        Endif
403       
404        return(0)
405End
406
407
408Function SaveMaskButtonProc(ba) : ButtonControl
409        STRUCT WMButtonAction &ba
410
411        switch( ba.eventCode )
412                case 2: // mouse up
413                        // click code here
414                       
415                        Execute "H_Setup_VSANS_MASK_Structure()"
416                       
417                        // fill with current "stuff"
418                                SetDataFolder root:VSANS_MASK_file:entry       
419                        Wave/T title    = title
420                        title = "This is a custom MASK file for VSANS"
421                        SetDataFolder root:
422                       
423                       
424                // copy over for all of the detector panels
425
426                        Variable ii
427                        String str
428                        for(ii=0;ii<ItemsInList(ksDetectorListAll);ii+=1)
429                                str = StringFromList(ii, ksDetectorListAll, ";")
430                                Wave det_str = $("root:VSANS_MASK_file:entry:instrument:detector_"+str+":data")
431                                wave maskW = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+str+":data")                   
432                                det_str = maskW
433                        endfor
434
435                        //save it
436//                      String fileName = "ThisIsAMASK"
437
438                        Execute "Save_VSANS_MASK_Nexus()"
439                       
440                        break
441                case -1: // control being killed
442                        break
443        endswitch
444
445        SetDataFolder root:
446        return 0
447End
448
449
450////////////// fake MASK file tests
451//
452//
453//      Make/O/T/N=1    file_name       = "VSANS_MASK_test.h5"
454//
455// simple generation of a fake MASK file. for sans, nothing other than the creation date was written to the
456// file header. nothing more is needed (possibly)
457//
458//
459// TODO -- make the number of pixels GLOBAL
460// TODO -- there will be lots of work to do to develop the procedures necessary to actually generate the
461//      9 data sets to become the MASK file contents. More complexity here than for the simple SANS case.
462//
463// TODO -- this is currently random 0|1 values, need to write an editor
464//
465// currently set up to use 1 = YES MASK == exclude the data
466//      and 0 = NO MASK == keep the data
467//
468Proc H_Setup_VSANS_MASK_Structure()
469       
470        NewDataFolder/O/S root:VSANS_MASK_file         
471
472        NewDataFolder/O/S root:VSANS_MASK_file:entry   
473                Make/O/T/N=1    title   = "This is a fake MASK file for VSANS"
474                Make/O/T/N=1    start_date      = "2015-02-28T08:15:30-5:00"
475                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument         
476                        Make/O/T/N=1    name    = "NG3_VSANS"
477                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_B     
478                        Make/O/I/N=(150,150)    data    = 0
479                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_MR             
480                        Make/O/I/N=(48,128)     data    = 0
481                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_ML             
482                        Make/O/I/N=(48,128)     data    = 0
483                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_MT             
484                        Make/O/I/N=(128,48)     data    = 0
485                        data[0,49][] = 1
486                        data[78,127][] = 1
487                        data[50,77][] = 0
488                       
489                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_MB             
490                        Make/O/I/N=(128,48)     data    = 0
491                        data[0,49][] = 1
492                        data[78,127][] = 1
493                        data[50,77][] = 0
494                       
495                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_FR             
496                        Make/O/I/N=(48,128)     data    = 0
497                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_FL             
498                        Make/O/I/N=(48,128)     data    = 0
499                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_FT             
500                        Make/O/I/N=(128,48)     data    = 0
501                        data[0,49][] = 1
502                        data[78,127][] = 1
503                        data[50,77][] = 0
504
505                NewDataFolder/O/S root:VSANS_MASK_file:entry:instrument:detector_FB             
506                        Make/O/I/N=(128,48)     data    = 0
507                        data[0,49][] = 1
508                        data[78,127][] = 1
509                        data[50,77][] = 0
510                       
511        SetDataFolder root:
512
513End
514
515////////////////////// MASK FILE
516
517
518// TODO
519// currently, there are no dummy fill values or attributes for the fake MASK file
520//
521Proc Setup_VSANS_MASK_Struct()
522
523        // lays out the tree and fills with dummy values
524        H_Setup_VSANS_MASK_Structure()
525       
526        // writes in the attributes
527//      H_Fill_VSANS_Attributes()
528       
529        // fill in with VCALC simulation bits
530//      H_Fill_VSANS_wSim()
531       
532End
533
534Proc Save_VSANS_MASK_Nexus(fileName)
535        String fileName="Test_VSANS_MASK_file"
536
537        // save as HDF5 (no attributes saved yet)
538        Save_VSANS_file("root:VSANS_MASK_file", fileName+".h5")
539       
540        // read in a data file using the gateway-- reads from the home path
541        H_HDF5Gate_Read_Raw(fileName+".h5")
542       
543        // after reading in a "partial" file using the gateway (to generate the xref)
544        // Save the xref to disk (for later use)
545        Save_HDF5___xref("root:"+fileName,"HDF5___xref")
546       
547        // after you've generated the HDF5___xref, load it in and copy it
548        // to the necessary folder location.
549        Copy_HDF5___xref("root:VSANS_MASK_file", "HDF5___xref")
550       
551        // writes out the contents of a data folder using the gateway
552        H_HDF5Gate_Write_Raw("root:VSANS_MASK_file", fileName+".h5")
553
554        // re-load the data file using the gateway-- reads from the home path
555        // now with attributes
556        H_HDF5Gate_Read_Raw(fileName+".h5")
557       
558End
Note: See TracBrowser for help on using the repository browser.