;+
;
; file UVSP_CAT.PRO -
;
; (UVSP_CAT) provides an input platform to accept parameters
;      either terminal (UVSP_SCREEN) 
;	   or widget   (UVSP_CATWIDGET) based.
; checks it for validity (UVSP_CATPARAM->UVSP_CATPARSE), 
; displays a condensed version of any valid user input, 
;      either terminal (UVSP_SHOW) 
;	   or widget   (UVSP_CATWIDGET)
; searches the catalog using input parameters (UVSP_CATREAD) and
;	proceeds to output qualifing experiments (UVSP_CATPRINT
;       [->UVSP_CATDISPLAY for widgets])
;
; (UVSP_CATRESET) allow user to go back to default values
; (UVSP_CATHELP)  provides help for terminal version, widget version
;       provides continuous help (UVSP_CATWIDGET subroutine WIDGET_INPUT)
;
;
; NAME: 
;	UVSP_CAT.PRO
;
; PURPOSE:
;       Sets up initial conditions for the catalog search program, an directs
;	program control to the appropriate interface.
;
; CATEGORY:
;	UVSP Catalog
;
; CALLING SEQUENCE:
;	result = uvsp_cat( screen=screen, font=font, nofull=nofull, $
;	             reset=reset, widget_handler=widget_handler, $
;		     widget_group=widget_group, widget_modal=widget_modal)
;
; CALLED BY:
;	none
;
; CALLS TO:
;	UVSP_CATRESET, UVSP_CATWIDGET, UVSP_CATSCREEN, CHECKFONT
;
; INPUTS:
;       none 
;
; OPTIONAL INPUTS:
;	SCREEN	:   If set, then the interface is in screen, not widget, mode.
;		    By default, if widgets are available then widgets will be 
;		    used. 
;
;	FONT	:   The hardware font to be used by the program's widgets,
;		    when executing in widget mode.  May be selected during 
;		    program execution, for any future widgets created.
;		    Wildcards are allowed.  Ignored in screen mode.
;
;	ALPHAFONT : When set select a small font.  Alpha IDL placed big gaps
;		    between lines, therfore the widget goes off the bottom.
;		    This keyword selects a font size about 12.  This keyword
;		    is ignored if the FONT keyword is set and by screen mode.
;
;	NOFULL	:   By default the entire catalog is read into memory.
;		    If this keyword is set then only the maximum extent of
;		    the experiment range is placed in memory.  If you select
;		    the experiments 1 and 99771, then the entire catalog is 
;		    placed in memory in order to access just those two
;		    experiments.  Once the whole catalog is in memory,
;		    subsequent program calls ingore the keyword /NOFULL.
;
;	NORESET :   By default the default parameters are reset to all possible
;		    values.  If set, then your last parameter values are 
;		    retained as the current settings.  Note that "SPEC" means 
;		    spectra only, so that is set to NO.
;		    
;	The following only apply to widget mode, and are ignored in screen mode.
;	These are meaningful only when this program is called by another widget
;	routine.
;
;	WIDGET_HANDLER  :  The name of the event handler to be used to handle
;			   the events produced by the "Array" or "Screen" 
;			   output selections.  If not present, the only event
;			   handled from these widgets is the CANCEL button.
;
;	WIDGET_GROUP	:  The widget ID of a group leader, which, when killed,
;			   kills the catalog program's widgets.  
;   
;	WIDGET_MODAL	:  The MODAL condition to be set for the final output
;			   widget listing the qualifing experiments.
;
; OUTPUTS:
;	Returns the result (CAT_TEXT). 
;	CAT_TEXT is null when no experiment qualify to meet the parameter.
;	CAT_TEXT is a long array of numbers when "NUMBERS" is selected in 
;		 screen mode or nothing is select in widget mode.
;	CAT_TEXT is the calalog output when, output condition "ARRAY"
;		 is selected.
;	CAT_TEXT is null for any other cases.
;
; OPTIONAL OUTPUTS:
;       none 
;
; COMMON BLOCKS:
;	COMMON UVCATTEXT, CAT_TEXT   	; The string array of entries,
;					;    returned from the event handler.
;	COMMON UVCATSTRUC, STR, OUT	; Current parameter settings (STR),
;					;    and parameter flags (OUT).
;	COMMON MYFONT, MYFONT		; Saves for next time.
;	COMMON UVCAT, CAT               ; The part of catalog in memory.
;
; SIDE EFFECTS:
;	If /NOFULL is not set, which is the case when you want the full catalog
;	to reside in memory at one time, then a great deal of memory is used.
;	This memory is not returned until you exit IDL or type 
;	COMMON UVCAT, CAT and then DELVAR, CAT.
;	
;
; RESTRICTIONS:
;	If this program is executing in widget mode then IDL must be version
;	3.0.0 or better.  The widget keyword font is used.  If the program is
;	executing in screen mode then this restriction is meaningless.
;
;	If /NOFULL is not set, which is the case when you want the full catalog
;	to reside in memory at one time, then you must have a enough for the 
;	whole UVSP catalog.
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	oct 93 - elaine einfalt (HSTX)
;	oct 93 - added /ALPHAFONT keyword and call to CHECKFONT
;	                          
;-

@uvsp_catdmo
@uvsp_cathelp
@uvsp_catparse
@uvsp_catradtht
@uvsp_catprint
@uvsp_catread
@uvsp_catparam
@uvsp_catreset
@uvsp_catscreen


function uvsp_cat, screen=screen, font=font, nofull=nofull, noreset=noreset, $
	             widget_handler=widget_handler, alphafont=alphafont,   $
		     widget_group=widget_group, widget_modal=widget_modal


common uvcattext, cat_text   			; the string array of entries
common uvcatstruc, str, out			; current parameter settings
common myfont, myfont				; save for next time
common uvcat, cat                               ; part of catalog in memory

 cat_text = ''
 
 if n_elements(nofull) eq 0 then nofull = 0	; default to full catalog
						; if present, then will be set
						; to LUN of catalog file for 
						; later ASSOC

 if (!d.flags and 65536) ne 0 then widget = 1 $         ; widgets are available
                              else widget = 0           ; must use screen menu

 if keyword_set(screen) then if (screen) then widget=0 	; use screen version

 if (widget) then $					; fonts only w/ widgets
   if (fix(strmid(!version.release,0,1)) ge 3) $
    then begin                                              ; IDL 3.0 widgets
;         if n_elements(font) ne 0 then myfont=font  $       ; use passed in font
;	   else if n_elements(myfont) eq 0 then myfont=''   ; no font in memory

         if n_elements(font) ne 0 then begin		; user passed in font

		myfont = checkfont(font=font, message = $
			'Your selected font does not exist on this machine.'+ $
		        '!C!CSelect a font or cancel.')

         endif else if keyword_set(alphafont) then begin
                     ; pick a small font from a list of little fonts
		     pick_me = [ $
				'*courier*med*-r-normal--14-140*', $
				'*fixed*med*-r-normal--14-110*',   $
				'*fixed*med*-r-normal--13*']

		    myfont = checkfont(font=pick_me, message= $
			'You need to select a small sized font in order ' + $
	                '!C!Cthe full widget to fit. Select a font or cancel.')
			 
         endif 

;12/93         endif else if not(keyword_set(alphafont)) then myfont='' $
;		else begin
;                     ; pick a small font from a list of little fonts
;		     pick_me = [ $
;				'*courier*med*-r-normal--14-140*', $
;				'*fixed*med*-r-normal--14-110*',   $
;				'*fixed*med*-r-normal--13*']
;
;		    myfont = checkfont(font=pick_me, message= $
;			'You need to select a small sized font in order ' + $
;	                '!C!Cthe full widget to fit. Select a font or cancel.')
;			 
;		endelse

         if n_elements(myfont) eq 0 then myfont=''   ; no font in memory
	 if myfont eq '' then print, 'Widget font is unchanged.'

    endif else begin                                        ; widgets need 3.0
	    print,'Sorry, must have IDL Version 3.0 or better.'
	    print,'Switching to terminal mode.'
	    widget = 0
    endelse

 ;
 ; Go get the UVSP Catalog, but only if it isn't already in memory.
 ; If /NOFULL is set, but the full catalog is already in memory from 
 ; some other pass, then treat this as a "FULL" (ignore nofull).
 ;

 if n_elements(cat) lt 64L*99771  then begin   ; don't already have full catalog
					       ; in memory
    ;
    ; Get the location of the UVSP catalog
    ;

    if !version.os eq 'vms' $                   	
	then dataplace = getenv('UVSP$CATALOG') $      ; VMS logical definition
	else dataplace = getenv('UVSP_CATALOG')        ; UNIX environment name

    if dataplace eq '' then read,'Enter full UVSP Catalog file name ', $
				 '(with disk location) : ',dataplace

    if strpos(strupcase(dataplace), 'CATALOG.CAT') eq -1 $ ; only have disk, so
       then dataplace = dataplace + 'CATALOG.CAT'          ; add the filename

    openr, unit, dataplace, error=err, /get_lun       ; open the catalog
    if err ne 0 then begin                            ; inform user and bail
       print, 'UVSPCAT - ',!err_string
       return,0
    endif

    if not(nofull) then begin 		; yes, get full catalog now
       ; 
       ; Associate file to an array
       ;

       print,'Wait a moment while the UVSP catalog is accessed (', $
   	      strmid(!stime,12,8),').'

       cat_assoc = assoc(unit, intarr(64,99771)) 

       ;
       ; Yes, user wants the whole catalog in memory.
       ; If nofull is set, then look in UVSP_CATREAD for this assignment.
       ; 
       print,'Wait moment while the UVSP catalog is placed ', $
		'in memory (', strmid(!stime,12,8),').'

       cat = cat_assoc(0)                          ; catalog array (64, 99771)

       free_lun, unit				   ; close file 
       nofull = 0

       print,'Thank you (', strmid(!stime,12,8),').'


    endif else nofull = unit		; The logical unit for catalog file.
					; File stays open until:
					;   screen mode - bottom this program
					;   widget mode - the "EXIT" button is
					;	pressed in UVSPCAT_EVENT
					; Will do the ASSOC, in UVSP_CATREAD


 endif else nofull = 0 		; Catalog already here, so don't open again.
                                ; Zero is LUN for standard input (terminal)
				; so it won't be considered a valid file 
				; LUN when we get to UVSP_CATREAD.

 if n_elements(str) eq 0 or n_elements(out) eq 0 $   ; no values yet
    then uvsp_catreset, str=str, out=out $ 		; get defaults
 else if not(keyword_set(noreset)) then uvsp_catreset, str=str, out=out  

 ;
 ; Call to the correct user interface
 ;

 if (widget) then begin
    ;
    ; For easy passing, load pre-existting widget info into a structure
    ; See UVSP_CATDISPLAY.PRO before changing.
    ;

    cat_widg = {uvwidg, handler:'CATDIS_EVENT', group:'', modal:0}
    if n_elements(widget_handler) ne 0 then cat_widg.handler = widget_handler
    if n_elements(widget_group) ne 0   then cat_widg.group = widget_group
    if n_elements(widget_modal) ne 0   then cat_widg.modal = widget_modal
	
    uvsp_catwidget, str=str, out=out, nofull=nofull, cat_widg=cat_widg 	

 endif else begin
	uvsp_catscreen, str=str, out=out, nofull=nofull	; screen menu

	if (nofull ne 0) then free_lun, unit		; done with catalog
 endelse

return, cat_text
end

