pro meridianmap, cn,cmap,pol,disp, dhdr, mapsize, RUNNING_DIFF=running_diff, LZ=lz, $
		DEBUG=debug, MVI=mvi, _EXTRA=_extra

;
;PURPOSE: 
;     Returns carrington map of meridian for a passed 
;     carrington number.
;
;INPUTS:
;     	cn	INT:	carrington number
;	cmap	FLTARR(mapsize,ht,num_r):	passed empty
;	pol	STRARR:	171,195,284 or 304 or all of these; indicates polarizer value;
;			Also is same as wlimb in CARRMAP3
;	disp	INT:	1 to display images
;	hdr	STRUCT: dummy header sent back to carrmap3 for fits header
;
;KEYWORDS:
;	RUNNING_DIFF	If set, returns a running difference map
;	LZ	IF set, uses LZ data only, else uses LZ or QL
;	MVI	Call wrunmovie with images used to make carrington maps
;
;
; OUTPUTS:	cmap = fltarr([360 or 720],ht,[1 or 4]):	carrington map
;		
;
;ROUTINES CALLED:
;    qdb.pro
;    carrdate2.pro
;    resize2.pro
;    getc2c3norm2.pro
;
; AUTHOR:	Nathan Rich, NRL, Aug. 1999
; HISTORY:	carrmapmaker2.pro
;	2001.06.07, nbr - Take mapsize out of common block; initialize cmap at beginning
;	2001.6.21, nbr - Change mapmax
;	2002. 1.29, nbr - Use get_cache for jukebox files
;	2002. 4.22, nbr - Add MVI
;	2006.10.03, nbr - Considerable upgrades/changes
;	2008.05.27, nbr - Accomodate new qdb structure format
;   	2009.06.15, nbr - Adjust normalization criteria
;
; @(#)meridianmap.pro	1.5 06/15/09 - IDL NRL LASCO Library
;
;-

COMMON warpit, carrmap_x, obs_timen0, stripn0, time0, duration, rat, time0_str, time1_str, normalize, sta_radius, end_radius, sta_center, end_center, rollcor, platescl, ht, factor, numim, ctr
COMMON map_display, mapmax,mapmin
;COMMON get_im, 		on_s,on_times,ff_ratio, hdr, prev, pylon_im,lulog
COMMON get_im, 		off_s,off_times,ff_ratio, off_hdr, prev, pylonim, lulog, ffv

IF keyword_set(MVI) THEN yloc = 0
cx0 = 505.5/2. & cy0 = 514.3/2.
cen=1
;DEBUG=0
cmap = FLTARR(mapsize,512)
y0=0
stripwidth=0
fullstripw=0
cmapxleft=0
mapnext=1
checkstrip=0
wlimb='mer'
cam='EIT'
barht = ht/36 < 20
prev = fltarr(512,512)
rotangle = 1
skiplast= 0
factor=1.
oldstripmeds = fltarr(10)
oldmedends = fltarr(20)
stripends = fltarr(22)
avg_strip_ends=[15,1,1.337,4.57]
skp=''
near = ''
listfile = ''
sta_str = ''
end_str = ''
endshort = 'no'
IF cam EQ 'c1' THEN scale = 60 ELSE scale = 200
mscale = 20
strip = fltarr(ht)
mnm = fltarr(512,512)		; c2 minimum
delta = 0.
first = 1
sta_center=fltarr(2)
end_center=fltarr(2)
refbox=0


IF cn GT 1900 THEN BEGIN
   time0s = carrdate2(cn)
   time1s = carrdate2(cn+1)
ENDIF ELSE BEGIN
   print, 'Carrington number invalid. Please try again.'
   return
ENDELSE


time0_str = utc2str(time0s,/ECS)
time1_str = utc2str(time1s,/ECS)
t_start = time0s.mjd + DOUBLE(time0s.time/86400000.)
t_end   = time1s.mjd + DOUBLE(time1s.time/86400000.)	
duration = t_end - t_start		; duration is in decimal days
degperday = 360./duration		; degrees of rotation per day
				; changed from 31.17, 4/3/00, nbr
obs_timen0 = time0_str

;rconv=79
filt = '0'		; EIT Al+1
platescl=2.59*2		; Baseline 512x512
;median0 = 4.5		; FOR SQRT
;median1 = 5.0		;
IF pol EQ '171' THEN median0=1.6 ELSE median0 = 0.85		; For LOG_SCL
median1 = 1.0		;
mapmax=3
mapmin=0
disp_factor=2
;deltend=0.015
deltend=0.5	; 4/5/02
x11=3/2		; Corners of 512x512 image (6/26/01)
x12=162/2
x21=861/2
x22=1020/2
y11=x11
y12=145/2		;x12
y21=x21
y22=x22-(x12-y12)
camnum = '3'
levelnum='2'
c1024=80	; guess based on 171A
IF pol EQ 304 THEN BEGIN
	mref = 1.9
	est_val=mref/10.
	polnum='0' 
	p10_25=	884	; average of DATAP10 and DATAP25
	;p25 = 892	; based on Clear Filter images on 98/01/01
ENDIF ELSE IF pol EQ 284 THEN BEGIN
	mref = 0.63
	est_val=mref
	polnum='2' 
	p10_25= 909	
	;p25 = 925
ENDIF ELSE IF pol EQ 171 THEN BEGIN
	mref = 2.4
	est_val=mref/10.
	polnum='3' 
	p10_25= 870
	;p25 = 1009	; based on datap25 for CR 1931
	c1024 = 80	;
ENDIF ELSE IF pol EQ 195 THEN BEGIN
	mref = 2.0
	est_val=mref/10.
	polnum='1' 
	p10_25= 909
	;p25 = 926
ENDIF
mapmax = 1.5*mref
;
;

IF keyword_set(RUNNING_DIFF) THEN BEGIN
	mapmax=20
	mapmin=0
	median0 = 0.9
	median1 = 1.1
	deltend = 0.5
ENDIF
   
szcm = SIZE(cmap)
IF szcm(0) EQ 2 THEN filts=1 ELSE filts=4
df = disp_factor
avg_med= [40,20,8]
carrmap_x = 0
stripn0 = fltarr(ht,filts)
obs_timen0 = STRARR(filts)
tempstrip = FLTARR(2,ht)

IF keyword_set(LZ) THEN levelnum='= 2' ELSE levelnum='> 0'

;IF keyword_set(QL) THEN BEGIN
;   levelnum = '1' 
;   levelstr = 'QUICK-LOOK'
;ENDIF ELSE BEGIN
;   levelnum = '2'
;   levelstr = 'LZ'
;ENDELSE
print
print,time0_str
print,'to'
print,time1_str

IF filts EQ 4 THEN polphrase='' ELSE polphrase= ' and polar='+polnum

query = 'select img_files.filename, date_obs, exptime, polar, naxis1, naxis2, diskpath, filter, lp_num, datap75, datazer from img_leb_hdr, img_files, img_stats where img_leb_hdr.fileorig=img_files.fileorig and img_files.filename=img_stats.filename and (lp_num=9 or lp_num=14) and img_files.date_mod=img_leb_hdr.date_mod and camera='+camnum+polphrase+' and (filter='+filt+' or filter=2) and p2col=1043 and p2row=1024 and naxis1 > 511 and naxis2=naxis1 and source '+levelnum+' and date_obs between "'+time0_str+'" and "'+time1_str+'"'
	; 
qu=query
print,query
s=qdb('lasco',qu)
help,/struct,s
	
sz = SIZE(s.filename)
IF sz[0] EQ 0 THEN BEGIN
	mapsize=0
	return
ENDIF
numim = sz(1)
nextim = STRARR(numim)
nextim(*) = 'Init'
factors=dblarr(numim)

n1024 = where(s.naxis1 EQ 1024)
s(n1024).datap75 = s(n1024).datap75+c1024
per = median(s.datap75)
print
print,'time0 = ',time0_str,'    time1 = ',time1_str
print
print,'Found ',numim,' ',cam,' images for CR ',cn, ',',pol,'A'
print
;stop
print,'Sorting s.'
ord = sort(s.date_obs)

;read,'Enter mapsize (720 or 360): ',mapsize
;IF numim GT 720 THEN mapsize = 720 ELSE mapsize = 360

;cmap=FLTARR(mapsize,ht)
pixperday = mapsize/duration		; factor to multiply days by to get 
					; the map size in x direction
					; (pixels per day)


OPENW,l1,cam+pol+'_'+TRIM(STRING(cn))+'.log' ,/get_lun
OPENW,l2,cam+pol+'_'+TRIM(STRING(cn))+'.info',/get_lun
printf,l1,'Found ',numim,' ',cam,' images for CR ',cn,pol,'A'
printf,l2,'White marks near the bottom of the image indicate where there is projected data.'
printf,l2
printf,l2,'Found ',numim,' ',cam,' images for CR ',cn
printf,l1,'Found ',numim,' ',cam,' images for CR ',cn
printf,l2
printf,l1,'time0 = ',time0_str,'    time1 = ',time1_str
printf,l2,'carrmap_x is column directly from image; either side +/- mapdelta/2 is projected'
printf,l2,''
   printf,l1,'Filename    ' $
	+'Carrmapx' $
	+'Mapdelta' $
;	+'Median(im[qi)' $
	+' Factor      ' $
	+' Max(im)     ' $
	+'Stripwi ' $
	+' Stripmedian ' 


IF disp EQ 1 THEN window,2,xsize=512,ysize=512 & window,1,xsiz=512,ysiz=512
window,0,xsize=mapsize,ysize=ht
window,1	,xsiz=256,ysiz=200
tn_now = t_start +double(0.5/pixperday)
ctr = 1

flist = s(ord).diskpath+'/'+s(ord).filename
filenames = s.filename(ord)

FOR k = 0,numim-1 DO BEGIN
   ;dispwin = 1 + (ctr MOD 2)
   dispwin = 2
   ;print
   toskip = 'n'
   filename = filenames[k]
   IF cn LT 1978 THEN filen = get_cache(k,flist) ELSE filen = flist[k]
   ;printf,l1,filen
   obs_timen = s(ord(k)).date_obs

   utcdate = str2utc(obs_timen)
   yymmdd = utc2yymmdd(utcdate)
   solar_ephem,yymmdd[0],radius=radius,/soho
   rconv = radius*3600/platescl		; rconv is solar radius in pixels
   print,filename,', meridianmap: k = ',k

;  ** Begin carrwarp1. **
;
   dayn = str2utc(obs_timen)
   tn = dayn.mjd + DOUBLE(dayn.time/86400000.)
 				; time is milliseconds, changed to day units
   				; day is modified julian day
   ;tn0 = t_start + DOUBLE((carrmap_x + 0.5)/pixperday)	;  = previous tn_now
   tn0 = tn_now
				; time at middle of last col (first column is 0)

   deltaday = tn - tn0		; time between middle of column of last image and present image
   fltmapdelta=deltaday*pixperday
   help,fltmapdelta
   mapdelta = ROUND(fltmapdelta)	; use ROUND, 5/8/00
   ;IF k gt 0 THEN 
   ;mapdelta=mapdelta+1	; +1, 4/28/00
   print, 'tn =',tn,'  deltaday =',deltaday,'  mapdelta =',mapdelta
help,tn0
;stop
   IF deltaday LE 0 and k GT 0 THEN BEGIN
	;printf,l1,'Skipped ',s.filename(ord(k)),': Out of Order or too close to previous.'
	;print,'Skipping ',s.filename(ord(k)),': Out of Order or too close to previous.'
	print,'Skipping file ',filen
	help,carrmap_x
	;wait,2
	GOTO, skipped
   ENDIF
;
;  ** End carrwarp1.   **

   IF nextim(ord(k)) NE 'ok' and k GT 0 THEN BEGIN
	print,'Skipping file: '+filen+' because '+ nextim(ord(k))
	print,nextim(ord(k))
	printf,l1,'Skipping file: '+filen+' because '+nextim(ord(k))
	wait,2
	goto,skipped
   ENDIF


   number = strmid(filename,0,8)
   utcday = str2utc(obs_timen)
   yymmdd = utc2yymmdd(utcday)

;***************************************************************    

help,k
   IF rat EQ 'T' THEN get_bkg = 1 ELSE get_bkg = 0
   im0 = mk_img(filen,/automax, /degrid, /NO_DISP, /flat, PROFILE=1, SUNC=cen, ROLL=rotangle, PAN=0.5)

   junk=lasco_readfits(filen,hdr,/no_img)
	;IF hdr.filter EQ 'Al +1' THEN im0=im0+0.5

   ;im = reduce_std_size(im0,hdr,/full)		; sizes im to 1024x1024 
   szim = SIZE(im0)

      IF szim(0) EQ 0 THEN BEGIN
	print,'File not found--skipping'
	printf,l1,'File not found--skipping '+filen
	GOTO, skipped
      ENDIF
;
; ** Begin shift image to common center **
;
   ;cen = get_sun_center(hdr,full=1024)		; done in mk_img
   cx = cen.xcen
   cy = cen.ycen
   IF first THEN BEGIN
	sta_radius = radius
	sta_center(0) = cx
	sta_center(1) = cy
   ENDIF
   IF tn GT 51211.0 and tn LT 51211.958 THEN BEGIN	;=1999/02/02 until ~23:00
	cx = 416/2
	cy = 658/2	; eyeballed, nbr, 4/11/00
   ENDIF

   shiftx=0 & shifty=0
   IF ABS(cx - cx0) GT 1. THEN shiftx = cx0-cx
   IF ABS(cy - cy0) GT 1. THEN shifty = cy0-cy

   shiftmsg = 'Shifting image '+trim(string(shiftx))+' in x and '+trim(string(shifty))+' in y.'
   print,shiftmsg
   wait, 1
   im0 = SHIFT(im0,shiftx,shifty)

; ** Im is now 512x12 centered at cx0, cy0 **
;
; ** Im is rotated in MK_IMG
;
;   IF (tn GT 50224.0 AND tn LT 50224.83) OR $
;      (tn GT 50408.724 AND tn LT 50409.33) then BEGIN
;	im = ROTATE(im,3)		; rotate images from 960521 and 961122
;	IF cam EQ 'c3' THEN im = SHIFT(im,-12,25)
;   ENDIF
;
;   pnt = GET_SC_POINT(obs_timen)
;   rotangle = -1*pnt.sc_roll	; degrees
;   IF filename EQ '42004728.fts' THEN rotangle= 38.	; ** nbr, 4/5/00
;
;   IF ABS(rotangle) GT 1 THEN BEGIN
;	print,'Rotating ',filename,rotangle,' degrees CW.'
;	printf,l1,'Rotating ',filename,rotangle,' degrees CW.'
;	im =rot(temporary(im),rotangle,1,cx,cy,/pivot,/interp)
	IF carrmap_x LE 2 and rotangle GT 2. THEN rollcor(0)=rotangle
;   ENDIF


   nz = where(im0 GT 0)

   ;stats, im[qi], arr

   
   mx0 = max(im0)
   m0=median(im0)
   ;IF k EQ 0 THEN mapmax = CEIL(m)

   IF disp EQ 1 or keyword_set(MVI) THEN BEGIN
	IF szim(1) GT 600 THEN BEGIN
	   xs = szim(1)/disp_factor
	   ys = szim(2)/disp_factor
	   disp_im = hist_equal(rebin(im0,xs,ys))
 	ENDIF ELSE disp_im = hist_equal(im0)
   	IF pol EQ 'p' OR pol EQ 'u' THEN scale = 80
	IF disp THEN BEGIN
		wset,dispwin
   		tv,disp_im
	ENDIF
	IF keyword_set(MVI) THEN BEGIN
      		WINDOW, XSIZE = 512, YSIZE = 512, /PIXMAP,/FREE
      		IF yloc EQ 0 THEN BEGIN
			win_index = !D.WINDOW 
			all_hdr = hdr
			labels = filename
		ENDIF ELSE BEGIN
			win_index=[win_index,!D.WINDOW]
			all_hdr = [all_hdr,hdr]
			labels = [labels,filename]
		ENDELSE
      		TV, disp_im
      		XYOUTS, 5, 5, cam+pol+' '+obs_timen, /DEVICE	;, CHARSIZE=csize
       		DEVICE, COPY = [0, 0, 512, 512, 0, 0, win_index(yloc)]
		yloc = yloc+1
	ENDIF

   ENDIF


   print,'median(im0) ='+STRING(m0)+'  max(im0) =' +STRING(mx0)	

;   testarea=im[*,0:99]
;print,'testarea:'
;maxmin,testarea  
;   IF m LE 0 or (median(testarea) GT 0.05 and cn EQ 1908) THEN BEGIN 	; value for 284A

   IF m0 LE 0 THEN BEGIN 	; value for 284A
	   	print,'Skipping file: Median out of range.'
	   	printf,l1,'Skipping file : Median out of range.',m0
		skiplast = skiplast + 1
		IF k+1 lt numim THEN nextim(ord(k+1))='ok'
		wait,2
	   	GOTO,skipped
   ENDIF 

;  ** Begin intensity normalization. **

    IF (normalize) THEN BEGIN	
       	squ1=DOUBLE(im0(x21:x22,y11:y12))	; lower right
       	squ2=DOUBLE(im0(x21:x22,y21:y22))	; upper right
   	ch1=where(squ1 LE 0 ,null1)
	disp_im(x12/df:x21/df, y11/df) = mx0
	disp_im(x12/df:x21/df, y12/df) = mx0
	disp_im(x21/df:x22/df,y21/df) = mx0
	disp_im(x21/df:x22/df,y22/df) = mx0
   	ch2=where(squ2 LE 0 ,null2)
	IF null1 GT 500 THEN BEGIN
		squ1=DOUBLE(im0(x11:x12,y11:y12))
		print,'Using Lower Left for squ1.'
	ENDIF
	IF null2 GT 500 THEN BEGIN
		squ2=DOUBLE(im0(x11:x12,y21:y22))
		print,'Using Upper Left for squ2.'
	ENDIF
	box_img=[squ1,squ2]
        good = WHERE(box_img GT 0, ng)
	IF ng LE 0 THEN goto, skipped
           ;IF (good(0) GE 0) THEN box_ref=TOTAL(box_img(good))/N_ELEMENTS(good) ELSE box=0
   	IF (k EQ 0 or carrmap_x LT 1) THEN BEGIN
		box_ref = median(box_img[good])
		box_ref2= median(im0(x12:x21,y11:y12))
	ENDIF
   	;IF (ch1(0) LT 0 OR ch2(0) LT 0) THEN BEGIN
	;	print,'Skipping file: box median out of range.'
	;	printf,l1,'Skipping file: box median out of range.'
	;	wait,2
	;	GOTO,skipped
	;ENDIF
   	int_corr=median(box_img(good))
	int_corr2= median(im0(x12:x21,y11:y12))
 	factor = box_ref/int_corr	;** box_ref 0.17 for c1?
	factor2= box_ref2/int_corr2
	factor = mref/m0
	help,factor,factor2
	im = factor*im0
;	print,'Using factor2.'
   	;IF int_corr GT 0 THEN im=factor*im ELSE BEGIN
	;	print,'Skipping file: box median out of range.'
	;	printf,l1,'Skipping file: box median out of range.'
	;	wait,2
	;	GOTO,skipped
   	;ENDELSE		; factor is about im = 0.12*im for c2, 0.15*im for c3?
	help,int_corr,box_ref,factor,factor2
	print,'BotRgt =',null1,'    TopRgt =',null2
	;if ABS(1-factor2) gt 0.15 THEN stop

	;im = eit_norm(im0,hdr.sector,factor)
	factors(k)=factor
    ENDIF ELSE BEGIN
    	message,'NOT NORMALIZING',/info
	wait,1
	im=im0
    ENDELSE
;  ** End intensity normalization. **
   
   ;im = im>0 + 0.001
   ;im = alog10(im)

;   IF cam NE 'c1' and noT(keyword_set(RUNNING_DIFF)) and k GT 0 THEN BEGIN
;help,m,median0,median1
;   	IF (m LE 1.1*median0 AND m GE 0.9*median0) THEN median0 = m ELSE $
;   	IF (m LE 1.1*median1 AND m GE 0.9*median1) THEN median1 = m $
;	ELSE BEGIN
;	   skip='y'
;;	   read,'Skip this one? [y/n]',skip
;	   IF skip EQ 'y' THEN BEGIN
;	   	print,'Skipping file: Median out of 10% range.'
;	   	printf,l1,'Skipping file : Median out of 10% range.',m
; 		skiplast = skiplast + 1
;		wait,2
;	   	GOTO,skipped
;	   ENDIF 
;   	ENDELSE
;   ENDIF

;  ** Begin carrwarp2  **
;
   oldmapnext=mapnext
   cmaprows=REBIN(cmap,mapsize,1)
   nonzero = where(cmaprows GT 0,n_nzero)

   IF n_nzero GT 0 THEN lastcol = max(nonzero) ELSE lastcol=0
   IF n_nzero LT mapsize THEN tlastcol = t_start+DOUBLE((lastcol+1)/pixperday) ELSE $
	tlastcol=t_end

   ;tlastcol = t_start + DOUBLE((cmapxleft-1.0+fullstripw)/pixperday)
; This is assuming t_start is at the left edge of column 0, and the time for 
; each column is at the middle of the column. halfdeltaday begins at the edge 
; of the last column (tlastcol). (lastcol+1) **NBR, 5/8/00

   dday = tn-t_start
   carrmap_x = FIX(dday*pixperday)
help,carrmap_x

   tn_now=t_start+ DOUBLE(carrmap_x+0.5)/pixperday	
						; time in middle of column of current image

   halfdeltaday = tn_now - tlastcol	; time between edge of last filled-in column 
					; and middle of current column

; ** Check next image for validity
   IF k LT numim-1 AND carrmap_x LT mapsize-1 THEN BEGIN
	incr=0
	getnext:
        IF k+incr LT numim-1 THEN REPEAT BEGIN
		incr = incr+1
		obs_timen1= s(ord(k+incr)).date_obs 
   		nextfile = s(ord(k+incr)).filename
   		dayn1= str2utc(obs_timen1)
   		tn1 = dayn1.mjd + DOUBLE(dayn1.time/86400000.)
   		deltanxt = tn1- tn_now		; time between midpoint of present column and next image
		fltmapnext = deltanxt*pixperday
		mapnext = ROUND(fltmapnext)
		help,nextfile
	ENDREP UNTIL mapnext GT 0 or k+incr GE numim-1		; 9/4/01, nbr

	IF k+incr GT numim-1  THEN BEGIN			; 9/4/01, nbr
   		tn1 = t_end
   		deltanxt = (tn1- tn_now	)	; time between present image/column and next image
		mapnext = mapsize-1-lastcol-1
		incr = incr+1
		print,'************* Last image of map. ***********'
		print
		help,mapnext
		GOTO, cont
	ENDIF

;
;	nextpath=s.diskpath(ord(k+incr))+'/'
;   	print, 'Reading next file ',nextpath+nextfile  
;	imt =lasco_readfits(nextpath+nextfile,hdrt)
;   	szimt = SIZE(imt)
;   	IF szimt(0) EQ 0 THEN BEGIN
;	   print,'File not found--trying next file...'
;	   GOTO, getnext
;   	ENDIF
;	;nz = where(imt GT 0)
;      	;IF nz(0) NE -1 THEN mt = median(imt(nz)) ELSE $
;	mt=median(imt)
;
;      	IF mt LE 0 THEN BEGIN
;	   	print,'Median of next file is zero; going to next...'
;	   	GOTO, getnext
;      	ENDIF 
;     	IF cam NE 'c1' and noT(keyword_set(RUNNING_DIFF)) and k GT 0 THEN BEGIN
;	  imgbias=offset_bias(hdrt,/sum)
;	  sumfac = (hdrt.sumcol*hdrt.sumcol) > (hdrt.lebxsum*hdrt.lebxsum)
;	  imt = (temporary(imt)-imgbias)/(hdrt.exptime*sumfac)
;	  mt = median(imt)

	mt = s(ord(k+incr)).datap75
	zeros = s(ord(k+incr)).datazer
	nxs1 = s(ord(k+incr)).naxis1
	nxs2 = s(ord(k+incr)).naxis2
;	help,mt,per, zeros
   	IF not(mt LE per*1.5 AND mt GE per*0.5) and mt GT 100 and (k+incr LT numim-1) THEN BEGIN
	   	print,'75percentile of next file is out of 50% range; going to next...'
		nextim(ord(k+incr)) = '75percentile of file is out of 50% range'
	   	GOTO, getnext

   	ENDIF
	IF zeros GT (0.25*nxs1*nxs2) THEN BEGIN
		print,'Will skip at k = ',trim(string(k+incr)),' due to GT 25% zeros.'
		nextim(ord(k+incr)) = 'Skipped due to GT 25% zeros.'
		GOTO, getnext
      	ENDIF
	IF tn1 GT 50176.656 and tn1 LT 50178.078 THEN BEGIN
		print,'Will skip at k = ',trim(string(k+incr)),' due to offpointing 96/04/03-05.'
		nextim(ord(k+incr)) = 'Skipped due to offpointing 96/04/03-05.'
		GOTO, getnext
      	ENDIF
	nextim(ord(k+incr))='ok'
	print,nextfile,' OK'

    ENDIF ELSE BEGIN
	tn1 = t_end
	deltanxt = (tn1- tn_now	)	; time between present image/column and next image
	mapnext = mapsize-1-carrmap_x
	incr = incr+1
	print,'End of map.'
	help,mapnext
    ENDELSE
; ** END check next image for validity **

   CONT:
   IF keyword_set(DEBUG) THEN help,fltmapnext,mapnext,nextfile,incr, deltanxt, mt,per,zeros

;if mapnext lt 4 then stop
   x1angle = halfdeltaday*degperday
   x2angle =     deltanxt*degperday/2.
   good1=1
   good2=1
   IF x1angle GE 90 THEN BEGIN
	x1angle=90
	good1=0
   ENDIF
   IF x2angle GE 90 THEN BEGIN
	x2angle=90
	good2=0
   ENDIF
   xoff1 = rconv * SIN(x1angle*!pi/180.)
   IF (mapdelta LE 1 and mapnext LE 1) THEN BEGIN
	stripwidth=1
	fullstripw=1
	
   ENDIF ELSE BEGIN
	leftstripw = halfdeltaday*pixperday
	rightstripw= deltanxt    *pixperday/2. 
   	stripwidth= leftstripw*good1+ rightstripw*good2 
   	fullstripw= leftstripw      + rightstripw
	help,leftstripw,rightstripw
   ENDELSE
   xoff2 = rconv * SIN(x2angle*!pi/180.)
   IF k EQ numim-1 THEN BEGIN
	x2angle = deltanxt*degperday
   	IF x2angle GT 90 THEN BEGIN
		x2angle=90
		good2=0
   	ENDIF
	xoff2 = rconv * SIN(x2angle*!pi/180.) 
	stripwidth=(deltanxt*good2+halfdeltaday*good1) * pixperday
	fullstripw=(deltanxt + halfdeltaday)*pixperday
   ENDIF 
   stripwidth = ROUND(stripwidth) > 1
   fullstripw = ROUND(fullstripw) > 1
   alpha = ((deltaday+deltanxt)/2.)*degperday*!pi/180.
IF keyword_set(DEBUG) THEN help,tn0,tn,tn1,tn_now,tlastcol,stripwidth,fullstripw,mapdelta,mapnext,halfdeltaday,deltanxt,xoff1,xoff2,cx,cy
help,x1angle,x2angle
print,'degrees'
;IF k+incr GE numim-1 THEN stop

; ** End carrwarp2 	**
;

   IF keyword_set(RUNNING_DIFF) and carrmap_x LE 2 then toskip='y'

;   IF disp EQ 1 THEN BEGIN
;	IF szim(1) GT 600 THEN BEGIN
;	   disp_im = rebin(im,xs,ys)
; 	ENDIF ELSE disp_im = im
;	;IF cam EQ 'c1' or cam EQ 'c3' THEN BEGIN
;		disp_im(x11/df:x12/df, y21/df) = mx
;		disp_im(x11/df:x12/df, y22/df) = mx
;		disp_im(x21/df:x22/df,y21/df) = mx
;		disp_im(x21/df:x22/df,y22/df) = mx
;	;ENDIF
;   ENDIF

   r0=rconv
   ;arc0 = alpha*r0
   pstrip = FLTARR(stripwidth,szim(2))
   FOR i=0,szim(2)-1 DO BEGIN
	y = ABS(i - cy0)
	IF y GT r0 THEN y=0
	r = SQRT(r0*r0 - y*y)
	;arc = alpha*r
	f = r/r0
	
	IF stripwidth EQ 1 THEN pstrip(*,i) = REPLICATE(im(cx0,i),stripwidth) ELSE $
        pstrip(*,i)= CONGRID(im(ROUND(cx-xoff2*f):ROUND(cx+xoff1*f),i),stripwidth,/interp)
;if k gt 35 then stop
	   IF disp EQ 1 THEN BEGIN
		xdisp1=ROUND((cx0-xoff2*f))
		xdisp2=ROUND((cx0+xoff1*f))
	   	disp_im(xdisp1,i)=255
	   	disp_im(xdisp2,i)=255
	   ENDIF
   ENDFOR
   pstrip = CONGRID(pstrip,stripwidth,ht,/interp)
   IF xoff1 EQ rconv THEN BEGIN		; rotation > 90 deg, display whole hemisphere
	stripw1=fullstripw - stripwidth
	strip = FLTARR(fullstripw,ht)
	days= 90./degperday
	npix = days*pixperday
	halfw = ROUND((szim(1) - cx0)*npix/rconv)
	strip1 = CONGRID(im(cx0+1:szim(1)-1,*),halfw,ht,/interp)
	IF stripw1 LT halfw THEN strip1=strip1(0:stripw1,*)
	strip(0,0)=pstrip
	strip(stripwidth-1,0)=strip1
   ENDIF ELSE IF xoff2 EQ rconv THEN BEGIN
	stripw1=fullstripw - stripwidth
	strip = FLTARR(fullstripw,ht)
	days= 90./degperday
	npix = days*pixperday
	halfw = ROUND((cx0)*npix/rconv)
	strip1 = CONGRID(im(0:cx0-1,*),halfw,ht,/interp)
	IF stripw1 LT halfw THEN BEGIN
		strip1=strip1(halfw-stripw1:halfw-1,*)
		xloc = 0
	ENDIF ELSE xloc=stripw1-halfw
	strip(stripw1,0)=pstrip
	strip(xloc,0)=strip1
   ENDIF ELSE strip=pstrip	

;  ** End take strip. **
;

;IF k LT 2 THEN stop
szs = SIZE(strip)

   IF disp EQ 1 THEN BEGIN
	wset,dispwin
   	IF pol EQ 'p' OR pol EQ 'u' THEN scale = 80
   	tv,disp_im
   ENDIF
   ;IF k LT 54 and k GT 44 THEN stop

;******************************************************************

;; ** BEgin  check strip **
   stripsz = size(strip)
   stripends=FLTARR(stripsz(1),22)
   stripends(*,0:10)=strip(*,0:10)
   stripends(*,11:21)=strip(*,ht-11:ht-1)
   newmedend = median(stripends)
   IF median(stripends(*,0:10)) LE 0 THEN newmedend = median(stripends(*,11:21))
   IF median(stripends(*,11:21)) LE 0 THEN newmedend = median(stripends(*,0:10))
	; added 981116, NBR
   newmed = median(strip)
   oldmed = median(stripn0)
w=where(oldmedends)
   ww =where(oldstripmeds)
   IF w(0) GE 0 THEN  BEGIN
   	y = oldmedends(w(0):19)
	sy = size(y)
   	wsz = size(w)
   	IF wsz(1) EQ 1 THEN est_val = y(0) ELSE BEGIN
   	   ;x = lindgen(wsz(1))
   	   ;fun = poly_fit(x,y,1,yfit,yband,sigma)
	   ;est_val = fun(0) + fun(1)*wsz(1)
	   est_val = total(y)/sy(1)
   	ENDELSE
   ENDIF
   IF ww(0) LT 0 THEN str_est_val = newmed ELSE BEGIN
   	yy = oldstripmeds(ww(0):9)
	syy= size(yy)
   	wwsz = size(ww)
   	IF wwsz(1) EQ 1 THEN str_est_val = yy(0) ELSE BEGIN
   	   ;xx = lindgen(wwsz(1))
   	   ;fun2 = poly_fit(xx,yy,1,yyfit,yyband,ssigma)
	   ;str_est_val = fun2(0) + fun2(1)*wwsz(1)
	   str_est_val = total(yy)/syy(1)
   	ENDELSE
   ENDELSE
   print,'est_val =',est_val,',   str_est_val =',str_est_val
   print,'ends =',newmedend,'   strip median =',newmed

IF checkstrip THEN BEGIN
   	IF newmedend LT 0 OR newmed LT 0 THEN BEGIN
		print,'Strip or end is negative; skipping image.'
		wait,2
		printf,l1,'Strip or end is negative; skipping image.'
		skiplast = skiplast + 1
		GOTO, skipped
	ENDIF
   ;wait,2
   ;IF carrmap_x EQ 160 THEN stop
   strip_per = ABS(newmed - str_est_val)/str_est_val
   IF ( strip_per GT deltend) and not(keyword_set(RUNNING_DIFF)) THEN toskip = 'y'
 			; changed from 3*str_est_val, 6/8/98
   
   end_per = ABS(newmedend - est_val)/est_val
;   IF ( end_per GT (deltend+0.004)) AND cn LT 1934 THEN toskip = 'y'
   IF ( end_per GT 1.5) THEN toskip = 'y'
			; changed from 3*est_val, 6/8/98
   IF toskip EQ 'y' THEN Begin
;   IF toskip EQ 'skip this part' THEN Begin
   	;IF skiplast GT 5 AND mapdelta GT 5 THEN BEGIN
	;	toskip = 'n'
	;	print,string(7b)
	;	print,'Skiplast GT 5 AND mapdelta GT 5; not skipping'
	;ENDIF
	IF (strip_per LT .4 or end_per LT .4) and not(keyword_set(RUNNING_DIFF))  or (k LT 5) THEN BEGIN
		print,string(7b)
		read,' Skip this one? (Default is no) ',toskip
		IF toskip EQ '' THEN toskip = 'n'
	ENDIF

	IF toskip NE 'n' THEN BEGIN
		skiplast = skiplast+1
		print,'Strip is too different; skipping image.'
		wait,2
		printf,l1,'Strip is too different; skipping image.'
		GOTO, skipped
	ENDIF
   ENDIF
   skiplast = 0

;  ** END check strip **
ENDIF
   IF carrmap_x GT mapsize-1 THEN carrmap_x = mapsize -1
   IF mapdelta GT 1 THEN printf,l2,'carrmap_x =',carrmap_x,'    mapdelta =',mapdelta

wait,2
   ;IF k EQ 0  THEN stripn0 = strip
;	** Begin carrwarp 3. **

	;IF mapdelta GT 1 THEN BEGIN 
	;   tempstrip(1,*) = strip(*)
	;   tempstrip(0,*) = stripn0(*)
	;   IF mapdelta GT (mapsize/360)*20 or total(stripn0(*)) LE 0 THEN BEGIN
	;	current = cmap(*,*)
	;	no_zero = WHERE (current GT 0)
	;	IF no_zero(0) LT 0 THEN no_zero = WHERE (strip GT 0)
	;	fill = median(current(no_zero))
	;	tempstrip(*,*) = fill		; ** Set 8/21/98 **
	;	print,'Fill =',fill
	;	printf,l1,'Fill =',fill
	;   ENDIF
	;   tempstrip(*,ht-barht:ht-1) = max(cmap)>max(strip)
	;   dummy = CONGRID(tempstrip,mapdelta*2,ht,/interp)
	;   newstrip = dummy(0:mapdelta,*)
	;   IF k GT 0 and NOT(first) THEN newstrip(0,*) = stripn0(*)
	;   newstrip(mapdelta,*) = strip(*)
	;   cmap(carrmap_x-mapdelta:carrmap_x,*) = newstrip(*,*)

   printf,l1,filename $
	+ STRING(carrmap_x) $
	+ STRING(mapdelta) $
;	+'  median(im)='+TRIM(STRING(m)) $
	+ string(factor) $
	+ STRING(mx0) $
	+ STRING(szs(1)) $
	+ STRING(newmed)
   printf,l1,shiftmsg
		;if carrmap_x GE 277 then stop
	;IF k EQ 0 or mapdelta GT oldmapnext THEN cmapxleft = carrmap_x-mapdelta $
	;	ELSE cmapxleft = carrmap_x-( ROUND(halfdeltaday*pixperday)>1 )
	cmapxleft=lastcol	
	dstrip = REVERSE(strip)
;	cdiff = ROUND((cy - 511)*float(ht)/szim(2))		; ** center sun in map ;vertically
IF keyword_set(DEBUG) THEN help,cdiff,cmapxleft,carrmap_x
;	IF cdiff GE 0 THEN dstrip = dstrip(*,cdiff:ht-1) ELSE BEGIN
;		tstrip = dstrip
;		dstrip = FLTARR(fullstripw,ht)
;		dstrip(0,ABS(cdiff)) = tstrip(*,0:ht-1+cdiff)
;	ENDELSE
		

	stripn0 = dstrip
	
	dstrip = dstrip>(-1)+1.
	dstrip = alog10(dstrip)
        IF first THEN fill=max(dstrip)

	IF mapdelta GT 1 THEN BEGIN
            ; add gap info - nbr, 05.08.10
            ctrcol=dstrip[carrmap_x-cmapxleft-1,*]
            dstrip[*,ht-3:ht-1]=fill
            dstrip[carrmap_x-cmapxleft-1,*]=ctrcol
        ENDIF 
	IF k EQ 0 THEN BEGIN
            cmap(0,0) = dstrip 
        ENDIF ELSE BEGIN
            cmap(cmapxleft+1,0) = dstrip 
        ENDELSE
	;ENDIF ELSE cmap(carrmap_x,*) = strip(*) 
	
	;if mapdelta gt 5 then stop
	 
;
;	** End carrwarp 3.   **
	wset,0

	tv, BYTSCL(cmap,mapmin,mapmax)
	help,mapmax,mapmin

;wset,1
;IF k EQ 0 THEN plot, cmap[*,298], yrang=[0,.5] ELSE oplot, cmap[*,298]
help,k
wait,1

   ;if mapnext gt 100 then stop
   ;carrmap_x = carrmap_x + mapdelta		
   ctr = ctr+1

   IF mapdelta GT (mapsize/360)*20 THEN oldmedends=fltarr(20)
   oldmedends = shift(oldmedends,-1)
   IF carrmap_x GT 2 THEN oldmedends(19) = newmedend
   IF mapdelta GT (mapsize/360)*20 THEN oldstripmeds=fltarr(10)
   oldstripmeds = shift(oldstripmeds,-1)
   oldstripmeds(9) = newmed
   skiplast = 0

   obs_timen0 = obs_timen
   first = 0
   
   skipped:
   ;stop
ENDFOR     ;k loop
end_radius = radius
end_center(0) = cx
end_center(1) = cy
IF rotangle GT 2. THEN rollcor(1)=rotangle
dhdr = hdr
   
;mnm=lasco_readfits('/net/corona/cplex3/monthly/c2min.fts',hm)

;diffcarr,cmap,time1_str,cam,rad,limb

close,l1
close,l2
free_lun,l1
free_lun,l2
;stop
cmap = reverse(cmap)
print
print,'time0 = ',time0_str,'    time1 = ',time1_str
print
print,'Found ',numim,' ',cam,' images for CR ',cn, '   ',wlimb
print

IF keyword_set(MVI) THEN WRUNMOVIE, win_index, NAMES=labels, HDRS=all_hdr


END ; meridianmap
