; $Id: fiord.pro,v 1.3 2003/04/24 19:35:18 dimeo Exp $
function fiord,filen
;+
; NAME:         FIORD
; PURPOSE:
;               Read a SPECTRA .fio file into a TASCOM data structure
; CATEGORY:
; CALLING SEQUENCE:
;               d=fiord('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:
;
;
; MODIFICATION HISTORY:
;               Paul Schleger, Riso National Laboratory, May 1994
;-
on_ioerror,bad
openr,unit,filen,/get_lun
LOTSOFDATA=10000			;Maximum number of data points to read
lines=[' ',' ']
datanext=0	;boolean to indicate that the next stuff will be data
ih=0		;number of header parameters
ipar=0		;number of scan parameters
errflg=0	;Is there an error in reading things?
heads=STRARR(1)
headn = FLTARR(1)
REPEAT BEGIN
itm = STRCOMPRESS(nextitem(unit, txtline),/REMOVE_ALL)
CASE itm OF
	'%c' : BEGIN
	        itm = STRCOMPRESS(nextitem(unit, txtline),/REMOVE_ALL)
	        list = STR_SEP(txtline,':')
		WHILE itm NE '!' DO BEGIN
			CASE itm OF
				'name' : lines(0) = lines(0)+list(1)+' '
			   'date/time' : lines(1) = lines(1)+list(1)+' '
			   ELSE        : BEGIN
			   	  	ih=ih+1
			   	 	number = STRCOMPRESS(list(1),/REMOVE_ALL)
			   	  	IF (isnumber(number) NE 0) THEN $
			   	  	IF ih EQ 1 THEN BEGIN
			   			heads(0)=STRCOMPRESS(list(0),/REMOVE_ALL)
			   			headn(0) = FLOAT(number)
			   	  	END ELSE BEGIN
			   			heads=[heads,STRCOMPRESS(list(0),/REMOVE_ALL)]
			   			headn = [headn,FLOAT(number)]
			   		END
			   	END
			ENDCASE
			itm = STRCOMPRESS(nextitem(unit, txtline),/REMOVE_ALL)
			list = STR_SEP(txtline,':')
		ENDWHILE
	       END
	'%d' : BEGIN
		itm = STRCOMPRESS(nextitem(unit, txtline),/REMOVE_ALL)
		list = STR_SEP(txtline,' ')
		;FOR jerk=0,n_elements(list)-1 DO print, '"',list(jerk),'"'
		WHILE itm EQ 'Col' DO BEGIN
			ipar=ipar+1
			IF ipar EQ 1 THEN prps=[STRUPCASE(STRCOMPRESS(list(3),/REMOVE_ALL))] $
			ELSE prps = [prps,STRUPCASE(STRCOMPRESS(list(3),/REMOVE_ALL))]
			itm = STRCOMPRESS(nextitem(unit, txtline),/REMOVE_ALL)
			list = STR_SEP(txtline,' ')
		END
		datanext=1
	       END
	ELSE :
ENDCASE
END UNTIL ((datanext EQ 1) OR (errflg NE 0))


;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)

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,*))
;In order to make it look like a TASCOM file, we've got to make an entry called 'I'
Icol = WHERE(prps EQ 'I',count)
IF count EQ 0 THEN BEGIN
	Icol = WHERE((prps EQ 'CANBERRA/NAI')  OR (prps EQ 'Canberra_Win'),Icount)
	IF Icount NE 0 THEN prps(Icol(0)) = 'I' $;Take the first instance of either
	ELSE BEGIN
		Icol = WHERE(STRPOS(prps,'CAN') NE -1,Icount)	;Something like Canberra????
		IF Icount NE 0 THEN prps(Icol(0)) = 'I' $
		ELSE prps(1) = 'I'				;to hell with it, use 2nd col.
	END
END
Icol = WHERE(prps EQ 'I',Icount)
Mcol = WHERE(prps EQ 'NAI_WIN',Mcount)
IF (counti EQ 0) AND (Icount NE 0) THEN BEGIN
	prps = [prps,'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']
	sig = SQRT(scandat(Mcol(0),*))
  	index = WHERE(sig EQ 0,count)
  	IF count NE 0 THEN sig(index) = 1
	scandat = [scandat,sig]
END

;There is a bug somewhere else in this huge program that can't deal with the
; possibility that one has only one header parameter. So trick it here, 'cause I'm
; too bored to fix it for good.
IF n_elements(heads) EQ 1 THEN BEGIN
	heads = [heads,'DUMMY']
	headn = [headn,0.0]
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
