c  DE_PREP3.FOR
c
c  To delete extended rows and columns after filtering of output of program
c  prep and, optionally, adding back the linear trend removed by program PREP3.
c  Requires file prep3.rec as output from program prep.
c
c  de_prep:  Lin Cordell Mar 1990.
c  de_prep3: Jeff Phillips February 1996
c
        character*56 ifile,ofile
        character id*56,prjnam*80
        dimension g(2048),cmbl(2)
        open(10,file='prep3.rec',status='old',form='formatted')
        read(10,*)nc,nr
        read(10,3)prjnam,cmbl
3       format(a50/2e16.8)
	close(10)
1       format(a)
        call pfinit('de_prep3')
        call askin
        ifile=' '
400     call askc('Enter name of grid to be de-prepped',ifile,ierr)
        if(ierr.lt.0) stop
        call gopen(10,ifile,'old','read',ierr)
        if(ierr.ne.0) then
          print*,'Error - try again'
          go to 400
        endif
        ofile=' '
        ofile=ifile(1:index(ifile,'.'))//'dp3'
401     call askc('Enter output grid name',ofile,ierr)
        if(ierr.eq.-2) then
          close(10)
          go to 400
        endif
        call gopen(11,ofile,'new','write',ierr)
        if(ierr.ne.0) then
          print*,'Error - try again'
          go to 401
        endif
        call gheader('r',10,id,ncol,nrow,xo,dx,yo,dy,ierr)
        if(ierr.ne.0) stop 'error reading grid header'
        call askalt
        call askc('Enter id',id,ierr)
        call askalt
        if(ncol.lt.nc.or.nrow.lt.nr)go to 900
        call mpsnam('w',idum,prjnam,nch,ierr)
        call mpsref('w',idum,cmbl,nch,ierr)
        call gheader('w',11,id,nc,nr,xo,dx,yo,dy,ierr)
	  do 20 j2=1,nr
        call grow('r',10,j2,g,nc,ierr)
20      call grow('w',11,j2,g,nc,ierr)
	go to 999
900     print*, 'Cant handle it.'
	close(10)
999     close(11)
	stop
	end

