function ssw_swpc_getdata, t0, t1, count=count, swpc_parent=swpc_parent , debug=debug,$
   _extra=_extra, no_strconvert=no_convert, verbatim=verbatim, $
   kp=kp, dst=dst, forecast=forecast, header=header
;
;+
;
;   Name: ssw_swpc_getdata
;
;   Purpose: return some Non-GOES SWPC products via json services (older method via SWPC ascii files depracated)
;
;   Input Parameters:
;      t0,t1 - optional user timerange; default is SWPC default most recent 7Days 
;              (as of now, User t0,t1 restricted to that most recent 7 day subrange)
;
;   Output:
;      function default return is SSW-ready structure vector (anytim/utplot ready times) - 
;         Data tag(s) are SWPC "product" dependent (est. KP or Dst for today)
;
;   Keyword Parameters:
;      Count (output) - # records returned
;      KP (switch) -  ,/kp  user wants Est KP
;      DST (switch) - ,/dst = Caller wants Dst
;  
;   Calling Sequence/Examples:
;      kp=ssw_swpc_getdata( [t0,t1],/KP)
;      dst=ssw_swpc_getdata( [t0,t1],/dst)
;
;  
;   History:
;      ~ 1-sep-2024 - S.L.Freeland - reinstate Latest Events Heritage values to follow SWPC text file->json service
;                                    (SWPC new SWPC paradigm ascii -> json/netCDF - doone for GOES products couple years ago ssw_get_goesdata.pro )
;      7-Jul-2026   - S.L.Freeland - update required due to "some" change in swpc json
;                                    (broken for some months due to swpc json KP change months ago..
;-;
     
;
no_struct=keyword_set(no_struct)          ; overrides default 'ssw structure vector' ; output is 2D array [ncolumns , nelements] 
struct=1-no_struct or n_params() gt 0     ; default is "ssw-ready" (structure-vector) e.g -> .utplot,anytim... 
debug=keyword_set(debug)
dst=keyword_set(dst)   ; return kyoto-dst
kp=1-dst               ; default is est KP
forecast=keyword_set(forecast)   ; swpc "forcast"  - as far as I know, 'forecast' only available for est. KP


if n_elements(swpc_parent) eq 0 then $
   swpc_parent='https://services.swpc.noaa.gov/products/' 

; user defined or implied swpc /products/
; https://services.swpc.noaa.gov/products/noaa-planetary-k-index-forecast.json
; https://services.swpc.noaa.gov/products/noaa-planetary-k-index.json
; https://services.swpc.noaa.gov/products/kyoto-dst.json
struct=1           ; SLF 7-July-2026 - made /STRUCT the default for least surprise
case 1 of 
    dst:   begin
       product='kyoto-dst'
       str_template={time_tag:'',dst:0.0}
    endcase

    kp:   begin
       product='noaa-planetary-k-index' + (['','-forecast'])(forecast)
       str_template={time_tag:'',kp:0.0,a_running:0, station_count:0}
    endcase
     
    else:  box_message,'Doubt you will see this message without hacking this code'
endcase

verbatim=keyword_set(verbatim) and ~struct
parse=1-verbatim
sock_list,swpc_parent + product + '.json',json
retval=json

if parse then begin
    ;retval=json_parse(json[0],/toarray)
    ;header=reform(retval[*,0])
    ;retval=retval[*,1:*] ; row0 is Header
    nout=data_chk(retval,/ny)               ;# items returned, 1:1 .TIME_TAG
    str_retval=ssw_list2structvect(json_parse(json,/tostruct),/verbatim)
    retval=str_retval                       ; new default 7-jul-2026 mod S.L.Freeland
    struct=1
    if struct then begin                    ;Default - convert array->structure (TBD issue with json_parse .TOARRAY/TOSTRUCT) 
     ;  str_retval=replicate(str_template,nout)
     ;  for t=0,n_tags(str_template)-1 do str_retval.(t) = reform(retval[t,*])
       retval=join_struct(str_retval,anytim(str_retval.time_tag,/mjd)) ; add SSW structure standard MJD times
       case 1 of
          n_params() eq 0:     ; No user time, so done
          else: begin
             if n_params() eq 1 then t0=reltime(t0,/days) ; no t1, so guess full day
             sse=ssw_time2epoch(retval,t0,t1)  ; times -> user defined epoch
             ssret=where(sse eq 0, ecnt)
             if ecnt eq 0 then box_message,'No SWPC entries witing your time range' else retval=retval[ssret]
          endcase
       endcase
     endif ; << end of structure define and optional time range trim
   if debug then  stop,'retval, header, nout,str_retval' 
    
endif else box_message,'Returning Verbatim SWPC KP/Dst  Json'

if debug then stop,'Before return, json,retval,parse'

return,retval
end
