      SUBROUTINE MASKSET(npro,m,ift,nmask)

C     MASKSET READS IN INFORMATION FROM MASKCHOICE.D (IF THIS
C     IS THE FIRST RUN THROUGH MASKSET) AND SETS MASKS PARAMETERS
C     ACCORDING TO THE INFORMATION THEREIN.  MASKS ARE SET
C     DEPENDING ON THE PROBE TYPE REQUESTED.  ONLY MASKS WITH THE
C     SAME PROBE TYPE OR UNIVERSAL PROBE TYPE (ZERO) ARE OPENED.
C     MASK FILE IDENTIFICATION NUMBERS, WHICH ARE SET HERE, START
C     AT 301.


cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Passed Variables:
c
c      npro - number of probe types being investigated
c      m - present probe type code
c      ift - one means open a new file, zero,
c            an old file ( if it exists ).
c      nmask - number of each type of mask
c       1. number of inclusive sequential masks to be read
c       2. number of inclusive + exclusive sequential to be read
c       3. nmask(2) + number of depth exclusion masks to be read
c       4. nmask(3) + number of sequential masks to be written
c       5. nmask(4) + number of depth exclusion masks to be written
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Parameter:
c
c      maxmask - maximum number of masks
c      maxprobe - maximum number of probe types
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      parameter (maxmask=100, maxprobe=100)

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Character Arrays:
c
c      cift - file opening code.  Either w+ for a new file or
c             rb+ for an old file, depending on value of ift.
c      cfile - file name with C end of array marker '\0\
c      filename - constructed file name from user input in maskchoice.d
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      character cift*5
      character*7 firsttime
      character cfile*80, cfl*80
      character filename*80

      dimension nmask(5)

c******************************************************************
c 
c     Arrays:
c
c      imask - full list of all masks in maskchoice.d
c      ipar - full list of all associated mask parameters
c      ipro - full list of all associated mask probes
c      cfile - full list of all mask names
c
c******************************************************************

      dimension imask(0:5),ipar(0:maxmask),ipro(0:maxmask)
      dimension cfile(maxmask)
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Common Blocks:
c
c      reqprobe - contains jp, probe codes.
c      maskfile - contains cfile, which stores mask file names
c      parm2 - contains ipar2, which stores parameter codes for
c              mask files, file identifier numbers, and probe
c              codes for each maskfile.
c      mfileinf - The mask file information gleened from maskchoice.d.
c                 From this information, the masks to be used in this
c                 iteration of the main program are drawn, depending
c                 on the requested probe type.
c     mskcodes - Code of what type of mask each present mask is
c                1 - (.s,index,.p) sequential mask
c                2 - (.d, .2) two integer mask
c                3 - (.3) three integer
c                4 - (.4) four integer
c                5 - (.c) one string
c                6 - (.c2) two string
c                7 - (2ri) two reals, one integer
c                8 - (2r2i) two reals, two integers
c                9 - (c2i) one string, two integers
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      common /reqprobe/ jp(0:maxprobe)
      common /maskfile/ cfl(maxmask)
      common /parm2/ ipar2(maxmask),ifn(maxmask),ipro2(maxmask)
      common /mfileinf/ imsk(5),ipr(0:maxmask),ipo(0:maxmask)
      common /mskcodes/ mskcode(maxmask)

c********************************************************************
c
c     Save iset to denote that maskchoice.d has been read.
c
c     The other saved variables are equivalent to common
c     block variables which have been passed from maskinput
c     where file names, number of masks, associated parameters
c     and probes have all been read in.  It is from this overall
c     list that masks are chosen depending on requested probe 
c     type
c
c*******************************************************************

      save firsttime
      save iset
      save cfile
      save imask,ipar,ipro
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Initialize number of each type of mask
c     Initialize nx which is a holder number denoting the
c     total number of masks already looked at in the mask loop.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      call arrayinit(nmask,1,5)
      nx = 0
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     If ift is one, a new mask will be opened, if ift is zero,
c     an old mask will be opened (if it exists).
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      if ( ift .eq. 0 ) cift = 'rb+'//CHAR(0)
      if ( ift .eq. 1 ) cift = 'w+'//CHAR(0)
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Read maskchoice if this is the first time through.
c     The values for common block mfileinf are set here.
c     maskchoice is read using subroutine maskinput
c
c     maskinputhw is a hardwired maskinput for the run of
c     finalsd with qcAupdate.shell.  It will only be accessed
c     if file dum.maskinputhw exists
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      if ( firsttime .ne. 'maskset' ) then

       open(9,file='./dum.maskinputhw',status='old',iostat=ios2)
       if ( ios2 .ne. 0 ) then
c       write(6,*) 'maskinput'
        call maskinput
c       write(6,*) 'maskinputx'
       else
        close(9)
        call system('rm dum.maskinputhw')
        call readinparam(ikx)
        call maskinputhw(ikx,m)
       endif
       iset=1

       firsttime='maskset'
 
c*****************************************************************
c
c    Put the values from common block mfileinf and from variable
c    cfl into local variable so that the local variables can
c    be saved for reuse without rereading maskchoice.d.
c
c*****************************************************************

      do 98 n=1,imsk(5)

       cfile(n)=cfl(n)
c      write(6,*) 'nimsk',n,cfl(n)
       ipar(n)=ipr(n)
       ipro(n)=ipo(n)
      
98    continue

      do 89 n=1,5

       imask(n)=imsk(n)

89    continue

      endif

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Do a loop looking at each type of mask.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      do 59 j=1,5

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Run a loop looking at each mask of this type.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

c      write(6,*) 'n',nx,j,imask(j)
       do 60 n=nx+1,imask(j)
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c      If this mask is for the requested probe type, or for all
c      probe types, it will be used in the main program this time
c      around.
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

        if (abs(ipro(n)).eq.m .or. ipro(n).eq.0.or.
     *      ipro(n) .eq. -1000 ) then

         nmask(j)=nmask(j)+1
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c      Run a loop for each requested probe type
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

         do 75 ii = 1,npro

          if ( npro .gt. 1 )  then
           m2=jp(ii)
          else
           m2=m
          endif

          if ( ii .gt. 1 )  nmask(j)=nmask(j)+1
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c       Add one to total mask number.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

         if ( j .ne. 5 ) nmask(5)=nmask(5)+1 
         nn=nmask(5)
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c       Set the temporary parameter value for this probe
c       This is so the value may be altered (from -1 to 0) 
c       if necessary without losing information.
c
c      maskname constructs a file name
c        cfile - file name
c        iparn - parameter code (zero is 'other')
c        filename- full filename (including directory structure)
c        m2 - probe code
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

          iparn = ipar(n)
          if ( iparn .lt. 0 ) iparn = 0
          do 88 n0=1,80
88         filename(n0:n0)=' '

c         write(6,*) 'mask',iparn,n,m2,cfile(n)
          call maskname(cfile(n), iparn, filename, m2)
c         write(6,*) 'mask',filename

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c   Set mask code
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

         call maskcode(cfile(n),mskcode(nn))
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c       Set file identification number, file parameter code and
c       file probe code.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

c         ifn(nn) = ibasefile + nn
          ifn(nn)=-1
          ipar2(nn)=ipar(n)
          ipro2(nn)=ipro(n)
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     fileopen opens the correct file:
c
c       filename - constructed file name
c       cift - opening code
c       ifn - file identification number
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

          if (j.le.3) then
           call fileopen(filename,cift,ifn(nn))
          else
           call fileopen(filename,'w+'//CHAR(0),ifn(nn))
          endif
 
75       continue
 
        endif
 
60     continue
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     Add the number of this type of mask to each higher arrayed
c     mask type.
c     Set nx to the number of the last of this file type
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      do 80 j2=j+1,4

       nmask(j2)=nmask(j)

80    continue

      nx=imask(j)
 
59    continue
 
c*********************************************************
c
c    Run uniqtoorder on those masks which are stored as
c    unique station numbers
c
c*********************************************************

      do 67 nn=1,npro
       if ( npro .eq. 1 ) then
        mm=m
       else
        mm=jp(nn)
       endif

       call ucheck(mm,iuniqn)
       if ( iuniqn .ne. 0 ) then
        call uniqtoorder(nmask,ifn,ipro2,maxmask,nn,npro,mm,
     *   mskcode)
       endif
67    continue

      return
      end

