      SUBROUTINE INPUT( npro, ireqpars, isoor )
      
C     INPUT IS THE MAIN INPUT PROGRAM FOR THE OCEAN CLIMATE
C     LABORATORY DATA ACCESS SYSTEM.  IT TAKES INFORMATION ON
C     THE PROBES AND PARAMETERS INCLUDED IN THE SYSTEM FROM
C     PROBEFILE.D.  IT GETS INFORMATION ON WHICH PROBES AND
C     PARAMETERS ARE BEING INVESTIGATED FROM CHOICEFILE.D. 
C     IT OBTAINS CALCULATED PARAMETER INFORMATION FROM 
C     CALCFILE.D.  ALL THIS INFORMATION IS SENT EITHER TO THE
C     MAIN PROGRAM, OR TO GLOBAL C VARIABLES, OR FORTRAN 
C     COMMON BLOCKS.  NECESSARY FILES FOR THIS SUBROUTINE TO
C     RUN PROPERLY ARE PROBEFILE.D, CHOICEFILE.D, AND CALCFILE.D.
C     
C     INPUT IS A UNIVERSAL INPUT PROGRAM FOR THE OCL SYSTEM
C     AND THEREFORE CONTAINS MUCH DATA THAT WILL NOT BE USED
C     IN MANY APPLICATIONS.

c------------------------------------------------------------
c
c     Subroutine Steps:
c
c      1. Set interpolation intervals
c         (FORTRAN subroutine setdel)
c      2. Set standard depths
c         (FORTRAN subroutine setstandeps)
c      3. Get overall information about parameters and probes
c         in the system from probefile.d.
c         (FORTRAN subroutine inprobe)
c      4. Get user input information on what probes and parameters
c         are to be investigated from probefile.d.
c         (FORTRAN subroutine inprobe)
c      5. Get user input information on calculated parameters from
c         probefile.d. (FORTRAN subroutine inprobe)
c      6. Set global C variables. (FORTRAN subroutine setglobal)
c
c------------------------------------------------------------

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c    Passed Variables:
c
c     npro - number of probes to be investigated in main program
c     ireqpars - number of parameters to be investigated
c     isoor - observed (0) or standard (1) level profiles to
c             be investigated
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c   Parameters:
c
c    kdim - number of standard depth levels
c    maxparm - maximum number of parameters
c    nprobe - maximum number of probes
c    maxcalc - maximum number of measured parameters plus
c              calculated parameters
c    maxlevel - maximum number of depth levels
c    maxcc - maximum number of NODC country codes
c    noffset - offset from biological (second header) data
c              for taxa (parameter specific) sets
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      parameter (kdim=40,maxparm=100,nprobe=100,maxcalc=200)
      parameter (maxlevel=6000,maxcc=200,noffset=200)
      parameter (maxkdim=137)
      
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c   Character Variables:
c
c    citf - 1-15 letter parameter names, used to name main files in
c           OCL directory structure.
c    citf2 - 1-6 letter parameter names, used to identify data in
c            written (ASCII) profiles.
c    ctype - 1-11 letter probe abbreviation, used to name directories
c            in OCL directory structure.
c    mtype - 1-3 letter parameter code, used for auxilary file names
c            for applications, such as analyzed data.
c    ncc - all NODC country codes
c
c    actionrec - string passed to mergelocking system
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      character citf*15,ctype*11,citf2*6,mtype*3
      character ncc*2
 
      character*15 actionrec
c     character*35 cPROGrec
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c    Arrays:
c
c     ip2 - numerical parameter codes (as enumerated in probefile.d)
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
      dimension ip2(0:maxlevel)

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c   Common Blocks:
c
c     setchoice - choices about which set of data (which probefile.d)
c                 to use.  Contains isetnum, the data set number 
c                 (0 corresponds to probefile.d, 1 to probefile1.d etc.)
c                 and isetadd, the number to add to parameter
c                 codes for this data set (for using two data sets
c                 simultaneously).
c     reqps - contains ip2, parameter codes for requested parameters
c     dels - inside (del) and outside (del2) limits for 
c            distances from a standard depth at which a value
c            may be used for interpolation to standard levels.
c     levels - contains standeps, depth (meters) of standard levels
c     parnames - contains citf, 1-15 letter parameter names
c     parname4 - contains citf2, 1-6 letter parameter names
c                used in writing out individual profiles.
c     parname1 - contains mtype, 1-3 letter parameter code used
c               in making file names for analyses and other applications.
c     probes - contains ctype, 1-11 letter probe names
c     nopars - contains nopas, thenumber of parameters a probe
c               measures
c     partype - contains nprm, the parameters which 
c               a probe measures, in probefile.d
c     parmord - contains nprec, the order in which a parameter is
c               listed for a probe in probefile.d.
c     reqprobe - contains jp, probe code (from probefile.d)
c                of requested probes
c     allprobe - contains jpm, which holds probe codes for requested
c                probes, including 'special use' probes, which are
c                marked with a 'y' instead of an 'x'
c     totalparm - contains npatot, number of probes which measure
c                 each type of parameter
c     surrogate - contains isur, which type of measured parameter
c                 is used to look at calculated parameters.  For
c                 example, temperature records would be used
c                 to look at potential temperature values, while
c                 oxygen records would be used to look at
c                 AOU values.
c     extra - contains ireqpars2, number of calculated parameters
c             which are being investigated.  Also contains ireqsecond
c             which is number of requested second header parameters,
c             and ireqbio, which is number of requested biological
c             parameters.
c     eachparm - contains number of each type of parameter.
c                numpar - number of measure parameters
c                numcalc - number of calculated parameters
c                numsecond - number of second header parameters
c                numbio - number of biological parameters
c                numset - number of second header parameter specific
c                         variables
c                numtaxa - number of taxa variables
c                numtot2 - number of first possible second header
c                          parameter minus one
c                numtot3 - number of first possible biological
c                          parameter minus one
c                ntax - number of taxa in individual profile
c                numsecset - number of second header parameter specific
c                            sets
c     twoparm - contains itwo.  If itwo is equal to 2 for a
c               parameter, it is the biological parameter.  If itwo
c               is equal to 1, it is the second header parameter.
c
c     depthcon - contains ixbt, marks whether a probe is of the 
c                XBT type and must have its depths corrected.
c                To include a probe in this list, its probe name must
c                start with an 'X' or an 'x'.
c     totprobes - total number of probes
c     countryc - all NODC country codes
c     reflevs - contains nplevs, the number of reference levels,
c               and dz2, the actual reference levels
c     calctype - contains ipx, the actual parameter codes for
c                each calculated parameter
c
c     ilockact  - 0=casual, 1=read-only, 2=exclusive
c     ilocknum  - number of probes to lock-check against (usually "0")
c     actionrec - string passed to mergelocking system
c
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
     
      common /setchoice/ isetnum,isetadd,isetadd2
      common /reqps/ ip2
      common /dels/ del(0:kdim),del2(0:kdim)
      common /levels/ standeps(kdim)
      common /parnames/ citf(maxlevel)
      common /parname1/ mtype(maxlevel)
      common /parname4/ citf2(maxlevel)
      common /probes/ ctype(nprobe)
      common /nopars/ nopas(nprobe)
      common /partype/ nprm(nprobe,maxparm)
      common /parmord/ nprec(maxcalc,nprobe)
      common /reqprobe/ jp(0:nprobe)
      common /allprobe/ jpm(0:nprobe),npromax
      common /totalparms/ npatot(maxparm)
      common /surrogate/ isur(maxcalc)
      common /extra/ ireqpars2,ireqsecond,ireqbio
      common /eachparm/ numpar,numcalc,numsecond,numbio,
     *     numtaxa,numset,numtot2,numtot3,ntax,numsecset
      common /twoparm/ itwo(0:maxparm)
      common /depthcon/ ixbt(nprobe)
      common /totprobes/ nprotot
      common /countryc/ ncc(maxcc)
      common /reflevs/ nplevs,dz2(maxkdim)
      common /calctype/ ipx(maxcalc)
      common /iMlockACT/ ilockact
      common /iMlockNUM/ ilocknum
      common /cMlockTXT/ actionrec
      
      data nplevs,dz2/ 11, 0.,500.,1000.,1500.,2000.,2500.,
     *                 3000.,3500.,4000.,4500.,5000.,126*0./

      ireqex = 0 
      isetnum=0
      isetadd=0
      isetadd2=0

c***********************************************************
c
c   Check to see if an alternate database is requested
c   (value of ialterbase is >0)
c
c***********************************************************

c       write(6,*) 'mainb'
      call changemainbranch(ialterbase)
c       write(6,*) 'mainb'

c***********************************************************
c
c   Initialize file tracking
c
c    maxfile = maximum number of files
c
c***********************************************************

      call zeroftrack(maxfile)
c     write(6,*) '#1'

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c    Read in the standard depths which correspond to each
c    standard level. 
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      call setstandeps(standeps)
c     write(6,*) '#2'

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Set del and del2.  These are the limits of distance
c     from a standard level at which observed values exist
c     which may be used for interpolation to standard levels
c     del is for the two inside values, del2 for the two outside
c     values.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      call setdel(kdim,dz,del,del2)
c     write(6,*) '#3'

c*****************************************************
c
c    Set country codes
c
c*****************************************************

      call countrycode
c     write(6,*) '#4'

c***********************************************************
c
c     See which data set is being investigated
c
c***********************************************************

      iostat=0
      open(10,file='./setchoice.d',status='old',iostat=ios)
      if ( ios .eq. 0 ) then
       read(10,*,iostat=ios) isetnum
       close(10)
       if ( ios .ne. 0 ) isetnum=0
      endif

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Get probe information from probefile.d
c    
c      npro is the number of requested probes
c      nprotot is the number of probes in the system,
c      numax is the number of parameters presently in the system.
c
c     This is the information on the makeup of the OCL system,
c     what probes are in the system, what parameters are in the
c     system and which probes measure which parameters, along with
c     other minor information such as first year of probe use, maximum
c     depth a probe measures, course ranges for parameters.
c   
c     Also input here is information on the file structure fo the
c     OCL system, the four letter codes for probes and the 15 letter
c     names for parameters which make up the directory structure of
c     the system.  Minor coding is also found here, 4 letter parameter
c     abbreviations used in writing out profiles and 1 letter parameter
c     abbreviations used in making file names for later applications
c     such as analyses.
c
c
c     Common Blocks set here:
c
c      parnames - 1-15 letter parameter names
c      parname4 - 1-6 letter parameter names
c      parname1 - 1-3 letter parameter names
c      probes - 1-11 letter probe names
c      nopas - number of parameters a probe measures
c      partype - order of recording of a parameter for each probe
c      totalparms - total number of probes which record a parameter
c      depthcon - probes which need the XBT depth correction
c
c
c     Also get user input from probefile.d.  This consists of whether
c     the user wants to investigate standard or observed levels,
c     and what parameters and probes are to be investigated.
c
c     numax - number of parameters presently in system
c     npro - number of probes to be investigated
c     ireqpars - number of parameters to be investigated
c     isoor - observed (0) or standard levels (1) to be looked at
c
c     Common Blocks Set:
c
c     jp - code numbers (order of probes in probefile.d) for
c          probes to be investigated (common block reqprobe).
c
c
c     Finally, set calculated parameter variables.
c     by reading information from probefile.d and putting
c     the codes of calculated parameters into ip2. 
c
c     ireqpars2 - number of requested calculated parameters
c
c
c     Common Blocks Set
c
c      reqps - contains ip2 which holds parameter codes for
c              requested parameters.
c      isur - surrogate for calculated parameter.  A surrogate
c             is a measured parameter from which information is
c             drawn about a calculated parameter.  So if one needed
c             information on Apparent Oxygen Utilization (AOU), the
c             surrogate is oxygen.  This is because of the three
c             measured parameters needed for calculating AOU,
c             temperature, salinity, and oxygen, oxygen is the least
c             measured, so AOU values will most closely correspond 
c             with oxygen values.  So to investigate AOU, oxygen record
c             numbers are used.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      call inprobe(npro,nprotot,numax,ireqpars,isoor)
c     write(6,*) 'inprobe'
      
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c    Set number of first possible second header parameter and
c    number of first possible biological parameter.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      numtot2=maxparm+maxcalc+1
      numtot3=(2*maxparm)+maxcalc+noffset+1
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Set needed names globally in C using setglobal.  Setting
c     all the parameter names, abbreviations, and codes, along
c     with the probe codes precludes having to carry these names
c     into the main program and send them back out to the C 
c     subroutines.  These characters are very important because
c     they are used to find the correct files in the OCL directory
c     structure.
c
c     ireqpars+ireqpars2 is the total number of requested parameters
c     ip2 are the parameters codes, they are given to setglobal in
c     common block reqps.
c     nprotot is the total number of probes in system.
c
c     Common Blocks sent are all character variable common blocks,
c      parname,parname1,parname4, and, ctype.
c
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc 
 
      if ( ireqpars2 .gt. 0 ) 
     * call calcneedset(ip2,maxlevel,numpar,ireqpars,
     * ireqpars2,ireqex) 
      call setglobal(ireqpars+ireqpars2+ireqex,ireqex,nprotot)

C     GETMAINPROBE SUBSTITUTES THE CORRECT MAIN PROBE TYPE
C     FOR THE FIRST MARKED PROBE.
C   
C     THIS IS ONLY DONE IF FILE DUM.MAINPROBE EXISTS AND
C     CONTAINS A VALID VALUE

      if ( actionrec(1:11) .ne. 'hasdupefile' ) then

       open(9,file='./dum.mainprobe',status='old', iostat=ios)

       if ( ios .eq. 0 ) then

        read(9,*) mainprobe
        close(9)

        call getmainprobe(iyes,mainprobe,jp(1),numax-1)

        endif

c*****************************************************************
c
c    If file dum.mainprobe exists, the designated mainprobe will
c    be substituted for the given first probe. 
c
c*****************************************************************

       call mainprober(npro,numax-1)

      endif

c************************************************************
c
c   Special for monthly retrievals:
c    only one probe will be selected, the probe number found
c    in file probechoice.retrieve.  This file should only
c    exist if the monthly retrievals are being run
c
c************************************************************

      open(9,file='./probechoice.retrieve',status='old',
     *  iostat=ios2 )

      if ( ios2 .eq. 0 ) then
       read(9,*) iprobechoice
       close(9)
       open(9,file='./retrievestop.d',status='unknown')
       if ( iprobechoice .gt. nprotot ) then
        write(9,*) 1
        close(9)
        stop
       else
        write(9,*) 0
        close(9)
       endif
       jp(1)=iprobechoice
       npro=1
       npromax=1
      endif

c     Check main probes, locks, update lock status information
      ilockup=0
      iverb=1
c     write(6,*) 'master'
      call masterlock(jp,maxprobe,npro,ilockact,actionrec,
     * ilocknum,iverb,ilockup)
c     write(6,*) 'master'

c     Update action log
      call updateactionlog(jp,maxprobe,npro,actionrec,ilockact,
     * ilocknum)
c     write(6,*) 'update'

c--------------------------------------------------------------------
 
      return
      end

