Skip to content

Commit 645184e

Browse files
committed
If not specifying a python file name, then it is deleted after used. Fixes #5.
Some other minor formatting restorations.
1 parent c9983ce commit 645184e

File tree

2 files changed

+105
-82
lines changed

2 files changed

+105
-82
lines changed

src/pyplot_module.f90

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
107107
integer, intent(in), optional :: xtick_labelsize !! size of x axis tick lables
108108
integer, intent(in), optional :: ytick_labelsize !! size of y axis tick lables
109109
integer, intent(in), optional :: legend_fontsize !! size of legend font
110-
character(len=max_int_len) :: width_str !! figure width dummy string
111-
character(len=max_int_len) :: height_str !! figure height dummy string
112-
character(len=max_int_len) :: font_size_str !! font size dummy string
113-
character(len=max_int_len) :: axes_labelsize_str !! size of axis labels dummy string
114-
character(len=max_int_len) :: xtick_labelsize_str !! sise of x axis tick labels dummy string
115-
character(len=max_int_len) :: ytick_labelsize_str !! sise of x axis tick labels dummy string
116-
character(len=max_int_len) :: legend_fontsize_str !! sise of legend font dummy string
117-
character(len=*), parameter :: default_font_size_str = '10' !! the default font size for plots
110+
111+
character(len=max_int_len) :: width_str !! figure width dummy string
112+
character(len=max_int_len) :: height_str !! figure height dummy string
113+
character(len=max_int_len) :: font_size_str !! font size dummy string
114+
character(len=max_int_len) :: axes_labelsize_str !! size of axis labels dummy string
115+
character(len=max_int_len) :: xtick_labelsize_str !! sise of x axis tick labels dummy string
116+
character(len=max_int_len) :: ytick_labelsize_str !! sise of x axis tick labels dummy string
117+
character(len=max_int_len) :: legend_fontsize_str !! sise of legend font dummy string
118+
character(len=*), parameter :: default_font_size_str = '10' !! the default font size for plots
118119

119120
call me%destroy()
120121

@@ -191,12 +192,13 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
191192
character(len=*), intent (in) :: linestyle !! style of the plot line
192193
integer, intent (in), optional :: markersize !! size of the plot markers
193194
integer, intent (in), optional :: linewidth !! width of the plot line
194-
character(len=:), allocatable :: xstr !! x values strinfied
195-
character(len=:), allocatable :: ystr !! y values strinfied
196-
character(len=max_int_len) :: imark !! actual markers size
197-
character(len=max_int_len) :: iline !! actual line width
198-
character(len=*), parameter :: xname = 'x' !! x variable name for script
199-
character(len=*), parameter :: yname = 'y' !! y variable name for script
195+
196+
character(len=:), allocatable :: xstr !! x values strinfied
197+
character(len=:), allocatable :: ystr !! y values strinfied
198+
character(len=max_int_len) :: imark !! actual markers size
199+
character(len=max_int_len) :: iline !! actual line width
200+
character(len=*), parameter :: xname = 'x' !! x variable name for script
201+
character(len=*), parameter :: yname = 'y' !! y variable name for script
200202

201203
if (allocated(me%str)) then
202204

@@ -244,15 +246,16 @@ subroutine add_bar(me, left, height, label, width, bottom, color)
244246
real(wp), dimension(:), intent(in), optional :: width !! width values
245247
real(wp), dimension(:), intent(in), optional :: bottom !! bottom values
246248
character(len=*), intent(in), optional :: color !! plot color
247-
character(len=:), allocatable :: xstr !! x axis values stringified
248-
character(len=:), allocatable :: ystr !! y axis values stringified
249-
character(len=:), allocatable :: wstr !! width values stringified
250-
character(len=:), allocatable :: bstr !! bottom values stringified
251-
character(len=:), allocatable :: plt_str !! plot string
252-
character(len=*), parameter :: xname = 'x' !! x axis name
253-
character(len=*), parameter :: yname = 'y' !! y axis name
254-
character(len=*), parameter :: wname = 'w' !! width name
255-
character(len=*), parameter :: bname = 'b' !! bottom name
249+
250+
character(len=:), allocatable :: xstr !! x axis values stringified
251+
character(len=:), allocatable :: ystr !! y axis values stringified
252+
character(len=:), allocatable :: wstr !! width values stringified
253+
character(len=:), allocatable :: bstr !! bottom values stringified
254+
character(len=:), allocatable :: plt_str !! plot string
255+
character(len=*), parameter :: xname = 'x' !! x axis name
256+
character(len=*), parameter :: yname = 'y' !! y axis name
257+
character(len=*), parameter :: wname = 'w' !! width name
258+
character(len=*), parameter :: bname = 'b' !! bottom name
256259

257260
if (allocated(me%str)) then
258261

@@ -341,9 +344,10 @@ subroutine vec_to_string(v, str, use_numpy)
341344
real(wp), dimension(:), intent(in) :: v !! real values
342345
character(len=:), allocatable, intent(out) :: str !! real values stringified
343346
logical, intent(in) :: use_numpy !! activate numpy python module usage
344-
integer :: i !! counter
345-
integer :: istat !! IO status
346-
character(len=max_real_len) :: tmp !! dummy string
347+
348+
integer :: i !! counter
349+
integer :: istat !! IO status
350+
character(len=max_real_len) :: tmp !! dummy string
347351

348352
str = '['
349353
do i=1, size(v)
@@ -362,37 +366,54 @@ end subroutine vec_to_string
362366

363367
!*****************************************************************************************
364368
!> author: Jacob Williams
369+
! date: 8/16/2015
365370
!
366371
! Write the buffer to a file, and then execute it with Python.
372+
!
373+
! If user specifies a Python file name, then the file is kept, otherwise
374+
! a temporary filename is used, and the file is deleted after it is used.
367375

368376
subroutine execute(me, pyfile)
369377

370378
class(pyplot), intent(inout) :: me !! pytplot handler
371379
character(len=*), intent(in), optional :: pyfile !! name of the python script to generate
372-
integer :: istat !! IO status
373-
integer :: iunit !! IO unit
374-
character(len=:), allocatable :: file !! file name
380+
381+
integer :: istat !! IO status
382+
integer :: iunit !! IO unit
383+
character(len=:), allocatable :: file !! file name
384+
logical :: scratch !! if a scratch file is to be used
375385

376386
if (allocated(me%str)) then
377-
378-
!file name for python script:
379-
if (present(pyfile)) then
380-
file = trim(pyfile) !use the user-specified name
381-
else
387+
388+
scratch = (.not. present(pyfile))
389+
390+
!file name to use:
391+
if (scratch) then
382392
file = trim(tmp_file) !use the default
393+
else
394+
file = trim(pyfile) !use the user-specified name
383395
end if
384-
385-
!generate the file:
396+
397+
!open the file:
386398
open(newunit=iunit, file=file, status='REPLACE', iostat=istat)
387-
if (istat/=0) error stop 'Error creating python script.'
399+
if (istat/=0) error stop 'Error opening file.'
400+
401+
!write to the file:
388402
write(iunit, '(A)') me%str
389-
close(iunit, iostat=istat)
390403

391404
!run the file using python:
392405
call execute_command_line(python_exe//' '//file)
393-
406+
407+
!close the file:
408+
if (scratch) then
409+
close(iunit, status='DELETE', iostat=istat)
410+
else
411+
close(iunit, iostat=istat)
412+
end if
413+
if (istat/=0) error stop 'Error closing file.'
414+
394415
!cleanup:
395-
deallocate(file)
416+
if (allocated(file)) deallocate(file)
396417

397418
end if
398419

src/test.f90

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1+
!*****************************************************************************************
12
!> author: Jacob Williams
23
! date: 4/14/2015
34
! license: BSD
45
!
5-
!# DESCRIPTION
66
! Unit test for [[pyplot_module]].
7-
program test
8-
9-
use, intrinsic :: iso_fortran_env, only : wp => real64
10-
use pyplot_module, only : pyplot
11-
12-
implicit none
13-
14-
real(wp), dimension(100) :: x !! x values
15-
real(wp), dimension(100) :: sx !! sin(x) values
16-
real(wp), dimension(100) :: cx !! cos(x) values
17-
real(wp), dimension(100) :: tx !! sin(x)*cos(x) values
18-
type(pyplot) :: plt !! pytplot handler
19-
integer :: i !! counter
20-
21-
!generate some data:
22-
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
23-
sx = sin(x)
24-
cx = cos(x)
25-
tx = sx * cx
26-
27-
!2d line plot:
28-
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
29-
title='plot test',legend=.true.)
30-
call plt%add_plot(x,sx,label='$\sin (x)$',linestyle='b-o',markersize=5,linewidth=2)
31-
call plt%add_plot(x,cx,label='$\cos (x)$',linestyle='r-o',markersize=5,linewidth=2)
32-
call plt%add_plot(x,tx,label='$\sin (x) \cos (x)$',linestyle='g-o',markersize=2,linewidth=1)
33-
call plt%savefig('plottest.png', pyfile='plottest.py')
34-
35-
!bar chart:
36-
tx = 0.1_wp !for bar width
37-
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
38-
title='bar test',legend=.true.,figsize=[20,5],&
39-
font_size = 20,&
40-
axes_labelsize = 20,&
41-
xtick_labelsize = 20,&
42-
ytick_labelsize = 20,&
43-
legend_fontsize = 20 )
44-
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',color='r')
45-
call plt%savefig('bartest.png', pyfile='bartest.py')
46-
47-
end program test
7+
8+
program test
9+
10+
use, intrinsic :: iso_fortran_env, only : wp => real64
11+
use pyplot_module, only : pyplot
12+
13+
implicit none
14+
15+
real(wp), dimension(100) :: x !! x values
16+
real(wp), dimension(100) :: sx !! sin(x) values
17+
real(wp), dimension(100) :: cx !! cos(x) values
18+
real(wp), dimension(100) :: tx !! sin(x)*cos(x) values
19+
type(pyplot) :: plt !! pytplot handler
20+
integer :: i !! counter
21+
22+
!generate some data:
23+
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
24+
sx = sin(x)
25+
cx = cos(x)
26+
tx = sx * cx
27+
28+
!2d line plot:
29+
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
30+
title='plot test',legend=.true.)
31+
call plt%add_plot(x,sx,label='$\sin (x)$',linestyle='b-o',markersize=5,linewidth=2)
32+
call plt%add_plot(x,cx,label='$\cos (x)$',linestyle='r-o',markersize=5,linewidth=2)
33+
call plt%add_plot(x,tx,label='$\sin (x) \cos (x)$',linestyle='g-o',markersize=2,linewidth=1)
34+
call plt%savefig('plottest.png', pyfile='plottest.py')
35+
36+
!bar chart:
37+
tx = 0.1_wp !for bar width
38+
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
39+
title='bar test',legend=.true.,figsize=[20,5],&
40+
font_size = 20,&
41+
axes_labelsize = 20,&
42+
xtick_labelsize = 20,&
43+
ytick_labelsize = 20,&
44+
legend_fontsize = 20 )
45+
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',color='r')
46+
call plt%savefig('bartest.png', pyfile='bartest.py')
47+
48+
end program test
49+
!*****************************************************************************************

0 commit comments

Comments
 (0)