Reading data from file into a two-dimensional array - arrays

I'm totally new to fortran and also programming (minor experience). I have a difficulty in writing a code for the following.
I need to store data in a two-dimension array using the data from two input files
Then I need to determine the size of the two files
Then need to select the variables from both dimensions for a specific value of one variable
and then calculate the average
Finally write the results to a new data file.
Eg:
There is two radiosonde data files with pressure, height, temperature, dew point values with different data sizes
Need to get the mean values for height, temperature and dew point, at specific pressures of 1000, 850 and 500 (both files have these records) and write to a new file.
I have gone through a couple of books but couldn’t grasp the exact thing I’m looking for. Can anyone of you give me some insight to the coding? I’m really thankful to you guys.

Your code will look something like this:
program main
implicit none
real::height(100)
integer::i
open(unit=16, file='height.dat')
do i = 1, 100
read(16, *) height(i)
enddo
close(16)
open(unit=16, file='mean_height.dat')
write(16, *) sum(height)/100
close(16)
end program
This assumes there is a file named height.dat with one height value per line, 100 lines total.
It reads the 100 heights in, then writes out the mean height to a file mean_height.dat.
You will obviously have to signficantly modify this.
For example, you won't know there are 100 lines in the file, so you will need to use an allocatable array for the values you read in:
program main
implicit none
real,allocatable::height(:)
integer::i, N
!Code that figures out how many lines there are
!...
allocate(height(N))
open(unit=16, file='height.dat')
do i = 1, N
This should get you started... Ideally you could start with a simpler input file than what you describe to learn the basics.

Related

Get datas from TXT to a matrix

I have a homework to make a file like this, License plate, date, time, speed
like: AAA-111, 2019.01.01, 12:12, 50
I have to read these into the program and check if how many cars went there at the same date, and how much % went over a speed which given by the user, then to make a file "speeding" which contain the punishment, like 50-60 100 dollar, and to check if how many cars got punished, and to write it to a file by license plate, date, and the amount to pay.
I'm new to C, and I dont know what can I do when there is strings and integers in a file.
(Actually I'm a vehicle engineer, but it's just a stuffing course which we have to do, the teached teached nothing, we have to give in this homework to get a rating)
I have tried a method which stores the file char by char ( I think) but because then I have to see it by rows and columns, a matrix would be better. But if it is possible without it, that is good too.
first you should use strucs to store each fields of a line from your file as char * attribues.
then when you have your structs, you can go on with a function that takes a line and returns a pointer to your struct:
struct ticket_t *get_ticket(char *line);
Then you should figure how to read a file line by line with getline(3) you should have a bit of code here for this.
I suggest you find the number of line the file has and then create an array of struct ticket_t* of the correct length to ease the code (memory management).
As for the other parts of you assigment you should be ok with what I told you here.
Some documentation: fopen(3), getline(3), fwrite(3), malloc(3), strcmp(3)

Read array from .txt data

I would like to read an array from a .txt file in Fortran. I tried it with the following code:
PROGRAM test_performance
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:), ALLOCATABLE :: ARRAYONE
OPEN(UNIT=20,FILE='testfile.txt',STATUS='OLD', ACTION='READ')
READ (20,*) ARRAYONE
print *, 'here I am!'
CLOSE(UNIT=20)
write(*,*)ARRAYONE
OPEN(UNIT=30,FILE='TEXTFILE.txt', STATUS='REPLACE', ACTION='WRITE')
WRITE(30,*)ARRAYONE
END PROGRAM test_performance
There is no error when I compile it. The point is that the array ARRAYONE as well as the file TEXTFILE.txt are empty.
The file testfile.txt Looks like this:
1 2 3 4 5 6 7 8 9 10 11
I have got a book in front of me according to which this should actually work. What puzzles me is the fact that if I put just one number in the input file, i.e., 1, and I want to write it on an integer, there is no problem and everything works fine :o!
In the vainglorious pursuit of rep, and with the noble intention that this question get an answer
You can't automatically allocate an array in a read statement, the rules forbid it (or, if you prefer, do not make allowance for it). You could either:
allocate the array to some size that you expect to be large enough, populate it with a guard value unlikely to be found in your inputs, and then read the data you have into the first n elements; or
read the data, figure out how many elements there are, allocate the array to that size, pass the data to the array; you could do this in two passes over the input file (one to determine the contents, the second to read it) or you could get clever and read the data into, say, a large string and parse it to find out how many numbers it contains (so simple it's scarcely worthy of the name 'parsing').
There are other ways, I'm sure you can think of some.

How to save a 2-Dimensional array to a file in C?

I am an beginner C programmer and I am currently working on a project to implement viola jones object detection algorithm using C. I would like to know how I would be able to store data in a 2-Dimensional array to a file that can be easily ported and accessed by different program files(e.g. main.c, header_file.h etc.)
Thank you in advance.
There's not quite enough detail to be sure what you're looking for, but the basic structure of what you want to do is going to look something like this:
open file.csv for writing
for(iterate through one dimension of the array using i)
{
for(iterate through the other dimension of the array using j)
{
fprintf(yourfilehandle,"%d,",yourvalue[i][j]);
}
fprintf(yourfilehandle,"\n");
}
close your file
As has been suggested by others, this will leave you with a .CSV file, which is a pretty good choice, as it's easy to read in and parse, and you can open your file in Notepad or Excel and view it no problems.
This is assuming you really meant to do this with C file I/O, which is a perfectly valid way of doing things, some just feel it's a bit dated.
Note this leaves an extraneous comma at the end of the line. If that bugs you it's easy enough to do the pre and post conditions to only get commas where you want. Hint: it involves printing the comma before the entry inside the second for loop, reducing the number of entries you iterate over for the interior for loop, and printing out the first and last case of each row special, immediately before and after the inner for loop, respectively. Harder to explain that to do, probably.
Here is a reference for C-style file I/O, and here is a tutorial.
Without knowing anything about what type of data you're storing, I would say to store this as a matrix. You'll need to choose a delimiter to separate your elements (tab or space are common choices, aka 'tsv' and 'csv', respectively) and then something to mark the end of a row (new line is a good choice here).
So your saved file might look something like:
10 162 1 5
7 1 4 12
9 2 2 0
You can also define your format as having some metadata in the first line -- the number of rows and columns may be useful if you want to pre-allocate memory, along with other information like character encoding. Start simple and add as necessary!

Reading specific line in txt file with C

I am working with Mac OSX, programming in C and using bash in terminal.
I am currently trying to make a lookup table for the gamma function. Calling gsl_sf_gamma I have been told is pretty expensive and a lookup table would be far faster. I did not wish to lose too much accuracy so I wanted to have a fairly large lookup table. Initializing a huge array would not be ideal since it then defeats the purpose.
My thoughts where to make a large text file with the values pre evaluated for the gamma function in the range of interest. A major problem with this is that I don't know how to call a specific line within a text file using C.
Thanks for any insight and help you guys can offer.
Warning: I know very little about strings and txt files, so I might just not know a simply function that does this already.
Gamma is basically factorial except in continuous form. You want to perform a lookup rather than a computation for the gamma function. You want to use a text file to represent these results. Each line of the file represents the input value multiplied by 1000. I guess for a high enough input value, the file scan could outperform doing the compute.
However, I think you will at minimum want to compute an index into your file. The file can still be arranged as a text file, but you have another step that scans the file, and notes the byte offset for each result line. These offsets get recorded into a binary file, which will serve as your index.
When you run your program, in the beginning, you load the index file into an array, which the index of the array is the floor of the gamma input multiplied by 1000, and the array value at that index is the offset that is recorded in the index file. When you want to compute gamma for a particular number, you multiply the input by 1000, and truncate the result to obtain your array index. You consult this array for the offset, and the next array value for to compute the length of the input. Then, your gamma text file is opened as a binary file. You seek to the offset, and read the length number of bytes to get your digits. You will need to read the next entry too to perform your interpolation.
Yes, calculating gamma is slow (I think GSL uses the Lancosz formula, which sums a series). If the number of values for which you need to calculate it is limited (say, you're only doing integers), then certainly a lookup table might help. But if the table is too big for memory, it won't help--it will be even slower than the calculation.
If the table will fit into memory, there's nothing wrong with storing it in a file until you need it and then loading the whole thing into memory at once.

Standard format for writing Compressed Row Storage into text files?

I have a large sparse matrix stored in Compressed Row Storage (CRS) format. This is basically three arrays: an array containing the Values, an array for Column Index, and a final array containing the Row Pointers. E.g. http://web.eecs.utk.edu/~dongarra/etemplates/node373.html
I want to write this information into a text (.txt) file, which is intended to be read and put into three arrays using C. I currently plan to do this by writing all the entries in the Value array in one long line separated by commas. E.g. 5.6,10,456,78.2,... etc. Then do the same for the other two arrays.
My C code will end read the first line, put all the values into an array labeled "Value". And so on.
Question
Is this "correct"? Or is there a standard way of putting CRS data into text files?
No standard format that I'm aware of. You decide on a format that makes your life easy.
First, consider that if you want to look at one of these text files, you'll be instantly put off by the long lines. Some text editors might simply hate you. There's nothing wrong with splitting lines up.
Second, consider writing out the number of elements in each array (well, I suppose there's only two different array lengths for the three arrays) at the beginning of the file. This will let you preallocate your arrays. If you have all array lengths at hand, you have the option of doing a single memory allocation.
Finally, consider writing out some sensible tag names. Some kind of header that can identify your file is the correct format, then something to denote the start of each array. It's kind of a sanity thing for your code to detect problems with the file. It might just be one character, but it's something.
Now... call me a grungy old programmer, but I'd probably just write whole lot in binary. Especially if it's floating point data, I wouldn't want to deal with the loss of precision you get when you write out numbers as text (or the space they can consume when you write them with full precision). Binary files are easy to write and quick to run. You just have to be careful if you're going to be using them across platforms with different endian order.
That's my 2 cents worth.. Hope it's useful to you.
If you want to stick to some widely-used standards, have a look at the Matrix Market. This is a repository with many matrices arising in a variety of engineering and science problems. You can find software libraries to save and read the matrices as well.

Resources