set_plot,'Z'
 
;+
;   Name: go_sotsp_time2level1
;
;   Purpose: main level program to run L0->L1 pipeline for SOT/SP
;
;   Run via ssw_batch
;   
;   S.L.Freeland - circa 1-oct-2007
; 
t0='1-dec-2006' ; example for Dec-2006
t1='31-dec-2006' 
quicklook=0 ; use level0/sirius

tg=timegrid(t0,t1,/days,out='ecs')
tg=anytim(tg,/ecs,/date_only)

if anytim(tg(0) ne anytim(t0)) then begin 
   box_message,'T0 ne 00:00'
   tg(0)=anytim(t0,/ecs) 
endif

for i=0,n_elements(tg)-1 do begin 
   nscans=0
   box_message,'Processing>> ' + arr2str([tg(i),tg(i+1)],' -TO- ')
   sps=sotsp_time2scan(tg(i),tg(i+1),quicklook=quicklook,$
      cat,files,nscans=nscans,/macroid_merge,/span_day)
   box_message,'Number scans for '+tg(i) + string(nscans)
   if i eq 0 and sps(0).partial then begin
      box_message,'Skipping 1st scn...' ; assume generated by "previous day"
      sps=sps(1:*)
      nscans=nscans-1
   endif
   for s=0,nscans-1 do begin 
      if sps(s).nrecs gt 12 then begin 
      ffs=files(sps(s).ssstart:sps(s).ssstop)
      break_file,ffs(0),ll,pp,ff,ee,vv      
      odir=str_replace(pp,'level0','level1')
      odir=str_replace(odir,'QuickLook','level1_qkl')
      odir=concat_dir(odir,time2file(file2time(ff),/sec))
      box_message,'Writing Level1 to >> ' + odir
      if not file_exist(odir) then mk_dir,odir
      sp_prep,ffs,outdir=odir
      endif else begin 
         box_message,'Short SCAN? - ignoring
         help,sps(s),/str,out=out
         file_append,'$SSW/site/logs/sp_shortscans.dat',['OUTDIR:'+odir,out]
      endelse
   end
endfor
end

