; $Id: ctasrd.pro,v 1.3 2003/04/24 19:35:18 dimeo Exp $
function ctasrd,filen
;+
; NAME:         CTASRD
; PURPOSE:
;               Read a **NEW** TASCOM *.DAT file
; CATEGORY:
; CALLING SEQUENCE:
;               d=ctasrd('filename')
; INPUTS:
;               filename= eg. ora059.dat
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUPUTS:
;               a data structure consisting of:
;                 d.points - integer - # of points in scan
;                 d.heads - integer - # of header parameters
;                 d.pars - integer - # of scan parameters
;                 d.text - string array - 2 lines of descriptive text
;                 d.hdef - string array - definition of header parameters
;                 d.hnum - float array - header parameters
;                 d.prp - string array - definition of scan parameters
;                 d.data - 2D float array - scan data (parameter,point#)
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
;		The new C version of TASCOM stores files differently.
;		Use this procedure to read in the new tascom files.
;		Note that tasrd() will call ctasrd() if the file is
;		a new type.
;
; MODIFICATION HISTORY:
;               Paul Schleger, Riso National Laboratory, September 1993
;-
; MORE MODIFICATION FOR THE AREA DETECTOR:
;               GUANGYONG XU, THE JOHNS HOPKINS UNIVERSITY, SEPTEMBER 1996

COMMON SEARP,sear,ddmask

print,'ny version'
on_ioerror,bad
openr,unit,filen,/get_lun
LOTSOFDATA=10000                ;Maximum number of data points to read
lines=strarr(2)
datanext=0                      ;boolean to indicate that the next stuff will be data
REPEAT BEGIN
    readtokey, unit,KEY='#',OUT=txtline,ERROR=errflg
    IF (errflg EQ 0) THEN BEGIN
        stuff=STRMID(txtline,4,strlen(txtline)-4)
        cmpare=STRMID(txtline,0,4)
        CASE cmpare OF
            '#fdt': lines(0)=STRCOMPRESS(stuff)	;Contains the file name and date
            '#txt': lines(1)=STRCOMPRESS(stuff)	;User comments
            '#fih': BEGIN       ;File header parameter string
                ih=nwrds(stuff)	;Find out how many
                IF (ih gt 0) THEN BEGIN
                    heads=STRARR(ih) ;create the string array
                    FOR i=0,ih-1 DO heads(i)=getwrd(stuff,i) ;get 'em
                END ELSE BEGIN
                    heads=STRARR(1)
                END
            END
            '#fhp': BEGIN       ;File header parameter numbers
                ih=nwrds(stuff)	;Find out how many...
                IF (ih gt 0) THEN BEGIN
                    headn=FLTARR(ih) ;create the float array
                    FOR i=0,ih-1 DO BEGIN ;readem in
                        IF (isnumber(STRCOMPRESS(getwrd(stuff,i),/REMOVE_ALL),val) NE 0) THEN $
                          headn(i)=val $
                        ELSE BEGIN ;Print error and set zero if not a no.
                            headn(i)=0.0
                            print, STRCOMPRESS('WARNING:'+$
                                               STRING(i+1,FORMAT='(I)')+$
                                               'th header parameter is not a number: '+ getwrd(stuff,i))
                        END
                    END
                END ELSE headn=FLTARR(1)
            END
            '#fip': BEGIN       ;File parameters
                ipar=nwrds(stuff) ;Find out how many
                prps=STRARR(ipar) ;create the array
                FOR i=0,ipar-1 DO prps(i)=getwrd(stuff,i) ;get 'em
                datanext=1      ;Indicate that the data comes...
            END
                                ; '#com': print, stuff
            ELSE :
        END
    END ELSE BEGIN
        print, 'ERROR: keyword "#" not found in file'
        print, '       File either has no data, or it is not a'
        print, '       C version TASCOM data file'
        print, '       Check the file to see if it has lines containing:'
        print, '       #fdt, #txt, #fih, #fip. Especially, #fip'
    END
END UNTIL ((datanext EQ 1) OR (errflg NE 0))

;**** Now do some processing of the existing information *******
;**** This is essentially a copy of what Thom Mason's tasrd did ****
if (strpos(lines(0),'.dat') gt 1) then lines(0)=strmid(lines(0),strpos(lines(0),'.dat')-6,6)

;Here we are checking if the std. deviation of the intensity and monitor counts were stored
;If not, we'll generate them later, and add these to the prp string
indexi = WHERE(prps EQ 'SIGI',counti)
indexm = WHERE(prps EQ 'SIGM',countm)
indexaux = where(prps eq 'AUX',counta)
indexsear = where(prps eq 'SEAR',counts)

;see if this is a file coming with '.dar' files (area analyzer arays)
IF counts NE 0 then begin
    sear=indexsear(0)
    ddmask=bytarr(128,128)
    ddregion
;    tv,ddmask*200
endif ELSE sear=0


scandat = readcol(unit,1,LOTSOFDATA,INDGEN(ipar)+1,ERR=errflg)
IF errflg NE 0 THEN message, 'Error reading the data file',/TRACEBACK
npt=n_elements(scandat(0,*))
Icol = WHERE(prps EQ 'I',Icount)
Mcol = WHERE(prps EQ 'MON',Mcount)

;; read the area detector data array

if ( sear ne 0 ) and (icount ne 0) then begin
    for jj=0,fix(npt)-1 do begin
        ;print,scandat(sear,jj)
        darread,filen,scandat(sear,jj),vaux
        scandat(Icol,jj)=vaux
    endfor
endif



;see if AUX is in the parameter file.
;Yes: Change AUX to I and delete I (if I exists)---Area detector
;No: go on as single detector
;but don't do it when using the array dd[]

;If (counta ne 0) and (sear eq 0) then begin
;    prps(indexaux)='I'
;    if Icount eq 0 then begin
;        Icount=1
;        Icol=indexaux
;    endif else begin
;        prps(Icol)='JUNK'
;        Icol=indexaux
;    endelse
;endif


IF (counti EQ 0) AND (Icount NE 0) THEN BEGIN
    prps = [prps,'SIGI']
                                ;prps(n_elements(prps)-1)='SIGI'
    sig = SQRT(scandat(Icol(0),*))
    index = WHERE(sig EQ 0,count)
    IF count NE 0 THEN sig(index) = 1
    scandat = [scandat,sig]
END
IF (countm EQ 0) AND (Mcount NE 0) THEN BEGIN
    prps = [prps,'SIGM']
                                ;prps(n_elements(prps)-1)='SIGM'
    sig = SQRT(scandat(Mcol(0),*))
    index = WHERE(sig EQ 0,count)
    IF count NE 0 THEN sig(index) = 1
    scandat = [scandat,sig]
END




scan={points: FIX(npt),heads: FIX(n_elements(heads)),pars: FIX(n_elements(prps)),text: lines,$
      hdef: heads,hnum: headn,prp: prps,$
      data: FLOAT(scandat)}
goto,done

bad:print,!err_string

done: free_lun,unit,/force
return,scan
end
