; $Id: crlrd.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
pro crlrd,filename
;+
; NAME:		CRLRD
; PURPOSE:
;		Read a Chalk River TRANS output and write a bunch
;		of TASCOM style dat files
; CATEGORY:
; CALLING SEQUENCE:
;		crlrd,'filename.crl'
; INPUTS:
;		filename - name of a TRANS 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='*.crl',title=$
 'Select a TRANS file')
on_ioerror,bad
openr,unit,filename,/get_lun
lines=[' ',' ']
ih=13
heads=strarr(ih)
headn=fltarr(ih)
heads(0)='A'
heads(1)='R'
heads(2)='DM'
heads(3)='DA'
heads(4)='EF'
heads(5)='ZETM'
heads(6)='ETAM'
heads(7)='NUM'
heads(8)='DZET'
heads(9)='DETA'
heads(10)='DNU'
heads(11)='MON1'
heads(12)='MON2'
ipar=14
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)='I'
prps(9)='CTMP'
prps(10)='RTMP'
prps(11)='TIM'
prps(12)='MON'
prps(13)='EM'
dum=' '

while (not EOF(unit)) do begin
 readf,unit,format='(/,a79,/,/,a79)',lines
 srun=strmid(lines(1),3,5)
 nrun=fix(srun)
 snpt=strmid(lines(1),15,5)
 npt=fix(snpt)
 readf,unit,format='(11x,f8.5,5x,f8.5,6x,f8.5,6x,f8.5,10x,f8.5)'$
    ,h0,h1,h2,h3,h4
 readf,unit,format='(5x,f8.4,7x,f7.4,7x,f7.4,9x,f7.4,8x,f7.4,7x,f7.4)'$
    ,h5,h6,h7,h8,h9,h10
 readf,unit,format='(09x,f10.0,12x,f10.0)',h11,h12
 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
 headn(12)=h12
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 readf,unit,format='(a79)',dum
 for i=0,npt-1 do begin
  readf,unit,format='(i5,7(1x,f9.4),1x,f9.0,2(1x,f9.4),1x,f9.0)'$
    ,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11
  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)=p9
  scandat(10,i)=p10
  scandat(11,i)=p11
  scandat(12,i)=headn(11)
  scandat(13,i)=p7*4.135
  endfor
 outname=strmid(filename,0,3)+string(nrun,format='(i3)')+'.dat'
 while (strpos(outname,' ') ge 0) do begin
  strput,outname,'0',strpos(outname,' ')
  endwhile
 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