
; CNA_UNITCORR: Convert NICMOS data from units of counts to countrates.
; The input SCI and ERR arrays are divided by the exposure time.
; The DQ, SAMP, and TIME arrays are unchanged.
; The BUNIT keyword in the SCI and ERR image headers are updated.

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

function cna_unitcorr, nic, input

@calnica_common

  ; Arguments:
  ;	nic	 i: NICMOS info structure
  ;	input	io: input image

  ; Skip conversion if units are already countrate.
  if (nic.bunit(nic.group-1) eq COUNTRATE) then begin
    print, 'Data already in units of countrates; UNITCORR will be skipped'
    nic.UNIT.corr = SKIP
    return, 0
  endif

  ; Compute the inverse exposure time.
  ; If we're processing a MultiAccum zeroth read, use the value of
  ; nic->sampzero for the exposure time (Vsn 3.2)

  if (nic.obsmode eq MULTIACCUM and nic.group eq nic.ngroups and $
      nic.ZSIG.corr eq PERFORM) then begin
    inv_time = 1.0 / nic.sampzero
  endif else if (nic.exptime(nic.group-1) eq 0) then begin
      inv_time = 1.0
  endif else begin
      inv_time = 1.0 / nic.exptime(nic.group-1)
  endelse

  ; Multiply the input SCI and ERR data by the inverse exposure time.
  tmp = cna_amulk(input,inv_time)

  ; Update the units keyword in the SCI and ERR headers .
  tmphdr = input.sci.hdr
  sxaddpar,tmphdr,'BUNIT','COUNTS/S'
  input.sci.hdr = tmphdr
  tmphdr = input.err.hdr
  sxaddpar,tmphdr,'BUNIT','COUNTS/S'
  input.err.hdr = tmphdr
  nic.bunit(nic.group-1) = COUNTRATE

  ; Successful return.
  return, 0

end
