      SUBROUTINE EVERYMASK(nmasks,nmaskse,iprof,ifn,irewind)

C     EVERYMASK LOOKS AT ALL REQUESTED MASKS AND MAKES ONE
C     MASK OF ALL PROFILES WHICH ARE INCLUDED IN EVERY REQUESTED
C     MASK

c***************************************************************
c
c    Passed Variables:
c
c     nmasks - number of masks
c     nmaskse - number of anti masks
c     iprof - number of profiles in each mask
c     ifn - record numbers in each mask
c
c***************************************************************

c***************************************************************
c
c    Parameter
c
c     maxmask - maximum number of masks
c
c     itempfile - file identification number for created mask
c
c***************************************************************

      parameter (maxmask=100)

c***************************************************************
c
c     Arrays:
c
c      iend - set to one if a mask has been completely read in
c      jjx - record numbers in each mask file to compare
c
c***************************************************************

      dimension iend(maxmask), ifn(maxmask), iprof(maxmask)
      dimension jjx(maxmask)

      if ( irewind .eq. 0 ) then
       itempfile=-1
      else
       itempfile=irewind
      endif
      iprofx=0
      iprofe=0

c***************************************************************
c
c    Open temporary 'everymask'
c
c***************************************************************

      call tempfiles(itempfile)

      if ( nmasks .eq. 0 ) then

       do 455 n=1,nmaskse
        iende=0
        do 505 nn=1,20000000
         call readmask(jje,ifn(n),iende) 
         if ( iende .gt. 0 ) goto 455
         call writetomask(jje,iprofe,itempfile)
505     continue
455    continue

      else

c***************************************************************
c
c     Determine which of the requested masks is the smallest.
c     This will be the mask which all others will be compared
c     to to determine if all masks contain a profile
c
c***************************************************************


      minmask = 1
      minprof = iprof(1)

      do 45 n = 2,nmasks

       if ( iprof(n) .lt. minprof ) then

        minmask = n
        minprof = iprof(n)

       endif

45    continue

c*******************************************************************
c
c      Initialize all mask holder values. (maskinit)
c
c       A mask holder value is a C storage unit which holds the
c       last profile number read from a mask.
c
c        nmasks - total number of masks
c        iend - marks end of mask has been reached (when set to one)
c
c       Initialize record holders for minimum mask (jj) and
c       all other masks (jjx)
c
c***************************************************************

      call maskinit(nmasks+nmaskse,iend)
      do 77 nn=1,nmasks+nmaskse
77     iend(nn)=0
   
      jj = 0
      iendt=0

      do 88 n=1,nmasks+nmaskse

       jjx(n) = 0

88    continue

      jjold=0
      do 50 jj0 = 1,minprof

c***************************************************************
c
c     Read the next value from the smallest mask
c
c      readmask reads in the next record number from a mask
c
c      readmask:
c       
c       jj - record number read in
c       ifn - file identification number for this mask
c       iendt - set to one if mask was completely read in
c
c*********************************************************

       call readmask(jj,ifn(minmask),iendt)
       if ( jj .eq. jjold ) goto 50
       jjold=jj
       if ( jj .eq. 660790 ) write(6,*) 'xx',jj,minmask,ifn(minmask)

       if ( iendt .gt. 0 ) goto 4

c*********************************************************
c
c      See if all other masks contain jj, the record
c      number read in from the smallest mask
c
c      itotal is the number of masks which contain jjx
c
c*********************************************************

       itotal = 1

       do 55 n2 = 1,nmasks

        if ( n2 .eq. minmask .or. iend(n2) .gt. 0)  goto 55        

c*********************************************************
c
c     Call readmask to read in value from a mask until
c     a number equal to or larger than jj is found 
c
c*********************************************************

        if ( jjx(n2) .lt. jj ) then

         do 58 i2 = 1,20000000

          call readmask(jjx(n2),ifn(n2),iend(n2))

          if ( jjx(n2) .ge. jj .or. iend(n2) .gt. 0 ) goto 59

58       continue

        endif

c*********************************************************
c
c     If this mask contains jj, add one to the number of
c     masks containing jj
c
c*********************************************************

59      if (iend(n2) .lt. 1 .and. jjx(n2) .eq. jj )
     *   itotal = itotal + 1
       if ( jj .eq. 660790 ) write(6,*) 'xx',jj,in2,jjx(n2),660790,
     *  iend(n2),itotal

55     continue

c*********************************************************
c
c     If all masks contain this record number, add it
c     to the temporary 'everymask' after making sure the
c     profile is not in any antimasks.
c
c     writetomask:
c
c      jj - record number
c      iprof - counter of number of profiles written to file
c      itempfile - file identification number for temporary
c                      mask
c
c************************************************************

       if ( itotal .eq. nmasks ) then

        do 157 n2=nmasks+1,nmasks+nmaskse

        if ( jjx(n2) .lt. jj ) then

         do 158 i2 = 1,20000000

          call readmask(jjx(n2),ifn(n2),iend(n2))

          if ( jjx(n2) .ge. jj .or. iend(n2) .gt. 0 ) goto 159

158      continue

        endif

159     continue 
        if (iend(n2) .lt. 1 .and. jjx(n2) .eq. jj ) goto 50

157     continue
        

        call writetomask(jj,iprofx,itempfile)

       endif

50    continue
4     continue

      endif

c***********************************************************
c
c     Close all masks except 'everymask'
c
c***********************************************************

      do 60 n = 1,nmasks+nmaskse

       if ( irewind .eq. 0 ) then
        call onefilecloseft(ifn(n))
       else
        call filerewind(ifn(n))
       endif

60    continue

      ifn(1) = itempfile
      call filerewind(itempfile)

      if ( nmasks .eq. 0 .and. nmaskse .gt. 1 ) then
       call sortfile(3,itempfile,iprofe)
      endif

      return
      end

