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

Last change on this file since 376 was 376, checked in by srkline, 14 years ago

corrections to ILL R/W and utilities for parsing run numbers

  • Property svn:executable set to *
File size: 45.8 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 position 1  by myself
854       
855        // 16 bites between numbers and 81 per line
856       
857        return(0)
858End
859
860//whole transmission is NCNR-specific right now
861// leave this stub empty
862Function WriteWholeTransToHeader(fname,trans)
863        String fname
864        Variable trans
865       
866        // do nothing for now
867        WriteReal(fname,trans,6885)   /// //I will define at position last  lines  position 1  by myself
868       
869        return(0)
870End
871
872//box sum counts is a real value
873// used for transmission calculation module
874Function WriteBoxCountsToHeader(fname,counts)
875        String fname
876        Variable counts
877       
878        // do nothing if not using NCNR Transmission module
879       
880        WriteReal(fname,counts,6868)
881       
882        ////I will define at position 2   lines before the end and  last position   by myself
883       
884       
885        return(0)
886End
887
888//beam stop X-position
889// used for transmission module to manually tag transmission files
890Function WriteBSXPosToHeader(fname,xpos)
891        String fname
892        Variable xpos
893       
894        // do nothing if not using NCNR Transmission module
895       
896        WriteReal(fname,xpos,5119)    ////should do it for ypos for ILL ....NEED TO REMEMBER HERE
897        /// line 4 column 2   
898       
899        return(0)
900End
901
902//sample thickness in cm
903Function WriteThicknessToHeader(fname,num)
904        String fname
905        Variable num
906       
907        // your code here
908       
909        WriteReal(fname,num,5508)  //define at 9 lines  column 1 just above transmission by myself
910       
911        return(0)
912End
913
914//beam center X pixel location (detector coordinates)
915Function WriteBeamCenterXToHeader(fname,num)
916        String fname
917        Variable num
918       
919        // your code here
920       
921        // pos (1) on line 71 => 70 lines x 81 char
922        WriteReal(fname,num,5670)
923       
924        ///   line 11 column 1
925       
926        return(0)
927End
928
929//beam center Y pixel location (detector coordinates)
930Function WriteBeamCenterYToHeader(fname,num)
931        String fname
932        Variable num
933       
934        // your code here
935       
936        WriteReal(fname,num,5686)
937       
938        ///   line 11 column 2
939       
940        return(0)
941End
942
943//attenuator number (not its transmission)
944// if your beam attenuation is indexed in some way, use that number here
945// if not, write a 1 to the file here as a default
946//
947Function WriteAttenNumberToHeader(fname,num)
948        String fname
949        Variable num
950       
951        // your code here, default of 1
952        WriteReal(fname,num,5799)
953       
954        ///   line 12 column 4
955       
956        return(0)
957End
958
959// total monitor count during data collection
960Function WriteMonitorCountToHeader(fname,num)
961        String fname
962        Variable num
963       
964        // your code here
965       
966        WriteReal(fname,num,4924)
967       
968        ///   line 1 column 5
969       
970        return(0)
971End
972
973//total detector count
974Function WriteDetectorCountToHeader(fname,num)
975        String fname
976        Variable num
977       
978        // your code here
979       
980        WriteReal(fname,num,4908)
981       
982        ///   line 1 column 4
983       
984        return(0)
985End
986
987//transmission detector count
988// (currently unused in data reduction)
989Function WriteTransDetCountToHeader(fname,num)
990        String fname
991        Variable num
992       
993        // do nothing for now
994       
995        return(0)
996End
997
998//wavelength (Angstroms)
999Function WriteWavelengthToHeader(fname,num)
1000        String fname
1001        Variable num
1002       
1003        // your code here
1004        WriteReal(fname,num,5703)
1005       
1006        //   line 11 column 3
1007       
1008        return(0)
1009End
1010
1011//wavelength spread (FWHM)
1012Function WriteWavelengthDistrToHeader(fname,num)
1013        String fname
1014        Variable num
1015       
1016        // your code here
1017       
1018        WriteReal(fname,num,5719)
1019       
1020        //   line 11 column 4
1021       
1022        return(0)
1023End
1024
1025//temperature of the sample (C)
1026Function WriteTemperatureToHeader(fname,num)
1027        String fname
1028        Variable num
1029       
1030        //  your code here
1031       
1032        WriteReal(fname,num,5347)
1033       
1034        //   line 7 column 1
1035       
1036        return(0)
1037End
1038
1039//magnetic field (Oe)
1040Function WriteMagnFieldToHeader(fname,num)
1041        String fname
1042        Variable num
1043       
1044        // your code here
1045       
1046        return(0)
1047End
1048
1049//Source Aperture diameter (millimeters)
1050Function WriteSourceApDiamToHeader(fname,num)
1051        String fname
1052        Variable num
1053       
1054        // your code here
1055//      WriteReal(fname,num,5348)
1056      WriteReal(fname,num,6027)    ///4*81 = 324
1057     
1058      // line 15 colum 3
1059       
1060        return(0)
1061End
1062
1063//Sample Aperture diameter (millimeters)
1064Function WriteSampleApDiamToHeader(fname,num)
1065        String fname
1066        Variable num
1067       
1068        //your code here
1069        WriteReal(fname,num,6043)
1070       
1071            // line 15 colum 4
1072       
1073        return(0)
1074End
1075
1076//Source aperture to sample aperture distance (meters)
1077Function WriteSrcToSamDistToHeader(fname,num)
1078        String fname
1079        Variable num
1080       
1081        //      your code here
1082        WriteReal(fname,num,5784)  //it is collimation at ILL
1083       
1084       
1085        return(0)
1086End
1087
1088//lateral detector offset (centimeters)
1089Function WriteDetectorOffsetToHeader(fname,num)
1090        String fname
1091        Variable num
1092       
1093        //your code here
1094       
1095        WriteReal(fname,num,5849)
1096       
1097       
1098       
1099        // line 13 column 2
1100       
1101        return(0)
1102End
1103
1104//beam stop diameter (millimeters)
1105Function WriteBeamStopDiamToHeader(fname,num)
1106        String fname
1107        Variable num
1108       
1109        // your code here
1110        WriteReal(fname,num,6059)
1111       
1112        //line 15 column 5
1113       
1114        return(0)
1115End
1116
1117//sample to detector distance (meters)
1118Function WriteSDDToHeader(fname,num)
1119        String fname
1120        Variable num
1121       
1122        //your code here
1123        WriteReal(fname,num,5152)
1124       
1125        // line 4 column 4
1126       
1127        return(0)
1128End
1129
1130// physical dimension of detector x-pixel (mm)
1131Function WriteDetPixelXToHeader(fname,num)
1132        String fname
1133        Variable num
1134       
1135        //your code here
1136        WriteReal(fname,num,5735)
1137       
1138        //  line 11 column 5   
1139       
1140        return(0)
1141end
1142
1143// physical dimension of detector y-pixel (mm)
1144Function WriteDetPixelYToHeader(fname,num)
1145        String fname
1146        Variable num
1147       
1148        //your code here
1149        WriteReal(fname,num,5752)
1150       
1151        return(0)
1152end
1153
1154// sample label string
1155Function WriteSamLabelToHeader(fname,str)
1156        String fname,str
1157       
1158        // your code here
1159        WriteText(fname,"                              ",2075)
1160        WriteText(fname,str,2075)   //// need to change that in order to erase the title and write a new one
1161
1162        return(0)
1163End
1164
1165// total counting time (stored here as seconds/10??)
1166Function WriteCountTimeToHeader(fname,num)
1167        String fname
1168        Variable num
1169       
1170        // your code here
1171//      WriteReal(fname,num,4894)
1172        WriteReal(fname,10*num,4892)
1173       
1174       
1175       
1176        return(0)
1177End
1178
1179
1180
1181//////// ACCESSORS FOR READING DATA FROM THE HEADER  //////////////
1182//
1183// read specific bits of information from the header
1184// each of these operations MUST take care of open/close on their own
1185//
1186// fname is the full path and filname to the file on disk
1187// return values are either strings or real values as appropriate
1188//
1189//////
1190
1191
1192// function that reads in the 2D detector data and fills the array
1193// data[nx][ny] with the data values
1194// fname is the full name and path to the data file for opening and closing
1195//
1196//
1197Function getDetectorData(fname,data)
1198        String fname
1199        Wave data
1200       
1201       
1202        // your reader here
1203       
1204//      value = getRealValueFromHeader_2(fname,60,28,5,1,3)  // data start at 60+58 lines
1205        ReadILLData2(fname,data)
1206       
1207        return (0)
1208End
1209
1210// file suffix (NCNR data file name specific)
1211// return null string
1212Function/S getSuffix(fname)
1213        String fname
1214       
1215        return("")
1216End
1217
1218// associated file suffix (for transmission)
1219// NCNR Transmission calculation specific
1220// return null string
1221Function/S getAssociatedFileSuffix(fname)
1222        String fname
1223       
1224        return("")
1225End
1226
1227// sample label
1228Function/S getSampleLabel(fname)
1229        String fname
1230       
1231        String str
1232       
1233        // your code, returning str
1234        str = (getStringFromHeader(fname,2075,30))  /// 25*81  +  50////+51 30 lines before the end
1235       
1236       
1237        return(str)
1238End
1239
1240// file creation date
1241Function/S getFileCreationDate(fname)
1242        String fname
1243       
1244        String str
1245       
1246        // your code, returning str
1247       
1248        str = (getStringFromHeader(fname,2106,50))   //324 +12
1249       
1250        return(str)
1251End
1252
1253
1254Function/S getFileInstrument(fname)
1255        String fname
1256       
1257        String str
1258       
1259        // your code, returning str
1260       
1261        str = (getStringFromHeader(fname,324,3))   //324 +12
1262       
1263        return(str)
1264End
1265
1266
1267
1268
1269//monitor count
1270Function getMonitorCount(fname)
1271        String fname
1272       
1273        Variable value
1274       
1275        // your code returning value
1276       
1277//      value = getRealValueFromHeader_2(fname,60,28,5,1,5)  //
1278       
1279        value = getRealValueFromHeader(fname,4)
1280       
1281//      print value
1282       
1283        return(value)
1284end
1285
1286//saved monitor count
1287// NCNR specific for pre-processed data, never used
1288// return 0
1289Function getSavMon(fname)
1290        String fname
1291       
1292        Variable value
1293       
1294        // your code returning value
1295       
1296        return(0)
1297end
1298
1299//total detector count
1300Function getDetCount(fname)
1301        String fname
1302       
1303        Variable value
1304       
1305        // your code returning value
1306       
1307//      value = getRealValueFromHeader_2(fname,60,28,5,1,4)
1308       
1309        value = getRealValueFromHeader(fname,3)
1310       
1311        return(value)
1312end
1313
1314//Attenuator number, return 1 if your attenuators are not
1315// indexed by number
1316Function getAttenNumber(fname)
1317        String fname
1318       
1319        Variable value
1320       
1321        // your code returning value
1322       
1323//      value = getRealValueFromHeader_2(fname,60,28,5,56,4)
1324       
1325        value = getRealValueFromHeader(fname,58)
1326       
1327        return(value)
1328end
1329
1330//transmission
1331Function getSampleTrans(fname)
1332        String fname
1333       
1334        Variable value
1335       
1336        // your code returning value
1337       
1338        value = getRealValueFromHeader(fname,45)
1339       
1340        return(value)
1341end
1342
1343//box counts from stored transmission calculation
1344// return 0 if not using NCNR transmission module
1345Function getBoxCounts(fname)
1346        String fname
1347       
1348        Variable value
1349       
1350        // your code returning value
1351        value = getRealValueFromHeader(fname,124)
1352       
1353        return(value)
1354end
1355
1356//whole detector trasmission
1357// return 0 if not using NCNR transmission module
1358Function getSampleTransWholeDetector(fname)
1359        String fname
1360       
1361        Variable value
1362       
1363        // your code returning value
1364       
1365        return(value)
1366end
1367
1368//SampleThickness in centimeters
1369Function getSampleThickness(fname)
1370        String fname
1371       
1372        Variable value
1373       
1374        // your code returning value
1375        value = getRealValueFromHeader(fname,40)  //mm
1376       
1377        return(value)
1378end
1379
1380//Sample Rotation Angle (degrees)
1381Function getSampleRotationAngle(fname)
1382        String fname
1383       
1384        Variable value
1385       
1386        // your code returning value
1387        value = getRealValueFromHeader(fname,64)
1388       
1389        return(value)
1390end
1391
1392//temperature (C)
1393Function getTemperature(fname)
1394        String fname
1395       
1396        Variable value
1397       
1398        // your code returning value
1399       
1400//      value = getRealValueFromHeader_2(fname,60,28,5,7,1)
1401       
1402        value = getRealValueFromHeader(fname,30)
1403       
1404        return(value)
1405end
1406
1407//field strength (Oe)
1408Function getFieldStrength(fname)
1409        String fname
1410       
1411        Variable value
1412       
1413        // your code returning value
1414       
1415        return(value)
1416end
1417
1418//center of beam xPos in pixel coordinates
1419Function getBeamXPos(fname)
1420        String fname
1421       
1422        Variable value
1423       
1424        // your code returning value   
1425//      value = getRealValueFromHeader(fname,14)                //Lionel
1426        value = getRealValueFromHeader(fname,50)                //SRK
1427       
1428        return(value)
1429end
1430
1431//center of beam Y pos in pixel coordinates
1432Function getBeamYPos(fname)
1433        String fname
1434       
1435        Variable value
1436       
1437        // your code returning value
1438//      value = getRealValueFromHeader(fname,15)                //Lionel
1439        value = getRealValueFromHeader(fname,51)                //SRK
1440       
1441        return(value)
1442end
1443
1444//sample to detector distance (meters)
1445Function getSDD(fname)
1446        String fname
1447       
1448        Variable value
1449       
1450        // your code returning value
1451//      value = getRealValueFromHeader_2(fname,60,28,5,4,4)
1452        value = getRealValueFromHeader(fname,18)
1453       
1454        return(value)
1455end
1456
1457//lateral detector offset (centimeters)
1458Function getDetectorOffset(fname)
1459        String fname
1460       
1461        Variable value
1462       
1463        // your code returning value
1464//      value = getRealValueFromHeader_2(fname,60,28,5,13,2)
1465        value = getRealValueFromHeader(fname,61)
1466       
1467        return(value)
1468end
1469
1470//Beamstop diameter (millimeters)
1471Function getBSDiameter(fname)
1472        String fname
1473       
1474        Variable value
1475       
1476        // your code returning value
1477       
1478//      value = getRealValueFromHeader_2(fname,60,28,5,15,5)
1479        value = getRealValueFromHeader(fname,74)
1480       
1481        return(value)
1482end
1483
1484//source aperture diameter (millimeters)
1485Function getSourceApertureDiam(fname)
1486        String fname
1487       
1488        Variable value
1489       
1490        // your code returning value
1491       
1492        value  = 52
1493       
1494        return(value)
1495end
1496
1497//sample aperture diameter (millimeters)
1498Function getSampleApertureDiam(fname)
1499        String fname
1500       
1501        Variable value
1502       
1503        // your code returning value
1504//      value = getRealValueFromHeader_2(fname,60,28,5,15,3)
1505        value = getRealValueFromHeader(fname,72)
1506       
1507        value = 10
1508       
1509        return(value)
1510end
1511
1512//source AP to Sample AP distance (meters)
1513Function getSourceToSampleDist(fname)
1514        String fname
1515       
1516        Variable value
1517       
1518        // your code returning value
1519       
1520//      value = getRealValueFromHeader_2(fname,60,28,5,12,3)
1521       
1522        value = getRealValueFromHeader(fname,57)
1523       
1524        return(value)
1525end
1526
1527//wavelength (Angstroms)
1528Function getWavelength(fname)
1529        String fname
1530       
1531        Variable value
1532       
1533        // your code returning value
1534//      value = getRealValueFromHeader_2(fname,60,28,5,11,3)
1535        value = getRealValueFromHeader(fname,52)
1536       
1537        return(value)
1538end
1539
1540//wavelength spread (FWHM)
1541Function getWavelengthSpread(fname)
1542        String fname
1543       
1544        Variable value
1545       
1546        // your code returning value
1547        value = getRealValueFromHeader(fname,53)
1548
1549       
1550        return(value)
1551end
1552
1553// physical x-dimension of a detector pixel, in mm
1554Function getDetectorPixelXSize(fname)
1555        String fname
1556       
1557        Variable value
1558       
1559        // your code here returning value
1560//      value = getRealValueFromHeader_2(fname,60,28,5,11,5)
1561        value = getRealValueFromHeader(fname,54)
1562       
1563        return(value)
1564end
1565
1566// physical y-dimension of a detector pixel, in mm
1567Function getDetectorPixelYSize(fname)
1568        String fname
1569       
1570        Variable value
1571       
1572        // your code here returning value
1573//      value = getRealValueFromHeader_2(fname,60,28,5,12,1)
1574        value = getRealValueFromHeader(fname,55)
1575       
1576        return(value)
1577end
1578
1579//transmission detector count (unused, return 0)
1580//
1581Function getTransDetectorCounts(fname)
1582        String fname
1583       
1584        Variable value
1585       
1586        // your code returning value
1587       
1588        return(0)
1589end
1590
1591
1592//total count time (seconds)
1593// stored here as (s/10), so multiply by 10 ?
1594Function getCountTime(fname)
1595        String fname
1596       
1597        Variable value
1598       
1599        // your code returning value
1600       
1601//      value = getRealValueFromHeader_2(fname,60,28,5,1,3)  ///  line 1 col 3
1602       
1603        value = getRealValueFromHeader(fname,2)/10
1604       
1605        return(value)
1606end
1607
1608
1609//reads the wavelength from a reduced data file (not very reliable)
1610// - does not work with NSORTed files
1611// - only used in FIT/RPA (which itself is almost NEVER used...)
1612//
1613// DOES NOT NEED TO BE CHANGED IF USING NCNR DATA WRITER
1614Function GetLambdaFromReducedData(tempName)
1615        String tempName
1616       
1617        String junkString
1618        Variable lambdaFromFile, fileVar
1619        lambdaFromFile = 6.0
1620        Open/R/P=catPathName fileVar as tempName
1621        FReadLine fileVar, junkString
1622        FReadLine fileVar, junkString
1623        FReadLine fileVar, junkString
1624        if(strsearch(LowerStr(junkString),"lambda",0) != -1)
1625                FReadLine/N=11 fileVar, junkString
1626                FReadLine/N=10 fileVar, junkString
1627                lambdaFromFile = str2num(junkString)
1628        endif
1629        Close fileVar
1630       
1631        return(lambdaFromFile)
1632End
1633
1634/////   TRANSMISSION RELATED FUNCTIONS    ////////
1635//box coordinate are returned by reference
1636//
1637// box to sum over is bounded (x1,y1) to (x2,y2)
1638//
1639// this function returns the bounding coordinates as stored in
1640// the header
1641//
1642// if not using the NCNR Transmission module, this function default to
1643// returning 0000, and no changes needed
1644//
1645// filename is the full path:name to the file
1646Function getXYBoxFromFile(filename,x1,x2,y1,y2)
1647        String filename
1648        Variable &x1,&x2,&y1,&y2
1649
1650        // return your bounding box coordinates or default values of 0
1651        x1=getRealValueFromHeader(filename,120)
1652        x2=getRealValueFromHeader(filename,121)
1653        y1=getRealValueFromHeader(filename,122)
1654        y2=getRealValueFromHeader(filename,123)
1655       
1656//      print "in get", x1,x2,y1,y2
1657       
1658        return(0)
1659End
1660
1661// Writes the coordinates of the box to the header after user input
1662//
1663// box to sum over bounded (x1,y1) to (x2,y2)
1664//
1665// if not using the NCNR Transmission module, this function is null
1666//
1667// filename as passed in is a full path:filename
1668//
1669Function WriteXYBoxToHeader(filename,x1,x2,y1,y2)
1670        String filename
1671        Variable x1,x2,y1,y2
1672       
1673        // your code to write bounding box to the header, or nothing
1674       
1675        WriteReal(filename,x1,6804)
1676        WriteReal(filename,x2,6820)
1677        WriteReal(filename,y1,6836)
1678        WriteReal(filename,y2,6852)
1679       
1680        // use the last full line and the 4 first numbers
1681       
1682//      print "in write",x1,x2,y1,y2
1683       
1684        //  should start at 120  for read and  line 25
1685       
1686        /// 84 *81
1687       
1688        return(0)
1689End
1690
1691// for transmission calculation, writes an NCNR-specific alphanumeric identifier
1692// (suffix of the data file)
1693//
1694// if not using the NCNR Transmission module, this function is null
1695Function WriteAssocFileSuffixToHeader(fname,suffix)
1696        String fname,suffix
1697               
1698        // your code to write bounding box to the header, or nothing
1699       
1700        return(0)
1701end
1702
1703Function/S getStringFromHeader(fname,start,num)
1704        String fname                            //full path:name
1705        Variable start,num              //starting byte and number of characters to read
1706       
1707        String str
1708        Variable refnum
1709        Open/R refNum as fname
1710        FSetPos refNum,start
1711        FReadLine/N=(num) refNum,str
1712        Close refnum
1713       
1714        return(str)
1715End
1716
1717Function getRealValueFromHeader(fname,start)
1718        String fname
1719        Variable start
1720        Variable numLinesLoaded = 0,firstchar,countTime
1721        variable refnum
1722        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v0,ii,valuesread,result
1723        String buffer
1724        Make/O/D/N=128 TemporaryHeader
1725//      Make/O/D/N=(128) data
1726
1727        Open/R refNum as fname          //if fname is "", a dialog will be presented
1728        if(refnum==0)
1729                return(1)               //user cancelled
1730        endif
1731
1732//skip the next 10 lines
1733        For(ii=0;ii<60;ii+=1)
1734                FReadLine refnum,buffer
1735        EndFor
1736
1737// read in the whole header
1738        For (ii=60;ii<188;ii+=1)
1739
1740                numlinesloaded = ii-60
1741                FReadLine refNum,buffer //assume a 2nd line is there, w/16 values
1742                sscanf buffer,"%g %g %g %g %g ",v0,v1,v2,v3,v4
1743                valuesRead = V_flag
1744//              print valuesRead
1745               
1746//              print buffer
1747               
1748                //valuesRead = V_flag
1749//              print ii,refnum,v0,v1,v2,v3,v4,v5,v6,v7,v8,v9
1750//              print buffer
1751               
1752                TemporaryHeader[5*numlinesloaded] = v0
1753                TemporaryHeader[5*numlinesloaded+1] = v1                //monitor
1754                TemporaryHeader[5*numlinesloaded+2] = v2
1755                TemporaryHeader[5*numlinesloaded+3] = v3
1756                TemporaryHeader[5*numlinesloaded+4] = v4
1757        Endfor
1758                                               
1759        Close refNum           
1760        result= TemporaryHeader[start]
1761//      KillWaves/Z TemporaryHeader
1762       
1763        return (result)
1764End
1765
1766//Function getRealValueFromHeader_1(fname,start)
1767//      String fname
1768//      Variable start
1769//
1770//      String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=1/W=1/Q"
1771//     
1772//      GBLoadStr += "/S="+num2str(start)+"/U=10" + "\"" + fname + "\""
1773//      Execute GBLoadStr
1774//      Wave w=$"tempGBWave0"
1775//     
1776//      return(w[0])
1777//End
1778
1779//Function getRealValueFromHeader_2(fname,start,numrow,numcol,rowbit,colbit)
1780//      String fname
1781//      Variable start,numcol,numrow,colbit,rowbit
1782//      variable result
1783//      make /O/D/N=(numrow) w0,w1,w2,w3,w4
1784//      Make/O/D/N=(numrow,numcol) dataread
1785//     
1786//             
1787//      loadwave  /A=tempGBwave/L={0, start, numrow, 0, numcol}/N/G/D/O/K=1 fname
1788//     
1789//      Wave/Z tempGBwave0=tempGBwave0
1790//      Wave/Z tempGBwave1=tempGBwave1
1791//      Wave/Z tempGBwave2=tempGBwave2
1792//      Wave/Z tempGBwave3=tempGBwave3
1793//      Wave/Z tempGBwave4=tempGBwave4
1794//     
1795//     
1796//      w0=tempGBWave0
1797//       w1=tempGBWave1
1798//       w2=tempGBWave2
1799//       w3=tempGBWave3
1800//       w4=tempGBWave4
1801//     
1802/////                   redimension could use that to redimension dataread in only one colunm... but will see later..
1803//
1804//
1805//
1806//      KillWaves/Z tempGBwave0,tempGBwave1,tempGBwave3,tempGBwave2,tempGBwave4
1807//      ///        Make/O/D/N=(numrow,numcol),wres
1808//     
1809//      dataread[][0] = w0[p]
1810//      dataread[][1] = w1[p]
1811//      dataread[][2] = w2[p]
1812//      dataread[][3] = w3[p]
1813//      dataread[][4] = w4[p]
1814//
1815//KillWaves/Z w0,w1,w2,w3,w4
1816//     
1817////    wave wres = $"tempGBwave"  +num2str(linebit-1)
1818//      result  = dataread[rowbit-1][colbit-1]
1819//     
1820//      return(result)
1821//End
1822
1823
1824//Function ReadILLData(fname)
1825//      String fname
1826//     
1827//      NVAR pixelsX = root:myGlobals:gNPixelsX
1828//      NVAR pixelsY = root:myGlobals:gNPixelsY
1829//      Variable refNum=0,ii,p1,p2,tot,num=pixelsX,numHdrLines=118   ///waas 20 for NIST
1830//      String str=""
1831//      //data is initially linear scale
1832//      Variable/G :gIsLogScale=0
1833//      Make/O/T/N=(numHdrLines) hdrLines
1834//      Make/O/D/N=(pixelsX*pixelsY) data       
1835//      Make/O/D/N=(1638,10)    datahelp        //,linear_data
1836//     
1837//      //full filename and path is now passed in...
1838//      //actually open the file
1839////    SetDataFolder destPath
1840//      Open/R/Z refNum as fname                // /Z flag means I must handle open errors
1841//      if(refnum==0)           //FNF error, get out
1842//              DoAlert 0,"Could not find file: "+fname
1843//              Close/A
1844//              SetDataFolder root:
1845//              return(1)
1846//      endif
1847//      if(V_flag!=0)
1848//              DoAlert 0,"File open error: V_flag="+num2Str(V_Flag)
1849//              Close/A
1850//              SetDataFolder root:
1851//              return(1)
1852//      Endif
1853//      //
1854//      for(ii=0;ii<numHdrLines;ii+=1)          //read (or skip) 18 header lines
1855//              FReadLine refnum,str
1856//              hdrLines[ii]=str
1857//      endfor
1858//      //     
1859//      Close refnum
1860//     
1861////    SetDataFolder destPath
1862//
1863//// loadwave  /A=tempt/L={0, 118, 1638, 1, 10}/N/J/D/O/E=1/K=1 fname
1864//
1865//loadwave  /A=tempt/L={0, 0, 1638, 0, 10}/N/G/D/O/E=1/K=1 fname
1866//
1867//Wave/Z tempt0=tempt0
1868//      Wave/Z tempt1=tempt1
1869//      Wave/Z tempt2=tempt2
1870//      Wave/Z tempt3=tempt3
1871//      Wave/Z tempt4=tempt4
1872//      Wave/Z tempt5=tempt5
1873//      Wave/Z tempt6=tempt6
1874//      Wave/Z tempt7=tempt7
1875//      Wave/Z tempt8=tempt8
1876//      Wave/Z tempt9=tempt9
1877//     
1878//     
1879//     
1880//     
1881//      datahelp[][0]=tempt0[p]
1882//      datahelp[][1]=tempt1[p]
1883//      datahelp[][2]=tempt2[p]
1884//      datahelp[][3]=tempt3[p]
1885//      datahelp[][4]=tempt4[p]
1886//      datahelp[][5]=tempt5[p]
1887//      datahelp[][6]=tempt6[p]
1888//      datahelp[][7]=tempt7[p]
1889//      datahelp[][8]=tempt8[p]
1890//      datahelp[][9]=tempt9[p]
1891//     
1892//      Killwaves/Z tempt1,tempt2,tempt3,tempt4,tempt0,tempt5,tempt6,tempt7,tempt8,tempt9
1893//
1894/////////////Wave/Z tempt0=tempt0
1895/////data=tempt
1896//     
1897//     
1898//      Redimension/N=(pixelsX,pixelsY) datahelp
1899//
1900//
1901////    LoadWave/Q/J/D/E=1/V={" " ,"non",1,1}/N=temp fName
1902////    Wave/Z temp0=temp0
1903////    data=temp0
1904////    Redimension/N=(pixelsX,pixelsY) data            //,linear_data
1905//     
1906//      //linear_data = data
1907//     
1908//      KillWaves/Z temp0
1909//     
1910//      //return the data folder to root
1911//      SetDataFolder root:
1912//     
1913//      Return(0)
1914//End
1915
1916
1917
1918Function ReadILLData2(fname,data)
1919        String fname
1920        wave data
1921
1922        Variable numLinesLoaded = 0,firstchar,countTime
1923        variable refnum
1924        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v0,ii,valuesread
1925        String buffer
1926        Make/O/D/N=16384 Dataline               //temporary storage
1927
1928        Open/R refNum as fname          //if fname is "", a dialog will be presented
1929        if(refnum==0)
1930                return(1)               //user cancelled
1931        endif
1932
1933//skip the next 10 lines
1934        For(ii=0;ii<118;ii+=1)
1935                FReadLine refnum,buffer
1936        EndFor
1937
1938        For (ii=118;ii<1757;ii+=1)
1939                numlinesloaded = ii-118
1940                FReadLine refNum,buffer //assume a 2nd line is there, w/16 values
1941                sscanf buffer,"%g %g %g %g %g %g %g %g %g %g",v0,v1,v2,v3,v4,v5,v6,v7,v8,v9
1942                valuesRead = V_flag
1943//              print valuesRead               
1944//               buffer         
1945                //valuesRead = V_flag
1946//              print ii,refnum,v0,v1,v2,v3,v4,v5,v6,v7,v8,v9
1947//              print buffer
1948               
1949                if(ii == 1756)
1950                        dataline[10*numlinesloaded] = v0                                //last line has only four values
1951                        dataline[10*numlinesloaded+1] = v1
1952                        dataline[10*numlinesloaded+2] = v2
1953                        dataline[10*numlinesloaded+3] = v3
1954                else
1955                        dataline[10*numlinesloaded] = v0                //reading in 10 values per line
1956                        dataline[10*numlinesloaded+1] = v1
1957                        dataline[10*numlinesloaded+2] = v2
1958                        dataline[10*numlinesloaded+3] = v3
1959                        dataline[10*numlinesloaded+4] = v4
1960                        dataline[10*numlinesloaded+5] = v5
1961                        dataline[10*numlinesloaded+6] = v6
1962                        dataline[10*numlinesloaded+7] = v7
1963                        dataline[10*numlinesloaded+8] = v8
1964                        dataline[10*numlinesloaded+9] = v9
1965                endif
1966        Endfor
1967                                               
1968        Close refNum   
1969       
1970        data = dataline
1971        redimension /N=(128,128) data
1972       
1973//      Killwaves/Z dataline
1974       
1975        return (0)
1976
1977end
1978
1979// Write* routines all must:
1980// (1) open file "fname". fname is a full file path and name to the file on disk
1981// (2) write the specified value to the header at the correct location in the file
1982// (3) close the file
1983
1984//
1985// 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" )
1986// SRK - May 2008
1987Function WriteReal(path,value,start)
1988        string path
1989        variable value,start
1990
1991        variable refnum
1992       
1993        String valStr
1994        sprintf valStr, "  %14e",value
1995//      print valstr, strlen(valStr)
1996       
1997        Open/A/T= "????" refnum as path
1998       
1999        FStatus refnum
2000        FSetPos refnum, start   
2001        FBinWrite refNum, valStr       
2002        FSetpos refnum,V_logEOF         // correct length is 142317 total bytes
2003       
2004        Close refnum
2005       
2006        return (0)
2007
2008end
2009
2010
2011Function/S PromptForPathtest(msgStr)
2012        String msgStr
2013        String fullPath
2014        Variable refnum
2015       
2016        //this just asks for the filename, doesn't open the file
2017        Open/D/R/T="????"/M=(msgStr) refNum
2018        fullPath = S_FileName           //fname is the full path
2019        //      Print refnum,fullPath
2020       
2021        //null string is returned in S_FileName if user cancelled, and is passed back to calling  function
2022        Return(fullPath)
2023End
2024
2025
2026/// !!!! Make sure the text string is the correct LENGTH before sending it here!!!
2027// SRK - May 2008
2028Function WriteText(path,value,start)
2029        string path,value
2030        variable start
2031       
2032        variable refnum
2033       
2034        Open/A/T= "????" refnum as path
2035       
2036        FStatus refnum
2037        FSetPos refnum, start
2038        FBinWrite refNum,value
2039        FSetPos refnum,V_logEOF
2040       
2041        Close refnum
2042       
2043        return (0)
2044
2045end
Note: See TracBrowser for help on using the repository browser.