;------------------------------------------------------------------------------;
;+
; NAME
;           EIS_PLAN_SUMMARY__DEFINE
;
; CATEGORY
;           EIS Timeline Planning - eis_mk_study & eis_mk_plan
;
; PURPOSE
;           This procedure defines the data members of the
;           Object "eis_plan_summary".
;
; NOTES
;           This object is called automatically during the CPT process.
;           The fileName is based on the CPT start_time
;
; INPUTS
;           cpt_tlc - an IDL_Container object. This object contains
;                     the timeline entries that are being processed
;                     by CPT.
;
; WRITTEN
;           John A Rainnie RAL 30-Mar-2007
;
; HISTORY
;           v0.1 JAR 3-Apr-2007
;               Added methods to generate lines for each timeline entry.
;               Also added fileName parameter (which is created in the
;               CPT gui)
;           v0.2 JAR 4-Apr-2007
;               Added spacecraft pointing and raster scan/sns fields.
;               Also move time-tagged field in TL block.
;           v0.3 JAR 25-Apr-2007
;               Changed slot to 266"
;           v0.4 JAR 15-Jun-2007
;               Added additional header to output file - as suggested
;               by David Brooks/Williams combo
;           v0.5 JAR 12-Jul-2007
;               Added metadata keywords, and tracking info.
;           v0.6 JAR 29-Oct-2008
;                Copy plan summary file to $CMDPLN_EISCO/latest folder
;           v0.7 JAR 22-Jun-2010
;                Moved raster info after duration by 2 spaces, since MCRW
;                reported that if the duration is more than a day then it
;                overflows into the Type/Reps field (1d16h26m24Scan) and
;                messes up his routine that reads this file.
;           v0.8 JAR 12-Aug-2014
;                Suggested by John Mariska. Add "OBS_ID" to plan summary
;                file. Decided to replace it for "JOP_ID" which was never
;                used.
;                JAR 26-Aug-2014
;                Email from JTM said that JOP_ID is used. So revert back.
;                Instead, indicate which obs_id mode is used by concatenating
;                a string to the end of the first line of the observation.
;
;-
;______________________________________________________________________________
FUNCTION eis_plan_summary::Init , cpt_tlc , fileName , widID = widID

OPENW , lun , fileName , /GET_LUN , width = 200

get_utc , utc
top_line = STRJOIN(['File created: ' , anytim2utc(utc , /STIME)] , /SINGLE)
PRINTF , lun , top_line

;PRINTF , lun , "Sci = Science Observations"
;PRINTF , lun , "Eng = Engineering"
PRINTF , lun , "TL  = Timeline entry"
PRINTF , lun , "ST  = Study"
PRINTF , lun , "RA  = Raster"


PRINTF , lun , ''

;
; For each timeline entry, process each study
;
FOR i = 0 , cpt_tlc->count() -1 DO BEGIN

    temp = cpt_tlc->get(POSITION = i)
    tl   = temp->get()
    ;
    ; Some unpacking...
    ;
    tc      = tl.time_component
    sc      = tl.science_component
    ;
    ; Is study Time-Tagged?
    ;
    CASE tc.time_tagged OF
        1    : tt = 'Time_Tagged'
        ELSE : tt = 'Chained'
    ENDCASE
    ;
    ; Is study Science or Engineering
    ;
    CASE tc.entry_type OF
        'S' : stype = 'Science'
        'E' : stype = 'Engineering'
    ENDCASE
    ;
    ; Is study Fixed Pointing or Feature Tracking
    ;
    CASE tl.science_component.studyRasters[0].cmp OF
        0    : track = 'Fixed_Pointing'
        ELSE : track = 'Feature_Tracking'
    ENDCASE
    ;
    ; Is study Recursive
    ;
    CASE tc.recursive OF
        1    : rec = 'Recursive'
        ELSE : rec = 'Non-Recursive'
    ENDCASE

    ; JAR 26-Aug-2014
    ; Add further note to line, indicating obs_id, suggested by JTM and HW;
    CASE sc.obs_id OF
       1    : obs_id_text = "pointing adjustment = AIA"
       2    : obs_id_text = "pointing adjustment = AIA+SOT"
       ELSE : obs_id_text = "pointing adjustment = none"
    ENDCASE


    ;
    ; Timeline entry
    ;
    seq_id          = STRING(format='(I06)',tc.sequence_id)
    tl_line         = STRING(format='(A-3,A-8)','TL',seq_id)

    tl_line_details = STRJOIN([stype,tt,track,rec,obs_id_text],', ',/SINGLE)
    tl_line_all     = STRJOIN([tl_line,tl_line_details],/SINGLE)

    PRINTF , lun , tl_line_all

    ;
    ; OBSTITLE
    ;
    PRINTF , lun , STRING(format='(A-11,A-80)','OBSTITLE',sc.obstitle)

    ;
    ; OBS_DEC
    ;
    PRINTF , lun , STRING(format='(A-11,A-80)','OBS_DEC',sc.obs_dec)

    ;
    ; JOIN_SB
    ;
    PRINTF , lun , STRING(format='(A-11,A-80)','JOIN_SB',sc.join_sb)

    ;
    ; NOAA_NUM
    ;
    PRINTF , lun , STRING(format='(A-11,I-8)','NOAA_NUM',sc.noaa_num)

    ;
    ; JOP_ID
    ;
    PRINTF , lun , STRING(format='(A-11,I-8)','JOP_ID',sc.jop_id)

    ;;
    ;; OBS_ID
    ;;
    ;PRINTF , lun , STRING(format='(A-11,I-8)','OBS_ID',sc.obs_id)

    ;
    ; SCI_OBJ
    ;
    PRINTF , lun , STRING(format='(A-11,A-80)','SCI_OBJ',sc.sci_obj)

    ; Check if study is science/engineering
    CASE tl.time_component.entry_type OF
        'S'  : Self->process_sci_study , tl , lun
        'E'  : Self->process_eng_study , tl , lun
        ELSE : stop
    ENDCASE

ENDFOR

FREE_LUN , lun

;
; Now copy plan summary file to folder;
;       $HOME/remote_planning/eisco/latest
;
cmdpln_eisco_path = EXPAND_PATH(getenv('CMDPLN_EISCO'))
latest_path       = STRJOIN([cmdpln_eisco_path,'latest'] ,           $
                                            PATH_SEP(),/SINGLE)

IF (FILE_TEST(latest_path,/DIRECTORY) EQ 1) THEN BEGIN

   baseName      = FILE_BASENAME(fileName)
   fileName_copy = CONCAT_DIR(latest_path , baseName)
   FILE_COPY , fileName , fileName_copy , /OVERWRITE , /FORCE
   ;IF ~FILE_TEST(fileName_copy) THEN FILE_COPY , fileName , fileName_copy
   mText = STRJOIN(['Plan Summary file       : ',fileName_copy],/SINGLE)
   CASE N_ELEMENTS(widID) OF
       0    : print,mText
       ELSE : WIDGET_CONTROL , widID , set_value = mText , /APPEND
   ENDCASE
ENDIF



RETURN , 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
FUNCTION eis_plan_summary::get_raster_size , sra

; Get slitSlotWidth
CASE sra.slitindex OF
        0    : ssWidth = 1.
        1    : ssWidth = 266.
        2    : ssWidth = 2.
        3    : ssWidth = 40.
ENDCASE

CASE sra.rasterType OF
    0    : width = (sra.scan_fm_nsteps * sra.scan_fm_stepsize) + ssWidth
    1    : width =                                               ssWidth
ENDCASE

ra_size = STRJOIN([STRCOMPRESS(FIX(width),/REMOVE_ALL)           ,          $
                   'x'                                           ,          $
                   STRCOMPRESS(FIX(sra.windHeight),/REMOVE_ALL)] ,          $
                   /SINGLE)

RETURN , ra_size

END
;______________________________________________________________________________
;
;______________________________________________________________________________
FUNCTION eis_plan_summary::ra_format

; 22-Jun-2010 JAR
; Modified this ..................++++ from A-10
RETURN , '(A-3,A-8,A-22,A-25,A-25,A-12,A-6,A-6,A-10,A-10,A-10)'

END
;______________________________________________________________________________
;
;______________________________________________________________________________
FUNCTION eis_plan_summary::sci_st_format

RETURN , '(A-3,A-8,A-22,A-25,A-25,A-10)'

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_plan_summary::process_sci_study , tl , lun

;
; Some unpacking...
;
tc      = tl.time_component
sc      = tl.science_component
;
; Study
;
st_id    = STRING(format='(I06)',tc.st_id)
st_ac    = STRTRIM(tc.acronym,2)
st_start = anytim2utc(tc.start_time , /STIME,/TRUNCATE)
st_stop  = anytim2utc(tc.end_time   , /STIME,/TRUNCATE)
st_dur   = sec2dhms(tc.end_time-tc.start_time)
st_line  = STRING(format=Self->sci_st_format()               ,              $
                         'ST',st_id,st_ac,st_start,st_stop,st_dur)

PRINTF , lun , st_line

PRINTF , lun , "RA ID      Acronym               Start Time"    +           $
               "               Stop Time                Duration  "  +      $
               "  Type/Reps   FOV       Solar X   Solar Y"

;
; Rasters
;
FOR i = 0, sc.nRasters - 1 DO BEGIN

    sra      = sc.studyRasters[i]
    ra_id    = STRING(format='(I06)',sra.ra_id)
    ra_ac    = STRTRIM(sra.ra_acronym,2)
    ra_start = anytim2utc(sra.ra_start_time , /STIME,/TRUNCATE)
    ra_stop  = anytim2utc(sra.ra_stop_time  , /STIME,/TRUNCATE)
    ra_dur   = sec2dhms(sra.ra_stop_time-sra.ra_start_time)
    ra_size  = STRTRIM(Self->get_raster_size(sra))
    x_point  = STRTRIM(STRING(format='(F-9.2)',sra.x_point),2)
    y_point  = STRTRIM(STRING(format='(F-9.2)',sra.y_point),2)
    ra_reps  = STRJOIN(['x',STRCOMPRESS(sra.ra_repeats , /REMOVE_ALL)] ,    $
                    /SINGLE)

    CASE sra.rasterType OF
        0 : rType = 'Scan'
        1 : rType = 'SNS'
    ENDCASE

;    ; S/C pointing
;    scp = STRJOIN(['(sc='                                         ,         $
;                   STRTRIM(STRING(format='(F-9.2)',sra.sc_x),2)   ,         $
;                   ','                                            ,         $
;                   STRTRIM(STRING(format='(F-9.2)',sra.sc_y),2)   ,         $
;                   ')'] , /SINGLE)

    ra_line = STRING(format=Self->ra_format()                  ,            $
                         'RA',ra_id,ra_ac,ra_start,ra_stop,ra_dur,          $
                         rType,ra_reps,ra_size,x_point,y_point)

    PRINTF , lun , ra_line

ENDFOR

PRINTF , lun , ''

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_plan_summary::process_eng_study , tl , lun

;
; Some unpacking...
;
tc      = tl.time_component
sc      = tl.science_component
;
; Study
;
st_id    = STRING(format='(I06)',tc.st_id)
st_ac    = STRTRIM(tc.acronym,2)
st_start = anytim2utc(tc.start_time , /STIME,/TRUNCATE)
st_stop  = anytim2utc(tc.end_time   , /STIME,/TRUNCATE)
st_dur   = sec2dhms(tc.end_time-tc.start_time)
st_line  = STRING(format=Self->sci_st_format()               ,              $
                         'ST',st_id,st_ac,st_start,st_stop,st_dur)

PRINTF , lun , st_line

PRINTF , lun , ''

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_plan_summary::setProperty , wavelength = wavelength

stop
IF (N_ELEMENTS(wavelength) GT 0) THEN stop

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_plan_summary::getProperty , ra = ra

stop
ra = Self.ra

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_plan_summary::Cleanup


END
;______________________________________________________________________________
Pro eis_plan_summary__define
;______________________________________________________________________________

junk =  {   eis_plan_summary,                                                $
                        fileName          : ''                             $
           }

End
