function ssw_make_query,t0,t1,_extra=_extra, structure=structure, debug=debug, $
   minute_window=minute_window, hour_window=hour_window, day_window=day_window,$
   in_query=in_query  ; << allow verbatim url encoded query string, or query-file (like for completed cutout_service tweaking
;
;+
;   Name: ssw_make_query
;
;   Purpose: construct a cgi-friendly query via SSW stds & keyword inherit
;
;   Input Parameters:
;      t0,t1 - optional positional STARTTIME & ENDTIME (or use keywords..)
;
;   Keyword Parameters:
;      _extra - undefined keywords assumed query name/value pairs:
;         <keyword>=<value> [,etc...] -> KEYWORD=<urlencoded value> [& etc...]
;      structure - (input) if supplied, use This as query starting template
;                  If supplied, will use those verbatim ; will Add tag=value or Modify via _extra 
;      in_query - (input) - optional Input query, query string -or- query file
;; serves same purpose as structure - serves as starting template
;                           (method: if in_query defined, transforms -> structure via url_decode(in_query)
;      minute_window,hour_window,day_window - optionally user-desired time range expand (only applied to positional t0,t1
;
;   Calling Example:
;      IDL> t0=reltime(/now,out='ccsds')
;      IDL> print,ssw_make_query(date_obs=t0,p2="blah blah", p3=[1,2])
;
;   History:
;      Dec 2009 - S.L.Freeland 
;      26-jan-2023 - S.L.Freeland - allow input/keyword  structure -and- _extra
;                    internally _extra renamed extra (dont clobber input)
;                    if _extra & <structure> - concat/"join"  -> extra
;      28-jan-2023   Be less positional 't0,t1' centric for STRUCTURE input - heritage artificat
;                    add IN_QUERY - query string -or- query_file in lieu of input STRUCTURE
;      29-jan-2023   "online" but still in progress - did not want to redo All this version of 
;                    ssw_make_query.pro recast yet - not under 'idl' & external name NE internal name
;                    Quite a bit of though/work, so off-site backup via distribution;  should be done ~tomorrow...
;       8-jun-2023   S.L.Freeland - Beta released/online ssw_make_query2.pro (was beta) -> Now ssw_make_query.pro  
;                    
;-
version='2.0'               ; V2.0 major recast - Jan 2023 ; per doc history above
debug=keyword_set(debug)

if data_chk(_extra,/struct) then extra=_extra ; user <param>=<value>
extra=add_tag(extra,trim(version),'ssw_make_query_ver') 

; heritage - HER specific check&addition; for backward compatibility; add often used 'ref_helio' string {N,S}lon{E,W}lat
if required_tags(extra,'hgs_x,hgs_y') then begin 
   extra=add_tag(extra,ssw_helio2string(float([hgs_x,hgs_y])),'ref_helio')
endif
 
if ~data_chk(query,/string) then query=''

if valid_time(t0) then begin
   if n_params() eq 1 then t1=t0  ; assure valid input for user-optional time window
   time_window,[anytim(t0,/yoh),anytim(t1,/yoh)],minute_window=minute_window,$
      hour_window=hour_window,day_window=day_window, time0, time1
   extra=add_tag(extra,time0,'starttime')
   extra=add_tag(extra,time1,'endtime')
;   query='starttime='+url_encode(anytim(time0,/yohkoh,/trunc)) + '&endtime=' + $
;        url_encode(anytim(time1,/yohkoh,/trunc)) ; SLF 29-jan-2023 - just add them to EXTRA, so piggy back on url_encoding below
endif

etn=tag_names(extra)
; input  <structure> options 
case 1 of 
   data_chk(structure,/struct):  ; user input STRUCTURE
   file_exist(in_query)       :  structure=url_decode(qfile=in_query)  ; query-file input
   data_chk(in_query,/string) :  structure=url_decode(in_query)        ; query string input
   else: structure=extra
endcase
qstruct=structure ; don't mutate the immutable (input structure - build this from STRUCTURE + optional _EXTRA mods or added tags
; 1st update STRUCTRE from EXTRA (aka _EXTRA input)
ssqs_index=tag_index(qstruct,etn) ; ind of QSTRUCT tags also present [_]EXTRA (MODIFY); -1 implies ADD new tag
sfuncs=(['add_tag','rep_tag_value'])(ssqs_index ge 0)
for et=0,n_tags(extra)-1 do qstruct=call_function(sfuncs[et],qstruct,extra.(et),etn[et]) 
stn=tag_names(qstruct)
if debug then stop,'ssqs'

for i=0,n_tags(qstruct)-1 do begin ; structure, including MODS/ADDItiONS -> query string
   query=query+(['','&'])(query ne '') +stn[i]+'='+url_encode(strtrim(arr2str(qstruct.(i)),2))
endfor
if debug then stop,'query,qstruct'
return,query
end

