Viewing contents of file '../idllib/iuedac/iuelib/pro/bgex.pro'
;****************************************************************************
;+
;*NAME:
;
;    	BGEX       (RDAF Production Library)     DEC. 12, 1986
; 
;*CLASS:
;
;    	Spectral Extraction
;
;*CATEGORY:
;
;*PURPOSE: 
;
;     	Procedure to run GEX1 and GEX2 procedures without user interaction.
;     	Parameters are input implicitly via the disk file GEX.TXT created
;     	by the routine PREGEX.PRO. For further information see GEX1 and
;     	GEX2 procedures.
; 
;*CALLING SEQUENCE:
;
;     	BGEX,FILENAME
; 
;*PARAMETERS:
;
;     	FILENAME   	(REQ) (I) (0) (S) 
;                   a string corresponding to the name of
;                   the data file containing image names and
;                   gex parameters (output from PREGEX) to be used
;                   as input to BGEX. If blank or null the default
;                   name 'gex.txt' is used.
;
;*EXAMPLES:
;
;     	To run bgex using gex.txt as the input file:
;     	   bgex,' '
;
;     	To run bgex using mygex.dat as the input file:
;     	   bgex,'mygex.dat'
;
;     	To create gex.txt with pregex (after displaying valid input files)
;      	and then running bgex:
;      	   pregex,0
;          bgex,' '
;
;	To run BGEX in the background (UNIX) outside of IDL on a file named 
;	'myfile.dat':
;	    if IUER_EXE defined (from earlier IDL session),
;		$IUER_EXE/gex.bat 'myfile.dat'
;	    if IUER_EXE is not defined,
;		/home/iuerdaf/iuelib/exe/gex.bat 'myfile.dat'
;	To run BGEX as a batch job (VMS) [the filename must be 'gex.txt']:
;		submit/noprint IUER_EXE:gex.bat
;
;*SYSTEM VARIABLES:
;
;	!err_string
;
;*INTERACTIVE INPUT:
;
;*SUBROUTINES CALLED:
;
;      	GEX2
;      	GEX1
;      	PARCHECK
;
;*FILES USED:
;
;    	FILENAME  [defaults to gex.txt] (I)
;    	        Input file of parameters created by PREGEX.  When running
;		BGEX on the VAX in batch mode, filename must equal gex.txt.
;
;    	IMAGET.TAB  (I/O)
;    	         Intermediate data set created by GEX1 and input to GEX2 in a
;    	         disk file with up to 200 records of 7 data pts;
;    	         data record 0 - header record contains 3 floating pt. values:
;    	                 1 - number of parameters per record (i.e., 7)
;    	                 2 - number of data points (i.e., NPT)
;    	                 3 - bin size (i.e., DELS)
;    	         data records 1 to NPT contain (in order):
;    	                  wavelength, center position, width (of fitted 
;			  Gaussian function), height, YINT (y-intercept for 
;			  linear background fit where F(bckgnd)=YINT+SLOPE*
;		 	  order #), SLOPE (slope for linear background fit 
;			  above), SIGMA (standard deviations from linear 
;			  background fit).
;
;    	IMAGET+'GX.SAV' (O)
;    	          (from GEX2) disk SAV file containing 2 records where record 1
;    	          contains the number of points in the H, W, F and E vectors
;    	          and record 2 contains the actual H, W, F, & E vectors.
;			
;
;*RESTRICTIONS:   
;
;	modified to run using unix/sun idl version 1.2
;
;		For batch mode on the VAX, the filename of the file created
;	by PREGEX must be gex.txt.  Also, all files must be in the user's
;	home directory.
;
;*SIDE EFFECTS:
;
;*NOTES:
;
;    	N output files are created for N input images.
;
;       For more information on "gex" routines see iuerdaf/manuals/gex.txt.
;
;	To run BGEX in the background (UNIX) outside of IDL:
;	    if IUER_EXE defined (from earlier IDL session),
;		$IUER_EXE/gex.bat filename
;	    if IUER_EXE is not defined,
;		/home/iuerdaf/iuelib/exe/gex.bat filename
;	To run BGEX as a batch job (VMS):
;		submit/noprint IUER_EXE:gex.bat
;
;	tested with IDL Version 2.2.0 (sunos sparc)	22 Nov 91
;	tested with IDL Version 2.2.0 (ultrix mispel)	22 Nov 91
;	tested with IDL Version 2.2.0 (vms vax)    	22 Nov 91
;
;*PROCEDURE:
;
;	Disk file - for which the default name is gex.txt - is used to input 
;	image file names, wavelength limits, and bin sizes. These parameters 
;	are displayed on the users terminal. Each image file is then submitted
;	to GEX1 and GEX2 producing the standard disk files output by these 
;	procedures.
;	
; 
;*MODIFICATION HISTORY:
;
;     	Programmer:   R. Thompson
;     	4-13-87 RWT VAX mods: add PARCHECK
;     	7-27-87 RWT modified to use new file name convention
;     	8-19-87 RWT use GET_LUN command
;     	1-15-88 RWT make IMN array longword integers
;     	3-08-88 HAA add RDAF Prolog
;     	5-09-88 RWT add reading CORDER parameter from GEX.DAT file
;       dec-19-89 jtb@gsfc modified for unix/sun idl
;       4-10-91 KBC modify filename structure based on operating system type
;	16- 7-91 PJL cleaned up; logical; tested on SUN and VAX; updated prolog
; 	16- 8-91 PJL modified to accepted files names with sm and lg in them
;		     and made the input file ascii; tested on SUN and VAX;
;		     updated prolog
;       23-10-91 PJL added to prolog
;       28-10-91 PJL corrected and added to prolog
;      22 Nov 91 GRA removed IUER_USERDATA logical; tested.
;      16 Dec 91 PJL corrected error in number of files equals 10 case
;      11 Mar 94 LLT Add code so this procedure can execute output of BATGEX.
;      29 APR 94 RWT use gex.txt as new default file name
;-
;************************************************************************
 pro bgex,fname
;
 npar = n_params()
 if npar eq 0 then begin
    print,'BGEX,FILENAME'
    retall
 endif  ; npar
 parcheck,npar,1,'BGEX'
;
 s = size(fname)
 if s(1) ne 7 then begin
    print,'filename must be quoted (null string for default)'
    retall
 endif  ; s(1)
 if fname eq '' or fname eq ' ' or fname eq '0' then fname = 'gex.txt'
;
; read disk file of parameters
;
 openr,/get_lun,un,fname,error=err
 if err ne 0 then begin
    printf,-2,!err_string
    retall
 endif  ; err
;
 n=0               ; force n to be integer
 line = strarr(1)
 readf,un,line     ; number of images to extract
if strmid(line(0),0,1) eq ';' then begin     ;File written by new pregex
    close,un
    if strlowcase(fname) ne 'gex.txt' then begin
       print,'BGEX can only read "gex.dat" (if produced by "pregex").'
       print,'If you have changed the name, you can type @name at the IDL'
       print,'prompt to execute the gex commands.'
       retall
    endif
@gex.txt  ;Included file: must be gex.txt in current directory.
endif else begin
 n = fix(strmid(line(0),36,3))
 im=strarr(n) 
 cam=intarr(n) & vn=cam & dela=cam & delb=cam & corder=cam
 wa1=fltarr(n) & wa2=wa1 & wb1=wa1 & wb2=wa1
 for i = 0,n-1 do begin
    readf,un,line
    cam(i) = fix(strmid(line,37,1))
    readf,un,line
    im(i) = strtrim(strmid(line,37,12),1)
    readf,un,line
    vn(i) = fix(strmid(line,37,3))
    readf,un,line
    dela(i) = fix(strmid(line,37,7))
    readf,un,line
    wa1(i) = float(strmid(line,37,7))
    readf,un,line
    wa2(i) = float(strmid(line,37,7))
    readf,un,line
    delb(i) = fix(strmid(line,37,7))
    readf,un,line
    wb1(i) = float(strmid(line,37,7))
    readf,un,line
    wb2(i) = float(strmid(line,37,7))
    readf,un,line
    corder(i) = fix(strmid(line,37,3))
 endfor  ; i
 free_lun,un
;
; list parameters read from gex.dat file
;
 print,' '
 print,'Images to be extracted:'
 print,' '
 print,form="(1x,t19,a,t46,a)",'----- gex1 -----','----- gex2 -----'
 print,form="(1x,2x,a,t17,a,t25,a,t33,a,t44,a,t52,a,t60,a,t69,a)", $
      'image','wmin','wmax','delw','wmin','wmax','delw','order'
 print,' '
 for i=0,n-1 do begin
    cm = strmid('lwplwrswpswr',3*cam(i)-3,3)
    if corder(i) ne 0 then $
       print, $
  form="(1x,a,1x,i5,t17,f6.1,t25,f6.1,t33,i3,t44,f6.1,t52,f6.1,t60,i3,t69,i3)" $
      ,cm,im(i),wa1(i),wa2(i),dela(i),wb1(i),wb2(i),delb(i),corder(i)  else    $
       print, $
  form="(1x,a,1x,i5,t17,f6.1,t25,f6.1,t33,i3,t44,f6.1,t52,f6.1,t60,i3,t69,a)"  $
          ,cm,im(i),wa1(i),wa2(i),dela(i),wb1(i),wb2(i),delb(i),'default'
 endfor  ; i
;
; start loop
;
;
 for i=0,n-1 do begin
   ;
   ; reconstruct file name(s)
   ;
    if cam(i) eq 3 then cmn='swp' 
    if cam(i) eq 2 then cmn='lwr'
    if cam(i) eq 1 then cmn='lwp'
    if vn(i) gt 0 then a=';'+strtrim(string(vn(i)),1) else a=''
    imt=cmn+im(i)+'.dat'+a
    print,' '
    print,'Beginning extraction for image ',imt
   ;
   ; call gex1 to determine psf and centers as a function of wavelength
   ; call gex2 to fit gaussians to x-cut and determine flux
   ;
    if corder(i) eq 0 then $
       gex1,imt,wa1(i),wa2(i),dela(i),wave,width,wcenter $
       else gex1,imt,wa1(i),wa2(i),dela(i),wave,width,wcenter,corder(i)
   ;
    gex2,imt,wb1(i),wb2(i),delb(i),wavelength,flux,err
 endfor  ; i
endelse ;File written by pregex.pro
;
 return
 end  ; bgex