source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_Initialize.ipf @ 1235

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

Changed the calculation of solid angle to properly reflect the tube geometry. This mode is now the (correct) default calculation. A global switch can be applied for testing to use the old method (with many warning alerts).

File size: 18.5 KB
Line 
1#pragma rtGlobals=3             // Use modern global access method and strict wave access.
2#pragma version=0.1
3#pragma IgorVersion=6.1
4
5//***********************
6// NOV 2015 Vers 0.1
7//
8// Initialization procedures that must be run before anything
9// this is accomplished by placing:
10//
11// Initialize()
12// #include "includes"
13//
14// in the built-in procedure window of the .pxt (template) experiment
15// IGOR recognizes this, and executes Initialize() immediately after
16// compiling all of the included procedures. This is all done as the blank
17// template is opened
18//
19// Choosing initialize from the VSANS menu will do the same, and no harm is done
20// to the experiment by re- initializing. no data or folders are overwritten
21//
22//************************
23
24Constant kVSANSVersion = 7.99
25
26// TODO -- need to set up a separate file of "constants" or "globals" where the actual numbers are
27//stored. If there are not a lot, that place could be here. InitFacilityGlobals() is currently in NCNR_Utils.ipf
28
29
30// for the change in July 2017 where the beam center is now defined in cm, rather than pixels.
31// this need not ever change from 1
32// the back detector is always treated as a beam center in pixels, since it is the natural definition
33Constant kBCTR_CM = 1                   //set to 1 to use beam center in cm. O to use pixels
34
35// // TODO: -- replace this constant with V_getDet_panel_gap(fname,detStr)
36//Constant kPanelTouchingGap = 10                       // TODO -- measure this gap when panels "touch", UNITS OF mm, not cm
37
38
39// the base data folder path where the raw data is loaded
40Strconstant ksBaseDFPath = "root:Packages:NIST:VSANS:RawVSANS:"
41
42
43// the list of WORK Folders
44// RawVSANS does not behave as a WORK folder, but it is local. so add it in explicitly to the list if needed
45// VCALC behaves *almost* as a WORK folder, but it is local. so add it in explicitly to the list if needed
46//Strconstant ksWorkFolderList = "RAW;SAM;EMP;BGD;COR;DIV;ABS;MSK;CAL;STO;SUB;DRK;ADJ;VCALC;RawVSANS;"
47Strconstant ksWorkFolderListShort = "RAW;SAM;EMP;BGD;COR;DIV;ABS;MSK;CAL;STO;SUB;DRK;ADJ;"
48
49
50// for defining which "bin type" corresponds to which set of extensions for I(q) data
51// !! see V_BinTypeStr2Num() for the numbering, not the order of the list
52//
53//////////////////
54//Strconstant ksBinTypeStr = "One;Two;Four;Slit Mode;"
55Strconstant ksBinTypeStr = "F4-M4-B;F2-M2-B;F1-M1-B;F2-M1-B;F1-M2xTB-B;F2-M2xTB-B;SLIT-F2-M2-B;"
56Strconstant ksBinType1 = "FT;FB;FL;FR;MT;MB;ML;MR;B;"           //these are the "active" extensions
57Strconstant ksBinType2 = "FTB;FLR;MTB;MLR;B;"
58Strconstant ksBinType3 = "FLRTB;MLRTB;B;"
59Strconstant ksBinType4 = "FL;FR;ML;MR;B;"               //in SLIT mode, disregard the T/B panels
60Strconstant ksBinType5 = "FTB;FLR;MLRTB;B;"
61Strconstant ksBinType6 = "FLRTB;MLR;B;"
62Strconstant ksBinType7 = "FTB;FLR;MLR;B;"
63///////////////////
64
65
66// for looping over each detector
67Strconstant ksDetectorListNoB = "FL;FR;FT;FB;ML;MR;MT;MB;"
68Strconstant ksDetectorListAll = "FL;FR;FT;FB;ML;MR;MT;MB;B;"
69
70
71// for Protocols
72Constant kNumProtocolSteps = 12
73// for trimming of the I(q) data sets, and part of the protocol
74Strconstant ksPanelBinTypeList = "B;FT;FB;FL;FR;MT;MB;ML;MR;FTB;FLR;MTB;MLR;FLRTB;MLRTB;"
75Strconstant ksBinTrimBegDefault = "B=5;FT=3;FB=3;FL=3;FR=3;MT=3;MB=3;ML=3;MR=3;FTB=2;FLR=2;MTB=2;MLR=2;FLRTB=1;MLRTB=1;"
76Strconstant ksBinTrimEndDefault = "B=10;FT=5;FB=5;FL=5;FR=5;MT=5;MB=5;ML=5;MR=5;FTB=4;FLR=4;MTB=4;MLR=4;FLRTB=3;MLRTB=3;"
77
78
79
80//////// HIGH RESOLUTION DETECTOR  ///////////////
81
82
83//
84// In May 2019 - after testing with Phil's procesing, the data from the detector has a
85// larger read noise value. It can also no longer be treated  as a constant value, but rather
86// a detector file that is read in and subtracted pixel-by-pixel.
87//
88
89
90// the average read noise level of the back detector
91// taken from multiple runs with the beam off, 6-28-18
92// runs sans12324 - sans12353
93//
94// used in V_Raw_to_Work()
95// average of whole panel (tested several data files) = 208 +/- 14
96//
97// 200 appears to be a better value - (empirical, based on teflon/converging pinhole data)
98//Constant kReadNoiseLevel_bin4 = 200
99//Constant kReadNoiseLevel_Err_bin4 = 14
100// after binning/processing changes from March 2019:
101Constant kReadNoiseLevel_bin4 = 3160                    // from bkg area of sans30201 (a transmission measurement)
102Constant kReadNoiseLevel_Err_bin4 = 50          //estimated
103
104// TODOHIGHRES: these values are complete fiction
105Constant kReadNoiseLevel_bin1 = 20
106Constant kReadNoiseLevel_Err_bin1 = 1
107
108// Pixel shifts for the back detector to bring the three CCDs into registry
109// data from pinholes used to match up CCDs
110// 27 JUN 2018
111// runs 12221,12225,27,33,34,35,38,42
112// middle CCD is not moved
113// See V_ShiftBackDetImage() for implementation
114Constant        kShift_TopX_bin4 = 7
115Constant                kShift_TopY_bin4 = 105
116Constant                kShift_BottomX_bin4 = 5
117Constant                kShift_BottomY_bin4 = 35
118
119// TODOHIGHRES -- these values need to be verified. they are currently simply 4x the bin4 values
120Constant        kShift_TopX_bin1 = 28
121Constant                kShift_TopY_bin1 = 420
122Constant                kShift_BottomX_bin1 = 20
123Constant                kShift_BottomY_bin1 = 130
124
125
126
127
128
129
130
131
132Proc Initialize_VSANS()
133        V_Initialize()
134End
135
136//this is the main initialization procedure that must be the first thing
137//done when opening a new Data reduction experiment
138//
139//sets up data folders, globals, protocols, and draws the main panel
140Proc V_Initialize()
141
142        Variable curVersion = kVSANSVersion
143        Variable oldVersion = NumVarOrDefault("root:VSANS_RED_VERSION",curVersion)
144               
145        if(oldVersion == curVersion)
146                //must just be a new startup with the current version
147                Variable/G root:VSANS_RED_VERSION=kVSANSVersion
148        endif
149       
150        if(oldVersion < curVersion)
151                String str =    "This experiment was created with version "+num2str(oldVersion)+" of the macros. I'll try to make this work, but please start new work with a current template"
152                DoAlert 0,str
153        endif
154       
155        V_InitFolders()
156       
157       
158        V_InitFakeProtocols()
159        V_InitGlobals()
160        V_InitFacilityGlobals()
161        DoWindow/F Main_VSANS_Panel
162        If(V_flag == 0)
163                //draw panel
164                Main_VSANS_Panel()
165        Endif
166//      V_ResizeCmdWindow()
167
168        VC_Initialize_Space()           //initialize folders for VCALC
169
170// TODO - be sure that NCNR is defined correctly       
171        //unload the NCNR_Package_Loader, if NCNR not defined
172        UnloadNCNR_VSANS_Procedures()
173
174End
175
176//creates all the necessary data folders in the root folder
177//does not overwrite any existing folders of the same name
178//it leaves data in them untouched
179//
180// x-- make sure that I have all of the folders that I need
181//
182Function V_InitFolders()
183       
184        NewDataFolder/O root:Packages
185        NewDataFolder/O root:Packages:NIST
186        NewDataFolder/O root:Packages:NIST:VSANS
187       
188// for the file catalog
189        NewDataFolder/O root:Packages:NIST:VSANS:CatVSHeaderInfo
190// for the globals
191        NewDataFolder/O root:Packages:NIST:VSANS:Globals
192// for the raw nexus data (so I don't need to reload to get a single value)
193        NewDataFolder/O root:Packages:NIST:VSANS:RawVSANS
194
195// folders for the reduction steps             
196        NewDataFolder/O root:Packages:NIST:VSANS:RAW
197        NewDataFolder/O root:Packages:NIST:VSANS:SAM
198        NewDataFolder/O root:Packages:NIST:VSANS:EMP
199        NewDataFolder/O root:Packages:NIST:VSANS:BGD
200        NewDataFolder/O root:Packages:NIST:VSANS:COR
201        NewDataFolder/O root:Packages:NIST:VSANS:DIV
202        NewDataFolder/O root:Packages:NIST:VSANS:MSK
203        NewDataFolder/O root:Packages:NIST:VSANS:ABS
204        NewDataFolder/O root:Packages:NIST:VSANS:CAL
205        NewDataFolder/O root:Packages:NIST:VSANS:STO
206        NewDataFolder/O root:Packages:NIST:VSANS:SUB
207        NewDataFolder/O root:Packages:NIST:VSANS:DRK
208        NewDataFolder/O root:Packages:NIST:VSANS:ADJ
209        NewDataFolder/O root:Packages:NIST:VSANS:RealTime
210        NewDataFolder/O root:Packages:NIST:VSANS:VCALC
211        NewDataFolder/O root:Packages:NIST:VSANS:ReadNoise
212
213
214// ?? anything else
215
216// for simulation
217//      NewDataFolder/O root:Packages:NIST:VSANS:SAS
218
219
220       
221        Return(0)
222End
223
224
225
226
227//
228//Global folder already exists...
229//adds appropriate globals to the newly created myGlobals folder
230//return data folder to root: before leaving
231//
232//
233Function V_InitGlobals()
234       
235       
236        Variable/G root:Packages:NIST:VSANS:Globals:gIsLogScale = 0
237        String/G root:Packages:NIST:VSANS:Globals:gCurDispType = "RAW"
238       
239        //check platform, so Angstrom can be drawn correctly
240
241        //TODO  -- this is different on Igor 7. Macintosh # has been updated, but Windows has not
242        // use Print char2num("Å") to find the magic number
243        if(cmpstr("Macintosh",IgorInfo(2)) == 0)
244                String/G root:Packages:NIST:VSANS:Globals:gAngstStr = num2char(197)
245                String/G root:Packages:NIST:gAngstStr = num2char(197)
246                Variable/G root:Packages:NIST:VSANS:Globals:gIsMac = 1
247        else
248                //either Windows or Windows NT
249                String/G root:Packages:NIST:VSANS:Globals:gAngstStr = num2char(-59)
250                String/G root:Packages:NIST:gAngstStr = num2char(-59)
251                Variable/G root:Packages:NIST:VSANS:Globals:gIsMac = 0
252                //SetIgorOption to keep some PC's (graphics cards?) from smoothing the 2D image
253                // SRK APRIL 2019 - removed this, does not exist in Igor 8 on WIN, and cause an error.
254//              Execute "SetIgorOption WinDraw,forceCOLORONCOLOR=1"
255        endif
256       
257        // TODO x- find the SANS preferences, copy over and update for VSANS
258        // x- these are all in PlotUtilsMacro_v40.ipf as the preferences are set up as common
259        // to all packages. I'm not sure that I want to do this with VSANS, but make the packages
260        // separate entities. I'm seeing little benefit of the crossover, especially now that
261        // Analysis is not mine. So for VSANS, there is a new, separate file: V_VSANS_Preferences.ipf
262
263        //this is critical to initialize now - as it has the y/n flags for the detector correction steps
264        Execute "Initialize_VSANSPreferences()"
265
266        Execute "V_TubeZeroPointTables()"                       // correction to the beam center
267       
268        // set the lookup waves for log/lin display of the detector images
269        V_MakeImageLookupTables(10000,0,1)
270
271
272
273        //set flag if Demo Version is detected
274        Variable/G root:Packages:NIST:VSANS:Globals:isDemoVersion = V_isDemo()
275
276
277// for testing
278// if this is set to 1, the OLD (incorrect) cos^3 solid angle will be applied to the tubes
279// and a lot of alerts will pop up...
280        Variable/G root:Packages:NIST:VSANS:Globals:gDo_OLD_SolidAngleCor = 0
281       
282        //set XML globals
283//      String/G root:Packages:NIST:gXMLLoader_Title = ""
284       
285        Return(0)
286End
287
288//
289//num = number of points (10000 seeems to be a good number so far)
290// lo = lower value (between 0 and 1)
291// hi = upper value (between 0 and 1)
292//
293// note that it is currenty NOT OK for lo > hi (!= reversed color scale, right now log(negative) == bad)
294//
295// TODO hi, lo not used properly here, seems to mangle log display now that I'm switching the lo,hi of the ctab
296//
297Function V_MakeImageLookupTables(num,lo,hi)
298        Variable num,lo,hi
299
300                // lookup waves for log and linear display of images
301        // this is used for the main data display. With this, I can use the original
302        // detector data (no copy) and the zeros in the data set are tolerated when displaying
303        // on log scale
304        SetDataFolder root:Packages:NIST:VSANS:Globals
305        Variable val,offset
306       
307        offset = 1/num          //can't use 1/lo if lo == 0
308       
309        Make/O/D/N=(num) logLookupWave,linearLookupWave
310       
311        linearLookupWave = (p+1)/num
312       
313       
314        logLookupWave = log(linearLookupWave)
315        val = logLookupWave[0]
316        logLookupWave += -val + offset
317        val = logLookupWave[num-1]
318        logLookupWave /= val
319       
320        SetDataFolder root:
321       
322        return(0)
323end
324
325//
326// initializes globals that are specific to VSANS
327//
328// there really should be nothing here... all of this should now be in the Nexus data file
329// and not tethered to hard-wired constants.
330//
331// -- what was here was:
332// number of detector pixels
333// pixel size
334// deadtime
335// beamstop "tolerance" to identify Trans files
336// sample aperture offset
337//
338Function V_InitFacilityGlobals()
339
340//      //Detector -specific globals
341//      Variable/G root:myGlobals:gNPixelsX=128
342//      Variable/G root:myGlobals:gNPixelsY=128
343//     
344//      // as of Jan2008, detector pixel sizes are read directly from the file header, so they MUST
345//      // be set correctly in instr.cfg - these values are not used, but declared to avoid errors
346//      Variable/G root:myGlobals:PixelResNG3_ILL = 1.0         //pixel resolution in cm
347//      Variable/G root:myGlobals:PixelResNG5_ILL = 1.0
348//      Variable/G root:myGlobals:PixelResNG7_ILL = 1.0
349//      Variable/G root:myGlobals:PixelResNG3_ORNL = 0.5
350//      Variable/G root:myGlobals:PixelResNG5_ORNL = 0.5
351//      Variable/G root:myGlobals:PixelResNG7_ORNL = 0.5
352//      Variable/G root:myGlobals:PixelResNGB_ORNL = 0.5
353////    Variable/G root:myGlobals:PixelResCGB_ORNL = 0.5                // fiction
354//
355//      Variable/G root:myGlobals:PixelResDefault = 0.5
356//     
357//      Variable/G root:myGlobals:DeadtimeNG3_ILL = 3.0e-6              //deadtime in seconds
358//      Variable/G root:myGlobals:DeadtimeNG5_ILL = 3.0e-6
359//      Variable/G root:myGlobals:DeadtimeNG7_ILL = 3.0e-6
360//      Variable/G root:myGlobals:DeadtimeNGB_ILL = 4.0e-6              // fictional
361//      Variable/G root:myGlobals:DeadtimeNG3_ORNL_VAX = 3.4e-6         //pre - 23-JUL-2009 used VAX
362//      Variable/G root:myGlobals:DeadtimeNG3_ORNL_ICE = 1.5e-6         //post - 23-JUL-2009 used ICE
363//      Variable/G root:myGlobals:DeadtimeNG5_ORNL = 0.6e-6                     //as of 9 MAY 2002
364//      Variable/G root:myGlobals:DeadtimeNG7_ORNL_VAX = 3.4e-6         //pre 25-FEB-2010 used VAX
365//      Variable/G root:myGlobals:DeadtimeNG7_ORNL_ICE = 2.3e-6         //post 25-FEB-2010 used ICE
366//      Variable/G root:myGlobals:DeadtimeNGB_ORNL_ICE = 4.0e-6         //per JGB 16-JAN-2013, best value we have for the oscillating data
367//
368////    Variable/G root:myGlobals:DeadtimeCGB_ORNL_ICE = 1.5e-6         // fiction
369//
370//      Variable/G root:myGlobals:DeadtimeDefault = 3.4e-6
371//
372//      //new 11APR07
373//      Variable/G root:myGlobals:BeamstopXTol = -8                     // (cm) is BS Xpos is -5 cm or less, it's a trans measurement
374//      // sample aperture offset is NOT stored in the VAX header, but it should be
375//      // - when it is, remove the global and write an accessor AND make a place for
376//      // it in the RealsRead
377//      Variable/G root:myGlobals:apOff = 5.0                           // (cm) distance from sample aperture to sample position
378
379End
380
381///////////////////////////////////////////////
382// TODO
383////////////// everything below needs to be re-written for VSANS
384//
385//////////////////////////////////////////////
386
387
388//
389// do I need to make the protocols any longer for VSANS? (yes -- now 12 points)
390// What other options for processing / averaging / saving are needed??
391//  TODO
392// x- likely that I'll want to have #pts to cut from I(q) as input to NSORT within the protocol so that the
393// entire reduction can be automatic
394//
395//
396// x- creates the "base" protocols that should be available, after creating the data folder
397// x- all protocols are kept in the root:Packages:NIST:VSANS:Globals:Protocols folder, created here
398//
399//
400//*****as of 05_2017, protocols are 12 points long, [6] is used for work.drk, [7,8] are for trimmig points, and [9,11] are currently unused
401//
402Function V_InitFakeProtocols()
403       
404        NewDataFolder/O root:Packages:NIST:VSANS:Globals:Protocols
405        Make/O/T $"root:Packages:NIST:VSANS:Globals:Protocols:Base"={"none","none","ask","ask","none","AVTYPE=Circular;SAVE=Yes;NAME=Auto;PLOT=Yes;BINTYPE=F4-M4-B;","DRK=none,DRKMODE=0,","","","","",""}
406        Make/O/T $"root:Packages:NIST:VSANS:Globals:Protocols:DoAll"={"ask","ask","ask","ask","ask","AVTYPE=Circular;SAVE=Yes;NAME=Auto;PLOT=Yes;BINTYPE=F4-M4-B;","DRK=none,DRKMODE=0,","","","","",""}
407        Make/O/T/N=(kNumProtocolSteps) $"root:Packages:NIST:VSANS:Globals:Protocols:CreateNew"                  //null wave
408        //Initialize waves to store values in
409       
410        String/G root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr=""
411        String/G root:Packages:NIST:VSANS:Globals:Protocols:gNewStr=""
412        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr = "AVTYPE=Circular;SAVE=Yes;NAME=Auto;PLOT=Yes;BINTYPE=F4-M4-B;"
413       
414        String/G root:Packages:NIST:VSANS:Globals:Protocols:gBegPtsStr=""
415        String/G root:Packages:NIST:VSANS:Globals:Protocols:gEndPtsStr=""
416        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr=""
417       
418        Return(0)
419End
420
421//simple function to resize the comand window to a nice size, no matter what the resolution
422//need to test out on several different monitors and both platforms
423//
424// could easily be incorporated into the initialization routines to ensure that the
425// command window is always visible at startup of the macros. No need for a hook function
426//
427Function V_ResizeCmdWindow()
428
429        String str=IgorInfo(0),rect="",platform=igorinfo(2)
430        Variable depth,left,top,right,bottom,factor
431       
432        if(cmpstr(platform,"Macintosh")==0)
433                factor=1
434        else
435                factor = 0.6            //fudge factor to get command window on-screen on Windows
436        endif
437        rect = StringByKey("SCREEN1", str  ,":",";")   
438        sscanf rect,"DEPTH=%d,RECT=%d,%d,%d,%d",depth, left,top,right,bottom
439        MoveWindow/C  (left+3)*factor,(bottom-150)*factor,(right-50)*factor,(bottom-10)*factor
440End
441
442// since the NCNR procedures can't be loaded concurrently with the other facility functions,
443// unload this procedure file, and add this to the functions that run at initialization of the
444// experiment
445//
446// TODO - be sure that this unloads correctly
447Function UnloadNCNR_VSANS_Procedures()
448
449#if (exists("NCNR_VSANS")==6)                   //defined in the main #includes file.
450        //do nothing if an NCNR reduction experiment
451#else
452        if(ItemsInList(WinList("NCNR_Package_Loader.ipf", ";","WIN:128")))
453                Execute/P "CloseProc /NAME=\"NCNR_Package_Loader.ipf\""
454                Execute/P "COMPILEPROCEDURES "
455        endif
456#endif
457
458End
459
460//returns 1 if demo version, 0 if full version
461Function V_IsDemo()
462
463        // create small offscreen graph
464        Display/W=(3000,3000,3010,3010)
465        DoWindow/C IsDemoGraph
466
467        // try to save a PICT or bitmap of it to the clipboard
468        SavePICT/Z  as "Clipboard"
469        Variable isDemo= V_Flag != 0    // if error: must be demo
470        DoWindow/K IsDemoGraph
471        return isDemo
472End
473
474// Clean out the RawVSANS folder before saving
475Function BeforeExperimentSaveHook(rN,fileName,path,type,creator,kind)
476        Variable rN,kind
477        String fileName,path,type,creator
478
479        // clean out, so that the file SAVE is not slow due to the large experiment size
480        // TODO -- decide if this is really necessary
481//     
482//      V_CleanOutRawVSANS()
483// present a progress window
484        V_CleanupData_w_Progress(0,1)
485        Printf "Hook cleaned out RawVSANS, experiment saved\r"
486
487        NVAR/Z gHighResBinning = root:Packages:NIST:VSANS:Globals:gHighResBinning
488        if(gHighResBinning == 1)
489// these KillDF are a bad idea - it wipes out all of the current work
490// whenever a save is done - which is the opposite of what you want
491// to happen when you save!
492       
493                Printf "Hook cleaned out WORK folders, experiment saved\r"
494
495                KillDataFolder/Z root:Packages:NIST:VSANS:RAW
496                KillDataFolder/Z root:Packages:NIST:VSANS:SAM
497                KillDataFolder/Z root:Packages:NIST:VSANS:EMP
498                KillDataFolder/Z root:Packages:NIST:VSANS:BGD
499                KillDataFolder/Z root:Packages:NIST:VSANS:COR
500                KillDataFolder/Z root:Packages:NIST:VSANS:DIV
501                KillDataFolder/Z root:Packages:NIST:VSANS:MSK
502                KillDataFolder/Z root:Packages:NIST:VSANS:ABS
503                KillDataFolder/Z root:Packages:NIST:VSANS:CAL
504                KillDataFolder/Z root:Packages:NIST:VSANS:STO
505                KillDataFolder/Z root:Packages:NIST:VSANS:SUB
506                KillDataFolder/Z root:Packages:NIST:VSANS:DRK
507                KillDataFolder/Z root:Packages:NIST:VSANS:ADJ
508                KillDataFolder/Z root:Packages:NIST:VSANS:VCALC
509
510        endif
511// re-create anthing that was killed
512        V_initFolders()
513
514End
Note: See TracBrowser for help on using the repository browser.