pro carrmapdisp0,mapmax,mapmin

;PURPOSE: 
;     Take an array containing images and display them with headings.
;
;INPUTS (prompted by the procedure):
;	cn	INT	carrington rotation
;	cam	STRING	camera (c1, c2, c3)
;	num_r	INT	number of radii to make maps for
;
;OPTIONAL PARAMETERS:
;	mapmax, mapmin	FLOAT/INT	scale factors for display
;
;OUTPUTS: displays on X device
;
;ROUTINES CALLED:
;    
;
;Author: Nathan Rich, NRL/Interferometrics, December 1996
;
; MODIFIED:	
;	970227 by N. Rich	reversed order of daynums
;	970311 by N. Rich	change tvscl max and min 
;	970313 by N. Rich	not ready for diff yet; comment out line
;	970714 by N. Rich	look for final data first; read iw from
;				filename; change column display
; 970918 by N. Rich	add option to write a gif file; took away difference 
;			option until it can be fixed
; 980618 by N. Rich	read FITS files
; 081121 by N. Rich	allow new start/end keywords
;

wlimb=''
saveone=''
min=0
limb = INTARR(4)
dir=''
;limb(0)= 1	; turn west limb on
;limb(1:3) = 0	; east limb, central meridian and carr longitude off
cam=''
cn=1
diff='n'

print
read,' Enter Carrington Rotation number: ',cn
read,' Enter camera:                     ',cam
read,' Enter identifying text:           ',wlimb
print
print,' In which directory are the maps? '
read,'(Default is /net/corona/cplex3/mapprogs)    ',dir
print
;IF cam NE 'c1' THEN read,' Subtract the minimum?  (y or n)        ',diff
;does not work for some reason
print

IF dir EQ '' THEN dir='/net/corona/cplex3/carrmaps'

filelist=findfile(dir+'/'+cam+'*'+wlimb+'*'+trim(string(cn))+'*.fts')
numfiles=n_elements(filelist)
IF numfiles EQ 1 THEN $
	filelist =findfile(dir+'/'+cam+wlimb+'*'+trim(string(cn))+'*ql.fts')
print,filelist
print
print,'Found ',numfiles,' arrays for ',cam,', CR ',cn
print
read,' How many images would you like to see? ',num_r
hdr = headfits(filelist(0))

thesta=fxpar(hdr,'STA-DATE')
IF datatype(thesta) NE 'STR' THEN thesta=fxpar(hdr,'DATE-STA')
thend=fxpar(hdr,'END-DATE')
IF datatype(thend) NE 'STR' THEN thend=fxpar(hdr,'DATE-END')
time_sta = str2utc(thesta)
time_end = str2utc(thend))
date_sta = strmid(thesta,2,8)
date_end = strmid(thend,2,8)
avg_time = {mjd:time_sta.mjd + 13,time:0}
avg_time_str = utc2str(avg_time,/ECS)

;print ,'       (up to',numfiles,')'
iw=fxpar(hdr,'NAXIS1')
imht=fxpar(hdr,'NAXIS2')
print
print,' Image width = ',iw
carrmap = fltarr(iw,imht,numfiles)
radii = strarr(numfiles)

FOR i = 0,num_r-1 DO BEGIN
   carrmap(*,*,i)=readfits(filelist(i),hdr)
   radii(i) = fxpar(hdr,'HEIGHT')
print,'radii(',i,') =',radii(i)
ENDFOR

print
;

IF diff eq 'y' THEN BEGIN
   basecarr = makebasecarr(avg_time_str,cam,radii,wlimb,iw)
;	wbasecarr = makebasecarr(wltime_str,cam,radii,'wl')
;	ebasecarr = makebasecarr(eltime_str,cam,radii,'el')
ENDIF

IF cam EQ 'c1' THEN scale = 150

num_ra = num_r
xsz = iw
ysz = num_r*(imht+10)

WINDOW,/FREE,xsize=xsz, ysize=ysz
dates=makedaynums(time_sta,time_end)

IF n_params() EQ 0 THEN BEGIN
   mapmax = max(carrmap)
   mapmin = min(carrmap)
ENDIF ELSE IF mapmin GT mapmax THEN BEGIN
   dummy = mapmin
   mapmin=mapmax
   mapmax = dummy
ENDIF
cam = STRUPCASE(cam)

FOR r=0,num_r-1 DO BEGIN
   ;IF r LE 5 THEN BEGIN
	x = 0 
	y = (num_ra - 1 -r)*(imht+10) + 15
   ;ENDIF ELSE BEGIN
;	x = iw+15
;	y = (num_ra - 1 -r)*(imht+10) +15
  ; ENDELSE
   
   IF diff EQ 'y' THEN carrmap(*,*,r) = carrmap(*,*,r) - basecarr(*,*,r)
   med = median(carrmap(*,*,r))
   mapmax=1.1*med
   mapmin=.9*med
   tvscl,carrmap(*,*,r)<mapmax>mapmin,x,y
   label = 'r = '+trim(string(radii(r)))+', CR '+TRIM(STRING(cn))+' '+strupcase(wlimb)+' for '+cam
   XYOUTS,x+iw/2,y+160, label, ALIGNMENT = 0.5, /DEVICE
   XYOUTS,x,y-10,date_end,ALIGNMENT=0, /device
   XYOUTS,x+iw,y-10,date_sta,ALIGNMENT=1, /device
   
   pr=2.
   FOR z=0,359 DO BEGIN
     IF dates(z) NE '' THEN BEGIN
	IF pr EQ 2 THEN XYOUTS, x+((iw-1)-(iw/360.)*z), $
		y,dates(z), $
		ALIGNMENT=0.5, /device
        pr=1/pr		; alternate printing days
     ENDIF
   ENDFOR
   ;stop
ENDFOR


XLOADCT

print
read,' Do you want to save one image as a gif file? [y/n] ',saveone
IF saveone EQ 'y' THEN BEGIN
   print
   read, "For which 'r'? ",str_rad
   ;dummy=trim(string(radii))
   ;read , '('+print_radii+') '
   radius=float(str_rad)
   sub = where(radii eq radius,count)
   temp = readfits(filelist(sub),h)

   IF diff EQ 'y' THEN temp = temp - basecarr(*,*,sub)
   title = 'LASCO '+cam+' CR '+trim(string(cn))+', R = '+str_rad

   x0=100
   y0=85
   iw=540
   ih=272
   x1=x0+iw
   y1=y0+ih

   window,xsize=680,ysize=420
   !p.background=255
   erase
   temp = congrid(temp,iw,ih,/interp)
   tvscl,temp,x0,y0
   contour,temp,/nodata, /noerase, /device, position=[x0,y0,x1,y1], xrange=[0,360], yrange=[-90,90], xstyle=5, ystyle=1, xticks=4, yticks=6, xticklen=.07, yticklen=.05

   axis, xticks=4, xrange=[0,360], xstyle=9, xticklen=-.02, color=0, xtitle='Longitude (Deg) & Day-of-Month', charsize=2, charthick=2
   axis, yaxis=0, yticks=6, yrange=[-90,90], ystyle=1, yticklen=-.01, color=0, ytitle='Latitude (Deg)', charsize=2, charthick=2
;stop

   XYOUTS,x0+iw/2,y1+30, title, ALIGNMENT = 0.5, charsize=2.5, charthick=2, color=0, /DEVICE
   XYOUTS,x0,y1+5,date_end,ALIGNMENT=0, charsize=1.8, charthick=2, color=0, /device
   XYOUTS,x1,y1+5,date_sta,ALIGNMENT=1, charsize=1.8, charthick=2, color=0, /device
   XYOUTS,675,5,'Nathan Rich/NRL', ALIGNMENT = 1, color=0,/device
   
   pr=2.
   FOR z=0,359 DO BEGIN
     IF dates(z) NE '' THEN BEGIN
	IF pr EQ 2 THEN XYOUTS, x0+((iw-1)-(iw/360.)*z), y0+3,dates(z), charsize=1.8, ALIGNMENT=0.5, /device
        pr=1/pr		; alternate printing days
     ENDIF
   ENDFOR

   tvlct,r,g,b,/get
   it = tvrd()
   write_gif,cam+wlimb+'_'+trim(string(cn))+'_'+str_rad+'.gif',it,r,g,b
ENDIF

!p.background=0

end 	; carrmapdisp.pro
