 subroutine remove_DZ(nobs,nwrf,rr_obs,rr_wrf,rr_wrf_new,threshold,day_obs_wiDZ,day_wrf_woDZ,day_wrf_wiDZ)

 implicit none
 
 integer, intent(in) :: nobs
 integer, intent(in) :: nwrf
 real,    intent(in) :: rr_obs(nobs)
 real,    intent(in) :: rr_wrf(nwrf)
 real,    intent(out):: rr_wrf_new(nwrf)
 real,    intent(out):: threshold(1)
 real,    intent(out):: day_obs_wiDZ(1)
 real,    intent(out):: day_wrf_woDZ(1)
 real,    intent(out):: day_wrf_wiDZ(1)

 real                :: limit 
 integer             :: day_sum_wrf, day_sum_obs, day_sum_obs_wiDZ, day_sum_obs_woDZ, day_sum_wrf_woDZ, day_sum_wrf_wiDZ
 integer             :: t, nt, tt, t2


 day_sum_wrf      = 0
 day_sum_obs      = 0
 day_sum_obs_wiDZ = 0
 day_sum_wrf_woDZ = 0
 day_sum_wrf_wiDZ = 0
 day_obs_wiDZ     = -999
 day_wrf_woDZ     = -999
 day_wrf_wiDZ     = -999
 rr_wrf_new       = -999
 threshold        = -999
 
       limit = 0.005
       do tt = 1,nobs  
          if (rr_obs(tt).ge.0.5) then
             day_sum_obs_woDZ = day_sum_obs + 1
          end if 
          if (rr_obs(tt).gt.0.0) then
             day_sum_obs_wiDZ = day_sum_obs_wiDZ + 1
          end if
       end do 
       day_sum_obs = day_sum_obs_wiDZ  
       day_sum_obs_wiDZ = 0
       day_sum_obs_woDZ = 0

100    if (limit.gt.0.1) then
         day_sum_wrf = 0
       else
         do t = 1,nwrf
           if (rr_wrf(t).ge.limit) then
            day_sum_wrf = day_sum_wrf + 1
           end if
         end do
       end if

       if (day_sum_wrf .gt. day_sum_obs) then
         limit = limit + 0.005
         day_sum_wrf = 0
         GO TO 100
       else
         day_sum_wrf      = 0
         day_sum_obs      = 0
         limit = limit - 0.001 
         threshold = limit

         do nt = 1,nwrf
            if (rr_wrf(nt).le. limit) then
               rr_wrf_new(nt) = 0.0
            else
               rr_wrf_new(nt) = rr_wrf(nt)
            end if
         end do


       end if 

         day_sum_wrf_wiDZ = 0
         day_sum_wrf_woDZ = 0

       do t2 = 1,nwrf

           if (rr_wrf(t2).gt.0.0) then
            day_sum_wrf_wiDZ = day_sum_wrf_wiDZ + 1
           end if

           if (rr_wrf_new(t2).gt.0.0) then
            day_sum_wrf_woDZ = day_sum_wrf_woDZ + 1
           end if

       end do

         day_wrf_wiDZ = day_sum_wrf_wiDZ 
         day_wrf_woDZ = day_sum_wrf_woDZ 
        

  return

  end subroutine remove_DZ
 

 


