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

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

extensive changes to accomodate 1x1 binning of the HighRes? detector. It is implemented as a global flag. Currently only 4x4 and 1x1 are allowed. 1x1 has never been tested in reality, only simulated data - so my assumed dimensions may not be correct. look for TODOHIGHRES in the file for places that may need to be updated for different file dimensions. Testing of the simulated data is proving to be excruciatingly slow, but passable for a test. Speed optimization will be needed if this is the final solution. Memory management will also be an issue since every "copy" of the highRes matrix is enormous. Carry as few of these around as possible in the future to keep the experiment size to something reasonable.

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