pro norh_3min_db_grid_1day, arg0, interval=interval, $
                  sy_st_tim = sy_st_tim, sy_ed_tim = sy_ed_tim,$
                  no_trans = no_trans, scr_dir = scr_dir,usetsx = usetsx, $
                  host = host, prog = prog, $
                  nodisp = nodisp, det_test = det_test, $
                  debug_prom = debug_prom ,$
                  send_mail = send_mail, mail_addr = mail_addr
               
spawn,'date'

nodisp = keyword_set(nodisp)
debug_prom = keyword_set(debug_prom)
send_mail = keyword_set(send_mail)

;; directory setting
;;;;;;;;;;;;;;;;;;;;;;;;

data_dir = '/nfs/nobeyama_solar/3min_arc/3min_fits_db/fits_db/'
scr_dir = '/nfs/scr01/3min_heliog'
min3_dirL = '/nfs/nobeyama_solar/3min_arc/3min_fits_db/3min/'
min3_dirS = '/nfs/nobeyama_solar/3min_arc/3min_fits_db/3mins/'

;;;;;;;;;;;;;;;;;;;;;;;;
;; Main Loop
;;;;;;;;;;;;;;;;;;;;;;;;

cd, current=prog_dir


chk_day=gt_day(anytim(arg0,/ints), /str, /leadzero)
if not nodisp then print, 'Starting process:'+chk_day
  
;;Make an FITS directory
;;;;;;;;;;;;;;;;;;;;;;;;

date_arr = anytim(chk_day, /ex)
year = strtrim(date_arr(6),2)

if date_arr(5) lt 10 then begin
   month = '0'+strtrim(date_arr(5),2)
endif else begin
   month = strtrim(date_arr(5),2)
endelse

if date_arr(4) lt 10 then begin
   day = '0'+strtrim(date_arr(4),2)
endif else begin
   day = strtrim(date_arr(4),2)
endelse

img_year_dir = data_dir+year
if not dir_exist(img_year_dir) then spawn,'mkdir -p '+img_year_dir

img_mon_dir = img_year_dir+'/'+month
if not dir_exist(img_mon_dir) then spawn,'mkdir -p '+img_mon_dir

img_day_dir = img_mon_dir+'/'+day
if not dir_exist(img_day_dir) then spawn,'mkdir -p '+img_day_dir


;;image synthesize
;;;;;;;;;;;;;;;;;;;;;;;;

norh_prom_mkimg_grid, chk_day, $
                      scr_dir = scr_dir, out_dir = img_day_dir, /nodisp

;;Make an PNG directory
;;;;;;;;;;;;;;;;;;;;;;;;

L3_year_dir = min3_dirL+year
if not dir_exist(L3_year_dir) then spawn,'mkdir -p '+L3_year_dir
L3_mon_dir = L3_year_dir+'/'+month
if not dir_exist(L3_mon_dir) then spawn,'mkdir -p '+L3_mon_dir
L3_day_dir = L3_mon_dir+'/'+day
if not dir_exist(L3_day_dir) then spawn,'mkdir -p '+L3_day_dir

S3_year_dir = min3_dirS+year
if not dir_exist(S3_year_dir) then spawn,'mkdir -p '+S3_year_dir
S3_mon_dir = S3_year_dir+'/'+month
if not dir_exist(S3_mon_dir) then spawn,'mkdir -p '+S3_mon_dir
S3_day_dir = S3_mon_dir+'/'+day
if not dir_exist(S3_day_dir) then spawn,'mkdir -p '+S3_day_dir

;; Make PNG file
;;;;;;;;;;;;;;;;;;;;;;;;;;;

cd, img_day_dir

fitsfns = file_search('ifa*', count=cnt)

if cnt[0] ne 0 then begin
   for i=0, n_elements(fitsfns)-1 do begin
      filepngL = L3_day_dir + '/' + fitsfns[i] + '.png'
      filepngS = S3_day_dir + '/' + fitsfns[i] + '.png'
      norh_mk10mingif, fitsfns[i], filepngL, /nodisp
      norh_mk10mingif, fitsfns[i], filepngS, /nodisp, /half
   endfor
   
   cd, prog_dir
   spawn, prog_dir+'/mkmv_3min '+year+month+day
   spawn, prog_dir+'/mkmv_3mins '+year+month+day
   spawn, prog_dir+'/mkmv_3min1'
   spawn, prog_dir+'/mkmv_3mins1'
   spawn, 'gzip ' + img_day_dir + '/if*'
   
   if send_mail then begin
      if n_elements(mail_addr) eq 0 then mail_addr = 'masumi.shimojo@nao.ac.jp'
      
      spawn,'date', fin_date
      subj = '[3min_db_normal] process report for {' + chk_day +'}'
      naiyo = 'The 3min archive process for ' $
              + chk_day + ' has been finished at '+ fin_date +'.'
      
      spawn,'echo "'+ naiyo +'" | mail '+ ' -s "' + subj + '" '+ mail_addr 
      
   endif
   
endif else begin

   cd, prog_dir
   
   if send_mail then begin
      if n_elements(mail_addr) eq 0 then mail_addr = 'masumi.shimojo@nao.ac.jp'
      
      spawn,'date', fin_date
      subj = '[3min_db_ABnormal] process report for {' + chk_day +'}'
      naiyo = 'The 3min archive process for ' $
              + chk_day + ' has been finished at '+ fin_date +' with Error.'
      
      spawn,'echo "'+ naiyo +'" | mail ' + ' -s "' + subj + '" '+ mail_addr
   endif
endelse


if debug_prom then stop

spawn,'date'

end
