      program split2
c F90 
c reads the output from xspl12 and removes duplicates 
c if a month is missing tries to infill using duplicate
c if duplicates both have data, then takes a weighted average, with weights defined 
c  according to inverse of length of record (1/N)
      parameter maxn=50000
      real dat(maxn,27)
      integer iwmo(maxn),index(maxn)
      character*20 name(maxn)
      write(*,*)'Enter start and end years'
      read(*,*) n1,n2
101   format(3f8.2,12f8.1,12f8.4,1x,i7,1x,a20)
102   format(2f8.2,f8.1,f8.4,1x,i7,1x,a20)
c
c     START OF MAIN YEAR LOOP
      do iy=n1,n2
      write(*,*) 'Running for year',iy
       do i=1,maxn
	read(iy,101,end=9)(dat(i,j),j=1,27),iwmo(i),name(i)
       enddo
       write(*,*) iy,' end of file reached - change maxn'
       stop
9      maxstn=i-1
       write(*,*)'Finished reading:',maxstn,' stations'
       close(iy)
       index=0
       do i=1,maxstn-1
	do j=i+1,maxstn
	 match=0
	 if(index(j).eq.0)then
	  if(iwmo(i).eq.iwmo(j))match=1
	  if(abs(dat(i,2)-dat(j,2)).le.0.09.and.abs(dat(i,1)-dat(j,1)).
     &     le.0.09)match=1
	  if(match.eq.1)then
	   index(j)=1
	   do im=1,12
	    if(dat(i,im+3).eq.-9999.0)then
	     dat(i,im+3)=dat(j,im+3)
	     dat(i,im+12+3)=dat(j,im+12+3)
            else
	     if(dat(j,im+3).ne.-9999.0)then
	      dat(i,im+3)=( dat(j,im+3)*dat(j,im+12+3)+dat(i,im+3)*
     &         dat(i,im+12+3) )/(dat(i,im+12+3)+dat(j,im+12+3))
	     endif
            endif
	   enddo
          endif
         endif
        enddo
19     enddo
       icount=0
       do i=1,maxstn
        if(index(i).eq.0)then
	 icount=icount+1
	 do im=1,12
	  if(dat(i,im+3).ge.-9998)then
           write(iy*100+im,102)dat(i,1),dat(i,2),dat(i,im+3),
     &      dat(i,im+12+3),
     &      iwmo(i),name(i)
	  endif
         enddo
        endif
       enddo
       write(*,*)'Finished sorting:',icount,'unique stations'
       close(iy*10)
      enddo
      end
