c G2MINCMD - creates a MINC command file from a grid header.
c            Used by PLUGGRID.BAT.
c            Written by J.Phillips 10/94
      character ifile*80,temp*80,id*56,pgm*8,prjnam*80
      dimension cmbl(2),cm(3),bl(3)
C      print*,'enter input grid:'
C      read(5,100) ifile
      call askin
      ifile=' '
400   call askc('enter input grid',ifile,ierr)
      if(ierr.lt.0) stop
  100 format(a)
C      open(10,file=ifile,status='old',form='unformatted')
C      read(10) id,pgm,nc,nr,nz,xo,dx,yo,dy
      call gopen(10,ifile,'old','read',ierr)
      if(ierr.ne.0) stop 'error opening file'
      call gheader('r',10,id,nc,nr,xo,dx,yo,dy,ierr)
      if(ierr.ne.0) stop 'error reading header'
      write(6,100) id
      open(11,file='title.tmp',form='formatted',status='unknown')
      write(11,100) id
      close(11)
      print*,'nc:',nc,' nr:',nr
      print*,'xo:',xo,' yo:',yo
      print*,'dx:',dx,' dy:',dy
      call mpsnam('r',1,prjnam,nchp,ierr)
      if(ierr.eq.0) then
        print 56, prjnam(1:nchp)
56      format(' projection = ',a)
        if(prjnam.ne.'not specified') then
          call mpsref('r',1,cmbl,ncmbl,ierr)
          if(ierr.eq.0) then
            print *, 'central meridian & base latitude =', cmbl
          endif
        endif
      endif
      close(10)
      if(dx.ne.dy) stop 'dx not equal dy'
C      print*,'enter minc command file name:'
C      read(5,100) ifile
      ifile=' '
401   call askc('enter minc command file name',ifile,ierr)
      if(ierr.eq.-1) go to 401
      if(ierr.eq.-2) go to 400
      open(10,file=ifile,status='new',form='formatted',iostat=ierr)
      if(ierr.eq.6415) then
        ians=0
        call aski4l(' File exists -- OK to overwrite?',ians,ierr)
        if(ians.eq.1) then
          open(10,file=ifile,status='unknown',form='formatted',
     1             iostat=ierr)
        endif
      endif
      if(ierr.ne.0) stop 'error opening file'
C      print*,'enter radius:'
C      read(5,*) radius
      radius=0.
402   call askf4('enter radius',radius,ierr)
      if(ierr.eq.-2) go to 401
      nim=100
403   call aski4('enter nim',nim,ierr)
      if(ierr.eq.-2) go to 402
c      ians=0
c      call aski4l('copy grid id to command file?',ians,ierr)
c      if(ierr.eq.-2) go to 402
c
      write(10,100) '$parms'
      write(ifile,101) xo
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='xo='//ifile(istart:iend)
      write(10,100) temp(1:iend+3)
      write(ifile,101) yo
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='yo='//ifile(istart:iend)
      write(10,100) temp(1:iend+3)
      write(ifile,102) nc
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='nc='//ifile(istart:iend)
      write(10,100) temp(1:iend+3)
      write(ifile,102) nr
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='nr='//ifile(istart:iend)
      write(10,100) temp(1:iend+3)
      write(ifile,101) dx
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='del='//ifile(istart:iend)
      write(10,100) temp(1:iend+4)
      write(ifile,101) radius
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='radius='//ifile(istart:iend)
      write(10,100) temp(1:iend+7)
      write(ifile,102) nim
      istart=verify(ifile,' ')
      iend=len_trim(ifile)
      temp='nim='//ifile(istart:iend)
      write(10,100) temp(1:iend+4)
c      if(ians.eq.1) then
c        iend=len_trim(id)
c        temp='title='//"'"//id(1:iend)//"'"
c        write(10,100) temp(1:iend+8)
c      endif
      if(prjnam.ne.'not specified') then
        call mpxnam(2,prjnam,iproj,ierr)
        call d2dms(cmbl(1),cm)
        call d2dms(cmbl(2),bl)
        write(ifile,102) iproj
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        temp='iproj='//ifile(istart:iend)
        write(10,100) temp(1:iend+6)
        write(ifile,102) int(cm(1))
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        temp='cm='//ifile(istart:iend)//','
        write(ifile,102) int(cm(2))
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        iendt=len_trim(temp)
        temp=temp(1:iendt)//ifile(istart:iend)//','
        write(ifile,101) cm(3)
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        iendt=len_trim(temp)
        temp=temp(1:iendt)//ifile(istart:iend)
        iend=len_trim(temp)
        write(10,100) temp(1:iend)
        write(ifile,102) int(bl(1))
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        temp='baslat='//ifile(istart:iend)//','
        write(ifile,102) int(bl(2))
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        iendt=len_trim(temp)
        temp=temp(1:iendt)//ifile(istart:iend)//','
        write(ifile,101) bl(3)
        istart=verify(ifile,' ')
        iend=len_trim(ifile)
        iendt=len_trim(temp)
        temp=temp(1:iendt)//ifile(istart:iend)
        iend=len_trim(temp)
        write(10,100) temp(1:iend)
      endif
      write(10,100) '$'
      stop
  101 format(1pg16.8)
  102 format(i10)
      end
        subroutine d2dms( d, dms)
        dimension dms(3)
        signd=1.
        if(d.lt.0.0) signd=-1.
        dms(1)=int(d)
        dms(2)=signd*int((d-dms(1))*60.)
        dms(3)=((signd*(d-dms(1))*60.)-dms(2))*60.
        return
        end

