      subroutine WeighGridate(infl,outfl,row,row1,knd,mode,mn,dm,nk,hd)
      implicit none
      !加权基函数插值格网化;缺省值>9900不参与格网化
      !hd(6)-格网规格：最小最大经度，最小最大纬度,经度间隔纬度间隔
      !row-格网化属性所在列序号,row1-权值所在列序号
      !mn-插值临近点数，dm-权函数峰度参数
      !knd-权函数类型：0余弦函数,1高斯函数，2指数函数
      !mode-0等权插值,此时row1列恒等于1.0
      !nk-离散点值文件头文件行数
	character*800::infl,outfl
 	character*800::line
	real*8::hd(6),rec(800)
	real*8::lon,lat,maxdl,pi,RAD,rdi,tmp,fun,val,dx,rr
	real*8::dm,st(4),pl(5000)
      integer::row,row1,knd,mode,mn,n,i,j,k,sn,len,nk
	integer::nlat,nlon,astat(5)
	real*8,allocatable::rst(:),pdt(:,:),dl(:),wgh(:)
	integer,allocatable::kh(:)
	integer::status=0
!-------------------------------------------------------------
      pi=datan(1.d0)*4.d0;RAD=pi/180.d0;rdi=6371.d-3
      maxdl=dsqrt(((hd(2)-hd(1))*dcos((hd(3)+hd(4))/2.d0*RAD))**2+(hd(4)-hd(3))**2)*rdi
      n=0;if(row1<5)mode=0
      open(unit=8,file=infl,status="old",iostat=status)
      if(status/=0)goto 908
      if(nk<1)goto 401
      do i=1,nk
        read(8,'(a)') line    !读取头文件
      enddo
401   continue
      do while(.not.eof(8))  
         read(8,'(a)') line
         call PickRecord(line,len,rec,sn)
         if(sn<4.or.sn<row)goto 906
         if(rec(row)>9900.d0)goto 906
         n=n+1
906	   continue
      enddo
      close(8)
      if(n<9)goto 908
 	allocate(pdt(n,3), stat=astat(1))
 	allocate(wgh(n), stat=astat(2))
 	allocate(dl(n), stat=astat(3))
 	allocate(kh(n), stat=astat(4))
	if (sum(astat(1:4)) /= 0) goto 908
      open(unit=8,file=infl,status="old",iostat=status)
      if(nk<1)goto 402
      do i=1,nk
        read(8,'(a)') line    !读取头文件
      enddo
402   continue
      n=0;wgh=1.d0
      do while(.not.eof(8))  
         read(8,'(a)') line
         call PickRecord(line,len,rec,sn)
         if(sn<4.or.sn<row)goto 910
         if(rec(row)>9900.d0)goto 910
         n=n+1;pdt(n,1:2)=rec(2:3);pdt(n,3)=rec(row);wgh(n)=1.d0
         if(mode>0)wgh(n)=dabs(rec(row1))
910	   continue
      enddo
      close(8)
	nlat=nint((hd(4)-hd(3))/hd(6))
	nlon=nint((hd(2)-hd(1))/hd(5))
	hd(5)=(hd(2)-hd(1))/real(nlon)
	hd(6)=(hd(4)-hd(3))/real(nlat)
 	allocate(rst(nlon))
      open(unit=10,file=outfl,status="replace")
      write(10,1001)(hd(i),i=1,6)
      do i=1,nlat
         lat=hd(3)+(real(i)-0.5d0)*hd(6)
         do j=1,nlon
            lon=hd(1)+(real(j)-0.5d0)*hd(5)
            do k=1,n
              dx=(lon-pdt(k,1))*dcos((lat+pdt(k,2))/2.d0*RAD)
              dl(k)=dsqrt(dx**2+(lat-pdt(k,2))**2)*rdi
            enddo
            tmp=0.d0;val=0.d0
            call sortselect(dl,pdt,wgh,n,3)
            maxdl=(dl(mn)-dl(1))/2.d0
            do k=1,mn
              dx=dl(k)-dl(1)
              if(knd==0)fun=1.d0/(1.d0+dx**2/maxdl*2.0*dm)
              if(knd==1)fun=dexp(-dx**2/maxdl**2/2.d0*dm)
              if(knd==2)fun=dexp(-dx**2/maxdl*dm)
              tmp=tmp+wgh(k)*fun
              val=val+pdt(k,3)*wgh(k)*fun
            enddo
            rst(j)=val/tmp
         enddo
 	   write(10,'(15F13.4)')(rst(j),j=1,nlon)
      enddo
      close(10)
      deallocate(rst,pdt,dl,wgh,kh)
908   continue
1001  format(4F12.6,2F12.8)
      end
