	Program combine
	! >>>>>> This program will combine OBS data from 3 sources (NCOM=3). <<<<<<
	integer, parameter :: NST=650, NYR=140, NMN=12, SYR=1880
	integer :: ist, ivar, iyr, iiyr, imn, icom, nnst, nnyr, info

	! >>> For station info <<<<<<
	character (len=20), dimension(NST) :: stname
	character (len=10) :: varname, tmp
	character (len=100) :: Dir
	character (len=100) :: filein, fileout

	real, parameter :: flag=-99.0
	real, dimension (NST,NYR,NMN) :: DAT
	real, dimension (NST,NYR) :: DTMP

	logical :: check

	DAT(:,:,:)=flag
	DTMP(:,:)=flag        

	Open (1,file="stlist.tmp",status="old")
	   read (1,*) varname, NNST
	   do ist=1, NNST
	      read (1,*) stname(ist)
	   enddo
	Close (1)
	
	! >>> Read data from Source 1 <<<<<<
	Dir="./1_SL_BASE/Converted/"
	Do ist=1, NNST
	   Filein=trim(Dir)//trim(varname)//'/'//trim(varname)//'_'//trim(stname(ist))//'.txt'
	   Inquire( file=filein, exist=check )
	   If ( check ) then
	      Open (2,file=filein,status="old")
	         Read (2,*) tmp, NNYR
	         Do iyr=1, NNYR
	            Read(2,*) yeartmp, (DTMP(iyr,imn),imn=1, NMN)
	            iiyr=yeartmp-SYR
	            Do imn=1, NMN
	               DAT(ist,iiyr,imn)= DTMP(iyr,imn)
	            Enddo
	         Enddo
	      Close(2)
	   Endif
	Enddo

	! >>>>>>> End of read source 1 <<<<<<

	! >>>>>> Read data from source 2 <<<<<<<
	Dir="./2_SL_IMHEN/Converted/"
	Do ist=1, NNST
	   Filein=trim(Dir)//trim(varname)//"/"//trim(varname)//"_"//trim(stname(ist))//".txt"
	   Inquire( file=filein, exist=check )
	   If ( check ) then 
	   Open (2,file=filein,status="old")
	      Read (2,*)
	      Do iyr=1, 50
	         Read (2,*) yeartmp, (DTMP(iyr,imn),imn=1, NMN)
	         iiyr=yeartmp-SYR
	         Do imn=1, NMN
	            If (DAT(ist,iiyr,imn) == flag) then
	               DAT(ist,iiyr,imn)= DTMP(iyr,imn)
	            Endif
	         Enddo
	      Enddo
	   Close(2)
	   Endif
	Enddo
	! >>>>> End of read source 2 <<<<<<


	! >>>>>> Read data from source 3 <<<<<<<
	Dir="./3_SL_Daily/Converted/"

	Do ist=1, NNST
	   filein=trim(Dir)//trim(varname)//"/"//trim(varname)//"_"//trim(stname(ist))//".txt"

	   Inquire( file=filein, exist=check )
	   If ( check ) then
	   Open (2,file=filein,status="old")
	      info=system ('cat '//trim(filein)//' |wc -l > aa')
	      Open (3,file="aa",status="old")
	         Read (3,*) NNYR
	      Close (3)

	      Read (2,*)
	      Read (2,*)
	      Do iyr=1, NNYR-2
	         Read (2,*) yeartmp, (DTMP(iyr,imn),imn=1, NMN)
	         iiyr=yeartmp-SYR
	         Do imn=1, NMN
	            If (DAT(ist,iiyr,imn) == flag) then
	               DAT(ist,iiyr,imn)= DTMP(iyr,imn)
	            Endif
	         Enddo
	      Enddo
	   Close(2)
	   Endif
	Enddo
	! >>>>> End of read source 3 <<<<<<

	! >>>>> Write OUTPUT <<<<<<<<
	Dir="./4_Combine/"
	Do ist=1, NNST
	   filein=trim(Dir)//trim(varname)//"/"//trim(varname)//"_"//trim(stname(ist))//".txt"
	   Open (2,file=filein)
	      Write (2,*) stname(ist)
	      iiyr=SYR+1
	      Do iyr=1, NYR
	         write(2,'i4,12(2x,F8.2)') iiyr, (DAT(ist,iyr,imn),imn=1, NMN)
	         iiyr=iiyr+1
	      Enddo
	   Close (2)
	Enddo
	END
