source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/ILL_DataReadWrite.ipf @ 573

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

Merging changes of NCNR_ in to the ILL_ files. This is mostly the :Packages:NIST: data folder structure that is new since Lionel made his changes.

Removed Lionel's InitializeILL file, and moved his specific changes (just some global variables) to the initialization routine in ILL_Utils, where it belongs.

  • Property svn:executable set to *
File size: 48.5 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5//**************************
6//
7// Vers. 1.2 092101
8// Vers. 5.0 29MAR07 - branched from main reduction to split out facility
9//                     specific calls
10//
11// functions for reading raw data files from the VAX
12// - RAW data files are read into the RAW folder - integer data from the detector
13//   is decompressed and given the proper orientation
14// - header information is placed into real,integer, or text waves in the order they appear
15//   in the file header
16//
17// Work data (DIV File) is read into the DIV folder
18//
19//*****************************
20
21//simple, main entry procedure that will load a RAW sans data file (not a work file)
22//into the RAW dataFolder. It is up to the calling procedure to display the file
23//
24// called by MainPanel.ipf and ProtocolAsPanel.ipf
25//
26Function LoadRawSANSData(msgStr)
27        String msgStr
28
29        String filename=""
30
31        //each routine is responsible for checking the current (displayed) data folder
32        //selecting it, and returning to root when done
33        PathInfo/S catPathName          //should set the next dialog to the proper path...
34        //get the filename, then read it in
35        filename = PromptForPath(msgStr)                //in SANS_Utils.ipf
36        //check for cancel from dialog
37        if(strlen(filename)==0)
38                //user cancelled, abort
39                SetDataFolder root:
40                DoAlert 0, "No file selected, action aborted"
41                return(1)
42        Endif
43
44        ReadHeaderAndData(filename)     //this is the full Path+file
45       
46        Return(0)
47End
48
49
50//function that does the guts of reading the binary data file
51//fname is the full path:name;vers required to open the file
52//
53// The final root:RAW:data wave is the real
54//neutron counts and can be directly operated on
55//
56// header information is put into three waves: integersRead, realsRead, and textRead
57// logicals in the header are currently skipped, since they are no use in the
58// data reduction process.
59//
60// The output is the three R/T/I waves that are filled at least with minimal values
61// and the detector data loaded into an array named "data"
62//
63// see documentation for the expected information in each element of the R/T/I waves
64// and the minimum set of information. These waves can be increased in length so that
65// more information can be accessed as needed (propagating changes...)
66//
67// called by multiple .ipfs (when the file name is known)
68//
69//
70// THIS FUNCTION DOES NOT NEED TO BE MODIFIED. ONLY THE DATA ACCESSORS NEED TO BE CONSTRUCTED
71//
72Function ReadHeaderAndData(fname)
73        String fname
74        //this function is for reading in RAW data only, so it will always put data in RAW folder
75        String curPath = "root:Packages:NIST:RAW:"
76        SetDataFolder curPath           //use the full path, so it will always work
77        Variable/G root:Packages:NIST:RAW:gIsLogScale = 0               //initial state is linear, keep this in RAW folder
78       
79        Variable refNum,integer,realval,ii
80        String sansfname,textstr,v0
81       
82        Make/O/D/N=23 $"root:Packages:NIST:RAW:IntegersRead"
83        Make/O/D/N=52 $"root:Packages:NIST:RAW:RealsRead"
84        Make/O/T/N=11 $"root:Packages:NIST:RAW:TextRead"
85        Make/O/N=7 $"root:Packages:NIST:RAW:LogicalsRead"
86       
87        Wave intw=$"root:Packages:NIST:RAW:IntegersRead"
88        Wave realw=$"root:Packages:NIST:RAW:RealsRead"
89        Wave/T textw=$"root:Packages:NIST:RAW:TextRead"
90        Wave logw=$"root:Packages:NIST:RAW:LogicalsRead"
91       
92        // FILL IN 3 ARRAYS WITH HEADER INFORMATION FOR LATER USE
93        // THESE ARE JUST THE MINIMALLY NECESSARY VALUES
94       
95        // filename as stored in the file header
96        Open/R refNum as fname
97        //skip first two bytes (VAX record length markers, not needed here)
98//      FSetPos refNum, 2
99        //read the next 21 bytes as characters (fname)
100       
101       
102       
103        for (ii=0;ii<2;ii+=1)
104                FReadLine refNum,textstr
105                sscanf textstr,"%s",v0
106       
107        endfor
108       
109        Close refnum
110       
111        textw[0]= v0
112       
113//      print "function read raw used"
114       
115        // date and time of collection
116        textw[1]= getFileCreationDate(fname)
117       
118        // run type identifier (this is a reader for RAW data)
119        textw[2]= "RAW"
120       
121        // user account identifier (currently used only for NCNR-specific operations)
122        textw[3]= getFileInstrument(fname)
123
124        // sample label
125        textw[6]= getSampleLabel(fname)
126       
127        // identifier of detector type, useful for setting detector constants
128        //(currently used only for NCNR-specific operations)
129        textw[9]= ""
130       
131       
132
133        //total counting time in seconds
134        intw[2] = getCountTime(fname)
135       
136       
137        // total monitor count
138        realw[0] = getMonitorCount(fname)
139       
140        // total detector count
141        realw[2] = getDetCount(fname)
142       
143        // attenuator number (NCNR-specific, your stub returns 0)
144        // may also be used to hold attenuator transmission (< 1)
145        realw[3] = getAttenNumber(fname)
146       
147        // sample transmission
148        realw[4] = getSampleTrans(fname)
149       
150        //sample thickness (cm)
151        realw[5] = getSampleThickness(fname)
152       
153        // following 6 values are for non-linear spatial corrections to a detector (RC timing)
154        // these values are set for a detctor that has a linear correspondence between
155        // pixel number and real-space distance
156        // 10 and 13 are the X and Y pixel dimensions, respectively (in mm!)
157        //(11,12 and 13,14 are set to values for a linear response, as from a new Ordela detector)
158        realw[10] = getDetectorPixelXSize(fname)
159        realw[11] = 10000
160        realw[12] = 0
161        realw[13] = getDetectorPixelYSize(fname)
162        realw[14] = 10000
163        realw[15] = 0
164       
165        // beam center X,Y on the detector (in units of pixel coordinates (1,N))
166        realw[16] = getBeamXPos(fname)
167        realw[17] = getBeamYPos(fname)
168       
169        // sample to detector distance (meters)
170        realw[18] = getSDD(fname)
171
172        // detector physical width (right now assumes square...) (in cm)
173        realw[20] = 102
174       
175        // beam stop diameter (assumes circular) (in mm)
176        realw[21] = getBSDiameter(fname)
177       
178        // source aperture diameter (mm)
179        realw[23] = getSourceApertureDiam(fname)
180       
181        // sample aperture diameter (mm)
182        realw[24] = getSampleApertureDiam(fname)
183       
184        // source aperture to sample aperture distance
185        realw[25] = getSourceToSampleDist(fname)
186       
187        // wavelength (A)
188        realw[26] = getWavelength(fname)
189       
190        // wavelength spread (FWHM)
191        realw[27] = getWavelengthSpread(fname)
192       
193        realw[52] = getreactorpower(fname)
194       
195        // beam stop X-position (motor reading, approximate cm from zero position)
196        // currently NCNR-specific use to identify transmission measurements
197        // you return 0
198        realw[37] = 0
199       
200       
201// the actual data array, dimensions are set as globals in
202// InitFacilityGlobals()
203        NVAR XPix = root:myGlobals:gNPixelsX
204        NVAR YPix = root:myGlobals:gNPixelsX
205       
206        SetDataFolder curPath
207       
208        Make/D/O/N=(XPix,YPix) $"root:Packages:NIST:RAW:data"
209        WAVE data=$"root:Packages:NIST:RAW:data"
210
211        // fill the data array with the detector values
212        getDetectorData(fname,data)
213       
214        Setdatafolder curpath
215       
216       
217        //keep a string with the filename in the RAW folder
218        String/G root:Packages:NIST:RAW:fileList = textw[0]
219       
220        Setdatafolder root:
221
222       
223        Return 0
224
225End
226
227//****************
228//main entry procedure for reading a "WORK.DIV" file
229//displays a quick image of the  file, to check that it's correct
230//data is deposited in root:DIV data folder
231//
232// local, just for testing
233//
234Proc ReadWork_DIV()
235       
236        String fname = PromptForPath("Select detector sensitivity file")
237        ReadHeaderAndWork("DIV",fname)          //puts what is read in work.div
238       
239        String waveStr = "root:Packages:NIST:DIV:data"
240        NewImage/F/K=1/S=2 $waveStr
241        ModifyImage '' ctab= {*,*,YellowHot,0}
242       
243        String/G root:Packages:NIST:DIV:fileList = "WORK.DIV"
244       
245        SetDataFolder root:             //(redundant)
246End
247
248
249
250// Detector sensitivity files have the same header structure as RAW SANS data
251// as NCNR, just with a different data array (real, rather than integer data)
252//
253// So for your facility, make this reader specific to the format of whatever
254// file you use for a pixel-by-pixel division of the raw detector data
255// to correct for non-uniform sensitivities of the detector. This is typically a
256// measurement of water, plexiglas, or another uniform scattering sample.
257//
258// The data must be normalized to a mean value of 1
259//
260// called from ProtocolAsPanel.ipf
261//
262// type is "DIV" on input
263Function ReadHeaderAndWork1(type,fname)
264        String type,fname
265       
266        //type is the desired folder to read the workfile to
267        //this data will NOT be automatically displayed
268        // gDataDisplayType is unchanged
269
270        String cur_folder = type
271        String curPath = "root:Packages:NIST:"+cur_folder
272        SetDataFolder curPath           //use the full path, so it will always work
273       
274        Variable refNum,integer,realval
275        String sansfname,textstr
276        Variable/G $(curPath + ":gIsLogScale") = 0              //initial state is linear, keep this in DIV folder
277       
278        Make/O/D/N=23 $(curPath + ":IntegersRead")
279        Make/O/D/N=52 $(curPath + ":RealsRead")
280        Make/O/T/N=11 $(curPath + ":TextRead")
281       
282        WAVE intw=$(curPath + ":IntegersRead")
283        WAVE realw=$(curPath + ":RealsRead")
284        WAVE/T textw=$(curPath + ":TextRead")
285       
286        // the actual data array, dimensions are set as globals in
287        // InitFacilityGlobals()
288        NVAR XPix = root:myGlobals:gNPixelsX
289        NVAR YPix = root:myGlobals:gNPixelsX
290       
291
292       
293        Make/O/D/N=(XPix,YPix) $(curPath + ":data")
294        WAVE data = $(curPath + ":data")
295       
296       
297//      print "function used"
298       
299
300                // (1)
301        // fill in your reader for the header here so that intw, realw, and textW are filled in
302        // ? possibly a duplication of the raw data reader
303        Duplicate/O $("root:Packages:NIST:raw:realsread"),$(curPath+ ":realsread")
304        Duplicate/O $("root:Packages:NIST:raw:integersread"),$(curPath+ ":integersread")
305        Duplicate/T $("root:Packages:NIST:raw:Textread"),$(curPath+ ":Textread")
306       
307
308        //(2)
309        // then fill in a reader for the data array that will DIVIDE your data
310        // to get the corrected values.
311        // fill the data array with the detector values
312
313        duplicate/O $("root:Packages:NIST:raw:data"),$(curPath+ ":data")
314       
315       
316        //keep a string with the filename in the DIV folder
317        String/G $(curPath + ":fileList") = textw[0]
318       
319        //return the data folder to root
320        SetDataFolder root:
321       
322        Return(0)
323End
324
325
326/////////DIV file created with NIST reduction so has the VAX format.... painful
327
328Function ReadHeaderAndWork(type,fname)
329        String type,fname
330       
331        //type is the desired folder to read the workfile to
332        //this data will NOT be automatically displayed gDataDisplayType is unchanged
333
334//      SVAR cur_folder=root:myGlobals:gDataDisplayType
335        String cur_folder = type
336        String curPath = "root:Packages:NIST:"+cur_folder
337        SetDataFolder curPath           //use the full path, so it will always work
338       
339        Variable refNum,integer,realval
340        String sansfname,textstr
341        Variable/G $(curPath + ":gIsLogScale") = 0              //initial state is linear, keep this in DIV folder
342       
343        Make/O/N=23 $(curPath + ":IntegersRead")
344        Make/O/N=52 $(curPath + ":RealsRead")
345        Make/O/T/N=11 $(curPath + ":TextRead")
346       
347        WAVE intw=$(curPath + ":IntegersRead")
348        WAVE realw=$(curPath + ":RealsRead")
349        WAVE/T textw=$(curPath + ":TextRead")
350       
351        //***NOTE ****
352        // the "current path" gets mysteriously reset to "root:" after the SECOND pass through
353        // this read routine, after the open dialog is presented
354        // the "--read" waves end up in the correct folder, but the data does not! Why?
355        //must re-set data folder before writing data array (done below)
356       
357        SetDataFolder curPath
358       
359        //actually open the file
360        Open/R refNum as fname
361        //skip first two bytes
362        FSetPos refNum, 2
363        //read the next 21 bytes as characters (fname)
364        FReadLine/N=21 refNum,textstr
365        textw[0]= textstr
366        //read four i*4 values  /F=3 flag, B=3 flag
367        FBinRead/F=3/B=3 refNum, integer
368        intw[0] = integer
369        //
370        FBinRead/F=3/B=3 refNum, integer
371        intw[1] = integer
372        //
373        FBinRead/F=3/B=3 refNum, integer
374        intw[2] = integer
375        //
376        FBinRead/F=3/B=3 refNum, integer
377        intw[3] = integer
378        // 6 text fields
379        FSetPos refNum,55               //will start reading at byte 56
380        FReadLine/N=20 refNum,textstr
381        textw[1]= textstr
382        FReadLine/N=3 refNum,textstr
383        textw[2]= textstr
384        FReadLine/N=11 refNum,textstr
385        textw[3]= textstr
386        FReadLine/N=1 refNum,textstr
387        textw[4]= textstr
388        FReadLine/N=8 refNum,textstr
389        textw[5]= textstr
390        FReadLine/N=60 refNum,textstr
391        textw[6]= textstr
392       
393        //3 integers
394        FSetPos refNum,174
395        FBinRead/F=3/B=3 refNum, integer
396        intw[4] = integer
397        FBinRead/F=3/B=3 refNum, integer
398        intw[5] = integer
399        FBinRead/F=3/B=3 refNum, integer
400        intw[6] = integer
401       
402        //2 integers, 3 text fields
403        FSetPos refNum,194
404        FBinRead/F=3/B=3 refNum, integer
405        intw[7] = integer
406        FBinRead/F=3/B=3 refNum, integer
407        intw[8] = integer
408        FReadLine/N=6 refNum,textstr
409        textw[7]= textstr
410        FReadLine/N=6 refNum,textstr
411        textw[8]= textstr
412        FReadLine/N=6 refNum,textstr
413        textw[9]= textstr
414       
415        //2 integers
416        FSetPos refNum,244
417        FBinRead/F=3/B=3 refNum, integer
418        intw[9] = integer
419        FBinRead/F=3/B=3 refNum, integer
420        intw[10] = integer
421       
422        //2 integers
423        FSetPos refNum,308
424        FBinRead/F=3/B=3 refNum, integer
425        intw[11] = integer
426        FBinRead/F=3/B=3 refNum, integer
427        intw[12] = integer
428       
429        //2 integers
430        FSetPos refNum,332
431        FBinRead/F=3/B=3 refNum, integer
432        intw[13] = integer
433        FBinRead/F=3/B=3 refNum, integer
434        intw[14] = integer
435       
436        //3 integers
437        FSetPos refNum,376
438        FBinRead/F=3/B=3 refNum, integer
439        intw[15] = integer
440        FBinRead/F=3/B=3 refNum, integer
441        intw[16] = integer
442        FBinRead/F=3/B=3 refNum, integer
443        intw[17] = integer
444       
445        //1 text field - the file association for transmission are the first 4 bytes
446        FSetPos refNum,404
447        FReadLine/N=42 refNum,textstr
448        textw[10]= textstr
449       
450        //1 integer
451        FSetPos refNum,458
452        FBinRead/F=3/B=3 refNum, integer
453        intw[18] = integer
454       
455        //4 integers
456        FSetPos refNum,478
457        FBinRead/F=3/B=3 refNum, integer
458        intw[19] = integer
459        FBinRead/F=3/B=3 refNum, integer
460        intw[20] = integer
461        FBinRead/F=3/B=3 refNum, integer
462        intw[21] = integer
463        FBinRead/F=3/B=3 refNum, integer
464        intw[22] = integer
465       
466        Close refNum
467       
468        //now get all of the reals
469        //
470        //Do all the GBLoadWaves at the end
471        //
472        //FBinRead Cannot handle 32 bit VAX floating point
473        //GBLoadWave, however, can properly read it
474        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
475        String strToExecute
476        //append "/S=offset/U=numofreals" to control the read
477        // then append fname to give the full file path
478        // then execute
479       
480        Variable a=0,b=0
481       
482        SetDataFolder curPath
483        // 4 R*4 values
484        strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + fname + "\""
485        Execute strToExecute
486       
487        SetDataFolder curPath
488        Wave w=$(curPath + ":tempGBWave0")
489        b=4     //num of reals read
490        realw[a,a+b-1] = w[p-a]
491        a+=b
492       
493        // 4 R*4 values
494        SetDataFolder curPath
495        strToExecute = GBLoadStr + "/S=158/U=4" + "\"" + fname + "\""
496        Execute strToExecute
497        b=4     
498        realw[a,a+b-1] = w[p-a]
499        a+=b
500
501///////////
502        // 2 R*4 values
503        SetDataFolder curPath
504        strToExecute = GBLoadStr + "/S=186/U=2" + "\"" + fname + "\""
505        Execute strToExecute
506        b=2     
507        realw[a,a+b-1] = w[p-a]
508        a+=b
509
510        // 6 R*4 values
511        SetDataFolder curPath
512        strToExecute = GBLoadStr + "/S=220/U=6" + "\"" + fname + "\""
513        Execute strToExecute
514        b=6     
515        realw[a,a+b-1] = w[p-a]
516        a+=b
517       
518        // 13 R*4 values
519        SetDataFolder curPath
520        strToExecute = GBLoadStr + "/S=252/U=13" + "\"" + fname + "\""
521        Execute strToExecute
522        b=13
523        realw[a,a+b-1] = w[p-a]
524        a+=b
525       
526        // 3 R*4 values
527        SetDataFolder curPath
528        strToExecute = GBLoadStr + "/S=320/U=3" + "\"" + fname + "\""
529        Execute strToExecute
530        b=3     
531        realw[a,a+b-1] = w[p-a]
532        a+=b
533       
534        // 7 R*4 values
535        SetDataFolder curPath
536        strToExecute = GBLoadStr + "/S=348/U=7" + "\"" + fname + "\""
537        Execute strToExecute
538        b=7
539        realw[a,a+b-1] = w[p-a]
540        a+=b
541       
542        // 4 R*4 values
543        SetDataFolder curPath
544        strToExecute = GBLoadStr + "/S=388/U=4" + "\"" + fname + "\""
545        Execute strToExecute
546        b=4     
547        realw[a,a+b-1] = w[p-a]
548        a+=b
549       
550        // 2 R*4 values
551        SetDataFolder curPath
552        strToExecute = GBLoadStr + "/S=450/U=2" + "\"" + fname + "\""
553        Execute strToExecute
554        b=2
555        realw[a,a+b-1] = w[p-a]
556        a+=b
557       
558        // 2 R*4 values
559        SetDataFolder curPath
560        strToExecute = GBLoadStr + "/S=470/U=2" + "\"" + fname + "\""
561        Execute strToExecute
562        b=2
563        realw[a,a+b-1] = w[p-a]
564        a+=b
565       
566        // 5 R*4 values
567        SetDataFolder curPath
568        strToExecute = GBLoadStr + "/S=494/U=5" + "\"" + fname + "\""
569        Execute strToExecute
570        b=5     
571        realw[a,a+b-1] = w[p-a]
572       
573        //if the binary VAX data ws transferred to a MAC, all is OK
574        //if the data was trasnferred to an Intel machine (IBM), all the real values must be
575        //divided by 4 to get the correct floating point values
576        // I can't find any combination of settings in GBLoadWave or FBinRead to read data in correctly
577        // on an Intel machine.
578        //With the corrected version of GBLoadWave XOP (v. 1.43 or higher) Mac and PC both read
579        //VAX reals correctly, and no checking is necessary 12 APR 99
580        //if(cmpstr("Macintosh",IgorInfo(2)) == 0)
581                //do nothing
582        //else
583                //either Windows or Windows NT
584                //realw /= 4
585        //endif
586       
587        //read in the data
588         GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
589
590        curPath = "root:Packages:NIST:"+cur_folder
591        SetDataFolder curPath           //use the full path, so it will always work
592       
593        Make/O/N=16384 $(curPath + ":data")
594        WAVE data = $(curPath + ":data")
595       
596        Variable skip,ii,offset
597       
598        //read in a total of 16384 values (ii)
599        //as follows :
600        // skip first 2 bytes
601        // skip 512 byte header
602        // skip first 2 bytes of data
603        //(read 511 reals, skip 2b, 510 reals, skip 2b) -16 times = 16336 values
604        // read the final 48 values in seperately to avoid EOF error
605       
606        /////////////
607        SetDataFolder curPath
608        skip = 0
609        ii=0
610        offset = 514 +2
611        a=0
612        do
613                SetDataFolder curPath
614               
615                strToExecute = GBLoadStr + "/S="+num2str(offset)+"/U=511" + "\"" + fname + "\""
616                Execute strToExecute
617                //Print strToExecute
618                b=511
619                data[a,a+b-1] = w[p-a]
620                a+=b
621               
622                offset += 511*4 +2
623               
624                strToExecute = GBLoadStr + "/S="+num2str(offset)+"/U=510" + "\"" + fname + "\""
625                SetDataFolder curPath
626                Execute strToExecute
627                //Print strToExecute
628                b=510
629                data[a,a+b-1] = w[p-a]
630                a+=b
631               
632                offset += 510*4 +2
633               
634                ii+=1
635                //Print "inside do, data[2] =",data[2]
636                //Print "inside do, tempGBwave0[0] = ",w[0]
637        while(ii<16)
638       
639        // 16336 values have been read in --
640        //read in last 64 values
641        strToExecute = GBLoadStr + "/S="+num2str(offset)+"/U=48" + "\"" + fname + "\""
642       
643        SetDataFolder curPath
644        Execute strToExecute
645        b=48
646        data[a,a+b-1] = w[p-a]
647        a+=b
648//
649/// done reading in raw data
650//
651        //Print "in workdatareader , data = ", data[1][1]
652
653        Redimension/n=(128,128) data
654       
655        //clean up - get rid of w = $"tempGBWave0"
656        KillWaves w
657       
658        //divide the FP data by 4 if read from a PC (not since GBLoadWave update)
659        //if(cmpstr("Macintosh",IgorInfo(2)) == 0)
660                //do nothing
661        //else
662                //either Windows or Windows NT
663                //data /= 4
664        //endif
665       
666        //keep a string with the filename in the DIV folder
667        String/G $(curPath + ":fileList") = textw[0]
668       
669        //return the data folder to root
670        SetDataFolder root:
671       
672        Return(0)
673End
674
675
676
677
678
679/////   ASC FORMAT READER  //////
680/////   FOR WORKFILE MATH PANEL //////
681//
682//function to read in the ASC output of SANS reduction
683// currently the file has 20 header lines, followed by a single column
684// of 16384 values, Data is written by row, starting with Y=1 and X=(1->128)
685//
686//returns 0 if read was ok
687//returns 1 if there was an error
688//
689// called by WorkFileUtils.ipf
690//
691//
692// If the ASC data was generated by the NCNR data writer, then
693// NOTHING NEEDS TO BE CHANGED HERE
694//
695Function ReadASCData(fname,destPath)
696        String fname, destPath
697        //this function is for reading in ASCII data so put data in user-specified folder
698        SetDataFolder "root:Packages:NIST:"+destPath
699
700        NVAR pixelsX = root:myGlobals:gNPixelsX
701        NVAR pixelsY = root:myGlobals:gNPixelsY
702        Variable refNum=0,ii,p1,p2,tot,num=pixelsX,numHdrLines=220   ///waas 20 for NIST
703        String str=""
704        //data is initially linear scale
705        Variable/G :gIsLogScale=0
706        Make/O/T/N=(numHdrLines) hdrLines
707        Make/O/D/N=(pixelsX*pixelsY) data                       //,linear_data
708       
709        //full filename and path is now passed in...
710        //actually open the file
711//      SetDataFolder destPath
712        Open/R/Z refNum as fname                // /Z flag means I must handle open errors
713        if(refnum==0)           //FNF error, get out
714                DoAlert 0,"Could not find file: "+fname
715                Close/A
716                SetDataFolder root:
717                return(1)
718        endif
719        if(V_flag!=0)
720                DoAlert 0,"File open error: V_flag="+num2Str(V_Flag)
721                Close/A
722                SetDataFolder root:
723                return(1)
724        Endif
725        //
726        for(ii=0;ii<numHdrLines;ii+=1)          //read (or skip) 18 header lines
727                FReadLine refnum,str
728                hdrLines[ii]=str
729        endfor
730        //     
731        Close refnum
732       
733//      SetDataFolder destPath
734
735//loadwave  /A=tempGBwave/L={0, start, numrow, 0, numcol}/N/G/D/O/E=1/K=1
736
737
738        LoadWave/Q/G/D/N=temp fName
739        Wave/Z temp0=temp0
740        data=temp0
741        Redimension/N=(pixelsX,pixelsY) data            //,linear_data
742       
743        //linear_data = data
744       
745        KillWaves/Z temp0
746       
747        //return the data folder to root
748        SetDataFolder root:
749       
750        Return(0)
751End
752
753// fills the "default" fake header so that the SANS Reduction machinery does not have to be altered
754// pay attention to what is/not to be trusted due to "fake" information.
755// uses what it can from the header lines from the ASC file (hdrLines wave)
756//
757// destFolder is of the form "myGlobals:WorkMath:AAA"
758//
759//
760// called by WorkFileUtils.ipf
761//
762// If the ASC data was generated by the NCNR data writer, then
763// NOTHING NEEDS TO BE CHANGED HERE
764//
765Function FillFakeHeader_ASC(destFolder)
766        String destFolder
767        Make/O/D/N=23 $("root:Packages:NIST:"+destFolder+":IntegersRead")
768        Make/O/D/N=52 $("root:Packages:NIST:"+destFolder+":RealsRead")
769        Make/O/T/N=11 $("root:Packages:NIST:"+destFolder+":TextRead")
770       
771        Wave intw=$("root:Packages:NIST:"+destFolder+":IntegersRead")
772        Wave realw=$("root:Packages:NIST:"+destFolder+":RealsRead")
773        Wave/T textw=$("root:Packages:NIST:"+destFolder+":TextRead")
774       
775        //Put in appropriate "fake" values
776        //parse values as needed from headerLines
777        Wave/T hdr=$("root:Packages:NIST:"+destFolder+":hdrLines")
778        Variable monCt,lam,offset,sdd,trans,thick
779        Variable xCtr,yCtr,a1,a2,a1a2Dist,dlam,bsDiam
780        String detTyp=""
781        String tempStr="",formatStr="",junkStr=""
782        formatStr = "%g %g %g %g %g %g"
783        tempStr=hdr[3]
784        sscanf tempStr, formatStr, monCt,lam,offset,sdd,trans,thick
785//      Print monCt,lam,offset,sdd,trans,thick,avStr,step
786        formatStr = "%g %g %g %g %g %g %g %s"
787        tempStr=hdr[5]
788        sscanf tempStr,formatStr,xCtr,yCtr,a1,a2,a1a2Dist,dlam,bsDiam,detTyp
789//      Print xCtr,yCtr,a1,a2,a1a2Dist,dlam,bsDiam,detTyp
790       
791        realw[16]=xCtr          //xCtr(pixels)
792        realw[17]=yCtr  //yCtr (pixels)
793        realw[18]=sdd           //SDD (m)
794        realw[26]=lam           //wavelength (A)
795        //
796        // necessary values
797        realw[10]=8                     //detector calibration constants, needed for averaging
798        realw[11]=10000
799        realw[12]=0
800        realw[13]=8
801        realw[14]=10000
802        realw[15]=0
803        //
804        // used in the resolution calculation, ONLY here to keep the routine from crashing
805        realw[20]=102           //det size
806        realw[27]=dlam  //delta lambda
807        realw[21]=bsDiam        //BS size
808        realw[23]=a1            //A1
809        realw[24]=a2    //A2
810        realw[25]=a1a2Dist      //A1A2 distance
811        realw[4]=trans          //trans
812        realw[3]=0              //atten
813        realw[5]=thick          //thick
814        //
815        //
816        realw[0]=monCt          //def mon cts
817
818        // fake values to get valid deadtime and detector constants
819        //
820        textw[9]=detTyp+"  "            //6 characters 4+2 spaces
821        textw[3]="[NGxSANS00]"  //11 chars, NGx will return default values for atten trans, deadtime...
822       
823        //set the string values
824        formatStr="FILE: %s CREATED: %s"
825        sscanf hdr[0],formatStr,tempStr,junkStr
826//      Print tempStr
827//      Print junkStr
828        String/G $("root:Packages:NIST:"+destFolder+":fileList") = tempStr
829        textw[0] = tempStr              //filename
830        textw[1] = junkStr              //run date-time
831       
832        //file label = hdr[1]
833        tempStr = hdr[1]
834        tempStr = tempStr[0,strlen(tempStr)-2]          //clean off the last LF
835//      Print tempStr
836        textW[6] = tempStr      //sample label
837       
838        return(0)
839End
840
841
842///////// ACCESSORS FOR WRITING DATA TO HEADER  /////////
843/////
844
845// Write* routines all must:
846// (1) open file "fname". fname is a full file path and name to the file on disk
847// (2) write the specified value to the header at the correct location in the file
848// (3) close the file
849
850//sample transmission (0<T<=1)
851Function WriteTransmissionToHeader(fname,trans)
852        String fname
853        Variable trans
854       
855        // your writer here
856       
857        WriteReal(fname,trans,5589)     //I will define at position 10 lines position 1  by myself
858       
859        // 16 bites between numbers and 81 per line
860       
861        return(0)
862End
863
864//whole transmission is NCNR-specific right now
865// leave this stub empty
866Function WriteWholeTransToHeader(fname,trans)
867        String fname
868        Variable trans
869       
870        // do nothing for now
871        WriteReal(fname,trans,6885)   /// //I will define at position last  lines  position 1  by myself
872       
873        return(0)
874End
875
876//box sum counts is a real value
877// used for transmission calculation module
878Function WriteBoxCountsToHeader(fname,counts)
879        String fname
880        Variable counts
881       
882        // do nothing if not using NCNR Transmission module
883       
884        WriteReal(fname,counts,6868)
885       
886        ////I will define at position 2   lines before the end and  last position   by myself
887       
888       
889        return(0)
890End
891
892//beam stop X-position
893// used for transmission module to manually tag transmission files
894Function WriteBSXPosToHeader(fname,xpos)
895        String fname
896        Variable xpos
897       
898        // do nothing if not using NCNR Transmission module
899       
900        WriteReal(fname,xpos,5119)    ////should do it for ypos for ILL ....NEED TO REMEMBER HERE
901        /// line 4 column 2   
902       
903        return(0)
904End
905
906//sample thickness in cm
907Function WriteThicknessToHeader(fname,num)
908        String fname
909        Variable num
910       
911        // your code here
912       
913        WriteReal(fname,num,5508)  //define at 9 lines  column 1 just above transmission by myself
914       
915        return(0)
916End
917
918//beam center X pixel location (detector coordinates)
919Function WriteBeamCenterXToHeader(fname,num)
920        String fname
921        Variable num
922       
923        // your code here
924       
925        // pos (1) on line 71 => 70 lines x 81 char
926        WriteReal(fname,num*10,5670)
927       
928        ///   line 11 column 1
929       
930        return(0)
931End
932
933//beam center Y pixel location (detector coordinates)
934Function WriteBeamCenterYToHeader(fname,num)
935        String fname
936        Variable num
937       
938        // your code here
939       
940        WriteReal(fname,num*10,5686)
941       
942        ///   line 11 column 2
943       
944        return(0)
945End
946
947//attenuator number (not its transmission)
948// if your beam attenuation is indexed in some way, use that number here
949// if not, write a 1 to the file here as a default
950//
951Function WriteAttenNumberToHeader(fname,num)
952        String fname
953        Variable num
954       
955        // your code here, default of 1
956        WriteReal(fname,num,5799)
957       
958        ///   line 12 column 4
959       
960        return(0)
961End
962
963
964Function WritereactorpowerToHeader(fname,num)
965        String fname
966        Variable num
967       
968        // your code here, default of 1
969        WriteReal(fname,num,6204)
970       
971        ///   line 12 column 4
972       
973        return(0)
974End
975
976
977
978
979
980
981
982
983// total monitor count during data collection
984Function WriteMonitorCountToHeader(fname,num)
985        String fname
986        Variable num
987       
988        // your code here
989       
990        WriteReal(fname,num,4924)
991       
992        ///   line 1 column 5
993       
994        return(0)
995End
996
997//total detector count
998Function WriteDetectorCountToHeader(fname,num)
999        String fname
1000        Variable num
1001       
1002        // your code here
1003       
1004        WriteReal(fname,num,4908)
1005       
1006        ///   line 1 column 4
1007       
1008        return(0)
1009End
1010
1011//transmission detector count
1012// (currently unused in data reduction)
1013Function WriteTransDetCountToHeader(fname,num)
1014        String fname
1015        Variable num
1016       
1017        // do nothing for now
1018       
1019        return(0)
1020End
1021
1022//wavelength (Angstroms)
1023Function WriteWavelengthToHeader(fname,num)
1024        String fname
1025        Variable num
1026       
1027        // your code here
1028        WriteReal(fname,num,5703)
1029       
1030        //   line 11 column 3
1031       
1032        return(0)
1033End
1034
1035//wavelength spread (FWHM)
1036Function WriteWavelengthDistrToHeader(fname,num)
1037        String fname
1038        Variable num
1039       
1040        // your code here
1041       
1042        WriteReal(fname,num,5719)
1043       
1044        //   line 11 column 4
1045       
1046        return(0)
1047End
1048
1049//temperature of the sample (C)
1050Function WriteTemperatureToHeader(fname,num)
1051        String fname
1052        Variable num
1053       
1054        //  your code here
1055       
1056        WriteReal(fname,num,5347)
1057       
1058        //   line 7 column 1
1059       
1060        return(0)
1061End
1062
1063//magnetic field (Oe)
1064Function WriteMagnFieldToHeader(fname,num)
1065        String fname
1066        Variable num
1067       
1068        // your code here
1069       
1070        return(0)
1071End
1072
1073//Source Aperture diameter (millimeters)
1074Function WriteSourceApDiamToHeader(fname,num)
1075        String fname
1076        Variable num
1077       
1078        // your code here
1079//      WriteReal(fname,num,5348)
1080      WriteReal(fname,num,6027)    ///4*81 = 324
1081     
1082      // line 15 colum 3
1083       
1084        return(0)
1085End
1086
1087//Sample Aperture diameter (millimeters)
1088Function WriteSampleApDiamToHeader(fname,num)
1089        String fname
1090        Variable num
1091       
1092        //your code here
1093        WriteReal(fname,num,6043)
1094       
1095            // line 15 colum 4
1096       
1097        return(0)
1098End
1099
1100//Source aperture to sample aperture distance (meters)
1101Function WriteSrcToSamDistToHeader(fname,num)
1102        String fname
1103        Variable num
1104       
1105        //      your code here
1106        WriteReal(fname,num,5784)  //it is collimation at ILL
1107       
1108       
1109        return(0)
1110End
1111
1112//lateral detector offset (centimeters)
1113Function WriteDetectorOffsetToHeader(fname,num)
1114        String fname
1115        Variable num
1116       
1117        //your code here
1118       
1119        WriteReal(fname,10*num,5849)
1120       
1121       
1122       
1123        // line 13 column 2
1124       
1125        return(0)
1126End
1127
1128//beam stop diameter (millimeters)
1129Function WriteBeamStopDiamToHeader(fname,num)
1130        String fname
1131        Variable num
1132       
1133        // your code here
1134        WriteReal(fname,num,6059)
1135       
1136        //line 15 column 5
1137       
1138        return(0)
1139End
1140
1141//sample to detector distance (meters)
1142Function WriteSDDToHeader(fname,num)
1143        String fname
1144        Variable num
1145       
1146        //your code here
1147        //WriteReal(fname,num,5152)
1148       
1149       
1150        WriteReal(fname,num,5265)
1151       
1152        // line 4 column 4
1153       
1154        // real calculated distance line 6 colunm1
1155       
1156        return(0)
1157End
1158
1159// physical dimension of detector x-pixel (mm)
1160Function WriteDetPixelXToHeader(fname,num)
1161        String fname
1162        Variable num
1163       
1164        //your code here
1165        WriteReal(fname,num,5735)
1166       
1167        //  line 11 column 5   
1168       
1169        return(0)
1170end
1171
1172// physical dimension of detector y-pixel (mm)
1173Function WriteDetPixelYToHeader(fname,num)
1174        String fname
1175        Variable num
1176       
1177        //your code here
1178        WriteReal(fname,num,5752)
1179       
1180        return(0)
1181end
1182
1183// sample label string
1184Function WriteSamLabelToHeader(fname,str)
1185        String fname,str
1186       
1187        // your code here
1188//              WriteText(fname,"                              ",2075)  // need to write in 30 bites no more....
1189
1190        Variable numChars=30
1191        String blankStr=""
1192        blankStr = PadString(blankStr, numChars, 0x20)
1193        WriteText(fname,blankStr,2075)
1194       
1195        if(strlen(str)>numChars)
1196                str = str[0,numchars-1]
1197        endif
1198       
1199        WriteText(fname,str,2075)   //// need to change that in order to erase the title and write a new one
1200
1201        return(0)
1202End
1203
1204// total counting time (stored here as seconds/10??)
1205Function WriteCountTimeToHeader(fname,num)
1206        String fname
1207        Variable num
1208       
1209        // your code here
1210//      WriteReal(fname,num,4894)
1211        WriteReal(fname,10*num,4892)
1212       
1213       
1214       
1215        return(0)
1216End
1217
1218
1219
1220//////// ACCESSORS FOR READING DATA FROM THE HEADER  //////////////
1221//
1222// read specific bits of information from the header
1223// each of these operations MUST take care of open/close on their own
1224//
1225// fname is the full path and filname to the file on disk
1226// return values are either strings or real values as appropriate
1227//
1228//////
1229
1230
1231// function that reads in the 2D detector data and fills the array
1232// data[nx][ny] with the data values
1233// fname is the full name and path to the data file for opening and closing
1234//
1235//
1236Function getDetectorData(fname,data)
1237        String fname
1238        Wave data
1239       
1240       
1241        // your reader here
1242       
1243//      value = getRealValueFromHeader_2(fname,60,28,5,1,3)  // data start at 60+58 lines
1244        ReadILLData2(fname,data)
1245       
1246        return (0)
1247End
1248
1249// file suffix (NCNR data file name specific)
1250// return null string
1251Function/S getSuffix(fname)
1252        String fname
1253       
1254        String suffix = getStringFromHeader(fname,9341,6)
1255       
1256        // replace the leading space w/ "0"
1257        suffix = ReplaceString(" ",suffix,"0")
1258       
1259        return(suffix)   //// file suffix (6 characters @ byte 9341)
1260End
1261
1262// associated file suffix (for transmission)
1263// NCNR Transmission calculation specific
1264// return null string
1265Function/S getAssociatedFileSuffix(fname)
1266        String fname
1267       
1268        string str
1269       
1270        str= getStringFromHeader(fname,9350,6)
1271       
1272        // replace leading space(s) w/zero
1273        str = ReplaceString(" ", str, "0" )
1274//      print  str
1275       
1276        return(str)  //  6 characters @ byte 9350
1277End
1278
1279// sample label
1280Function/S getSampleLabel(fname)
1281        String fname
1282       
1283        String str
1284       
1285        // your code, returning str
1286        str = (getStringFromHeader(fname,2075,30))  /// 25*81  +  50////+51 30 lines before the end
1287       
1288       
1289        return(str)
1290End
1291
1292// file creation date
1293Function/S getFileCreationDate(fname)
1294        String fname
1295       
1296        String str
1297       
1298        // your code, returning str
1299       
1300        str = (getStringFromHeader(fname,2106,50))   //324 +12
1301       
1302        return(str)
1303End
1304
1305
1306Function/S getFileInstrument(fname)
1307        String fname
1308       
1309        String str
1310       
1311        // your code, returning str
1312       
1313        str = (getStringFromHeader(fname,324,3))   //324 +12
1314       
1315        return(str)
1316End
1317
1318
1319//reactor power
1320Function getReactorpower(fname)
1321        String fname
1322       
1323        Variable value
1324       
1325        // your code returning value
1326       
1327//      value = getRealValueFromHeader_2(fname,60,28,5,1,5)  //
1328       
1329        value = getRealValueFromHeader(fname,83)
1330       
1331//      print value
1332       
1333        return(value)
1334end
1335
1336
1337
1338
1339
1340//monitor count
1341Function getMonitorCount(fname)
1342        String fname
1343       
1344        Variable value
1345       
1346        // your code returning value
1347       
1348//      value = getRealValueFromHeader_2(fname,60,28,5,1,5)  //
1349       
1350        value = getRealValueFromHeader(fname,4)
1351       
1352//      print value
1353       
1354        return(value)
1355end
1356
1357//saved monitor count
1358// NCNR specific for pre-processed data, never used
1359// return 0
1360Function getSavMon(fname)
1361        String fname
1362       
1363        Variable value
1364       
1365        // your code returning value
1366       
1367        return(0)
1368end
1369
1370//total detector count
1371Function getDetCount(fname)
1372        String fname
1373       
1374        Variable value
1375       
1376        // your code returning value
1377       
1378//      value = getRealValueFromHeader_2(fname,60,28,5,1,4)
1379       
1380        value = getRealValueFromHeader(fname,3)
1381       
1382        return(value)
1383end
1384
1385//Attenuator number, return 1 if your attenuators are not
1386// indexed by number
1387Function getAttenNumber(fname)
1388        String fname
1389       
1390        Variable value
1391       
1392        // your code returning value
1393       
1394//      value = getRealValueFromHeader_2(fname,60,28,5,56,4)
1395       
1396        value = getRealValueFromHeader(fname,58)
1397       
1398        return(value)
1399end
1400
1401//transmission
1402Function getSampleTrans(fname)
1403        String fname
1404       
1405        Variable value
1406       
1407        // your code returning value
1408       
1409        value = getRealValueFromHeader(fname,45)
1410       
1411        return(value)
1412end
1413
1414//box counts from stored transmission calculation
1415// return 0 if not using NCNR transmission module
1416Function getBoxCounts(fname)
1417        String fname
1418       
1419        Variable value
1420       
1421        // your code returning value
1422        value = getRealValueFromHeader(fname,124)
1423       
1424        return(value)
1425end
1426
1427//whole detector trasmission
1428// return 0 if not using NCNR transmission module
1429Function getSampleTransWholeDetector(fname)
1430        String fname
1431       
1432        Variable value
1433       
1434        // your code returning value
1435       
1436        return(value)
1437end
1438
1439//SampleThickness in centimeters
1440Function getSampleThickness(fname)
1441        String fname
1442       
1443        Variable value
1444       
1445        // your code returning value
1446        value = getRealValueFromHeader(fname,40)  //mm
1447       
1448        return(value)
1449end
1450
1451//Sample Rotation Angle (degrees)
1452Function getSampleRotationAngle(fname)
1453        String fname
1454       
1455        Variable value
1456       
1457        // your code returning value
1458        value = getRealValueFromHeader(fname,64)
1459       
1460        return(value)
1461end
1462
1463//temperature (C)
1464Function getTemperature(fname)
1465        String fname
1466       
1467        Variable value
1468       
1469        // your code returning value
1470       
1471//      value = getRealValueFromHeader_2(fname,60,28,5,7,1)
1472       
1473        value = getRealValueFromHeader(fname,30)
1474       
1475        return(value)
1476end
1477
1478//field strength (Oe)
1479Function getFieldStrength(fname)
1480        String fname
1481       
1482        Variable value
1483       
1484        // your code returning value
1485       
1486        return(value)
1487end
1488
1489//center of beam xPos in pixel coordinates
1490Function getBeamXPos(fname)
1491        String fname
1492       
1493        Variable value
1494       
1495        // your code returning value   
1496//      value = getRealValueFromHeader(fname,14)                //Lionel
1497        value = getRealValueFromHeader(fname,50)/10             //SRK
1498       
1499        return(value)
1500end
1501
1502//center of beam Y pos in pixel coordinates
1503Function getBeamYPos(fname)
1504        String fname
1505       
1506        Variable value
1507       
1508        // your code returning value
1509//      value = getRealValueFromHeader(fname,15)                //Lionel
1510        value = getRealValueFromHeader(fname,51)/10             //SRK
1511       
1512        return(value)
1513end
1514
1515//sample to detector distance (meters)
1516Function getSDD(fname)
1517        String fname
1518       
1519        Variable value
1520       
1521        // your code returning value
1522//      value = getRealValueFromHeader_2(fname,60,28,5,4,4)
1523//      value = getRealValueFromHeader(fname,18)  detector distance but need to add the offset due to the table
1524       
1525        value = getRealValueFromHeader(fname,25)
1526       
1527       
1528        return(value)
1529end
1530
1531//lateral detector offset (centimeters)
1532Function getDetectorOffset(fname)
1533        String fname
1534       
1535        Variable value
1536       
1537        // your code returning value
1538//      value = getRealValueFromHeader_2(fname,60,28,5,13,2)
1539        value = getRealValueFromHeader(fname,61)
1540       
1541        return(value/10)  // need in cm ILL write in mm
1542end
1543
1544//Beamstop diameter (millimeters)
1545Function getBSDiameter(fname)
1546        String fname
1547       
1548        Variable value
1549       
1550        // your code returning value
1551       
1552//      value = getRealValueFromHeader_2(fname,60,28,5,15,5)
1553        value = getRealValueFromHeader(fname,74)
1554       
1555        return(value)
1556end
1557
1558//source aperture diameter (millimeters)
1559Function getSourceApertureDiam(fname)
1560        String fname
1561       
1562        Variable value
1563       
1564        // your code returning value
1565       
1566        value  = 52
1567       
1568        return(value)
1569end
1570
1571//sample aperture diameter (millimeters)
1572Function getSampleApertureDiam(fname)
1573        String fname
1574       
1575        Variable value
1576       
1577        // your code returning value
1578//      value = getRealValueFromHeader_2(fname,60,28,5,15,3)
1579        value = getRealValueFromHeader(fname,72)
1580       
1581        value = 10
1582       
1583        return(value)
1584end
1585
1586//source AP to Sample AP distance (meters)
1587Function getSourceToSampleDist(fname)
1588        String fname
1589       
1590        Variable value
1591       
1592        // your code returning value
1593       
1594//      value = getRealValueFromHeader_2(fname,60,28,5,12,3)
1595       
1596        value = getRealValueFromHeader(fname,57)
1597       
1598        return(value)
1599end
1600
1601//wavelength (Angstroms)
1602Function getWavelength(fname)
1603        String fname
1604       
1605        Variable value
1606       
1607        // your code returning value
1608//      value = getRealValueFromHeader_2(fname,60,28,5,11,3)
1609        value = getRealValueFromHeader(fname,52)
1610       
1611        return(value)
1612end
1613
1614//wavelength spread (FWHM)
1615Function getWavelengthSpread(fname)
1616        String fname
1617       
1618        Variable value
1619       
1620        // your code returning value
1621        value = getRealValueFromHeader(fname,53)
1622
1623       
1624        return(value)
1625end
1626
1627// physical x-dimension of a detector pixel, in mm
1628Function getDetectorPixelXSize(fname)
1629        String fname
1630       
1631        Variable value
1632       
1633        // your code here returning value
1634//      value = getRealValueFromHeader_2(fname,60,28,5,11,5)
1635        value = getRealValueFromHeader(fname,54)
1636       
1637        return(value)
1638end
1639
1640// physical y-dimension of a detector pixel, in mm
1641Function getDetectorPixelYSize(fname)
1642        String fname
1643       
1644        Variable value
1645       
1646        // your code here returning value
1647//      value = getRealValueFromHeader_2(fname,60,28,5,12,1)
1648        value = getRealValueFromHeader(fname,55)
1649       
1650        return(value)
1651end
1652
1653//transmission detector count (unused, return 0)
1654//
1655Function getTransDetectorCounts(fname)
1656        String fname
1657       
1658        Variable value
1659       
1660        // your code returning value
1661       
1662        return(0)
1663end
1664
1665
1666//total count time (seconds)
1667// stored here as (s/10), so multiply by 10 ?
1668Function getCountTime(fname)
1669        String fname
1670       
1671        Variable value
1672       
1673        // your code returning value
1674       
1675//      value = getRealValueFromHeader_2(fname,60,28,5,1,3)  ///  line 1 col 3
1676       
1677        value = getRealValueFromHeader(fname,2)/10
1678       
1679        return(value)
1680end
1681
1682
1683//reads the wavelength from a reduced data file (not very reliable)
1684// - does not work with NSORTed files
1685// - only used in FIT/RPA (which itself is almost NEVER used...)
1686//
1687// DOES NOT NEED TO BE CHANGED IF USING NCNR DATA WRITER
1688Function GetLambdaFromReducedData(tempName)
1689        String tempName
1690       
1691        String junkString
1692        Variable lambdaFromFile, fileVar
1693        lambdaFromFile = 6.0
1694        Open/R/P=catPathName fileVar as tempName
1695        FReadLine fileVar, junkString
1696        FReadLine fileVar, junkString
1697        FReadLine fileVar, junkString
1698        if(strsearch(LowerStr(junkString),"lambda",0) != -1)
1699                FReadLine/N=11 fileVar, junkString
1700                FReadLine/N=10 fileVar, junkString
1701                lambdaFromFile = str2num(junkString)
1702        endif
1703        Close fileVar
1704       
1705        return(lambdaFromFile)
1706End
1707
1708/////   TRANSMISSION RELATED FUNCTIONS    ////////
1709//box coordinate are returned by reference
1710//
1711// box to sum over is bounded (x1,y1) to (x2,y2)
1712//
1713// this function returns the bounding coordinates as stored in
1714// the header
1715//
1716// if not using the NCNR Transmission module, this function default to
1717// returning 0000, and no changes needed
1718//
1719// filename is the full path:name to the file
1720Function getXYBoxFromFile(filename,x1,x2,y1,y2)
1721        String filename
1722        Variable &x1,&x2,&y1,&y2
1723
1724        // return your bounding box coordinates or default values of 0
1725        x1=getRealValueFromHeader(filename,120)
1726        x2=getRealValueFromHeader(filename,121)
1727        y1=getRealValueFromHeader(filename,122)
1728        y2=getRealValueFromHeader(filename,123)
1729       
1730//      print "in get", x1,x2,y1,y2
1731       
1732        return(0)
1733End
1734
1735// Writes the coordinates of the box to the header after user input
1736//
1737// box to sum over bounded (x1,y1) to (x2,y2)
1738//
1739// if not using the NCNR Transmission module, this function is null
1740//
1741// filename as passed in is a full path:filename
1742//
1743Function WriteXYBoxToHeader(filename,x1,x2,y1,y2)
1744        String filename
1745        Variable x1,x2,y1,y2
1746       
1747        // your code to write bounding box to the header, or nothing
1748       
1749        WriteReal(filename,x1,6804)
1750        WriteReal(filename,x2,6820)
1751        WriteReal(filename,y1,6836)
1752        WriteReal(filename,y2,6852)
1753       
1754        // use the last full line and the 4 first numbers
1755       
1756//      print "in write",x1,x2,y1,y2
1757       
1758        //  should start at 120  for read and  line 25
1759       
1760        /// 84 *81
1761       
1762        return(0)
1763End
1764
1765// for transmission calculation, writes an NCNR-specific alphanumeric identifier
1766// (suffix of the data file)
1767//
1768// if not using the NCNR Transmission module, this function is null
1769Function WriteAssocFileSuffixToHeader(fname,suffix)
1770        String fname,suffix
1771               
1772        // replace leading space(s) w/zero
1773        suffix = ReplaceString(" ", suffix, "0" )
1774               
1775        suffix = suffix[0,5]            //limit to 6 characters
1776       
1777        WriteText(fname,suffix,9350)
1778       
1779       
1780        return(0)
1781end
1782
1783Function/S getStringFromHeader(fname,start,num)
1784        String fname                            //full path:name
1785        Variable start,num              //starting byte and number of characters to read
1786       
1787        String str
1788        Variable refnum
1789        Open/R refNum as fname
1790        FSetPos refNum,start
1791        FReadLine/N=(num) refNum,str
1792        Close refnum
1793       
1794        return(str)
1795End
1796
1797Function getRealValueFromHeader(fname,start)
1798        String fname
1799        Variable start
1800        Variable numLinesLoaded = 0,firstchar,countTime
1801        variable refnum
1802        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v0,ii,valuesread,result
1803        String buffer
1804        Make/O/D/N=128 TemporaryHeader
1805//      Make/O/D/N=(128) data
1806
1807        Open/R refNum as fname          //if fname is "", a dialog will be presented
1808        if(refnum==0)
1809                return(1)               //user cancelled
1810        endif
1811
1812//skip the next 10 lines
1813        For(ii=0;ii<60;ii+=1)
1814                FReadLine refnum,buffer
1815        EndFor
1816
1817// read in the whole header
1818        For (ii=60;ii<188;ii+=1)
1819
1820                numlinesloaded = ii-60
1821                FReadLine refNum,buffer //assume a 2nd line is there, w/16 values
1822                sscanf buffer,"%g %g %g %g %g ",v0,v1,v2,v3,v4
1823                valuesRead = V_flag
1824//              print valuesRead
1825               
1826//              print buffer
1827               
1828                //valuesRead = V_flag
1829//              print ii,refnum,v0,v1,v2,v3,v4,v5,v6,v7,v8,v9
1830//              print buffer
1831               
1832                TemporaryHeader[5*numlinesloaded] = v0
1833                TemporaryHeader[5*numlinesloaded+1] = v1                //monitor
1834                TemporaryHeader[5*numlinesloaded+2] = v2
1835                TemporaryHeader[5*numlinesloaded+3] = v3
1836                TemporaryHeader[5*numlinesloaded+4] = v4
1837        Endfor
1838                                               
1839        Close refNum           
1840        result= TemporaryHeader[start]
1841//      KillWaves/Z TemporaryHeader
1842       
1843        return (result)
1844End
1845
1846//Function getRealValueFromHeader_1(fname,start)
1847//      String fname
1848//      Variable start
1849//
1850//      String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=1/W=1/Q"
1851//     
1852//      GBLoadStr += "/S="+num2str(start)+"/U=10" + "\"" + fname + "\""
1853//      Execute GBLoadStr
1854//      Wave w=$"tempGBWave0"
1855//     
1856//      return(w[0])
1857//End
1858
1859//Function getRealValueFromHeader_2(fname,start,numrow,numcol,rowbit,colbit)
1860//      String fname
1861//      Variable start,numcol,numrow,colbit,rowbit
1862//      variable result
1863//      make /O/D/N=(numrow) w0,w1,w2,w3,w4
1864//      Make/O/D/N=(numrow,numcol) dataread
1865//     
1866//             
1867//      loadwave  /A=tempGBwave/L={0, start, numrow, 0, numcol}/N/G/D/O/K=1 fname
1868//     
1869//      Wave/Z tempGBwave0=tempGBwave0
1870//      Wave/Z tempGBwave1=tempGBwave1
1871//      Wave/Z tempGBwave2=tempGBwave2
1872//      Wave/Z tempGBwave3=tempGBwave3
1873//      Wave/Z tempGBwave4=tempGBwave4
1874//     
1875//     
1876//      w0=tempGBWave0
1877//       w1=tempGBWave1
1878//       w2=tempGBWave2
1879//       w3=tempGBWave3
1880//       w4=tempGBWave4
1881//     
1882/////                   redimension could use that to redimension dataread in only one colunm... but will see later..
1883//
1884//
1885//
1886//      KillWaves/Z tempGBwave0,tempGBwave1,tempGBwave3,tempGBwave2,tempGBwave4
1887//      ///        Make/O/D/N=(numrow,numcol),wres
1888//     
1889//      dataread[][0] = w0[p]
1890//      dataread[][1] = w1[p]
1891//      dataread[][2] = w2[p]
1892//      dataread[][3] = w3[p]
1893//      dataread[][4] = w4[p]
1894//
1895//KillWaves/Z w0,w1,w2,w3,w4
1896//     
1897////    wave wres = $"tempGBwave"  +num2str(linebit-1)
1898//      result  = dataread[rowbit-1][colbit-1]
1899//     
1900//      return(result)
1901//End
1902
1903
1904//Function ReadILLData(fname)
1905//      String fname
1906//     
1907//      NVAR pixelsX = root:myGlobals:gNPixelsX
1908//      NVAR pixelsY = root:myGlobals:gNPixelsY
1909//      Variable refNum=0,ii,p1,p2,tot,num=pixelsX,numHdrLines=118   ///waas 20 for NIST
1910//      String str=""
1911//      //data is initially linear scale
1912//      Variable/G :gIsLogScale=0
1913//      Make/O/T/N=(numHdrLines) hdrLines
1914//      Make/O/D/N=(pixelsX*pixelsY) data       
1915//      Make/O/D/N=(1638,10)    datahelp        //,linear_data
1916//     
1917//      //full filename and path is now passed in...
1918//      //actually open the file
1919////    SetDataFolder destPath
1920//      Open/R/Z refNum as fname                // /Z flag means I must handle open errors
1921//      if(refnum==0)           //FNF error, get out
1922//              DoAlert 0,"Could not find file: "+fname
1923//              Close/A
1924//              SetDataFolder root:
1925//              return(1)
1926//      endif
1927//      if(V_flag!=0)
1928//              DoAlert 0,"File open error: V_flag="+num2Str(V_Flag)
1929//              Close/A
1930//              SetDataFolder root:
1931//              return(1)
1932//      Endif
1933//      //
1934//      for(ii=0;ii<numHdrLines;ii+=1)          //read (or skip) 18 header lines
1935//              FReadLine refnum,str
1936//              hdrLines[ii]=str
1937//      endfor
1938//      //     
1939//      Close refnum
1940//     
1941////    SetDataFolder destPath
1942//
1943//// loadwave  /A=tempt/L={0, 118, 1638, 1, 10}/N/J/D/O/E=1/K=1 fname
1944//
1945//loadwave  /A=tempt/L={0, 0, 1638, 0, 10}/N/G/D/O/E=1/K=1 fname
1946//
1947//Wave/Z tempt0=tempt0
1948//      Wave/Z tempt1=tempt1
1949//      Wave/Z tempt2=tempt2
1950//      Wave/Z tempt3=tempt3
1951//      Wave/Z tempt4=tempt4
1952//      Wave/Z tempt5=tempt5
1953//      Wave/Z tempt6=tempt6
1954//      Wave/Z tempt7=tempt7
1955//      Wave/Z tempt8=tempt8
1956//      Wave/Z tempt9=tempt9
1957//     
1958//     
1959//     
1960//     
1961//      datahelp[][0]=tempt0[p]
1962//      datahelp[][1]=tempt1[p]
1963//      datahelp[][2]=tempt2[p]
1964//      datahelp[][3]=tempt3[p]
1965//      datahelp[][4]=tempt4[p]
1966//      datahelp[][5]=tempt5[p]
1967//      datahelp[][6]=tempt6[p]
1968//      datahelp[][7]=tempt7[p]
1969//      datahelp[][8]=tempt8[p]
1970//      datahelp[][9]=tempt9[p]
1971//     
1972//      Killwaves/Z tempt1,tempt2,tempt3,tempt4,tempt0,tempt5,tempt6,tempt7,tempt8,tempt9
1973//
1974/////////////Wave/Z tempt0=tempt0
1975/////data=tempt
1976//     
1977//     
1978//      Redimension/N=(pixelsX,pixelsY) datahelp
1979//
1980//
1981////    LoadWave/Q/J/D/E=1/V={" " ,"non",1,1}/N=temp fName
1982////    Wave/Z temp0=temp0
1983////    data=temp0
1984////    Redimension/N=(pixelsX,pixelsY) data            //,linear_data
1985//     
1986//      //linear_data = data
1987//     
1988//      KillWaves/Z temp0
1989//     
1990//      //return the data folder to root
1991//      SetDataFolder root:
1992//     
1993//      Return(0)
1994//End
1995
1996
1997
1998Function ReadILLData2(fname,data)
1999        String fname
2000        wave data
2001
2002        Variable numLinesLoaded = 0,firstchar,countTime
2003        variable refnum
2004        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v0,ii,valuesread
2005        String buffer
2006        Make/O/D/N=16384 Dataline               //temporary storage
2007
2008        Open/R refNum as fname          //if fname is "", a dialog will be presented
2009        if(refnum==0)
2010                return(1)               //user cancelled
2011        endif
2012
2013//skip the next 10 lines
2014        For(ii=0;ii<118;ii+=1)
2015                FReadLine refnum,buffer
2016        EndFor
2017
2018        For (ii=118;ii<1757;ii+=1)
2019                numlinesloaded = ii-118
2020                FReadLine refNum,buffer //assume a 2nd line is there, w/16 values
2021                sscanf buffer,"%g %g %g %g %g %g %g %g %g %g",v0,v1,v2,v3,v4,v5,v6,v7,v8,v9
2022                valuesRead = V_flag
2023//              print valuesRead               
2024//               buffer         
2025                //valuesRead = V_flag
2026//              print ii,refnum,v0,v1,v2,v3,v4,v5,v6,v7,v8,v9
2027//              print buffer
2028               
2029                if(ii == 1756)
2030                        dataline[10*numlinesloaded] = v0                                //last line has only four values
2031                        dataline[10*numlinesloaded+1] = v1
2032                        dataline[10*numlinesloaded+2] = v2
2033                        dataline[10*numlinesloaded+3] = v3
2034                else
2035                        dataline[10*numlinesloaded] = v0                //reading in 10 values per line
2036                        dataline[10*numlinesloaded+1] = v1
2037                        dataline[10*numlinesloaded+2] = v2
2038                        dataline[10*numlinesloaded+3] = v3
2039                        dataline[10*numlinesloaded+4] = v4
2040                        dataline[10*numlinesloaded+5] = v5
2041                        dataline[10*numlinesloaded+6] = v6
2042                        dataline[10*numlinesloaded+7] = v7
2043                        dataline[10*numlinesloaded+8] = v8
2044                        dataline[10*numlinesloaded+9] = v9
2045                endif
2046        Endfor
2047                                               
2048        Close refNum   
2049       
2050        data = dataline
2051        redimension /N=(128,128) data
2052       
2053//      Killwaves/Z dataline
2054       
2055        return (0)
2056
2057end
2058
2059// Write* routines all must:
2060// (1) open file "fname". fname is a full file path and name to the file on disk
2061// (2) write the specified value to the header at the correct location in the file
2062// (3) close the file
2063
2064//
2065// ILL header values are 16 characters: one space followed by 15 bytes of the value, in scientific notation (pos 2 is the sign, last 4 are "e(+-)XX" )
2066// SRK - May 2008
2067Function WriteReal(path,value,start)
2068        string path
2069        variable value,start
2070
2071        variable refnum
2072       
2073        String valStr
2074        sprintf valStr, "  %14e",value
2075//      print valstr, strlen(valStr)
2076       
2077        Open/A/T= "????" refnum as path
2078       
2079        FStatus refnum
2080        FSetPos refnum, start   
2081        FBinWrite refNum, valStr       
2082        FSetpos refnum,V_logEOF         // correct length is 142317 total bytes
2083       
2084        Close refnum
2085       
2086        return (0)
2087
2088end
2089
2090
2091Function/S PromptForPathtest(msgStr)
2092        String msgStr
2093        String fullPath
2094        Variable refnum
2095       
2096        //this just asks for the filename, doesn't open the file
2097        Open/D/R/T="????"/M=(msgStr) refNum
2098        fullPath = S_FileName           //fname is the full path
2099        //      Print refnum,fullPath
2100       
2101        //null string is returned in S_FileName if user cancelled, and is passed back to calling  function
2102        Return(fullPath)
2103End
2104
2105
2106/// !!!! Make sure the text string is the correct LENGTH before sending it here!!!
2107// SRK - May 2008
2108Function WriteText(path,str,start)
2109        string path,str
2110        variable start
2111       
2112        variable refnum
2113       
2114        Open/A/T= "????TEXT" refnum as path
2115       
2116        FStatus refnum
2117        FSetPos refnum, start
2118        FBinWrite/F=0 refnum, str      //native object format (character)
2119        FSetPos refnum,V_logEOF
2120       
2121        Close refnum
2122       
2123        return (0)
2124
2125end
2126////// OCT 2009, facility specific bits from ProDiv()
2127//"type" is the data folder that has the corrected, patched, and normalized DIV data array
2128//
2129// the header of this file is rather unimportant. Filling in a title at least would be helpful/
2130//
2131Function Write_DIV_File(type)
2132        String type
2133       
2134        // Your file writing function here. Don't try to duplicate the VAX binary format...
2135       
2136        return(0)
2137End
2138
2139////// OCT 2009, facility specific bits from MonteCarlo functions()
2140//"type" is the data folder that has the data array that is to be (re)written as a full
2141// data file, as if it was a raw data file
2142//
2143// not really necessary
2144//
2145Function Write_RawData_File(type,fullpath,dialog)
2146        String type,fullpath
2147        Variable dialog         //=1 will present dialog for name
2148       
2149        // Your file writing function here. Don't try to duplicate the VAX binary format...
2150        Print "Write_RawData_File stub"
2151       
2152        return(0)
2153End
Note: See TracBrowser for help on using the repository browser.