
       implicit none
       integer   k,kk,kkk,kpass,pass,irec,ks,iyr,ddd,no1,no2
       integer   mon,dd,regs,vars,days,i,j,reg,kkc,ttt
       parameter (regs=3,vars=6,days=10*366)
       real*4    aa(vars,regs,days),missing
       character infile1*58,bb1*40,bb2*4,bb3*1,regc(3)*2,regc0*2
       data regc/"na","np","sp"/
c************************************************************************
       open(21,file='data/plot-files-list.dat',form='formatted')
       open(22,file='data/plot-files-list-cp.dat',form='formatted')

       open(8,file='data/plot_stat_values.dat',
     & form='unformatted', status='unknown', access='direct',
     & recl=4*vars)
       open(9,file='GRADS_TIME_LNTH',form='formatted')

       missing=-999
c************************************************************************
       do i=1,vars
       do j=1,regs
       do k=1,days
       aa(i,j,k)=missing
       enddo
       enddo
       enddo
c************************************************************************

       irec=0
 120   continue
       read(21,101,end=140) infile1
       write(96,*) infile1

       read(22,103) bb2,regc0,bb2,iyr,bb3,ddd

       call pick_days_time(iyr,ddd,ttt)

       if(regc0.eq.regc(1)) kkc=1
       if(regc0.eq.regc(2)) kkc=2
       if(regc0.eq.regc(3)) kkc=3
       write(96,'(x,a2,x,i3,3i6)') regc0,kkc,iyr,ddd,ttt
c************************************************************************

       open(31,file= infile1,form='formatted')
       read(31,105) (aa(i,kkc,ttt),i=1,vars)
c************************************************************************

       irec=irec+1
       goto 120
 140   continue
c       write(96,*) irec
c************************************************************************

       irec=1
       do k=1,days
       do j=1,regs
       write(8,rec=irec) (aa(i,j,k),i=1,vars)
       irec=irec+1
       enddo
       write(95,*) aa(1,3,k)
       enddo

      write(9,'(i5)') ttt
c************************************************************************

 101   format(a103)
 103   format(a40,a2,a4,i4,a1,i3.3)
 105   format(6f14.5)
       close(8)
       stop
       end

      subroutine pick_days_time(iyr,ddd,ttt)
      implicit none
      integer iyr,ddd,k,kk,yy,year,ddd0,kmons,ttt
      real*4  xx

      kk=iyr-2021+1
      if(kk.eq.1) ttt=ddd

      ddd0=0
c     ********************************************************
      if(kk.gt.1) then

      do year=2021,iyr-1
      xx=float(year)/4.0
      yy=int(xx)

      if(float(yy).eq.xx) ddd0=ddd0+366
      if(float(yy).ne.xx) ddd0=ddd0+365
      enddo

      ttt=ddd0+ddd
      endif
c     ********************************************************
c     ********************************************************
      return
      end



