
; CNA_MKNAMES -- Build the complete input and output file names from
; whatever the user supplied as arguments.

pro cna_mkNames, rawfile, imafile, calfile

  ; Arguments:
  ;  rawfile  io: input raw file name
  ;  imafile   o: output intermediate file name (used for MULTIACCUM only)
  ;  calfile  io: output cal file name

  fitsext1 = '.fits' ; FITS file extension (default).
  fitsext2 = '.fit'  ; FITS file extension (alternate).
  rawsuf = '_raw'    ; raw file suffix.
  imasuf = '_ima'    ; intermediate file suffix.
  calsuf = '_cal'    ; cal file suffix.
  rwbsuf = '_rwb'    ; raw targ acq bck file suffix.
  rwfsuf = '_rwf'    ; raw targ acq flt file suffix.
  clbsuf = '_clb'    ; cal targ acq bck file suffix.
  clfsuf = '_clf'    ; cal targ acq flt file suffix.

  ; Initialize.
  ext1  = 0
  ext2  = 0
  israw = 0
  isrwb = 0
  isrwf = 0
  iscal = 0
  isclb = 0
  isclf = 0
  inroot  = rawfile
  outroot = calfile

  ; Search for 'fits' extension on input file name.
  rlen = strlen(inroot)
  if (strpos (inroot,fitsext1,rlen-5) ne -1) then begin
    ext1 = 1
    inroot = strmid(inroot,0,rlen-5)
    rlen = strlen (inroot)

  ; Search for 'fit' extension on input file name.
  endif else if (strpos (inroot,fitsext2,rlen-4) ne -1) then begin
    ext2 = 1
    inroot = strmid(inroot,0,rlen-4)
    rlen = strlen (inroot)
  endif

  ; Search for 'raw' suffix on input file name.
  if (strpos (inroot,rawsuf,rlen-4) ne -1) then begin
    israw = 1
    inroot = strmid(inroot,0,rlen-4)
    rlen = strlen (inroot)

  ; Search for 'rwb' suffix on input file name.
  endif else if (strpos (inroot,rwbsuf,rlen-4) ne -1) then begin
    isrwb = 1
    inroot = strmid(inroot,0,rlen-4)
    rlen = strlen (inroot)

  ; Search for 'rwf' suffix on input file name.
  endif else if (strpos (inroot,rwfsuf,rlen-4) ne -1) then begin
    isrwf = 1
    inroot = strmid(inroot,0,rlen-4)
    rlen = strlen (inroot)
  endif
  
  ; Add any missing pieces to the input raw file name.
  if ((israw or isrwb or isrwf) and ext1 eq 0 and ext2 eq 0) then begin
    rawfile = rawfile + fitsext1
    ext1 = 1
  endif else if (israw eq 0 and isrwb eq 0 and isrwf eq 0 and $
      ext1 eq 0 and ext2 eq 0) then begin
    rawfile = rawfile + rawsuf + fitsext1
    ext1 = 1
  endif

  ; If no output name was specified, build the cal and ima file
  ; file names from the input root name.
  if (calfile eq '') then begin

    calfile = inroot
    imafile = inroot

  endif else begin

    ; Search for 'fits' extension on output file name.
    rlen = strlen (outroot)
    if (strpos (outroot,fitsext1,rlen-5) ne -1) then begin
      ext1 = 1
      ext2 = 0
      outroot = strmid(outroot,0,rlen-5)
      rlen = strlen (outroot)

    ; Search for 'fit' extension on output file name.
    endif else if (strpos (outroot,fitsext2,rlen-4) ne -1) then begin
      ext1 = 0
      ext2 = 1
      outroot = strmid(outroot,0,rlen-4)
      rlen = strlen (outroot)
    endif

    ; Search for 'cal' suffix on output file name.
    if (strpos (outroot,calsuf,rlen-4) ne -1) then begin
      iscal = 1
      outroot = strmid(outroot,0,rlen-4)
      rlen = strlen (outroot)

    ; Search for 'clb' suffix on output file name.
    endif else if (strpos (outroot,clbsuf,rlen-4) ne -1) then begin
      isclb = 1
      outroot = strmid(outroot,0,rlen-4)
      rlen = strlen (outroot)

    ; Search for 'clf' suffix on output file name.
    endif else if (strpos (outroot,clfsuf,rlen-4) ne -1) then begin
      isclf = 1
      outroot = strmid(outroot,0,rlen-4)
      rlen = strlen (outroot)
    endif

    calfile = outroot
    imafile = outroot
  endelse

  ; Build the cal and ima file names from their roots.
  if (isrwb eq 1 or isclb eq 1) then begin
    imafile = imafile + imasuf
    calfile = calfile + clbsuf
  endif else if (isrwf eq 1 or isclf eq 1) then begin
    imafile = imafile + imasuf
    calfile = calfile + clfsuf
  endif else begin
    imafile = imafile + imasuf
    calfile = calfile + calsuf
  endelse

  if (ext1 eq 1) then begin
    calfile = calfile + fitsext1
    imafile = imafile + fitsext1
  endif else if (ext2 eq 1) then begin
    calfile = calfile + fitsext2
    imafile = imafile + fitsext2
  endif else begin
    calfile = calfile + fitsext2
    imafile = imafile + fitsext2
  endelse

end
