      program ecm2rams

      integer sYear, sMonth, sDay, sHour
      integer eYear, eMonth, eDay, eHour
      real dtDay
      parameter(imax=41,jmax=41,kmax=21,nmax=6,wlon=130,slat=30)
      parameter(sYear =2000, sMonth =4 , sDay = 20 , sHour = 0)
      parameter(eYear =2000, eMonth =4 , eDay = 28, eHour = 18)
      parameter(dtDay = 0.25)
      real dx,dy,d1(imax,jmax,kmax,nmax),dtmp
      real sDate, eDate
      integer numdata,press,roop,levpr(kmax),fake(6)
      integer yy, mm, dd, hh
      character fname1*10,fname2*11
c---- for 2.5 degree data format-----
c      data levpr/ 1000, 850, 700, 500, 400, 300, 250, 200,
c     &             150, 100,  70,  50,  30/
c      data fake/4,3,1,2,6,5/
c-----for 0.5 degree data format-----
      data levpr/ 1000, 925, 850, 700, 500, 400, 300, 250, 200,
     &             150, 100, 70, 50, 30, 20, 10, 7, 5, 3, 2, 1/
      data fake/4,3,6,5,1,2/
      
      sDate = Date2Real(sYear, sMonth, sDay, sHour)
      eDate = Date2Real(eYear, eMonth, eDay, eHour)
      roop = nint((eDate - sDate) / dtDay) + 1
      
      do ii=1,roop

      call Real2Date(sDate + dtDay * (ii - 1), yy, mm, dd, hh)
      write(fname1, 100) yy , mm, dd, hh
      write(fname2, 110) yy , mm, dd, hh

 100  format(i4.4,3(i2.2))
 110  format('E',i4.4,3(i2.2))

      open(10+ii,file=fname1,status='old')
      open(100+ii,file=fname2,status='unknown')

      do k=1,kmax
       do n=1,nmax
      read(10+ii,'(i10)') inumdata
      read(10+ii,'(i10)') ipress
      read(10+ii,'(4i9)') iyear,imonth,idate,ihour
      read(10+ii,'(2i9,2f9.0)') im,jm,dx,dy
        do j=1,jmax
         do i=1,imax
          read(10+ii,*) dtmp
          if (n.eq.1) then
             d1(i,j,k,fake(n)) = dtmp / 9.8
c---- for 2.5 degree data format-----
c          else if (n.eq.6) then
c---- for 0.5 degree data format-----
          else if (n.eq.4) then
             if (dtmp.gt.100) dtmp = 100.
             d1(i,j,k,fake(n)) = dtmp / 100.
          else
             d1(i,j,k,fake(n)) = dtmp
          end if
         end do
        end do
       end do
      end do

      dx=dx/1000
      dy=dy/1000
      
      write(100+ii,120) iyear,imonth,idate,ihour,kmax,im,jm,
     +     wlon,slat,dx,dy,(levpr(k),k=1,kmax)
      
 120  format(7i4,4f8.2,/,(13i6))
      
      do k=1,kmax
       do n=1,5
        write(100+ii,130) ((d1(i,j,k,n),i=1,imax),j=jmax,1,-1)
       end do
      end do
      
      close(100+ii)
      close(10+ii)
      
      end do
      
 130  format(8f10.3)
      
      stop
      end
      
      
c****************************************************
      real function Date2Real(yy, mm, dd, hh)
c****************************************************
c(*)
      parameter(ioYear=1980)
      integer yy, mm, dd, hh
      integer YearSum, i, isUruudoshi, year, MDays(12)
      real DaySum
      data MDays / 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/
c
      DaySum = 0.
      YearSum = yy - ioYear
      do i = 0, YearSum
          isUruudoshi = 0
          year = ioYear + i
          if(mod(year, 4).eq.0) then
              isUruudoshi = 1
              if(mod(year, 100).eq.0) then
                  isUruudoshi = 0
                  if(mod(year, 400).eq.0) then
                      isUruudoshi = 1
                  end if
              end if
          end if
          if(isUruudoshi.eq.1) then
              DaySum = DaySum + 366.
          else
              DaySum = DaySum + 365.
          end if
      end do
      do i = 1, mm - 1
          DaySum = DaySum + MDays(i)
      end do
      if(isUruudoshi.eq.1) then
          DaySum = DaySum - 366
          if(mm.ge.3) then
              DaySum = DaySum + 1
          end if
      else
          DaySum = DaySum - 365
      end if
      DaySum = DaySum + dd + hh / 24.0
      Date2Real = DaySum
      return
      end
c
c****************************************************
      subroutine Real2Date(days, yy, mm, dd, hh)
c****************************************************
c(*)
      parameter(ioYear=1980)
      real days
      integer yy, mm, dd, hh
      integer MDays(12), idays, i, year, isUruudoshi
      real work
      data MDays / 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/
c
      work = days - int(days)
      idays = nint(days - work)
      yy = ioYear
      mm = 1
      hh = nint(24.0 * work)
c
      do i = 0, 100
          isUruudoshi = 0
          year = ioYear + i
          if(mod(year, 4).eq.0) then
              isUruudoshi = 1
              if(mod(year, 100).eq.0) then
                  isUruudoshi = 0
                  if(mod(year, 400).eq.0) then
                      isUruudoshi = 1
                  end if
              end if
          end if
          if(isUruudoshi.eq.1) then
              if(idays.lt.366) then
                  go to 100
              else
                  idays = idays - 366
                  yy = yy + 1
              end if
          else
              if(idays.lt.365) then
                  go to 100
              else
                  idays = idays - 365
                  yy = yy + 1
              end if
          end if
      end do
 100  continue
      if(isUruudoshi.eq.1) then
          MDays(2) = 29
      end if
      do i = 1, 13
          if(idays.le.MDays(i)) then
              go to 200
          else
              idays = idays - MDays(i)
              mm = mm + 1
          end if
      end do
 200  continue
      dd = idays
c
      return
      end
