function grid_data, iindex , debug=debug, $
   seconds=seconds, minutes=minutes, hours=hours, days=days, $
   ss=ss, not_uniq=not_uniq, delta=delta, oindex=oindex, nsamp=nsamp, $
   match_times=match_times, grid_timesa=grid_times, cadence=cadence
;+
;   Name: grid_data
;
;   Purpose: return gridded subset of input (~fixed cadence if no gaps)
;
;   Input Parametrs:
;      index - generally, an index or roadmap array - any SSW standard time
;
;   Keyword Parameters:
;      seconds, hours, minutes, days (one) - desired cadence passed to timegrid.pro
;      not_uniq - if set, return duplicates (default is uniq matches)
;      oindex -   if set, return index(SS) - default is subscripts (SS)
;      delta (output) - delta seconds for data set/grid matches
;      match_times/grid_times (synonyms)  Optional User supplied grid - alternate times to match
;      cadence=cadence - optional string cadence - mimic JSOC covention "<VALUE><UNIT>"
;                        Where one-character <UNIT> in {s,m,h,d}
;
;   Output Parameters:
;      function returns SS vector of index records closest to grid times
;      if /OINDEX is set, return INDEX[SS]; ie, reduced structure array
;
;   Calling Examples:
;      gdata=grid_data(index, min=5)	       ;return SS vector, cad.=5minutes
;      gdata=grid_data(rmap, hour=6],/oindex)  ;return index(SS), cad.= 6 hours
;      evt_grid,grid_data(rmap,min=5,/oindex,tickpos=.9)  ; overlay on utplot
;      ss=grid_data(index,MATCH_TIMES=<User_grid_times>   ; Skips the TIMEGRID call
;      gdata=grid_data(index,Cadence='15m') ; Cadence like '2h' ~ HOURS=2 , 27.7d ~ DAYS=27.7 
;      
;
;   Method: calls timegrid.pro and tim2dset
;
;   History:
;      19-may-1995 (S.L.Freeland) - allow tasting (sampling) files at low cadence
;       5-mar-1997 (SLF) -> SSW
;      16-Sep-1998 - S.L.Freeland - add NSAMP and pass->timegrid
;      12-Nov-1998 - S.L.Freeland - put an anytim wrapper on input
;      18-sep-2000 - S.L.Freeland - added DAYS keyword (->timegrid)
;      26-oct-2009 - S.L.Freeland - require /NOT_UNIQ - default is uniq subset
;                                   (historically, function did not match do/intention)
;      15-dec-2024 - S.L.Freeland - add MATCH_TIMES & GRID_TIMES (synonyms) keyword and function
;                                   Add optional CADENCE ~ "jsoc-style" {5m, 12h, 27.2d ...etc}
;-
;


if n_params() eq 0 then begin 
   box_messge,['Need input times -AND-  A cadence OR number samples OR caller supplied MATCH_TIMES vector', $
               'IDL> ss=grid_data(index, [min=mins][hour=hour][nsamp=NN] )']
   return,-1
endif

index=anytim(iindex,/int)             ; standardize input via anytim

; Cadence check/handling - alternate cadence option - convert to heritage timegrid-like {minutes=10, days=27.2 ...etc }

if data_chk(cadence,/STRING) then begin ; JSOC Cadence Style <value><unit>
   lcadence=strlowcase(cadence)         ; simplify -> case Insensitive
   value=str2number(lcadence)
   unit=strlastchar(lcadence)
   case is_number(value) of    ; convert 'jsoc-style' cadence to heritage equivalent (so handled via heritage below)
      unit eq 's' : seconds=value
      unit eq 'm' : minutes=value
      unit eq 'h' : hours=value
      unit eq 'd' : days=value
      else: box_message,'No expected Cadence/time unit - valid units={s,m,h,d}'
   endcase
endif   ; 
   
s=keyword_set(seconds)
m=keyword_set(minutes)
h=keyword_set(hours)
d=keyword_set(days)

not_uniq=keyword_set(not_uniq) 

; SLF - removed some strange default message/action here - 15-dec-2024

unik=1-not_uniq
ndata=n_elements(index)

; make an evenly spaced grid at user specified granularity via <timegrid.pro>
; SLF  15-dec-2024 - Add User optional supplied  time tie-points via MATCH_TIMES aka GRID_TIMES

case 1 of 
   n_elements(match_times) gt 0 : outgrid = anytim(match_times,/int) ; User supplied time/grid tie points via MATCH_TIMES
   n_elements(grid_times)  gt 0 : outgrid = anytim(grid_times, /int) ; User supplied time/grid tie points via GRID_TIMES
   else: begin                  ; No user/caller supplied grid       ; Default grid is heritage time grid via timegrid.pro
      outgrid=timegrid(index(0),index(ndata-1), nsamp=nsamp, $
         sec=seconds, min=minutes, hour=hours, days=days)
   endcase
endcase

; now match index records sets
ssg=tim2dset(index,outgrid,delta=delta)

if unik then begin
   ussg=uniq(ssg)
   ssg=ssg(ussg)
   delta=delta(ussg)
endif

case 1 of 
   keyword_set(ss): retval=ssg
   keyword_set(oindex): retval=index(ssg)
   else: retval=ssg
endcase

return,retval
end
