; $Id: tasinfo.pro,v 1.4 2002/09/16 20:43:56 jmspark Exp $
;-----------------------------------------------------------------------------
;+
; This prints out the header definition and the ranges of the scan pars
; If the scan pars seem to be constant, then their mean and stdev are
; printed
;-
;-----------------------------------------------------------------------------
FUNCTION tasinfo, d $
	, font=fontnum, bold_font = bold_fontnum $
	, ongraph=ongraph

;Check out the font
IF NOT keyword_set(fontnum) THEN fontnum=5
IF (fontnum LT 3) OR (fontnum GT 20) THEN fontnum=5
IF NOT keyword_set(bold_fontnum) THEN bold_fontnum=4
IF (bold_fontnum LT 3) OR (bold_fontnum GT 20) THEN bold_fontnum=4

IF keyword_set(ongraph) THEN BEGIN
	fntstr = STRCOMPRESS('!'+STRING(fontnum,format='(I)'),/REMOVE_ALL)
	fntstr1 = '!' + strtrim( string( bold_fontnum ), 2 )
	IF !P.FONT EQ 0 THEN pmsym = STRCOMPRESS(fntstr+'!9!S!B!E-!R!A!I+!N',/REMOVE_ALL) $
	ELSE pmsym = '!M+'
	newline = '!c'
	TAB = '    '
	scfactor=1
END ELSE BEGIN
	fntstr = ' '
	fntstr1 = fntstr
	pmsym = '+/-'
	newline = STRING('0d'XB)+STRING('0a'XB)
	TAB = STRING('09'XB)
	scfactor=1
END
CONSTR = fntstr1+'Constants:'+fntstr+newline
LINSTR = fntstr1+'Swept Variables:'+fntstr+newline
FLUCTSTR = fntstr1+'Fluctuating Variables:'+fntstr+newline
formstr = '(G9.4)'

hnum=STRING(d.hnum,FORMAT=formstr)



;Get the numbers. (I could get them from other elements of d, But why trust anyone?
nhdef=n_elements(d.hdef)
nprp=n_elements(d.prp)

;Create the descriptive strings, so that  they have all the same length
prpstr=strarr(nprp)

hdefstr=strarr(nhdef)
FOR i=0,nprp-1 DO prpstr(i)=STRING(d.prp(i),FORMAT=mkstrformat(d.prp))
FOR i=0,nhdef-1 DO hdefstr(i)=STRING(d.hdef(i),FORMAT=mkstrformat(d.hdef))

line=strarr(3)
;First the two lines of descriptive text
line(0)=fntstr1+'Description:'+fntstr+newline
line(1)=d.text(0)+newline
line(2)=d.text(1)+newline+newline
line=[line,fntstr1+'No. of Data Points:'+fntstr,STRING(n_elements(d.data(0,*)))+newline+newline]

;next the header strings and values 3 per line
IF nhdef NE 0 THEN BEGIN
	happiness = strarr(nhdef)
	happiness(*) = '  '
	FOR i=0,nhdef-1 DO IF (((i+1) MOD (4*scfactor)) EQ 0) THEN happiness(i)=newline
	happiness(nhdef-1)=newline+newline
	line=[line,fntstr1+'Header Info:'+fntstr+newline,hdefstr+' = '+hnum+happiness]
END


;next the data and it's range
mind = fltarr(nprp) & maxd = fltarr(nprp)
all=indgen(nprp)

mean=fltarr(nprp)
std=fltarr(nprp)
FOR i=0,nprp-1 DO BEGIN
	mind(i)=min(d.data(i,*))
	maxd(i)=max(d.data(i,*))
	mean[i]=TOTAL(d.data(i,*))/n_elements(d.data(i,*))
	IF n_elements(d.data(i,*)) GT 1 THEN std(i)=stdev(d.data(i,*)) ELSE std(i)=0
END

;If the data seems to be roughly constant, also add it's mean and stddev
;Find out which pars are not always increasing or decreasing, ie fluctuating
if n_elements( d.data[0,*] ) eq 1 then $
	direction = 1d $
else direction = d.data(*,0)-d.data(*,1)
notok=intarr(nprp)
FOR i=0,nprp-1 DO $
FOR j=0,d.points-2 DO $
	IF (direction(i)*(d.data(i,j)-d.data(i,j+1)) LT 0) THEN notok(i)=1


mindstr=STRING(mind,FORMAT=formstr)
maxdstr=STRING(maxd,FORMAT=formstr)

;First add the ones who actually don't change
index=WHERE(std EQ 0, count)
IF count NE 0 THEN BEGIN
	happiness = strarr(count)
	happiness(*) = '  '
	FOR i=0,count-1 DO IF (((i+1) MOD (4*scfactor)) EQ 0) THEN happiness(i)=newline
	happiness(count-1)=newline+newline		;make sure last one has a newline too
	line = [line,CONSTR,prpstr(index)+' = '+mindstr(index)+happiness]
END


;Then add the ones who change always in one direction
index=WHERE((std NE 0) AND (notok EQ 0),count)
;Here we'll allow just two variables per line (MOD 2)
IF count NE 0 THEN BEGIN
	happiness = strarr(count)
	happiness(*) = TAB
	FOR i=0,count-1 DO IF (((i+1) MOD (2*scfactor)) EQ 0) THEN happiness(i)=newline
	happiness(count-1)=newline+newline		;make sure last one has a newline too
	line = [line,LINSTR,prpstr(index)+' = '+mindstr(index)+' ...'+maxdstr(index)+happiness]
END


;Now add the ones who are fluctuating a bit
index=WHERE((std NE 0) AND (notok EQ 1),count)
meanstr=STRING(mean,FORMAT=formstr)
stdstr=STRING(std,FORMAT=formstr)
IF (count NE 0) THEN $
	line=[line,FLUCTSTR,'<'+prpstr(index)+'> = '+$
   	meanstr(index)+pmsym+fntstr+stdstr(index)+$
   	TAB+'   Min: '+mindstr(index)+'   Max: '+maxdstr(index)+newline]

;Now concatenate into one big string
stuff=' '
FOR i=0,n_elements(line)-1 DO stuff=stuff+line(i)


RETURN, stuff+newline
END
