source: sans/Release/trunk/NCNR_User_Procedures/SANS/Reduction/ILL_DataReadWrite.ipf @ 363

Last change on this file since 363 was 363, checked in by srkline, 15 years ago

Merging changes from Dev/trunk up to revision #362 into Release/trunk

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