
; CNA_DOCALIB2 -- Applies second set of CALNICA calibration steps to 
; all input data groups.

; Revision history:
; I. Barg 10-Jan-2000 Modified structure names (and tag names) to match 
;             "/iraf/stsdas/lib/hstio.h".


function cna_docalib2, nic, nois, phot, input, crimage

@calnica_common

  if nic.ngroups gt 1 then begin
    ; Do photometric correction .
    if (nic.PHOT.corr eq PERFORM) then begin
      if (cna_photcalc (nic, phot, input.group[0]) ne 0) then return,20
    endif
    
    for i = 0, nic.ngroups-1 do begin
      tmpscihdr = input.group[i].sci.hdr
      tmpgblhdr = input.group[0].globalhdr
      if (i eq 0) then begin
        t = cna_calReport (nic.PHOT, 0, tmpscihdr, tmpgblhdr)
      endif else begin
        t = cna_calReport (nic.PHOT, -1, tmpscihdr, tmpglbhdr)
      endelse
      input.group[i].sci.hdr = tmpscihdr 
      input.group[0].globalhdr = tmpgblhdr
    endfor
  endif else begin
    ; Do photometric correction .
    if (nic.PHOT.corr eq PERFORM) then begin
       if (cna_photcalc (nic, phot, input) ne 0) then return,20
    endif
    tmpscihdr = input.sci.hdr
    tmpgblhdr = input.globalhdr
    t = cna_calReport (nic.PHOT, 0, tmpscihdr, tmpgblhdr)
    input.sci.hdr = tmpscihdr 
    input.globalhdr = tmpgblhdr
  endelse

  ; Do cosmic-ray identification and rejection.
  ;print,input.err.data.data
  if (nic.CRID.corr eq PERFORM) then begin

    if (cna_cridcalc (nic, input, crimage) ne 0) then return, 20
     
    if (nic.obsmode eq MULTIACCUM) then begin
      tmpgblhdr = crimage.globalhdr
      tmpscihdr = crimage.sci.hdr
      if (updatePriHdr (nic.CRID, tmpgblhdr) ne 0) then return, 20
      if (updateSciHdr (nic.CRID, tmpscihdr) ne 0) then return, 20
      crimage.globalhdr = tmpgblhdr 
      crimage.sci.hdr = tmpscihdr 
  
      ; Compute statistics for CR image.
      if (cna_statcalc (crimage) ne 0) then return, 20
  
    endif
  endif

  if nic.ngroups gt 1 then begin
    for i = 0, nic.ngroups-1 do begin
      tmpscihdr = input.group[i].sci.hdr
      tmpgblhdr = input.group[0].globalhdr
      t = cna_calReport (nic.CRID, -1, tmpscihdr, tmpgblhdr)
      input.group[i].sci.hdr = tmpscihdr 
      input.group[0].globalhdr = tmpgblhdr
    endfor
  endif else begin
    tmpscihdr = input.sci.hdr
    tmpgblhdr = input.globalhdr
    t = cna_calReport (nic.CRID, 0, tmpscihdr, tmpgblhdr)
    input.sci.hdr = tmpscihdr 
    input.globalhdr = tmpgblhdr
  endelse

  ; Check MultiACCUM groups for decreasing counts.
  if (nic.obsmode eq MULTIACCUM) then begin
     cna_macheck, nic, input
  endif

  ; Compute statistics for calibrated data.
  if (nic.ngroups gt 1) then begin
    for i = 0, nic.ngroups-1 do begin
      tmpgroup = input.group[i]
      if (cna_statcalc (tmpgroup) ne 0) then return, 20
      input.group[i] = tmpgroup
    endfor
  endif else begin
    if (cna_statcalc (input) ne 0) then return, 20
  endelse

  ; Do predicted background calculation.
  if (nic.BACK.corr eq PERFORM) then begin
    if (cna_backcalc (nic, input) ne 0) then return, 20
  endif

  if nic.ngroups gt 1 then begin
    for i = 0, nic.ngroups-1 do begin
      tmpscihdr = input.group[i].sci.hdr
      tmpgblhdr = input.group[0].globalhdr
      if (i eq 0) then begin
        t = cna_calReport (nic.BACK, 0, tmpscihdr, tmpgblhdr)
      endif else begin
        t = cna_calReport (nic.BACK, -1, tmpscihdr, tmpglbhdr)
      endelse
      input.group[i].sci.hdr = tmpscihdr 
      input.group[0].globalhdr = tmpgblhdr
    endfor
  endif else begin
    tmpscihdr = input.sci.hdr
    tmpgblhdr = input.globalhdr
    t = cna_calReport (nic.BACK, 0, tmpscihdr, tmpgblhdr)
    input.sci.hdr = tmpscihdr 
    input.globalhdr = tmpgblhdr
  endelse
 
  ; Generate User Warnings. 
  if (nic.WARN.corr eq PERFORM) then begin
    if (nic.ngroups gt 1) then begin
      if (cna_userwarn (nic, input.group[0]) ne 0) then return, 999
    endif else begin
      if (cna_userwarn (nic, input) ne 0) then return, 20
    endelse
  endif

  if (nic.ngroups gt 1) then begin
    for i = 0, nic.ngroups-1 do begin
      tmpscihdr = input.group[i].sci.hdr
      tmpgblhdr = input.group[0].globalhdr
      t = cna_calReport (nic.WARN, -1, tmpscihdr, tmpgblhdr)
      input.group[i].sci.hdr = tmpscihdr 
      input.group[0].globalhdr = tmpgblhdr
    endfor
  endif else begin
    tmpscihdr = input.sci.hdr
    tmpgblhdr = input.globalhdr
    t = cna_calReport (nic.WARN, 0, tmpscihdr, tmpgblhdr)
    input.sci.hdr = tmpscihdr 
    input.globalhdr = tmpgblhdr
  endelse

  ; Successful return.
  return, 0

end

