Skip to content

Commit 780f6f2

Browse files
committed
added additional options for sphere. added unit test.
1 parent befcd24 commit 780f6f2

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

src/pyplot_module.f90

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -557,27 +557,51 @@ end subroutine add_3d_plot
557557
!
558558
!@note Must initialize the class with `mplot3d=.true.` and `use_numpy=.true.`.
559559

560-
subroutine add_sphere(me, r, xc, yc, zc, istat)
560+
subroutine add_sphere(me, r, xc, yc, zc, n_facets, linewidth, antialiased, color, istat)
561561

562562
implicit none
563563

564-
class(pyplot), intent (inout) :: me !! pyplot handler
565-
real(wp), intent (in) :: r !! radius of the sphere
566-
real(wp), intent (in) :: xc !! x value of sphere center
567-
real(wp), intent (in) :: yc !! y value of sphere center
568-
real(wp), intent (in) :: zc !! z value of sphere center
569-
integer, intent (out) :: istat !! status output (0 means no problems)
570-
571-
character(len=:), allocatable :: rstr !! r value stringified
572-
character(len=:), allocatable :: xcstr !! xc value stringified
573-
character(len=:), allocatable :: ycstr !! yc value stringified
574-
character(len=:), allocatable :: zcstr !! zc value stringified
575-
character(len=*), parameter :: xname = 'x' !! x variable name for script
576-
character(len=*), parameter :: yname = 'y' !! y variable name for script
577-
character(len=*), parameter :: zname = 'z' !! z variable name for script
564+
class(pyplot), intent (inout) :: me !! pyplot handler
565+
real(wp), intent (in) :: r !! radius of the sphere
566+
real(wp), intent (in) :: xc !! x value of sphere center
567+
real(wp), intent (in) :: yc !! y value of sphere center
568+
real(wp), intent (in) :: zc !! z value of sphere center
569+
integer, intent (in), optional :: n_facets !! [default is 100]
570+
integer, intent (in), optional :: linewidth !! line width
571+
logical, intent (in), optional :: antialiased !! enabled anti-aliasing
572+
character(len=*), intent (in), optional :: color !! color of the contour line
573+
integer, intent (out) :: istat !! status output (0 means no problems)
574+
575+
character(len=:), allocatable :: rstr !! `r` value stringified
576+
character(len=:), allocatable :: xcstr !! `xc` value stringified
577+
character(len=:), allocatable :: ycstr !! `yc` value stringified
578+
character(len=:), allocatable :: zcstr !! `zc` value stringified
579+
character(len=*), parameter :: xname = 'x' !! `x` variable name for script
580+
character(len=*), parameter :: yname = 'y' !! `y` variable name for script
581+
character(len=*), parameter :: zname = 'z' !! `z` variable name for script
582+
583+
character(len=max_int_len) :: linewidth_str !! `linewidth` input stringified
584+
character(len=:), allocatable :: antialiased_str !! `antialised` input stringified
585+
character(len=max_int_len) :: n_facets_str !! `n_facets` input stringified
586+
character(len=:), allocatable :: extras !! optional stuff string
578587

579588
if (allocated(me%str)) then
580589

590+
!get optional inputs (if not present, set default value):
591+
call optional_int_to_string(n_facets, n_facets_str, '100')
592+
extras = ''
593+
if (present(linewidth)) then
594+
call optional_int_to_string(linewidth, linewidth_str, '1')
595+
extras = extras//','//'linewidth='//linewidth_str
596+
end if
597+
if (present(antialiased)) then
598+
call optional_logical_to_string(antialiased, antialiased_str, 'False')
599+
extras = extras//','//'antialiased='//antialiased_str
600+
end if
601+
if (present(color)) then
602+
extras = extras//','//'color="'//trim(color)//'"'
603+
end if
604+
581605
istat = 0
582606

583607
!convert the arrays to strings:
@@ -586,12 +610,13 @@ subroutine add_sphere(me, r, xc, yc, zc, istat)
586610
call real_to_string(yc, me%real_fmt, ycstr)
587611
call real_to_string(zc, me%real_fmt, zcstr)
588612

589-
call me%add_str('u = np.linspace(0, 2 * np.pi, 100)')
590-
call me%add_str('v = np.linspace(0, np.pi, 100)')
613+
! sphere code:
614+
call me%add_str('u = np.linspace(0, 2 * np.pi, '//n_facets_str//')')
615+
call me%add_str('v = np.linspace(0, np.pi, '//n_facets_str//')')
591616
call me%add_str(xname//' = '//xcstr//' + '//rstr//' * np.outer(np.cos(u), np.sin(v))')
592617
call me%add_str(yname//' = '//ycstr//' + '//rstr//' * np.outer(np.sin(u), np.sin(v))')
593618
call me%add_str(zname//' = '//zcstr//' + '//rstr//' * np.outer(np.ones(np.size(u)), np.cos(v))')
594-
call me%add_str('ax.plot_surface('//xname//', '//yname//', '//zname//', color="Grey")')
619+
call me%add_str('ax.plot_surface('//xname//', '//yname//', '//zname//extras//')')
595620
call me%add_str('')
596621

597622
else

src/tests/test.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ program test
1919
real(wp), dimension(n) :: yerr !! error values for bar chart
2020
real(wp), dimension(n) :: sx !! sin(x) values
2121
real(wp), dimension(n) :: cx !! cos(x) values
22+
real(wp), dimension(n) :: zx !!
2223
real(wp), dimension(n) :: tx !! sin(x)*cos(x) values
2324
real(wp), dimension(n,n) :: z !! z matrix for contour plot
2425
type(pyplot) :: plt !! pytplot handler
@@ -28,11 +29,15 @@ program test
2829
real(wp), dimension(n,n) :: mat !! image values
2930
integer :: istat !! status code
3031

32+
real(wp),parameter :: pi = acos(-1.0_wp)
33+
real(wp),parameter :: deg2rad = pi/180.0_wp
34+
3135
!generate some data:
3236
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
3337
sx = sin(x)
3438
cx = cos(x)
3539
tx = sx * cx
40+
zx = 0.0_wp
3641
yerr = abs(sx*.25_wp)
3742

3843
do i=1,n
@@ -124,5 +129,28 @@ program test
124129
dpi='200', &
125130
transparent=.true.,istat=istat)
126131

132+
!3D plot:
133+
call plt%initialize(grid=.true.,&
134+
xlabel='x (km)',ylabel='y (km)',zlabel='z (km)',&
135+
title='Orbit',&
136+
axis_equal=.true.,&
137+
mplot3d=.true.,&
138+
figsize=[20,10])
139+
140+
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
141+
sx = 7000.0_wp * cos(x * deg2rad)
142+
cx = 7000.0_wp * sin(x * deg2rad)
143+
zx = 0.0_wp
144+
call plt%add_3d_plot(sx,cx,zx,&
145+
label='orbit',linestyle='r-',&
146+
linewidth=2,istat=istat)
147+
call plt%add_sphere(6378.0_wp,0.0_wp,0.0_wp,0.0_wp,&
148+
color='Blue',n_facets=500,&
149+
antialiased=.true.,istat=istat)
150+
call plt%savefig('orbit.png', &
151+
pyfile='orbit.py', &
152+
dpi='200', &
153+
transparent=.true.,istat=istat)
154+
127155
end program test
128156
!*****************************************************************************************

0 commit comments

Comments
 (0)