c  TRIMGRD
c
c  TRIMGRD will find either a row or column in a standard gridded file
c  that is entirely dvals, delete it and change row and column count and
c  x0 and y0 if necessary.  
c  Tien Grauch.  Completely revised from program slice.  June 1991
c
      character*56 infile,outfile
      dimension x(5000),id(14),pgm(2)
      data ddval/1.e+30/,dum/0.0/
      call pfinit('trimgrd')
      call askin
1     print 801
801   format(/' This program trims off rows and columns around',
     1 ' the edges of the grid '/' that are entirely dvals',/)
C5     print*, 'Enter input file name'
C      read 802,infile
C802   format(a56)
      infile=' '
5     call askc('Enter input file name',infile,ierr)
      if(ierr.eq.-1) go to 5
      if(ierr.eq.-2) stop
C      open(10,file=infile,form='unformatted',status='old')
      call gopen(10,infile,'old','read',ierr)
      if(ierr.ne.0) stop 'error opening file'
C      read(10) id,pgm,ncol,nrow,nz,x0,dx,y0,dy
      call gheader('r',10,id,ncol,nrow,x0,dx,y0,dy,ierr)
      if(ierr.ne.0) stop 'error reading header'
C      print*, 'Enter output file name'
C      read 802,outfile
C10    open(11,file=outfile,form='unformatted',status='new')
      outfile=' '
      outfile=infile(1:index(infile,'.'))//'trm'
10    call askc('Enter output file name',outfile,ierr)
c      if(ierr.eq.-1) go to 10
      if(ierr.eq.-2) go to 5
      call gopen(11,outfile,'new','write',ierr)
      if(ierr.ne.0) go to 10
c
c  find the dvaled borders of the grid
c
	ileft= ncol
	iright= 0
	ibot= 0
	itop= 0
	ibottest=1
      do 25 i= 1, nrow
C      call rowrow(10,ncol,x,1)
      call grow('r',10,i,x,ncol,ierr)
      if(ierr.ne.0) stop 'error reading row'
	ilefttest=1
      do 15 j=1,ncol
      if(x(j).lt.ddval) then
	go to (155, 160), ibottest
155		ibottest=2
		ibot=i
	go to 170
160		itop=i
170	go to (180,185), ilefttest
180		if(j.lt.ileft) ileft=j
		ilefttest=2
	go to 15
185		if(j.gt.iright) iright=j
      endif
15    continue
25    continue
c check if whole grid is dvaled
	if(ibot.eq.0. and .itop.eq.0) stop 'All values are dvals'
c calculate new grid specs
	x0new= x0 + float(ileft - 1)* dx
	y0new= y0 + float(ibot - 1)* dy
	ncol2= iright - ileft + 1
        nrow2= itop - ibot + 1
	print*,' writing output file...'
c write subgrid
	rewind 10
      read(10) id
c      pgm(1)='trim'
c      pgm(2)='med '
c      write(11) id,pgm,ncol2,nrow2,nz,x0new,dx,y0new,dy
       call gheader('w',11,id,ncol2,nrow2,x0new,dx,y0new,dy,ierr)
	if (ibot.eq.1) go to 210
	do 200 j= 1, ibot-1
C        call rowrow(10,ncol,x,1)
        call grow('r',10,j,x,ncol,ierr)
200	continue
210	do 220 j= ibot, itop
C        call rowrow(10,ncol,x,1)
        call grow('r',10,j,x,ncol,ierr)
	write(11) dum, (x(k),k= ileft, iright)
220	continue	
c
100   close(10)
      close(11)
300      stop
      end
c************************************************************************
Cc reads or writes to standard gridded file one row at a time
Cc
C      subroutine rowrow(iunit,ncol,row,ikey)
C      dimension row(ncol)
C      go to (1,2),ikey
C1     read(iunit) dum,row
C      return
C2     write(iunit) dum,row
C      return
C      end
