c     gt_boxcox.f
c
c     Perform Box Cox transformation on regional Gt
c
      use commondata, only : ensmem,mx,my,seasons,area,boxcox,tmp_path,
     & lambda_path,date,time

      implicit none

      real :: values(20000),results(20000),lambda(20000)
      integer :: iyear,imonth,iday,ihour,valid,mem,runs,k,i
      character(len=50) :: sno(3),snd(3),smem,input,model,a

      data snd /'_r','_Gt_','.txt'/
      data sno /'_r','_trGt_','.txt'/

c      call idate(date)
c      call itime(time)
c      write(*,1000) date(2),date(1),date(3)+2000,time
c1000  format('gt_boxcox start at: Date ', i2.2, '/', i2.2, '/', i4.4,
c     &         '; time ',i2.2, ':', i2.2, ':', i2.2 )

      call get_command_argument(1,input)
      read(input,*) model
      call get_command_argument(2,input)
      read(input,*) runs

      do k=1,4

         valid=0
         open(37,file=trim(lambda_path)//trim(boxcox(1))//
     & trim(seasons(k))//trim(boxcox(2)))
         do
            read(37,*,end=77) a
            valid=valid+1
         enddo
77       close(37)
         open(38,file=trim(lambda_path)//trim(boxcox(1))//
     & trim(seasons(k))//trim(boxcox(2)))
         do i=1,valid
            read(38,*) a,a,lambda(i)
         enddo
         close(38)

         do mem=runs,runs
         smem=ensmem(mem)

         open(10,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(seasons(k))//trim(sno(3)))
         open(2,file=trim(tmp_path)//trim(model)//trim(snd(1))//
     & trim(smem)//trim(snd(2))//trim(seasons(k))//trim(snd(3)))
C        print*,trim(model)//trim(sno(1))//trim(smem)//trim(sno(2))//
C    & trim(seasons(k))//trim(sno(3))
         
         do
            read(2,*,end=99) iyear,imonth,iday,ihour
            read(2,'(10f9.3)') (values(i),i=1,valid)

            do i=1,valid
               if(values(i).lt.-899.99) then
                  results(i)=-900.99
               else
                  if(abs(lambda(i)).lt.1E-5.and.
     &            abs(lambda(i)).ge.0) then
                     if(abs(values(i)).lt.1E-5) then
                        values(i)=0.0001
                     endif
                     results(i)=log(values(i))
                  else
                     if(abs(values(i)).lt.1E-5.and.
     &               lambda(i).lt.0) then
                        values(i)=0.0001
                     endif
                     results(i)=((values(i)**lambda(i))-1)/
     &               lambda(i)
                  endif
               endif
               if(abs(results(i)).gt.100000) then
                  print*,iyear,imonth,iday,ihour
                  print*,i,results(i),values(i),lambda(i)
                  stop
               endif
            enddo
            write(10,'(i6,3i3,i6)') iyear,imonth,iday,ihour,valid
            write(10,'(10f9.3)') (results(i),i=1,valid)
         enddo
99       close(2)
         close(10)
         enddo

      enddo
      
c      call idate(date)
c      call itime(time)
c      write(*,2000) date(2),date(1),date(3)+2000,time
c2000  format('gt_boxcox end at: Date ', i2.2, '/', i2.2, '/', i4.4,
c     &         '; time ',i2.2, ':', i2.2, ':', i2.2 )

      end
