; .comp eis_study_duration.pro
; .comp eis_study_time.pro

pro screen_output, fname, start_time, end_time, st_ac, ra_ac, sid, rid, st, sd
    print, format='(A27," ",I03," ",Z02," ",I05," ",Z04," ",2(A20),2(A29),A16)', $
      fname, sid, sid, rid, rid, st_ac, ra_ac, start_time, end_time, sd
end

pro log_output, lu, fname, start_time, end_time, st_ac, ra_ac, sid, rid, st, sd
    printf, lu, format='(A27,"\",I03,"\",Z02,"\",I05,"\",Z04,"\",A20,"\",A20,"\",A29,"\",A29,"\",A16)', $
      fname, sid, sid, rid, rid, st_ac, ra_ac, start_time, end_time, sd
end

; No check whether file is a fits file or not
pro eis_study_list, file=in_file, dir=in_dir, log=log_file, study=study

    using_log = 0

    if n_elements(in_dir) ne 0 then begin
        in_files = findfile(in_dir + 'eis_l0*', count=count)
        if count eq 0 then begin
            print, 'No files'
            return
        endif
    endif else begin
        if n_elements(in_file) ne 0 then begin
            in_files = findfile(in_file, count=count)
            if count eq 0 then begin
                print, 'Cant find the file'
                return
            endif
        endif
    endelse

    if n_elements(log_file) ne 0 then begin
            openw, lu, log_file, /append, /get_lun
            using_log = 1
    endif

    hdrobj = obj_new('fits_hdr')
    for i = 0, count - 1 do begin
        parts = str_sep(in_files[i], '/')
        fname = parts[n_elements(parts)-1]

        hdrobj-> read, in_files[i]

        rid = hdrobj->getmain_keyword('RAST_ID')
        seq = hdrobj->getmain_keyword('SEQ_NR')
        date_obs = hdrobj->getmain_keyword('DATE_OBS')
        date_end = hdrobj->getmain_keyword('DATE_END')
        stud_acr = hdrobj->getmain_keyword('STUD_ACR')
        rast_acr = hdrobj->getmain_keyword('RAST_ACR')
        
        st = eis_study_time(date_obs, date_end)
        sd = eis_study_duration(st)

        screen_output, fname, date_obs, date_end, stud_acr, rast_acr, seq, rid, st, sd

        if using_log then $
          log_output, lu, fname, date_obs, date_end, stud_acr, rast_acr, seq, rid, st, sd

    endfor

    obj_destroy, hdrobj

    if using_log then begin
        printf,lu,''
        close,lu
        free_lun,lu
    endif

end
