pro carrmap3, cn, cam, wlimb, MAP_SIZE=map_size, GET_BKG=get_bkg, RUNNING_DIFF=running_diff, FULL360=full360, SUMR=sumr, MAPHT=mapht, ANY_YEAR=any_year, LZ=lz, NORM=norm

;NAME: carrmap3
;PURPOSE: 
;     Create carrington maps using the carrmapmaker procedure. Arrays
; 	are output into files specified by the user at the end of the 
;	procedure.
;     Program only works for maps beginning after 6 January 1996.
;
;INPUTS:
; Input by user or procedure call:
;	cn	INT	carrington rotation
;	cam	STRING	camera (c1, c2, c3)
;	pol	STRING	'p' is polarized, 'u' is unpolarized, all else 
;			is regular intensity
;	wlimb	STRING 	enter 'wl' for west limb map, 'el' for east limb map -OR-
;			enter 304,284,195, or 171 for EIT
;
;KEYWORDS:
;	SUMR	Set to number of strips to sum for a summed-image map
;	ANY_YEAR	Use ANY_YEAR models from GETBKGIMG
;
; radii may be entered in the carrmapmaker routine
;
;ROUTINES CALLED:
;    carrmapmaker.pro
;
;Author: Nathan Rich, NRL, November 1996
;	 Julia Kraemer, NRL, June 7, 1996
;
; MODIFIED:	970108	J. Kraemer	add east limb option
;		970109 N. Rich		automate file save, change to carrmap3
;		970124 N. Rich		correct safefile for c3 maps
;		970213 N. Rich		modify query order
;		970304 N. Rich	stop before displaying maps
;		970311 N. Rich	add COMMON block map_display
;		970414 N. Rich	allow c1 maps, manual date input
;		970516 N. Rich  use mapsize in filename
;		970718 N. Rich	edit read prompts
;		970721 N. Rich  change image display
;		970730 N. RIch	save c*log/info to dir
;		971216 N. Rich  make automatic--no user prompt
;	980610 N. Rich	save as FITS file
;	981113 N. Rich	add ROLL-STA and ROLL-END to header
;	981118 N. Rich	Fix MEDIAN, RATIO header fields
;	990831 N. Rich	Update to make EIT maps using MERIDIANMAP.PRO
;	9911	N Rich	Add factor to WARPIT and header
;	2000/02	N Rich	Add ANY_YEAR keyword: no longer default for C2;
;			Remove level input--database picks latest level 
;			automatically; add LZ keyword
;	2000/05	nbr	Change c2 Radii; add WAVELENG to header; fix median calculation
;	2000/08 nbr	Take mapsize out of common block and move to carrmapmaker2 call
;

COMMON warpit, carrmap_x,obs_timen0, stripn0, time0, duration, rat, startdate, endate, normalize, sta_radius, end_radius, sta_center, end_center, rollcor, platescl, ht, factor, n, ctr
COMMON map_display, mapmax,mapmin

history1=''
pol=''
disp=''
IF N_PARAMS() LT 1 THEN BEGIN
   cam = ''
   level=''
   wlimb=''
   cn = 1
   num_r = 1
   READ, '           Which camera? (c1, c2 or c3)           ', cam
   print, '           Enter number of Carrington Rotation: '
   read, '            (zero to enter start/end manually)     ',cn
   IF cam EQ 'eit' THEN BEGIN
   	read, 'Enter 304,284,195, or 171: ',wlimb
   ENDIF ELSE $
   read, 'Enter "el" for east limb or "wl" for west limb: ', wlimb
   IF cam NE 'eit' THEN READ, '           How many maps?                       ', num_r
   READ, 'Enter longitudinal size: (default is 360):      ',mapsize
   print
   read,'Enter "r" for doing ratio: ',level
   IF level EQ 'r' THEN GET_BKG=1
   read,'Enter "nd" to NOT display each image as it is used:  ',disp
ENDIF
cam = STRLOWCASE(cam)
wlimb = TRIM(STRING(wlimb))
IF keyword_set(GET_BKG) THEN rat = 'T' ELSE rat = 'F'
IF keyword_set(MAPHT) THEN ht = mapht ELSE IF cam EQ 'eit' THEN ht = 512 ELSE ht = 181
rollcor=fltarr(2)
dir=''
display = 0
min=0
IF keyword_set(NORM) THEN normalize = 'T' ELSE normalize = 'F'
IF keyword_set(MAP_SIZE) THEN mapsize=map_size ELSE $
	IF cam EQ 'eit' THEN mapsize=720 ELSE mapsize=0
IF disp NE 'nd' THEN disp='d' 
num = 1
scale = 20
IF cam EQ 'c1' THEN rad=[1.1,1.125,1.15, 1.2,1.4, 1.8]
IF cam EQ 'c2' THEN BEGIN
	rad=[2.5,3,4,4.5,5]
	rrange = [2.4,6]
ENDIF
IF cam EQ 'c3' THEN BEGIN
	rad=[5,7,10,15,20,25]
	rrange = [5,28]
ENDIF
IF cam EQ 'eit' THEN BEGIN
	IF keyword_set(ALL) THEN num_r=4 ELSE num_r=1
	rad=STRARR(num_r)
	rad(*)='N/A'
	normalize= 'T'
ENDIF

IF keyword_set(SUMR) THEN $
	rad = rrange(0) +findgen(sumr+1)*(rrange(1)-rrange(0))/sumr
;rad = [5,6,7,8,9,10,12,14,16,18,20,22,24,26,28]
;rad = [2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.6,6]
;rad = [18,22,24,26,28,30]
rsz = size(rad)
num_r = rsz(1) 
;carrmap = fltarr(mapsize,ht,num_r)
;IF NOT(keyword_set(ALL)) THEN wlimb=REPLICATE(wlimb,num_r) ELSE wlimb= ['304','195','284','171']

print
;print,'    For Polarized Intensity, enter p. 
;print,'  For Unpolarized Intensity, enter u. 
;READ, '             For neither, hit return:   ', pol

IF keyword_set(RUNNING_DIFF) THEN processhdr = 'Running difference images' $
	ELSE processhdr = 'NONE'

IF cam EQ 'c1' THEN mapsize = 360 ;ELSE mapsize = 720

IF keyword_set(ANY_YEAR) THEN rat = 'ANY_YEAR'

limb = INTARR(4)
wlimb=STRING(wlimb)
eastlimb = strpos(wlimb,'e')
IF eastlimb GT -1 THEN BEGIN
   limb(1)=1	; turn east limb on
   limb(0)=0	; west limb, central meridian and carr longitude off
   limb(2:3)=0
   limbhdr = 'East'
ENDIF ELSE BEGIN
   limb(0)= 1	; turn west limb on
   limb(1:3) = 0
   limbhdr = 'West'
ENDELSE

IF disp eq 'd' THEN display=1

IF cam NE 'eit' THEN $
carrmapmaker2,cn, cam, num_r,rad, carrmap,pol,limb,level,display, wlimb, dhdr, FULL360=full360, RUNNING_DIFF=running_diff, LZ=lz, MAP_SIZE=mapsize $
ELSE meridianmap,cn,carrmap,wlimb,display,dhdr,RUNNING_DIFF=running_diff, LZ=lz

iw=mapsize

IF keyword_set(FULL360) THEN carrmap = rebin(carrmap,iw,360,num_r)

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*(ht+10)
;ENDIF ELSE BEGIN 
	num_ra = num_r
	xsz = iw
	ysz = num_r*(ht+10)
;ENDELSE
WINDOW,1,xsize=xsz, ysize=ysz	;,/FREE

IF keyword_set(SUMR) THEN BEGIN
	ysz = ht < 360
	summap = carrmap(*,*,0)
	FOR i=1,num_r-1 DO summap = summap + carrmap(*,*,i)
	summap = summap/num_r
   	tvscl,summap<mapmax>mapmin
   	label = 'SUMMED CR '+TRIM(STRING(cn))+' for '+cam+' '+wlimb
   	XYOUTS,iw/2,10, label, ALIGNMENT = 0.5, /DEVICE
ENDIF ELSE BEGIN
   FOR r=0,num_r-1 DO BEGIN
;   IF r LE 5 THEN BEGIN
	x = 0 
	y = (num_ra - 1 -r)*(ht+10) + 15
   	tvscl,carrmap(*,*,r)<mapmax>mapmin,x,y
   	label = 'r = '+TRIM(STRING(rad(r)))+', CR '+TRIM(STRING(cn))+' for '+cam+' '+wlimb
   	XYOUTS,x+mapsize/2,y, label, ALIGNMENT = 0.5, /DEVICE
;   ENDIF ELSE BEGIN
;	x = iw+15
;	y = (num_ra - 1 -r)*(ht+10) +15
;   ENDELSE
   ENDFOR
ENDELSE

savefile = ''
dir=''
print,'Please enter the directory to which you would 
print,'like to save the carrington maps to below.'
print,'Example: /net/corona/cplex3/carrmaps  (This is the default.)'
print,'You do not need quotes:'
;read, '       > ',dir
IF dir EQ '' THEN dir = '/net/corona/cplex3/carrmaps'
IF cam EQ 'c3' THEN dir = dir+'/c3'
IF cam EQ 'eit' THEN dir = dir+'/eit'
spawn,'mv -f *.log '+dir
spawn,'mv -f *.info '+dir
source = STRMID(dhdr.filename,1,1)
IF source EQ 1 THEN BEGIN
	levelhdr = 'Quick-Look Level 0.5'
	level = 'ql'
ENDIF ELSE BEGIN
	levelhdr = 'LZ/Final Level 0.5'
	level = ''
ENDELSE
wave='White Light'
IF cam NE 'eit' THEN instrument='LASCO' ELSE BEGIN
	instrument='EIT'
	limbhdr='Meridian'
	processhdr='DEGRID, FLAT_FIELD, DN/SEC, +1.0, LOG_SCL'
	wave=wlimb+'A'
	history1='Normalized to area above N limb'	; 4/11/00, NBR
ENDELSE
IF cam EQ 'c2' THEN history1='Uses 4-year monthly median, 1996-1999.'

IF keyword_set(SUMR) THEN BEGIN
	newhdr=strarr(2)
	savefile=cam+wlimb+'_'+trim(string(cn))+'_sum' +level +'.fts'
	print,'Saving ',savefile


	MKHDR,newhdr,summap
	;sxaddpar, newhdr, 'SIMPLE','T'
	;sxaddpar, newhdr, 'BITPIX',-32
	;sxaddpar, newhdr, 'NAXIS', 2
	;sxaddpar, newhdr, 'NAXIS1',mapsize
	;sxaddpar, newhdr, 'NAXIS2',ht
	get_utc,today,/ecs
	sxaddpar, newhdr, 'DATE',today
	
	sxaddpar, newhdr, 'CARROTAT',cn,' Carrington Rotation Number'
	sxaddpar, newhdr, 'LIMB',limbhdr
	sxaddpar, newhdr, 'HEIGHT','N/A'
	sxaddpar, newhdr, 'HTUNIT','Rsun'
	sxaddpar, newhdr, 'PLATESCL',platescl,' ARCSEC/PIXEL'
	sxaddpar, newhdr, 'RSUN-STA',sta_radius,' Solar radius at rotation start'
	sxaddpar, newhdr, 'RSUN-END',end_radius,' Solar radius at rotation end'
	sxaddpar, newhdr, 'RSUNUNIT','DEGREES'
	sxaddpar, newhdr, 'CENX-STA',sta_center(0)
	sxaddpar, newhdr, 'CENY-STA',sta_center(1)
	sxaddpar, newhdr, 'CENX-END',end_center(0)
	sxaddpar, newhdr, 'CENY-END',end_center(1)
	sxaddpar, newhdr, 'ROLL-STA',rollcor(0),' Clockwise correction to roll angle (Deg)'
	sxaddpar, newhdr, 'ROLL-END',rollcor(1),' Clockwise correction to roll angle (Deg)'
	sxaddpar, newhdr, 'FILENAME',savefile
	sxaddpar, newhdr, 'STA-DATE',startdate,' Time of rotation start at the limb'
	sxaddpar, newhdr, 'END-DATE',endate,' Time of rotation end at the limb'
	sxaddpar, newhdr, 'N_FRAMES',ctr,' Number of images used in this map'
	sxaddpar, newhdr, 'TELESCOP','SOHO'
	sxaddpar, newhdr, 'INSTRUME',instrument
	sxaddpar, newhdr, 'DETECTOR',dhdr.detector
	sxaddpar, newhdr, 'FILTER',dhdr.filter
	sxaddpar, newhdr, 'POLAR',dhdr.polar
	sxaddpar, newhdr, 'WAVELENG',wave
	sxaddpar, newhdr, 'LEVEL',levelhdr
	sxaddpar, newhdr, 'COMMENT',' All images are normalized to exposure time.'
	sxaddpar, newhdr, 'COMMENT',' Summed heights over entire image'
	sxaddpar, newhdr, 'BOXNORM',normalize,' normalized to the median of a box in each image; True or False'
	sxaddpar, newhdr, 'RATIO', rat,' monthly minimum model; T or F or ANY_YEAR'
	sxaddpar, newhdr, 'MOREPROC',processhdr,' Additional processing'
	sxaddpar, newhdr, 'COMMENT',' 1 rotation of the sun = 27.3 days'

	writefits,dir+'/'+savefile,summap,newhdr
ENDIF ELSE BEGIN
;stop
FOR r = 0,num_r-1 DO BEGIN
	newhdr=strarr(2)
	radius=rad(r)
	if cam EQ 'eit' THEN radstr='mer' ELSE BEGIN
		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=zero+trim(string(fix(radius)))
	ENDELSE
	savefile=cam+wlimb+'_'+trim(string(cn))+'_'+radstr +level +'.fts'
	print,'Saving ',savefile


	MKHDR,newhdr,carrmap(*,*,r)	; from $NRL_LIB/astro/pro/fits
	;sxaddpar, newhdr, 'SIMPLE','T'
	;sxaddpar, newhdr, 'BITPIX',-32
	;sxaddpar, newhdr, 'NAXIS', 2
	;sxaddpar, newhdr, 'NAXIS1',mapsize
	;sxaddpar, newhdr, 'NAXIS2',ht
	get_utc,today,/ecs
	sxaddpar, newhdr, 'DATE',today
	
	sxaddpar, newhdr, 'CARROTAT',cn,' Carrington Rotation Number'
	sxaddpar, newhdr, 'LIMB',limbhdr
	sxaddpar, newhdr, 'HEIGHT',rad(r)
	sxaddpar, newhdr, 'HTUNIT','Rsun'
	sxaddpar, newhdr, 'PLATESCL',platescl,' ARCSEC/PIXEL'
	sxaddpar, newhdr, 'RSUN-STA',sta_radius,' Solar radius at rotation start'
	sxaddpar, newhdr, 'RSUN-END',end_radius,' Solar radius at rotation end'
	sxaddpar, newhdr, 'RSUNUNIT','DEGREES'
	sxaddpar, newhdr, 'CENX-STA',sta_center(0)
	sxaddpar, newhdr, 'CENY-STA',sta_center(1)
	sxaddpar, newhdr, 'CENX-END',end_center(0)
	sxaddpar, newhdr, 'CENY-END',end_center(1)
	sxaddpar, newhdr, 'ROLL-STA',rollcor(0),' Clockwise correction to roll angle (Deg)'
	sxaddpar, newhdr, 'ROLL-END',rollcor(1),' Clockwise correction to roll angle (Deg)'
	sxaddpar, newhdr, 'FILENAME',savefile
	sxaddpar, newhdr, 'STA-DATE',startdate,' Time of rotation start at the limb (meridian)'
	sxaddpar, newhdr, 'END-DATE',endate,' Time of rotation end at the limb (meridian)'
	sxaddpar, newhdr, 'N_FRAMES',ctr,' Number of images used in this map'
	sxaddpar, newhdr, 'TELESCOP','SOHO'
	sxaddpar, newhdr, 'INSTRUME',instrument
	sxaddpar, newhdr, 'DETECTOR',dhdr.detector
	sxaddpar, newhdr, 'FILTER',dhdr.filter
	sxaddpar, newhdr, 'POLAR',dhdr.polar
	sxaddpar, newhdr, 'WAVELENG',wave
	sxaddpar, newhdr, 'LEVEL',levelhdr
	sxaddpar, newhdr, 'COMMENT',' All images are divided by exposure time.'
	sxaddpar, newhdr, 'BOXNORM',normalize,'T if each image normalized to first image'
	sxaddpar, newhdr, 'RATIO', rat,' monthly minimum model; T or F or ANY_YEAR'
	sxaddpar, newhdr, 'MOREPROC',processhdr,' Additional processing'
	sxaddpar, newhdr, 'HISTORY',history1
	map0 = carrmap(*,*,r)
	good = where(map0 GT 0)
	sxaddpar, newhdr, 'MEDIAN',median(map0(good)), ' To assist in display'
	sxaddpar, newhdr, 'COMMENT',' 1 rotation of the sun = 27.3 days'
	sxaddpar, newhdr, 'COMMENT',' Images corrected for roll angle > 1 deg'

	writefits,dir+'/'+savefile,map0,newhdr


	;openw,lu,savefile,/get_lun
	;writeu,lu,carrmap(*,*,r)
	;close,lu
	;free_lun,lu
ENDFOR
ENDELSE

end 	; carrmap3.pro
