source: sans/SANSReduction/branches/kline_29MAR07/Put in User Procedures/SANS_Reduction_v5.00/NCNR_DataReadWrite.ipf @ 71

Last change on this file since 71 was 71, checked in by srkline, 16 years ago

many changes to PatchFiles?.ipf to get the reads and writes separated
and put into NCNR_DataReadWrite.ipf

File size: 29.0 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion = 5.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
23//(typically called from main panel button)
24//
25Proc LoadSANSData()
26        String msgStr = "Select a SANS data file"
27       
28        Variable err
29        err = ReadBinarySANS(msgStr)            //will prompt for file
30        //ignore the error here
31End
32
33
34//function called by the main entry procedure
35//sets global display variable, reads in the data, and displays it
36//displays alert, and returns error if no file was selected
37//
38// calling routine must decide whether to abort the execution or not...
39Function ReadBinarySANS(msgStr)
40        String msgStr
41
42        String filename=""
43
44        //each routine is responsible for checking the current (displayed) data folder
45        //selecting it, and returning to root when done
46        PathInfo/S catPathName          //should set the next dialog to the proper path...
47        //get the filename, then read it in
48        filename = PromptForPath(msgStr)
49        //check for cancel from dialog
50        if(strlen(filename)==0)
51                //user cancelled, abort
52                SetDataFolder root:
53                DoAlert 0, "No file selected, action aborted"
54                return(1)
55        Endif
56        //Print  "GetFileNameFromPath(filename) = " +  GetFileNameFromPathNoSemi(filename)
57        ReadHeaderAndData(filename)     //this is the full Path+file
58
59        //the calling macro must change the display type
60        String/G root:myGlobals:gDataDisplayType="RAW"          //displayed data type is raw
61       
62        //data is displayed here
63        fRawWindowHook()
64       
65        Return(0)
66End
67
68//function that does the guts of reading the binary data file
69//fname is the full path:name;vers required to open the file
70//VAX record markers are skipped as needed
71//the data as read in is in compressed I*2 format, and is decompressed
72//immediately after being read in. The final root:RAW:data wave is the real
73//neutron counts and can be directly operated on
74//header information is put into three wave, integers,reals, and text
75//logicals in the header are currently skipped, since they are no use in the
76//data reduction process. the logicals, however are left untouched.
77//
78Function ReadHeaderAndData(fname)
79        String fname
80        //this function is for reading in RAW data only, so it will always put data in RAW folder
81        String curPath = "root:RAW"
82        SetDataFolder curPath           //use the full path, so it will always work
83        Variable/G root:RAW:gIsLogScale = 0             //initial state is linear, keep this in RAW folder
84       
85        Variable refNum,integer,realval
86        String sansfname,textstr
87       
88        Make/O/N=23 $"root:RAW:IntegersRead"
89        Make/O/N=52 $"root:RAW:RealsRead"
90        Make/O/T/N=11 $"root:RAW:TextRead"
91       
92        Wave intw=$"root:RAW:IntegersRead"
93        Wave realw=$"root:RAW:RealsRead"
94        Wave/T textw=$"root:RAW:TextRead"
95       
96        //***NOTE ****
97        // the "current path" gets mysteriously reset to "root:" after the SECOND pass through
98        // this read routine, after the open dialog is presented
99        // the "--read" waves end up in the correct folder, but the data does not! Why?
100        //must re-set data folder before writing data array (done below)
101       
102        //full filename and path is now passed in...
103        //actually open the file
104        Open/R refNum as fname
105        //skip first two bytes (VAX record length markers, not needed here)
106        FSetPos refNum, 2
107        //read the next 21 bytes as characters (fname)
108        FReadLine/N=21 refNum,textstr
109        textw[0]= textstr
110        //read four i*4 values  /F=3 flag, B=3 flag
111        FBinRead/F=3/B=3 refNum, integer
112        intw[0] = integer
113        //
114        FBinRead/F=3/B=3 refNum, integer
115        intw[1] = integer
116        //
117        FBinRead/F=3/B=3 refNum, integer
118        intw[2] = integer
119        //
120        FBinRead/F=3/B=3 refNum, integer
121        intw[3] = integer
122        // 6 text fields
123        FSetPos refNum,55               //will start reading at byte 56
124        FReadLine/N=20 refNum,textstr
125        textw[1]= textstr
126        FReadLine/N=3 refNum,textstr
127        textw[2]= textstr
128        FReadLine/N=11 refNum,textstr
129        textw[3]= textstr
130        FReadLine/N=1 refNum,textstr
131        textw[4]= textstr
132        FReadLine/N=8 refNum,textstr
133        textw[5]= textstr
134        FReadLine/N=60 refNum,textstr
135        textw[6]= textstr
136       
137        //3 integers
138        FSetPos refNum,174
139        FBinRead/F=3/B=3 refNum, integer
140        intw[4] = integer
141        FBinRead/F=3/B=3 refNum, integer
142        intw[5] = integer
143        FBinRead/F=3/B=3 refNum, integer
144        intw[6] = integer
145       
146        //2 integers, 3 text fields
147        FSetPos refNum,194
148        FBinRead/F=3/B=3 refNum, integer
149        intw[7] = integer
150        FBinRead/F=3/B=3 refNum, integer
151        intw[8] = integer
152        FReadLine/N=6 refNum,textstr
153        textw[7]= textstr
154        FReadLine/N=6 refNum,textstr
155        textw[8]= textstr
156        FReadLine/N=6 refNum,textstr
157        textw[9]= textstr
158       
159        //2 integers
160        FSetPos refNum,244
161        FBinRead/F=3/B=3 refNum, integer
162        intw[9] = integer
163        FBinRead/F=3/B=3 refNum, integer
164        intw[10] = integer
165       
166        //2 integers
167        FSetPos refNum,308
168        FBinRead/F=3/B=3 refNum, integer
169        intw[11] = integer
170        FBinRead/F=3/B=3 refNum, integer
171        intw[12] = integer
172       
173        //2 integers
174        FSetPos refNum,332
175        FBinRead/F=3/B=3 refNum, integer
176        intw[13] = integer
177        FBinRead/F=3/B=3 refNum, integer
178        intw[14] = integer
179       
180        //3 integers
181        FSetPos refNum,376
182        FBinRead/F=3/B=3 refNum, integer
183        intw[15] = integer
184        FBinRead/F=3/B=3 refNum, integer
185        intw[16] = integer
186        FBinRead/F=3/B=3 refNum, integer
187        intw[17] = integer
188       
189        //1 text field
190        FSetPos refNum,404
191        FReadLine/N=42 refNum,textstr
192        textw[10]= textstr
193       
194        //1 integer
195        FSetPos refNum,458
196        FBinRead/F=3/B=3 refNum, integer
197        intw[18] = integer
198       
199        //4 integers
200        FSetPos refNum,478
201        FBinRead/F=3/B=3 refNum, integer
202        intw[19] = integer
203        FBinRead/F=3/B=3 refNum, integer
204        intw[20] = integer
205        FBinRead/F=3/B=3 refNum, integer
206        intw[21] = integer
207        FBinRead/F=3/B=3 refNum, integer
208        intw[22] = integer
209       
210        Close refNum
211       
212        //now get all of the reals
213        //
214        //Do all the GBLoadWaves at the end
215        //
216        //FBinRead Cannot handle 32 bit VAX floating point
217        //GBLoadWave, however, can properly read it
218        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
219        String strToExecute
220        //append "/S=offset/U=numofreals" to control the read
221        // then append fname to give the full file path
222        // then execute
223       
224        Variable a=0,b=0
225       
226        SetDataFolder curPath
227       
228        // 4 R*4 values
229        strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + fname + "\""
230        Execute strToExecute
231        Wave w=$"root:RAW:tempGBWave0"
232        b=4     //num of reals read
233        realw[a,a+b-1] = w[p-a]
234        a+=b
235       
236        // 4 R*4 values
237        SetDataFolder curPath
238        strToExecute = GBLoadStr + "/S=158/U=4" + "\"" + fname + "\""
239        Execute strToExecute
240        b=4     
241        realw[a,a+b-1] = w[p-a]
242        a+=b
243
244///////////
245        // 2 R*4 values
246        SetDataFolder curPath
247        strToExecute = GBLoadStr + "/S=186/U=2" + "\"" + fname + "\""
248        Execute strToExecute
249        b=2     
250        realw[a,a+b-1] = w[p-a]
251        a+=b
252
253        // 6 R*4 values
254        SetDataFolder curPath
255        strToExecute = GBLoadStr + "/S=220/U=6" + "\"" + fname + "\""
256        Execute strToExecute
257        b=6     
258        realw[a,a+b-1] = w[p-a]
259        a+=b
260       
261        // 13 R*4 values
262        SetDataFolder curPath
263        strToExecute = GBLoadStr + "/S=252/U=13" + "\"" + fname + "\""
264        Execute strToExecute
265        b=13
266        realw[a,a+b-1] = w[p-a]
267        a+=b
268       
269        // 3 R*4 values
270        SetDataFolder curPath
271        strToExecute = GBLoadStr + "/S=320/U=3" + "\"" + fname + "\""
272        Execute strToExecute
273        b=3     
274        realw[a,a+b-1] = w[p-a]
275        a+=b
276       
277        // 7 R*4 values
278        SetDataFolder curPath
279        strToExecute = GBLoadStr + "/S=348/U=7" + "\"" + fname + "\""
280        Execute strToExecute
281        b=7
282        realw[a,a+b-1] = w[p-a]
283        a+=b
284       
285        // 4 R*4 values
286        SetDataFolder curPath
287        strToExecute = GBLoadStr + "/S=388/U=4" + "\"" + fname + "\""
288        Execute strToExecute
289        b=4     
290        realw[a,a+b-1] = w[p-a]
291        a+=b
292       
293        // 2 R*4 values
294        SetDataFolder curPath
295        strToExecute = GBLoadStr + "/S=450/U=2" + "\"" + fname + "\""
296        Execute strToExecute
297        b=2
298        realw[a,a+b-1] = w[p-a]
299        a+=b
300       
301        // 2 R*4 values
302        SetDataFolder curPath
303        strToExecute = GBLoadStr + "/S=470/U=2" + "\"" + fname + "\""
304        Execute strToExecute
305        b=2
306        realw[a,a+b-1] = w[p-a]
307        a+=b
308       
309        // 5 R*4 values
310        SetDataFolder curPath
311        strToExecute = GBLoadStr + "/S=494/U=5" + "\"" + fname + "\""
312        Execute strToExecute
313        b=5     
314        realw[a,a+b-1] = w[p-a]
315       
316        //if the binary VAX data ws transferred to a MAC, all is OK
317        //if the data was trasnferred to an Intel machine (IBM), all the real values must be
318        //divided by 4 to get the correct floating point values
319        // I can't find any combination of settings in GBLoadWave or FBinRead to read data in correctly
320        // on an Intel machine.
321        //With the corrected version of GBLoadWave XOP (v. 1.43 or higher) Mac and PC both read
322        //VAX reals correctly, and no checking is necessary 12 APR 99
323        //if(cmpstr("Macintosh",IgorInfo(2)) == 0)
324                //do nothing
325        //else
326                //either Windows or Windows NT
327                //realw /= 4
328        //endif
329       
330        SetDataFolder curPath
331        //read in the data
332        strToExecute = "GBLoadWave/O/N=tempGBwave/B/T={16,2}/S=514/Q" + "\"" + fname + "\""
333        Execute strToExecute
334
335        SetDataFolder curPath           //use the full path, so it will always work
336       
337        Make/O/N=16384 $"root:RAW:data"
338        WAVE data=$"root:RAW:data"
339        SkipAndDecompressVAX(w,data)
340        Redimension/N=(128,128) data                    //NIST raw data is 128x128 - do not generalize
341       
342        //keep a string with the filename in the RAW folder
343        String/G root:RAW:fileList = textw[0]
344       
345        //set the globals to the detector dimensions (pixels)
346        Variable/G root:myGlobals:gNPixelsX=128         //default for Ordela data (also set in Initialize.ipf)
347        Variable/G root:myGlobals:gNPixelsY=128
348//      if(cmpstr(textW[9],"ILL   ")==0)                //override if OLD Cerca data
349//              Variable/G root:myGlobals:gNPixelsX=64
350//              Variable/G root:myGlobals:gNPixelsY=64
351//      endif
352       
353        //clean up - get rid of w = $"root:RAW:tempGBWave0"
354        KillWaves/Z w
355       
356        //return the data folder to root
357        SetDataFolder root:
358       
359        Return 0
360
361End
362
363
364//function to take the I*2 data that was read in, in VAX format
365//where the integers are "normal", but there are 2-byte record markers
366//sprinkled evenly through the data
367//there are skipped, leaving 128x128=16384 data values
368//the input array (in) is larger than 16384
369//(out) is 128x128 data (single precision) as defined in ReadHeaderAndData()
370//
371Function SkipAndDecompressVAX(in,out)
372        Wave in,out
373       
374        Variable skip,ii
375
376        ii=0
377        skip=0
378        do
379                if(mod(ii+skip,1022)==0)
380                        skip+=1
381                endif
382                out[ii] = Decompress(in[ii+skip])
383                ii+=1
384        while(ii<16384)
385        return(0)
386End
387
388//decompresses each I*2 data value to its real I*4 value
389//using the decompression routine written by Jim Ryhne, many moons ago
390//
391// the compression routine (not shown here, contained in the VAX fortran RW_DATAFILE.FOR) maps I4 to I2 values.
392// (back in the days where disk space *really* mattered). the I4toI2 function spit out:
393// I4toI2 = I4                                                          when I4 in [0,32767]
394// I4toI2 = -777                                                        when I4 in [2,767,000,...]
395// I4toI2 mapped to -13277 to -32768    otherwise
396//
397// the mapped values [-776,-1] and [-13276,-778] are not used.
398// in this compression scheme, only 4 significant digits are retained (to allow room for the exponent)
399// technically, the maximum value should be 2,768,499 since this maps to -32768. But this is of
400// little consequence. If you have individual pixel values on the detector that are that large, you need
401// to re-think your experiment.
402Function Decompress(val)
403        Variable val
404
405        Variable i4,npw,ipw,ib,nd
406
407        ib=10
408        nd=4
409        ipw=ib^nd
410        i4=val
411
412        if (i4 <= -ipw)
413                npw=trunc(-i4/ipw)
414                i4=mod(-i4,ipw)*(ib^npw)
415                return i4
416        else
417                return i4
418        endif
419End
420
421//****************
422//main entry procedure for reading a "WORK.DIV" file
423//displays a quick image of the  file, to check that it's correct
424//data is deposited in root:DIV data folder
425//
426Proc ReadWork_DIV()
427//      Silent 1
428       
429        String fname = PromptForPath("Select detector sensitivity file")
430        ReadHeaderAndWork("DIV",fname)          //puts what is read in work.div
431       
432        String waveStr = "root:DIV:data"
433        NewImage/F/K=1/S=2 $waveStr             //this is an experimental IGOR operation
434        ModifyImage '' ctab= {*,*,YellowHot,0}
435        //Display;AppendImage $waveStr
436       
437        //change the title string to WORK.DIV, rather than PLEXnnn_TST_asdfa garbage
438        String/G root:DIV:fileList = "WORK.DIV"
439       
440        SetDataFolder root:             //(redundant)
441//      Silent 0
442End
443
444
445//this function is the guts of reading a binary VAX file of real (4-byte) values
446// (i.e. a WORK.aaa file)
447// work files have the same header structure as RAW SANS data, just with
448//different data (real, rather than compressed integer data)
449//
450//************
451//this routine incorrectly reads in several data values where the VAX record
452//marker splits the 4-byte real (at alternating record markers)
453//this error seems to not be severe, but shoud be corrected (at great pain)
454//************
455//
456Function ReadHeaderAndWork(type,fname)
457        String type,fname
458       
459        //type is the desired folder to read the workfile to
460        //this data will NOT be automatically displayed gDataDisplayType is unchanged
461
462//      SVAR cur_folder=root:myGlobals:gDataDisplayType
463        String cur_folder = type
464        String curPath = "root:"+cur_folder
465        SetDataFolder curPath           //use the full path, so it will always work
466       
467        Variable refNum,integer,realval
468        String sansfname,textstr
469        Variable/G $(curPath + ":gIsLogScale") = 0              //initial state is linear, keep this in DIV folder
470       
471        Make/O/N=23 $(curPath + ":IntegersRead")
472        Make/O/N=52 $(curPath + ":RealsRead")
473        Make/O/T/N=11 $(curPath + ":TextRead")
474       
475        WAVE intw=$(curPath + ":IntegersRead")
476        WAVE realw=$(curPath + ":RealsRead")
477        WAVE/T textw=$(curPath + ":TextRead")
478       
479        //***NOTE ****
480        // the "current path" gets mysteriously reset to "root:" after the SECOND pass through
481        // this read routine, after the open dialog is presented
482        // the "--read" waves end up in the correct folder, but the data does not! Why?
483        //must re-set data folder before writing data array (done below)
484       
485        SetDataFolder curPath
486       
487        //actually open the file
488        Open/R refNum as fname
489        //skip first two bytes
490        FSetPos refNum, 2
491        //read the next 21 bytes as characters (fname)
492        FReadLine/N=21 refNum,textstr
493        textw[0]= textstr
494        //read four i*4 values  /F=3 flag, B=3 flag
495        FBinRead/F=3/B=3 refNum, integer
496        intw[0] = integer
497        //
498        FBinRead/F=3/B=3 refNum, integer
499        intw[1] = integer
500        //
501        FBinRead/F=3/B=3 refNum, integer
502        intw[2] = integer
503        //
504        FBinRead/F=3/B=3 refNum, integer
505        intw[3] = integer
506        // 6 text fields
507        FSetPos refNum,55               //will start reading at byte 56
508        FReadLine/N=20 refNum,textstr
509        textw[1]= textstr
510        FReadLine/N=3 refNum,textstr
511        textw[2]= textstr
512        FReadLine/N=11 refNum,textstr
513        textw[3]= textstr
514        FReadLine/N=1 refNum,textstr
515        textw[4]= textstr
516        FReadLine/N=8 refNum,textstr
517        textw[5]= textstr
518        FReadLine/N=60 refNum,textstr
519        textw[6]= textstr
520       
521        //3 integers
522        FSetPos refNum,174
523        FBinRead/F=3/B=3 refNum, integer
524        intw[4] = integer
525        FBinRead/F=3/B=3 refNum, integer
526        intw[5] = integer
527        FBinRead/F=3/B=3 refNum, integer
528        intw[6] = integer
529       
530        //2 integers, 3 text fields
531        FSetPos refNum,194
532        FBinRead/F=3/B=3 refNum, integer
533        intw[7] = integer
534        FBinRead/F=3/B=3 refNum, integer
535        intw[8] = integer
536        FReadLine/N=6 refNum,textstr
537        textw[7]= textstr
538        FReadLine/N=6 refNum,textstr
539        textw[8]= textstr
540        FReadLine/N=6 refNum,textstr
541        textw[9]= textstr
542       
543        //2 integers
544        FSetPos refNum,244
545        FBinRead/F=3/B=3 refNum, integer
546        intw[9] = integer
547        FBinRead/F=3/B=3 refNum, integer
548        intw[10] = integer
549       
550        //2 integers
551        FSetPos refNum,308
552        FBinRead/F=3/B=3 refNum, integer
553        intw[11] = integer
554        FBinRead/F=3/B=3 refNum, integer
555        intw[12] = integer
556       
557        //2 integers
558        FSetPos refNum,332
559        FBinRead/F=3/B=3 refNum, integer
560        intw[13] = integer
561        FBinRead/F=3/B=3 refNum, integer
562        intw[14] = integer
563       
564        //3 integers
565        FSetPos refNum,376
566        FBinRead/F=3/B=3 refNum, integer
567        intw[15] = integer
568        FBinRead/F=3/B=3 refNum, integer
569        intw[16] = integer
570        FBinRead/F=3/B=3 refNum, integer
571        intw[17] = integer
572       
573        //1 text field
574        FSetPos refNum,404
575        FReadLine/N=42 refNum,textstr
576        textw[10]= textstr
577       
578        //1 integer
579        FSetPos refNum,458
580        FBinRead/F=3/B=3 refNum, integer
581        intw[18] = integer
582       
583        //4 integers
584        FSetPos refNum,478
585        FBinRead/F=3/B=3 refNum, integer
586        intw[19] = integer
587        FBinRead/F=3/B=3 refNum, integer
588        intw[20] = integer
589        FBinRead/F=3/B=3 refNum, integer
590        intw[21] = integer
591        FBinRead/F=3/B=3 refNum, integer
592        intw[22] = integer
593       
594        Close refNum
595       
596        //now get all of the reals
597        //
598        //Do all the GBLoadWaves at the end
599        //
600        //FBinRead Cannot handle 32 bit VAX floating point
601        //GBLoadWave, however, can properly read it
602        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
603        String strToExecute
604        //append "/S=offset/U=numofreals" to control the read
605        // then append fname to give the full file path
606        // then execute
607       
608        Variable a=0,b=0
609       
610        SetDataFolder curPath
611        // 4 R*4 values
612        strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + fname + "\""
613        Execute strToExecute
614       
615        SetDataFolder curPath
616        Wave w=$(curPath + ":tempGBWave0")
617        b=4     //num of reals read
618        realw[a,a+b-1] = w[p-a]
619        a+=b
620       
621        // 4 R*4 values
622        SetDataFolder curPath
623        strToExecute = GBLoadStr + "/S=158/U=4" + "\"" + fname + "\""
624        Execute strToExecute
625        b=4     
626        realw[a,a+b-1] = w[p-a]
627        a+=b
628
629///////////
630        // 2 R*4 values
631        SetDataFolder curPath
632        strToExecute = GBLoadStr + "/S=186/U=2" + "\"" + fname + "\""
633        Execute strToExecute
634        b=2     
635        realw[a,a+b-1] = w[p-a]
636        a+=b
637
638        // 6 R*4 values
639        SetDataFolder curPath
640        strToExecute = GBLoadStr + "/S=220/U=6" + "\"" + fname + "\""
641        Execute strToExecute
642        b=6     
643        realw[a,a+b-1] = w[p-a]
644        a+=b
645       
646        // 13 R*4 values
647        SetDataFolder curPath
648        strToExecute = GBLoadStr + "/S=252/U=13" + "\"" + fname + "\""
649        Execute strToExecute
650        b=13
651        realw[a,a+b-1] = w[p-a]
652        a+=b
653       
654        // 3 R*4 values
655        SetDataFolder curPath
656        strToExecute = GBLoadStr + "/S=320/U=3" + "\"" + fname + "\""
657        Execute strToExecute
658        b=3     
659        realw[a,a+b-1] = w[p-a]
660        a+=b
661       
662        // 7 R*4 values
663        SetDataFolder curPath
664        strToExecute = GBLoadStr + "/S=348/U=7" + "\"" + fname + "\""
665        Execute strToExecute
666        b=7
667        realw[a,a+b-1] = w[p-a]
668        a+=b
669       
670        // 4 R*4 values
671        SetDataFolder curPath
672        strToExecute = GBLoadStr + "/S=388/U=4" + "\"" + fname + "\""
673        Execute strToExecute
674        b=4     
675        realw[a,a+b-1] = w[p-a]
676        a+=b
677       
678        // 2 R*4 values
679        SetDataFolder curPath
680        strToExecute = GBLoadStr + "/S=450/U=2" + "\"" + fname + "\""
681        Execute strToExecute
682        b=2
683        realw[a,a+b-1] = w[p-a]
684        a+=b
685       
686        // 2 R*4 values
687        SetDataFolder curPath
688        strToExecute = GBLoadStr + "/S=470/U=2" + "\"" + fname + "\""
689        Execute strToExecute
690        b=2
691        realw[a,a+b-1] = w[p-a]
692        a+=b
693       
694        // 5 R*4 values
695        SetDataFolder curPath
696        strToExecute = GBLoadStr + "/S=494/U=5" + "\"" + fname + "\""
697        Execute strToExecute
698        b=5     
699        realw[a,a+b-1] = w[p-a]
700       
701        //if the binary VAX data ws transferred to a MAC, all is OK
702        //if the data was trasnferred to an Intel machine (IBM), all the real values must be
703        //divided by 4 to get the correct floating point values
704        // I can't find any combination of settings in GBLoadWave or FBinRead to read data in correctly
705        // on an Intel machine.
706        //With the corrected version of GBLoadWave XOP (v. 1.43 or higher) Mac and PC both read
707        //VAX reals correctly, and no checking is necessary 12 APR 99
708        //if(cmpstr("Macintosh",IgorInfo(2)) == 0)
709                //do nothing
710        //else
711                //either Windows or Windows NT
712                //realw /= 4
713        //endif
714       
715        //read in the data
716         GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
717
718        curPath = "root:"+cur_folder
719        SetDataFolder curPath           //use the full path, so it will always work
720       
721        Make/O/N=16384 $(curPath + ":data")
722        WAVE data = $(curPath + ":data")
723       
724        Variable skip,ii,offset
725       
726        //read in a total of 16384 values (ii)
727        //as follows :
728        // skip first 2 bytes
729        // skip 512 byte header
730        // skip first 2 bytes of data
731        //(read 511 reals, skip 2b, 510 reals, skip 2b) -16 times = 16336 values
732        // read the final 48 values in seperately to avoid EOF error
733       
734        /////////////
735        SetDataFolder curPath
736        skip = 0
737        ii=0
738        offset = 514 +2
739        a=0
740        do
741                SetDataFolder curPath
742               
743                strToExecute = GBLoadStr + "/S="+num2str(offset)+"/U=511" + "\"" + fname + "\""
744                Execute strToExecute
745                //Print strToExecute
746                b=511
747                data[a,a+b-1] = w[p-a]
748                a+=b
749               
750                offset += 511*4 +2
751               
752                strToExecute = GBLoadStr + "/S="+num2str(offset)+"/U=510" + "\"" + fname + "\""
753                SetDataFolder curPath
754                Execute strToExecute
755                //Print strToExecute
756                b=510
757                data[a,a+b-1] = w[p-a]
758                a+=b
759               
760                offset += 510*4 +2
761               
762                ii+=1
763                //Print "inside do, data[2] =",data[2]
764                //Print "inside do, tempGBwave0[0] = ",w[0]
765        while(ii<16)
766       
767        // 16336 values have been read in --
768        //read in last 64 values
769        strToExecute = GBLoadStr + "/S="+num2str(offset)+"/U=48" + "\"" + fname + "\""
770       
771        SetDataFolder curPath
772        Execute strToExecute
773        b=48
774        data[a,a+b-1] = w[p-a]
775        a+=b
776//
777/// done reading in raw data
778//
779        //Print "in workdatareader , data = ", data[1][1]
780
781        Redimension/n=(128,128) data
782       
783        //clean up - get rid of w = $"tempGBWave0"
784        KillWaves w
785       
786        //divide the FP data by 4 if read from a PC (not since GBLoadWave update)
787        //if(cmpstr("Macintosh",IgorInfo(2)) == 0)
788                //do nothing
789        //else
790                //either Windows or Windows NT
791                //data /= 4
792        //endif
793       
794        //keep a string with the filename in the DIV folder
795        String/G $(curPath + ":fileList") = textw[0]
796       
797        //return the data folder to root
798        SetDataFolder root:
799       
800        Return(0)
801End
802
803////testing procedure that is not called anymore and is out-of-date
804////with the new data structures. update before re-implementing
805//Proc ReadBinaryWork()         //this procedure is not called anymore
806//
807////    Silent 1
808//     
809//      //make sure the globals are reset properly
810//      Variable/G root:myGlobals:gIsLogScale=0 //image is linearly scaled to begin with
811//      String/G root:myGlobals:gDataDisplayType="RAW"          //displayed data type is raw
812//     
813//      //each routine is responsible for checking the current (displayed) data folder
814//      //selecting it, and returning to root when done
815//     
816//      ReadHeaderAndWork()
817//
818//      SetDataFolder root:RAW
819//      //data is displayed here
820//      Display;AppendImage data
821//     
822////    Silent 0
823//     
824//      //data folder was changed from root in this macro
825//      SetDataFolder root:
826//End
827
828
829//
830
831
832
833/////*****************
834////unused testing procedure for writing a 4 byte floating point value in VAX format
835//Proc TestReWriteReal()
836//      String Path
837//      Variable value,start
838//     
839//      GetFileAndPath()
840//      Path = S_Path + S_filename
841//     
842//      value = 0.2222
843//      start = 158             //trans starts at byte 159
844//      ReWriteReal(path,value,start)
845//     
846//      SetDataFolder root:
847//End
848
849//function will re-write a real value (4bytes) to the header of a RAW data file
850//to ensure re-readability, the real value must be written mimicking VAX binary format
851//which is done in this function
852//path is the full path:file;vers to the file
853//value is the real value to write
854//start is the position to move the file marker to, to begin writing
855//--so start is actually the "end byte" of the previous value
856//
857//this procedure takes care of all file open/close pairs needed
858//
859Function ReWriteReal(path,value,start)
860        String path
861        Variable value,start
862       
863        //Print " in F(), path = " + path
864        Variable refnum,int1,int2, value4
865
866//////
867        value4 = 4*value
868       
869        Open/A/T="????TEXT" refnum as path
870        //write IEEE FP, 4*desired value
871        FSetPos refnum,start
872        FBinWrite/B=3/F=4 refnum,value4         //write out as little endian
873       
874        //move to the end of the file
875        FStatus refnum
876        FSetPos refnum,V_logEOF
877        //Print "Wrote end of header to " + num2str(V_filePOS)
878       
879        Close refnum
880       
881///////
882        Open/R refnum as path
883        //read back as two 16-bit integers
884        FSetPos refnum,start
885        FBinRead/B=2/F=2 refnum,int1    //read as big-endian
886        FBinRead/B=2/F=2 refnum,int2
887       
888        //file was opened read-only
889        //no need to move to the end of the file, just close it
890       
891        Close refnum
892///////
893        Open/A/T="????TEXT" refnum as path
894        //write the two 16-bit integers, reversed
895        FSetPos refnum,start
896        FBinWrite/B=2/F=2 refnum,int2   //re-write as big endian
897        FBinWrite/B=2/F=2 refnum,int1
898       
899        //move to the end of the file
900        FStatus refnum
901        FSetPos refnum,V_logEOF
902        //Print "Wrote end of header to " + num2str(V_filePOS)
903       
904        Close refnum                            //at this point, it is as the VAX would have written it.
905       
906        Return(0)
907End
908
909//rewrite a text field back to the header
910// fname is the full path:name
911// str is the CORRECT length - it will all be written - pad before sending
912// start is the start byte
913Function RewriteTextToHeader(fname,str,start)
914        String fname,str
915        Variable start
916       
917        Variable refnum
918        Open/A/T="????TEXT" refnum as fname      //Open for writing! Move to EOF before closing!
919        FSetPos refnum,start
920        FBinWrite/F=0 refnum, str      //native object format (character)
921        //move to the end of the file before closing
922        FStatus refnum
923        FSetPos refnum,V_logEOF
924        //Print "Wrote end of header to " + num2str(V_filePOS)
925        Close refnum
926               
927        return(0)
928end
929
930//rewrite an integer field back to the header
931// fname is the full path:name
932// val is the integer value
933// start is the start byte
934Function RewriteIntegerToHeader(fname,val,start)
935        String fname
936        Variable val,start
937       
938        Variable refnum
939        Open/A/T="????TEXT" refnum as fname      //Open for writing! Move to EOF before closing!
940        FSetPos refnum,31
941        FBinWrite/B=3/F=3 refnum, val      //write a 4-byte integer
942        //move to the end of the file before closing
943        FStatus refnum
944        FSetPos refnum,V_logEOF
945        //Print "Wrote end of header to " + num2str(V_filePOS)
946        Close refnum
947               
948        return(0)
949end
950
951// read specific bits of information from the header
952// each of these operations MUST take care of open/close on their own
953
954Function/S getStringFromHeader(fname,start,num)
955        String fname                            //full path:name
956        Variable start,num              //starting byte and number of characters to read
957       
958        String str
959        Variable refnum
960        Open/R refNum as fname
961        FSetPos refNum,start
962        FReadLine/N=(num) refNum,str
963        Close refnum
964       
965        return(str)
966End
967// file suffix (4 characters @ byte 19)
968Function/S getSuffix(fname)
969        String fname
970       
971        return(getStringFromHeader(fname,19,4))
972End
973
974// sample label (60 characters @ byte 98)
975Function/S getSampleLabel(fname)
976        String fname
977       
978        return(getStringFromHeader(fname,98,60))
979End
980
981// read a single real value with GBLoadWave
982Function getRealValueFromHeader(fname,start)
983        String fname
984        Variable start
985
986        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
987       
988        GBLoadStr += "/S="+num2str(start)+"/U=1" + "\"" + fname + "\""
989        Execute GBLoadStr
990        Wave w=$"tempGBWave0"
991       
992        return(w[0])
993End
994
995//monitor count is at byte 39
996Function getMonitorCount(fname)
997        String fname
998       
999        return(getRealValueFromHeader(fname,39))
1000end
1001
1002//saved monitor count is at byte 43
1003Function getSavMon(fname)
1004        String fname
1005       
1006        return(getRealValueFromHeader(fname,43))
1007end
1008
1009//detector count is at byte 47
1010Function getDetCount(fname)
1011        String fname
1012       
1013        return(getRealValueFromHeader(fname,47))
1014end
1015
1016//Attenuator number is at byte 51
1017Function getAttenNumber(fname)
1018        String fname
1019       
1020        return(getRealValueFromHeader(fname,51))
1021end
1022
1023//transmission is at byte 158
1024Function getSampleTrans(fname)
1025        String fname
1026       
1027        return(getRealValueFromHeader(fname,158))
1028end
1029
1030//SampleThickness is at byte 162
1031Function getSampleThickness(fname)
1032        String fname
1033       
1034        return(getRealValueFromHeader(fname,162))
1035end
1036
1037//temperature is at byte 186
1038Function getTemperature(fname)
1039        String fname
1040       
1041        return(getRealValueFromHeader(fname,186))
1042end
1043
1044//field strength is at byte 190
1045Function getFieldStrength(fname)
1046        String fname
1047       
1048        return(getRealValueFromHeader(fname,190))
1049end
1050
1051//beamstop xPos is at byte 252
1052Function getBSXPos(fname)
1053        String fname
1054       
1055        return(getRealValueFromHeader(fname,252))
1056end
1057
1058//beamstop Y pos is at byte 256
1059Function getBSYPos(fname)
1060        String fname
1061       
1062        return(getRealValueFromHeader(fname,256))
1063end
1064
1065//SDD is at byte 260
1066Function getSDD(fname)
1067        String fname
1068       
1069        return(getRealValueFromHeader(fname,260))
1070end
1071
1072//detector offset is at byte 264
1073Function getDetectorOffset(fname)
1074        String fname
1075       
1076        return(getRealValueFromHeader(fname,264))
1077end
1078
1079//Beamstop diameter is at byte 272
1080Function getBSDiameter(fname)
1081        String fname
1082       
1083        return(getRealValueFromHeader(fname,272))
1084end
1085
1086//source aperture diameter is at byte 280
1087Function getSourceApertureDiam(fname)
1088        String fname
1089       
1090        return(getRealValueFromHeader(fname,280))
1091end
1092
1093//sample aperture diameter is at byte 284
1094Function getSampleApertureDiam(fname)
1095        String fname
1096       
1097        return(getRealValueFromHeader(fname,284))
1098end
1099
1100//source AP to Sample AP distance is at byte 288
1101Function getSourceToSampleDist(fname)
1102        String fname
1103       
1104        return(getRealValueFromHeader(fname,288))
1105end
1106
1107//wavelength is at byte 292
1108Function getWavelength(fname)
1109        String fname
1110       
1111        return(getRealValueFromHeader(fname,292))
1112end
1113
1114//wavelength spread is at byte 296
1115Function getWavelengthSpread(fname)
1116        String fname
1117       
1118        return(getRealValueFromHeader(fname,296))
1119end
1120
1121//transmission detector count is at byte 388
1122Function getTransDetectorCounts(fname)
1123        String fname
1124       
1125        return(getRealValueFromHeader(fname,388))
1126end
1127
1128
1129
1130Function getIntegerFromHeader(fname,start)
1131        String fname                            //full path:name
1132        Variable start          //starting byte
1133       
1134        Variable refnum,val
1135        Open/R refNum as fname
1136        FSetPos refNum,start
1137        FBinRead/B=3/F=3 refnum,val
1138        Close refnum
1139       
1140        return(val)
1141End
1142
1143//total count time is at byte 31       
1144Function getCountTime(fname)
1145        String fname
1146        return(getIntegerFromHeader(fname,31))
1147end
1148
1149
1150//reads the wavelength from a reduced data file (not very reliable)
1151// - does not work with NSORTed files
1152// - only used in FIT/RPA (which itself is almost NEVER used...)
1153//
1154Function GetLambdaFromReducedData(tempName)
1155        String tempName
1156       
1157        String junkString
1158        Variable lambdaFromFile, fileVar
1159        lambdaFromFile = 6.0
1160        Open/R/P=catPathName fileVar as tempName
1161        FReadLine fileVar, junkString
1162        FReadLine fileVar, junkString
1163        FReadLine fileVar, junkString
1164        if(strsearch(LowerStr(junkString),"lambda",0) != -1)
1165                FReadLine/N=11 fileVar, junkString
1166                FReadLine/N=10 fileVar, junkString
1167                lambdaFromFile = str2num(junkString)
1168        endif
1169        Close fileVar
1170       
1171        return(lambdaFromFile)
1172End
1173
Note: See TracBrowser for help on using the repository browser.