source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_WorkFolderUtils.ipf @ 980

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

more minor changes

File size: 26.2 KB
Line 
1#pragma rtGlobals=3             // Use modern global access method and strict wave access.
2#pragma version=1.0
3#pragma IgorVersion=6.1
4
5//*******************
6// Vers 1.0 JAN2016
7//
8//*******************
9//  VSANS Utility procedures for handling of workfiles (each is housed in a separate datafolder)
10//
11// - adding RAW data to a workfile
12// -- this conversion applies the detector corrections
13//
14// - copying workfiles to another folder
15//
16// - absolute scaling
17//
18// - (no) the WorkFile Math panel for simple image math (not done - maybe in the future?)
19// -
20// - (no) adding work.drk data without normalizing to monitor counts (the case not currently handled)
21//***************************
22
23//
24// Functions used for manipulation of the local Igor "WORK" folder
25// structure as raw data is displayed and processed.
26//
27//
28Strconstant ksDetectorListNoB = "FT;FB;FL;FR;MT;MB;ML;MR;"
29Strconstant ksDetectorListAll = "FT;FB;FL;FR;MT;MB;ML;MR;B;"
30
31
32//
33//Entry procedure from main panel
34//
35Proc CopyWorkFolder(oldType,newType)
36        String oldType,newType
37        Prompt oldType,"Source WORK data type",popup,"SAM;EMP;BGD;DIV;COR;CAL;RAW;ABS;STO;SUB;DRK;"
38        Prompt newType,"Destination WORK data type",popup,"SAM;EMP;BGD;DIV;COR;CAL;RAW;ABS;STO;SUB;DRK;"
39
40        // data folder "old" will be copied to "new" (either kills/copies or will overwrite)
41        CopyHDFToWorkFolder(oldtype,newtype)
42End
43
44//
45// copy what is needed for data processing (not the DAS_logs)
46// from the RawVSANS storage folder to the local WORK folder as needed
47//
48// TODO -- at what stage do I make copies of data in linear/log forms for data display?
49//                      -- when do I make the 2D error waves?
50//
51// TODO - decide what exactly I need to copy over. May be best to copy all, and delete
52//       what I know that I don't need
53//
54// TODO !!! DuplicateDataFolder will FAIL - in the base case of RAW data files, the
55//  data is actually in use - so it will fail every time. need an alternate solution. in SANS,
56// there are a limited number of waves to carry over, so Dupliate/O is used for rw, tw, data, etc.
57//
58//
59//
60// TODO : I also need a list of what is generated during processing that may be hanging around - that I need to
61//     be sure to get rid of - like the calibration waves, solidAngle, etc.
62//
63// hdfDF is the name only of the data in storage. May be full file name with extension (clean as needed)
64// type is the destination WORK folder for the copy
65//
66Function CopyHDFToWorkFolder(fromStr,toStr)
67        String fromStr,toStr
68       
69        String fromDF, toDF
70       
71        // make the DF paths - source and destination
72        fromDF = "root:Packages:NIST:VSANS:"+fromStr
73        toDF = "root:Packages:NIST:VSANS:"+toStr
74       
75//      // make a copy of the file name for my own use, since it's not in the file
76//      String/G $(toDF+":file_name") = root:
77       
78        // copy the folders
79        KillDataFolder/Z $toDF                  //DuplicateDataFolder will not overwrite, so Kill
80       
81        if(V_flag == 0)         // kill DF was OK
82                DuplicateDataFolder $("root:Packages:NIST:VSANS:"+fromStr),$("root:Packages:NIST:VSANS:"+toStr)
83               
84                // I can delete these if they came along with RAW
85                //   DAS_logs
86                //   top-level copies of data (duplicate links)
87                KillDataFolder/Z $(toDF+":entry:entry:DAS_logs")
88                KillDataFolder/Z $(toDF+":entry:entry:data")
89                KillDataFolder/Z $(toDF+":entry:entry:data_B")
90                KillDataFolder/Z $(toDF+":entry:entry:data_ML")
91                KillDataFolder/Z $(toDF+":entry:entry:data_MR")
92                KillDataFolder/Z $(toDF+":entry:entry:data_MT")
93                KillDataFolder/Z $(toDF+":entry:entry:data_MB")
94                KillDataFolder/Z $(toDF+":entry:entry:data_FL")
95                KillDataFolder/Z $(toDF+":entry:entry:data_FR")
96                KillDataFolder/Z $(toDF+":entry:entry:data_FT")
97                KillDataFolder/Z $(toDF+":entry:entry:data_FB")
98
99                return(0)
100        else
101                // need to do this the hard way, duplicate/O recursively
102                // see V_CopyToWorkFolder()
103               
104                // everything on the top level
105                V_DuplicateDataFolder($(toDF+":entry:entry"),fromStr,toStr,0,"",0)      //no recursion here
106                // control
107                V_DuplicateDataFolder($(toDF+":entry:entry:control"),fromStr,toStr,0,"",1)      //yes recursion here
108                // instrument
109                V_DuplicateDataFolder($(toDF+":entry:entry:instrument"),fromStr,toStr,0,"",1)   //yes recursion here
110                // reduction
111                V_DuplicateDataFolder($(toDF+":entry:entry:reduction"),fromStr,toStr,0,"",1)    //yes recursion here
112                // sample
113                V_DuplicateDataFolder($(toDF+":entry:entry:sample"),fromStr,toStr,0,"",1)       //yes recursion here
114
115        endif   
116       
117        return(0)
118end
119
120
121////////
122// see the help entry for IndexedDir for help on (possibly) how to do this faster
123// -- see the function Function ScanDirectories(pathName, printDirNames)
124//
125
126
127// from IgorExchange On July 17th, 2011 jtigor
128// started from "Recursively List Data Folder Contents"
129// Posted July 15th, 2011 by hrodstein
130//
131//
132//
133Proc V_CopyWorkFolderTest(dataFolderStr, fromStr, toStr, level, sNBName, recurse)
134        String dataFolderStr="root:Packages:NIST:VSANS:RAW"
135        String fromStr = "RAW"
136        String toStr="SAM"
137        Variable level=0
138        String sNBName="DataFolderTree"
139        Variable recurse = 1
140       
141        V_DuplicateDataFolder($dataFolderStr, fromStr, toStr, level, sNBName, recurse)
142
143
144end
145
146// ListDataFolder(dfr, level)
147// Recursively lists objects in data folder.
148// Pass data folder path for dfr and 0 for level.
149// pass level == 0 for the first call
150//  sNBName = "" prints nothing. any name will generate a notebook
151//
152// recurse == 0 will do only the specified folder, anything else will recurse all levels
153// toStr is the string name of the top-level folder only, not the full path
154//
155//
156Function V_DuplicateDataFolder(dfr, fromStr, toStr, level, sNBName,recurse)
157        DFREF dfr
158        String fromStr
159        String toStr
160        Variable level                  // Pass 0 to start
161        String sNBName
162        Variable recurse
163 
164        String name
165        String dfName
166        String sString
167       
168        String toDF = ""
169 
170        if (level == 0)         // this is the data folder, generate if needed in the destination
171                name = GetDataFolder(1, dfr)
172//              sPrintf sString, "%s (data folder)\r", name
173                toDF = ReplaceString(fromStr,name,toStr,1)              // case-sensitive replace
174                sprintf sString, "NewDataFolder/O %s\r",toDF
175                NewDataFolder/O $(RemoveEnding(toDF,":"))                       // remove trailing semicolon if it's there
176               
177                V_WriteBrowserInfo(sString, 1, sNBName)
178        endif
179 
180        dfName = GetDataFolder(1, dfr)
181        toDF = ReplaceString(fromStr,dfName,toStr,1)            // case-sensitive replace
182        Variable i
183 
184        String indentStr = "\t"
185        for(i=0; i<level; i+=1)
186                indentStr += "\t"
187        endfor
188 
189        Variable numWaves = CountObjectsDFR(dfr, 1)
190        for(i=0; i<numWaves; i+=1)
191                name = GetIndexedObjNameDFR(dfr, 1, i)
192                //
193                // wave type does not matter now. Duplicate does not care
194                //
195                sPrintf sString, "Duplicate/O  %s,  %s\r",dfName+name,toDF+name
196                Duplicate/O $(dfName+name),$(toDF+name)
197               
198                V_WriteBrowserInfo(sString, 2, sNBName)
199        endfor 
200 
201        Variable numNumericVariables = CountObjectsDFR(dfr, 2) 
202        for(i=0; i<numNumericVariables; i+=1)
203                name = GetIndexedObjNameDFR(dfr, 2, i)
204                sPrintf sString, "%s%s (numeric variable)\r", indentStr, name
205                V_WriteBrowserInfo(sString, 3, sNBName)
206        endfor 
207 
208        Variable numStringVariables = CountObjectsDFR(dfr, 3)   
209        for(i=0; i<numStringVariables; i+=1)
210                name = GetIndexedObjNameDFR(dfr, 3, i)
211                sPrintf sString, "%s%s (string variable)\r", indentStr, name
212                V_WriteBrowserInfo(sString, 4, sNBName)
213        endfor 
214
215        if(recurse)
216                Variable numDataFolders = CountObjectsDFR(dfr, 4)       
217                for(i=0; i<numDataFolders; i+=1)
218                        name = GetIndexedObjNameDFR(dfr, 4, i)
219//                      sPrintf sString, "%s%s (data folder)\r", indentStr, name
220                         dfName = GetDataFolder(1, dfr)
221                         
222                        toDF = ReplaceString(fromStr,dfName,toStr,1)            // case-sensitive replace
223                        sprintf sString, "NewDataFolder/O %s\r",toDF+name
224                        NewDataFolder/O $(toDF+name)
225                       
226                       
227                        V_WriteBrowserInfo(sString, 1, sNBName)
228                        DFREF childDFR = dfr:$(name)
229                        V_DuplicateDataFolder(childDFR, fromStr, toStr, level+1, sNBName, recurse)
230                endfor 
231        endif
232         
233//when finished walking tree, save as RTF with dialog   
234//      if(level == 0 && strlen(sNBName) != 0)
235//              SaveNotebook /I /S=4  $sNBName
236//      endif
237End
238 
239Function V_WriteBrowserInfo(sString, vType, sNBName)
240        String sString
241        Variable vType
242        String sNBName
243 
244        if(strlen(sNBName) == 0)
245//              print sString
246                return 0
247        endif
248        DoWindow $sNBName
249        if(V_flag != 1)
250                NewNoteBook/F=0 /N=$sNBName /V=1 as sNBName
251        else
252                DoWindow/F $sNBName
253        endif
254        Notebook $sNBName selection={endOfFile, endOfFile}
255        if(vType == 1)          // a data folder
256//              Notebook $sNBName fstyle=1
257                Notebook $sNBName text=sString
258//              Notebook $sNBName fstyle=-1
259        else
260                Notebook $sNBName text=sString 
261        endif
262 
263End
264
265///////////////////////////////
266
267
268// given the folder, duplicate the data -> linear_data and generate the error
269Function V_MakeDataError(folderStr)
270        String folderStr
271       
272        SetDataFolder $folderStr
273        Wave data=data
274        Duplicate/O data linear_data                    // at this point, the data is still the raw data, and is linear_data
275       
276        // proper error for counting statistics, good for low count values too
277        // rather than just sqrt(n)
278        // see N. Gehrels, Astrophys. J., 303 (1986) 336-346, equation (7)
279        // for S = 1 in eq (7), this corresponds to one sigma error bars
280        Duplicate/O linear_data linear_data_error
281        linear_data_error = 1 + sqrt(linear_data + 0.75)                               
282        //
283       
284        SetDataFolder root:
285        return(0)
286End
287
288
289
290
291
292/////////////////////
293
294
295
296//testing procedure
297// TODO -- can't duplicate this with another proceudre, but if I change the name of the variable
298//   "newType" to "type", then when Raw_to_work() gets to CopyHDFToWorkFolder(), the KillDataFolder/Z
299//   line fails (but reports no error), then DuplicateDataFolder fails, and reports an error. Trying
300//   to simplify this condition, I can't duplicate the error for WM...
301Proc Add_to_Workfile(newtype, doadd)
302        String newtype,doadd
303        Prompt newtype,"WORK data type",popup,"SAM;EMP;BGD;ADJ;"
304        Prompt doadd,"Add to current WORK contents?",popup,"No;Yes;"
305       
306        //macro will take whatever is in RAW folder and "ADD" it to the folder specified
307        //in the popup menu
308       
309        //"add" = yes/no, don't add to previous runs
310        //switch here - two separate functions to avoid (my) confusion
311        Variable err// = Raw_to_work(newtype)
312        if(cmpstr(doadd,"No")==0)
313                //don't add to prev work contents, copy RAW contents to work and convert
314                err = Raw_to_work(newtype)
315        else
316                //yes, add RAW to the current work folder contents
317                err = Add_raw_to_work(newtype)
318        endif
319       
320        String newTitle = "WORK_"+newtype
321        DoWindow/F VSANS_Data
322        DoWindow/T VSANS_Data, newTitle
323        KillStrings/Z newTitle
324       
325        //need to update the display with "data" from the correct dataFolder
326        UpdateDisplayInformation(newtype)
327       
328End
329
330
331//
332//will copy the current contents of the RAW folder to the newType work folder
333//and do the geometric corrections and normalization to monitor counts
334//(the function Add_Raw_to_work(type) adds multiple runs together)
335//
336//the current display type is updated to newType (global)
337//
338Function Raw_to_work(newType)
339        String newType
340       
341        Variable deadTime,defmon,total_mon,total_det,total_trn,total_numruns,total_rtime
342        Variable ii,jj,itim,cntrate,dscale,scale,uscale
343        String destPath
344       
345        String fname = newType
346        String detStr
347        Variable ctTime
348
349        //initialize values before normalization
350        total_mon=0
351        total_det=0
352        total_trn=0
353        total_numruns=0
354        total_rtime=0
355       
356        //Not adding multiple runs, so wipe out the old contents of the work folder and
357        // replace with the contents of raw
358
359        destPath = "root:Packages:NIST:VSANS:" + newType
360       
361        //copy from current dir (RAW) to work, defined by newType
362        CopyHDFToWorkFolder("RAW",newType)
363       
364        // now work with the waves from the destination folder.
365        Variable/G $(destPath + ":gIsLogscale")=0                       //overwite flag in newType folder, data converted (above) to linear scale
366//      String/G $(destPath + ":fileList") = textread[0]                        //a list of names of the files in the work file (1)             //02JUL13
367       
368        // apply corrections ---
369        // switches to control what is done, don't do the transmission correction for the BGD measurement
370        // start with the DIV correction, before conversion to mm
371        // then do all of the other corrections, order doesn't matter.
372        // rescaling to default monitor counts however, must be LAST.
373
374// each correction must loop over each detector. tedious.
375       
376        // (1) DIV correction
377        // do this in terms of pixels.
378        // TODO : This must also exist at the time the first work folder is generated.
379        //   So it must be in the user folder at the start of the experiment, and defined.
380        NVAR gDoDIVCor = root:Packages:NIST:VSANS:Globals:gDoDIVCor
381        if (gDoDIVCor == 1)
382                // need extra check here for file existence
383                // if not in DIV folder, load.
384                // if unable to load, skip correction and report error (Alert?)
385                for(ii=0;ii<8;ii+=1)
386                        detStr = StringFromList(ii, ksDetectorListNoB, ";")
387                        Wave w = V_getDetectorDataW(fname,detStr)
388                        Wave w_err = V_getDetectorDataErrW(fname,detStr)
389                        Wave w_dt = V_getDetector_deadtime(fname,detStr)
390                        Print "Doing DIV correction for "+ detStr
391//                      DIVCorrection(fill this in)
392                       
393                endfor
394        else
395                Print "DIV correction not done"         // not an error since correction was unchecked
396        endif
397       
398        // (2) non-linear correction   
399        NVAR gDoNonLinearCor = root:Packages:NIST:VSANS:Globals:gDoNonLinearCor
400        // generate a distance matrix for each of the detectors
401        if (gDoNonLinearCor == 1)
402                for(ii=0;ii<8;ii+=1)
403                        detStr = StringFromList(ii, ksDetectorListNoB, ";")
404                        Wave w = V_getDetectorDataW(fname,detStr)
405                        Wave w_err = V_getDetectorDataErrW(fname,detStr)
406                        Wave w_dt = V_getDetector_deadtime(fname,detStr)
407                        Print "Doing Non-linear correction for "+ detStr
408//                      NonLinearCorrection(fill this in)
409                       
410                endfor
411        else
412                Print "Non-linear correction not done"
413        endif
414       
415        // (3) solid angle correction
416        NVAR gDoSolidAngleCor = root:Packages:NIST:VSANS:Globals:gDoSolidAngleCor
417        if (gDoSolidAngleCor == 1)
418                for(ii=0;ii<8;ii+=1)
419                        detStr = StringFromList(ii, ksDetectorListNoB, ";")
420                        Wave w = V_getDetectorDataW(fname,detStr)
421                        Wave w_err = V_getDetectorDataErrW(fname,detStr)
422                        Wave w_dt = V_getDetector_deadtime(fname,detStr)
423                        Print "Doing Solid Angle correction for "+ detStr
424//                      SolidAngleCorrection(fill this in)
425                       
426                endfor
427        else
428                Print "Solid Angle correction not done"
429        endif   
430       
431        // (4) dead time correction
432        // TODO: -- remove the hard-wired test
433        // -- test for correct operation
434        // -- loop over all of the detectors
435        // -- B detector is a special case
436        NVAR gDoDeadTimeCor = root:Packages:NIST:VSANS:Globals:gDoDeadTimeCor
437        ctTime = V_getCount_time(fname)
438        if (gDoDeadTimeCor == 1)
439                for(ii=0;ii<8;ii+=1)
440                        detStr = StringFromList(ii, ksDetectorListNoB, ";")
441                        Wave w = V_getDetectorDataW(fname,detStr)
442                        Wave w_err = V_getDetectorDataErrW(fname,detStr)
443                        Wave w_dt = V_getDetector_deadtime(fname,detStr)
444                        Print "Doing DeadTime correction for "+ detStr
445//                      DeadTimeCorrectionTubes(w,w_err,w_dt,ctTime)
446                                //deadtime corrections
447//      itim = integersread[2]
448//      cntrate = sum(data,-inf,inf)/itim               //use sum of detector counts rather than scaler value
449//      //TODO - do correct dead time correction for tubes
450//      deadtime = 1//DetectorDeadtime(textread[3],textread[9],dateAndTimeStr=textRead[1],dtime=realsRead[48])  //pick the correct deadtime
451//      dscale = 1/(1-deadTime*cntrate)
452//     
453       
454// dead time correction
455//      data *= dscale          //deadtime correction for everyone else, including NCNR
456//      data_err *= dscale
457
458                endfor
459        else
460                Print "Dead Time correction not done"
461        endif   
462       
463        // (5) angle-dependent tube shadowing
464        NVAR gDoTubeShadowCor = root:Packages:NIST:VSANS:Globals:gDoTubeShadowCor
465        if (gDoTubeShadowCor == 1)
466       
467        else
468                Print "Tube shadowing correction not done"
469        endif   
470               
471        // (6) angle dependent transmission correction
472        NVAR gDoTrans = root:Packages:NIST:VSANS:Globals:gDoTransmissionCor
473        if (gDoTrans == 1)
474                for(ii=0;ii<8;ii+=1)
475                        detStr = StringFromList(ii, ksDetectorListNoB, ";")
476                        Wave w = V_getDetectorDataW(fname,detStr)
477                        Wave w_err = V_getDetectorDataErrW(fname,detStr)
478                        Wave w_dt = V_getDetector_deadtime(fname,detStr)
479                        Print "Doing Large-angle transmission correction for "+ detStr
480//                      TransmissionCorrection(fill this in)
481                       
482                endfor
483        else
484                Print "Sample Transmission correction not done"
485        endif   
486       
487        // (7) normalize to default monitor counts
488        // TODO -- each detector is rescaled separately, but the rescaling factor is global (only one monitor!)
489        // TODO -- but there are TWO monitors - so how to switch?
490        // TODO -- what do I really need to save?
491        defmon=1e8                      //default monitor counts
492        for(ii=0;ii<8;ii+=1)
493                detStr = StringFromList(ii, ksDetectorListNoB, ";")
494                Wave w = V_getDetectorDataW(fname,detStr)
495                Wave w_err = V_getDetectorDataErrW(fname,detStr)
496                Variable monCt = V_getBeamMonNormData(fname)
497                Print "Doing monitor normalization for "+ detStr
498//                      MonitorNormalization(fill this in)
499        //scale the data to the default montor counts
500                scale = defmon/monCt
501                w *= scale
502                w_err *= scale          //assumes total monitor count is so large there is essentially no error
503               
504                // V_getBeamMonNormSaved_count()
505                // save the true monitor counts? save the scaling factor?
506                String path = "entry:instrument:beam_monitor_norm:saved_count"
507                Wave/Z savW = $("root:Packages:NIST:VSANS:"+fname+":entry:"+path)
508                savW[0] = scale
509        endfor
510       
511       
512        // (not done) angle dependent efficiency correction
513        NVAR doEfficiency = root:Packages:NIST:VSANS:Globals:gDoDetectorEffCor
514
515       
516// this function, in the past did the non-linear, solid angle, transmission, and efficiency corrections all at once
517//      DetCorr(data,data_err,realsread,doEfficiency,doTrans)           //the parameters are waves, and will be changed by the function
518
519
520       
521        //update totals to put in the work header (at the end of the function)
522//      total_mon += realsread[0]
523//
524//      total_det += dscale*realsread[2]
525//
526//      total_trn += realsread[39]
527//      total_rtime += integersread[2]
528//      total_numruns +=1
529//     
530
531        //all is done, except for the bookkeeping, updating the header information in the work folder
532
533//      integersread[3] = total_numruns                                         //numruns = 1
534//      realsread[1] = total_mon                        //save the true monitor count
535//      realsread[0] = defmon                                   //monitor ct = defmon
536//      realsread[2] = scale*total_det                  //scaled detector counts
537//     
538        //reset the current displaytype to "newtype"
539        String/G root:myGlobals:gDataDisplayType=newType
540       
541        //return to root folder (redundant)
542        SetDataFolder root:
543       
544        Return(0)
545End
546
547
548//will "ADD" the current contents of the RAW folder to the newType work folder
549//and will ADD the RAW contents to the existing content of the newType folder
550// - used when adding multiple runs together
551//(the function Raw_to_work(type) makes a fresh workfile)
552//
553//the current display type is updated to newType (global)
554Function Add_raw_to_work(newType)
555        String newType
556       
557        // NEW OCT 2014
558        // this corrects for adding raw data files with different attenuation   
559        // does nothing if the attenuation of RAW and destination are the same
560        NVAR doAdjustRAW_Atten = root:Packages:NIST:gDoAdjustRAW_Atten
561        if(doAdjustRAW_Atten)
562                Adjust_RAW_Attenuation(newType)
563        endif
564       
565        String destPath=""
566       
567        // if the desired workfile doesn't exist, let the user know, and just make a new one
568        if(WaveExists($("root:Packages:NIST:"+newType + ":data")) == 0)
569                Print "There is no old work file to add to - a new one will be created"
570                //call Raw_to_work(), then return from this function
571                Raw_to_Work(newType)
572                Return(0)               //does not generate an error - a single file was converted to work.newtype
573        Endif
574       
575        NVAR pixelsX = root:myGlobals:gNPixelsX
576        NVAR pixelsY = root:myGlobals:gNPixelsY
577       
578        //now make references to data in newType folder
579        DestPath="root:Packages:NIST:"+newType 
580        WAVE data=$(destPath +":linear_data")                   // these wave references point to the EXISTING work data
581        WAVE data_copy=$(destPath +":data")                     // these wave references point to the EXISTING work data
582        WAVE dest_data_err=$(destPath +":linear_data_error")                    // these wave references point to the EXISTING work data
583        WAVE/T textread=$(destPath + ":textread")
584        WAVE integersread=$(destPath + ":integersread")
585        WAVE realsread=$(destPath + ":realsread")
586       
587        Variable deadTime,defmon,total_mon,total_det,total_trn,total_numruns,total_rtime
588        Variable ii,jj,itim,cntrate,dscale,scale,uscale,wrk_beamx,wrk_beamy,xshift,yshift
589
590
591        defmon=1e8                      //default monitor counts
592       
593        //Yes, add to previous run(s) in work, that does exist
594        //use the actual monitor count run.savmon rather than the normalized monitor count
595        //in run.moncnt and unscale the work data
596       
597        total_mon = realsread[1]        //saved monitor count
598        uscale = total_mon/defmon               //unscaling factor
599        total_det = uscale*realsread[2]         //unscaled detector count
600        total_trn = uscale*realsread[39]        //unscaled trans det count
601        total_numruns = integersread[3] //number of runs in workfile
602        total_rtime = integersread[2]           //total counting time in workfile
603        //retrieve workfile beamcenter
604        wrk_beamx = realsread[16]
605        wrk_beamy = realsread[17]
606        //unscale the workfile data in "newType"
607        //
608        //check for log-scaling and adjust if necessary
609        // should not be needed now - using display flag instead
610//      ConvertFolderToLinearScale(newType)
611        //
612        //then unscale the data array
613        data *= uscale
614        dest_data_err *= uscale
615       
616        //DetCorr() has not been applied to the data in RAW , do it now in a local reference to the raw data
617        WAVE raw_data = $"root:Packages:NIST:RAW:linear_data"
618        WAVE raw_data_err = $"root:Packages:NIST:RAW:linear_data_error"
619        WAVE raw_reals =  $"root:Packages:NIST:RAW:realsread"
620        WAVE/T raw_text = $"root:Packages:NIST:RAW:textread"
621        WAVE raw_ints = $"root:Packages:NIST:RAW:integersread"
622       
623        //check for log-scaling of the raw data - make sure it's linear
624        // should not be needed now - using display flag instead
625//      ConvertFolderToLinearScale("RAW")
626       
627        // switches to control what is done, don't do the transmission correction for the BGD measurement
628        NVAR doEfficiency = root:Packages:NIST:gDoDetectorEffCorr
629        NVAR gDoTrans = root:Packages:NIST:gDoTransmissionCorr
630        Variable doTrans = gDoTrans
631        if(cmpstr("BGD",newtype) == 0)
632                doTrans = 0             //skip the trans correction for the BGD file but don't change the value of the global
633        endif   
634       
635        DetCorr(raw_data,raw_data_err,raw_reals,doEfficiency,doTrans)   //applies correction to raw_data, and overwrites it
636       
637        //deadtime corrections to raw data
638        // TODO - do the tube correction for dead time now
639        deadTime = 1//DetectorDeadtime(raw_text[3],raw_text[9],dateAndTimeStr=raw_text[1],dtime=raw_reals[48])          //pick the correct detector deadtime, switch on date too
640        itim = raw_ints[2]
641        cntrate = sum(raw_data,-inf,inf)/itim           //080802 use data sum, rather than scaler value
642        dscale = 1/(1-deadTime*cntrate)
643
644#if (exists("ILL_D22")==6)
645        Variable tubeSum
646        // for D22 detector might need to use cntrate/128 as it is the tube response
647        for(ii=0;ii<pixelsX;ii+=1)
648                //sum the counts in each tube
649                tubeSum = 0
650                for(jj=0;jj<pixelsY;jj+=1)
651                        tubeSum += data[jj][ii]
652                endfor
653                // countrate in tube ii
654                cntrate = tubeSum/itim
655                // deadtime scaling in tube ii
656                dscale = 1/(1-deadTime*cntrate)
657                // multiply data[ii][] by the dead time
658                raw_data[][ii] *= dscale
659                raw_data_err[][ii] *= dscale
660        endfor
661#else
662        // dead time correction on all other RAW data, including NCNR
663        raw_data *= dscale
664        raw_data_err *= dscale
665#endif
666
667        //update totals by adding RAW values to the local ones (write to work header at end of function)
668        total_mon += raw_reals[0]
669
670        total_det += dscale*raw_reals[2]
671
672        total_trn += raw_reals[39]
673        total_rtime += raw_ints[2]
674        total_numruns +=1
675       
676        //do the beamcenter shifting if there is a mismatch
677        //and then add the two data sets together, changing "data" since it is the workfile data
678        xshift = raw_reals[16] - wrk_beamx
679        yshift = raw_reals[17] - wrk_beamy
680       
681        If((xshift != 0) || (yshift != 0))
682                DoAlert 1,"Do you want to ignore the beam center mismatch?"
683                if(V_flag==1)
684                        xshift=0
685                        yshift=0
686                endif
687        endif
688       
689        If((xshift == 0) && (yshift == 0))              //no shift, just add them
690                data += raw_data                //deadtime correction has already been done to the raw data
691                dest_data_err = sqrt(dest_data_err^2 + raw_data_err^2)                  // error of the sum
692        Endif
693       
694        //scale the data to the default montor counts
695        scale = defmon/total_mon
696        data *= scale
697        dest_data_err *= scale
698       
699        // keep "data" and linear_data in sync in the destination folder
700        data_copy = data
701       
702        //all is done, except for the bookkeeping of updating the header info in the work folder
703        textread[1] = date() + " " + time()             //date + time stamp
704        integersread[3] = total_numruns                                         //numruns = more than one
705        realsread[1] = total_mon                        //save the true monitor count
706        realsread[0] = defmon                                   //monitor ct = defmon
707        integersread[2] = total_rtime                   // total counting time
708        realsread[2] = scale*total_det                  //scaled detector counts
709        realsread[39] = scale*total_trn                 //scaled transmission counts
710       
711        //Add the added raw filename to the list of files in the workfile
712        String newfile = ";" + raw_text[0]
713        SVAR oldList = $(destPath + ":fileList")
714        String/G $(destPath + ":fileList") = oldList + newfile
715       
716        //reset the current displaytype to "newtype"
717        String/G root:myGlobals:gDataDisplayType=newType
718       
719        //return to root folder (redundant)
720        SetDataFolder root:
721       
722        Return(0)
723End
724
725
726//used for adding DRK (beam shutter CLOSED) data to a workfile
727//force the monitor count to 1, since it's irrelevant
728// run data through normal "add" step, then unscale default monitor counts
729//to get the data back on a simple time basis
730//
731Function Raw_to_Work_NoNorm(type)
732        String type
733       
734        WAVE reals=$("root:Packages:NIST:RAW:realsread")
735        reals[1]=1              //true monitor counts, still in raw
736        Raw_to_work(type)
737        //data is now in "type" folder
738        WAVE data=$("root:Packages:NIST:"+type+":linear_data")
739        WAVE data_copy=$("root:Packages:NIST:"+type+":data")
740        WAVE data_err=$("root:Packages:NIST:"+type+":linear_data_error")
741        WAVE new_reals=$("root:Packages:NIST:"+type+":realsread")
742       
743        Variable norm_mon,tot_mon,scale
744       
745        norm_mon = new_reals[0]         //should be 1e8
746        tot_mon = new_reals[1]          //should be 1
747        scale= norm_mon/tot_mon
748       
749        data /= scale           //unscale the data
750        data_err /= scale
751       
752        // to keep "data" and linear_data in sync
753        data_copy = data
754       
755        return(0)
756End
757
758//used for adding DRK (beam shutter CLOSED) data to a workfile
759//force the monitor count to 1, since it's irrelevant
760// run data through normal "add" step, then unscale default monitor counts
761//to get the data back on a simple time basis
762//
763Function Add_Raw_to_Work_NoNorm(type)
764        String type
765       
766        WAVE reals=$("root:Packages:NIST:RAW:realsread")
767        reals[1]=1              //true monitor counts, still in raw
768        Add_Raw_to_work(type)
769        //data is now in "type" folder
770        WAVE data=$("root:Packages:NIST:"+type+":linear_data")
771        WAVE data_copy=$("root:Packages:NIST:"+type+":data")
772        WAVE data_err=$("root:Packages:NIST:"+type+":linear_data_error")
773        WAVE new_reals=$("root:Packages:NIST:"+type+":realsread")
774       
775        Variable norm_mon,tot_mon,scale
776       
777        norm_mon = new_reals[0]         //should be 1e8
778        tot_mon = new_reals[1]          //should be equal to the number of runs (1 count per run)
779        scale= norm_mon/tot_mon
780       
781        data /= scale           //unscale the data
782        data_err /= scale
783       
784        // to keep "data" and linear_data in sync
785        data_copy = data
786       
787        return(0)
788End
789
Note: See TracBrowser for help on using the repository browser.