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

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

Several changes:

New version of ILL_DataReadWrite. Some changes added to Lionel's work to get the transmission calculation working corectly.

Changes to the wrapper to get the cursors on/off working correctly, as well as USANS matrix recalculation during normal fitting and during Global fitting. It may ask to recalculate the matrix occasionally when using the full data set - even though it really doesn't need to - but this is as a precaution.

Re-worked the header of the GRASP-export ASCII data to much more closely match the VAX output. I couldn't find any problem with the data block, so maybe GRASP was having trouble with the header.

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