Viewing contents of file '../idllib/iuedac/iuelib/pro/deciphmx.pro'
;******************************************************************************
;+
;*NAME:
;
;	DECIPHMX
;
;*CLASS:
;
;*CATEGORY:
;
;	NEWSIPS
;
;*PURPOSE:
;
;	Decipher IUE merged extracted spectrum image fits files and return the
;	aperture.  The wavelengths, calibrated flux, NEWSIPS flags, sigmas,
;	background flux, and net flux might be returned.
;
;*CALLING SEQUENCE:
;
;	DECIPHMX,FILENAME,ROW,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 for IFITSRD.
;
;	APERTURE  (REQ) (I) (0) (S)
;		The aperture extracted.  If an error occurs, it is set equal to
;		'ERROR'.

;	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:
;
;	deciphmx,filename,1,aper
;
;	deciphmx,filename,1,aper,wave,flux,flags,sigma,bkgrd,net
;
;*SYSTEM VARIABLES USED:
;
;	none
;
;*INTERACTIVE INPUT:
;
;*SUBROUTINES CALLED:
;
;	PARCHECK
;	CHKFITS
;	STPAR
;	IFITSRD
;
;*FILES USED:
;
;	filename given in calling sequence
;
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;	only tested with SWP data
;	not tested with high dispersion data
;
;*NOTES:
;
;*PROCEDURE:
;
;	Determines if file exists and if it is a fits file.  IFITSRD is used
;	to read the data.  The number of fields is found via STPAR and the
;	TFIELDS keyword.  The data is assigned to the appropriate variable.
;
;*I_HELP  nn:
;
;*MODIFICATION HISTORY:
;
;	14 Jun 93  PJL  wrote
;	15 Jun 93  PJL  set no values to defaults;  changed IFITSREAD to
;			IFITSRD
;	12 Aug 93  PJL  add /silent keyword to IFITSRD call
;
;-
;******************************************************************************
 pro deciphmx,filename,row,aperture,wave,abs_flux,flags,sigma,bkgrd,net
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'DECIPHMX,FILENAME,ROW,APERTURE,wave,abs_flux,flags,sigma,bkgrd,net'
    retall
 endif  ; npar eq 0
 parcheck,npar,indgen(7)+3,'DECIPHMX'
;
;  see if file exists
;
 temp = findfile(filename,count=ct)
 if (ct eq 0) then begin
    print,'Unable to find ' + filename
    print,'ACTION:  Returning
    retall
 endif  ; ct eq 0
;
;  check that the file in question is a fits file
;
 chkfits,filename,checked,/silent
 if (not (checked)) then begin
    print,filename + 'is not a fits file.'
    print,'ACTION: Returning.'
    aperture = 'ERROR'
    return
 endif  ; not (checked)
;
;  read data
;
 ifitsrd,filename,row,mfit,extn,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,   $
    f13,f14,f15,/silent
;
;  check number of fields
;
 stpar,extn,'TFIELDS',fldnum,err
 if (err ne 0) then begin
    print,'Error extracting number of fields via fits keyword TFIELDS.'
    print,'ACTION:  Returning.'
    aperture = 'ERROR'
    return
 endif  ; err ne 0
 if (fldnum gt 15) then begin
    print,' '
    print,strtrim(fldnum,2) + ' fields in ' + strtrim(filename,2)
    print,'Only first 15 have been extracted and will be examined.'
    print,' '
 endif  ; fldnum gt 15
;
;  determine which field is which
;
 check = ['APERTURE','NPOINTS','WAVELENGTH','DELTAW','FLUX','QUALITY',   $
          'SIGMA','BACKGROUND','NET']
 position = intarr(9)
 case npar of
    3: check(1:8) = ''
    4: check(4:8) = ''
    9:
    else:  check(npar:8) = ''
 endcase  ; npar
;
 count = 0
 while (count lt fldnum) do begin
    temp = where(check ne '',ct)
    if (ct gt 0) then begin
;
;  set variable
;
       case count of
           0  :  variable = f1
           1  :  variable = f2
           2  :  variable = f3
           3  :  variable = f4
           4  :  variable = f5
           5  :  variable = f6
           6  :  variable = f7
           7  :  variable = f8
           8  :  variable = f9
           9  :  variable = f10
          10  :  variable = f11
          11  :  variable = f12
          12  :  variable = f13
          13  :  variable = f14
          14  :  variable = f15
          else:  print,'This should not happen.'
       endcase  ; count
;
       stpar,extn,'TTYPE'+strtrim(count+1,2),field,err
       field = strtrim(field,2)
       if (err eq 0) then begin
          temp = where(check eq field,ct)
          if (ct gt 0) then begin
             case check(temp(0)) of
                'APERTURE'  :  aperture = variable
                'NPOINTS'   :  npts = variable
                'WAVELENGTH':  wavest = variable
                'DELTAW'    :  dwave = variable
                '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 + ' not in the list of fields to be recovered.'
             print,'ACTION:  Continuing.'
             print,' '
          endelse  ; ct gt 0
       endif else begin
          print,' '
          print,'Problem determining field name in fits keyword TTYPE' +    $
             strtrim(count+1,2)
          print,'ACTION:  Countinuing.'
          print,' '
       endelse  ; err eq 0
    endif else count = fldnum
    count = count + 1
 endwhile  ; count lt fldnum
;
;  all requested fields found?
;
 temp = where(check ne '',ct)
 if (ct ne 0) then begin
    print,' '
    print,'WARNING:  The following field(s) was (were) not found in '
    print,'          ' + strtrim(filename,2) + ':'
    print,'          ' + check(temp)
    print,'ACTION:  Continuing.'
    print,' '
 endif  ; ct ne 0
;
;  wavelength vector
;
 if (npar gt 3) then begin
    if (ct ne 0) then begin
       temp1 = where(temp eq 1,ct1)
       temp2 = where(temp eq 2,ct2)
       temp3 = where(temp eq 3,ct3)
       if ( (ct1 eq 0) and (ct2 eq 0) and (ct3 eq 0) ) then    $
          wave = wavest + dindgen(npts) * dwave    $
       else begin
          print,' '
          print,'Unable to obtain wavelength vector.'
          print,' '
          wave = 0
          check(1) = ''
          check(2) = ''
          check(3) = ''
       endelse  ; (ct1 eq 0) and (ct2 eq 0) and (ct3 eq 0)
    endif else wave = wavest + dindgen(npts) * dwave
 endif  ; npar gt 3
;
;  zero out the rest
;
 temp = where(check ne '',ct)
 if (ct gt 0) then begin
    for i = 0,ct-1 do begin
       case temp(i) of
          0:  aperture = 'UNKNOWN'
          1:  wave = 0
          2:  wave = 0
          3:  wave = 0
          4:  abs_flux = 0
          5:  flags = 0
          6:  sigma = 0
          7:  bkgrd = 0
          8:  net = 0
          else:  print,'This should not happen.'
       endcase  ; temp(i)
    endfor  ; i
 endif  ; count gt 0
;
 return
 end  ; readmx