
; CNA_GETDATAUNITS -- Read the BUNIT keyword from group header.

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


function cna_getDataUnits, nic, header

@calnica_common

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

  ; Read the BUNIT keyword from the header.
  temp = sxpar(header, 'BUNIT')
  if (!ERR eq -1) then begin
      print, 'ERROR: Keyword BUNIT not found in ',nic.filename
      return, 77
  endif

  ; Check the BUNIT keyword value for validity.
  if (strpos (temp, 'COUNTS/S') eq 0) then begin
    nic.bunit(nic.group-1) = COUNTRATE
  endif else if (strpos (temp, 'COUNTS') eq 0) then begin
    nic.bunit(nic.group-1) = COUNTS
  endif else begin
    print, 'Unrecognized BUNIT value ',temp,' in ',nic.filename
    return, 77
  endelse

  ; Successful return.
  return, 0
end


; CNA_GETTDFTRANS -- Read the TDFTRANS keyword from group header.

function cna_getTDFTrans, nic, header

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


  ; Read the TDFTRANS keyword.
  nic.tdftrans(nic.group-1) = -1
  nic.tdftrans(nic.group-1) = sxpar(header, 'TDFTRANS')
  if (!ERR eq -1) then begin
      print, 'ERROR: Keyword TDFTRANS not found in ',nic.filename
      return, 78
  endif

  ; Check the TDFTRANS keyword value for validity.
  if (nic.tdftrans(nic.group-1) lt 0) then begin
    print, 'TDFTRANS value ',nic.tdftrans(nic.group-1),' in ',nic.filename, $
      ' is illegal'
    return, 78
  endif

  ; Successful return.
  return, 0
end


; CNA_GETEXPTIME -- Read the exposure time keyword from group header.

function cna_getExpTime,nic,header

@calnica_common

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

  ; Initialize the exposure time.
  nic.exptime(nic.group-1) = 0

  ; Read the exposure time keyword from the header.
  nic.exptime(nic.group-1) = sxpar(header, 'SAMPTIME')
  if (!ERR eq -1) then begin
    print, 'ERROR: Keyword SAMPTIME not found in ',nic.filename
    return, 7
  endif

  ; Check the exposure time value for validity.
  if (nic.exptime(nic.group-1) lt 0) then begin
    print, 'SAMPTIME keyword value ',nic.exptime(nic.group-1), $
	   ' not valid in ',nic.filename
    return, 7
  endif else if (nic.exptime(nic.group-1) eq 0) then begin
    if (not(nic.obsmode eq MULTIACCUM and nic.group eq nic.ngroups)) then begin
	print, 'Warning: SAMPTIME equal to zero in ',nic.filename
    endif
  endif

  ; Successful return.
  return, 0

end


; CNA_GETGROUPINFO -- Check input group for null data and get exposure time.

function cna_getGroupInfo,nic,in

  ;   Arguments:
  ;     nic      io: NICMOS info structure
  ;     in        i: input image data (SingleNicmosGroup)

  ; Compute min and max of science image to check for null data.
  datamin = min(in.sci.data.data)
  datamax = max(in.sci.data.data)

  ; If both DATAMIN and DATAMAX = 0, then it's a null image.
  if (datamin eq 0.0 and datamax eq 0.0) then begin
    nic.NullData(nic.group-1) = 1
  endif else begin
    nic.NullData(nic.group-1) = 0
  endelse

  ; Get the exposure time.
  if (cna_getExpTime (nic, in.sci.hdr) ne 0) then return, 6

  ; Get the data units.
  if (cna_getDataUnits (nic, in.sci.hdr) ne 0) then return, 66

  ; Get the TDF status.
  if (cna_getTDFTrans (nic, in.sci.hdr) ne 0) then return, 88

  ; Successful return.
  return, 0

end
 
