       program readdata
       parameter (ny=2021-1978, nday=31+30+31+30+31)
c  nday: March - July
       parameter (ni=81, nj=41)

       real td(ni,nj,21), td7(ni,nj,nday,ny)

       dimension nb(ni,nj,21)

       character*8 cyr, cmon, cday, cmag

       open(21,file='comp.mod_average.map.gr',
     1  access='direct',form='unformatted',recl=ni*nj*21*4)

       open(10,file='td.MAMJj.1979-2021.7d-mean.map.gr',
     1  access='direct',form='unformatted',recl=ni*nj*nday*ny*4)

       read(10,rec=1) 
     1  ((((td7(i,j,nd,iy),i=1,ni),j=1,nj),nd=1,nday),iy=1,ny)

       open(11, file='../MJO/mod_average_year')
       open(12, file='../MJO/mod_average_month')
       open(13, file='../MJO/mod_average_day_94L')


       read(11,*) cyr
       read(12,*) cmon
       read(13,*) cday

       print *, 'LSR:    ', cyr, '     ',cmon,'    ',cday

       do n=1,21
       do i=1,ni
       do j=1,nj
        td(i,j,n)=0.0
        nb(i,j,n)=0
       end do
       end do
       end do

       ntot=94-1

       do ii=1,ntot
       read(11,*) iyear 
       read(12,*) imon
       read(13,*) iday

       if(ii.le.10) then
         print *, iyear, imon, iday
       end if

c Year:  1979-2021
c Month: March - July
       if(iyear.ge.1979.and.iyear.le.2021) then
       if(imon.ge.3.and.imon.le.7) then

         iy=iyear - 1978

         if(imon.eq.3) nd=iday
         if(imon.eq.4) nd=iday+31
         if(imon.eq.5) nd=iday+61
         if(imon.eq.6) nd=iday+92
         if(imon.eq.7) nd=iday+122 

c March - July: 31+30+31+30+31 = 153 days
         do i=1,ni
         do j=1,nj
         do n=1,21 
         td(i,j,n)=td(i,j,n) + td7(i,j,nd+n-1,iy)
         nb(i,j,n)=nb(i,j,n) + 1
         end do
         end do
         end do

       end if
       end if

       end do

c 7-day mean: March - June

cc       write(*,*) td

       do i=1,ni
       do j=1,nj
       do n=1,21
       if(nb(i,j,n).ne.0) td(i,j,n)=td(i,j,n)/real(nb(i,j,n))
       end do
       end do
       end do

       write(21,rec=1) (((td(i,j,n),i=1,ni),j=1,nj),n=1,21)

       stop
       end
      
