pro norh_prom_mkimg_grid, mk_day, interval=interval, $
                     sy_st_tim = sy_st_tim, sy_ed_tim = sy_ed_tim, scr_dir = scr_dir, $
                     no_trans = no_trans, out_dir = out_dir, $
                     event = event, usetsx = usetsx, $
                     prog = prog,$
                     nodisp = nodisp

;+
; NAME: norh_prom_mkimg
;
; CATEGORY: 
;     norh prominence detector (ver. 2)
;
; INPUTS: 
;     date (JST) :  ex. '2001-05-05'
;
; OPTIONAL INPUTS: 
;     sy_st_tim (JST) :  def. '09:00'
;     sy_ed_tim (JST) :  def. '15:20'
;
; EXAMPLE: 
;    IDL> norh_prom_mkimg,/today
;    IDL> norh_prom_mkimg, '2004-08-10'
;
; MODIFICATION HISTORY:
;
;       Tue Aug 10 11:59:00 2004, Masumi Shimojo
;       <shimojo@delta.nro.nao.ac.jp>
;
;-


;; keyword
;;;;;;;;;;;;;;;;;;;;;;;;


if n_elements(interval) eq 0 then interval = 180.
if n_elements(sy_st_tim) eq 0 then $
  st_tim = mk_day +' 08:50' else $
  st_tim = mk_day + ' ' + sy_st_tim
if n_elements(sy_ed_tim) eq 0 then $
  ed_tim = mk_day +' 15:30' else $
  ed_tim = mk_day + ' ' + sy_ed_tim

no_trans = keyword_set(no_trans)
nodisp = keyword_set(nodisp)

st_timU = norh_jst2ut(st_tim)
ed_timU = norh_jst2ut(ed_tim)

if n_elements(prog) eq 0 then prog = 'koshix'

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

if n_elements(scr_dir) eq 0 then begin
    raw_scr = '/nfs/scr01/3min_heliog'
    if not dir_exist(raw_scr) then spawn,'mkdir -p '+raw_scr
endif else begin
    raw_scr = scr_dir
    if not dir_exist(raw_scr) then spawn,'mkdir -p '+raw_scr
endelse

if n_elements(out_dir) eq 0 then begin
    data_dir = './'
    date_arr = anytim(mk_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

    out_dir = data_dir+'prom'+year+month+day
    if not dir_exist(out_dir) then spawn,'mkdir -p '+out_dir
endif

;;data_transfer
;;;;;;;;;;;;;;;;;;;;;;;;

print, st_timU, ed_timU

if not no_trans then begin
    norh_trans, st_timU, ed_timU, interval, freq=17, $
                rawdir = raw_scr, usetsx = usetsx
    useac = 0
endif else begin
    useac = 1
    delvarx, raw_scr
endelse

;;image synth
;;;;;;;;;;;;;;;;;;;;;;;;

int_stJ = anytim(st_tim,/ints)
int_edJ = anytim(ed_tim,/ints)
img_num = (int_edJ.time-int_stJ.time)/(interval*1000)

grid_cmd = 'bsub '
norh_cmd = 'norh_synth -p koshix17 '

for i=0, img_num-1 do begin
    img_tim = anytim(anytim2ints(anytim(st_timU,/ints),$
                                 offset = i * interval),/ccsds)
    if not nodisp then print, img_tim
    norh_synth, img_tim, freq = 17, prog = prog, $
      rawdir = raw_scr, imgdir = out_dir, $
      usetsx = usetsx, event = event, useac = useac,$
      outfile='norh_synth_input_'+string(i, format='(I03)'),$
      /norun

    ;grid_cmd = 'bsub -o '+string(i, format='(I03)')+'.out -e '+string(i, format='(I03)')+'.err '

    if not nodisp then print, grid_cmd+norh_cmd+'norh_synth_input_'+string(i, format='(I03)')+'0'
    spawn, grid_cmd+norh_cmd+'norh_synth_input_'+string(i, format='(I03)')+'0'
    
endfor

wait_pcc:

wait,10

spawn, 'bjobs -u heliog | wc -l', bjobL
if not nodisp then print, bjobL
if bjobL ne 0 then goto, wait_pcc

if not useac then spawn,'\rm -f '+raw_scr+'/*'
spawn, '\rm -f norh_synth_input*'

end
