; $Id: aofrd.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
pro aofrd,filename
;+
; NAME:		AOFRD
; PURPOSE:
;		Read a Chalk River PHYSICA output and write a bunch
;		of TASCOM style dat files
; CATEGORY:
; CALLING SEQUENCE:
;		aofrd,'filename.crl'
; INPUTS:
;		filename - name of a PHYSICA ascii file with all fields
;		outputted except background and second sig ie 11 fields
; OPTIONAL INPUT PATRAMETERS:
;		none
; KEYWORD PARAMETERS:
; OUTPUTS:
;		a series of numbered *.dat TASCOM files, one for each
;		scan in the TRANS file
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
;		overwrites dat files with the same name
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;		Thom Mason, University of Toronto Physics, Dec. 1994
;-
if (n_params() lt 1) then filename=pickfile(filter='*.aof',title=$
 'Select a AOF file')
on_ioerror,bad
openr,unit,filename,/get_lun
lines=[' ',' ']
ih=12
heads=strarr(ih)
headn=fltarr(ih)
heads(0)='MON1'
heads(1)='MON2'
heads(2)='A'
heads(3)='R'
heads(4)='EPRIM'
heads(5)='KPRIM'
heads(6)='ZEM'
heads(7)='ETM'
heads(8)='NUM'
heads(9)='DZE'
heads(10)='DET'
heads(11)='DNU'
;heads(12)='TMFR'
;heads(13)='TMTR'
;heads(14)='TMTI'
;heads(15)='TMCR'
ipar=21
prps=strarr(ipar)
scandat=fltarr(ipar,3000)
prps(0)='PT'
prps(1)='2TM'
prps(2)='PSI'
prps(3)='PHI'
prps(4)='2TA'
prps(5)='ZETA'
prps(6)='ETA'
prps(7)='NU'
prps(8)='STMP'
prps(9)='RTMP'
prps(10)='CTMP'
prps(11)='MON'
prps(12)='TIM'
prps(13)='I'
prps(14)='CFMN'
prps(15)='CFTM'
prps(16)='I2'
prps(17)='EM'
prps(18)='SIGI'
prps(19)='SIG2'
prps(20)='SIGM'
dum=' '
p9=0

just_started=1
while (not EOF(unit)) do begin
 if (just_started eq 1) then readf,unit,format='(/,/,/,/,a79,/,a79)',lines $
  else if ((headn(1) ne 0) and (gnpt gt npt)) $
   then readf,unit,format='(/,a79,/,a79)',lines $
   else readf,unit,format='(/,/,a79,/,a79)',lines
 just_started=0
 srun=strmid(lines(0),3,6)
; print,'lines :',srun,p9
 nrun=fix(srun)
 snpt=strmid(lines(1),19,6)
 npt=fix(snpt)
 gnpt=npt
 h0=long(strmid(lines(1),39,7))*long(strmid(lines(1),49,5))
 ;I was not able to divine how the CF Monitor is output in general.
 ;Do not trust the next line!
 h1=long(strmid(lines(1),39,7))*long(strmid(lines(1),67,6))
 readf,unit,format='(2x,f8.5,4x,f8.5,8x,f8.5,8x,f8.5)'$
    ,h2,h3,h4,h5
 readf,unit,format='(4x,f8.4,5x,f8.4,5x,f8.4,5x,f8.4,5x,f8.4,5x,f8.4)'$
    ,h6,h7,h8,h9,h10,h11
 headn(0)=h0
 headn(1)=h1
 headn(2)=h2
 headn(3)=h3
 headn(4)=h4
 headn(5)=h5
 headn(6)=h6
 headn(7)=h7
 headn(8)=h8
 headn(9)=h9
 headn(10)=h10
 headn(11)=h11
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 i=0
 repeat begin
  readf,unit,format='(i5,8(1x,f8.4))'$
    ,p0,p1,p2,p3,p4,p5,p6,p7,p8
  readf,unit,format='(i5,2(1x,f8.4),3(1x,i8))'$
    ,p9,p10,p11,p12,p13,p14 
  if ((headn(1) ne 0) and (headn(0) ne 0)) then begin
   readf,unit,format='(i5,2(1x,f8.4),3(1x,i8))'$
     ,dum1,dum2,dum3,p15,p16,p17
;     print,'non-zero crystal flipped'
   endif else begin 
    p15=0.0
    p16=0.0
    p17=0.0
    endelse
  scandat(0,i)=p0
  scandat(1,i)=p1
  scandat(2,i)=p2
  scandat(3,i)=p3
  scandat(4,i)=p4
  scandat(5,i)=p5
  scandat(6,i)=p6
  scandat(7,i)=p7
  scandat(8,i)=p8
  scandat(9,i)=p10
  scandat(10,i)=p11
  scandat(11,i)=p12
  scandat(12,i)=p13
  scandat(13,i)=p14
  scandat(14,i)=p15
  scandat(15,i)=p16
  scandat(16,i)=p17
  scandat(17,i)=p7*4.135
  scandat(18,i)=sqrt(p14)
  scandat(19,i)=sqrt(p17)
  scandat(20,i)=sqrt(p12)
  i=i+1
  endrep until (p9 eq npt) or ((scandat(0,i-1) eq 0) and (scandat(2,i-1) eq 0)$
   and (scandat(3,i-1) eq 0))
 if ((scandat(0,i-1) eq 0) and (scandat(2,i-1) eq 0) and (scandat(3,i-1) eq 0))$
   then npt=i-1  else readf, unit, format='(/,a79)', dum
 outname=strmid(filename,0,3)+string(nrun,format='(i3)')+'.dat'
 while (strpos(outname,' ') ge 0) do begin
  strput,outname,'0',strpos(outname,' ')
  endwhile
 lines(1)=lines(0)
 lines(0)='FILE NAME : '+strupcase(outname)
 scan={points: npt, heads: ih, pars: ipar, text: lines,$
 hdef: heads,hnum: headn,prp: prps,data: scandat(*,0:npt-1)}
 print,'Processing run',nrun,' with ',npt,' points. Output file ',outname
 taswr,scan,outname
 endwhile

goto,done

bad:print,!err_string

done: free_lun,unit
end
