;+
pro carrmapdisp, limb, r, RANGE=range, VERTICAL=vertical, CMAPDIR=cmapdir
;
; Purpose:	Display carrington maps in a scrollable format
;
; (NOTE: previous carrmapdisp.pro renamed carrmapdisp0.pro)
;
; Optional Inputs:
;	limb	STRING	'w', 'e' or search parameter
;	r	FLOAT	height of desired map
; 
; KEYWORDS:
;	RANGE = [cn0, cn1]	INTARR	range of carrington numbers to display
;	/VERTICAL	If set, display maps stacked, not side-by-side
;	CMAP='/path/of/fits/files'	defaults to location at NRL
;
; Written 980917 by Nathan Rich, Interferometrics/NRL
;  030821, nbr - add RANGE, vertical display options, rename from slide_carrmap.pro
;  030829, nbr - fix bug in list=findfile()
;  200327, nbr - fix list(); adjust display
;
; @(#)carrmapdisp.pro	1.2 09/02/03 - LASCO NRL IDL Library
;-

IF N_PARAMS() LT 2 THEN BEGIN
	limb = ''
	r = 2.5
	range=[1900,2000]
	read,'Please enter limb or search param. (e/w): ',limb
	read,'Please enter desired height in Rsun: ',r
	read,'Please enter Carr Number range ("1910,1920"):',range
ENDIF


radii = [2.5,3,4.5,5,7,10,15,20,25]

loc = find_closest(r,radii)
r = radii(loc)

IF r LT 2.5 THEN cam = 'c1' ELSE $
IF r LT 5 THEN cam = 'c2' ELSE cam = 'c3'

; compute scale factors for display
IF cam EQ 'c3' THEN BEGIN
	rstr = strtrim(string(r,format='(i2.2)'))
	upper = 1.085 - 0.0033*r
	lower = 0.89 + 0.005*r
	mapwid=360
ENDIF
IF cam EQ 'c2' THEN BEGIN
	rstr = strtrim(string(r*10,format='(i2.2)'))
	upper = 1.8 - 0.05*r
	lower = 0.73
	mapwid=720
ENDIF
IF cam EQ 'c1' THEN rstr = strtrim(string(r*100,format='(i3.2)'))

IF keyword_set(CMAPDIR) THEN cd,cmapdir, current=dir0 ELSE $
cd,'/net/corona/cplex3/carrmaps/', current=dir0
IF cam eq 'c3' THEN cd,'c3'
list = findfile(cam+limb+'*_'+rstr+'*.fts')
last = n_elements(list) - 1
first= 0
cns=strmid(list,5,4)
IF keyword_set(RANGE) THEN BEGIN
	first=find_closest(range[0],cns)
	last =find_closest(range[1],cns)
ENDIF
print,list[first:last]
cmap = readfits(list[first],hdr)
;cmap = shift(cmap,1)			; ** fix for blank first column
; doesn't seem necessary - nbr, 030821
cmap = REBIN(cmap,mapwid,181)
mapmax=upper*median(cmap)
mapmin=lower*median(cmap)
IF keyword_set(VERTICAL) THEN BEGIN
	bigmap = bytscl(cmap,mapmin,mapmax) 
	mapht = 720
ENDIF ELSE BEGIN
	bigmap = reverse(bytscl(cmap,mapmin,mapmax))
	mapht = 181
ENDELSE
bsz = size(bigmap)

FOR i = first+1,last DO BEGIN
   cmap = readfits(list[i],hdr)
   ;cmap = shift(cmap,1)			; ** fix for blank first column
   cmap = REBIN(cmap,mapwid,181)
   mapmax=upper*median(cmap)
   mapmin=lower*median(cmap)
   sz = size(cmap)
   IF keyword_set(VERTICAL) THEN BEGIN
   	dummy = fltarr(sz(1),181+bsz[2]+5)
   	dummy[0,181+5] = bigmap
   	dummy[0,0] = bytscl(cmap,mapmin,mapmax)
   ENDIF ELSE BEGIN
   	dummy = fltarr(sz(1)+bsz(1),181)
   	dummy[0,0] = bigmap
   	dummy[bsz(1),0] = reverse(bytscl(cmap,mapmin,mapmax))
	
   ENDELSE
   bigmap = dummy
   bsz = size(bigmap)
ENDFOR

print
print,'Maps shown for ',STRUPCASE(cam),' Rsun =',r
print
IF cam EQ 'c2' THEN print,' NOTE: C2 Maps are shown twice the size/resolution of C3 maps.'
print
slide_image,bigmap, show_full=0, slide_window=win0,xvisible=mapwid, yvisible=mapht+1,ysize=mapht+1

; label separate files
wset,win0
FOR i=first,last DO BEGIN
	IF keyword_set(VERTICAL) THEN BEGIN
		x=0
		y=bsz[2]-(i-first)*186 - 181
	ENDIF ELSE BEGIn
		x=(i-first)*720
		y=5
	ENDELSE
	xyouts,x,y,list[i],/device
ENDFOR
cd,dir0
END
