resize_attribute Subroutine

public subroutine resize_attribute(attribute, new_size)

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(inout), allocatable :: attribute(:)
integer, intent(in) :: new_size

Contents

Source Code


Source Code

    subroutine resize_attribute(attribute, new_size)
        ! take an array and expand/shrink it to a new size
        ! Input: attribute - array to resize
        !        new_size - new size of the array. If larger than the current one,
        !                   data will be duplicated, if smaller, data will be deleted
        implicit none
        integer, intent(in) :: new_size
        real(dp), allocatable, intent(inout) :: attribute(:)

        real(dp), allocatable :: tmp(:)
        integer :: old_size
        integer :: ierr

        old_size = size(attribute)
        !store the old entries
        allocate(tmp(old_size), stat=ierr)
        tmp(:) = attribute(:)

        deallocate(attribute)
        allocate(attribute(new_size), stat=ierr)

        ! resize
        if (old_size < new_size) then
            attribute(1:old_size) = tmp(1:old_size)
            attribute(old_size + 1:new_size) = tmp(old_size)
        else
            attribute(1:new_size) = tmp(1:new_size)
        end if

        deallocate(tmp)
    end subroutine resize_attribute