46 character(len=1024) :: header =
""
47 logical :: header_is_written = .false.
67 class(*),
target,
intent(in) :: data
68 real(kind=
rp),
intent(in),
optional :: t
79 if (.not.
allocated(data%x))
then
80 call neko_error(
"Vector is not allocated. Use &
81 &vector%init() to associate your array &
82 &with a vector_t object")
87 if (.not.
allocated(data%x))
then
88 call neko_error(
"Matrix is not allocated. Use &
89 &matrix%init() to associate your array &
90 &with a matrix_t object")
95 call neko_error(
"Invalid data. Expected vector_t or &
102 call neko_log%message(
"Writing to " // trim(this%fname))
103 if (
associated(vec))
then
105 else if (
associated(mat))
then
124 real(kind=
rp),
intent(in),
optional :: t
125 character(len=1024) :: fname
126 integer :: suffix_pos, file_unit, i, ierr
128 open(
file = trim(f%fname), position =
"append", iostat = ierr, &
130 if (ierr .ne. 0)
call neko_error(
"Error while opening " // trim(f%fname))
133 if (f%header .ne.
"" .and. .not. f%header_is_written)
then
134 write (file_unit,
'(A)') trim(f%header)
135 f%header_is_written = .true.
139 if (
present(t))
write (file_unit,
'(g0,",")', advance =
"no") t
141 write (file_unit,
'(*(g0,","))', advance =
"no") data%x(1:data%n-1)
142 write (file_unit,
'(g0)') data%x(data%n)
156 real(kind=
rp),
intent(in),
optional :: t
157 integer :: file_unit, i,j, ierr
159 open(
file = trim(f%fname), position =
"append", iostat = ierr, &
161 if (ierr .ne. 0)
call neko_error(
"Error while opening " // trim(f%fname))
164 if (f%header .ne.
"" .and. .not. f%header_is_written)
then
165 write (file_unit,
'(A)') trim(f%header)
166 f%header_is_written = .true.
170 if (
present(t))
write (file_unit,
'(g0,",")', advance =
"no") t
171 write (file_unit,
'(*(g0,","))', advance =
"no") &
172 data%x(i, 1:data%ncols-1)
173 write (file_unit,
'(g0)') data%x(i, data%ncols)
185 class(*),
target,
intent(inout) :: data
189 call this%check_exists()
197 if (.not.
allocated(data%x))
then
198 call neko_error(
"Vector is not allocated. Use &
199 &vector%init() to associate your array &
200 &with a vector_t object")
205 if (.not.
allocated(data%x))
then
206 call neko_error(
"Matrix is not allocated. Use &
207 &matrix%init() to associate your array &
208 &with a matrix_t object")
213 call neko_error(
"Invalid data type for csv_file (expected: vector_t, &
220 call neko_log%message(
"Reading csv file " // trim(this%fname))
221 if (
associated(vec))
then
223 else if (
associated(mat))
then
240 type(
vector_t),
intent(inout) :: vec
241 integer :: ierr, file_unit, n_lines
242 character(len=80) :: tmp
244 n_lines = f%count_lines()
246 open(
file = trim(f%fname), status =
'old', newunit = file_unit, &
248 if (ierr .ne. 0)
call neko_error(
"Error while opening " // trim(f%fname))
251 if (n_lines .gt. 1)
then
252 read (file_unit,
'(A)') tmp
256 read (file_unit,*) vec%x
257 close(unit = file_unit)
269 type(
matrix_t),
intent(inout) :: mat
270 integer :: ierr, file_unit, i, n_lines
271 character(len=80) :: tmp
273 n_lines = f%count_lines()
275 open(
file = trim(f%fname), status =
'old', newunit = file_unit, &
277 if (ierr .ne. 0)
call neko_error(
"Error while opening " // trim(f%fname))
281 if (n_lines .lt. mat%nrows)
then
282 read (file_unit,
'(A)') tmp
287 read (file_unit,*) mat%x(i,:)
289 close(unit = file_unit)
299 character(len=*),
intent(in) :: hd
301 this%header = trim(hd)
311 integer :: ierr, file_unit
313 open(
file = trim(this%fname), status =
'old', newunit = file_unit)
320 read (file_unit, *, iostat = ierr)
321 if (ierr .ne. 0)
exit
325 close(unit = file_unit)
File format for .csv files, used for any read/write operations involving floating point data.
subroutine csv_file_set_header(this, hd)
Sets the header for a csv file. For example: hd = "u,v,w,p".
subroutine csv_file_write_matrix(f, data, t)
Writes a matrix_t object to an output file. If the parameter t is present, it will be appended at the...
subroutine csv_file_write_vector(f, data, t)
Writes a vector_t object to an output file, in a row format. If the parameter t is present,...
subroutine csv_file_write(this, data, t)
Writes data to an output file.
subroutine csv_file_read(this, data)
Reads data from an input file.
subroutine csv_file_read_matrix(f, mat)
Read a matrix from a csv file.
integer function csv_file_count_lines(this)
Count the number of lines in a file by going through it entirely until the end is reached.
subroutine csv_file_read_vector(f, vec)
Read a vector (i.e. data on a single row) from a csv file.
Module for file I/O operations.
type(log_t), public neko_log
Global log stream.
integer, parameter, public log_size
integer, parameter, public rp
Global precision used in computations.