46 character(len=:),
allocatable :: header
47 logical :: header_is_written = .false.
69 class(*),
target,
intent(in) :: data
70 real(kind=
dp),
intent(in),
optional :: t
80 if (.not.
allocated(data%x))
then
81 call neko_error(
"Vector is not allocated. Use &
82 &vector%init() to associate your array &
83 &with a vector_t object")
88 if (.not.
allocated(data%x))
then
89 call neko_error(
"Matrix is not allocated. Use &
90 &matrix%init() to associate your array &
91 &with a matrix_t object")
96 call neko_error(
"Invalid data. Expected vector_t or &
103 call neko_log%message(
"Writing to " // trim(this%get_fname()))
104 if (
associated(vec))
then
106 else if (
associated(mat))
then
125 real(kind=
dp),
intent(in),
optional :: t
126 integer :: file_unit, ierr, n
129 if (f%overwrite .and. .not. f%header_is_written)
then
130 open(newunit = file_unit,
file = trim(f%get_fname()), status =
"old", &
132 if (ierr == 0)
close(file_unit, status =
"delete")
135 open(
file = trim(f%get_fname()), position =
"append", iostat = ierr, &
137 if (ierr .ne. 0)
then
138 call neko_error(
"Error while opening " // trim(f%get_fname()))
142 if (
allocated(f%header))
then
143 if (f%header .ne.
"" .and. .not. f%header_is_written)
then
144 write (file_unit,
'(A)') trim(f%header)
145 f%header_is_written = .true.
150 if (
present(t))
write (file_unit,
'(g0,",")', advance =
"no") t
153 write (file_unit,
'(*(g0,","))', advance =
"no") data%x(1:n-1)
154 write (file_unit,
'(g0)') data%x(n)
168 real(kind=
dp),
intent(in),
optional :: t
169 integer :: file_unit, i, ierr, nc
172 if (f%overwrite .and. .not. f%header_is_written)
then
173 open(newunit = file_unit,
file = trim(f%get_fname()), status =
"old", &
175 if (ierr == 0)
close(file_unit, status =
"delete")
178 open(
file = trim(f%get_fname()), position =
"append", iostat = ierr, &
180 if (ierr .ne. 0)
then
181 call neko_error(
"Error while opening " // trim(f%get_fname()))
185 if (
allocated(f%header))
then
186 if (f%header .ne.
"" .and. .not. f%header_is_written)
then
187 write (file_unit,
'(A)') trim(f%header)
188 f%header_is_written = .true.
192 do i = 1, data%get_nrows()
193 if (
present(t))
write (file_unit,
'(g0,",")', advance =
"no") t
194 nc = data%get_ncols()
195 write (file_unit,
'(*(g0,","))', advance =
"no") &
197 write (file_unit,
'(g0)') data%x(i, nc)
209 class(*),
target,
intent(inout) :: data
213 call this%check_exists()
221 if (.not.
allocated(data%x))
then
222 call neko_error(
"Vector is not allocated. Use &
223 &vector%init() to associate your array &
224 &with a vector_t object")
229 if (.not.
allocated(data%x))
then
230 call neko_error(
"Matrix is not allocated. Use &
231 &matrix%init() to associate your array &
232 &with a matrix_t object")
237 call neko_error(
"Invalid data type for csv_file (expected: vector_t, &
244 call neko_log%message(
"Reading csv file " // trim(this%get_fname()))
245 if (
associated(vec))
then
247 else if (
associated(mat))
then
264 type(
vector_t),
intent(inout) :: vec
265 integer :: ierr, file_unit, n_lines
266 character(len=80) :: tmp
268 n_lines = f%count_lines()
270 open(
file = trim(f%get_fname()), status =
'old', newunit = file_unit, &
272 if (ierr .ne. 0)
then
273 call neko_error(
"Error while opening " // trim(f%get_fname()))
277 if (n_lines .gt. 1)
then
278 read (file_unit,
'(A)') tmp
282 read (file_unit,*) vec%x
283 close(unit = file_unit)
295 type(
matrix_t),
intent(inout) :: mat
296 integer :: ierr, file_unit, i, n_lines
297 character(len=80) :: tmp
299 n_lines = f%count_lines()
301 open(
file = trim(f%get_fname()), status =
'old', newunit = file_unit, &
303 if (ierr .ne. 0)
then
304 call neko_error(
"Error while opening " // trim(f%get_fname()))
309 if (n_lines .gt. mat%get_nrows())
then
310 read (file_unit,
'(A)') tmp
314 do i = 1, mat%get_nrows()
315 read (file_unit,*) mat%x(i,:)
317 close(unit = file_unit)
327 character(len=*),
intent(in) :: hd
329 this%header = trim(hd)
339 integer :: ierr, file_unit
341 call this%check_exists()
343 open(
file = trim(this%get_fname()), status =
'old', newunit = file_unit, &
345 if (ierr .ne. 0)
then
346 call neko_error(
"Error while opening " // trim(this%get_fname()))
354 read (file_unit, *, iostat = ierr)
355 if (ierr .ne. 0)
exit
359 close(unit = file_unit)
368 logical,
intent(in) :: overwrite
370 this%overwrite = overwrite
integer, public pe_rank
MPI rank.
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_set_overwrite(this, overwrite)
Sets the overwrite flag for a csv 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 dp