      SUBROUTINE OPENER(isoor,m,ireqpars,ns,ih2)
 
C     OPENER DECIDES WHICH FILES NEED TO BE OPENED
C     BASED ON USER INPUT AND OPENS THEM.
C     THE BASIC FILE NAME CONFIGURATION IS AS FOLLOWS:
C     /MAIN DIRECTORY BRANCH/PROBETYPE/'OBSERVED' OR 'STANDARD'/
C      PARAMETERNAME.  THE MAIN DIRECTORY BRANCH IS HARDWIRED
C     IN C SUBROUTINE FILEOPEN, THE PROBETYPE AND PARAMETERNAME
C     ARE GLOBAL C VARIABLES, THE CORRECT ONE BEING ACCESSED
C     ACCORDING TO PROBE AND PARAMETER CODES.  OBSERVED OR 
C     STANDARD IS BASED ON PARAMETER ISOOR.  FILES ARE 
C     UNIFORMILY NUMBERED IN THE OCL SYSTEM. 0 IS FOR DEPTH
C     FILE, 1 IS FOR HEADER FILE, THE PARAMETER FILES ARE NUMBERED
C     BASED ON THE PARAMETER CODE.  IF TWO SETS OF FILES ARE
C     OPENED, THE NUMBERS ARE SEPERATED BY A FACTOR BASED ON 
C     THE NUMBER OF REQUESTED PARAMETERS. 

c-----------------------------------------------------------
c
c     Subroutine Steps:
c
c      1. calculate nselect, based on file set number and 
c         number of desired parameters
c      2. open depth file if observed levels are being
c         investigated
c      3. open parameter files
c      4. open header file
c      5. open second header file
c
c------------------------------------------------------------

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Passed Variables:
c
c      isoor - observed (0) or standard (1) levels
c      m - probe code
c      ireqpars - number of requested parameters
c      ns - set number, usually one, unless more than one
c           probe type (or the same probe type twice) are 
c           being investigated.
c      ih2 - special file opening instructions.
c            -2 - no second header, no header, no depth
c            -1 - second header, no header, no depth
c             0 - no second header, header, depth
c             1 - second header, header, depth
c             2 - no second header, header, no depth
c             3 - no second header, no header, depth
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Parameters:
c
c      maxlevel - maximum number of depth levels
c      nprobe - maximum number of proves the system can accomodate.
c      maxparm - maximum number of measured parameters
c      maxcalc - maximum number of measured and calculated parameters
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      parameter (maxlevel=6000, nprobe=100, maxparm=100, maxcalc=200)

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Character Variable:
c
c      filename - constructed file name
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      character*11 ctype(nprobe)
      character*80 filename
      
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Common Blocks:
c
c      reqps - contains ip2, the requested parameter codes
c      extra - contains ireqpars2 which is the number of 
c              requested calculated parameters.  Also contains
c              ireqsecond, which is number of second header
c              parameters, and ireqbio, which is number of
c              biological parameters
c      twoparm - contains itwo.  If itwo is greater than zero,
c                a special data type is associated with this
c                parameter.  An itwo of one is a second header,
c                of two is biological data.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      
      common /reqps/ ip2(0:maxlevel)
      common /extra/ ireqpars2,ireqsecond,ireqbio
      common /twoparm/ itwo(0:maxparm)
      common /probes/ ctype
 

      do iclr=1,80 !- LINUX clearing
       filename(iclr:iclr) = " "
      enddo      

      ifn=0
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Set the file set factor:  This factor seperates file numbers
c     for different sets of files being opened in the same 
c     program.  This is necessary since file numbers are set 
c     according to order of parameter requests.  The factor
c     (nselect) is twice the number of requested parameters 
c     ( to include observed and standard files ) * the set number + 
c     3* the set number to accomodate depth, header and
c     second header files.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
      ireqtot = ireqpars
      nselect = (2 * ireqtot * (ns - 1)) + 3*( ns - 1)
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     If observed levels are being investigated  
c     then open depth file, (if either depth
c     is specifically requested by parameter code -1, or if 
c     more than just header information is being investigated
c     (ireqtots greater than zero)).
c
c     nameother sets the file name for headers and depth files
c     taking the name (with C end character array marker ('\0')
c     and the probe code and returning a constructed filename.
c
c     fileopen takes the constructed file name, an opener code
c     and the file number and opens the correct file
c
c     the prefered file code is rb+.  This opens a file for
c     binary reading and writing.
c
c     depth files are file number zero
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      if (isoor.lt.1) then
       if ( ireqtot .gt. 0 .or. ip2(1) .eq. -1) then
        
        if ( ih2 .ge. 0 .and. ih2.ne.2 ) then
         
         call nameother('depths'//CHAR(0),m,filename)
         call fileopen( filename,'rb+'//CHAR(0),nselect+0)
         
        endif
        
       endif
      endif
      

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Run through the same procedure for each parameter
c
c     Note that calculated parameters are only opened on
c     standard levels, since they only exist on standard
c     levels.
c
c     namefile is the basic parameter file naming subroutine.
c
c      inputs are:
c
c      isoor - either zero for observed levels
c      or one for standard levels.     
c      i0 - parameter code
c      i - parameter order
c      m - probe number
c      ireqtot - number of requested measured and calculated
c                 parameters.
c
c      outputs are:
c
c      ifn - calculated file number
c      filename - constructed file number 
c
c      If this is a special parameter (itwo greater than zero),
c      only standard level data files are opened, even if 
c      observed levels are being investigated.  This is because
c      special data is not depth dependent.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      do 100 i=1,ireqtot

       i0 = ip2(i)
       
       if ( i0.lt.0 .or. itwo(i0).eq.1 ) goto 100
       
       if ( itwo(i0) .gt. 0 ) then
        iso=1
       else
        iso=isoor
       endif
       
       if ( i .le. ireqpars .or. isoor .eq. 1 ) then
        
        call namefile(iso,i0,i,m,ifn,ireqtot,filename)
        call fileopen( filename,'rb+'//CHAR(0),nselect+ifn)
        
       endif
       
 100  continue
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Open header file in the same manner as depth file
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  
      if ( ih2 .ge. 0 .and. ih2.ne. 3 ) then

       call nameother('headers'//CHAR(0),m,filename)
       call fileopen( filename,'rb+'//CHAR(0),nselect+1)
      endif

ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Open second header file in the same manner as depth file
c     if desired.  Header2 file number is fileset number (fselect)
c     + number of parameters *2 (for observed and standard) +
c     2
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      
      if ( abs(ih2) .eq.1 ) then

       call nameother('header2'//CHAR(0),m,filename)
       call fileopen( filename,'rb+'//CHAR(0),
     *  nselect+(2*ireqtot)+2)
       
      endif
      
      return
      end

