c  DE_PREP2.FOR
c
c  2D version of DE_PREP.  Uses prep2.rec file created by program PREP2
c  to trim the profile back and (optionally) restore the linear trend.
c
c  Lin Cordell
c  22 October 92
c
      character*50 ifile, ofile
      dimension f(2000),id(14),pgm(2)
c      data dval/0.170412e+39/
c      dval2=1.e20
      open(10,file='prep2.rec',form='formatted',err=90)
      read(10,*)a,b,nc
      close (10)
C      print*,'Enter input prepped 1-row-grid file:'
C      print 1
C1     format(' *'$)
C      read(5,100)ifile
C100   format(a50)
        call pfinit('de_prep2')
        call askin
        ifile=' '
400     call askc('Enter input prepped 1-row-grid file',ifile,ierr)
        if(ierr.lt.0) stop
        call gopen(10,ifile,'old','read',ierr)
C      print*,'Enter output de-prepped file name:'
C      print 1
C      read(5,100)ofile
        ofile=' '
        ofile=ifile(1:index(ifile,'.'))//'1rg'
401     call askc('Enter output de-prepped file name',ofile,ierr)
        if(ierr.lt.0) then
          close(10)
          go to 400
        endif
        call gopen(11,ofile,'new','write',ierr)
C20    print*,'Enter mode: 1 to add back linear trend, or 2 not'
20    print*,'Enter mode: 1 to add back linear trend, or 2 not'
C      print*,'to add back linear trend:'
C      read*,mode
        mode=2
        call aski4('to add back linear trend',mode,ierr)
      if(mode.lt.1.or.mode.gt.2)then
        print*,'Enter 1 or 2.'
        go to 20
      endif
C      open(10,file=ifile,form='unformatted',status='old')
C      read(10)id,pgm,ncol,nrow,nz,x0,dx,y0,dy
        call gheader('r',10,id,ncol,nrow,x0,dx,y0,dy,ierr)
C      open(11,file=ofile,form='unformatted',status='new')
C      print*,'Enter new id:'
C      print 1
C      read(5,203)(id(i),i=1,14)
C203   format(14a4)
C      pgm(1)='de_p'
C      pgm(2)='rep2'
        call askalt
        call askc('Enter new id',id,ierr)
        call askalt
C      write(11)id,pgm,nc,nrow,nz,x0,dx,y0,dy
        call gheader('w',11,id,nc,nrow,x0,dx,y0,dy,ierr)
C      call rowio(f,y,1,ncol,10,1)
      call grow('r',10,1,f,ncol,ierr)
      if(mode.eq.2)go to 22
      do 21 i=1,nc
      ri=i
      f(i)=f(i)+a+ri*b
21    continue
C22    call rowio(f,y,1,nc,11,2)
22    call grow('w',11,1,f,nc,ierr)
      go to 99
90    print*,'PREP2.REC file not found.'
99    stop
      end
c      subroutine rowio(a,y,nz,ncol,ld,key)
c      dimension a(nz,ncol)
c      go to (1,2),key
c1     read(ld) y,a
c      go to 90
c2     write(ld)y,a
c90    return
c      end
