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

Last change on this file since 776 was 776, checked in by srkline, 12 years ago

fixed some sloppy logic in converting log/lin data. Now the raw data readers create data (for display) and linear_data (to keep) upon loading.

Updated the Histogram Pair with Cedric's suggestions. width is adjustable, and the cursor is more visible.

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