pro norh_3min_db, arg0, arg1, 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
               

case n_params() of
  2 : begin                         ; "st_day,ed_day"
      st_time=arg0
      ed_time=arg1
  end
  1 : begin                         ; "date"
      st_time=arg0
      ed_time=anytim(st_time,/ints)
      ed_time.day=ed_time.day+1
  end
  else : begin
    message,'Number of arguments invalid'
  end
endcase

st_daytim=anytim(st_time,/ints)
ed_daytim=anytim(ed_time,/ints)

ndayx=ed_daytim.day-st_daytim.day + 1
days=anytim(transpose([[lonarr(ndayx)],[st_daytim.day+lindgen(ndayx)]]),/yo)

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

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

data_dir = '/mnt_for_arc/3min_fits_db/fits_db/'
scr_dir = '/mnt_for_arc/3min_fits_db/work_dir2/'
min3_dirL = '/mnt_for_arc/3min_fits_db/3min/'
min3_dirS = '/mnt_for_arc/3min_fits_db/3mins/'

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

cd, current=prog_dir

for nday=0, ndayx-1 do begin

  chk_day=gt_day(days(nday), /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, chk_day, $
    scr_dir = scr_dir, out_dir = img_day_dir

;;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 = 'shimojo@nro.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 '+ mail_addr + ' -s "' + subj + '"'
     
     endif

  endif else begin

     cd, prog_dir

     if send_mail then begin
        if n_elements(mail_addr) eq 0 then mail_addr = 'shimojo@nro.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 '+ mail_addr + ' -s "' + subj + '"'
     endif
  endelse


endfor

if debug_prom then stop

spawn,'date'

end
