source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/ProDiv.ipf @ 571

Last change on this file since 571 was 571, checked in by srkline, 13 years ago

Changed to ProDiv?.ipf to pull out the NCNR-specific writing of the DIV file. Also removed 128x128 specific and made it xy generic.

Changed NCNR, FACILITY, HFIR, and ILL DataReadWrite?.ipf to reflect these changes.

File size: 14.4 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// Vers. 1.2 092101
8//
9// Procedures to create a "DIV" file for use as a detector sensitivity file
10// Follows the same procedure as PRODIV on the VAX
11// -requires two "full" reduced runs from plexiglass or water
12// -prompts the user for the locations of the offset and no-offset files
13// and for the range of data to replace
14// - then writes of the "div" file and fake-VAX format, which is rather ugly
15// since the DIV file is floating point...
16//
17//
18// 08 AUG 03
19// allowed for creation of DIV files on 8m SANS with two beamstops
20//
21// JAN2006 - not modified! still hard-wired to take a 128x128 detector image
22//
23// Oct 2009 - SRK - pulled out the writing of the data file to NCNR_DataReadWrite.ipf
24//      leaving a stub Write_DIV_File() for the writer. Fully corrected, patched, and normalized DIV data
25//      is written out from "type" folder. The DIV file written out must be written in a
26//      format that is readable by ReadHeaderAndWork(type,fname). Each facility gets to pick their own format.
27//
28//      no longer hard-wired to 128x128
29//
30//*********************
31
32
33//works on the data in "type" folder
34//sums all of the data, and normalizes by the number of cells (=pixelX*pixelY)
35// calling procedure must make sure that the folder is on linear scale FIRST
36Function NormalizeDIV(type)
37        String type
38       
39        WAVE data=$("root:Packages:NIST:"+type+":data")
40        Variable totCts=sum(data,Inf,-Inf)              //sum all of the data
41        NVAR pixelX = root:myGlobals:gNPixelsX
42        NVAR pixelY = root:myGlobals:gNPixelsY
43
44       
45        data /= totCts
46        data *= pixelX*pixelY
47       
48        return(0)
49End
50
51// prompts the user for the location of the "COR" -level data
52// data can be copied to any data folder (except DIV) for use here...
53//
54// then there is a "pause for user" to allow the user to select the "box"
55// in the ON-AXIS datset that is to be replaced by the data in the off-axis data
56//
57// corrections are done...
58//
59// finally, the DIV file is written to disk
60Function MakeDIVFile(ctrType,offType)
61        String ctrType,offType
62       
63        Prompt ctrType,"On-Center Plex data (corrected)",popup,"STO;SUB;BGD;COR;CAL;SAM;EMP;"
64        Prompt offType,"Offset Plex data (corrected)",popup,"STO;SUB;BGD;COR;CAL;SAM;EMP;"
65        DoPrompt "Pick the data types",ctrType,offType
66        //"COR" data in both places - reduction must be done ahead of time
67       
68        //temporarily set data display to linear
69        NVAR gLog = root:myGlobals:gLogScalingAsDefault
70        Variable oldState = gLog
71        gLog=0  //linear
72       
73        if(V_Flag==1)
74                //user cancelled
75                return(1)
76        endif
77       
78        //show the ctrType
79        //get the xy range to replace
80        Execute "ChangeDisplay(\""+ctrType+"\")"
81       
82        NewPanel/K=2/W=(139,341,382,432) as "Get XY Range"
83        DoWindow/C tmp_GetXY
84        AutoPositionWindow/E/M=1/R=SANS_Data
85        DrawText 15,20,"Find the (X1,X2) and (Y1,Y2) range to"
86        DrawText 15,40,"replace and press continue"
87        Button button0, pos={80,58},size={92,20},title="Continue"
88        Button button0,proc=XYContinueButtonProc
89       
90        PauseForUser tmp_GetXY,SANS_Data
91       
92        //replace the center section of the "on" data with the center of the "off" data
93        Variable x1,x2,y1,y2
94        GetXYRange(x1,x2,y1,y2)
95        Printf "X=(%d,%d)  Y=(%d,%d)\r", x1,x2,y1,y2
96        ReplaceDataBlock(ctrType,offType,x1,x2,y1,y2)
97       
98        DoAlert 1,"Is this NG1 data with a second beamstop?"
99        if(V_flag==1)
100                GetXYRange(x1,x2,y1,y2)
101                Printf "X=(%d,%d)  Y=(%d,%d)\r", x1,x2,y1,y2
102                ReplaceDataBlock(ctrType,offType,x1,x2,y1,y2)
103        endif
104       
105        //normalize the new data (and show it)
106        NormalizeDiv(ctrtype)
107        UpdateDisplayInformation(ctrtype)
108        //write out the new data file
109        Write_DIV_File(ctrtype)
110        gLog = oldState         //set log/lin pref back to user - set preference
111        Return(0)
112End
113
114//ctrData is changed -- offData is not touched
115//simple replacement of the selected data...
116//
117// working in detector coordinates
118Function ReplaceDataBlock(ctrType,offType,x1,x2,y1,y2)
119        String ctrType,offType
120        Variable x1,x2,y1,y2
121       
122        //do it crudely, with nested for loops
123        WAVE ctrData=$("root:Packages:NIST:"+ctrtype+":data")
124        WAVE offData=$("root:Packages:NIST:"+offtype+":data")
125        Variable ii,jj
126       
127        for(ii=x1;ii<=x2;ii+=1)
128                for(jj=y1;jj<=y2;jj+=1)
129                        ctrData[ii][jj] = offData[ii][jj]
130                endfor
131        endfor
132       
133        return(0)
134End
135
136//continue button waiting for the user to pick the range, and continue the execution
137//
138Function XYContinueButtonProc(ctrlName)
139        String ctrlName
140       
141        DoWindow/K tmp_GetXY
142End
143
144// prompts the user to enter the XY range for the box replacement
145// user can get these numbers by printing out marquee coordinates to the command window
146//
147Function GetXYRange(x1,x2,y1,y2)
148        Variable &x1,&x2,&y1,&y2
149       
150        Variable x1p,x2p,y1p,y2p
151        Prompt x1p,"X1"
152        Prompt x2p,"X2"
153        Prompt y1p,"Y1"
154        Prompt y2p,"Y2"
155        DoPrompt "Enter the range to replace",x1p,x2p,y1p,y2p
156        x1=x1p
157        x2=x2p
158        y1=y1p
159        y2=y2p
160       
161//      Print x1,x2,y1,y2
162        Return(0)
163End
164
165
166/////////////////////
167//
168// for the DIV "protocol" panel, I probably need to have parts of the protocol panel initialized...
169// folders are generated at the startup initialization, before protocol panel
170//
171Proc BuildDIVPanel()
172        DoWindow/F DIV_Panel
173        if(V_flag==0)
174                InitDIVPanel()
175                DIV_Panel()
176        Endif
177End
178
179//initialization procedure for the protocol panel
180//note that :gAbsStr is also shared (common global) to that used in
181//the questionnare form of the protcol (see protocol.ipf)
182//
183//0901, uses 8 points in protocol wave
184Proc InitDIVPanel()
185
186        //set up the global variables needed for the protocol panel
187        //global strings to put in a temporary protocol textwave
188        Variable ii=0,nsteps=8
189        String waveStr="DIV_Protocol"
190        SetDataFolder root:myGlobals:Protocols
191        Make/O/T/N=(nsteps) $"root:myGlobals:Protocols:DIV_Protocol" = ""
192       
193        DIV_protocol[2] = "none"
194        DIV_protocol[3] = "none"
195        DIV_protocol[4] = "none"
196        DIV_protocol[5] = "AVTYPE=none;"
197        DIV_protocol[6] = "DRK=none,DRKMODE=0,"
198       
199
200        String/G root:myGlobals:Protocols:gPlex="Plex"
201        String/G root:myGlobals:Protocols:gPlexBgd="Bgd"
202        String/G root:myGlobals:Protocols:gPlexEmp="Emp"
203        String/G root:myGlobals:Protocols:gPlex_off="Plex offset"
204        String/G root:myGlobals:Protocols:gPlexBgd_off="Bgd offset"
205        String/G root:myGlobals:Protocols:gPlexEmp_off="Emp offset"
206        String/G root:myGlobals:Protocols:gPlexName="Plex_date.div"
207       
208        Variable/G root:myGlobals:Protocols:gPlexX1=45
209        Variable/G root:myGlobals:Protocols:gPlexX2=87
210        Variable/G root:myGlobals:Protocols:gPlexY1=43
211        Variable/G root:myGlobals:Protocols:gPlexY2=85
212        Variable/G root:myGlobals:Protocols:gPlexTrans=0.48
213       
214        SetDataFolder root:
215       
216End
217
218// load in one on-center file and show the box
219//
220Function ShowBoxButtonProc(ba) : ButtonControl
221        STRUCT WMButtonAction &ba
222
223        switch( ba.eventCode )
224                case 2: // mouse up
225                        // click code here
226                        if(cmpstr(ba.ctrlName, "ShowBox") == 0)
227                       
228                                //parse for the first run number
229                                SVAR gPlex = root:myGlobals:Protocols:gPlex
230                                String item,fname
231                               
232                                item = StringFromList(0, gPlex ,",")
233                                fname = FindFileFromRunNumber(str2num(item))
234                                if(strlen(fname) == 0)
235                                        Abort "Bad file number in Plex field"
236                                endif
237                                // load the file
238                                ReadHeaderAndData(fname)        //this is the full Path+file
239                                UpdateDisplayInformation("RAW")
240                                //draw a box of the specified size. This is persistent on the display as you scroll to the offset data
241                                NVAR x1 = root:myGlobals:Protocols:gPlexX1
242                                NVAR x2 = root:myGlobals:Protocols:gPlexX2
243                                NVAR y1 = root:myGlobals:Protocols:gPlexY1
244                                NVAR y2 = root:myGlobals:Protocols:gPlexY2
245                               
246                                SetDrawLayer/W=SANS_Data/K UserFront                    //set the layer, and clear it
247                                SetDrawEnv/W=SANS_Data xcoord=bottom,ycoord=left,fillpat=0,linethick=3,linefgc=(65535, 65535, 65535)
248                                DrawRect/W=SANS_Data x1, y2, x2, y1
249                               
250                                Button $ba.ctrlName,title="Clear Box",rename=HideBox,win=DIV_Panel
251                               
252                        else
253                                if(winType("SANS_Data")==1)
254                                        SetDrawLayer/W=SANS_Data/K UserFront                    //set the layer, and clear it
255                                        Button $ba.ctrlName,title="Show Box",rename=ShowBox,win=DIV_Panel
256                                else
257                                        Button $ba.ctrlName,title="Show Box",rename=ShowBox,win=DIV_Panel
258                                endif
259                        endif   
260                       
261                        break
262        endswitch
263
264        return 0
265End
266
267// do everything...
268//
269Function GenerateDIVButtonProc(ba) : ButtonControl
270        STRUCT WMButtonAction &ba
271
272        switch( ba.eventCode )
273                case 2: // mouse up
274                        // click code here
275                       
276                        //temporarily set data display to linear
277                        NVAR gLog = root:myGlobals:gLogScalingAsDefault
278                        Variable oldState = gLog
279                        gLog=0  //linear
280                       
281                       
282                        SVAR gPlex = root:myGlobals:Protocols:gPlex
283                        SVAR gPlexBgd = root:myGlobals:Protocols:gPlexBgd
284                        SVAR gPlexEmp = root:myGlobals:Protocols:gPlexEmp
285                        SVAR gPlex_off = root:myGlobals:Protocols:gPlex_off
286                        SVAR gPlexBgd_off = root:myGlobals:Protocols:gPlexBgd_off
287                        SVAR gPlexEmp_off = root:myGlobals:Protocols:gPlexEmp_off
288                        SVAR gPlexName = root:myGlobals:Protocols:gPlexName
289                       
290                        NVAR X1 = root:myGlobals:Protocols:gPlexX1
291                        NVAR X2 = root:myGlobals:Protocols:gPlexX2
292                        NVAR Y1 = root:myGlobals:Protocols:gPlexY1
293                        NVAR Y2 = root:myGlobals:Protocols:gPlexY2
294                        NVAR gPlexTrans = root:myGlobals:Protocols:gPlexTrans
295                       
296                        WAVE/T proto = $"root:myGlobals:Protocols:DIV_Protocol"
297                       
298                        String item,fname,str
299                        Variable ii,num
300                // reduce the on-center
301                        //patch trans
302                        num = ItemsInList(gPlex, ",")
303                        for(ii=0;ii<num;ii+=1)
304                                item = StringFromList(ii, gPlex ,",")
305                                fname = FindFileFromRunNumber(str2num(item))
306                                if(strlen(fname) == 0)
307                                        Abort "Bad file number in no offset Plex field"
308                                endif
309                                WriteTransmissionToHeader(fname,gPlexTrans)
310                        endfor
311                       
312                        //go through the protocol
313                        str = ParseRunNumberList(gPlexBgd)
314                        if(strlen(str) > 0)
315                                proto[0] = str
316                        else
317                                Abort "Bad file number in no offset Bgd"
318                        endif
319                        str = ParseRunNumberList(gPlexEmp)
320                        if(strlen(str) > 0)
321                                proto[1] = str
322                        else
323                                Abort "Bad file number in no offset Emp"
324                        endif
325                        str = ParseRunNumberList(gPlex)
326                        if(strlen(str) > 0)
327                                ExecuteProtocol("root:myGlobals:Protocols:DIV_Protocol",str)
328                        else
329                                Abort "Bad file number in no offset Plex"
330                        endif
331                        // move it into STO
332                        Execute "CopyWorkFolder(\"COR\",\"STO\")"
333                       
334                       
335                       
336                // reduce the off-center, keep in STO
337                        //patch trans
338                        num = ItemsInList(gPlex_off, ",")
339                        for(ii=0;ii<num;ii+=1)
340                                item = StringFromList(ii, gPlex_off ,",")
341                                fname = FindFileFromRunNumber(str2num(item))
342                                if(strlen(fname) == 0)
343                                        Abort "Bad file number in Plex field"
344                                endif
345                                WriteTransmissionToHeader(fname,gPlexTrans)
346                        endfor
347                       
348                        //go through the protocol
349                        str = ParseRunNumberList(gPlexBgd_off)
350                        if(strlen(str) > 0)
351                                proto[0] = str
352                        else
353                                Abort "Bad file number in offset Bgd"
354                        endif
355                        str = ParseRunNumberList(gPlexEmp_off)
356                        if(strlen(str) > 0)
357                                proto[1] = str
358                        else
359                                Abort "Bad file number in offset Emp"
360                        endif
361                        str = ParseRunNumberList(gPlex_off)
362                        if(strlen(str) > 0)
363                                ExecuteProtocol("root:myGlobals:Protocols:DIV_Protocol",str)
364                        else
365                                Abort "Bad file number in offset Emp"
366                        endif
367                        ConvertFolderToLinearScale("COR")
368                       
369                       
370                // replace the patch
371                // on-center data is changed (STO)
372                        ReplaceDataBlock("STO","COR",x1,x2,y1,y2)
373                // normalize
374                        NormalizeDiv("STO")
375                        UpdateDisplayInformation("STO")
376                //write out the new data file
377                        Write_DIV_File("STO")
378                               
379                        gLog=oldState           //revert display preference to old state       
380                        break
381        endswitch
382
383        return 0
384End
385
386// if a dark color is used, then
387//¥SetVariable setvar0 labelBack=(65535,65535,65535)
388// for each variable will give a white background to the label text
389Window DIV_Panel() : Panel
390        PauseUpdate; Silent 1           // building window...
391        NewPanel /W=(594,44,932,570)/K=1 as "DIV_Panel"
392//      ModifyPanel cbRGB=(35867,28177,65535)           //purple
393//      ModifyPanel cbRGB=(1,16019,65535)                               //electric blue
394        ModifyPanel cbRGB=(36631,59604,33902)           //spring green
395        SetDrawLayer UserBack
396        DrawRect 71,324,145,391
397        TitleBox title0,pos={14,16},size={50,20},title="No Offset"
398        TitleBox title0_1,pos={17,125},size={35,20},title="Offset"
399        SetVariable setvar0,pos={15,46},size={250,15},title="PLEX",value= root:myGlobals:Protocols:gPlex
400        SetVariable setvar0_1,pos={16,69},size={250,15},title="EMP",value= root:myGlobals:Protocols:gPlexEmp
401        SetVariable setvar0_2,pos={14,92},size={250,15},title="BGD",value= root:myGlobals:Protocols:gPlexBgd
402        SetVariable setvar1,pos={17,158},size={250,15},title="PLEX",value= root:myGlobals:Protocols:gPlex_off
403        SetVariable setvar001,pos={18,181},size={250,15},title="EMP",value= root:myGlobals:Protocols:gPlexEmp_off
404        SetVariable setvar002,pos={16,204},size={250,15},title="BGD",value= root:myGlobals:Protocols:gPlexBgd_off
405        SetVariable setvar002_1,pos={14,251},size={150,15},title="Transmission"
406        SetVariable setvar002_1,limits={0,1,0.01},value= root:myGlobals:Protocols:gPlexTrans
407//      SetVariable setvar003,pos={16,441},size={250,15},title="DIV FILE NAME"
408//      SetVariable setvar003,value= root:myGlobals:Protocols:gPlexName
409        Button ShowBox,pos={226,325},size={90,20},proc=ShowBoxButtonProc,title="Show Box"
410        Button button1,pos={25,441},size={150,20},proc=GenerateDIVButtonProc,title="Generate DIV File"
411        Button button2,pos={25,481},size={150,20},proc=ReloadDIVButtonProc,title="Load DIV File"
412        Button button4,pos={240,481},size={80,20},proc=DoneDIVButtonProc,title="Done"
413        Button button3,pos={240,10},size={50,20},proc=DIVHelpButtonProc,title="Help"
414        SetVariable setvar00201,pos={84,297},size={50,15},limits={0,root:myGlobals:gNPixelsY-1,1},title=" ",value= root:myGlobals:Protocols:gPlexY2
415        SetVariable setvar00202,pos={15,350},size={50,15},limits={0,root:myGlobals:gNPixelsX-1,1},title=" ",value= root:myGlobals:Protocols:gPlexX1
416        SetVariable setvar00203,pos={85,399},size={50,15},limits={0,root:myGlobals:gNPixelsY-1,1},title=" ",value= root:myGlobals:Protocols:gPlexY1
417        SetVariable setvar00204,pos={156,348},size={50,15},limits={0,root:myGlobals:gNPixelsX-1,1},title=" ",value= root:myGlobals:Protocols:gPlexX2
418EndMacro
419
420
421// done
422//
423Function DoneDIVButtonProc(ba) : ButtonControl
424        STRUCT WMButtonAction &ba
425
426        switch( ba.eventCode )
427                case 2: // mouse up
428                        // click code here
429                        DoWindow/K DIV_Panel
430                        break
431        endswitch
432
433        return 0
434End
435
436// load in a DIV file, print out the stats, display in SANS_Data
437//
438Function ReloadDIVButtonProc(ba) : ButtonControl
439        STRUCT WMButtonAction &ba
440
441        switch( ba.eventCode )
442                case 2: // mouse up
443                        // click code here
444                        Execute "ReadWork_DIV()"
445                        WaveStats root:Packages:NIST:DIV:data
446                        Print "*"                       
447//                      Execute "ChangeDisplay(\"DIV\")"       
448                        break
449        endswitch
450
451        return 0
452End
453
454//
455Function DIVHelpButtonProc(ba) : ButtonControl
456        STRUCT WMButtonAction &ba
457
458        switch( ba.eventCode )
459                case 2: // mouse up
460                        // click code here
461                        DisplayHelpTopic/Z/K=1 "SANS Data Reduction Tutorial[Detector Sensitivity File]"
462                        if(V_flag !=0)
463                                DoAlert 0,"The SANS Data Reduction Tutorial Help file could not be found"
464                        endif
465                        break
466        endswitch
467
468        return 0
469End
470
Note: See TracBrowser for help on using the repository browser.