source: sans/Release/trunk/NCNR_User_Procedures/SANS/Reduction/WorkFileUtils.ipf @ 374

Last change on this file since 374 was 374, checked in by srkline, 15 years ago

Merging r367+ into Release for version 6.003

File size: 41.6 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.0
4
5//*******************
6// Vers 1.2 100901
7//
8//*******************
9// Utility procedures for handling of workfiles (each is housed in a separate datafolder)
10//
11// - adding data to a workfile
12// - copying workfiles to another folder
13//
14// - absolute scaling
15// - DIV detector sensitivity corrections
16//
17// - the WorkFile Math panel for simple image math
18// -
19// - adding work.drk data without normalizing to monitor counts
20//***************************
21
22
23//testing procedure, not called anymore
24Proc Add_to_Workfile(type, add)
25        String type,add
26        Prompt type,"WORK data type",popup,"SAM;EMP;BGD"
27        Prompt add,"Add to current WORK contents?",popup,"No;Yes"
28       
29        //macro will take whatever is in RAW folder and "ADD" it to the folder specified
30        //in the popup menu
31       
32        //"add" = yes/no, don't add to previous runs
33        //switch here - two separate functions to avoid (my) confusion
34        Variable err
35        if(cmpstr(add,"No")==0)
36                //don't add to prev work contents, copy RAW contents to work and convert
37                err = Raw_to_work(type)
38        else
39                //yes, add RAW to the current work folder contents
40                err = Add_raw_to_work(type)
41        endif
42       
43        String newTitle = "WORK_"+type
44        DoWindow/F SANS_Data
45        DoWindow/T SANS_Data, newTitle
46        KillStrings/Z newTitle
47       
48        //need to update the display with "data" from the correct dataFolder
49        fRawWindowHook()
50       
51End
52
53//will "ADD" the current contents of the RAW folder to the newType work folder
54//and will ADD the RAW contents to the existing content of the newType folder
55// - used when adding multiple runs together
56//(the function Raw_to_work(type) makes a fresh workfile)
57//
58//the current display type is updated to newType (global)
59Function Add_raw_to_work(newType)
60        String newType
61               
62        String destPath=""
63       
64        // if the desired workfile doesn't exist, let the user know, and just make a new one
65        destPath = "root:"+newType + ":data"
66        if(WaveExists($destpath) == 0)
67                Print "There is no old work file to add to - a new one will be created"
68                //call Raw_to_work(), then return from this function
69                Raw_to_Work(newType)
70                Return(0)               //does not generate an error - a single file was converted to work.newtype
71        Endif
72       
73        //now make references to data in newType folder
74        DestPath="root:"+newType       
75        WAVE data=$(destPath +":data")                  // these wave references point to the EXISTING work data
76        WAVE/T textread=$(destPath + ":textread")
77        WAVE integersread=$(destPath + ":integersread")
78        WAVE realsread=$(destPath + ":realsread")
79       
80        Variable deadTime,defmon,total_mon,total_det,total_trn,total_numruns,total_rtime
81        Variable ii,jj,itim,cntrate,dscale,scale,uscale,wrk_beamx,wrk_beamy,xshift,yshift
82
83// 08/01 detector constants are now returned from a function, based on the detector type and beamline
84//      dt_ornl = 3.4e-6                //deadtime of Ordella detectors as of 30-AUG-99
85//      dt_ill=3.0e-6                   //Cerca detector deadtime constant as of 30-AUG-99
86
87        defmon=1e8                      //default monitor counts
88       
89        //Yes, add to previous run(s) in work, that does exist
90        //use the actual monitor count run.savmon rather than the normalized monitor count
91        //in run.moncnt and unscale the work data
92       
93        total_mon = realsread[1]        //saved monitor count
94        uscale = total_mon/defmon               //unscaling factor
95        total_det = uscale*realsread[2]         //unscaled detector count
96        total_trn = uscale*realsread[39]        //unscaled trans det count
97        total_numruns = integersread[3] //number of runs in workfile
98        total_rtime = integersread[2]           //total counting time in workfile
99        //retrieve workfile beamcenter
100        wrk_beamx = realsread[16]
101        wrk_beamy = realsread[17]
102        //unscale the workfile data in "newType"
103        //
104        //check for log-scaling and adjust if necessary
105        ConvertFolderToLinearScale(newType)
106        //
107        //then unscale the data array
108        data *= uscale
109       
110        //DetCorr() has not been applied to the data in RAW , do it now in a local reference to the raw data
111        WAVE raw_data = $"root:RAW:data"
112        WAVE raw_reals =  $"root:RAW:realsread"
113        WAVE/T raw_text = $"root:RAW:textread"
114        WAVE raw_ints = $"root:RAW:integersread"
115       
116        //check for log-scaling of the raw data - make sure it's linear
117        ConvertFolderToLinearScale("RAW")
118       
119        // switches to control what is done, don't do the transmission correction for the BGD measurement
120        NVAR doEfficiency = root:myGlobals:gDoDetectorEffCorr
121        NVAR gDoTrans = root:myGlobals:gDoTransmissionCorr
122        Variable doTrans = gDoTrans
123        if(cmpstr("BGD",newtype) == 0)
124                doTrans = 0             //skip the trans correction for the BGD file but don't change the value of the global
125        endif   
126       
127        DetCorr(raw_data,raw_reals,doEfficiency,doTrans)        //applies correction to raw_data, and overwrites it
128       
129        //if RAW data is ILL type detector, correct raw_data for same counts being written to 4 pixels
130        if(cmpstr(raw_text[9], "ILL   ") == 0 )         //text field in header is 6 characters "ILL---"
131                raw_data /= 4
132        endif
133       
134        //deadtime corrections to raw data
135        deadTime = DetectorDeadtime(raw_text[3],raw_text[9])            //pick the correct detector deadtime
136        itim = raw_ints[2]
137        cntrate = sum(raw_data,-inf,inf)/itim           //080802 use data sum, rather than scaler value
138        dscale = 1/(1-deadTime*cntrate)
139
140        //update totals by adding RAW values to the local ones (write to work header at end of function)
141        total_mon += raw_reals[0]
142        total_det += dscale*raw_reals[2]
143        total_trn += raw_reals[39]
144        total_rtime += raw_ints[2]
145        total_numruns +=1
146       
147        //do the beamcenter shifting if there is a mismatch
148        //and then add the two data sets together, changing "data" since it is the workfile data
149        xshift = raw_reals[16] - wrk_beamx
150        yshift = raw_reals[17] - wrk_beamy
151       
152        If((xshift != 0) || (yshift != 0))
153                DoAlert 1,"Do you want to ignore the beam center mismatch?"
154                if(V_flag==1)
155                        xshift=0
156                        yshift=0
157                endif
158        endif
159       
160        NVAR pixelsX = root:myGlobals:gNPixelsX
161        NVAR pixelsY = root:myGlobals:gNPixelsY
162       
163        If((xshift == 0) && (yshift == 0))              //no shift, just add them
164                data += dscale*raw_data         //do the deadtime correction on RAW here
165        else
166                //shift the beamcenter, then add
167                Make/O/N=1 $(destPath + ":noadd")               //needed to get noadd condition back from ShiftSum()
168                WAVE noadd = $(destPath + ":noadd")
169                Variable sh_sum                 //returned value
170                Print "BEAM CENTER MISMATCH - - BEAM CENTER WILL BE SHIFTED TO THIS FILE'S VALUE"
171                //ii,jj are just indices here, not physical locations - so [0,127] is fine
172                ii=0
173                do
174                        jj=0
175                        do
176                                //get the contribution of shifted data
177                                sh_sum = ShiftSum(data,ii,jj,xshift,yshift,noadd)
178                                if(noadd[0])
179                                        //don't do anything to data[][]
180                                else
181                                        //add the raw_data + shifted sum (and do the deadtime correction on both)
182                                        data[ii][jj] += dscale*(raw_data[ii][jj]+sh_sum)                //do the deadtime correction on RAW here
183                                Endif
184                                jj+=1
185                        while(jj<pixelsY)
186                        ii+=1
187                while(ii<pixelsX)
188        Endif
189       
190        //scale the data to the default montor counts
191        scale = defmon/total_mon
192        data *= scale
193       
194        //all is done, except for the bookkeeping of updating the header info in the work folder
195        textread[1] = date() + " " + time()             //date + time stamp
196        integersread[3] = total_numruns                                         //numruns = more than one
197        realsread[1] = total_mon                        //save the true monitor count
198        realsread[0] = defmon                                   //monitor ct = defmon
199        integersread[2] = total_rtime                   // total counting time
200        realsread[2] = scale*total_det                  //scaled detector counts
201        realsread[39] = scale*total_trn                 //scaled transmission counts
202       
203        //Add the added raw filename to the list of files in the workfile
204        String newfile = ";" + raw_text[0]
205        SVAR oldList = $(destPath + ":fileList")
206        String/G $(destPath + ":fileList") = oldList + newfile
207       
208        //reset the current displaytype to "newtype"
209        String/G root:myGlobals:gDataDisplayType=newType
210       
211        //return to root folder (redundant)
212        SetDataFolder root:
213       
214        Return(0)
215End
216
217//will copy the current contents of the RAW folder to the newType work folder
218//and do the geometric corrections and normalization to monitor counts
219//(the function Add_Raw_to_work(type) adds multiple runs together)
220//
221//the current display type is updated to newType (global)
222//
223Function Raw_to_work(newType)
224        String newType
225       
226        Variable deadTime,defmon,total_mon,total_det,total_trn,total_numruns,total_rtime
227        Variable ii,jj,itim,cntrate,dscale,scale,uscale,wrk_beamx,wrk_beamy
228        String destPath
229       
230// 08/01 detector constants are now returned from a function, based on the detector type and beamline
231//      dt_ornl = 3.4e-6                //deadtime of Ordella detectors as of 30-AUG-99
232//      dt_ill=3.0e-6                   //Cerca detector deadtime constant as of 30-AUG-99
233        defmon=1e8                      //default monitor counts
234       
235        //initialize values before normalization
236        total_mon=0
237        total_det=0
238        total_trn=0
239        total_numruns=0
240        total_rtime=0
241       
242        //Not adding multiple runs, so wipe out the old contents of the work folder and
243        // replace with the contents of raw
244
245        destPath = "root:" + newType
246       
247        //check for log-scaling of the RAW data and adjust if necessary
248        ConvertFolderToLinearScale("RAW")
249        //then continue
250
251        //copy from current dir (RAW) to work, defined by destpath
252        DestPath = "root:"+newType
253        Duplicate/O $"root:RAW:data",$(destPath + ":data")
254//      Duplicate/O $"root:RAW:vlegend",$(destPath + ":vlegend")
255        Duplicate/O $"root:RAW:textread",$(destPath + ":textread")
256        Duplicate/O $"root:RAW:integersread",$(destPath + ":integersread")
257        Duplicate/O $"root:RAW:realsread",$(destPath + ":realsread")
258        Variable/G $(destPath + ":gIsLogscale")=0                       //overwite flag in newType folder, data converted (above) to linear scale
259       
260        WAVE data=$(destPath + ":data")                         // these wave references point to the data in work
261        WAVE/T textread=$(destPath + ":textread")                       //that are to be directly operated on
262        WAVE integersread=$(destPath + ":integersread")
263        WAVE realsread=$(destPath + ":realsread")
264        String/G $(destPath + ":fileList") = textread[0]                        //a list of names of the files in the work file (1)
265       
266        //apply nonlinear, Jacobian corrections ---
267        // switches to control what is done, don't do the transmission correction for the BGD measurement
268        NVAR doEfficiency = root:myGlobals:gDoDetectorEffCorr
269        NVAR gDoTrans = root:myGlobals:gDoTransmissionCorr
270        Variable doTrans = gDoTrans
271        if(cmpstr("BGD",newtype) == 0)
272                doTrans = 0             //skip the trans correction for the BGD file but don't change the value of the global
273        endif
274       
275        DetCorr(data,realsread,doEfficiency,doTrans)            //the parameters are waves, and will be changed by the function
276       
277        //if ILL type detector, correct for same counts being written to 4 pixels
278        if(cmpstr(textread[9], "ILL   ") == 0 )         //text field in header is 6 characters "ILL---"
279                data /= 4
280        endif
281       
282        //deadtime corrections
283        itim = integersread[2]
284        cntrate = sum(data,-inf,inf)/itim               //use sum of detector counts rather than scaler value
285        deadtime = DetectorDeadtime(textread[3],textread[9])    //pick the correct deadtime
286        dscale = 1/(1-deadTime*cntrate)
287       
288        //update totals to put in the work header (at the end of the function)
289        total_mon += realsread[0]
290        total_det += dscale*realsread[2]
291        total_trn += realsread[39]
292        total_rtime += integersread[2]
293        total_numruns +=1
294       
295        // NO xcenter,ycenter shifting is done - this is the first (and only) file in the work folder
296       
297        //only ONE data file- no addition of multiple runs in this function, so data is
298        //just simply corrected for deadtime.
299        data *= dscale          //deadtime correction
300       
301        //scale the data to the default montor counts
302        scale = defmon/total_mon
303        data *= scale
304       
305        //all is done, except for the bookkeeping, updating the header information in the work folder
306        textread[1] = date() + " " + time()             //date + time stamp
307        integersread[3] = total_numruns                                         //numruns = 1
308        realsread[1] = total_mon                        //save the true monitor count
309        realsread[0] = defmon                                   //monitor ct = defmon
310        integersread[2] = total_rtime                   // total counting time
311        realsread[2] = scale*total_det                  //scaled detector counts
312        realsread[39] = scale*total_trn                 //scaled transmission counts
313       
314        //reset the current displaytype to "newtype"
315        String/G root:myGlobals:gDataDisplayType=newType
316       
317        //return to root folder (redundant)
318        SetDataFolder root:
319       
320        Return(0)
321End
322
323//used for adding DRK (beam shutter CLOSED) data to a workfile
324//force the monitor count to 1, since it's irrelevant
325// run data through normal "add" step, then unscale default monitor counts
326//to get the data back on a simple time basis
327//
328Function Raw_to_Work_NoNorm(type)
329        String type
330       
331        WAVE reals=$("root:RAW:realsread")
332        reals[1]=1              //true monitor counts, still in raw
333        Raw_to_work(type)
334        //data is now in "type" folder
335        WAVE data=$("root:"+type+":data")
336        WAVE new_reals=$("root:"+type+":realsread")
337       
338        Variable norm_mon,tot_mon,scale
339       
340        norm_mon = new_reals[0]         //should be 1e8
341        tot_mon = new_reals[1]          //should be 1
342        scale= norm_mon/tot_mon
343       
344        data /= scale           //unscale the data
345       
346        return(0)
347End
348
349//used for adding DRK (beam shutter CLOSED) data to a workfile
350//force the monitor count to 1, since it's irrelevant
351// run data through normal "add" step, then unscale default monitor counts
352//to get the data back on a simple time basis
353//
354Function Add_Raw_to_Work_NoNorm(type)
355        String type
356       
357        WAVE reals=$("root:RAW:realsread")
358        reals[1]=1              //true monitor counts, still in raw
359        Add_Raw_to_work(type)
360        //data is now in "type" folder
361        WAVE data=$("root:"+type+":data")
362        WAVE new_reals=$("root:"+type+":realsread")
363       
364        Variable norm_mon,tot_mon,scale
365       
366        norm_mon = new_reals[0]         //should be 1e8
367        tot_mon = new_reals[1]          //should be equal to the number of runs (1 count per run)
368        scale= norm_mon/tot_mon
369       
370        data /= scale           //unscale the data
371       
372        return(0)
373End
374
375//performs solid angle and non-linear detector corrections to raw data as it is "added" to a work folder
376//function is called by Raw_to_work() and Add_raw_to_work() functions
377//works on the actual data array, assumes that is is already on LINEAR scale
378//
379Function DetCorr(data,realsread,doEfficiency,doTrans)
380        Wave data,realsread
381        Variable doEfficiency,doTrans
382       
383        Variable xcenter,ycenter,x0,y0,sx,sx3,sy,sy3,xx0,yy0
384        Variable ii,jj,dtdist,dtdis2
385        Variable xi,xd,yd,rad,ratio,domega,xy
386        Variable lambda,trans
387       
388//      Print "...doing jacobian and non-linear corrections"
389
390        NVAR pixelsX = root:myGlobals:gNPixelsX
391        NVAR pixelsY = root:myGlobals:gNPixelsY
392       
393        //set up values to send to auxiliary trig functions
394        xcenter = pixelsX/2 + 0.5               // == 64.5 for 128x128 Ordela
395        ycenter = pixelsY/2 + 0.5               // == 64.5 for 128x128 Ordela
396
397        x0 = realsread[16]
398        y0 = realsread[17]
399        sx = realsread[10]
400        sx3 = realsread[11]
401        sy = realsread[13]
402        sy3 = realsread[14]
403       
404        dtdist = 1000*realsread[18]     //sdd in mm
405        dtdis2 = dtdist^2
406       
407        lambda = realsRead[26]
408        trans = RealsRead[4]
409       
410        xx0 = dc_fx(x0,sx,sx3,xcenter)
411        yy0 = dc_fy(y0,sy,sy3,ycenter)
412       
413
414        //waves to contain repeated function calls
415        Make/O/N=(pixelsX) fyy,xx,yy            //Assumes square detector !!!
416        ii=0
417        do
418                xi = ii
419                fyy[ii] = dc_fy(ii+1,sy,sy3,ycenter)
420                xx[ii] = dc_fxn(ii+1,sx,sx3,xcenter)
421                yy[ii] = dc_fym(ii+1,sy,sy3,ycenter)
422                ii+=1
423        while(ii<pixelsX)
424       
425        Make/O/N=(pixelsX,pixelsY) SolidAngle           // testing only
426       
427        ii=0
428        do
429                xi = ii
430                xd = dc_fx(ii+1,sx,sx3,xcenter)-xx0
431                jj=0
432                do
433                        yd = fyy[jj]-yy0
434                        //rad is the distance of pixel ij from the sample
435                        //domega is the ratio of the solid angle of pixel ij versus center pixel
436                        rad = sqrt(dtdis2 + xd^2 + yd^2)
437                        domega = rad/dtdist
438                        ratio = domega^3
439//                      solidAngle[ii][jj] = ratio              //testing only 
440                        xy = xx[ii]*yy[jj]
441                        data[ii][jj] *= xy*ratio
442                       
443                        // multiplicative correction for detector efficiency JBG memo det_eff_cor2.doc 3/20/07
444                        // correction inserted 11/2007 SRK
445                        // large angle detector efficiency correction is >= 1 and will "bump up" the highest angles
446                        // so divide here to get the correct answer (5/22/08 SRK)
447                        if(doEfficiency)
448                                data[ii][jj] /= DetEffCorr(lambda,dtdist,xd,yd)
449        //                      solidAngle[ii][jj] = DetEffCorr(lambda,dtdist,xd,yd)            //testing only
450                        endif
451                       
452                        // large angle transmission correction is <= 1 and will "bump up" the highest angles
453                        if(doTrans)
454                                data[ii][jj] /= LargeAngleTransmissionCorr(trans,dtdist,xd,yd)          //moved from 1D avg SRK 11/2007
455                                solidAngle[ii][jj] = LargeAngleTransmissionCorr(trans,dtdist,xd,yd)             //testing only
456                        endif
457                       
458                        jj+=1
459                while(jj<pixelsX)
460                ii+=1
461        while(ii<pixelsX)
462       
463        //clean up waves
464//      Killwaves/Z fyy,xx,yy
465       
466        Return(0)
467End
468
469//trig function used by DetCorr()
470Function dc_fx(x,sx,sx3,xcenter)
471        Variable x,sx,sx3,xcenter
472       
473        Variable result
474       
475        result = sx3*tan((x-xcenter)*sx/sx3)
476        Return(result)
477End
478
479//trig function used by DetCorr()
480Function dc_fy(y,sy,sy3,ycenter)
481        Variable y,sy,sy3,ycenter
482       
483        Variable result
484       
485        result = sy3*tan((y-ycenter)*sy/sy3)
486        Return(result)
487End
488
489//trig function used by DetCorr()
490Function dc_fxn(x,sx,sx3,xcenter)
491        Variable x,sx,sx3,xcenter
492       
493        Variable result
494       
495        result = (cos((x-xcenter)*sx/sx3))^2
496        Return(result)
497End
498
499//trig function used by DetCorr()
500Function dc_fym(y,sy,sy3,ycenter)
501        Variable y,sy,sy3,ycenter
502       
503        Variable result
504       
505        result = (cos((y-ycenter)*sy/sy3))^2
506        Return(result)
507End
508
509//distances passed in are in mm
510// dtdist is SDD
511// xd and yd are distances from the beam center to the current pixel
512//
513Function DetEffCorr(lambda,dtdist,xd,yd)
514        Variable lambda,dtdist,xd,yd
515       
516        Variable theta,cosT,ff,stAl,stHe
517       
518        theta = atan( (sqrt(xd^2 + yd^2))/dtdist )
519        cosT = cos(theta)
520       
521        stAl = 0.00967*lambda*0.8               //dimensionless, constants from JGB memo
522        stHe = 0.146*lambda*2.5
523       
524        ff = exp(-stAl/cosT)*(1-exp(-stHe/cosT)) / ( exp(-stAl)*(1-exp(-stHe)) )
525               
526        return(ff)
527End
528
529// DIVIDE the intensity by this correction to get the right answer
530Function LargeAngleTransmissionCorr(trans,dtdist,xd,yd)
531        Variable trans,dtdist,xd,yd
532
533        //angle dependent transmission correction
534        Variable uval,arg,cos_th,correction,theta
535       
536        ////this section is the trans_correct() VAX routine
537        if(trans<0.1)
538                Print "***transmission is less than 0.1*** and is a significant correction"
539        endif
540        if(trans==0)
541                Print "***transmission is ZERO*** and has been reset to 1.0 for the averaging calculation"
542                trans = 1
543        endif
544       
545        theta = atan( (sqrt(xd^2 + yd^2))/dtdist )              //theta at the input pixel
546       
547        //optical thickness
548        uval = -ln(trans)               //use natural logarithm
549
550//      theta = 2*asin(lambda*qval/(4*pi))
551
552        cos_th = cos(theta)
553        arg = (1-cos_th)/cos_th
554        if((uval<0.01) || (cos_th>0.99))                //OR
555                //small arg, approx correction
556                correction= 1-0.5*uval*arg
557        else
558                //large arg, exact correction
559                correction = (1-exp(-uval*arg))/(uval*arg)
560        endif
561
562        //end of transmission/pathlength correction
563
564        return(correction)
565end
566
567//******************
568//direct port of the FORTRAN code for calculating the weighted
569//shifted element to add when beam centers in data headers do not match
570//(indices updated to [0,n-1] indexing rather than (1,n) of fortran
571//
572// as of IGOR 4.0, could be rewritten to pass-by-reference noadd, rather than wave, but the function
573// is so little used, it's not worth the time
574Function ShiftSum(DATA,ip,jp,XSHIFT,YSHIFT,noadd)
575        Wave data
576        Variable ip,jp,xshift,yshift
577        Wave noadd
578//
579//       COMPUTE WEIGHTED OFFSET ELEMENT SUM FOR USE IN SANS DATA
580//       ANALYSIS MODULES.
581//
582// "data" wave passed in is the current contents of the work file
583// sum_val is the return value of the function
584// "noadd" is passed back to the calling function as a one-point wave
585
586        Variable XDELTA,YDELTA,kk,II,JJ,ISHIFT,JSHIFT,sum_val
587        Make/O/N=4 iii,jjj,a
588
589//       -----------------------------------------------------------------
590
591        ISHIFT = trunc(XSHIFT)          // INTEGER PART, trunc gives int closest in dierction of zero
592        XDELTA = XSHIFT - ISHIFT        //FRACTIONAL PART.
593        JSHIFT = trunc(YSHIFT)
594        YDELTA = YSHIFT - JSHIFT
595        II = ip + ISHIFT
596        JJ = jp + JSHIFT
597
598//       SHIFT IS DEFINED AS A VECTOR ANCHORED AT THE STATIONARY CENTER
599//       AND POINTING TO THE MOVABLE CENTER.  THE MOVABLE FIELD IS THUS
600//       ACTUALLY MOVED BY -SHIFT.
601//
602        IF ((XDELTA>= 0) && (YDELTA >= 0))              // CASE I ---- "%&" is "and"
603                III[0] = II
604                JJJ[0] = JJ
605                III[1] = II + 1
606                JJJ[1] = JJ
607                III[2] = II + 1
608                JJJ[2] = JJ + 1
609                III[3] = II
610                JJJ[3] = JJ + 1
611                A[0] = (1. - XDELTA)*(1. - YDELTA)
612                A[1] = XDELTA*(1. - YDELTA)
613                A[2] = XDELTA*YDELTA
614                A[3] = (1. - XDELTA)*YDELTA
615        Endif
616        IF ((XDELTA >= 0) && (YDELTA < 0))              // CASE II.
617                III[0] = II
618                JJJ[0] = JJ
619                III[1] = II
620                JJJ[1] = JJ - 1
621                III[2] = II + 1
622                JJJ[2] = JJ - 1
623                III[3] = II + 1
624                JJJ[3] = JJ
625                A[0] = (1. - XDELTA)*(1. + YDELTA)
626                A[1] = (1. - XDELTA)*(-YDELTA)
627                A[2] = XDELTA*(-YDELTA)
628                A[3] = XDELTA*(1. + YDELTA)
629        Endif
630        IF ((XDELTA < 0) && (YDELTA >= 0))              // CASE III.
631                III[0] = II
632                JJJ[0] = JJ
633                III[1] = II
634                JJJ[1] = JJ + 1
635                III[2] = II - 1
636                JJJ[2] = JJ + 1
637                III[3] = II - 1
638                JJJ[3] = JJ
639                A[0] = (1. + XDELTA)*(1 - YDELTA)
640                A[1] = (1. + XDELTA)*YDELTA
641                A[2] = -XDELTA*YDELTA
642                A[3] = -XDELTA*(1. - YDELTA)
643        Endif
644        IF ((XDELTA < 0) && (YDELTA < 0))               //CASE IV.
645                III[0] = II
646                JJJ[0] = JJ
647                III[1] = II - 1
648                JJJ[1] = JJ
649                III[2] = II - 1
650                JJJ[2] = JJ - 1
651                III[3] = II
652                JJJ[3] = JJ - 1
653                A[0] = (1. + XDELTA)*(1. + YDELTA)
654                A[1] = -XDELTA*(1. + YDELTA)
655                A[2] = (-XDELTA)*(-YDELTA)
656                A[3] = (1. + XDELTA)*(-YDELTA)
657        Endif
658
659        NVAR pixelsX = root:myGlobals:gNPixelsX
660        NVAR pixelsY = root:myGlobals:gNPixelsY
661//check to see if iii[0],jjj[0] are valid detector elements, in [0,127]
662//if not set noadd[0] to 1, to let calling routine know NOT to add
663//        CALL TESTIJ(III(1),JJJ(1),OKIJ)
664        NOADD[0] = 0
665        if( (iii[0]<0) || (iii[0]>(pixelsX-1)) )
666                noadd[0] = 1
667        endif
668        if((jjj[0]<0) || (jjj[0]>(pixelsY-1)) )
669                noadd[0] = 1
670        endif
671       
672
673       
674        sum_val = 0.
675        kk = 0
676        Do
677                IF(JJJ[kk] == pixelsX)
678                        //do nothing
679                else
680                        sum_val += A[kk]*DATA[III[kk]][JJJ[kk]]
681                endif
682                kk+=1
683        while(kk<4)
684       
685        //clean up waves
686        KillWaves/z iii,jjj,a
687       
688        RETURN (sum_val)
689       
690End             //function ShiftSum
691
692//************************
693//unused testing procedure, may not be up-to-date with other procedures
694//check before re-implementing
695//
696Proc DIV_a_Workfile(type)
697        String type
698        Prompt type,"WORK data type",popup,"COR;SAM;EMP;BGD"
699       
700        //macro will take whatever is in SELECTED folder and DIVide it by the current
701        //contents of the DIV folder - the function will check for existence
702        //before proceeding
703       
704        Variable err
705        err = Divide_work(type)         //returns err = 1 if data doesn't exist in specified folders
706       
707        if(err)
708                Abort "error in Divide_work"
709        endif
710       
711        //contents are always dumped to CAL
712        type = "CAL"
713       
714        String newTitle = "WORK_"+type
715        DoWindow/F SANS_Data
716        DoWindow/T SANS_Data, newTitle
717        KillStrings/Z newTitle
718       
719        //need to update the display with "data" from the correct dataFolder
720        //reset the current displaytype to "type"
721        String/G root:myGlobals:gDataDisplayType=Type
722       
723        fRawWindowHook()
724       
725End
726
727//function will divide the contents of "type" folder with the contents of
728//the DIV folder
729// all data is converted to linear scale for the calculation
730//
731Function Divide_work(type)
732        String type
733       
734        //check for existence of data in type and DIV
735        // if the desired workfile doesn't exist, let the user know, and abort
736        String destPath=""
737        destPath = "root:"+Type + ":data"
738        if(WaveExists($destpath) == 0)
739                Print "There is no work file in "+type+"--Aborting"
740                Return(1)               //error condition
741        Endif
742        //check for DIV
743        // if the DIV workfile doesn't exist, let the user know,and abort
744        destPath = "root:DIV:data"
745        if(WaveExists($destpath) == 0)
746                Print "There is no work file in DIV --Aborting"
747                Return(1)               //error condition
748        Endif
749        //files exist, proceed
750       
751        //check for log-scaling of the "DIV" data and adjust if necessary
752        ConvertFolderToLinearScale("DIV")
753       
754        //copy type information to CAL, wiping out the old contents of the CAL folder first
755       
756        //destPath = "root:CAL"
757        //SetDataFolder destPath
758        //KillWaves/A/Z                 //get rid of the old data in CAL folder
759
760        //check for log-scaling of the "type" data and adjust if necessary
761        ConvertFolderToLinearScale(type)
762        //then continue
763
764        //copy from current dir (type)=destPath to CAL, overwriting CAL contents
765        destPath = "root:" + type
766        Duplicate/O $(destPath + ":data"),$"root:CAL:data"
767//      Duplicate/O $(destPath + ":vlegend"),$"root:CAL:vlegend"
768        Duplicate/O $(destPath + ":textread"),$"root:CAL:textread"
769        Duplicate/O $(destPath + ":integersread"),$"root:CAL:integersread"
770        Duplicate/O $(destPath + ":realsread"),$"root:CAL:realsread"
771        //need to save a copy of filelist string too (from the current type folder)
772        SVAR oldFileList = $(destPath + ":fileList")
773
774        //now switch to reference waves in CAL folder
775        destPath = "root:CAL"
776        //make appropriate wave references
777        Wave data=$(destPath + ":data")                                 // these wave references point to the data in CAL
778        Wave/t textread=$(destPath + ":textread")                       //that are to be directly operated on
779        Wave integersread=$(destPath + ":integersread")
780        Wave realsread=$(destPath + ":realsread")
781        Variable/G $(destPath + ":gIsLogScale")=0                       //make new flag in CAL folder, data is linear scale
782        //need to copy filelist string too
783        String/G $(destPath + ":fileList") = oldFileList
784
785        Wave div_data = $"root:DIV:data"                //hard-wired in....
786        //do the division, changing data in CAL
787        data /= div_data
788       
789        //update CAL header
790        textread[1] = date() + " " + time()             //date + time stamp
791       
792        Return(0)
793End
794
795//test procedure, not called anymore
796Proc AbsoluteScaling(type,c0,c1,c2,c3,c4,c5)
797        String type
798        Variable c0=1,c1=0.1,c2=0.95,c3=0.1,c4=1,c5=32.0
799        Prompt type,"WORK data type",popup,"CAL;COR;SAM"
800        Prompt c0, "Sample Transmission"
801        Prompt c1, "Sample Thickness (cm)"
802        Prompt c2, "Standard Transmission"
803        Prompt c3, "Standard Thickness (cm)"
804        Prompt c4, "I(0) from standard fit (normalized to 1E8 monitor cts)"
805        Prompt c5, "Standard Cross-Section (cm-1)"
806
807        Variable err
808        //call the function to do the math
809        //data from "type" will be scaled and deposited in ABS
810        err = Absolute_Scale(type,c0,c1,c2,c3,c4,c5)
811       
812        if(err)
813                Abort "Error in Absolute_Scale()"
814        endif
815       
816        //contents are always dumped to ABS
817        type = "ABS"
818       
819        String newTitle = "WORK_"+type
820        DoWindow/F SANS_Data
821        DoWindow/T SANS_Data, newTitle
822        KillStrings/Z newTitle
823       
824        //need to update the display with "data" from the correct dataFolder
825        //reset the current displaytype to "type"
826        String/G root:myGlobals:gDataDisplayType=Type
827       
828        fRawWindowHook()
829       
830End
831
832//s_ is the standard
833//w_ is the "work" file
834//both are work files and should already be normalized to 10^8 monitor counts
835Function Absolute_Scale(type,w_trans,w_thick,s_trans,s_thick,s_izero,s_cross)
836        String type
837        Variable w_trans,w_thick,s_trans,s_thick,s_izero,s_cross
838       
839        //convert the "type" data to absolute scale using the given standard information
840        //copying the "type" waves to ABS
841       
842        //check for existence of data, rescale to linear if needed
843        String destPath
844        //check for "type"
845        destPath = "root:"+Type + ":data"
846        if(WaveExists($destpath) == 0)
847                Print "There is no work file in "+type+"--Aborting"
848                Return(1)               //error condition
849        Endif
850        //check for log-scaling of the "type" data and adjust if necessary
851        destPath = "root:"+Type
852        NVAR gIsLogScale = $(destPath + ":gIsLogScale")
853        if(gIsLogScale)
854                Duplicate/O $(destPath + ":linear_data") $(destPath + ":data")//back to linear scale
855                Variable/G $(destPath + ":gIsLogScale")=0       //the "type" data is not logscale anymore
856        endif
857       
858        //copy "oldtype" information to ABS
859        //overwriting out the old contents of the ABS folder (/O option in Duplicate)
860        //copy over the waves data,vlegend,text,integers,reals(read)
861
862        String oldType= "root:"+type            //this is where the data to be absoluted is
863        //copy from current dir (type) to ABS, defined by destPath
864        Duplicate/O $(oldType + ":data"),$"root:ABS:data"
865//      Duplicate/O $(oldType + ":vlegend"),$"root:ABS:vlegend"
866        Duplicate/O $(oldType + ":textread"),$"root:ABS:textread"
867        Duplicate/O $(oldType + ":integersread"),$"root:ABS:integersread"
868        Duplicate/O $(oldType + ":realsread"),$"root:ABS:realsread"
869        //need to save a copy of filelist string too (from the current type folder)
870        SVAR oldFileList = $(oldType + ":fileList")
871        //need to copy filelist string too
872        String/G $"root:ABS:fileList" = oldFileList
873       
874        //now switch to ABS folder
875        //make appropriate wave references
876        WAVE data=$"root:ABS:data"                                      // these wave references point to the "type" data in ABS
877        WAVE/T textread=$"root:ABS:textread"                    //that are to be directly operated on
878        WAVE integersread=$"root:ABS:integersread"
879        WAVE realsread=$"root:ABS:realsread"
880        Variable/G $"root:ABS:gIsLogscale"=0                    //make new flag in ABS folder, data is linear scale
881       
882        //do the actual absolute scaling here, modifying the data in ABS
883        Variable defmon = 1e8,w_moncount,s1,s2,s3,s4
884       
885        w_moncount = realsread[0]               //monitor count in "type"
886        if(w_moncount == 0)
887                //zero monitor counts will give divide by zero ---
888                DoAlert 0,"Total monitor count in data file is zero. No rescaling of data"
889                Return(1)               //report error
890        Endif
891       
892        //calculate scale factor
893        s1 = defmon/realsread[0]                //[0] is monitor count (s1 should be 1)
894        s2 = s_thick/w_thick
895        s3 = s_trans/w_trans
896        s4 = s_cross/s_izero
897       
898        data *= s1*s2*s3*s4
899       
900        //********* 15APR02
901        // DO NOt correct for atenuators here - the COR step already does this, putting all of the data one equal
902        // footing (zero atten) before doing the subtraction.
903        //
904        //Print "ABS data multiplied by  ",s1*s2*s3*s4/attenFactor
905       
906        //update the ABS header information
907        textread[1] = date() + " " + time()             //date + time stamp
908       
909        Return (0) //no error
910End
911
912//*************
913// start of section of functions used for workfile math panel
914//*************
915
916
917//function will copy the contents of oldtype folder to newtype folder
918//converted to linear scale before copying
919//******data in newtype is overwritten********
920//
921Function CopyWorkContents(oldtype,newtype)
922        String oldtype,newtype
923       
924        //check for existence of data in oldtype
925        // if the desired workfile doesn't exist, let the user know, and abort
926        String destPath=""
927        destPath = "root:"+oldType + ":data"
928        if(WaveExists($destpath) == 0)
929                Print "There is no work file in "+oldtype+"--Aborting"
930                Return(1)               //error condition
931        Endif
932       
933        //check for log-scaling of the "type" data and adjust if necessary
934        ConvertFolderToLinearScale(oldtype)
935        Fix_LogLinButtonState(0)                //make sure the button reflects the new linear scaling
936        //then continue
937
938        //copy from current dir (type)=destPath to newtype, overwriting newtype contents
939        destPath = "root:" + oldtype
940        Duplicate/O $(destPath + ":data"),$("root:"+newtype+":data")
941        Duplicate/O $(destPath + ":textread"),$("root:"+newtype+":textread")
942        Duplicate/O $(destPath + ":integersread"),$("root:"+newtype+":integersread")
943        Duplicate/O $(destPath + ":realsread"),$("root:"+newtype+":realsread")
944        //need to save a copy of filelist string too (from the current type folder)
945        SVAR oldFileList = $(destPath + ":fileList")
946
947        //now switch to reference waves in newtype folder
948        destPath = "root:"+newtype
949        Variable/G $(destPath + ":gIsLogScale")=0                       //make new flag in newtype folder, data is linear scale
950        //need to copy filelist string too
951        String/G $(destPath + ":fileList") = oldFileList
952
953        Return(0)
954End
955
956//Entry procedure from main panel
957//
958Proc CopyWorkFolder(oldType,newType)
959        String oldType,newType
960        Prompt oldType,"Source WORK data type",popup,"SAM;EMP;BGD;DIV;COR;CAL;RAW;ABS;STO;SUB;DRK;"
961        Prompt newType,"Destination WORK data type",popup,"SAM;EMP;BGD;DIV;COR;CAL;RAW;ABS;STO;SUB;DRK;"
962//      Prompt oldType,"Source WORK data type",popup,"AAA;BBB;CCC;DDD;EEE;FFF;GGG;"
963//      Prompt newType,"Destination WORK data type",popup,"AAA;BBB;CCC;DDD;EEE;FFF;GGG;"
964
965        // data folder "old" will be copied to "new" (and will overwrite)
966        CopyWorkContents(oldtype,newtype)
967End
968
969//initializes datafolder and constants necessary for the workfilemath panel
970//
971Proc Init_WorkMath()
972        //create the data folder
973        //String str = "AAA;BBB;CCC;DDD;EEE;FFF;GGG;"
974        String str = "File_1;File_2;Result;"
975        NewDataFolder/O/S root:myGlobals:WorkMath
976        String/G gFolderList=str
977        Variable ii=0,num=itemsinlist(str)
978        do
979                Execute "NewDataFolder/O "+StringFromList(ii, str ,";")
980                ii+=1
981        while(ii<num)
982        Variable/G const1=1,const2=1
983       
984        SetDataFolder root:
985End
986
987//entry procedure to invoke the workfilemath panel, initializes if needed
988//
989Proc Show_WorkMath_Panel()
990        DoWindow/F WorkFileMath
991        if(V_flag==0)
992                Init_WorkMath()
993                WorkFileMath()
994        Endif
995End
996
997//attempts to perform the selected math operation based on the selections in the panel
998// aborts with an error condition if operation can't be completed
999// or puts the final answer in the Result folder, and displays the selected data
1000//
1001Function WorkMath_DoIt_ButtonProc(ctrlName) : ButtonControl
1002        String ctrlName
1003
1004        String str1,str2,oper,dest = "Result"
1005        String pathStr,workMathStr="myGlobals:WorkMath:"
1006       
1007        //get the panel selections (these are the names of the files on disk)
1008        PathInfo catPathName
1009        pathStr=S_Path
1010        ControlInfo popup0
1011        str1=S_Value
1012        ControlInfo popup1
1013        str2=S_Value
1014        ControlInfo popup2
1015        oper=S_Value
1016       
1017        //check that something has been selected for operation and destination
1018        if(cmpstr(oper,"Operation")==0)
1019                Abort "Select a math operand from the popup"
1020        Endif
1021
1022        //constants from globals
1023        NVAR const1=root:myGlobals:WorkMath:const1
1024        NVAR const2=root:myGlobals:WorkMath:const2
1025        Printf "(%g)%s %s (%g)%s = %s\r", const1,str1,oper,const2,str2,dest
1026        //check for proper folders (all 3 must be different)
1027       
1028        //load the data in here...
1029        //set #1
1030        Load_NamedASC_File(pathStr+str1,workMathStr+"File_1")
1031       
1032        NVAR pixelsX = root:myGlobals:gNPixelsX
1033        NVAR pixelsY = root:myGlobals:gNPixelsY
1034       
1035        WAVE/Z data1=$("root:"+workMathStr+"File_1:data")
1036        If(cmpstr(str2,"UNIT MATRIX")==0)
1037                Make/O/N=(pixelsX,pixelsY) root:myGlobals:WorkMath:data         //don't put in File_2 folder
1038                Wave/Z data2 =  root:myGlobals:WorkMath:data                    //it's not real data!
1039                data2=1
1040        else
1041                //Load set #2
1042                Load_NamedASC_File(pathStr+str2,workMathStr+"File_2")
1043                WAVE/Z data2=$("root:"+workMathStr+"File_2:data")
1044        Endif
1045
1046        ///////
1047       
1048        //now that we know that data exists, convert each of the operands to linear scale
1049        ConvertFolderToLinearScale(workMathStr+"File_1")
1050        If(cmpstr(str2,"UNIT MATRIX")!=0)
1051                ConvertFolderToLinearScale(workMathStr+"File_2")                //don't need to convert unit matrix to linear
1052        endif
1053        //copy contents of str1 folder to dest and create the wave ref (it will exist)
1054        CopyWorkContents(workMathStr+"File_1",workMathStr+dest)
1055        WAVE/Z destData=$("root:"+workMathStr+dest+":data")
1056       
1057        //dispatch
1058        strswitch(oper)
1059                case "*":               //multiplication
1060                        destData = const1*data1 * const2*data2
1061                        break   
1062                case "_":               //subtraction
1063                        destData = const1*data1 - const2*data2
1064                        break
1065                case "/":               //division
1066                        destData = (const1*data1) / (const2*data2)
1067                        break
1068                case "+":               //addition
1069                        destData = const1*data1 + const2*data2
1070                        break                   
1071        endswitch
1072       
1073        //show the result
1074        WorkMath_Display_PopMenuProc("",0,"Result")
1075End
1076
1077// closes the panel and kills the data folder when done
1078//
1079Function WorkMath_Done_ButtonProc(ctrlName) : ButtonControl
1080        String ctrlName
1081       
1082        DoAlert 1,"Closing the panel will kill all of the data you have loaded into memory. Do you want to continue?"
1083        If(V_Flag==2)
1084                return(0)               //don't do anything
1085        Endif
1086        //kill the panel
1087        DoWindow/K WorkFileMath
1088        //wipe out the data folder of globals
1089        SVAR dataType=root:myGlobals:gDataDisplayType
1090        if(strsearch(dataType, "myGlobals", 0 ) != -1)          //kill the SANS_Data graph if needed
1091                DoWindow/K SANS_Data
1092        Endif
1093        KillDataFolder root:myGlobals:WorkMath
1094End
1095
1096// loads data into the specified folder
1097//
1098// currently unused since button has been removed from panel
1099//
1100Function WorkMath_Load_ButtonProc(ctrlName) : ButtonControl
1101        String ctrlName
1102       
1103        String destStr=""
1104        SVAR folderList=root:myGlobals:WorkMath:gFolderList
1105        Prompt destStr,"Select the destination folder",popup,folderList
1106        DoPrompt "Folder for ASC Load",destStr
1107       
1108        if(V_flag==1)
1109                return(1)               //user abort, do nothing
1110        Endif
1111       
1112        String destFolder = "myGlobals:WorkMath:"+destStr
1113       
1114        Load_ASC_File("Pick the ASC file",destFolder)
1115End
1116
1117// changes the display of the SANS_Data window based on popped data type
1118// first loads in the data from the File1 or File2 popup as needed
1119// then displays the selcted dataset, if it exists
1120// makes use of procedure from DisplayUtils
1121//
1122// - Always replaces File1 or File2 with fresh data from disk
1123//
1124Function WorkMath_Display_PopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
1125        String ctrlName
1126        Variable popNum
1127        String popStr
1128       
1129        String folder="myGlobals:WorkMath:",pathStr,str1
1130
1131        PathInfo catPathName
1132        pathStr=S_Path
1133       
1134        //if display result, just do it and return
1135        if(cmpstr(popStr,"Result")==0)
1136                Execute "ChangeDisplay(\""+folder+popstr+"\")"
1137                return(0)
1138        endif
1139        // if file1 or file2, load in the data and display
1140        if(cmpstr(popStr,"File_1")==0)
1141                ControlInfo popup0
1142                str1 = S_Value
1143        Endif
1144        if(cmpstr(popStr,"File_2")==0)
1145                ControlInfo popup1
1146                str1 = S_Value
1147        Endif
1148        //don't load or display the unit matrix
1149        Print str1
1150        if(cmpstr(str1,"UNIT MATRIX")!=0)
1151                Load_NamedASC_File(pathStr+str1,folder+popStr)
1152                //change the display
1153                Execute "ChangeDisplay(\""+folder+popstr+"\")"
1154        endif
1155        return(0)       
1156End
1157
1158//simple panel to do workfile arithmetic
1159//
1160Proc WorkFileMath()
1161        PauseUpdate; Silent 1           // building window...
1162        NewPanel /W=(610,211,880,490)/K=2 as "Work File Math"           // replace /K=2 flag
1163        DoWindow/C WorkFileMath
1164        ModifyPanel cbRGB=(47802,54484,6682)
1165        ModifyPanel fixedSize=1
1166        SetDrawLayer UserBack
1167        DrawLine 6,166,214,166
1168        SetDrawEnv fstyle= 4
1169        DrawText 10,34,"File #1:"
1170        SetDrawEnv fstyle= 4
1171        DrawText 13,129,"File #2:"
1172        DrawText 78,186,"= Result"
1173        Button button0,pos={28,245},size={50,20},proc=WorkMath_DoIt_ButtonProc,title="Do It"
1174        Button button0,help={"Performs the specified arithmetic"}
1175        Button button1,pos={183,245},size={50,20},proc=WorkMath_Done_ButtonProc,title="Done"
1176        Button button1,help={"Closes the panel"}
1177//      Button button2,pos={30,8},size={110,20},proc=WorkMath_Load_ButtonProc,title="Load ASC Data"
1178//      Button button2,help={"Loads ASC data files into the specified folder"}
1179        Button button3,pos={205,8},size={25,20},proc=ShowWorkMathHelp,title="?"
1180        Button button3,help={"Show help file for math operations on 2-D data sets"}
1181        SetVariable setvar0,pos={9,46},size={70,15},title=" "
1182        SetVariable setvar0,limits={-Inf,Inf,0},value= root:myGlobals:WorkMath:const1
1183        SetVariable setvar0,help={"Multiplicative constant for the first dataset"}
1184        PopupMenu popup0,pos={89,44},size={84,20},title="X  "
1185        PopupMenu popup0,mode=1,popvalue="1st Set",value= ASC_FileList()
1186        PopupMenu popup0,help={"Selects the first dataset for the operation"}
1187        PopupMenu popup1,pos={93,136},size={89,20},title="X  "
1188        PopupMenu popup1,mode=1,popvalue="2nd Set",value= "UNIT MATRIX;"+ASC_FileList()
1189        PopupMenu popup1,help={"Selects the second dataset for the operation"}
1190        PopupMenu popup2,pos={50,82},size={70,20},title="Operator  "
1191        PopupMenu popup2,mode=3,popvalue="Operation",value= #"\"+;_;*;/;\""
1192        PopupMenu popup2,help={"Selects the mathematical operator"}
1193        SetVariable setvar1,pos={13,139},size={70,15},title=" "
1194        SetVariable setvar1,limits={-Inf,Inf,0},value= root:myGlobals:WorkMath:const2
1195        SetVariable setvar1,help={"Multiplicative constant for the second dataset"}
1196//      PopupMenu popup3,pos={27,167},size={124,20},title=" = Destination"
1197//      PopupMenu popup3,mode=1,popvalue="Destination",value= root:myGlobals:WorkMath:gFolderList
1198//      PopupMenu popup3,help={"Selects the destination folder"}
1199        PopupMenu popup4,pos={55,204},size={103,20},proc=WorkMath_Display_PopMenuProc,title="Display"
1200        PopupMenu popup4,mode=3,value= "File_1;File_2;Result;"
1201        PopupMenu popup4,help={"Displays the data in the specified folder"}
1202EndMacro
1203
1204//jump to the help topic
1205Function ShowWorkMathHelp(ctrlName) : ButtonControl
1206        String ctrlName
1207        DisplayHelpTopic/Z/K=1 "SANS Data Reduction Tutorial[2-D Work File Arithmetic]"
1208        if(V_flag !=0)
1209                DoAlert 0,"The SANS Data Reduction Tutorial Help file could not be found"
1210        endif
1211End
1212
1213//utility function to clear the contents of a data folder
1214//won't clear data that is in use -
1215//
1216Function ClearDataFolder(type)
1217        String type
1218       
1219        SetDataFolder $("root:"+type)
1220        KillWaves/a/z
1221        KillStrings/a/z
1222        KillVariables/a/z
1223        SetDataFolder root:
1224End
1225
1226
1227
1228//fileStr must be the FULL path and filename on disk
1229//destFolder is the path to the Igor folder where the data is to be deposited
1230// - "myGlobals:WorkMath:File_1" for example, compatible with SANS_Data display type
1231//
1232Function Load_NamedASC_File(fileStr,destFolder)
1233        String fileStr,destFolder
1234
1235        Variable refnum
1236       
1237        //read in the data
1238        ReadASCData(fileStr,destFolder)
1239
1240        //the calling macro must change the display type
1241        String/G root:myGlobals:gDataDisplayType=destFolder
1242       
1243        FillFakeHeader_ASC(destFolder)          //uses info on the panel, if available
1244
1245        //data is displayed here, and needs header info
1246       
1247        fRawWindowHook()
1248       
1249        return(0)
1250End
1251
1252
1253//function called by the main entry procedure (the load button)
1254//loads data into the specified folder, and wipes out what was there
1255//
1256//aborts if no file was selected
1257//
1258// reads the data in if all is OK
1259//
1260// currently unused, as load button has been replaced
1261//
1262Function Load_ASC_File(msgStr,destFolder)
1263        String msgStr,destFolder
1264
1265        String filename="",pathStr=""
1266        Variable refnum
1267
1268        //check for the path
1269        PathInfo catPathName
1270        If(V_Flag==1)           //      /D does not open the file
1271                Open/D/R/T="????"/M=(msgStr)/P=catPathName refNum
1272        else
1273                Open/D/R/T="????"/M=(msgStr) refNum
1274        endif
1275        filename = S_FileName           //get the filename, or null if canceled from dialog
1276        if(strlen(filename)==0)
1277                //user cancelled, abort
1278                SetDataFolder root:
1279                Abort "No file selected, action aborted"
1280        Endif
1281       
1282        //read in the data
1283        ReadASCData(filename,destFolder)
1284
1285        //the calling macro must change the display type
1286        String/G root:myGlobals:gDataDisplayType=destFolder
1287       
1288        FillFakeHeader_ASC(destFolder)          //uses info on the panel, if available
1289
1290        //data is displayed here, and needs header info
1291       
1292        fRawWindowHook()
1293       
1294        return(0)
1295End
1296
1297
1298
1299//function called by the popups to get a file list of data that can be sorted
1300// this procedure simply removes the raw data files from the string - there
1301//can be lots of other junk present, but this is very fast...
1302//
1303Function/S ASC_FileList()
1304
1305        String list="",newList="",item=""
1306        Variable num,ii
1307       
1308        //check for the path
1309        PathInfo catPathName
1310        if(V_Flag==0)
1311                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
1312                Return("")
1313        Endif
1314       
1315        list = IndexedFile(catpathName,-1,"????")
1316        num=ItemsInList(list,";")
1317        //print "num = ",num
1318        for(ii=(num-1);ii>=0;ii-=1)
1319                item = StringFromList(ii, list  ,";")
1320                //simply remove all that are not raw data files (SA1 SA2 SA3)
1321                if( !stringmatch(item,"*.SA1*") && !stringmatch(item,"*.SA2*") && !stringmatch(item,"*.SA3*") )
1322                        if( !stringmatch(item,".*") && !stringmatch(item,"*.pxp") && !stringmatch(item,"*.DIV"))                //eliminate mac "hidden" files, pxp, and div files
1323                                if(!stringmatch(item,"*.AVE") && !stringmatch(item,"*.ABS"))
1324                                        newlist += item + ";"
1325                                endif
1326                        endif
1327                endif
1328        endfor
1329        //remove VAX version numbers
1330        newList = RemoveVersNumsFromList(newList)
1331        //sort
1332        newList = SortList(newList,";",0)
1333
1334        return newlist
1335End
Note: See TracBrowser for help on using the repository browser.