c     gt_surroundings.f
c
c     Interpolate 2x2 regional transformed Gt into 1x1 datasets
c
      use commondata, only : ensmem,mx,my,maxi,seasons,area,loc,sur,
     & tmp_path,sur_path,date,time

      implicit none

      real :: values(20000),results(33000),dis(33000,4)
      real :: tmp(20000),d(4),dall
      integer :: num(33000,4),pos(20000),valid,numpts,mem,runs
      integer :: iyear,imonth,iday,ihour,k,i,j,z
      character(len=50) :: sno(4),snd(4),smem,input,model,a

      data snd /'_r','_trGt_Standardized_','_','.txt'/
      data sno /'_r','_trGtonHs_','_','.txt'/

c      call idate(date)
c      call itime(time)
c      write(*,1000) date(2),date(1),date(3)+2000,time
c1000  format('gt_surroundings start at: Date ', i2.2, '/', i2.2, 
c     &         '/', i4.4,'; time ',i2.2, ':', i2.2, ':', i2.2 )

      call get_command_argument(1,input)
      read(input,*) model
      call get_command_argument(2,input)
      read(input,*) runs

      do z=1,13
      do k=1,4

         valid=0
         open(19,file=trim(sur_path)//trim(sur(1))//trim(area(z))//
     & trim(sur(2))//trim(seasons(k))//trim(sur(3)))
         read(19,*) a
         do
            read(19,*,end=77) a
            valid=valid+1
         enddo
77       close(19)
         open(9,file=trim(sur_path)//trim(sur(1))//trim(area(z))//
     & trim(sur(2))//trim(seasons(k))//trim(sur(3)))
         read(9,*) a
         do i=1,valid
            read(9,*) a,num(i,1),num(i,2),num(i,3),num(i,4),
     & dis(i,1),dis(i,2),dis(i,3),dis(i,4)
         enddo
         close(9)

         open(38,file=trim(sur_path)//trim(area(z))//trim(loc(1))//
     & trim(seasons(k))//trim(loc(2)))
         i=1
         do
            read(38,*,end=78) pos(i)
            i=i+1
         enddo
78       close(38)

         do mem=runs,runs
         smem=ensmem(mem)

         open(10,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(area(z))//trim(sno(3))//
     & trim(seasons(k))//trim(sno(4)))
         open(2,file=trim(tmp_path)//trim(model)//trim(snd(1))//
     & trim(smem)//trim(snd(2))//trim(area(z))//trim(snd(3))//
     & trim(seasons(k))//trim(snd(4)))
C        print*,trim(model)//trim(sno(1))//trim(smem)//trim(sno(2))//
C    & trim(area(z))//trim(sno(3))//trim(seasons(k))//trim(sno(4))

         do
            read(2,*,end=99) iyear,imonth,iday,ihour,numpts
            read(2,'(10f9.3)') (tmp(i),i=1,numpts)
            do i=1,numpts
               values(pos(i))=tmp(i)
            enddo

            do i=1,valid
               do j=1,4
                  d(j)=dis(i,j)
                  if(abs(d(j)).lt.1e-8) d(j)=1e-8
                  d(j)=1./d(j)
               enddo
               dall=d(1)+d(2)+d(3)+d(4)
               do j=1,4
                  d(j)=d(j)/dall
               enddo
               if(values(num(i,1)).lt.-899.99.or.
     & values(num(i,2)).lt.-899.99.or.values(num(i,3)).lt.-899.99.or.
     & values(num(i,4)).lt.-899.99) then
                  results(i)=-900.99
               else
                  results(i)=values(num(i,1))*d(1)+
     & values(num(i,2))*d(2)+values(num(i,3))*d(3)+
     & values(num(i,4))*d(4)
               endif
            enddo

            write(10,'(i6,3i3,i6)') iyear,imonth,iday,ihour,valid
            write(10,'(10f9.3)') (results(i),i=1,valid)
         enddo
99       close(2)
         close(10)
         enddo

      enddo
      enddo

c      call idate(date)
c      call itime(time)
c      write(*,2000) date(2),date(1),date(3)+2000,time
c2000  format('gt_surroundings end at: Date ', i2.2, '/', i2.2, 
c     &         '/', i4.4,'; time ',i2.2, ':', i2.2, ':', i2.2 )

      end
