
pro REDUCE_L1_BACKLOG,Path,Fname
;+
; NAME:
;               REDUCE_L_1_BACKLOG
;
; PURPOSE:
;               This procedure is used to perform level 0.5 backlog processing.
;               It calls REDUCE_LEVEL_1 routine to take the level 0.5 image to
;               Level 1.
;
; CATEGORY:
;               LASCO REDUCTION
;
; CALLING SEQUENCE:
;               REDUCE_L1BACKLOG, Path , Fits_name
;
; INPUTS:
;               Path = Pointer to the level 0.5 FITS file(s)
;                      ex: '/net/corona/cplex2/lz_data/level_05/960610/c3'
;
; OPTIONAL INPUTS:
;               Fname = Name of FITS file to process (ex: 32005910.fts). If 
;                       omitted, "*.fts" is used to process all FITS files
;                       in the directory that Path points to.
;
; KEYWORD PARAMETERS:
;               None
;
; OUTPUTS:
;               Fits_name = Name of FITS file created               
;                 ex: /net/corona/cplex2/lz_data/level_01/960610/c3/35005910.fts
;
;               Log file  = Log file for each processed level 0.5 FITS file
;                 ex: /net/corona/corona01/reduce_log/lz/log/red_l1_960610_35005905.log
;
;               DB file   = Data base file for each processed level 0.5 FITS 
;                           file. This file is created only if environment
;                           variable REDUCE_L1_OPTS contains DBMS.
;                 ex: /net/corona/corona01/reduce_log/lz/log/red_l1_960610_35005905.db
;
; OPTIONAL OUTPUTS:
;               None
;
; COMMON BLOCKS:
;               DBMS
;
; PROCEDURE:
;            For each Fits file, REDUCE_LEVEL_1 routine is called to take 
;            the Level 0.5 image to Level 1. It is useful for processing a
;            day worth of Fits files for a particular camera at a time. It 
;            creates a separate LOG and DB file for each Fits file.
;
;
; MODIFICATION HISTORY:
;               Written  Ed Esfandiari, NRL
;   Version 1   aee 30 Sep 1997    Initial release
;
; SCCS variables for IDL use
; 
; @(#)reduce_l1_backlog.pro	1.2 11/07/97 :NRL Solar Physics
;
;
common dbms,ludb,lulog

ver= 'V1  Sep 30 1997'

; check to see if Fits_name is provided, if not use "*.fts":

names= "*.fts"
p2= ''
if(n_params() eq 2) then begin
  names= fname
  p2= fname
end
 
if(strmid(path,strlen(path)-1,1) ne "/") then path= path+"/"
fts_names= findfile(path+names)
fsize=size(fts_names)
p=strpos(path,'level_05')
if(fsize(0) eq 0 or p lt 0) then begin
  print,'ERROR: reduce_l1_backlog - No files: '+path+names
  return
end

; setup the open log and db files:

red_path= strlowcase(getenv('RED_L1_PATH'))
; red_path= '/net/corona/corona01/reduce_log'
if(strpos(path,'lz_data') ge 0) then begin  ; using LZ .fts file(s)
  logdir= red_path+'/lz/log/' 
  dbdir= red_path+'/lz/db/'
  updir= red_path+'/lz/updates/'
  src= '5'
endif else begin  ; using QL .fts file(s)
  logdir= red_path+'/ql/log/'
  dbdir= red_path+'/ql/db/'
  updir= red_path+'/ql/updates/'
  src= '4'
endelse

; Create level-1 subdirectory:

; Note that /net/corona/cplex2/lz_data/level_1 (extracted from path)  points to
; /net/corona/corona01/lz/level_1 so level 1 images are created on corona01.

sd = strmid(path,0,p+6)+'1'+strmid(path,p+8,strlen(path))
spawn,'mkdir -p '+sd

date= strmid(path,strlen(path)-10,6)
fn_pos= strlen(path)
for i=0,n_elements(fts_names)-1 do begin
  fn= strmid(fts_names(i),fn_pos,8)
  fn= 'red_l1_'+date+'_'+strmid(fn,0,1)+src+strmid(fn,2,6)
  openw,lulog,logdir+fn+".log",/get_lun 
  openw,ludb,dbdir+fn+".db",/get_lun
  get_utc,dstart,/ecs
  printf,lulog,'Procedure reduce_l1_backlog started at '+dstart
  printf,lulog,'Procedure version = '+ver
  printf,lulog,'Input parameter #1: path= '
  printf,lulog,path
  printf,lulog,'Input parameter #2 (optional): file_name = '+p2
  printf,lulog,'Calling reduce_level_1 with the following level 0.5 FITS file:'
  printf,lulog,fts_names(i)
  reduce_level_1,fts_names(i)
  printf,lulog,'Returned from reduce_level_1'
  get_utc,dend,/ecs
  printf,lulog,'Procedure reduce_l1_backlog ended at '+dend
  free_lun,lulog
  free_lun,ludb
end

return
end
