pro drawclock,index,data,outdata $
              ,pos=position,dig=dig,win=win,nolong=nolong,second=second $
              ,year=year,black=black,shadow=shad

;+
; NAME    : drawclock
;
; PURPOSE : to draw analogue (and digital) clocks on images
;
; USE     : IDL> drawclock,index,data,outdata
;           IDL> drawclock,index,data,outdata,pos=2,/dig
;           IDL> drawclock,index,data,outdata,/dig,/sh
;           IDL> drawclock,index,data,outdata,/dig,/win
;           IDL> drawclock,index,data,outdata,/dig,/no
;           IDL> drawclock,index,data,outdata,/y
;           IDL> drawclock,index,data,outdata,/sec,/dig
;
; INPUTS  : index = structure array including "date_obs"
;           data  = 2 or 3 dimension array
;
; OUTPUTS : outdata = same array as data
;
; OPTION  : position = (default) 1 : drawing clocks on the bottom-left corner
;                                2 : drawing clocks on the bottom-right corner
;                                3 : drawing clocks on the top-left corner
;                                4 : drawing clocks on the top-left corner
;                                5 : drawing clocks on the bottom center
;                                6 : drawing clocks on the top center
;
;                                      ---------------------
;                                     |(3)      (6)      (4)|
;                                     |                     |
;                                     |                     |
;                                     |                     |
;                                     |(1)      (5)      (2)|
;                                      ---------------------
;
;           /dig     = adding digital clocks
;           /sh      = drawing shadow instead of dark background
;           /win     = if you use IDL for Windows
;           /no      = long hand is not drawn (for low-cadence observation)
;           /y       = for very low-cadence and long-term observation
;           /sec     = sweep hand is drawn for very high cadence observation
;
; VERSION : 4.1  Written, 2006 Dec 26
; VERSION : 4.3  Written, 2007 Apr 14
; VERSION : 4.4  Written, 2008 Mar 13
; VERSION : 4.7  Written, 2009 Mar 17
; VERSION : 5.0  Written, 2009 May 29
; VERSION : 5.3  Written, 2009 Jul 15
; VERSION : 6.0  Written, 2011 Feb 21
; VERSION : 7.0  Written, 2011 Dec 02
; VERSION : 7.1  Written, 2011 Dec 29
;
; CREDIT  : Takenori Joten OKAMOTO (joten.okamoto@nao.ac.jp)
;-


if keyword_set(year) eq 0 then begin

; -------------Normal clock -----------------------------------------------

if keyword_set(position) eq 0 then position=1

tmp1=indgen(101,101)*0
tmp2=tmp1
tmp_l=tmp1 ; long hand
tmp_s=tmp1 ; short hand
tmp_swp=tmp1 ; sweep hand

tmp1[47:53,2:4]=1
tmp1[48:52,5:7]=1
tmp1[49:51,8:9]=1
tmp1[50,10]=1

tmp2[49:51,2:4]=1

base=tmp1+rot(tmp1,90,1,50,50)+rot(tmp1,180,1,50,50)+rot(tmp1,270,1,50,50)
for j=0,3 do for i=1,2 do base=base+rot(tmp2,30*i+90*j,1,50,50)

for ii=0,16 do tmp_l[50-(ii+3)/5:50+(ii+3)/5,50+ii]=1
for ii=0,22 do tmp_l[48+ii/8:52-ii/8,67+ii]=1
for ii=0,13 do tmp_s[50-(ii+2)/4:50+(ii+2)/4,50+ii]=1
for ii=0,11 do tmp_s[48+ii/4:52-ii/4,64+ii]=1
for ii=0,11 do tmp_swp[50,50:89]=1


outdata=byte(data)
set_plot,'z'
device,set_resolution=[n_elements(data[*,0,0]),n_elements(data[0,*,0])]

for tt=0,n_elements(index)-1 do begin

    tmp_data=data[*,*,tt]-min(data[*,*,tt])
    maxint=max(tmp_data)
    time=index[tt].date_obs
    sec=anytim(time)-fix(anytim(time)/86400)*86400
    if keyword_set(nolong) then $
      base2=(base+rot(tmp_s,sec/120.0,1,50,50))<1 $
      else base2=(base+rot(tmp_l,sec/10,1,50,50)+rot(tmp_s,sec/120.0,1,50,50))<1
    if keyword_set(second) then $
       base2=(base2+rot(tmp_swp,6.*sec,1,50,50))<1

    if keyword_set(shad) then base2=(base2*2-shift_img(base2,[1,-1]))<1>(-1)

    nx=n_elements(tmp_data[*,0])
    ny=n_elements(tmp_data[0,*])

    xloc=([0, 0, nx-101,      0, nx-101, nx/2-50, nx/2-50, 0])[position>0<7]
    yloc=([0, 0,      0, ny-131, ny-131,       0,  ny-131, 0])[position>0<7]

	tmp_data[xloc:xloc+100,yloc:yloc+100]= $
      (tmp_data[xloc:xloc+100,yloc:yloc+100]*(0.8+0.2*keyword_set(shad))* $
      (1-keyword_set(black))+base2*maxint)<maxint>0
    if keyword_set(dig) then begin
      if not keyword_set(shad) then tmp_data[xloc:xloc+100,yloc+100:yloc+130]=0
      tvscl,tmp_data
      if keyword_set(shad) then xyouts,xloc+4,yloc+117,charsize=1.0, $
        strmid(anytim(time,/vms),0,11),/dev,color=0
      if keyword_set(shad) then xyouts,xloc+19,yloc+102,charsize=1.0, $
        strmid(anytim(time,/vms),12,8)+' UT',/dev,color=0
      xyouts,xloc+3,yloc+118,charsize=1.0, $
        strmid(anytim(time,/vms),0,11),/dev
      xyouts,xloc+18,yloc+103,charsize=1.0, $
        strmid(anytim(time,/vms),12,8)+' UT',/dev
    endif else tvscl,tmp_data

    outdata[*,*,tt]=tvrd(0,0,nx,ny)
endfor

endif else begin

; ------------- year -----------------------------------------------------

base1=indgen(101,101)*0
tmp=base1
line=base1

tmp[49:51,2:4]=1
line[50,50:90]=1

for ii=0,11 do base1=base1+rot(tmp,30*ii,1,50,50)

outdata=byte(data)
set_plot,'z'
device,set_resolution=[n_elements(data[*,0,0]),n_elements(data[0,*,0])]

for tt=0,n_elements(index)-1 do begin

    base2=base1
    tmp_data=data[*,*,tt]-min(data[*,*,tt])
    maxint=max(tmp_data)
    time=index[tt].date_obs
    yr1=(anytim(time)-anytim('1990-01-01T00:00:00.000'))/3.15360e+07
    yr2=(yr1-fix(yr1))*360

    if(fix(yr1) mod 2 eq 0) then $
      for ii=0,yr2,1 do base2=(base2+line+rot(line,ii,1,50,50))<1 $
    else $
      for ii=yr2,360,1 do base2=(base2+line+rot(line,ii,1,50,50))<1

    if keyword_set(shad) then base2=(base2*2-shift_img(base2,[1,-1]))<1>(-1)

    nx=n_elements(tmp_data[*,0])
    ny=n_elements(tmp_data[0,*])

    tmp_data[0:100,0:100]=(tmp_data[0:100,0:100]* $
                           (0.8+0.2*keyword_set(shad))>0 $
                           +base2*maxint*1.0)<maxint>0
    if not keyword_set(shad) then tmp_data[0:100,100:130]=0
    tvscl,tmp_data
    if keyword_set(shad) then xyouts,4,117,charsize=1.0, $
      strmid(anytim(time,/vms),0,11),/dev,color=0
    if keyword_set(shad) then xyouts,19,102,charsize=1.0, $
      strmid(anytim(time,/vms),12,8)+' UT',/dev,color=0
    xyouts,3,118,charsize=1.0, $
      strmid(anytim(time,/vms),0,11),/dev
    xyouts,18,103,charsize=1.0, $
      strmid(anytim(time,/vms),12,8)+' UT',/dev
  
    outdata[*,*,tt]=tvrd(0,0,nx,ny)
endfor

endelse

if(n_elements(win) eq 1) then set_plot,'win' else set_plot,'x'

end
