;+
; $Id: deep_field_v3.pro,v 1.2 2023/06/05 14:16:34 secchia Exp $
; :Description:
;    Background processing
;
;
; :Inputs:
;    The inputs are:
;    processed_date	like '230325'
;
; :Keywords:
;   -
;
; :Example:
;
; :Author:
;   This code made by Evangelos Paouris (Apr. 2023).
;   GMU and APL/JHU.
;
; :History:
;   May 31, 2023: There is a possibility of some 36-second exposures being missing. To ensure accurate calculations, 
;                 we have implemented a process that identifies the missing FITS files. The calculations are then 
;                 performed using the available files. Please note that it is essential to check the log file for 
;                 information on the number of missing files and the corresponding dates.
;-

PRO Deep_Field_v3, processed_date

  ;---Give the date to be processed:
;  processed_date = '2023-03-25' input yymmdd to be compatable with pipeline
  year_in = '20'+STRMID(processed_date, 0, 2)
  print, year_in
  month_in = STRMID(processed_date, 2, 2)
  print, month_in
  day_in = STRMID(processed_date, 4, 2)
  print, day_in

  file_out = STRARR(27)
  index = FLTARR(27)

  FOR j = -13.0, 13.0 DO BEGIN
    Print, '13+j: ', 13+j
    ;---We find 13 days before this date:
    B1 = JULDAY(month_in, day_in, year_in, 00, 00, 00)
    CALDAT, (DOUBLE(B1+j)), Month1, Day1, Year1, Hour1, Minute1, Second1
    Print, Month1, Day1, Year1
    before_date = STRING(Year1, FORMAT='(I04)')+"-"+STRING(Month1, FORMAT='(I02)')+"-"+STRING(Day1, FORMAT='(I02)')
    Print, before_date


    ;---We find 12 days before this date:
    B1 = JULDAY(month_in, day_in, year_in, 00, 00, 00)
    CALDAT, (DOUBLE(B1+(j+1.0))), Month1, Day1, Year1, Hour1, Minute1, Second1
    Print, Month1, Day1, Year1
    before_date2 = STRING(Year1, FORMAT='(I04)')+"-"+STRING(Month1, FORMAT='(I02)')+"-"+STRING(Day1, FORMAT='(I02)')
    Print, before_date2
    ;---

    ;---Define the start and end dates:
    starttime = STRING(before_date, FORMAT='(A10)');'2023-02-01';'Feb-1-2023';'2023-01-22';
    starttime2 = STRING(before_date2, FORMAT='(A10)')
    ;  endtime   = STRING(after_date, FORMAT='(A10)');'2023-02-20';'Feb-20-2023';'2023-02-18';
    Print, ' '

    ;---Get all the files between two dates:
    l = SCC_READ_SUMMARY(DATE=[starttime,starttime2],TEL='COR2',SPACE='A')
    Print, 'Files for the period ' + starttime + ' and ' + starttime2
    Print, 'scc_read_summary finished'
    Print, ' '
    ;help, l, /str
    Print, N_ELEMENTS(l)

    if datatype(l) ne 'STC' then goto,nofiles

    ;---Get the files with a specific exposure time:
    i = WHERE(l.EXPTIME EQ 36.0, count)
    Print, i
    Print, STRING(count, FORMAT='(I3)') + ' file(s) were found!'

    IF i[0] EQ -1 THEN BEGIN
      
      Print, 'No file found - i=-1'
      file_out[13+j] = STRING(Year1, FORMAT='(I04)')+STRING(Month1, FORMAT='(I02)')+STRING(Day1, FORMAT='(I02)') + '_999999_d4c2A.fts'
      Print, 'new name for "ghost" file: ', file_out[13+j]
      index[13+j] = -1

    ENDIF ELSE BEGIN
      index[13+j] = 1
      Print, 'Filenames: '
      Print, l[i[0]].filename
      Print, ' '

      file_out[13+j] = l[i[0]].filename
    ENDELSE
    Print, '---'
  ENDFOR
;FOR i = 0, N_ELEMENTS(index)-1 DO BEGIN
;  Print, i, ' ', index[i]
;ENDFOR

ii = WHERE(index NE -1, countii)
if ii[0] eq (-1) then goto,nofiles
Print, ii
;if ii ne (-1) then REMOVE, ii, file_out
file_out=file_out[ii]
FOR i = 0, N_ELEMENTS(file_out)-1 DO BEGIN
  Print, i, ' ', file_out[i]
ENDFOR





  ;---Get the full path + name for each file:
  Files_in = SCCFINDFITS(file_out)
  FOR i = 0, N_ELEMENTS(file_out)-1 DO BEGIN
    Print, i, Files_in[i]
    Print, ' '
  ENDFOR

  ;---Now run the scc_monthly_min for the "Files_in" list defined before:
  ;SCC_MONTHLY_MIN_EP,'cor2','A','Feb-21-2023','dbl',NDAYS=8,OUTDIR='/Users/paoure1/',FILES=Files_in
  ;SCC_MONTHLY_MIN_EP,'cor2','A','Feb-7-2023','dbl',OUTDIR='/Users/paoure1/',FILES=Files_in

  SCC_MONTHLY_MIN,'cor2','A',processed_date,'df',FILES=Files_in

nofiles:
print,'All Done'
END
;
; $Log: deep_field_v3.pro,v $
; Revision 1.2  2023/06/05 14:16:34  secchia
; corrected DF file names when sending in file name
;
; Revision 1.1  2023/06/02 17:40:42  secchia
; made name lowercase and if statment befroe remove
;
; Revision 1.2  2023/06/02 16:03:03  nathan
; add DF option to scc_monthly_min based on scc_monthly_min_ep.pro from Evangelos Paouris
;
; Revision 1.1  2023/06/02 15:52:24  nathan
; from Evangelos
;
