function mk_timarr, index, y, ref_time, wbs=wbs, ok_map=ok_map, struct=struct
;
;+
;NAME:
;	mk_timarr
;PURPOSE:
;	Since there are cases when there is only one time tag for
;	2,4,8,16 or 32 values, this routine will return a time vector
;	that can be used with UTPLOT.
;CALLING SEQUENCE:
;	timarr = mk_timarr(index, 8)
;	timarr = mk_timarr(index, data.pc.sxs_pc11, /wbs)
;	timarr = mk_timarr(index, data.pc.sxs_pc11, /wbs, ref_time)
;
;	then use: utplot, timarr, data.pc.sxs_pc11, index(0)
;	or        utplot, timarr, data.pc.sxs_pc11, ref_time
;INPUT:
;	index	- the index structure that goes with the data
;	y	- either (1) the number of repeats per DATA SET or
;		  (2) the data array that is to be plotted.  If (1)
;		  is used, it must be the number of repeats per data
;		  set.  Since WBS data takes two major frames per data
;		  set, be sure to use the correct number.  For example,
;		  data.pc.sxs_pc11 has 8 samples per major frame, but
;		  that is 16 samples per data set.
;OPTIONAL INPUT:
;	ref_time- a reference time to use to generate the time array.
;		  If it is not passed, then index(0) is used as the
;		  reference
;	wbs	- if set, then the number of samples in the data array
;		  took TWO major frames to accumulate and adjust
;		  accordingly.  
;OUTPUT:
;	returns an floating point array with the number of seconds
;	past the reference time for each data point.
;OPTIONAL KEYWORD OUPTUT:
;	ok_map	- A byte array the same size/dimention as the output
;		  array saysing where there is data or not.  
;HISTORY:
;	Written 31-May-92 by M.Morrison
;	29-Apr-93 (MDM) - Added check for WBS missing data
;			- Added output OK_MAP
;			- Added check for duplicate WBS data
;			- Added STRUCT option
;	11-Aug-93 (MDM) - Corrected an error which was not allowing
;			  "y" to be a scalar
;       23-Nov-93 (JMM) - Allows index to have only one element.
;	 7-Jun-94 (MDM) - Corrected error which was happening when
;			  "y" was a simple 1-D vector, or an
;			  array 1xN, and "nx" was supposed to be 1
;			  (it was being set to "N")
;	 9-Jun-94 (MDM) - Furthur correction to 7-Jun-94 mod
;			  (ok_map subscript out of range)
;	17-Jun-94 (MDM) - Added some comments
;	20-Jun-94 (MDM) - Patched error which did not allow an ADA
;			  single nested structure to work
;-
;
n = n_elements(index)
dprate = dprate2sec(index)
if (keyword_set(wbs)) then dprate = dprate*2		;time between datasets is 2 Major Frames
;
if (n_elements(y) eq 1) then begin	;a scalar was passed
    nx = y(0)
end else begin
    ;siz = size(reform(y)) 	;need to remove the first dimension if it is 1 (happens, for example, with data.iru(0,*)  )
    ;nx = siz(1)

    siz = size(y)		;need to remove the first dimension (and maybe even the second?) IF
				;it is 1 (happens, for example, with data.iru(0,*)  )
				;
				;last dimension should be the number of datasets.
				;IDL> help,indexh,indexh.hxt.dhk_data
				;INDEXH          STRUCT    = -> MS_141164914001 Array(21)
				;<Expression>    BYTE      = Array(64, 21)
    ndim = siz(0)
    case ndim of
	1: nx = 1		;simple 1-D X and Y array
	else: nx = max(siz(1:ndim-1))
    endcase
end
;
xx = int2secarr(index, ref_time)
x = fltarr(nx, n)
for i=0,nx-1 do x(i,*) = xx + dprate*i/float(nx)
;
ok_map = bytarr(nx, n) + 1b		;added 29-Apr-93
tags = tag_names(index)
;if (tags(1) eq 'WBS') then begin
if (n_elements(tags) ge 2) then if (tags(1) eq 'WBS') then begin
    print, 'Checking number of major frame available in the dataset for WBS'
    for i=0,n-1 do begin
	if (i ne n-1) then begin
	    del = int2secarr(index(i+1).gen, index(i).gen2)	;should be positive
	    if (del lt 0) then index(i).wbs.nmf = 1		;second half is bad - flag it as not there
	end

	bits, index(i).wbs.nmf, bits
	if (bits(0) eq 0) then x(*,i) = x(*,i) - dprate(i)/2.	;since the first half is missing, but the time tag for the
								;second half was put the the first half index (.GEN), it means
								;that we have to back the time up a major frame so that the time
								;in the matrix is correct, and so that we can use the "OK_MAP"
								;properly with the data

	if (bits(0) eq 0) then ok_map(0:(nx/2-1)>0,i) = 0b	;missing first half - MDM patched 9-Jun-94
	if (bits(1) eq 0) then ok_map(nx/2:nx-1,i) = 0b		;missing second half
    end
    
    if(n gt 1) then begin                ;JMM 23-Nov-93, you don't have duplicate data sets, if there's only one...
       dummy = deriv_arr(x(0, *))
       ss = where(dummy eq 0)
       if (ss(0) ne -1) then begin
          for i = 0, n_elements(ss)-1 do begin
	    ;ss = 0 means dset 0 and 1 have the same time
             index0 = index( [ss(0), ss(0)+1] )
             dummy = min( index0.wbs.nmf, imin )
             ok_map(*, ss(0)+imin) = 0b
             print, 'MK_TIMARR: Found duplicate datasets.  Flagging', fmt_tim(index0(imin)), ' as bad'
          end
       end
    end
end
;
if (keyword_set(struct)) then begin	;added 29-Apr-93
    off = x
    if (n_elements(ref_tim) eq 0) then x0 = anytim2ints(index(0)) else x0 = anytim2ints(ref_tim)
    x = replicate(x0, nx, n)
    for i=0,n-1 do x(*,i) = anytim2ints(x(*,i), off=off(*,i))
    ;;x = anytim2ints(x, off=off)	;cannot do because ANYTIM2INTS does not handle 2-D input arrays properly
end
;
return, x
end






