pro mk_smthy_jpgz,genxlist,gfil=gfil,cadence=cadence,outdir=outdir

;NAME
;	mk_smthy_jpgz
;PURPOSE
;	Create even-cadence jpeg images for smoothy movie.
;CALLING SEQUENCE
;	mk_smthy_jpgz,genxlist,gfil=gfil,cadence=cadence,outdir=outdir
;INPUTS
;	genxlist, list of .genx files with the names of selected
;	   level-3 images.  E.g., 'fil_list/ok_fts_1991_10.genx'
;	   contains the filenames for Oct-1991.  genxlist can be
;	   an array of such filenames.  The program goes through
;	   these one at a time.
;KEYWORD INPUT
;	cadence, desired time spacing of smoothy images in minutes.
;	outdir, place to write the jpeg files.  
;	   E.g., '/yla/smoothy/smthy_jpg/1992/'
;OPTIONAL KEYWORD INPUT
;	gfil, array of level-3 file names.
;	   If gfil is used set genxlist as 'junk'.
;OUTPUT
;	interpolated jpegs with names in the following form,
;	   'pyyyyMMdd_hhmm.jpg' = 'p19911001_1156.jpg'
;HISTORY
;	16-May-2016 LWA  Built from Aki's code.
;	 2-May-2017 LWA  Reset it=4, corrected output file name.
;	 5-May-2017 LWA  Corrected "CH cloud" bug, eliminate data3=data.
;			 Shrunk sphe to ameliorate limb imperfections.
;	 7-Jul-2017 LWA  Removed obsolete color table reference.
;			 Removed YLA/Kyoto U.
;**********************************************************************
; NAME: SXT_SMOOTH_ROT98_NOUSM.PRO  (for solar-min, no unsharp masking)
;            final version from Jun Okumura (yohkoh3d.txt), Apr-2009 
;               plus modification by Aki Takeda, 7-July-2010.
;               plus modification by Aki Takeda, 19-Oct-2011.
;                  (SXTMOV33, PNG --> JPG)
; 
; INPUT:   co-aligned FITS images in FITS_DIR.
; OUTPUT:  PNG files in OUTDIR.
;**********************************************************************
;-

if not_exist(genxlist) or not_exist(cadence) or $
   not_exist(outdir) then begin
      print,' Must have all inputs.'
      print,'   mk_smthy_jpgz,genxlist,cadence=cadence,outdir=outdir'
      return
endif

stt=anytim(!stime)
;--- output directory ---
outdir=outdir

;--- image size ---
xs = 512
ys = xs

;--- image center ---
xc = xs*.5
yc = fix(280.5) ; center of sxtmov images
; yc = ys*.5

;--- create a superposed image orbit (98 minutes) ---
dt = long(cadence*60)

;--- (+/-)4 images are used to create 1 superposed image  ---
it = 4

;--- counter for output file name ---
count = long(0)

;--- color table ---
set_plot,'z'
erase
loadct,42; ,file='/mithra_home/takeda/idlpro/y_legacy/color_tab/colors1.tbl'
tvlct,rrr,ggg,bbb,/get
set_plot,'x'

;--- loop thrugh the .genx file lists ---
ngl=n_elements(genxlist)
for h=0,n_elements(genxlist)-1 do begin
   ngh=h
   stti=anytim(!stime)
   if keyword_set(gfil) then files0=gfil else $
      restgen,file=genxlist(h),files0
   nf=n_elements(files0)

;--- get time info from index ---
   secarr=lonarr(nf)

   for i=0,nf-1 do begin
      if i mod 100 eq 0 then print,'reading FITS files...', i,' out of',nf
      mreadfits,files0(i),index0,/nodata,/quiet
      secarr(i)=anytim(index0,/utime)
   endfor

;--- time of first and last image ---
   t0=long(secarr(0))/dt*dt
   t1=long(secarr(nf-1))/dt*dt
   tnarr=timegrid(t0,t1,minutes=cadence,/utime)

;--- number of superposed images to be created ---
   dn=(t1-t0)/dt+1
   if dn gt n_elements(tnarr) then dn=dn-1  ;LWA 5/17/2016.

;--- selection of 8(=it*2) images for a superposed image ---
   ii=lonarr(dn)       ; index of the first image AFTER the reference time.
   dtm=fltarr(dn,it)   ; time difference to the reference time for the
   dtp=dtm             ;   preceding(dtm) and following(dtp) images.
   for j=0,dn-1 do begin
      ss=where(secarr gt tnarr(j))
      ii(j) = ss(0) 
      for iit=0,it-1 do begin
         dtm(j,iit) = secarr((ss(0)-1-iit)>0)-tnarr(j)
         dtp(j,iit) = secarr((ss(0)+iit)<(nf-1))-tnarr(j)
      endfor
   endfor

;--- preparation of the preceding/following images ---
   limg = fltarr(xs,ys,it)  ; array for preceding images (disk part).
   rimg = fltarr(xs,ys,it)  ;           following images (disk part).
   lw = fltarr(xs,ys,it)    ; weight of preceding images used when summed up.
   rw = fltarr(xs,ys,it)    ;           following images.    
   limg2 = fltarr(xs,ys,it) ; array for preceding images (corona).
   rimg2 = fltarr(xs,ys,it) ;           following images (corona).
   lw2 = fltarr(xs,ys,it)   ; weight of preceding images for corona.
   rw2 = fltarr(xs,ys,it)   ;           following images.

   for i=0,dn-1 do begin       ; loop for each supurposed image.
      if i mod 25 eq 0 then print,i,dn

;--- preparation for making weight function ---
      ddd = (total( abs(dtp(i,*))+abs(dtm(i,*)) )/float(it))
      dm = fltarr(it)
      dp = dm

      al=fltarr(xs,ys,it)
      ar=fltarr(xs,ys,it)

;--- parepare disk pix and image(disk=1 & else=0) at reference time ---
      sphe=intarr(xs,ys)
      work=pb0r(anytim(tnarr(i),/ecs),/arcsec)
      srad=work(2)/index0.cdelt1 ; solar radius (pix)
      xx=rebin(indgen(xs),xs,ys)
      yy=transpose(xx)
      rad=sqrt((xx-xc)^2d + (yy-yc)^2d)
      disk0=where(rad lt srad-6.)   ;The -6 determined by test. 5/5/17
      sphe(disk0)=1

      for iit=0,it-1 do begin   ; iit-loop for disk.
;-------- preceding iamges ----------
         mreadfits,files0((ii(i)-1-iit)>0),index,data,/quiet
;   al(*,*,iit) = UNSHARP_MASK(data)
         al(*,*,iit) = data
         index2map,index,data,map
         map2 = drot_map(map, -dtm(i,iit)/3600., missing=0.0, $
                        /same_center,olimb=olimbpix)
         data2=map2.data
;         data3=data
;         disk=where(data2 gt 0.)
;         data3=data
;         data3(disk)=data2(disk)
;   limg(*,*,iit) = UNSHARP_MASK(data3)*sphe
         limg(*,*,iit) = data2*sphe      ;5/5/17

;-------- following iamges ----------
         mreadfits,files0((ii(i)+iit)<(nf-1)),index,data,/quiet
;   ar(*,*,iit) = UNSHARP_MASK(data)
         ar(*,*,iit) = data
         index2map,index,data,map
         map2 = drot_map(map, -dtp(i,iit)/3600., missing=0.0, $
                   /same_center,olimb=olimbpix)
         data2=map2.data
;         data3=data
;         disk=where(data2 gt 0.)
;         data3=data
;         data3(disk)=data2(disk)
;   rimg(*,*,iit) = UNSHARP_MASK(data3)*sphe
         rimg(*,*,iit) = data2*sphe      ;5/5/17

;--- definition of weight ----
         dm(iit) = 10^(-( (abs(dtm(i,iit))/ddd)<1. )^2)
         dp(iit) = 10^(-( (abs(dtp(i,iit))/ddd)<1. )^2)
         lw(*,*,iit) = dm(iit)*sphe
         rw(*,*,iit) = dp(iit)*sphe
      endfor   ; end of iit-loop  (for disk)

     sfm=replicate(27.5*24*60*60.,ys)   ; rigid rotation
     sfm=360./sfm*!dtor

   for iit=0,it-1 do begin   ; loop for off-limb corona.
      for y=0,ys-1 do begin
         wm = sfm(y)*dtm(i,iit)
         wp = sfm(y)*dtp(i,iit)
         ddm = abs( fix(cos(wm)*xs*.5) )>1
         ddp = abs( fix(cos(wp)*xs*.5) )>1
         limg2(xc-ddm:xc+ddm-1,y,iit) = congrid( al(*,y,iit), ddm*2 ,cubic=-.5)
         rimg2(xc-ddp:xc+ddp-1,y,iit) = congrid( ar(*,y,iit), ddp*2 ,cubic=-.5)
         lw2(xc-ddm:xc+ddm-1,y,iit)   = dm(iit)*congrid( (1.-sphe(*,y)), ddm*2 )
         rw2(xc-ddp:xc+ddp-1,y,iit)   = dp(iit)*congrid( (1.-sphe(*,y)), ddp*2 )
         lw2(xc:*,y,iit)              = lw2(xc:*,y,iit)*(1.-sphe(xc:*,y))
         rw2(0:xc-1,y,iit)            = rw2(0:xc-1,y,iit)*(1.-sphe(0:xc-1,y))
         if sin(-2.*sfm(y)*dtm(i,iit)) lt 0. then begin
            lw2(xc-ddm:xc+ddm-1,y,iit)   = dm(iit)*congrid( (1.-sphe(*,y)), ddm*2 )
            lw2(0:xc-1,y,iit)            = lw2(0:xc-1,y,iit)*(1.-sphe(0:xc-1,y))
         endif
;-----  handling of over-rotating cases (more than a half rot.) ---
         if sin(2.*sfm(y)*dtp(i,iit)) lt 0. then begin
            rw2(xc-ddp:xc+ddp-1,y,iit)   = dp(iit)*congrid( (1.-sphe(*,y)), ddp*2 )
            rw2(xc:*,y,iit)              = rw2(xc:*,y,iit)*(1.-sphe(xc:*,y))
         endif
         if cos(sfm(y)*dtm(i,iit)) lt 0. then begin
            wm = sfm(y)*dtm(i,iit)
            ddm = fix(abs(cos(wm)*xs*.5))>1
            limg2(xc-ddm:xc+ddm-1,y,iit) = $
               interpolate( limg2(xc-ddm:xc+ddm-1,y,iit), 2*ddm-1-indgen(2*ddm) ,cubic=-.5 )
         endif
         if cos(sfm(y)*dtp(i,iit)) lt 0. then begin
            wp = sfm(y)*dtp(i,iit)
            ddp = fix(abs(cos(wp)*xs*.5))>1
            rimg2(xc-ddp:xc+ddp-1,y,iit) = $
               interpolate( rimg2(xc-ddp:xc+ddp-1,y,iit), 2*ddp-1-indgen(2*ddp) ,cubic=-.5 )
         endif
      endfor
      limg2(*,*,iit)=limg2(*,*,iit)*(1-sphe)
      rimg2(*,*,iit)=rimg2(*,*,iit)*(1-sphe)
   
   endfor    ; end of iit-loop  (for off-limb corona)

   lw2(where(limg2 lt 1.)) = limg2(where(limg2 lt 1.))
   rw2(where(rimg2 lt 1.)) = rimg2(where(rimg2 lt 1.))
   lw = lw>(10.^(-10))
   rw = rw>(10.^(-10))
   lw2= lw2>(10.^(-5))
   rw2= rw2>(10.^(-5))

;--- final superposed image ---
   nur0 = limg * lw + rimg * rw + limg2 * lw2 + rimg2 * rw2
   nur  = total(nur0,3)
   den0 = lw + rw + lw2 + rw2
   den  = total(den0,3)

   img = (nur/den)>0.<255.

   count = count + long(1)
   count = long(count)

;--- output image through Z-buffer to final form (PNG) ---
   set_plot,'z'
   device,set_resolution=[512,512],z_buffer=0
   erase

   tvscl,img

   cal=anytim(tnarr(i),/ecs)
   cal1=strmid(cal,0,16)
   cal2=strmid(cal,0,4)+strmid(cal,5,2)+strmid(cal,8,2)+'_'+ $
      strmid(cal,11,2)+strmid(cal,14,2) 

   xyouts,10,10,/dev,'!3'+cal1+'!X',charsize=1.6,charthick=1.0
   xyouts,11,10,/dev,'!3'+cal1+'!X',charsize=1.6,charthick=1.0
   ;xyouts,390,10,/dev,'!18YLA/Kyoto U.!X',charsize=1.1
   ;xyouts,391,10,/dev,'!18YLA/Kyoto U.!X',charsize=1.1

   img2 = tvrd()

   device,z_buffer=1 & set_plot,'x'

   image24=bytarr(3, 512, 512)
   image24[0,*,*] = rrr[img2]
   image24[1,*,*] = ggg[img2]
   image24[2,*,*] = bbb[img2]
  
   write_jpeg,outdir+'p'+cal2+'.jpg', image24, True=1, Quality=90 
;   write_jpeg,outdir+'p'+cal2+'_'+string(count,format='(i6.6)')+'.jpg', $
;      image24, True=1, Quality=90 

   endfor  ; end of i-loop.
;   etti=anytim(!stime)
;   dtti=(etti-stti)/60.
;   print,' It took '+strtrim(dtti,2)+' minutes to do '+genxlist(h)+'.'

goto,skipit
; Patch the gap between the months
   print,' Gap check ',ngl,ngh
   if ngl gt 1 and ngh+1 lt ngl then begin
      restgen,file=genxlist(h),gimg0
      restgen,file=genxlist(h+1),gimg1
      gimg=[gimg0(nf-10:nf-1),gimg1(0:9)]
      mk_smthy_jpgz,'junk',gfil=gimg,cadence=cadence,outdir=outdir
   endif
skipit:

endfor   ; end of h-loop

ett=anytim(!stime)
dtt=(ett-stt)/60.
print,' It took '+strtrim(dtt,2)+' minutes to make the jpegs.'
end
