      subroutine rot_ij(RLAT,RLON,ii,ji,kgds)
c
      implicit none
c
      integer kgds(200)
c
      REAL*4, ALLOCATABLE:: RALMD_C(:,:),RAPHD_C(:,:)
      REAL*4, ALLOCATABLE:: ALD_C(:,:),APD_C(:,:)
      real rlat,rlon
      real*4 WBD_C,SBD_C,DLMD_C,DPHD_C
      real*4 TPH0D_C,TLM0D_C
      real*8 DISTXH,DISTYH,RDIST,distmin,rtmp
      real*4 CTPH0,STPH0,dtr
      integer nxsec,nysec,i,j,k,ii,ji
c
      parameter (dtr=0.017453293) ! ioibm
c
c
         nxsec=KGDS(2)
         nysec=KGDS(3)
         WBD_C=KGDS(5)*0.001
         SBD_C=KGDS(4)*0.001
         DLMD_C=KGDS(9)*0.001
         DPHD_C=KGDS(10)*0.001
         TPH0D_C=(KGDS(13)*0.001)+90.0 !rotated grid
         TLM0D_C=KGDS(14)*0.001
         
cioan         write(6,*)'Finding the closest grid-point'
cioan         write(6,*)'for coordinates, LAT, LON-->',RLAT,RLON
cioan         write(6,*)'Coarse grid - NX, NY, WBD, SBD, DLMD, DPHD=',
cioan     $  NXSEC, NYSEC, WBD_C,SBD_C,DLMD_C,DPHD_C
cioan         write(6,*)'Coarse grid - TPH0D, TLM0D=',TPH0D_C,TLM0D_C

        ALLOCATE(RALMD_C(nxsec,nysec))
        ALLOCATE(RAPHD_C(nxsec,nysec))
        ALLOCATE(ALD_C(nxsec,nysec))
        ALLOCATE(APD_C(nxsec,nysec))

        rtmp=TPH0D_C*DTR
        CTPH0=COS(rtmp)
        STPH0=SIN(rtmp)

        DO J=1,NYSEC
         DO I=1,NXSEC
            RAPHD_C(I,J)=SBD_C+(J-1)*DPHD_C
            RALMD_C(I,J)=WBD_C+(I-1)*DLMD_C
            CALL RTLLNEW
     &(RALMD_C(I,J),RAPHD_C(I,J),TLM0D_C,DTR,CTPH0,STPH0
     &,ALD_C(I,J),APD_C(I,J))
cc      write(60,*)i,j,RALMD_C(I,J),RAPHD_C(I,J),ALD_C(I,J),APD_C(I,J)
         ENDDO
        ENDDO

        distmin=9999999.9
        ii=0
        ji=0
        DO J=1,NYSEC
         DO I=1,NXSEC
          DISTXH=ABS(ald_c(i,j)-rlon)
          DISTYH=ABS(apd_c(i,j)-rlat)
          RDIST=SQRT(DISTXH**2.+DISTYH**2.)
c          print*,i,j,distxh,distyh,rdist
          IF(rdist.lt.distmin)then
            distmin=rdist
            ii=i
            ji=j
          ENDIF
         ENDDO
        ENDDO

cioan        write(6,*)'Coords lat,lon=',rlat,rlon
cioan        write(6,*)'Closest point, i, j, lat, lon-->',ii,ji
cioan     $,apd_c(ii,ji),ald_c(ii,ji)
        DEALLOCATE(RALMD_C)
        DEALLOCATE(RAPHD_C)
        DEALLOCATE(ALD_C)
        DEALLOCATE(APD_C)

          return
          end
