      subroutine gridset(idimgs0,idimge0,jdimgs0,jdimge0,idim,m,
     *                   igeo,igeofile,numprotot)
 
C     GRIDSET CONSTRUCTS A MASK OF ALL PROFILES WITHIN
C     THE BOUNDARIES OF USER INPUT LATITUDE AND LONGITUDE NUMBERS
C     WRITES THESE TO MASK AND SORTS THE MASK

C     NOW CAN ALSO CONSTRUCT A SURFACE FILE DATE MASK (IGEO=1)
C     SPECIAL CODE ADDED TO ACCOMODATE 1/10 DEGREE CLIMATOLOGY

c*********************************************************
c
c    Passed Variables:
c
c      idimgs - starting longitude number
c      idimge - ending longitude number
c      jdimgs - starting latitude number
c      jdimge - ending latitude number
c
c      idim - number of longitude spacings
c    
c      m - probe type
c      igeo - 0 = geographic masks
c             1 = surface probe date mask
c      igeofile = output mask file
c
c**********************************************************

c**********************************************************
c
c   Parameter:
c
c    maxmask - maximum number of masks
c
c**********************************************************

      parameter (maxmask=100,idim0=360)
      parameter (igeo1=436,igeo2=437)

c**********************************************************
c
c    Character Array:
c
c      filename - constructed file name
c
c**********************************************************

      character*80 filename

      idimgs=idimgs0
      idimge=idimge0
      jdimgs=jdimgs0
      jdimge=jdimge0
      idimx=idim

c**********************************************************
c
c     Open geomask.p the primary geographic sorting mask
c     and geomask.2 the secondary geographic sorting mask.
c
c      maskname constructs a file name
c        'xxxxx.x' - file name
c        0 - parameter code (zero is 'other')
c        filename- full filename (including directory structure)
c        m - probe code
c
c     fileopen opens the correct file:
c
c       filename - constructed file name
c       'rb+' - opening code meaning open file for binary
c               reading and writing.
c       igeo1,igeo2 - file identification number
c
c************************************************************

      if ( igeo .eq. 0 ) then
       if ( idim .eq. idim0 .or. idim/10 .eq. idim0 ) then

        if ( idim/10 .eq. idim0 ) then
         idimgs=idimgs/10+1
         idimge=idimge/10
         jdimgs=jdimgs/10+1
         jdimge=jdimge/10
         idimx=idim0
        endif

        call maskname('geomask.p'//CHAR(0),0,filename,m)
        call fileopen(filename,'rb+'//CHAR(0),igeo1)
 
        call maskname('geomask.2'//CHAR(0),0,filename,m)
        call fileopen(filename,'rb+'//CHAR(0),igeo2)
       else
        call maskname('geomask25.p'//CHAR(0),0,filename,m)
        call fileopen(filename,'rb+'//CHAR(0),igeo1)
 
        call maskname('geomask25.2'//CHAR(0),0,filename,m)
        call fileopen(filename,'rb+'//CHAR(0),igeo2)
       endif
      else
       call maskname('datemask.p'//CHAR(0),0,filename,m)
       call fileopen(filename,'rb+'//CHAR(0),igeo1)
 
       call maskname('datemask.2'//CHAR(0),0,filename,m)
       call fileopen(filename,'rb+'//CHAR(0),igeo2)
      endif
 
c************************************************************
c
c     For each individual grid box in the requested domain,
c     call georead to read in the profile record numbers for
c     the profiles in each grid box and write out the number
c     to the temporary file using writetomask.
c
c     georead:
c
c      i,j - longitude and latitude grid numbers
c      igridpos - record numbers of profiles in grid box
c      idim - number of longitude spacings
c      igeo - primary mask file identification number
c      igeo2 - secondary mask file identification number
c      igeofile - output mask file
c
c
c************************************************************

      numprotot=0

c     write(6,*) 'getting',jdimgs,jdimge,idimgs,idimge
      do 50 j=jdimgs,jdimge
      do 51 i=idimgs,idimge
 
       call georeadtomask(i,j, numpros,idimx, igeo1,igeo2, igeofile )
       numprotot=numprotot+numpros
c      write(6,*) 'xxy',i,j,numpros
 
51    continue
50    continue
 
c***********************************************************
c
c     Sort file by record number.
c
c      sortfile:
c
c      3 - structure indicator
c          0 - one real value
c          1 - two integer values
c          2 - one integer one real value
c          3 - one integer value
c      igeofile - file identification number
c      numprotot - number of structures to free space for
c************************************************************

      call sortfile(3,igeofile,numprotot)
 
c************************************************************
c
c     Close geographic sorting files
c
c************************************************************

      call onefileclose(igeo1)
      call onefileclose(igeo2)
 
      return
      end
