-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stdlib_io] add disp
(display variable values formatted).
#520
base: master
Are you sure you want to change the base?
Changes from all commits
f39b481
38edc33
4bae170
761dacf
575c944
fd00762
4299e5a
710e115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -131,3 +131,126 @@ program demo_savetxt | |||||
call savetxt('example.dat', x) | ||||||
end program demo_savetxt | ||||||
``` | ||||||
|
||||||
## `disp` - display the value of the variable | ||||||
|
||||||
### Status | ||||||
|
||||||
Experimental | ||||||
|
||||||
### Class | ||||||
|
||||||
Impure subroutine. | ||||||
|
||||||
### Description | ||||||
|
||||||
Outputs a `logical/integer/real/complex/character/string_type` scalar, | ||||||
or `logical/integer/real/complex/string_type` and rank-1/rank-2 array to the screen or a file `unit`. | ||||||
|
||||||
### Syntax | ||||||
|
||||||
`call [[stdlib_io(module):disp(interface)]]( [x, header, unit, brief, format, width, sep] )` | ||||||
|
||||||
### Arguments | ||||||
|
||||||
- `x`: Shall be a `logical/integer/real/complex/character(len=*)/string_type` scalar or `logical/integer/real/complex/string_type` and rank-1/rank-2 array. | ||||||
This argument is `intent(in)` and `optional`. | ||||||
|
||||||
- `header`: Shall be a `character(len=*)` scalar. | ||||||
This argument is `intent(in)` and `optional`. | ||||||
|
||||||
- `unit`: Shall be an `integer` scalar, linked to an IO stream. | ||||||
This argument is `intent(in)` and `optional`.<br> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
The default value is `output_unit` from `iso_fortran_env` module. | ||||||
|
||||||
- `brief`: Shall be a `logical` scalar, controls an abridged version of the `x` array to be outputted. | ||||||
This argument is `intent(in)` and `optional`.<br> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
The default value is `.false.` | ||||||
|
||||||
- `format`: Shall be a `character(len=*)` scalar. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
This argument is `intent(in)` and `optional`.<br> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
The default value is `g0.4`. | ||||||
|
||||||
- `width`: Shall be an `integer` scalar, controls the outputted maximum width (`>=80`). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence is unclear |
||||||
This argument is `intent(in)` and `optional`.<br> | ||||||
The default value is `80`. | ||||||
|
||||||
- `sep`: Shall be a `character(len=*)` scalar, separator. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is this the aim of |
||||||
This argument is `intent(in)` and `optional`.<br> | ||||||
The default value is "  ", two spaces. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by |
||||||
|
||||||
### Output | ||||||
|
||||||
The result is to print `header` and `x` on the screen (or another output `unit/file`) in this order.<br> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
If `disp` is not passed any arguments, a blank line will be printed. | ||||||
|
||||||
### Example | ||||||
|
||||||
```fortran | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to reduce the example. One or two |
||||||
program test_io_disp | ||||||
|
||||||
use stdlib_io, only: disp | ||||||
|
||||||
real :: r(2, 3) | ||||||
complex :: c(2, 3), c_3d(2, 100, 20) | ||||||
integer :: i(2, 3) | ||||||
logical :: l(10, 10) | ||||||
|
||||||
r = 1.; c = 1.; c_3d = 2.; i = 1; l = .true. | ||||||
c_3d(1,3,1) = (1000, 0.001) | ||||||
|
||||||
call disp('string', header='disp(string):') | ||||||
call disp('It is a note.') | ||||||
call disp() | ||||||
call disp(r, header='disp(r):') | ||||||
call disp(r(1,:), header='disp(r(1,:))', format="f6.2") | ||||||
call disp(c, header='disp(c):') | ||||||
call disp(i, header='disp(i):', sep=",") | ||||||
call disp(l, header='disp(l):', brief=.true.) | ||||||
call disp(c_3d(:,3,1:10), header='disp(c_3d(:,3,1:10)):', width=100) | ||||||
call disp(c_3d(2,:,:), header='disp(c_3d(2,:,:)):', brief=.true.) | ||||||
|
||||||
end program test_io_disp | ||||||
``` | ||||||
**Results:** | ||||||
```fortran | ||||||
disp(string): | ||||||
string | ||||||
It is a note. | ||||||
|
||||||
disp(r): | ||||||
[matrix size: 2×3] | ||||||
1.000 1.000 1.000 | ||||||
1.000 1.000 1.000 | ||||||
disp(r(1,:)) | ||||||
[vector size: 3] | ||||||
1.00 1.00 1.00 | ||||||
disp(c): | ||||||
[matrix size: 2×3] | ||||||
(1.000,0.000) (1.000,0.000) (1.000,0.000) | ||||||
(1.000,0.000) (1.000,0.000) (1.000,0.000) | ||||||
disp(i): | ||||||
[matrix size: 2×3] | ||||||
1, 1, 1, | ||||||
1, 1, 1, | ||||||
disp(l): | ||||||
[matrix size: 10×10] | ||||||
T T T .. T | ||||||
T T T .. T | ||||||
T T T .. T | ||||||
: : : : : | ||||||
T T T .. T | ||||||
disp(c_3d(:,3,1:10)): | ||||||
[matrix size: 2×10] | ||||||
(1000.,0.1000E-2) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) & | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) & | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) | ||||||
disp(c_3d(2,:,:)): | ||||||
[matrix size: 100×20] | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000) | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000) | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000) | ||||||
: : : : : | ||||||
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000) | ||||||
``` |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,14 +7,15 @@ module stdlib_io | |||||
!! ([Specification](../page/specs/stdlib_io.html)) | ||||||
|
||||||
use stdlib_kinds, only: sp, dp, qp, & | ||||||
int8, int16, int32, int64 | ||||||
int8, int16, int32, int64, lk, c_bool | ||||||
use stdlib_error, only: error_stop | ||||||
use stdlib_optval, only: optval | ||||||
use stdlib_ascii, only: is_blank | ||||||
use stdlib_string_type, only: string_type | ||||||
implicit none | ||||||
private | ||||||
! Public API | ||||||
public :: loadtxt, savetxt, open | ||||||
public :: loadtxt, savetxt, open, disp | ||||||
|
||||||
! Private API that is exposed so that we can test it in tests | ||||||
public :: parse_mode | ||||||
|
@@ -28,6 +29,36 @@ module stdlib_io | |||||
FMT_COMPLEX_SP = '(*(es15.8e2,1x,es15.8e2))', & | ||||||
FMT_COMPLEX_DP = '(*(es24.16e3,1x,es24.16e3))', & | ||||||
FMT_COMPLEX_QP = '(*(es44.35e4,1x,es44.35e4))' | ||||||
|
||||||
!> version: experimental | ||||||
!> | ||||||
!> Display a scalar, vector or matrix formatted. | ||||||
!> ([Specification](../page/specs/stdlib_io.html#display-the-value-of-the-variable)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
interface disp | ||||||
#:set ALL_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES + INT_KINDS_TYPES + LOG_KINDS_TYPES + STRING_KINDS_TYPES | ||||||
module subroutine disp_char(x, header, unit, brief, format, width, sep) | ||||||
character(*), intent(in), optional :: x | ||||||
character(len=*), intent(in), optional :: header | ||||||
integer, intent(in), optional :: unit | ||||||
logical, intent(in), optional :: brief | ||||||
character(len=*), intent(in), optional :: format | ||||||
integer, intent(in), optional :: width | ||||||
character(len=*), intent(in), optional :: sep | ||||||
end subroutine disp_char | ||||||
#:for r1 in range(0, 3) | ||||||
#:for k1, t1 in ALL_KINDS_TYPES | ||||||
module subroutine disp_${r1}$_${t1[0]}$${k1}$(x, header, unit, brief, format, width, sep) | ||||||
${t1}$, intent(in) :: x${ranksuffix(r1)}$ | ||||||
character(len=*), intent(in), optional :: header | ||||||
integer, intent(in), optional :: unit | ||||||
logical, intent(in), optional :: brief | ||||||
character(len=*), intent(in), optional :: format | ||||||
integer, intent(in), optional :: width | ||||||
character(len=*), intent(in), optional :: sep | ||||||
end subroutine disp_${r1}$_${t1[0]}$${k1}$ | ||||||
#:endfor | ||||||
#:endfor | ||||||
end interface disp | ||||||
|
||||||
interface loadtxt | ||||||
!! version: experimental | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.