execute Subroutine

private subroutine execute(me, pyfile, istat, python)

Write the buffer to a file, and then execute it with Python.

If user specifies a Python file name, then the file is kept, otherwise a temporary filename is used, and the file is deleted after it is used.

Type Bound

pyplot

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pytplot handler

character(len=*), intent(in), optional :: pyfile

name of the python script to generate

integer, intent(out), optional :: istat

status output (0 means no problems)

character(len=*), intent(in), optional :: python

python executable to use. (by default, this is 'python')


Called by

proc~~execute~~CalledByGraph proc~execute pyplot%execute proc~savefig pyplot%savefig proc~savefig->proc~execute proc~showfig pyplot%showfig proc~showfig->proc~execute

Source Code

    subroutine execute(me, pyfile, istat, python)

    class(pyplot),    intent(inout)         :: me     !! pytplot handler
    character(len=*), intent(in),  optional :: pyfile !! name of the python script to generate
    integer,          intent (out),optional :: istat  !! status output (0 means no problems)
    character(len=*), intent(in),optional   :: python !! python executable to use. (by default, this is 'python')

    integer                       :: iunit   !! IO unit
    character(len=:), allocatable :: file    !! file name
    logical                       :: scratch !! if a scratch file is to be used
    integer                       :: iostat  !! open/close status code
    character(len=:), allocatable :: python_ !! python executable to use

    if (allocated(me%str)) then

        if (present(istat)) istat = 0

        scratch = (.not. present(pyfile))

        !file name to use:
        if (scratch) then
            file = trim(tmp_file)  !use the default
        else
            file = trim(pyfile)    !use the user-specified name
        end if

        if (file == '') then
            if (present(istat)) istat = -1
            write(error_unit,'(A)') 'Error: filename is blank.'
            return
        end if

        !open the file:
        open(newunit=iunit, file=file, status='REPLACE', iostat=iostat)
        if (iostat/=0) then
            if (present(istat)) istat = iostat
            write(error_unit,'(A)') 'Error opening file: '//trim(file)
            return
        end if

        !write to the file:
        write(iunit, '(A)') me%str

        !to ensure that the file is there for the next
        !command line call, we have to close it here.
        close(iunit, iostat=iostat)
        if (iostat/=0) then
            if (present(istat)) istat = iostat
            write(error_unit,'(A)') 'Error closing file: '//trim(file)
        else

            if (present(python)) then
                python_ = trim(python)
            else
                python_ = python_exe
            end if

            !run the file using python:
            if (file(1:1)/='"') then
                ! if not already in quotes, should enclose in quotes
                call execute_command_line(python_//' "'//file//'"')
            else
                call execute_command_line(python_//' '//file)
            end if

            if (scratch) then
                !delete the file (have to reopen it because
                !Fortran has no file delete function)
                open(newunit=iunit, file=file, status='OLD', iostat=iostat)
                if (iostat==0) close(iunit, status='DELETE', iostat=iostat)
            end if
            if (iostat/=0) then
                if (present(istat)) istat = iostat
                write(error_unit,'(A)') 'Error closing file.'
            end if

        end if

        !cleanup:
        if (allocated(file)) deallocate(file)

    else
        if (present(istat)) istat = -1
    end if

    end subroutine execute