      Program read_all
c
      implicit none
c
      integer ni, nj, nk, nt, sizeofreal
      parameter (ni=181, nj=91, nk=10, nt=21)
      parameter (sizeofreal=4)
      integer i, j, k
      integer*4  irec
      character*8 direc
      character*70 filea,fileb,filec,filed,filee,filen,filet
      character*70 filenb
      character*90 direca
      character*1 ens
      real dum, es, qs, tc
      real 
     &     u850u850(ni,nj,nt), u850v850(ni,nj,nt),
     &     u850u250(ni,nj,nt), u850v250(ni,nj,nt),
     &     v850v850(ni,nj,nt), v850u250(ni,nj,nt),
     &     v850v250(ni,nj,nt), u250u250(ni,nj,nt),
     &     u250v250(ni,nj,nt), v250v250(ni,nj,nt),
     &     pi(ni,nj,nt), u850(ni,nj,nt), v850(ni,nj,nt),
     &     u250(ni,nj,nt), v250(ni,nj,nt),
     &     T600(ni,nj,nt), Q600(ni,nj,nt)
c
	real*8 u850u8508(ni,nj,nt),u850v8508(ni,nj,nt),
     &         u250u8508(ni,nj,nt),v850v8508(ni,nj,nt),
     &         v250v8508(ni,nj,nt),u250u2508(ni,nj,nt),
     &         u250v2508(ni,nj,nt),v250v2508(ni,nj,nt),
     &         pi8(ni,nj,nt),u8508(ni,nj,nt),v8508(ni,nj,nt),
     &         u2508(ni,nj,nt),v2508(ni,nj,nt),
     &         T6008(ni,nj,nt),rh8(ni,nj,nt)
c
	read*, filen,filet
        filenb=filet
	if(filet(1:2).eq.'AL')filenb(1:2)='al'  
        if(filet(1:2).eq.'EP')filenb(1:2)='ep'
	if(filet(1:2).eq.'WP')filenb(1:2)='wp'	
        if(filet(1:2).eq.'IO')filenb(1:2)='io'
        if(filet(1:2).eq.'SH')filenb(1:2)='sh'
c
       filea=TRIM(filenb)//'/Filtered_tight2sigma2_UVcov_ens_'//
     1       TRIM(filen)//'.gdat'
c      fileb='ens_mean_Vmax.'//TRIM(filen)//'.gdat'
      fileb='Vmax.'//TRIM(filen)//'.gdat'
      filec=TRIM(filenb)//'/Filtered_tight2sigma2_ens_mean_UV_'//
     1   TRIM(filen)//'.gdat'
c      filed='ens_mean_600hPa_Q_T.'//TRIM(filen)//'.gdat'
      filed='Q_T_600hPa.'//TRIM(filen)//'.gdat'
      filee=TRIM(filenb)//'/Full_ens_mean_UV_'//TRIM(filen)//'.gdat'
c
      direca='data/ecmwf/'
      filen=TRIM(filen)//'/'//TRIM(filet)
      direca=TRIM(direca)//TRIM(filen)
c
      OPEN(8,FILE=TRIM(filea),FORM='UNFORMATTED',
     & convert='big_endian',ACCESS='DIRECT', recl=ni*nj*sizeofreal)

      irec=1 

	do k=1,nt

      read (8,REC=irec) ((u850u850(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((u850v850(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((u850u250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((u850v250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((v850v850(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((v850u250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((v850v250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((u250u250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((u250v250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (8,REC=irec) ((v250v250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1
c
	end do
c
      close(8)
c
	do i=1,ni
	 do j=1,nj
	  do k=1,nt
	  u250u2508(i,j,k)=u250u250(i,j,k)
	  u250v2508(i,j,k)=u250v250(i,j,k)
	  u250u8508(i,j,k)=u850u250(i,j,k)
	  u850u8508(i,j,k)=u850u850(i,j,k)
	  u850v8508(i,j,k)=u850v850(i,j,k)
	  v250v2508(i,j,k)=v250v250(i,j,k)
	  v250v8508(i,j,k)=v850v250(i,j,k)
	  v850v8508(i,j,k)=v850v850(i,j,k)
	 end do
	end do
	end do
c
        open(unit=12,file=TRIM(direca)//'/u250u250',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u250u2508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/u250v250',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u250v2508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/u250u850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u250u8508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/u850u850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u850u8508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/u850v850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u850v8508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/v250v250',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)v250v2508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/v250v850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)v250v8508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/v850v850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)v850v8508
        close(12)
c
      OPEN(8,FILE=TRIM(fileb),FORM='UNFORMATTED',
     & convert='big_endian',ACCESS='DIRECT', recl=ni*nj*sizeofreal)

      irec=1
c
      do k=1,nt

         read (8,REC=irec) ((pi(i,j,k),i=1,ni),j=1,nj)

         irec=irec+1
      end do

      close(8)
c
        do i=1,ni
         do j=1,nj
          do k=1,nt
          pi8(i,j,k)=pi(i,j,k)
         end do
        end do
        end do
c
        open(unit=12,file=TRIM(direca)//'/pi',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)pi8
        close(12)
c
      OPEN(8,FILE=TRIM(filec),FORM='UNFORMATTED',
     & convert='big_endian',ACCESS='DIRECT', recl=ni*nj*sizeofreal)
c
      OPEN(9,FILE=TRIM(filee),FORM='UNFORMATTED',
     & convert='big_endian',ACCESS='DIRECT', recl=ni*nj*sizeofreal)

      irec=1

        do k=1,nt

      read (8,REC=irec) ((u850(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1
c
      read (8,REC=irec) ((v850(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (9,REC=irec) ((u250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

      read (9,REC=irec) ((v250(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

        end do

      close(8)
      close(9)
c
        do i=1,ni
         do j=1,nj
          do k=1,nt
          u2508(i,j,k)=u250(i,j,k)
          v2508(i,j,k)=v250(i,j,k)
          u8508(i,j,k)=u850(i,j,k)
          v8508(i,j,k)=v850(i,j,k)
         end do
        end do
        end do
c
c
        open(unit=12,file=TRIM(direca)//'/u250',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u2508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/v250',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)v2508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/u850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)u8508
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/v850',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)v8508
        close(12)
c
      OPEN(8,FILE=TRIM(filed),FORM='UNFORMATTED',
     & convert='big_endian',ACCESS='DIRECT', recl=ni*nj*sizeofreal)

      irec=1

        do k=1,nt

      read (8,REC=irec) ((T600(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1
c
      read (8,REC=irec) ((Q600(i,j,k),i=1,ni),j=1,nj)
      irec=irec+1

        end do

      close(8)
c
        do i=1,ni
         do j=1,nj
          do k=1,nt
          T6008(i,j,k)=T600(i,j,k)
          tc=T600(i,j,k)-273.15
          es=6.112*exp(17.67*tc/(243.5+tc))
          qs=0.622*es/(600.0-es)
          dum=100.0*Q600(i,j,k)/qs
          dum=min(dum,100.0)
          rh8(i,j,k)=max(dum,0.0)
         end do
        end do
        end do
c
        open(unit=12,file=TRIM(direca)//'/T600',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)T6008
        close(12)
c
        open(unit=12,file=TRIM(direca)//'/rh',access='direct',
     1   status='unknown',recl=8*ni*nj*nt)
        write(12,rec=1)rh8
        close(12)
c
      end
