I am using a software that gives me as an output a .csv
file which I want to read with the aid of a fortran code. The .csv
file has the following form :
balance for 1. Unit: kg N/ha
___________________________________________________________________________________________________________________________________________________________________________
,N Pools,,,,,Influx N,,,,,Efflux N
Day,iniSON,iniSIN,endSON,endSIN,dSoilN,Deposit,Fertilizer,Manure,Litter,Sum-In...(**20 parameters**)
___________________________________________________________________________________________________________________________________________________________________________
1,5973.55, 20.20,5973.51, 20.23, -0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.00, 0.00
.........
I have 365 lines with such values.
In order to read the first lines I used the following :
program od
implicit none
integer :: res
character(LEN=200) :: head1,head2,head3,head4,head5
open(10, file="Balance_N_1.csv",access='sequential',form="formatted",iostat=res)
open(9,file="out.txt")
read(10,fmt='(A)', iostat=res) head1,head2,head3,head4,head5
write(9,*) head1,head2,head3,head4,head5
end program od
How is it possible to read the data that follow and to put them in a matrix so that I can perform calculations with some of the values?
In this chapter you will study file input and output functionalities provided by Fortran. You can read and write to one or more files. The OPEN, WRITE, READ and CLOSE statements allow you to achieve this. Before using a file you must open the file. The open command is used to open files for reading or writing. The simplest form of the command is −
Fortran - File Input Output. Fortran allows you to read data from, and write data into files. In the last chapter, you have seen how to read data from, and write data to the terminal.
When reading a CSV file, the data is stored internally in the class as allocatable character strings, which can be retrieved as real, integer, logical or character vectors as necessary. For example, to get the x, y, z, and t vectors from the previously-generated file:
A modern Fortran library for reading and writing CSV (comma-separated value) files. Everything is handled by an object-oriented csv_file class. Here is an example for writing a file: Which produces the following file: Real, integer, logical, or character data can be added as scalars, vectors, and matrices.
If I read your question right you have a file with 365 lines of data, each line has 1 integer (the day number) and 20 real numbers and the values on each line are comma-separated. You could declare a data array like this;
real, dimension(365,20) :: data_array
and an integer variable such as
integer :: line_no
then, once you've read, or skipped over, the lines of text at the top of your file, you can read the array like this:
do ix = 1,365
read(10,*) line_no, data_array(ix,:)
end do
By using *
for the format in your read statement you are using list-directed input which is quick and easy but somewhat limited. However, if your data file is clean and consistent this should be good enough.
If list-directed input doesn't work you'll have to use edit descriptors, something like this (untested)
read(10,'(i4,20f9.4)') line_no, data_array(ix,:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With