run_length_encode Function

public function run_length_encode(missing) result(str)

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: missing(:)

Return Value character(len=:), allocatable


Called by

proc~~run_length_encode~~CalledByGraph proc~run_length_encode run_length_encode proc~regrid regrid proc~regrid->proc~run_length_encode proc~write_data~2 RegridSupport%write_data proc~write_data~2->proc~regrid program~main~17 main program~main~17->proc~write_data~2

Source Code

   function run_length_encode(missing) result(str)
      character(len=:), allocatable :: str
      logical, intent(in) :: missing(:)

      integer :: i
      integer :: count
      logical :: value

      if (size(missing) == 0) then
         str = ''
         return
      end if

      count = 1
      value = missing(1)
      str = to_string_bool(value)

      do i = 2, size(missing)
        if (value .eqv. missing(i)) then
           count = count + 1
        else
           value = missing(i)
           str = str // to_string(count) // to_string_bool(value)
           count = 1
        end if
     end do

     str = str // to_string(count)

   contains

      function to_string(count) result(str)
         character(len=:), allocatable :: str
         integer, intent(in) :: count
         character(len=8) :: buffer

         write(buffer,'(i0)') count
         str = trim(buffer)
      end function to_string

      function to_string_bool(bool) result(str)
         character(len=1) :: str
         logical, intent(in) :: bool
         if (bool) then
            str = 'T'
         else
            str = 'F'
          end if
       end function to_string_bool

   end function run_length_encode