-
Notifications
You must be signed in to change notification settings - Fork 217
Savetxt unit #1085
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
base: master
Are you sure you want to change the base?
Savetxt unit #1085
Changes from all commits
d6cc685
e2d1c09
94c36cc
af2137b
4db2dd0
5c8397c
eb4acc6
dc2c9f8
e6a02f7
d390d79
9cd8e51
b75e631
036ce25
ab357fa
e6af81a
04061b8
fbe6d0a
3f6aff3
d491c8f
390857d
4e574eb
d470409
6c43047
a01ffe7
e3c13f3
86fa060
8403d30
241e146
45ee7c2
672ad8a
2b16ff3
5ecd021
20c1a3f
aacc748
8b9189b
ecacf54
8d02086
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 |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| program example_savetxt | ||
| use stdlib_io, only: savetxt | ||
| use, intrinsic :: iso_fortran_env, only: output_unit | ||
| implicit none | ||
| real :: x(3, 2) = 1 | ||
| call savetxt('example.dat', x) | ||
| call savetxt('example.csv', x, delimiter=',') | ||
| call savetxt('example1.dat', x, header='x (x-units) y (y-units)') | ||
| call savetxt('example2.dat', x, header='x (x-units) y (y-units)', comments='!#', footer='This is all data') | ||
| call savetxt('example3.dat', x, fmt='g0.7') | ||
| call savetxt(output_unit, x, header='x (x-units) y (y-units)') | ||
| end program example_savetxt |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,7 +13,7 @@ module stdlib_io | |||||||
| FMT_COMPLEX_SP, FMT_COMPLEX_DP, FMT_COMPLEX_XDP, FMT_COMPLEX_QP | ||||||||
| use stdlib_error, only: error_stop, state_type, STDLIB_IO_ERROR | ||||||||
| use stdlib_optval, only: optval | ||||||||
| use stdlib_ascii, only: is_blank | ||||||||
| use stdlib_ascii, only: is_blank, whitespace, CR,LF,VT,FF | ||||||||
| use stdlib_string_type, only : string_type, assignment(=), move | ||||||||
| implicit none | ||||||||
| private | ||||||||
|
|
@@ -47,6 +47,8 @@ module stdlib_io | |||||||
|
|
||||||||
| !> Default delimiter for loadtxt, savetxt and number_of_columns | ||||||||
| character(len=1), parameter :: delimiter_default = " " | ||||||||
| character(len=1), parameter :: comment_default = "#" | ||||||||
| character(len=1), parameter :: nl = new_line('a') | ||||||||
|
|
||||||||
| public :: FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_REAL_XDP, FMT_REAL_QP | ||||||||
| public :: FMT_COMPLEX_SP, FMT_COMPLEX_DP, FMT_COMPLEX_XDP, FMT_COMPLEX_QP | ||||||||
|
|
@@ -76,8 +78,10 @@ module stdlib_io | |||||||
| !! | ||||||||
| !! Saves a 2D array into a text file | ||||||||
| !! ([Specification](../page/specs/stdlib_io.html#description_2)) | ||||||||
| #:for a1 in ['f', 'u'] | ||||||||
|
Member
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
|
||||||||
| #:for k1, t1 in KINDS_TYPES | ||||||||
| module procedure savetxt_${t1[0]}$${k1}$ | ||||||||
| module procedure savetxt_${t1[0]}$${k1}$${a1}$ | ||||||||
|
Member
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
Contributor
Author
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 can use arg1 but I was thinking on only using the first letter to identify the name of the routine, as in the definition (
Member
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.
|
||||||||
| #:endfor | ||||||||
| #:endfor | ||||||||
| end interface | ||||||||
|
|
||||||||
|
|
@@ -230,19 +234,27 @@ contains | |||||||
| end subroutine loadtxt_${t1[0]}$${k1}$ | ||||||||
| #:endfor | ||||||||
|
|
||||||||
|
|
||||||||
| #:for arg1 in ['filename', 'unit'] | ||||||||
| #:for k1, t1 in KINDS_TYPES | ||||||||
| subroutine savetxt_${t1[0]}$${k1}$(filename, d, delimiter) | ||||||||
| subroutine savetxt_${t1[0]}$${k1}$${arg1[0]}$ (${arg1}$, d, delimiter, fmt, header, footer, comments) | ||||||||
| !! version: experimental | ||||||||
| !! | ||||||||
| !! Saves a 2D array into a text file. | ||||||||
| !! | ||||||||
| !! Arguments | ||||||||
| !! --------- | ||||||||
| !! | ||||||||
| #:if 'filename' in arg1 | ||||||||
| character(len=*), intent(in) :: filename ! File to save the array to | ||||||||
| #:elif 'unit' in arg1 | ||||||||
| integer, intent(in) :: unit | ||||||||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| #:endif | ||||||||
| ${t1}$, intent(in) :: d(:,:) ! The 2D array to save | ||||||||
| character(len=1), intent(in), optional :: delimiter ! Column delimiter. Default is a space. | ||||||||
| character(len=*), intent(in), optional :: delimiter ! Column delimiter. Default is a space ' '. | ||||||||
| character(len=*), intent(in), optional :: fmt !< Fortran format specifier. Defaults to the write format for the data type. | ||||||||
| character(len=*), intent(in), optional :: header !< If present, text to write before data. | ||||||||
| character(len=*), intent(in), optional :: footer !< If present, text to write after data. | ||||||||
| character(len=*), intent(in), optional :: comments !< Comment character. Default "#". | ||||||||
| !! | ||||||||
| !! Example | ||||||||
| !! ------- | ||||||||
|
|
@@ -253,42 +265,131 @@ contains | |||||||
| !!``` | ||||||||
| !! | ||||||||
| integer :: s, i, ios | ||||||||
|
||||||||
| integer :: s, i, ios | |
| integer :: i, ios |
Copilot
AI
Jan 29, 2026
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.
When the unit is not opened, the code writes an error message but does not stop execution with call error_stop(msg=trim(msgout)). This will cause the program to continue and potentially write to an unopened unit, leading to undefined behavior or runtime errors.
| write (msgout,'(a,i0,a)') 'savetxt error: unit ',unit,' not open' | |
| write (msgout,'(a,i0,a)') 'savetxt error: unit ',unit,' not open' | |
| call error_stop(msg=trim(msgout)) |
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.
@fiolj Is this a valid suggestion?
Uh oh!
There was an error while loading. Please reload this page.