Viewing contents of file '../idllib/iuedac/iuelib/pro/fdmx.pro'
;******************************************************************************
;+
;*NAME:
;
;	FDMX
;
;*CLASS:
;
;*CATEGORY:
;
;	NEWSIPS
;
;*PURPOSE:
;
;	Decipher IUE merged extracted spectrum fits files and return data.
;
;*CALLING SEQUENCE:
;
;	FDMX,FILENAME,ROW,SREC,EXTHD,APERTURE,wave,abs_flux,flags,sigma, $
;            bkgrd,net
;
;*PARAMETERS:
;
;	FILENAME  (REQ) (I) (0) (S)
;		The filename - including extension.
;
;	ROW	(REQ) (I) (0) (I)
;		The row to be read from binary table
;
;	SREC    (REQ) (I) (0) (I)
;		The starting data record for IUE3DRD
;
;	EXTHD 	(REQ) (I) (1) (S)
;               FITS header for binary table extension
;
;	APERTURE  (REQ) (O) (0) (S)
;		The aperture extracted.  
;
;	WAVE	(OPT) (O) (1) (D)
;		The wavelength vector.
;
;	ABS_FLUX  (OPT) (O) (1) (R)
;		The absolute flux vector.
;
;	FLAGS	(OPT) (O) (1) (I)
;		The NEWSIPS error flags vector.
;
;	SIGMA	(OPT) (O) (1) (R)
;		The sigma vector.
;
;	BKGRD	(OPT) (O) (1) (R)
;		The background flux vector.
;
;	NET	(OPT) (O) (1) (R)
;		The net flux vector.
;
;*EXAMPLES:
;
;       iuefhrd,filename,p,head,lab,exthd
;       srec = total(p(0:2))
;	rdmx,filename,1,srec,exthd,aper,wave,...
;
;*SYSTEM VARIABLES USED:
;
;	none
;
;*INTERACTIVE INPUT:
;
;*SUBROUTINES CALLED:
;
;	PARCHECK
;	STPAR
;       IUE3DRD
;
;*FILES USED:
;
;	filename given in calling sequence
;
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;	only tested with SWP data
;	not tested with high dispersion data
;
;*NOTES:
;       - Parameters for which no data are found are set to 0.
;       - Assumes first 4 fields are (in order) aperture,npts,
;         starting wavelength, and wavelength increment.
;
;*PROCEDURE:
;
;	IUE3DRD is used to read the data.  The number of fields is found 
;       via STPAR and the TTYPE keywords.  The data is assigned to the 
;       appropriate variable.
;
;*I_HELP  nn:
;
;*MODIFICATION HISTORY:
;
;	14 Jun 93  PJL  wrote DECIPHMX
;	15 Jun 93  PJL  set no values to defaults;  changed IFITSREAD to
;			IFITSRD
;	12 Aug 93  PJL  add /silent keyword to IFITSRD call
;       27 Dec 94  RWT  wrote FDMX (based on DECIPHMX) to speed up READMX 
;-
;******************************************************************************
 pro fdmx,filename,row,srec,exthd,aperture,wave,abs_flux,flags,sigma,bkgrd,net
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'FDMX,FILENAME,ROW,SREC,EXTHD,APERTURE,wave,abs_flux,flags, $
    print,' sigma,bkgrd,net'
    retall
 endif  ; npar eq 0
 parcheck,npar,indgen(7)+5,'FDMX'
;
; initialize output parameters to 0
;
wave = 0
abs_flux = 0
flags = 0
sigma = 0
bkgrd = 0
net = 0
;
; find field types & number of fields using TTYPE keywords
;
 stpar,exthd,'TTYPE*',field,err                   ; err = # of field entries
 if (err lt 4) or (err gt 9) then begin
    print,'Error extracting fields from FITS file'
    print,strtrim(err,2) + ' fields found in ' + strtrim(filename,2)
    print,'(Expecting between 4 and 9)'
    print,'ACTION:  Returning from FDMX.'
    retall 
 endif  ; err ne 0
 field = strtrim(field,2)
;
; read first 9 fields (assume first 4 entries are known)
;
 iue3drd,filename,row,srec,exthd,aperture,npts,w0,dw,f5,f6,f7,f8,f9,/silent
 wave = w0 + dindgen(npts) * dw
;
;  determine which remaining fields is which
;
 check = ['FLUX','QUALITY','SIGMA','BACKGROUND','NET']
 position = intarr(5)
;
 count = 4
 while (count lt err) do begin
    temp = where(check ne '',ct)
    if (ct gt 0) then begin
;
;  set variable
;
       case count of
           4  :  variable = f5
           5  :  variable = f6
           6  :  variable = f7
           7  :  variable = f8
           8  :  variable = f9
          else:  print,'This should not happen.'
       endcase  ; count
;
       temp = where(check eq field(count),ct)
       if (ct gt 0) then begin
             case check(temp(0)) of
                'FLUX'      :  abs_flux = variable
                'QUALITY'   :  flags = variable
                'SIGMA'     :  sigma = variable
                'BACKGROUND':  bkgrd = variable
                'NET'       :  net = variable
                else        :  print,'This should not happen.'
             endcase  ; check(temp(0))
             check(temp(0)) = ''
        endif else begin
             print,' '
             print,field(count)+ ' not in the list of fields to be recovered.'
             print,'ACTION:  Continuing.'
             print,' '
        endelse  ; ct gt 0
    endif else count = err
    count = count + 1
 endwhile  ; count lt err
;
;  all requested fields found?
;
 temp = where(check ne '',ct)
 if (ct ne 0) then begin
    print,' '
    print,'WARNING:  Missing following field(s) from '
    print,'          ' + strtrim(filename,2) + ':'
    print,'          ' + check(temp)
    print,'ACTION:  Continuing.'
    print,' '
 endif  ; ct ne 0
;
 return
 end  ; fdmx