function mk_dset_str, filvar, ss, sxtp=sxtp, sxtf=sxtf, files=files
;
;+
;NAME:
;	mk_dset_str
;PURPOSE:
;	Create a dataset structre for input to the "RD_xxx" routines
;	from an "SS" array of roadmap indices
;CALLING SEQUENCE:
;	dset_arr = mk_dset_str(infil, ss)
;	dset_arr = mk_dset_str(filidx, ss)
;	dset_arr = mk_dset_str(infil, -1)	;select all dset in file(s)
;	dset_arr = mk_dset_str(infil, dset_arr)
;	dset_arr = mk_dset_str(fnmap, sxtp=dsxtp, files=files)
;INPUT:
;	filvar	- file name variable.  Can be
;		1. a string array of filenames.  If this calling format is
;		   used, then the range of ".dset" values is checked to make
;		   sure that it is within range for each file and the ".ifil"
;		   maximum is checked to be smaller than the number of file
;		   names passed in.
;		2. an integer vector of filename indicies (the optional
;		   output from rd_roadmap)
;		3. Filename map "FNMAP" structure from a search program.  If this
;		   is used, then the "sxtp" or "sxtf" inputs should be used
;		   to tell which filenames to extract.  The ss parameter is
;		   not used in this case.
;	ss	- a list of the indicies to extract
;		1. Most common method is that "SS" is the indicie of 
;		   the roadmap.  When used with filvar=filenames, this must be 
;		   the indicie with respect to ALL datasets in ALL files 
;		   available in "filenames"
;		2. SS can equal -1, and then all datasets are selected
;		   (the explicit extraction list is created).  "SS" is
;		   modified in the program an returns a "INDGEN" with
;		   the total # of datasets selected.
;		3. If "SS" is a structure type, the routine assumes that
;		   the conversion to a dataset structure has already occured
;		   and returns the input.  It is checked first to make sure
;		   that there ".dset" does not equal -1 anywhere and expands
;		   it if it does.
;OPTIONAL KEYWORD INPUT:
;	sxtp	- only used if "filvar" is FNMAP.  It is the PFI dataset # 
;		  array (from credvec).  Should not be used sxtf option.
;	sxtf	- only used if "filvar" is FNMAP.  It is the FFI dataset # 
;		  array (from credvec).  Should not be used sxtp option
;OPTIONAL KEYWORD OUTPUT:
;	files	- when using the FNMAP option, the filename array to be
;		  used with the ".ifil" field in the output is returned here.
;RESTRICTION:
;	The "SS" input cannot mix convensions 1 and 2 (passing a vector of
;	roadmap indicies with a -1 in there somewhere).  A -1 can be mixed
;	in the "SS" if it is a structure type (option 3), but if that
;	option is used, then filvar must the the filename list (so that the
;	number of datasets in the file can be known)
;OUTPUT:
;	returns the variable that can be passed directly to "RD_xxx" routines
;HISTORY:
;	Written 27-Feb-92 by M.Morrison
;	 1-Mar-92 (MDM) - Added option to pass the filename map (FNMAP)
;	 5-Mar-92 (MDM) - Adjustments to fix bug with FNMAP option
;	19-Mar-92 (MDM) - Removed use of MAKE_STR function use, just use a
;			  fixed structure name
;	20-Apr-92 (MDM) - Fixed case where the input files passed in have
;			  no datasets in them.  Return a structure with the
;			  "ifil" and "dset" set to -1.
;	21-Apr-92 (MDM) - Fixed case where one of the files listed has
;			  no datasets in the file.
;	11-Jun-92 (MDM) - Fixed case where first file has zero datasets
;	20-Jun-94 (MDM) - Modified case where all files have zero datasets
;-
;
out0 = {mk_dset_str, dset: fix(0), ifil: fix(0)}
;
siz = size(filvar)
typ = siz(siz(0) + 1)
siz = size(ss)
typ2 = siz( siz(0) + 1)
;
case typ of
    7: begin			;filvar = infil
	nfiles = n_elements(filvar)
	rd_fheader, filvar, fheader, fil_ndsets
	if (total(fil_ndsets) eq 0) then begin
	    out0.ifil = 0		;MDM changed from -1 to 0 20-Jun-94
	    out0.dset = -1
	    return, out0
	end
	filidx = -99
	for i=0,nfiles-1 do if (fil_ndsets(i) ne 0) then filidx = [filidx, intarr(fil_ndsets(i)) + i]
	filidx = filidx(1:*)
	tot_ndset = total(fil_ndsets)
       end
     8: begin			;filvar = fnmap
	files=get_fninfo(filvar, limits, sxtf=sxtf, sxtp=sxtp)
	nfiles = n_elements(files)
	;;rd_fheader, files, fheader, fil_ndsets

	if (keyword_set(sxtp)) then ss = sxtp	;copy dataset # vector into "ss"
	if (keyword_set(sxtf)) then ss = sxtf
	nout = n_elements(ss)

	filidx = intarr(nout) -1	;set to -1
	for i=0,nfiles -1 do if (limits(i,0) ne -1) then filidx(limits(i,0):limits(i,1)) = i
       end
     else: begin		;filvar = filidx
	nfiles = max(filvar)+1
	fil_ndsets = intarr(nfiles)
	for i=0,nfiles-1 do begin
	    dummy = where(filvar eq i, count)
	    fil_ndsets(i) = count
	end
	
	filidx = filvar
	tot_ndset = total(fil_ndsets)
      end
endcase
;
case typ2 of
  0: begin			;undefined (using fnmap)
	out = replicate(out0, nout)
	if (nout eq 1) then begin	;because of trouble with IDL with one element arrays
	    out.dset = ss(0)
	    out.ifil = filidx(0)
	end else begin
	    out.dset = ss
	    out.ifil = filidx
	end
     end
  8: begin			;ss = dset_arr
	out = ss
	sss = where(out.dset eq -1, count)
 	while (count ne 0) do begin		;check for cases where asking for while file (using -1 option)
	    ist = sss(0)
	    ifil = out(ist).ifil
	    n = fil_ndsets(ifil)
	    tmp = replicate(out(0), n)
	    tmp.dset = indgen(n)
	    tmp.ifil = intarr(n)+ifil
	    case 1 of
		(n_elements(out) eq 1):	out = tmp
		(ist eq 0): 			out = [tmp, out(1:*)] 
		(ist eq n_elements(out)-1):	out = [out(0:ist-1), tmp]
		else:			out = [out(0:ist-1), tmp, out(ist+1:*)]
	    endcase
	    sss = where(out.dset eq -1, count)
	end
     end
else: begin		;input was an integer or integer array (fnmap or ss options)
	nout = n_elements(ss)

	if (typ ne 8) then begin	;only do following check if not using "fnmap" option
	    ;offset correction to convert from roadmap indicie to dataset # within each file
	    offset = intarr(nfiles)
	    for i=1,nfiles-1 do offset(i) = total(fil_ndsets(0:i-1))
	
	    ;If there are any -1 in the SS input, extract all datasets
	    dummy = where(ss eq -1, count)
	    if (count ne 0) then ss = indgen(tot_ndset)		;If there are any -1 in the SS input, extract all datasets of ALL files
	    nout = n_elements(ss)
	
	    if (max(ss) gt tot_ndset) then begin
		print, 'MK_DSET_STR: You asked for a relative dataset number out of range'
		print, 'There are only a total of ', tot_ndset, ' datasets available in 
		print, 'the file(s) that you specified. 
		print, 'You asked for dataset #: ', max(ss)
		print, 'Stopping...
		stop
	    end
	end
 	;
	out = replicate(out0, nout)
	if (nout eq 1) then begin
	    out.dset = ss(0) - offset(filidx(ss(0)))
	    out.ifil = filidx(ss(0))
	end else begin
	    out.dset = ss - offset(filidx(ss))    ;adjust back to dset number within the file
	    out.ifil = filidx(ss)
	end
    end
endcase
;
sss = where(out.dset lt 0, count)
if (count ne 0) then begin
    print, 'MK_DSET_STR: You asked for a negative data set number
    print, 'other than -1.  Stopping'
    stop
end
if (typ eq 7) then begin	;if passed in filename list, check the data range for errors
    for i=0,nfiles-1 do begin
	sss = where(out.ifil eq i, count)
	if (count ne 0) then begin
	    if (max(out(sss).dset) gt fil_ndsets(i)-1) then begin
		print, 'MK_DSET_STR: You asked for a dataset number out of range'
		print, 'FILE:  ', filvar(i)
		print, 'ASK FOR DATASET #:', max(out(sss).dset)
		print, '# DATASETS AVAILABLE: ', fil_ndsets(i)
		print, 'Stopping...
		stop
	    end
	end
    end

    if (max(out.ifil) gt nfiles-1) then begin
	print, 'MKDSET_STR: The ".IFIL" field is larger than the number of
	print, 'files passed in.  Stopping ....'
	print, 'YOU REQUESTED FILE#: ', max(out.ifil)
	print, 'LAST FILE AVAILABLE: ', nfiles-1
	stop
    end
end
;
return, out
end
