c PMASK.FOR
c
c To make a 1,dval gravity-station (post-file) mask for images
c  Output is a grid (make it same size as the regridded grid
c  input to GRDREMU) with a dot, cross, or diamond of dvals 
c  around the station, and ones everywhere else.  Multiply this
c  times the data grid to obtain dvals at the stations.
c  Lin Cordell 13 Mar 91.
c
      character*50 ifile,ofile
      character*8 ident
      dimension id(14),pgm(2),g(2000),v(6)
      dval=.170412e+39
c  Read data.
      print*,'Enter input post file name:'
      print 1
1     format(' *',$)
      read(5,800)ifile
800   format(a)
      print*,'Enter output grid file name:'
      print 1
      read(5,800)ofile
      print*,'Enter x0,y0,dx,dy,ncol,nrow:'
      print 1
      read*,x0,y0,dx,dy,ncol,nrow
      open(10,file=ifile,form='unformatted',status='old')
      print*,'Enter id:'
      print 1
      read(5,4)(id(i),i=1,14)
4     format(14a4)
      pgm(1)='pmas'
      pgm(2)='k   '
402   print*,'Want 1-pixel spot,3-pixel cross or'
      print*,' 5-pixel diamond [1,3 or 5]?:'
      print 1
      read*,mode
      if(mode.ne.1.and.mode.ne.3.and.mode.ne.5)go to 402
      if(mode.eq.5)print*,'Symbols near edges are shifted inward.'
      lrec=4*ncol
      open(12,status='scratch',access='direct',
     & recl=lrec,form='unformatted')
      do 400 i=1,ncol
400   g(i)=1
      do 401 j=1,nrow
401   call rrowio(g,ncol,12,j,2)
450   read(10,end=490)ident,x,y,v
      i=1.5+(x-x0)/dx
      if(i.lt.1)go to 450
      if(i.gt.ncol)go to 450
      j=1.5+(y-y0)/dy
      if(j.lt.1)go to 450
      if(j.gt.nrow)go to 450
      if(mode.eq.5)go to 429
      if(mode.eq.3)go to 430
      call rrowio(g,ncol,12,j,1)
      g(i)=dval
      call rrowio(g,ncol,12,j,2)
      go to 450
430   continue
      if(j.gt.1)then
        call rrowio(g,ncol,12,j-1,1)
        g(i)=dval
        call rrowio(g,ncol,12,j-1,2)
      endif
      call rrowio(g,ncol,12,j,1)
      do 421 k=i-1,i+1
      if(k.lt.1.or.k.gt.ncol)go to 421
      g(k)=dval
421   continue
      call rrowio(g,ncol,12,j,2)
      if(j.lt.nrow)then
        call rrowio(g,ncol,12,j+1,1)
        g(i)=dval
        call rrowio(g,ncol,12,j+1,2)
      endif
      go to 450
429   continue
      if(i.lt.3)i=3
      if(i.gt.ncol-2)i=ncol-2
      if(j.lt.3)j=3
      if(j.gt.nrow-2)j=nrow-2
      call rrowio(g,ncol,12,j-2,1)
      g(i)=dval
      call rrowio(g,ncol,12,j-2,2)
      call rrowio(g,ncol,12,j-1,1)
      do 423 k=i-1,i+1
423   g(k)=dval
      call rrowio(g,ncol,12,j-1,2)
      call rrowio(g,ncol,12,j,1)
      do 424 k=i-2,i+2
424   g(k)=dval
      call rrowio(g,ncol,12,j,2)
      call rrowio(g,ncol,12,j+1,1)
      do 425 k=i-1,i+1
425   g(k)=dval
      call rrowio(g,ncol,12,j+1,2)
      call rrowio(g,ncol,12,j+2,1)
      g(i)=dval
      call rrowio(g,ncol,12,j+2,2)
      go to 450
490   continue
      close(10)
      open(11,file=ofile,form='unformatted',status='new')
      nz=1
      write(11)id,pgm,ncol,nrow,nz,x0,dx,y0,dy
      yy=y0-dy
      do 409 j=1,nrow
      call rrowio(g,ncol,12,j,1)
      yy=yy+dy
409   call rowio(g,yy,1,ncol,11,2)
      stop
      end
      subroutine rowio(a,y,nz,ncol,ld,key)
      dimension a(nz,ncol)
      go to (1,2),key
1     read(ld) y,a
      go to 90
2     write(ld)y,a
90    return
      end
      subroutine rrowio(a,ncol,ld,jrec,key)
c  To read/write row from random-access file.
      dimension a(ncol)
      go to (1,2),key
1     read(ld,rec=jrec) a
      go to 90
2     write(ld,rec=jrec) a
90    return
      end
