Pro makecube, indir, filelist, outdir = outdir

    ; determine path directory delimiter from OS type
    if (!version.os_family eq 'unix') then begin
        pathdelim = '/'
    endif else begin
        pathdelim = '\'
    endelse

    ; check to see if input directory name ends in path delimiter--if not,
    ; add it to the end
    if (strmid(indir, strlen(indir)-1, 1) ne pathdelim) then begin
        indir = indir + pathdelim
    endif

    ; set outdir to indir if not set by user
    if (not keyword_set(outdir)) then begin
        outdir = indir
    endif

    ; check to see if output directory name ends in path delimiter--if not, add it
    ; to the end
    if (strmid(outdir, strlen(outdir)-1, 1) ne pathdelim) then begin
        outdir = outdir + pathdelim
    endif

    ; read the file names from the input list
    readcol, indir + filelist, files, format = '(A)'

    ; get the image dimensions
    fits_read, indir + files[n_elements(files) - 1], 0, h, /header_only
    xsize = sxpar(h, 'naxis1')
    ysize = sxpar(h, 'naxis2')

    ; get the data type
    datatype = sxpar(h, 'bitpix')

    ; get the exposure times from the first and last images in the cube
    fits_read, indir + files[0], 0, h_first, /header_only
    et_first = sxpar(h_first, 'exp_time')
    et_last = sxpar(h, 'exp_time')

    ; cube exposure time should be difference between first and last times
    et_cube = et_last - et_first

    ; make a cube to hold the images from the files based on the data type
    case datatype of
        16: begin    
            imgcube = make_array(xsize, ysize, n_elements(files), /int)
        end
        32: begin    
            imgcube = make_array(xsize, ysize, n_elements(files), /long)
        end
        -32: begin    
            imgcube = make_array(xsize, ysize, n_elements(files), /float)
        end
        -64: begin    
            imgcube = make_array(xsize, ysize, n_elements(files), /double)
        end
    endcase
    
    ; read the image frames into the cube
    for i = 0, n_elements(files) - 1 do begin
        fits_read, indir + files[i], imgframe
        imgcube[*,*,i] = imgframe        
    endfor

    ; construct an outname
    nameparts = strsplit(files[0], '_', /extract)
    nameparts[n_elements(nameparts) - 1] = 'cube.fits'
    outname = strjoin(nameparts, '_')

    ; modify the header that was initially read from the last image in the list
    sxaddpar, h, 'numutr', 6
    sxaddpar, h, 'exp_time', et_cube, 'time (sec) between 1st, last reads'  

    ; write out the cube with the header from the last image
    ; fits_write will automatically adjust naxis keywords
    fits_write, outdir + outname, imgcube, h
End
