       program test_read_dataset
c
c  This program a test code to read the dataset described in the paper.
c  While the code was tested and runs on many LINUX platform, there is
c  no warranty explicit or implict that goes with its use.  It is provided 
c  free and and to be used 'as is'.  Users can modifiy as pleased.
c  We do not garranty the validity of outputs obtained from this code.
c
c  Definitions of parameters
c  nr         = number of rows
c  nf         = number of columns
c  nperiod    = number of periods in the biophysical dataset
c  nclass     = number of land cover classes
c  itest      = row nymber of the point to be tested (pinted values)
c  jtest      = column nymber of the point to be tested (pinted values)
c
       implicit none
c
      integer ip,nperiod,nr,nc,nclass,i,j,nf,itest,jtest
      integer l,m
c
       parameter (nr=490,nc=1160,nperiod = 46,nclass=11)
c
c  input : time dependent biophysical data.
c  in the order in whihc they are written/read : FPAR, LAI, Greeness,
c  roghness length, zero-plane displacement, boundary layer resistance,
c  ground-canopy resistance.
c
      real  aparc (nr,nc),zlt (nr,nc),green (nr,nc),z0d    (nr,nc)
      real    dd  (nr,nc),cc1 (nr,nc),cc2   (nr,nc) 
c
c  Define the vegetation type and fraction variables
c
      real    veg  (nr,nc),frac (nr,nc)
c
c  Define file names
c
      character*20 file1(nclass),file2(nclass),file3(nperiod)

      character*30 stng
c
c  Define logical for printing
c
      logical first,test_map_var
c
c  input data
c

      data file1/
     &'LC1_types.bin','LC2_types.bin',
     &'LC3_types.bin','LC4_types.bin',
     &'LC5_types.bin','LC6_types.bin',
     &'LC7_types.bin','LC8_types.bin',
     &'LC9_types.bin','LC11_types.bin',
     &'LC12_types.bin'/
c
      data file2/
     &'LC1_fractions.bin','LC2_fractions.bin',
     &'LC3_fractions.bin','LC4_fractions.bin',
     &'LC5_fractions.bin','LC6_fractions.bin',
     &'LC7_fractions.bin','LC8_fractions.bin',
     &'LC9_fractions.bin','LC11_fractions.bin',
     &'LC12_fractions.bin'/
c
      data file3/
     &'biophys1.bin ','biophys2.bin ','biophys3.bin ','biophys4.bin ',
     &'biophys5.bin ','biophys6.bin ','biophys7.bin ','biophys8.bin ',
     &'biophys9.bin ','biophys10.bin','biophys11.bin','biophys12.bin',
     &'biophys13.bin','biophys14.bin','biophys15.bin','biophys16.bin',
     &'biophys17.bin','biophys18.bin','biophys19.bin','biophys20.bin',
     &'biophys21.bin','biophys22.bin','biophys23.bin','biophys24.bin',
     &'biophys25.bin','biophys26.bin','biophys27.bin','biophys28.bin',
     &'biophys29.bin','biophys30.bin','biophys31.bin','biophys32.bin',
     &'biophys33.bin','biophys34.bin','biophys35.bin','biophys36.bin',
     &'biophys37.bin','biophys38.bin','biophys39.bin','biophys40.bin',
     &'biophys41.bin','biophys42.bin','biophys43.bin','biophys44.bin',
     &'biophys45.bin','biophys46.bin'/
c
c     Set logical flags for testing data       
c
       test_map_var =.true.  
c 
       itest = 208
       jtest = 992   
c
c  Open files per vegetation class
c
         first =.true.
c	  
      do 100 nf = 1, nclass
c
      if (nf.eq.1) then
      open (92,file=
     &'DATA_OUT/BIN/LC1/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC1/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.2) then
      open (92,file=
     &'DATA_OUT/BIN/LC2/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC2/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.3) then
      open (92,file=
     &'DATA_OUT/BIN/LC3/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC3/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.4) then
      open (92,file=
     &'DATA_OUT/BIN/LC4/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC4/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.5) then
      open (92,file=
     &'DATA_OUT/BIN/LC5/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC5/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.6) then
      open (92,file=
     &'DATA_OUT/BIN/LC6/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC6/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.7) then
      open (92,file=
     &'DATA_OUT/BIN/LC7/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC7/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.8) then
      open (92,file=
     &'DATA_OUT/BIN/LC8/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC8/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.9) then
      open (92,file=
     &'DATA_OUT/BIN/LC9/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC9/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.10) then
      open (92,file=
     &'DATA_OUT/BIN/LC11/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC11/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
      if (nf.eq.11) then
      open (92,file=
     &'DATA_OUT/BIN/LC12/'//file1(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      open (93,file=
     &'DATA_OUT/BIN/LC12/'//file2(nf),status='old',form='unformatted',
     &access='direct',recl=4*nr*nc)
      endif
c
c     Read vegetation and fraction data from SOUTH to NORTH
c
      read(92,rec=1)   ((veg   (i,j),j=1,nc),i=1,nr)
      read(93,rec=1)   ((frac  (i,j),j=1,nc),i=1,nr)
c
c    Detemine the minimum and maximum values of the vegetation type and fractions
c    remove the comment line to display min-max values of fields.
c
      stng=' vegtype  '
c      call minmax1   (veg ,nr,nc,veg,stng)                                   
      stng=' fraction '
c      call minmax1   (frac,nr,nc,veg,stng)
c
c Read tape 89 for all time period
c
c
      do 200 ip = 1, nperiod          
c	  
c  Open files containing all variables for each period
c
      if (nf.eq.1)open (90,file=
     &'DATA_OUT/BIN/LC1/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.2)open (90,file=
     &'DATA_OUT/BIN/LC2/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.3)open (90,file=
     &'DATA_OUT/BIN/LC3/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.4)open (90,file=
     &'DATA_OUT/BIN/LC4/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.5)open (90,file=
     &'DATA_OUT/BIN/LC5/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.6)open (90,file=
     &'DATA_OUT/BIN/LC6/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.7)open (90,file=
     &'DATA_OUT/BIN/LC7/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.8)open (90,file=
     &'DATA_OUT/BIN/LC8/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.9)open (90,file=
     &'DATA_OUT/BIN/LC9/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.10)open (90,file=
     &'DATA_OUT/BIN/LC11/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
      if (nf.eq.11)open (90,file=
     &'DATA_OUT/BIN/LC12/'//file3(ip),status='old',form='unformatted',
     &access='direct',recl=7*4*nr*nc)
c
c     Read biophysical data from SOUTH to NORTH
c

900   format (1160(f7.2,1x))    
      read(90,rec= 1)((aparc (i,j),j=1,nc),i=1,nr)                            
     &,              ((zlt   (i,j),j=1,nc),i=1,nr)                              
     &,              ((green (i,j),j=1,nc),i=1,nr)
     &,              ((z0d   (i,j),j=1,nc),i=1,nr)                              
     &,              ((dd    (i,j),j=1,nc),i=1,nr)                               
     &,              ((cc1   (i,j),j=1,nc),i=1,nr)                              
     &,              ((cc2   (i,j),j=1,nc),i=1,nr)                              
c
      if (test_map_var.and.first) then
c           
      print *, '       '
      print *, ' Test Vegetation, fraction and LAI values for point'
     &, itest,jtest         
	   print *, '       '
	   print *, ' Period   ','  VEGETATION ',' FRACTION   ',' LAI    ' 
	   print *, '       '
	   first=.false.
      endif
c
      if (test_map_var.and.ip.eq.18) then
c
 66   format(1x,i6,4x,f8.2,4x,f8.2,4x,f8.2)
      write(6,66)ip,veg(itest,jtest),frac(itest,jtest)
     &,zlt (itest,jtest)
c       
      endif
c
200    continue
c
100   continue 
      stop 99
       end
c
      subroutine minmax1 (data,l,m,veg,stng)                                   
      real data (l,m),veg(l,m)
      real amin,amax
      character*30 stng
      amax      = -999.0                                                            
      amin      = +999.0                                                          
      do 3250   j = 1, m                                                               
      do 3250   i = 1, l
       if (veg (i,j).ne.-999.0) then 
          if (data(i,j).gt.amax) amax = data(i,j)                                      
          if (data(i,j).lt.amin) amin = data(i,j)
       endif
3250  continue 
  86  format(a20,'  :',2f8.2)
      write(6,86)stng,amin,amax                                                
      return                                                                
      end                                                                       
