Skip to content

Commit dc8e01f

Browse files
committed
Implement add_array
1 parent d2a5ad7 commit dc8e01f

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

src/stdlib_io_np.fypp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,22 @@ module stdlib_io_np
160160
#:endfor
161161
end interface
162162

163-
contains
164-
165-
subroutine add_array()
166-
end
163+
interface add_array
164+
#:for k1, t1 in KINDS_TYPES
165+
#:for rank in RANKS
166+
module subroutine add_array_${t1[0]}$${k1}$_${rank}$(arrays, array, name, stat, msg)
167+
!> Array of arrays to which the array is to be added.
168+
type(t_array_wrapper), allocatable, intent(inout) :: arrays(:)
169+
!> Array to be added.
170+
${t1}$, intent(in) :: array${ranksuffix(rank)}$
171+
!> Name of the array to be added.
172+
character(len=*), intent(in) :: name
173+
!> Status of addition.
174+
integer, intent(out), optional :: stat
175+
!> Error message.
176+
character(len=:), allocatable, intent(out), optional :: msg
177+
end
178+
#:endfor
179+
#:endfor
180+
end interface
167181
end

src/stdlib_io_np_save.fypp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,54 @@ contains
133133
end if
134134
end
135135
#:endfor
136+
#:endfor
137+
138+
#:for k1, t1 in KINDS_TYPES
139+
#:for rank in RANKS
140+
module subroutine add_array_${t1[0]}$${k1}$_${rank}$(arrays, array, name, stat, msg)
141+
!> Array of arrays to which the array is to be added.
142+
type(t_array_wrapper), allocatable, intent(inout) :: arrays(:)
143+
!> Array to be added.
144+
${t1}$, intent(in) :: array${ranksuffix(rank)}$
145+
!> Name of the array to be added.
146+
character(len=*), intent(in) :: name
147+
!> Status of addition.
148+
integer, intent(out), optional :: stat
149+
!> Error message.
150+
character(len=:), allocatable, intent(out), optional :: msg
151+
152+
integer :: i
153+
type(t_array_${t1[0]}$${k1}$_${rank}$) :: t_arr
154+
155+
if (present(stat)) stat = 0
156+
157+
if (trim(name) == '') then
158+
if (present(stat)) stat = 1
159+
if (present(msg)) msg = "Array name cannot be empty."
160+
return
161+
end if
162+
163+
t_arr%name = name
164+
t_arr%values = array
165+
166+
if (.not. allocated(arrays)) then
167+
allocate(arrays(1))
168+
allocate(arrays(1)%array, source=t_arr)
169+
return
170+
end if
171+
172+
do i = 1, size(arrays)
173+
if (arrays(i)%array%name == name) then
174+
if (present(stat)) stat = 1
175+
if (present(msg)) msg = "Array with the same name '"//name//"' already exists."
176+
return
177+
end if
178+
end do
179+
180+
allocate(arrays(size(arrays) + 1))
181+
arrays(size(arrays))%array = t_arr
182+
end
183+
#:endfor
136184
#:endfor
137185

138186
!> Version: experimental

test/io/test_np.f90

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module test_np
22
use stdlib_array
33
use stdlib_filesystem, only : temp_dir
44
use stdlib_kinds, only : int8, int16, int32, int64, sp, dp
5-
use stdlib_io_np, only : save_npy, load_npy, load_npz, save_npz
5+
use stdlib_io_np, only : save_npy, load_npy, load_npz, add_array, save_npz
66
use testdrive, only : new_unittest, unittest_type, error_type, check, test_failed
77
implicit none
88
private
@@ -953,11 +953,17 @@ subroutine npz_save_rdp_2(error)
953953
integer :: stat
954954
character(*), parameter :: filename = "npz_save_rdp_2.npz"
955955
character(*), parameter :: arr_name = "arr_0.npy"
956-
real(dp), allocatable :: input(:,:), output(:,:)
956+
real(dp), allocatable :: input_array(:,:), output(:,:)
957957

958-
allocate(input(10, 4))
959-
call random_number(input)
960-
! call add_array(arrays, input)
958+
allocate(input_array(10, 4))
959+
call random_number(input_array)
960+
call add_array(arrays, input_array, arr_name, stat)
961+
call check(error, stat, "Error adding an array to the list of arrays.")
962+
if (allocated(error)) return
963+
call check(error, size(arrays) == 1, "Array was not added to the list of arrays.")
964+
if (allocated(error)) return
965+
call check(error, arrays(1)%array%name == arr_name, "Wrong array name.")
966+
if (allocated(error)) return
961967

962968
! call save_npz(filename, arrays, stat)
963969
! call check(error, stat, "Writing of npz file failed")

0 commit comments

Comments
 (0)