-
Notifications
You must be signed in to change notification settings - Fork 191
Feature: loadtxt skiprows and max_rows #652
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
Changes from 7 commits
d980c64
de74cd8
ea80574
bcc3f36
3c66fcc
938ba8c
5af87c6
ad2faa9
6c01940
93689c9
ec049ea
c046e7e
764322a
0f356db
b428213
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 |
---|---|---|
|
@@ -81,7 +81,7 @@ module stdlib_io | |
contains | ||
|
||
#:for k1, t1 in KINDS_TYPES | ||
subroutine loadtxt_${t1[0]}$${k1}$(filename, d) | ||
subroutine loadtxt_${t1[0]}$${k1}$(filename, d, skiprows, max_rows) | ||
!! version: experimental | ||
!! | ||
!! Loads a 2D array from a text file. | ||
|
@@ -93,6 +93,10 @@ contains | |
character(len=*), intent(in) :: filename | ||
!! The array 'd' will be automatically allocated with the correct dimensions | ||
${t1}$, allocatable, intent(out) :: d(:,:) | ||
!! Skip the first `skiprows` lines, default: 0. | ||
integer, intent(in), optional :: skiprows | ||
!! Read max_rows lines of content after skiprows lines. The default is -1, to read all the lines. | ||
MuellerSeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
integer, intent(in), optional :: max_rows | ||
!! | ||
!! Example | ||
!! ------- | ||
|
@@ -111,7 +115,10 @@ contains | |
!! ... | ||
!! | ||
integer :: s | ||
integer :: nrow, ncol, i | ||
integer :: nrow, ncol, i, skiprows_, max_rows_ | ||
|
||
skiprows_ = max(optval(skiprows, 0), 0) | ||
max_rows_ = optval(max_rows, -1) | ||
MuellerSeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
s = open(filename) | ||
|
||
|
@@ -122,10 +129,17 @@ contains | |
#:endif | ||
|
||
! determine number or rows | ||
nrow = number_of_rows(s) | ||
nrow = number_of_rows(s) - skiprows_ | ||
if ( nrow < 0 ) call error_stop("loadtxt: skipping more rows than present.") | ||
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. Should this really be a fatal error? 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. We could also cut off 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. But numpy also throws an error for skiprows > nrow. 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. gfortran also crashes when trying to read after EOF. So having a meaningful error message is preferable IMO. 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. Why not using 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. Unfortunately, I am not familiar with do i = 1, skiprows_
read(s, *)
end do |
||
if ( max_rows_ < 0 .or. max_rows_ > nrow) max_rows_ = nrow | ||
MuellerSeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
allocate(d(max_rows_, ncol)) | ||
|
||
do i = 1, skiprows_ | ||
read(s, *) | ||
end do | ||
MuellerSeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
allocate(d(nrow, ncol)) | ||
do i = 1, nrow | ||
do i = 1, max_rows_ | ||
#:if 'real' in t1 | ||
read(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") d(i, :) | ||
#:elif 'complex' in t1 | ||
|
Uh oh!
There was an error while loading. Please reload this page.