Skip to content

Commit a801963

Browse files
committed
Implement save_npz
1 parent fe8e786 commit a801963

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

src/stdlib_io_np.fypp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module stdlib_io_np
137137
interface save_npz
138138
module subroutine save_npz_from_arrays(filename, arrays, compressed, iostat, iomsg)
139139
character(len=*), intent(in) :: filename
140-
type(t_array_wrapper), intent(in) :: arrays(*)
140+
type(t_array_wrapper), intent(in) :: arrays(:)
141141
!> If true, the file is saved in compressed format. The default is false.
142142
logical, intent(in), optional :: compressed
143143
integer, intent(out), optional :: iostat

src/stdlib_io_np_load.fypp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ submodule(stdlib_io_np) stdlib_io_np_load
99
use stdlib_array
1010
use stdlib_error, only: error_stop
1111
use stdlib_filesystem, only: exists, list_dir, temp_dir
12-
use stdlib_io_zip, only: unzip, default_unzip_dir, zip_contents
12+
use stdlib_io_zip, only: unzip, default_unzip_dir, zip_contents, zip
1313
use stdlib_strings, only: to_string, starts_with
1414
use stdlib_string_type, only: string_type, as_string => char
1515
implicit none

src/stdlib_io_np_save.fypp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
!> Implementation of saving multidimensional arrays to npy files
88
submodule(stdlib_io_np) stdlib_io_np_save
9+
use stdlib_array
910
use stdlib_error, only: error_stop
11+
use stdlib_filesystem, only: run
1012
use stdlib_strings, only: to_string
13+
use stdlib_string_type, only: string_type, char
14+
use stdlib_io_zip, only: zip
1115
implicit none
1216

1317
contains
@@ -137,18 +141,79 @@ contains
137141
!> ([Specification](../page/specs/stdlib_io.html#save_npz))
138142
module subroutine save_npz_from_arrays(filename, arrays, compressed, iostat, iomsg)
139143
character(len=*), intent(in) :: filename
140-
type(t_array_wrapper), intent(in) :: arrays(*)
144+
type(t_array_wrapper), intent(in) :: arrays(:)
141145
!> If true, the file is saved in compressed format. The default is false.
142146
logical, intent(in), optional :: compressed
143147
integer, intent(out), optional :: iostat
144148
character(len=:), allocatable, intent(out), optional :: iomsg
145149

150+
integer :: i, j, stat
146151
logical :: is_compressed
152+
character(len=:), allocatable :: msg
153+
type(string_type), allocatable :: files(:)
154+
155+
if (present(iostat)) iostat = 0
147156

148157
if (present(compressed)) then
149158
is_compressed = compressed
150159
else
151160
is_compressed = .false.
152161
end if
162+
163+
allocate(files(size(arrays)))
164+
do i = 1, size(arrays)
165+
select type (typed_array => arrays(i)%array)
166+
#:for k1, t1 in KINDS_TYPES
167+
#:for rank in RANKS
168+
class is (t_array_${t1[0]}$${k1}$_${rank}$)
169+
do j = 1, size(files)
170+
if (char(files(j)) == typed_array%name) then
171+
if (present(iostat)) iostat = 1
172+
if (present(iomsg)) iomsg = "Error saving array to file '"//filename// &
173+
"': Array with the same name '"//typed_array%name//"' already exists."
174+
call delete_files(files)
175+
return
176+
end if
177+
end do
178+
179+
call save_npy(typed_array%name, typed_array%values, stat, msg)
180+
if (stat /= 0) then
181+
if (present(iostat)) iostat = stat
182+
if (present(iomsg)) iomsg = msg
183+
call delete_files(files)
184+
return
185+
end if
186+
187+
files = [files, string_type(typed_array%name)]
188+
#:endfor
189+
#:endfor
190+
class default
191+
if (present(iostat)) iostat = 1
192+
if (present(iomsg)) iomsg = "Error saving array to file '"//filename//"': Unsupported array type."
193+
call delete_files(files)
194+
return
195+
end select
196+
end do
197+
198+
call zip(filename, files, stat, msg)
199+
if (stat /= 0) then
200+
if (present(iostat)) iostat = stat
201+
if (present(iomsg)) iomsg = msg
202+
call delete_files(files)
203+
return
204+
end if
205+
206+
call delete_files(files)
207+
end
208+
209+
subroutine delete_files(files)
210+
type(string_type), allocatable, intent(in) :: files(:)
211+
212+
integer :: i, unit
213+
214+
do i = 1, size(files)
215+
open(newunit=unit, file=char(files(i)))
216+
close(unit, status="delete")
217+
end do
153218
end
154219
end

0 commit comments

Comments
 (0)