source: sans/Release/trunk/NCNR_User_Procedures/SANS/Reduction/ILL_DataReadWrite.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

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