;+
;
; NAME: 
;  	UVSP_CATPARAM
;
; PURPOSE:
;       Assign the user's choice of parameter inputs to the structure, "STR".
;
; CATEGORY:
;       UVSP Catalog
;
; CALLING SEQUENCE:
;       UVSP_CATPARAM, str=str, out=out
;
; CALLED BY:
;       UVSP_CATSCREEN, UVSP_CATWIDGET
;
; CALLS TO:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;
;-

pro uvsp_catparam, str=str, out=out, input=input, legal=legal, nofull=nofull

 legal = 0                          		; assume user input junk

 input = strcompress(strtrim(input,2))   	; a request for modification
 incase = strmid(input, 0, 3)  			; only 3 char. min. required
 len_input = strlen(input) 			; how much did the user input

 com_only = ['RAN', 'NAM', 'TYP', 'SLI', 'DET', 'XPI', 'YPI', 'XRE',    $
	     'YRE', 'SPE', 'ANG', 'WLD', 'SHI', 'INT', 'POL',           $
 	     'NPO', 'DUR', 'RSU', 'NOA', 'MAN',				$
             'NAME', 'TYPE', 'SLIT', 'XPIX', 'YPIX', 'XRES', 'YRES',    $
             'SPEC', 'ANGS', 'SHIF', 'INTV', 'NPOL', 'RSUN', 'NOAA',    $
	     'SHIFT', 'INTVL' ]

 ;
 ; If only the command was entered then prompt user for new parameter,
 ; else parse off the parameter part of the users input.
 ;

 if (where(com_only eq input))(0) ne -1 $    		; no value entered	
 then read,'Enter your '+ incase+ ' value > ',input $ 	; ask for a value
 else begin 				                ; value was included

    delim = where(byte(input) eq 32b or byte(input) eq 44b $
		or (byte(input) eq 61b)) 	; blank or comma or equal


    if delim(0) eq -1 then begin	; no delimeter
         print,'ERROR - Invalid command, "',input,'".'
         return
    endif else begin 	; ok there is a delimeter, was the command legal
        if (where(com_only eq strmid(input,0,delim(0))))(0) eq -1 then begin
            print,'ERROR - Invalid command, "',strmid(input,0,delim(0)),'".'
            return
        endif
    endelse

    input = strmid(input, delim(0)+1, 999)	; just the parameter values
 endelse

 new_param = strcompress(strtrim(input,2)) 	; just the parameter part

 case incase of
    'RAN' : begin  		; it's a range but is it time or number
	       ;
	       ; This range input is assumed to be a time if there is a
	       ; colon or letter (letter W, L, D are ignored for !WLD,
	       ;			 A, E, R, O for !AERO).
	       ;

	       bline = byte(new_param)            
	       is_time = where( bline eq 58b or $     		     ; ":"
			(bline ge 98b and bline le 99b) or     $  ; b-c
			(bline ge 102b and bline le 107b) or   $  ; f-k
			(bline ge 109b and bline le 110b) or   $  ; m-n
			(bline ge 112b and bline le 113b) or   $  ; p-q
			(bline ge 115b and bline le 118b) or   $  ; s-v
			(bline ge 120b and bline le 122b) or   $  ; x-z
			(bline ge 66b and bline le 67b)   or $  ; B-C
			(bline ge 70b and bline le 75b)   or $  ; F-K
			(bline ge 77b and bline le 78b)   or $  ; M-N
			(bline ge 80b and bline le 81b)   or $  ; P-Q
			(bline ge 83b and bline le 86b)   or $  ; S-V
			(bline ge 88b and bline le 90b) )       ; X-Z


	       rmin = 1   &   rmax=99771

	       if is_time(0) ne -1 then begin    ; letters or ":" indicate time 
                  
		  ;
		  ; Turn new_param of times into experiment number
		  ;

		  exp_ran = uvsp_cat_ptime(new_param=new_param, nofull=nofull)

		  if exp_ran eq '' then begin 
		  	print,'No valid values input, RAN is unchanged.' 
			return
		  endif else if exp_ran eq 'none' then begin
		  	print,'*** WARNING *** No experiments in time range.'+$
				' RAN is unchanged.'
        		return
		  endif
	
	          legal = uvsp_catparse(new_param=exp_ran, rmin=rmin,rmax=rmax)

	          if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.range_flag = 0         ; now using time not exp #
		     str.tim = new_param        ; cleaned up version for "SHOW"
		     str.exp = exp_ran          ; UVSP_CATREAD only uses exp #

		     out.ran_min = rmin-1  	; -1 to get index ranges
		     out.ran_max = rmax-1	; -1 to get index ranges
	          endif else print,'No valid values input, RAN is unchanged.' 

	       endif else begin           	 ; no "/" or ":" not time 

	          legal = uvsp_catparse(new_param=new_param,rmin=rmin,rmax=rmax)

	          if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.range_flag = 1         ; now using exp # not time
		     str.exp = new_param        ; cleaned up version for "SHOW"

		     out.ran_min = rmin-1  	; -1 to get index ranges
		     out.ran_max = rmax-1	; -1 to get index ranges
	          endif else print,'No valid values input, RAN is unchanged.' 
	       endelse

	    end

    'NAM' : begin

	     new_param = strupcase(new_param)
	     legal = 1				; user may enter anything
	     str.name = strcompress(new_param,/remove)
	     out.name = 1b

            end

    'TYP' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=1, rmax=21)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.type = new_param       ; cleaned up version for "SHOW"
		     if str.type ne '1-21' then out.type=1b else out.type=0b 
		endif else print,'No valid values input, TYPE is unchanged.' 
	    end

    'SLI' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=1, rmax=22)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.slit = new_param       ; cleaned up version for "SHOW"
		     if str.slit ne '1-22' then out.slit=1b else out.slit=0b
		endif else print,'No valid values input, SLIT is unchanged.' 
	    end

    'DET' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=1, rmax=5)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.det = new_param        ; cleaned up version for "SHOW"
		     if str.det ne '1-5' then out.det=1b else out.det=0b
		endif else print,'No valid values input, DET is unchanged.' 
	    end

    'XPI' : begin
		; there are some cases of NX equal to 0 (13761)
		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=255)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.xpix = new_param       ; cleaned up version for "SHOW"
		     if str.xpix ne '0-255' then out.xpix=1b else out.xpix=0b
		endif else print,'No valid values input, XPIX is unchanged.' 
	    end

    'YPI' : begin
		; there are some cases of NY equal to 0 (13761)
		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=255)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.ypix = new_param       ; cleaned up version for "SHOW"
		     if str.ypix ne '0-255' then out.ypix=1b else out.ypix=0b
		endif else print,'No valid values input, YPIX is unchanged.' 
	    end

    'XRE' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=32)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.xres = new_param       ; cleaned up version for "SHOW"
		     if str.xres ne '0-32' then out.xres=1b else out.xres=0b
		endif else print,'No valid values input, XRES is unchanged.' 
	    end

    'YRE' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=32)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.yres = new_param       ; cleaned up version for "SHOW"
		     if str.yres ne '0-32' then out.yres=1b else out.yres=0b
		endif else print,'No valid values input, YRES is unchanged.' 
	    end

    'SPE' : begin      ; same as entering 0 or 1 for WLD
		legal = 1                       ; for widgets
		if (strupcase(strmid(new_param, 0, 1)) eq 'Y') then begin
		   str.spec = 'YES'
		   out.spec = 1b
		endif else begin
		   str.spec = 'NO'
		   out.spec = 0b
		endelse
	    end

    'ANG' : begin 

		legal = uvsp_catparse(new_param=new_param, rmin=1000, rmax=3600)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.angs = new_param       ; cleaned up version for "SHOW"
		   if str.angs ne '1000-3600' then out.angs=1b else out.angs=0b
		endif else print,'No valid values input, ANGS is unchanged.' 
	    end

    'WLD' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=7500)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.wld = new_param        ; cleaned up version for "SHOW"
		     if str.wld ne '0-7500' then out.wld=1b else out.wld=0b
		endif else print,'No valid values input, WLD is unchanged.' 
	    end

    'SHI' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=60)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.shft = new_param       ; cleaned up version for "SHOW"
		     if str.shft ne '0-60' then out.shft=1b else out.shft=0b
		endif else print,'No valid values input, SHIFT is unchanged.' 
	    end

    'INT' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=255)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.intvl = new_param      ; cleaned up version for "SHOW"
		     if str.intvl ne '0-255' then out.intvl=1b else out.intvl=0b
		endif else print,'No valid values input, INTVL is unchanged.' 
	    end

    'POL' : begin
		
		; turn any accidental zeroes into the letter "O"
		new_param = strupcase(new_param)
		wzero = where(byte(new_param) eq 48b, nzero) 	; any zeroes
		for i = 0, nzero-1 do strput, new_param, 'O', wzero(i)

                valid = ['A','B','O']	; only valid user inputs
                n_pol = ['1','2','0']	; catalog # value for polarimeter
		polar = ''

                ;
		; Are any of the valid pol. parameters in the user's input
                ;

		for i = 0, n_elements(valid)-1 do $
                   if (strpos(new_param, valid(i)) ne -1)  $ ; a valid input
		      and (strpos(polar, valid(i)) eq -1)  $ ; don't have, yet 
		   then polar = polar + ',' + valid(i)

		if strlen(polar) ne 0 then begin	
		   legal = 1				; for widgets
		   str.pol  = strmid(polar,1,99) 	; version for "SHOW"
		   if str.pol ne 'A,B,O' then out.pol=1b else out.pol=0b
		endif else print,'No valid values input, POL is unchanged.' 
	    end

    'NPO' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=16)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.npol = new_param       ; cleaned up version for "SHOW"
		     if str.npol ne '0-16' then out.npol=1b else out.npol=0b 
		endif else print,'No valid values input, NPOL is unchanged.' 
	    end

    'DUR' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=13000)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.dur = new_param        ; cleaned up version for "SHOW"
		     if str.dur  ne '0-13000' then out.dur=1b else out.dur=0b
		endif else print,'No valid values input, DUR is unchanged.' 
	    end

    'RSU' : begin

		legal = uvsp_catparse(new_param=new_param, $
					rmin=0.0, rmax=3.5, /floater)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		    str.rsun = new_param        ; cleaned up version for "SHOW"
		    if str.rsun ne '0.00-3.50' then out.rsun=1b else out.rsun=0b
		endif else print,'No valid values input, DUR is unchanged.' 
	    end

    'NOA' : begin

		legal = uvsp_catparse(new_param=new_param, rmin=0, rmax=5786)

		if legal gt 0 then begin 	; if parsed out any legal inputs
		     str.noaa = new_param       ; cleaned up version for "SHOW"
		     if str.noaa ne '0-16,2287-5786' then out.noaa=1b $
						     else out.noaa=0b
		endif else print,'No valid values input, NOAA is unchanged.' 
	    end

    'MAN' : begin
		legal = 1			  ; for widgets
		out.man = '(' + new_param + ')'   ; no checking performed
	    end

    else: print, 'ERROR - Invalid command, "',input,'".'

 endcase

return
end