33 implicit none
44
55 integer , parameter :: message_len = 128
6- contains
76
8- subroutine download_and_uncompress ()
9- character (len=* ), parameter :: download_mechanism = ' curl -LO '
10- character (len=* ), parameter :: base_url= ' https://github.com/modern-fortran/neural-fortran/files/8498876/'
11- character (len=* ), parameter :: download_filename = ' mnist.tar.gz'
12- character (len=* ), parameter :: download_command = download_mechanism // base_url // download_filename
13- character (len=* ), parameter :: uncompress_file= ' tar xvzf ' // download_filename
14- character (len= message_len) :: command_message
15- character (len= :), allocatable :: error_message
16- integer exit_status, command_status
17- exit_status= 0
18- call execute_command_line(command= download_command, &
19- wait= .true. , exitstat= exit_status, cmdstat= command_status, cmdmsg= command_message)
20- if (any ([exit_status, command_status]/= 0 )) then
21- error_message = ' command "' // download_command // ' " failed'
22- if (command_status/= 0 ) error_message = error_message // " with message " // trim (command_message)
23- error stop error_message
24- end if
25- call execute_command_line(command= uncompress_file , &
26- wait= .true. , exitstat= exit_status, cmdstat= command_status, cmdmsg= command_message)
27- if (any ([exit_status, command_status]/= 0 )) then
28- error_message = ' command "' // uncompress_file // ' " failed'
29- if (command_status/= 0 ) error_message = error_message // " with message " // trim (command_message)
30- error stop error_message
31- end if
32- end subroutine
7+ contains
338
349 module subroutine read_binary_file_1d (filename , dtype , nrec , array )
3510 character (len=* ), intent (in ) :: filename
3611 integer (ik), intent (in ) :: dtype, nrec
3712 real (rk), allocatable , intent (in out ) :: array(:)
3813 integer (ik) :: fileunit
39- character (len= message_len) io_message, command_message
40- integer io_status
41- io_status= 0
42- open (newunit= fileunit, file= filename, access= ' direct' ,&
43- action= ' read' , recl= dtype * nrec, status= ' old' , iostat= io_status)
44- if (io_status/= 0 ) then
45- call download_and_uncompress
46- open (newunit= fileunit, file= filename, access= ' direct' ,&
47- action= ' read' , recl= dtype * nrec, status= ' old' , iostat= io_status, iomsg= io_message)
48- if (io_status/= 0 ) error stop trim (io_message)
49- end if
14+ character (len= message_len) :: io_message
15+ integer :: io_status
16+ io_status = 0
17+ open (newunit= fileunit, file= filename, access= ' direct' , action= ' read' , &
18+ recl= dtype * nrec, status= ' old' , iostat= io_status, iomsg= io_message)
19+ if (io_status /= 0 ) error stop trim (io_message)
5020 allocate (array(nrec))
5121 read (fileunit, rec= 1 ) array
5222 close (fileunit)
@@ -57,19 +27,13 @@ module subroutine read_binary_file_2d(filename, dtype, dsize, nrec, array)
5727 integer (ik), intent (in ) :: dtype, dsize, nrec
5828 real (rk), allocatable , intent (in out ) :: array(:,:)
5929 integer (ik) :: fileunit, i
60- character (len= message_len) io_message, command_message
61- integer io_status
62- open (newunit= fileunit, file= filename, access= ' direct' ,&
63- action= ' read' , recl= dtype * nrec, status= ' old' , iostat= io_status)
64- if (io_status/= 0 ) then
65- call download_and_uncompress
66- open (newunit= fileunit, file= filename, access= ' direct' ,&
67- action= ' read' , recl= dtype * nrec, status= ' old' , iostat= io_status, iomsg= io_message)
68- if (io_status/= 0 ) error stop trim (io_message)
69- end if
30+ character (len= message_len) :: io_message
31+ integer :: io_status
32+ io_status = 0
33+ open (newunit= fileunit, file= filename, access= ' direct' , action= ' read' , &
34+ recl= dtype * dsize, status= ' old' , iostat= io_status, iomsg= io_message)
35+ if (io_status /= 0 ) error stop trim (io_message)
7036 allocate (array(dsize, nrec))
71- open (newunit= fileunit, file= filename, access= ' direct' ,&
72- action= ' read' , recl= dtype * dsize, status= ' old' )
7337 do i = 1 , nrec
7438 read (fileunit, rec= i) array(:,i)
7539 end do
0 commit comments