pro list2maps, num_r, map_wdth, map_ht, wlimb, list,name, VERTICAL=vertical, DISPLAY= display, RATIO=ratio, NO_NORM=no_norm, RANGE=range, CN = cn

;
;PURPOSE: 
;    	Calls the listmapmaker procedure to make synoptic maps from
;	a list of LASCO FITS images. Saves resulting maps as FITS files.
;
;INPUTS:
; Input by user or procedure call:
;	num_r	INT	number of heights to make maps for
;	map_wdth  INT	desired width of product, in pixels
;	map_ht	INT	desired height of product, in pixels
;	wlimb	STRING 	enter 'wl' for west limb map, 'el' for east limb map
;	list	STRING	file name with list of FITS images, including path
;
;OPTIONAL INPUTS:
;	name	STRING	identifier for output files, besides limb, camera, time
;			Default is something like 'c2wl_970823_3.00.fts'
;
; radii are entered in the carrmapmaker routine
;
;KEYWORDS:
;	VERTICAL  	set if strips are to be vertical; default is constant radius about sun center
;	DISPLAY		set to display images on screen as they are processed
;	RATIO		set to use ratio (with background) for each image
;	NO_NORM		set if box normalization is NOT desired;
;			(The boxes that appear in the display images are for
;			 normalization.)
;	RANGE		set if entering a custom angular range for your map
;			(Default is 0 - 180 degrees/top to bottom.)
;	CN		set to desired Carrington Number; default is list range
;
;ROUTINES CALLED:
;    listmapmaker.pro
;
;Author: Nathan Rich, NRL, 1997/12/30
;
; MODIFIED:	971230 NBR	based on carrmap3.pro
;

;COMMON warpit, carrmap_x,obs_timen0, stripn0, time0, duration, mapsize
COMMON map_disp, mapmax,mapmin, mapsize, cam, vert, cnum, mapht, dhdr, normalize, rat, startdate, platescl


dir=''
min=0
pol=''
print
IF not(keyword_set(CN)) then cnum = 1 ELSE cnum = cn
disp=''


IF N_PARAMS() GT 2 THEN BEGIN
   mapsize=map_wdth
   mapht = map_ht
   listfile=list
   IF DATATYPE(name) EQ 'UND' THEN name = ''
;   cn = carrnum
ENDIF ELSE BEGIN
   name=''
   cam = ''
   level=''
   wlimb=''
   wstrip=''
   listfile=''
   num_r=3
   ratio='n'
   print,'Enter filename with names of files (including path) to be used'
   read,': ',listfile
   print
   read,'Use vertical strips? [y/n]  ',wstrip
   read, 'Enter "el" for east limb or "wl" for west limb: ', wlimb
   READ, '           How many maps?                       ', num_r
   print
   print,'How would you like to label these maps? 
   read,'(besides camera,limb,startdate; default is nothing else): ',name
   print
   read,'Enter "d" to display each image as it is processed:  ',disp
   mapht = 181
   mapsize= 360
ENDELSE

IF keyword_set(vertical) THEN vert = 'y' ELSE vert = ''
num = 1
scale = 20

IF disp eq 'd' THEN display=1
rad=FLTARR(num_r) 
carrmap = fltarr(mapsize,mapht,num_r)

; 			** Define header values **

IF keyword_set(NO_NORM) THEN normalize = 'F' ELSE normalize = 'T'
IF keyword_set(RATIO) THEN rat = 'T' ELSE rat = 'F'
processhdr = 'NONE'
levelhdr = 'LZ LEVEL 0.5'
eastlimb = strpos(wlimb,'e')
IF eastlimb GT -1 THEN  limbhdr = 'East' ELSE limbhdr = 'West'

;

listmapmaker, listfile, num_r, rad, carrmap, wlimb, display=display, range=range

cn=trim(string(cn))
dispname = cam+' '+wlimb+' '+cn
iw=mapsize
blah=''
print
;read,'Please press RETURN to continue.',blah
IF cam EQ 'c1' THEN scale = 150

;
;
;IF num_r GE 6 THEN BEGIN
;	num_ra = num_r+1 
;	xsz = 2*iw+15
;	ysz = 5*195
;ENDIF ELSE BEGIN 
	num_ra = num_r
	xsz = iw
	ysz = num_r*(mapht+10)
;ENDELSE
WINDOW,/FREE,xsize=xsz, ysize=ysz

FOR r=0,num_r-1 DO BEGIN
;   IF r LE 5 THEN BEGIN
	x = 0 
	y = (num_ra - 1 -r)*(mapht+10) + 15
;   ENDIF ELSE BEGIN
;	x = iw+15
;	y = (num_ra - 1 -r)*(mapht+10) +15
;   ENDELSE
   tvscl,carrmap(*,*,r)<mapmax>mapmin,x,y
   label = 'r = '+TRIM(STRING(rad(r)))+', '+dispname
   XYOUTS,x+mapsize/2,y, label, ALIGNMENT = 0.5, /DEVICE
ENDFOR

savefile = ''
dir=''
print,'Please enter the directory to which you would 
print,'like to save the synoptic maps to below.'
print,'Example: ~  (This is the default.)'
print,'You do not need quotes:'
read, '       > ',dir
IF dir EQ '' THEN dir = '~'
spawn,'mv c*log '+dir
spawn,'mv c*info '+dir
;IF level EQ 'ql' THEN level = '.ql' ELSE level = ''
FOR r = 0,num_r-1 DO BEGIN
	newhdr=strarr(2)
	radius=rad(r)
	;if cam EQ 'c3' and radius LT 10 then zero = '0' else zero = ''
	;if cam EQ 'c2' then radius = radius*10.
	;if cam EQ 'c1' then radius = radius*100.
	radstr=strmid(trim(string(radius)),0,4)
	savefile=cam+wlimb+'_'+cn+'_'+radstr +name+'.fts'
	print,'Saving ',savefile
;stop

	FXHMAKE,newhdr,carrmap(*,*,r)
	get_utc,today,/ecs
	fxaddpar, newhdr, 'DATE',today
	
	;fxaddpar, newhdr, 'CARROTAT',cn,' Carrington Rotation Number'
	fxaddpar, newhdr, 'LIMB',limbhdr
	fxaddpar, newhdr, 'HEIGHT',rad(r)
	fxaddpar, newhdr, 'HTUNIT','Rsun'
	fxaddpar, newhdr, 'PLATESCL',platescl,' ARCSEC/PIXEL used'
	;fxaddpar, newhdr, 'RSUN-STA',sta_radius,' Solar radius at rotation start'
	;fxaddpar, newhdr, 'RSUN-END',end_radius,' Solar radius at rotation end'
	fxaddpar, newhdr, 'RSUNUNIT','DEGREES'
	;fxaddpar, newhdr, 'CENX-STA',sta_center(0)
	;fxaddpar, newhdr, 'CENY-STA',sta_center(1)
	;fxaddpar, newhdr, 'CENX-END',end_center(0)
	;fxaddpar, newhdr, 'CENY-END',end_center(1)
	;fxaddpar, newhdr, 'ROLL-STA',rollcor(0),' Clockwise correction to roll angle (Deg)'
	;fxaddpar, newhdr, 'ROLL-END',rollcor(1),' Clockwise correction to roll angle (Deg)'
	fxaddpar, newhdr, 'FILENAME',savefile
	fxaddpar, newhdr, 'STA-DATE',startdate,' Time of first image'
	fxaddpar, newhdr, 'END-DATE',dhdr.date_obs+' '+dhdr.time_obs,' Time of last image'
	fxaddpar, newhdr, 'TELESCOP','SOHO'
	fxaddpar, newhdr, 'INSTRUME','LASCO'
	fxaddpar, newhdr, 'DETECTOR',dhdr.detector
	fxaddpar, newhdr, 'FILTER',dhdr.filter
	fxaddpar, newhdr, 'POLAR',dhdr.polar
	fxaddpar, newhdr, 'LEVEL',levelhdr
	;fxaddpar, newhdr, 'COMMENT',' All images are normalized to exposure time.'
	fxaddpar, newhdr, 'BOXNORM',normalize,' normalized to the median of a box in each image; True or False'
	fxaddpar, newhdr, 'RATIO', rat,' monthly minimum model; T or F'
	fxaddpar, newhdr, 'MOREPROC',processhdr,' Additional processing'
	fxaddpar, newhdr, 'MEDIAN',median(carrmap(*,*,r)), ' To assist in display'
	;fxaddpar, newhdr, 'COMMENT',' 1 rotation of the sun = 27.3 days'

	writefits,dir+'/'+savefile,carrmap(*,*,r),newhdr
ENDFOR

end 	; carrmap3.pro
