extract Subroutine

private pure subroutine extract(self, extractedLevels, name, zmin, zmax, reverse)

Extracts colors from the colormap based on specified number of levels (nl)

Type Bound

Colormap

Arguments

Type IntentOptional Attributes Name
class(Colormap), intent(inout) :: self
integer, intent(in) :: extractedLevels
character(len=*), intent(in), optional :: name
real(kind=wp), intent(in), optional :: zmin
real(kind=wp), intent(in), optional :: zmax
logical, intent(in), optional :: reverse

Calls

proc~~extract~~CallsGraph proc~extract Colormap%extract proc~create Colormap%create proc~extract->proc~create proc~reverse Colormap%reverse proc~extract->proc~reverse proc~scale_real_int scale_real_int proc~extract->proc~scale_real_int proc~create->proc~reverse proc~check Colormap%check proc~create->proc~check proc~scale_real_real scale_real_real proc~scale_real_int->proc~scale_real_real proc~get_levels~2 Colormaps_info%get_levels proc~check->proc~get_levels~2 proc~get_name~2 Colormaps_info%get_name proc~check->proc~get_name~2 proc~get_ncolormaps Colormaps_info%get_ncolormaps proc~check->proc~get_ncolormaps proc~set_all Colormaps_info%set_all proc~check->proc~set_all proc~set_info table%set_info proc~set_all->proc~set_info

Called by

proc~~extract~~CalledByGraph proc~extract Colormap%extract program~extract extract program~extract->proc~extract

Source Code

    pure subroutine extract(self, extractedLevels, name, zmin, zmax, reverse)
        class(Colormap), intent(inout) :: self
        integer, intent(in) :: extractedLevels
        character(*), intent(in), optional :: name
        real(wp), intent(in), optional :: zmin, zmax
        logical, intent(in), optional :: reverse
        integer :: extracted_map(extractedLevels,3)
        integer :: ind(extractedLevels,3)
        real(wp) :: ind_rel(extractedLevels,3), array_rel(self%levels,3), step(3), current_element(3)
        integer :: i
        integer, dimension(self%levels,3) :: array
        character(3) :: extractedLevels_char

        ! Initialize array with indices
        do concurrent (i = 1: self%levels)
            array(i,:) = i-1
        end do

        ! Normalize array elements to the range [0, 1]
        do concurrent (i = 1: 3)
            array_rel(:,i) = array(:,i)/ maxval(array(:,i))
        end do

        ! Check if the number of extractedLevels is valid
        if (extractedLevels <= 1 .or. extractedLevels > self%levels) then
            error stop "Error: Invalid number of extractedLevels. Must be > 1 and <= levels"
        end if

        step(:) = array_rel(self%levels,:) / real(extractedLevels-1, kind=wp)

        current_element(:) = array_rel(1,:)

        do i = 1, extractedLevels
            ind_rel(i,:) = current_element
            current_element = current_element + step
        end do

        ! Scale interpolated indices to integers between 0 and self%levels - 1
        do concurrent (i = 1:3)
            ind(:,i) = scale_real_int(ind_rel(:,i), 0, self%levels-1)
        end do

        ! Extract colors from the colormap based on interpolated indices
        do concurrent (i = 1: 3)
            extracted_map(:,i) = self%map(ind(:,i),i)
        end do

        ! Set colormap name if provided, otherwise use the number of levels as part of the name
        if (present(name)) then
            self%name = name
        else
            write(extractedLevels_char, '(I3)') extractedLevels
            self%name = self%name//trim(extractedLevels_char)
        end if

        ! Set zmin and zmax if provided
        if (present(zmin)) self%zmin = zmin
        if (present(zmax)) self%zmax = zmax

        ! Create the extracted colormap with the specified parameters
        call self%create(self%name, self%zmin, self%zmax, extracted_map)

        if (present(reverse)) then
            if (reverse) call self%reverse()
        end if
    end subroutine extract